From d7caf78f5ebaf37e7099f470bf360e62e36fc02a Mon Sep 17 00:00:00 2001 From: "Joseph C. Lehner" Date: Thu, 18 Feb 2016 22:11:12 +0100 Subject: [PATCH] Accept pretty names for -R instead of codes --- main.c | 12 +++++------- nmrp.c | 29 +++++++++++++++++++++++++++-- nmrpd.h | 3 +-- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/main.c b/main.c index 9760c61..73f22f7 100644 --- a/main.c +++ b/main.c @@ -42,7 +42,7 @@ void usage(FILE *fp) " -T Time (seconds) to wait after successfull TFTP upload\n" " -p Port to use for TFTP upload\n" #ifdef NMRPFLASH_SET_REGION - " -R Set device region\n" + " -R Set device region (NA, WW, GR, PR, RU, BZ, IN, KO, JP)\n" #endif #ifdef NMRPFLASH_TFTP_TEST " -U Test TFTP upload\n" @@ -83,7 +83,7 @@ int main(int argc, char **argv) .mac = "ff:ff:ff:ff:ff:ff", .op = NMRP_UPLOAD_FW, .port = 69, - .force_root = 1 + .region = NULL, }; #ifdef NMRPFLASH_WINDOWS WSADATA wsa; @@ -121,16 +121,16 @@ int main(int argc, char **argv) case 'M': args.ipmask = optarg; break; - case 'p': #ifdef NMRPFLASH_SET_REGION case 'R': + args.region = optarg; + break; #endif + case 'p': case 'T': case 't': if (c == 'p') { max = 0xffff; - } else if (c == 'R') { - max = 0x0009; } val = atoi(optarg); @@ -145,8 +145,6 @@ int main(int argc, char **argv) args.rx_timeout = val; } else if (c == 'T') { args.ul_timeout = val * 1000; - } else if (c == 'R') { - args.region = val; } break; diff --git a/nmrp.c b/nmrp.c index db351fa..327bf81 100644 --- a/nmrp.c +++ b/nmrp.c @@ -118,6 +118,22 @@ static const char *msg_code_str(uint16_t code) #undef CASE_CODE } +static uint16_t to_region_code(const char *region) +{ +#define REGION_CODE(r, c) if (!strcasecmp(region, r)) return c + REGION_CODE("NA", 0x0001); + REGION_CODE("WW", 0x0002); + REGION_CODE("GR", 0x0003); + REGION_CODE("PR", 0x0004); + REGION_CODE("RU", 0x0005); + REGION_CODE("BZ", 0x0006); + REGION_CODE("IN", 0x0007); + REGION_CODE("KO", 0x0008); + REGION_CODE("JP", 0x0009); +#undef REGION_CODE + return 0; +} + static void msg_dump(struct nmrp_msg *msg, int dump_opts) { struct nmrp_opt *opt; @@ -420,6 +436,16 @@ int nmrp_do(struct nmrpd_args *args) } } + if (args->region) { + region = htons(to_region_code(args->region)); + if (!region) { + fprintf(stderr, "Invalid region code '%s'.\n", args->region); + return 1; + } + } else { + region = 0; + } + status = 1; sock = ethsock_create(args->intf, ETH_P_NMRP); @@ -513,8 +539,7 @@ int nmrp_do(struct nmrpd_args *args) msg_opt_add(&tx.msg, NMRP_O_FW_UP, NULL, 0); #ifdef NMRPFLASH_SET_REGION - if (args->region) { - region = htons(args->region); + if (region) { msg_opt_add(&tx.msg, NMRP_O_DEV_REGION, ®ion, 2); } #endif diff --git a/nmrpd.h b/nmrpd.h index 28d3f0f..841b4f3 100644 --- a/nmrpd.h +++ b/nmrpd.h @@ -71,8 +71,7 @@ struct nmrpd_args { const char *mac; enum nmrp_op op; uint16_t port; - uint16_t region; - int force_root; + const char *region; }; const char *leafname(const char *path); -- 2.25.1