]> git.zerfleddert.de Git - fs20pcs/blob - fs20pcs.c
8bc28c7fc2d35e4b8684d991ea29907a7fbac651
[fs20pcs] / fs20pcs.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <libusb-1.0/libusb.h>
7
8 #define USB_TIMEOUT 10000
9
10 #define ID_VENDOR 0x18ef
11 #define ID_PRODUCT 0xe015
12
13 /* Not in all libusb-1.0 versions, so we have to roll our own :-( */
14 static char * usb_strerror(int e)
15 {
16 static char unknerr[256];
17
18 switch (e) {
19 case LIBUSB_SUCCESS:
20 return "Success";
21 case LIBUSB_ERROR_IO:
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:
36 return "Overflow";
37 case LIBUSB_ERROR_PIPE:
38 return "Pipe error";
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:
46 return "Other error";
47 };
48 snprintf(unknerr, sizeof(unknerr), "Unknown error code %d / 0x%02x", e, e);
49 return unknerr;
50 }
51
52 libusb_device_handle *fs20pcs_find() {
53 libusb_device_handle *devh = NULL;
54 libusb_device **list;
55 ssize_t cnt;
56 ssize_t i;
57 int err;
58
59 cnt = libusb_get_device_list(NULL, &list);
60 if (cnt < 0) {
61 fprintf(stderr, "Can't get USB device list: %d\n", (int)cnt);
62 return NULL;
63 }
64
65 for (i = 0; i < cnt; i++){
66 struct libusb_device_descriptor desc;
67
68 err = libusb_get_device_descriptor(list[i], &desc);
69 if (err)
70 continue;
71
72 if ((desc.idVendor == ID_VENDOR) && (desc.idProduct == ID_PRODUCT)) {
73 libusb_device *dev = list[i];
74
75 err = libusb_open(dev, &devh);
76 if (err) {
77 fprintf(stderr, "Can't open device: %s\n", usb_strerror(err));
78 return NULL;
79 }
80
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));
84 return NULL;
85 }
86
87 err = libusb_claim_interface(devh, 0);
88 if ((err != 0)) {
89 fprintf(stderr, "Can't claim interface: %s\n", usb_strerror(err));
90 return NULL;
91 }
92
93 return devh;
94 }
95
96 }
97
98 return NULL;
99 }
100
101 int fs20pcs_send(libusb_device_handle *devh, unsigned char* send_data)
102 {
103 unsigned char recv_data[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
104 int err;
105 int cnt;
106 int i;
107
108 err = libusb_interrupt_transfer(devh, 0x01, send_data, 11, &cnt, 5000);
109 if (err) {
110 fprintf(stderr, "Can't send data: %s\n", usb_strerror(err));
111 return 0;
112 }
113
114 err = libusb_interrupt_transfer(devh, 0x81, recv_data, sizeof(recv_data), &cnt, 5000);
115 if (err) {
116 fprintf(stderr, "Can't receive data: %s\n", usb_strerror(err));
117 return 0;
118 }
119
120 for (i = 0; i < cnt; i++) {
121 printf("0x%02x ", recv_data[i]);
122 }
123 printf("\n");
124
125 return 1;
126 }
127
128 int main(int argc, char **argv)
129 {
130 unsigned char send_data[11];
131 libusb_device_handle *devh = NULL;
132 int err;
133 int i;
134
135 if ((argc < 2) || (argc > 12)) {
136 fprintf(stderr, "Invalid number of parameters!\n");
137 return EXIT_FAILURE;
138 }
139
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);
143 }
144
145 err = libusb_init(NULL);
146 if (err != 0) {
147 fprintf(stderr, "Can't initialize libusb: %s\n", usb_strerror(err));
148 return EXIT_FAILURE;
149 }
150
151 devh = fs20pcs_find();
152 if (!devh) {
153 fprintf(stderr, "Can't find/open fs20pcs!\n");
154 return EXIT_FAILURE;
155 }
156
157 if (fs20pcs_send(devh, send_data) == 0) {
158 fprintf(stderr, "Can't communicate with fs20pcs!\n");
159 return EXIT_FAILURE;
160 }
161
162 libusb_close(NULL);
163
164 return EXIT_SUCCESS;
165 }
Impressum, Datenschutz