- fix Building without Nagra not possible at Nagra_Merlin https://trac.streamboard...
[oscam.git] / utils / list_smargo.c
blob4bb8e91b322aa7d5de16868d11e79d05ba009974
1 /*
2 * libusb example program to list devices on the bus
3 * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <string.h>
24 #include <strings.h>
25 #include "../globals.h"
26 #include "../csctapi/ifd_smartreader_types.h"
28 #if defined(__FreeBSD__)
29 #include <libusb.h>
30 #else
31 #include <libusb-1.0/libusb.h>
32 #endif
34 static void smartreader_check_endpoint(libusb_device *usb_dev, libusb_device_handle *handle)
36 struct libusb_device_descriptor usbdesc;
37 struct libusb_config_descriptor *configDesc;
38 int32_t ret;
39 int32_t j, k, l;
40 uint32_t m;
41 uint8_t tmpEndpointAddress;
42 int32_t nb_endpoint_ok = 0;
43 int32_t busid, devid;
44 unsigned char iserialbuffer[128], iproductbuffer[128];
45 char *productptr = (char *)iproductbuffer;
46 static const char *const typename_str[6] = {"SR", "Infinity", "SRv2", "TripleP1", "TripleP2", "TripleP3"};
48 ret = libusb_get_device_descriptor(usb_dev, &usbdesc);
49 if(ret < 0)
51 printf("Smartreader : couldn't read device descriptor, assuming this is not a smartreader");
52 return;
54 if(usbdesc.bNumConfigurations)
56 ret = libusb_get_active_config_descriptor(usb_dev, &configDesc);
57 if(ret)
59 printf("Smartreader : couldn't read config descriptor , assuming this is not a smartreader");
60 return;
62 for(m = 0; m < sizeof(reader_types) / sizeof(struct s_reader_types); ++m)
64 nb_endpoint_ok = 0;
65 for(j = 0; j < (configDesc->bNumInterfaces); j++)
67 for(k = 0; k < configDesc->interface[j].num_altsetting; k++)
69 for(l = 0; l < configDesc->interface[j].altsetting[k].bNumEndpoints; l++)
71 tmpEndpointAddress = configDesc->interface[j].altsetting[k].endpoint[l].bEndpointAddress;
72 if((tmpEndpointAddress == reader_types[m].in_ep || tmpEndpointAddress == reader_types[m].out_ep))
74 nb_endpoint_ok++;
80 if(nb_endpoint_ok == 2)
82 busid = libusb_get_bus_number(usb_dev);
83 devid = libusb_get_device_address(usb_dev);
84 memset(iserialbuffer, 0, sizeof(iserialbuffer));
85 memset(iproductbuffer, 0, sizeof(iproductbuffer));
86 libusb_get_string_descriptor_ascii(handle, usbdesc.iSerialNumber, iserialbuffer, sizeof(iserialbuffer));
87 libusb_get_string_descriptor_ascii(handle, usbdesc.iProduct, iproductbuffer, sizeof(iproductbuffer));
88 if ((!((!strcasecmp(productptr, "Triple Reader+")) && (m == 2))) && (!((!strcasecmp(productptr, "Smartreader2 plus")) && (m == 3)))) {
89 printf("bus %03d, device %03d : %04x:%04x %s (type=%s, in_ep=%02x, out_ep=%02x; insert in oscam.server 'device = %s%sSerial:%s')\n",
90 busid, devid,
91 usbdesc.idVendor, usbdesc.idProduct, strlen(productptr) > 0 ? productptr : "Smartreader",
92 typename_str[reader_types[m].rdrtypename], reader_types[m].in_ep, reader_types[m].out_ep,
93 reader_types[m].rdrtypename == 0 ? "" : typename_str[reader_types[m].rdrtypename] , reader_types[m].rdrtypename == 0 ? "" : ";", iserialbuffer
94 );}
100 static void print_devs(libusb_device **devs)
102 libusb_device *dev;
103 libusb_device_handle *handle;
104 int32_t i = 0;
105 int32_t ret;
107 while((dev = devs[i++]) != NULL)
109 struct libusb_device_descriptor usbdesc;
110 int32_t r = libusb_get_device_descriptor(dev, &usbdesc);
111 if(r < 0)
113 fprintf(stderr, "failed to get device descriptor");
114 return;
116 if(usbdesc.idVendor == 0x0403 && (usbdesc.idProduct == 0x6001 || usbdesc.idProduct == 0x6011))
118 ret = libusb_open(dev, &handle);
119 if(ret)
121 printf("couldn't open device %03d:%03d\n", libusb_get_bus_number(dev), libusb_get_device_address(dev));
122 continue;
124 // check for smargo endpoints.
125 smartreader_check_endpoint(dev, handle);
127 libusb_close(handle);
132 int32_t main(void)
134 libusb_device **devs;
135 int32_t r;
136 ssize_t cnt;
138 r = libusb_init(NULL);
139 if(r < 0)
140 { return r; }
142 printf("Looking for smartreader compatible devices...\n");
144 cnt = libusb_get_device_list(NULL, &devs);
145 if(cnt < 0)
146 { return (int32_t) cnt; }
148 print_devs(devs);
149 libusb_free_device_list(devs, 1);
151 libusb_exit(NULL);
153 return 0;