Accept pretty names for -R instead of codes
authorJoseph C. Lehner <joseph.c.lehner@gmail.com>
Thu, 18 Feb 2016 21:11:12 +0000 (22:11 +0100)
committerJoseph C. Lehner <joseph.c.lehner@gmail.com>
Thu, 18 Feb 2016 21:11:12 +0000 (22:11 +0100)
main.c
nmrp.c
nmrpd.h

diff --git a/main.c b/main.c
index 9760c61fad06bd59862276981cb18730d26151de..73f22f7e1e925d895ec62b8616ebe202371e816d 100644 (file)
--- a/main.c
+++ b/main.c
@@ -42,7 +42,7 @@ void usage(FILE *fp)
                        " -T <timeout>    Time (seconds) to wait after successfull TFTP upload\n"
                        " -p <port>       Port to use for TFTP upload\n"
 #ifdef NMRPFLASH_SET_REGION
-                       " -R <region>     Set device region\n"
+                       " -R <region>     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 db351fa231a9e61759a750ecea6468356a7edcbc..327bf816bc0d2d956dbe67cb472496721f164a04 100644 (file)
--- 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, &region, 2);
                                }
 #endif
diff --git a/nmrpd.h b/nmrpd.h
index 28d3f0fd825a145cad0ca2a41f41292a306cc615..841b4f3758ef82d78d0b1e1a3d4e69e6c03e0559 100644 (file)
--- 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);