Add multi-channel Stasis messages; refactor Dial AMI events to Stasis
authorMatthew Jordan <mjordan@digium.com>
Mon, 8 Apr 2013 14:26:37 +0000 (14:26 +0000)
committerMatthew Jordan <mjordan@digium.com>
Mon, 8 Apr 2013 14:26:37 +0000 (14:26 +0000)
This patch does the following:
 * A new Stasis payload has been defined for multi-channel messages. This
   payload can store multiple ast_channel_snapshot objects along with a single
   JSON blob. The payload object itself is opaque; the snapshots are stored
   in a container keyed by roles. APIs have been provided to query for and
   retrieve the snapshots from the payload object.
 * The Dial AMI events have been refactored onto Stasis. This includes dial
   messages in app_dial, as well as the core dialing framework. The AMI events
   have been modified to send out a DialBegin/DialEnd events, as opposed to
   the subevent type that was previously used.
 * Stasis messages, types, and other objects related to channels have been
   placed in their own file, stasis_channels. Unit tests for some of these
   objects/messages have also been written.

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

17 files changed:
CHANGES
apps/app_dial.c
apps/app_stasis.c
apps/app_userevent.c
apps/stasis_json.c
include/asterisk/app_stasis.h
include/asterisk/channel.h
include/asterisk/stasis_channels.h [new file with mode: 0644]
main/channel.c
main/channel_internal_api.c
main/dial.c
main/features.c
main/manager_channels.c
main/pbx.c
main/stasis_channels.c [new file with mode: 0644]
pbx/pbx_realtime.c
tests/test_stasis_channels.c [new file with mode: 0644]

diff --git a/CHANGES b/CHANGES
index 8bcd533..fa7cbff 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -53,6 +53,12 @@ AMI (Asterisk Manager Interface)
  * The deprecated use of | (pipe) as a separator in the channelvars setting in
    manager.conf has been removed.
 
+ * Channel Variables conveyed with a channel no longer contain the name of the
+   channel as part of the key field, i.e., ChanVariable(SIP/foo): bar=baz is now
+   ChanVariable: bar=baz. When multiple channels are present in a single AMI
+   event, the various ChanVariable fields will contain a suffix that specifies
+   which channel they correspond to.
+
 Channel Drivers
 ------------------
 
index 0c6b288..74f41c0 100644 (file)
@@ -67,6 +67,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/ccss.h"
 #include "asterisk/indications.h"
 #include "asterisk/framehook.h"
+#include "asterisk/stasis_channels.h"
 
 /*** DOCUMENTATION
        <application name="Dial" language="en_US">
@@ -818,63 +819,6 @@ static const char *get_cid_name(char *name, int namelen, struct ast_channel *cha
        return ast_get_hint(NULL, 0, name, namelen, chan, context, exten) ? name : "";
 }
 
-static void senddialevent(struct ast_channel *src, struct ast_channel *dst, const char *dialstring)
-{
-       struct ast_channel *chans[] = { src, dst };
-       /*** DOCUMENTATION
-               <managerEventInstance>
-                       <synopsis>Raised when a dial action has started.</synopsis>
-                       <syntax>
-                               <parameter name="SubEvent">
-                                       <para>A sub event type, specifying whether the dial action has begun or ended.</para>
-                                       <enumlist>
-                                               <enum name="Begin"/>
-                                               <enum name="End"/>
-                                       </enumlist>
-                               </parameter>
-                       </syntax>
-               </managerEventInstance>
-       ***/
-       ast_manager_event_multichan(EVENT_FLAG_CALL, "Dial", 2, chans,
-               "SubEvent: Begin\r\n"
-               "Channel: %s\r\n"
-               "Destination: %s\r\n"
-               "CallerIDNum: %s\r\n"
-               "CallerIDName: %s\r\n"
-               "ConnectedLineNum: %s\r\n"
-               "ConnectedLineName: %s\r\n"
-               "UniqueID: %s\r\n"
-               "DestUniqueID: %s\r\n"
-               "Dialstring: %s\r\n",
-               ast_channel_name(src), ast_channel_name(dst),
-               S_COR(ast_channel_caller(src)->id.number.valid, ast_channel_caller(src)->id.number.str, "<unknown>"),
-               S_COR(ast_channel_caller(src)->id.name.valid, ast_channel_caller(src)->id.name.str, "<unknown>"),
-               S_COR(ast_channel_connected(src)->id.number.valid, ast_channel_connected(src)->id.number.str, "<unknown>"),
-               S_COR(ast_channel_connected(src)->id.name.valid, ast_channel_connected(src)->id.name.str, "<unknown>"),
-               ast_channel_uniqueid(src), ast_channel_uniqueid(dst),
-               dialstring ? dialstring : "");
-}
-
-static void senddialendevent(struct ast_channel *src, const char *dialstatus)
-{
-       /*** DOCUMENTATION
-               <managerEventInstance>
-                       <synopsis>Raised when a dial action has ended.</synopsis>
-                       <syntax>
-                               <parameter name="DialStatus">
-                                       <para>The value of the <variable>DIALSTATUS</variable> channel variable.</para>
-                               </parameter>
-                       </syntax>
-               </managerEventInstance>
-       ***/
-       ast_manager_event(src, EVENT_FLAG_CALL, "Dial",
-               "SubEvent: End\r\n"
-               "Channel: %s\r\n"
-               "UniqueID: %s\r\n"
-               "DialStatus: %s\r\n",
-               ast_channel_name(src), ast_channel_uniqueid(src), dialstatus);
-}
-
 /*!
  * helper function for wait_for_answer()
  *
@@ -1069,7 +1013,7 @@ static void do_forward(struct chanlist *o, struct cause_args *num,
                        num->nochan++;
                } else {
                        ast_channel_lock_both(c, in);
-                       senddialevent(in, c, stuff);
+                       ast_channel_publish_dial(c, in, stuff, NULL);
                        ast_channel_unlock(in);
                        ast_channel_unlock(c);
                        /* Hangup the original channel now, in case we needed it */
@@ -1090,6 +1034,33 @@ struct privacy_args {
        char status[256];
 };
 
+static const char *hangup_cause_to_dial_status(int hangup_cause)
+{
+       switch(hangup_cause) {
+       case AST_CAUSE_BUSY:
+               return "BUSY";
+       case AST_CAUSE_CONGESTION:
+               return "CONGESTION";
+       case AST_CAUSE_NO_ROUTE_DESTINATION:
+       case AST_CAUSE_UNREGISTERED:
+               return "CHANUNAVAIL";
+       case AST_CAUSE_NO_ANSWER:
+       default:
+               return "NOANSWER";
+       }
+}
+
+static void publish_dial_end_event(struct ast_channel *in, struct dial_head *out_chans, struct ast_channel *exception, const char *status)
+{
+       struct chanlist *outgoing;
+       AST_LIST_TRAVERSE(out_chans, outgoing, node) {
+               if (!outgoing->chan || outgoing->chan == exception) {
+                       continue;
+               }
+               ast_channel_publish_dial(in, outgoing->chan, NULL, status);
+       }
+}
+
 static struct ast_channel *wait_for_answer(struct ast_channel *in,
        struct dial_head *out_chans, int *to, struct ast_flags64 *peerflags,
        char *opt_args[],
@@ -1133,6 +1104,7 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                                *to = -1;
                                strcpy(pa->status, "CONGESTION");
                                ast_cdr_failed(ast_channel_cdr(in));
+                               ast_channel_publish_dial(in, outgoing->chan, NULL, pa->status);
                                return NULL;
                        }
                }
@@ -1293,6 +1265,7 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
 #ifdef HAVE_EPOLL
                                ast_poll_channel_del(in, c);
 #endif
+                               ast_channel_publish_dial(in, c, NULL, hangup_cause_to_dial_status(ast_channel_hangupcause(c)));
                                ast_hangup(c);
                                c = o->chan = NULL;
                                ast_clear_flag64(o, DIAL_STILLGOING);
@@ -1333,6 +1306,8 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                                                        }
                                                }
                                                peer = c;
+                                               ast_channel_publish_dial(in, peer, NULL, "ANSWER");
+                                               publish_dial_end_event(in, out_chans, peer, "CANCEL");
                                                if (ast_channel_cdr(peer)) {
                                                        ast_channel_cdr(peer)->answer = ast_tvnow();
                                                        ast_channel_cdr(peer)->disposition = AST_CDR_ANSWERED;
@@ -1346,9 +1321,10 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                                                        DIAL_NOFORWARDHTML);
                                                ast_channel_dialcontext_set(c, "");
                                                ast_channel_exten_set(c, "");
-                                               if (CAN_EARLY_BRIDGE(peerflags, in, peer))
+                                               if (CAN_EARLY_BRIDGE(peerflags, in, peer)) {
                                                        /* Setup early bridge if appropriate */
                                                        ast_channel_early_bridge(in, peer);
+                                               }
                                        }
                                        /* If call has been answered, then the eventual hangup is likely to be normal hangup */
                                        ast_channel_hangupcause_set(in, AST_CAUSE_NORMAL_CLEARING);
@@ -1357,6 +1333,7 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                                case AST_CONTROL_BUSY:
                                        ast_verb(3, "%s is busy\n", ast_channel_name(c));
                                        ast_channel_hangupcause_set(in, ast_channel_hangupcause(c));
+                                       ast_channel_publish_dial(in, c, NULL, hangup_cause_to_dial_status(ast_channel_hangupcause(c)));
                                        ast_hangup(c);
                                        c = o->chan = NULL;
                                        ast_clear_flag64(o, DIAL_STILLGOING);
@@ -1365,6 +1342,7 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                                case AST_CONTROL_CONGESTION:
                                        ast_verb(3, "%s is circuit-busy\n", ast_channel_name(c));
                                        ast_channel_hangupcause_set(in, ast_channel_hangupcause(c));
+                                       ast_channel_publish_dial(in, c, NULL, hangup_cause_to_dial_status(ast_channel_hangupcause(c)));
                                        ast_hangup(c);
                                        c = o->chan = NULL;
                                        ast_clear_flag64(o, DIAL_STILLGOING);
@@ -1572,6 +1550,7 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                                *to = -1;
                                strcpy(pa->status, "CANCEL");
                                ast_cdr_noanswer(ast_channel_cdr(in));
+                               publish_dial_end_event(in, out_chans, NULL, pa->status);
                                if (f) {
                                        if (f->data.uint32) {
                                                ast_channel_hangupcause_set(in, f->data.uint32);
@@ -1596,6 +1575,7 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                                                ast_cdr_noanswer(ast_channel_cdr(in));
                                                *result = f->subclass.integer;
                                                strcpy(pa->status, "CANCEL");
+                                               publish_dial_end_event(in, out_chans, NULL, pa->status);
                                                ast_frfree(f);
                                                ast_channel_unlock(in);
                                                if (is_cc_recall) {
@@ -1612,6 +1592,7 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in,
                                        *to = 0;
                                        strcpy(pa->status, "CANCEL");
                                        ast_cdr_noanswer(ast_channel_cdr(in));
+                                       publish_dial_end_event(in, out_chans, NULL, pa->status);
                                        ast_frfree(f);
                                        if (is_cc_recall) {
                                                ast_cc_completed(in, "CC completed, but the caller hung up with DTMF");
@@ -1707,6 +1688,7 @@ skip_frame:;
 
        if (!*to) {
                ast_verb(3, "Nobody picked up in %d ms\n", orig);
+               publish_dial_end_event(in, out_chans, NULL, "NOANSWER");
        }
        if (!*to || ast_check_hangup(in)) {
                ast_cdr_noanswer(ast_channel_cdr(in));
@@ -2621,7 +2603,7 @@ static int dial_exec_full(struct ast_channel *chan, const char *data, struct ast
                        continue;
                }
 
-               senddialevent(chan, tmp->chan, tmp->number);
+               ast_channel_publish_dial(chan, tmp->chan, tmp->number, NULL);
                ast_channel_unlock(chan);
 
                ast_verb(3, "Called %s\n", tmp->interface);
@@ -3098,7 +3080,6 @@ out:
        ast_channel_early_bridge(chan, NULL);
        hanguptree(&out_chans, NULL, ast_channel_hangupcause(chan)==AST_CAUSE_ANSWERED_ELSEWHERE || ast_test_flag64(&opts, OPT_CANCEL_ELSEWHERE) ? 1 : 0 ); /* forward 'answered elsewhere' if we received it */
        pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
-       senddialendevent(chan, pa.status);
        ast_debug(1, "Exiting with DIALSTATUS=%s.\n", pa.status);
 
        if ((ast_test_flag64(peerflags, OPT_GO_ON)) && !ast_check_hangup(chan) && (res != AST_PBX_INCOMPLETE)) {
index 769c915..d60f42f 100644 (file)
@@ -38,6 +38,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/module.h"
 #include "asterisk/stasis.h"
 #include "asterisk/strings.h"
+#include "asterisk/stasis_channels.h"
 
 /*** DOCUMENTATION
        <application name="Stasis" language="en_US">
index cfc0943..0c44cf2 100644 (file)
@@ -34,6 +34,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/manager.h"
 #include "asterisk/app.h"
 #include "asterisk/json.h"
+#include "asterisk/stasis_channels.h"
 
 /*** DOCUMENTATION
        <application name="UserEvent" language="en_US">
index 6b66b5d..a62aba1 100644 (file)
@@ -28,6 +28,7 @@
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "asterisk/app_stasis.h"
+#include "asterisk/stasis_channels.h"
 
 struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot)
 {
index 921a35e..3001992 100644 (file)
@@ -49,6 +49,8 @@
 #include "asterisk/channel.h"
 #include "asterisk/json.h"
 
+struct ast_channel_snapshot;
+
 /*! @{ */
 
 /*!
index 91d2f80..939c8db 100644 (file)
@@ -365,7 +365,7 @@ struct ast_party_dialed {
  * PSTN gateway).
  *
  * \todo Implement settings for transliteration between UTF8 Caller ID names in
- *       to ASCII Caller ID's (DAHDI). Östen Åsklund might be transliterated into
+ *       to ASCII Caller ID's (DAHDI). Östen Åsklund might be transliterated into
  *       Osten Asklund or Oesten Aasklund depending upon language and person...
  *       We need automatic routines for incoming calls and static settings for
  *       our own accounts.
@@ -3015,7 +3015,7 @@ void ast_party_id_reset(struct ast_party_id *id);
  *
  * \details
  * This function will generate an effective party id.
- * 
+ *
  * Each party id component of the party id 'base' is overwritten
  * by components of the party id 'overlay' if the overlay
  * component is marked as valid.  However the component 'tag' of
@@ -3788,7 +3788,7 @@ int ast_channel_get_cc_agent_type(struct ast_channel *chan, char *agent_type, si
 void ast_channel_unlink(struct ast_channel *chan);
 
 /*!
- * \brief Sets the HANGUPCAUSE hash and optionally the SIP_CAUSE hash 
+ * \brief Sets the HANGUPCAUSE hash and optionally the SIP_CAUSE hash
  * on the given channel
  *
  * \param chan channel on which to set the cause information
@@ -4125,69 +4125,6 @@ void ast_channel_set_manager_vars(size_t varc, char **vars);
  */
 struct varshead *ast_channel_get_manager_vars(struct ast_channel *chan);
 
-/*! \addtogroup StasisTopicsAndMessages
- * @{
- */
-
-/*!
- * \since 12
- * \brief Structure representing a snapshot of channel state.
- *
- * While not enforced programmatically, this object is shared across multiple
- * threads, and should be threated as an immutable object.
- */
-struct ast_channel_snapshot {
-       AST_DECLARE_STRING_FIELDS(
-               AST_STRING_FIELD(name);                 /*!< ASCII unique channel name */
-               AST_STRING_FIELD(accountcode);          /*!< Account code for billing */
-               AST_STRING_FIELD(peeraccount);          /*!< Peer account code for billing */
-               AST_STRING_FIELD(userfield);            /*!< Userfield for CEL billing */
-               AST_STRING_FIELD(uniqueid);             /*!< Unique Channel Identifier */
-               AST_STRING_FIELD(linkedid);             /*!< Linked Channel Identifier -- gets propagated by linkage */
-               AST_STRING_FIELD(parkinglot);           /*!< Default parking lot, if empty, default parking lot */
-               AST_STRING_FIELD(hangupsource);         /*!< Who is responsible for hanging up this channel */
-               AST_STRING_FIELD(appl);                 /*!< Current application */
-               AST_STRING_FIELD(data);                 /*!< Data passed to current application */
-               AST_STRING_FIELD(context);              /*!< Dialplan: Current extension context */
-               AST_STRING_FIELD(exten);                /*!< Dialplan: Current extension number */
-               AST_STRING_FIELD(caller_name);          /*!< Caller ID Name */
-               AST_STRING_FIELD(caller_number);        /*!< Caller ID Number */
-               AST_STRING_FIELD(connected_name);       /*!< Connected Line Name */
-               AST_STRING_FIELD(connected_number);     /*!< Connected Line Number */
-       );
-
-       struct timeval creationtime;    /*!< The time of channel creation */
-       enum ast_channel_state state;   /*!< State of line */
-       int priority;                   /*!< Dialplan: Current extension priority */
-       int amaflags;                   /*!< AMA flags for billing */
-       int hangupcause;                /*!< Why is the channel hanged up. See causes.h */
-       int caller_pres;                /*!< Caller ID presentation. */
-
-       struct ast_flags flags;         /*!< channel flags of AST_FLAG_ type */
-
-       struct varshead *manager_vars;  /*!< Variables to be appended to manager events */
-};
-
-/*!
- * \since 12
- * \brief Generate a snapshot of the channel state. This is an ao2 object, so
- * ao2_cleanup() to deallocate.
- *
- * \param chan The channel from which to generate a snapshot
- *
- * \retval pointer on success (must be ast_freed)
- * \retval NULL on error
- */
-struct ast_channel_snapshot *ast_channel_snapshot_create(struct ast_channel *chan);
-
-/*!
- * \since 12
- * \brief Message type for \ref ast_channel_snapshot.
- *
- * \retval Message type for \ref ast_channel_snapshot.
- */
-struct stasis_message_type *ast_channel_snapshot_type(void);
-
 /*!
  * \since 12
  * \brief A topic which publishes the events for a particular channel.
@@ -4201,81 +4138,4 @@ struct stasis_message_type *ast_channel_snapshot_type(void);
  */
 struct stasis_topic *ast_channel_topic(struct ast_channel *chan);
 
-/*!
- * \since 12
- * \brief A topic which publishes the events for all channels.
- * \retval Topic for all channel events.
- */
-struct stasis_topic *ast_channel_topic_all(void);
-
-/*!
- * \since 12
- * \brief A caching topic which caches \ref ast_channel_snapshot messages from
- * ast_channel_events_all(void).
- *
- * \retval Topic for all channel events.
- */
-struct stasis_caching_topic *ast_channel_topic_all_cached(void);
-
-/*!
- * \since 12
- * \brief Blob of data associated with a channel.
- *
- * The \c blob is actually a JSON object of structured data. It has a "type" field
- * which contains the type string describing this blob.
- */
-struct ast_channel_blob {
-       /*! Channel blob is associated with (or NULL for global/all channels) */
-       struct ast_channel_snapshot *snapshot;
-       /*! JSON blob of data */
-       struct ast_json *blob;
-};
-
-/*!
- * \since 12
- * \brief Message type for \ref ast_channel_blob messages.
- *
- * \retval Message type for \ref ast_channel_blob messages.
- */
-struct stasis_message_type *ast_channel_blob_type(void);
-
-/*!
- * \since 12
- * \brief Extracts the type field from a \ref ast_channel_blob.
- * Returned \c char* is still owned by \a obj
- * \param obj Channel blob object.
- * \return Type field value from the blob.
- * \return \c NULL on error.
- */
-const char *ast_channel_blob_json_type(struct ast_channel_blob *obj);
-
-/*!
- * \since 12
- * \brief Creates a \ref ast_channel_blob message.
- *
- * The \a blob JSON object requires a \c "type" field describing the blob. It
- * should also be treated as immutable and not modified after it is put into the
- * message.
- *
- * \param chan Channel blob is associated with, or NULL for global/all channels.
- * \param blob JSON object representing the data.
- * \return \ref ast_channel_blob message.
- * \return \c NULL on error
- */
-struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
-                                              struct ast_json *blob);
-
-/*! @} */
-
-/*!
- * \since 12
- * \brief Publish a \ref ast_channel_varset for a channel.
- *
- * \param chan Channel to pulish the event for, or \c NULL for 'none'.
- * \param variable Name of the variable being set
- * \param value Value.
- */
-void ast_channel_publish_varset(struct ast_channel *chan,
-                               const char *variable, const char *value);
-
 #endif /* _ASTERISK_CHANNEL_H */
diff --git a/include/asterisk/stasis_channels.h b/include/asterisk/stasis_channels.h
new file mode 100644 (file)
index 0000000..c5464ba
--- /dev/null
@@ -0,0 +1,304 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Matt Jordan <mjordan@digium.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.
+ */
+
+
+#ifndef STASIS_CHANNELS_H_
+#define STASIS_CHANNELS_H_
+
+#include "asterisk/stringfields.h"
+#include "asterisk/stasis.h"
+#include "asterisk/json.h"
+#include "asterisk/channel.h"
+
+/*! \addtogroup StasisTopicsAndMessages
+ * @{
+ */
+
+/*!
+ * \since 12
+ * \brief Structure representing a snapshot of channel state.
+ *
+ * While not enforced programmatically, this object is shared across multiple
+ * threads, and should be threated as an immutable object.
+ */
+struct ast_channel_snapshot {
+       AST_DECLARE_STRING_FIELDS(
+               AST_STRING_FIELD(name);                 /*!< ASCII unique channel name */
+               AST_STRING_FIELD(accountcode);          /*!< Account code for billing */
+               AST_STRING_FIELD(peeraccount);          /*!< Peer account code for billing */
+               AST_STRING_FIELD(userfield);            /*!< Userfield for CEL billing */
+               AST_STRING_FIELD(uniqueid);             /*!< Unique Channel Identifier */
+               AST_STRING_FIELD(linkedid);             /*!< Linked Channel Identifier -- gets propagated by linkage */
+               AST_STRING_FIELD(parkinglot);           /*!< Default parking lot, if empty, default parking lot */
+               AST_STRING_FIELD(hangupsource);         /*!< Who is responsible for hanging up this channel */
+               AST_STRING_FIELD(appl);                 /*!< Current application */
+               AST_STRING_FIELD(data);                 /*!< Data passed to current application */
+               AST_STRING_FIELD(context);              /*!< Dialplan: Current extension context */
+               AST_STRING_FIELD(exten);                /*!< Dialplan: Current extension number */
+               AST_STRING_FIELD(caller_name);          /*!< Caller ID Name */
+               AST_STRING_FIELD(caller_number);        /*!< Caller ID Number */
+               AST_STRING_FIELD(connected_name);       /*!< Connected Line Name */
+               AST_STRING_FIELD(connected_number);     /*!< Connected Line Number */
+       );
+
+       struct timeval creationtime;    /*!< The time of channel creation */
+       enum ast_channel_state state;   /*!< State of line */
+       int priority;                   /*!< Dialplan: Current extension priority */
+       int amaflags;                   /*!< AMA flags for billing */
+       int hangupcause;                /*!< Why is the channel hanged up. See causes.h */
+       int caller_pres;                /*!< Caller ID presentation. */
+       struct ast_flags flags;         /*!< channel flags of AST_FLAG_ type */
+       struct varshead *manager_vars;  /*!< Variables to be appended to manager events */
+};
+
+/*!
+ * \since 12
+ * \brief Blob of data associated with a channel.
+ *
+ * The \c blob is actually a JSON object of structured data. It has a "type" field
+ * which contains the type string describing this blob.
+ */
+struct ast_channel_blob {
+       /*! Channel blob is associated with (or NULL for global/all channels) */
+       struct ast_channel_snapshot *snapshot;
+       /*! JSON blob of data */
+       struct ast_json *blob;
+};
+
+/*!
+ * \since 12
+ * \brief A set of channels with blob objects - see \ref ast_channel_blob
+ */
+struct ast_multi_channel_blob;
+
+/*!
+ * \since 12
+ * \brief A topic which publishes the events for all channels.
+ * \retval Topic for all channel events.
+ */
+struct stasis_topic *ast_channel_topic_all(void);
+
+/*!
+ * \since 12
+ * \brief A caching topic which caches \ref ast_channel_snapshot messages from
+ * ast_channel_events_all(void).
+ *
+ * \retval Topic for all channel events.
+ */
+struct stasis_caching_topic *ast_channel_topic_all_cached(void);
+
+/*!
+ * \since 12
+ * \brief Message type for \ref ast_channel_snapshot.
+ *
+ * \retval Message type for \ref ast_channel_snapshot.
+ */
+struct stasis_message_type *ast_channel_snapshot_type(void);
+
+/*!
+ * \since 12
+ * \brief Message type for \ref ast_channel_blob messages.
+ *
+ * \retval Message type for \ref ast_channel_blob messages.
+ */
+struct stasis_message_type *ast_channel_blob_type(void);
+
+/*!
+ * \since 12
+ * \brief Generate a snapshot of the channel state. This is an ao2 object, so
+ * ao2_cleanup() to deallocate.
+ *
+ * \param chan The channel from which to generate a snapshot
+ *
+ * \retval pointer on success (must be ast_freed)
+ * \retval NULL on error
+ */
+struct ast_channel_snapshot *ast_channel_snapshot_create(struct ast_channel *chan);
+
+/*!
+ * \since 12
+ * \brief Creates a \ref ast_channel_blob message.
+ *
+ * The \a blob JSON object requires a \c "type" field describing the blob. It
+ * should also be treated as immutable and not modified after it is put into the
+ * message.
+ *
+ * \param chan Channel blob is associated with, or NULL for global/all channels.
+ * \param blob JSON object representing the data.
+ * \return \ref ast_channel_blob message.
+ * \return \c NULL on error
+ */
+struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
+                                              struct ast_json *blob);
+
+/*!
+ * \since 12
+ * \brief Extracts the type field from a \ref ast_channel_blob.
+ * Returned \c char* is still owned by \a obj
+ * \param obj Channel blob object.
+ * \return Type field value from the blob.
+ * \return \c NULL on error.
+ */
+const char *ast_channel_blob_json_type(struct ast_channel_blob *obj);
+
+/*!
+ * \since 12
+ * \brief Create a \ref ast_multi_channel_blob suitable for a \ref stasis_message
+ *
+ * \note Similar to a \ref ast_channel_blob, the \ref ast_multi_channel_blob requires
+ * a \a blob JSON object containing a \c "type" field describing the blob. It
+ * should also be treated as immutable and not modified after it is put into the
+ * message.
+ *
+ * \param blob The JSON blob that defines the type of this \ref ast_multi_channel_blob
+ *
+ * \return \ref ast_multi_channel_blob object
+ * \return \c NULL on error
+*/
+struct ast_multi_channel_blob *ast_multi_channel_blob_create(struct ast_json *blob);
+
+/*!
+ * \since 12
+ * \brief Retrieve a channel snapshot associated with a specific role from a
+ * \ref ast_multi_channel_blob
+ *
+ * \note The reference count of the \ref ast_channel_snapshot returned from
+ * this function is not changed. The caller of this function does not own the
+ * reference to the snapshot.
+ *
+ * \param obj The \ref ast_multi_channel_blob containing the channel snapshot
+ * to retrieve
+ * \param role The role associated with the channel snapshot
+ *
+ * \retval \ref ast_channel_snapshot matching the role on success
+ * \retval NULL on error or not found for the role specified
+ */
+struct ast_channel_snapshot *ast_multi_channel_blob_get_channel(
+                                              struct ast_multi_channel_blob *obj,
+                                              const char *role);
+
+/*!
+ * \since 12
+ * \brief Retrieve all channel snapshots associated with a specific role from
+ * a \ref ast_multi_channel_blob
+ *
+ * \note Because this function returns an ao2_container (hashed by channel name)
+ * of all channel snapshots that matched the passed in role, the reference of
+ * the snapshots is increased by this function. The caller of this function must
+ * release the reference to the snapshots by disposing of the container
+ * appropriately.
+ *
+ * \param obj The \ref ast_multi_channel_blob containing the channel snapshots to
+ * retrieve
+ * \param role The role associated with the channel snapshots
+ *
+ * \retval A container containing all \ref ast_channel_snapshot objects matching
+ * the role on success.
+ * \retval NULL on error or not found for the role specified
+ */
+struct ao2_container *ast_multi_channel_blob_get_channels(
+                                              struct ast_multi_channel_blob *obj,
+                                              const char *role);
+
+/*!
+ * \since 12
+ * \brief Retrieve the JSON blob from a \ref ast_multi_channel_blob.
+ * Returned \ref ast_json is still owned by \a obj
+ *
+ * \param obj Channel blob object.
+ * \return Type field value from the blob.
+ * \return \c NULL on error.
+ */
+struct ast_json *ast_multi_channel_blob_get_json(struct ast_multi_channel_blob *obj);
+
+/*!
+ * \since 12
+ * \brief Extracts the type field from a \ref ast_multi_channel_blob.
+ * Returned \c char* is still owned by \a obj
+ *
+ * \param obj Channel blob object.
+ * \return Type field value from the blob.
+ * \return \c NULL on error.
+ */
+const char *ast_multi_channel_blob_get_type(struct ast_multi_channel_blob *obj);
+
+/*!
+ * \since 12
+ * \brief Add a \ref ast_channel_snapshot to a \ref ast_multi_channel_blob object
+ *
+ * \note This will increase the reference count by 1 for the channel snapshot. It is
+ * assumed that the \ref ast_multi_channel_blob will own a reference to the object.
+ *
+ * \param obj The \ref ast_multi_channel_blob object that will reference the snapshot
+ * \param role A \a role that the snapshot has in the multi channel relationship
+ * \param snapshot The \ref ast_channel_snapshot being added to the
+ * \ref ast_multi_channel_blob object
+ */
+void ast_multi_channel_blob_add_channel(struct ast_multi_channel_blob *obj,
+                                              const char *role,
+                                              struct ast_channel_snapshot *snapshot);
+
+/*!
+ * \since 12
+ * \brief Publish a \ref ast_channel_varset for a channel.
+ *
+ * \param chan Channel to pulish the event for, or \c NULL for 'none'.
+ * \param variable Name of the variable being set
+ * \param value Value.
+ */
+void ast_channel_publish_varset(struct ast_channel *chan,
+                               const char *variable, const char *value);
+
+/*!
+ * \since 12
+ * \brief Message type for when a channel dials another channel
+ *
+ * \retval A stasis message type
+ */
+struct stasis_message_type *ast_channel_dial_type(void);
+
+/*!
+ * \since 12
+ * \brief Publish in the \ref ast_channel_topic or \ref ast_channel_topic_all
+ * topics a stasis message for the channels involved in a dial operation.
+ *
+ * \param caller The channel performing the dial operation
+ * \param peer The channel being dialed
+ * \param dialstring When beginning a dial, the information passed to the
+ * dialing application
+ * \param dialstatus The current status of the dial operation (NULL if no
+ * status is known)
+ */
+void ast_channel_publish_dial(struct ast_channel *caller,
+               struct ast_channel *peer,
+               const char *dialstring,
+               const char *dialstatus);
+
+/*! @} */
+
+/*!
+ * \brief Dispose of the stasis channel topics and message types
+ */
+void ast_stasis_channels_shutdown(void);
+
+/*!
+ * \brief Initialize the stasis channel topic and message types
+ */
+void ast_stasis_channels_init(void);
+
+#endif /* STASIS_CHANNELS_H_ */
index fd5ac67..a9bcf44 100644 (file)
@@ -74,6 +74,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/channel_internal.h"
 #include "asterisk/features.h"
 #include "asterisk/test.h"
+#include "asterisk/stasis_channels.h"
 
 /*** DOCUMENTATION
  ***/
@@ -152,15 +153,6 @@ static AST_RWLIST_HEAD_STATIC(backends, chanlist);
 /*! \brief All active channels on the system */
 static struct ao2_container *channels;
 
-/*! \brief Message type for channel snapshot events */
-static struct stasis_message_type *channel_snapshot_type;
-
-static struct stasis_message_type *channel_blob_type;
-
-struct stasis_topic *channel_topic_all;
-
-struct stasis_caching_topic *channel_topic_all_cached;
-
 /*! \brief map AST_CAUSE's to readable string representations
  *
  * \ref causes.h
@@ -223,116 +215,6 @@ static const struct causes_map causes[] = {
        { AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
 };
 
-static void publish_channel_state(struct ast_channel *chan)
-{
-       RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
-       RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
-
-       snapshot = ast_channel_snapshot_create(chan);
-       if (!snapshot) {
-               ast_log(LOG_ERROR, "Allocation error\n");
-               return;
-       }
-
-       message = stasis_message_create(ast_channel_snapshot_type(), snapshot);
-       if (!message) {
-               return;
-       }
-
-       ast_assert(ast_channel_topic(chan) != NULL);
-       stasis_publish(ast_channel_topic(chan), message);
-}
-
-static void publish_channel_blob(struct ast_channel *chan, struct ast_json *blob)
-{
-       RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
-       if (blob) {
-               message = ast_channel_blob_create(chan, blob);
-       }
-       if (message) {
-               stasis_publish(ast_channel_topic(chan), message);
-       }
-}
-
-
-static void channel_blob_dtor(void *obj)
-{
-       struct ast_channel_blob *event = obj;
-       ao2_cleanup(event->snapshot);
-       ast_json_unref(event->blob);
-}
-
-struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
-                                              struct ast_json *blob)
-{
-       RAII_VAR(struct ast_channel_blob *, obj, NULL, ao2_cleanup);
-       RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
-       struct ast_json *type;
-
-       ast_assert(blob != NULL);
-
-       type = ast_json_object_get(blob, "type");
-       if (type == NULL) {
-               ast_log(LOG_ERROR, "Invalid ast_channel_blob; missing type field");
-               return NULL;
-       }
-
-       obj = ao2_alloc(sizeof(*obj), channel_blob_dtor);
-       if (!obj) {
-               return NULL;
-       }
-
-       if (chan) {
-               obj->snapshot = ast_channel_snapshot_create(chan);
-               if (obj->snapshot == NULL) {
-                       return NULL;
-               }
-       }
-
-       obj->blob = ast_json_ref(blob);
-
-       msg = stasis_message_create(ast_channel_blob_type(), obj);
-       if (!msg) {
-               return NULL;
-       }
-
-       ao2_ref(msg, +1);
-       return msg;
-}
-
-const char *ast_channel_blob_json_type(struct ast_channel_blob *obj)
-{
-       if (obj == NULL) {
-               return NULL;
-       }
-
-       return ast_json_string_get(ast_json_object_get(obj->blob, "type"));
-}
-
-void ast_channel_publish_varset(struct ast_channel *chan, const char *name, const char *value)
-{
-       RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
-       RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
-
-       ast_assert(name != NULL);
-       ast_assert(value != NULL);
-
-       blob = ast_json_pack("{s: s, s: s, s: s}",
-                            "type", "varset",
-                            "variable", name,
-                            "value", value);
-       publish_channel_blob(chan, blob);
-}
-
-
-static void publish_cache_clear(struct ast_channel *chan)
-{
-       RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
-
-       message = stasis_cache_clear_create(ast_channel_snapshot_type(), ast_channel_uniqueid(chan));
-       stasis_publish(ast_channel_topic(chan), message);
-}
-
 struct ast_variable *ast_channeltype_list(void)
 {
        struct chanlist *cl;
@@ -934,6 +816,34 @@ int ast_str2cause(const char *name)
        return -1;
 }
 
+static void publish_channel_state(struct ast_channel *chan)
+{
+       RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+       RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
+
+       snapshot = ast_channel_snapshot_create(chan);
+       if (!snapshot) {
+               ast_log(LOG_ERROR, "Allocation error\n");
+               return;
+       }
+
+       message = stasis_message_create(ast_channel_snapshot_type(), snapshot);
+       if (!message) {
+               return;
+       }
+
+       ast_assert(ast_channel_topic(chan) != NULL);
+       stasis_publish(ast_channel_topic(chan), message);
+}
+
+static void publish_cache_clear(struct ast_channel *chan)
+{
+       RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
+
+       message = stasis_cache_clear_create(ast_channel_snapshot_type(), ast_channel_uniqueid(chan));
+       stasis_publish(ast_channel_topic(chan), message);
+}
+
 /*! \brief Gives the string form of a given channel state.
  *
  * \note This function is not reentrant.
@@ -1457,6 +1367,18 @@ int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
        return __ast_queue_frame(chan, fin, 1, NULL);
 }
 
+/*! \internal \brief Publish a channel blob message */
+static void publish_channel_blob(struct ast_channel *chan, struct ast_json *blob)
+{
+       RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
+       if (blob) {
+               message = ast_channel_blob_create(chan, blob);
+       }
+       if (message) {
+               stasis_publish(ast_channel_topic(chan), message);
+       }
+}
+
 /*! \brief Queue a hangup frame for channel */
 int ast_queue_hangup(struct ast_channel *chan)
 {
@@ -8700,14 +8622,11 @@ struct varshead *ast_channel_get_manager_vars(struct ast_channel *chan)
 
 static void channels_shutdown(void)
 {
+
+       ast_stasis_channels_shutdown();
+
        free_channelvars();
-       ao2_cleanup(channel_snapshot_type);
-       channel_snapshot_type = NULL;
-       ao2_cleanup(channel_blob_type);
-       channel_blob_type = NULL;
-       ao2_cleanup(channel_topic_all);
-       channel_topic_all = NULL;
-       channel_topic_all_cached = stasis_caching_unsubscribe(channel_topic_all_cached);
+
        ast_data_unregister(NULL);
        ast_cli_unregister_multiple(cli_channel, ARRAY_LEN(cli_channel));
        if (channels) {
@@ -8717,16 +8636,6 @@ static void channels_shutdown(void)
        }
 }
 
-static const char *channel_snapshot_get_id(struct stasis_message *message)
-{
-       struct ast_channel_snapshot *snapshot;
-       if (ast_channel_snapshot_type() != stasis_message_type(message)) {
-               return NULL;
-       }
-       snapshot = stasis_message_data(message);
-       return snapshot->uniqueid;
-}
-
 void ast_channels_init(void)
 {
        channels = ao2_container_alloc(NUM_CHANNEL_BUCKETS,
@@ -8735,11 +8644,7 @@ void ast_channels_init(void)
                ao2_container_register("channels", channels, prnt_channel_key);
        }
 
-       channel_snapshot_type = stasis_message_type_create("ast_channel_snapshot");
-       channel_blob_type = stasis_message_type_create("ast_channel_blob");
-
-       channel_topic_all = stasis_topic_create("ast_channel_topic_all");
-       channel_topic_all_cached = stasis_caching_topic_create(channel_topic_all, channel_snapshot_get_id);
+       ast_stasis_channels_init();
 
        ast_cli_register_multiple(cli_channel, ARRAY_LEN(cli_channel));
 
@@ -11322,83 +11227,6 @@ int ast_channel_get_cc_agent_type(struct ast_channel *chan, char *agent_type, si
        return 0;
 }
 
-static void ast_channel_snapshot_dtor(void *obj)
-{
-       struct ast_channel_snapshot *snapshot = obj;
-       ast_string_field_free_memory(snapshot);
-       ao2_cleanup(snapshot->manager_vars);
-}
-
-struct ast_channel_snapshot *ast_channel_snapshot_create(struct ast_channel *chan)
-{
-       RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
-
-       snapshot = ao2_alloc(sizeof(*snapshot), ast_channel_snapshot_dtor);
-       if (ast_string_field_init(snapshot, 1024)) {
-               return NULL;
-       }
-
-       ast_string_field_set(snapshot, name, ast_channel_name(chan));
-       ast_string_field_set(snapshot, accountcode, ast_channel_accountcode(chan));
-       ast_string_field_set(snapshot, peeraccount, ast_channel_peeraccount(chan));
-       ast_string_field_set(snapshot, userfield, ast_channel_userfield(chan));
-       ast_string_field_set(snapshot, uniqueid, ast_channel_uniqueid(chan));
-       ast_string_field_set(snapshot, linkedid, ast_channel_linkedid(chan));
-       ast_string_field_set(snapshot, parkinglot, ast_channel_parkinglot(chan));
-       ast_string_field_set(snapshot, hangupsource, ast_channel_hangupsource(chan));
-       if (ast_channel_appl(chan)) {
-               ast_string_field_set(snapshot, appl, ast_channel_appl(chan));
-       }
-       if (ast_channel_data(chan)) {
-               ast_string_field_set(snapshot, data, ast_channel_data(chan));
-       }
-       ast_string_field_set(snapshot, context, ast_channel_context(chan));
-       ast_string_field_set(snapshot, exten, ast_channel_exten(chan));
-
-       ast_string_field_set(snapshot, caller_name,
-               S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, ""));
-       ast_string_field_set(snapshot, caller_number,
-               S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, ""));
-
-       ast_string_field_set(snapshot, connected_name,
-               S_COR(ast_channel_connected(chan)->id.name.valid, ast_channel_connected(chan)->id.name.str, ""));
-       ast_string_field_set(snapshot, connected_number,
-               S_COR(ast_channel_connected(chan)->id.number.valid, ast_channel_connected(chan)->id.number.str, ""));
-
-       snapshot->creationtime = ast_channel_creationtime(chan);
-       snapshot->state = ast_channel_state(chan);
-       snapshot->priority = ast_channel_priority(chan);
-       snapshot->amaflags = ast_channel_amaflags(chan);
-       snapshot->hangupcause = ast_channel_hangupcause(chan);
-       snapshot->flags = *ast_channel_flags(chan);
-       snapshot->caller_pres = ast_party_id_presentation(&ast_channel_caller(chan)->id);
-
-       snapshot->manager_vars = ast_channel_get_manager_vars(chan);
-
-       ao2_ref(snapshot, +1);
-       return snapshot;
-}
-
-struct stasis_message_type *ast_channel_blob_type(void)
-{
-       return channel_blob_type;
-}
-
-struct stasis_message_type *ast_channel_snapshot_type(void)
-{
-       return channel_snapshot_type;
-}
-
-struct stasis_topic *ast_channel_topic_all(void)
-{
-       return channel_topic_all;
-}
-
-struct stasis_caching_topic *ast_channel_topic_all_cached(void)
-{
-       return channel_topic_all_cached;
-}
-
 /* DO NOT PUT ADDITIONAL FUNCTIONS BELOW THIS BOUNDARY
  *
  * ONLY FUNCTIONS FOR PROVIDING BACKWARDS ABI COMPATIBILITY BELONG HERE
index d635b46..cb9ed67 100644 (file)
@@ -42,6 +42,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/stringfields.h"
 #include "asterisk/data.h"
 #include "asterisk/indications.h"
+#include "asterisk/stasis_channels.h"
 #include "asterisk/channel_internal.h"
 #include "asterisk/test.h"
 
@@ -1400,4 +1401,7 @@ void ast_channel_internal_setup_topics(struct ast_channel *chan)
 
        chan->topic = stasis_topic_create(topic_name);
        chan->forwarder = stasis_forward_all(chan->topic, ast_channel_topic_all());
+
+       ast_assert(chan->topic != NULL);
+       ast_assert(chan->forwarder != NULL);
 }
index 52a2709..f8b0c97 100644 (file)
@@ -42,6 +42,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/pbx.h"
 #include "asterisk/musiconhold.h"
 #include "asterisk/app.h"
+#include "asterisk/causes.h"
+#include "asterisk/stasis_channels.h"
 
 /*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
 struct ast_dial {
@@ -316,6 +318,7 @@ static int begin_dial_channel(struct ast_dial_channel *channel, struct ast_chann
        } else {
                if (chan)
                        ast_poll_channel_add(chan, channel->owner);
+               ast_channel_publish_dial(chan, channel->owner, channel->device, NULL);
                res = 1;
                ast_verb(3, "Called %s\n", numsubst);
        }
@@ -413,15 +416,18 @@ static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel
                        AST_LIST_REMOVE(&dial->channels, channel, list);
                        AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
                        AST_LIST_UNLOCK(&dial->channels);
+                       ast_channel_publish_dial(chan, channel->owner, channel->device, "ANSWER");
                        set_state(dial, AST_DIAL_RESULT_ANSWERED);
                        break;
                case AST_CONTROL_BUSY:
                        ast_verb(3, "%s is busy\n", ast_channel_name(channel->owner));
+                       ast_channel_publish_dial(chan, channel->owner, channel->device, "BUSY");
                        ast_hangup(channel->owner);
                        channel->owner = NULL;
                        break;
                case AST_CONTROL_CONGESTION:
                        ast_verb(3, "%s is circuit-busy\n", ast_channel_name(channel->owner));
+                       ast_channel_publish_dial(chan, channel->owner, channel->device, "CONGESTION");
                        ast_hangup(channel->owner);
                        channel->owner = NULL;
                        break;
@@ -507,15 +513,18 @@ static void handle_frame_ownerless(struct ast_dial *dial, struct ast_dial_channe
                AST_LIST_REMOVE(&dial->channels, channel, list);
                AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
                AST_LIST_UNLOCK(&dial->channels);
+               ast_channel_publish_dial(NULL, channel->owner, channel->device, "ANSWER");
                set_state(dial, AST_DIAL_RESULT_ANSWERED);
                break;
        case AST_CONTROL_BUSY:
                ast_verb(3, "%s is busy\n", ast_channel_name(channel->owner));
+               ast_channel_publish_dial(NULL, channel->owner, channel->device, "BUSY");
                ast_hangup(channel->owner);
                channel->owner = NULL;
                break;
        case AST_CONTROL_CONGESTION:
                ast_verb(3, "%s is circuit-busy\n", ast_channel_name(channel->owner));
+               ast_channel_publish_dial(NULL, channel->owner, channel->device, "CONGESTION");
                ast_hangup(channel->owner);
                channel->owner = NULL;
                break;
@@ -567,6 +576,25 @@ static int handle_timeout_trip(struct ast_dial *dial, struct timeval start)
        return new_timeout;
 }
 
+/*! \since 12
+ * \internal \brief Convert a hangup cause to a publishable dial status
+ */
+static const char *hangup_cause_to_dial_status(int hangup_cause)
+{
+       switch(hangup_cause) {
+       case AST_CAUSE_BUSY:
+               return "BUSY";
+       case AST_CAUSE_CONGESTION:
+               return "CONGESTION";
+       case AST_CAUSE_NO_ROUTE_DESTINATION:
+       case AST_CAUSE_UNREGISTERED:
+               return "CHANUNAVAIL";
+       case AST_CAUSE_NO_ANSWER:
+       default:
+               return "NOANSWER";
+       }
+}
+
 /*! \brief Helper function that basically keeps tabs on dialing attempts */
 static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
 {
@@ -631,7 +659,7 @@ static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_chann
                /* Wait for frames from channels */
                who = ast_waitfor_n(cs, pos, &timeout);
 
-               /* Check to see if our thread is being cancelled */
+               /* Check to see if our thread is being canceled */
                if (dial->thread == AST_PTHREADT_STOP)
                        break;
 
@@ -660,6 +688,7 @@ static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_chann
                        }
                        if (chan)
                                ast_poll_channel_del(chan, channel->owner);
+                       ast_channel_publish_dial(chan, who, channel->device, hangup_cause_to_dial_status(ast_channel_hangupcause(who)));
                        ast_hangup(who);
                        channel->owner = NULL;
                        continue;
@@ -684,6 +713,7 @@ static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_chann
                                continue;
                        if (chan)
                                ast_poll_channel_del(chan, channel->owner);
+                       ast_channel_publish_dial(chan, channel->owner, channel->device, "CANCEL");
                        ast_hangup(channel->owner);
                        channel->owner = NULL;
                }
@@ -707,6 +737,7 @@ static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_chann
                                continue;
                        if (chan)
                                ast_poll_channel_del(chan, channel->owner);
+                       ast_channel_publish_dial(chan, channel->owner, channel->device, "CANCEL");
                        ast_hangup(channel->owner);
                        channel->owner = NULL;
                }
index 0f05fad..a7206e7 100644 (file)
@@ -3626,9 +3626,7 @@ static int feature_interpret_helper(struct ast_channel *chan, struct ast_channel
 
        ast_rwlock_unlock(&features_lock);
 
-       ast_assert(dynamic_features_buf != NULL);
-
-       if (!ast_str_strlen(dynamic_features_buf) || feature_detected) {
+       if (!dynamic_features_buf || !ast_str_strlen(dynamic_features_buf) || feature_detected) {
                return res;
        }
 
@@ -3745,7 +3743,6 @@ static int feature_interpret(struct ast_channel *chan, struct ast_channel *peer,
 
 
 int ast_feature_detect(struct ast_channel *chan, struct ast_flags *features, const char *code, struct ast_call_feature *feature) {
-
        return feature_interpret_helper(chan, NULL, NULL, code, 0, NULL, features, FEATURE_INTERPRET_DETECT, feature);
 }
 
index ce8e087..89ccd4a 100644 (file)
@@ -35,6 +35,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/manager.h"
 #include "asterisk/stasis_message_router.h"
 #include "asterisk/pbx.h"
+#include "asterisk/stasis_channels.h"
 
 static struct stasis_message_router *channel_state_router;
 
@@ -154,6 +155,81 @@ static struct stasis_message_router *channel_state_router;
                        </syntax>
                </managerEventInstance>
        </managerEvent>
+       <managerEvent language="en_US" name="DialBegin">
+               <managerEventInstance class="EVENT_FLAG_CALL">
+                       <synopsis>Raised when a dial action has started.</synopsis>
+                       <syntax>
+                               <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
+                               <parameter name="ChannelDest">
+                               </parameter>
+                               <parameter name="ChannelStateDest">
+                                       <para>A numeric code for the channel's current state, related to ChannelStateDescDest</para>
+                               </parameter>
+                               <parameter name="ChannelStateDescDest">
+                                       <enumlist>
+                                               <enum name="Down"/>
+                                               <enum name="Rsrvd"/>
+                                               <enum name="OffHook"/>
+                                               <enum name="Dialing"/>
+                                               <enum name="Ring"/>
+                                               <enum name="Ringing"/>
+                                               <enum name="Up"/>
+                                               <enum name="Busy"/>
+                                               <enum name="Dialing Offhook"/>
+                                               <enum name="Pre-ring"/>
+                                               <enum name="Unknown"/>
+                                       </enumlist>
+                               </parameter>
+                               <parameter name="CallerIDNumDest">
+                               </parameter>
+                               <parameter name="CallerIDNameDest">
+                               </parameter>
+                               <parameter name="ConnectedLineNumDest">
+                               </parameter>
+                               <parameter name="ConnectedLineNameDest">
+                               </parameter>
+                               <parameter name="AccountCodeDest">
+                               </parameter>
+                               <parameter name="ContextDest">
+                               </parameter>
+                               <parameter name="ExtenDest">
+                               </parameter>
+                               <parameter name="PriorityDest">
+                               </parameter>
+                               <parameter name="UniqueidDest">
+                               </parameter>
+                               <parameter name="DialString">
+                                       <para>The non-technology specific device being dialed.</para>
+                               </parameter>
+                       </syntax>
+                       <see-also>
+                               <ref type="application">Dial</ref>
+                       </see-also>
+               </managerEventInstance>
+       </managerEvent>
+       <managerEvent language="en_US" name="DialEnd">
+               <managerEventInstance class="EVENT_FLAG_CALL">
+                       <synopsis>Raised when a dial action has completed.</synopsis>
+                       <syntax>
+                               <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
+                               <xi:include xpointer="xpointer(/docs/managerEvent[@name='DialBegin']/managerEventInstance/syntax/parameter[contains(@name, 'Dest')])" />
+                               <parameter name="DialStatus">
+                                       <para>The result of the dial operation.</para>
+                                       <enumlist>
+                                               <enum name="ANSWER" />
+                                               <enum name="BUSY" />
+                                               <enum name="CANCEL" />
+                                               <enum name="CHANUNAVAIL" />
+                                               <enum name="CONGESTION" />
+                                               <enum name="NOANSWER" />
+                                       </enumlist>
+                               </parameter>
+                       </syntax>
+                       <see-also>
+                               <ref type="application">Dial</ref>
+                       </see-also>
+               </managerEventInstance>
+       </managerEvent>
  ***/
 
 /*!
@@ -162,12 +238,14 @@ static struct stasis_message_router *channel_state_router;
  *
  * \param snapshot the channel snapshot for which to generate an AMI message
  *                 body
+ * \param suffix the suffix to append to the channel fields
  *
  * \retval NULL on error
  * \retval ast_str* on success (must be ast_freed by caller)
  */
-static struct ast_str *manager_build_channel_state_string(
-       const struct ast_channel_snapshot *snapshot)
+static struct ast_str *manager_build_channel_state_string_suffix(
+               const struct ast_channel_snapshot *snapshot,
+               const char *suffix)
 {
        struct ast_str *out = ast_str_create(1024);
        int res = 0;
@@ -175,30 +253,30 @@ static struct ast_str *manager_build_channel_state_string(
                return NULL;
        }
        res = ast_str_set(&out, 0,
-               "Channel: %s\r\n"
-               "ChannelState: %d\r\n"
-               "ChannelStateDesc: %s\r\n"
-               "CallerIDNum: %s\r\n"
-               "CallerIDName: %s\r\n"
-               "ConnectedLineNum: %s\r\n"
-               "ConnectedLineName: %s\r\n"
-               "AccountCode: %s\r\n"
-               "Context: %s\r\n"
-               "Exten: %s\r\n"
-               "Priority: %d\r\n"
-               "Uniqueid: %s\r\n",
-               snapshot->name,
-               snapshot->state,
-               ast_state2str(snapshot->state),
-               snapshot->caller_number,
-               snapshot->caller_name,
-               snapshot->connected_number,
-               snapshot->connected_name,
-               snapshot->accountcode,
-               snapshot->context,
-               snapshot->exten,
-               snapshot->priority,
-               snapshot->uniqueid);
+               "Channel%s: %s\r\n"
+               "ChannelState%s: %d\r\n"
+               "ChannelStateDesc%s: %s\r\n"
+               "CallerIDNum%s: %s\r\n"
+               "CallerIDName%s: %s\r\n"
+               "ConnectedLineNum%s: %s\r\n"
+               "ConnectedLineName%s: %s\r\n"
+               "AccountCode%s: %s\r\n"
+               "Context%s: %s\r\n"
+               "Exten%s: %s\r\n"
+               "Priority%s: %d\r\n"
+               "Uniqueid%s: %s\r\n",
+               suffix, snapshot->name,
+               suffix, snapshot->state,
+               suffix, ast_state2str(snapshot->state),
+               suffix, S_OR(snapshot->caller_number, "<unknown>"),
+               suffix, S_OR(snapshot->caller_name, "<unknown>"),
+               suffix, S_OR(snapshot->connected_number, "<unknown>"),
+               suffix, S_OR(snapshot->connected_name, "<unknown>"),
+               suffix, snapshot->accountcode,
+               suffix, snapshot->context,
+               suffix, snapshot->exten,
+               suffix, snapshot->priority,
+               suffix, snapshot->uniqueid);
 
        if (!res) {
                return NULL;
@@ -207,15 +285,31 @@ static struct ast_str *manager_build_channel_state_string(
        if (snapshot->manager_vars) {
                struct ast_var_t *var;
                AST_LIST_TRAVERSE(snapshot->manager_vars, var, entries) {
-                       ast_str_append(&out, 0, "ChanVariable(%s): %s=%s\r\n",
-                                      snapshot->name,
-                                      var->name, var->value);
+                       ast_str_append(&out, 0, "ChanVariable%s: %s=%s\r\n",
+                                                  suffix,
+                                                  var->name, var->value);
                }
        }
 
        return out;
 }
 
+/*!
+ * \brief Generate the AMI message body from a channel snapshot
+ * \internal
+ *
+ * \param snapshot the channel snapshot for which to generate an AMI message
+ *                 body
+ *
+ * \retval NULL on error
+ * \retval ast_str* on success (must be ast_freed by caller)
+ */
+static struct ast_str *manager_build_channel_state_string(
+       const struct ast_channel_snapshot *snapshot)
+{
+       return manager_build_channel_state_string_suffix(snapshot, "");
+}
+
 /*! \brief Struct containing info for an AMI channel event to send out. */
 struct snapshot_manager_event {
        /*! event_flags manager_event() flags parameter. */
@@ -602,6 +696,65 @@ static void channel_blob_cb(void *data, struct stasis_subscription *sub,
        }
 }
 
+/*!
+ * \brief Callback processing messages for channel dialing
+ */
+static void channel_dial_cb(void *data, struct stasis_subscription *sub,
+                               struct stasis_topic *topic,
+                               struct stasis_message *message)
+{
+       struct ast_multi_channel_blob *obj = stasis_message_data(message);
+       const char *dialstatus;
+       const char *dialstring;
+       struct ast_channel_snapshot *caller;
+       struct ast_channel_snapshot *peer;
+       RAII_VAR(struct ast_str *, caller_event_string, NULL, ast_free);
+       RAII_VAR(struct ast_str *, peer_event_string, NULL, ast_free);
+
+       if (strcmp("dial", ast_multi_channel_blob_get_type(obj))) {
+               ast_assert(0);
+               return;
+       }
+
+       caller = ast_multi_channel_blob_get_channel(obj, "caller");
+       peer = ast_multi_channel_blob_get_channel(obj, "peer");
+
+       /* Peer is required - otherwise, who are we dialing? */
+       ast_assert(peer != NULL);
+       peer_event_string = manager_build_channel_state_string_suffix(peer, "Dest");
+       if (!peer_event_string) {
+               return;
+       }
+
+       if (caller) {
+               caller_event_string = manager_build_channel_state_string(caller);
+               if (!caller_event_string) {
+                       return;
+               }
+               dialstatus = ast_json_string_get(ast_json_object_get(ast_multi_channel_blob_get_json(obj), "dialstatus"));
+               dialstring = ast_json_string_get(ast_json_object_get(ast_multi_channel_blob_get_json(obj), "dialstring"));
+               if (ast_strlen_zero(dialstatus)) {
+                       manager_event(EVENT_FLAG_CALL, "DialBegin",
+                                       "%s"
+                                       "%s"
+                                       "DialString: %s\r\n",
+                                       ast_str_buffer(caller_event_string),
+                                       ast_str_buffer(peer_event_string),
+                                       S_OR(dialstring, "unknown"));
+               } else {
+                       manager_event(EVENT_FLAG_CALL, "DialEnd",
+                                       "%s"
+                                       "%s"
+                                       "DialStatus: %s\r\n",
+                                       ast_str_buffer(caller_event_string),
+                                       ast_str_buffer(peer_event_string),
+                                       S_OR(dialstatus, "unknown"));
+               }
+       } else {
+               /* TODO: If we don't have a caller, this should be treated as an Originate */
+       }
+}
+
 static void manager_channels_shutdown(void)
 {
        stasis_message_router_unsubscribe(channel_state_router);
@@ -636,6 +789,11 @@ int manager_channels_init(void)
                                         channel_blob_cb,
                                         NULL);
 
+       ret |= stasis_message_router_add(channel_state_router,
+                                        ast_channel_dial_type(),
+                                        channel_dial_cb,
+                                        NULL);
+
        /* If somehow we failed to add any routes, just shut down the whole
         * thing and fail it.
         */
@@ -646,3 +804,4 @@ int manager_channels_init(void)
 
        return 0;
 }
+
index bc79cff..8bc0c3f 100644 (file)
@@ -72,6 +72,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/taskprocessor.h"
 #include "asterisk/xmldoc.h"
 #include "asterisk/astobj2.h"
+#include "asterisk/stasis_channels.h"
 
 /*!
  * \note I M P O R T A N T :
diff --git a/main/stasis_channels.c b/main/stasis_channels.c
new file mode 100644 (file)
index 0000000..4fc6319
--- /dev/null
@@ -0,0 +1,478 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Matt Jordan <mjordan@digium.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 Stasis Messages and Data Types for Channel Objects
+ *
+ * \author \verbatim Matt Jordan <mjordan@digium.com> \endverbatim
+ *
+ */
+
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/stasis.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/stasis_channels.h"
+
+#define NUM_MULTI_CHANNEL_BLOB_BUCKETS 7
+
+/*! \brief Message type for channel snapshot messages */
+static struct stasis_message_type *channel_snapshot_type;
+
+/*! \brief Message type for channel blob messages */
+static struct stasis_message_type *channel_blob_type;
+
+/*! \brief Message type for channel dial messages */
+static struct stasis_message_type *channel_dial_type;
+
+/*! \brief Topic for all channels */
+struct stasis_topic *channel_topic_all;
+
+/*! \brief Caching topic for all channels */
+struct stasis_caching_topic *channel_topic_all_cached;
+
+struct stasis_message_type *ast_channel_dial_type(void)
+{
+       return channel_dial_type;
+}
+
+struct stasis_message_type *ast_channel_blob_type(void)
+{
+       return channel_blob_type;
+}
+
+struct stasis_message_type *ast_channel_snapshot_type(void)
+{
+       return channel_snapshot_type;
+}
+
+struct stasis_topic *ast_channel_topic_all(void)
+{
+       return channel_topic_all;
+}
+
+struct stasis_caching_topic *ast_channel_topic_all_cached(void)
+{
+       return channel_topic_all_cached;
+}
+
+static const char *channel_snapshot_get_id(struct stasis_message *message)
+{
+       struct ast_channel_snapshot *snapshot;
+       if (ast_channel_snapshot_type() != stasis_message_type(message)) {
+               return NULL;
+       }
+       snapshot = stasis_message_data(message);
+       return snapshot->uniqueid;
+}
+
+/*! \internal \brief Hash function for \ref ast_channel_snapshot objects */
+static int channel_snapshot_hash_cb(const void *obj, const int flags)
+{
+       const struct ast_channel_snapshot *snapshot = obj;
+       const char *name = (flags & OBJ_KEY) ? obj : snapshot->name;
+       return ast_str_case_hash(name);
+}
+
+/*! \internal \brief Comparison function for \ref ast_channel_snapshot objects */
+static int channel_snapshot_cmp_cb(void *obj, void *arg, int flags)
+{
+       struct ast_channel_snapshot *left = obj;
+       struct ast_channel_snapshot *right = arg;
+       const char *match = (flags & OBJ_KEY) ? arg : right->name;
+       return strcasecmp(left->name, match) ? 0 : (CMP_MATCH | CMP_STOP);
+}
+
+static void channel_snapshot_dtor(void *obj)
+{
+       struct ast_channel_snapshot *snapshot = obj;
+       ast_string_field_free_memory(snapshot);
+}
+
+struct ast_channel_snapshot *ast_channel_snapshot_create(struct ast_channel *chan)
+{
+       RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+
+       snapshot = ao2_alloc(sizeof(*snapshot), channel_snapshot_dtor);
+       if (!snapshot || ast_string_field_init(snapshot, 1024)) {
+               return NULL;
+       }
+
+       ast_string_field_set(snapshot, name, ast_channel_name(chan));
+       ast_string_field_set(snapshot, accountcode, ast_channel_accountcode(chan));
+       ast_string_field_set(snapshot, peeraccount, ast_channel_peeraccount(chan));
+       ast_string_field_set(snapshot, userfield, ast_channel_userfield(chan));
+       ast_string_field_set(snapshot, uniqueid, ast_channel_uniqueid(chan));
+       ast_string_field_set(snapshot, linkedid, ast_channel_linkedid(chan));
+       ast_string_field_set(snapshot, parkinglot, ast_channel_parkinglot(chan));
+       ast_string_field_set(snapshot, hangupsource, ast_channel_hangupsource(chan));
+       if (ast_channel_appl(chan)) {
+               ast_string_field_set(snapshot, appl, ast_channel_appl(chan));
+       }
+       if (ast_channel_data(chan)) {
+               ast_string_field_set(snapshot, data, ast_channel_data(chan));
+       }
+       ast_string_field_set(snapshot, context, ast_channel_context(chan));
+       ast_string_field_set(snapshot, exten, ast_channel_exten(chan));
+
+       ast_string_field_set(snapshot, caller_name,
+               S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, ""));
+       ast_string_field_set(snapshot, caller_number,
+               S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, ""));
+
+       ast_string_field_set(snapshot, connected_name,
+               S_COR(ast_channel_connected(chan)->id.name.valid, ast_channel_connected(chan)->id.name.str, ""));
+       ast_string_field_set(snapshot, connected_number,
+               S_COR(ast_channel_connected(chan)->id.number.valid, ast_channel_connected(chan)->id.number.str, ""));
+
+       snapshot->creationtime = ast_channel_creationtime(chan);
+       snapshot->state = ast_channel_state(chan);
+       snapshot->priority = ast_channel_priority(chan);
+       snapshot->amaflags = ast_channel_amaflags(chan);
+       snapshot->hangupcause = ast_channel_hangupcause(chan);
+       snapshot->flags = *ast_channel_flags(chan);
+
+       ao2_ref(snapshot, +1);
+       return snapshot;
+}
+
+static void publish_message_for_channel_topics(struct stasis_message *message, struct ast_channel *chan)
+{
+       if (chan) {
+               stasis_publish(ast_channel_topic(chan), message);
+       } else {
+               stasis_publish(ast_channel_topic_all(), message);
+       }
+}
+
+static void channel_blob_dtor(void *obj)
+{
+       struct ast_channel_blob *event = obj;
+       ao2_cleanup(event->snapshot);
+       ast_json_unref(event->blob);
+}
+
+void ast_channel_publish_dial(struct ast_channel *caller, struct ast_channel *peer, const char *dialstring, const char *dialstatus)
+{
+       RAII_VAR(struct ast_multi_channel_blob *, payload, NULL, ao2_cleanup);
+       RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
+       struct ast_channel_snapshot *caller_snapshot;
+       struct ast_channel_snapshot *peer_snapshot;
+
+       ast_assert(peer != NULL);
+       blob = ast_json_pack("{s: s, s: s, s: s}",
+                            "type", "dial",
+                            "dialstatus", S_OR(dialstatus, ""),
+                            "dialstring", S_OR(dialstring, ""));
+       if (!blob) {
+               return;
+       }
+       payload = ast_multi_channel_blob_create(blob);
+       if (!payload) {
+               return;
+       }
+
+       if (caller) {
+               caller_snapshot = ast_channel_snapshot_create(caller);
+               if (!caller_snapshot) {
+                       return;
+               }
+               ast_multi_channel_blob_add_channel(payload, "caller", caller_snapshot);
+       }
+
+       peer_snapshot = ast_channel_snapshot_create(peer);
+       if (!peer_snapshot) {
+               return;
+       }
+       ast_multi_channel_blob_add_channel(payload, "peer", peer_snapshot);
+
+       msg = stasis_message_create(ast_channel_dial_type(), payload);
+       if (!msg) {
+               return;
+       }
+
+       publish_message_for_channel_topics(msg, caller);
+}
+
+struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
+                                              struct ast_json *blob)
+{
+       RAII_VAR(struct ast_channel_blob *, obj, NULL, ao2_cleanup);
+       RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+       struct ast_json *type;
+
+       ast_assert(blob != NULL);
+
+       type = ast_json_object_get(blob, "type");
+       if (type == NULL) {
+               ast_log(LOG_ERROR, "Invalid ast_channel_blob; missing type field\n");
+               return NULL;
+       }
+
+       obj = ao2_alloc(sizeof(*obj), channel_blob_dtor);
+       if (!obj) {
+               return NULL;
+       }
+
+       if (chan) {
+               obj->snapshot = ast_channel_snapshot_create(chan);
+               if (obj->snapshot == NULL) {
+                       return NULL;
+               }
+       }
+
+       obj->blob = ast_json_ref(blob);
+
+       msg = stasis_message_create(ast_channel_blob_type(), obj);
+       if (!msg) {
+               return NULL;
+       }
+
+       ao2_ref(msg, +1);
+       return msg;
+}
+
+const char *ast_channel_blob_json_type(struct ast_channel_blob *obj)
+{
+       if (obj == NULL) {
+               return NULL;
+       }
+
+       return ast_json_string_get(ast_json_object_get(obj->blob, "type"));
+}
+
+/*! \brief A channel snapshot wrapper object used in \ref ast_multi_channel_blob objects */
+struct channel_role_snapshot {
+       struct ast_channel_snapshot *snapshot;  /*!< A channel snapshot */
+       char role[0];                                                   /*!< The role assigned to the channel */
+};
+
+/*! \brief A multi channel blob data structure for multi_channel_blob stasis messages */
+struct ast_multi_channel_blob {
+       struct ao2_container *channel_snapshots;        /*!< A container holding the snapshots */
+       struct ast_json *blob;                                          /*< A blob of JSON data */
+};
+
+/*! \internal \brief Standard comparison function for \ref channel_role_snapshot objects */
+static int channel_role_single_cmp_cb(void *obj, void *arg, int flags)
+{
+       struct channel_role_snapshot *left = obj;
+       struct channel_role_snapshot *right = arg;
+       const char *match = (flags & OBJ_KEY) ? arg : right->role;
+       return strcasecmp(left->role, match) ? 0 : (CMP_MATCH | CMP_STOP);
+}
+
+/*! \internal \brief Multi comparison function for \ref channel_role_snapshot objects */
+static int channel_role_multi_cmp_cb(void *obj, void *arg, int flags)
+{
+       struct channel_role_snapshot *left = obj;
+       struct channel_role_snapshot *right = arg;
+       const char *match = (flags & OBJ_KEY) ? arg : right->role;
+       return strcasecmp(left->role, match) ? 0 : (CMP_MATCH);
+}
+
+/*! \internal \brief Hash function for \ref channel_role_snapshot objects */
+static int channel_role_hash_cb(const void *obj, const int flags)
+{
+       const struct channel_role_snapshot *snapshot = obj;
+       const char *name = (flags & OBJ_KEY) ? obj : snapshot->role;
+       return ast_str_case_hash(name);
+}
+
+/*! \internal \brief Destructor for \ref ast_multi_channel_blob objects */
+static void multi_channel_blob_dtor(void *obj)
+{
+       struct ast_multi_channel_blob *multi_blob = obj;
+
+       ao2_cleanup(multi_blob->channel_snapshots);
+       ast_json_unref(multi_blob->blob);
+}
+
+struct ast_multi_channel_blob *ast_multi_channel_blob_create(struct ast_json *blob)
+{
+       RAII_VAR(struct ast_multi_channel_blob *, obj,
+                       ao2_alloc(sizeof(*obj), multi_channel_blob_dtor),
+                       ao2_cleanup);
+       struct ast_json *type;
+
+       ast_assert(blob != NULL);
+
+       if (!obj) {
+               return NULL;
+       }
+
+       type = ast_json_object_get(blob, "type");
+       if (type == NULL) {
+               ast_log(LOG_ERROR, "Invalid ast_multi_channel_blob; missing type field\n");
+               return NULL;
+       }
+
+       obj->channel_snapshots = ao2_container_alloc(NUM_MULTI_CHANNEL_BLOB_BUCKETS,
+                       channel_role_hash_cb, channel_role_single_cmp_cb);
+       if (!obj->channel_snapshots) {
+               return NULL;
+       }
+
+       obj->blob = ast_json_ref(blob);
+
+       ao2_ref(obj, +1);
+       return obj;
+}
+
+static void channel_role_snapshot_dtor(void *obj)
+{
+       struct channel_role_snapshot *role_snapshot = obj;
+       ao2_cleanup(role_snapshot->snapshot);
+}
+
+void ast_multi_channel_blob_add_channel(struct ast_multi_channel_blob *obj, const char *role, struct ast_channel_snapshot *snapshot)
+{
+       RAII_VAR(struct channel_role_snapshot *, role_snapshot, NULL, ao2_cleanup);
+       int role_len = strlen(role) + 1;
+
+       if (!obj || ast_strlen_zero(role) || !snapshot) {
+               return;
+       }
+
+       role_snapshot = ao2_alloc(sizeof(*role_snapshot) + role_len, channel_role_snapshot_dtor);
+       if (!role_snapshot) {
+               return;
+       }
+       ast_copy_string(role_snapshot->role, role, role_len);
+       role_snapshot->snapshot = snapshot;
+       ao2_ref(role_snapshot->snapshot, +1);
+       ao2_link(obj->channel_snapshots, role_snapshot);
+}
+
+struct ast_channel_snapshot *ast_multi_channel_blob_get_channel(struct ast_multi_channel_blob *obj, const char *role)
+{
+       struct channel_role_snapshot *role_snapshot;
+
+       if (!obj || ast_strlen_zero(role)) {
+               return NULL;
+       }
+       role_snapshot = ao2_find(obj->channel_snapshots, role, OBJ_KEY);
+       /* Note that this function does not increase the ref count on snapshot */
+       if (!role_snapshot) {
+               return NULL;
+       }
+       ao2_ref(role_snapshot, -1);
+       return role_snapshot->snapshot;
+}
+
+struct ao2_container *ast_multi_channel_blob_get_channels(struct ast_multi_channel_blob *obj, const char *role)
+{
+       RAII_VAR(struct ao2_container *, ret_container,
+               ao2_container_alloc(NUM_MULTI_CHANNEL_BLOB_BUCKETS, channel_snapshot_hash_cb, channel_snapshot_cmp_cb),
+               ao2_cleanup);
+       struct ao2_iterator *it_role_snapshots;
+       struct channel_role_snapshot *role_snapshot;
+       char *arg;
+
+       if (!obj || ast_strlen_zero(role) || !ret_container) {
+               return NULL;
+       }
+       arg = ast_strdupa(role);
+
+       it_role_snapshots = ao2_callback(obj->channel_snapshots, OBJ_MULTIPLE | OBJ_KEY, channel_role_multi_cmp_cb, arg);
+       if (!it_role_snapshots) {
+               return NULL;
+       }
+
+       while ((role_snapshot = ao2_iterator_next(it_role_snapshots))) {
+               ao2_link(ret_container, role_snapshot->snapshot);
+               ao2_ref(role_snapshot, -1);
+       }
+       ao2_iterator_destroy(it_role_snapshots);
+
+       ao2_ref(ret_container, +1);
+       return ret_container;
+}
+
+struct ast_json *ast_multi_channel_blob_get_json(struct ast_multi_channel_blob *obj)
+{
+       if (!obj) {
+               return NULL;
+       }
+       return obj->blob;
+}
+
+const char *ast_multi_channel_blob_get_type(struct ast_multi_channel_blob *obj)
+{
+       if (!obj) {
+               return NULL;
+       }
+
+       return ast_json_string_get(ast_json_object_get(obj->blob, "type"));
+}
+
+void ast_channel_publish_varset(struct ast_channel *chan, const char *name, const char *value)
+{
+       RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
+
+       ast_assert(name != NULL);
+       ast_assert(value != NULL);
+
+       blob = ast_json_pack("{s: s, s: s, s: s}",
+                            "type", "varset",
+                            "variable", name,
+                            "value", value);
+       if (!blob) {
+               ast_log(LOG_ERROR, "Error creating message\n");
+               return;
+       }
+
+       msg = ast_channel_blob_create(chan, ast_json_ref(blob));
+
+       if (!msg) {
+               return;
+       }
+
+       publish_message_for_channel_topics(msg, chan);
+}
+
+void ast_stasis_channels_shutdown(void)
+{
+       ao2_cleanup(channel_snapshot_type);
+       channel_snapshot_type = NULL;
+       ao2_cleanup(channel_blob_type);
+       channel_blob_type = NULL;
+       ao2_cleanup(channel_dial_type);
+       channel_dial_type = NULL;
+       ao2_cleanup(channel_topic_all);
+       channel_topic_all = NULL;
+       channel_topic_all_cached = stasis_caching_unsubscribe(channel_topic_all_cached);
+}
+
+void ast_stasis_channels_init(void)
+{
+       channel_snapshot_type = stasis_message_type_create("ast_channel_snapshot");
+       channel_blob_type = stasis_message_type_create("ast_channel_blob");
+       channel_dial_type = stasis_message_type_create("ast_channel_dial");
+       channel_topic_all = stasis_topic_create("ast_channel_topic_all");
+       channel_topic_all_cached = stasis_caching_topic_create(channel_topic_all, channel_snapshot_get_id);
+}
index 65685e0..f9f9cb2 100644 (file)
@@ -52,6 +52,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/astdb.h"
 #include "asterisk/app.h"
 #include "asterisk/astobj2.h"
+#include "asterisk/stasis_channels.h"
 
 #define MODE_MATCH             0
 #define MODE_MATCHMORE         1
diff --git a/tests/test_stasis_channels.c b/tests/test_stasis_channels.c
new file mode 100644 (file)
index 0000000..fe39837
--- /dev/null
@@ -0,0 +1,223 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Matt Jordan <mjordan@digium.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 Test Stasis Channel messages and objects
+ *
+ * \author\verbatim Matt Jordan <mjordan@digium.com> \endverbatim
+ *
+ * \ingroup tests
+ */
+
+/*** MODULEINFO
+       <depend>TEST_FRAMEWORK</depend>
+       <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/astobj2.h"
+#include "asterisk/module.h"
+#include "asterisk/stasis.h"
+#include "asterisk/stasis_message_router.h"
+#include "asterisk/test.h"
+#include "asterisk/stasis_channels.h"
+#include "asterisk/channel.h"
+
+static const char *test_category = "/stasis/channels/";
+
+static void safe_channel_release(struct ast_channel *chan)
+{
+       if (!chan) {
+               return;
+       }
+       ast_channel_release(chan);
+}
+
+AST_TEST_DEFINE(channel_blob_create)
+{
+       struct ast_channel_blob *blob;
+       RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
+       RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+       RAII_VAR(struct ast_json *, bad_json, NULL, ast_json_unref);
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = __func__;
+               info->category = test_category;
+               info->summary = "Test creation of ast_channel_blob objects";
+               info->description = "Test creation of ast_channel_blob objects";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
+
+       chan = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, 0, "TEST/Alice");
+       json = ast_json_pack("{s: s}",
+                    "type", "test");
+       bad_json = ast_json_pack("{s: s}",
+                    "bad_key", "test");
+
+       /* Off nominal creation */
+       ast_test_validate(test, NULL == ast_channel_blob_create(NULL, bad_json));
+       ast_test_validate(test, NULL == ast_channel_blob_create(chan, bad_json));
+
+       /* Test for single channel */
+       msg = ast_channel_blob_create(chan, json);
+       ast_test_validate(test, NULL != msg);
+       blob = stasis_message_data(msg);
+       ast_test_validate(test, NULL != blob);
+       ast_test_validate(test, NULL != blob->snapshot);
+       ast_test_validate(test, NULL != blob->blob);
+       ast_test_validate(test, 0 == strcmp(ast_channel_blob_json_type(blob), "test"));
+
+       ast_test_validate(test, 1 == ao2_ref(msg, 0));
+       ao2_cleanup(msg);
+
+       /* Test for global channels */
+       msg = ast_channel_blob_create(NULL, json);
+       ast_test_validate(test, NULL != msg);
+       blob = stasis_message_data(msg);
+       ast_test_validate(test, NULL != blob);
+       ast_test_validate(test, NULL == blob->snapshot);
+       ast_test_validate(test, NULL != blob->blob);
+       ast_test_validate(test, 0 == strcmp(ast_channel_blob_json_type(blob), "test"));
+
+       return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(multi_channel_blob_create)
+{
+       RAII_VAR(struct ast_multi_channel_blob *, blob, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+       RAII_VAR(struct ast_json *, bad_json, NULL, ast_json_unref);
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = __func__;
+               info->category = test_category;
+               info->summary = "Test creation of ast_multi_channel_blob objects";
+               info->description = "Test creation of ast_multi_channel_blob objects";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
+
+       json = ast_json_pack("{s: s}",
+                    "type", "test");
+       bad_json = ast_json_pack("{s: s}",
+                    "bad_key", "test");
+
+       /* Off nominal creation */
+       ast_test_validate(test, NULL == ast_multi_channel_blob_create(bad_json));
+
+       /* Test for single channel */
+       blob = ast_multi_channel_blob_create(json);
+       ast_test_validate(test, NULL != blob);
+       ast_test_validate(test, 0 == strcmp(ast_multi_channel_blob_get_type(blob), "test"));
+       ast_test_validate(test, NULL != ast_multi_channel_blob_get_json(blob));
+
+       return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(multi_channel_blob_snapshots)
+{
+       RAII_VAR(struct ast_multi_channel_blob *, blob, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+       RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
+       RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
+       RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
+       struct ast_channel_snapshot *snapshot;
+       struct ao2_container *matches;
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = __func__;
+               info->category = test_category;
+               info->summary = "Test creation of ast_multi_channel_blob objects";
+               info->description = "Test creation of ast_multi_channel_blob objects";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
+
+       json = ast_json_pack("{s: s}",
+                    "type", "test");
+       chan_alice = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, 0, "TEST/Alice");
+       chan_bob = ast_channel_alloc(0, AST_STATE_DOWN, "200", "Bob", "200", "200", "default", NULL, 0, "TEST/Bob");
+       chan_charlie = ast_channel_alloc(0, AST_STATE_DOWN, "300", "Bob", "300", "300", "default", NULL, 0, "TEST/Charlie");
+
+       blob = ast_multi_channel_blob_create(json);
+       ast_multi_channel_blob_add_channel(blob, "Caller", ast_channel_snapshot_create(chan_alice));
+       ast_multi_channel_blob_add_channel(blob, "Peer", ast_channel_snapshot_create(chan_bob));
+       ast_multi_channel_blob_add_channel(blob, "Peer", ast_channel_snapshot_create(chan_charlie));
+
+       /* Test for unknown role */
+       ast_test_validate(test, NULL == ast_multi_channel_blob_get_channel(blob, "Foobar"));
+
+       /* Test for single match */
+       snapshot = ast_multi_channel_blob_get_channel(blob, "Caller");
+       ast_test_validate(test, NULL != snapshot);
+       ast_test_validate(test, 0 == strcmp("TEST/Alice", snapshot->name));
+
+       /* Test for single match, multiple possibilities */
+       snapshot = ast_multi_channel_blob_get_channel(blob, "Peer");
+       ast_test_validate(test, NULL != snapshot);
+       ast_test_validate(test, 0 != strcmp("TEST/Alice", snapshot->name));
+
+       /* Multi-match */
+       matches = ast_multi_channel_blob_get_channels(blob, "Peer");
+       ast_test_validate(test, NULL != matches);
+       ast_test_validate(test, 2 == ao2_container_count(matches));
+       snapshot = ao2_find(matches, "TEST/Bob", OBJ_KEY);
+       ast_test_validate(test, NULL != snapshot);
+       ao2_cleanup(snapshot);
+       snapshot = ao2_find(matches, "TEST/Charlie", OBJ_KEY);
+       ast_test_validate(test, NULL != snapshot);
+       ao2_cleanup(snapshot);
+       ast_test_validate(test, 1 == ao2_ref(matches, 0));
+       ao2_cleanup(matches);
+
+       return AST_TEST_PASS;
+}
+
+static int unload_module(void)
+{
+       AST_TEST_UNREGISTER(channel_blob_create);
+       AST_TEST_UNREGISTER(multi_channel_blob_create);
+       AST_TEST_UNREGISTER(multi_channel_blob_snapshots);
+
+       return 0;
+}
+
+static int load_module(void)
+{
+       AST_TEST_REGISTER(channel_blob_create);
+       AST_TEST_REGISTER(multi_channel_blob_create);
+       AST_TEST_REGISTER(multi_channel_blob_snapshots);
+
+       return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, 0, "Stasis Channel Testing",
+               .load = load_module,
+               .unload = unload_module
+       );