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