X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=cli%2Fcom_bitrate.c;h=60415822baeb07bd6d55f39e347fc9b1eca64dd3;hb=ba1e16863bdedc5f52b077932aa7301ed14a0f06;hp=94afad882efdb2bbfc280916bd4649994e8ff90e;hpb=47fbfc0635ef174f71047d75c328f8e848e6a02d;p=ngadmin diff --git a/cli/com_bitrate.c b/cli/com_bitrate.c index 94afad8..6041582 100644 --- a/cli/com_bitrate.c +++ b/cli/com_bitrate.c @@ -2,119 +2,119 @@ #include "commands.h" - -// helper function to analyse bitrate speed specifications -static int bitrate_analyse (int nb, const char **com, int *ports) { - - int i=0, s; - - - while ( i SPEEDSPEC [ SPEEDSPEC ...]\n"); - printf("SPEEDSPEC: [inout ] [in ] [out ]\n"); - ret=false; - goto end; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - ports=malloc(2*sa->ports*sizeof(int)); - - // get defaults if present - if ( strcmp(com[k], "all")==0 ) { - ++k; - k+=bitrate_analyse(nb-k, &com[k], defs); - } - - // apply defaults - for (i=0; iports; ++i) { - memcpy(&ports[2*i], defs, sizeof(defs)); - } - - // get ports specifics - while ( k=0 && pports ) { - k+=bitrate_analyse(nb-k, &com[k], &ports[2*p]); - } - } - - // send it to the switch - i=ngadmin_setBitrateLimits(nga, ports); - printErrCode(i); - - - end: - free(ports); - - return ret; - +bool do_bitrate_set (int argc, const char **argv, struct ngadmin *nga) +{ + int i, k = 0, defs[] = {12, 12}, p, *ports = NULL; + const struct swi_attr *sa; + bool ret = true; + + if (argc < 2) { + printf( + "usage: bitrate set [all SPEEDSPEC] SPEEDSPEC [ SPEEDSPEC ...]\n" + "SPEEDSPEC: [inout ] [in ] [out ]\n" + ); + ret = false; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + ports = malloc(2 * sa->ports * sizeof(int)); + + /* get defaults if present */ + if (strcmp(argv[k], "all") == 0) { + k++; + k += bitrate_analyse(argc - k, &argv[k], defs); + } + + /* apply defaults */ + for (i = 0; i < sa->ports; i++) + memcpy(&ports[2 * i], defs, sizeof(defs)); + + /* get ports specifics */ + while (k < argc) { + p = strtol(argv[k++], NULL, 0) - 1; + if (p >= 0 && p ports) + k += bitrate_analyse(argc - k, &argv[k], &ports[2 * p]); + } + + /* send it to the switch */ + i = ngadmin_setBitrateLimits(nga, ports); + printErrCode(i); + +end: + free(ports); + + return ret; } - -bool do_bitrate_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i, ret=true, *ports=NULL; - const struct swi_attr *sa; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - - ports=malloc(2*sa->ports*sizeof(int)); - if ( (i=ngadmin_getBitrateLimits(nga, ports))!=ERR_OK ) { - printErrCode(i); - ret=false; - goto end; - } - - for (i=0; iports; ++i) { - printf("port %i: in %s, out %s\n", i+1, bitrates[ports[2*i+0]], bitrates[ports[2*i+1]]); - } - - end: - free(ports); - - return ret; - +bool do_bitrate_show (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i, ret = true, *ports = NULL; + const struct swi_attr *sa; + + + if (argc > 0) { + printf("this command takes no argument\n"); + ret = false; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + + ports = malloc(2 * sa->ports * sizeof(int)); + i = ngadmin_getBitrateLimits(nga, ports); + if (i != ERR_OK) { + printErrCode(i); + ret = false; + goto end; + } + + for (i = 0; i < sa->ports; i++) + printf("port %i: in %s, out %s\n", i + 1, bitrates[ports[2 * i + 0]], bitrates[ports[2 * i + 1]]); + +end: + free(ports); + + return ret; }