]> git.sur5r.net Git - ngadmin/blob - lib/src/bitrate.c
Raw: refactor attribute encoding and decoding
[ngadmin] / lib / src / bitrate.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_getStormFilterState (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_STORM_ENABLE));
26         ret = readRequest(nga, attr);
27         if (ret != ERR_OK)
28                 goto end;
29         
30         filterAttributes(attr, ATTR_STORM_ENABLE, 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_setStormFilterState (struct ngadmin *nga, int s)
49 {
50         List *attr;
51         
52         
53         attr = createEmptyList();
54         pushBackList(attr, newByteAttr(ATTR_STORM_ENABLE, s != 0));
55         
56         
57         return writeRequest(nga, attr);
58 }
59
60
61 int ngadmin_getStormFilterValues (struct ngadmin *nga, int *ports)
62 {
63         List *attr;
64         ListNode *ln;
65         struct attr *at;
66         int ret = ERR_OK, port;
67         struct attr_bitrate *sb;
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         
79         attr = createEmptyList();
80         pushBackList(attr, newEmptyAttr(ATTR_STORM_BITRATE));
81         ret = readRequest(nga, attr);
82         if (ret != ERR_OK)
83                 goto end;
84         
85         filterAttributes(attr, ATTR_STORM_BITRATE, ATTR_END);
86         
87         for (port = 0; port < sa->ports; port++)
88                 ports[port] = BITRATE_UNSPEC;
89         
90         for (ln = attr->first; ln != NULL; ln = ln->next) {
91                 at = ln->data;
92                 sb = at->data;
93                 if (sb->port <= sa->ports)
94                         ports[sb->port - 1] = sb->bitrate;
95         }
96         
97         
98 end:
99         destroyList(attr, (void(*)(void*))freeAttr);
100         
101         
102         return ret;
103 }
104
105
106 int ngadmin_setStormFilterValues (struct ngadmin *nga, const int *ports)
107 {
108         List *attr;
109         int port;
110         struct attr_bitrate *sb;
111         
112         
113         if (nga == NULL || ports == NULL)
114                 return ERR_INVARG;
115         else if (nga->current == NULL)
116                 return ERR_NOTLOG;
117         
118         
119         attr = createEmptyList();
120         
121         for (port = 0; port < nga->current->ports; port++) {
122                 if (ports[port] != BITRATE_UNSPEC) {
123                         sb = malloc(sizeof(struct attr_bitrate));
124                         if (sb == NULL)
125                                 return ERR_MEM;
126                         sb->port = port + 1;
127                         sb->bitrate = ports[port];
128                         pushBackList(attr, newAttr(ATTR_STORM_BITRATE, sizeof(struct attr_bitrate), sb));
129                 }
130         }
131         
132         return writeRequest(nga, attr);
133 }
134
135
136 int ngadmin_getBitrateLimits (struct ngadmin *nga, int *ports)
137 {
138         List *attr;
139         ListNode *ln;
140         struct attr *at;
141         int ret = ERR_OK, port;
142         struct attr_bitrate *pb;
143         struct swi_attr *sa;
144         
145         
146         if (nga == NULL || ports == NULL)
147                 return ERR_INVARG;
148         
149         sa = nga->current;
150         if (sa == NULL)
151                 return ERR_NOTLOG;
152         
153         
154         attr = createEmptyList();
155         pushBackList(attr, newEmptyAttr(ATTR_BITRATE_INPUT));
156         pushBackList(attr, newEmptyAttr(ATTR_BITRATE_OUTPUT));
157         ret = readRequest(nga, attr);
158         if (ret != ERR_OK)
159                 goto end;
160         
161         filterAttributes(attr, ATTR_BITRATE_INPUT, ATTR_BITRATE_OUTPUT, ATTR_END);
162         
163         for (port = 0; port < sa->ports; port++) {
164                 ports[2 * port + 0] = BITRATE_UNSPEC;
165                 ports[2 * port + 1] = BITRATE_UNSPEC;
166         }
167         
168         for (ln = attr->first; ln != NULL; ln = ln->next) {
169                 at = ln->data;
170                 pb = at->data;
171                 if (pb->port > sa->ports)
172                         continue;
173                 else if (at->attr == ATTR_BITRATE_INPUT)
174                         ports[(pb->port - 1) * 2 + 0] = pb->bitrate;
175                 else
176                         ports[(pb->port - 1) * 2 + 1] = pb->bitrate;
177         }
178         
179         
180 end:
181         destroyList(attr, (void(*)(void*))freeAttr);
182         
183         return ret;
184 }
185
186
187 int ngadmin_setBitrateLimits (struct ngadmin *nga, const int *ports)
188 {
189         List *attr;
190         int port;
191         struct attr_bitrate *pb;
192         
193         
194         if (nga == NULL || ports == NULL)
195                 return ERR_INVARG;
196         else if (nga->current == NULL)
197                 return ERR_NOTLOG;
198         
199         
200         attr = createEmptyList();
201         
202         for (port = 0; port < nga->current->ports; port++) {
203                 if (ports[2 * port + 0] >= BITRATE_NOLIMIT && ports[2 * port + 0] <= BITRATE_512M) {
204                         pb = malloc(sizeof(struct attr_bitrate));
205                         if (pb == NULL)
206                                 return ERR_MEM;
207                         pb->port = port + 1;
208                         pb->bitrate = ports[2 * port + 0];
209                         pushBackList(attr, newAttr(ATTR_BITRATE_INPUT, sizeof(struct attr_bitrate), pb));
210                 }
211                 if (ports[2 * port + 1] >= BITRATE_NOLIMIT && ports[2 * port + 1] <= BITRATE_512M) {
212                         pb = malloc(sizeof(struct attr_bitrate));
213                         if (pb == NULL)
214                                 return ERR_MEM;
215                         pb->port = port + 1;
216                         pb->bitrate = ports[2 * port + 1];
217                         pushBackList(attr, newAttr(ATTR_BITRATE_OUTPUT, sizeof(struct attr_bitrate), pb));
218                 }
219         }
220         
221         
222         return writeRequest(nga, attr);
223 }
224
225