1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018, Olivier MATZ <zer0@droids-corp.org>
13 #ifndef EC_COUNT_OF //XXX
14 #define EC_COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / \
15 ((size_t)(!(sizeof(x) % sizeof(0[x])))))
22 * The type identifier for a config value.
25 EC_CONFIG_TYPE_NONE = 0,
28 EC_CONFIG_TYPE_UINT64,
29 EC_CONFIG_TYPE_STRING,
36 * Structure describing the format of a configuration value.
38 * This structure is used in a const array which is referenced by a
39 * struct ec_config. Each entry of the array represents a key/value
40 * storage of the configuration dictionary.
42 struct ec_config_schema {
43 const char *key; /**< The key string (NULL for list elts). */
44 const char *desc; /**< A description of the value. */
45 enum ec_config_type type; /**< Type of the value */
47 /** If type is dict or list, the schema of the dict or list
48 * elements. Else must be NULL. */
49 const struct ec_config_schema *subschema;
52 TAILQ_HEAD(ec_config_list, ec_config);
55 * Structure storing the configuration data.
58 /** type of value stored in the union */
59 enum ec_config_type type;
62 bool boolean; /** Boolean value */
63 int64_t i64; /** Signed integer value */
64 uint64_t u64; /** Unsigned integer value */
65 char *string; /** String value */
66 struct ec_node *node; /** Node value */
67 struct ec_keyval *dict; /** Hash table value */
68 struct ec_config_list list; /** List value */
72 * Next in list, only valid if type is list.
74 TAILQ_ENTRY(ec_config) next;
80 * Validate a configuration schema array.
83 * Pointer to the first element of the schema array. The array
84 * must be terminated by a sentinel entry (type == EC_CONFIG_TYPE_NONE).
86 * 0 if the schema is valid, or -1 on error (errno is set).
88 int ec_config_schema_validate(const struct ec_config_schema *schema);
91 * Dump a configuration schema array.
94 * Output stream on which the dump will be sent.
96 * Pointer to the first element of the schema array. The array
97 * must be terminated by a sentinel entry (type == EC_CONFIG_TYPE_NONE).
99 void ec_config_schema_dump(FILE *out, const struct ec_config_schema *schema);
102 * Find a schema entry matching the key.
104 * Browse the schema array and lookup for the given key.
107 * Pointer to the first element of the schema array. The array
108 * must be terminated by a sentinel entry (type == EC_CONFIG_TYPE_NONE).
110 * The schema entry if it matches a key, or NULL if not found.
112 const struct ec_config_schema *
113 ec_config_schema_lookup(const struct ec_config_schema *schema,
117 * Check if a key name is reserved in a config dict.
119 * Some key names are reserved and should not be used in configs.
122 * The name of the key to test.
124 * True if the key name is reserved and must not be used, else false.
126 bool ec_config_key_is_reserved(const char *name);
129 * Array of reserved key names.
131 extern const char *ec_config_reserved_keys[];
137 * Get the type of the configuration.
142 * The configuration type.
144 enum ec_config_type ec_config_get_type(const struct ec_config *config);
147 * Create a boolean configuration value.
150 * The boolean value to be set.
152 * The configuration object, or NULL on error (errno is set).
154 struct ec_config *ec_config_bool(bool boolean);
157 * Create a signed integer configuration value.
160 * The signed integer value to be set.
162 * The configuration object, or NULL on error (errno is set).
164 struct ec_config *ec_config_i64(int64_t i64);
167 * Create an unsigned configuration value.
170 * The unsigned integer value to be set.
172 * The configuration object, or NULL on error (errno is set).
174 struct ec_config *ec_config_u64(uint64_t u64);
177 * Create a string configuration value.
180 * The string value to be set. The string is copied into the
181 * configuration object.
183 * The configuration object, or NULL on error (errno is set).
185 struct ec_config *ec_config_string(const char *string);
188 * Create a node configuration value.
191 * The node pointer to be set. The node is "consumed" by
192 * the function and should not be used by the caller, even
193 * on error. The caller can use ec_node_clone() to keep a
194 * reference on the node.
196 * The configuration object, or NULL on error (errno is set).
198 struct ec_config *ec_config_node(struct ec_node *node);
201 * Create a hash table configuration value.
204 * A configuration object containing an empty hash table, or NULL on
205 * error (errno is set).
207 struct ec_config *ec_config_dict(void);
210 * Create a list configuration value.
213 * The configuration object containing an empty list, or NULL on
214 * error (errno is set).
216 struct ec_config *ec_config_list(void);
219 * Add a config object into a list.
222 * The list configuration in which the value will be added.
224 * The value configuration to add in the list. The value object
225 * will be freed when freeing the list object. On error, the
226 * value object is also freed.
228 * 0 on success, else -1 (errno is set).
230 int ec_config_list_add(struct ec_config *list, struct ec_config *value);
233 * Remove an element from a list.
235 * The element is freed and should not be accessed.
238 * The list configuration.
240 * The element to remove from the list.
242 * 0 on success, -1 on error (errno is set).
244 int ec_config_list_del(struct ec_config *list, struct ec_config *config);
247 * Validate a configuration.
250 * A hash table configuration to validate.
252 * Pointer to the first element of the schema array. The array
253 * must be terminated by a sentinel entry (type == EC_CONFIG_TYPE_NONE).
255 * 0 on success, -1 on error (errno is set).
257 int ec_config_validate(const struct ec_config *dict,
258 const struct ec_config_schema *schema);
261 * Set a value in a hash table configuration
264 * A hash table configuration to validate.
268 * The value to set. The value object will be freed when freeing the
269 * dict object. On error, the value object is also freed.
271 * 0 on success, -1 on error (errno is set).
273 int ec_config_dict_set(struct ec_config *dict, const char *key,
274 struct ec_config *value);
277 * Remove an element from a hash table configuration.
279 * The element is freed and should not be accessed.
282 * A hash table configuration to validate.
284 * The key of the configuration to delete.
286 * 0 on success, -1 on error (errno is set).
288 int ec_config_dict_del(struct ec_config *config, const char *key);
291 * Compare two configurations.
293 int ec_config_cmp(const struct ec_config *config1,
294 const struct ec_config *config2);
297 * Get configuration value.
299 struct ec_config *ec_config_dict_get(const struct ec_config *config,
303 * Get the first element of a list.
306 * for (config = ec_config_list_iter(list);
308 * config = ec_config_list_next(list, config)) {
313 * The list configuration to iterate.
315 * The first configuration element, or NULL on error (errno is set).
317 struct ec_config *ec_config_list_first(struct ec_config *list);
320 * Get next element in list.
323 * The list configuration beeing iterated.
325 * The current configuration element.
327 * The next configuration element, or NULL if there is no more element.
330 ec_config_list_next(struct ec_config *list, struct ec_config *config);
333 * Free a configuration.
336 * The element to free.
338 void ec_config_free(struct ec_config *config);
341 * Compare two configurations.
344 * 0 if the configurations are equal, else -1.
346 int ec_config_cmp(const struct ec_config *value1,
347 const struct ec_config *value2);
350 * Duplicate a configuration.
353 * The configuration to duplicate.
355 * The duplicated configuration, or NULL on error (errno is set).
358 ec_config_dup(const struct ec_config *config);
361 * Dump a configuration.
364 * Output stream on which the dump will be sent.
366 * The configuration to dump.
368 void ec_config_dump(FILE *out, const struct ec_config *config);