+unsigned char *parse_housecode(char *hc)
+{
+ static unsigned char housecode[2];
+ char hc_fixed[9];
+ char *ep;
+ unsigned long val;
+ int i;
+
+ if (hc == NULL)
+ return NULL;
+
+ memset(housecode, 0, sizeof(housecode));
+
+ switch(strlen(hc)) {
+ case 6:
+ if (strncmp(hc, "0x", 2)) {
+ fprintf(stderr, "Not a 2 byte hexstring: %s\n", hc);
+ return NULL;
+ }
+ case 4:
+ val = strtoul(hc, &ep, 16);
+ if (*ep != '\0') {
+ fprintf(stderr, "Not a 2 byte hexstring: %s\n", hc);
+ return NULL;
+ }
+ break;
+ case 8:
+ memset(hc_fixed, 0, sizeof(hc_fixed));
+ for (i = 0; i < 8; i++) {
+ if ((hc[i] < '1') || (hc[i] > '4')) {
+ fprintf(stderr, "Not a valid ELV housecode: %s\n", hc);
+ return NULL;
+ }
+ hc_fixed[i] = hc[i] - 1;
+ val = strtoul(hc_fixed, &ep, 4);
+
+ if (*ep != '\0') {
+ fprintf(stderr, "Can't parse fixed ELV housecode: %s\n", hc_fixed);
+ return NULL;
+ }
+ }
+ break;
+ default:
+ fprintf(stderr, "Housecode has to be 4 or 8 chars long!\n");
+ return NULL;
+ break;
+ }
+
+ housecode[0] = (val & 0xff00) >> 8;
+ housecode[1] = (val & 0xff);
+
+ return housecode;
+}
+
+void syntax(char *prog)
+{
+ fprintf(stderr, "Syntax: %s options\n\n", prog);
+ fprintf(stderr, "Possible options:\n");
+ fprintf(stderr, "\t-V\t\trequest hardware-version\n");
+ fprintf(stderr, "\t-x\t\tabort sending long press\n");
+ fprintf(stderr, "\t-h [ELV|hex]\thousecode in ELV- or hex-notation\n");
+ fprintf(stderr, "The following actions need an housecode:\n");
+ fprintf(stderr, "\t-a n\t\tsend commands to device at address n\n");
+ fprintf(stderr, "\t-s hex\t\tsend command byte\n");
+ fprintf(stderr, "\t-e hex\t\textension byte for command\n");
+ fprintf(stderr, "\t-r n\t\trepeat sending n times\n");
+ fprintf(stderr, "\nCommand bytes (without extension byte):\n");
+ fprintf(stderr, "0x00\t\tswitch off\n");
+ fprintf(stderr, "0x01-0x10\tswitch on dimmed (0x01: 6.25%%, 0x02: 12.5%%, ..., 0x10: 100%%)\n");
+ fprintf(stderr, "0x11\t\tswitch on\n");
+ fprintf(stderr, "0x12\t\ttoggle\n");
+ fprintf(stderr, "0x13\t\tdimup\n");
+ fprintf(stderr, "0x14\t\tdimdown\n");
+ fprintf(stderr, "0x15\t\tdimup, pause, dimdown, pause, ...\n");
+ fprintf(stderr, "0x16\t\tstart/stop programming internal timer\n");
+ fprintf(stderr, "0x17\t\tlearn housecode/address\n");
+ fprintf(stderr, "0x18\t\toff for internal timer, then back on with current level\n");
+ fprintf(stderr, "0x19\t\ton (100%%) for internal timer, then off\n");
+ fprintf(stderr, "0x1a\t\ton (old level) for internal timer, then off\n");
+ fprintf(stderr, "0x1b\t\tfactory reset\n");
+ fprintf(stderr, "0x1e\t\ton (100%%) for internal timer, then old level\n");
+ fprintf(stderr, "0x1f\t\ton (old level) for internal timer, then old state\n");
+ fprintf(stderr, "\nCommand bytes (with timer as extension byte):\n");
+ fprintf(stderr, "0x20\t\tdim down to off while timer is running\n");
+ fprintf(stderr, "0x21-0x30\tdim to (0x01: 6.25%%, 0x02: 12.5%%, ..., 0x10: 100%%) while timer is running\n");
+ fprintf(stderr, "0x31\t\tdim to last level while timer is running\n");
+ fprintf(stderr, "0x32\t\tdim to off, then after timer dim to off\n");
+ fprintf(stderr, "0x33\t\tdimup now and switch off after timer\n");
+ fprintf(stderr, "0x34\t\tdimdown now and switch off after timer\n");
+ fprintf(stderr, "0x35\t\tdimup or dimdown (toggle) and switch off after timer\n");
+ fprintf(stderr, "0x36\t\tprogram internal timer\n");
+ fprintf(stderr, "0x38\t\toff for timer, then back on with current level\n");
+ fprintf(stderr, "0x39\t\ton (100%%) for timer, then off\n");
+ fprintf(stderr, "0x3a\t\ton (old level) for timer, then off\n");
+ fprintf(stderr, "0x3c\t\tprogram internal ramp-up-time\n");
+ fprintf(stderr, "0x3d\t\tprogram internal ramp-down-time\n");
+ fprintf(stderr, "0x3e\t\ton (100%%) for timer, then old level\n");
+ fprintf(stderr, "0x3f\t\ton (old level) for timer, then old state\n");
+};
+
+enum {
+ NO_ACTION,
+ REQUEST_FIRMWARE,
+ SEND_COMMAND,
+ ABORT_LONG_PRESS
+};
+
+#define DUPLICATE_ACTION if (action != NO_ACTION) { \
+ fprintf(stderr, "duplicate action specified!\n"); \
+ exit(EXIT_FAILURE); }
+