+int parse_addr(char *ad)
+{
+ int addr = -1;
+ char ad_fixed[5];
+ char *ep;
+ unsigned long val;
+ int i;
+
+ if (ad == NULL)
+ return -1;
+
+ switch(strlen(ad)) {
+ case 4:
+ if (strncmp(ad, "0x", 2)) {
+ memset(ad_fixed, 0, sizeof(ad_fixed));
+ for (i = 0; i < 4; i++) {
+ if ((ad[i] < '1') || (ad[i] > '4')) {
+ fprintf(stderr, "Not a valid ELV address: %s\n", ad);
+ return -1;
+ }
+ ad_fixed[i] = ad[i] - 1;
+ val = strtoul(ad_fixed, &ep, 4);
+
+ if (*ep != '\0') {
+ fprintf(stderr, "Can't parse fixed ELV housecode: %s\n", ad_fixed);
+ return -1;
+ }
+ }
+
+ break;
+ }
+ case 2:
+ val = strtoul(ad, &ep, 16);
+ if (*ep != '\0') {
+ fprintf(stderr, "Not a 1 byte hexstring: %s\n", ad);
+ return -1;
+ }
+ break;
+ default:
+ fprintf(stderr, "Address has to be in hex (01, 0x01) or ELV (1112) format!\n");
+ return -1;
+ break;
+ }
+
+ addr = val & 0xff;
+
+ return addr;
+}
+
+int parse_command(char *cmd)
+{
+ int command = -1;
+ char *ep;
+ unsigned long val;
+
+ if (cmd == NULL)
+ return -1;
+
+ if (!strcasecmp(cmd, "off")) {
+ command = 0x00;
+ } else if (!strcasecmp(cmd, "on")) {
+ command = 0x11;
+ } else if (!strcasecmp(cmd, "toggle")) {
+ command = 0x12;
+ } else if (!strcasecmp(cmd, "dimup")) {
+ command = 0x13;
+ } else if (!strcasecmp(cmd, "dimdown")) {
+ command = 0x14;
+ } else if (!strcasecmp(cmd, "on-for-timer")) {
+ command = 0x39;
+ } else {
+ val = strtoul(cmd, &ep, 16);
+ if (*ep != '\0') {
+ fprintf(stderr, "Not a 1 byte hexstring or alias: %s\n", cmd);
+ return -1;
+ }
+
+ command = val & 0xff;
+ }
+
+ return command;
+}
+
+int parse_extension(char *ext)
+{
+ int extension = -1;
+ char *ep;
+
+ if (ext == NULL)
+ return -1;
+
+ if ((ext[strlen(ext)-1] == 's') ||
+ (ext[strlen(ext)-1] == 'S')) {
+ double t;
+ double diff = 1;
+ uint8_t high = 0;
+ uint8_t low = 0;
+ uint8_t i;
+
+ t = strtod(ext, &ep);
+ if ((*ep != 's') && (*ep != 'S')) {
+ fprintf(stderr, "Not a valid time in seconds: %s\n", ext);
+ return -1;
+ }
+
+ /* time = 2^(high nibble) * (low nibble) * 0.25 , high may only be 12 */
+ t /= 0.25;
+#ifdef DEBUG
+ printf("t: %f\n", t);
+#endif
+
+ for(i = 1; i <= 0xf; i++) {
+ double h;
+ double l;
+ uint8_t i_l;
+ double d;
+
+#ifdef DEBUG
+ printf("i: 0x%01x\n", i);
+#endif
+
+ h = t / i;
+#ifdef DEBUG
+ printf("h: %f\n", h);
+#endif
+
+ l = log2(h);
+#ifdef DEBUG
+ printf("l: %f\n", l);
+#endif
+
+ i_l = l;
+ if (i_l > 12)
+ i_l = 12;
+
+ d = fabs(l - i_l);
+
+#ifdef DEBUG
+ printf("d: %f\n", d);
+#endif
+
+ if (d < diff) {
+#ifdef DEBUG
+ printf("Current best match!\n");
+#endif
+ diff = d;
+ high = i_l;
+ low = i;
+ }
+ }
+
+#ifdef DEBUG
+ printf("Got: %f, high: %01x, low: %01x\n", t, high, low);
+#endif
+
+ if ((high == 0) && (low == 0)) {
+ fprintf(stderr, "Can't find extension byte for %s!\n", ext);
+ return -1;
+ }
+
+ extension = ((high & 0xf) << 4) | (low & 0xf);
+ } else {
+ unsigned long val;
+
+ val = strtoul(ext, &ep, 16);
+ if (*ep != '\0') {
+ fprintf(stderr, "Not a 1 byte hexstring or time: %s\n", ext);
+ return -1;
+ }
+
+ extension = val & 0xff;
+ }
+
+ return extension;
+}
+