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