}
break;
default:
- fprintf(stderr, "Housecode has to be 4 or 8 chars long!\n");
+ fprintf(stderr, "Housecode has to be in hex (1234, 0x1234) or ELV (12341234) format!\n");
return NULL;
break;
}
return housecode;
}
+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;
+}
+
void syntax(char *prog)
{
fprintf(stderr, "Syntax: %s options\n\n", prog);
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-a [ELV|hex]\t\tsend commands to device at specified address\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");
#endif
break;
case 'a':
- addr = strtoul(optarg, &ep, 10);
- if (*ep != '\0') {
+ addr = parse_addr(optarg);
+ if (addr == -1) {
fprintf(stderr, "Can't parse address!\n");
exit(EXIT_FAILURE);
}