2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2009, Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
19 * \brief Data retrieval API.
21 * \author Brett Bryant <brettbryant@gmail.com>
22 * \author Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
26 <support_level>core</support_level>
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/_private.h"
37 #include "asterisk/module.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/data.h"
41 #include "asterisk/astobj2.h"
42 #include "asterisk/xml.h"
43 #include "asterisk/cli.h"
44 #include "asterisk/term.h"
45 #include "asterisk/manager.h"
46 #include "asterisk/test.h"
47 #include "asterisk/frame.h"
50 <manager name="DataGet" language="en_US">
52 Retrieve the data api tree.
55 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
56 <parameter name="Path" required="true" />
57 <parameter name="Search" />
58 <parameter name="Filter" />
61 <para>Retrieve the data api tree.</para>
66 #define NUM_DATA_NODE_BUCKETS 59
67 #define NUM_DATA_RESULT_BUCKETS 59
68 #define NUM_DATA_SEARCH_BUCKETS 59
69 #define NUM_DATA_FILTER_BUCKETS 59
71 /*! \brief The last compatible version. */
72 static const uint32_t latest_handler_compatible_version = 0;
74 /*! \brief The last compatible version. */
75 static const uint32_t latest_query_compatible_version = 0;
77 /*! \brief Current handler structure version. */
78 static const uint32_t current_handler_version = AST_DATA_HANDLER_VERSION;
80 /*! \brief Current query structure version. */
81 static const uint32_t current_query_version = AST_DATA_QUERY_VERSION;
83 /*! \brief The data tree to be returned by the callbacks and
84 managed by functions local to this file. */
86 enum ast_data_type type;
88 /*! \brief The node content. */
96 struct in_addr ipaddr;
100 /*! \brief The filter node that depends on the current node,
101 * this is used only when creating the result tree. */
102 const struct data_filter *filter;
104 /*! \brief The list of nodes inside this node. */
105 struct ao2_container *children;
106 /*! \brief The name of the node. */
110 /*! \brief Type of comparisons allow in the search string. */
111 enum data_search_comparison {
114 DATA_CMP_NEQ, /* != */
116 DATA_CMP_GE, /* >= */
121 /*! \brief The list of nodes with their search requirement. */
122 struct ast_data_search {
123 /*! \brief The value of the comparison. */
125 /*! \brief The type of comparison. */
126 enum data_search_comparison cmp_type;
127 /*! \brief reference another node. */
128 struct ao2_container *children;
129 /*! \brief The name of the node we are trying to compare. */
135 /*! \brief The filter node. */
137 /*! \brief node childrens. */
138 struct ao2_container *children;
139 /*! \brief glob list */
140 AST_LIST_HEAD_NOLOCK(glob_list_t, data_filter) glob_list;
141 /*! \brief glob list entry */
142 AST_LIST_ENTRY(data_filter) list;
143 /*! \brief node name. */
147 /*! \brief A data container node pointing to the registered handler. */
148 struct data_provider {
149 /*! \brief node content handler. */
150 const struct ast_data_handler *handler;
151 /*! \brief Module providing this handler. */
152 struct ast_module *module;
153 /*! \brief children nodes. */
154 struct ao2_container *children;
155 /*! \brief Who registered this node. */
156 const char *registrar;
157 /*! \brief Node name. */
161 /*! \brief This structure is used by the iterator. */
162 struct ast_data_iterator {
163 /*! \brief The internal iterator. */
164 struct ao2_iterator internal_iterator;
165 /*! \brief The last returned node. */
166 struct ast_data *last;
167 /*! \brief The iterator pattern. */
169 /*! \brief The compiled patter. */
170 regex_t regex_pattern;
171 /*! \brief is a regular expression. */
172 unsigned int is_pattern:1;
176 /*! \brief The asterisk data main content structure. */
177 struct ao2_container *container;
178 /*! \brief asterisk data locking mechanism. */
182 static void __data_result_print_cli(int fd, const struct ast_data *root, uint32_t depth);
186 * \brief Common string hash function.
189 static int data_provider_hash(const void *obj, const int flags)
191 const struct data_provider *node = obj;
192 return ast_str_case_hash(node->name);
197 * \brief Compare two data_provider's.
200 static int data_provider_cmp(void *obj1, void *obj2, int flags)
202 struct data_provider *node1 = obj1, *node2 = obj2;
203 return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
208 * \brief Common string hash function for data nodes
210 static int data_result_hash(const void *obj, const int flags)
212 const struct ast_data *node = obj;
213 return ast_str_hash(node->name);
218 * \brief Common string comparison function
220 static int data_result_cmp(void *obj, void *arg, int flags)
222 struct ast_data *node1 = obj, *node2 = arg;
223 return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
228 * \brief Lock the data registered handlers structure for writing.
231 #define data_write_lock() ast_rwlock_wrlock(&root_data.lock)
235 * \brief Lock the data registered handlers structure for reading.
238 #define data_read_lock() ast_rwlock_rdlock(&root_data.lock)
242 * \brief Unlock the data registered handlers structure.
244 #define data_unlock() ast_rwlock_unlock(&root_data.lock)
248 * \brief Check if a version is compatible with the current core.
249 * \param[in] structure_version The current structure version.
250 * \param[in] latest_compatible The latest compatible version.
251 * \param[in] current The current Data API version.
252 * \retval 1 If the module is compatible.
253 * \retval 0 If the module is NOT compatible.
255 static int data_structure_compatible(int structure_version, uint32_t latest_compatible,
258 if (structure_version >= latest_compatible && structure_version <= current) {
262 ast_log(LOG_ERROR, "A module is not compatible with the"
263 "current data api version\n");
270 * \brief Get the next node name in a path (/node1/node2)
271 * Avoid null nodes like //node1//node2/node3.
272 * \param[in] path The path where we are going to search for the next node name.
273 * \retval The next node name we found inside the given path.
274 * \retval NULL if there are no more node names.
276 static char *next_node_name(char **path)
281 res = strsep(path, "/");
282 } while (res && ast_strlen_zero(res));
289 * \brief Release the memory allocated by a call to ao2_alloc.
291 static void data_provider_destructor(void *obj)
293 struct data_provider *provider = obj;
295 ao2_ref(provider->children, -1);
300 * \brief Create a new data node.
301 * \param[in] name The name of the node we are going to create.
302 * \param[in] handler The handler registered for this node.
303 * \param[in] registrar The name of the registrar.
304 * \retval NULL on error.
305 * \retval The allocated data node structure.
307 static struct data_provider *data_provider_new(const char *name,
308 const struct ast_data_handler *handler, const char *registrar)
310 struct data_provider *node;
313 namelen = strlen(name) + 1;
315 node = ao2_alloc(sizeof(*node) + namelen, data_provider_destructor);
320 node->handler = handler;
321 node->registrar = registrar;
322 strcpy(node->name, name);
324 /* initialize the childrens container. */
325 if (!(node->children = ao2_container_alloc(NUM_DATA_NODE_BUCKETS,
326 data_provider_hash, data_provider_cmp))) {
336 * \brief Add a child node named 'name' to the 'parent' node.
337 * \param[in] parent Where to add the child node.
338 * \param[in] name The name of the child node.
339 * \param[in] handler The handler structure.
340 * \param[in] registrar Who registered this node.
341 * \retval NULL on error.
342 * \retval A newly allocated child in parent.
344 static struct data_provider *data_provider_add_child(struct ao2_container *parent,
345 const char *name, const struct ast_data_handler *handler, const char *registrar)
347 struct data_provider *child;
349 child = data_provider_new(name, handler, registrar);
354 ao2_link(parent, child);
361 * \brief Find a child node, based on his name.
362 * \param[in] parent Where to find the node.
363 * \param[in] name The node name to find.
364 * \param[in] registrar Also check if the node was being used by this registrar.
365 * \retval NULL if a node wasn't found.
366 * \retval The node found.
367 * \note Remember to decrement the ref count of the returned node after using it.
369 static struct data_provider *data_provider_find(struct ao2_container *parent,
370 const char *name, const char *registrar)
372 struct data_provider *find_node, *found;
374 /* XXX avoid allocating a new data node for searching... */
375 find_node = data_provider_new(name, NULL, NULL);
380 found = ao2_find(parent, find_node, OBJ_POINTER);
382 /* free the created node used for searching. */
383 ao2_ref(find_node, -1);
385 if (found && found->registrar && registrar) {
386 if (strcmp(found->registrar, registrar)) {
387 /* if the name doesn't match, do not return this node. */
388 ast_debug(1, "Registrar doesn't match, node was registered"
389 " by '%s' and we are searching for '%s'\n",
390 found->registrar, registrar);
401 * \brief Release a group of nodes.
402 * \param[in] parent The parent node.
403 * \param[in] path The path of nodes to release.
404 * \param[in] registrar Who registered this node.
405 * \retval <0 on error.
406 * \retval 0 on success.
407 * \see data_provider_create
409 static int data_provider_release(struct ao2_container *parent, const char *path,
410 const char *registrar)
412 char *node_name, *rpath;
413 struct data_provider *child;
416 rpath = ast_strdupa(path);
418 node_name = next_node_name(&rpath);
423 child = data_provider_find(parent, node_name, registrar);
428 /* if this is not a terminal node. */
429 if (!child->handler && rpath) {
430 ret = data_provider_release(child->children, rpath, registrar);
433 /* if this node is empty, unlink it. */
434 if (!ret && !ao2_container_count(child->children)) {
435 ao2_unlink(parent, child);
445 * \brief Release every node registered by 'registrar'.
446 * \param[in] parent The parent node.
447 * \param[in] registrar
448 * \see __ast_data_unregister
450 static void data_provider_release_all(struct ao2_container *parent,
451 const char *registrar)
453 struct ao2_iterator i;
454 struct data_provider *node;
456 i = ao2_iterator_init(parent, 0);
457 while ((node = ao2_iterator_next(&i))) {
458 if (!node->handler) {
459 /* this is a non-terminal node, go inside it. */
460 data_provider_release_all(node->children, registrar);
461 if (!ao2_container_count(node->children)) {
462 /* if this node was left empty, unlink it. */
463 ao2_unlink(parent, node);
466 if (!strcmp(node->registrar, registrar)) {
467 /* if the registrars match, release it! */
468 ao2_unlink(parent, node);
473 ao2_iterator_destroy(&i);
479 * \brief Create the middle nodes for the specified path (asterisk/testnode1/childnode)
480 * \param[in] parent Where to add the middle nodes structure.
481 * \param[in] path The path of nodes to add.
482 * \param[in] registrar Who is trying to create this node provider.
483 * \retval NULL on error.
484 * \retval The created node.
485 * \see data_provider_release
487 static struct data_provider *data_provider_create(struct ao2_container *parent,
488 const char *path, const char *registrar)
490 char *rpath, *node_name;
491 struct data_provider *child, *ret = NULL;
493 rpath = ast_strdupa(path);
495 node_name = next_node_name(&rpath);
497 /* no more nodes to create. */
501 child = data_provider_find(parent, node_name, NULL);
504 /* nodes without handler are non-terminal nodes. */
505 child = data_provider_add_child(parent, node_name, NULL, registrar);
509 ret = data_provider_create(child->children, rpath, registrar);
515 return ret ? ret : child;
518 int __ast_data_register(const char *path, const struct ast_data_handler *handler,
519 const char *registrar, struct ast_module *mod)
521 struct data_provider *node;
527 /* check if the handler structure is compatible. */
528 if (!data_structure_compatible(handler->version,
529 latest_handler_compatible_version,
530 current_handler_version)) {
534 /* create the node structure for the registered handler. */
537 node = data_provider_create(root_data.container, path, registrar);
539 ast_log(LOG_ERROR, "Unable to create the specified path (%s) "
540 "for '%s'.\n", path, registrar);
545 if (ao2_container_count(node->children) || node->handler) {
546 ast_log(LOG_ERROR, "The node '%s' was already registered. "
547 "We were unable to register '%s' for registrar '%s'.\n",
548 node->name, path, registrar);
554 /* add handler to that node. */
555 node->handler = handler;
565 int __ast_data_register_multiple(const struct ast_data_entry *data_entries,
566 size_t entries, const char *registrar, struct ast_module *mod)
570 for (i = 0; i < entries; i++) {
571 res = __ast_data_register(data_entries[i].path, data_entries[i].handler,
574 /* unregister all the already registered nodes, and make
575 * this an atomic action. */
577 __ast_data_unregister(data_entries[i].path, registrar);
586 int __ast_data_unregister(const char *path, const char *registrar)
592 ret = data_provider_release(root_data.container, path, registrar);
594 data_provider_release_all(root_data.container, registrar);
599 ast_log(LOG_ERROR, "Unable to unregister '%s' for '%s'\n",
608 * \brief Is a char used to specify a comparison?
609 * \param[in] a Character to evaluate.
610 * \retval 1 It is a char used to specify a comparison.
611 * \retval 0 It is NOT a char used to specify a comparison.
613 static int data_search_comparison_char(char a)
628 * \brief Get the type of comparison.
630 static enum data_search_comparison data_search_comparison_type(const char *comparison)
632 if (!strcmp(comparison, "=")) {
634 } else if (!strcmp(comparison, "!=")) {
636 } else if (!strcmp(comparison, "<")) {
638 } else if (!strcmp(comparison, ">")) {
640 } else if (!strcmp(comparison, "<=")) {
642 } else if (!strcmp(comparison, ">=")) {
646 return DATA_CMP_UNKNOWN;
651 * \brief Common string hash function for data nodes
653 static int data_search_hash(const void *obj, const int flags)
655 const struct ast_data_search *node = obj;
656 return ast_str_hash(node->name);
661 * \brief Common string comparison function
663 static int data_search_cmp(void *obj, void *arg, int flags)
665 struct ast_data_search *node1 = obj, *node2 = arg;
666 return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
671 * \brief Destroy the ao2 search node.
673 static void data_search_destructor(void *obj)
675 struct ast_data_search *node = obj;
678 ast_free(node->value);
681 ao2_ref(node->children, -1);
686 * \brief Allocate a search node.
687 * \retval NULL on error.
688 * \retval non-NULL The allocated search node structure.
690 static struct ast_data_search *data_search_alloc(const char *name)
692 struct ast_data_search *res;
693 size_t name_len = strlen(name) + 1;
695 res = ao2_alloc(sizeof(*res) + name_len, data_search_destructor);
700 res->children = ao2_container_alloc(NUM_DATA_SEARCH_BUCKETS, data_search_hash,
703 if (!res->children) {
708 strcpy(res->name, name);
715 * \brief Find a child node, based on his name.
716 * \param[in] parent Where to find the node.
717 * \param[in] name The node name to find.
718 * \retval NULL if a node wasn't found.
719 * \retval The node found.
720 * \note Remember to decrement the ref count of the returned node after using it.
722 static struct ast_data_search *data_search_find(struct ao2_container *parent,
725 struct ast_data_search *find_node, *found;
727 find_node = data_search_alloc(name);
732 found = ao2_find(parent, find_node, OBJ_POINTER);
734 /* free the created node used for searching. */
735 ao2_ref(find_node, -1);
742 * \brief Add a child node named 'name' to the 'parent' node.
743 * \param[in] parent Where to add the child node.
744 * \param[in] name The name of the child node.
745 * \retval NULL on error.
746 * \retval A newly allocated child in parent.
748 static struct ast_data_search *data_search_add_child(struct ao2_container *parent,
751 struct ast_data_search *child;
753 child = data_search_alloc(name);
758 ao2_link(parent, child);
765 * \brief Create the middle nodes for the specified path (asterisk/testnode1/childnode)
766 * \param[in] parent Where to add the middle nodes structure.
767 * \param[in] path The path of nodes to add.
768 * \retval NULL on error.
769 * \retval The created node.
771 static struct ast_data_search *data_search_create(struct ao2_container *parent,
774 char *rpath, *node_name;
775 struct ast_data_search *child = NULL;
776 struct ao2_container *current = parent;
778 rpath = ast_strdupa(path);
780 node_name = next_node_name(&rpath);
782 child = data_search_find(current, node_name);
784 child = data_search_add_child(current, node_name);
787 current = child->children;
788 node_name = next_node_name(&rpath);
796 * \brief Allocate a tree with the search string parsed.
797 * \param[in] search_string The search string.
798 * \retval NULL on error.
799 * \retval non-NULL A dynamically allocated search tree.
801 static struct ast_data_search *data_search_generate(const char *search_string)
803 struct ast_str *name, *value, *comparison;
804 char *elements, *search_string_dup, *saveptr;
806 struct ast_data_search *root, *child;
807 enum data_search_comparison cmp_type;
808 size_t search_string_len;
810 if (!search_string) {
811 ast_log(LOG_ERROR, "You must pass a valid search string.\n");
815 search_string_len = strlen(search_string);
817 name = ast_str_create(search_string_len);
821 value = ast_str_create(search_string_len);
826 comparison = ast_str_create(search_string_len);
833 search_string_dup = ast_strdupa(search_string);
835 /* Create the root node (just used as a container) */
836 root = data_search_alloc("/");
840 ast_free(comparison);
844 for (elements = strtok_r(search_string_dup, ",", &saveptr); elements;
845 elements = strtok_r(NULL, ",", &saveptr)) {
848 for (i = 0; !data_search_comparison_char(elements[i]) &&
850 ast_str_append(&name, 0, "%c", elements[i]);
853 /* check if the syntax is ok. */
854 if (!data_search_comparison_char(elements[i])) {
855 /* if this is the end of the string, then this is
857 ast_log(LOG_ERROR, "Invalid search string!\n");
861 /* parse the comparison string. */
862 ast_str_reset(comparison);
863 for (; data_search_comparison_char(elements[i]) && elements[i]; i++) {
864 ast_str_append(&comparison, 0, "%c", elements[i]);
867 /* parse the value string. */
868 ast_str_reset(value);
869 for (; elements[i]; i++) {
870 ast_str_append(&value, 0, "%c", elements[i]);
873 cmp_type = data_search_comparison_type(ast_str_buffer(comparison));
874 if (cmp_type == DATA_CMP_UNKNOWN) {
875 ast_log(LOG_ERROR, "Invalid comparison '%s'\n",
876 ast_str_buffer(comparison));
880 /* add this node to the tree. */
881 child = data_search_create(root->children, ast_str_buffer(name));
883 child->cmp_type = cmp_type;
884 child->value = ast_strdup(ast_str_buffer(value));
890 ast_free(comparison);
897 * \brief Release the allocated memory for the search tree.
898 * \param[in] search The search tree root node.
900 static void data_search_release(struct ast_data_search *search)
907 * \brief Based on the kind of comparison and the result in cmpval, return
909 * \param[in] cmpval A result returned by a strcmp() for example.
910 * \param[in] comparison_type The kind of comparison (<,>,=,!=,...)
911 * \retval 1 If the comparison doesn't match.
912 * \retval 0 If the comparison matches.
914 static inline int data_search_comparison_result(int cmpval,
915 enum data_search_comparison comparison_type)
917 switch (comparison_type) {
948 case DATA_CMP_UNKNOWN:
956 * \brief Get an internal node, from the search tree.
957 * \param[in] node A node container.
958 * \param[in] path The path to the needed internal node.
959 * \retval NULL if the internal node is not found.
960 * \retval non-NULL the internal node with path 'path'.
962 static struct ast_data_search *data_search_get_node(const struct ast_data_search *node,
965 char *savepath, *node_name;
966 struct ast_data_search *child, *current = (struct ast_data_search *) node;
972 savepath = ast_strdupa(path);
973 node_name = next_node_name(&savepath);
976 child = data_search_find(current->children, node_name);
977 if (current != node) {
978 ao2_ref(current, -1);
984 node_name = next_node_name(&savepath);
992 * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
993 * current string value.
994 * .search = "somename=somestring"
996 * value is the current value of something and will be evaluated against "somestring".
997 * \param[in] root The root node pointer of the search tree.
998 * \param[in] name The name of the specific.
999 * \param[in] value The value to compare.
1000 * \returns The strcmp return value.
1002 static int data_search_cmp_string(const struct ast_data_search *root, const char *name,
1005 struct ast_data_search *child;
1006 enum data_search_comparison cmp_type;
1009 child = data_search_get_node(root, name);
1014 ret = strcmp(value, child->value);
1015 cmp_type = child->cmp_type;
1019 return data_search_comparison_result(ret, cmp_type);
1024 * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
1025 * current pointer address value.
1026 * .search = "something=0x32323232"
1027 * name = "something"
1028 * value is the current value of something and will be evaluated against "0x32323232".
1029 * \param[in] root The root node pointer of the search tree.
1030 * \param[in] name The name of the specific.
1031 * \param[in] ptr The pointer address to compare.
1032 * \returns The (value - current_value) result.
1034 static int data_search_cmp_ptr(const struct ast_data_search *root, const char *name,
1037 struct ast_data_search *child;
1038 enum data_search_comparison cmp_type;
1041 child = data_search_get_node(root, name);
1046 cmp_type = child->cmp_type;
1048 if (sscanf(child->value, "%p", &node_ptr) <= 0) {
1055 return data_search_comparison_result((node_ptr - ptr), cmp_type);
1060 * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
1061 * current ipv4 address value.
1062 * .search = "something=192.168.2.2"
1063 * name = "something"
1064 * value is the current value of something and will be evaluated against "192.168.2.2".
1065 * \param[in] root The root node pointer of the search tree.
1066 * \param[in] name The name of the specific.
1067 * \param[in] addr The ipv4 address value to compare.
1068 * \returns The (value - current_value) result.
1070 static int data_search_cmp_ipaddr(const struct ast_data_search *root, const char *name,
1071 struct in_addr addr)
1073 struct ast_data_search *child;
1074 enum data_search_comparison cmp_type;
1075 struct in_addr node_addr;
1077 child = data_search_get_node(root, name);
1081 cmp_type = child->cmp_type;
1083 inet_aton(child->value, &node_addr);
1087 return data_search_comparison_result((node_addr.s_addr - addr.s_addr), cmp_type);
1092 * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
1093 * current boolean value.
1094 * .search = "something=true"
1095 * name = "something"
1096 * value is the current value of something and will be evaluated against "true".
1097 * \param[in] root The root node pointer of the search tree.
1098 * \param[in] name The name of the specific.
1099 * \param[in] value The boolean value to compare.
1100 * \returns The (value - current_value) result.
1102 static int data_search_cmp_bool(const struct ast_data_search *root, const char *name,
1105 struct ast_data_search *child;
1106 unsigned int node_value;
1107 enum data_search_comparison cmp_type;
1109 child = data_search_get_node(root, name);
1114 node_value = abs(ast_true(child->value));
1115 cmp_type = child->cmp_type;
1119 return data_search_comparison_result(value - node_value, cmp_type);
1124 * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
1125 * current double value.
1126 * .search = "something=222"
1127 * name = "something"
1128 * value is the current value of something and will be evaluated against "222".
1129 * \param[in] root The root node pointer of the search tree.
1130 * \param[in] name The name of the specific.
1131 * \param[in] value The double value to compare.
1132 * \returns The (value - current_value) result.
1134 static int data_search_cmp_dbl(const struct ast_data_search *root, const char *name,
1137 struct ast_data_search *child;
1139 enum data_search_comparison cmp_type;
1141 child = data_search_get_node(root, name);
1146 node_value = strtod(child->value, NULL);
1147 cmp_type = child->cmp_type;
1151 return data_search_comparison_result(value - node_value, cmp_type);
1156 * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
1157 * current unsigned integer value.
1158 * .search = "something=10"
1159 * name = "something"
1160 * value is the current value of something and will be evaluated against "10".
1161 * \param[in] root The root node pointer of the search tree.
1162 * \param[in] name The name of the specific.
1163 * \param[in] value The unsigned value to compare.
1164 * \returns The strcmp return value.
1166 static int data_search_cmp_uint(const struct ast_data_search *root, const char *name,
1169 struct ast_data_search *child;
1170 unsigned int node_value;
1171 enum data_search_comparison cmp_type;
1173 child = data_search_get_node(root, name);
1178 node_value = atoi(child->value);
1179 cmp_type = child->cmp_type;
1183 return data_search_comparison_result(value - node_value, cmp_type);
1188 * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
1189 * current signed integer value.
1190 * .search = "something=10"
1191 * name = "something"
1192 * value is the current value of something and will be evaluated against "10".
1193 * \param[in] root The root node pointer of the search tree.
1194 * \param[in] name The name of the specific.
1195 * \param[in] value The value to compare.
1196 * \returns The strcmp return value.
1198 static int data_search_cmp_int(const struct ast_data_search *root, const char *name,
1201 struct ast_data_search *child;
1203 enum data_search_comparison cmp_type;
1205 child = data_search_get_node(root, name);
1210 node_value = atoi(child->value);
1211 cmp_type = child->cmp_type;
1215 return data_search_comparison_result(value - node_value, cmp_type);
1220 * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
1221 * current character value.
1222 * .search = "something=c"
1223 * name = "something"
1224 * value is the current value of something and will be evaluated against "c".
1225 * \param[in] root The root node pointer of the search tree.
1226 * \param[in] name The name of the specific.
1227 * \param[in] value The boolean value to compare.
1228 * \returns The (value - current_value) result.
1230 static int data_search_cmp_char(const struct ast_data_search *root, const char *name,
1233 struct ast_data_search *child;
1235 enum data_search_comparison cmp_type;
1237 child = data_search_get_node(root, name);
1242 node_value = *(child->value);
1243 cmp_type = child->cmp_type;
1247 return data_search_comparison_result(value - node_value, cmp_type);
1252 * \brief Get the member pointer, from a mapping structure, based on its name.
1253 * \XXX We will need to improve performance here!!.
1254 * \retval <0 if the member was not found.
1255 * \retval >=0 The member position in the mapping structure.
1257 static inline int data_search_mapping_find(const struct ast_data_mapping_structure *map,
1259 const char *member_name)
1263 for (i = 0; i < mapping_len; i++) {
1264 if (!strcmp(map[i].name, member_name)) {
1272 int __ast_data_search_cmp_structure(const struct ast_data_search *search,
1273 const struct ast_data_mapping_structure *mapping, size_t mapping_len,
1274 void *structure, const char *structure_name)
1276 struct ao2_iterator i;
1277 struct ast_data_search *node, *struct_children;
1278 int member, notmatch = 0;
1284 struct_children = data_search_get_node(search, structure_name);
1285 if (!struct_children) {
1289 i = ao2_iterator_init(struct_children->children, 0);
1290 while ((node = ao2_iterator_next(&i))) {
1291 member = data_search_mapping_find(mapping, mapping_len, node->name);
1293 /* the structure member name doesn't match! */
1295 ao2_ref(struct_children, -1);
1296 ao2_iterator_destroy(&i);
1301 switch (mapping[member].type) {
1302 case AST_DATA_PASSWORD:
1303 notmatch = data_search_cmp_string(struct_children,
1305 mapping[member].get.AST_DATA_PASSWORD(structure));
1307 case AST_DATA_TIMESTAMP:
1308 notmatch = data_search_cmp_uint(struct_children,
1310 mapping[member].get.AST_DATA_TIMESTAMP(structure));
1312 case AST_DATA_SECONDS:
1313 notmatch = data_search_cmp_uint(struct_children,
1315 mapping[member].get.AST_DATA_SECONDS(structure));
1317 case AST_DATA_MILLISECONDS:
1318 notmatch = data_search_cmp_uint(struct_children,
1320 mapping[member].get.AST_DATA_MILLISECONDS(structure));
1322 case AST_DATA_STRING:
1323 notmatch = data_search_cmp_string(struct_children,
1325 mapping[member].get.AST_DATA_STRING(structure));
1327 case AST_DATA_CHARACTER:
1328 notmatch = data_search_cmp_char(struct_children,
1330 mapping[member].get.AST_DATA_CHARACTER(structure));
1332 case AST_DATA_INTEGER:
1333 notmatch = data_search_cmp_int(struct_children,
1335 mapping[member].get.AST_DATA_INTEGER(structure));
1337 case AST_DATA_BOOLEAN:
1338 notmatch = data_search_cmp_bool(struct_children,
1340 mapping[member].get.AST_DATA_BOOLEAN(structure));
1342 case AST_DATA_UNSIGNED_INTEGER:
1343 notmatch = data_search_cmp_uint(struct_children,
1345 mapping[member].get.AST_DATA_UNSIGNED_INTEGER(structure));
1347 case AST_DATA_DOUBLE:
1348 notmatch = data_search_cmp_dbl(struct_children,
1350 mapping[member].get.AST_DATA_DOUBLE(structure));
1352 case AST_DATA_IPADDR:
1353 notmatch = data_search_cmp_ipaddr(struct_children,
1355 mapping[member].get.AST_DATA_IPADDR(structure));
1357 case AST_DATA_POINTER:
1358 notmatch = data_search_cmp_ptr(struct_children,
1360 mapping[member].get.AST_DATA_POINTER(structure));
1362 case AST_DATA_CONTAINER:
1368 ao2_iterator_destroy(&i);
1370 ao2_ref(struct_children, -1);
1377 * \brief Release the memory allocated by a call to ao2_alloc.
1379 static void data_result_destructor(void *obj)
1381 struct ast_data *root = obj;
1383 switch (root->type) {
1384 case AST_DATA_PASSWORD:
1385 case AST_DATA_STRING:
1386 ast_free(root->payload.str);
1387 ao2_ref(root->children, -1);
1389 case AST_DATA_POINTER:
1390 case AST_DATA_CHARACTER:
1391 case AST_DATA_CONTAINER:
1392 case AST_DATA_INTEGER:
1393 case AST_DATA_TIMESTAMP:
1394 case AST_DATA_SECONDS:
1395 case AST_DATA_MILLISECONDS:
1396 case AST_DATA_UNSIGNED_INTEGER:
1397 case AST_DATA_DOUBLE:
1398 case AST_DATA_BOOLEAN:
1399 case AST_DATA_IPADDR:
1400 ao2_ref(root->children, -1);
1405 static struct ast_data *data_result_create(const char *name)
1407 struct ast_data *res;
1410 namelen = ast_strlen_zero(name) ? 1 : strlen(name) + 1;
1412 res = ao2_alloc(sizeof(*res) + namelen, data_result_destructor);
1417 strcpy(res->name, namelen ? name : "");
1419 /* initialize the children container */
1420 res->children = ao2_container_alloc(NUM_DATA_RESULT_BUCKETS, data_result_hash,
1422 if (!res->children) {
1427 /* set this node as a container. */
1428 res->type = AST_DATA_CONTAINER;
1435 * \brief Find a child node, based on its name.
1436 * \param[in] root The starting point.
1437 * \param[in] name The child name.
1438 * \retval NULL if the node wasn't found.
1439 * \retval non-NULL the node we were looking for.
1441 static struct ast_data *data_result_find_child(struct ast_data *root, const char *name)
1443 struct ast_data *found, *find_node;
1445 find_node = data_result_create(name);
1450 found = ao2_find(root->children, find_node, OBJ_POINTER);
1452 /* release the temporary created node used for searching. */
1453 ao2_ref(find_node, -1);
1458 int ast_data_search_match(const struct ast_data_search *search, struct ast_data *data)
1460 struct ao2_iterator i, ii;
1461 struct ast_data_search *s, *s_child;
1462 struct ast_data *d_child;
1469 s_child = data_search_find(search->children, data->name);
1471 /* nothing to compare */
1472 ao2_ref(s_child, -1);
1476 i = ao2_iterator_init(s_child->children, 0);
1477 while ((s = ao2_iterator_next(&i))) {
1478 if (!ao2_container_count(s->children)) {
1479 /* compare this search node with every data node */
1480 d_child = data_result_find_child(data, s->name);
1487 switch (d_child->type) {
1488 case AST_DATA_PASSWORD:
1489 case AST_DATA_STRING:
1490 notmatch = data_search_cmp_string(s_child, d_child->name,
1491 d_child->payload.str);
1493 case AST_DATA_CHARACTER:
1494 notmatch = data_search_cmp_char(s_child, d_child->name,
1495 d_child->payload.character);
1497 case AST_DATA_INTEGER:
1498 notmatch = data_search_cmp_int(s_child, d_child->name,
1499 d_child->payload.sint);
1501 case AST_DATA_BOOLEAN:
1502 notmatch = data_search_cmp_bool(s_child, d_child->name,
1503 d_child->payload.boolean);
1505 case AST_DATA_UNSIGNED_INTEGER:
1506 notmatch = data_search_cmp_uint(s_child, d_child->name,
1507 d_child->payload.uint);
1509 case AST_DATA_TIMESTAMP:
1510 case AST_DATA_SECONDS:
1511 case AST_DATA_MILLISECONDS:
1512 case AST_DATA_DOUBLE:
1513 notmatch = data_search_cmp_uint(s_child, d_child->name,
1514 d_child->payload.dbl);
1516 case AST_DATA_IPADDR:
1517 notmatch = data_search_cmp_ipaddr(s_child, d_child->name,
1518 d_child->payload.ipaddr);
1520 case AST_DATA_POINTER:
1521 notmatch = data_search_cmp_ptr(s_child, d_child->name,
1522 d_child->payload.ptr);
1524 case AST_DATA_CONTAINER:
1527 ao2_ref(d_child, -1);
1529 ii = ao2_iterator_init(data->children, 0);
1530 while ((d_child = ao2_iterator_next(&ii))) {
1531 if (strcmp(d_child->name, s->name)) {
1532 ao2_ref(d_child, -1);
1535 if (!(notmatch = !ast_data_search_match(s_child, d_child))) {
1536 /* do not continue if we have a match. */
1537 ao2_ref(d_child, -1);
1540 ao2_ref(d_child, -1);
1542 ao2_iterator_destroy(&ii);
1546 /* do not continue if we don't have a match. */
1550 ao2_iterator_destroy(&i);
1552 ao2_ref(s_child, -1);
1559 * \brief Get an internal node, from the result set.
1560 * \param[in] node A node container.
1561 * \param[in] path The path to the needed internal node.
1562 * \retval NULL if the internal node is not found.
1563 * \retval non-NULL the internal node with path 'path'.
1565 static struct ast_data *data_result_get_node(struct ast_data *node,
1568 char *savepath, *node_name;
1569 struct ast_data *child, *current = node;
1571 savepath = ast_strdupa(path);
1572 node_name = next_node_name(&savepath);
1575 child = data_result_find_child(current, node_name);
1576 if (current != node) {
1577 ao2_ref(current, -1);
1583 node_name = next_node_name(&savepath);
1586 /* do not increment the refcount of the returned object. */
1587 if (current != node) {
1588 ao2_ref(current, -1);
1596 * \brief Add a child to the specified root node.
1597 * \param[in] root The root node pointer.
1598 * \param[in] child The child to add to the root node.
1600 static void data_result_add_child(struct ast_data *root, struct ast_data *child)
1602 ao2_link(root->children, child);
1607 * \brief Common string hash function for data nodes
1609 static int data_filter_hash(const void *obj, const int flags)
1611 const struct data_filter *node = obj;
1612 return ast_str_hash(node->name);
1617 * \brief Common string comparison function
1619 static int data_filter_cmp(void *obj, void *arg, int flags)
1621 struct data_filter *node1 = obj, *node2 = arg;
1622 return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
1627 * \brief Destroy a data filter tree.
1628 * \param[in] obj Data filter list to be destroyed.
1630 static void data_filter_destructor(void *obj)
1632 struct data_filter *filter = obj, *globres;
1634 AST_LIST_TRAVERSE(&(filter->glob_list), globres, list) {
1635 ao2_ref(globres, -1);
1638 ao2_ref(filter->children, -1);
1643 * \brief Allocate a filter node.
1644 * \retval NULL on error.
1645 * \retval non-NULL The allocated search node structure.
1647 static struct data_filter *data_filter_alloc(const char *name)
1649 char *globname, *token;
1650 struct data_filter *res, *globfilter;
1651 size_t name_len = strlen(name) + 1;
1653 res = ao2_alloc(sizeof(*res) + name_len, data_filter_destructor);
1658 res->children = ao2_container_alloc(NUM_DATA_FILTER_BUCKETS, data_filter_hash,
1661 if (!res->children) {
1666 strcpy(res->name, name);
1668 if (strchr(res->name, '*')) {
1669 globname = ast_strdupa(res->name);
1671 while ((token = strsep(&globname, "*"))) {
1672 globfilter = data_filter_alloc(token);
1673 AST_LIST_INSERT_TAIL(&(res->glob_list), globfilter, list);
1682 * \brief Release a filter tree.
1683 * \param[in] filter The filter tree root node.
1685 static void data_filter_release(struct data_filter *filter)
1687 ao2_ref(filter, -1);
1692 * \brief Find a child node, based on his name.
1693 * \param[in] parent Where to find the node.
1694 * \param[in] name The node name to find.
1695 * \retval NULL if a node wasn't found.
1696 * \retval The node found.
1697 * \note Remember to decrement the ref count of the returned node after using it.
1699 static struct data_filter *data_filter_find(struct ao2_container *parent,
1702 int i, olend, orend, globfound;
1703 size_t name_len = strlen(name), glob_len;
1704 struct ao2_iterator iter;
1705 struct data_filter *find_node, *found, *globres;
1707 find_node = data_filter_alloc(name);
1712 found = ao2_find(parent, find_node, OBJ_POINTER);
1714 /* free the created node used for searching. */
1715 ao2_ref(find_node, -1);
1721 iter = ao2_iterator_init(parent, 0);
1722 while ((found = ao2_iterator_next(&iter))) {
1723 if (!AST_LIST_EMPTY(&(found->glob_list))) {
1727 olend = ast_strlen_zero(AST_LIST_FIRST(&(found->glob_list))->name);
1728 orend = ast_strlen_zero(AST_LIST_LAST(&(found->glob_list))->name);
1730 AST_LIST_TRAVERSE(&(found->glob_list), globres, list) {
1731 if (!*globres->name) {
1735 glob_len = strlen(globres->name);
1738 if (strncasecmp(name, globres->name, glob_len)) {
1747 for (globfound = 0; name_len - i >= glob_len; ++i) {
1748 if (!strncasecmp(name + i, globres->name, glob_len)) {
1760 if (globfound && (i == name_len || orend)) {
1761 ao2_iterator_destroy(&iter);
1768 ao2_iterator_destroy(&iter);
1775 * \brief Add a child to the specified node.
1776 * \param[in] root The root node where to add the child.
1777 * \param[in] name The name of the node to add.
1778 * \note Remember to decrement the ref count after using the returned node.
1780 static struct data_filter *data_filter_add_child(struct ao2_container *root,
1783 struct data_filter *node;
1785 node = data_filter_find(root, name);
1790 node = data_filter_alloc(name);
1795 ao2_link(root, node);
1802 * \brief Add a node to a filter list from a path
1803 * \param[in] Filter list to add the path onto.
1804 * \param[in] The path to add into the filter list.
1805 * \retval NULL on error.
1806 * \retval non-NULL A tree with the wanted nodes.
1808 static int data_filter_add_nodes(struct ao2_container *root, char *path)
1810 struct data_filter *node;
1811 char *savepath, *saveptr, *token, *node_name;
1818 savepath = ast_strdupa(path);
1820 node_name = next_node_name(&savepath);
1826 for (token = strtok_r(node_name, "|", &saveptr);
1827 token; token = strtok_r(NULL, "|", &saveptr)) {
1828 node = data_filter_add_child(root, token);
1832 data_filter_add_nodes(node->children, savepath);
1842 * \brief Generate a filter list based on a filter string provided by the API user.
1843 * \param[in] A filter string to create a filter from.
1845 static struct data_filter *data_filter_generate(const char *constfilter)
1847 struct data_filter *filter = NULL;
1848 char *strfilter, *token, *saveptr;
1855 strfilter = ast_strdupa(constfilter);
1857 filter = data_filter_alloc("/");
1862 for (token = strtok_r(strfilter, ",", &saveptr); token;
1863 token = strtok_r(NULL, ",", &saveptr)) {
1864 node_added = data_filter_add_nodes(filter->children, token);
1868 ao2_ref(filter, -1);
1877 * \brief Generate all the tree from a specified provider.
1878 * \param[in] query The query executed.
1879 * \param[in] root_provider The provider specified in the path of the query.
1880 * \param[in] parent_node_name The root node name.
1881 * \retval NULL on error.
1882 * \retval non-NULL The generated result tree.
1884 static struct ast_data *data_result_generate_node(const struct ast_data_query *query,
1885 const struct data_provider *root_provider,
1886 const char *parent_node_name,
1887 const struct ast_data_search *search,
1888 const struct data_filter *filter)
1890 struct ast_data *generated, *node;
1891 struct ao2_iterator i;
1892 struct data_provider *provider;
1893 struct ast_data_search *search_child = NULL;
1894 struct data_filter *filter_child;
1896 node = data_result_create(parent_node_name);
1898 ast_log(LOG_ERROR, "Unable to allocate '%s' node\n", parent_node_name);
1902 if (root_provider->module) {
1903 ast_module_ref(root_provider->module);
1906 /* if this is a terminal node, just run the callback function. */
1907 if (root_provider->handler && root_provider->handler->get) {
1908 node->filter = filter;
1909 root_provider->handler->get(search, node);
1910 if (root_provider->module) {
1911 ast_module_unref(root_provider->module);
1916 if (root_provider->module) {
1917 ast_module_unref(root_provider->module);
1920 /* if this is not a terminal node, generate every child node. */
1921 i = ao2_iterator_init(root_provider->children, 0);
1922 while ((provider = ao2_iterator_next(&i))) {
1923 filter_child = NULL;
1926 /* get the internal search node. */
1928 search_child = data_search_find(search->children, provider->name);
1930 /* get the internal filter node. */
1932 filter_child = data_filter_find(filter->children, provider->name);
1935 if (!filter || filter_child) {
1936 /* only generate the internal node, if we have something to
1937 * generate based on the filtering string. */
1938 generated = data_result_generate_node(query, provider,
1940 search_child, filter_child);
1943 /* decrement the refcount of the internal search node. */
1945 ao2_ref(search_child, -1);
1948 /* decrement the refcount of the internal filter node. */
1950 ao2_ref(filter_child, -1);
1954 data_result_add_child(node, generated);
1955 ao2_ref(generated, -1);
1958 ao2_ref(provider, -1);
1960 ao2_iterator_destroy(&i);
1967 * \brief Generate a result tree based on a query.
1968 * \param[in] query The complete query structure.
1969 * \param[in] search_path The path to retrieve.
1970 * \retval NULL on error.
1971 * \retval non-NULL The generated data result.
1973 static struct ast_data *data_result_generate(const struct ast_data_query *query,
1974 const char *search_path)
1976 char *node_name, *tmp_path;
1977 struct data_provider *provider_child, *tmp_provider_child;
1978 struct ast_data *result, *result_filtered;
1979 struct ast_data_search *search = NULL, *search_child = NULL;
1980 struct data_filter *filter = NULL, *filter_child = NULL;
1983 /* generate all the trees?. */
1987 tmp_path = ast_strdupa(search_path);
1989 /* start searching the root node name */
1990 node_name = next_node_name(&tmp_path);
1994 provider_child = data_provider_find(root_data.container, node_name, NULL);
1996 /* continue with the rest of the path. */
1997 while (provider_child) {
1998 node_name = next_node_name(&tmp_path);
2003 tmp_provider_child = data_provider_find(provider_child->children,
2006 /* release the reference from this child */
2007 ao2_ref(provider_child, -1);
2009 provider_child = tmp_provider_child;
2012 if (!provider_child) {
2013 ast_log(LOG_ERROR, "Invalid path '%s', '%s' not found.\n",
2014 tmp_path, node_name);
2018 /* generate the search tree. */
2019 if (query->search) {
2020 search = data_search_generate(query->search);
2022 search_child = data_search_find(search->children,
2023 provider_child->name);
2027 /* generate the filter tree. */
2028 if (query->filter) {
2029 filter = data_filter_generate(query->filter);
2031 filter_child = data_filter_find(filter->children,
2032 provider_child->name);
2036 result = data_result_generate_node(query, provider_child, provider_child->name,
2037 search_child, filter_child);
2039 /* release the requested provider. */
2040 ao2_ref(provider_child, -1);
2042 /* release the generated search tree. */
2044 ao2_ref(search_child, -1);
2048 ao2_ref(filter_child, -1);
2052 data_search_release(search);
2055 result_filtered = result;
2057 /* release the generated filter tree. */
2059 data_filter_release(filter);
2062 return result_filtered;
2065 struct ast_data *ast_data_get(const struct ast_data_query *query)
2067 struct ast_data *res;
2069 /* check compatibility */
2070 if (!data_structure_compatible(query->version, latest_query_compatible_version,
2071 current_query_version)) {
2076 res = data_result_generate(query, query->path);
2080 ast_log(LOG_ERROR, "Unable to get data from %s\n", query->path);
2090 * \brief Helper function to move an ast_data tree to xml.
2091 * \param[in] parent_data The initial ast_data node to be passed to xml.
2092 * \param[out] parent_xml The root node to insert the xml.
2094 static void data_get_xml_add_child(struct ast_data *parent_data,
2095 struct ast_xml_node *parent_xml)
2097 struct ao2_iterator i;
2098 struct ast_data *node;
2099 struct ast_xml_node *child_xml;
2100 char node_content[256];
2102 i = ao2_iterator_init(parent_data->children, 0);
2103 while ((node = ao2_iterator_next(&i))) {
2104 child_xml = ast_xml_new_node(node->name);
2110 switch (node->type) {
2111 case AST_DATA_CONTAINER:
2112 data_get_xml_add_child(node, child_xml);
2114 case AST_DATA_PASSWORD:
2115 ast_xml_set_text(child_xml, node->payload.str);
2117 case AST_DATA_TIMESTAMP:
2118 snprintf(node_content, sizeof(node_content), "%d",
2119 node->payload.uint);
2120 ast_xml_set_text(child_xml, node_content);
2122 case AST_DATA_SECONDS:
2123 snprintf(node_content, sizeof(node_content), "%d",
2124 node->payload.uint);
2125 ast_xml_set_text(child_xml, node_content);
2127 case AST_DATA_MILLISECONDS:
2128 snprintf(node_content, sizeof(node_content), "%d",
2129 node->payload.uint);
2130 ast_xml_set_text(child_xml, node_content);
2132 case AST_DATA_STRING:
2133 ast_xml_set_text(child_xml, node->payload.str);
2135 case AST_DATA_CHARACTER:
2136 snprintf(node_content, sizeof(node_content), "%c",
2137 node->payload.character);
2138 ast_xml_set_text(child_xml, node_content);
2140 case AST_DATA_INTEGER:
2141 snprintf(node_content, sizeof(node_content), "%d",
2142 node->payload.sint);
2143 ast_xml_set_text(child_xml, node_content);
2145 case AST_DATA_UNSIGNED_INTEGER:
2146 snprintf(node_content, sizeof(node_content), "%u",
2147 node->payload.uint);
2148 ast_xml_set_text(child_xml, node_content);
2150 case AST_DATA_DOUBLE:
2151 snprintf(node_content, sizeof(node_content), "%f",
2153 ast_xml_set_text(child_xml, node_content);
2155 case AST_DATA_BOOLEAN:
2156 if (node->payload.boolean) {
2157 ast_xml_set_text(child_xml, "true");
2159 ast_xml_set_text(child_xml, "false");
2162 case AST_DATA_POINTER:
2163 snprintf(node_content, sizeof(node_content), "%p",
2165 ast_xml_set_text(child_xml, node_content);
2167 case AST_DATA_IPADDR:
2168 snprintf(node_content, sizeof(node_content), "%s",
2169 ast_inet_ntoa(node->payload.ipaddr));
2170 ast_xml_set_text(child_xml, node_content);
2173 ast_xml_add_child(parent_xml, child_xml);
2177 ao2_iterator_destroy(&i);
2181 struct ast_xml_doc *ast_data_get_xml(const struct ast_data_query *query)
2183 struct ast_xml_doc *doc;
2184 struct ast_xml_node *root;
2185 struct ast_data *res;
2187 res = ast_data_get(query);
2192 doc = ast_xml_new();
2198 root = ast_xml_new_node(res->name);
2203 ast_xml_set_root(doc, root);
2205 data_get_xml_add_child(res, root);
2213 enum ast_data_type ast_data_retrieve_type(struct ast_data *node, const char *path)
2215 struct ast_data *internal;
2217 internal = data_result_get_node(node, path);
2222 return internal->type;
2225 char *ast_data_retrieve_name(struct ast_data *node)
2232 * \brief Insert a child node inside a passed parent node.
2233 * \param root Where we are going to insert the child node.
2234 * \param name The name of the child node to add.
2235 * \param type The type of content inside the child node.
2236 * \param ptr The actual content of the child node.
2237 * \retval NULL on error.
2238 * \retval non-NULL The added child node pointer.
2240 static struct ast_data *__ast_data_add(struct ast_data *root, const char *name,
2241 enum ast_data_type type, void *ptr)
2243 struct ast_data *node;
2244 struct data_filter *filter, *filter_child = NULL;
2246 if (!root || !root->children) {
2247 /* invalid data result node. */
2251 /* check if we need to add this node, based on the filter. */
2253 filter = data_filter_find(root->filter->children, name);
2257 ao2_ref(filter, -1);
2260 node = data_result_create(name);
2268 case AST_DATA_BOOLEAN:
2269 node->payload.boolean = *(unsigned int *) ptr;
2271 case AST_DATA_INTEGER:
2272 node->payload.sint = *(int *) ptr;
2274 case AST_DATA_TIMESTAMP:
2275 case AST_DATA_SECONDS:
2276 case AST_DATA_MILLISECONDS:
2277 case AST_DATA_UNSIGNED_INTEGER:
2278 node->payload.uint = *(unsigned int *) ptr;
2280 case AST_DATA_DOUBLE:
2281 node->payload.dbl = *(double *) ptr;
2283 case AST_DATA_PASSWORD:
2284 case AST_DATA_STRING:
2285 node->payload.str = (char *) ptr;
2287 case AST_DATA_CHARACTER:
2288 node->payload.character = *(char *) ptr;
2290 case AST_DATA_POINTER:
2291 node->payload.ptr = ptr;
2293 case AST_DATA_IPADDR:
2294 node->payload.ipaddr = *(struct in_addr *) ptr;
2296 case AST_DATA_CONTAINER:
2298 filter_child = data_filter_find(root->filter->children, name);
2300 /* do not increment the refcount because it is not neccesary. */
2301 ao2_ref(filter_child, -1);
2304 node->filter = filter_child;
2310 data_result_add_child(root, node);
2317 struct ast_data *ast_data_add_node(struct ast_data *root, const char *name)
2319 return __ast_data_add(root, name, AST_DATA_CONTAINER, NULL);
2322 struct ast_data *ast_data_add_int(struct ast_data *root, const char *name, int value)
2324 return __ast_data_add(root, name, AST_DATA_INTEGER, &value);
2327 struct ast_data *ast_data_add_char(struct ast_data *root, const char *name, char value)
2329 return __ast_data_add(root, name, AST_DATA_CHARACTER, &value);
2332 struct ast_data *ast_data_add_uint(struct ast_data *root, const char *name,
2335 return __ast_data_add(root, name, AST_DATA_UNSIGNED_INTEGER, &value);
2338 struct ast_data *ast_data_add_dbl(struct ast_data *root, const char *childname,
2341 return __ast_data_add(root, childname, AST_DATA_DOUBLE, &dbl);
2344 struct ast_data *ast_data_add_bool(struct ast_data *root, const char *childname,
2345 unsigned int boolean)
2347 return __ast_data_add(root, childname, AST_DATA_BOOLEAN, &boolean);
2350 struct ast_data *ast_data_add_ipaddr(struct ast_data *root, const char *childname,
2351 struct in_addr addr)
2353 return __ast_data_add(root, childname, AST_DATA_IPADDR, &addr);
2356 struct ast_data *ast_data_add_ptr(struct ast_data *root, const char *childname,
2359 return __ast_data_add(root, childname, AST_DATA_POINTER, ptr);
2362 struct ast_data *ast_data_add_timestamp(struct ast_data *root, const char *childname,
2363 unsigned int timestamp)
2365 return __ast_data_add(root, childname, AST_DATA_TIMESTAMP, ×tamp);
2368 struct ast_data *ast_data_add_seconds(struct ast_data *root, const char *childname,
2369 unsigned int seconds)
2371 return __ast_data_add(root, childname, AST_DATA_SECONDS, &seconds);
2374 struct ast_data *ast_data_add_milliseconds(struct ast_data *root, const char *childname,
2375 unsigned int milliseconds)
2377 return __ast_data_add(root, childname, AST_DATA_MILLISECONDS, &milliseconds);
2380 struct ast_data *ast_data_add_password(struct ast_data *root, const char *childname,
2384 size_t namelen = 1 + (ast_strlen_zero(value) ? 0 : strlen(value));
2385 struct ast_data *res;
2387 if (!(name = ast_malloc(namelen))) {
2391 strcpy(name, (ast_strlen_zero(value) ? "" : value));
2393 res = __ast_data_add(root, childname, AST_DATA_PASSWORD, name);
2401 struct ast_data *ast_data_add_str(struct ast_data *root, const char *childname,
2405 size_t namelen = 1 + (ast_strlen_zero(value) ? 0 : strlen(value));
2406 struct ast_data *res;
2408 if (!(name = ast_malloc(namelen))) {
2412 strcpy(name, (ast_strlen_zero(value) ? "" : value));
2414 res = __ast_data_add(root, childname, AST_DATA_STRING, name);
2422 int __ast_data_add_structure(struct ast_data *root,
2423 const struct ast_data_mapping_structure *mapping, size_t mapping_len,
2428 for (i = 0; i < mapping_len; i++) {
2429 switch (mapping[i].type) {
2430 case AST_DATA_INTEGER:
2431 ast_data_add_int(root, mapping[i].name,
2432 mapping[i].get.AST_DATA_INTEGER(structure));
2434 case AST_DATA_UNSIGNED_INTEGER:
2435 ast_data_add_uint(root, mapping[i].name,
2436 mapping[i].get.AST_DATA_UNSIGNED_INTEGER(structure));
2438 case AST_DATA_DOUBLE:
2439 ast_data_add_dbl(root, mapping[i].name,
2440 mapping[i].get.AST_DATA_DOUBLE(structure));
2442 case AST_DATA_BOOLEAN:
2443 ast_data_add_bool(root, mapping[i].name,
2444 mapping[i].get.AST_DATA_BOOLEAN(structure));
2446 case AST_DATA_PASSWORD:
2447 ast_data_add_password(root, mapping[i].name,
2448 mapping[i].get.AST_DATA_PASSWORD(structure));
2450 case AST_DATA_TIMESTAMP:
2451 ast_data_add_timestamp(root, mapping[i].name,
2452 mapping[i].get.AST_DATA_TIMESTAMP(structure));
2454 case AST_DATA_SECONDS:
2455 ast_data_add_seconds(root, mapping[i].name,
2456 mapping[i].get.AST_DATA_SECONDS(structure));
2458 case AST_DATA_MILLISECONDS:
2459 ast_data_add_milliseconds(root, mapping[i].name,
2460 mapping[i].get.AST_DATA_MILLISECONDS(structure));
2462 case AST_DATA_STRING:
2463 ast_data_add_str(root, mapping[i].name,
2464 mapping[i].get.AST_DATA_STRING(structure));
2466 case AST_DATA_CHARACTER:
2467 ast_data_add_char(root, mapping[i].name,
2468 mapping[i].get.AST_DATA_CHARACTER(structure));
2470 case AST_DATA_CONTAINER:
2472 case AST_DATA_IPADDR:
2473 ast_data_add_ipaddr(root, mapping[i].name,
2474 mapping[i].get.AST_DATA_IPADDR(structure));
2476 case AST_DATA_POINTER:
2477 ast_data_add_ptr(root, mapping[i].name,
2478 mapping[i].get.AST_DATA_POINTER(structure));
2486 void ast_data_remove_node(struct ast_data *root, struct ast_data *child)
2488 ao2_unlink(root->children, child);
2491 void ast_data_free(struct ast_data *root)
2493 /* destroy it, this will destroy all the internal nodes. */
2497 struct ast_data_iterator *ast_data_iterator_init(struct ast_data *tree,
2498 const char *elements)
2500 struct ast_data_iterator *iterator;
2501 struct ao2_iterator i;
2502 struct ast_data *internal = tree;
2503 char *path, *ptr = NULL;
2509 /* tree is the node we want to use to iterate? or we are going
2510 * to iterate thow an internal node? */
2511 path = ast_strdupa(elements);
2513 ptr = strrchr(path, '/');
2516 internal = data_result_get_node(tree, path);
2522 iterator = ast_calloc(1, sizeof(*iterator));
2527 i = ao2_iterator_init(internal->children, 0);
2529 iterator->pattern = (ptr ? strrchr(elements, '/') + 1 : elements);
2531 /* is the last node a regular expression?, compile it! */
2532 if (!regcomp(&(iterator->regex_pattern), iterator->pattern,
2533 REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
2534 iterator->is_pattern = 1;
2537 iterator->internal_iterator = i;
2542 void ast_data_iterator_end(struct ast_data_iterator *iterator)
2544 /* decrement the reference counter. */
2545 if (iterator->last) {
2546 ao2_ref(iterator->last, -1);
2549 /* release the generated pattern. */
2550 if (iterator->is_pattern) {
2551 regfree(&(iterator->regex_pattern));
2554 ao2_iterator_destroy(&(iterator->internal_iterator));
2560 struct ast_data *ast_data_iterator_next(struct ast_data_iterator *iterator)
2562 struct ast_data *res;
2564 if (iterator->last) {
2565 /* release the last retrieved node reference. */
2566 ao2_ref(iterator->last, -1);
2569 while ((res = ao2_iterator_next(&iterator->internal_iterator))) {
2570 /* if there is no node name pattern specified, return
2572 if (!iterator->pattern) {
2576 /* if the pattern is a regular expression, check if this node
2578 if (iterator->is_pattern && !regexec(&(iterator->regex_pattern),
2579 res->name, 0, NULL, 0)) {
2583 /* if there is a pattern specified, check if this node matches
2584 * the wanted node names. */
2585 if (!iterator->is_pattern && (iterator->pattern &&
2586 !strcasecmp(res->name, iterator->pattern))) {
2593 iterator->last = res;
2598 int ast_data_retrieve(struct ast_data *tree, const char *path,
2599 struct ast_data_retrieve *content)
2601 struct ast_data *node;
2607 node = data_result_get_node(tree, path);
2609 ast_log(LOG_ERROR, "Invalid internal node %s\n", path);
2613 content->type = node->type;
2614 switch (node->type) {
2615 case AST_DATA_STRING:
2616 content->value.AST_DATA_STRING = node->payload.str;
2618 case AST_DATA_PASSWORD:
2619 content->value.AST_DATA_PASSWORD = node->payload.str;
2621 case AST_DATA_TIMESTAMP:
2622 content->value.AST_DATA_TIMESTAMP = node->payload.uint;
2624 case AST_DATA_SECONDS:
2625 content->value.AST_DATA_SECONDS = node->payload.uint;
2627 case AST_DATA_MILLISECONDS:
2628 content->value.AST_DATA_MILLISECONDS = node->payload.uint;
2630 case AST_DATA_CHARACTER:
2631 content->value.AST_DATA_CHARACTER = node->payload.character;
2633 case AST_DATA_INTEGER:
2634 content->value.AST_DATA_INTEGER = node->payload.sint;
2636 case AST_DATA_UNSIGNED_INTEGER:
2637 content->value.AST_DATA_UNSIGNED_INTEGER = node->payload.uint;
2639 case AST_DATA_BOOLEAN:
2640 content->value.AST_DATA_BOOLEAN = node->payload.boolean;
2642 case AST_DATA_IPADDR:
2643 content->value.AST_DATA_IPADDR = node->payload.ipaddr;
2645 case AST_DATA_DOUBLE:
2646 content->value.AST_DATA_DOUBLE = node->payload.dbl;
2648 case AST_DATA_CONTAINER:
2650 case AST_DATA_POINTER:
2651 content->value.AST_DATA_POINTER = node->payload.ptr;
2660 * \brief One color for each node type.
2662 static const struct {
2663 enum ast_data_type type;
2665 } data_result_color[] = {
2666 { AST_DATA_STRING, COLOR_BLUE },
2667 { AST_DATA_PASSWORD, COLOR_BRBLUE },
2668 { AST_DATA_TIMESTAMP, COLOR_CYAN },
2669 { AST_DATA_SECONDS, COLOR_MAGENTA },
2670 { AST_DATA_MILLISECONDS, COLOR_BRMAGENTA },
2671 { AST_DATA_CHARACTER, COLOR_GRAY },
2672 { AST_DATA_INTEGER, COLOR_RED },
2673 { AST_DATA_UNSIGNED_INTEGER, COLOR_RED },
2674 { AST_DATA_DOUBLE, COLOR_RED },
2675 { AST_DATA_BOOLEAN, COLOR_BRRED },
2676 { AST_DATA_CONTAINER, COLOR_GREEN },
2677 { AST_DATA_IPADDR, COLOR_BROWN },
2678 { AST_DATA_POINTER, COLOR_YELLOW },
2683 * \brief Get the color configured for a specific node type.
2684 * \param[in] type The node type.
2685 * \returns The color specified for the passed type.
2687 static int data_result_get_color(enum ast_data_type type)
2690 for (i = 0; i < ARRAY_LEN(data_result_color); i++) {
2691 if (data_result_color[i].type == type) {
2692 return data_result_color[i].color;
2701 * \brief Print a node to the CLI.
2702 * \param[in] fd The CLI file descriptor.
2703 * \param[in] node The node to print.
2704 * \param[in] depth The actual node depth in the tree.
2706 static void data_result_print_cli_node(int fd, const struct ast_data *node, uint32_t depth)
2709 struct ast_str *tabs, *output;
2711 tabs = ast_str_create(depth * 10 + 1);
2715 ast_str_reset(tabs);
2716 for (i = 0; i < depth; i++) {
2717 ast_str_append(&tabs, 0, " ");
2720 output = ast_str_create(20);
2726 ast_str_reset(output);
2727 ast_term_color_code(&output, data_result_get_color(node->type), 0);
2729 switch (node->type) {
2730 case AST_DATA_POINTER:
2731 ast_str_append(&output, 0, "%s%s: %p\n", ast_str_buffer(tabs),
2732 node->name, node->payload.ptr);
2734 case AST_DATA_PASSWORD:
2735 ast_str_append(&output, 0, "%s%s: \"%s\"\n",
2736 ast_str_buffer(tabs),
2740 case AST_DATA_STRING:
2741 ast_str_append(&output, 0, "%s%s: \"%s\"\n",
2742 ast_str_buffer(tabs),
2746 case AST_DATA_CHARACTER:
2747 ast_str_append(&output, 0, "%s%s: \'%c\'\n",
2748 ast_str_buffer(tabs),
2750 node->payload.character);
2752 case AST_DATA_CONTAINER:
2753 ast_str_append(&output, 0, "%s%s\n", ast_str_buffer(tabs),
2756 case AST_DATA_TIMESTAMP:
2757 ast_str_append(&output, 0, "%s%s: %d\n", ast_str_buffer(tabs),
2759 node->payload.uint);
2761 case AST_DATA_SECONDS:
2762 ast_str_append(&output, 0, "%s%s: %d\n", ast_str_buffer(tabs),
2764 node->payload.uint);
2766 case AST_DATA_MILLISECONDS:
2767 ast_str_append(&output, 0, "%s%s: %d\n", ast_str_buffer(tabs),
2769 node->payload.uint);
2771 case AST_DATA_INTEGER:
2772 ast_str_append(&output, 0, "%s%s: %d\n", ast_str_buffer(tabs),
2774 node->payload.sint);
2776 case AST_DATA_UNSIGNED_INTEGER:
2777 ast_str_append(&output, 0, "%s%s: %u\n", ast_str_buffer(tabs),
2779 node->payload.uint);
2781 case AST_DATA_DOUBLE:
2782 ast_str_append(&output, 0, "%s%s: %lf\n", ast_str_buffer(tabs),
2786 case AST_DATA_BOOLEAN:
2787 ast_str_append(&output, 0, "%s%s: %s\n", ast_str_buffer(tabs),
2789 ((node->payload.boolean) ? "True" : "False"));
2791 case AST_DATA_IPADDR:
2792 ast_str_append(&output, 0, "%s%s: %s\n", ast_str_buffer(tabs),
2794 ast_inet_ntoa(node->payload.ipaddr));
2800 ast_term_color_code(&output, 0, 0);
2802 ast_cli(fd, "%s", ast_str_buffer(output));
2806 if (node->type == AST_DATA_CONTAINER) {
2807 __data_result_print_cli(fd, node, depth + 1);
2813 * \brief Print out an ast_data tree to the CLI.
2814 * \param[in] fd The CLI file descriptor.
2815 * \param[in] root The root node of the tree.
2816 * \param[in] depth Actual depth.
2818 static void __data_result_print_cli(int fd, const struct ast_data *root, uint32_t depth)
2820 struct ao2_iterator iter;
2821 struct ast_data *node;
2823 if (root->type == AST_DATA_CONTAINER) {
2824 iter = ao2_iterator_init(root->children, 0);
2825 while ((node = ao2_iterator_next(&iter))) {
2826 data_result_print_cli_node(fd, node, depth + 1);
2829 ao2_iterator_destroy(&iter);
2831 data_result_print_cli_node(fd, root, depth);
2838 * \param[in] fd The CLI file descriptor.
2839 * \param[in] root The root node of the tree.
2841 static void data_result_print_cli(int fd, const struct ast_data *root)
2843 ast_cli(fd, COLORIZE_FMT "\n", COLORIZE(data_result_get_color(root->type), 0, root->name));
2845 __data_result_print_cli(fd, root, 0);
2852 * \brief Handle the CLI command "data get".
2854 static char *handle_cli_data_get(struct ast_cli_entry *e, int cmd,
2855 struct ast_cli_args *a)
2857 struct ast_data_query query = {
2858 .version = AST_DATA_QUERY_VERSION
2860 struct ast_data *tree;
2864 e->command = "data get";
2866 "Usage: data get <path> [<search> [<filter>]]\n"
2867 " Get the tree based on a path.\n";
2873 if (a->argc < e->args + 1) {
2874 return CLI_SHOWUSAGE;
2877 query.path = (char *) a->argv[e->args];
2879 if (a->argc > e->args + 1) {
2880 query.search = (char *) a->argv[e->args + 1];
2883 if (a->argc > e->args + 2) {
2884 query.filter = (char *) a->argv[e->args + 2];
2887 tree = ast_data_get(&query);
2892 data_result_print_cli(a->fd, tree);
2894 ast_data_free(tree);
2901 * \brief Print the list of data providers.
2902 * \param[in] fd The CLI file descriptor.
2903 * \param[in] name The last node visited name.
2904 * \param[in] container The childrens of the last node.
2905 * \param[in] path The path to the current node.
2907 static void data_provider_print_cli(int fd, const char *name,
2908 struct ao2_container *container, struct ast_str *path)
2910 struct ao2_iterator i;
2911 struct ast_str *current_path;
2912 struct data_provider *provider;
2914 current_path = ast_str_create(60);
2915 if (!current_path) {
2919 ast_str_reset(current_path);
2921 ast_str_set(¤t_path, 0, "%s/%s", ast_str_buffer(path), name);
2923 ast_str_set(¤t_path, 0, "%s", name);
2926 i = ao2_iterator_init(container, 0);
2927 while ((provider = ao2_iterator_next(&i))) {
2928 if (provider->handler) {
2929 /* terminal node, print it. */
2930 ast_cli(fd, "%s/%s (", ast_str_buffer(current_path),
2932 if (provider->handler->get) {
2935 ast_cli(fd, ") [%s]\n", provider->registrar);
2937 data_provider_print_cli(fd, provider->name, provider->children,
2939 ao2_ref(provider, -1);
2941 ao2_iterator_destroy(&i);
2943 ast_free(current_path);
2948 * \brief Handle CLI command "data show providers"
2950 static char *handle_cli_data_show_providers(struct ast_cli_entry *e, int cmd,
2951 struct ast_cli_args *a)
2955 e->command = "data show providers";
2957 "Usage: data show providers\n"
2958 " Show the list of registered providers\n";
2965 data_provider_print_cli(a->fd, "", root_data.container, NULL);
2973 * \brief Data API CLI commands.
2975 static struct ast_cli_entry cli_data[] = {
2976 AST_CLI_DEFINE(handle_cli_data_get, "Data API get"),
2977 AST_CLI_DEFINE(handle_cli_data_show_providers, "Show data providers")
2982 * \brief Output a tree to the AMI.
2983 * \param[in] s AMI session.
2984 * \param[in] name The root node name.
2985 * \param[in] container The root container.
2986 * \param[in] path The current path.
2988 static void data_result_manager_output(struct mansession *s, const char *name,
2989 struct ao2_container *container, struct ast_str *path, int id)
2991 struct ao2_iterator i;
2992 struct ast_str *current_path;
2993 struct ast_data *node;
2994 int current_id = id;
2996 current_path = ast_str_create(60);
2997 if (!current_path) {
3001 ast_str_reset(current_path);
3003 ast_str_set(¤t_path, 0, "%s.%s", ast_str_buffer(path), name);
3005 ast_str_set(¤t_path, 0, "%s", name);
3008 i = ao2_iterator_init(container, 0);
3009 while ((node = ao2_iterator_next(&i))) {
3010 /* terminal node, print it. */
3011 if (node->type != AST_DATA_CONTAINER) {
3012 astman_append(s, "%d-%s.%s", id, ast_str_buffer(current_path),
3015 switch (node->type) {
3016 case AST_DATA_CONTAINER:
3017 data_result_manager_output(s, node->name, node->children, current_path, ++current_id);
3019 case AST_DATA_INTEGER:
3020 astman_append(s, ": %d\r\n", node->payload.sint);
3022 case AST_DATA_TIMESTAMP:
3023 case AST_DATA_SECONDS:
3024 case AST_DATA_MILLISECONDS:
3025 case AST_DATA_UNSIGNED_INTEGER:
3026 astman_append(s, ": %u\r\n", node->payload.uint);
3028 case AST_DATA_PASSWORD:
3029 astman_append(s, ": %s\r\n", node->payload.str);
3031 case AST_DATA_STRING:
3032 astman_append(s, ": %s\r\n", node->payload.str);
3034 case AST_DATA_CHARACTER:
3035 astman_append(s, ": %c\r\n", node->payload.character);
3037 case AST_DATA_IPADDR:
3038 astman_append(s, ": %s\r\n", ast_inet_ntoa(node->payload.ipaddr));
3040 case AST_DATA_POINTER:
3042 case AST_DATA_DOUBLE:
3043 astman_append(s, ": %f\r\n", node->payload.dbl);
3045 case AST_DATA_BOOLEAN:
3046 astman_append(s, ": %s\r\n",
3047 (node->payload.boolean ? "True" : "False"));
3053 ao2_iterator_destroy(&i);
3055 ast_free(current_path);
3060 * \brief Implements the manager action: "DataGet".
3062 static int manager_data_get(struct mansession *s, const struct message *m)
3064 const char *path = astman_get_header(m, "Path");
3065 const char *search = astman_get_header(m, "Search");
3066 const char *filter = astman_get_header(m, "Filter");
3067 const char *id = astman_get_header(m, "ActionID");
3068 struct ast_data *res;
3069 struct ast_data_query query = {
3070 .version = AST_DATA_QUERY_VERSION,
3071 .path = (char *) path,
3072 .search = (char *) search,
3073 .filter = (char *) filter,
3076 if (ast_strlen_zero(path)) {
3077 astman_send_error(s, m, "'Path' parameter not specified");
3081 res = ast_data_get(&query);
3083 astman_send_error(s, m, "No data returned");
3087 astman_append(s, "Event: DataGet Tree\r\n");
3088 if (!ast_strlen_zero(id)) {
3089 astman_append(s, "ActionID: %s\r\n", id);
3091 data_result_manager_output(s, res->name, res->children, NULL, 0);
3092 astman_append(s, "\r\n");
3096 return RESULT_SUCCESS;
3099 int ast_data_add_codec(struct ast_data *root, const char *node_name, struct ast_format *format)
3101 struct ast_data *codecs, *codec;
3103 const struct ast_format_list *fmlist;
3106 codecs = ast_data_add_node(root, node_name);
3110 fmlist = ast_format_list_get(&fmlist_size);
3111 for (x = 0; x < fmlist_size; x++) {
3112 if (ast_format_cmp(&fmlist[x].format, format) == AST_FORMAT_CMP_EQUAL) {
3113 codec = ast_data_add_node(codecs, "codec");
3115 ast_format_list_destroy(fmlist);
3118 ast_data_add_str(codec, "name", fmlist[x].name);
3119 ast_data_add_int(codec, "samplespersecond", fmlist[x].samplespersecond);
3120 ast_data_add_str(codec, "description", fmlist[x].desc);
3121 ast_data_add_int(codec, "frame_length", fmlist[x].fr_len);
3124 ast_format_list_destroy(fmlist);
3129 int ast_data_add_codecs(struct ast_data *root, const char *node_name, struct ast_format_cap *cap)
3131 struct ast_data *codecs, *codec;
3133 const struct ast_format_list *fmlist;
3136 codecs = ast_data_add_node(root, node_name);
3140 fmlist = ast_format_list_get(&fmlist_size);
3141 for (x = 0; x < fmlist_size; x++) {
3142 if (ast_format_cap_iscompatible(cap, &fmlist[x].format)) {
3143 codec = ast_data_add_node(codecs, "codec");
3145 ast_format_list_destroy(fmlist);
3148 ast_data_add_str(codec, "name", fmlist[x].name);
3149 ast_data_add_int(codec, "samplespersecond", fmlist[x].samplespersecond);
3150 ast_data_add_str(codec, "description", fmlist[x].desc);
3151 ast_data_add_int(codec, "frame_length", fmlist[x].fr_len);
3154 ast_format_list_destroy(fmlist);
3159 #ifdef TEST_FRAMEWORK
3163 * \brief Structure used to test how to add a complete structure,
3164 * and how to compare it.
3166 struct test_structure {
3168 unsigned int b_bool:1;
3170 unsigned int a_uint;
3175 * \brief test_structure mapping.
3177 #define DATA_EXPORT_TEST_STRUCTURE(MEMBER) \
3178 MEMBER(test_structure, a_int, AST_DATA_INTEGER) \
3179 MEMBER(test_structure, b_bool, AST_DATA_BOOLEAN) \
3180 MEMBER(test_structure, c_str, AST_DATA_STRING) \
3181 MEMBER(test_structure, a_uint, AST_DATA_UNSIGNED_INTEGER)
3183 AST_DATA_STRUCTURE(test_structure, DATA_EXPORT_TEST_STRUCTURE);
3187 * \brief Callback implementation.
3189 static int test_data_full_provider(const struct ast_data_search *search,
3190 struct ast_data *root)
3192 struct ast_data *test_structure;
3193 struct test_structure local_test_structure = {
3196 .c_str = "test string",
3200 test_structure = ast_data_add_node(root, "test_structure");
3201 if (!test_structure) {
3202 ast_debug(1, "Internal data api error\n");
3206 /* add the complete structure. */
3207 ast_data_add_structure(test_structure, test_structure, &local_test_structure);
3209 if (!ast_data_search_match(search, test_structure)) {
3210 ast_data_remove_node(root, test_structure);
3218 * \brief Handler definition for the full provider.
3220 static const struct ast_data_handler full_provider = {
3221 .version = AST_DATA_HANDLER_VERSION,
3222 .get = test_data_full_provider
3227 * \brief Structure used to define multiple providers at once.
3229 static const struct ast_data_entry test_providers[] = {
3230 AST_DATA_ENTRY("test/node1/node11/node111", &full_provider)
3233 AST_TEST_DEFINE(test_data_get)
3235 struct ast_data *res, *node;
3236 struct ast_data_iterator *i;
3237 struct ast_data_query query = {
3238 .version = AST_DATA_QUERY_VERSION,
3239 .path = "test/node1/node11/node111",
3240 .search = "node111/test_structure/a_int=10",
3241 .filter = "node111/test_structure/a*int"
3246 info->name = "data_test";
3247 info->category = "/main/data/";
3248 info->summary = "Data API unit test";
3250 "Tests whether data API get implementation works as expected.";
3251 return AST_TEST_NOT_RUN;
3256 ast_data_register_multiple_core(test_providers, ARRAY_LEN(test_providers));
3258 res = ast_data_get(&query);
3260 ast_test_status_update(test, "Unable to get tree.");
3261 ast_data_unregister("test/node1/node11/node111");
3262 return AST_TEST_FAIL;
3265 /* initiate the iterator and check for errors. */
3266 i = ast_data_iterator_init(res, "test_structure/");
3268 ast_test_status_update(test, "Unable to initiate the iterator.");
3270 ast_data_unregister("test/node1/node11/node111");
3271 return AST_TEST_FAIL;
3274 /* walk the returned nodes. */
3275 while ((node = ast_data_iterator_next(i))) {
3276 if (!strcmp(ast_data_retrieve_name(node), "a_int")) {
3277 if (ast_data_retrieve_int(node, "/") != 10) {
3278 ast_data_iterator_end(i);
3280 ast_data_unregister("test/node1/node11/node111");
3281 return AST_TEST_FAIL;
3283 } else if (!strcmp(ast_data_retrieve_name(node), "a_uint")) {
3284 if (ast_data_retrieve_uint(node, "/") != 20) {
3285 ast_data_iterator_end(i);
3287 ast_data_unregister("test/node1/node11/node111");
3288 return AST_TEST_FAIL;
3293 /* finish the iterator. */
3294 ast_data_iterator_end(i);
3298 ast_data_unregister("test/node1/node11/node111");
3300 return AST_TEST_PASS;
3305 /*! \internal \brief Clean up resources on Asterisk shutdown */
3306 static void data_shutdown(void)
3308 ast_manager_unregister("DataGet");
3309 ast_cli_unregister_multiple(cli_data, ARRAY_LEN(cli_data));
3310 ao2_t_ref(root_data.container, -1, "Unref root_data.container in data_shutdown");
3311 root_data.container = NULL;
3312 ast_rwlock_destroy(&root_data.lock);
3315 int ast_data_init(void)
3319 ast_rwlock_init(&root_data.lock);
3321 if (!(root_data.container = ao2_container_alloc(NUM_DATA_NODE_BUCKETS,
3322 data_provider_hash, data_provider_cmp))) {
3326 res |= ast_cli_register_multiple(cli_data, ARRAY_LEN(cli_data));
3328 res |= ast_manager_register_xml_core("DataGet", 0, manager_data_get);
3330 #ifdef TEST_FRAMEWORK
3331 AST_TEST_REGISTER(test_data_get);
3334 ast_register_atexit(data_shutdown);