]> git.sur5r.net Git - ngadmin/blob - lib/src/ports.c
lib: remove unused variable
[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         struct swi_attr *sa;
19         
20         
21         if (nga == NULL || ports == NULL)
22                 return ERR_INVARG;
23         
24         sa = nga->current;
25         if (sa == NULL)
26                 return ERR_NOTLOG;
27         
28         attr = createEmptyList();
29         pushBackList(attr, newEmptyAttr(ATTR_PORT_STATUS));
30         ret = readRequest(nga, attr);
31         if (ret != ERR_OK)
32                 goto end;
33         
34         filterAttributes(attr, ATTR_PORT_STATUS, ATTR_END);
35         
36         memset(ports, SPEED_UNK, sa->ports);
37         
38         for (ln = attr->first; ln != NULL; ln = ln->next) {
39                 at = ln->data;
40                 ps = at->data;
41                 if (at->size == 0) {
42                         ret = ERR_BADREPLY;
43                         goto end;
44                 }
45                 if (ps->port <= sa->ports)
46                         ports[ps->port - 1] = ps->status;
47         }
48         
49 end:
50         destroyList(attr, (void(*)(void*))freeAttr);
51         
52         
53         return ret;
54 }
55
56
57 int ngadmin_getPortsStatistics (struct ngadmin *nga, struct port_stats *ps)
58 {
59         List *attr;
60         ListNode *ln;
61         struct attr *at;
62         int ret = ERR_OK;
63         struct attr_port_stat *aps;
64         struct swi_attr *sa;
65         
66         
67         if (nga == NULL || ps == NULL)
68                 return ERR_INVARG;
69         
70         sa = nga->current;
71         if (sa == NULL)
72                 return ERR_NOTLOG;
73         
74         attr = createEmptyList();
75         pushBackList(attr, newEmptyAttr(ATTR_PORT_STATISTICS));
76         ret = readRequest(nga, attr);
77         if (ret != ERR_OK)
78                 goto end;
79         
80         filterAttributes(attr, ATTR_PORT_STATISTICS, ATTR_END);
81         
82         memset(ps, 0, sa->ports * sizeof(struct port_stats));
83         
84         for (ln = attr->first; ln != NULL; ln = ln->next) {
85                 at = ln->data;
86                 aps = at->data;
87                 if (at->size == 0) {
88                         ret = ERR_BADREPLY;
89                         goto end;
90                 }
91                 if (aps->port <= sa->ports) {
92                         ps[aps->port -1].recv = aps->recv;
93                         ps[aps->port -1].sent = aps->sent;
94                         ps[aps->port -1].crc = aps->crc;
95                 }
96         }
97         
98 end:
99         destroyList(attr, (void(*)(void*))freeAttr);
100         
101         
102         return ret;
103 }
104
105
106 int ngadmin_resetPortsStatistics (struct ngadmin *nga)
107 {
108         List *attr;
109         
110         
111         attr = createEmptyList();
112         pushBackList(attr, newByteAttr(ATTR_STATS_RESET, 1));
113         
114         
115         return writeRequest(nga, attr);
116 }
117
118
119 int ngadmin_cabletest (struct ngadmin *nga, struct cabletest *ct, int nb)
120 {
121         List *attr;
122         ListNode *ln;
123         struct attr *at;
124         int ret = ERR_OK, i;
125         struct attr_cabletest_do *acd;
126         struct attr_cabletest_result *acr;
127         
128         
129         if (nga == NULL || ct == NULL)
130                 return ERR_INVARG;
131         else if (nga->current == NULL)
132                 return ERR_NOTLOG;
133         
134         
135         attr = createEmptyList();
136         
137         for (i = 0; i < nb; i++) {
138                 
139                 acd = malloc(sizeof(struct attr_cabletest_do));
140                 if (acd == NULL)
141                         return ERR_MEM;
142                 acd->port = ct[i].port;
143                 acd->action = 1;
144                 pushBackList(attr, newAttr(ATTR_CABLETEST_DO, sizeof(struct attr_cabletest_do), acd));
145                 
146                 ret = writeRequest(nga, attr);
147                 attr = NULL;
148                 if (ret < 0)
149                         goto end;
150                 
151                 /* the list is destroyed by writeRequest, so we need to recreate it */
152                 attr = createEmptyList();
153                 pushBackList(attr, newByteAttr(ATTR_CABLETEST_RESULT, ct[i].port));
154                 ret = readRequest(nga, attr);
155                 if (ret < 0)
156                         goto end;
157                 
158                 filterAttributes(attr, ATTR_CABLETEST_RESULT, ATTR_END);
159                 
160                 for (ln = attr->first; ln != NULL; ln = ln->next) {
161                         at = ln->data;
162                         acr = at->data;
163                         if (at->size != sizeof(struct attr_cabletest_result)) {
164                                 ret = ERR_BADREPLY;
165                                 goto end;
166                         }
167                         if (acr->port == ct[i].port) {
168                                 ct[i].v1 = acr->v1;
169                                 ct[i].v2 = acr->v2;
170                                 break;
171                         }
172                 }
173                 
174                 /* just empty the list, it will be used at next iteration */
175                 clearList(attr, (void(*)(void*))freeAttr);
176         }
177         
178         
179 end:
180         destroyList(attr, (void(*)(void*))freeAttr);
181         
182         
183         return ret;
184 }
185
186
187 int ngadmin_getLoopDetectionState (struct ngadmin *nga, int *s)
188 {
189         List *attr;
190         struct attr *at;
191         int ret = ERR_OK;
192         
193         
194         if (nga == NULL || s == NULL)
195                 return ERR_INVARG;
196         else if (nga->current == NULL)
197                 return ERR_NOTLOG;
198         
199         attr = createEmptyList();
200         pushBackList(attr, newEmptyAttr(ATTR_LOOP_DETECT));
201         ret = readRequest(nga, attr);
202         if (ret != ERR_OK)
203                 goto end;
204         
205         filterAttributes(attr, ATTR_LOOP_DETECT, ATTR_END);
206         
207         *s = 0;
208         
209         if (attr->first == NULL) {
210                 ret = ERR_BADREPLY;
211                 goto end;
212         }
213         at = attr->first->data;
214         if (at->size != 1) {
215                 ret = ERR_BADREPLY;
216                 goto end;
217         }
218         *s = *(char *)at->data;
219         
220         
221 end:
222         destroyList(attr, (void(*)(void*))freeAttr);
223         
224         
225         return ret;
226 }
227
228
229 int ngadmin_setLoopDetectionState (struct ngadmin *nga, int s)
230 {
231         List *attr;
232         
233         
234         attr = createEmptyList();
235         pushBackList(attr, newByteAttr(ATTR_LOOP_DETECT, s != 0));
236         
237         
238         return writeRequest(nga, attr);
239 }
240
241