1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018, Olivier MATZ <zer0@droids-corp.org>
6 * @defgroup config Node configuration
9 * @brief Configure nodes behavior through generic API.
15 #include <sys/queue.h>
24 * The type identifier for a config value.
27 EC_CONFIG_TYPE_NONE = 0,
30 EC_CONFIG_TYPE_UINT64,
31 EC_CONFIG_TYPE_STRING,
38 * Structure describing the format of a configuration value.
40 * This structure is used in a const array which is referenced by a
41 * struct ec_config. Each entry of the array represents a key/value
42 * storage of the configuration dictionary.
44 struct ec_config_schema {
45 const char *key; /**< The key string (NULL for list elts). */
46 const char *desc; /**< A description of the value. */
47 enum ec_config_type type; /**< Type of the value */
48 /* XXX flags: mandatory */
51 /** If type is dict or list, the schema of the dict or list
52 * elements. Else must be NULL. */
53 const struct ec_config_schema *subschema;
56 TAILQ_HEAD(ec_config_list, ec_config);
59 * Structure storing the configuration data.
62 /** type of value stored in the union */
63 enum ec_config_type type;
66 bool boolean; /** Boolean value */
67 int64_t i64; /** Signed integer value */
68 uint64_t u64; /** Unsigned integer value */
69 char *string; /** String value */
70 struct ec_node *node; /** Node value */
71 struct ec_dict *dict; /** Hash table value */
72 struct ec_config_list list; /** List value */
76 * Next in list, only valid if type is list.
78 TAILQ_ENTRY(ec_config) next;
84 * Validate a configuration schema array.
87 * Pointer to the first element of the schema array. The array
88 * must be terminated by a sentinel entry (type == EC_CONFIG_TYPE_NONE).
90 * 0 if the schema is valid, or -1 on error (errno is set).
92 int ec_config_schema_validate(const struct ec_config_schema *schema);
95 * Dump a configuration schema array.
98 * Output stream on which the dump will be sent.
100 * Pointer to the first element of the schema array. The array
101 * must be terminated by a sentinel entry (type == EC_CONFIG_TYPE_NONE).
103 void ec_config_schema_dump(FILE *out, const struct ec_config_schema *schema);
106 * Find a schema entry matching the key.
108 * Browse the schema array and lookup for the given key.
111 * Pointer to the first element of the schema array. The array
112 * must be terminated by a sentinel entry (type == EC_CONFIG_TYPE_NONE).
114 * The schema entry if it matches a key, or NULL if not found.
116 const struct ec_config_schema *
117 ec_config_schema_lookup(const struct ec_config_schema *schema,
121 * Get the type of a schema entry.
124 * Pointer to an element of the schema array.
126 * The type of the schema entry.
129 ec_config_schema_type(const struct ec_config_schema *schema_elt);
132 * Get the subschema of a schema entry.
135 * Pointer to an element of the schema array.
137 * The subschema if any, or NULL.
139 const struct ec_config_schema *
140 ec_config_schema_sub(const struct ec_config_schema *schema_elt);
143 * Check if a key name is reserved in a config dict.
145 * Some key names are reserved and should not be used in configs.
148 * The name of the key to test.
150 * True if the key name is reserved and must not be used, else false.
152 bool ec_config_key_is_reserved(const char *name);
155 * Array of reserved key names.
157 extern const char *ec_config_reserved_keys[];
163 * Get the type of the configuration.
168 * The configuration type.
170 enum ec_config_type ec_config_get_type(const struct ec_config *config);
173 * Create a boolean configuration value.
176 * The boolean value to be set.
178 * The configuration object, or NULL on error (errno is set).
180 struct ec_config *ec_config_bool(bool boolean);
183 * Create a signed integer configuration value.
186 * The signed integer value to be set.
188 * The configuration object, or NULL on error (errno is set).
190 struct ec_config *ec_config_i64(int64_t i64);
193 * Create an unsigned configuration value.
196 * The unsigned integer value to be set.
198 * The configuration object, or NULL on error (errno is set).
200 struct ec_config *ec_config_u64(uint64_t u64);
203 * Create a string configuration value.
206 * The string value to be set. The string is copied into the
207 * configuration object.
209 * The configuration object, or NULL on error (errno is set).
211 struct ec_config *ec_config_string(const char *string);
214 * Create a node configuration value.
217 * The node pointer to be set. The node is "consumed" by
218 * the function and should not be used by the caller, even
219 * on error. The caller can use ec_node_clone() to keep a
220 * reference on the node.
222 * The configuration object, or NULL on error (errno is set).
224 struct ec_config *ec_config_node(struct ec_node *node);
227 * Create a hash table configuration value.
230 * A configuration object containing an empty hash table, or NULL on
231 * error (errno is set).
233 struct ec_config *ec_config_dict(void);
236 * Create a list configuration value.
239 * The configuration object containing an empty list, or NULL on
240 * error (errno is set).
242 struct ec_config *ec_config_list(void);
245 * Add a config object into a list.
248 * The list configuration in which the value will be added.
250 * The value configuration to add in the list. The value object
251 * will be freed when freeing the list object. On error, the
252 * value object is also freed.
254 * 0 on success, else -1 (errno is set).
256 int ec_config_list_add(struct ec_config *list, struct ec_config *value);
259 * Remove an element from a list.
261 * The element is freed and should not be accessed.
264 * The list configuration.
266 * The element to remove from the list.
268 * 0 on success, -1 on error (errno is set).
270 int ec_config_list_del(struct ec_config *list, struct ec_config *config);
273 * Count the number of elements in a list or dict.
276 * The configuration that must be a list or a dict.
278 * The number of elements, or -1 on error (errno is set).
280 ssize_t ec_config_count(const struct ec_config *config);
283 * Validate a configuration.
286 * A hash table configuration to validate.
288 * Pointer to the first element of the schema array. The array
289 * must be terminated by a sentinel entry (type == EC_CONFIG_TYPE_NONE).
291 * 0 on success, -1 on error (errno is set).
293 int ec_config_validate(const struct ec_config *dict,
294 const struct ec_config_schema *schema);
297 * Set a value in a hash table configuration
300 * A hash table configuration to validate.
304 * The value to set. The value object will be freed when freeing the
305 * dict object. On error, the value object is also freed.
307 * 0 on success, -1 on error (errno is set).
309 int ec_config_dict_set(struct ec_config *dict, const char *key,
310 struct ec_config *value);
313 * Remove an element from a hash table configuration.
315 * The element is freed and should not be accessed.
318 * A hash table configuration to validate.
320 * The key of the configuration to delete.
322 * 0 on success, -1 on error (errno is set).
324 int ec_config_dict_del(struct ec_config *config, const char *key);
327 * Compare two configurations.
329 int ec_config_cmp(const struct ec_config *config1,
330 const struct ec_config *config2);
333 * Get configuration value.
335 struct ec_config *ec_config_dict_get(const struct ec_config *config,
339 * Get the first element of a list.
342 * for (config = ec_config_list_iter(list);
344 * config = ec_config_list_next(list, config)) {
349 * The list configuration to iterate.
351 * The first configuration element, or NULL on error (errno is set).
353 struct ec_config *ec_config_list_first(struct ec_config *list);
356 * Get next element in list.
359 * The list configuration beeing iterated.
361 * The current configuration element.
363 * The next configuration element, or NULL if there is no more element.
366 ec_config_list_next(struct ec_config *list, struct ec_config *config);
369 * Free a configuration.
372 * The element to free.
374 void ec_config_free(struct ec_config *config);
377 * Compare two configurations.
380 * 0 if the configurations are equal, else -1.
382 int ec_config_cmp(const struct ec_config *value1,
383 const struct ec_config *value2);
386 * Duplicate a configuration.
389 * The configuration to duplicate.
391 * The duplicated configuration, or NULL on error (errno is set).
394 ec_config_dup(const struct ec_config *config);
397 * Dump a configuration.
400 * Output stream on which the dump will be sent.
402 * The configuration to dump.
404 void ec_config_dump(FILE *out, const struct ec_config *config);