Asterisk data retrieval API.
authorEliel C. Sardanons <eliels@gmail.com>
Thu, 22 Apr 2010 18:07:02 +0000 (18:07 +0000)
committerEliel C. Sardanons <eliels@gmail.com>
Thu, 22 Apr 2010 18:07:02 +0000 (18:07 +0000)
This module implements an abstraction for retrieving and exporting
asterisk data.
Developed by:
Brett Bryant <brettbryant@gmail.com>
Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
For the Google Summer of code 2009 Project.
Documentation can be found in doxygen format and inside the
header include/asterisk/data.h

Review: https://reviewboard.asterisk.org/r/275/

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@258517 65c4cc65-6c06-0410-ace0-fbb531ad65f3

13 files changed:
CHANGES
apps/app_queue.c
apps/app_voicemail.c
doc/manager_1_1.txt
include/asterisk/_private.h
include/asterisk/channel.h
include/asterisk/data.h [new file with mode: 0644]
include/asterisk/doxyref.h
include/asterisk/xml.h
main/asterisk.c
main/channel.c
main/data.c [new file with mode: 0644]
main/xml.c

diff --git a/CHANGES b/CHANGES
index 82250d9..511e702 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -783,6 +783,10 @@ IAX Changes
 
 CLI Changes
 -----------
+  * New CLI command, "data get <path> [<search> [<filter>]]" which retrieves the
+     data tree based on the given <path>.
+  * New CLI command "data show providers" that will display all the registered
+     callbacks.
   * New CLI command, "config reload <file.conf>" which reloads any module that
      references that particular configuration file.  Also added "config list"
      which shows which configuration files are in use.
index e689cb0..bc1ca96 100644 (file)
@@ -96,6 +96,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/taskprocessor.h"
 #include "asterisk/callerid.h"
 #include "asterisk/cel.h"
+#include "asterisk/data.h"
 
 /* Define, to debug reference counts on queues, without debugging reference counts on queue members */
 /* #define REF_DEBUG_ONLY_QUEUES */
@@ -7616,6 +7617,281 @@ static struct ast_cli_entry cli_queue[] = {
        AST_CLI_DEFINE(handle_queue_reset, "Reset statistics for a queue"),
 };
 
+/* struct call_queue astdata mapping. */
+#define DATA_EXPORT_CALL_QUEUE(MEMBER)                                 \
+       MEMBER(call_queue, name, AST_DATA_STRING)                       \
+       MEMBER(call_queue, moh, AST_DATA_STRING)                        \
+       MEMBER(call_queue, announce, AST_DATA_STRING)                   \
+       MEMBER(call_queue, context, AST_DATA_STRING)                    \
+       MEMBER(call_queue, membermacro, AST_DATA_STRING)                \
+       MEMBER(call_queue, membergosub, AST_DATA_STRING)                \
+       MEMBER(call_queue, defaultrule, AST_DATA_STRING)                \
+       MEMBER(call_queue, sound_next, AST_DATA_STRING)                 \
+       MEMBER(call_queue, sound_thereare, AST_DATA_STRING)             \
+       MEMBER(call_queue, sound_calls, AST_DATA_STRING)                \
+       MEMBER(call_queue, queue_quantity1, AST_DATA_STRING)            \
+       MEMBER(call_queue, queue_quantity2, AST_DATA_STRING)            \
+       MEMBER(call_queue, sound_holdtime, AST_DATA_STRING)             \
+       MEMBER(call_queue, sound_minutes, AST_DATA_STRING)              \
+       MEMBER(call_queue, sound_minute, AST_DATA_STRING)               \
+       MEMBER(call_queue, sound_seconds, AST_DATA_STRING)              \
+       MEMBER(call_queue, sound_thanks, AST_DATA_STRING)               \
+       MEMBER(call_queue, sound_callerannounce, AST_DATA_STRING)       \
+       MEMBER(call_queue, sound_reporthold, AST_DATA_STRING)           \
+       MEMBER(call_queue, dead, AST_DATA_BOOLEAN)                      \
+       MEMBER(call_queue, eventwhencalled, AST_DATA_BOOLEAN)           \
+       MEMBER(call_queue, ringinuse, AST_DATA_INTEGER)                 \
+       MEMBER(call_queue, setinterfacevar, AST_DATA_BOOLEAN)           \
+       MEMBER(call_queue, setqueuevar, AST_DATA_BOOLEAN)               \
+       MEMBER(call_queue, setqueueentryvar, AST_DATA_BOOLEAN)          \
+       MEMBER(call_queue, reportholdtime, AST_DATA_BOOLEAN)            \
+       MEMBER(call_queue, wrapped, AST_DATA_BOOLEAN)                   \
+       MEMBER(call_queue, timeoutrestart, AST_DATA_BOOLEAN)            \
+       MEMBER(call_queue, announceholdtime, AST_DATA_INTEGER)          \
+       MEMBER(call_queue, announceposition, AST_DATA_INTEGER)          \
+       MEMBER(call_queue, strategy, AST_DATA_INTEGER)                  \
+       MEMBER(call_queue, maskmemberstatus, AST_DATA_BOOLEAN)          \
+       MEMBER(call_queue, realtime, AST_DATA_BOOLEAN)                  \
+       MEMBER(call_queue, found, AST_DATA_BOOLEAN)                     \
+       MEMBER(call_queue, announcepositionlimit, AST_DATA_INTEGER)     \
+       MEMBER(call_queue, announcefrequency, AST_DATA_INTEGER)         \
+       MEMBER(call_queue, minannouncefrequency, AST_DATA_INTEGER)      \
+       MEMBER(call_queue, periodicannouncefrequency, AST_DATA_INTEGER) \
+       MEMBER(call_queue, numperiodicannounce, AST_DATA_INTEGER)       \
+       MEMBER(call_queue, randomperiodicannounce, AST_DATA_INTEGER)    \
+       MEMBER(call_queue, roundingseconds, AST_DATA_INTEGER)           \
+       MEMBER(call_queue, holdtime, AST_DATA_INTEGER)                  \
+       MEMBER(call_queue, talktime, AST_DATA_INTEGER)                  \
+       MEMBER(call_queue, callscompleted, AST_DATA_INTEGER)            \
+       MEMBER(call_queue, callsabandoned, AST_DATA_INTEGER)            \
+       MEMBER(call_queue, servicelevel, AST_DATA_INTEGER)              \
+       MEMBER(call_queue, callscompletedinsl, AST_DATA_INTEGER)        \
+       MEMBER(call_queue, monfmt, AST_DATA_STRING)                     \
+       MEMBER(call_queue, montype, AST_DATA_INTEGER)                   \
+       MEMBER(call_queue, count, AST_DATA_INTEGER)                     \
+       MEMBER(call_queue, maxlen, AST_DATA_INTEGER)                    \
+       MEMBER(call_queue, wrapuptime, AST_DATA_INTEGER)                \
+       MEMBER(call_queue, retry, AST_DATA_INTEGER)                     \
+       MEMBER(call_queue, timeout, AST_DATA_INTEGER)                   \
+       MEMBER(call_queue, weight, AST_DATA_INTEGER)                    \
+       MEMBER(call_queue, autopause, AST_DATA_INTEGER)                 \
+       MEMBER(call_queue, timeoutpriority, AST_DATA_INTEGER)           \
+       MEMBER(call_queue, rrpos, AST_DATA_INTEGER)                     \
+       MEMBER(call_queue, memberdelay, AST_DATA_INTEGER)               \
+       MEMBER(call_queue, autofill, AST_DATA_INTEGER)                  \
+       MEMBER(call_queue, members, AST_DATA_CONTAINER)                 \
+       MEMBER(call_queue, membercount, AST_DATA_INTEGER)
+
+AST_DATA_STRUCTURE(call_queue, DATA_EXPORT_CALL_QUEUE);
+
+/* struct member astdata mapping. */
+#define DATA_EXPORT_MEMBER(MEMBER)                                     \
+       MEMBER(member, interface, AST_DATA_STRING)                      \
+       MEMBER(member, state_interface, AST_DATA_STRING)                \
+       MEMBER(member, membername, AST_DATA_STRING)                     \
+       MEMBER(member, penalty, AST_DATA_INTEGER)                       \
+       MEMBER(member, calls, AST_DATA_INTEGER)                         \
+       MEMBER(member, dynamic, AST_DATA_INTEGER)                       \
+       MEMBER(member, realtime, AST_DATA_INTEGER)                      \
+       MEMBER(member, status, AST_DATA_INTEGER)                        \
+       MEMBER(member, paused, AST_DATA_BOOLEAN)                        \
+       MEMBER(member, rt_uniqueid, AST_DATA_STRING)
+
+AST_DATA_STRUCTURE(member, DATA_EXPORT_MEMBER);
+
+#define DATA_EXPORT_QUEUE_ENT(MEMBER)                                          \
+       MEMBER(queue_ent, moh, AST_DATA_STRING)                                 \
+       MEMBER(queue_ent, announce, AST_DATA_STRING)                            \
+       MEMBER(queue_ent, context, AST_DATA_STRING)                             \
+       MEMBER(queue_ent, digits, AST_DATA_STRING)                              \
+       MEMBER(queue_ent, valid_digits, AST_DATA_INTEGER)                       \
+       MEMBER(queue_ent, pos, AST_DATA_INTEGER)                                \
+       MEMBER(queue_ent, prio, AST_DATA_INTEGER)                               \
+       MEMBER(queue_ent, last_pos_said, AST_DATA_INTEGER)                      \
+       MEMBER(queue_ent, last_periodic_announce_time, AST_DATA_INTEGER)        \
+       MEMBER(queue_ent, last_periodic_announce_sound, AST_DATA_INTEGER)       \
+       MEMBER(queue_ent, last_pos, AST_DATA_INTEGER)                           \
+       MEMBER(queue_ent, opos, AST_DATA_INTEGER)                               \
+       MEMBER(queue_ent, handled, AST_DATA_INTEGER)                            \
+       MEMBER(queue_ent, pending, AST_DATA_INTEGER)                            \
+       MEMBER(queue_ent, max_penalty, AST_DATA_INTEGER)                        \
+       MEMBER(queue_ent, min_penalty, AST_DATA_INTEGER)                        \
+       MEMBER(queue_ent, linpos, AST_DATA_INTEGER)                             \
+       MEMBER(queue_ent, linwrapped, AST_DATA_INTEGER)                         \
+       MEMBER(queue_ent, start, AST_DATA_INTEGER)                              \
+       MEMBER(queue_ent, expire, AST_DATA_INTEGER)                             \
+       MEMBER(queue_ent, cancel_answered_elsewhere, AST_DATA_INTEGER)
+
+AST_DATA_STRUCTURE(queue_ent, DATA_EXPORT_QUEUE_ENT);
+
+/*!
+ * \internal
+ * \brief Add a queue to the data_root node.
+ * \param[in] search The search tree.
+ * \param[in] data_root The main result node.
+ * \param[in] queue The queue to add.
+ */
+static void queues_data_provider_get_helper(const struct ast_data_search *search,
+       struct ast_data *data_root, struct call_queue *queue)
+{
+       int member_notmatch, caller_notmatch, caller_channel_notmatch;
+       struct ao2_iterator im;
+       struct member *member;
+       struct queue_ent *qe;
+       struct ast_data *data_queue, *data_members = NULL;
+       struct ast_data *data_member, *data_callers = NULL, *data_caller, *data_caller_channel;
+
+       /* compare the search pattern. */
+       if (ast_data_search_cmp_structure(search, call_queue, queue, "queue")) {
+               /* this doesn't match! continue! */
+               return;
+       }
+
+       data_queue = ast_data_add_node(data_root, "queue");
+       if (!data_queue) {
+               return;
+       }
+
+       ast_data_add_structure(call_queue, data_queue, queue);
+
+       member_notmatch = ast_data_search_has_condition(search, "queue/members/member");
+       /* add queue members */
+       im = ao2_iterator_init(queue->members, 0);
+       while ((member = ao2_iterator_next(&im))) {
+               /* compare the member structure. */
+               if (!ast_data_search_cmp_structure(search, member, member,
+                                       "queue/members/member")) {
+                       member_notmatch = 0;
+               }
+
+               if (!data_members) {
+                       data_members = ast_data_add_node(data_queue, "members");
+                       if (!data_members) {
+                               ao2_ref(member, -1);
+                               continue;
+                       }
+               }
+
+               data_member = ast_data_add_node(data_members, "member");
+               if (!data_member) {
+                       ao2_ref(member, -1);
+                       continue;
+               }
+
+               ast_data_add_structure(member, data_member, member);
+
+               ao2_ref(member, -1);
+       }
+
+       if (member_notmatch) {
+               ast_data_remove_node(data_root, data_queue);
+               return;
+       }
+
+       caller_notmatch = ast_data_search_has_condition(search, "queue/callers/caller");
+       caller_channel_notmatch = ast_data_search_has_condition(search,
+               "queue/callers/caller/channel");
+       /* include the callers inside the result. */
+       if (queue->head) {
+               for (qe = queue->head; qe; qe = qe->next) {
+                       /* compare the member structure. */
+                       if (!ast_data_search_cmp_structure(search, queue_ent, qe,
+                                               "queue/callers/caller")) {
+                               caller_notmatch = 0;
+                       }
+
+                       if (!ast_channel_data_cmp_structure(search, qe->chan,
+                               "queue/callers/caller/channel")) {
+                               caller_channel_notmatch = 0;
+                       }
+
+                       if (!data_callers) {
+                               data_callers = ast_data_add_node(data_queue, "callers");
+                               if (!data_callers) {
+                                       continue;
+                               }
+                       }
+
+                       data_caller = ast_data_add_node(data_callers, "caller");
+                       if (!data_caller) {
+                               continue;
+                       }
+
+                       ast_data_add_structure(queue_ent, data_caller, qe);
+
+                       /* add the caller channel. */
+                       data_caller_channel = ast_data_add_node(data_caller, "channel");
+                       if (!data_caller_channel) {
+                               continue;
+                       }
+
+                       ast_channel_data_add_structure(data_caller_channel, qe->chan);
+               }
+       }
+
+       /* if this queue doesn't match remove the added queue. */
+       if (caller_notmatch || caller_channel_notmatch) {
+               ast_data_remove_node(data_root, data_queue);
+       }
+}
+
+/*!
+ * \internal
+ * \brief Callback used to generate the queues tree.
+ * \param[in] search The search pattern tree.
+ * \retval NULL on error.
+ * \retval non-NULL The generated tree.
+ */
+static int queues_data_provider_get(const struct ast_data_search *search,
+       struct ast_data *data_root)
+{
+       struct ao2_iterator i;
+       struct call_queue *queue, *queue_realtime = NULL;
+       struct ast_config *cfg;
+       char *queuename;
+
+       /* load realtime queues. */
+       cfg = ast_load_realtime_multientry("queues", "name LIKE", "%", SENTINEL);
+       if (cfg) {
+               for (queuename = ast_category_browse(cfg, NULL);
+                               !ast_strlen_zero(queuename);
+                               queuename = ast_category_browse(cfg, queuename)) {
+                       if ((queue = load_realtime_queue(queuename))) {
+                               queue_unref(queue);
+                       }
+               }
+               ast_config_destroy(cfg);
+       }
+
+       /* static queues. */
+       i = ao2_iterator_init(queues, 0);
+       while ((queue = ao2_iterator_next(&i))) {
+               ao2_lock(queue);
+               if (queue->realtime && !(queue_realtime = load_realtime_queue(queue->name))) {
+                       ao2_unlock(queue);
+                       queue_unref(queue);
+                       continue;
+               } else if (queue->realtime) {
+                       queue_unref(queue_realtime);
+               }
+
+               queues_data_provider_get_helper(search, data_root, queue);
+               ao2_unlock(queue);
+               queue_unref(queue);
+       }
+
+       return 0;
+}
+
+static const struct ast_data_handler queues_data_provider = {
+       .version = AST_DATA_HANDLER_VERSION,
+       .get = queues_data_provider_get
+};
+
+static const struct ast_data_entry queue_data_providers[] = {
+       AST_DATA_ENTRY("asterisk/application/app_queue/queues", &queues_data_provider),
+};
+
 static int unload_module(void)
 {
        int res;
@@ -7646,6 +7922,8 @@ static int unload_module(void)
        res |= ast_custom_function_unregister(&queuewaitingcount_function);
        res |= ast_custom_function_unregister(&queuememberpenalty_function);
 
+       res |= ast_data_unregister(NULL);
+
        if (device_state_sub)
                ast_event_unsubscribe(device_state_sub);
 
@@ -7690,6 +7968,8 @@ static int load_module(void)
        if (queue_persistent_members)
                reload_queue_members();
 
+       ast_data_register_multiple(queue_data_providers, ARRAY_LEN(queue_data_providers));
+
        ast_cli_register_multiple(cli_queue, ARRAY_LEN(cli_queue));
        res = ast_register_application_xml(app, queue_exec);
        res |= ast_register_application_xml(app_aqm, aqm_exec);
index 48ef85e..7fd9a39 100644 (file)
@@ -10739,6 +10739,206 @@ static struct ast_cli_entry cli_voicemail[] = {
        AST_CLI_DEFINE(handle_voicemail_reload, "Reload voicemail configuration"),
 };
 
+#ifdef IMAP_STORAGE
+       #define DATA_EXPORT_VM_USERS(USER)                                      \
+               USER(ast_vm_user, context, AST_DATA_STRING)                     \
+               USER(ast_vm_user, mailbox, AST_DATA_STRING)                     \
+               USER(ast_vm_user, password, AST_DATA_STRING)                    \
+               USER(ast_vm_user, fullname, AST_DATA_STRING)                    \
+               USER(ast_vm_user, email, AST_DATA_STRING)                       \
+               USER(ast_vm_user, emailsubject, AST_DATA_STRING)                \
+               USER(ast_vm_user, emailbody, AST_DATA_STRING)                   \
+               USER(ast_vm_user, pager, AST_DATA_STRING)                       \
+               USER(ast_vm_user, serveremail, AST_DATA_STRING)                 \
+               USER(ast_vm_user, mailcmd, AST_DATA_STRING)                     \
+               USER(ast_vm_user, language, AST_DATA_STRING)                    \
+               USER(ast_vm_user, zonetag, AST_DATA_STRING)                     \
+               USER(ast_vm_user, callback, AST_DATA_STRING)                    \
+               USER(ast_vm_user, dialout, AST_DATA_STRING)                     \
+               USER(ast_vm_user, uniqueid, AST_DATA_STRING)                    \
+               USER(ast_vm_user, exit, AST_DATA_STRING)                        \
+               USER(ast_vm_user, attachfmt, AST_DATA_STRING)                   \
+               USER(ast_vm_user, flags, AST_DATA_UNSIGNED_INTEGER)             \
+               USER(ast_vm_user, saydurationm, AST_DATA_INTEGER)               \
+               USER(ast_vm_user, maxmsg, AST_DATA_INTEGER)                     \
+               USER(ast_vm_user, maxdeletedmsg, AST_DATA_INTEGER)              \
+               USER(ast_vm_user, maxsecs, AST_DATA_INTEGER)                    \
+               USER(ast_vm_user, imapuser, AST_DATA_STRING)                    \
+               USER(ast_vm_user, imappassword, AST_DATA_STRING)                \
+               USER(ast_vm_user, imapvmshareid, AST_DATA_STRING)               \
+               USER(ast_vm_user, volgain, AST_DATA_DOUBLE)
+#else
+       #define DATA_EXPORT_VM_USERS(USER)                                      \
+               USER(ast_vm_user, context, AST_DATA_STRING)                     \
+               USER(ast_vm_user, mailbox, AST_DATA_STRING)                     \
+               USER(ast_vm_user, password, AST_DATA_STRING)                    \
+               USER(ast_vm_user, fullname, AST_DATA_STRING)                    \
+               USER(ast_vm_user, email, AST_DATA_STRING)                       \
+               USER(ast_vm_user, emailsubject, AST_DATA_STRING)                \
+               USER(ast_vm_user, emailbody, AST_DATA_STRING)                   \
+               USER(ast_vm_user, pager, AST_DATA_STRING)                       \
+               USER(ast_vm_user, serveremail, AST_DATA_STRING)                 \
+               USER(ast_vm_user, mailcmd, AST_DATA_STRING)                     \
+               USER(ast_vm_user, language, AST_DATA_STRING)                    \
+               USER(ast_vm_user, zonetag, AST_DATA_STRING)                     \
+               USER(ast_vm_user, callback, AST_DATA_STRING)                    \
+               USER(ast_vm_user, dialout, AST_DATA_STRING)                     \
+               USER(ast_vm_user, uniqueid, AST_DATA_STRING)                    \
+               USER(ast_vm_user, exit, AST_DATA_STRING)                        \
+               USER(ast_vm_user, attachfmt, AST_DATA_STRING)                   \
+               USER(ast_vm_user, flags, AST_DATA_UNSIGNED_INTEGER)             \
+               USER(ast_vm_user, saydurationm, AST_DATA_INTEGER)               \
+               USER(ast_vm_user, maxmsg, AST_DATA_INTEGER)                     \
+               USER(ast_vm_user, maxdeletedmsg, AST_DATA_INTEGER)              \
+               USER(ast_vm_user, maxsecs, AST_DATA_INTEGER)                    \
+               USER(ast_vm_user, volgain, AST_DATA_DOUBLE)
+#endif
+
+AST_DATA_STRUCTURE(ast_vm_user, DATA_EXPORT_VM_USERS);
+
+#define DATA_EXPORT_VM_ZONES(ZONE)                     \
+       ZONE(vm_zone, name, AST_DATA_STRING)            \
+       ZONE(vm_zone, timezone, AST_DATA_STRING)        \
+       ZONE(vm_zone, msg_format, AST_DATA_STRING)
+
+AST_DATA_STRUCTURE(vm_zone, DATA_EXPORT_VM_ZONES);
+
+#ifdef IMAP_STORAGE
+       #define DATA_EXPORT_VM_STATES(STATE)                                            \
+               STATE(vm_state, curbox, AST_DATA_STRING)                                \
+               STATE(vm_state, username, AST_DATA_STRING)                              \
+               STATE(vm_state, context, AST_DATA_STRING)                               \
+               STATE(vm_state, curdir, AST_DATA_STRING)                                \
+               STATE(vm_state, vmbox, AST_DATA_STRING)                                 \
+               STATE(vm_state, fn, AST_DATA_STRING)                                    \
+               STATE(vm_state, intro, AST_DATA_STRING)                                 \
+               STATE(vm_state, curmsg, AST_DATA_INTEGER)                               \
+               STATE(vm_state, lastmsg, AST_DATA_INTEGER)                              \
+               STATE(vm_state, newmessages, AST_DATA_INTEGER)                          \
+               STATE(vm_state, oldmessages, AST_DATA_INTEGER)                          \
+               STATE(vm_state, urgentmessages, AST_DATA_INTEGER)                       \
+               STATE(vm_state, starting, AST_DATA_INTEGER)                             \
+               STATE(vm_state, repeats, AST_DATA_INTEGER)                              \
+               STATE(vm_state, updated, AST_DATA_INTEGER)                              \
+               STATE(vm_state, msgArray, AST_DATA_CONTAINER)                           \
+               STATE(vm_state, vmArrayIndex, AST_DATA_INTEGER)                         \
+               STATE(vm_state, imapuser, AST_DATA_STRING)                              \
+               STATE(vm_state, interactive, AST_DATA_INTEGER)                          \
+               STATE(vm_state, introfn, AST_DATA_STRING)                               \
+               STATE(vm_state, quota_limit, AST_DATA_UNSIGNED_INTEGER)                 \
+               STATE(vm_state, quota_usage, AST_DATA_UNSIGNED_INTEGER)
+#else
+       #define DATA_EXPORT_VM_STATES(STATE)                                            \
+               STATE(vm_state, curbox, AST_DATA_STRING)                                \
+               STATE(vm_state, username, AST_DATA_STRING)                              \
+               STATE(vm_state, context, AST_DATA_STRING)                               \
+               STATE(vm_state, curdir, AST_DATA_STRING)                                \
+               STATE(vm_state, vmbox, AST_DATA_STRING)                                 \
+               STATE(vm_state, fn, AST_DATA_STRING)                                    \
+               STATE(vm_state, intro, AST_DATA_STRING)                                 \
+               STATE(vm_state, curmsg, AST_DATA_INTEGER)                               \
+               STATE(vm_state, lastmsg, AST_DATA_INTEGER)                              \
+               STATE(vm_state, newmessages, AST_DATA_INTEGER)                          \
+               STATE(vm_state, oldmessages, AST_DATA_INTEGER)                          \
+               STATE(vm_state, urgentmessages, AST_DATA_INTEGER)                       \
+               STATE(vm_state, starting, AST_DATA_INTEGER)                             \
+               STATE(vm_state, repeats, AST_DATA_INTEGER)
+#endif
+
+AST_DATA_STRUCTURE(vm_state, DATA_EXPORT_VM_STATES);
+
+/*!
+ * \internal
+ * \brief Add voicemail user to the data_root.
+ * \param[in] search The search tree.
+ * \param[in] data_root The main result node.
+ * \param[in] user The voicemail user.
+ */
+static void vm_users_data_provider_get_helper(const struct ast_data_search *search,
+    struct ast_data *data_root, struct ast_vm_user *user)
+{
+       struct ast_data *data_user, *data_zone;
+#ifdef IMAP_STORAGE
+       struct ast_data *data_state;
+       struct vm_state *state;
+#endif
+       struct vm_zone *zone = NULL;
+
+       /* check the search pattern to make sure it's valid to add it */
+       if (ast_data_search_cmp_structure(search, ast_vm_user, user, "user")) {
+               return;
+       }
+
+       data_user = ast_data_add_node(data_root, "user");
+       if (!data_user) {
+               return;
+       }
+
+       ast_data_add_structure(ast_vm_user, data_user, user);
+
+#ifdef IMAP_STORAGE
+       state = get_vm_state_by_mailbox(user->mailbox, user->context, user->interactive);
+#endif
+
+       AST_LIST_LOCK(&zones);
+       AST_LIST_TRAVERSE(&zones, zone, list) {
+               if (!strcmp(zone->name, user->zonetag)) {
+                       break;
+               }
+       }
+       AST_LIST_UNLOCK(&zones);
+
+       /* TODO: Should a user's vm state be accessible without compiling in
+        *       IMAP support? */
+
+       if (
+#ifdef IMAP_STORAGE
+               !ast_data_search_cmp_structure(search, vm_state, state, "user/state") ||
+#endif
+               (zone && !ast_data_search_cmp_structure(search, vm_zone,
+                                                       zone, "user/zone"))) {
+               ast_data_remove_node(data_root, data_user);
+               return;
+       }
+
+#ifdef IMAP_STORAGE
+       data_state = ast_data_add_node(data_user, "state");
+       ast_data_add_structure(vm_state, data_state, state);
+       ast_data_add_int(data_state, "deleted", *(state->deleted));
+       ast_data_add_int(data_state, "heard", *(state->heard));
+#endif
+
+       if (zone) {
+               data_zone = ast_data_add_node(data_user, "zone");
+               ast_data_add_structure(vm_zone, data_zone, zone);
+       }
+
+       return;
+}
+
+static int vm_data_provider_get(const struct ast_data_search *search,
+       struct ast_data *data_root)
+{
+       struct ast_vm_user *user;
+
+       AST_LIST_LOCK(&users);
+       AST_LIST_TRAVERSE(&users, user, list) {
+               vm_users_data_provider_get_helper(search, data_root, user);
+       }
+       AST_LIST_UNLOCK(&users);
+
+       return 0;
+}
+
+static const struct ast_data_handler vm_data_provider = {
+       .version = AST_DATA_HANDLER_VERSION,
+       .get = vm_data_provider_get
+};
+
+static const struct ast_data_entry vm_data_providers[] = {
+       AST_DATA_ENTRY("asterisk/application/app_voicemail/voicemail", &vm_data_provider)
+};
+
 static void poll_subscribed_mailbox(struct mwi_sub *mwi_sub)
 {
        int new = 0, old = 0, urgent = 0;
@@ -12241,6 +12441,7 @@ static int unload_module(void)
        res |= ast_unregister_application(app4);
        res |= ast_custom_function_unregister(&mailbox_exists_acf);
        res |= ast_manager_unregister("VoicemailUsersList");
+       res |= ast_data_unregister(NULL);
 #ifdef TEST_FRAMEWORK
        res |= AST_TEST_UNREGISTER(test_voicemail_vmsayname);
        res |= AST_TEST_UNREGISTER(test_voicemail_msgcount);
@@ -12301,6 +12502,7 @@ static int load_module(void)
                return res;
 
        ast_cli_register_multiple(cli_voicemail, ARRAY_LEN(cli_voicemail));
+       ast_data_register_multiple(vm_data_providers, ARRAY_LEN(vm_data_providers));
 
        ast_install_vm_functions(has_voicemail, inboxcount, inboxcount2, messagecount, sayname);
        ast_realtime_require_field("voicemail", "uniqueid", RQ_UINTEGER3, 11, "password", RQ_CHAR, 10, SENTINEL);
index 53299eb..a9b47de 100644 (file)
@@ -137,6 +137,16 @@ Changes to manager version 1.1:
 
 * NEW ACTIONS
 -------------
+- Action: DataGet
+       Modules: data.c
+       Purpose:
+               To be able to retrieve the asterisk data tree.
+       Variables:
+         ActionID: <id>          Action ID for this transaction. Will be returned.
+         Path: <data path>       The path to the callback node to retrieve.
+         Filter: <filter>        Which nodes to retrieve.
+         Search: <search>        Search condition.
+
 - Action: IAXregistry
        Modules: chan_iax2
        Purpose:
index cb1b618..435ee09 100644 (file)
@@ -35,6 +35,7 @@ int astobj2_init(void);                       /*!< Provided by astobj2.c */
 int ast_file_init(void);               /*!< Provided by file.c */
 int ast_features_init(void);            /*!< Provided by features.c */
 void ast_autoservice_init(void);       /*!< Provided by autoservice.c */
+int ast_data_init(void);               /*!< Provided by data.c */
 int ast_http_init(void);               /*!< Provided by http.c */
 int ast_http_reload(void);             /*!< Provided by http.c */
 int ast_tps_init(void);                /*!< Provided by taskprocessor.c */
index 119dc42..b2cb8e5 100644 (file)
@@ -148,6 +148,7 @@ extern "C" {
 #include "asterisk/linkedlists.h"
 #include "asterisk/stringfields.h"
 #include "asterisk/datastore.h"
+#include "asterisk/data.h"
 #include "asterisk/channelstate.h"
 #include "asterisk/ccss.h"
 
@@ -2758,6 +2759,26 @@ void ast_channel_queue_redirecting_update(struct ast_channel *chan, const struct
  */
 int ast_channel_connected_line_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, const void *connected_info, int caller, int frame);
 
+/*!
+ * \brief Insert into an astdata tree, the channel structure.
+ * \param[in] tree The ast data tree.
+ * \param[in] chan The channel structure to add to tree.
+ * \retval <0 on error.
+ * \retval 0 on success.
+ */
+int ast_channel_data_add_structure(struct ast_data *tree, struct ast_channel *chan);
+
+/*!
+ * \brief Compare to channel structures using the data api.
+ * \param[in] tree The search tree generated by the data api.
+ * \param[in] chan The channel to compare.
+ * \param[in] structure_name The name of the node of the channel structure.
+ * \retval 0 The structure matches.
+ * \retval 1 The structure doesn't matches.
+ */
+int ast_channel_data_cmp_structure(const struct ast_data_search *tree, struct ast_channel *chan,
+       const char *structure_name);
+
 #include "asterisk/ccss.h"
 
 /*!
diff --git a/include/asterisk/data.h b/include/asterisk/data.h
new file mode 100644 (file)
index 0000000..cc92a0c
--- /dev/null
@@ -0,0 +1,788 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief Data retrieval API.
+ * \author Brett Bryant <brettbryant@gmail.com>
+ * \author Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
+ * \arg \ref AstDataRetrieval
+ */
+
+#ifndef ASTERISK_DATA_H
+#define ASTERISK_DATA_H
+
+/*!
+ * \page AstDataRetrieval The Asterisk DATA retrieval API.
+ *
+ * This module implements an abstraction for retrieving asterisk data and
+ * export it.
+ *
+ * \section USAGE
+ *
+ * \subsection Provider
+ *
+ * \b Register
+ *
+ * To register a callback use:
+ *
+ * \code
+ *     static const struct ast_data_handler callback_handler = {
+ *             .get = callback_handler_get_function,
+ *     };
+ *
+ *     ast_data_register("/node/path", &callback_handler);
+ * \endcode
+ *
+ * If you instead want to register multiple nodes at once use:
+ * \code
+ *     static const struct ast_data_handler handler_struct1 = {
+ *             .get = handler_callback_read,
+ *     };
+ *     ... other handlers ...
+ *
+ *     static const struct ast_data_entry list_providers[] = {
+ *             AST_DATA_ENTRY("/path1/node1", &handler_struct1),
+ *             AST_DATA_ENTRY("/path2/node2", &handler_struct2),
+ *             AST_DATA_ENTRY("/path3/node3", &handler_struct3),
+ *     };
+ *
+ *      ...
+ *
+ *     ast_data_register_multiple(list_providers, ARRAY_LEN(list_providers));
+ * \endcode
+ *
+ * \b Unregister
+ *
+ * To unregister a callback function already registered you can just call:
+ *
+ * \code
+ *     ast_data_unregister(NULL);
+ * \endcode
+ * And every node registered by the current module (file) will be unregistered.
+ * If you want to unregister a specific node use:
+ *
+ * \code
+ *     ast_data_unregister("/node/path");
+ * \endcode
+ *
+ * \b Implementation
+ *
+ * A simple callback function implementation:
+ *
+ * \code
+ *     #include <data.h>
+ *
+ *     struct test_structure {
+ *             int a;
+ *             double b;
+ *     };
+ *
+ *     DATA_EXPORT_TEST_STRUCTURE(MEMBER)                      \
+ *             MEMBER(test_structure, a, AST_DATA_INTEGER)     \
+ *             MEMBER(test_structure, b, AST_DATA_DOUBLE)
+ *
+ *     AST_DATA_STRUCTURE(test_structure, DATA_EXPORT_TEST_STRUCTURE)
+ *
+ *     static int my_callback_function(struct ast_data_search *search,
+ *             struct ast_data *root_node)
+ *     {
+ *             struct ast_data *internal_node;
+ *             struct test_structure ts = {
+ *                     .a = 10,
+ *                     .b = 20
+ *             };
+ *
+ *             if (ast_data_search_cmp_structure(search, test_structure, "test_node")) {
+ *                     return 0;
+ *             }
+ *
+ *             internal_node = ast_data_add_node(root_node, "test_node");
+ *             if (!internal_node) {
+ *                     return -1;
+ *             }
+ *
+ *             ast_data_add_structure(test_structure, internal_node, ts);
+ *
+ *             return 0;
+ *     }
+ *
+ * \endcode
+ *
+ * \subsection Get
+ *
+ * \b Getting \b the \b tree
+ *
+ * To get the tree you need to create a query, a query is based on three parameters
+ * a \b path to the provider, a \b search condition and a \b filter condition.
+ * \code
+ *     struct ast_data *result;
+ *     struct ast_data_query query = {
+ *             .path = "/asterisk/application/app_queue/queues",
+ *             .search = "/queues/queue/name=queue1",
+ *             .filter = "/queues/queue/name|wrapuptime|members/member/interface"
+ *     };
+ *
+ *     result = ast_data_get(&query);
+ * \endcode
+ *
+ * After using it you need to release the allocated memory of the returned tree:
+ * \code
+ *     ast_data_free(result);
+ * \endcode
+ *
+ * \b Iterate
+ *
+ * To retrieve nodes from the tree, it is possible to iterate through the returned
+ * nodes of the tree using:
+ * \code
+ *     struct ast_data_iterator *i;
+ *     struct ast_data *internal_node;
+ *
+ *     i = ast_data_iterator_init(result_tree, "path/node_name");
+ *     while ((internal_node = ast_data_iterator_next(i))) {
+ *             ... do something with node ...
+ *     }
+ *     ast_data_iterator_end(i);
+ * \endcode
+ * node_name is the name of the nodes to retrieve and path is the path to the internal
+ * nodes to retrieve (if needed).
+ *
+ * \b Retrieving
+ *
+ * After getting the node you where searching for, you will need to retrieve its value,
+ * to do that you may use one of the ast_data_retrieve_##type functions:
+ * \code
+ *     int a = ast_data_retrieve_int(tree, "path/to/the/node");
+ *     double b = ast_data_retrieve_dbl(tree, "path/to/the/node");
+ *     unsigned int c = ast_data_retrieve_bool(tree, "path/to/the/node");
+ *     char *d = ast_data_retrieve_string(tree, "path/to/the/node");
+ *     struct sockaddr_in e = ast_data_retrieve_ipaddr(tree, "path/to/the/node");
+ *     unsigned int f = ast_data_retrieve_uint(tree, "path/to/the/node");
+ *     void *g = ast_data_retrieve_ptr(tree, "path/to/the/node");
+ * \endcode
+ *
+ */
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+/*! \brief The data type of the data node. */
+enum ast_data_type {
+       AST_DATA_CONTAINER,
+       AST_DATA_INTEGER,
+       AST_DATA_UNSIGNED_INTEGER,
+       AST_DATA_DOUBLE,
+       AST_DATA_BOOLEAN,
+       AST_DATA_STRING,
+       AST_DATA_IPADDR,
+       AST_DATA_POINTER
+};
+
+/*! \brief The Data API structures version. */
+#define AST_DATA_HANDLER_VERSION 1
+#define AST_DATA_QUERY_VERSION  1
+
+/*! \brief opaque definition of an ast_data handler, a tree node. */
+struct ast_data;
+
+/*! \brief opaque definition of an ast_data_iterator handler. */
+struct ast_data_iterator;
+
+/*! \brief opaque definition of an ast_data_search structure. */
+struct ast_data_search;
+
+/*! \brief structure retrieved from a node, with the nodes content. */
+struct ast_data_retrieve {
+       /*! \brief The type of the node retrieved. */
+       enum ast_data_type type;
+
+       union {
+               char *AST_DATA_STRING;
+               int AST_DATA_INTEGER;
+               double AST_DATA_DOUBLE;
+               unsigned int AST_DATA_UNSIGNED_INTEGER;
+               unsigned int AST_DATA_BOOLEAN;
+               void *AST_DATA_POINTER;
+               struct in_addr AST_DATA_IPADDR;
+               void *AST_DATA_CONTAINER;
+       } value;
+};
+
+/*!
+ * \brief The get callback definition.
+ */
+typedef int (*ast_data_get_cb)(const struct ast_data_search *search,
+       struct ast_data *root);
+
+/*! \brief The structure of the node handler. */
+struct ast_data_handler {
+       /*! \brief Structure version. */
+       uint32_t version;
+       /*! \brief Data get callback implementation. */
+       ast_data_get_cb get;
+};
+
+/*! \brief This entries are for multiple registers. */
+struct ast_data_entry {
+       /*! \brief Path of the node to register. */
+       const char *path;
+       /*! \brief Data handler structure. */
+       const struct ast_data_handler *handler;
+};
+
+#define AST_DATA_ENTRY(__path, __handler) { .path = __path, .handler = __handler }
+
+/*! \brief A query to the data API is specified in this structure. */
+struct ast_data_query {
+       /*! \brief Data query version. */
+       uint32_t version;
+       /*! \brief Path to the node to retrieve. */
+       char *path;
+       /*! \brief Filter string, return the internal nodes specified here.
+        *         Setting it to NULL will return every internal node. */
+       char *filter;
+       /*! \brief Search condition. */
+       char *search;
+};
+
+/*! \brief Map the members of a structure. */
+struct ast_data_mapping_structure {
+       /*! \brief structure member name. */
+       const char *name;
+       /*! \brief structure member type. */
+       enum ast_data_type type;
+       /*! \brief member getter. */
+       union {
+               char *(*AST_DATA_STRING)(void *ptr);
+               int (*AST_DATA_INTEGER)(void *ptr);
+               double (*AST_DATA_DOUBLE)(void *ptr);
+               unsigned int (*AST_DATA_UNSIGNED_INTEGER)(void *ptr);
+               unsigned int (*AST_DATA_BOOLEAN)(void *ptr);
+               void *(*AST_DATA_POINTER)(void *ptr);
+               struct in_addr (*AST_DATA_IPADDR)(void *ptr);
+               void *(*AST_DATA_CONTAINER)(void *ptr);
+       } get;
+};
+
+/* Generate the structure and the functions to access the members of a structure. */
+#define AST_DATA_STRUCTURE(__struct, __name)                                                           \
+       __name(__AST_DATA_MAPPING_FUNCTION);                                                            \
+       static const struct ast_data_mapping_structure __data_mapping_structure_##__struct[] = {        \
+               __name(__AST_DATA_MAPPING_STRUCTURE)                                                    \
+       }
+
+/* Generate the structure to access the members and setup the pointer of the getter. */
+#define __AST_DATA_MAPPING_STRUCTURE(__structure, __member, __type)                            \
+       { .name = #__member, .get.__type = data_mapping_structure_get_##__structure##__member,  \
+       .type = __type },
+
+/* based on the data type, specifify the type of return value for the getter function. */
+#define __AST_DATA_MAPPING_FUNCTION_AST_DATA_STRING(__structure, __member)                             \
+       __AST_DATA_MAPPING_FUNCTION_TYPE(__structure, __member, AST_DATA_STRING, char *)
+#define __AST_DATA_MAPPING_FUNCTION_AST_DATA_INTEGER(__structure, __member)                            \
+       __AST_DATA_MAPPING_FUNCTION_TYPE(__structure, __member, AST_DATA_INTEGER, int)
+#define __AST_DATA_MAPPING_FUNCTION_AST_DATA_UNSIGNED_INTEGER(__structure, __member)                   \
+       __AST_DATA_MAPPING_FUNCTION_TYPE(__structure, __member, AST_DATA_UNSIGNED_INTEGER, unsigned int)
+#define __AST_DATA_MAPPING_FUNCTION_AST_DATA_BOOLEAN(__structure, __member)                            \
+       __AST_DATA_MAPPING_FUNCTION_TYPE(__structure, __member, AST_DATA_BOOLEAN, unsigned int)
+#define __AST_DATA_MAPPING_FUNCTION_AST_DATA_POINTER(__structure, __member)                            \
+       __AST_DATA_MAPPING_FUNCTION_TYPE(__structure, __member, AST_DATA_POINTER, void *)
+#define __AST_DATA_MAPPING_FUNCTION_AST_DATA_IPADDR(__structure, __member)                             \
+       __AST_DATA_MAPPING_FUNCTION_TYPE(__structure, __member, AST_DATA_IPADDR, struct in_addr)
+#define __AST_DATA_MAPPING_FUNCTION_AST_DATA_DOUBLE(__structure, __member)                             \
+       __AST_DATA_MAPPING_FUNCTION_TYPE(__structure, __member, AST_DATA_DBL, double)
+#define __AST_DATA_MAPPING_FUNCTION_AST_DATA_CONTAINER(__structure, __member)                          \
+       __AST_DATA_MAPPING_FUNCTION_TYPE(__structure, __member, AST_DATA_CONTAINER, void *)
+
+#define __AST_DATA_MAPPING_FUNCTION(__structure, __member, __type)             \
+       __AST_DATA_MAPPING_FUNCTION_##__type(__structure, __member)
+
+/* Create the function to retrieve a member of the structure. */
+#define __AST_DATA_MAPPING_FUNCTION_TYPE(__structure, __member, __type, __real_type)           \
+       static __real_type data_mapping_structure_get_##__structure##__member(void *ptr) {      \
+               struct __structure *struct_##__member = (struct __structure *) ptr;             \
+               return (__real_type) struct_##__member->__member;                               \
+       }
+
+/*!
+ * \brief Register a data provider.
+ * \param[in] path The path of the node to register.
+ * \param[in] handler The structure defining this node handler.
+ * \param[in] registrar Who is registering this node.
+ * \param[in] mod The module registering this handler.
+ * \see ast_data_unregister
+ * \retval <0 on error.
+ * \retval 0 on success.
+ * \see __ast_data_unregister, __ast_data_register_multiple
+ */
+int __ast_data_register(const char *path, const struct ast_data_handler *handler,
+       const char *registrar, struct ast_module *mod);
+#define ast_data_register(path, handler) __ast_data_register(path, handler, __FILE__, ast_module_info->self)
+#define ast_data_register_core(path, handler) __ast_data_register(path, handler, __FILE__, NULL)
+
+/*!
+ * \brief Register multiple data providers at once.
+ * \param[in] data_entries An array of data_entries structures.
+ * \param[in] entries The number of entries in the data_entries array.
+ * \param[in] registrar Who is registering this nodes.
+ * \param[in] mod The module registering this handlers.
+ * \retval <0 on error (none of the nodes are being registered on error).
+ * \retval 0 on success.
+ * \see __ast_data_register, __ast_data_unregister
+ */
+int __ast_data_register_multiple(const struct ast_data_entry *data_entries,
+       size_t entries, const char *registrar, struct ast_module *mod);
+#define ast_data_register_multiple(data_entries, entries) \
+       __ast_data_register_multiple(data_entries, entries, __FILE__, ast_module_info->self)
+#define ast_data_register_multiple_core(data_entries, entries) \
+       __ast_data_register_multiple(data_entries, entries, __FILE__, NULL)
+
+/*!
+ * \brief Unregister a data provider.
+ * \param[in] path Which node to unregister, if path is NULL unregister every node
+ *                 registered by the passed 'registrar'.
+ * \param[in] registrar Who is trying to unregister this node, only the owner (the
+ *                      one who registered the node) will be able to unregister it.
+ * \see ast_data_register
+ * \retval <0 on error.
+ * \retval 0 on success.
+ * \see __ast_data_register, __ast_data_register_multiple
+ */
+int __ast_data_unregister(const char *path, const char *registrar);
+#define ast_data_unregister(path) __ast_data_unregister(path, __FILE__)
+
+/*!
+ * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
+ *        current string value.
+ *        .search = "somename=somestring"
+ *        name = "somename"
+ *        value is the current value of something and will be evaluated against "somestring".
+ * \param[in] root The root node pointer of the search tree.
+ * \param[in] name The name of the specific.
+ * \param[in] value The value to compare.
+ * \returns The strcmp return value.
+ */
+int ast_data_search_cmp_string(const struct ast_data_search *root, const char *name, char *value);
+
+/*!
+ * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
+ *        current pointer address value.
+ *        .search = "something=0x32323232"
+ *        name = "something"
+ *        value is the current value of something and will be evaluated against "0x32323232".
+ * \param[in] root The root node pointer of the search tree.
+ * \param[in] name The name of the specific.
+ * \param[in] ptr The pointer address to compare.
+ * \returns The (value - current_value) result.
+ */
+int ast_data_search_cmp_ptr(const struct ast_data_search *root, const char *name,
+       void *ptr);
+
+/*!
+ * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
+ *        current ipv4 address value.
+ *        .search = "something=192.168.2.2"
+ *        name = "something"
+ *        value is the current value of something and will be evaluated against "192.168.2.2".
+ * \param[in] root The root node pointer of the search tree.
+ * \param[in] name The name of the specific.
+ * \param[in] addr The ipv4 address value to compare.
+ * \returns The (value - current_value) result.
+ */
+int ast_data_search_cmp_ipaddr(const struct ast_data_search *root, const char *name,
+       struct in_addr addr);
+
+/*!
+ * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
+ *        current double value.
+ *        .search = "something=222"
+ *        name = "something"
+ *        value is the current value of something and will be evaluated against "222".
+ * \param[in] root The root node pointer of the search tree.
+ * \param[in] name The name of the specific.
+ * \param[in] value The double value to compare.
+ * \returns The (value - current_value) result.
+ */
+int ast_data_search_cmp_dbl(const struct ast_data_search *root, const char *name,
+       double value);
+
+/*!
+ * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
+ *        current boolean value.
+ *        .search = "something=true"
+ *        name = "something"
+ *        value is the current value of something and will be evaluated against "true".
+ * \param[in] root The root node pointer of the search tree.
+ * \param[in] name The name of the specific.
+ * \param[in] value The boolean value to compare.
+ * \returns The (value - current_value) result.
+ */
+int ast_data_search_cmp_bool(const struct ast_data_search *root, const char *name,
+       unsigned int value);
+
+/*!
+ * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
+ *        current unsigned integer value.
+ *        .search = "something=10"
+ *        name = "something"
+ *        value is the current value of something and will be evaluated against "10".
+ * \param[in] root The root node pointer of the search tree.
+ * \param[in] name The name of the specific.
+ * \param[in] value The unsigned value to compare.
+ * \returns The strcmp return value.
+ */
+int ast_data_search_cmp_uint(const struct ast_data_search *root, const char *name,
+       unsigned int value);
+
+/*!
+ * \brief Based on a search tree, evaluate the specified 'name' inside the tree with the
+ *        current signed integer value.
+ *        .search = "something=10"
+ *        name = "something"
+ *        value is the current value of something and will be evaluated against "10".
+ * \param[in] root The root node pointer of the search tree.
+ * \param[in] name The name of the specific.
+ * \param[in] value The value to compare.
+ * \returns The strcmp return value.
+ */
+int ast_data_search_cmp_int(const struct ast_data_search *root, const char *name, int value);
+
+/*!
+ * \brief Based on a search tree, evaluate every member of a structure against it.
+ * \param[in] search The search tree.
+ * \param[in] mapping The structure mapping.
+ * \param[in] mapping_len The lenght of the structure mapping.
+ * \param[in] structure The structure pointer.
+ * \param[in] structure_name The name of the structure to compare.
+ * \retval 0 If the structure matches.
+ * \retval 1 If the structure doesn't match.
+ */
+int __ast_data_search_cmp_structure(const struct ast_data_search *search,
+       const struct ast_data_mapping_structure *mapping, size_t mapping_len,
+       void *structure, const char *structure_name);
+#define ast_data_search_cmp_structure(search, structure_name, structure, structure_name_cmp)           \
+       __ast_data_search_cmp_structure(search, __data_mapping_structure_##structure_name,              \
+       ARRAY_LEN(__data_mapping_structure_##structure_name), structure, structure_name_cmp)
+
+/*!
+ * \brief Check if there is a compare condition inside the search tree with the
+ *       passed 'compare_condition' node names.
+ * \param[in] search The search tree.
+ * \param[in] compare_condition The path of the compare condition.
+ * \retval 0 There is no compare condition.
+ * \retval 1 There is a compare condition.
+ */
+int ast_data_search_has_condition(const struct ast_data_search *search,
+       const char *compare_condition);
+
+/*!
+ * \brief Retrieve a subtree from the asterisk data API.
+ * \param[in] query The query structure specifying what nodes to retrieve.
+ * \retval NULL on error.
+ * \retval non-NULL The dynamically allocated requested sub-tree (it needs to be
+ *         released using ast_data_free.
+ * \see ast_data_free, ast_data_get_xml
+ */
+struct ast_data *ast_data_get(const struct ast_data_query *query);
+
+/*!
+ * \brief Retrieve a subtree from the asterisk data API in XML format..
+ * \param[in] query The query structure specifying what nodes to retrieve.
+ * \retval NULL on error.
+ * \retval non-NULL The dynamically allocated requested sub-tree (it needs to be
+ *         released using ast_data_free.
+ * \see ast_data_free, ast_data_get
+ */
+struct ast_xml_doc *ast_data_get_xml(const struct ast_data_query *query);
+
+/*!
+ * \brief Release the allocated memory of a tree.
+ * \param[in] root The sub-tree pointer returned by a call to ast_data_get.
+ * \see ast_data_get
+ */
+void ast_data_free(struct ast_data *root);
+
+/*!
+ * \brief Get a node type.
+ * \param[in] res A pointer to the ast_data result set.
+ * \param[in] path A path to the node to get the type.
+ * \return The type of the requested node type.
+ */
+enum ast_data_type ast_data_retrieve_type(struct ast_data *res, const char *path);
+
+/*!
+ * \brief Get the node name.
+ * \param[in] node The node pointer.
+ * \returns The node name.
+ */
+char *ast_data_retrieve_name(struct ast_data *node);
+
+/*!
+ * \brief Add a container child.
+ * \param[in] root The root of the ast_data to insert into.
+ * \param[in] childname The name of the child element to be added.
+ * \retval NULL on error (memory exhaustion only).
+ * \retval non-NULL a newly allocated node.
+ */
+struct ast_data *ast_data_add_node(struct ast_data *root, const char *childname);
+
+/*!
+ * \brief Add an integer node type.
+ * \param[in] root The root of the ast_data to insert into.
+ * \param[in] childname The name of the child element to be added.
+ * \param[in] value The value for the new node.
+ * \retval NULL on error (memory exhaustion only).
+ * \retval non-NULL a newly allocated node.
+ */
+struct ast_data *ast_data_add_int(struct ast_data *root, const char *childname,
+       int value);
+
+/*!
+ * \brief Add an unsigned integer node type.
+ * \param[in] root The root of the ast_data to insert into.
+ * \param[in] childname The name of the child element to be added.
+ * \param[in] value The value for the new node.
+ * \retval NULL on error (memory exhaustion only).
+ * \retval non-NULL a newly allocated node.
+ */
+struct ast_data *ast_data_add_uint(struct ast_data *root, const char *childname,
+       unsigned int value);
+
+/*!
+ * \brief Add a floating point node type.
+ * \param[in] root The root of the ast_data to insert into.
+ * \param[in] childname The name of the child element to be added.
+ * \param[in] dbl The value for the new node.
+ * \retval NULL on error (memory exhaustion only).
+ * \retval non-NULL a newly allocated node.
+ */
+struct ast_data *ast_data_add_dbl(struct ast_data *root, const char *childname,
+       double dbl);
+/*!
+ * \brief Add a ipv4 address type.
+ * \param[in] root The root of the ast_data to insert into.
+ * \param[in] childname The name of the child element to be added.
+ * \param[in] addr The ipv4 address value.
+ * \retval NULL on error (memory exhaustion only).
+ * \retval non-NULL a newly allocated node.
+ */
+struct ast_data *ast_data_add_ipaddr(struct ast_data *root, const char *childname,
+       struct in_addr addr);
+
+/*!
+ * \brief Add a ptr node type.
+ * \param[in] root The root of the ast_data to insert into.
+ * \param[in] childname The name of the child element to be added.
+ * \param[in] ptr The pointer value to add.
+ * \retval NULL on error (memory exhaustion only).
+ * \retval non-NULL a newly allocated node.
+ */
+struct ast_data *ast_data_add_ptr(struct ast_data *root, const char *childname,
+       void *ptr);
+
+/*!
+ * \brief Add a string node type.
+ * \param[in] root The root of the ast_data to insert into.
+ * \param[in] childname The name of the child element to be added.
+ * \param[in] value The value for the new node.
+ * \retval NULL on error (memory exhaustion only).
+ * \retval non-NULL a newly allocated node.
+ */
+struct ast_data *ast_data_add_str(struct ast_data *root, const char *childname,
+       const char *string);
+
+/*!
+ * \brief Add a boolean node type.
+ * \param[in] root The root of the ast_data to insert into.
+ * \param[in] childname The name of the child element to be added.
+ * \param[in] value The value for the new node.
+ * \retval NULL on error (memory exhaustion only).
+ * \retval non-NULL a newly allocated node.
+ */
+struct ast_data *ast_data_add_bool(struct ast_data *root, const char *childname,
+       unsigned int boolean);
+
+/*!
+ * \brief Add a complete structure to a node.
+ * \param[in] root Where to add the structure.
+ * \param[in] mapping The structure mapping array.
+ * \param[in] mapping_len The lenght of the mapping array.
+ * \param[in] structure The structure pointer.
+ * \retval 0 on success.
+ * \retval 1 on error.
+ */
+int __ast_data_add_structure(struct ast_data *root,
+       const struct ast_data_mapping_structure *mapping,
+       size_t mapping_len, void *structure);
+#define ast_data_add_structure(structure_name, root, structure)                                \
+       __ast_data_add_structure(root, __data_mapping_structure_##structure_name,       \
+               ARRAY_LEN(__data_mapping_structure_##structure_name), structure)
+
+/*!
+ * \brief Remove a node that was added using ast_data_add_
+ * \param[in] root The root node of the node to be removed.
+ * \param[in] child The node pointer to remove.
+ */
+void ast_data_remove_node(struct ast_data *root, struct ast_data *child);
+
+/*!
+ * \brief Initialize an iterator.
+ * \param[in] tree The returned tree by a call to ast_data_get.
+ * \param[in] elements Which elements to iterate through.
+ * \retval NULL on error.
+ * \retval non-NULL A dinamically allocated iterator structure.
+ */
+struct ast_data_iterator *ast_data_iterator_init(struct ast_data *tree,
+       const char *elements);
+
+/*!
+ * \brief Release (stop using) an iterator.
+ * \param[in] iterator The iterator created by ast_data_iterator_start.
+ * \see ast_data_iterator_start
+ */
+void ast_data_iterator_end(struct ast_data_iterator *iterator);
+
+/*!
+ * \brief Get the next node of the tree.
+ * \param[in] iterator The iterator structure returned by ast_data_iterator_start.
+ * \retval NULL when no more nodes to return.
+ * \retval non-NULL A node of the ast_data tree.
+ * \see ast_data_iterator_start, ast_data_iterator_stop
+ */
+struct ast_data *ast_data_iterator_next(struct ast_data_iterator *iterator);
+
+/*!
+ * \brief Retrieve a value from a node in the tree.
+ * \param[in] tree The structure returned by a call to ast_data_get.
+ * \param[in] path The path to the node.
+ * \param[out] content The node content.
+ * \retval 0 on success.
+ * \retval <0 on error.
+ */
+int ast_data_retrieve(struct ast_data *tree, const char *path, struct ast_data_retrieve *content);
+
+/*!
+ * \brief Retrieve the integer value of a node.
+ * \param[in] tree The tree from where to get the value.
+ * \param[in] path The node name or path.
+ * \returns The value of the node.
+ */
+static inline int ast_data_retrieve_int(struct ast_data *tree, const char *path)
+{
+       struct ast_data_retrieve ret;
+
+       ast_data_retrieve(tree, path, &ret);
+
+       return ret.value.AST_DATA_INTEGER;
+}
+
+/*!
+ * \brief Retrieve the boolean value of a node.
+ * \param[in] tree The tree from where to get the value.
+ * \param[in] path The node name or path.
+ * \returns The value of the node.
+ */
+static inline unsigned int ast_data_retrieve_bool(struct ast_data *tree, const char *path)
+{
+       struct ast_data_retrieve ret;
+
+       ast_data_retrieve(tree, path, &ret);
+
+       return ret.value.AST_DATA_BOOLEAN;
+}
+
+/*!
+ * \brief Retrieve the unsigned integer value of a node.
+ * \param[in] tree The tree from where to get the value.
+ * \param[in] path The node name or path.
+ * \returns The value of the node.
+ */
+static inline unsigned int ast_data_retrieve_uint(struct ast_data *tree, const char *path)
+{
+       struct ast_data_retrieve ret;
+
+       ast_data_retrieve(tree, path, &ret);
+
+       return ret.value.AST_DATA_UNSIGNED_INTEGER;
+}
+
+/*!
+ * \brief Retrieve the string value of a node.
+ * \param[in] tree The tree from where to get the value.
+ * \param[in] path The node name or path.
+ * \returns The value of the node.
+ */
+static inline const char *ast_data_retrieve_string(struct ast_data *tree, const char *path)
+{
+       struct ast_data_retrieve ret;
+
+       ast_data_retrieve(tree, path, &ret);
+
+       return ret.value.AST_DATA_STRING;
+}
+
+/*!
+ * \brief Retrieve the ptr value of a node.
+ * \param[in] tree The tree from where to get the value.
+ * \param[in] path The node name or path.
+ * \returns The value of the node.
+ */
+static inline void *ast_data_retrieve_ptr(struct ast_data *tree, const char *path)
+{
+       struct ast_data_retrieve ret;
+
+       ast_data_retrieve(tree, path, &ret);
+
+       return ret.value.AST_DATA_POINTER;
+}
+
+/*!
+ * \brief Retrieve the double value of a node.
+ * \param[in] tree The tree from where to get the value.
+ * \param[in] path The node name or path.
+ * \returns The value of the node.
+ */
+static inline double ast_data_retrieve_dbl(struct ast_data *tree, const char *path)
+{
+       struct ast_data_retrieve ret;
+
+       ast_data_retrieve(tree, path, &ret);
+
+       return ret.value.AST_DATA_DOUBLE;
+}
+
+/*!
+ * \brief Retrieve the ipv4 address value of a node.
+ * \param[in] tree The tree from where to get the value.
+ * \param[in] path The node name or path.
+ * \returns The value of the node.
+ */
+static inline struct in_addr ast_data_retrieve_ipaddr(struct ast_data *tree, const char *path)
+{
+       struct ast_data_retrieve ret;
+
+       ast_data_retrieve(tree, path, &ret);
+
+       return ret.value.AST_DATA_IPADDR;
+}
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif /* ASTERISK_DATA_H */
index 5e24274..43186b2 100644 (file)
@@ -90,6 +90,7 @@
  * \arg \ref AstThreadStorage
  * \arg \ref DataStores
  * \arg \ref AstExtState
+ * \arg \ref AstDataRetrieval
  *
  * \subsection model_txt Generic Model
  * \verbinclude model.txt
index 2c30986..836a4f3 100644 (file)
 #define _ASTERISK_XML_H
 
 /*! \file
- *  \brief Asterisk XML abstraction layer
+ * \brief Asterisk XML abstraction layer
  */
 
 struct ast_xml_node;
 struct ast_xml_doc;
 
-/*! \brief Initialize the XML library implementation.
+/*!
+ * \brief Initialize the XML library implementation.
  *         This function is used to setup everything needed
  *         to start working with the xml implementation.
- *  \retval 0 On success.
- *  \retval 1 On error.
+ * \retval 0 On success.
+ * \retval 1 On error.
  */
 int ast_xml_init(void);
 
-/*! \brief Cleanup library allocated global data.
- *  \retval 0 On success.
- *  \retval 1 On error.
+/*!
+ * \brief Cleanup library allocated global data.
+ * \retval 0 On success.
+ * \retval 1 On error.
  */
 int ast_xml_finish(void);
 
-/*! \brief Open an XML document.
- *  \param filename Document path.
- *  \retval NULL on error.
- *  \retval The ast_xml_doc reference to the open document.
+/*!
+ * \brief Open an XML document.
+ * \param filename Document path.
+ * \retval NULL on error.
+ * \retval The ast_xml_doc reference to the open document.
  */
 struct ast_xml_doc *ast_xml_open(char *filename);
 
+/*!
+ * \brief Create a XML document.
+ * \retval NULL on error.
+ * \retval non-NULL The allocated document structure.
+ */
+struct ast_xml_doc *ast_xml_new(void);
+
+/*!
+ * \brief Create a XML node.
+ * \param name The name of the node to be created.
+ * \retval NULL on error.
+ * \retval non-NULL The allocated node structe.
+ */
+struct ast_xml_node *ast_xml_new_node(const char *name);
+
+/*!
+ * \brief Add a child node inside a passed parent node.
+ * \param parent The pointer of the parent node.
+ * \param child_name The name of the child node to add.
+ * \retval NULL on error.
+ * \retval non-NULL The created child node pointer.
+ */
+struct ast_xml_node *ast_xml_new_child(struct ast_xml_node *parent, const char *child_name);
+
+/*!
+ * \brief Add a child node, to a specified parent node.
+ * \param parent Where to add the child node.
+ * \param child The child node to add.
+ * \retval NULL on error.
+ * \retval non-NULL The add child node on success.
+ */
+struct ast_xml_node *ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child);
+
+/*!
+ * \brief Close an already open document and free the used
+ *        structure.
+ * \retval doc The document reference.
+ */
+void ast_xml_close(struct ast_xml_doc *doc);
+
 /*! \brief Open an XML document that resides in memory.
  * \param buffer The address where the document is stored
  * \size The number of bytes in the document
@@ -53,76 +96,117 @@ struct ast_xml_doc *ast_xml_open(char *filename);
  */
 struct ast_xml_doc *ast_xml_read_memory(char *buffer, size_t size);
 
-/*! \brief Close an already open document and free the used
- *        structure.
- *  \retval doc The document reference.
+/*!
+ * \brief Specify the root node of a XML document.
+ * \param doc The document pointer.
+ * \param node A pointer to the node we want to set as root node.
  */
-void ast_xml_close(struct ast_xml_doc *doc);
+void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node);
 
-/*! \brief Get the document root node.
- *  \param doc Document reference
- *  \retval NULL on error
- *  \retval The root node on success.
+/*!
+ * \brief Get the document root node.
+ * \param doc Document reference
+ * \retval NULL on error
+ * \retval The root node on success.
  */
 struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc);
 
-/*! \brief Free node
- *  \param node Node to be released.
+/*!
+ * \brief Free node
+ * \param node Node to be released.
  */
 void ast_xml_free_node(struct ast_xml_node *node);
 
-/*! \brief Free an attribute returned by ast_xml_get_attribute()
- *  \param data pointer to be freed.
+/*!
+ * \brief Free an attribute returned by ast_xml_get_attribute()
+ * \param data pointer to be freed.
  */
 void ast_xml_free_attr(const char *attribute);
 
-/*! \brief Free a content element that was returned by ast_xml_get_text()
- *  \param text text to be freed.
+/*!
+ * \brief Get the document based on a node.
+ * \param node A node that is part of the dom.
+ * \returns The dom pointer where this node resides.
+ */
+struct ast_xml_doc *ast_xml_get_doc(struct ast_xml_node *node);
+
+/*!
+ * \brief Free a content element that was returned by ast_xml_get_text()
+ * \param text text to be freed.
  */
 void ast_xml_free_text(const char *text);
 
-/*! \brief Get a node attribute by name
- *  \param node Node where to search the attribute.
- *  \param attrname Attribute name.
- *  \retval NULL on error
- *  \retval The attribute value on success.
+/*!
+ * \brief Get a node attribute by name
+ * \param node Node where to search the attribute.
+ * \param attrname Attribute name.
+ * \retval NULL on error
+ * \retval The attribute value on success.
  */
 const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname);
 
-/*! \brief Find a node element by name.
- *  \param node This is the node starting point.
- *  \param name Node name to find.
- *  \param attrname attribute name to match (if NULL it won't be matched).
- *  \param attrvalue attribute value to match (if NULL it won't be matched).
- *  \retval NULL if not found
- *  \retval The node on success.
+/*!
+ * \brief Set an attribute to a node.
+ * \param node In which node we want to insert the attribute.
+ * \param name The attribute name.
+ * \param value The attribute value.
+ * \retval 0 on success.
+ * \retval -1 on error.
+ */
+int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value);
+
+/*!
+ * \brief Find a node element by name.
+ * \param node This is the node starting point.
+ * \param name Node name to find.
+ * \param attrname attribute name to match (if NULL it won't be matched).
+ * \param attrvalue attribute value to match (if NULL it won't be matched).
+ * \retval NULL if not found
+ * \retval The node on success.
  */
 struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue);
 struct ast_xml_ns *ast_xml_find_namespace(struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name);
 const char *ast_xml_get_ns_href(struct ast_xml_ns *ns);
 
-/*! \brief Get an element content string.
- *  \param node Node from where to get the string.
- *  \retval NULL on error.
- *  \retval The text content of node.
+/*!
+ * \brief Get an element content string.
+ * \param node Node from where to get the string.
+ * \retval NULL on error.
+ * \retval The text content of node.
  */
 const char *ast_xml_get_text(struct ast_xml_node *node);
 
-/*! \brief Get the name of a node. */
+/*!
+ * \brief Set an element content string.
+ * \param node Node from where to set the content string.
+ * \param content The text to insert in the node.
+ */
+void ast_xml_set_text(struct ast_xml_node *node, const char *content);
+
+/*!
+ * \brief Get the name of a node. */
 const char *ast_xml_node_get_name(struct ast_xml_node *node);
 
-/*! \brief Get the node's children. */
+/*!
+ * \brief Get the node's children. */
 struct ast_xml_node *ast_xml_node_get_children(struct ast_xml_node *node);
 
-/*! \brief Get the next node in the same level. */
+/*!
+ * \brief Get the next node in the same level. */
 struct ast_xml_node *ast_xml_node_get_next(struct ast_xml_node *node);
 
-/*! \brief Get the previous node in the same leve. */
+/*!
+ * \brief Get the previous node in the same leve. */
 struct ast_xml_node *ast_xml_node_get_prev(struct ast_xml_node *node);
 
-/*! \brief Get the parent of a specified node. */
+/*!
+ * \brief Get the parent of a specified node. */
 struct ast_xml_node *ast_xml_node_get_parent(struct ast_xml_node *node);
 
+/*!
+ * \brief Dump the specified document to a file. */
+int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc);
+
 /* Features using ast_xml_ */
 #ifdef HAVE_LIBXML2
 #define AST_XML_DOCS
index 57ccc3f..6751456 100644 (file)
@@ -3611,6 +3611,12 @@ int main(int argc, char *argv[])
        ast_xmldoc_load_documentation();
 #endif
 
+       /* initialize the data retrieval API */
+       if (ast_data_init()) {
+               printf ("%s", term_quit());
+               exit(1);
+       }
+
        ast_channels_init();
 
        if ((moduleresult = load_modules(1))) {         /* Load modules, pre-load only */
index 03c657c..dfa61b2 100644 (file)
@@ -65,6 +65,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/timing.h"
 #include "asterisk/autochan.h"
 #include "asterisk/stringfields.h"
+#include "asterisk/data.h"
 
 #ifdef HAVE_EPOLL
 #include <sys/epoll.h>
@@ -133,6 +134,52 @@ static AST_RWLIST_HEAD_STATIC(backends, chanlist);
 #define NUM_CHANNEL_BUCKETS 1567
 #endif
 
+#define DATA_EXPORT_CHANNEL(MEMBER)                                            \
+       MEMBER(ast_channel, blockproc, AST_DATA_STRING)                         \
+       MEMBER(ast_channel, appl, AST_DATA_STRING)                              \
+       MEMBER(ast_channel, data, AST_DATA_STRING)                              \
+       MEMBER(ast_channel, name, AST_DATA_STRING)                              \
+       MEMBER(ast_channel, language, AST_DATA_STRING)                          \
+       MEMBER(ast_channel, musicclass, AST_DATA_STRING)                        \
+       MEMBER(ast_channel, accountcode, AST_DATA_STRING)                       \
+       MEMBER(ast_channel, peeraccount, AST_DATA_STRING)                       \
+       MEMBER(ast_channel, userfield, AST_DATA_STRING)                         \
+       MEMBER(ast_channel, call_forward, AST_DATA_STRING)                      \
+       MEMBER(ast_channel, uniqueid, AST_DATA_STRING)                          \
+       MEMBER(ast_channel, linkedid, AST_DATA_STRING)                          \
+       MEMBER(ast_channel, parkinglot, AST_DATA_STRING)                        \
+       MEMBER(ast_channel, hangupsource, AST_DATA_STRING)                      \
+       MEMBER(ast_channel, dialcontext, AST_DATA_STRING)                       \
+       MEMBER(ast_channel, _softhangup, AST_DATA_INTEGER)                      \
+       MEMBER(ast_channel, streamid, AST_DATA_INTEGER)                         \
+       MEMBER(ast_channel, vstreamid, AST_DATA_INTEGER)                        \
+       MEMBER(ast_channel, oldwriteformat, AST_DATA_INTEGER)                   \
+       MEMBER(ast_channel, _state, AST_DATA_INTEGER)                           \
+       MEMBER(ast_channel, rings, AST_DATA_INTEGER)                            \
+       MEMBER(ast_channel, priority, AST_DATA_INTEGER)                         \
+       MEMBER(ast_channel, macropriority, AST_DATA_INTEGER)                    \
+       MEMBER(ast_channel, amaflags, AST_DATA_INTEGER)                         \
+       MEMBER(ast_channel, adsicpe, AST_DATA_INTEGER)                          \
+       MEMBER(ast_channel, fin, AST_DATA_UNSIGNED_INTEGER)                     \
+       MEMBER(ast_channel, fout, AST_DATA_UNSIGNED_INTEGER)                    \
+       MEMBER(ast_channel, hangupcause, AST_DATA_INTEGER)                      \
+       MEMBER(ast_channel, flags, AST_DATA_UNSIGNED_INTEGER)                   \
+       MEMBER(ast_channel, nativeformats, AST_DATA_INTEGER)                    \
+       MEMBER(ast_channel, readformat, AST_DATA_INTEGER)                       \
+       MEMBER(ast_channel, writeformat, AST_DATA_INTEGER)                      \
+       MEMBER(ast_channel, rawreadformat, AST_DATA_INTEGER)                    \
+       MEMBER(ast_channel, rawwriteformat, AST_DATA_INTEGER)                   \
+       MEMBER(ast_channel, emulate_dtmf_duration, AST_DATA_UNSIGNED_INTEGER)   \
+       MEMBER(ast_channel, visible_indication, AST_DATA_INTEGER)               \
+       MEMBER(ast_channel, transfercapability, AST_DATA_INTEGER)               \
+       MEMBER(ast_channel, context, AST_DATA_STRING)                           \
+       MEMBER(ast_channel, exten, AST_DATA_STRING)                             \
+       MEMBER(ast_channel, macrocontext, AST_DATA_STRING)                      \
+       MEMBER(ast_channel, macroexten, AST_DATA_STRING)
+
+AST_DATA_STRUCTURE(ast_channel, DATA_EXPORT_CHANNEL);
+
+
 /*! \brief All active channels on the system */
 static struct ao2_container *channels;
 
@@ -211,6 +258,17 @@ struct ast_variable *ast_channeltype_list(void)
        return var;
 }
 
+int ast_channel_data_add_structure(struct ast_data *tree, struct ast_channel *chan)
+{
+       return ast_data_add_structure(ast_channel, tree, chan);
+}
+
+int ast_channel_data_cmp_structure(const struct ast_data_search *tree,
+       struct ast_channel *chan, const char *structure_name)
+{
+       return ast_data_search_cmp_structure(tree, ast_channel, chan, structure_name);
+}
+
 /*! \brief Show channel types - CLI command */
 static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
@@ -6422,12 +6480,88 @@ static int ast_channel_hash_cb(const void *obj, const int flags)
        return ast_str_case_hash(chan->name);
 }
 
+/*!
+ * \internal
+ * \brief Implements the channels provider.
+ */
+static int data_channels_provider_handler(const struct ast_data_search *search,
+       struct ast_data *root)
+{
+       struct ast_channel *c, *bc;
+       struct ast_channel_iterator *iter = NULL;
+       struct ast_data *data_channel, *data_bridged;
+       int channel_match, bridged_match;
+
+       channel_match = ast_data_search_has_condition(search,
+                       "channel");
+       bridged_match = ast_data_search_has_condition(search,
+                       "channel/bridged");
+
+       for (iter = ast_channel_iterator_all_new();
+               iter && (c = ast_channel_iterator_next(iter)); ast_channel_unref(c)) {
+               ast_channel_lock(c);
+
+               if (channel_match &&
+                       ast_channel_data_cmp_structure(search, c, "channel")) {
+                       ast_channel_unlock(c);
+                       continue;
+               }
+
+               bc = ast_bridged_channel(c);
+
+               if (bridged_match && bc &&
+                       ast_channel_data_cmp_structure(search, bc, "channel/bridged")) {
+                       ast_channel_unlock(c);
+                       continue;
+               }
+
+               data_channel = ast_data_add_node(root, "channel");
+               if (!data_channel) {
+                       ast_channel_unlock(c);
+                       continue;
+               }
+
+               ast_channel_data_add_structure(data_channel, c);
+
+               if (bc) {
+                       data_bridged = ast_data_add_node(data_channel, "bridged");
+                       if (!data_bridged) {
+                               ast_channel_unlock(c);
+                               continue;
+                       }
+                       ast_channel_data_add_structure(data_bridged, bc);
+               }
+
+               ast_channel_unlock(c);
+       }
+       if (iter) {
+               ast_channel_iterator_destroy(iter);
+       }
+
+       return 0;
+}
+
+/*!
+ * \internal
+ * \brief /asterisk/core/channels provider.
+ */
+static const struct ast_data_handler channels_provider = {
+       .version = AST_DATA_HANDLER_VERSION,
+       .get = data_channels_provider_handler
+};
+
+static const struct ast_data_entry channel_providers[] = {
+       AST_DATA_ENTRY("/asterisk/core/channels", &channels_provider),
+};
+
 void ast_channels_init(void)
 {
        channels = ao2_container_alloc(NUM_CHANNEL_BUCKETS,
                        ast_channel_hash_cb, ast_channel_cmp_cb);
 
        ast_cli_register_multiple(cli_channel, ARRAY_LEN(cli_channel));
+
+       ast_data_register_multiple_core(channel_providers, ARRAY_LEN(channel_providers));
 }
 
 /*! \brief Print call group and pickup group ---*/
diff --git a/main/data.c b/main/data.c
new file mode 100644 (file)
index 0000000..1b12d45
--- /dev/null
@@ -0,0 +1,2879 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Data retrieval API.
+ *
+ * \author Brett Bryant <brettbryant@gmail.com>
+ * \author Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/_private.h"
+
+#include <regex.h>
+
+#include "asterisk/module.h"
+#include "asterisk/utils.h"
+#include "asterisk/lock.h"
+#include "asterisk/data.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/xml.h"
+#include "asterisk/cli.h"
+#include "asterisk/term.h"
+#include "asterisk/manager.h"
+#include "asterisk/test.h"
+
+/*** DOCUMENTATION
+       <manager name="DataGet" language="en_US">
+               <synopsis>
+                       Retrieve the data api tree.
+               </synopsis>
+               <syntax>
+                       <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
+                       <parameter name="Path" required="true" />
+                       <parameter name="Search" />
+                       <parameter name="Filter" />
+               </syntax>
+               <description>
+                       <para>Retrieve the data api tree.</para>
+               </description>
+       </manager>
+ ***/
+
+#define NUM_DATA_NODE_BUCKETS  59
+#define NUM_DATA_RESULT_BUCKETS 59
+#define NUM_DATA_SEARCH_BUCKETS 59
+#define NUM_DATA_FILTER_BUCKETS 59
+
+/*! \brief The last compatible version. */
+static const uint32_t latest_handler_compatible_version = 0;
+
+/*! \brief The last compatible version. */
+static const uint32_t latest_query_compatible_version = 0;
+
+/*! \brief Current handler structure version. */
+static const uint32_t current_handler_version = AST_DATA_HANDLER_VERSION;
+
+/*! \brief Current query structure version. */
+static const uint32_t current_query_version = AST_DATA_QUERY_VERSION;
+
+/*! \brief The data tree to be returned by the callbacks and
+          managed by functions local to this file. */
+struct ast_data {
+       enum ast_data_type type;
+
+       /*! \brief The node content. */
+       union {
+               int32_t sint;
+               uint32_t uint;
+               double dbl;
+               unsigned int boolean:1;
+               char *str;
+               struct in_addr ipaddr;
+               void *ptr;
+       } payload;
+
+       /*! \brief The filter node that depends on the current node,
+        * this is used only when creating the result tree. */
+       const struct data_filter *filter;
+
+       /*! \brief The list of nodes inside this node. */
+       struct ao2_container *children;
+       /*! \brief The name of the node. */
+       char name[0];
+};
+
+/*! \brief Type of comparisons allow in the search string. */
+enum data_search_comparison {
+       DATA_CMP_UNKNOWN,
+       DATA_CMP_EQ,    /* =  */
+       DATA_CMP_NEQ,   /* != */
+       DATA_CMP_GT,    /* >  */
+       DATA_CMP_GE,    /* >= */
+       DATA_CMP_LT,    /* <  */
+       DATA_CMP_LE     /* <= */
+};
+
+/*! \brief The list of nodes with their search requirement. */
+struct ast_data_search {
+       /*! \brief The value of the comparison. */
+       char *value;
+       /*! \brief The type of comparison. */
+       enum data_search_comparison cmp_type;
+       /*! \brief reference another node. */
+       struct ao2_container *children;
+       /*! \brief The name of the node we are trying to compare. */
+       char name[0];
+};
+
+struct data_filter;
+
+/*! \brief The filter node. */
+struct data_filter {
+       /*! \brief node childrens. */
+       struct ao2_container *children;
+       /*! \brief glob list */
+       AST_LIST_HEAD_NOLOCK(glob_list_t, data_filter) glob_list;
+       /*! \brief glob list entry */
+       AST_LIST_ENTRY(data_filter) list;
+       /*! \brief node name. */
+       char name[0];
+};
+
+/*! \brief A data container node pointing to the registered handler. */
+struct data_provider {
+       /*! \brief node content handler. */
+       const struct ast_data_handler *handler;
+       /*! \brief Module providing this handler. */
+       struct ast_module *module;
+       /*! \brief children nodes. */
+       struct ao2_container *children;
+       /*! \brief Who registered this node. */
+       const char *registrar;
+       /*! \brief Node name. */
+       char name[0];
+};
+
+/*! \brief This structure is used by the iterator. */
+struct ast_data_iterator {
+       /*! \brief The internal iterator. */
+       struct ao2_iterator internal_iterator;
+       /*! \brief The last returned node. */
+       struct ast_data *last;
+       /*! \brief The iterator pattern. */
+       const char *pattern;
+       /*! \brief The compiled patter. */
+       regex_t regex_pattern;
+       /*! \brief is a regular expression. */
+       unsigned int is_pattern:1;
+};
+
+struct {
+       /*! \brief The asterisk data main content structure. */
+       struct ao2_container *container;
+       /*! \brief asterisk data locking mechanism. */
+       ast_rwlock_t lock;
+} root_data;
+
+static void __data_result_print_cli(int fd, const struct ast_data *root, uint32_t depth);
+
+/*!
+ * \internal
+ * \brief Common string hash function.
+ * \see ast_data_init
+ */
+static int data_provider_hash(const void *obj, const int flags)
+{
+       const struct data_provider *node = obj;
+       return ast_str_case_hash(node->name);
+}
+
+/*!
+ * \internal
+ * \brief Compare two data_provider's.
+ * \see ast_data_init
+ */
+static int data_provider_cmp(void *obj1, void *obj2, int flags)
+{
+       struct data_provider *node1 = obj1, *node2 = obj2;
+       return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
+}
+
+/*!
+ * \internal
+ * \brief Common string hash function for data nodes
+ */
+static int data_result_hash(const void *obj, const int flags)
+{
+       const struct ast_data *node = obj;
+       return ast_str_hash(node->name);
+}
+
+/*!
+ * \internal
+ * \brief Common string comparison function
+ */
+static int data_result_cmp(void *obj, void *arg, int flags)
+{
+       struct ast_data *node1 = obj, *node2 = arg;
+       return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
+}
+
+/*!
+ * \internal
+ * \brief Lock the data registered handlers structure for writing.
+ * \see data_unlock
+ */
+#define data_write_lock() ast_rwlock_wrlock(&root_data.lock)
+
+/*!
+ * \internal
+ * \brief Lock the data registered handlers structure for reading.
+ * \see data_unlock
+ */
+#define data_read_lock() ast_rwlock_rdlock(&root_data.lock)
+
+/*!
+ * \internal
+ * \brief Unlock the data registered handlers structure.
+ */
+#define data_unlock() ast_rwlock_unlock(&root_data.lock)
+
+/*!
+ * \internal
+ * \brief Check if a version is compatible with the current core.
+ * \param[in] structure_version The current structure version.
+ * \param[in] latest_compatible The latest compatible version.
+ * \param[in] current The current Data API version.
+ * \retval 1 If the module is compatible.
+ * \retval 0 If the module is NOT compatible.
+ */
+static int data_structure_compatible(int structure_version, uint32_t latest_compatible,
+       uint32_t current)
+{
+       if (structure_version >= latest_compatible && structure_version <= current) {
+               return 1;
+       }
+
+       ast_log(LOG_ERROR, "A module is not compatible with the"
+               "current data api version\n");
+
+       return 0;
+}
+
+/*!
+ * \internal
+ * \brief Get the next node name in a path (/node1/node2)
+ *        Avoid null nodes like //node1//node2/node3.
+ * \param[in] path The path where we are going to search for the next node name.
+ * \retval The next node name we found inside the given path.
+ * \retval NULL if there are no more node names.
+ */
+static char *next_node_name(char **path)
+{
+       char *res;
+
+       do {
+               res = strsep(path, "/");
+       } while (res && ast_strlen_zero(res));
+
+       return res;
+}
+
+/*!
+ * \internal
+ * \brief Release the memory allocated by a call to ao2_alloc.
+ */
+static void data_provider_destructor(void *obj)
+{
+       struct data_provider *provider = obj;
+
+       ao2_ref(provider->children, -1);
+}
+
+/*!
+ * \internal
+ * \brief Create a new data node.
+ * \param[in] name The name of the node we are going to create.
+ * \param[in] handler The handler registered for this node.
+ * \param[in] registrar The name of the registrar.
+ * \retval NULL on error.
+ * \retval The allocated data node structure.
+ */
+static struct data_provider *data_provider_new(const char *name,
+       const struct ast_data_handler *handler, const char *registrar)
+{
+       struct data_provider *node;
+       size_t namelen;
+
+       namelen = strlen(name) + 1;
+
+       node = ao2_alloc(sizeof(*node) + namelen, data_provider_destructor);
+       if (!node) {
+               return NULL;
+       }
+
+       node->handler = handler;
+       node->registrar = registrar;
+       strcpy(node->name, name);
+
+       /* initialize the childrens container. */
+       if (!(node->children = ao2_container_alloc(NUM_DATA_NODE_BUCKETS,
+                       data_provider_hash, data_provider_cmp))) {
+               ao2_ref(node, -1);
+               return NULL;
+       }
+
+       return node;
+}
+
+/*!
+ * \internal
+ * \brief Add a child node named 'name' to the 'parent' node.
+ * \param[in] parent Where to add the child node.
+ * \param[in] name The name of the child node.
+ * \param[in] handler The handler structure.
+ * \param[in] registrar Who registered this node.
+ * \retval NULL on error.
+ * \retval A newly allocated child in parent.
+ */
+static struct data_provider *data_provider_add_child(struct ao2_container *parent,
+       const char *name, const struct ast_data_handler *handler, const char *registrar)
+{
+       struct data_provider *child;
+
+       child = data_provider_new(name, handler, registrar);
+       if (!child) {
+               return NULL;
+       }
+
+       ao2_link(parent, child);
+
+       return child;
+}
+
+/*!
+ * \internal
+ * \brief Find a child node, based on his name.
+ * \param[in] parent Where to find the node.
+ * \param[in] name The node name to find.
+ * \param[in] registrar Also check if the node was being used by this registrar.
+ * \retval NULL if a node wasn't found.
+ * \retval The node found.
+ * \note Remember to decrement the ref count of the returned node after using it.
+ */
+static struct data_provider *data_provider_find(struct ao2_container *parent,
+       const char *name, const char *registrar)
+{
+       struct data_provider *find_node, *found;
+
+       /* XXX avoid allocating a new data node for searching... */
+       find_node = data_provider_new(name, NULL, NULL);
+       if (!find_node) {
+               return NULL;
+       }
+
+       found = ao2_find(parent, find_node, OBJ_POINTER);
+
+       /* free the created node used for searching. */
+       ao2_ref(find_node, -1);
+
+       if (found && found->registrar && registrar) {
+               if (strcmp(found->registrar, registrar)) {
+                       /* if the name doesn't match, do not return this node. */
+                       ast_debug(1, "Registrar doesn't match, node was registered"
+                               " by '%s' and we are searching for '%s'\n",
+                               found->registrar, registrar);
+                       ao2_ref(found, -1);
+                       return NULL;
+               }
+       }
+
+       return found;
+}
+
+/*!
+ * \internal
+ * \brief Release a group of nodes.
+ * \param[in] parent The parent node.
+ * \param[in] path The path of nodes to release.
+ * \param[in] registrar Who registered this node.
+ * \retval <0 on error.
+ * \retval 0 on success.
+ * \see data_provider_create
+ */
+static int data_provider_release(struct ao2_container *parent, const char *path,
+       const char *registrar)
+{
+       char *node_name, *rpath;
+       struct data_provider *child;
+       int ret = 0;
+
+       rpath = ast_strdupa(path);
+
+       node_name = next_node_name(&rpath);
+       if (!node_name) {
+               return -1;
+       }
+
+       child = data_provider_find(parent, node_name, registrar);
+       if (!child) {
+               return -1;
+       }
+
+       /* if this is not a terminal node. */
+       if (!child->handler && rpath) {
+               ret = data_provider_release(child->children, rpath, registrar);
+       }
+
+       /* if this node is empty, unlink it. */
+       if (!ret && !ao2_container_count(child->children)) {
+               ao2_unlink(parent, child);
+       }
+
+       ao2_ref(child, -1);
+
+       return ret;
+}
+
+/*!
+ * \internal
+ * \brief Release every node registered by 'registrar'.
+ * \param[in] parent The parent node.
+ * \param[in] registrar
+ * \see __ast_data_unregister
+ */
+static void data_provider_release_all(struct ao2_container *parent,
+       const char *registrar)
+{
+       struct ao2_iterator i;
+       struct data_provider *node;
+
+       i = ao2_iterator_init(parent, 0);
+       while ((node = ao2_iterator_next(&i))) {
+               if (!node->handler) {
+                       /* this is a non-terminal node, go inside it. */
+                       data_provider_release_all(node->children, registrar);
+                       if (!ao2_container_count(node->children)) {
+                               /* if this node was left empty, unlink it. */
+                               ao2_unlink(parent, node);
+                       }
+               } else {
+                       if (!strcmp(node->registrar, registrar)) {
+                               /* if the registrars match, release it! */
+                               ao2_unlink(parent, node);
+                       }
+               }
+               ao2_ref(node, -1);
+       }
+       ao2_iterator_destroy(&i);
+
+}
+
+/*!
+ * \internal
+ * \brief Create the middle nodes for the specified path (asterisk/testnode1/childnode)
+ * \param[in] parent Where to add the middle nodes structure.
+ * \param[in] path The path of nodes to add.
+ * \param[in] registrar Who is trying to create this node provider.
+ * \retval NULL on error.
+ * \retval The created node.
+ * \see data_provider_release
+ */
+static struct data_provider *data_provider_create(struct ao2_container *parent,
+       const char *path, const char *registrar)
+{
+       char *rpath, *node_name;
+       struct data_provider *child, *ret = NULL;
+
+       rpath = ast_strdupa(path);
+
+       node_name = next_node_name(&rpath);
+       if (!node_name) {
+               /* no more nodes to create. */
+               return NULL;
+       }
+
+       child = data_provider_find(parent, node_name, NULL);
+
+       if (!child) {
+               /* nodes without handler are non-terminal nodes. */
+               child = data_provider_add_child(parent, node_name, NULL, registrar);
+       }
+
+       if (rpath) {
+               ret = data_provider_create(child->children, rpath, registrar);
+               if (ret) {
+                       ao2_ref(child, -1);
+               }
+       }
+
+       return ret ? ret : child;
+}
+
+int __ast_data_register(const char *path, const struct ast_data_handler *handler,
+       const char *registrar, struct ast_module *mod)
+{
+       struct data_provider *node;
+
+       if (!path) {
+               return -1;
+       }
+
+       /* check if the handler structure is compatible. */
+       if (!data_structure_compatible(handler->version,
+               latest_handler_compatible_version,
+               current_handler_version)) {
+               return -1;
+       }
+
+       /* create the node structure for the registered handler. */
+       data_write_lock();
+
+       node = data_provider_create(root_data.container, path, registrar);
+       if (!node) {
+               ast_log(LOG_ERROR, "Unable to create the specified path (%s) "
+                       "for '%s'.\n", path, registrar);
+               data_unlock();
+               return -1;
+       }
+
+       if (ao2_container_count(node->children) || node->handler) {
+               ast_log(LOG_ERROR, "The node '%s' was already registered. "
+                       "We were unable to register '%s' for registrar '%s'.\n",
+                       node->name, path, registrar);
+               ao2_ref(node, -1);
+               data_unlock();
+               return -1;
+       }
+
+       /* add handler to that node. */
+       node->handler = handler;
+       node->module = mod;
+
+       ao2_ref(node, -1);
+
+       data_unlock();
+
+       return 0;
+}
+
+int __ast_data_register_multiple(const struct ast_data_entry *data_entries,
+       size_t entries, const char *registrar, struct ast_module *mod)
+{
+       int i, res;
+
+       for (i = 0; i < entries; i++) {
+               res = __ast_data_register(data_entries[i].path, data_entries[i].handler,
+                               registrar, mod);
+               if (res) {
+                       /* unregister all the already registered nodes, and make
+                        * this an atomic action. */
+                       while ((--i) >= 0) {
+                               __ast_data_unregister(data_entries[i].path, registrar);
+                       }
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
+int __ast_data_unregister(const char *path, const char *registrar)
+{
+       int ret = 0;
+
+       data_write_lock();
+       if (path) {
+               ret = data_provider_release(root_data.container, path, registrar);
+       } else {
+               data_provider_release_all(root_data.container, registrar);
+       }
+       data_unlock();
+
+       if (path && ret) {
+               ast_log(LOG_ERROR, "Unable to unregister '%s' for '%s'\n",
+                       path, registrar);
+       }
+
+       return ret;
+}
+
+/*!
+ * \internal
+ * \brief Is a char used to specify a comparison?
+ * \param[in] a Character to evaluate.
+ * \retval 1 It is a char used to specify a comparison.
+ * \retval 0 It is NOT a char used to specify a comparison.
+ */
+static int data_search_comparison_char(char a)
+{
+       switch (a) {
+       case '!':
+       case '=':
+       case '<':
+       case '>':
+               return 1;
+       }
+
+       return 0;
+}
+
+/*!
+ * \internal
+ * \brief Get the type of comparison.
+ */
+static enum data_search_comparison data_search_comparison_type(const char *comparison)
+{
+       if (!strcmp(comparison, "=")) {
+               return DATA_CMP_EQ;
+       } else if (!strcmp(comparison, "!=")) {
+               return DATA_CMP_NEQ;
+       } else if (!strcmp(comparison, "<")) {
+               return DATA_CMP_LT;
+       } else if (!strcmp(comparison, ">")) {
+               return DATA_CMP_GT;
+       } else if (!strcmp(comparison, "<=")) {
+               return DATA_CMP_LE;
+       } else if (!strcmp(comparison, ">=")) {
+               return DATA_CMP_GE;
+       }
+
+       return DATA_CMP_UNKNOWN;
+}
+
+/*!
+ * \internal
+ * \brief Common string hash function for data nodes
+ */
+static int data_search_hash(const void *obj, const int flags)
+{
+       const struct ast_data_search *node = obj;
+       return ast_str_hash(node->name);
+}
+
+/*!
+ * \internal
+ * \brief Common string comparison function
+ */
+static int data_search_cmp(void *obj, void *arg, int flags)
+{
+       struct ast_data_search *node1 = obj, *node2 = arg;
+       return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
+}
+
+/*!
+ * \internal
+ * \brief Destroy the ao2 search node.
+ */
+static void data_search_destructor(void *obj)
+{
+       struct ast_data_search *node = obj;
+
+       if (node->value) {
+               ast_free(node->value);
+       }
+
+       ao2_ref(node->children, -1);
+}
+
+/*!
+ * \internal
+ * \brief Allocate a search node.
+ * \retval NULL on error.
+ * \retval non-NULL The allocated search node structure.
+ */
+static struct ast_data_search *data_search_alloc(const char *name)
+{
+       struct ast_data_search *res;
+       size_t name_len = strlen(name) + 1;
+
+       res = ao2_alloc(sizeof(*res) + name_len, data_search_destructor);
+       if (!res) {
+               return NULL;
+       }
+
+       res->children = ao2_container_alloc(NUM_DATA_SEARCH_BUCKETS, data_search_hash,
+               data_search_cmp);
+
+       if (!res) {
+               ao2_ref(res, -1);
+               return NULL;
+       }
+
+       strcpy(res->name, name);
+
+       return res;
+}
+
+/*!
+ * \internal
+ * \brief Find a child node, based on his name.
+ * \param[in] parent Where to find the node.
+ * \param[in] name The node name to find.
+ * \retval NULL if a node wasn't found.
+ * \retval The node found.
+ * \note Remember to decrement the ref count of the returned node after using it.
+ */
+static struct ast_data_search *data_search_find(struct ao2_container *parent,
+       const char *name)
+{
+       struct ast_data_search *find_node, *found;
+
+       find_node = data_search_alloc(name);
+       if (!find_node) {
+               return NULL;
+       }
+
+       found = ao2_find(parent, find_node, OBJ_POINTER);
+
+       /* free the created node used for searching. */
+       ao2_ref(find_node, -1);
+
+       return found;
+}
+
+/*!
+ * \internal
+ * \brief Add a child node named 'name' to the 'parent' node.
+ * \param[in] parent Where to add the child node.
+ * \param[in] name The name of the child node.
+ * \retval NULL on error.
+ * \retval A newly allocated child in parent.
+ */
+static struct ast_data_search *data_search_add_child(struct ao2_container *parent,
+       const char *name)
+{
+       struct ast_data_search *child;
+
+       child = data_search_alloc(name);
+       if (!child) {
+               return NULL;
+       }
+
+       ao2_link(parent, child);
+
+       return child;
+}
+
+/*!
+ * \internal
+ * \brief Create the middle nodes for the specified path (asterisk/testnode1/childnode)
+ * \param[in] parent Where to add the middle nodes structure.
+ * \param[in] path The path of nodes to add.
+ * \retval NULL on error.
+ * \retval The created node.
+ */
+static struct ast_data_search *data_search_create(struct ao2_container *parent,
+       const char *path)
+{
+       char *rpath, *node_name;
+       struct ast_data_search *child = NULL;
+       struct ao2_container *current = parent;
+
+       rpath = ast_strdupa(path);
+
+       node_name = next_node_name(&rpath);
+       while (node_name) {
+               child = data_search_find(current, node_name);
+               if (!child) {
+                       child = data_search_add_child(current, node_name);
+               }
+               ao2_ref(child, -1);
+               current = child->children;
+               node_name = next_node_name(&rpath);
+       }
+
+       return child;
+}
+
+/*!
+ * \internal
+ * \brief Allocate a tree with the search string parsed.
+ * \param[in] search_string The search string.
+ * \retval NULL on error.
+ * \retval non-NULL A dynamically allocated search tree.
+ */
+static struct ast_data_search *data_search_generate(const char *search_string)
+{
+       struct ast_str *name, *value, *comparison;
+       char *elements, *search_string_dup, *saveptr;
+       int i;
+       struct ast_data_search *root, *child;
+       enum data_search_comparison cmp_type;
+       size_t search_string_len;
+
+       if (!search_string) {
+               ast_log(LOG_ERROR, "You must pass a valid search string.\n");
+               return NULL;
+       }
+
+       search_string_len = strlen(search_string);
+
+       name = ast_str_create(search_string_len);
+       if (!name) {
+               return NULL;
+       }
+       value = ast_str_create(search_string_len);
+       if (!value) {
+               ast_free(name);
+               return NULL;
+       }
+       comparison = ast_str_create(search_string_len);
+       if (!comparison) {
+               ast_free(name);
+               ast_free(value);
+               return NULL;
+       }
+
+       search_string_dup = ast_strdupa(search_string);
+
+       /* Create the root node (just used as a container) */
+       root = data_search_alloc("/");
+       if (!root) {
+               ast_free(name);
+               ast_free(value);
+               ast_free(comparison);
+               return NULL;
+       }
+
+       for (elements = strtok_r(search_string_dup, ",", &saveptr); elements;
+               elements = strtok_r(NULL, ",", &saveptr)) {
+               /* Parse the name */
+               ast_str_reset(name);
+               for (i = 0; !data_search_comparison_char(elements[i]) &&
+                       elements[i]; i++) {
+                       ast_str_append(&name, 0, "%c", elements[i]);
+               }
+
+               /* check if the syntax is ok. */
+               if (!data_search_comparison_char(elements[i])) {
+                       /* if this is the end of the string, then this is
+                        * an error! */
+                       ast_log(LOG_ERROR, "Invalid search string!\n");
+                       continue;
+               }
+
+               /* parse the comparison string. */
+               ast_str_reset(comparison);
+               for (; data_search_comparison_char(elements[i]) && elements[i]; i++) {
+                       ast_str_append(&comparison, 0, "%c", elements[i]);
+               }
+
+               /* parse the value string. */
+               ast_str_reset(value);
+               for (; elements[i]; i++) {
+                       ast_str_append(&value, 0, "%c", elements[i]);
+               }
+
+               cmp_type = data_search_comparison_type(ast_str_buffer(comparison));
+               if (cmp_type == DATA_CMP_UNKNOWN) {
+                       ast_log(LOG_ERROR, "Invalid comparison '%s'\n",
+                               ast_str_buffer(comparison));
+                       continue;
+               }
+
+               /* add this node to the tree. */
+               child = data_search_create(root->children, ast_str_buffer(name));
+               if (child) {
+                       child->cmp_type = cmp_type;
+                       child->value = ast_strdup(ast_str_buffer(value));
+               }
+       }
+
+       ast_free(name);
+       ast_free(value);
+       ast_free(comparison);
+
+       return root;
+}
+
+/*!
+ * \internal
+ * \brief Release the allocated memory for the search tree.
+ * \param[in] search The search tree root node.
+ */
+static void data_search_release(struct ast_data_search *search)
+{
+       ao2_ref(search, -1);
+}
+
+/*!
+ * \internal
+ * \brief Based on the kind of comparison and the result in cmpval, return
+ *        if it matches.
+ * \param[in] cmpval A result returned by a strcmp() for example.
+ * \param[in] comparison_type The kind of comparison (<,>,=,!=,...)
+ * \retval 1 If the comparison doesn't match.
+ * \retval 0 If the comparison matches.
+ */
+static inline int data_search_comparison_result(int cmpval,
+       enum data_search_comparison comparison_type)
+{
+       switch (comparison_type) {
+       case DATA_CMP_GE:
+               if (cmpval >= 0) {
+                       return 0;
+               }
+               break;
+       case DATA_CMP_LE:
+               if (cmpval <= 0) {
+                       return 0;
+               }
+               break;
+       case DATA_CMP_EQ:
+               if (cmpval == 0) {
+                       return 0;
+               }
+               break;
+       case DATA_CMP_NEQ:
+               if (cmpval != 0) {
+                       return 0;
+               }
+               break;
+       case DATA_CMP_LT:
+               if (cmpval < 0) {
+                       return 0;
+               }
+               break;
+       case DATA_CMP_GT:
+               if (cmpval > 0) {
+                       return 0;
+               }
+               break;
+       case DATA_CMP_UNKNOWN:
+               break;
+       }
+       return 1;
+}
+
+/*!
+ * \internal
+ * \brief Get an internal node, from the search tree.
+ * \param[in] node A node container.
+ * \param[in] path The path to the needed internal node.
+ * \retval NULL if the internal node is not found.
+ * \retval non-NULL the internal node with path 'path'.
+ */
+static struct ast_data_search *data_search_get_node(const struct ast_data_search *node,
+       const char *path)
+{
+       char *savepath, *node_name;
+       struct ast_data_search *child, *current = (struct ast_data_search *) node;
+
+       if (!node) {
+               return NULL;
+       }
+
+       savepath = ast_strdupa(path);
+       node_name = next_node_name(&savepath);
+
+       while (node_name) {
+               child = data_search_find(current->children, node_name);
+               if (current != node) {
+                       ao2_ref(current, -1);
+               }
+               if (!child) {
+                       return NULL;
+               };
+               current = child;
+               node_name = next_node_name(&savepath);
+       }
+
+       return current;
+}
+
+int ast_data_search_cmp_string(const struct ast_data_search *root, const char *name,
+       char *value)
+{
+       struct ast_data_search *child;
+       enum data_search_comparison cmp_type;
+       int ret;
+
+       child = data_search_get_node(root, name);
+       if (!child) {
+               return 0;
+       }
+
+       ret = strcmp(value, child->value);
+       cmp_type = child->cmp_type;
+
+       ao2_ref(child, -1);
+
+       return data_search_comparison_result(ret, cmp_type);
+}
+
+int ast_data_search_cmp_ptr(const struct ast_data_search *root, const char *name,
+       void *ptr)
+{
+       struct ast_data_search *child;
+       enum data_search_comparison cmp_type;
+       void *node_ptr;
+
+       child = data_search_get_node(root, name);
+       if (!child) {
+               return 0;
+       }
+
+       cmp_type = child->cmp_type;
+
+       if (sscanf(child->value, "%p", &node_ptr) <= 0) {
+               return 1;
+       }
+
+       ao2_ref(child, -1);
+
+       return data_search_comparison_result((node_ptr - ptr), cmp_type);
+}
+
+int ast_data_search_cmp_ipaddr(const struct ast_data_search *root, const char *name,
+       struct in_addr addr)
+{
+       struct ast_data_search *child;
+       enum data_search_comparison cmp_type;
+       struct in_addr node_addr;
+
+       child = data_search_get_node(root, name);
+       if (!child) {
+               return 0;
+       }
+       cmp_type = child->cmp_type;
+
+       inet_aton(child->value, &node_addr);
+
+       ao2_ref(child, -1);
+
+       return data_search_comparison_result((node_addr.s_addr - addr.s_addr), cmp_type);
+}
+
+int ast_data_search_cmp_bool(const struct ast_data_search *root, const char *name,
+       unsigned int value)
+{
+       struct ast_data_search *child;
+       unsigned int node_value;
+       enum data_search_comparison cmp_type;
+
+       child = data_search_get_node(root, name);
+       if (!child) {
+               return 0;
+       }
+
+       node_value = abs(ast_true(child->value));
+       cmp_type = child->cmp_type;
+
+       ao2_ref(child, -1);
+
+       return data_search_comparison_result(value - node_value, cmp_type);
+}
+
+int ast_data_search_cmp_dbl(const struct ast_data_search *root, const char *name,
+       double value)
+{
+       struct ast_data_search *child;
+       double node_value;
+       enum data_search_comparison cmp_type;
+
+       child = data_search_get_node(root, name);
+       if (!child) {
+               return 0;
+       }
+
+       node_value = strtod(child->value, NULL);
+       cmp_type = child->cmp_type;
+
+       ao2_ref(child, -1);
+
+       return data_search_comparison_result(value - node_value, cmp_type);
+}
+
+int ast_data_search_cmp_uint(const struct ast_data_search *root, const char *name,
+       unsigned int value)
+{
+       struct ast_data_search *child;
+       unsigned int node_value;
+       enum data_search_comparison cmp_type;
+
+       child = data_search_get_node(root, name);
+       if (!child) {
+               return 0;
+       }
+
+       node_value = atoi(child->value);
+       cmp_type = child->cmp_type;
+
+       ao2_ref(child, -1);
+
+       return data_search_comparison_result(value - node_value, cmp_type);
+}
+
+int ast_data_search_cmp_int(const struct ast_data_search *root, const char *name,
+       int value)
+{
+       struct ast_data_search *child;
+       int node_value;
+       enum data_search_comparison cmp_type;
+
+       child = data_search_get_node(root, name);
+       if (!child) {
+               return 0;
+       }
+
+       node_value = atoi(child->value);
+       cmp_type = child->cmp_type;
+
+       ao2_ref(child, -1);
+
+       return data_search_comparison_result(value - node_value, cmp_type);
+}
+
+/*!
+ * \internal
+ * \brief Get the member pointer, from a mapping structure, based on its name.
+ * \XXX We will need to improve performance here!!.
+ * \retval <0 if the member was not found.
+ * \retval >=0 The member position in the mapping structure.
+ */
+static inline int data_search_mapping_find(const struct ast_data_mapping_structure *map,
+       size_t mapping_len,
+       const char *member_name)
+{
+       int i;
+
+       for (i = 0; i < mapping_len; i++) {
+               if (!strcmp(map[i].name, member_name)) {
+                       return i;
+               }
+       }
+
+       return -1;
+}
+
+int ast_data_search_has_condition(const struct ast_data_search *search,
+       const char *compare_condition)
+{
+       struct ast_data_search *child;
+
+       child = data_search_get_node(search, compare_condition);
+       if (!child) {
+               return 0;
+       }
+
+       ao2_ref(child, -1);
+
+       return 1;
+}
+
+int __ast_data_search_cmp_structure(const struct ast_data_search *search,
+       const struct ast_data_mapping_structure *mapping, size_t mapping_len,
+       void *structure, const char *structure_name)
+{
+       struct ao2_iterator i;
+       struct ast_data_search *node, *struct_children;
+       int member, notmatch = 0;
+
+       if (!search) {
+               return 0;
+       }
+
+       struct_children = data_search_get_node(search, structure_name);
+       if (!struct_children) {
+               return 0;
+       }
+
+       i = ao2_iterator_init(struct_children->children, 0);
+       while ((node = ao2_iterator_next(&i))) {
+               member = data_search_mapping_find(mapping, mapping_len, node->name);
+               if (member < 0) {
+                       /* the structure member name doesn't match! */
+                       ao2_ref(node, -1);
+                       ao2_ref(struct_children, -1);
+                       ao2_iterator_destroy(&i);
+                       return 0;
+               }
+
+               notmatch = 0;
+               switch (mapping[member].type) {
+               case AST_DATA_STRING:
+                       notmatch = ast_data_search_cmp_string(struct_children,
+                               node->name,
+                               mapping[member].get.AST_DATA_STRING(structure));
+                       break;
+               case AST_DATA_INTEGER:
+                       notmatch = ast_data_search_cmp_int(struct_children,
+                               node->name,
+                               mapping[member].get.AST_DATA_INTEGER(structure));
+                       break;
+               case AST_DATA_BOOLEAN:
+                       notmatch = ast_data_search_cmp_bool(struct_children,
+                               node->name,
+                               mapping[member].get.AST_DATA_BOOLEAN(structure));
+                       break;
+               case AST_DATA_UNSIGNED_INTEGER:
+                       notmatch = ast_data_search_cmp_uint(struct_children,
+                               node->name,
+                               mapping[member].get.AST_DATA_UNSIGNED_INTEGER(structure));
+                       break;
+               case AST_DATA_DOUBLE:
+                       notmatch = ast_data_search_cmp_dbl(struct_children,
+                               node->name,
+                               mapping[member].get.AST_DATA_DOUBLE(structure));
+                       break;
+               case AST_DATA_IPADDR:
+                       notmatch = ast_data_search_cmp_ipaddr(struct_children,
+                               node->name,
+                               mapping[member].get.AST_DATA_IPADDR(structure));
+                       break;
+               case AST_DATA_POINTER:
+                       notmatch = ast_data_search_cmp_ptr(struct_children,
+                               node->name,
+                               mapping[member].get.AST_DATA_POINTER(structure));
+                       break;
+               case AST_DATA_CONTAINER:
+                       break;
+               }
+
+               ao2_ref(node, -1);
+       }
+       ao2_iterator_destroy(&i);
+
+       ao2_ref(struct_children, -1);
+
+       return notmatch;
+}
+
+/*!
+ * \internal
+ * \brief Release the memory allocated by a call to ao2_alloc.
+ */
+static void data_result_destructor(void *obj)
+{
+       struct ast_data *root = obj;
+
+       switch (root->type) {
+       case AST_DATA_POINTER:
+       case AST_DATA_STRING:
+               ast_free(root->payload.ptr);
+       case AST_DATA_CONTAINER:
+       case AST_DATA_INTEGER:
+       case AST_DATA_UNSIGNED_INTEGER:
+       case AST_DATA_DOUBLE:
+       case AST_DATA_BOOLEAN:
+       case AST_DATA_IPADDR:
+               ao2_ref(root->children, -1);
+               break;
+       }
+}
+
+static struct ast_data *data_result_create(const char *name)
+{
+       struct ast_data *res;
+       size_t namelen;
+
+       namelen = ast_strlen_zero(name) ? 1 : strlen(name) + 1;
+
+       res = ao2_alloc(sizeof(*res) + namelen, data_result_destructor);
+       if (!res) {
+               return NULL;
+       }
+
+       strcpy(res->name, namelen ? name : "");
+
+       /* initialize the children container */
+       res->children = ao2_container_alloc(NUM_DATA_RESULT_BUCKETS, data_result_hash,
+               data_result_cmp);
+       if (!res->children) {
+               ao2_ref(res, -1);
+               return NULL;
+       }
+
+       /* set this node as a container. */
+       res->type = AST_DATA_CONTAINER;
+
+       return res;
+}
+
+/*!
+ * \internal
+ * \brief Find a child node, based on its name.
+ * \param[in] root The starting point.
+ * \param[in] name The child name.
+ * \retval NULL if the node wasn't found.
+ * \retval non-NULL the node we were looking for.
+ */
+static struct ast_data *data_result_find_child(struct ast_data *root, const char *name)
+{
+       struct ast_data *found, *find_node;
+
+       find_node = data_result_create(name);
+       if (!find_node) {
+               return NULL;
+       }
+
+       found = ao2_find(root->children, find_node, OBJ_POINTER);
+
+       /* release the temporary created node used for searching. */
+       ao2_ref(find_node, -1);
+
+       return found;
+}
+
+/*!
+ * \internal
+ * \brief Get an internal node, from the result set.
+ * \param[in] node A node container.
+ * \param[in] path The path to the needed internal node.
+ * \retval NULL if the internal node is not found.
+ * \retval non-NULL the internal node with path 'path'.
+ */
+static struct ast_data *data_result_get_node(struct ast_data *node,
+       const char *path)
+{
+       char *savepath, *node_name;
+       struct ast_data *child, *current = node;
+
+       savepath = ast_strdupa(path);
+       node_name = next_node_name(&savepath);
+
+       while (node_name) {
+               child = data_result_find_child(current, node_name);
+               if (current != node) {
+                       ao2_ref(current, -1);
+               }
+               if (!child) {
+                       return NULL;
+               }
+               current = child;
+               node_name = next_node_name(&savepath);
+       }
+
+       /* do not increment the refcount of the returned object. */
+       if (current != node) {
+               ao2_ref(current, -1);
+       }
+
+       return current;
+}
+
+/*!
+ * \internal
+ * \brief Add a child to the specified root node.
+ * \param[in] root The root node pointer.
+ * \param[in] child The child to add to the root node.
+ */
+static void data_result_add_child(struct ast_data *root, struct ast_data *child)
+{
+       ao2_link(root->children, child);
+}
+
+/*!
+ * \internal
+ * \brief Common string hash function for data nodes
+ */
+static int data_filter_hash(const void *obj, const int flags)
+{
+       const struct data_filter *node = obj;
+       return ast_str_hash(node->name);
+}
+
+/*!
+ * \internal
+ * \brief Common string comparison function
+ */
+static int data_filter_cmp(void *obj, void *arg, int flags)
+{
+       struct data_filter *node1 = obj, *node2 = arg;
+       return strcasecmp(node1->name, node2->name) ? 0 : CMP_MATCH;
+}
+
+/*!
+ * \internal
+ * \brief Destroy a data filter tree.
+ * \param[in] obj Data filter list to be destroyed.
+ */
+static void data_filter_destructor(void *obj)
+{
+       struct data_filter *filter = obj, *globres;
+
+       AST_LIST_TRAVERSE(&(filter->glob_list), globres, list) {
+               ao2_ref(globres, -1);
+       }
+
+       ao2_ref(filter->children, -1);
+}
+
+/*!
+ * \internal
+ * \brief Allocate a filter node.
+ * \retval NULL on error.
+ * \retval non-NULL The allocated search node structure.
+ */
+static struct data_filter *data_filter_alloc(const char *name)
+{
+       char *globname, *token;
+       struct data_filter *res, *globfilter;
+       size_t name_len = strlen(name) + 1;
+
+       res = ao2_alloc(sizeof(*res) + name_len, data_filter_destructor);
+       if (!res) {
+               return NULL;
+       }
+
+       res->children = ao2_container_alloc(NUM_DATA_FILTER_BUCKETS, data_filter_hash,
+               data_filter_cmp);
+
+       if (!res) {
+               ao2_ref(res, -1);
+               return NULL;
+       }
+
+       strcpy(res->name, name);
+
+       if (strchr(res->name, '*')) {
+               globname = ast_strdupa(res->name);
+
+               while ((token = strsep(&globname, "*"))) {
+                       globfilter = data_filter_alloc(token);
+                       AST_LIST_INSERT_TAIL(&(res->glob_list), globfilter, list);
+               }
+       }
+
+       return res;
+}
+
+/*!
+ * \internal
+ * \brief Release a filter tree.
+ * \param[in] filter The filter tree root node.
+ */
+static void data_filter_release(struct data_filter *filter)
+{
+       ao2_ref(filter, -1);
+}
+
+/*!
+ * \internal
+ * \brief Find a child node, based on his name.
+ * \param[in] parent Where to find the node.
+ * \param[in] name The node name to find.
+ * \retval NULL if a node wasn't found.
+ * \retval The node found.
+ * \note Remember to decrement the ref count of the returned node after using it.
+ */
+static struct data_filter *data_filter_find(struct ao2_container *parent,
+       const char *name)
+{
+       int i, olend, orend, globfound;
+       size_t name_len = strlen(name), glob_len;
+       struct ao2_iterator iter;
+       struct data_filter *find_node, *found, *globres;
+
+       find_node = data_filter_alloc(name);
+       if (!find_node) {
+               return NULL;
+       }
+
+       found = ao2_find(parent, find_node, OBJ_POINTER);
+
+       /* free the created node used for searching. */
+       ao2_ref(find_node, -1);
+
+       if (found) {
+               return found;
+       }
+
+       iter = ao2_iterator_init(parent, 0);
+       while ((found = ao2_iterator_next(&iter))) {
+               if (!AST_LIST_EMPTY(&(found->glob_list))) {
+                       i = 0;
+                       globfound = 1;
+
+                       olend = ast_strlen_zero(AST_LIST_FIRST(&(found->glob_list))->name);
+                       orend = ast_strlen_zero(AST_LIST_LAST(&(found->glob_list))->name);
+
+                       AST_LIST_TRAVERSE(&(found->glob_list), globres, list) {
+                               if (!*globres->name) {
+                                       continue;
+                               }
+
+                               glob_len = strlen(globres->name);
+
+                               if (!i && !olend) {
+                                       if (strncasecmp(name, globres->name, glob_len)) {
+                                               globfound = 0;
+                                               break;
+                                       }
+
+                                       i += glob_len;
+                                       continue;
+                               }
+
+                               for (globfound = 0; name_len - i >= glob_len; ++i) {
+                                       if (!strncasecmp(name + i, globres->name, glob_len)) {
+                                               globfound = 1;
+                                               i += glob_len;
+                                               break;
+                                       }
+                               }
+
+                               if (!globfound) {
+                                       break;
+                               }
+                       }
+
+                       if (globfound && (i == name_len || orend)) {
+                               ao2_iterator_destroy(&iter);
+                               return found;
+                       }
+               }
+
+               ao2_ref(found, -1);
+       }
+       ao2_iterator_destroy(&iter);
+
+       return NULL;
+}
+
+/*!
+ * \internal
+ * \brief Add a child to the specified node.
+ * \param[in] root The root node where to add the child.
+ * \param[in] name The name of the node to add.
+ * \note Remember to decrement the ref count after using the returned node.
+ */
+static struct data_filter *data_filter_add_child(struct ao2_container *root,
+       char *name)
+{
+       struct data_filter *node;
+
+       node = data_filter_find(root, name);
+       if (node) {
+               return node;
+       }
+
+       node = data_filter_alloc(name);
+       if (!node) {
+               return NULL;
+       }
+
+       ao2_link(root, node);
+
+       return node;
+}
+
+/*!
+ * \internal
+ * \brief Add a node to a filter list from a path
+ * \param[in] Filter list to add the path onto.
+ * \param[in] The path to add into the filter list.
+ * \retval NULL on error.
+ * \retval non-NULL A tree with the wanted nodes.
+ */
+static int data_filter_add_nodes(struct ao2_container *root, char *path)
+{
+       struct data_filter *node;
+       char *savepath, *saveptr, *token, *node_name;
+       int ret = 0;
+
+       if (!path) {
+               return 0;
+       }
+
+       savepath = ast_strdupa(path);
+
+       node_name = next_node_name(&savepath);
+
+       if (!node_name) {
+               return 0;
+       }
+
+       for (token = strtok_r(node_name, "|", &saveptr);
+                       token; token = strtok_r(NULL, "|", &saveptr)) {
+               node = data_filter_add_child(root, token);
+               if (!node) {
+                       continue;
+               }
+               data_filter_add_nodes(node->children, savepath);
+               ret = 1;
+               ao2_ref(node, -1);
+       }
+
+       return ret;
+}
+
+/*!
+ * \internal
+ * \brief Generate a filter list based on a filter string provided by the API user.
+ * \param[in] A filter string to create a filter from.
+ */
+static struct data_filter *data_filter_generate(const char *constfilter)
+{
+       struct data_filter *filter = NULL;
+       char *strfilter, *token, *saveptr;
+       int node_added = 0;
+
+       if (!constfilter) {
+               return NULL;
+       }
+
+       strfilter = ast_strdupa(constfilter);
+
+       filter = data_filter_alloc("/");
+       if (!filter) {
+               return NULL;
+       }
+
+       for (token = strtok_r(strfilter, ",", &saveptr); token;
+                       token = strtok_r(NULL, ",", &saveptr)) {
+               node_added = data_filter_add_nodes(filter->children, token);
+       }
+
+       if (!node_added) {
+               ao2_ref(filter, -1);
+               return NULL;
+       }
+
+       return filter;
+}
+
+/*!
+ * \internal
+ * \brief Generate all the tree from a specified provider.
+ * \param[in] query The query executed.
+ * \param[in] root_provider The provider specified in the path of the query.
+ * \param[in] parent_node_name The root node name.
+ * \retval NULL on error.
+ * \retval non-NULL The generated result tree.
+ */
+static struct ast_data *data_result_generate_node(const struct ast_data_query *query,
+       const struct data_provider *root_provider,
+       const char *parent_node_name,
+       const struct ast_data_search *search,
+       const struct data_filter *filter)
+{
+       struct ast_data *generated, *node;
+       struct ao2_iterator i;
+       struct data_provider *provider;
+       struct ast_data_search *search_child = NULL;
+       struct data_filter *filter_child;
+
+       node = data_result_create(parent_node_name);
+       if (!node) {
+               ast_log(LOG_ERROR, "Unable to allocate '%s' node\n", parent_node_name);
+               return NULL;
+       }
+
+       if (root_provider->module) {
+               ast_module_ref(root_provider->module);
+       }
+
+       /* if this is a terminal node, just run the callback function. */
+       if (root_provider->handler && root_provider->handler->get) {
+               node->filter = filter;
+               root_provider->handler->get(search, node);
+               if (root_provider->module) {
+                       ast_module_unref(root_provider->module);
+               }
+               return node;
+       }
+
+       if (root_provider->module) {
+               ast_module_unref(root_provider->module);
+       }
+
+       /* if this is not a terminal node, generate every child node. */
+       i = ao2_iterator_init(root_provider->children, 0);
+       while ((provider = ao2_iterator_next(&i))) {
+               filter_child = NULL;
+               generated = NULL;
+
+               /* get the internal search node. */
+               if (search) {
+                       search_child = data_search_find(search->children, provider->name);
+               }
+               /* get the internal filter node. */
+               if (filter) {
+                       filter_child = data_filter_find(filter->children, provider->name);
+               }
+
+               if (!filter || filter_child) {
+                       /* only generate the internal node, if we have something to
+                        * generate based on the filtering string. */
+                       generated = data_result_generate_node(query, provider,
+                               provider->name,
+                               search_child, filter_child);
+               }
+
+               /* decrement the refcount of the internal search node. */
+               if (search_child) {
+                       ao2_ref(search_child, -1);
+               }
+
+               /* decrement the refcount of the internal filter node. */
+               if (filter_child) {
+                       ao2_ref(filter_child, -1);
+               }
+
+               if (generated) {
+                       data_result_add_child(node, generated);
+                       ao2_ref(generated, -1);
+               }
+
+               ao2_ref(provider, -1);
+       }
+       ao2_iterator_destroy(&i);
+
+       return node;
+}
+
+/*!
+ * \internal
+ * \brief Generate a result tree based on a query.
+ * \param[in] query The complete query structure.
+ * \param[in] search_path The path to retrieve.
+ * \retval NULL on error.
+ * \retval non-NULL The generated data result.
+ */
+static struct ast_data *data_result_generate(const struct ast_data_query *query,
+       const char *search_path)
+{
+       char *node_name, *tmp_path;
+       struct data_provider *provider_child, *tmp_provider_child;
+       struct ast_data *result, *result_filtered;
+       struct ast_data_search *search = NULL, *search_child = NULL;
+       struct data_filter *filter = NULL, *filter_child = NULL;
+
+       if (!search_path) {
+               /* generate all the trees?. */
+               return NULL;
+       }
+
+       tmp_path = ast_strdupa(search_path);
+
+       /* start searching the root node name */
+       node_name = next_node_name(&tmp_path);
+       if (!node_name) {
+               return NULL;
+       }
+       provider_child = data_provider_find(root_data.container, node_name, NULL);
+
+       /* continue with the rest of the path. */
+       while (provider_child) {
+               node_name = next_node_name(&tmp_path);
+               if (!node_name) {
+                       break;
+               }
+
+               tmp_provider_child = data_provider_find(provider_child->children,
+                               node_name, NULL);
+
+               /* release the reference from this child */
+               ao2_ref(provider_child, -1);
+
+               provider_child = tmp_provider_child;
+       }
+
+       if (!provider_child) {
+               ast_log(LOG_ERROR, "Invalid path '%s', '%s' not found.\n",
+                               tmp_path, node_name);
+               return NULL;
+       }
+
+       /* generate the search tree. */
+       if (query->search) {
+               search = data_search_generate(query->search);
+               if (search) {
+                       search_child = data_search_find(search->children,
+                               provider_child->name);
+               }
+       }
+
+       /* generate the filter tree. */
+       if (query->filter) {
+               filter = data_filter_generate(query->filter);
+               if (filter) {
+                       filter_child = data_filter_find(filter->children,
+                               provider_child->name);
+               }
+       }
+
+       result = data_result_generate_node(query, provider_child, provider_child->name,
+                       search_child, filter_child);
+
+       /* release the requested provider. */
+       ao2_ref(provider_child, -1);
+
+       /* release the generated search tree. */
+       if (search_child) {
+               ao2_ref(search_child, -1);
+       }
+
+       if (filter_child) {
+               ao2_ref(filter_child, -1);
+       }
+
+       if (search) {
+               data_search_release(search);
+       }
+
+       result_filtered = result;
+
+       /* release the generated filter tree. */
+       if (filter) {
+               data_filter_release(filter);
+       }
+
+       return result_filtered;
+}
+
+struct ast_data *ast_data_get(const struct ast_data_query *query)
+{
+       struct ast_data *res;
+
+       /* check compatibility */
+       if (!data_structure_compatible(query->version, latest_query_compatible_version,
+               current_query_version)) {
+               return NULL;
+       }
+
+       data_read_lock();
+       res = data_result_generate(query, query->path);
+       data_unlock();
+
+       if (!res) {
+               ast_log(LOG_ERROR, "Unable to get data from %s\n", query->path);
+               return NULL;
+       }
+
+       return res;
+}
+
+/*!
+ * \internal
+ * \brief Helper function to move an ast_data tree to xml.
+ * \param[in] parent_data The initial ast_data node to be passed to xml.
+ * \param[out] parent_xml The root node to insert the xml.
+ */
+static void data_get_xml_add_child(struct ast_data *parent_data,
+       struct ast_xml_node *parent_xml)
+{
+       struct ao2_iterator i;
+       struct ast_data *node;
+       struct ast_xml_node *child_xml;
+       char node_content[256];
+
+       i = ao2_iterator_init(parent_data->children, 0);
+       while ((node = ao2_iterator_next(&i))) {
+               child_xml = ast_xml_new_node(node->name);
+               if (!child_xml) {
+                       ao2_ref(node, -1);
+                       continue;
+               }
+
+               switch (node->type) {
+               case AST_DATA_CONTAINER:
+                       data_get_xml_add_child(node, child_xml);
+                       break;
+               case AST_DATA_STRING:
+                       ast_xml_set_text(child_xml, node->payload.str);
+                       break;
+               case AST_DATA_INTEGER:
+                       snprintf(node_content, sizeof(node_content), "%d",
+                               node->payload.sint);
+                       ast_xml_set_text(child_xml, node_content);
+                       break;
+               case AST_DATA_UNSIGNED_INTEGER:
+                       snprintf(node_content, sizeof(node_content), "%u",
+                               node->payload.uint);
+                       ast_xml_set_text(child_xml, node_content);
+                       break;
+               case AST_DATA_DOUBLE:
+                       snprintf(node_content, sizeof(node_content), "%f",
+                               node->payload.dbl);
+                       ast_xml_set_text(child_xml, node_content);
+                       break;
+               case AST_DATA_BOOLEAN:
+                       if (node->payload.boolean) {
+                               ast_xml_set_text(child_xml, "true");
+                       } else {
+                               ast_xml_set_text(child_xml, "false");
+                       }
+                       break;
+               case AST_DATA_POINTER:
+                       snprintf(node_content, sizeof(node_content), "%p",
+                               node->payload.ptr);
+                       ast_xml_set_text(child_xml, node_content);
+                       break;
+               case AST_DATA_IPADDR:
+                       snprintf(node_content, sizeof(node_content), "%s",
+                               ast_inet_ntoa(node->payload.ipaddr));
+                       ast_xml_set_text(child_xml, node_content);
+                       break;
+               }
+               ast_xml_add_child(parent_xml, child_xml);
+
+               ao2_ref(node, -1);
+       }
+       ao2_iterator_destroy(&i);
+
+}
+
+struct ast_xml_doc *ast_data_get_xml(const struct ast_data_query *query)
+{
+       struct ast_xml_doc *doc;
+       struct ast_xml_node *root;
+       struct ast_data *res;
+
+       res = ast_data_get(query);
+       if (!res) {
+               return NULL;
+       }
+
+       doc = ast_xml_new();
+       if (!doc) {
+               return NULL;
+       }
+
+       root = ast_xml_new_node(res->name);
+       if (!root) {
+               ast_xml_close(doc);
+       }
+
+       ast_xml_set_root(doc, root);
+
+       data_get_xml_add_child(res, root);
+
+       ast_data_free(res);
+
+       return doc;
+}
+
+enum ast_data_type ast_data_retrieve_type(struct ast_data *node, const char *path)
+{
+       struct ast_data *internal;
+
+       internal = data_result_get_node(node, path);
+       if (!internal) {
+               return -1;
+       }
+
+       return internal->type;
+}
+
+char *ast_data_retrieve_name(struct ast_data *node)
+{
+       return node->name;
+}
+
+/*!
+ * \internal
+ * \brief Insert a child node inside a passed parent node.
+ * \param root Where we are going to insert the child node.
+ * \param name The name of the child node to add.
+ * \param type The type of content inside the child node.
+ * \param ptr The actual content of the child node.
+ * \retval NULL on error.
+ * \retval non-NULL The added child node pointer.
+ */
+static struct ast_data *__ast_data_add(struct ast_data *root, const char *name,
+       enum ast_data_type type, void *ptr)
+{
+       struct ast_data *node;
+       struct data_filter *filter, *filter_child = NULL;
+
+       if (!root || !root->children) {
+               /* invalid data result node. */
+               return NULL;
+       }
+
+       /* check if we need to add this node, based on the filter. */
+       if (root->filter) {
+               filter = data_filter_find(root->filter->children, name);
+               if (!filter) {
+                       return NULL;
+               }
+               ao2_ref(filter, -1);
+       }
+
+       node = data_result_create(name);
+       if (!node) {
+               return NULL;
+       }
+
+       node->type = type;
+
+       switch (type) {
+       case AST_DATA_BOOLEAN:
+               node->payload.boolean = *(unsigned int *) ptr;
+               break;
+       case AST_DATA_INTEGER:
+               node->payload.sint = *(unsigned int *) ptr;
+               break;
+       case AST_DATA_UNSIGNED_INTEGER:
+               node->payload.sint = *(unsigned int *) ptr;
+               break;
+       case AST_DATA_DOUBLE:
+               node->payload.dbl = *(double *) ptr;
+               break;
+       case AST_DATA_STRING:
+       case AST_DATA_POINTER:
+               node->payload.ptr = ptr;
+               break;
+       case AST_DATA_IPADDR:
+               node->payload.ipaddr = *(struct in_addr *) ptr;
+               break;
+       case AST_DATA_CONTAINER:
+               if (root->filter) {
+                       filter_child = data_filter_find(root->filter->children, name);
+                       if (filter_child) {
+                               /* do not increment the refcount because it is not neccesary. */
+                               ao2_ref(filter_child, -1);
+                       }
+               }
+               node->filter = filter_child;
+               break;
+       default:
+               break;
+       }
+
+       data_result_add_child(root, node);
+
+       ao2_ref(node, -1);
+
+       return node;
+}
+
+struct ast_data *ast_data_add_node(struct ast_data *root, const char *name)
+{
+       return __ast_data_add(root, name, AST_DATA_CONTAINER, NULL);
+}
+
+struct ast_data *ast_data_add_int(struct ast_data *root, const char *name, int value)
+{
+       return __ast_data_add(root, name, AST_DATA_INTEGER, &value);
+}
+
+struct ast_data *ast_data_add_uint(struct ast_data *root, const char *name,
+       unsigned int value)
+{
+       return __ast_data_add(root, name, AST_DATA_UNSIGNED_INTEGER, &value);
+}
+
+struct ast_data *ast_data_add_dbl(struct ast_data *root, const char *childname,
+       double dbl)
+{
+       return __ast_data_add(root, childname, AST_DATA_DOUBLE, &dbl);
+}
+
+struct ast_data *ast_data_add_bool(struct ast_data *root, const char *childname,
+       unsigned int boolean)
+{
+       return __ast_data_add(root, childname, AST_DATA_BOOLEAN, &boolean);
+}
+
+struct ast_data *ast_data_add_ipaddr(struct ast_data *root, const char *childname,
+       struct in_addr addr)
+{
+       return __ast_data_add(root, childname, AST_DATA_IPADDR, &addr);
+}
+
+struct ast_data *ast_data_add_ptr(struct ast_data *root, const char *childname,
+       void *ptr)
+{
+       return __ast_data_add(root, childname, AST_DATA_POINTER, ptr);
+}
+
+struct ast_data *ast_data_add_str(struct ast_data *root, const char *childname,
+       const char *value)
+{
+       char *name;
+       size_t namelen = 1 + (ast_strlen_zero(value) ? 0 : strlen(value));
+       struct ast_data *res;
+
+       if (!(name = ast_malloc(namelen))) {
+               return NULL;
+       }
+
+       strcpy(name, (ast_strlen_zero(value) ? "" : value));
+
+       res = __ast_data_add(root, childname, AST_DATA_STRING, name);
+       if (!res) {
+               ast_free(name);
+       }
+
+       return res;
+}
+
+int __ast_data_add_structure(struct ast_data *root,
+       const struct ast_data_mapping_structure *mapping, size_t mapping_len,
+       void *structure)
+{
+       int i;
+
+       for (i = 0; i < mapping_len; i++) {
+               switch (mapping[i].type) {
+               case AST_DATA_INTEGER:
+                       ast_data_add_int(root, mapping[i].name,
+                               mapping[i].get.AST_DATA_INTEGER(structure));
+                       break;
+               case AST_DATA_UNSIGNED_INTEGER:
+                       ast_data_add_uint(root, mapping[i].name,
+                               mapping[i].get.AST_DATA_UNSIGNED_INTEGER(structure));
+                       break;
+               case AST_DATA_DOUBLE:
+                       ast_data_add_dbl(root, mapping[i].name,
+                               mapping[i].get.AST_DATA_DOUBLE(structure));
+                       break;
+               case AST_DATA_BOOLEAN:
+                       ast_data_add_bool(root, mapping[i].name,
+                               mapping[i].get.AST_DATA_BOOLEAN(structure));
+                       break;
+               case AST_DATA_STRING:
+                       ast_data_add_str(root, mapping[i].name,
+                               mapping[i].get.AST_DATA_STRING(structure));
+                       break;
+               case AST_DATA_CONTAINER:
+                       break;
+               case AST_DATA_IPADDR:
+                       ast_data_add_ipaddr(root, mapping[i].name,
+                               mapping[i].get.AST_DATA_IPADDR(structure));
+                       break;
+               case AST_DATA_POINTER:
+                       ast_data_add_ptr(root, mapping[i].name,
+                               mapping[i].get.AST_DATA_POINTER(structure));
+                       break;
+               }
+       }
+
+       return 0;
+}
+
+void ast_data_remove_node(struct ast_data *root, struct ast_data *child)
+{
+       ao2_unlink(root->children, child);
+}
+
+void ast_data_free(struct ast_data *root)
+{
+       /* destroy it, this will destroy all the internal nodes. */
+       ao2_ref(root, -1);
+}
+
+struct ast_data_iterator *ast_data_iterator_init(struct ast_data *tree,
+       const char *elements)
+{
+       struct ast_data_iterator *iterator;
+       struct ao2_iterator i;
+       struct ast_data *internal = tree;
+       char *path, *ptr = NULL;
+
+       /* tree is the node we want to use to iterate? or we are going
+        * to iterate thow an internal node? */
+       if (elements) {
+               path = ast_strdupa(elements);
+
+               ptr = strrchr(path, '/');
+               if (ptr) {
+                       *ptr = '\0';
+                       internal = data_result_get_node(tree, path);
+                       if (!internal) {
+                               return NULL;
+                       }
+               }
+       }
+
+       iterator = ast_calloc(1, sizeof(*iterator));
+       if (!iterator) {
+               return NULL;
+       }
+
+       i = ao2_iterator_init(internal->children, 0);
+
+       iterator->pattern = (ptr ? strrchr(elements, '/') + 1 : elements);
+
+       /* is the last node a regular expression?, compile it! */
+       if (!regcomp(&(iterator->regex_pattern), iterator->pattern,
+                       REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
+               iterator->is_pattern = 1;
+       }
+
+       iterator->internal_iterator = i;
+
+       return iterator;
+}
+
+void ast_data_iterator_end(struct ast_data_iterator *iterator)
+{
+       /* decrement the reference counter. */
+       if (iterator->last) {
+               ao2_ref(iterator->last, -1);
+       }
+
+       /* release the generated pattern. */
+       if (iterator->is_pattern) {
+               regfree(&(iterator->regex_pattern));
+       }
+
+       ao2_iterator_destroy(&(iterator->internal_iterator));
+
+       ast_free(iterator);
+       iterator = NULL;
+}
+
+struct ast_data *ast_data_iterator_next(struct ast_data_iterator *iterator)
+{
+       struct ast_data *res;
+
+       if (iterator->last) {
+               /* release the last retrieved node reference. */
+               ao2_ref(iterator->last, -1);
+       }
+
+       while ((res = ao2_iterator_next(&iterator->internal_iterator))) {
+               /* if there is no node name pattern specified, return
+                * the next node. */
+               if (!iterator->pattern) {
+                       break;
+               }
+
+               /* if the pattern is a regular expression, check if this node
+                * matches. */
+               if (iterator->is_pattern && !regexec(&(iterator->regex_pattern),
+                       res->name, 0, NULL, 0)) {
+                       break;
+               }
+
+               /* if there is a pattern specified, check if this node matches
+                * the wanted node names. */
+               if (!iterator->is_pattern && (iterator->pattern &&
+                               !strcasecmp(res->name, iterator->pattern))) {
+                       break;
+               }
+
+               ao2_ref(res, -1);
+       }
+
+       iterator->last = res;
+
+       return res;
+}
+
+int ast_data_retrieve(struct ast_data *tree, const char *path,
+       struct ast_data_retrieve *content)
+{
+       struct ast_data *node;
+
+       if (!content) {
+               return -1;
+       }
+
+       node = data_result_get_node(tree, path);
+       if (!node) {
+               ast_log(LOG_ERROR, "Invalid internal node %s\n", path);
+               return -1;
+       }
+
+       content->type = node->type;
+       switch (node->type) {
+       case AST_DATA_STRING:
+               content->value.AST_DATA_STRING = node->payload.str;
+               break;
+       case AST_DATA_INTEGER:
+               content->value.AST_DATA_INTEGER = node->payload.sint;
+               break;
+       case AST_DATA_UNSIGNED_INTEGER:
+               content->value.AST_DATA_UNSIGNED_INTEGER = node->payload.uint;
+               break;
+       case AST_DATA_BOOLEAN:
+               content->value.AST_DATA_BOOLEAN = node->payload.boolean;
+               break;
+       case AST_DATA_IPADDR:
+               content->value.AST_DATA_IPADDR = node->payload.ipaddr;
+               break;
+       case AST_DATA_DOUBLE:
+               content->value.AST_DATA_DOUBLE = node->payload.dbl;
+               break;
+       case AST_DATA_CONTAINER:
+               break;
+       case AST_DATA_POINTER:
+               content->value.AST_DATA_POINTER = node->payload.ptr;
+               break;
+       }
+
+       return 0;
+}
+
+/*!
+ * \internal
+ * \brief One color for each node type.
+ */
+static const struct {
+       enum ast_data_type type;
+       int color;
+} data_result_color[] = {
+       { AST_DATA_STRING, COLOR_CYAN },
+       { AST_DATA_INTEGER, COLOR_RED },
+       { AST_DATA_UNSIGNED_INTEGER, COLOR_RED },
+       { AST_DATA_DOUBLE, COLOR_RED },
+       { AST_DATA_BOOLEAN, COLOR_BRRED },
+       { AST_DATA_CONTAINER, COLOR_GREEN },
+       { AST_DATA_IPADDR, COLOR_BROWN },
+       { AST_DATA_POINTER, COLOR_YELLOW },
+};
+
+/*!
+ * \internal
+ * \brief Get the color configured for a specific node type.
+ * \param[in] type The node type.
+ * \returns The color specified for the passed type.
+ */
+static int data_result_get_color(enum ast_data_type type)
+{
+       int i;
+       for (i = 0; i < ARRAY_LEN(data_result_color); i++) {
+               if (data_result_color[i].type == type) {
+                       return data_result_color[i].color;
+               }
+       }
+
+       return COLOR_BLUE;
+}
+
+/*!
+ * \internal
+ * \brief Print a node to the CLI.
+ * \param[in] fd The CLI file descriptor.
+ * \param[in] node The node to print.
+ * \param[in] depth The actual node depth in the tree.
+ */
+static void data_result_print_cli_node(int fd, const struct ast_data *node, uint32_t depth)
+{
+       int i;
+       struct ast_str *tabs, *output;
+
+       tabs = ast_str_create(depth * 10 + 1);
+       if (!tabs) {
+               return;
+       }
+       ast_str_reset(tabs);
+       for (i = 0; i < depth; i++) {
+               ast_str_append(&tabs, 0, "  ");
+       }
+
+       output = ast_str_create(20);
+       if (!output) {
+               ast_free(tabs);
+               return;
+       }
+
+       ast_str_reset(output);
+       ast_term_color_code(&output, data_result_get_color(node->type), 0);
+
+       switch (node->type) {
+       case AST_DATA_POINTER:
+               ast_str_append(&output, 0, "%s%s: %p\n", ast_str_buffer(tabs),
+                               node->name, node->payload.ptr);
+               break;
+       case AST_DATA_STRING:
+               ast_str_append(&output, 0, "%s%s: \"%s\"\n",
+                               ast_str_buffer(tabs),
+                               node->name,
+                               node->payload.str);
+               break;
+       case AST_DATA_CONTAINER:
+               ast_str_append(&output, 0, "%s%s\n", ast_str_buffer(tabs),
+                               node->name);
+               break;
+       case AST_DATA_INTEGER:
+               ast_str_append(&output, 0, "%s%s: %d\n", ast_str_buffer(tabs),
+                               node->name,
+                               node->payload.sint);
+               break;
+       case AST_DATA_UNSIGNED_INTEGER:
+               ast_str_append(&output, 0, "%s%s: %u\n", ast_str_buffer(tabs),
+                               node->name,
+                               node->payload.uint);
+               break;
+       case AST_DATA_DOUBLE:
+               ast_str_append(&output, 0, "%s%s: %lf\n", ast_str_buffer(tabs),
+                               node->name,
+                               node->payload.dbl);
+               break;
+       case AST_DATA_BOOLEAN:
+               ast_str_append(&output, 0, "%s%s: %s\n", ast_str_buffer(tabs),
+                               node->name,
+                               ((node->payload.boolean) ? "True" : "False"));
+               break;
+       case AST_DATA_IPADDR:
+               ast_str_append(&output, 0, "%s%s: %s\n", ast_str_buffer(tabs),
+                               node->name,
+                               ast_inet_ntoa(node->payload.ipaddr));
+               break;
+       }
+
+       ast_free(tabs);
+
+       ast_term_color_code(&output, COLOR_WHITE, 0);
+
+       ast_cli(fd, "%s", ast_str_buffer(output));
+
+       ast_free(output);
+
+       if (node->type == AST_DATA_CONTAINER) {
+               __data_result_print_cli(fd, node, depth + 1);
+       }
+}
+
+/*!
+ * \internal
+ * \brief Print out an ast_data tree to the CLI.
+ * \param[in] fd The CLI file descriptor.
+ * \param[in] root The root node of the tree.
+ * \param[in] depth Actual depth.
+ */
+static void __data_result_print_cli(int fd, const struct ast_data *root, uint32_t depth)
+{
+       struct ao2_iterator iter;
+       struct ast_data *node;
+
+       if (root->type == AST_DATA_CONTAINER) {
+               iter = ao2_iterator_init(root->children, 0);
+               while ((node = ao2_iterator_next(&iter))) {
+                       data_result_print_cli_node(fd, node, depth + 1);
+                       ao2_ref(node, -1);
+               }
+               ao2_iterator_destroy(&iter);
+       } else {
+               data_result_print_cli_node(fd, root, depth);
+       }
+}
+
+/*!
+ * \internal
+ * \brief
+ * \param[in] fd The CLI file descriptor.
+ * \param[in] root The root node of the tree.
+ */
+static void data_result_print_cli(int fd, const struct ast_data *root)
+{
+       struct ast_str *output;
+
+       /* print the initial node. */
+       output = ast_str_create(30);
+       if (!output) {
+               return;
+       }
+
+       ast_term_color_code(&output, data_result_get_color(root->type), 0);
+       ast_str_append(&output, 0, "%s\n", root->name);
+       ast_term_color_code(&output, COLOR_WHITE, 0);
+       ast_cli(fd, "%s", ast_str_buffer(output));
+       ast_free(output);
+
+       __data_result_print_cli(fd, root, 0);
+}
+
+/*!
+ * \internal
+ * \brief Handle the CLI command "data get".
+ */
+static char *handle_cli_data_get(struct ast_cli_entry *e, int cmd,
+               struct ast_cli_args *a)
+{
+       struct ast_data_query query = {
+               .version = AST_DATA_QUERY_VERSION
+       };
+       struct ast_data *tree;
+
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "data get";
+               e->usage = ""
+                       "Usage: data get <path> [<search> [<filter>]]\n"
+                       "       Get the tree based on a path.\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
+       }
+
+       if (a->argc < e->args + 1) {
+               return CLI_SHOWUSAGE;
+       }
+
+       query.path = (char *) a->argv[e->args];
+
+       if (a->argc > e->args + 1) {
+               query.search = (char *) a->argv[e->args + 1];
+       }
+
+       if (a->argc > e->args + 2) {
+               query.filter = (char *) a->argv[e->args + 2];
+       }
+
+       tree = ast_data_get(&query);
+       if (!tree) {
+               return CLI_FAILURE;
+       }
+
+       data_result_print_cli(a->fd, tree);
+
+       ast_data_free(tree);
+
+       return CLI_SUCCESS;
+}
+
+/*!
+ * \internal
+ * \brief Print the list of data providers.
+ * \param[in] fd The CLI file descriptor.
+ * \param[in] name The last node visited name.
+ * \param[in] container The childrens of the last node.
+ * \param[in] path The path to the current node.
+ */
+static void data_provider_print_cli(int fd, const char *name,
+       struct ao2_container *container, struct ast_str *path)
+{
+       struct ao2_iterator i;
+       struct ast_str *current_path;
+       struct data_provider *provider;
+
+       current_path = ast_str_create(60);
+       if (!current_path) {
+               return;
+       }
+
+       ast_str_reset(current_path);
+       if (path) {
+               ast_str_set(&current_path, 0, "%s/%s", ast_str_buffer(path), name);
+       } else {
+               ast_str_set(&current_path, 0, "%s", name);
+       }
+
+       i = ao2_iterator_init(container, 0);
+       while ((provider = ao2_iterator_next(&i))) {
+               if (provider->handler) {
+                       /* terminal node, print it. */
+                       ast_cli(fd, "%s/%s (", ast_str_buffer(current_path),
+                               provider->name);
+                       if (provider->handler->get) {
+                               ast_cli(fd, "get");
+                       }
+                       ast_cli(fd, ") [%s]\n", provider->registrar);
+               }
+               data_provider_print_cli(fd, provider->name, provider->children,
+                       current_path);
+               ao2_ref(provider, -1);
+       }
+       ao2_iterator_destroy(&i);
+
+       ast_free(current_path);
+}
+
+/*!
+ * \internal
+ * \brief Handle CLI command "data show providers"
+ */
+static char *handle_cli_data_show_providers(struct ast_cli_entry *e, int cmd,
+               struct ast_cli_args *a)
+{
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "data show providers";
+               e->usage = ""
+                       "Usage: data show providers\n"
+                       "       Show the list of registered providers\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
+       }
+
+       data_read_lock();
+       data_provider_print_cli(a->fd, "", root_data.container, NULL);
+       data_unlock();
+
+       return CLI_SUCCESS;
+}
+
+/*!
+ * \internal
+ * \brief Data API CLI commands.
+ */
+static struct ast_cli_entry cli_data[] = {
+       AST_CLI_DEFINE(handle_cli_data_get, "Data API get"),
+       AST_CLI_DEFINE(handle_cli_data_show_providers, "Show data providers")
+};
+
+/*!
+ * \internal
+ * \brief Output a tree to the AMI.
+ * \param[in] s AMI session.
+ * \param[in] name The root node name.
+ * \param[in] container The root container.
+ * \param[in] path The current path.
+ */
+static void data_result_manager_output(struct mansession *s, const char *name,
+       struct ao2_container *container, struct ast_str *path, int id)
+{
+       struct ao2_iterator i;
+       struct ast_str *current_path;
+       struct ast_data *node;
+       int current_id = id;
+
+       current_path = ast_str_create(60);
+       if (!current_path) {
+               return;
+       }
+
+       ast_str_reset(current_path);
+       if (path) {
+               ast_str_set(&current_path, 0, "%s.%s", ast_str_buffer(path), name);
+       } else {
+               ast_str_set(&current_path, 0, "%s", name);
+       }
+
+       i = ao2_iterator_init(container, 0);
+       while ((node = ao2_iterator_next(&i))) {
+               /* terminal node, print it. */
+               if (node->type != AST_DATA_CONTAINER) {
+                       astman_append(s, "%d-%s.%s", id, ast_str_buffer(current_path),
+                                       node->name);
+               }
+               switch (node->type) {
+               case AST_DATA_CONTAINER:
+                       data_result_manager_output(s, node->name, node->children, current_path, ++current_id);
+                       break;
+               case AST_DATA_INTEGER:
+                       astman_append(s, ": %d\r\n", node->payload.sint);
+                       break;
+               case AST_DATA_UNSIGNED_INTEGER:
+                       astman_append(s, ": %u\r\n", node->payload.uint);
+                       break;
+               case AST_DATA_STRING:
+                       astman_append(s, ": %s\r\n", node->payload.str);
+                       break;
+               case AST_DATA_IPADDR:
+                       astman_append(s, ": %s\r\n", ast_inet_ntoa(node->payload.ipaddr));
+                       break;
+               case AST_DATA_POINTER:
+                       break;
+               case AST_DATA_DOUBLE:
+                       astman_append(s, ": %f\r\n", node->payload.dbl);
+                       break;
+               case AST_DATA_BOOLEAN:
+                       astman_append(s, ": %s\r\n",
+                               (node->payload.boolean ? "True" : "False"));
+                       break;
+               }
+
+               ao2_ref(node, -1);
+       }
+       ao2_iterator_destroy(&i);
+
+       ast_free(current_path);
+}
+
+/*!
+ * \internal
+ * \brief Implements the manager action: "DataGet".
+ */
+static int manager_data_get(struct mansession *s, const struct message *m)
+{
+       const char *path = astman_get_header(m, "Path");
+       const char *search = astman_get_header(m, "Search");
+       const char *filter = astman_get_header(m, "Filter");
+       const char *id = astman_get_header(m, "ActionID");
+       struct ast_data *res;
+       struct ast_data_query query = {
+               .version = AST_DATA_QUERY_VERSION,
+               .path = (char *) path,
+               .search = (char *) search,
+               .filter = (char *) filter,
+       };
+
+       if (ast_strlen_zero(path)) {
+               astman_send_error(s, m, "'Path' parameter not specified");
+               return 0;
+       }
+
+       res = ast_data_get(&query);
+       if (!res) {
+               astman_send_error(s, m, "No data returned");
+               return 0;
+       }
+
+       astman_append(s, "Event: DataGet Tree\r\n");
+       if (!ast_strlen_zero(id)) {
+               astman_append(s, "ActionID: %s\r\n", id);
+       }
+       data_result_manager_output(s, res->name, res->children, NULL, 0);
+       astman_append(s, "\r\n");
+
+       ast_data_free(res);
+
+       return RESULT_SUCCESS;
+}
+
+#ifdef TEST_FRAMEWORK
+
+/*!
+ * \internal
+ * \brief Structure used to test how to add a complete structure,
+ *        and how to compare it.
+ */
+struct test_structure {
+       int a_int;
+       unsigned int b_bool:1;
+       char *c_str;
+       unsigned int a_uint;
+};
+
+/*!
+ * \internal
+ * \brief test_structure mapping.
+ */
+#define DATA_EXPORT_TEST_STRUCTURE(MEMBER)                              \
+       MEMBER(test_structure, a_int, AST_DATA_INTEGER)                 \
+       MEMBER(test_structure, b_bool, AST_DATA_BOOLEAN)                \
+       MEMBER(test_structure, c_str, AST_DATA_STRING)                  \
+       MEMBER(test_structure, a_uint, AST_DATA_UNSIGNED_INTEGER)
+
+AST_DATA_STRUCTURE(test_structure, DATA_EXPORT_TEST_STRUCTURE);
+
+/*!
+ * \internal
+ * \brief Callback implementation.
+ */
+static int test_data_full_provider(const struct ast_data_search *search,
+               struct ast_data *root)
+{
+       struct ast_data *test_structure;
+       struct test_structure local_test_structure = {
+               .a_int = 10,
+               .b_bool = 1,
+               .c_str = "test string",
+               .a_uint = 20
+       };
+
+       if (ast_data_search_cmp_structure(search, test_structure, &local_test_structure, "test_structure")) {
+               return 0;
+       }
+
+       test_structure = ast_data_add_node(root, "test_structure");
+       if (!test_structure) {
+               ast_debug(1, "Internal data api error\n");
+               return 0;
+       }
+
+       /* add the complete structure. */
+       ast_data_add_structure(test_structure, test_structure, &local_test_structure);
+
+       return 0;
+}
+
+/*!
+ * \internal
+ * \brief Handler definition for the full provider.
+ */
+static const struct ast_data_handler full_provider = {
+       .version = AST_DATA_HANDLER_VERSION,
+       .get = test_data_full_provider
+};
+
+/*!
+ * \internal
+ * \brief Structure used to define multiple providers at once.
+ */
+static const struct ast_data_entry test_providers[] = {
+       AST_DATA_ENTRY("test/node1/node11/node111", &full_provider)
+};
+
+AST_TEST_DEFINE(test_data_get)
+{
+       struct ast_data *res, *node;
+       struct ast_data_iterator *i;
+       struct ast_data_query query = {
+               .version = AST_DATA_QUERY_VERSION,
+               .path = "test/node1/node11/node111",
+               .search = "node111/test_structure/a_int=10",
+               .filter = "node111/test_structure/a*int"
+       };
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "data_test";
+               info->category = "main/data/";
+               info->summary = "Data API unit test";
+               info->description =
+                       "Tests whether data API get implementation works as expected.";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
+
+       ast_data_register_multiple_core(test_providers, ARRAY_LEN(test_providers));
+
+       res = ast_data_get(&query);
+       if (!res) {
+               ast_test_status_update(test, "Unable to get tree.");
+               ast_data_unregister("test/node1/node11/node111");
+               return AST_TEST_FAIL;
+       }
+
+       /* initiate the iterator and check for errors. */
+       i = ast_data_iterator_init(res, "test_structure/");
+       if (!i) {
+               ast_test_status_update(test, "Unable to initiate the iterator.");
+               ast_data_free(res);
+               ast_data_unregister("test/node1/node11/node111");
+               return AST_TEST_FAIL;
+       }
+
+       /* walk the returned nodes. */
+       while ((node = ast_data_iterator_next(i))) {
+               if (!strcmp(ast_data_retrieve_name(node), "a_int")) {
+                       if (ast_data_retrieve_int(node, "/") != 10) {
+                               ast_data_iterator_end(i);
+                               ast_data_free(res);
+                               ast_data_unregister("test/node1/node11/node111");
+                               return AST_TEST_FAIL;
+                       }
+               } else if (!strcmp(ast_data_retrieve_name(node), "a_uint")) {
+                       if (ast_data_retrieve_uint(node, "/") != 20) {
+                               ast_data_iterator_end(i);
+                               ast_data_free(res);
+                               ast_data_unregister("test/node1/node11/node111");
+                               return AST_TEST_FAIL;
+                       }
+               }
+       }
+
+       /* finish the iterator. */
+       ast_data_iterator_end(i);
+
+       ast_data_free(res);
+
+       ast_data_unregister("test/node1/node11/node111");
+
+       return AST_TEST_PASS;
+}
+
+#endif
+
+int ast_data_init(void)
+{
+       int res = 0;
+
+       ast_rwlock_init(&root_data.lock);
+
+       if (!(root_data.container = ao2_container_alloc(NUM_DATA_NODE_BUCKETS,
+               data_provider_hash, data_provider_cmp))) {
+               return -1;
+       }
+
+       res |= ast_cli_register_multiple(cli_data, ARRAY_LEN(cli_data));
+
+       res |= ast_manager_register_xml("DataGet", 0, manager_data_get);
+
+#ifdef TEST_FRAMEWORK
+       AST_TEST_REGISTER(test_data_get);
+#endif
+
+       return res;
+}
index 0f93abc..e28a989 100644 (file)
@@ -68,6 +68,46 @@ struct ast_xml_doc *ast_xml_open(char *filename)
        return (struct ast_xml_doc *) doc;
 }
 
+struct ast_xml_doc *ast_xml_new(void)
+{
+       xmlDoc *doc;
+
+       doc = xmlNewDoc((const xmlChar *) "1.0");
+       return (struct ast_xml_doc *) doc;
+}
+
+struct ast_xml_node *ast_xml_new_node(const char *name)
+{
+       xmlNode *node;
+       if (!name) {
+               return NULL;
+       }
+
+       node = xmlNewNode(NULL, (const xmlChar *) name);
+
+       return (struct ast_xml_node *) node;
+}
+
+struct ast_xml_node *ast_xml_new_child(struct ast_xml_node *parent, const char *child_name)
+{
+       xmlNode *child;
+
+       if (!parent || !child_name) {
+               return NULL;
+       }
+
+       child = xmlNewChild((xmlNode *) parent, NULL, (const xmlChar *) child_name, NULL);
+       return (struct ast_xml_node *) child;
+}
+
+struct ast_xml_node *ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child)
+{
+       if (!parent || !child) {
+               return NULL;
+       }
+       return (struct ast_xml_node *) xmlAddChild((xmlNode *) parent, (xmlNode *) child);
+}
+
 struct ast_xml_doc *ast_xml_read_memory(char *buffer, size_t size)
 {
        xmlDoc *doc;
@@ -97,6 +137,14 @@ void ast_xml_close(struct ast_xml_doc *doc)
        doc = NULL;
 }
 
+void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node)
+{
+       if (!doc || !node) {
+               return;
+       }
+
+       xmlDocSetRootElement((xmlDoc *) doc, (xmlNode *) node);
+}
 
 struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc)
 {
@@ -152,6 +200,19 @@ const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrnam
        return (const char *) attrvalue;
 }
 
+int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value)
+{
+       if (!name || !value) {
+               return -1;
+       }
+
+       if (!xmlSetProp((xmlNode *) node, (xmlChar *) name, (xmlChar *) value)) {
+               return -1;
+       }
+
+       return 0;
+}
+
 struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
 {
        struct ast_xml_node *cur;
@@ -184,6 +245,15 @@ struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const
        return NULL;
 }
 
+struct ast_xml_doc *ast_xml_get_doc(struct ast_xml_node *node)
+{
+       if (!node) {
+               return NULL;
+       }
+
+       return (struct ast_xml_doc *) ((xmlNode *)node)->doc;
+}
+
 struct ast_xml_ns *ast_xml_find_namespace(struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name) {
        xmlNsPtr ns = xmlSearchNs((xmlDocPtr) doc, (xmlNodePtr) node, (xmlChar *) ns_name);
        return (struct ast_xml_ns *) ns;
@@ -203,6 +273,20 @@ const char *ast_xml_get_text(struct ast_xml_node *node)
        return (const char *) xmlNodeGetContent((xmlNode *) node);
 }
 
+void ast_xml_set_text(struct ast_xml_node *node, const char *content)
+{
+       if (!node || !content) {
+               return;
+       }
+
+       xmlNodeSetContent((xmlNode *) node, (const xmlChar *) content);
+}
+
+int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)
+{
+       return xmlDocDump(output, (xmlDocPtr)doc);
+}
+
 const char *ast_xml_node_get_name(struct ast_xml_node *node)
 {
        return (const char *) ((xmlNode *) node)->name;