6 #include <libusb-1.0/libusb.h>
8 #define USB_TIMEOUT 10000
10 #define ID_VENDOR 0x18ef
11 #define ID_PRODUCT 0xe015
13 /* Not in all libusb-1.0 versions, so we have to roll our own :-( */
14 static char * usb_strerror(int e
)
16 static char unknerr
[256];
22 return "Input/output error";
23 case LIBUSB_ERROR_INVALID_PARAM
:
24 return "Invalid parameter";
25 case LIBUSB_ERROR_ACCESS
:
26 return "Access denied (insufficient permissions)";
27 case LIBUSB_ERROR_NO_DEVICE
:
28 return "No such device (it may have been disconnected)";
29 case LIBUSB_ERROR_NOT_FOUND
:
30 return "Entity not found";
31 case LIBUSB_ERROR_BUSY
:
32 return "Resource busy";
33 case LIBUSB_ERROR_TIMEOUT
:
34 return "Operation timed out";
35 case LIBUSB_ERROR_OVERFLOW
:
37 case LIBUSB_ERROR_PIPE
:
39 case LIBUSB_ERROR_INTERRUPTED
:
40 return "System call interrupted (perhaps due to signal)";
41 case LIBUSB_ERROR_NO_MEM
:
42 return "Insufficient memory";
43 case LIBUSB_ERROR_NOT_SUPPORTED
:
44 return "Operation not supported or unimplemented on this platform";
45 case LIBUSB_ERROR_OTHER
:
48 snprintf(unknerr
, sizeof(unknerr
), "Unknown error code %d / 0x%02x", e
, e
);
52 libusb_device_handle
*fs20pcs_find() {
53 libusb_device_handle
*devh
= NULL
;
59 cnt
= libusb_get_device_list(NULL
, &list
);
61 fprintf(stderr
, "Can't get USB device list: %d\n", (int)cnt
);
65 for (i
= 0; i
< cnt
; i
++){
66 struct libusb_device_descriptor desc
;
68 err
= libusb_get_device_descriptor(list
[i
], &desc
);
72 if ((desc
.idVendor
== ID_VENDOR
) && (desc
.idProduct
== ID_PRODUCT
)) {
73 libusb_device
*dev
= list
[i
];
75 err
= libusb_open(dev
, &devh
);
77 fprintf(stderr
, "Can't open device: %s\n", usb_strerror(err
));
81 err
= libusb_detach_kernel_driver(devh
, 0);
82 if ((err
!= 0) && (err
!= LIBUSB_ERROR_NOT_FOUND
)) {
83 fprintf(stderr
, "Can't detach kernel driver: %s\n", usb_strerror(err
));
87 err
= libusb_claim_interface(devh
, 0);
89 fprintf(stderr
, "Can't claim interface: %s\n", usb_strerror(err
));
101 int fs20pcs_send(libusb_device_handle
*devh
, unsigned char* send_data
)
103 unsigned char recv_data
[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
108 err
= libusb_interrupt_transfer(devh
, 0x01, send_data
, 11, &cnt
, 5000);
110 fprintf(stderr
, "Can't send data: %s\n", usb_strerror(err
));
114 err
= libusb_interrupt_transfer(devh
, 0x81, recv_data
, sizeof(recv_data
), &cnt
, 5000);
116 fprintf(stderr
, "Can't receive data: %s\n", usb_strerror(err
));
120 for (i
= 0; i
< cnt
; i
++) {
121 printf("0x%02x ", recv_data
[i
]);
128 int main(int argc
, char **argv
)
130 unsigned char send_data
[11];
131 libusb_device_handle
*devh
= NULL
;
135 if ((argc
< 2) || (argc
> 12)) {
136 fprintf(stderr
, "Invalid number of parameters!\n");
140 memset(send_data
, 0, sizeof(send_data
));
141 for (i
= 0; i
< argc
- 1; i
++) {
142 send_data
[i
] = strtoul(argv
[i
+1], NULL
, 16);
145 err
= libusb_init(NULL
);
147 fprintf(stderr
, "Can't initialize libusb: %s\n", usb_strerror(err
));
151 devh
= fs20pcs_find();
153 fprintf(stderr
, "Can't find/open fs20pcs!\n");
157 if (fs20pcs_send(devh
, send_data
) == 0) {
158 fprintf(stderr
, "Can't communicate with fs20pcs!\n");