]> git.sur5r.net Git - ngadmin/blob - cli/com_ports.c
Let commands handle themselves absence of arguments
[ngadmin] / cli / com_ports.c
1
2 #include "commands.h"
3
4
5 bool do_ports_state (int argc, const char **argv UNUSED, struct ngadmin *nga)
6 {
7         int i;
8         const struct swi_attr *sa;
9         unsigned char *ports = NULL;
10         bool ret = true;
11         
12         
13         if (argc > 0) {
14                 printf("this command takes no argument\n");
15                 ret = false;
16                 goto end;
17         }
18         
19         sa = ngadmin_getCurrentSwitch(nga);
20         if (sa == NULL) {
21                 printf("must be logged\n");
22                 ret = false;
23                 goto end;
24         }
25         
26         ports = malloc(sa->ports * sizeof(unsigned char));
27         i = ngadmin_getPortsStatus(nga, ports);
28         if (i < 0) {
29                 printErrCode(i);
30                 ret = false;
31                 goto end;
32         }
33         
34         for (i = 0; i < sa->ports; i++) {
35                 printf("port %i: ", i + 1);
36                 switch (ports[i]) {
37                 
38                 case 0:
39                         printf("down");
40                         break;
41                 
42                 case SPEED_10:
43                         printf("up, 10M");
44                         break;
45                 
46                 case SPEED_100:
47                         printf("up, 100M");
48                         break;
49                 
50                 case SPEED_1000:
51                         printf("up, 1000M");
52                         break;
53                 
54                 default:
55                         printf("unknown (%i)", ports[i]);
56                 }
57                 putchar('\n');
58         }
59         
60 end:
61         free(ports);
62         
63         
64         return ret;
65 }
66
67
68 bool do_ports_statistics_reset (int argc, const char **argv UNUSED, struct ngadmin *nga)
69 {
70         int i;
71         
72         
73         if (argc > 0) {
74                 printf("this command takes no argument\n");
75                 return false;
76         }
77         
78         if (ngadmin_getCurrentSwitch(nga) == NULL) {
79                 printf("must be logged\n");
80                 return false;
81         }
82         
83         i = ngadmin_resetPortsStatistics(nga);
84         printErrCode(i);
85         
86         return true;
87 }
88
89
90 bool do_ports_statistics_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
91 {
92         int i;
93         const struct swi_attr *sa;
94         bool ret = true;
95         struct port_stats *ps = NULL;
96         
97         
98         if (argc > 0) {
99                 printf("this command takes no argument\n");
100                 ret = false;
101                 goto end;
102         }
103         
104         sa = ngadmin_getCurrentSwitch(nga);
105         if (sa == NULL) {
106                 printf("must be logged\n");
107                 ret = false;
108                 goto end;
109         }
110         
111         ps = calloc(sa->ports, sizeof(struct port_stats));
112         i = ngadmin_getPortsStatistics(nga, ps);
113         if (i < 0) {
114                 printErrCode(i);
115                 ret = false;
116                 goto end;
117         }
118         
119         printf("Port\tReceived\tSent\tCRC errors\n");
120         for (i = 0; i < sa->ports; i++)
121                 printf("% 4i%12llu%12llu%14llu\n", i + 1, ps[i].recv, ps[i].sent, ps[i].crc);
122         
123 end:
124         free(ps);
125         
126         return ret;
127 }
128
129