]> git.sur5r.net Git - ngadmin/blob - lib/src/ports.c
0b4afc2e20148fddcbcc8339b58055890aff90a8
[ngadmin] / lib / src / ports.c
1
2 #include <ngadmin.h>
3
4 #include <nsdp/attr.h>
5 #include <nsdp/protocol.h>
6
7 #include "lib.h"
8 #include "network.h"
9
10
11 int ngadmin_getPortsStatus (struct ngadmin *nga, unsigned char *ports)
12 {
13         List *attr;
14         ListNode *ln;
15         struct attr *at;
16         int ret = ERR_OK;
17         struct attr_port_status *ps;
18         
19         
20         if (nga == NULL || ports == NULL)
21                 return ERR_INVARG;
22         else if (nga->current == NULL)
23                 return ERR_NOTLOG;
24         
25         
26         attr = createEmptyList();
27         pushBackList(attr, newEmptyAttr(ATTR_PORT_STATUS));
28         ret = readRequest(nga, attr);
29         if (ret != ERR_OK)
30                 goto end;
31         
32         filterAttributes(attr, ATTR_PORT_STATUS, ATTR_END);
33         
34         memset(ports, SPEED_UNK, nga->current->ports);
35         
36         for (ln = attr->first; ln != NULL; ln = ln->next) {
37                 at = ln->data;
38                 ps = at->data;
39                 ports[ps->port - 1] = ps->status;
40         }
41         
42 end:
43         destroyList(attr, (void(*)(void*))freeAttr);
44         
45         
46         return ret;
47 }
48
49
50
51 int ngadmin_getPortsStatistics (struct ngadmin *nga, struct port_stats *ps)
52 {
53         List *attr;
54         ListNode *ln;
55         struct attr *at;
56         int ret = ERR_OK;
57         struct attr_port_stat *aps;
58         
59         
60         if (nga == NULL || ps == NULL)
61                 return ERR_INVARG;
62         else if (nga->current == NULL)
63                 return ERR_NOTLOG;
64         
65         attr = createEmptyList();
66         pushBackList(attr, newEmptyAttr(ATTR_PORT_STATISTICS));
67         ret = readRequest(nga, attr);
68         if (ret != ERR_OK)
69                 goto end;
70         
71         filterAttributes(attr, ATTR_PORT_STATISTICS, ATTR_END);
72         
73         memset(ps, 0, nga->current->ports * sizeof(struct port_stats));
74         
75         for (ln = attr->first; ln != NULL; ln = ln->next) {
76                 at = ln->data;
77                 aps = at->data;
78                 ps[aps->port -1].recv = aps->recv;
79                 ps[aps->port -1].sent = aps->sent;
80                 ps[aps->port -1].crc = aps->crc;
81         }
82         
83 end:
84         destroyList(attr, (void(*)(void*))freeAttr);
85         
86         
87         return ret;
88 }
89
90
91 int ngadmin_resetPortsStatistics (struct ngadmin *nga)
92 {
93         List *attr;
94         
95         
96         attr = createEmptyList();
97         pushBackList(attr, newByteAttr(ATTR_STATS_RESET, 1));
98         
99         
100         return writeRequest(nga, attr);
101 }
102
103
104
105 int ngadmin_cabletest (struct ngadmin *nga, struct cabletest *ct, int nb)
106 {
107         List *attr;
108         ListNode *ln;
109         struct attr *at;
110         int ret = ERR_OK, i;
111         struct attr_cabletest_do *acd;
112         struct attr_cabletest_result *acr;
113         
114         
115         if (nga == NULL || ct == NULL)
116                 return ERR_INVARG;
117         else if (nga->current == NULL)
118                 return ERR_NOTLOG;
119         
120         
121         attr = createEmptyList();
122         
123         for (i = 0; i < nb; i++) {
124                 
125                 acd = malloc(sizeof(struct attr_cabletest_do));
126                 if (acd == NULL)
127                         return ERR_MEM;
128                 acd->port = ct[i].port;
129                 acd->action = 1;
130                 pushBackList(attr, newAttr(ATTR_CABLETEST_DO, sizeof(struct attr_cabletest_do), acd));
131                 
132                 ret = writeRequest(nga, attr);
133                 attr = NULL;
134                 if (ret < 0)
135                         goto end;
136                 
137                 /* the list is destroyed by writeRequest, so we need to recreate it */
138                 attr = createEmptyList();
139                 pushBackList(attr, newByteAttr(ATTR_CABLETEST_RESULT, ct[i].port));
140                 ret = readRequest(nga, attr);
141                 if (ret < 0)
142                         goto end;
143                 
144                 filterAttributes(attr, ATTR_CABLETEST_RESULT, ATTR_END);
145                 
146                 for (ln = attr->first; ln != NULL; ln = ln->next) {
147                         at = ln->data;
148                         acr = at->data;
149                         if (at->size == sizeof(struct attr_cabletest_result) && acr->port == ct[i].port) {
150                                 ct[i].v1 = acr->v1;
151                                 ct[i].v2 = acr->v2;
152                                 break;
153                         }
154                 }
155                 
156                 /* just empty the list, it will be used at next iteration */
157                 clearList(attr, (void(*)(void*))freeAttr);
158         }
159         
160         
161 end:
162         destroyList(attr, (void(*)(void*))freeAttr);
163         
164         
165         return ret;
166 }
167
168