data.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2009, Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * \brief Data retrieval API.
00020  *
00021  * \author Brett Bryant <brettbryant@gmail.com>
00022  * \author Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
00023  */
00024 
00025 /*** MODULEINFO
00026    <support_level>core</support_level>
00027  ***/
00028 
00029 #include "asterisk.h"
00030 
00031 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 433498 $")
00032 
00033 #include "asterisk/_private.h"
00034 
00035 #include <regex.h>
00036 
00037 #include "asterisk/module.h"
00038 #include "asterisk/utils.h"
00039 #include "asterisk/lock.h"
00040 #include "asterisk/data.h"
00041 #include "asterisk/astobj2.h"
00042 #include "asterisk/xml.h"
00043 #include "asterisk/cli.h"
00044 #include "asterisk/term.h"
00045 #include "asterisk/manager.h"
00046 #include "asterisk/test.h"
00047 #include "asterisk/frame.h"
00048 #include "asterisk/codec.h"
00049 
00050 /*** DOCUMENTATION
00051    <manager name="DataGet" language="en_US">
00052       <synopsis>
00053          Retrieve the data api tree.
00054       </synopsis>
00055       <syntax>
00056          <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
00057          <parameter name="Path" required="true" />
00058          <parameter name="Search" />
00059          <parameter name="Filter" />
00060       </syntax>
00061       <description>
00062          <para>Retrieve the data api tree.</para>
00063       </description>
00064    </manager>
00065  ***/
00066 
00067 #define NUM_DATA_NODE_BUCKETS 59
00068 #define NUM_DATA_RESULT_BUCKETS 59
00069 #define NUM_DATA_SEARCH_BUCKETS 59
00070 #define NUM_DATA_FILTER_BUCKETS 59
00071 
00072 /*! \brief The last compatible version. */
00073 static const uint32_t latest_handler_compatible_version = 0;
00074 
00075 /*! \brief The last compatible version. */
00076 static const uint32_t latest_query_compatible_version = 0;
00077 
00078 /*! \brief Current handler structure version. */
00079 static const uint32_t current_handler_version = AST_DATA_HANDLER_VERSION;
00080 
00081 /*! \brief Current query structure version. */
00082 static const uint32_t current_query_version = AST_DATA_QUERY_VERSION;
00083 
00084 /*! \brief The data tree to be returned by the callbacks and
00085       managed by functions local to this file. */
00086 struct ast_data {
00087    enum ast_data_type type;
00088 
00089    /*! \brief The node content. */
00090    union {
00091       int32_t sint;
00092       uint32_t uint;
00093       double dbl;
00094       unsigned int boolean;
00095       char *str;
00096       char character;
00097       struct in_addr ipaddr;
00098       void *ptr;
00099    } payload;
00100 
00101    /*! \brief The filter node that depends on the current node,
00102     * this is used only when creating the result tree. */
00103    const struct data_filter *filter;
00104 
00105    /*! \brief The list of nodes inside this node. */
00106    struct ao2_container *children;
00107    /*! \brief The name of the node. */
00108    char name[0];
00109 };
00110 
00111 /*! \brief Type of comparisons allow in the search string. */
00112 enum data_search_comparison {
00113    DATA_CMP_UNKNOWN,
00114    DATA_CMP_EQ,   /* =  */
00115    DATA_CMP_NEQ,  /* != */
00116    DATA_CMP_GT,   /* >  */
00117    DATA_CMP_GE,   /* >= */
00118    DATA_CMP_LT,   /* <  */
00119    DATA_CMP_LE /* <= */
00120 };
00121 
00122 /*! \brief The list of nodes with their search requirement. */
00123 struct ast_data_search {
00124    /*! \brief The value of the comparison. */
00125    char *value;
00126    /*! \brief The type of comparison. */
00127    enum data_search_comparison cmp_type;
00128    /*! \brief reference another node. */
00129    struct ao2_container *children;
00130    /*! \brief The name of the node we are trying to compare. */
00131    char name[0];
00132 };
00133 
00134 struct data_filter;
00135 
00136 /*! \brief The filter node. */
00137 struct data_filter {
00138    /*! \brief node childrens. */
00139    struct ao2_container *children;
00140    /*! \brief glob list */
00141    AST_LIST_HEAD_NOLOCK(glob_list_t, data_filter) glob_list;
00142    /*! \brief glob list entry */
00143    AST_LIST_ENTRY(data_filter) list;
00144    /*! \brief node name. */
00145    char name[0];
00146 };
00147 
00148 /*! \brief A data container node pointing to the registered handler. */
00149 struct data_provider {
00150    /*! \brief node content handler. */
00151    const struct ast_data_handler *handler;
00152    /*! \brief Module providing this handler. */
00153    struct ast_module *module;
00154    /*! \brief children nodes. */
00155    struct ao2_container *children;
00156    /*! \brief Who registered this node. */
00157    const char *registrar;
00158    /*! \brief Node name. */
00159    char name[0];
00160 };
00161 
00162 /*! \brief This structure is used by the iterator. */
00163 struct ast_data_iterator {
00164    /*! \brief The internal iterator. */
00165    struct ao2_iterator internal_iterator;
00166    /*! \brief The last returned node. */
00167    struct ast_data *last;
00168    /*! \brief The iterator pattern. */
00169    const char *pattern;
00170    /*! \brief The compiled patter. */
00171    regex_t regex_pattern;
00172    /*! \brief is a regular expression. */
00173    unsigned int is_pattern:1;
00174 };
00175 
00176 struct {
00177    /*! \brief The asterisk data main content structure. */
00178    struct ao2_container *container;
00179    /*! \brief asterisk data locking mechanism. */
00180    ast_rwlock_t lock;
00181 } root_data;
00182 
00183 static void __data_result_print_cli(int fd, const struct ast_data *root, uint32_t depth);
00184 
00185 /*!
00186  * \internal
00187  * \brief Common string hash function.
00188  * \see ast_data_init
00189  */
00190 static int data_provider_hash(const void *obj, const int flags)
00191 {
00192    const struct data_provider *node = obj;
00193    return ast_str_case_hash(node->name);
00194 }
00195 
00196 /*!
00197  * \internal
00198  * \brief Compare two data_provider's.
00199  * \see ast_data_init
00200  */
00201 static int data_provider_cmp(void *obj1, void *obj2, int flags)
00202 {
00203    struct data_provider *node1 = obj1, *node2 = obj2;
00204    return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
00205 }
00206 
00207 /*!
00208  * \internal
00209  * \brief Common string hash function for data nodes
00210  */
00211 static int data_result_hash(const void *obj, const int flags)
00212 {
00213    const struct ast_data *node = obj;
00214    return ast_str_hash(node->name);
00215 }
00216 
00217 /*!
00218  * \internal
00219  * \brief Common string comparison function
00220  */
00221 static int data_result_cmp(void *obj, void *arg, int flags)
00222 {
00223    struct ast_data *node1 = obj, *node2 = arg;
00224    return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
00225 }
00226 
00227 /*!
00228  * \internal
00229  * \brief Lock the data registered handlers structure for writing.
00230  * \see data_unlock
00231  */
00232 #define data_write_lock() ast_rwlock_wrlock(&root_data.lock)
00233 
00234 /*!
00235  * \internal
00236  * \brief Lock the data registered handlers structure for reading.
00237  * \see data_unlock
00238  */
00239 #define data_read_lock() ast_rwlock_rdlock(&root_data.lock)
00240 
00241 /*!
00242  * \internal
00243  * \brief Unlock the data registered handlers structure.
00244  */
00245 #define data_unlock() ast_rwlock_unlock(&root_data.lock)
00246 
00247 /*!
00248  * \internal
00249  * \brief Check if a version is compatible with the current core.
00250  * \param[in] structure_version The current structure version.
00251  * \param[in] latest_compatible The latest compatible version.
00252  * \param[in] current The current Data API version.
00253  * \retval 1 If the module is compatible.
00254  * \retval 0 If the module is NOT compatible.
00255  */
00256 static int data_structure_compatible(int structure_version, uint32_t latest_compatible,
00257    uint32_t current)
00258 {
00259    if (structure_version >= latest_compatible && structure_version <= current) {
00260       return 1;
00261    }
00262 
00263    ast_log(LOG_ERROR, "A module is not compatible with the"
00264       "current data api version\n");
00265 
00266    return 0;
00267 }
00268 
00269 /*!
00270  * \internal
00271  * \brief Get the next node name in a path (/node1/node2)
00272  *        Avoid null nodes like //node1//node2/node3.
00273  * \param[in] path The path where we are going to search for the next node name.
00274  * \retval The next node name we found inside the given path.
00275  * \retval NULL if there are no more node names.
00276  */
00277 static char *next_node_name(char **path)
00278 {
00279    char *res;
00280 
00281    do {
00282       res = strsep(path, "/");
00283    } while (res && ast_strlen_zero(res));
00284 
00285    return res;
00286 }
00287 
00288 /*!
00289  * \internal
00290  * \brief Release the memory allocated by a call to ao2_alloc.
00291  */
00292 static void data_provider_destructor(void *obj)
00293 {
00294    struct data_provider *provider = obj;
00295 
00296    ao2_ref(provider->children, -1);
00297 }
00298 
00299 /*!
00300  * \internal
00301  * \brief Create a new data node.
00302  * \param[in] name The name of the node we are going to create.
00303  * \param[in] handler The handler registered for this node.
00304  * \param[in] registrar The name of the registrar.
00305  * \retval NULL on error.
00306  * \retval The allocated data node structure.
00307  */
00308 static struct data_provider *data_provider_new(const char *name,
00309    const struct ast_data_handler *handler, const char *registrar)
00310 {
00311    struct data_provider *node;
00312    size_t namelen;
00313 
00314    namelen = strlen(name) + 1;
00315 
00316    node = ao2_alloc(sizeof(*node) + namelen, data_provider_destructor);
00317    if (!node) {
00318       return NULL;
00319    }
00320 
00321    node->handler = handler;
00322    node->registrar = registrar;
00323    strcpy(node->name, name);
00324 
00325    /* initialize the childrens container. */
00326    if (!(node->children = ao2_container_alloc(NUM_DATA_NODE_BUCKETS,
00327          data_provider_hash, data_provider_cmp))) {
00328       ao2_ref(node, -1);
00329       return NULL;
00330    }
00331 
00332    return node;
00333 }
00334 
00335 /*!
00336  * \internal
00337  * \brief Add a child node named 'name' to the 'parent' node.
00338  * \param[in] parent Where to add the child node.
00339  * \param[in] name The name of the child node.
00340  * \param[in] handler The handler structure.
00341  * \param[in] registrar Who registered this node.
00342  * \retval NULL on error.
00343  * \retval A newly allocated child in parent.
00344  */
00345 static struct data_provider *data_provider_add_child(struct ao2_container *parent,
00346    const char *name, const struct ast_data_handler *handler, const char *registrar)
00347 {
00348    struct data_provider *child;
00349 
00350    child = data_provider_new(name, handler, registrar);
00351    if (!child) {
00352       return NULL;
00353    }
00354 
00355    ao2_link(parent, child);
00356 
00357    return child;
00358 }
00359 
00360 /*!
00361  * \internal
00362  * \brief Find a child node, based on his name.
00363  * \param[in] parent Where to find the node.
00364  * \param[in] name The node name to find.
00365  * \param[in] registrar Also check if the node was being used by this registrar.
00366  * \retval NULL if a node wasn't found.
00367  * \retval The node found.
00368  * \note Remember to decrement the ref count of the returned node after using it.
00369  */
00370 static struct data_provider *data_provider_find(struct ao2_container *parent,
00371    const char *name, const char *registrar)
00372 {
00373    struct data_provider *find_node, *found;
00374 
00375    /* XXX avoid allocating a new data node for searching... */
00376    find_node = data_provider_new(name, NULL, NULL);
00377    if (!find_node) {
00378       return NULL;
00379    }
00380 
00381    found = ao2_find(parent, find_node, OBJ_POINTER);
00382 
00383    /* free the created node used for searching. */
00384    ao2_ref(find_node, -1);
00385 
00386    if (found && found->registrar && registrar) {
00387       if (strcmp(found->registrar, registrar)) {
00388          /* if the name doesn't match, do not return this node. */
00389          ast_debug(1, "Registrar doesn't match, node was registered"
00390             " by '%s' and we are searching for '%s'\n",
00391             found->registrar, registrar);
00392          ao2_ref(found, -1);
00393          return NULL;
00394       }
00395    }
00396 
00397    return found;
00398 }
00399 
00400 /*!
00401  * \internal
00402  * \brief Release a group of nodes.
00403  * \param[in] parent The parent node.
00404  * \param[in] path The path of nodes to release.
00405  * \param[in] registrar Who registered this node.
00406  * \retval <0 on error.
00407  * \retval 0 on success.
00408  * \see data_provider_create
00409  */
00410 static int data_provider_release(struct ao2_container *parent, const char *path,
00411    const char *registrar)
00412 {
00413    char *node_name, *rpath;
00414    struct data_provider *child;
00415    int ret = 0;
00416 
00417    rpath = ast_strdupa(path);
00418 
00419    node_name = next_node_name(&rpath);
00420    if (!node_name) {
00421       return -1;
00422    }
00423 
00424    child = data_provider_find(parent, node_name, registrar);
00425    if (!child) {
00426       return -1;
00427    }
00428 
00429    /* if this is not a terminal node. */
00430    if (!child->handler && rpath) {
00431       ret = data_provider_release(child->children, rpath, registrar);
00432    }
00433 
00434    /* if this node is empty, unlink it. */
00435    if (!ret && !ao2_container_count(child->children)) {
00436       ao2_unlink(parent, child);
00437    }
00438 
00439    ao2_ref(child, -1);
00440 
00441    return ret;
00442 }
00443 
00444 /*!
00445  * \internal
00446  * \brief Release every node registered by 'registrar'.
00447  * \param[in] parent The parent node.
00448  * \param[in] registrar
00449  * \see __ast_data_unregister
00450  */
00451 static void data_provider_release_all(struct ao2_container *parent,
00452    const char *registrar)
00453 {
00454    struct ao2_iterator i;
00455    struct data_provider *node;
00456 
00457    i = ao2_iterator_init(parent, 0);
00458    while ((node = ao2_iterator_next(&i))) {
00459       if (!node->handler) {
00460          /* this is a non-terminal node, go inside it. */
00461          data_provider_release_all(node->children, registrar);
00462          if (!ao2_container_count(node->children)) {
00463             /* if this node was left empty, unlink it. */
00464             ao2_unlink(parent, node);
00465          }
00466       } else {
00467          if (!strcmp(node->registrar, registrar)) {
00468             /* if the registrars match, release it! */
00469             ao2_unlink(parent, node);
00470          }
00471       }
00472       ao2_ref(node, -1);
00473    }
00474    ao2_iterator_destroy(&i);
00475 
00476 }
00477 
00478 /*!
00479  * \internal
00480  * \brief Create the middle nodes for the specified path (asterisk/testnode1/childnode)
00481  * \param[in] parent Where to add the middle nodes structure.
00482  * \param[in] path The path of nodes to add.
00483  * \param[in] registrar Who is trying to create this node provider.
00484  * \retval NULL on error.
00485  * \retval The created node.
00486  * \see data_provider_release
00487  */
00488 static struct data_provider *data_provider_create(struct ao2_container *parent,
00489    const char *path, const char *registrar)
00490 {
00491    char *rpath, *node_name;
00492    struct data_provider *child, *ret = NULL;
00493 
00494    rpath = ast_strdupa(path);
00495 
00496    node_name = next_node_name(&rpath);
00497    if (!node_name) {
00498       /* no more nodes to create. */
00499       return NULL;
00500    }
00501 
00502    child = data_provider_find(parent, node_name, NULL);
00503 
00504    if (!child) {
00505       /* nodes without handler are non-terminal nodes. */
00506       child = data_provider_add_child(parent, node_name, NULL, registrar);
00507    }
00508 
00509    if (rpath) {
00510       ret = data_provider_create(child->children, rpath, registrar);
00511       if (ret) {
00512          ao2_ref(child, -1);
00513       }
00514    }
00515 
00516    return ret ? ret : child;
00517 }
00518 
00519 int __ast_data_register(const char *path, const struct ast_data_handler *handler,
00520    const char *registrar, struct ast_module *mod)
00521 {
00522    struct data_provider *node;
00523 
00524    if (!path) {
00525       return -1;
00526    }
00527 
00528    /* check if the handler structure is compatible. */
00529    if (!data_structure_compatible(handler->version,
00530       latest_handler_compatible_version,
00531       current_handler_version)) {
00532       return -1;
00533    }
00534 
00535    /* create the node structure for the registered handler. */
00536    data_write_lock();
00537 
00538    node = data_provider_create(root_data.container, path, registrar);
00539    if (!node) {
00540       ast_log(LOG_ERROR, "Unable to create the specified path (%s) "
00541          "for '%s'.\n", path, registrar);
00542       data_unlock();
00543       return -1;
00544    }
00545 
00546    if (ao2_container_count(node->children) || node->handler) {
00547       ast_log(LOG_ERROR, "The node '%s' was already registered. "
00548          "We were unable to register '%s' for registrar '%s'.\n",
00549          node->name, path, registrar);
00550       ao2_ref(node, -1);
00551       data_unlock();
00552       return -1;
00553    }
00554 
00555    /* add handler to that node. */
00556    node->handler = handler;
00557    node->module = mod;
00558 
00559    ao2_ref(node, -1);
00560 
00561    data_unlock();
00562 
00563    return 0;
00564 }
00565 
00566 int __ast_data_register_multiple(const struct ast_data_entry *data_entries,
00567    size_t entries, const char *registrar, struct ast_module *mod)
00568 {
00569    int i, res;
00570 
00571    for (i = 0; i < entries; i++) {
00572       res = __ast_data_register(data_entries[i].path, data_entries[i].handler,
00573             registrar, mod);
00574       if (res) {
00575          /* unregister all the already registered nodes, and make
00576           * this an atomic action. */
00577          while ((--i) >= 0) {
00578             __ast_data_unregister(data_entries[i].path, registrar);
00579          }
00580          return -1;
00581       }
00582    }
00583 
00584    return 0;
00585 }
00586 
00587 int __ast_data_unregister(const char *path, const char *registrar)
00588 {
00589    int ret = 0;
00590 
00591    data_write_lock();
00592    if (path) {
00593       ret = data_provider_release(root_data.container, path, registrar);
00594    } else {
00595       data_provider_release_all(root_data.container, registrar);
00596    }
00597    data_unlock();
00598 
00599    if (path && ret) {
00600       ast_log(LOG_ERROR, "Unable to unregister '%s' for '%s'\n",
00601          path, registrar);
00602    }
00603 
00604    return ret;
00605 }
00606 
00607 /*!
00608  * \internal
00609  * \brief Is a char used to specify a comparison?
00610  * \param[in] a Character to evaluate.
00611  * \retval 1 It is a char used to specify a comparison.
00612  * \retval 0 It is NOT a char used to specify a comparison.
00613  */
00614 static int data_search_comparison_char(char a)
00615 {
00616    switch (a) {
00617    case '!':
00618    case '=':
00619    case '<':
00620    case '>':
00621       return 1;
00622    }
00623 
00624    return 0;
00625 }
00626 
00627 /*!
00628  * \internal
00629  * \brief Get the type of comparison.
00630  */
00631 static enum data_search_comparison data_search_comparison_type(const char *comparison)
00632 {
00633    if (!strcmp(comparison, "=")) {
00634       return DATA_CMP_EQ;
00635    } else if (!strcmp(comparison, "!=")) {
00636       return DATA_CMP_NEQ;
00637    } else if (!strcmp(comparison, "<")) {
00638       return DATA_CMP_LT;
00639    } else if (!strcmp(comparison, ">")) {
00640       return DATA_CMP_GT;
00641    } else if (!strcmp(comparison, "<=")) {
00642       return DATA_CMP_LE;
00643    } else if (!strcmp(comparison, ">=")) {
00644       return DATA_CMP_GE;
00645    }
00646 
00647    return DATA_CMP_UNKNOWN;
00648 }
00649 
00650 /*!
00651  * \internal
00652  * \brief Common string hash function for data nodes
00653  */
00654 static int data_search_hash(const void *obj, const int flags)
00655 {
00656    const struct ast_data_search *node = obj;
00657    return ast_str_hash(node->name);
00658 }
00659 
00660 /*!
00661  * \internal
00662  * \brief Common string comparison function
00663  */
00664 static int data_search_cmp(void *obj, void *arg, int flags)
00665 {
00666    struct ast_data_search *node1 = obj, *node2 = arg;
00667    return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
00668 }
00669 
00670 /*!
00671  * \internal
00672  * \brief Destroy the ao2 search node.
00673  */
00674 static void data_search_destructor(void *obj)
00675 {
00676    struct ast_data_search *node = obj;
00677 
00678    if (node->value) {
00679       ast_free(node->value);
00680    }
00681 
00682    ao2_ref(node->children, -1);
00683 }
00684 
00685 /*!
00686  * \internal
00687  * \brief Allocate a search node.
00688  * \retval NULL on error.
00689  * \retval non-NULL The allocated search node structure.
00690  */
00691 static struct ast_data_search *data_search_alloc(const char *name)
00692 {
00693    struct ast_data_search *res;
00694    size_t name_len = strlen(name) + 1;
00695 
00696    res = ao2_alloc(sizeof(*res) + name_len, data_search_destructor);
00697    if (!res) {
00698       return NULL;
00699    }
00700 
00701    res->children = ao2_container_alloc(NUM_DATA_SEARCH_BUCKETS, data_search_hash,
00702       data_search_cmp);
00703 
00704    if (!res->children) {
00705       ao2_ref(res, -1);
00706       return NULL;
00707    }
00708 
00709    strcpy(res->name, name);
00710 
00711    return res;
00712 }
00713 
00714 /*!
00715  * \internal
00716  * \brief Find a child node, based on his name.
00717  * \param[in] parent Where to find the node.
00718  * \param[in] name The node name to find.
00719  * \retval NULL if a node wasn't found.
00720  * \retval The node found.
00721  * \note Remember to decrement the ref count of the returned node after using it.
00722  */
00723 static struct ast_data_search *data_search_find(struct ao2_container *parent,
00724    const char *name)
00725 {
00726    struct ast_data_search *find_node, *found;
00727 
00728    find_node = data_search_alloc(name);
00729    if (!find_node) {
00730       return NULL;
00731    }
00732 
00733    found = ao2_find(parent, find_node, OBJ_POINTER);
00734 
00735    /* free the created node used for searching. */
00736    ao2_ref(find_node, -1);
00737 
00738    return found;
00739 }
00740 
00741 /*!
00742  * \internal
00743  * \brief Add a child node named 'name' to the 'parent' node.
00744  * \param[in] parent Where to add the child node.
00745  * \param[in] name The name of the child node.
00746  * \retval NULL on error.
00747  * \retval A newly allocated child in parent.
00748  */
00749 static struct ast_data_search *data_search_add_child(struct ao2_container *parent,
00750    const char *name)
00751 {
00752    struct ast_data_search *child;
00753 
00754    child = data_search_alloc(name);
00755    if (!child) {
00756       return NULL;
00757    }
00758 
00759    ao2_link(parent, child);
00760 
00761    return child;
00762 }
00763 
00764 /*!
00765  * \internal
00766  * \brief Create the middle nodes for the specified path (asterisk/testnode1/childnode)
00767  * \param[in] parent Where to add the middle nodes structure.
00768  * \param[in] path The path of nodes to add.
00769  * \retval NULL on error.
00770  * \retval The created node.
00771  */
00772 static struct ast_data_search *data_search_create(struct ao2_container *parent,
00773    const char *path)
00774 {
00775    char *rpath, *node_name;
00776    struct ast_data_search *child = NULL;
00777    struct ao2_container *current = parent;
00778 
00779    rpath = ast_strdupa(path);
00780 
00781    node_name = next_node_name(&rpath);
00782    while (node_name) {
00783       child = data_search_find(current, node_name);
00784       if (!child) {
00785          child = data_search_add_child(current, node_name);
00786       }
00787       ao2_ref(child, -1);
00788       current = child->children;
00789       node_name = next_node_name(&rpath);
00790    }
00791 
00792    return child;
00793 }
00794 
00795 /*!
00796  * \internal
00797  * \brief Allocate a tree with the search string parsed.
00798  * \param[in] search_string The search string.
00799  * \retval NULL on error.
00800  * \retval non-NULL A dynamically allocated search tree.
00801  */
00802 static struct ast_data_search *data_search_generate(const char *search_string)
00803 {
00804    struct ast_str *name, *value, *comparison;
00805    char *elements, *search_string_dup, *saveptr;
00806    int i;
00807    struct ast_data_search *root, *child;
00808    enum data_search_comparison cmp_type;
00809    size_t search_string_len;
00810 
00811    if (!search_string) {
00812       ast_log(LOG_ERROR, "You must pass a valid search string.\n");
00813       return NULL;
00814    }
00815 
00816    search_string_len = strlen(search_string);
00817 
00818    name = ast_str_create(search_string_len);
00819    if (!name) {
00820       return NULL;
00821    }
00822    value = ast_str_create(search_string_len);
00823    if (!value) {
00824       ast_free(name);
00825       return NULL;
00826    }
00827    comparison = ast_str_create(search_string_len);
00828    if (!comparison) {
00829       ast_free(name);
00830       ast_free(value);
00831       return NULL;
00832    }
00833 
00834    search_string_dup = ast_strdupa(search_string);
00835 
00836    /* Create the root node (just used as a container) */
00837    root = data_search_alloc("/");
00838    if (!root) {
00839       ast_free(name);
00840       ast_free(value);
00841       ast_free(comparison);
00842       return NULL;
00843    }
00844 
00845    for (elements = strtok_r(search_string_dup, ",", &saveptr); elements;
00846       elements = strtok_r(NULL, ",", &saveptr)) {
00847       /* Parse the name */
00848       ast_str_reset(name);
00849       for (i = 0; !data_search_comparison_char(elements[i]) &&
00850          elements[i]; i++) {
00851          ast_str_append(&name, 0, "%c", elements[i]);
00852       }
00853 
00854       /* check if the syntax is ok. */
00855       if (!data_search_comparison_char(elements[i])) {
00856          /* if this is the end of the string, then this is
00857           * an error! */
00858          ast_log(LOG_ERROR, "Invalid search string!\n");
00859          continue;
00860       }
00861 
00862       /* parse the comparison string. */
00863       ast_str_reset(comparison);
00864       for (; data_search_comparison_char(elements[i]) && elements[i]; i++) {
00865          ast_str_append(&comparison, 0, "%c", elements[i]);
00866       }
00867 
00868       /* parse the value string. */
00869       ast_str_reset(value);
00870       for (; elements[i]; i++) {
00871          ast_str_append(&value, 0, "%c", elements[i]);
00872       }
00873 
00874       cmp_type = data_search_comparison_type(ast_str_buffer(comparison));
00875       if (cmp_type == DATA_CMP_UNKNOWN) {
00876          ast_log(LOG_ERROR, "Invalid comparison '%s'\n",
00877             ast_str_buffer(comparison));
00878          continue;
00879       }
00880 
00881       /* add this node to the tree. */
00882       child = data_search_create(root->children, ast_str_buffer(name));
00883       if (child) {
00884          child->cmp_type = cmp_type;
00885          child->value = ast_strdup(ast_str_buffer(value));
00886       }
00887    }
00888 
00889    ast_free(name);
00890    ast_free(value);
00891    ast_free(comparison);
00892 
00893    return root;
00894 }
00895 
00896 /*!
00897  * \internal
00898  * \brief Release the allocated memory for the search tree.
00899  * \param[in] search The search tree root node.
00900  */
00901 static void data_search_release(struct ast_data_search *search)
00902 {
00903    ao2_ref(search, -1);
00904 }
00905 
00906 /*!
00907  * \internal
00908  * \brief Based on the kind of comparison and the result in cmpval, return
00909  *        if it matches.
00910  * \param[in] cmpval A result returned by a strcmp() for example.
00911  * \param[in] comparison_type The kind of comparison (<,>,=,!=,...)
00912  * \retval 1 If the comparison doesn't match.
00913  * \retval 0 If the comparison matches.
00914  */
00915 static inline int data_search_comparison_result(int cmpval,
00916    enum data_search_comparison comparison_type)
00917 {
00918    switch (comparison_type) {
00919    case DATA_CMP_GE:
00920       if (cmpval >= 0) {
00921          return 0;
00922       }
00923       break;
00924    case DATA_CMP_LE:
00925       if (cmpval <= 0) {
00926          return 0;
00927       }
00928       break;
00929    case DATA_CMP_EQ:
00930       if (cmpval == 0) {
00931          return 0;
00932       }
00933       break;
00934    case DATA_CMP_NEQ:
00935       if (cmpval != 0) {
00936          return 0;
00937       }
00938       break;
00939    case DATA_CMP_LT:
00940       if (cmpval < 0) {
00941          return 0;
00942       }
00943       break;
00944    case DATA_CMP_GT:
00945       if (cmpval > 0) {
00946          return 0;
00947       }
00948       break;
00949    case DATA_CMP_UNKNOWN:
00950       break;
00951    }
00952    return 1;
00953 }
00954 
00955 /*!
00956  * \internal
00957  * \brief Get an internal node, from the search tree.
00958  * \param[in] node A node container.
00959  * \param[in] path The path to the needed internal node.
00960  * \retval NULL if the internal node is not found.
00961  * \retval non-NULL the internal node with path 'path'.
00962  */
00963 static struct ast_data_search *data_search_get_node(const struct ast_data_search *node,
00964    const char *path)
00965 {
00966    char *savepath, *node_name;
00967    struct ast_data_search *child, *current = (struct ast_data_search *) node;
00968 
00969    if (!node) {
00970       return NULL;
00971    }
00972 
00973    savepath = ast_strdupa(path);
00974    node_name = next_node_name(&savepath);
00975 
00976    while (node_name) {
00977       child = data_search_find(current->children, node_name);
00978       if (current != node) {
00979          ao2_ref(current, -1);
00980       }
00981       if (!child) {
00982          return NULL;
00983       };
00984       current = child;
00985       node_name = next_node_name(&savepath);
00986    }
00987 
00988    return current;
00989 }
00990 
00991 /*!
00992  * \internal
00993  * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
00994  *        current string value.
00995  *        .search = "somename=somestring"
00996  *        name = "somename"
00997  *        value is the current value of something and will be evaluated against "somestring".
00998  * \param[in] root The root node pointer of the search tree.
00999  * \param[in] name The name of the specific.
01000  * \param[in] value The value to compare.
01001  * \returns The strcmp return value.
01002  */
01003 static int data_search_cmp_string(const struct ast_data_search *root, const char *name,
01004    char *value)
01005 {
01006    struct ast_data_search *child;
01007    enum data_search_comparison cmp_type;
01008    int ret;
01009 
01010    child = data_search_get_node(root, name);
01011    if (!child) {
01012       return 0;
01013    }
01014 
01015    ret = strcmp(value, child->value);
01016    cmp_type = child->cmp_type;
01017 
01018    ao2_ref(child, -1);
01019 
01020    return data_search_comparison_result(ret, cmp_type);
01021 }
01022 
01023 /*!
01024  * \internal
01025  * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
01026  *        current pointer address value.
01027  *        .search = "something=0x32323232"
01028  *        name = "something"
01029  *        value is the current value of something and will be evaluated against "0x32323232".
01030  * \param[in] root The root node pointer of the search tree.
01031  * \param[in] name The name of the specific.
01032  * \param[in] ptr The pointer address to compare.
01033  * \returns The (value - current_value) result.
01034  */
01035 static int data_search_cmp_ptr(const struct ast_data_search *root, const char *name,
01036    void *ptr)
01037 {
01038    struct ast_data_search *child;
01039    enum data_search_comparison cmp_type;
01040    void *node_ptr;
01041 
01042    child = data_search_get_node(root, name);
01043    if (!child) {
01044       return 0;
01045    }
01046 
01047    cmp_type = child->cmp_type;
01048 
01049    if (sscanf(child->value, "%p", &node_ptr) <= 0) {
01050       ao2_ref(child, -1);
01051       return 1;
01052    }
01053 
01054    ao2_ref(child, -1);
01055 
01056    return data_search_comparison_result((node_ptr - ptr), cmp_type);
01057 }
01058 
01059 /*!
01060  * \internal
01061  * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
01062  *        current ipv4 address value.
01063  *        .search = "something=192.168.2.2"
01064  *        name = "something"
01065  *        value is the current value of something and will be evaluated against "192.168.2.2".
01066  * \param[in] root The root node pointer of the search tree.
01067  * \param[in] name The name of the specific.
01068  * \param[in] addr The ipv4 address value to compare.
01069  * \returns The (value - current_value) result.
01070  */
01071 static int data_search_cmp_ipaddr(const struct ast_data_search *root, const char *name,
01072    struct in_addr addr)
01073 {
01074    struct ast_data_search *child;
01075    enum data_search_comparison cmp_type;
01076    struct in_addr node_addr;
01077 
01078    child = data_search_get_node(root, name);
01079    if (!child) {
01080       return 0;
01081    }
01082    cmp_type = child->cmp_type;
01083 
01084    inet_aton(child->value, &node_addr);
01085 
01086    ao2_ref(child, -1);
01087 
01088    return data_search_comparison_result((node_addr.s_addr - addr.s_addr), cmp_type);
01089 }
01090 
01091 /*!
01092  * \internal
01093  * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
01094  *        current boolean value.
01095  *        .search = "something=true"
01096  *        name = "something"
01097  *        value is the current value of something and will be evaluated against "true".
01098  * \param[in] root The root node pointer of the search tree.
01099  * \param[in] name The name of the specific.
01100  * \param[in] value The boolean value to compare.
01101  * \returns The (value - current_value) result.
01102  */
01103 static int data_search_cmp_bool(const struct ast_data_search *root, const char *name,
01104    unsigned int value)
01105 {
01106    struct ast_data_search *child;
01107    unsigned int node_value;
01108    enum data_search_comparison cmp_type;
01109 
01110    child = data_search_get_node(root, name);
01111    if (!child) {
01112       return 0;
01113    }
01114 
01115    node_value = abs(ast_true(child->value));
01116    cmp_type = child->cmp_type;
01117 
01118    ao2_ref(child, -1);
01119 
01120    return data_search_comparison_result(value - node_value, cmp_type);
01121 }
01122 
01123 /*!
01124  * \internal
01125  * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
01126  *        current double value.
01127  *        .search = "something=222"
01128  *        name = "something"
01129  *        value is the current value of something and will be evaluated against "222".
01130  * \param[in] root The root node pointer of the search tree.
01131  * \param[in] name The name of the specific.
01132  * \param[in] value The double value to compare.
01133  * \returns The (value - current_value) result.
01134  */
01135 static int data_search_cmp_dbl(const struct ast_data_search *root, const char *name,
01136    double value)
01137 {
01138    struct ast_data_search *child;
01139    double node_value;
01140    enum data_search_comparison cmp_type;
01141 
01142    child = data_search_get_node(root, name);
01143    if (!child) {
01144       return 0;
01145    }
01146 
01147    node_value = strtod(child->value, NULL);
01148    cmp_type = child->cmp_type;
01149 
01150    ao2_ref(child, -1);
01151 
01152    return data_search_comparison_result(value - node_value, cmp_type);
01153 }
01154 
01155 /*!
01156  * \internal
01157  * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
01158  *        current unsigned integer value.
01159  *        .search = "something=10"
01160  *        name = "something"
01161  *        value is the current value of something and will be evaluated against "10".
01162  * \param[in] root The root node pointer of the search tree.
01163  * \param[in] name The name of the specific.
01164  * \param[in] value The unsigned value to compare.
01165  * \returns The strcmp return value.
01166  */
01167 static int data_search_cmp_uint(const struct ast_data_search *root, const char *name,
01168    unsigned int value)
01169 {
01170    struct ast_data_search *child;
01171    unsigned int node_value;
01172    enum data_search_comparison cmp_type;
01173 
01174    child = data_search_get_node(root, name);
01175    if (!child) {
01176       return 0;
01177    }
01178 
01179    node_value = atoi(child->value);
01180    cmp_type = child->cmp_type;
01181 
01182    ao2_ref(child, -1);
01183 
01184    return data_search_comparison_result(value - node_value, cmp_type);
01185 }
01186 
01187 /*!
01188  * \internal
01189  * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
01190  *        current signed integer value.
01191  *        .search = "something=10"
01192  *        name = "something"
01193  *        value is the current value of something and will be evaluated against "10".
01194  * \param[in] root The root node pointer of the search tree.
01195  * \param[in] name The name of the specific.
01196  * \param[in] value The value to compare.
01197  * \returns The strcmp return value.
01198  */
01199 static int data_search_cmp_int(const struct ast_data_search *root, const char *name,
01200    int value)
01201 {
01202    struct ast_data_search *child;
01203    int node_value;
01204    enum data_search_comparison cmp_type;
01205 
01206    child = data_search_get_node(root, name);
01207    if (!child) {
01208       return 0;
01209    }
01210 
01211    node_value = atoi(child->value);
01212    cmp_type = child->cmp_type;
01213 
01214    ao2_ref(child, -1);
01215 
01216    return data_search_comparison_result(value - node_value, cmp_type);
01217 }
01218 
01219 /*!
01220  * \internal
01221  * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
01222  *        current character value.
01223  *        .search = "something=c"
01224  *        name = "something"
01225  *        value is the current value of something and will be evaluated against "c".
01226  * \param[in] root The root node pointer of the search tree.
01227  * \param[in] name The name of the specific.
01228  * \param[in] value The boolean value to compare.
01229  * \returns The (value - current_value) result.
01230  */
01231 static int data_search_cmp_char(const struct ast_data_search *root, const char *name,
01232    char value)
01233 {
01234    struct ast_data_search *child;
01235    char node_value;
01236    enum data_search_comparison cmp_type;
01237 
01238    child = data_search_get_node(root, name);
01239    if (!child) {
01240       return 0;
01241    }
01242 
01243    node_value = *(child->value);
01244    cmp_type = child->cmp_type;
01245 
01246    ao2_ref(child, -1);
01247 
01248    return data_search_comparison_result(value - node_value, cmp_type);
01249 }
01250 
01251 /*!
01252  * \internal
01253  * \brief Get the member pointer, from a mapping structure, based on its name.
01254  * \XXX We will need to improve performance here!!.
01255  * \retval <0 if the member was not found.
01256  * \retval >=0 The member position in the mapping structure.
01257  */
01258 static inline int data_search_mapping_find(const struct ast_data_mapping_structure *map,
01259    size_t mapping_len,
01260    const char *member_name)
01261 {
01262    int i;
01263 
01264    for (i = 0; i < mapping_len; i++) {
01265       if (!strcmp(map[i].name, member_name)) {
01266          return i;
01267       }
01268    }
01269 
01270    return -1;
01271 }
01272 
01273 int __ast_data_search_cmp_structure(const struct ast_data_search *search,
01274    const struct ast_data_mapping_structure *mapping, size_t mapping_len,
01275    void *structure, const char *structure_name)
01276 {
01277    struct ao2_iterator i;
01278    struct ast_data_search *node, *struct_children;
01279    int member, notmatch = 0;
01280 
01281    if (!search) {
01282       return 0;
01283    }
01284 
01285    struct_children = data_search_get_node(search, structure_name);
01286    if (!struct_children) {
01287       return 0;
01288    }
01289 
01290    i = ao2_iterator_init(struct_children->children, 0);
01291    while ((node = ao2_iterator_next(&i))) {
01292       member = data_search_mapping_find(mapping, mapping_len, node->name);
01293       if (member < 0) {
01294          /* the structure member name doesn't match! */
01295          ao2_ref(node, -1);
01296          ao2_ref(struct_children, -1);
01297          ao2_iterator_destroy(&i);
01298          return 0;
01299       }
01300 
01301       notmatch = 0;
01302       switch (mapping[member].type) {
01303       case AST_DATA_PASSWORD:
01304          notmatch = data_search_cmp_string(struct_children,
01305             node->name,
01306             mapping[member].get.AST_DATA_PASSWORD(structure));
01307          break;
01308       case AST_DATA_TIMESTAMP:
01309          notmatch = data_search_cmp_uint(struct_children,
01310             node->name,
01311             mapping[member].get.AST_DATA_TIMESTAMP(structure));
01312          break;
01313       case AST_DATA_SECONDS:
01314          notmatch = data_search_cmp_uint(struct_children,
01315             node->name,
01316             mapping[member].get.AST_DATA_SECONDS(structure));
01317          break;
01318       case AST_DATA_MILLISECONDS:
01319          notmatch = data_search_cmp_uint(struct_children,
01320             node->name,
01321             mapping[member].get.AST_DATA_MILLISECONDS(structure));
01322          break;
01323       case AST_DATA_STRING:
01324          notmatch = data_search_cmp_string(struct_children,
01325             node->name,
01326             mapping[member].get.AST_DATA_STRING(structure));
01327          break;
01328       case AST_DATA_CHARACTER:
01329          notmatch = data_search_cmp_char(struct_children,
01330             node->name,
01331             mapping[member].get.AST_DATA_CHARACTER(structure));
01332          break;
01333       case AST_DATA_INTEGER:
01334          notmatch = data_search_cmp_int(struct_children,
01335             node->name,
01336             mapping[member].get.AST_DATA_INTEGER(structure));
01337          break;
01338       case AST_DATA_BOOLEAN:
01339          notmatch = data_search_cmp_bool(struct_children,
01340             node->name,
01341             mapping[member].get.AST_DATA_BOOLEAN(structure));
01342          break;
01343       case AST_DATA_UNSIGNED_INTEGER:
01344          notmatch = data_search_cmp_uint(struct_children,
01345             node->name,
01346             mapping[member].get.AST_DATA_UNSIGNED_INTEGER(structure));
01347          break;
01348       case AST_DATA_DOUBLE:
01349          notmatch = data_search_cmp_dbl(struct_children,
01350             node->name,
01351             mapping[member].get.AST_DATA_DOUBLE(structure));
01352          break;
01353       case AST_DATA_IPADDR:
01354          notmatch = data_search_cmp_ipaddr(struct_children,
01355             node->name,
01356             mapping[member].get.AST_DATA_IPADDR(structure));
01357          break;
01358       case AST_DATA_POINTER:
01359          notmatch = data_search_cmp_ptr(struct_children,
01360             node->name,
01361             mapping[member].get.AST_DATA_POINTER(structure));
01362          break;
01363       case AST_DATA_CONTAINER:
01364          break;
01365       }
01366 
01367       ao2_ref(node, -1);
01368    }
01369    ao2_iterator_destroy(&i);
01370 
01371    ao2_ref(struct_children, -1);
01372 
01373    return notmatch;
01374 }
01375 
01376 /*!
01377  * \internal
01378  * \brief Release the memory allocated by a call to ao2_alloc.
01379  */
01380 static void data_result_destructor(void *obj)
01381 {
01382    struct ast_data *root = obj;
01383 
01384    switch (root->type) {
01385    case AST_DATA_PASSWORD:
01386    case AST_DATA_STRING:
01387       ast_free(root->payload.str);
01388       ao2_ref(root->children, -1);
01389       break;
01390    case AST_DATA_POINTER:
01391    case AST_DATA_CHARACTER:
01392    case AST_DATA_CONTAINER:
01393    case AST_DATA_INTEGER:
01394    case AST_DATA_TIMESTAMP:
01395    case AST_DATA_SECONDS:
01396    case AST_DATA_MILLISECONDS:
01397    case AST_DATA_UNSIGNED_INTEGER:
01398    case AST_DATA_DOUBLE:
01399    case AST_DATA_BOOLEAN:
01400    case AST_DATA_IPADDR:
01401       ao2_ref(root->children, -1);
01402       break;
01403    }
01404 }
01405 
01406 static struct ast_data *data_result_create(const char *name)
01407 {
01408    struct ast_data *res;
01409    size_t namelen;
01410 
01411    namelen = ast_strlen_zero(name) ? 1 : strlen(name) + 1;
01412 
01413    res = ao2_alloc(sizeof(*res) + namelen, data_result_destructor);
01414    if (!res) {
01415       return NULL;
01416    }
01417 
01418    strcpy(res->name, namelen ? name : "");
01419 
01420    /* initialize the children container */
01421    res->children = ao2_container_alloc(NUM_DATA_RESULT_BUCKETS, data_result_hash,
01422       data_result_cmp);
01423    if (!res->children) {
01424       ao2_ref(res, -1);
01425       return NULL;
01426    }
01427 
01428    /* set this node as a container. */
01429    res->type = AST_DATA_CONTAINER;
01430 
01431    return res;
01432 }
01433 
01434 /*!
01435  * \internal
01436  * \brief Find a child node, based on its name.
01437  * \param[in] root The starting point.
01438  * \param[in] name The child name.
01439  * \retval NULL if the node wasn't found.
01440  * \retval non-NULL the node we were looking for.
01441  */
01442 static struct ast_data *data_result_find_child(struct ast_data *root, const char *name)
01443 {
01444    struct ast_data *found, *find_node;
01445 
01446    find_node = data_result_create(name);
01447    if (!find_node) {
01448       return NULL;
01449    }
01450 
01451    found = ao2_find(root->children, find_node, OBJ_POINTER);
01452 
01453    /* release the temporary created node used for searching. */
01454    ao2_ref(find_node, -1);
01455 
01456    return found;
01457 }
01458 
01459 int ast_data_search_match(const struct ast_data_search *search, struct ast_data *data)
01460 {
01461    struct ao2_iterator i, ii;
01462    struct ast_data_search *s, *s_child;
01463    struct ast_data *d_child;
01464    int notmatch = 1;
01465 
01466    if (!search) {
01467       return 1;
01468    }
01469 
01470    s_child = data_search_find(search->children, data->name);
01471    if (!s_child) {
01472       /* nothing to compare */
01473       ao2_ref(s_child, -1);
01474       return 1;
01475    }
01476 
01477    i = ao2_iterator_init(s_child->children, 0);
01478    while ((s = ao2_iterator_next(&i))) {
01479       if (!ao2_container_count(s->children)) {
01480          /* compare this search node with every data node */
01481          d_child = data_result_find_child(data, s->name);
01482          if (!d_child) {
01483             ao2_ref(s, -1);
01484             notmatch = 1;
01485             continue;
01486          }
01487 
01488          switch (d_child->type) {
01489          case AST_DATA_PASSWORD:
01490          case AST_DATA_STRING:
01491             notmatch = data_search_cmp_string(s_child, d_child->name,
01492                d_child->payload.str);
01493             break;
01494          case AST_DATA_CHARACTER:
01495             notmatch = data_search_cmp_char(s_child, d_child->name,
01496                d_child->payload.character);
01497             break;
01498          case AST_DATA_INTEGER:
01499             notmatch = data_search_cmp_int(s_child, d_child->name,
01500                d_child->payload.sint);
01501             break;
01502          case AST_DATA_BOOLEAN:
01503             notmatch = data_search_cmp_bool(s_child, d_child->name,
01504                d_child->payload.boolean);
01505             break;
01506          case AST_DATA_UNSIGNED_INTEGER:
01507             notmatch = data_search_cmp_uint(s_child, d_child->name,
01508                d_child->payload.uint);
01509             break;
01510          case AST_DATA_TIMESTAMP:
01511          case AST_DATA_SECONDS:
01512          case AST_DATA_MILLISECONDS:
01513          case AST_DATA_DOUBLE:
01514             notmatch = data_search_cmp_uint(s_child, d_child->name,
01515                d_child->payload.dbl);
01516             break;
01517          case AST_DATA_IPADDR:
01518             notmatch = data_search_cmp_ipaddr(s_child, d_child->name,
01519                d_child->payload.ipaddr);
01520             break;
01521          case AST_DATA_POINTER:
01522             notmatch = data_search_cmp_ptr(s_child, d_child->name,
01523                d_child->payload.ptr);
01524             break;
01525          case AST_DATA_CONTAINER:
01526             break;
01527          }
01528          ao2_ref(d_child, -1);
01529       } else {
01530          ii = ao2_iterator_init(data->children, 0);
01531          while ((d_child = ao2_iterator_next(&ii))) {
01532             if (strcmp(d_child->name, s->name)) {
01533                ao2_ref(d_child, -1);
01534                continue;
01535             }
01536             if (!(notmatch = !ast_data_search_match(s_child, d_child))) {
01537                /* do not continue if we have a match. */
01538                ao2_ref(d_child, -1);
01539                break;
01540             }
01541             ao2_ref(d_child, -1);
01542          }
01543          ao2_iterator_destroy(&ii);
01544       }
01545       ao2_ref(s, -1);
01546       if (notmatch) {
01547          /* do not continue if we don't have a match. */
01548          break;
01549       }
01550    }
01551    ao2_iterator_destroy(&i);
01552 
01553    ao2_ref(s_child, -1);
01554 
01555    return !notmatch;
01556 }
01557 
01558 /*!
01559  * \internal
01560  * \brief Get an internal node, from the result set.
01561  * \param[in] node A node container.
01562  * \param[in] path The path to the needed internal node.
01563  * \retval NULL if the internal node is not found.
01564  * \retval non-NULL the internal node with path 'path'.
01565  */
01566 static struct ast_data *data_result_get_node(struct ast_data *node,
01567    const char *path)
01568 {
01569    char *savepath, *node_name;
01570    struct ast_data *child, *current = node;
01571 
01572    savepath = ast_strdupa(path);
01573    node_name = next_node_name(&savepath);
01574 
01575    while (node_name) {
01576       child = data_result_find_child(current, node_name);
01577       if (current != node) {
01578          ao2_ref(current, -1);
01579       }
01580       if (!child) {
01581          return NULL;
01582       }
01583       current = child;
01584       node_name = next_node_name(&savepath);
01585    }
01586 
01587    /* do not increment the refcount of the returned object. */
01588    if (current != node) {
01589       ao2_ref(current, -1);
01590    }
01591 
01592    return current;
01593 }
01594 
01595 /*!
01596  * \internal
01597  * \brief Add a child to the specified root node.
01598  * \param[in] root The root node pointer.
01599  * \param[in] child The child to add to the root node.
01600  */
01601 static void data_result_add_child(struct ast_data *root, struct ast_data *child)
01602 {
01603    ao2_link(root->children, child);
01604 }
01605 
01606 /*!
01607  * \internal
01608  * \brief Common string hash function for data nodes
01609  */
01610 static int data_filter_hash(const void *obj, const int flags)
01611 {
01612    const struct data_filter *node = obj;
01613    return ast_str_hash(node->name);
01614 }
01615 
01616 /*!
01617  * \internal
01618  * \brief Common string comparison function
01619  */
01620 static int data_filter_cmp(void *obj, void *arg, int flags)
01621 {
01622    struct data_filter *node1 = obj, *node2 = arg;
01623    return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
01624 }
01625 
01626 /*!
01627  * \internal
01628  * \brief Destroy a data filter tree.
01629  * \param[in] obj Data filter list to be destroyed.
01630  */
01631 static void data_filter_destructor(void *obj)
01632 {
01633    struct data_filter *filter = obj, *globres;
01634 
01635    while ((globres = AST_LIST_REMOVE_HEAD(&(filter->glob_list), list))) {
01636       ao2_ref(globres, -1);
01637    }
01638 
01639    ao2_ref(filter->children, -1);
01640 }
01641 
01642 /*!
01643  * \internal
01644  * \brief Allocate a filter node.
01645  * \retval NULL on error.
01646  * \retval non-NULL The allocated search node structure.
01647  */
01648 static struct data_filter *data_filter_alloc(const char *name)
01649 {
01650    char *globname, *token;
01651    struct data_filter *res, *globfilter;
01652    size_t name_len = strlen(name) + 1;
01653 
01654    res = ao2_alloc(sizeof(*res) + name_len, data_filter_destructor);
01655    if (!res) {
01656       return NULL;
01657    }
01658 
01659    res->children = ao2_container_alloc(NUM_DATA_FILTER_BUCKETS, data_filter_hash,
01660       data_filter_cmp);
01661 
01662    if (!res->children) {
01663       ao2_ref(res, -1);
01664       return NULL;
01665    }
01666 
01667    strcpy(res->name, name);
01668 
01669    if (strchr(res->name, '*')) {
01670       globname = ast_strdupa(res->name);
01671 
01672       while ((token = strsep(&globname, "*"))) {
01673          globfilter = data_filter_alloc(token);
01674          AST_LIST_INSERT_TAIL(&(res->glob_list), globfilter, list);
01675       }
01676    }
01677 
01678    return res;
01679 }
01680 
01681 /*!
01682  * \internal
01683  * \brief Release a filter tree.
01684  * \param[in] filter The filter tree root node.
01685  */
01686 static void data_filter_release(struct data_filter *filter)
01687 {
01688    ao2_ref(filter, -1);
01689 }
01690 
01691 /*!
01692  * \internal
01693  * \brief Find a child node, based on his name.
01694  * \param[in] parent Where to find the node.
01695  * \param[in] name The node name to find.
01696  * \retval NULL if a node wasn't found.
01697  * \retval The node found.
01698  * \note Remember to decrement the ref count of the returned node after using it.
01699  */
01700 static struct data_filter *data_filter_find(struct ao2_container *parent,
01701    const char *name)
01702 {
01703    int i, olend, orend, globfound;
01704    size_t name_len = strlen(name), glob_len;
01705    struct ao2_iterator iter;
01706    struct data_filter *find_node, *found, *globres;
01707 
01708    find_node = data_filter_alloc(name);
01709    if (!find_node) {
01710       return NULL;
01711    }
01712 
01713    found = ao2_find(parent, find_node, OBJ_POINTER);
01714 
01715    /* free the created node used for searching. */
01716    ao2_ref(find_node, -1);
01717 
01718    if (found) {
01719       return found;
01720    }
01721 
01722    iter = ao2_iterator_init(parent, 0);
01723    while ((found = ao2_iterator_next(&iter))) {
01724       if (!AST_LIST_EMPTY(&(found->glob_list))) {
01725          i = 0;
01726          globfound = 1;
01727 
01728          olend = ast_strlen_zero(AST_LIST_FIRST(&(found->glob_list))->name);
01729          orend = ast_strlen_zero(AST_LIST_LAST(&(found->glob_list))->name);
01730 
01731          AST_LIST_TRAVERSE(&(found->glob_list), globres, list) {
01732             if (!*globres->name) {
01733                continue;
01734             }
01735 
01736             glob_len = strlen(globres->name);
01737 
01738             if (!i && !olend) {
01739                if (strncasecmp(name, globres->name, glob_len)) {
01740                   globfound = 0;
01741                   break;
01742                }
01743 
01744                i += glob_len;
01745                continue;
01746             }
01747 
01748             for (globfound = 0; name_len - i >= glob_len; ++i) {
01749                if (!strncasecmp(name + i, globres->name, glob_len)) {
01750                   globfound = 1;
01751                   i += glob_len;
01752                   break;
01753                }
01754             }
01755 
01756             if (!globfound) {
01757                break;
01758             }
01759          }
01760 
01761          if (globfound && (i == name_len || orend)) {
01762             ao2_iterator_destroy(&iter);
01763             return found;
01764          }
01765       }
01766 
01767       ao2_ref(found, -1);
01768    }
01769    ao2_iterator_destroy(&iter);
01770 
01771    return NULL;
01772 }
01773 
01774 /*!
01775  * \internal
01776  * \brief Add a child to the specified node.
01777  * \param[in] root The root node where to add the child.
01778  * \param[in] name The name of the node to add.
01779  * \note Remember to decrement the ref count after using the returned node.
01780  */
01781 static struct data_filter *data_filter_add_child(struct ao2_container *root,
01782    char *name)
01783 {
01784    struct data_filter *node;
01785 
01786    node = data_filter_find(root, name);
01787    if (node) {
01788       return node;
01789    }
01790 
01791    node = data_filter_alloc(name);
01792    if (!node) {
01793       return NULL;
01794    }
01795 
01796    ao2_link(root, node);
01797 
01798    return node;
01799 }
01800 
01801 /*!
01802  * \internal
01803  * \brief Add a node to a filter list from a path
01804  * \param[in] Filter list to add the path onto.
01805  * \param[in] The path to add into the filter list.
01806  * \retval NULL on error.
01807  * \retval non-NULL A tree with the wanted nodes.
01808  */
01809 static int data_filter_add_nodes(struct ao2_container *root, char *path)
01810 {
01811    struct data_filter *node;
01812    char *savepath, *saveptr, *token, *node_name;
01813    int ret = 0;
01814 
01815    if (!path) {
01816       return 0;
01817    }
01818 
01819    savepath = ast_strdupa(path);
01820 
01821    node_name = next_node_name(&savepath);
01822 
01823    if (!node_name) {
01824       return 0;
01825    }
01826 
01827    for (token = strtok_r(node_name, "|", &saveptr);
01828          token; token = strtok_r(NULL, "|", &saveptr)) {
01829       node = data_filter_add_child(root, token);
01830       if (!node) {
01831          continue;
01832       }
01833       data_filter_add_nodes(node->children, savepath);
01834       ret = 1;
01835       ao2_ref(node, -1);
01836    }
01837 
01838    return ret;
01839 }
01840 
01841 /*!
01842  * \internal
01843  * \brief Generate a filter list based on a filter string provided by the API user.
01844  * \param[in] A filter string to create a filter from.
01845  */
01846 static struct data_filter *data_filter_generate(const char *constfilter)
01847 {
01848    struct data_filter *filter = NULL;
01849    char *strfilter, *token, *saveptr;
01850    int node_added = 0;
01851 
01852    if (!constfilter) {
01853       return NULL;
01854    }
01855 
01856    strfilter = ast_strdupa(constfilter);
01857 
01858    filter = data_filter_alloc("/");
01859    if (!filter) {
01860       return NULL;
01861    }
01862 
01863    for (token = strtok_r(strfilter, ",", &saveptr); token;
01864          token = strtok_r(NULL, ",", &saveptr)) {
01865       node_added = data_filter_add_nodes(filter->children, token);
01866    }
01867 
01868    if (!node_added) {
01869       ao2_ref(filter, -1);
01870       return NULL;
01871    }
01872 
01873    return filter;
01874 }
01875 
01876 /*!
01877  * \internal
01878  * \brief Generate all the tree from a specified provider.
01879  * \param[in] query The query executed.
01880  * \param[in] root_provider The provider specified in the path of the query.
01881  * \param[in] parent_node_name The root node name.
01882  * \retval NULL on error.
01883  * \retval non-NULL The generated result tree.
01884  */
01885 static struct ast_data *data_result_generate_node(const struct ast_data_query *query,
01886    const struct data_provider *root_provider,
01887    const char *parent_node_name,
01888    const struct ast_data_search *search,
01889    const struct data_filter *filter)
01890 {
01891    struct ast_data *generated, *node;
01892    struct ao2_iterator i;
01893    struct data_provider *provider;
01894    struct ast_data_search *search_child = NULL;
01895    struct data_filter *filter_child;
01896 
01897    node = data_result_create(parent_node_name);
01898    if (!node) {
01899       ast_log(LOG_ERROR, "Unable to allocate '%s' node\n", parent_node_name);
01900       return NULL;
01901    }
01902 
01903    if (root_provider->module) {
01904       ast_module_ref(root_provider->module);
01905    }
01906 
01907    /* if this is a terminal node, just run the callback function. */
01908    if (root_provider->handler && root_provider->handler->get) {
01909       node->filter = filter;
01910       root_provider->handler->get(search, node);
01911       if (root_provider->module) {
01912          ast_module_unref(root_provider->module);
01913       }
01914       return node;
01915    }
01916 
01917    if (root_provider->module) {
01918       ast_module_unref(root_provider->module);
01919    }
01920 
01921    /* if this is not a terminal node, generate every child node. */
01922    i = ao2_iterator_init(root_provider->children, 0);
01923    while ((provider = ao2_iterator_next(&i))) {
01924       filter_child = NULL;
01925       generated = NULL;
01926 
01927       /* get the internal search node. */
01928       if (search) {
01929          search_child = data_search_find(search->children, provider->name);
01930       }
01931       /* get the internal filter node. */
01932       if (filter) {
01933          filter_child = data_filter_find(filter->children, provider->name);
01934       }
01935 
01936       if (!filter || filter_child) {
01937          /* only generate the internal node, if we have something to
01938           * generate based on the filtering string. */
01939          generated = data_result_generate_node(query, provider,
01940             provider->name,
01941             search_child, filter_child);
01942       }
01943 
01944       /* decrement the refcount of the internal search node. */
01945       if (search_child) {
01946          ao2_ref(search_child, -1);
01947       }
01948 
01949       /* decrement the refcount of the internal filter node. */
01950       if (filter_child) {
01951          ao2_ref(filter_child, -1);
01952       }
01953 
01954       if (generated) {
01955          data_result_add_child(node, generated);
01956          ao2_ref(generated, -1);
01957       }
01958 
01959       ao2_ref(provider, -1);
01960    }
01961    ao2_iterator_destroy(&i);
01962 
01963    return node;
01964 }
01965 
01966 /*!
01967  * \internal
01968  * \brief Generate a result tree based on a query.
01969  * \param[in] query The complete query structure.
01970  * \param[in] search_path The path to retrieve.
01971  * \retval NULL on error.
01972  * \retval non-NULL The generated data result.
01973  */
01974 static struct ast_data *data_result_generate(const struct ast_data_query *query,
01975    const char *search_path)
01976 {
01977    char *node_name, *tmp_path;
01978    struct data_provider *provider_child, *tmp_provider_child;
01979    struct ast_data *result, *result_filtered;
01980    struct ast_data_search *search = NULL, *search_child = NULL;
01981    struct data_filter *filter = NULL, *filter_child = NULL;
01982 
01983    if (!search_path) {
01984       /* generate all the trees?. */
01985       return NULL;
01986    }
01987 
01988    tmp_path = ast_strdupa(search_path);
01989 
01990    /* start searching the root node name */
01991    node_name = next_node_name(&tmp_path);
01992    if (!node_name) {
01993       return NULL;
01994    }
01995    provider_child = data_provider_find(root_data.container, node_name, NULL);
01996 
01997    /* continue with the rest of the path. */
01998    while (provider_child) {
01999       node_name = next_node_name(&tmp_path);
02000       if (!node_name) {
02001          break;
02002       }
02003 
02004       tmp_provider_child = data_provider_find(provider_child->children,
02005             node_name, NULL);
02006 
02007       /* release the reference from this child */
02008       ao2_ref(provider_child, -1);
02009 
02010       provider_child = tmp_provider_child;
02011    }
02012 
02013    if (!provider_child) {
02014       ast_log(LOG_ERROR, "Invalid path '%s', '%s' not found.\n",
02015             tmp_path, node_name);
02016       return NULL;
02017    }
02018 
02019    /* generate the search tree. */
02020    if (query->search) {
02021       search = data_search_generate(query->search);
02022       if (search) {
02023          search_child = data_search_find(search->children,
02024             provider_child->name);
02025       }
02026    }
02027 
02028    /* generate the filter tree. */
02029    if (query->filter) {
02030       filter = data_filter_generate(query->filter);
02031       if (filter) {
02032          filter_child = data_filter_find(filter->children,
02033             provider_child->name);
02034       }
02035    }
02036 
02037    result = data_result_generate_node(query, provider_child, provider_child->name,
02038          search_child, filter_child);
02039 
02040    /* release the requested provider. */
02041    ao2_ref(provider_child, -1);
02042 
02043    /* release the generated search tree. */
02044    if (search_child) {
02045       ao2_ref(search_child, -1);
02046    }
02047 
02048    if (filter_child) {
02049       ao2_ref(filter_child, -1);
02050    }
02051 
02052    if (search) {
02053       data_search_release(search);
02054    }
02055 
02056    result_filtered = result;
02057 
02058    /* release the generated filter tree. */
02059    if (filter) {
02060       data_filter_release(filter);
02061    }
02062 
02063    return result_filtered;
02064 }
02065 
02066 struct ast_data *ast_data_get(const struct ast_data_query *query)
02067 {
02068    struct ast_data *res;
02069 
02070    /* check compatibility */
02071    if (!data_structure_compatible(query->version, latest_query_compatible_version,
02072       current_query_version)) {
02073       return NULL;
02074    }
02075 
02076    data_read_lock();
02077    res = data_result_generate(query, query->path);
02078    data_unlock();
02079 
02080    if (!res) {
02081       ast_log(LOG_ERROR, "Unable to get data from %s\n", query->path);
02082       return NULL;
02083    }
02084 
02085    return res;
02086 }
02087 
02088 #ifdef HAVE_LIBXML2
02089 /*!
02090  * \internal
02091  * \brief Helper function to move an ast_data tree to xml.
02092  * \param[in] parent_data The initial ast_data node to be passed to xml.
02093  * \param[out] parent_xml The root node to insert the xml.
02094  */
02095 static void data_get_xml_add_child(struct ast_data *parent_data,
02096    struct ast_xml_node *parent_xml)
02097 {
02098    struct ao2_iterator i;
02099    struct ast_data *node;
02100    struct ast_xml_node *child_xml;
02101    char node_content[256];
02102 
02103    i = ao2_iterator_init(parent_data->children, 0);
02104    while ((node = ao2_iterator_next(&i))) {
02105       child_xml = ast_xml_new_node(node->name);
02106       if (!child_xml) {
02107          ao2_ref(node, -1);
02108          continue;
02109       }
02110 
02111       switch (node->type) {
02112       case AST_DATA_CONTAINER:
02113          data_get_xml_add_child(node, child_xml);
02114          break;
02115       case AST_DATA_PASSWORD:
02116          ast_xml_set_text(child_xml, node->payload.str);
02117          break;
02118       case AST_DATA_TIMESTAMP:
02119          snprintf(node_content, sizeof(node_content), "%u",
02120             node->payload.uint);
02121          ast_xml_set_text(child_xml, node_content);
02122          break;
02123       case AST_DATA_SECONDS:
02124          snprintf(node_content, sizeof(node_content), "%u",
02125             node->payload.uint);
02126          ast_xml_set_text(child_xml, node_content);
02127          break;
02128       case AST_DATA_MILLISECONDS:
02129          snprintf(node_content, sizeof(node_content), "%u",
02130             node->payload.uint);
02131          ast_xml_set_text(child_xml, node_content);
02132          break;
02133       case AST_DATA_STRING:
02134          ast_xml_set_text(child_xml, node->payload.str);
02135          break;
02136       case AST_DATA_CHARACTER:
02137          snprintf(node_content, sizeof(node_content), "%c",
02138             node->payload.character);
02139          ast_xml_set_text(child_xml, node_content);
02140          break;
02141       case AST_DATA_INTEGER:
02142          snprintf(node_content, sizeof(node_content), "%d",
02143             node->payload.sint);
02144          ast_xml_set_text(child_xml, node_content);
02145          break;
02146       case AST_DATA_UNSIGNED_INTEGER:
02147          snprintf(node_content, sizeof(node_content), "%u",
02148             node->payload.uint);
02149          ast_xml_set_text(child_xml, node_content);
02150          break;
02151       case AST_DATA_DOUBLE:
02152          snprintf(node_content, sizeof(node_content), "%f",
02153             node->payload.dbl);
02154          ast_xml_set_text(child_xml, node_content);
02155          break;
02156       case AST_DATA_BOOLEAN:
02157          if (node->payload.boolean) {
02158             ast_xml_set_text(child_xml, "true");
02159          } else {
02160             ast_xml_set_text(child_xml, "false");
02161          }
02162          break;
02163       case AST_DATA_POINTER:
02164          snprintf(node_content, sizeof(node_content), "%p",
02165             node->payload.ptr);
02166          ast_xml_set_text(child_xml, node_content);
02167          break;
02168       case AST_DATA_IPADDR:
02169          snprintf(node_content, sizeof(node_content), "%s",
02170             ast_inet_ntoa(node->payload.ipaddr));
02171          ast_xml_set_text(child_xml, node_content);
02172          break;
02173       }
02174       ast_xml_add_child(parent_xml, child_xml);
02175 
02176       ao2_ref(node, -1);
02177    }
02178    ao2_iterator_destroy(&i);
02179 
02180 }
02181 
02182 struct ast_xml_doc *ast_data_get_xml(const struct ast_data_query *query)
02183 {
02184    struct ast_xml_doc *doc;
02185    struct ast_xml_node *root;
02186    struct ast_data *res;
02187 
02188    res = ast_data_get(query);
02189    if (!res) {
02190       return NULL;
02191    }
02192 
02193    doc = ast_xml_new();
02194    if (!doc) {
02195       ast_data_free(res);
02196       return NULL;
02197    }
02198 
02199    root = ast_xml_new_node(res->name);
02200    if (!root) {
02201       ast_xml_close(doc);
02202    }
02203 
02204    ast_xml_set_root(doc, root);
02205 
02206    data_get_xml_add_child(res, root);
02207 
02208    ast_data_free(res);
02209 
02210    return doc;
02211 }
02212 #endif
02213 
02214 enum ast_data_type ast_data_retrieve_type(struct ast_data *node, const char *path)
02215 {
02216    struct ast_data *internal;
02217 
02218    internal = data_result_get_node(node, path);
02219    if (!internal) {
02220       return -1;
02221    }
02222 
02223    return internal->type;
02224 }
02225 
02226 char *ast_data_retrieve_name(struct ast_data *node)
02227 {
02228    return node->name;
02229 }
02230 
02231 /*!
02232  * \internal
02233  * \brief Insert a child node inside a passed parent node.
02234  * \param root Where we are going to insert the child node.
02235  * \param name The name of the child node to add.
02236  * \param type The type of content inside the child node.
02237  * \param ptr The actual content of the child node.
02238  * \retval NULL on error.
02239  * \retval non-NULL The added child node pointer.
02240  */
02241 static struct ast_data *__ast_data_add(struct ast_data *root, const char *name,
02242    enum ast_data_type type, void *ptr)
02243 {
02244    struct ast_data *node;
02245    struct data_filter *filter, *filter_child = NULL;
02246 
02247    if (!root || !root->children) {
02248       /* invalid data result node. */
02249       return NULL;
02250    }
02251 
02252    /* check if we need to add this node, based on the filter. */
02253    if (root->filter) {
02254       filter = data_filter_find(root->filter->children, name);
02255       if (!filter) {
02256          return NULL;
02257       }
02258       ao2_ref(filter, -1);
02259    }
02260 
02261    node = data_result_create(name);
02262    if (!node) {
02263       return NULL;
02264    }
02265 
02266    node->type = type;
02267 
02268    switch (type) {
02269    case AST_DATA_BOOLEAN:
02270       node->payload.boolean = *(unsigned int *) ptr;
02271       break;
02272    case AST_DATA_INTEGER:
02273       node->payload.sint = *(int *) ptr;
02274       break;
02275    case AST_DATA_TIMESTAMP:
02276    case AST_DATA_SECONDS:
02277    case AST_DATA_MILLISECONDS:
02278    case AST_DATA_UNSIGNED_INTEGER:
02279       node->payload.uint = *(unsigned int *) ptr;
02280       break;
02281    case AST_DATA_DOUBLE:
02282       node->payload.dbl = *(double *) ptr;
02283       break;
02284    case AST_DATA_PASSWORD:
02285    case AST_DATA_STRING:
02286       node->payload.str = (char *) ptr;
02287       break;
02288    case AST_DATA_CHARACTER:
02289       node->payload.character = *(char *) ptr;
02290       break;
02291    case AST_DATA_POINTER:
02292       node->payload.ptr = ptr;
02293       break;
02294    case AST_DATA_IPADDR:
02295       node->payload.ipaddr = *(struct in_addr *) ptr;
02296       break;
02297    case AST_DATA_CONTAINER:
02298       if (root->filter) {
02299          filter_child = data_filter_find(root->filter->children, name);
02300          if (filter_child) {
02301             /* do not increment the refcount because it is not neccesary. */
02302             ao2_ref(filter_child, -1);
02303          }
02304       }
02305       node->filter = filter_child;
02306       break;
02307    default:
02308       break;
02309    }
02310 
02311    data_result_add_child(root, node);
02312 
02313    ao2_ref(node, -1);
02314 
02315    return node;
02316 }
02317 
02318 struct ast_data *ast_data_add_node(struct ast_data *root, const char *name)
02319 {
02320    return __ast_data_add(root, name, AST_DATA_CONTAINER, NULL);
02321 }
02322 
02323 struct ast_data *ast_data_add_int(struct ast_data *root, const char *name, int value)
02324 {
02325    return __ast_data_add(root, name, AST_DATA_INTEGER, &value);
02326 }
02327 
02328 struct ast_data *ast_data_add_char(struct ast_data *root, const char *name, char value)
02329 {
02330    return __ast_data_add(root, name, AST_DATA_CHARACTER, &value);
02331 }
02332 
02333 struct ast_data *ast_data_add_uint(struct ast_data *root, const char *name,
02334    unsigned int value)
02335 {
02336    return __ast_data_add(root, name, AST_DATA_UNSIGNED_INTEGER, &value);
02337 }
02338 
02339 struct ast_data *ast_data_add_dbl(struct ast_data *root, const char *childname,
02340    double dbl)
02341 {
02342    return __ast_data_add(root, childname, AST_DATA_DOUBLE, &dbl);
02343 }
02344 
02345 struct ast_data *ast_data_add_bool(struct ast_data *root, const char *childname,
02346    unsigned int boolean)
02347 {
02348    return __ast_data_add(root, childname, AST_DATA_BOOLEAN, &boolean);
02349 }
02350 
02351 struct ast_data *ast_data_add_ipaddr(struct ast_data *root, const char *childname,
02352    struct in_addr addr)
02353 {
02354    return __ast_data_add(root, childname, AST_DATA_IPADDR, &addr);
02355 }
02356 
02357 struct ast_data *ast_data_add_ptr(struct ast_data *root, const char *childname,
02358    void *ptr)
02359 {
02360    return __ast_data_add(root, childname, AST_DATA_POINTER, ptr);
02361 }
02362 
02363 struct ast_data *ast_data_add_timestamp(struct ast_data *root, const char *childname,
02364    unsigned int timestamp)
02365 {
02366    return __ast_data_add(root, childname, AST_DATA_TIMESTAMP, &timestamp);
02367 }
02368 
02369 struct ast_data *ast_data_add_seconds(struct ast_data *root, const char *childname,
02370    unsigned int seconds)
02371 {
02372    return __ast_data_add(root, childname, AST_DATA_SECONDS, &seconds);
02373 }
02374 
02375 struct ast_data *ast_data_add_milliseconds(struct ast_data *root, const char *childname,
02376    unsigned int milliseconds)
02377 {
02378    return __ast_data_add(root, childname, AST_DATA_MILLISECONDS, &milliseconds);
02379 }
02380 
02381 struct ast_data *ast_data_add_password(struct ast_data *root, const char *childname,
02382    const char *value)
02383 {
02384    char *name;
02385    size_t namelen = 1 + (ast_strlen_zero(value) ? 0 : strlen(value));
02386    struct ast_data *res;
02387 
02388    if (!(name = ast_malloc(namelen))) {
02389       return NULL;
02390    }
02391 
02392    strcpy(name, (ast_strlen_zero(value) ? "" : value));
02393 
02394    res = __ast_data_add(root, childname, AST_DATA_PASSWORD, name);
02395    if (!res) {
02396       ast_free(name);
02397    }
02398 
02399    return res;
02400 }
02401 
02402 struct ast_data *ast_data_add_str(struct ast_data *root, const char *childname,
02403    const char *value)
02404 {
02405    char *name;
02406    size_t namelen = 1 + (ast_strlen_zero(value) ? 0 : strlen(value));
02407    struct ast_data *res;
02408 
02409    if (!(name = ast_malloc(namelen))) {
02410       return NULL;
02411    }
02412 
02413    strcpy(name, (ast_strlen_zero(value) ? "" : value));
02414 
02415    res = __ast_data_add(root, childname, AST_DATA_STRING, name);
02416    if (!res) {
02417       ast_free(name);
02418    }
02419 
02420    return res;
02421 }
02422 
02423 int __ast_data_add_structure(struct ast_data *root,
02424    const struct ast_data_mapping_structure *mapping, size_t mapping_len,
02425    void *structure)
02426 {
02427    int i;
02428 
02429    for (i = 0; i < mapping_len; i++) {
02430       switch (mapping[i].type) {
02431       case AST_DATA_INTEGER:
02432          ast_data_add_int(root, mapping[i].name,
02433             mapping[i].get.AST_DATA_INTEGER(structure));
02434          break;
02435       case AST_DATA_UNSIGNED_INTEGER:
02436          ast_data_add_uint(root, mapping[i].name,
02437             mapping[i].get.AST_DATA_UNSIGNED_INTEGER(structure));
02438          break;
02439       case AST_DATA_DOUBLE:
02440          ast_data_add_dbl(root, mapping[i].name,
02441             mapping[i].get.AST_DATA_DOUBLE(structure));
02442          break;
02443       case AST_DATA_BOOLEAN:
02444          ast_data_add_bool(root, mapping[i].name,
02445             mapping[i].get.AST_DATA_BOOLEAN(structure));
02446          break;
02447       case AST_DATA_PASSWORD:
02448          ast_data_add_password(root, mapping[i].name,
02449             mapping[i].get.AST_DATA_PASSWORD(structure));
02450          break;
02451       case AST_DATA_TIMESTAMP:
02452          ast_data_add_timestamp(root, mapping[i].name,
02453             mapping[i].get.AST_DATA_TIMESTAMP(structure));
02454          break;
02455       case AST_DATA_SECONDS:
02456          ast_data_add_seconds(root, mapping[i].name,
02457             mapping[i].get.AST_DATA_SECONDS(structure));
02458          break;
02459       case AST_DATA_MILLISECONDS:
02460          ast_data_add_milliseconds(root, mapping[i].name,
02461             mapping[i].get.AST_DATA_MILLISECONDS(structure));
02462          break;
02463       case AST_DATA_STRING:
02464          ast_data_add_str(root, mapping[i].name,
02465             mapping[i].get.AST_DATA_STRING(structure));
02466          break;
02467       case AST_DATA_CHARACTER:
02468          ast_data_add_char(root, mapping[i].name,
02469             mapping[i].get.AST_DATA_CHARACTER(structure));
02470          break;
02471       case AST_DATA_CONTAINER:
02472          break;
02473       case AST_DATA_IPADDR:
02474          ast_data_add_ipaddr(root, mapping[i].name,
02475             mapping[i].get.AST_DATA_IPADDR(structure));
02476          break;
02477       case AST_DATA_POINTER:
02478          ast_data_add_ptr(root, mapping[i].name,
02479             mapping[i].get.AST_DATA_POINTER(structure));
02480          break;
02481       }
02482    }
02483 
02484    return 0;
02485 }
02486 
02487 void ast_data_remove_node(struct ast_data *root, struct ast_data *child)
02488 {
02489    ao2_unlink(root->children, child);
02490 }
02491 
02492 void ast_data_free(struct ast_data *root)
02493 {
02494    /* destroy it, this will destroy all the internal nodes. */
02495    ao2_ref(root, -1);
02496 }
02497 
02498 struct ast_data_iterator *ast_data_iterator_init(struct ast_data *tree,
02499    const char *elements)
02500 {
02501    struct ast_data_iterator *iterator;
02502    struct ao2_iterator i;
02503    struct ast_data *internal = tree;
02504    char *path, *ptr = NULL;
02505 
02506    if (!elements) {
02507       return NULL;
02508    }
02509 
02510    /* tree is the node we want to use to iterate? or we are going
02511     * to iterate thow an internal node? */
02512    path = ast_strdupa(elements);
02513 
02514    ptr = strrchr(path, '/');
02515    if (ptr) {
02516       *ptr = '\0';
02517       internal = data_result_get_node(tree, path);
02518       if (!internal) {
02519          return NULL;
02520       }
02521    }
02522 
02523    iterator = ast_calloc(1, sizeof(*iterator));
02524    if (!iterator) {
02525       return NULL;
02526    }
02527 
02528    i = ao2_iterator_init(internal->children, 0);
02529 
02530    iterator->pattern = (ptr ? strrchr(elements, '/') + 1 : elements);
02531 
02532    /* is the last node a regular expression?, compile it! */
02533    if (!regcomp(&(iterator->regex_pattern), iterator->pattern,
02534          REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
02535       iterator->is_pattern = 1;
02536    }
02537 
02538    iterator->internal_iterator = i;
02539 
02540    return iterator;
02541 }
02542 
02543 void ast_data_iterator_end(struct ast_data_iterator *iterator)
02544 {
02545    /* decrement the reference counter. */
02546    if (iterator->last) {
02547       ao2_ref(iterator->last, -1);
02548    }
02549 
02550    /* release the generated pattern. */
02551    if (iterator->is_pattern) {
02552       regfree(&(iterator->regex_pattern));
02553    }
02554 
02555    ao2_iterator_destroy(&(iterator->internal_iterator));
02556 
02557    ast_free(iterator);
02558    iterator = NULL;
02559 }
02560 
02561 struct ast_data *ast_data_iterator_next(struct ast_data_iterator *iterator)
02562 {
02563    struct ast_data *res;
02564 
02565    if (iterator->last) {
02566       /* release the last retrieved node reference. */
02567       ao2_ref(iterator->last, -1);
02568    }
02569 
02570    while ((res = ao2_iterator_next(&iterator->internal_iterator))) {
02571       /* if there is no node name pattern specified, return
02572        * the next node. */
02573       if (!iterator->pattern) {
02574          break;
02575       }
02576 
02577       /* if the pattern is a regular expression, check if this node
02578        * matches. */
02579       if (iterator->is_pattern && !regexec(&(iterator->regex_pattern),
02580          res->name, 0, NULL, 0)) {
02581          break;
02582       }
02583 
02584       /* if there is a pattern specified, check if this node matches
02585        * the wanted node names. */
02586       if (!iterator->is_pattern && (iterator->pattern &&
02587             !strcasecmp(res->name, iterator->pattern))) {
02588          break;
02589       }
02590 
02591       ao2_ref(res, -1);
02592    }
02593 
02594    iterator->last = res;
02595 
02596    return res;
02597 }
02598 
02599 int ast_data_retrieve(struct ast_data *tree, const char *path,
02600    struct ast_data_retrieve *content)
02601 {
02602    struct ast_data *node;
02603 
02604    if (!content) {
02605       return -1;
02606    }
02607 
02608    node = data_result_get_node(tree, path);
02609    if (!node) {
02610       ast_log(LOG_ERROR, "Invalid internal node %s\n", path);
02611       return -1;
02612    }
02613 
02614    content->type = node->type;
02615    switch (node->type) {
02616    case AST_DATA_STRING:
02617       content->value.AST_DATA_STRING = node->payload.str;
02618       break;
02619    case AST_DATA_PASSWORD:
02620       content->value.AST_DATA_PASSWORD = node->payload.str;
02621       break;
02622    case AST_DATA_TIMESTAMP:
02623       content->value.AST_DATA_TIMESTAMP = node->payload.uint;
02624       break;
02625    case AST_DATA_SECONDS:
02626       content->value.AST_DATA_SECONDS = node->payload.uint;
02627       break;
02628    case AST_DATA_MILLISECONDS:
02629       content->value.AST_DATA_MILLISECONDS = node->payload.uint;
02630       break;
02631    case AST_DATA_CHARACTER:
02632       content->value.AST_DATA_CHARACTER = node->payload.character;
02633       break;
02634    case AST_DATA_INTEGER:
02635       content->value.AST_DATA_INTEGER = node->payload.sint;
02636       break;
02637    case AST_DATA_UNSIGNED_INTEGER:
02638       content->value.AST_DATA_UNSIGNED_INTEGER = node->payload.uint;
02639       break;
02640    case AST_DATA_BOOLEAN:
02641       content->value.AST_DATA_BOOLEAN = node->payload.boolean;
02642       break;
02643    case AST_DATA_IPADDR:
02644       content->value.AST_DATA_IPADDR = node->payload.ipaddr;
02645       break;
02646    case AST_DATA_DOUBLE:
02647       content->value.AST_DATA_DOUBLE = node->payload.dbl;
02648       break;
02649    case AST_DATA_CONTAINER:
02650       break;
02651    case AST_DATA_POINTER:
02652       content->value.AST_DATA_POINTER = node->payload.ptr;
02653       break;
02654    }
02655 
02656    return 0;
02657 }
02658 
02659 /*!
02660  * \internal
02661  * \brief One color for each node type.
02662  */
02663 static const struct {
02664    enum ast_data_type type;
02665    int color;
02666 } data_result_color[] = {
02667    { AST_DATA_STRING, COLOR_BLUE },
02668    { AST_DATA_PASSWORD, COLOR_BRBLUE },
02669    { AST_DATA_TIMESTAMP, COLOR_CYAN },
02670    { AST_DATA_SECONDS, COLOR_MAGENTA },
02671    { AST_DATA_MILLISECONDS, COLOR_BRMAGENTA },
02672    { AST_DATA_CHARACTER, COLOR_GRAY },
02673    { AST_DATA_INTEGER, COLOR_RED },
02674    { AST_DATA_UNSIGNED_INTEGER, COLOR_RED },
02675    { AST_DATA_DOUBLE, COLOR_RED },
02676    { AST_DATA_BOOLEAN, COLOR_BRRED },
02677    { AST_DATA_CONTAINER, COLOR_GREEN },
02678    { AST_DATA_IPADDR, COLOR_BROWN },
02679    { AST_DATA_POINTER, COLOR_YELLOW },
02680 };
02681 
02682 /*!
02683  * \internal
02684  * \brief Get the color configured for a specific node type.
02685  * \param[in] type The node type.
02686  * \returns The color specified for the passed type.
02687  */
02688 static int data_result_get_color(enum ast_data_type type)
02689 {
02690    int i;
02691    for (i = 0; i < ARRAY_LEN(data_result_color); i++) {
02692       if (data_result_color[i].type == type) {
02693          return data_result_color[i].color;
02694       }
02695    }
02696 
02697    return COLOR_BLUE;
02698 }
02699 
02700 /*!
02701  * \internal
02702  * \brief Print a node to the CLI.
02703  * \param[in] fd The CLI file descriptor.
02704  * \param[in] node The node to print.
02705  * \param[in] depth The actual node depth in the tree.
02706  */
02707 static void data_result_print_cli_node(int fd, const struct ast_data *node, uint32_t depth)
02708 {
02709    int i;
02710    struct ast_str *tabs, *output;
02711 
02712    tabs = ast_str_create(depth * 10 + 1);
02713    if (!tabs) {
02714       return;
02715    }
02716    ast_str_reset(tabs);
02717    for (i = 0; i < depth; i++) {
02718       ast_str_append(&tabs, 0, "  ");
02719    }
02720 
02721    output = ast_str_create(20);
02722    if (!output) {
02723       ast_free(tabs);
02724       return;
02725    }
02726 
02727    ast_str_reset(output);
02728    ast_term_color_code(&output, data_result_get_color(node->type), 0);
02729 
02730    switch (node->type) {
02731    case AST_DATA_POINTER:
02732       ast_str_append(&output, 0, "%s%s: %p\n", ast_str_buffer(tabs),
02733             node->name, node->payload.ptr);
02734       break;
02735    case AST_DATA_PASSWORD:
02736       ast_str_append(&output, 0, "%s%s: \"%s\"\n",
02737             ast_str_buffer(tabs),
02738             node->name,
02739             node->payload.str);
02740       break;
02741    case AST_DATA_STRING:
02742       ast_str_append(&output, 0, "%s%s: \"%s\"\n",
02743             ast_str_buffer(tabs),
02744             node->name,
02745             node->payload.str);
02746       break;
02747    case AST_DATA_CHARACTER:
02748       ast_str_append(&output, 0, "%s%s: \'%c\'\n",
02749             ast_str_buffer(tabs),
02750             node->name,
02751             node->payload.character);
02752       break;
02753    case AST_DATA_CONTAINER:
02754       ast_str_append(&output, 0, "%s%s\n", ast_str_buffer(tabs),
02755             node->name);
02756       break;
02757    case AST_DATA_TIMESTAMP:
02758       ast_str_append(&output, 0, "%s%s: %u\n", ast_str_buffer(tabs),
02759             node->name,
02760             node->payload.uint);
02761       break;
02762    case AST_DATA_SECONDS:
02763       ast_str_append(&output, 0, "%s%s: %u\n", ast_str_buffer(tabs),
02764             node->name,
02765             node->payload.uint);
02766       break;
02767    case AST_DATA_MILLISECONDS:
02768       ast_str_append(&output, 0, "%s%s: %u\n", ast_str_buffer(tabs),
02769             node->name,
02770             node->payload.uint);
02771       break;
02772    case AST_DATA_INTEGER:
02773       ast_str_append(&output, 0, "%s%s: %d\n", ast_str_buffer(tabs),
02774             node->name,
02775             node->payload.sint);
02776       break;
02777    case AST_DATA_UNSIGNED_INTEGER:
02778       ast_str_append(&output, 0, "%s%s: %u\n", ast_str_buffer(tabs),
02779             node->name,
02780             node->payload.uint);
02781       break;
02782    case AST_DATA_DOUBLE:
02783       ast_str_append(&output, 0, "%s%s: %lf\n", ast_str_buffer(tabs),
02784             node->name,
02785             node->payload.dbl);
02786       break;
02787    case AST_DATA_BOOLEAN:
02788       ast_str_append(&output, 0, "%s%s: %s\n", ast_str_buffer(tabs),
02789             node->name,
02790             ((node->payload.boolean) ? "True" : "False"));
02791       break;
02792    case AST_DATA_IPADDR:
02793       ast_str_append(&output, 0, "%s%s: %s\n", ast_str_buffer(tabs),
02794             node->name,
02795             ast_inet_ntoa(node->payload.ipaddr));
02796       break;
02797    }
02798 
02799    ast_free(tabs);
02800 
02801    ast_term_color_code(&output, 0, 0);
02802 
02803    ast_cli(fd, "%s", ast_str_buffer(output));
02804 
02805    ast_free(output);
02806 
02807    if (node->type == AST_DATA_CONTAINER) {
02808       __data_result_print_cli(fd, node, depth + 1);
02809    }
02810 }
02811 
02812 /*!
02813  * \internal
02814  * \brief Print out an ast_data tree to the CLI.
02815  * \param[in] fd The CLI file descriptor.
02816  * \param[in] root The root node of the tree.
02817  * \param[in] depth Actual depth.
02818  */
02819 
02820 static void __data_result_print_cli(int fd, const struct ast_data *root, uint32_t depth)
02821 {
02822    struct ao2_iterator iter;
02823    struct ast_data *node;
02824 
02825    if (root->type == AST_DATA_CONTAINER) {
02826       iter = ao2_iterator_init(root->children, 0);
02827       while ((node = ao2_iterator_next(&iter))) {
02828          data_result_print_cli_node(fd, node, depth + 1);
02829          ao2_ref(node, -1);
02830       }
02831       ao2_iterator_destroy(&iter);
02832    } else {
02833       data_result_print_cli_node(fd, root, depth);
02834    }
02835 }
02836 
02837 /*!
02838  * \internal
02839  * \brief
02840  * \param[in] fd The CLI file descriptor.
02841  * \param[in] root The root node of the tree.
02842  */
02843 static void data_result_print_cli(int fd, const struct ast_data *root)
02844 {
02845    ast_cli(fd, COLORIZE_FMT "\n", COLORIZE(data_result_get_color(root->type), 0, root->name));
02846 
02847    __data_result_print_cli(fd, root, 0);
02848 
02849    ast_cli(fd, "\n");
02850 }
02851 
02852 /*!
02853  * \internal
02854  * \brief Handle the CLI command "data get".
02855  */
02856 static char *handle_cli_data_get(struct ast_cli_entry *e, int cmd,
02857       struct ast_cli_args *a)
02858 {
02859    struct ast_data_query query = {
02860       .version = AST_DATA_QUERY_VERSION
02861    };
02862    struct ast_data *tree;
02863 
02864    switch (cmd) {
02865    case CLI_INIT:
02866       e->command = "data get";
02867       e->usage = ""
02868          "Usage: data get <path> [<search> [<filter>]]\n"
02869          "       Get the tree based on a path.\n";
02870       return NULL;
02871    case CLI_GENERATE:
02872       return NULL;
02873    }
02874 
02875    if (a->argc < e->args + 1) {
02876       return CLI_SHOWUSAGE;
02877    }
02878 
02879    query.path = (char *) a->argv[e->args];
02880 
02881    if (a->argc > e->args + 1) {
02882       query.search = (char *) a->argv[e->args + 1];
02883    }
02884 
02885    if (a->argc > e->args + 2) {
02886       query.filter = (char *) a->argv[e->args + 2];
02887    }
02888 
02889    tree = ast_data_get(&query);
02890    if (!tree) {
02891       return CLI_FAILURE;
02892    }
02893 
02894    data_result_print_cli(a->fd, tree);
02895 
02896    ast_data_free(tree);
02897 
02898    return CLI_SUCCESS;
02899 }
02900 
02901 /*!
02902  * \internal
02903  * \brief Print the list of data providers.
02904  * \param[in] fd The CLI file descriptor.
02905  * \param[in] name The last node visited name.
02906  * \param[in] container The childrens of the last node.
02907  * \param[in] path The path to the current node.
02908  */
02909 static void data_provider_print_cli(int fd, const char *name,
02910    struct ao2_container *container, struct ast_str *path)
02911 {
02912    struct ao2_iterator i;
02913    struct ast_str *current_path;
02914    struct data_provider *provider;
02915 
02916    current_path = ast_str_create(60);
02917    if (!current_path) {
02918       return;
02919    }
02920 
02921    ast_str_reset(current_path);
02922    if (path) {
02923       ast_str_set(&current_path, 0, "%s/%s", ast_str_buffer(path), name);
02924    } else {
02925       ast_str_set(&current_path, 0, "%s", name);
02926    }
02927 
02928    i = ao2_iterator_init(container, 0);
02929    while ((provider = ao2_iterator_next(&i))) {
02930       if (provider->handler) {
02931          /* terminal node, print it. */
02932          ast_cli(fd, "%s/%s (", ast_str_buffer(current_path),
02933             provider->name);
02934          if (provider->handler->get) {
02935             ast_cli(fd, "get");
02936          }
02937          ast_cli(fd, ") [%s]\n", provider->registrar);
02938       }
02939       data_provider_print_cli(fd, provider->name, provider->children,
02940          current_path);
02941       ao2_ref(provider, -1);
02942    }
02943    ao2_iterator_destroy(&i);
02944 
02945    ast_free(current_path);
02946 }
02947 
02948 /*!
02949  * \internal
02950  * \brief Handle CLI command "data show providers"
02951  */
02952 static char *handle_cli_data_show_providers(struct ast_cli_entry *e, int cmd,
02953       struct ast_cli_args *a)
02954 {
02955    switch (cmd) {
02956    case CLI_INIT:
02957       e->command = "data show providers";
02958       e->usage = ""
02959          "Usage: data show providers\n"
02960          "       Show the list of registered providers\n";
02961       return NULL;
02962    case CLI_GENERATE:
02963       return NULL;
02964    }
02965 
02966    data_read_lock();
02967    data_provider_print_cli(a->fd, "", root_data.container, NULL);
02968    data_unlock();
02969 
02970    return CLI_SUCCESS;
02971 }
02972 
02973 /*!
02974  * \internal
02975  * \brief Data API CLI commands.
02976  */
02977 static struct ast_cli_entry cli_data[] = {
02978    AST_CLI_DEFINE(handle_cli_data_get, "Data API get"),
02979    AST_CLI_DEFINE(handle_cli_data_show_providers, "Show data providers")
02980 };
02981 
02982 /*!
02983  * \internal
02984  * \brief Output a tree to the AMI.
02985  * \param[in] s AMI session.
02986  * \param[in] name The root node name.
02987  * \param[in] container The root container.
02988  * \param[in] path The current path.
02989  */
02990 static void data_result_manager_output(struct mansession *s, const char *name,
02991    struct ao2_container *container, struct ast_str *path, int id)
02992 {
02993    struct ao2_iterator i;
02994    struct ast_str *current_path;
02995    struct ast_data *node;
02996    int current_id = id;
02997 
02998    current_path = ast_str_create(60);
02999    if (!current_path) {
03000       return;
03001    }
03002 
03003    ast_str_reset(current_path);
03004    if (path) {
03005       ast_str_set(&current_path, 0, "%s.%s", ast_str_buffer(path), name);
03006    } else {
03007       ast_str_set(&current_path, 0, "%s", name);
03008    }
03009 
03010    i = ao2_iterator_init(container, 0);
03011    while ((node = ao2_iterator_next(&i))) {
03012       /* terminal node, print it. */
03013       if (node->type != AST_DATA_CONTAINER) {
03014          astman_append(s, "%d-%s.%s", id, ast_str_buffer(current_path),
03015                node->name);
03016       }
03017       switch (node->type) {
03018       case AST_DATA_CONTAINER:
03019          data_result_manager_output(s, node->name, node->children, current_path, ++current_id);
03020          break;
03021       case AST_DATA_INTEGER:
03022          astman_append(s, ": %d\r\n", node->payload.sint);
03023          break;
03024       case AST_DATA_TIMESTAMP:
03025       case AST_DATA_SECONDS:
03026       case AST_DATA_MILLISECONDS:
03027       case AST_DATA_UNSIGNED_INTEGER:
03028          astman_append(s, ": %u\r\n", node->payload.uint);
03029          break;
03030       case AST_DATA_PASSWORD:
03031          astman_append(s, ": %s\r\n", node->payload.str);
03032          break;
03033       case AST_DATA_STRING:
03034          astman_append(s, ": %s\r\n", node->payload.str);
03035          break;
03036       case AST_DATA_CHARACTER:
03037          astman_append(s, ": %c\r\n", node->payload.character);
03038          break;
03039       case AST_DATA_IPADDR:
03040          astman_append(s, ": %s\r\n", ast_inet_ntoa(node->payload.ipaddr));
03041          break;
03042       case AST_DATA_POINTER:
03043          break;
03044       case AST_DATA_DOUBLE:
03045          astman_append(s, ": %f\r\n", node->payload.dbl);
03046          break;
03047       case AST_DATA_BOOLEAN:
03048          astman_append(s, ": %s\r\n",
03049             (node->payload.boolean ? "True" : "False"));
03050          break;
03051       }
03052 
03053       ao2_ref(node, -1);
03054    }
03055    ao2_iterator_destroy(&i);
03056 
03057    ast_free(current_path);
03058 }
03059 
03060 /*!
03061  * \internal
03062  * \brief Implements the manager action: "DataGet".
03063  */
03064 static int manager_data_get(struct mansession *s, const struct message *m)
03065 {
03066    const char *path = astman_get_header(m, "Path");
03067    const char *search = astman_get_header(m, "Search");
03068    const char *filter = astman_get_header(m, "Filter");
03069    const char *id = astman_get_header(m, "ActionID");
03070    struct ast_data *res;
03071    struct ast_data_query query = {
03072       .version = AST_DATA_QUERY_VERSION,
03073       .path = (char *) path,
03074       .search = (char *) search,
03075       .filter = (char *) filter,
03076    };
03077 
03078    if (ast_strlen_zero(path)) {
03079       astman_send_error(s, m, "'Path' parameter not specified");
03080       return 0;
03081    }
03082 
03083    res = ast_data_get(&query);
03084    if (!res) {
03085       astman_send_error(s, m, "No data returned");
03086       return 0;
03087    }
03088 
03089    astman_append(s, "Event: DataGet Tree\r\n");
03090    if (!ast_strlen_zero(id)) {
03091       astman_append(s, "ActionID: %s\r\n", id);
03092    }
03093    data_result_manager_output(s, res->name, res->children, NULL, 0);
03094    astman_append(s, "\r\n");
03095 
03096    ast_data_free(res);
03097 
03098    return RESULT_SUCCESS;
03099 }
03100 
03101 static int data_add_codec(struct ast_data *codecs, struct ast_format *format) {
03102    struct ast_data *codec;
03103    struct ast_codec *tmp;
03104 
03105    tmp = ast_codec_get_by_id(ast_format_get_codec_id(format));
03106    if (!tmp) {
03107       return -1;
03108    }
03109 
03110    codec = ast_data_add_node(codecs, "codec");
03111    if (!codec) {
03112       ao2_ref(tmp, -1);
03113       return -1;
03114    }
03115 
03116    ast_data_add_str(codec, "name", tmp->name);
03117    ast_data_add_int(codec, "samplespersecond", tmp->sample_rate);
03118    ast_data_add_str(codec, "description", tmp->description);
03119    ast_data_add_int(codec, "frame_length", tmp->minimum_bytes);
03120    ao2_ref(tmp, -1);
03121 
03122    return 0;
03123 }
03124 
03125 int ast_data_add_codec(struct ast_data *root, const char *node_name, struct ast_format *format)
03126 {
03127    struct ast_data *codecs;
03128 
03129    codecs = ast_data_add_node(root, node_name);
03130    if (!codecs) {
03131       return -1;
03132    }
03133 
03134    return data_add_codec(codecs, format);
03135 }
03136 
03137 int ast_data_add_codecs(struct ast_data *root, const char *node_name, struct ast_format_cap *cap)
03138 {
03139    struct ast_data *codecs;
03140    size_t i;
03141    size_t count;
03142 
03143    codecs = ast_data_add_node(root, node_name);
03144    if (!codecs) {
03145       return -1;
03146    }
03147 
03148    count = ast_format_cap_count(cap);
03149    for (i = 0; i < count; ++i) {
03150       struct ast_format *fmt;
03151 
03152       fmt = ast_format_cap_get_format(cap, i);
03153       if (!fmt) {
03154          return -1;
03155       }
03156 
03157       if (data_add_codec(codecs, fmt)) {
03158          ao2_ref(fmt, -1);
03159          return -1;
03160       }
03161 
03162       ao2_ref(fmt, -1);
03163    }
03164 
03165    return 0;
03166 }
03167 
03168 #ifdef TEST_FRAMEWORK
03169 
03170 /*!
03171  * \internal
03172  * \brief Structure used to test how to add a complete structure,
03173  *        and how to compare it.
03174  */
03175 struct test_structure {
03176    int a_int;
03177    unsigned int b_bool:1;
03178    char *c_str;
03179    unsigned int a_uint;
03180 };
03181 
03182 /*!
03183  * \internal
03184  * \brief test_structure mapping.
03185  */
03186 #define DATA_EXPORT_TEST_STRUCTURE(MEMBER)                              \
03187    MEMBER(test_structure, a_int, AST_DATA_INTEGER)                 \
03188    MEMBER(test_structure, b_bool, AST_DATA_BOOLEAN)                \
03189    MEMBER(test_structure, c_str, AST_DATA_STRING)                  \
03190    MEMBER(test_structure, a_uint, AST_DATA_UNSIGNED_INTEGER)
03191 
03192 AST_DATA_STRUCTURE(test_structure, DATA_EXPORT_TEST_STRUCTURE);
03193 
03194 /*!
03195  * \internal
03196  * \brief Callback implementation.
03197  */
03198 static int test_data_full_provider(const struct ast_data_search *search,
03199       struct ast_data *root)
03200 {
03201    struct ast_data *test_structure;
03202    struct test_structure local_test_structure = {
03203       .a_int = 10,
03204       .b_bool = 1,
03205       .c_str = "test string",
03206       .a_uint = 20
03207    };
03208 
03209    test_structure = ast_data_add_node(root, "test_structure");
03210    if (!test_structure) {
03211       ast_debug(1, "Internal data api error\n");
03212       return 0;
03213    }
03214 
03215    /* add the complete structure. */
03216    ast_data_add_structure(test_structure, test_structure, &local_test_structure);
03217 
03218    if (!ast_data_search_match(search, test_structure)) {
03219       ast_data_remove_node(root, test_structure);
03220    }
03221 
03222    return 0;
03223 }
03224 
03225 /*!
03226  * \internal
03227  * \brief Handler definition for the full provider.
03228  */
03229 static const struct ast_data_handler full_provider = {
03230    .version = AST_DATA_HANDLER_VERSION,
03231    .get = test_data_full_provider
03232 };
03233 
03234 /*!
03235  * \internal
03236  * \brief Structure used to define multiple providers at once.
03237  */
03238 static const struct ast_data_entry test_providers[] = {
03239    AST_DATA_ENTRY("test/node1/node11/node111", &full_provider)
03240 };
03241 
03242 AST_TEST_DEFINE(test_data_get)
03243 {
03244    struct ast_data *res, *node;
03245    struct ast_data_iterator *i;
03246    struct ast_data_query query = {
03247       .version = AST_DATA_QUERY_VERSION,
03248       .path = "test/node1/node11/node111",
03249       .search = "node111/test_structure/a_int=10",
03250       .filter = "node111/test_structure/a*int"
03251    };
03252 
03253    switch (cmd) {
03254    case TEST_INIT:
03255       info->name = "data_test";
03256       info->category = "/main/data/";
03257       info->summary = "Data API unit test";
03258       info->description =
03259          "Tests whether data API get implementation works as expected.";
03260       return AST_TEST_NOT_RUN;
03261    case TEST_EXECUTE:
03262       break;
03263    }
03264 
03265    ast_data_register_multiple_core(test_providers, ARRAY_LEN(test_providers));
03266 
03267    res = ast_data_get(&query);
03268    if (!res) {
03269       ast_test_status_update(test, "Unable to get tree.");
03270       ast_data_unregister("test/node1/node11/node111");
03271       return AST_TEST_FAIL;
03272    }
03273 
03274    /* initiate the iterator and check for errors. */
03275    i = ast_data_iterator_init(res, "test_structure/");
03276    if (!i) {
03277       ast_test_status_update(test, "Unable to initiate the iterator.");
03278       ast_data_free(res);
03279       ast_data_unregister("test/node1/node11/node111");
03280       return AST_TEST_FAIL;
03281    }
03282 
03283    /* walk the returned nodes. */
03284    while ((node = ast_data_iterator_next(i))) {
03285       if (!strcmp(ast_data_retrieve_name(node), "a_int")) {
03286          if (ast_data_retrieve_int(node, "/") != 10) {
03287             ast_data_iterator_end(i);
03288             ast_data_free(res);
03289             ast_data_unregister("test/node1/node11/node111");
03290             return AST_TEST_FAIL;
03291          }
03292       } else if (!strcmp(ast_data_retrieve_name(node), "a_uint")) {
03293          if (ast_data_retrieve_uint(node, "/") != 20) {
03294             ast_data_iterator_end(i);
03295             ast_data_free(res);
03296             ast_data_unregister("test/node1/node11/node111");
03297             return AST_TEST_FAIL;
03298          }
03299       }
03300    }
03301 
03302    /* finish the iterator. */
03303    ast_data_iterator_end(i);
03304 
03305    ast_data_free(res);
03306 
03307    ast_data_unregister("test/node1/node11/node111");
03308 
03309    return AST_TEST_PASS;
03310 }
03311 
03312 #endif
03313 
03314 /*!
03315  * \internal
03316  * \brief Clean up resources on Asterisk shutdown
03317  */
03318 static void data_shutdown(void)
03319 {
03320    ast_manager_unregister("DataGet");
03321    ast_cli_unregister_multiple(cli_data, ARRAY_LEN(cli_data));
03322    ao2_t_ref(root_data.container, -1, "Unref root_data.container in data_shutdown");
03323    root_data.container = NULL;
03324    ast_rwlock_destroy(&root_data.lock);
03325    AST_TEST_UNREGISTER(test_data_get);
03326 }
03327 
03328 int ast_data_init(void)
03329 {
03330    int res = 0;
03331 
03332    ast_rwlock_init(&root_data.lock);
03333 
03334    if (!(root_data.container = ao2_container_alloc(NUM_DATA_NODE_BUCKETS,
03335       data_provider_hash, data_provider_cmp))) {
03336       return -1;
03337    }
03338 
03339    res |= ast_cli_register_multiple(cli_data, ARRAY_LEN(cli_data));
03340 
03341    res |= ast_manager_register_xml_core("DataGet", 0, manager_data_get);
03342 
03343    AST_TEST_REGISTER(test_data_get);
03344 
03345    ast_register_cleanup(data_shutdown);
03346 
03347    return res;
03348 }

Generated on Thu Apr 16 06:27:32 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6