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