source: trunk/utils/list_smargo.c@ 4008

Last change on this file since 4008 was 4008, checked in by dingo35, 10 years ago

list_smargo: small clarification in print statement

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