]> git.ipfire.org Git - thirdparty/lldpd.git/blame - src/lib/lldpctl.h
build: reformat code
[thirdparty/lldpd.git] / src / lib / lldpctl.h
CommitLineData
4b292b55
VB
1/* -*- mode: c; c-file-style: "openbsd" -*- */
2/*
3 * Copyright (c) 2012 Vincent Bernat <bernat@luffy.cx>
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#ifndef LLDPCTL_H
19#define LLDPCTL_H
20
21/**
b1eceab6 22 * @defgroup liblldpctl liblldpctl: library to interface with lldpd
4b292b55 23 *
b1eceab6
VB
24 * `liblldpctl` allows any program to convenienty query and modify the behaviour
25 * of a running lldpd daemon.
26 *
27 * To use this library, use `pkg-config` to get the appropriate options:
28 * * `pkg-config --libs lldpctl` for `LIBS` or `LDFLAGS`
29 * * `pkg-config --cflags lldpctl` for `CFLAGS`
30 *
31 * @warning This library is tightly coupled with lldpd. The library to use
32 * should be the one shipped with lldpd. Clients of the library are then tied
33 * by the classic API/ABI rules and may be compiled separatly.
34 *
35 * There are two important structures in this library: @c lldpctl_conn_t which
36 * represents a connection and @c lldpctl_atom_t which represents a piece of
37 * information. Those types are opaque. No direct access to them should be done.
38 *
39 * The library is expected to be reentrant and therefore thread-safe. It is
40 * however not expected that a connection to be used in several thread
41 * simultaneously. This also applies to the different pieces of information
42 * gathered through this connection. Several connection to lldpd can be used
43 * simultaneously.
44 *
45 * The first step is to establish a connection. See @ref lldpctl_connection for
46 * more information about this. The next step is to query the lldpd daemon. See
47 * @ref lldpctl_atoms on how to do this.
48 *
49 * `liblldpctl` tries to handle errors in a coherent way. Any function returning
50 * a pointer will return @c NULL on error and the last error can be retrieved
51 * through @ref lldpctl_last_error() function. Most functions returning integers
52 * will return a negative integer representing the error if something goes
53 * wrong. The use of @ref lldpctl_last_error() allows one to check if this is a
54 * real error if there is a doubt. See @ref lldpctl_errors_logs for more about
55 * this.
56 *
57 * @{
4b292b55
VB
58 */
59
60#ifdef __cplusplus
61extern "C" {
62#endif
63
64#include <stdlib.h>
65#include <stdint.h>
66#include <sys/types.h>
67
68/**
b1eceab6 69 * @defgroup lldpctl_connection Managing connection to lldpd
4b292b55 70 *
94d2efe5
VB
71 * Connection with lldpd.
72 *
b1eceab6
VB
73 * This library does not handle IO. They are delegated to a set of functions to
74 * allow a user to specify exactly how IO should be done. A user is expected to
75 * provide two functions: the first one is called when the library requests
76 * incoming data, the other one when it requests outgoing data. Moreover, the
77 * user is also expected to call the appropriate functions when data comes back
78 * (@ref lldpctl_recv()) or needs to be sent (@ref lldpctl_send()).
79 *
80 * Because the most common case is synchronous IO, `liblldpctl` will use classic
81 * synchronous IO with the Unix socket if no IO functions are provided by the
82 * user. For all other cases, the user must provide the appropriate functions.
83 *
84 * A connection should be allocated by using @ref lldpctl_new(). It needs to be
85 * released with @ref lldpctl_release().
86 *
87 * @{
4b292b55 88 */
4b292b55
VB
89
90/**
b1eceab6 91 * Get default transport name.
4b292b55 92 *
b1eceab6 93 * Currently, this is the default location of the Unix socket.
4b292b55 94 */
8b549648 95const char *lldpctl_get_default_transport(void);
4b292b55 96
4b292b55
VB
97/**
98 * Structure referencing a connection with lldpd.
99 *
100 * This structure should be handled as opaque. It can be allocated
101 * with @c lldpctl_new() and the associated resources will be freed
102 * with @c lldpctl_release().
103 */
104typedef struct lldpctl_conn_t lldpctl_conn_t;
105
106/**
107 * Callback function invoked to send data to lldpd.
108 *
b35cfd03 109 * @param conn Handle to the connection to lldpd.
4b292b55
VB
110 * @param data Bytes to be sent.
111 * @param length Length of provided data.
112 * @param user_data Provided user data.
113 * @return The number of bytes really sent or either @c LLDPCTL_ERR_WOULDBLOCK
114 * if no bytes can be sent without blocking or @c
115 * LLDPCTL_ERR_CALLBACK_FAILURE for other errors.
116 */
8b549648
VB
117typedef ssize_t (*lldpctl_send_callback)(lldpctl_conn_t *conn, const uint8_t *data,
118 size_t length, void *user_data);
4b292b55
VB
119
120/**
121 * Callback function invoked to receive data from lldpd.
122 *
b35cfd03 123 * @param conn Handle to the connection to lldpd.
4b292b55
VB
124 * @param data Buffer for receiving data
125 * @param length Maximum bytes we can receive
126 * @param user_data Provided user data.
127 * @return The number of bytes really received or either @c
128 * LLDPCTL_ERR_WOULDBLOCK if no bytes can be received without blocking,
129 * @c LLDPCTL_ERR_CALLBACK_FAILURE for other errors or @c
130 * LLDPCTL_ERR_EOF if end of file was reached.
131 */
8b549648
VB
132typedef ssize_t (*lldpctl_recv_callback)(lldpctl_conn_t *conn, const uint8_t *data,
133 size_t length, void *user_data);
4b292b55
VB
134
135/**
136 * Function invoked when additional data is available from lldpd.
137 *
138 * This function should be invoked in case of asynchronous IO when new data is
139 * available from lldpd (expected or unexpected).
140 *
b1eceab6 141 * @param conn Handle to the connection to lldpd.
4b292b55
VB
142 * @param data Data received from lldpd.
143 * @param length Length of data received.
4e90a9e0
VB
144 * @return The number of bytes available or a negative integer if an error has
145 * occurred. 0 is not an error. It usually means that a notification has
146 * been processed.
4b292b55 147 */
b1eceab6 148ssize_t lldpctl_recv(lldpctl_conn_t *conn, const uint8_t *data, size_t length);
4b292b55
VB
149
150/**
151 * Function invoked when there is an opportunity to send data to lldpd.
152 *
153 * This function should be invoked in case of asynchronous IO when new data can
154 * be written to lldpd.
155 *
b1eceab6 156 * @param conn Handle to the connection to lldpd.
4b292b55 157 * @return The number of bytes processed or a negative integer if an error has
0cddb489 158 * occurred.
4b292b55 159 */
b1eceab6 160ssize_t lldpctl_send(lldpctl_conn_t *conn);
4b292b55 161
1fa64c11
ST
162/**
163 * Function invoked to see if there's more data to be processed in the buffer.
164 *
165 * This function should be invoked to check for notifications in the data that
166 * has already been read. Its used typically for asynchronous connections.
167 *
168 * @param conn Handle to the connection to lldpd.
169 * @return 0 to indicate maybe more data is available for processing
170 * !0 to indicate no data or insufficient data for processing
171 */
172int lldpctl_process_conn_buffer(lldpctl_conn_t *conn);
173
4b292b55
VB
174/**
175 * Allocate a new handler for connecting to lldpd.
176 *
4b292b55
VB
177 * @param send Callback to be used when sending new data is requested.
178 * @param recv Callback to be used when receiving new data is requested.
179 * @param user_data Data to pass to callbacks.
180 * @return An handler to be used to connect to lldpd or @c NULL in
181 * case of error. In the later case, the error is probable an
182 * out of memory condition.
183 *
184 * The allocated handler can be released with @c lldpctl_release(). If the
185 * provided parameters are both @c NULL, default synchronous callbacks will be
186 * used.
187 */
8b549648
VB
188lldpctl_conn_t *lldpctl_new(lldpctl_send_callback send, lldpctl_recv_callback recv,
189 void *user_data);
4b292b55 190
0262adbb
ZM
191/**
192 * Allocate a new handler for connecting to lldpd.
193 *
194 * @param ctlname the Unix-domain socket to connect to lldpd.
195 * @param send Callback to be used when sending new data is requested.
196 * @param recv Callback to be used when receiving new data is requested.
197 * @param user_data Data to pass to callbacks.
198 * @return An handler to be used to connect to lldpd or @c NULL in
199 * case of error. In the later case, the error is probable an
200 * out of memory condition.
201 *
202 * The allocated handler can be released with @c lldpctl_release(). If the
203 * provided parameters are both @c NULL, default synchronous callbacks will be
204 * used.
205 */
206lldpctl_conn_t *lldpctl_new_name(const char *ctlname, lldpctl_send_callback send,
207 lldpctl_recv_callback recv, void *user_data);
208
4b292b55
VB
209/**
210 * Release resources associated with a connection to lldpd.
211 *
b1eceab6 212 * @param conn Previously allocated handler to a connection to lldpd.
4b292b55
VB
213 * @return 0 on success or a negative integer
214 *
215 * @see lldpctl_new()
216 */
b1eceab6
VB
217int lldpctl_release(lldpctl_conn_t *conn);
218/**@}*/
219
220/**
221 * @defgroup lldpctl_errors_logs Errors and logs handling
222 *
94d2efe5
VB
223 * Error codes and logs handling.
224 *
b1eceab6
VB
225 * When a function returns a pointer, it may return @c NULL to indicate an error
226 * condition. In this case, it is possible to use @ref lldpctl_last_error() to
227 * get the related error code which is one of the values in @ref lldpctl_error_t
228 * enumeration. For display purpose @ref lldpctl_strerror() may be used to
229 * translate this error code.
230 *
231 * When a function returns an integer, it may return a negative value. It
07ecfcd1 232 * usually means this is an error but some functions may return a legitimate
b1eceab6
VB
233 * negative value (for example @ref lldpctl_atom_get_int()). When there is a
234 * doubt, @ref lldpctl_last_error() should be checked.
235 *
236 * An error is attached to a connection. If there is no connection, no error
237 * handling is available. Most functions use a connection or an atom as first
238 * argument and therefore are attached to a connection. To get the connection
239 * related to an atom, use @ref lldpctl_atom_get_connection().
240 *
241 * Also have a look at @ref lldpctl_log_callback() function if you want a custom
242 * log handling.
243 *
244 * @{
245 */
246
247/**
248 * Setup log handlers.
249 *
250 * By default, liblldpctl will log to stderr. The following function will
251 * register another callback for this purpose. Messages logged through this
252 * callback may be cryptic. They are targeted for the developer. Message for end
253 * users should rely on return codes.
254 */
255void lldpctl_log_callback(void (*cb)(int severity, const char *msg));
4b292b55 256
2389d2cc
VB
257/**
258 * Setup log level.
259 *
260 * By default, liblldpctl will only log warnings. The following function allows
261 * to increase verbosity. This function has no effect if callbacks are
262 * registered with the previous function.
263 *
264 * @param level Level of verbosity (1 = warnings, 2 = info, 3 = debug).
265 */
266void lldpctl_log_level(int level);
267
4b292b55
VB
268/**
269 * Possible error codes for functions that return negative integers on
270 * this purpose or for @c lldpctl_last_error().
271 */
272typedef enum {
273 /**
274 * No error has happened (yet).
275 */
276 LLDPCTL_NO_ERROR = 0,
277 /**
278 * A IO related operation would block if performed.
279 */
280 LLDPCTL_ERR_WOULDBLOCK = -501,
281 /**
282 * A IO related operation has reached a end of file condition.
283 */
284 LLDPCTL_ERR_EOF = -502,
285 /**
286 * The requested information does not exist. For example, when
287 * requesting an inexistant information from an atom.
288 */
289 LLDPCTL_ERR_NOT_EXIST = -503,
290 /**
291 * Cannot connect to the lldpd daemon. This error only happens with
292 * default synchronous handlers.
293 */
294 LLDPCTL_ERR_CANNOT_CONNECT = -504,
295 /**
296 * Atom is of incorrect type for the requested operation.
297 */
298 LLDPCTL_ERR_INCORRECT_ATOM_TYPE = -505,
299 /**
300 * An error occurred during serialization of message.
301 */
8b549648 302 LLDPCTL_ERR_SERIALIZATION = -506,
4b292b55
VB
303 /**
304 * The requested operation cannot be performed because we have another
305 * operation already running.
306 */
8b549648 307 LLDPCTL_ERR_INVALID_STATE = -507,
4b292b55
VB
308 /**
309 * The provided atom cannot be iterated.
310 */
8b549648 311 LLDPCTL_ERR_CANNOT_ITERATE = -508,
4b292b55
VB
312 /**
313 * The provided value is invalid.
314 */
8b549648 315 LLDPCTL_ERR_BAD_VALUE = -509,
4b292b55
VB
316 /**
317 * No new element can be created for this element.
318 */
8b549648 319 LLDPCTL_ERR_CANNOT_CREATE = -510,
4b292b55
VB
320 /**
321 * The library is under unexpected conditions and cannot process
322 * any further data reliably.
323 */
324 LLDPCTL_ERR_FATAL = -900,
325 /**
326 * Out of memory condition. Things may get havoc here but we
327 * should be able to recover.
328 */
329 LLDPCTL_ERR_NOMEM = -901,
330 /**
331 * An error occurred in a user provided callback.
332 */
333 LLDPCTL_ERR_CALLBACK_FAILURE = -902
334} lldpctl_error_t;
335
336/**
337 * Describe a provided error code.
338 *
339 * @param error Error code to be described.
340 * @return Statically allocated string describing the error.
341 */
342const char *lldpctl_strerror(lldpctl_error_t error);
343
344/**
345 * Get the last error associated to a connection to lldpd.
346 *
b1eceab6 347 * @param conn Previously allocated handler to a connection to lldpd.
4b292b55
VB
348 * @return 0 if no error is currently registered. A negative integer
349 * otherwise.
350 *
351 * For functions returning int, this function will return the same
352 * error number. For functions returning something else, you can use
353 * this function to get the appropriate error number.
354 */
b1eceab6 355lldpctl_error_t lldpctl_last_error(lldpctl_conn_t *conn);
4b292b55
VB
356
357/**
358 * Describe the last error associate to a connection.
359 *
360 * @param conn Previously allocated handler to a connection to lldpd.
361 * @return Statically allocated string describing the error
362 */
363#define lldpctl_last_strerror(conn) lldpctl_strerror(lldpctl_last_error(conn))
b1eceab6 364/**@}*/
4b292b55 365
4b292b55 366/**
b1eceab6
VB
367 * @defgroup lldpctl_atoms Extracting information: atoms
368 *
94d2efe5 369 * Information retrieved from lldpd is represented as an atom.
4b292b55
VB
370 *
371 * This is an opaque structure that can be passed along some functions to
372 * transmit chassis, ports, VLAN and other information related to LLDP. Most
373 * information are extracted using @c lldpctl_atom_get(), @c
374 * lldpctl_atom_get_str(), @c lldpctl_atom_get_buffer() or @c
375 * lldpctl_atom_get_int(), unless some IO with lldpd is needed to retrieve the
376 * requested information. In this case, there exists an appropriate function to
377 * convert the "deferred" atom into a normal one (like @c lldpctl_get_port()).
378 *
b1eceab6
VB
379 * For some information, setters are also available: @c lldpctl_atom_set(), @c
380 * lldpctl_atom_set_str(), @c lldpctl_atom_set_buffer() or @c
381 * lldpctl_atom_set_int(). Unlike getters, some of those may require IO to
382 * achieve their goal.
383 *
3c5634c1
VB
384 * An atom is reference counted. The semantics are quite similar to Python and
385 * you must be careful of the ownership of a reference. It is possible to own a
386 * reference by calling @c lldpctl_atom_inc_ref(). Once the atom is not needed
387 * any more, you can abandon ownership with @c lldpctl_atom_dec_ref(). Unless
388 * documented otherwise, a function returning an atom will return a new
389 * reference (the ownership is assigned to the caller, no need to call @c
390 * lldpctl_atom_inc_ref()). Unless documented otherwise, when providing an atom
391 * to a function, the atom is usually borrowed (no change in reference
392 * counting). Currently, no function will steal ownership.
393 *
394 * It is quite important to use the reference counting functions
395 * correctly. Segfaults or memory leaks may occur otherwise. Once the reference
396 * count reaches 0, the atom is immediately freed. Reusing it will likely lead
397 * to memory corruption.
b1eceab6
VB
398 *
399 * @{
400 */
401
402/**
403 * Structure representing an element (chassis, port, VLAN, ...)
4b292b55
VB
404 *
405 * @see lldpctl_atom_inc_ref(), lldpctl_atom_dec_ref().
406 */
407typedef struct lldpctl_atom_t lldpctl_atom_t;
408
6fcf11ca
VB
409/**
410 * Structure representing a map from an integer to a character string.
411 *
412 * @see lldpctl_key_get_map().
413 */
414typedef const struct {
8b549648 415 int value;
6fcf11ca
VB
416 char *string;
417} lldpctl_map_t;
418
4b292b55
VB
419/**
420 * Return the reference to connection with lldpd.
421 *
422 * @param atom The atom we want reference from.
423 * @return The reference to the connection to lldpd.
424 *
425 * Each atom contains an internal reference to the corresponding connection to
426 * lldpd. Use this function to get it.
427 */
428lldpctl_conn_t *lldpctl_atom_get_connection(lldpctl_atom_t *atom);
429
430/**
431 * Increment reference count for an atom.
432 *
433 * @param atom Atom we which to increase reference count.
434 */
435void lldpctl_atom_inc_ref(lldpctl_atom_t *atom);
436
437/**
438 * Decrement reference count for an atom.
439 *
440 * @param atom Atom we want to decrease reference count. Can be @c NULL. In this
441 * case, nothing happens.
442 *
4e90a9e0 443 * When the reference count becomes 0, the atom is freed.
4b292b55
VB
444 */
445void lldpctl_atom_dec_ref(lldpctl_atom_t *atom);
446
4e90a9e0 447/**
b1eceab6
VB
448 * Possible events for a change (notification).
449 *
9d7e82a1 450 * @see lldpctl_watch_callback2
4e90a9e0
VB
451 */
452typedef enum {
8b549648
VB
453 lldpctl_c_deleted, /**< The neighbor has been deleted */
454 lldpctl_c_updated, /**< The neighbor has been updated */
455 lldpctl_c_added, /**< This is a new neighbor */
4e90a9e0
VB
456} lldpctl_change_t;
457
458/**
459 * Callback function invoked when a change is detected.
460 *
c0363157 461 * @param conn Connection with lldpd. Should not be used.
4e90a9e0
VB
462 * @param type Type of change detected.
463 * @param interface Physical interface on which the change has happened.
464 * @param neighbor Changed neighbor.
465 * @param data Data provided when registering the callback.
466 *
3c5634c1
VB
467 * The provided interface and neighbor atoms are stolen by the callback: their
468 * reference count are decremented when the callback ends. If you want to keep a
469 * reference to it, be sure to increment the reference count in the callback.
b1eceab6 470 *
c0363157 471 * @warning The provided connection should not be used at all. Do not use @c
cb38337c
VB
472 * lldpctl_atom_set_*() functions on @c interface or @c neighbor either. If you
473 * do, you will get a @c LLDPCTL_ERR_INVALID_STATE error.
c0363157 474 *
b1eceab6 475 * @see lldpctl_watch_callback
4e90a9e0 476 */
8b549648
VB
477typedef void (*lldpctl_change_callback)(lldpctl_conn_t *conn, lldpctl_change_t type,
478 lldpctl_atom_t *interface, lldpctl_atom_t *neighbor, void *data);
4e90a9e0 479
9d7e82a1
VB
480/**
481 * Callback function invoked when a change is detected.
482 *
483 * @param type Type of change detected.
484 * @param interface Physical interface on which the change has happened.
485 * @param neighbor Changed neighbor.
486 * @param data Data provided when registering the callback.
487 *
488 * The provided interface and neighbor atoms are stolen by the callback: their
489 * reference count are decremented when the callback ends. If you want to keep a
490 * reference to it, be sure to increment the reference count in the callback.
491 *
492 * @see lldpctl_watch_callback2
493 */
494typedef void (*lldpctl_change_callback2)(lldpctl_change_t type,
8b549648 495 lldpctl_atom_t *interface, lldpctl_atom_t *neighbor, void *data);
9d7e82a1 496
4e90a9e0
VB
497/**
498 * Register a callback to be called on changes.
499 *
500 * @param conn Connection with lldpd.
501 * @param cb Replace the current callback with the provided one.
502 * @param data Data that will be passed to the callback.
503 * @return 0 in case of success or -1 in case of errors.
504 *
505 * This function will register the necessity to push neighbor changes to lldpd
506 * and therefore will issue IO operations. The error code could then be @c
507 * LLDPCTL_ERR_WOULDBLOCK.
09a267fc 508 *
c0363157 509 * @warning Once a callback is registered, the connection shouldn't be used for
cb38337c
VB
510 * anything else than receiving notifications. If you do, you will get a @c
511 * LLDPCTL_ERR_INVALID_STATE error.
9d7e82a1
VB
512 *
513 * @deprecated This function is deprecated and lldpctl_watch_callback2 should be
514 * used instead.
4e90a9e0 515 */
8b549648
VB
516int lldpctl_watch_callback(lldpctl_conn_t *conn, lldpctl_change_callback cb, void *data)
517 __attribute__((deprecated));
9d7e82a1
VB
518
519/**
520 * Register a callback to be called on changes.
521 *
522 * @param conn Connection with lldpd.
523 * @param cb Replace the current callback with the provided one.
524 * @param data Data that will be passed to the callback.
525 * @return 0 in case of success or -1 in case of errors.
526 *
527 * This function will register the necessity to push neighbor changes to lldpd
528 * and therefore will issue IO operations. The error code could then be @c
529 * LLDPCTL_ERR_WOULDBLOCK.
530 *
531 * @warning Once a callback is registered, the connection shouldn't be used for
532 * anything else than receiving notifications. If you do, you will get a @c
533 * LLDPCTL_ERR_INVALID_STATE error.
534 */
8b549648 535int lldpctl_watch_callback2(lldpctl_conn_t *conn, lldpctl_change_callback2 cb,
4e90a9e0
VB
536 void *data);
537
538/**
539 * Wait for the next change.
540 *
541 * @param conn Connection with lldpd.
542 * @return 0 on success or a negative integer in case of error.
543 *
544 * This function will return once a change has been detected. It is only useful
545 * as a main loop when using the builtin blocking IO mechanism.
546 */
547int lldpctl_watch(lldpctl_conn_t *conn);
548
b1eceab6
VB
549/**
550 * @defgroup liblldpctl_atom_get_special Retrieving atoms from lldpd
551 *
552 * Special access functions.
553 *
554 * Most information can be retrieved through @ref lldpctl_atom_get(), @ref
555 * lldpctl_atom_get_int(), @ref lldpctl_atom_get_str() or @ref
556 * lldpctl_atom_get_buffer() but some information can only be retrieved through
557 * special functions because IO operation is needed (and also, for some of them,
558 * because we don't have an atom yet).
559 *
560 * @{
561 */
562
8729d69f
VB
563/**
564 * Retrieve global configuration of lldpd daemon.
565 *
566 * @param conn Connection with lldpd.
567 * @return The global configuration or @c NULL if an error happened.
568 *
569 * This function will make IO with the daemon to get the
570 * configuration. Depending on the IO model, information may not be available
571 * right now and the function should be called again later. If @c NULL is
572 * returned, check the last error. If it is @c LLDPCTL_ERR_WOULDBLOCK, try again
573 * later.
574 */
575lldpctl_atom_t *lldpctl_get_configuration(lldpctl_conn_t *conn);
576
4b292b55
VB
577/**
578 * Retrieve the list of available interfaces.
579 *
b1eceab6 580 * @param conn Previously allocated handler to a connection to lldpd.
4b292b55
VB
581 * @return The list of available ports or @c NULL if an error happened.
582 *
583 * This function will make IO with the daemon to get the list of
584 * ports. Depending on the IO model, information may not be available right now
585 * and the function should be called again later. If @c NULL is returned, check
586 * what the last error is. If it is @c LLDPCTL_ERR_WOULDBLOCK, try again later
587 * (when more data is available).
b1eceab6
VB
588 *
589 * The list of available ports can be iterated with @ref lldpctl_atom_foreach().
4b292b55 590 */
b1eceab6 591lldpctl_atom_t *lldpctl_get_interfaces(lldpctl_conn_t *conn);
4b292b55 592
99ef55d3
VB
593/**
594 * Retrieve the information related to the local chassis.
595 *
596 * @param conn Previously allocated handler to a connection to lldpd.
597 * @return Atom related to the local chassis which may be used in subsequent functions.
598 *
599 * This function may have to do IO to get the information related to the local
600 * chassis. Depending on the IO mode, information may not be available right now
601 * and the function should be called again later. If @c NULL is returned, check
602 * what the last error is. If it is @c LLDPCTL_ERR_WOULDBLOCK, try again later
603 * (when more data is available).
604 */
605lldpctl_atom_t *lldpctl_get_local_chassis(lldpctl_conn_t *conn);
606
4b292b55
VB
607/**
608 * Retrieve the information related to a given interface.
609 *
610 * @param port The port we want to retrieve information from. This port is an
b1eceab6 611 * atom retrieved from an interation on @c lldpctl_get_interfaces().
4b292b55
VB
612 * @return Atom related to this port which may be used in subsequent functions.
613 *
9da663f7 614 * This function may have to do IO to get the information related to the given
99ef55d3 615 * port. Depending on the IO mode, information may not be available right now
4b292b55
VB
616 * and the function should be called again later. If @c NULL is returned, check
617 * what the last error is. If it is @c LLDPCTL_ERR_WOULDBLOCK, try again later
618 * (when more data is available).
619 */
620lldpctl_atom_t *lldpctl_get_port(lldpctl_atom_t *port);
621
9da663f7
VB
622/**
623 * Retrieve the default port information.
624 *
625 * This port contains default settings whenever a new port needs to be created.
626 *
627 * @param conn Previously allocated handler to a connection to lldpd.
628 * @return Atom of the default port which may be used in subsequent functions.
629 *
630 * This function may have to do IO to get the information related to the given
631 * port. Depending on the IO mode, information may not be available right now
632 * and the function should be called again later. If @c NULL is returned, check
633 * what the last error is. If it is @c LLDPCTL_ERR_WOULDBLOCK, try again later
634 * (when more data is available).
635 */
636lldpctl_atom_t *lldpctl_get_default_port(lldpctl_conn_t *conn);
637
b1eceab6
VB
638/**@}*/
639
4b292b55
VB
640/**
641 * Piece of information that can be retrieved from/written to an atom.
642 *
643 * Each piece of information can potentially be retrieved as an atom (A), a
644 * string (S), a buffer (B) or an integer (I). Additionaly, when an information
645 * can be retrieved as an atom, it is usually iterable (L). When an atom can be
646 * retrieved as a string and as an additional type, the string is expected to be
647 * formatted. For example, the MAC address of a local port can be retrieved as a
648 * buffer and a string. As a string, you'll get something like
649 * "00:11:22:33:44:55". Also, all values that can be get as an integer or a
650 * buffer can be get as a string too. There is no special formatting in this
651 * case. "(BS)" means that the string get a special appropriate format.
652 *
653 * The name of a key is an indication on the type of atom that information can
654 * be extracted from. For example, @c lldpctl_k_med_policy_type can be extracted
655 * from an atom you got by iterating on @c lldpctl_k_port_med_policies. On the
656 * other hand, @c lldpctl_k_port_descr and @c lldpctl_k_chassis can be retrieved
657 * from an atom retrieved either by iterating @c lldpctl_k_port_neighbors or
658 * with @c lldpctl_get_port().
659 *
660 * Some values may be written. They are marked with (W). Such a change may or
661 * may not be transmitted immediatly. If they are not transmitted immediatly,
662 * this means that the resulting atom should be written to another atom. For
663 * example, when writting @c lldpctl_k_med_policy_tagged, you need to write the
664 * resulting atom to @c lldpctl_k_port_med_policies. If the change is
665 * transmitted immediatly, you need to check the error status of the connection
666 * to know if it has been transmitted correctly. Notably, if you get @c
667 * LLDPCTL_ERR_WOULDBLOCK, you need to try again later. Usually, changes are
668 * transmitted immediatly. The exception are changes that need to be grouped to
669 * be consistent, like a LLDP MED location. When a change is transmitted
28431fae
VB
670 * immediatly, it is marked with (O). @c lldpctl_atom_set_str() may accept a @c
671 * NULL value. This case is marked with (N) and usually reset the item to the
672 * default value or no value.
4b292b55
VB
673 *
674 * Some values may also be created. They are flagged with (C). This only applies
675 * to elements that can be iterated (L) and written (W). The element created
676 * still needs to be appended to the list by being written to it. The creation
677 * is done with @c lldpctl_atom_create().
678 *
679 * An atom marked with (S) can be retrieved as a string only. It cannot be
680 * written. An atom marked with (IS) can be retrieved as an integer and features
681 * an appropriate representation as a string (usually, the name of a constant)
682 * which is more meaningful than just the integer. An atom marked as (I) can be
db76b228
VB
683 * retrieved as an integer and as a string. In the later case, this is just a
684 * string representation of the integer. An atom marked with (AL) can be
685 * retrieved as an atom only and can be iterated over. This is usually a list of
686 * things. An atom marked (I,W) can be read as an integer or a string and can be
687 * written as an integer. The change would not be commited until the atom is
688 * written to the nearest atom supporting (A,WO) operation (eventually with an
689 * indirection, i.e first write to a (A,W), then to a (A,WO)).
4b292b55
VB
690 */
691typedef enum {
8b549648
VB
692 lldpctl_k_config_tx_interval, /**< `(I,WO)` Transmit interval. When set to -1,
693 it is meant to transmit now. */
694 lldpctl_k_config_receiveonly, /**< `(I)` Receive only mode */
695 lldpctl_k_config_mgmt_pattern, /**< `(S,WON)` Pattern to choose the management
696 address */
28431fae 697 lldpctl_k_config_iface_pattern, /**< `(S,WON)` Pattern of enabled interfaces */
8b549648
VB
698 lldpctl_k_config_cid_pattern, /**< `(S)` Interface pattern to choose the chassis
699 ID */
700 lldpctl_k_config_description, /**< `(S,WON)` Chassis description overridden */
701 lldpctl_k_config_platform, /**< `(S,WON)` Platform description overridden (CDP)
702 */
703 lldpctl_k_config_hostname, /**< `(S,WON)` System name overridden */
704 lldpctl_k_config_advertise_version, /**< `(I)` Advertise version */
b1eceab6 705 lldpctl_k_config_lldpmed_noinventory, /**< `(I)` Disable LLDP-MED inventory */
7742be39 706 lldpctl_k_config_paused, /**< `(I,WO)` lldpd is paused */
8b549648
VB
707 lldpctl_k_config_fast_start_enabled, /**< `(I,WO)` Is fast start enabled */
708 lldpctl_k_config_fast_start_interval, /**< `(I,WO)` Start fast transmit interval
709 */
710 lldpctl_k_config_ifdescr_update, /**< `(I,WO)` Enable or disable setting
711 interface description */
712 lldpctl_k_config_iface_promisc, /**< `(I,WO)` Enable or disable promiscuous mode
713 on interfaces */
714 lldpctl_k_config_chassis_cap_advertise, /**< `(I,WO)` Enable or disable chassis
715 capabilities advertisement */
716 lldpctl_k_config_chassis_mgmt_advertise, /**< `(I,WO)` Enable or disable
717 management addresses advertisement
718 */
719 lldpctl_k_config_cid_string, /**< `(S,WON)` User defined string for the chassis
720 ID */
721 lldpctl_k_config_perm_iface_pattern, /**< `(S,WON)` Pattern of permanent
722 interfaces */
723 lldpctl_k_config_tx_interval_ms, /**< `(I,WO)` Transmit interval in
724 milliseconds. Set to -1 to transmit now. */
725 lldpctl_k_config_chassis_cap_override, /**< `(I,WO)` Override chassis
726 capabilities */
b1eceab6 727
d1dc456f 728 lldpctl_k_interface_name = 1000, /**< `(S)` The interface name. */
b1eceab6 729
8b549648
VB
730 lldpctl_k_port_name =
731 1100, /**< `(S)` The port name. Only works for a local port. */
732 lldpctl_k_port_index, /**< `(I)` The port index. Only works for a local port. */
4b292b55 733 /**
b1eceab6 734 * `(AL)` The list of known neighbors for this port.
4b292b55
VB
735 *
736 * A neighbor is in fact a remote port.
737 */
d1dc456f 738 lldpctl_k_port_neighbors = 1200,
8b549648
VB
739 lldpctl_k_port_protocol, /**< `(IS)` The protocol that was used to retrieve this
740 information. */
741 lldpctl_k_port_age, /**< `(I)` Age of information, seconds from epoch. */
b1eceab6 742 lldpctl_k_port_id_subtype, /**< `(IS)` The subtype ID of this port. */
8e46010c 743 lldpctl_k_port_id, /**< `(BS,WO)` The ID of this port. */
8003e789 744 lldpctl_k_port_descr, /**< `(S,WO)` The description of this port. */
8b549648
VB
745 lldpctl_k_port_hidden, /**< `(I)` Is this port hidden (or should it be
746 displayed?)? */
747 lldpctl_k_port_status, /**< `(IS,WO)` Operational status of this (local) port */
748 lldpctl_k_port_chassis, /**< `(A)` Chassis associated to the port */
749 lldpctl_k_port_ttl, /**< `(I)` TTL for port, 0 if info is attached to chassis */
750 lldpctl_k_port_vlan_tx, /**< `(I,W)` VLAN tag for TX on port, -1 VLAN disabled
751 */
752
753 lldpctl_k_port_dot3_mfs = 1300, /**< `(I)` MFS */
754 lldpctl_k_port_dot3_aggregid, /**< `(I)` Port aggregation ID */
755 lldpctl_k_port_dot3_autoneg_support, /**< `(I)` Autonegotiation support. */
756 lldpctl_k_port_dot3_autoneg_enabled, /**< `(I)` Autonegotiation enabled. */
757 lldpctl_k_port_dot3_autoneg_advertised, /**< `(I)` Advertised protocols. See
758 `LLDP_DOT3_LINK_AUTONEG_*` */
759 lldpctl_k_port_dot3_mautype, /**< `(IS)` Current MAU type. See `LLDP_DOT3_MAU_*`
760 */
b1eceab6 761
d1dc456f 762 lldpctl_k_port_dot3_power = 1400, /**< `(A,WO)` Dot3 power related stuff. */
8b549648
VB
763 lldpctl_k_dot3_power_devicetype, /**< `(IS,W)` Device type. See
764 `LLDP_DOT3_POWER_PSE/PD` */
765 lldpctl_k_dot3_power_supported, /**< `(I,W)` Is MDI power supported. */
766 lldpctl_k_dot3_power_enabled, /**< `(I,W)` Is MDI power enabled. */
b1eceab6 767 lldpctl_k_dot3_power_paircontrol, /**< `(I,W)` Pair-control enabled? */
8b549648
VB
768 lldpctl_k_dot3_power_pairs, /**< `(IS,W)` See `LLDP_DOT3_POWERPAIRS_*` */
769 lldpctl_k_dot3_power_class, /**< `(IS,W)` Power class. */
770 lldpctl_k_dot3_power_type, /**< `(I,W)` 802.3AT power type */
771 lldpctl_k_dot3_power_source, /**< `(IS,W)` 802.3AT power source */
772 lldpctl_k_dot3_power_priority, /**< `(IS,W)` 802.3AT power priority */
773 lldpctl_k_dot3_power_allocated, /**< `(I,W)` 802.3AT power allocated */
774 lldpctl_k_dot3_power_requested, /**< `(I,W)` 802.3AT power requested */
b1eceab6 775
7cfcd3b7 776 /* 802.3bt additions */
8b549648
VB
777 lldpctl_k_dot3_power_pd_4pid, /**< `(IS,W)` 802.3BT both modes supported? */
778 lldpctl_k_dot3_power_requested_a, /**< `(I,W)` 802.3BT power value requested for
779 A */
780 lldpctl_k_dot3_power_requested_b, /**< `(I,W)` 802.3BT power value requested for
781 B */
782 lldpctl_k_dot3_power_allocated_a, /**< `(I,W)` 802.3BT power value allocated for
783 A */
784 lldpctl_k_dot3_power_allocated_b, /**< `(I,W)` 802.3BT power value allocated for
785 B */
786 lldpctl_k_dot3_power_pse_status, /**< `(IS,W)` 802.3BT PSE powering status */
787 lldpctl_k_dot3_power_pd_status, /**< `(IS,W)` 802.3BT PD powering status */
7cfcd3b7 788 lldpctl_k_dot3_power_pse_pairs_ext, /**< `(IS,W)` 802.3BT PSE power pairs */
8b549648
VB
789 lldpctl_k_dot3_power_class_a, /**< `(IS,W)` 802.3BT power class for A */
790 lldpctl_k_dot3_power_class_b, /**< `(IS,W)` 802.3BT power class for B */
791 lldpctl_k_dot3_power_class_ext, /**< `(IS,W)` 802.3BT power class */
792 lldpctl_k_dot3_power_type_ext, /**< `(IS,W)` 802.3BT power type */
793 lldpctl_k_dot3_power_pd_load, /**< `(IS,W)` 802.3BT dualsig isolated? */
7cfcd3b7
PD
794 lldpctl_k_dot3_power_pse_max, /**< `(I,W)` 802.3BT maximum available power */
795
d1dc456f 796 lldpctl_k_port_vlan_pvid = 1500, /**< `(I)` Primary VLAN ID */
8b549648
VB
797 lldpctl_k_port_vlans, /**< `(AL)` List of VLAN */
798 lldpctl_k_vlan_id, /**< `(I)` VLAN ID */
799 lldpctl_k_vlan_name, /**< `(S)` VLAN name */
b1eceab6 800
d1dc456f 801 lldpctl_k_port_ppvids = 1600, /**< `(AL)` List of PPVIDs */
b1eceab6 802 lldpctl_k_ppvid_status, /**< `(I)` Status of PPVID (see `LLDP_PPVID_CAP_*`) */
8b549648 803 lldpctl_k_ppvid_id, /**< `(I)` ID of PPVID */
b1eceab6 804
d1dc456f 805 lldpctl_k_port_pis = 1700, /**< `(AL)` List of PIDs */
8b549648
VB
806 lldpctl_k_pi_id, /**< `(B)` PID value */
807
808 lldpctl_k_chassis_index = 1800, /**< `(I)` The chassis index. */
809 lldpctl_k_chassis_id_subtype, /**< `(IS)` The subtype ID of this chassis. */
810 lldpctl_k_chassis_id, /**< `(BS)` The ID of this chassis. */
811 lldpctl_k_chassis_name, /**< `(S)` The name of this chassis. */
812 lldpctl_k_chassis_descr, /**< `(S)` The description of this chassis. */
813 lldpctl_k_chassis_cap_available, /**< `(I)` Available capabilities (see
814 `LLDP_CAP_*`) */
815 lldpctl_k_chassis_cap_enabled, /**< `(I)` Enabled capabilities (see
816 `LLDP_CAP_*`) */
b1eceab6 817 lldpctl_k_chassis_mgmt, /**< `(AL)` List of management addresses */
71b0f981 818 lldpctl_k_chassis_ttl, /**< Deprecated */
b1eceab6 819
8b549648
VB
820 lldpctl_k_chassis_med_type =
821 1900, /**< `(IS)` Chassis MED type. See `LLDP_MED_CLASS_*` */
822 lldpctl_k_chassis_med_cap, /**< `(I)` Available MED capabilities. See
823 `LLDP_MED_CAP_*` */
824 lldpctl_k_chassis_med_inventory_hw, /**< `(S,W)` LLDP MED inventory "Hardware
825 Revision" */
826 lldpctl_k_chassis_med_inventory_sw, /**< `(S,W)` LLDP MED inventory "Software
827 Revision" */
828 lldpctl_k_chassis_med_inventory_fw, /**< `(S,W)` LLDP MED inventory "Firmware
829 Revision" */
830 lldpctl_k_chassis_med_inventory_sn, /**< `(S,W)` LLDP MED inventory "Serial
831 Number" */
832 lldpctl_k_chassis_med_inventory_manuf, /**< `(S,W)` LLDP MED inventory
833 "Manufacturer" */
834 lldpctl_k_chassis_med_inventory_model, /**< `(S,W)` LLDP MED inventory "Model"
835 */
836 lldpctl_k_chassis_med_inventory_asset, /**< `(S,W)` LLDP MED inventory "Asset
837 ID" */
838
839 lldpctl_k_port_med_policies =
840 2000, /**< `(AL,WO)` MED policies attached to a port. */
841 lldpctl_k_med_policy_type, /**< `(IS,W)` MED policy app type. See
842 `LLDP_MED_APPTYPE_*`. 0 if a policy is not
843 defined. */
b1eceab6 844 lldpctl_k_med_policy_unknown, /**< `(I,W)` Is MED policy defined? */
8b549648
VB
845 lldpctl_k_med_policy_tagged, /**< `(I,W)` MED policy tagging */
846 lldpctl_k_med_policy_vid, /**< `(I,W)` MED policy VID */
b1eceab6
VB
847 lldpctl_k_med_policy_priority, /**< `(I,W)` MED policy priority */
848 lldpctl_k_med_policy_dscp, /**< `(I,W)` MED policy DSCP */
849
8b549648
VB
850 lldpctl_k_port_med_locations =
851 2100, /**< `(AL,WO)` MED locations attached to a port. */
852 lldpctl_k_med_location_format, /**< `(IS,W)` MED location format. See
853 * `LLDP_MED_LOCFORMAT_*`. 0 if this
854 * location is not defined. When written,
855 * the following fields will be zeroed
856 * out. */
857 lldpctl_k_med_location_geoid, /**< `(IS,W)` MED geoid. See
858 `LLDP_MED_LOCATION_GEOID_*`. Only if format is
859 COORD. */
860 lldpctl_k_med_location_latitude, /**< `(S,W)` MED latitude. Only if format is
861 COORD. */
862 lldpctl_k_med_location_longitude, /**< `(S,W)` MED longitude. Only if format is
863 COORD. */
864 lldpctl_k_med_location_altitude, /**< `(S,W)` MED altitude. Only if format is
865 COORD. */
866 lldpctl_k_med_location_altitude_unit, /**< `(S,W)` MED altitude unit. See
867 * `LLDP_MED_LOCATION_ALTITUDE_UNIT_*`.
4b292b55
VB
868 * Only if format is COORD. */
869
8b549648
VB
870 lldpctl_k_med_location_country =
871 2200, /**< `(S,W)` MED country. Only if format is CIVIC. */
b1eceab6 872 lldpctl_k_med_location_elin, /**< `(S,W)` MED ELIN. Only if format is ELIN. */
4b292b55 873
8b549648
VB
874 lldpctl_k_med_location_ca_elements =
875 2300, /**< `(AL,WC)` MED civic address elements. Only if format is CIVIC */
876 lldpctl_k_med_civicaddress_type, /**< `(IS,W)` MED civic address type. */
b1eceab6 877 lldpctl_k_med_civicaddress_value, /**< `(S,W)` MED civic address value. */
4b292b55 878
d1dc456f 879 lldpctl_k_port_med_power = 2400, /**< `(A,WO)` LLDP-MED power related stuff. */
8b549648
VB
880 lldpctl_k_med_power_type, /**< `(IS,W)` LLDP MED power device type. See
881 `LLDP_MED_POW_TYPE_*` */
882 lldpctl_k_med_power_source, /**< `(IS,W)` LLDP MED power source. See
883 `LLDP_MED_POW_SOURCE_*` */
884 lldpctl_k_med_power_priority, /**< `(IS,W)` LLDP MED power priority. See
885 `LLDP_MED_POW_PRIO_*` */
886 lldpctl_k_med_power_val, /**< `(I,W)` LLDP MED power value */
887
888 lldpctl_k_mgmt_ip = 3000, /**< `(S)` IP address */
889 lldpctl_k_mgmt_iface_index = 30001, /**< `(I)` Interface index */
890
891 lldpctl_k_tx_cnt = 4000, /**< `(I)` tx cnt. Only works for a local port. */
892 lldpctl_k_rx_cnt, /**< `(I)` rx cnt. Only works for a local port. */
893 lldpctl_k_rx_discarded_cnt, /**< `(I)` discarded cnt. Only works for a local
894 port. */
895 lldpctl_k_rx_unrecognized_cnt, /**< `(I)` unrecognized cnt. Only works for a
896 local port. */
897 lldpctl_k_ageout_cnt, /**< `(I)` ageout cnt. Only works for a local port. */
898 lldpctl_k_insert_cnt, /**< `(I)` insert cnt. Only works for a local port. */
899 lldpctl_k_delete_cnt, /**< `(I)` delete cnt. Only works for a local port. */
c10302a3 900 lldpctl_k_config_tx_hold, /**< `(I,WO)` Transmit hold interval. */
8b549648
VB
901 lldpctl_k_config_bond_slave_src_mac_type, /**< `(I,WO)` bond slave src mac type.
902 */
8fbd3195 903 lldpctl_k_config_lldp_portid_type, /**< `(I,WO)` LLDP PortID TLV Subtype */
8b549648
VB
904 lldpctl_k_config_lldp_agent_type, /**< `(I,WO)` LLDP agent type */
905 lldpctl_k_config_max_neighbors, /**< `(I,WO)`Maximum number of neighbors per
906 port. */
907
908 lldpctl_k_custom_tlvs = 5000, /**< `(AL)` custom TLVs */
909 lldpctl_k_custom_tlvs_clear, /**< `(WO)` clear list of custom TLVs */
910 lldpctl_k_custom_tlv, /**< `(AL,WO)` custom TLV **/
911 lldpctl_k_custom_tlv_oui, /**< `(B,W)` custom TLV Organizationally Unique
912 Identifier. Default is 0 (3 bytes) */
913 lldpctl_k_custom_tlv_oui_subtype, /**< `(I,W)` custom TLV subtype. Default is 0
914 (1 byte) */
915 lldpctl_k_custom_tlv_oui_info_string, /**< `(BS,W)` custom TLV Organizationally
916 Unique Identifier Information String
917 (up to 507 bytes) */
918 lldpctl_k_custom_tlv_op, /**< `(S,W)` custom TLV operation */
c576fd21 919
4b292b55
VB
920} lldpctl_key_t;
921
6fcf11ca
VB
922/**
923 * Get a map related to a key.
924 *
925 * Many keys expect to be written with a discrete number of values. Take for
926 * example @c lldpctl_k_med_civicaddress_type, it can take any integer between 1
927 * and 128. However, each integer can be named. It can be useful for an
928 * application to get a translation between the integer that can be provided and
929 * a more human-readable name. This function allows to retrieve the
930 * corresponding map.
931 *
932 * @param key The piece of information we want a map from.
9cac8fed 933 * @return The map or @c NULL if no map is available.
6fcf11ca
VB
934 *
935 * The returned map has its last element set to 0. It is also expected that the
936 * string value can be used with a set operation. It will be translated to the
937 * integer value.
938 */
939lldpctl_map_t *lldpctl_key_get_map(lldpctl_key_t key);
940
4b292b55
VB
941/**
942 * Retrieve a bit of information as an atom.
943 *
944 * @param atom The atom we want to query.
945 * @param key The information we want from the atom.
946 * @return The atom representing the requested information or @c NULL if the
947 * information is not available.
948 *
949 * Not every value of @c info will be available as an atom. See the
950 * documentation of @c lldpctl_key_t for values accepting to be extracted as an
951 * atom. Usually, this is only iterable values or values representing a complex
952 * object.
953 *
954 * The provided atom is not a _borrowed_ reference. You need to decrement the
955 * reference count when you don't need it anymore.
956 *
957 * As a convenience, this function will return @c NULL if the first parameter is
958 * @c NULL and no error will be raised.
959 */
960lldpctl_atom_t *lldpctl_atom_get(lldpctl_atom_t *atom, lldpctl_key_t key);
961
962/**
963 * Set a bit of information with an atom.
964 *
965 * @param atom The atom we want to write to.
966 * @param key The key information we want to write.
967 * @param value The value of the information we want to write.
968 * @return The updated atom with the appropriate information.
969 *
970 * This function will return @c NULL in case of error. If the last error is @c
971 * LLDPCTL_ERR_WOULDBLOCK, the write should be retried later with the exact same
972 * parameters. LLDPCTL_ERR_BAD_VALUE is raised when the provided atom is not
973 * correct.
974 */
975lldpctl_atom_t *lldpctl_atom_set(lldpctl_atom_t *atom, lldpctl_key_t key,
976 lldpctl_atom_t *value);
977
978/**
979 * Retrieve a bit of information as a null-terminated string.
980 *
981 * @param atom The atom we want to query.
982 * @param key The information we want from the atom.
983 * @return The requested string or @c NULL if the information is not available.
984 *
985 * Not every value of @c info will be available as a string. See the
986 * documentation of @c lldpctl_key_t for values accepting to be extracted as a
987 * string. Usually, only piece of information stored as string are available in
988 * this form but sometimes, you can get a nice formatted string instead of an
989 * integer with this function.
990 *
991 * As a convenience, this function will return @c NULL if the first parameter is
992 * @c NULL and no error will be raised.
993 *
994 * The provided string may live inside the atom providing it. If you need it
995 * longer, duplicate it.
996 */
997const char *lldpctl_atom_get_str(lldpctl_atom_t *atom, lldpctl_key_t key);
998
999/**
1000 * Set a bit of information using a null-terminated string.
1001 *
1002 * @param atom The atom we want to write to.
1003 * @param key The key information we want to write.
1004 * @param value The value of the information we want to write.
1005 * @return The updated atom with the appropriate information.
1006 *
1007 * This function will return @c NULL in case of error. If the last error is @c
1008 * LLDPCTL_ERR_WOULDBLOCK, the write should be retried later with the exact same
1009 * parameters. LLDPCTL_ERR_BAD_VALUE is raised when the provided atom is not
1010 * correct.
1011 */
1012lldpctl_atom_t *lldpctl_atom_set_str(lldpctl_atom_t *atom, lldpctl_key_t key,
1013 const char *value);
1014
1015/**
1016 * Retrieve a bit of information as a buffer.
1017 *
1018 * @param atom The atom we want to query.
1019 * @param key The information we want from the atom.
b1eceab6 1020 * @param[out] length The size of the returned buffer.
4b292b55
VB
1021 * @return The requested buffer or @c NULL if the information is not available.
1022 *
1023 * Not every value of @c info will be available as a buffer. See the
1024 * documentation of @c lldpctl_key_t for values accepting to be extracted as a
1025 * string. Usually, only piece of information stored as buffer are available in
1026 * this form.
1027 *
1028 * As a convenience, this function will return @c NULL if the first parameter is
1029 * @c NULL and no error will be raised. If this function returns @c NULL, the
1030 * third parameter is set to 0.
1031 *
1032 * The provided buffer may live inside the atom providing it. If you need it
1033 * longer, duplicate it.
1034 */
ae5cae02 1035const uint8_t *lldpctl_atom_get_buffer(lldpctl_atom_t *atom, lldpctl_key_t key,
4b292b55
VB
1036 size_t *length);
1037
1038/**
1039 * Set a bit of information using a buffer
1040 *
1041 * @param atom The atom we want to write to.
1042 * @param key The key information we want to write.
1043 * @param value The value of the information we want to write.
1044 * @param length The length of the provided buffer.
1045 * @return The updated atom with the appropriate information.
1046 *
1047 * This function will return @c NULL in case of error. If the last error is @c
1048 * LLDPCTL_ERR_WOULDBLOCK, the write should be retried later with the exact same
1049 * parameters. LLDPCTL_ERR_BAD_VALUE is raised when the provided atom is not
1050 * correct.
1051 */
1052lldpctl_atom_t *lldpctl_atom_set_buffer(lldpctl_atom_t *atom, lldpctl_key_t key,
ae5cae02 1053 const uint8_t *value, size_t length);
4b292b55
VB
1054
1055/**
1056 * Retrieve a bit of information as an integer.
1057 *
1058 * @param atom The atom we want to query.
1059 * @param key The information we want from the atom.
1060 * @return The requested integer or -1 if the information is not available
1061 *
1062 * Not every value of @c info will be available as an integer. See the
1063 * documentation of @c lldpctl_key_t for values accepting to be extracted as a
1064 * string. Usually, only piece of information stored as an integer are available
1065 * in this form.
1066 *
1067 * Only @c lldpctl_last_error() can tell if the returned value is an error or
1068 * not. However, most values extracted from lldpd cannot be negative.
1069 */
1070long int lldpctl_atom_get_int(lldpctl_atom_t *atom, lldpctl_key_t key);
1071
1072/**
1073 * Set a bit of information using an integer
1074 *
1075 * @param atom The atom we want to write to.
1076 * @param key The key information we want to write.
1077 * @param value The value of the information we want to write.
1078 * @return The updated atom with the appropriate information.
1079 *
1080 * This function will return @c NULL in case of error. If the last error is @c
1081 * LLDPCTL_ERR_WOULDBLOCK, the write should be retried later with the exact same
1082 * parameters. LLDPCTL_ERR_BAD_VALUE is raised when the provided atom is not
1083 * correct.
1084 */
1085lldpctl_atom_t *lldpctl_atom_set_int(lldpctl_atom_t *atom, lldpctl_key_t key,
1086 long int value);
1087
b1eceab6
VB
1088/**
1089 * @defgroup liblldpctl_atom_iter Iterating over atoms
1090 *
1091 * Iterate over atoms (lists).
1092 *
1093 * @{
1094 */
4b292b55
VB
1095/**
1096 * Iterator over an iterable atom (a list of ports, a list of VLAN, ...). When
1097 * an atom is a list, it can be iterated over to extract the appropriate values.
1098 *
1099 * @see lldpctl_atom_iter(), lldpctl_atom_iter_next(), lldpctl_atom_iter_value()
1100 */
1101typedef struct lldpctl_atom_iter_t lldpctl_atom_iter_t;
1102
1103/**
1104 * Return an iterator over a given atom.
1105 *
1106 * If an atom is iterable (if it is a list, like a list of ports, a list of
1107 * VLAN, a list of neighbors), it is possible to iterate over it. First use this
1108 * function to get an iterator then use @c lldpctl_atom_iter_next() to get the
1109 * next item and @c lldpctl_atom_iter_value() to the actuel item.
1110 *
1111 * @param atom The atom we want to create an iterator from.
1112 * @return The iterator or @c NULL if an error happened or if the atom is empty
1113 * (check with @c lldpctl_last_error()).
1114 *
1115 * As a convenience, if the provided atom is @c NULL, this function will return
1116 * @c NULL and no error will be raised.
1117 */
1118lldpctl_atom_iter_t *lldpctl_atom_iter(lldpctl_atom_t *atom);
1119
1120/**
1121 * Return the next element of an iterator.
1122 *
1123 * @param atom The atom we are currently iterating.
1124 * @param iter The iterator we want the next element from.
1125 * @return An iterator starting on the next element or @c NULL if we have no
1126 * more elements
1127 *
1128 * @see lldpctl_atom_iter(), lldpctl_atom_iter_value().
1129 *
1130 * As a convenience, if the provided atom is @c NULL, this function will return
1131 * @c NULL and no error will be raised.
1132 */
8b549648
VB
1133lldpctl_atom_iter_t *lldpctl_atom_iter_next(lldpctl_atom_t *atom,
1134 lldpctl_atom_iter_t *iter);
4b292b55
VB
1135
1136/**
1137 * Return the value of an iterator.
1138 *
1139 * @param atom The atom we are currently iterating.
1140 * @param iter The iterator we want the next element from.
1141 * @return The atom currently associated with the iterator.
1142 *
1143 * @see lldpctl_atom_iter(), lldpctl_atom_iter_next().
1144 */
8b549648
VB
1145lldpctl_atom_t *lldpctl_atom_iter_value(lldpctl_atom_t *atom,
1146 lldpctl_atom_iter_t *iter);
4b292b55
VB
1147
1148/**
1149 * Convenience macro to iter over every value of an iterable object.
1150 *
1151 * @param atom The atom you want to iterate on.
3c5634c1 1152 * @param value Atom name that will be used to contain each value.
4b292b55 1153 *
3c5634c1
VB
1154 * This macro behaves as a for loop. Moreover, at the end of each iteration, the
1155 * reference count of the provided value is decremented. If you need to use it
1156 * outside of the loop, you need to increment it.
4b292b55 1157 */
8b549648
VB
1158#define lldpctl_atom_foreach(atom, value) \
1159 for (lldpctl_atom_iter_t *iter##_LINE_ = lldpctl_atom_iter(atom); \
1160 iter##_LINE_ && (value = lldpctl_atom_iter_value(atom, iter##_LINE_)); \
1161 iter##_LINE_ = lldpctl_atom_iter_next(atom, iter##_LINE_), \
1162 lldpctl_atom_dec_ref(value))
4b292b55
VB
1163
1164/**
1165 * Create a new value for an iterable element.
1166 *
1167 * The value is meant to be appended using @c lldpctl_atom_set(). Currently,
1168 * there is no way to delete an element from a list. It is also not advisable to
1169 * use getters on a newly created object until it is fully initialized. If its
1170 * internal representation is using a buffer, it may not be initialized until
1171 * the first set.
1172 *
1173 * @param atom The atom we want to create a new element for.
1174 * @return The new element.
1175 */
1176lldpctl_atom_t *lldpctl_atom_create(lldpctl_atom_t *atom);
b1eceab6
VB
1177/**@}*/
1178/**@}*/
4b292b55
VB
1179
1180#ifdef __cplusplus
1181}
1182#endif
1183
b1eceab6
VB
1184/**@}*/
1185
4b292b55 1186#endif