]> git.sur5r.net Git - ngadmin/blob - cli/com_bitrate.c
Cli: refactor, change coding style
[ngadmin] / cli / com_bitrate.c
1
2 #include "commands.h"
3
4
5 /* helper function to analyse bitrate speed specifications */
6 static int bitrate_analyse (int nb, const char **com, int *ports)
7 {
8         int i = 0, s;
9         
10         
11         while (i < nb - 1) {
12                 s = parseBitrate(com[i + 1]);
13                 if (strcmp(com[i], "inout") == 0) {
14                         ports[0] = s;
15                         ports[1] = s;
16                 } else if (strcmp(com[i], "in") == 0) {
17                         ports[0] = s;
18                 } else if (strcmp(com[i], "out") == 0) {
19                         ports[1] = s;
20                 } else {
21                         break;
22                 }
23                 i += 2;
24         }
25         
26         
27         return i;
28 }
29
30
31 bool do_bitrate_set (int nb, const char **com, struct ngadmin *nga)
32 {
33         int i, k = 0, defs[] = {12, 12}, p, *ports = NULL;
34         const struct swi_attr *sa;
35         bool ret = true;
36         
37         if (nb < 2) {
38                 printf(
39                 "Usage: bitrate set [all SPEEDSPEC] <port1> SPEEDSPEC [<port2> SPEEDSPEC ...]\n"
40                 "SPEEDSPEC: [inout <speed>] [in <ispeed>] [out <ospeed>]\n"
41                 );
42                 ret = false;
43                 goto end;
44         }
45         
46         sa = ngadmin_getCurrentSwitch(nga);
47         if (sa == NULL) {
48                 printf("must be logged\n");
49                 ret = false;
50                 goto end;
51         }
52         
53         ports = malloc(2 * sa->ports * sizeof(int));
54         
55         /* get defaults if present */
56         if (strcmp(com[k], "all") == 0) {
57                 k++;
58                 k += bitrate_analyse(nb-k, &com[k], defs);
59         }
60         
61         /* apply defaults */
62         for (i = 0; i < sa->ports; i++)
63                 memcpy(&ports[2 * i], defs, sizeof(defs));
64         
65         /* get ports specifics */
66         while (k < nb) {
67                 p = strtol(com[k++], NULL, 0) - 1;
68                 if (p >= 0 &&  p <sa->ports)
69                         k += bitrate_analyse(nb - k, &com[k], &ports[2 * p]);
70         }
71         
72         /* send it to the switch */
73         i = ngadmin_setBitrateLimits(nga, ports);
74         printErrCode(i);
75         
76 end:
77         free(ports);
78         
79         return ret;
80 }
81
82
83 bool do_bitrate_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
84 {
85         int i, ret = true, *ports = NULL;
86         const struct swi_attr *sa;
87         
88         
89         sa = ngadmin_getCurrentSwitch(nga);
90         if (sa == NULL) {
91                 printf("must be logged\n");
92                 ret = false;
93                 goto end;
94         }
95         
96         
97         ports = malloc(2 * sa->ports * sizeof(int));
98         i = ngadmin_getBitrateLimits(nga, ports);
99         if (i != ERR_OK) {
100                 printErrCode(i);
101                 ret = false;
102                 goto end;
103         }
104         
105         for (i = 0; i < sa->ports; i++)
106                 printf("port %i: in %s, out %s\n", i + 1, bitrates[ports[2 * i + 0]], bitrates[ports[2 * i + 1]]);
107         
108 end:
109         free(ports);
110         
111         return ret;
112 }
113
114
115