From 6c7fff3be076538f9587eb02c800c2e34d623dfc Mon Sep 17 00:00:00 2001 From: Michael Gernoth Date: Sat, 18 May 2013 21:55:16 +0000 Subject: [PATCH] parse ELV address, too --- fs20pcs.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/fs20pcs.c b/fs20pcs.c index d793c55..1223ceb 100644 --- a/fs20pcs.c +++ b/fs20pcs.c @@ -209,7 +209,7 @@ unsigned char *parse_housecode(char *hc) } 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; } @@ -220,6 +220,55 @@ unsigned char *parse_housecode(char *hc) 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); @@ -228,7 +277,7 @@ void syntax(char *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"); @@ -329,8 +378,8 @@ int main(int argc, char **argv) #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); } -- 2.39.5