X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=main.c;h=39f327c32e3efc720fe8cd5a7c1e651b79912df0;hb=186ec81be4b2c18f3bf41c622175da1b1f2f2a96;hp=7ee086c41294310fc146da2142bfd65ac30f6b76;hpb=875aad86c872a0d4adebfbcb2a97339519de46d0;p=oweals%2Fnmrpflash.git diff --git a/main.c b/main.c index 7ee086c..39f327c 100644 --- a/main.c +++ b/main.c @@ -1,44 +1,97 @@ +#include +#include #include #include "nmrpd.h" -void usage() +void usage(FILE *fp) { - printf( - "Usage: nmrpd [options] [command] [command args]\n" + fprintf(fp, + "Usage: nmrpd [OPTIONS...]\n" "\n" - "Available options:\n" - " -m [mac] MAC address of target device (xx:xx:xx:xx:xx:xx)\n" - " -a [ipaddr] IP address to assign to target device\n" - " -M [netmask] Subnet mask to assign to target device\n" - " -t [timeout] Timeout (in milliseconds) for regular messages\n" - " -T [timeout] Time to wait after successfull TFTP upload\n" - " -p [port] Port to use for TFTP upload\n" - " -i [interface] Network interface directly connected to device\n" + "Options:\n" + " -a IP address to assign to target device\n" + " -f Firmware file\n" + " -i Network interface directly connected to device\n" + " -m MAC address of target device (xx:xx:xx:xx:xx:xx)\n" + " -M Subnet mask to assign to target device\n" + " -t Timeout (in milliseconds) for regular messages\n" + " -T Time to wait after successfull TFTP upload\n" + " -p Port to use for TFTP upload\n" + " -h Show this screen\n" "\n" - "Available commands:\n" - " set-region Set region of device\n" - " upload-firmware Upload new firmware\n" - " upload-strings Upload string table\n" + "Options -a, -i and -f are mandatory!\n" "\n" ); } - - int main(int argc, char **argv) { + int c, val, max; struct nmrpd_args args = { .rx_timeout = 200, .ul_timeout = 60000, - .filename = argc >= 2 ? argv[1] : NULL, - .ipaddr = "192.168.2.2", + .filename = NULL, + .ipaddr = NULL, .ipmask = "255.255.255.0", - .intf = "enp4s0", + .intf = NULL, .mac = "ff:ff:ff:ff:ff:ff", .op = NMRP_UPLOAD_FW, .port = 69, - .force_root = 0 + .force_root = 1 }; + opterr = 0; + + while ((c = getopt(argc, argv, "a:f:i:m:M:p:t:T:")) != -1) { + max = 0xffffffff; + switch (c) { + case 'a': + args.ipaddr = optarg; + break; + case 'f': + args.filename = optarg; + break; + case 'i': + args.intf = optarg; + break; + case 'm': + args.mac = optarg; + break; + case 'M': + args.ipmask = optarg; + break; + case 'p': + max = 0xffff; + case 'T': + case 't': + val = atoi(optarg); + if (val <= 0 || val > max) { + fprintf(stderr, "Invalid numeric value for -%c.\n", c); + return 1; + } + + if (c == 'p') { + args.port = val; + } else if (c == 't') { + args.rx_timeout = val; + } else { + args.ul_timeout = val; + } + + break; + case 'h': + usage(stdout); + return 0; + default: + usage(stderr); + return 1; + } + } + + if (!args.filename || !args.intf || !args.ipaddr) { + usage(stderr); + return 1; + } + return nmrp_do(&args); }