Accept pretty names for -R instead of codes

This commit is contained in:
Joseph C. Lehner 2016-02-18 22:11:12 +01:00
parent cbb94be759
commit d7caf78f5e
3 changed files with 33 additions and 11 deletions

12
main.c
View file

@ -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;

29
nmrp.c
View file

@ -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

View file

@ -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);