1 | /*
|
---|
2 | * libusb example program to list devices on the bus
|
---|
3 | * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
|
---|
4 | *
|
---|
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.
|
---|
9 | *
|
---|
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.
|
---|
14 | *
|
---|
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
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include <stdio.h>
|
---|
21 | #include <stdlib.h>
|
---|
22 | #include <sys/types.h>
|
---|
23 | #include <string.h>
|
---|
24 |
|
---|
25 | #ifdef __FreeBSD__
|
---|
26 | #include <libusb.h>
|
---|
27 | #else
|
---|
28 | #include <libusb-1.0/libusb.h>
|
---|
29 | #endif
|
---|
30 | #define FALSE 0
|
---|
31 | #define TRUE 1
|
---|
32 |
|
---|
33 | int out_endpoint;
|
---|
34 |
|
---|
35 | static int smartreader_check_endpoint(libusb_device *usb_dev)
|
---|
36 | {
|
---|
37 | struct libusb_device_descriptor desc;
|
---|
38 | struct libusb_config_descriptor *configDesc;
|
---|
39 | int ret;
|
---|
40 | int j,k,l;
|
---|
41 | u_int8_t tmpEndpointAddress;
|
---|
42 | int nb_endpoint_ok;
|
---|
43 |
|
---|
44 | nb_endpoint_ok=0;
|
---|
45 |
|
---|
46 | ret = libusb_get_device_descriptor(usb_dev, &desc);
|
---|
47 | if (ret < 0) {
|
---|
48 | printf("Smartreader : couldn't read device descriptor, assuming this is not a smartreader");
|
---|
49 | return FALSE;
|
---|
50 | }
|
---|
51 | if (desc.bNumConfigurations) {
|
---|
52 | ret=libusb_get_active_config_descriptor(usb_dev,&configDesc);
|
---|
53 | if(ret) {
|
---|
54 | printf("Smartreader : couldn't read config descriptor , assuming this is not a smartreader");
|
---|
55 | return FALSE;
|
---|
56 | }
|
---|
57 |
|
---|
58 | for(j=0; j<configDesc->bNumInterfaces; j++)
|
---|
59 | for(k=0; k<configDesc->interface[j].num_altsetting; k++)
|
---|
60 | for(l=0; l<configDesc->interface[j].altsetting[k].bNumEndpoints; l++) {
|
---|
61 | tmpEndpointAddress=configDesc->interface[j].altsetting[k].endpoint[l].bEndpointAddress;
|
---|
62 | if((tmpEndpointAddress== 0x1) || (tmpEndpointAddress== 0x81) || (tmpEndpointAddress== 0x82))
|
---|
63 | {
|
---|
64 | if(tmpEndpointAddress == 0x1 || tmpEndpointAddress==out_endpoint)
|
---|
65 | {
|
---|
66 | nb_endpoint_ok++;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | if(nb_endpoint_ok!=2)
|
---|
73 | return FALSE;
|
---|
74 | return TRUE;
|
---|
75 | }
|
---|
76 |
|
---|
77 | static void print_devs(libusb_device **devs)
|
---|
78 | {
|
---|
79 | libusb_device *dev;
|
---|
80 | libusb_device_handle *handle;
|
---|
81 | int i = 0;
|
---|
82 | int ret;
|
---|
83 | int busid, devid;
|
---|
84 | unsigned char iserialbuffer[128];
|
---|
85 |
|
---|
86 | while ((dev = devs[i++]) != NULL) {
|
---|
87 | struct libusb_device_descriptor desc;
|
---|
88 | int r = libusb_get_device_descriptor(dev, &desc);
|
---|
89 | if (r < 0) {
|
---|
90 | fprintf(stderr, "failed to get device descriptor");
|
---|
91 | return;
|
---|
92 | }
|
---|
93 | if (desc.idVendor==0x0403 && desc.idProduct==0x6001) {
|
---|
94 |
|
---|
95 | ret=libusb_open(dev,&handle);
|
---|
96 | if (ret) {
|
---|
97 | printf ("coulnd't open device %03d:%03d\n", libusb_get_bus_number(dev), libusb_get_device_address(dev));
|
---|
98 | continue;
|
---|
99 | }
|
---|
100 | // check for smargo endpoints.
|
---|
101 | if(smartreader_check_endpoint(dev)) {
|
---|
102 | busid=libusb_get_bus_number(dev);
|
---|
103 | devid=libusb_get_device_address(dev);
|
---|
104 | libusb_get_string_descriptor_ascii(handle,desc.iSerialNumber,iserialbuffer,sizeof(iserialbuffer));
|
---|
105 | printf("bus %03d, device %03d : %04x:%04x Smartreader (Device=%03d:%03d EndPoint=0x%2X insert in oscam.server 'Device = Serial:%s')\n",
|
---|
106 | busid, devid,
|
---|
107 | desc.idVendor, desc.idProduct,
|
---|
108 | busid, devid, out_endpoint, iserialbuffer);
|
---|
109 | }
|
---|
110 |
|
---|
111 | libusb_close(handle);
|
---|
112 | }
|
---|
113 |
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | int main(int argc, char **argv)
|
---|
118 | {
|
---|
119 | libusb_device **devs;
|
---|
120 | int r;
|
---|
121 | ssize_t cnt;
|
---|
122 |
|
---|
123 | r = libusb_init(NULL);
|
---|
124 | if (r < 0)
|
---|
125 | return r;
|
---|
126 |
|
---|
127 | out_endpoint=0x82;
|
---|
128 | if(argc==2) {
|
---|
129 | sscanf(argv[1],"%x",&out_endpoint);
|
---|
130 | }
|
---|
131 | else
|
---|
132 | out_endpoint=0x82;
|
---|
133 |
|
---|
134 | printf("Looking for smartreader with an out endpoint = 0x%02x :\n",out_endpoint);
|
---|
135 |
|
---|
136 | cnt = libusb_get_device_list(NULL, &devs);
|
---|
137 | if (cnt < 0)
|
---|
138 | return (int) cnt;
|
---|
139 |
|
---|
140 |
|
---|
141 | print_devs(devs);
|
---|
142 | libusb_free_device_list(devs, 1);
|
---|
143 |
|
---|
144 | libusb_exit(NULL);
|
---|
145 | return 0;
|
---|
146 | }
|
---|
147 |
|
---|