]>
Commit | Line | Data |
---|---|---|
94c98157 AA |
1 | /* -*- mode: c; c-file-style: "openbsd" -*- */ |
2 | /* | |
3 | * Copyright (c) 2015 Vincent Bernat <vincent@bernat.im> | |
4 | * | |
5 | * Permission to use, copy, modify, and/or distribute this software for any | |
6 | * purpose with or without fee is hereby granted, provided that the above | |
7 | * copyright notice and this permission notice appear in all copies. | |
8 | * | |
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
16 | */ | |
17 | ||
18 | #include <stdio.h> | |
19 | #include <stdarg.h> | |
20 | #include <string.h> | |
21 | #include <arpa/inet.h> | |
22 | ||
3fd81d89 JL |
23 | #include "../lldpctl.h" |
24 | #include "../../log.h" | |
25 | #include "../atom.h" | |
26 | #include "../helpers.h" | |
27 | #include "../fixedpoint.h" | |
94c98157 AA |
28 | |
29 | #ifdef ENABLE_LLDPMED | |
30 | ||
31 | static lldpctl_map_t port_med_location_map[] = { | |
32 | { LLDP_MED_LOCFORMAT_COORD, "Coordinates" }, | |
33 | { LLDP_MED_LOCFORMAT_CIVIC, "Civic address" }, | |
34 | { LLDP_MED_LOCFORMAT_ELIN, "ELIN" }, | |
35 | { 0, NULL }, | |
36 | }; | |
37 | ||
38 | static lldpctl_map_t port_med_pow_devicetype_map[] = { | |
39 | { LLDP_MED_POW_TYPE_PSE, "PSE" }, | |
8b549648 | 40 | { LLDP_MED_POW_TYPE_PD, "PD" }, |
94c98157 AA |
41 | { 0, NULL }, |
42 | }; | |
43 | ||
44 | static lldpctl_map_t port_med_pow_source_map[] = { | |
45 | { LLDP_MED_POW_SOURCE_PRIMARY, "Primary Power Source" }, | |
8b549648 VB |
46 | { LLDP_MED_POW_SOURCE_BACKUP, "Backup Power Source / Power Conservation Mode" }, |
47 | { LLDP_MED_POW_SOURCE_PSE, "PSE" }, | |
48 | { LLDP_MED_POW_SOURCE_LOCAL, "Local" }, | |
49 | { LLDP_MED_POW_SOURCE_BOTH, "PSE + Local" }, | |
94c98157 AA |
50 | { 0, NULL }, |
51 | }; | |
52 | ||
53 | static lldpctl_map_t port_med_pow_source_map2[] = { | |
8b549648 | 54 | { 0, "unknown" }, |
94c98157 | 55 | { LLDP_MED_POW_SOURCE_PRIMARY, "primary" }, |
8b549648 VB |
56 | { LLDP_MED_POW_SOURCE_BACKUP, "backup" }, |
57 | { LLDP_MED_POW_SOURCE_PSE, "pse" }, | |
58 | { LLDP_MED_POW_SOURCE_LOCAL, "local" }, | |
59 | { LLDP_MED_POW_SOURCE_BOTH, "both" }, | |
94c98157 AA |
60 | { 0, NULL }, |
61 | }; | |
62 | ||
63 | static struct atom_map port_med_geoid_map = { | |
64 | .key = lldpctl_k_med_location_geoid, | |
65 | .map = { | |
66 | { LLDP_MED_LOCATION_GEOID_WGS84, "WGS84" }, | |
67 | { LLDP_MED_LOCATION_GEOID_NAD83, "NAD83" }, | |
68 | { LLDP_MED_LOCATION_GEOID_NAD83_MLLW, "NAD83/MLLW" }, | |
69 | { 0, NULL }, | |
70 | }, | |
71 | }; | |
72 | ||
73 | static struct atom_map civic_address_type_map = { | |
74 | .key = lldpctl_k_med_civicaddress_type, | |
75 | .map = { | |
76 | { 0, "Language" }, | |
77 | { 1, "Country subdivision" }, | |
78 | { 2, "County" }, | |
79 | { 3, "City" }, | |
80 | { 4, "City division" }, | |
81 | { 5, "Block" }, | |
82 | { 6, "Street" }, | |
83 | { 16, "Direction" }, | |
84 | { 17, "Trailing street suffix" }, | |
85 | { 18, "Street suffix" }, | |
86 | { 19, "Number" }, | |
87 | { 20, "Number suffix" }, | |
88 | { 21, "Landmark" }, | |
89 | { 22, "Additional" }, | |
90 | { 23, "Name" }, | |
91 | { 24, "ZIP" }, | |
92 | { 25, "Building" }, | |
93 | { 26, "Unit" }, | |
94 | { 27, "Floor" }, | |
95 | { 28, "Room" }, | |
96 | { 29, "Place type" }, | |
97 | { 128, "Script" }, | |
98 | { 0, NULL }, | |
99 | }, | |
100 | }; | |
101 | ||
8b549648 | 102 | static struct atom_map port_med_policy_map = { .key = lldpctl_k_med_policy_type, |
94c98157 | 103 | .map = { |
8b549648 VB |
104 | { LLDP_MED_APPTYPE_VOICE, "Voice" }, |
105 | { LLDP_MED_APPTYPE_VOICESIGNAL, "Voice Signaling" }, | |
106 | { LLDP_MED_APPTYPE_GUESTVOICE, "Guest Voice" }, | |
107 | { LLDP_MED_APPTYPE_GUESTVOICESIGNAL, "Guest Voice Signaling" }, | |
108 | { LLDP_MED_APPTYPE_SOFTPHONEVOICE, "Softphone Voice" }, | |
109 | { LLDP_MED_APPTYPE_VIDEOCONFERENCE, "Video Conferencing" }, | |
110 | { LLDP_MED_APPTYPE_VIDEOSTREAM, "Streaming Video" }, | |
111 | { LLDP_MED_APPTYPE_VIDEOSIGNAL, "Video Signaling" }, | |
112 | { 0, NULL }, | |
113 | } }; | |
94c98157 AA |
114 | |
115 | static struct atom_map port_med_policy_prio_map = { | |
116 | .key = lldpctl_k_med_policy_priority, | |
117 | .map = { | |
e7f83dc0 | 118 | { 1, "Background" }, |
e7f83dc0 | 119 | { 0, "Best effort" }, |
c56e4067 VB |
120 | { 2, "Excellent effort" }, |
121 | { 3, "Critical applications" }, | |
122 | { 4, "Video" }, | |
123 | { 5, "Voice" }, | |
124 | { 6, "Internetwork control" }, | |
94c98157 AA |
125 | { 7, "Network control" }, |
126 | { 0, NULL }, | |
127 | }, | |
128 | }; | |
129 | ||
130 | static struct atom_map port_med_pow_priority_map = { | |
131 | .key = lldpctl_k_med_power_priority, | |
132 | .map = { | |
133 | { 0, "unknown" }, | |
134 | { LLDP_MED_POW_PRIO_CRITICAL, "critical" }, | |
135 | { LLDP_MED_POW_PRIO_HIGH, "high" }, | |
136 | { LLDP_MED_POW_PRIO_LOW, "low" }, | |
137 | { 0, NULL }, | |
138 | }, | |
139 | }; | |
140 | ||
8b549648 VB |
141 | ATOM_MAP_REGISTER(port_med_geoid_map, 7); |
142 | ATOM_MAP_REGISTER(civic_address_type_map, 8); | |
143 | ATOM_MAP_REGISTER(port_med_policy_map, 9); | |
144 | ATOM_MAP_REGISTER(port_med_policy_prio_map, 10); | |
94c98157 AA |
145 | ATOM_MAP_REGISTER(port_med_pow_priority_map, 11); |
146 | ||
8b549648 | 147 | static lldpctl_atom_iter_t * |
94c98157 AA |
148 | _lldpctl_atom_iter_med_policies_list(lldpctl_atom_t *atom) |
149 | { | |
150 | int i; | |
151 | struct _lldpctl_atom_any_list_t *vlist = | |
152 | (struct _lldpctl_atom_any_list_t *)atom; | |
153 | for (i = 0; i < LLDP_MED_APPTYPE_LAST; i++) | |
154 | vlist->parent->port->p_med_policy[i].index = i; | |
8b549648 | 155 | return (lldpctl_atom_iter_t *)&vlist->parent->port->p_med_policy[0]; |
94c98157 AA |
156 | } |
157 | ||
8b549648 | 158 | static lldpctl_atom_iter_t * |
94c98157 AA |
159 | _lldpctl_atom_next_med_policies_list(lldpctl_atom_t *atom, lldpctl_atom_iter_t *iter) |
160 | { | |
161 | struct lldpd_med_policy *policy = (struct lldpd_med_policy *)iter; | |
162 | if (policy->index == LLDP_MED_APPTYPE_LAST - 1) return NULL; | |
8b549648 | 163 | return (lldpctl_atom_iter_t *)(++policy); |
94c98157 AA |
164 | } |
165 | ||
8b549648 | 166 | static lldpctl_atom_t * |
94c98157 AA |
167 | _lldpctl_atom_value_med_policies_list(lldpctl_atom_t *atom, lldpctl_atom_iter_t *iter) |
168 | { | |
169 | struct _lldpctl_atom_any_list_t *vlist = | |
170 | (struct _lldpctl_atom_any_list_t *)atom; | |
171 | struct lldpd_med_policy *policy = (struct lldpd_med_policy *)iter; | |
172 | return _lldpctl_new_atom(atom->conn, atom_med_policy, vlist->parent, policy); | |
173 | } | |
174 | ||
175 | static int | |
176 | _lldpctl_atom_new_med_policy(lldpctl_atom_t *atom, va_list ap) | |
177 | { | |
178 | struct _lldpctl_atom_med_policy_t *policy = | |
179 | (struct _lldpctl_atom_med_policy_t *)atom; | |
180 | policy->parent = va_arg(ap, struct _lldpctl_atom_port_t *); | |
181 | policy->policy = va_arg(ap, struct lldpd_med_policy *); | |
182 | lldpctl_atom_inc_ref((lldpctl_atom_t *)policy->parent); | |
183 | return 1; | |
184 | } | |
185 | ||
186 | static void | |
187 | _lldpctl_atom_free_med_policy(lldpctl_atom_t *atom) | |
188 | { | |
189 | struct _lldpctl_atom_med_policy_t *policy = | |
190 | (struct _lldpctl_atom_med_policy_t *)atom; | |
191 | lldpctl_atom_dec_ref((lldpctl_atom_t *)policy->parent); | |
192 | } | |
193 | ||
194 | static long int | |
195 | _lldpctl_atom_get_int_med_policy(lldpctl_atom_t *atom, lldpctl_key_t key) | |
196 | { | |
197 | struct _lldpctl_atom_med_policy_t *m = | |
198 | (struct _lldpctl_atom_med_policy_t *)atom; | |
199 | ||
200 | /* Local and remote port */ | |
201 | switch (key) { | |
202 | case lldpctl_k_med_policy_type: | |
203 | return m->policy->type; | |
204 | case lldpctl_k_med_policy_unknown: | |
205 | return m->policy->unknown; | |
206 | case lldpctl_k_med_policy_tagged: | |
207 | return m->policy->tagged; | |
208 | case lldpctl_k_med_policy_vid: | |
209 | return m->policy->vid; | |
210 | case lldpctl_k_med_policy_dscp: | |
211 | return m->policy->dscp; | |
212 | case lldpctl_k_med_policy_priority: | |
213 | return m->policy->priority; | |
214 | default: | |
215 | return SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
216 | } | |
217 | } | |
218 | ||
8b549648 | 219 | static lldpctl_atom_t * |
94c98157 AA |
220 | _lldpctl_atom_set_int_med_policy(lldpctl_atom_t *atom, lldpctl_key_t key, |
221 | long int value) | |
222 | { | |
223 | struct _lldpctl_atom_med_policy_t *m = | |
224 | (struct _lldpctl_atom_med_policy_t *)atom; | |
225 | ||
226 | /* Only local port can be modified */ | |
9da663f7 | 227 | if (!m->parent->local) { |
94c98157 AA |
228 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); |
229 | return NULL; | |
230 | } | |
231 | ||
232 | switch (key) { | |
233 | case lldpctl_k_med_policy_type: | |
234 | /* We let set any policy type, including one whose are not | |
235 | * compatible with the index. If a policy type is set, the index | |
236 | * will be ignored. If a policy type is 0, the index will be | |
237 | * used to know which policy to "erase". */ | |
238 | if (value < 0 || value > LLDP_MED_APPTYPE_LAST) goto bad; | |
239 | m->policy->type = value; | |
240 | return atom; | |
241 | case lldpctl_k_med_policy_unknown: | |
242 | if (value != 0 && value != 1) goto bad; | |
243 | m->policy->unknown = value; | |
244 | return atom; | |
245 | case lldpctl_k_med_policy_tagged: | |
246 | if (value != 0 && value != 1) goto bad; | |
247 | m->policy->tagged = value; | |
248 | return atom; | |
249 | case lldpctl_k_med_policy_vid: | |
250 | if (value < 0 || value > 4094) goto bad; | |
251 | m->policy->vid = value; | |
252 | return atom; | |
253 | case lldpctl_k_med_policy_dscp: | |
254 | if (value < 0 || value > 63) goto bad; | |
255 | m->policy->dscp = value; | |
256 | return atom; | |
257 | case lldpctl_k_med_policy_priority: | |
258 | if (value < 0 || value > 7) goto bad; | |
259 | m->policy->priority = value; | |
260 | return atom; | |
261 | default: | |
262 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
263 | return NULL; | |
264 | } | |
265 | ||
266 | return atom; | |
267 | bad: | |
268 | SET_ERROR(atom->conn, LLDPCTL_ERR_BAD_VALUE); | |
269 | return NULL; | |
270 | } | |
271 | ||
8b549648 | 272 | static const char * |
94c98157 AA |
273 | _lldpctl_atom_get_str_med_policy(lldpctl_atom_t *atom, lldpctl_key_t key) |
274 | { | |
275 | struct _lldpctl_atom_med_policy_t *m = | |
276 | (struct _lldpctl_atom_med_policy_t *)atom; | |
277 | ||
278 | /* Local and remote port */ | |
279 | switch (key) { | |
280 | case lldpctl_k_med_policy_type: | |
281 | return map_lookup(port_med_policy_map.map, m->policy->type); | |
282 | case lldpctl_k_med_policy_priority: | |
283 | return map_lookup(port_med_policy_prio_map.map, m->policy->priority); | |
284 | default: | |
285 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
286 | return NULL; | |
287 | } | |
288 | } | |
289 | ||
8b549648 | 290 | static lldpctl_atom_t * |
94c98157 AA |
291 | _lldpctl_atom_set_str_med_policy(lldpctl_atom_t *atom, lldpctl_key_t key, |
292 | const char *value) | |
293 | { | |
294 | /* Local and remote port */ | |
295 | switch (key) { | |
296 | case lldpctl_k_med_policy_type: | |
297 | return _lldpctl_atom_set_int_med_policy(atom, key, | |
298 | map_reverse_lookup(port_med_policy_map.map, value)); | |
299 | case lldpctl_k_med_policy_priority: | |
300 | return _lldpctl_atom_set_int_med_policy(atom, key, | |
301 | map_reverse_lookup(port_med_policy_prio_map.map, value)); | |
302 | default: | |
303 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
304 | return NULL; | |
305 | } | |
306 | } | |
307 | ||
8b549648 | 308 | static lldpctl_atom_iter_t * |
94c98157 AA |
309 | _lldpctl_atom_iter_med_locations_list(lldpctl_atom_t *atom) |
310 | { | |
311 | int i; | |
312 | struct _lldpctl_atom_any_list_t *vlist = | |
313 | (struct _lldpctl_atom_any_list_t *)atom; | |
314 | for (i = 0; i < LLDP_MED_LOCFORMAT_LAST; i++) | |
315 | vlist->parent->port->p_med_location[i].index = i; | |
8b549648 | 316 | return (lldpctl_atom_iter_t *)&vlist->parent->port->p_med_location[0]; |
94c98157 AA |
317 | } |
318 | ||
8b549648 | 319 | static lldpctl_atom_iter_t * |
94c98157 AA |
320 | _lldpctl_atom_next_med_locations_list(lldpctl_atom_t *atom, lldpctl_atom_iter_t *iter) |
321 | { | |
322 | struct lldpd_med_loc *location = (struct lldpd_med_loc *)iter; | |
323 | if (location->index == LLDP_MED_LOCFORMAT_LAST - 1) return NULL; | |
8b549648 | 324 | return (lldpctl_atom_iter_t *)(++location); |
94c98157 AA |
325 | } |
326 | ||
8b549648 | 327 | static lldpctl_atom_t * |
94c98157 AA |
328 | _lldpctl_atom_value_med_locations_list(lldpctl_atom_t *atom, lldpctl_atom_iter_t *iter) |
329 | { | |
330 | struct _lldpctl_atom_any_list_t *vlist = | |
331 | (struct _lldpctl_atom_any_list_t *)atom; | |
332 | struct lldpd_med_loc *location = (struct lldpd_med_loc *)iter; | |
8b549648 VB |
333 | return _lldpctl_new_atom(atom->conn, atom_med_location, vlist->parent, |
334 | location); | |
94c98157 AA |
335 | } |
336 | ||
337 | static int | |
338 | _lldpctl_atom_new_med_location(lldpctl_atom_t *atom, va_list ap) | |
339 | { | |
340 | struct _lldpctl_atom_med_location_t *location = | |
341 | (struct _lldpctl_atom_med_location_t *)atom; | |
342 | location->parent = va_arg(ap, struct _lldpctl_atom_port_t *); | |
343 | location->location = va_arg(ap, struct lldpd_med_loc *); | |
344 | lldpctl_atom_inc_ref((lldpctl_atom_t *)location->parent); | |
345 | return 1; | |
346 | } | |
347 | ||
348 | static void | |
349 | _lldpctl_atom_free_med_location(lldpctl_atom_t *atom) | |
350 | { | |
351 | struct _lldpctl_atom_med_location_t *location = | |
352 | (struct _lldpctl_atom_med_location_t *)atom; | |
353 | lldpctl_atom_dec_ref((lldpctl_atom_t *)location->parent); | |
354 | } | |
355 | ||
356 | static long int | |
357 | _lldpctl_atom_get_int_med_location(lldpctl_atom_t *atom, lldpctl_key_t key) | |
358 | { | |
359 | struct _lldpctl_atom_med_location_t *m = | |
360 | (struct _lldpctl_atom_med_location_t *)atom; | |
361 | ||
362 | /* Local and remote port */ | |
363 | switch (key) { | |
364 | case lldpctl_k_med_location_format: | |
365 | switch (m->location->format) { | |
366 | case LLDP_MED_LOCFORMAT_COORD: | |
367 | if (m->location->data_len != 16) break; | |
368 | return LLDP_MED_LOCFORMAT_COORD; | |
369 | case LLDP_MED_LOCFORMAT_CIVIC: | |
370 | if ((m->location->data_len < 3) || | |
8b549648 VB |
371 | (m->location->data_len - 1 < m->location->data[0])) |
372 | break; | |
94c98157 AA |
373 | return LLDP_MED_LOCFORMAT_CIVIC; |
374 | case LLDP_MED_LOCFORMAT_ELIN: | |
375 | return LLDP_MED_LOCFORMAT_ELIN; | |
376 | default: | |
377 | return 0; | |
378 | } | |
379 | return SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
380 | case lldpctl_k_med_location_geoid: | |
381 | if (m->location->format != LLDP_MED_LOCFORMAT_COORD) | |
382 | return SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
383 | return m->location->data[15]; | |
384 | case lldpctl_k_med_location_altitude_unit: | |
385 | if (m->location->format != LLDP_MED_LOCFORMAT_COORD) | |
386 | return SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
387 | return (m->location->data[10] & 0xf0) >> 4; | |
388 | default: | |
389 | return SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
390 | } | |
391 | } | |
392 | ||
8b549648 | 393 | static lldpctl_atom_t * |
94c98157 AA |
394 | _lldpctl_atom_set_int_med_location(lldpctl_atom_t *atom, lldpctl_key_t key, |
395 | long int value) | |
396 | { | |
397 | struct _lldpctl_atom_med_location_t *mloc = | |
398 | (struct _lldpctl_atom_med_location_t *)atom; | |
399 | ||
400 | /* Only local port can be modified */ | |
9da663f7 | 401 | if (!mloc->parent->local) { |
94c98157 AA |
402 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); |
403 | return NULL; | |
404 | } | |
405 | ||
406 | switch (key) { | |
407 | case lldpctl_k_med_location_format: | |
408 | switch (value) { | |
8b549648 | 409 | case 0: /* Disabling */ |
94c98157 AA |
410 | case LLDP_MED_LOCFORMAT_COORD: |
411 | mloc->location->format = value; | |
412 | free(mloc->location->data); | |
413 | mloc->location->data = calloc(1, 16); | |
414 | if (mloc->location->data == NULL) { | |
415 | mloc->location->data_len = 0; | |
416 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOMEM); | |
417 | return NULL; | |
418 | } | |
419 | mloc->location->data_len = 16; | |
420 | return atom; | |
421 | case LLDP_MED_LOCFORMAT_CIVIC: | |
422 | mloc->location->format = value; | |
423 | free(mloc->location->data); | |
424 | mloc->location->data = calloc(1, 4); | |
425 | if (mloc->location->data == NULL) { | |
426 | mloc->location->data_len = 0; | |
427 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOMEM); | |
428 | return NULL; | |
429 | } | |
430 | mloc->location->data_len = 4; | |
431 | mloc->location->data[0] = 3; | |
432 | mloc->location->data[1] = 2; /* Client */ | |
433 | mloc->location->data[2] = 'U'; | |
434 | mloc->location->data[3] = 'S'; | |
435 | return atom; | |
436 | case LLDP_MED_LOCFORMAT_ELIN: | |
437 | mloc->location->format = value; | |
438 | free(mloc->location->data); | |
439 | mloc->location->data = NULL; | |
440 | mloc->location->data_len = 0; | |
441 | return atom; | |
8b549648 VB |
442 | default: |
443 | goto bad; | |
94c98157 AA |
444 | } |
445 | case lldpctl_k_med_location_geoid: | |
446 | if (mloc->location->format != LLDP_MED_LOCFORMAT_COORD) goto bad; | |
8b549648 VB |
447 | if (mloc->location->data == NULL || mloc->location->data_len != 16) |
448 | goto bad; | |
94c98157 AA |
449 | switch (value) { |
450 | case 0: | |
451 | case LLDP_MED_LOCATION_GEOID_WGS84: | |
452 | case LLDP_MED_LOCATION_GEOID_NAD83: | |
453 | case LLDP_MED_LOCATION_GEOID_NAD83_MLLW: | |
454 | mloc->location->data[15] = value; | |
455 | return atom; | |
8b549648 VB |
456 | default: |
457 | goto bad; | |
94c98157 AA |
458 | } |
459 | case lldpctl_k_med_location_altitude_unit: | |
460 | if (mloc->location->format != LLDP_MED_LOCFORMAT_COORD) goto bad; | |
8b549648 VB |
461 | if (mloc->location->data == NULL || mloc->location->data_len != 16) |
462 | goto bad; | |
94c98157 AA |
463 | switch (value) { |
464 | case 0: | |
465 | case LLDP_MED_LOCATION_ALTITUDE_UNIT_METER: | |
466 | case LLDP_MED_LOCATION_ALTITUDE_UNIT_FLOOR: | |
467 | mloc->location->data[10] &= 0x0f; | |
468 | mloc->location->data[10] |= value << 4; | |
469 | return atom; | |
8b549648 VB |
470 | default: |
471 | goto bad; | |
94c98157 AA |
472 | } |
473 | default: | |
474 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
475 | return NULL; | |
476 | } | |
477 | ||
478 | return atom; | |
479 | bad: | |
480 | SET_ERROR(atom->conn, LLDPCTL_ERR_BAD_VALUE); | |
481 | return NULL; | |
94c98157 AA |
482 | } |
483 | ||
8b549648 VB |
484 | static const char * |
485 | read_fixed_precision(lldpctl_atom_t *atom, char *buffer, unsigned shift, | |
94c98157 AA |
486 | unsigned intbits, unsigned fltbits, const char *suffix) |
487 | { | |
8b549648 VB |
488 | struct fp_number fp = |
489 | fp_buftofp((unsigned char *)buffer, intbits, fltbits, shift); | |
94c98157 AA |
490 | char *result = fp_fptostr(fp, suffix); |
491 | if (result == NULL) { | |
492 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOMEM); | |
493 | return NULL; | |
494 | } | |
495 | ||
496 | size_t len = strlen(result) + 1; | |
497 | char *stored = _lldpctl_alloc_in_atom(atom, len); | |
498 | if (stored == NULL) { | |
499 | free(result); | |
500 | return NULL; | |
501 | } | |
502 | strlcpy(stored, result, len); | |
fa289087 | 503 | free(result); |
94c98157 AA |
504 | return stored; |
505 | } | |
506 | ||
8b549648 | 507 | static const char * |
94c98157 AA |
508 | _lldpctl_atom_get_str_med_location(lldpctl_atom_t *atom, lldpctl_key_t key) |
509 | { | |
510 | struct _lldpctl_atom_med_location_t *m = | |
511 | (struct _lldpctl_atom_med_location_t *)atom; | |
512 | char *value; | |
513 | ||
514 | /* Local and remote port */ | |
515 | switch (key) { | |
516 | case lldpctl_k_med_location_format: | |
517 | return map_lookup(port_med_location_map, m->location->format); | |
518 | case lldpctl_k_med_location_geoid: | |
519 | if (m->location->format != LLDP_MED_LOCFORMAT_COORD) break; | |
8b549648 | 520 | return map_lookup(port_med_geoid_map.map, m->location->data[15]); |
94c98157 AA |
521 | case lldpctl_k_med_location_latitude: |
522 | if (m->location->format != LLDP_MED_LOCFORMAT_COORD) break; | |
8b549648 | 523 | return read_fixed_precision(atom, m->location->data, 0, 9, 25, "NS"); |
94c98157 AA |
524 | case lldpctl_k_med_location_longitude: |
525 | if (m->location->format != LLDP_MED_LOCFORMAT_COORD) break; | |
8b549648 | 526 | return read_fixed_precision(atom, m->location->data, 40, 9, 25, "EW"); |
94c98157 AA |
527 | case lldpctl_k_med_location_altitude: |
528 | if (m->location->format != LLDP_MED_LOCFORMAT_COORD) break; | |
8b549648 | 529 | return read_fixed_precision(atom, m->location->data, 84, 22, 8, NULL); |
94c98157 AA |
530 | case lldpctl_k_med_location_altitude_unit: |
531 | if (m->location->format != LLDP_MED_LOCFORMAT_COORD) break; | |
532 | switch (m->location->data[10] & 0xf0) { | |
533 | case (LLDP_MED_LOCATION_ALTITUDE_UNIT_METER << 4): | |
534 | return "m"; | |
535 | case (LLDP_MED_LOCATION_ALTITUDE_UNIT_FLOOR << 4): | |
536 | return "floor"; | |
537 | } | |
538 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
539 | return NULL; | |
540 | case lldpctl_k_med_location_country: | |
541 | if (m->location->format != LLDP_MED_LOCFORMAT_CIVIC) break; | |
5c347946 | 542 | if (m->location->data_len < 4) return NULL; |
94c98157 AA |
543 | value = _lldpctl_alloc_in_atom(atom, 3); |
544 | if (!value) return NULL; | |
545 | memcpy(value, m->location->data + 2, 2); | |
546 | return value; | |
547 | case lldpctl_k_med_location_elin: | |
548 | if (m->location->format != LLDP_MED_LOCFORMAT_ELIN) break; | |
549 | value = _lldpctl_alloc_in_atom(atom, m->location->data_len + 1); | |
550 | if (!value) return NULL; | |
551 | memcpy(value, m->location->data, m->location->data_len); | |
552 | return value; | |
553 | default: | |
554 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
555 | return NULL; | |
556 | } | |
557 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
558 | return NULL; | |
559 | } | |
560 | ||
8b549648 | 561 | static lldpctl_atom_t * |
94c98157 AA |
562 | _lldpctl_atom_set_str_med_location(lldpctl_atom_t *atom, lldpctl_key_t key, |
563 | const char *value) | |
564 | { | |
565 | struct _lldpctl_atom_med_location_t *mloc = | |
566 | (struct _lldpctl_atom_med_location_t *)atom; | |
567 | struct fp_number fp; | |
568 | char *end = NULL; | |
569 | ||
570 | /* Only local port can be modified */ | |
9da663f7 | 571 | if (!mloc->parent->local) { |
94c98157 AA |
572 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); |
573 | return NULL; | |
574 | } | |
575 | ||
576 | switch (key) { | |
577 | case lldpctl_k_med_location_latitude: | |
578 | if (mloc->location->format != LLDP_MED_LOCFORMAT_COORD) goto bad; | |
8b549648 VB |
579 | if (mloc->location->data == NULL || mloc->location->data_len != 16) |
580 | goto bad; | |
94c98157 AA |
581 | if (value) fp = fp_strtofp(value, &end, 9, 25); |
582 | if (!end) goto bad; | |
583 | if (end && *end != '\0') { | |
8b549648 VB |
584 | if (*(end + 1) != '\0') goto bad; |
585 | if (*end == 'S') | |
586 | fp = fp_negate(fp); | |
587 | else if (*end != 'N') | |
588 | goto bad; | |
94c98157 | 589 | } |
8b549648 | 590 | fp_fptobuf(fp, (unsigned char *)mloc->location->data, 0); |
94c98157 AA |
591 | return atom; |
592 | case lldpctl_k_med_location_longitude: | |
593 | if (mloc->location->format != LLDP_MED_LOCFORMAT_COORD) goto bad; | |
8b549648 VB |
594 | if (mloc->location->data == NULL || mloc->location->data_len != 16) |
595 | goto bad; | |
94c98157 AA |
596 | if (value) fp = fp_strtofp(value, &end, 9, 25); |
597 | if (!end) goto bad; | |
598 | if (end && *end != '\0') { | |
8b549648 VB |
599 | if (*(end + 1) != '\0') goto bad; |
600 | if (*end == 'W') | |
601 | fp = fp_negate(fp); | |
602 | else if (*end != 'E') | |
603 | goto bad; | |
94c98157 | 604 | } |
8b549648 | 605 | fp_fptobuf(fp, (unsigned char *)mloc->location->data, 40); |
94c98157 AA |
606 | return atom; |
607 | case lldpctl_k_med_location_altitude: | |
608 | if (mloc->location->format != LLDP_MED_LOCFORMAT_COORD) goto bad; | |
8b549648 VB |
609 | if (mloc->location->data == NULL || mloc->location->data_len != 16) |
610 | goto bad; | |
94c98157 AA |
611 | if (value) fp = fp_strtofp(value, &end, 22, 8); |
612 | if (!end || *end != '\0') goto bad; | |
8b549648 | 613 | fp_fptobuf(fp, (unsigned char *)mloc->location->data, 84); |
94c98157 AA |
614 | return atom; |
615 | case lldpctl_k_med_location_altitude_unit: | |
616 | if (!value) goto bad; | |
617 | if (mloc->location->format != LLDP_MED_LOCFORMAT_COORD) goto bad; | |
8b549648 VB |
618 | if (mloc->location->data == NULL || mloc->location->data_len != 16) |
619 | goto bad; | |
94c98157 AA |
620 | if (!strcmp(value, "m")) |
621 | return _lldpctl_atom_set_int_med_location(atom, key, | |
622 | LLDP_MED_LOCATION_ALTITUDE_UNIT_METER); | |
8b549648 | 623 | if (!strcmp(value, "f") || (!strcmp(value, "floor"))) |
94c98157 AA |
624 | return _lldpctl_atom_set_int_med_location(atom, key, |
625 | LLDP_MED_LOCATION_ALTITUDE_UNIT_FLOOR); | |
626 | goto bad; | |
627 | break; | |
628 | case lldpctl_k_med_location_geoid: | |
629 | return _lldpctl_atom_set_int_med_location(atom, key, | |
630 | map_reverse_lookup(port_med_geoid_map.map, value)); | |
631 | case lldpctl_k_med_location_country: | |
632 | if (mloc->location->format != LLDP_MED_LOCFORMAT_CIVIC) goto bad; | |
8b549648 VB |
633 | if (mloc->location->data == NULL || mloc->location->data_len < 3) |
634 | goto bad; | |
94c98157 AA |
635 | if (!value || strlen(value) != 2) goto bad; |
636 | memcpy(mloc->location->data + 2, value, 2); | |
637 | return atom; | |
638 | case lldpctl_k_med_location_elin: | |
639 | if (!value) goto bad; | |
640 | if (mloc->location->format != LLDP_MED_LOCFORMAT_ELIN) goto bad; | |
641 | free(mloc->location->data); | |
642 | mloc->location->data = calloc(1, strlen(value)); | |
643 | if (mloc->location->data == NULL) { | |
644 | mloc->location->data_len = 0; | |
645 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOMEM); | |
646 | return NULL; | |
647 | } | |
648 | mloc->location->data_len = strlen(value); | |
8b549648 | 649 | memcpy(mloc->location->data, value, mloc->location->data_len); |
94c98157 AA |
650 | return atom; |
651 | default: | |
652 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
653 | return NULL; | |
654 | } | |
655 | ||
656 | return atom; | |
657 | bad: | |
658 | SET_ERROR(atom->conn, LLDPCTL_ERR_BAD_VALUE); | |
659 | return NULL; | |
94c98157 AA |
660 | } |
661 | ||
8b549648 | 662 | static lldpctl_atom_t * |
94c98157 AA |
663 | _lldpctl_atom_get_atom_med_location(lldpctl_atom_t *atom, lldpctl_key_t key) |
664 | { | |
665 | struct _lldpctl_atom_med_location_t *m = | |
666 | (struct _lldpctl_atom_med_location_t *)atom; | |
667 | ||
668 | /* Local and remote port */ | |
669 | switch (key) { | |
670 | case lldpctl_k_med_location_ca_elements: | |
671 | if (m->location->format != LLDP_MED_LOCFORMAT_CIVIC) { | |
672 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
673 | return NULL; | |
674 | } | |
675 | return _lldpctl_new_atom(atom->conn, atom_med_caelements_list, m); | |
676 | default: | |
677 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
678 | return NULL; | |
679 | } | |
680 | } | |
681 | ||
8b549648 | 682 | static lldpctl_atom_t * |
94c98157 AA |
683 | _lldpctl_atom_set_atom_med_location(lldpctl_atom_t *atom, lldpctl_key_t key, |
684 | lldpctl_atom_t *value) | |
685 | { | |
686 | struct _lldpctl_atom_med_location_t *m = | |
687 | (struct _lldpctl_atom_med_location_t *)atom; | |
688 | struct _lldpctl_atom_med_caelement_t *el; | |
689 | uint8_t *new; | |
690 | ||
691 | /* Only local port can be modified */ | |
9da663f7 | 692 | if (!m->parent->local) { |
94c98157 AA |
693 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); |
694 | return NULL; | |
695 | } | |
696 | ||
697 | switch (key) { | |
698 | case lldpctl_k_med_location_ca_elements: | |
699 | if (value->type != atom_med_caelement) { | |
700 | SET_ERROR(atom->conn, LLDPCTL_ERR_INCORRECT_ATOM_TYPE); | |
701 | return NULL; | |
702 | } | |
703 | if (m->location->format != LLDP_MED_LOCFORMAT_CIVIC) goto bad; | |
704 | if (m->location->data == NULL || m->location->data_len < 3) goto bad; | |
705 | ||
706 | /* We append this element. */ | |
707 | el = (struct _lldpctl_atom_med_caelement_t *)value; | |
708 | new = malloc(m->location->data_len + 2 + el->len); | |
709 | if (new == NULL) { | |
710 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOMEM); | |
711 | return NULL; | |
712 | } | |
713 | memcpy(new, m->location->data, m->location->data_len); | |
714 | new[m->location->data_len] = el->type; | |
715 | new[m->location->data_len + 1] = el->len; | |
716 | memcpy(new + m->location->data_len + 2, el->value, el->len); | |
717 | new[0] += 2 + el->len; | |
718 | free(m->location->data); | |
8b549648 | 719 | m->location->data = (char *)new; |
94c98157 AA |
720 | m->location->data_len += 2 + el->len; |
721 | return atom; | |
722 | default: | |
723 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
724 | return NULL; | |
725 | } | |
726 | bad: | |
727 | SET_ERROR(atom->conn, LLDPCTL_ERR_BAD_VALUE); | |
728 | return NULL; | |
729 | } | |
730 | ||
731 | struct ca_iter { | |
732 | uint8_t *data; | |
733 | size_t data_len; | |
734 | }; | |
735 | ||
8b549648 | 736 | static lldpctl_atom_iter_t * |
94c98157 AA |
737 | _lldpctl_atom_iter_med_caelements_list(lldpctl_atom_t *atom) |
738 | { | |
739 | struct _lldpctl_atom_med_caelements_list_t *plist = | |
740 | (struct _lldpctl_atom_med_caelements_list_t *)atom; | |
5c347946 VB |
741 | struct ca_iter *iter; |
742 | if (plist->parent->location->data_len < 4 || | |
8b549648 | 743 | *(uint8_t *)plist->parent->location->data < 3 || |
5c347946 VB |
744 | !(iter = _lldpctl_alloc_in_atom(atom, sizeof(struct ca_iter)))) |
745 | return NULL; | |
8b549648 VB |
746 | iter->data = (uint8_t *)plist->parent->location->data + 4; |
747 | iter->data_len = *(uint8_t *)plist->parent->location->data - 3; | |
748 | return (lldpctl_atom_iter_t *)iter; | |
94c98157 AA |
749 | } |
750 | ||
8b549648 | 751 | static lldpctl_atom_iter_t * |
94c98157 AA |
752 | _lldpctl_atom_next_med_caelements_list(lldpctl_atom_t *atom, lldpctl_atom_iter_t *iter) |
753 | { | |
754 | struct ca_iter *cai = (struct ca_iter *)iter; | |
755 | int len; | |
756 | if (cai->data_len < 2) return NULL; | |
757 | len = *((uint8_t *)cai->data + 1); | |
758 | if (cai->data_len < 2 + len) return NULL; | |
759 | cai->data += 2 + len; | |
760 | cai->data_len -= 2 + len; | |
8b549648 | 761 | return (lldpctl_atom_iter_t *)cai; |
94c98157 AA |
762 | } |
763 | ||
8b549648 | 764 | static lldpctl_atom_t * |
94c98157 AA |
765 | _lldpctl_atom_value_med_caelements_list(lldpctl_atom_t *atom, lldpctl_atom_iter_t *iter) |
766 | { | |
767 | struct _lldpctl_atom_med_caelements_list_t *plist = | |
768 | (struct _lldpctl_atom_med_caelements_list_t *)atom; | |
769 | struct ca_iter *cai = (struct ca_iter *)iter; | |
770 | size_t len; | |
771 | if (cai->data_len < 2) return NULL; | |
772 | len = *((uint8_t *)cai->data + 1); | |
773 | if (cai->data_len < 2 + len) return NULL; | |
774 | return _lldpctl_new_atom(atom->conn, atom_med_caelement, plist->parent, | |
775 | (int)*cai->data, cai->data + 2, len); | |
776 | } | |
777 | ||
8b549648 | 778 | static lldpctl_atom_t * |
94c98157 AA |
779 | _lldpctl_atom_create_med_caelements_list(lldpctl_atom_t *atom) |
780 | { | |
781 | struct _lldpctl_atom_med_caelements_list_t *plist = | |
782 | (struct _lldpctl_atom_med_caelements_list_t *)atom; | |
8b549648 VB |
783 | return _lldpctl_new_atom(atom->conn, atom_med_caelement, plist->parent, -1, |
784 | NULL, 0); | |
94c98157 AA |
785 | } |
786 | ||
787 | static int | |
788 | _lldpctl_atom_new_med_caelement(lldpctl_atom_t *atom, va_list ap) | |
789 | { | |
790 | struct _lldpctl_atom_med_caelement_t *el = | |
791 | (struct _lldpctl_atom_med_caelement_t *)atom; | |
792 | el->parent = va_arg(ap, struct _lldpctl_atom_med_location_t *); | |
8b549648 VB |
793 | el->type = va_arg(ap, int); |
794 | el->value = va_arg(ap, uint8_t *); | |
795 | el->len = va_arg(ap, size_t); | |
94c98157 AA |
796 | lldpctl_atom_inc_ref((lldpctl_atom_t *)el->parent); |
797 | return 1; | |
798 | } | |
799 | ||
800 | static void | |
801 | _lldpctl_atom_free_med_caelement(lldpctl_atom_t *atom) | |
802 | { | |
803 | struct _lldpctl_atom_med_caelement_t *el = | |
804 | (struct _lldpctl_atom_med_caelement_t *)atom; | |
805 | lldpctl_atom_dec_ref((lldpctl_atom_t *)el->parent); | |
806 | } | |
807 | ||
808 | static long int | |
809 | _lldpctl_atom_get_int_med_caelement(lldpctl_atom_t *atom, lldpctl_key_t key) | |
810 | { | |
811 | struct _lldpctl_atom_med_caelement_t *m = | |
812 | (struct _lldpctl_atom_med_caelement_t *)atom; | |
813 | ||
814 | switch (key) { | |
815 | case lldpctl_k_med_civicaddress_type: | |
816 | return m->type; | |
817 | default: | |
818 | return SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
819 | } | |
820 | } | |
821 | ||
8b549648 | 822 | static lldpctl_atom_t * |
94c98157 AA |
823 | _lldpctl_atom_set_int_med_caelement(lldpctl_atom_t *atom, lldpctl_key_t key, |
824 | long int value) | |
825 | { | |
826 | struct _lldpctl_atom_med_caelement_t *el = | |
827 | (struct _lldpctl_atom_med_caelement_t *)atom; | |
828 | ||
829 | /* Only local port can be modified */ | |
9da663f7 | 830 | if (!el->parent->parent->local) { |
94c98157 AA |
831 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); |
832 | return NULL; | |
833 | } | |
834 | ||
835 | switch (key) { | |
836 | case lldpctl_k_med_civicaddress_type: | |
451b0c3c | 837 | if (value < 0 || value > 128) goto bad; |
94c98157 AA |
838 | el->type = value; |
839 | return atom; | |
840 | default: | |
841 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
842 | return NULL; | |
843 | } | |
844 | ||
845 | return atom; | |
846 | bad: | |
847 | SET_ERROR(atom->conn, LLDPCTL_ERR_BAD_VALUE); | |
848 | return NULL; | |
849 | } | |
850 | ||
8b549648 | 851 | static const char * |
94c98157 AA |
852 | _lldpctl_atom_get_str_med_caelement(lldpctl_atom_t *atom, lldpctl_key_t key) |
853 | { | |
854 | char *value = NULL; | |
855 | struct _lldpctl_atom_med_caelement_t *m = | |
856 | (struct _lldpctl_atom_med_caelement_t *)atom; | |
857 | ||
858 | /* Local and remote port */ | |
859 | switch (key) { | |
860 | case lldpctl_k_med_civicaddress_type: | |
861 | return map_lookup(civic_address_type_map.map, m->type); | |
862 | case lldpctl_k_med_civicaddress_value: | |
863 | value = _lldpctl_alloc_in_atom(atom, m->len + 1); | |
864 | if (!value) return NULL; | |
865 | memcpy(value, m->value, m->len); | |
866 | return value; | |
867 | default: | |
868 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
869 | return NULL; | |
870 | } | |
871 | } | |
872 | ||
8b549648 | 873 | static lldpctl_atom_t * |
94c98157 AA |
874 | _lldpctl_atom_set_str_med_caelement(lldpctl_atom_t *atom, lldpctl_key_t key, |
875 | const char *value) | |
876 | { | |
877 | struct _lldpctl_atom_med_caelement_t *el = | |
878 | (struct _lldpctl_atom_med_caelement_t *)atom; | |
879 | size_t len; | |
880 | ||
881 | /* Only local port can be modified */ | |
9da663f7 | 882 | if (!el->parent->parent->local) { |
94c98157 AA |
883 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); |
884 | return NULL; | |
885 | } | |
886 | ||
887 | switch (key) { | |
888 | case lldpctl_k_med_civicaddress_value: | |
889 | if (!value) goto bad; | |
890 | len = strlen(value) + 1; | |
891 | if (len > 251) goto bad; | |
892 | el->value = _lldpctl_alloc_in_atom(atom, len); | |
893 | if (el->value == NULL) return NULL; | |
8b549648 | 894 | strlcpy((char *)el->value, value, len); |
94c98157 AA |
895 | el->len = strlen(value); |
896 | return atom; | |
897 | case lldpctl_k_med_civicaddress_type: | |
898 | return _lldpctl_atom_set_int_med_caelement(atom, key, | |
899 | map_reverse_lookup(civic_address_type_map.map, value)); | |
900 | default: | |
901 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
902 | return NULL; | |
903 | } | |
904 | ||
905 | return atom; | |
906 | bad: | |
907 | SET_ERROR(atom->conn, LLDPCTL_ERR_BAD_VALUE); | |
908 | return NULL; | |
909 | } | |
910 | ||
911 | static int | |
912 | _lldpctl_atom_new_med_power(lldpctl_atom_t *atom, va_list ap) | |
913 | { | |
914 | struct _lldpctl_atom_med_power_t *mpow = | |
915 | (struct _lldpctl_atom_med_power_t *)atom; | |
916 | mpow->parent = va_arg(ap, struct _lldpctl_atom_port_t *); | |
917 | lldpctl_atom_inc_ref((lldpctl_atom_t *)mpow->parent); | |
918 | return 1; | |
919 | } | |
920 | ||
921 | static void | |
922 | _lldpctl_atom_free_med_power(lldpctl_atom_t *atom) | |
923 | { | |
924 | struct _lldpctl_atom_med_power_t *mpow = | |
925 | (struct _lldpctl_atom_med_power_t *)atom; | |
926 | lldpctl_atom_dec_ref((lldpctl_atom_t *)mpow->parent); | |
927 | } | |
928 | ||
8b549648 | 929 | static const char * |
94c98157 AA |
930 | _lldpctl_atom_get_str_med_power(lldpctl_atom_t *atom, lldpctl_key_t key) |
931 | { | |
932 | struct _lldpctl_atom_med_power_t *mpow = | |
933 | (struct _lldpctl_atom_med_power_t *)atom; | |
934 | struct lldpd_port *port = mpow->parent->port; | |
935 | ||
936 | /* Local and remote port */ | |
937 | switch (key) { | |
938 | case lldpctl_k_med_power_type: | |
939 | return map_lookup(port_med_pow_devicetype_map, | |
940 | port->p_med_power.devicetype); | |
941 | case lldpctl_k_med_power_source: | |
8b549648 | 942 | return map_lookup(port_med_pow_source_map, port->p_med_power.source); |
94c98157 AA |
943 | case lldpctl_k_med_power_priority: |
944 | return map_lookup(port_med_pow_priority_map.map, | |
945 | port->p_med_power.priority); | |
946 | default: | |
947 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
948 | return NULL; | |
949 | } | |
950 | } | |
951 | ||
952 | static long int | |
953 | _lldpctl_atom_get_int_med_power(lldpctl_atom_t *atom, lldpctl_key_t key) | |
954 | { | |
955 | struct _lldpctl_atom_med_power_t *dpow = | |
956 | (struct _lldpctl_atom_med_power_t *)atom; | |
8b549648 | 957 | struct lldpd_port *port = dpow->parent->port; |
94c98157 AA |
958 | |
959 | /* Local and remote port */ | |
960 | switch (key) { | |
961 | case lldpctl_k_med_power_type: | |
962 | return port->p_med_power.devicetype; | |
963 | case lldpctl_k_med_power_source: | |
964 | return port->p_med_power.source; | |
965 | case lldpctl_k_med_power_priority: | |
966 | return port->p_med_power.priority; | |
967 | case lldpctl_k_med_power_val: | |
968 | return port->p_med_power.val * 100; | |
969 | default: | |
970 | return SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
971 | } | |
972 | } | |
973 | ||
8b549648 VB |
974 | static lldpctl_atom_t * |
975 | _lldpctl_atom_set_int_med_power(lldpctl_atom_t *atom, lldpctl_key_t key, long int value) | |
94c98157 AA |
976 | { |
977 | struct _lldpctl_atom_med_power_t *dpow = | |
978 | (struct _lldpctl_atom_med_power_t *)atom; | |
979 | struct lldpd_port *port = dpow->parent->port; | |
980 | ||
981 | /* Only local port can be modified */ | |
9da663f7 | 982 | if (!dpow->parent->local) { |
94c98157 AA |
983 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); |
984 | return NULL; | |
985 | } | |
986 | ||
987 | switch (key) { | |
988 | case lldpctl_k_med_power_type: | |
989 | switch (value) { | |
990 | case 0: | |
991 | case LLDP_MED_POW_TYPE_PSE: | |
992 | case LLDP_MED_POW_TYPE_PD: | |
993 | port->p_med_power.devicetype = value; | |
994 | return atom; | |
8b549648 VB |
995 | default: |
996 | goto bad; | |
94c98157 AA |
997 | } |
998 | case lldpctl_k_med_power_source: | |
999 | switch (value) { | |
1000 | case LLDP_MED_POW_SOURCE_PRIMARY: | |
1001 | case LLDP_MED_POW_SOURCE_BACKUP: | |
1002 | if (port->p_med_power.devicetype != LLDP_MED_POW_TYPE_PSE) | |
1003 | goto bad; | |
1004 | port->p_med_power.source = value; | |
1005 | return atom; | |
1006 | case LLDP_MED_POW_SOURCE_PSE: | |
1007 | case LLDP_MED_POW_SOURCE_LOCAL: | |
1008 | case LLDP_MED_POW_SOURCE_BOTH: | |
1009 | if (port->p_med_power.devicetype != LLDP_MED_POW_TYPE_PD) | |
1010 | goto bad; | |
1011 | port->p_med_power.source = value; | |
1012 | return atom; | |
1013 | case LLDP_MED_POW_SOURCE_UNKNOWN: | |
1014 | port->p_med_power.source = value; | |
1015 | return atom; | |
8b549648 VB |
1016 | default: |
1017 | goto bad; | |
94c98157 AA |
1018 | } |
1019 | case lldpctl_k_med_power_priority: | |
1020 | if (value < 0 || value > 3) goto bad; | |
1021 | port->p_med_power.priority = value; | |
1022 | return atom; | |
1023 | case lldpctl_k_med_power_val: | |
1024 | if (value < 0) goto bad; | |
1025 | port->p_med_power.val = value / 100; | |
1026 | return atom; | |
1027 | default: | |
1028 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
1029 | return NULL; | |
1030 | } | |
1031 | ||
1032 | return atom; | |
1033 | bad: | |
1034 | SET_ERROR(atom->conn, LLDPCTL_ERR_BAD_VALUE); | |
1035 | return NULL; | |
1036 | } | |
1037 | ||
8b549648 | 1038 | static lldpctl_atom_t * |
94c98157 AA |
1039 | _lldpctl_atom_set_str_med_power(lldpctl_atom_t *atom, lldpctl_key_t key, |
1040 | const char *value) | |
1041 | { | |
1042 | switch (key) { | |
1043 | case lldpctl_k_med_power_type: | |
1044 | return _lldpctl_atom_set_int_med_power(atom, key, | |
1045 | map_reverse_lookup(port_med_pow_devicetype_map, value)); | |
1046 | case lldpctl_k_med_power_source: | |
1047 | return _lldpctl_atom_set_int_med_power(atom, key, | |
1048 | map_reverse_lookup(port_med_pow_source_map2, value)); | |
1049 | case lldpctl_k_med_power_priority: | |
1050 | return _lldpctl_atom_set_int_med_power(atom, key, | |
1051 | map_reverse_lookup(port_med_pow_priority_map.map, value)); | |
1052 | default: | |
1053 | SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); | |
1054 | return NULL; | |
1055 | } | |
1056 | } | |
1057 | ||
8b549648 VB |
1058 | static struct atom_builder med_policies_list = { atom_med_policies_list, |
1059 | sizeof(struct _lldpctl_atom_any_list_t), .init = _lldpctl_atom_new_any_list, | |
1060 | .free = _lldpctl_atom_free_any_list, | |
1061 | .iter = _lldpctl_atom_iter_med_policies_list, | |
1062 | .next = _lldpctl_atom_next_med_policies_list, | |
1063 | .value = _lldpctl_atom_value_med_policies_list }; | |
1064 | ||
1065 | static struct atom_builder med_policy = { atom_med_policy, | |
1066 | sizeof(struct _lldpctl_atom_med_policy_t), .init = _lldpctl_atom_new_med_policy, | |
1067 | .free = _lldpctl_atom_free_med_policy, | |
1068 | .get_int = _lldpctl_atom_get_int_med_policy, | |
1069 | .set_int = _lldpctl_atom_set_int_med_policy, | |
1070 | .get_str = _lldpctl_atom_get_str_med_policy, | |
1071 | .set_str = _lldpctl_atom_set_str_med_policy }; | |
1072 | ||
1073 | static struct atom_builder med_locations_list = { atom_med_locations_list, | |
1074 | sizeof(struct _lldpctl_atom_any_list_t), .init = _lldpctl_atom_new_any_list, | |
1075 | .free = _lldpctl_atom_free_any_list, | |
1076 | .iter = _lldpctl_atom_iter_med_locations_list, | |
1077 | .next = _lldpctl_atom_next_med_locations_list, | |
1078 | .value = _lldpctl_atom_value_med_locations_list }; | |
1079 | ||
1080 | static struct atom_builder med_location = { atom_med_location, | |
1081 | sizeof(struct _lldpctl_atom_med_location_t), | |
1082 | .init = _lldpctl_atom_new_med_location, .free = _lldpctl_atom_free_med_location, | |
1083 | .get = _lldpctl_atom_get_atom_med_location, | |
1084 | .set = _lldpctl_atom_set_atom_med_location, | |
1085 | .get_int = _lldpctl_atom_get_int_med_location, | |
1086 | .set_int = _lldpctl_atom_set_int_med_location, | |
1087 | .get_str = _lldpctl_atom_get_str_med_location, | |
1088 | .set_str = _lldpctl_atom_set_str_med_location }; | |
1089 | ||
1090 | static struct atom_builder med_caelements_list = { atom_med_caelements_list, | |
1091 | sizeof(struct _lldpctl_atom_med_caelements_list_t), | |
1092 | .init = _lldpctl_atom_new_any_list, .free = _lldpctl_atom_free_any_list, | |
1093 | .iter = _lldpctl_atom_iter_med_caelements_list, | |
1094 | .next = _lldpctl_atom_next_med_caelements_list, | |
1095 | .value = _lldpctl_atom_value_med_caelements_list, | |
1096 | .create = _lldpctl_atom_create_med_caelements_list }; | |
1097 | ||
1098 | static struct atom_builder med_caelement = { atom_med_caelement, | |
1099 | sizeof(struct _lldpctl_atom_med_caelement_t), | |
1100 | .init = _lldpctl_atom_new_med_caelement, | |
1101 | .free = _lldpctl_atom_free_med_caelement, | |
1102 | .get_int = _lldpctl_atom_get_int_med_caelement, | |
1103 | .set_int = _lldpctl_atom_set_int_med_caelement, | |
1104 | .get_str = _lldpctl_atom_get_str_med_caelement, | |
1105 | .set_str = _lldpctl_atom_set_str_med_caelement }; | |
1106 | ||
1107 | static struct atom_builder med_power = { atom_med_power, | |
1108 | sizeof(struct _lldpctl_atom_med_power_t), .init = _lldpctl_atom_new_med_power, | |
1109 | .free = _lldpctl_atom_free_med_power, | |
1110 | .get_int = _lldpctl_atom_get_int_med_power, | |
1111 | .set_int = _lldpctl_atom_set_int_med_power, | |
1112 | .get_str = _lldpctl_atom_get_str_med_power, | |
1113 | .set_str = _lldpctl_atom_set_str_med_power }; | |
1114 | ||
1115 | ATOM_BUILDER_REGISTER(med_policies_list, 15); | |
1116 | ATOM_BUILDER_REGISTER(med_policy, 16); | |
1117 | ATOM_BUILDER_REGISTER(med_locations_list, 17); | |
1118 | ATOM_BUILDER_REGISTER(med_location, 18); | |
94c98157 | 1119 | ATOM_BUILDER_REGISTER(med_caelements_list, 19); |
8b549648 VB |
1120 | ATOM_BUILDER_REGISTER(med_caelement, 20); |
1121 | ATOM_BUILDER_REGISTER(med_power, 21); | |
94c98157 AA |
1122 | |
1123 | #endif |