1 /* FS20PCS libusb-driver
3 * Copyright (c) 2013 Michael Gernoth <michael@gernoth.net>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include <libusb-1.0/libusb.h>
32 #define USB_TIMEOUT 10000
34 #define ID_VENDOR 0x18ef
35 #define ID_PRODUCT 0xe015
37 #define EXT_TO_SEC(ext) ((1 << ((ext & 0xf0) >> 4)) * (ext & 0x0f) * 0.25)
41 /* Not in all libusb-1.0 versions, so we have to roll our own :-( */
42 static char * usb_strerror(int e
)
44 static char unknerr
[256];
50 return "Input/output error";
51 case LIBUSB_ERROR_INVALID_PARAM
:
52 return "Invalid parameter";
53 case LIBUSB_ERROR_ACCESS
:
54 return "Access denied (insufficient permissions)";
55 case LIBUSB_ERROR_NO_DEVICE
:
56 return "No such device (it may have been disconnected)";
57 case LIBUSB_ERROR_NOT_FOUND
:
58 return "Entity not found";
59 case LIBUSB_ERROR_BUSY
:
60 return "Resource busy";
61 case LIBUSB_ERROR_TIMEOUT
:
62 return "Operation timed out";
63 case LIBUSB_ERROR_OVERFLOW
:
65 case LIBUSB_ERROR_PIPE
:
67 case LIBUSB_ERROR_INTERRUPTED
:
68 return "System call interrupted (perhaps due to signal)";
69 case LIBUSB_ERROR_NO_MEM
:
70 return "Insufficient memory";
71 case LIBUSB_ERROR_NOT_SUPPORTED
:
72 return "Operation not supported or unimplemented on this platform";
73 case LIBUSB_ERROR_OTHER
:
76 snprintf(unknerr
, sizeof(unknerr
), "Unknown error code %d / 0x%02x", e
, e
);
80 libusb_device_handle
*fs20pcs_find() {
81 libusb_device_handle
*devh
= NULL
;
87 cnt
= libusb_get_device_list(NULL
, &list
);
89 fprintf(stderr
, "Can't get USB device list: %d\n", (int)cnt
);
93 for (i
= 0; i
< cnt
; i
++){
94 struct libusb_device_descriptor desc
;
96 err
= libusb_get_device_descriptor(list
[i
], &desc
);
100 if ((desc
.idVendor
== ID_VENDOR
) && (desc
.idProduct
== ID_PRODUCT
)) {
101 libusb_device
*dev
= list
[i
];
103 err
= libusb_open(dev
, &devh
);
105 fprintf(stderr
, "Can't open device: %s\n", usb_strerror(err
));
109 err
= libusb_detach_kernel_driver(devh
, 0);
110 if ((err
!= 0) && (err
!= LIBUSB_ERROR_NOT_FOUND
)) {
111 fprintf(stderr
, "Can't detach kernel driver: %s\n", usb_strerror(err
));
115 err
= libusb_claim_interface(devh
, 0);
117 fprintf(stderr
, "Can't claim interface: %s\n", usb_strerror(err
));
129 int fs20pcs_send(libusb_device_handle
*devh
, unsigned char* send_data
)
131 unsigned char recv_data
[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
137 err
= libusb_interrupt_transfer(devh
, 0x01, send_data
, 11, &cnt
, USB_TIMEOUT
);
139 fprintf(stderr
, "Can't send data: %s\n", usb_strerror(err
));
143 err
= libusb_interrupt_transfer(devh
, 0x81, recv_data
, sizeof(recv_data
), &cnt
, USB_TIMEOUT
);
145 fprintf(stderr
, "Can't receive data: %s\n", usb_strerror(err
));
149 if ((recv_data
[0] != 0x02) ||
150 (recv_data
[1] != 0x03) ||
151 (recv_data
[2] != 0xa0)) {
152 fprintf(stderr
, "Unexpected response: ");
153 for (i
= 0; i
< cnt
; i
++) {
154 fprintf(stderr
, "0x%02x ", recv_data
[i
]);
156 fprintf(stderr
, "\n");
163 switch(recv_data
[3]) {
168 printf("Firmware: V%d.%d", ((recv_data
[4] & 0xf0) >> 4) & 0x0f, recv_data
[4] & 0x0f);
171 printf("Unknown command");
175 printf("Wrong length");
179 printf("Aborted sending long press");
182 printf("Nothing to stop");
185 printf("Unknown response: 0x%02x 0x%02x", recv_data
[3], recv_data
[4]);
195 unsigned char *parse_housecode(char *hc
)
197 static unsigned char housecode
[2];
206 memset(housecode
, 0, sizeof(housecode
));
210 if (strncmp(hc
, "0x", 2)) {
211 fprintf(stderr
, "Not a 2 byte hexstring: %s\n", hc
);
215 val
= strtoul(hc
, &ep
, 16);
217 fprintf(stderr
, "Not a 2 byte hexstring: %s\n", hc
);
222 memset(hc_fixed
, 0, sizeof(hc_fixed
));
223 for (i
= 0; i
< 8; i
++) {
224 if ((hc
[i
] < '1') || (hc
[i
] > '4')) {
225 fprintf(stderr
, "Not a valid ELV housecode: %s\n", hc
);
228 hc_fixed
[i
] = hc
[i
] - 1;
229 val
= strtoul(hc_fixed
, &ep
, 4);
232 fprintf(stderr
, "Can't parse fixed ELV housecode: %s\n", hc_fixed
);
238 fprintf(stderr
, "Housecode has to be in hex (1234, 0x1234) or ELV (12341234) format!\n");
243 housecode
[0] = (val
& 0xff00) >> 8;
244 housecode
[1] = (val
& 0xff);
249 int parse_addr(char *ad
)
262 if (strncmp(ad
, "0x", 2)) {
263 memset(ad_fixed
, 0, sizeof(ad_fixed
));
264 for (i
= 0; i
< 4; i
++) {
265 if ((ad
[i
] < '1') || (ad
[i
] > '4')) {
266 fprintf(stderr
, "Not a valid ELV address: %s\n", ad
);
269 ad_fixed
[i
] = ad
[i
] - 1;
270 val
= strtoul(ad_fixed
, &ep
, 4);
273 fprintf(stderr
, "Can't parse fixed ELV housecode: %s\n", ad_fixed
);
281 val
= strtoul(ad
, &ep
, 16);
283 fprintf(stderr
, "Not a 1 byte hexstring: %s\n", ad
);
288 fprintf(stderr
, "Address has to be in hex (01, 0x01) or ELV (1112) format!\n");
298 int parse_command(char *cmd
)
307 if (!strcasecmp(cmd
, "off")) {
309 } else if (!strcasecmp(cmd
, "on")) {
311 } else if (!strcasecmp(cmd
, "toggle")) {
313 } else if (!strcasecmp(cmd
, "dimup")) {
315 } else if (!strcasecmp(cmd
, "dimdown")) {
317 } else if (!strcasecmp(cmd
, "on-for-timer")) {
320 val
= strtoul(cmd
, &ep
, 16);
322 fprintf(stderr
, "Not a 1 byte hexstring or alias: %s\n", cmd
);
326 command
= val
& 0xff;
332 int parse_extension(char *ext
)
340 if ((ext
[strlen(ext
)-1] == 's') ||
341 (ext
[strlen(ext
)-1] == 'S')) {
348 t
= strtod(ext
, &ep
);
349 if ((*ep
!= 's') && (*ep
!= 'S')) {
350 fprintf(stderr
, "Not a valid time in seconds: %s\n", ext
);
354 /* time = 2^(high nibble) * (low nibble) * 0.25 , high may only be 12 */
357 printf("t: %f\n", t
);
360 for(i
= 1; i
<= 0xf; i
++) {
367 printf("i: 0x%01x\n", i
);
372 printf("h: %f\n", h
);
377 printf("l: %f\n", l
);
387 printf("d: %f\n", d
);
392 printf("Current best match!\n");
401 printf("Got: %f, high: %01x, low: %01x\n", t
, high
, low
);
404 if ((high
== 0) && (low
== 0)) {
405 fprintf(stderr
, "Can't find extension byte for %s!\n", ext
);
409 extension
= ((high
& 0xf) << 4) | (low
& 0xf);
413 val
= strtoul(ext
, &ep
, 16);
415 fprintf(stderr
, "Not a 1 byte hexstring or time: %s\n", ext
);
419 extension
= val
& 0xff;
425 void syntax(char *prog
)
427 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
428 fprintf(stderr
, "Possible options:\n");
429 fprintf(stderr
, "\t-V\t\trequest firmware-version\n");
430 fprintf(stderr
, "\t-x\t\tabort sending long press\n");
431 fprintf(stderr
, "\t-h [ELV|hex]\thousecode in ELV- or hex-notation\n");
432 fprintf(stderr
, "The following options need an housecode:\n");
433 fprintf(stderr
, "\t-a [ELV|hex]\tdestination address\n");
434 fprintf(stderr
, "\t-s [hex|alias]\tsend command byte\n");
435 fprintf(stderr
, "\t-e [hex|time]\textension byte for command or time in seconds (e.g. 10s)\n");
436 fprintf(stderr
, "\t-r n\t\trepeat sending n times\n");
437 fprintf(stderr
, "\nCommand bytes (without extension byte):\n");
438 fprintf(stderr
, "hex\t\talias\tdescription\n");
439 fprintf(stderr
, "0x00\t\toff\tswitch off\n");
440 fprintf(stderr
, "0x01-0x10\t\tswitch on dimmed (0x01: 6.25%%, 0x02: 12.5%%, ..., 0x10: 100%%)\n");
441 fprintf(stderr
, "0x11\t\ton\tswitch on\n");
442 fprintf(stderr
, "0x12\t\ttoggle\ttoggle\n");
443 fprintf(stderr
, "0x13\t\tdimup\tdimup\n");
444 fprintf(stderr
, "0x14\t\tdimdown\tdimdown\n");
445 fprintf(stderr
, "0x15\t\t\tdimup, pause, dimdown, pause, ...\n");
446 fprintf(stderr
, "0x16\t\t\tstart/stop programming internal timer\n");
447 fprintf(stderr
, "0x17\t\t\tlearn housecode/address\n");
448 fprintf(stderr
, "0x18\t\t\toff for internal timer, then back on with current level\n");
449 fprintf(stderr
, "0x19\t\t\ton (100%%) for internal timer, then off\n");
450 fprintf(stderr
, "0x1a\t\t\ton (old level) for internal timer, then off\n");
451 fprintf(stderr
, "0x1b\t\t\tfactory reset\n");
452 fprintf(stderr
, "0x1e\t\t\ton (100%%) for internal timer, then old level\n");
453 fprintf(stderr
, "0x1f\t\t\ton (old level) for internal timer, then old state\n");
454 fprintf(stderr
, "\nCommand bytes (with timer as extension byte):\n");
455 fprintf(stderr
, "hex\t\talias\tdescription\n");
456 fprintf(stderr
, "0x20\t\t\tdim down to off while timer is running\n");
457 fprintf(stderr
, "0x21-0x30\t\tdim to (0x01: 6.25%%, 0x02: 12.5%%, ..., 0x10: 100%%) while timer is running\n");
458 fprintf(stderr
, "0x31\t\t\tdim to last level while timer is running\n");
459 fprintf(stderr
, "0x32\t\t\tdim to off, then after timer dim to off\n");
460 fprintf(stderr
, "0x33\t\t\tdimup now and switch off after timer\n");
461 fprintf(stderr
, "0x34\t\t\tdimdown now and switch off after timer\n");
462 fprintf(stderr
, "0x35\t\t\tdimup or dimdown (toggle) and switch off after timer\n");
463 fprintf(stderr
, "0x36\t\t\tprogram internal timer\n");
464 fprintf(stderr
, "0x38\t\t\toff for timer, then back on with current level\n");
465 fprintf(stderr
, "0x39\ton-for-timer\ton (100%%) for timer, then off\n");
466 fprintf(stderr
, "0x3a\t\t\ton (old level) for timer, then off\n");
467 fprintf(stderr
, "0x3c\t\t\tprogram internal ramp-up-time\n");
468 fprintf(stderr
, "0x3d\t\t\tprogram internal ramp-down-time\n");
469 fprintf(stderr
, "0x3e\t\t\ton (100%%) for timer, then old level\n");
470 fprintf(stderr
, "0x3f\t\t\ton (old level) for timer, then old state\n");
480 #define DUPLICATE_ACTION if (action != NO_ACTION) { \
481 fprintf(stderr, "duplicate action specified!\n"); \
482 exit(EXIT_FAILURE); }
484 int main(int argc
, char **argv
)
486 unsigned char send_data
[11];
487 unsigned char *housecode
= NULL
;
488 libusb_device_handle
*devh
= NULL
;
492 int action
= NO_ACTION
;
498 memset(send_data
, 0, sizeof(send_data
));
502 while ((opt
= getopt(argc
, argv
, "Vxs:h:a:r:e:")) != -1) {
506 action
= REQUEST_FIRMWARE
;
510 action
= ABORT_LONG_PRESS
;
514 action
= SEND_COMMAND
;
516 command
= parse_command(optarg
);
518 fprintf(stderr
, "Can't parse command!\n");
522 printf("Got command: %d\n", command
);
526 housecode
= parse_housecode(optarg
);
528 fprintf(stderr
, "Can't parse housecode!\n");
532 printf("Got housecode: 0x%02x%02x\n", housecode
[0], housecode
[1]);
536 addr
= parse_addr(optarg
);
538 fprintf(stderr
, "Can't parse address!\n");
542 printf("Got address: 0x%02x\n", addr
);
546 repeat
= strtoul(optarg
, &ep
, 10);
548 fprintf(stderr
, "Can't parse repeat!\n");
552 printf("Got repeat: %d\n", repeat
);
556 extension
= parse_extension(optarg
);
557 if (extension
== -1) {
558 fprintf(stderr
, "Can't parse extension!\n");
562 printf("Got extension: %d\n", extension
);
575 case REQUEST_FIRMWARE
:
578 printf("Requesting firmware version...\n");
581 case ABORT_LONG_PRESS
:
584 printf("Aborting long press...\n");
588 if (housecode
== NULL
) {
589 fprintf(stderr
, "housecode needed!\n");
593 fprintf(stderr
, "address needed!\n");
600 send_data
[8] = repeat
;
601 printf("Sending 0x%02x 0x%02x (%.2fs) to address %d (hc: 0x%02x%02x) (repeated %d times)\n",
602 command
, extension
, EXT_TO_SEC(extension
), addr
,
603 housecode
[0], housecode
[1],
608 printf("Sending 0x%02x 0x%02x (%.2fs) to address %d (hc: 0x%02x%02x)\n",
609 command
, extension
, EXT_TO_SEC(extension
), addr
,
610 housecode
[0], housecode
[1]);
612 send_data
[3] = housecode
[0];
613 send_data
[4] = housecode
[1];
615 send_data
[6] = command
;
616 send_data
[7] = extension
;
621 fprintf(stderr
, "No action specified!\n\n");
627 err
= libusb_init(NULL
);
629 fprintf(stderr
, "Can't initialize libusb: %s\n", usb_strerror(err
));
633 devh
= fs20pcs_find();
635 fprintf(stderr
, "Can't find/open fs20pcs!\n");
639 if (fs20pcs_send(devh
, send_data
) == 0) {
640 fprintf(stderr
, "Can't communicate with fs20pcs!\n");