]> git.sur5r.net Git - ngadmin/blob - lib/src/qos.c
Raw: refactor attribute encoding and decoding
[ngadmin] / lib / src / qos.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_getQOSMode (struct ngadmin *nga, int *s)
12 {
13         List *attr;
14         struct attr *at;
15         int ret = ERR_OK;
16         
17         
18         if (nga == NULL || s == NULL)
19                 return ERR_INVARG;
20         else if (nga->current == NULL)
21                 return ERR_NOTLOG;
22         
23         
24         attr = createEmptyList();
25         pushBackList(attr, newEmptyAttr(ATTR_QOS_TYPE));
26         ret = readRequest(nga, attr);
27         if (ret != ERR_OK)
28                 goto end;
29         
30         filterAttributes(attr, ATTR_QOS_TYPE, ATTR_END);
31         
32         *s = 0;
33         
34         if (attr->first != NULL) {
35                 at = attr->first->data;
36                 *s = *(char*)at->data;
37         }
38         
39         
40 end:
41         destroyList(attr, (void(*)(void*))freeAttr);
42         
43         
44         return ret;
45 }
46
47
48 int ngadmin_setQOSMode (struct ngadmin *nga, int s)
49 {
50         List *attr;
51         
52         
53         attr = createEmptyList();
54         pushBackList(attr, newByteAttr(ATTR_QOS_TYPE, s));
55         
56         
57         return writeRequest(nga, attr);
58 }
59
60
61 int ngadmin_getQOSValues (struct ngadmin *nga, char *ports)
62 {
63         List *attr;
64         ListNode *ln;
65         struct attr *at;
66         int ret = ERR_OK, port;
67         struct attr_qos *aq;
68         struct swi_attr *sa;
69         
70         
71         if (nga == NULL || ports == NULL)
72                 return ERR_INVARG;
73         
74         sa = nga->current;
75         if (sa == NULL)
76                 return ERR_NOTLOG;
77         
78         attr = createEmptyList();
79         pushBackList(attr, newEmptyAttr(ATTR_QOS_CONFIG));
80         ret = readRequest(nga, attr);
81         if (ret < 0)
82                 goto end;
83         
84         filterAttributes(attr, ATTR_QOS_CONFIG, ATTR_END);
85         
86         for (port = 0; port < sa->ports; port++)
87                 ports[port] = PRIO_UNSPEC;
88         
89         for (ln = attr->first; ln != NULL; ln = ln->next) {
90                 at = ln->data;
91                 aq = at->data;
92                 if (aq->port <= sa->ports)
93                         ports[aq->port - 1] = aq->prio;
94         }
95         
96         
97 end:
98         destroyList(attr, (void(*)(void*))freeAttr);
99         
100         
101         return ret;
102 }
103
104
105 int ngadmin_setQOSValues (struct ngadmin *nga, const char *ports)
106 {
107         List *attr;
108         int port;
109         struct attr_qos *aq;
110         
111         
112         if (nga == NULL || ports == NULL)
113                 return ERR_INVARG;
114         else if (nga->current == NULL)
115                 return ERR_NOTLOG;
116         
117         
118         attr = createEmptyList();
119         
120         for (port = 0; port < nga->current->ports; port++) {
121                 if (ports[port] >= PRIO_HIGH && ports[port] <= PRIO_LOW) {
122                         aq = malloc(sizeof(struct attr_qos));
123                         if (aq == NULL)
124                                 return ERR_MEM;
125                         aq->port = port + 1;
126                         aq->prio = ports[port];
127                         pushBackList(attr, newAttr(ATTR_QOS_CONFIG, sizeof(struct attr_qos), aq));
128                 }
129         }
130         
131         
132         return writeRequest(nga, attr);
133 }
134
135