6 #include <libusb-1.0/libusb.h>
8 #define USB_TIMEOUT 10000
10 #define ID_VENDOR 0x18ef
11 #define ID_PRODUCT 0xe015
15 /* Not in all libusb-1.0 versions, so we have to roll our own :-( */
16 static char * usb_strerror(int e
)
18 static char unknerr
[256];
24 return "Input/output error";
25 case LIBUSB_ERROR_INVALID_PARAM
:
26 return "Invalid parameter";
27 case LIBUSB_ERROR_ACCESS
:
28 return "Access denied (insufficient permissions)";
29 case LIBUSB_ERROR_NO_DEVICE
:
30 return "No such device (it may have been disconnected)";
31 case LIBUSB_ERROR_NOT_FOUND
:
32 return "Entity not found";
33 case LIBUSB_ERROR_BUSY
:
34 return "Resource busy";
35 case LIBUSB_ERROR_TIMEOUT
:
36 return "Operation timed out";
37 case LIBUSB_ERROR_OVERFLOW
:
39 case LIBUSB_ERROR_PIPE
:
41 case LIBUSB_ERROR_INTERRUPTED
:
42 return "System call interrupted (perhaps due to signal)";
43 case LIBUSB_ERROR_NO_MEM
:
44 return "Insufficient memory";
45 case LIBUSB_ERROR_NOT_SUPPORTED
:
46 return "Operation not supported or unimplemented on this platform";
47 case LIBUSB_ERROR_OTHER
:
50 snprintf(unknerr
, sizeof(unknerr
), "Unknown error code %d / 0x%02x", e
, e
);
54 libusb_device_handle
*fs20pcs_find() {
55 libusb_device_handle
*devh
= NULL
;
61 cnt
= libusb_get_device_list(NULL
, &list
);
63 fprintf(stderr
, "Can't get USB device list: %d\n", (int)cnt
);
67 for (i
= 0; i
< cnt
; i
++){
68 struct libusb_device_descriptor desc
;
70 err
= libusb_get_device_descriptor(list
[i
], &desc
);
74 if ((desc
.idVendor
== ID_VENDOR
) && (desc
.idProduct
== ID_PRODUCT
)) {
75 libusb_device
*dev
= list
[i
];
77 err
= libusb_open(dev
, &devh
);
79 fprintf(stderr
, "Can't open device: %s\n", usb_strerror(err
));
83 err
= libusb_detach_kernel_driver(devh
, 0);
84 if ((err
!= 0) && (err
!= LIBUSB_ERROR_NOT_FOUND
)) {
85 fprintf(stderr
, "Can't detach kernel driver: %s\n", usb_strerror(err
));
89 err
= libusb_claim_interface(devh
, 0);
91 fprintf(stderr
, "Can't claim interface: %s\n", usb_strerror(err
));
103 int fs20pcs_send(libusb_device_handle
*devh
, unsigned char* send_data
)
105 unsigned char recv_data
[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
111 err
= libusb_interrupt_transfer(devh
, 0x01, send_data
, 11, &cnt
, 5000);
113 fprintf(stderr
, "Can't send data: %s\n", usb_strerror(err
));
117 err
= libusb_interrupt_transfer(devh
, 0x81, recv_data
, sizeof(recv_data
), &cnt
, 5000);
119 fprintf(stderr
, "Can't receive data: %s\n", usb_strerror(err
));
123 if ((recv_data
[0] != 0x02) ||
124 (recv_data
[1] != 0x03) ||
125 (recv_data
[2] != 0xa0)) {
126 fprintf(stderr
, "Unexpected response: ");
127 for (i
= 0; i
< cnt
; i
++) {
128 fprintf(stderr
, "0x%02x ", recv_data
[i
]);
130 fprintf(stderr
, "\n");
137 switch(recv_data
[3]) {
142 printf("Firmware: V%d.%d", ((recv_data
[4] & 0xf0) >> 4) & 0x0f, recv_data
[4] & 0x0f);
145 printf("Unknown command");
149 printf("Wrong length");
153 printf("Aborted sending long press");
156 printf("Nothing to stop");
159 printf("Unknown response: 0x%02x 0x%02x", recv_data
[3], recv_data
[4]);
169 unsigned char *parse_housecode(char *hc
)
171 static unsigned char housecode
[2];
180 memset(housecode
, 0, sizeof(housecode
));
184 if (strncmp(hc
, "0x", 2)) {
185 fprintf(stderr
, "Not a 2 byte hexstring: %s\n", hc
);
189 val
= strtoul(hc
, &ep
, 16);
191 fprintf(stderr
, "Not a 2 byte hexstring: %s\n", hc
);
196 memset(hc_fixed
, 0, sizeof(hc_fixed
));
197 for (i
= 0; i
< 8; i
++) {
198 if ((hc
[i
] < '1') || (hc
[i
] > '4')) {
199 fprintf(stderr
, "Not a valid ELV housecode: %s\n", hc
);
202 hc_fixed
[i
] = hc
[i
] - 1;
203 val
= strtoul(hc_fixed
, &ep
, 4);
206 fprintf(stderr
, "Can't parse fixed ELV housecode: %s\n", hc_fixed
);
212 fprintf(stderr
, "Housecode has to be in hex (1234, 0x1234) or ELV (12341234) format!\n");
217 housecode
[0] = (val
& 0xff00) >> 8;
218 housecode
[1] = (val
& 0xff);
223 int parse_addr(char *ad
)
236 if (strncmp(ad
, "0x", 2)) {
237 memset(ad_fixed
, 0, sizeof(ad_fixed
));
238 for (i
= 0; i
< 4; i
++) {
239 if ((ad
[i
] < '1') || (ad
[i
] > '4')) {
240 fprintf(stderr
, "Not a valid ELV address: %s\n", ad
);
243 ad_fixed
[i
] = ad
[i
] - 1;
244 val
= strtoul(ad_fixed
, &ep
, 4);
247 fprintf(stderr
, "Can't parse fixed ELV housecode: %s\n", ad_fixed
);
255 val
= strtoul(ad
, &ep
, 16);
257 fprintf(stderr
, "Not a 1 byte hexstring: %s\n", ad
);
262 fprintf(stderr
, "Address has to be in hex (01, 0x01) or ELV (1112) format!\n");
272 void syntax(char *prog
)
274 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
275 fprintf(stderr
, "Possible options:\n");
276 fprintf(stderr
, "\t-V\t\trequest firmware-version\n");
277 fprintf(stderr
, "\t-x\t\tabort sending long press\n");
278 fprintf(stderr
, "\t-h [ELV|hex]\thousecode in ELV- or hex-notation\n");
279 fprintf(stderr
, "The following actions need an housecode:\n");
280 fprintf(stderr
, "\t-a [ELV|hex]\t\tsend commands to device at specified address\n");
281 fprintf(stderr
, "\t-s hex\t\tsend command byte\n");
282 fprintf(stderr
, "\t-e hex\t\textension byte for command\n");
283 fprintf(stderr
, "\t-r n\t\trepeat sending n times\n");
284 fprintf(stderr
, "\nCommand bytes (without extension byte):\n");
285 fprintf(stderr
, "0x00\t\tswitch off\n");
286 fprintf(stderr
, "0x01-0x10\tswitch on dimmed (0x01: 6.25%%, 0x02: 12.5%%, ..., 0x10: 100%%)\n");
287 fprintf(stderr
, "0x11\t\tswitch on\n");
288 fprintf(stderr
, "0x12\t\ttoggle\n");
289 fprintf(stderr
, "0x13\t\tdimup\n");
290 fprintf(stderr
, "0x14\t\tdimdown\n");
291 fprintf(stderr
, "0x15\t\tdimup, pause, dimdown, pause, ...\n");
292 fprintf(stderr
, "0x16\t\tstart/stop programming internal timer\n");
293 fprintf(stderr
, "0x17\t\tlearn housecode/address\n");
294 fprintf(stderr
, "0x18\t\toff for internal timer, then back on with current level\n");
295 fprintf(stderr
, "0x19\t\ton (100%%) for internal timer, then off\n");
296 fprintf(stderr
, "0x1a\t\ton (old level) for internal timer, then off\n");
297 fprintf(stderr
, "0x1b\t\tfactory reset\n");
298 fprintf(stderr
, "0x1e\t\ton (100%%) for internal timer, then old level\n");
299 fprintf(stderr
, "0x1f\t\ton (old level) for internal timer, then old state\n");
300 fprintf(stderr
, "\nCommand bytes (with timer as extension byte):\n");
301 fprintf(stderr
, "0x20\t\tdim down to off while timer is running\n");
302 fprintf(stderr
, "0x21-0x30\tdim to (0x01: 6.25%%, 0x02: 12.5%%, ..., 0x10: 100%%) while timer is running\n");
303 fprintf(stderr
, "0x31\t\tdim to last level while timer is running\n");
304 fprintf(stderr
, "0x32\t\tdim to off, then after timer dim to off\n");
305 fprintf(stderr
, "0x33\t\tdimup now and switch off after timer\n");
306 fprintf(stderr
, "0x34\t\tdimdown now and switch off after timer\n");
307 fprintf(stderr
, "0x35\t\tdimup or dimdown (toggle) and switch off after timer\n");
308 fprintf(stderr
, "0x36\t\tprogram internal timer\n");
309 fprintf(stderr
, "0x38\t\toff for timer, then back on with current level\n");
310 fprintf(stderr
, "0x39\t\ton (100%%) for timer, then off\n");
311 fprintf(stderr
, "0x3a\t\ton (old level) for timer, then off\n");
312 fprintf(stderr
, "0x3c\t\tprogram internal ramp-up-time\n");
313 fprintf(stderr
, "0x3d\t\tprogram internal ramp-down-time\n");
314 fprintf(stderr
, "0x3e\t\ton (100%%) for timer, then old level\n");
315 fprintf(stderr
, "0x3f\t\ton (old level) for timer, then old state\n");
325 #define DUPLICATE_ACTION if (action != NO_ACTION) { \
326 fprintf(stderr, "duplicate action specified!\n"); \
327 exit(EXIT_FAILURE); }
329 int main(int argc
, char **argv
)
331 unsigned char send_data
[11];
332 unsigned char *housecode
= NULL
;
333 libusb_device_handle
*devh
= NULL
;
337 int action
= NO_ACTION
;
338 uint8_t command
= 0x00;
339 uint8_t extension
= 0x00;
343 memset(send_data
, 0, sizeof(send_data
));
347 while ((opt
= getopt(argc
, argv
, "Vxs:h:a:r:e:")) != -1) {
351 action
= REQUEST_FIRMWARE
;
355 action
= ABORT_LONG_PRESS
;
359 action
= SEND_COMMAND
;
361 command
= strtoul(optarg
, &ep
, 16);
363 fprintf(stderr
, "Can't parse command!\n");
367 printf("Got command: %d\n", command
);
371 housecode
= parse_housecode(optarg
);
373 fprintf(stderr
, "Can't parse housecode!\n");
377 printf("Got housecode: 0x%02x%02x\n", housecode
[0], housecode
[1]);
381 addr
= parse_addr(optarg
);
383 fprintf(stderr
, "Can't parse address!\n");
387 printf("Got address: 0x%02x\n", addr
);
391 repeat
= strtoul(optarg
, &ep
, 10);
393 fprintf(stderr
, "Can't parse repeat!\n");
397 printf("Got repeat: %d\n", repeat
);
401 extension
= strtoul(optarg
, &ep
, 16);
403 fprintf(stderr
, "Can't parse extension!\n");
407 printf("Got extension: %d\n", extension
);
420 case REQUEST_FIRMWARE
:
423 printf("Requesting firmware version...\n");
426 case ABORT_LONG_PRESS
:
429 printf("Aborting long press...\n");
433 if (housecode
== NULL
) {
434 fprintf(stderr
, "housecode needed!\n");
438 fprintf(stderr
, "address needed!\n");
445 send_data
[8] = repeat
;
446 printf("Sending 0x%02x 0x%02x to address %d (hc: 0x%02x%02x) (repeated %d times)\n",
447 command
, extension
, addr
,
448 housecode
[0], housecode
[1],
453 printf("Sending 0x%02x 0x%02x to address %d (hc: 0x%02x%02x)\n",
454 command
, extension
, addr
,
455 housecode
[0], housecode
[1]);
457 send_data
[3] = housecode
[0];
458 send_data
[4] = housecode
[1];
460 send_data
[6] = command
;
461 send_data
[7] = extension
;
466 fprintf(stderr
, "No action specified!\n\n");
472 err
= libusb_init(NULL
);
474 fprintf(stderr
, "Can't initialize libusb: %s\n", usb_strerror(err
));
478 devh
= fs20pcs_find();
480 fprintf(stderr
, "Can't find/open fs20pcs!\n");
484 if (fs20pcs_send(devh
, send_data
) == 0) {
485 fprintf(stderr
, "Can't communicate with fs20pcs!\n");