]> git.sur5r.net Git - ngadmin/blob - lib/src/ports.c
Raw: refactor attribute encoding and decoding
[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 (ps->port <= sa->ports)
42                         ports[ps->port - 1] = ps->status;
43         }
44         
45 end:
46         destroyList(attr, (void(*)(void*))freeAttr);
47         
48         
49         return ret;
50 }
51
52
53 int ngadmin_getPortsStatistics (struct ngadmin *nga, struct port_stats *ps)
54 {
55         List *attr;
56         ListNode *ln;
57         struct attr *at;
58         int ret = ERR_OK;
59         struct attr_port_stat *aps;
60         struct swi_attr *sa;
61         
62         
63         if (nga == NULL || ps == NULL)
64                 return ERR_INVARG;
65         
66         sa = nga->current;
67         if (sa == NULL)
68                 return ERR_NOTLOG;
69         
70         attr = createEmptyList();
71         pushBackList(attr, newEmptyAttr(ATTR_PORT_STATISTICS));
72         ret = readRequest(nga, attr);
73         if (ret != ERR_OK)
74                 goto end;
75         
76         filterAttributes(attr, ATTR_PORT_STATISTICS, ATTR_END);
77         
78         memset(ps, 0, sa->ports * sizeof(struct port_stats));
79         
80         for (ln = attr->first; ln != NULL; ln = ln->next) {
81                 at = ln->data;
82                 aps = at->data;
83                 if (aps->port <= sa->ports) {
84                         ps[aps->port -1].recv = aps->recv;
85                         ps[aps->port -1].sent = aps->sent;
86                         ps[aps->port -1].crc = aps->crc;
87                 }
88         }
89         
90 end:
91         destroyList(attr, (void(*)(void*))freeAttr);
92         
93         
94         return ret;
95 }
96
97
98 int ngadmin_resetPortsStatistics (struct ngadmin *nga)
99 {
100         List *attr;
101         
102         
103         attr = createEmptyList();
104         pushBackList(attr, newByteAttr(ATTR_STATS_RESET, 1));
105         
106         
107         return writeRequest(nga, attr);
108 }
109
110
111 int ngadmin_cabletest (struct ngadmin *nga, struct cabletest *ct, int nb)
112 {
113         List *attr;
114         ListNode *ln;
115         struct attr *at;
116         int ret = ERR_OK, i;
117         struct attr_cabletest_do *acd;
118         struct attr_cabletest_result *acr;
119         
120         
121         if (nga == NULL || ct == NULL)
122                 return ERR_INVARG;
123         else if (nga->current == NULL)
124                 return ERR_NOTLOG;
125         
126         
127         attr = createEmptyList();
128         
129         for (i = 0; i < nb; i++) {
130                 
131                 acd = malloc(sizeof(struct attr_cabletest_do));
132                 if (acd == NULL)
133                         return ERR_MEM;
134                 acd->port = ct[i].port;
135                 acd->action = 1;
136                 pushBackList(attr, newAttr(ATTR_CABLETEST_DO, sizeof(struct attr_cabletest_do), acd));
137                 
138                 ret = writeRequest(nga, attr);
139                 attr = NULL;
140                 if (ret < 0)
141                         goto end;
142                 
143                 /* the list is destroyed by writeRequest, so we need to recreate it */
144                 attr = createEmptyList();
145                 pushBackList(attr, newByteAttr(ATTR_CABLETEST_RESULT, ct[i].port));
146                 ret = readRequest(nga, attr);
147                 if (ret < 0)
148                         goto end;
149                 
150                 filterAttributes(attr, ATTR_CABLETEST_RESULT, ATTR_END);
151                 
152                 for (ln = attr->first; ln != NULL; ln = ln->next) {
153                         at = ln->data;
154                         acr = at->data;
155                         if (at->size == sizeof(struct attr_cabletest_result) && acr->port == ct[i].port) {
156                                 ct[i].v1 = acr->v1;
157                                 ct[i].v2 = acr->v2;
158                                 break;
159                         }
160                 }
161                 
162                 /* just empty the list, it will be used at next iteration */
163                 clearList(attr, (void(*)(void*))freeAttr);
164         }
165         
166         
167 end:
168         destroyList(attr, (void(*)(void*))freeAttr);
169         
170         
171         return ret;
172 }
173
174