git migration: Refactor the ASTERISK_FILE_VERSION macro
[asterisk/asterisk.git] / main / channel_internal_api.c
index 768afc6..a2fafbf 100644 (file)
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+ASTERISK_REGISTER_FILE()
 
 #include <unistd.h>
 #include <fcntl.h>
 
+#include "asterisk/paths.h"
 #include "asterisk/channel.h"
-#include "asterisk/stringfields.h"
+#include "asterisk/channel_internal.h"
 #include "asterisk/data.h"
+#include "asterisk/endpoints.h"
 #include "asterisk/indications.h"
-#include "asterisk/channel_internal.h"
+#include "asterisk/stasis_cache_pattern.h"
+#include "asterisk/stasis_channels.h"
+#include "asterisk/stasis_endpoints.h"
+#include "asterisk/stringfields.h"
+#include "asterisk/test.h"
+
+/*!
+ * \brief Channel UniqueId structure
+ * \note channel creation time used for determining LinkedId Propagation
+ */
+struct ast_channel_id {
+       time_t creation_time;                           /*!< Creation time */
+       int creation_unique;                            /*!< sub-second unique value */
+       char unique_id[AST_MAX_UNIQUEID];       /*!< Unique Identifier */
+};
 
 /*!
  * \brief Main Channel structure associated with a channel.
  *
- * \note XXX It is important to remember to increment .cleancount each time
- *       this structure is changed. XXX
- *
  * \note When adding fields to this structure, it is important to add the field
  *       'in position' with like-aligned fields, so as to keep the compiler from
  *       having to add padding to align fields. The structure's fields are sorted
@@ -64,9 +77,6 @@ struct ast_channel {
        void *music_state;                              /*!< Music State*/
        void *generatordata;                            /*!< Current generator data if there is any */
        struct ast_generator *generator;                /*!< Current active data generator */
-       struct ast_channel * bridged_channel;                   /*!< Who are we bridged to, if we're bridged.
-                                                        *   Who is proxying for us, if we are proxied (i.e. chan_agent).
-                                                        *   Do not access directly, use ast_bridged_channel(chan) */
        struct ast_channel *masq;                       /*!< Channel that will masquerade as us */
        struct ast_channel *masqr;                      /*!< Who we are masquerading as */
        const char *blockproc;                          /*!< Procedure causing blocking */
@@ -86,26 +96,29 @@ struct ast_channel {
        struct ast_tone_zone *zone;                     /*!< Tone zone as set in indications.conf or
                                                         *   in the CHANNEL dialplan function */
        struct ast_channel_monitor *monitor;            /*!< Channel monitoring */
-       struct ast_callid *callid;                      /*!< Bound call identifier pointer */
+       ast_callid callid;                      /*!< Bound call identifier pointer */
 #ifdef HAVE_EPOLL
        struct ast_epoll_data *epfd_data[AST_MAX_FDS];
 #endif
+       struct ao2_container *dialed_causes;            /*!< Contains tech-specific and Asterisk cause data from dialed channels */
 
        AST_DECLARE_STRING_FIELDS(
                AST_STRING_FIELD(name);         /*!< ASCII unique channel name */
                AST_STRING_FIELD(language);     /*!< Language requested for voice prompts */
                AST_STRING_FIELD(musicclass);   /*!< Default music class */
+               AST_STRING_FIELD(latest_musicclass);   /*!< Latest active music class */
                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(call_forward); /*!< Where to forward to if asked to dial on this interface */
-               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(dialcontext);  /*!< Dial: Extension context that we were called from */
        );
 
+       struct ast_channel_id uniqueid;         /*!< Unique Channel Identifier - can be specified on creation */
+       struct ast_channel_id linkedid;         /*!< Linked Channel Identifier - oldest propagated when bridged */
+
        struct timeval whentohangup; /*!< Non-zero, set to actual time when channel is to be hung up */
        pthread_t blocker;           /*!< If anyone is blocking, this is them */
 
@@ -130,6 +143,11 @@ struct ast_channel {
         */
        struct ast_party_connected_line connected;
 
+       /*!
+        * \brief Channel Connected Line ID information that was last indicated.
+        */
+       struct ast_party_connected_line connected_indicated;
+
        /*! \brief Redirecting/Diversion information */
        struct ast_party_redirecting redirecting;
 
@@ -137,9 +155,14 @@ struct ast_channel {
        struct varshead varshead;                       /*!< A linked list for channel variables. See \ref AstChanVar */
        ast_group_t callgroup;                          /*!< Call group for call pickups */
        ast_group_t pickupgroup;                        /*!< Pickup group - which calls groups can be picked up? */
+       struct ast_namedgroups *named_callgroups;       /*!< Named call group for call pickups */
+       struct ast_namedgroups *named_pickupgroups;     /*!< Named pickup group - which call groups can be picked up? */
+       struct timeval creationtime;                    /*!< The time of channel creation */
+       struct timeval answertime;                              /*!< The time the channel was answered */
        struct ast_readq_list readq;
        struct ast_jb jb;                               /*!< The jitterbuffer state */
        struct timeval dtmf_tv;                         /*!< The time that an in process digit began, or the last digit ended */
+       struct ast_hangup_handler_list hangup_handlers;/*!< Hangup handlers on the channel. */
        struct ast_datastore_list datastores; /*!< Data stores on the channel */
        struct ast_autochan_list autochans; /*!< Autochans on the channel */
        unsigned long insmpl;                           /*!< Track the read/written samples for monitor use */
@@ -150,10 +173,12 @@ struct ast_channel {
                                                         *   See \arg \ref AstFileDesc */
        int softhangup;                         /*!< Whether or not we have been hung up...  Do not set this value
                                                         *   directly, use ast_softhangup() */
+       int unbridged;              /*!< If non-zero, the bridge core needs to re-evaluate the current
+                                        bridging technology which is in use by this channel's bridge. */
        int fdno;                                       /*!< Which fd had an event detected on */
        int streamid;                                   /*!< For streaming playback, the schedule ID */
        int vstreamid;                                  /*!< For streaming video playback, the schedule ID */
-       struct ast_format oldwriteformat;  /*!< Original writer format */
+       struct ast_format *oldwriteformat;  /*!< Original writer format */
        int timingfd;                                   /*!< Timing fd */
        enum ast_channel_state state;                   /*!< State of line -- Don't write directly, use ast_setstate() */
        int rings;                                      /*!< Number of rings so far */
@@ -170,19 +195,21 @@ struct ast_channel {
        struct ast_flags flags;                         /*!< channel flags of AST_FLAG_ type */
        int alertpipe[2];
        struct ast_format_cap *nativeformats;         /*!< Kinds of data this channel can natively handle */
-       struct ast_format readformat;            /*!< Requested read format (after translation) */
-       struct ast_format writeformat;           /*!< Requested write format (after translation) */
-       struct ast_format rawreadformat;         /*!< Raw read format (before translation) */
-       struct ast_format rawwriteformat;        /*!< Raw write format (before translation) */
+       struct ast_format *readformat;            /*!< Requested read format (after translation) */
+       struct ast_format *writeformat;           /*!< Requested write format (before translation) */
+       struct ast_format *rawreadformat;         /*!< Raw read format (before translation) */
+       struct ast_format *rawwriteformat;        /*!< Raw write format (after translation) */
        unsigned int emulate_dtmf_duration;             /*!< Number of ms left to emulate DTMF for */
 #ifdef HAVE_EPOLL
        int epfd;
 #endif
        int visible_indication;                         /*!< Indication currently playing on the channel */
+       int hold_state;                                                 /*!< Current Hold/Unhold state */
 
        unsigned short transfercapability;              /*!< ISDN Transfer Capability - AST_FLAG_DIGITAL is not enough */
 
        struct ast_bridge *bridge;                      /*!< Bridge this channel is participating in */
+       struct ast_bridge_channel *bridge_channel;/*!< The bridge_channel this channel is linked with. */
        struct ast_timer *timer;                        /*!< timer object that provided timingfd */
 
        char context[AST_MAX_CONTEXT];                  /*!< Dialplan: Current extension context */
@@ -190,8 +217,16 @@ struct ast_channel {
        char macrocontext[AST_MAX_CONTEXT];             /*!< Macro: Current non-macro context. See app_macro.c */
        char macroexten[AST_MAX_EXTENSION];             /*!< Macro: Current non-macro extension. See app_macro.c */
        char dtmf_digit_to_emulate;                     /*!< Digit being emulated */
+       char sending_dtmf_digit;                        /*!< Digit this channel is currently sending out. (zero if not sending) */
+       struct timeval sending_dtmf_tv;         /*!< The time this channel started sending the current digit. (Invalid if sending_dtmf_digit is zero.) */
+       struct stasis_cp_single *topics;                /*!< Topic for all channel's events */
+       struct stasis_forward *endpoint_forward;        /*!< Subscription for event forwarding to endpoint's topic */
+       struct stasis_forward *endpoint_cache_forward; /*!< Subscription for cache updates to endpoint's topic */
 };
 
+/*! \brief The monotonically increasing integer counter for channel uniqueids */
+static int uniqueint;
+
 /* AST_DATA definitions, which will probably have to be re-thought since the channel will be opaque */
 
 #if 0  /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
@@ -218,8 +253,6 @@ AST_DATA_STRUCTURE(ast_callerid, DATA_EXPORT_CALLERID);
        MEMBER(ast_channel, peeraccount, AST_DATA_STRING)                       \
        MEMBER(ast_channel, userfield, AST_DATA_STRING)                         \
        MEMBER(ast_channel, call_forward, AST_DATA_STRING)                      \
-       MEMBER(ast_channel, uniqueid, AST_DATA_STRING)                          \
-       MEMBER(ast_channel, linkedid, AST_DATA_STRING)                          \
        MEMBER(ast_channel, parkinglot, AST_DATA_STRING)                        \
        MEMBER(ast_channel, hangupsource, AST_DATA_STRING)                      \
        MEMBER(ast_channel, dialcontext, AST_DATA_STRING)                       \
@@ -248,7 +281,6 @@ static void channel_data_add_flags(struct ast_data *tree,
        ast_data_add_bool(tree, "EXCEPTION", ast_test_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION));
        ast_data_add_bool(tree, "MOH", ast_test_flag(ast_channel_flags(chan), AST_FLAG_MOH));
        ast_data_add_bool(tree, "SPYING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_SPYING));
-       ast_data_add_bool(tree, "NBRIDGE", ast_test_flag(ast_channel_flags(chan), AST_FLAG_NBRIDGE));
        ast_data_add_bool(tree, "IN_AUTOLOOP", ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP));
        ast_data_add_bool(tree, "OUTGOING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING));
        ast_data_add_bool(tree, "IN_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF));
@@ -256,14 +288,16 @@ static void channel_data_add_flags(struct ast_data *tree,
        ast_data_add_bool(tree, "END_DTMF_ONLY", ast_test_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY));
        ast_data_add_bool(tree, "MASQ_NOSTREAM", ast_test_flag(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM));
        ast_data_add_bool(tree, "BRIDGE_HANGUP_RUN", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_RUN));
-       ast_data_add_bool(tree, "BRIDGE_HANGUP_DONT", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_DONT));
        ast_data_add_bool(tree, "DISABLE_WORKAROUNDS", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_WORKAROUNDS));
+       ast_data_add_bool(tree, "DISABLE_DEVSTATE_CACHE", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE));
+       ast_data_add_bool(tree, "BRIDGE_DUAL_REDIRECT_WAIT", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT));
+       ast_data_add_bool(tree, "ORIGINATED", ast_test_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED));
+       ast_data_add_bool(tree, "DEAD", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEAD));
 }
 
 int ast_channel_data_add_structure(struct ast_data *tree,
        struct ast_channel *chan, int add_bridged)
 {
-       struct ast_channel *bc;
        struct ast_data *data_bridged;
        struct ast_data *data_cdr;
        struct ast_data *data_flags;
@@ -282,7 +316,7 @@ int ast_channel_data_add_structure(struct ast_data *tree,
        ast_data_add_structure(ast_channel, tree, chan);
 
        if (add_bridged) {
-               bc = ast_bridged_channel(chan);
+               RAII_VAR(struct ast_channel *, bc, ast_channel_bridge_peer(chan), ast_channel_cleanup);
                if (bc) {
                        data_bridged = ast_data_add_node(tree, "bridged");
                        if (!data_bridged) {
@@ -292,6 +326,9 @@ int ast_channel_data_add_structure(struct ast_data *tree,
                }
        }
 
+       ast_data_add_str(tree, "uniqueid", ast_channel_uniqueid(chan));
+       ast_data_add_str(tree, "linkedid", ast_channel_linkedid(chan));
+
        ast_data_add_codec(tree, "oldwriteformat", ast_channel_oldwriteformat(chan));
        ast_data_add_codec(tree, "readformat", ast_channel_readformat(chan));
        ast_data_add_codec(tree, "writeformat", ast_channel_writeformat(chan));
@@ -320,7 +357,7 @@ int ast_channel_data_add_structure(struct ast_data *tree,
        if (!enum_node) {
                return -1;
        }
-       ast_data_add_str(enum_node, "text", ast_cdr_flags2str(ast_channel_amaflags(chan)));
+       ast_data_add_str(enum_node, "text", ast_channel_amaflags2string(ast_channel_amaflags(chan)));
        ast_data_add_int(enum_node, "value", ast_channel_amaflags(chan));
 
        /* transfercapability */
@@ -342,7 +379,6 @@ int ast_channel_data_add_structure(struct ast_data *tree,
        ast_data_add_bool(data_softhangup, "timeout", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_TIMEOUT);
        ast_data_add_bool(data_softhangup, "appunload", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_APPUNLOAD);
        ast_data_add_bool(data_softhangup, "explicit", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_EXPLICIT);
-       ast_data_add_bool(data_softhangup, "unbridge", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_UNBRIDGE);
 
        /* channel flags */
        data_flags = ast_data_add_node(tree, "flags");
@@ -387,8 +423,6 @@ int ast_channel_data_add_structure(struct ast_data *tree,
                return -1;
        }
 
-       ast_cdr_data_add_structure(data_cdr, ast_channel_cdr(chan), 1);
-
        return 0;
 }
 
@@ -400,15 +434,19 @@ int ast_channel_data_cmp_structure(const struct ast_data_search *tree,
 
 /* ACCESSORS */
 
-#define DEFINE_STRINGFIELD_SETTERS_FOR(field) \
+#define DEFINE_STRINGFIELD_SETTERS_FOR(field, publish, assert_on_null) \
 void ast_channel_##field##_set(struct ast_channel *chan, const char *value) \
 { \
+       if ((assert_on_null)) ast_assert(!ast_strlen_zero(value)); \
+       if (!strcmp(value, chan->field)) return; \
        ast_string_field_set(chan, field, value); \
+       if (publish && ast_channel_internal_is_finalized(chan)) ast_channel_publish_snapshot(chan); \
 } \
   \
 void ast_channel_##field##_build_va(struct ast_channel *chan, const char *fmt, va_list ap) \
 { \
        ast_string_field_build_va(chan, field, fmt, ap); \
+       if (publish && ast_channel_internal_is_finalized(chan)) ast_channel_publish_snapshot(chan); \
 } \
 void ast_channel_##field##_build(struct ast_channel *chan, const char *fmt, ...) \
 { \
@@ -418,17 +456,17 @@ void ast_channel_##field##_build(struct ast_channel *chan, const char *fmt, ...)
        va_end(ap); \
 }
 
-DEFINE_STRINGFIELD_SETTERS_FOR(name);
-DEFINE_STRINGFIELD_SETTERS_FOR(language);
-DEFINE_STRINGFIELD_SETTERS_FOR(musicclass);
-DEFINE_STRINGFIELD_SETTERS_FOR(accountcode);
-DEFINE_STRINGFIELD_SETTERS_FOR(peeraccount);
-DEFINE_STRINGFIELD_SETTERS_FOR(userfield);
-DEFINE_STRINGFIELD_SETTERS_FOR(call_forward);
-DEFINE_STRINGFIELD_SETTERS_FOR(uniqueid);
-DEFINE_STRINGFIELD_SETTERS_FOR(parkinglot);
-DEFINE_STRINGFIELD_SETTERS_FOR(hangupsource);
-DEFINE_STRINGFIELD_SETTERS_FOR(dialcontext);
+DEFINE_STRINGFIELD_SETTERS_FOR(name, 0, 1);
+DEFINE_STRINGFIELD_SETTERS_FOR(language, 1, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(musicclass, 0, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(latest_musicclass, 0, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(accountcode, 1, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(peeraccount, 1, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(userfield, 0, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(call_forward, 0, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(parkinglot, 0, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(hangupsource, 0, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(dialcontext, 0, 0);
 
 #define DEFINE_STRINGFIELD_GETTER_FOR(field) const char *ast_channel_##field(const struct ast_channel *chan) \
 { \
@@ -438,20 +476,25 @@ DEFINE_STRINGFIELD_SETTERS_FOR(dialcontext);
 DEFINE_STRINGFIELD_GETTER_FOR(name);
 DEFINE_STRINGFIELD_GETTER_FOR(language);
 DEFINE_STRINGFIELD_GETTER_FOR(musicclass);
+DEFINE_STRINGFIELD_GETTER_FOR(latest_musicclass);
 DEFINE_STRINGFIELD_GETTER_FOR(accountcode);
 DEFINE_STRINGFIELD_GETTER_FOR(peeraccount);
 DEFINE_STRINGFIELD_GETTER_FOR(userfield);
 DEFINE_STRINGFIELD_GETTER_FOR(call_forward);
-DEFINE_STRINGFIELD_GETTER_FOR(uniqueid);
-DEFINE_STRINGFIELD_GETTER_FOR(linkedid);
 DEFINE_STRINGFIELD_GETTER_FOR(parkinglot);
 DEFINE_STRINGFIELD_GETTER_FOR(hangupsource);
 DEFINE_STRINGFIELD_GETTER_FOR(dialcontext);
 
-void ast_channel_linkedid_set(struct ast_channel *chan, const char *value)
+const char *ast_channel_uniqueid(const struct ast_channel *chan)
+{
+       ast_assert(chan->uniqueid.unique_id[0] != '\0');
+       return chan->uniqueid.unique_id;
+}
+
+const char *ast_channel_linkedid(const struct ast_channel *chan)
 {
-       ast_assert(!ast_strlen_zero(value));
-       ast_string_field_set(chan, linkedid, value);
+       ast_assert(chan->linkedid.unique_id[0] != '\0');
+       return chan->linkedid.unique_id;
 }
 
 const char *ast_channel_appl(const struct ast_channel *chan)
@@ -520,14 +563,39 @@ void ast_channel_dtmf_digit_to_emulate_set(struct ast_channel *chan, char value)
 {
        chan->dtmf_digit_to_emulate = value;
 }
-int ast_channel_amaflags(const struct ast_channel *chan)
+
+char ast_channel_sending_dtmf_digit(const struct ast_channel *chan)
+{
+       return chan->sending_dtmf_digit;
+}
+void ast_channel_sending_dtmf_digit_set(struct ast_channel *chan, char value)
+{
+       chan->sending_dtmf_digit = value;
+}
+
+struct timeval ast_channel_sending_dtmf_tv(const struct ast_channel *chan)
+{
+       return chan->sending_dtmf_tv;
+}
+void ast_channel_sending_dtmf_tv_set(struct ast_channel *chan, struct timeval value)
+{
+       chan->sending_dtmf_tv = value;
+}
+
+enum ama_flags ast_channel_amaflags(const struct ast_channel *chan)
 {
        return chan->amaflags;
 }
-void ast_channel_amaflags_set(struct ast_channel *chan, int value)
+
+void ast_channel_amaflags_set(struct ast_channel *chan, enum ama_flags value)
 {
+       if (chan->amaflags == value) {
+               return;
+       }
        chan->amaflags = value;
+       ast_channel_publish_snapshot(chan);
 }
+
 #ifdef HAVE_EPOLL
 int ast_channel_epfd(const struct ast_channel *chan)
 {
@@ -602,6 +670,14 @@ void ast_channel_visible_indication_set(struct ast_channel *chan, int value)
 {
        chan->visible_indication = value;
 }
+int ast_channel_hold_state(const struct ast_channel *chan)
+{
+       return chan->hold_state;
+}
+void ast_channel_hold_state_set(struct ast_channel *chan, int value)
+{
+       chan->hold_state = value;
+}
 int ast_channel_vstreamid(const struct ast_channel *chan)
 {
        return chan->vstreamid;
@@ -752,7 +828,7 @@ struct ast_format_cap *ast_channel_nativeformats(const struct ast_channel *chan)
 }
 void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
 {
-       chan->nativeformats = value;
+       ao2_replace(chan->nativeformats, value);
 }
 struct ast_framehook_list *ast_channel_framehooks(const struct ast_channel *chan)
 {
@@ -838,54 +914,80 @@ enum ast_channel_state ast_channel_state(const struct ast_channel *chan)
 {
        return chan->state;
 }
-struct ast_callid *ast_channel_callid(const struct ast_channel *chan)
+ast_callid ast_channel_callid(const struct ast_channel *chan)
 {
-       if (chan->callid) {
-               ast_callid_ref(chan->callid);
-               return chan->callid;
-       }
-       return NULL;
+       return chan->callid;
 }
-void ast_channel_callid_set(struct ast_channel *chan, struct ast_callid *callid)
+void ast_channel_callid_set(struct ast_channel *chan, ast_callid callid)
 {
+       char call_identifier_from[AST_CALLID_BUFFER_LENGTH];
+       char call_identifier_to[AST_CALLID_BUFFER_LENGTH];
+       call_identifier_from[0] = '\0';
+       ast_callid_strnprint(call_identifier_to, sizeof(call_identifier_to), callid);
        if (chan->callid) {
+               ast_callid_strnprint(call_identifier_from, sizeof(call_identifier_from), chan->callid);
+               ast_debug(3, "Channel Call ID changing from %s to %s\n", call_identifier_from, call_identifier_to);
+       }
 
-               if ((option_debug >= 3) || (ast_opt_dbg_module && ast_debug_get_by_module(AST_MODULE) >= 3)) {
-                       char call_identifier_from[AST_CALLID_BUFFER_LENGTH];
-                       char call_identifier_to[AST_CALLID_BUFFER_LENGTH];
-                       ast_callid_strnprint(call_identifier_from, sizeof(call_identifier_from), chan->callid);
-                       ast_callid_strnprint(call_identifier_to, sizeof(call_identifier_to), callid);
-                       ast_log(LOG_DEBUG, "Channel Call ID changing from %s to %s\n", call_identifier_from, call_identifier_to);
-               }
+       chan->callid = callid;
 
-               /* unbind if already set */
-               ast_callid_unref(chan->callid);
-       }
-       chan->callid = ast_callid_ref(callid);
+       ast_test_suite_event_notify("CallIDChange",
+               "State: CallIDChange\r\n"
+               "Channel: %s\r\n"
+               "CallID: %s\r\n"
+               "PriorCallID: %s",
+               ast_channel_name(chan),
+               call_identifier_to,
+               call_identifier_from);
 }
+
 void ast_channel_state_set(struct ast_channel *chan, enum ast_channel_state value)
 {
        chan->state = value;
 }
+void ast_channel_set_oldwriteformat(struct ast_channel *chan, struct ast_format *format)
+{
+       ao2_replace(chan->oldwriteformat, format);
+}
+void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
+{
+       ao2_replace(chan->rawreadformat, format);
+}
+void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
+{
+       ao2_replace(chan->rawwriteformat, format);
+}
+void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
+{
+       ao2_replace(chan->readformat, format);
+}
+void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
+{
+       ao2_replace(chan->writeformat, format);
+}
 struct ast_format *ast_channel_oldwriteformat(struct ast_channel *chan)
 {
-       return &chan->oldwriteformat;
+       return chan->oldwriteformat;
 }
 struct ast_format *ast_channel_rawreadformat(struct ast_channel *chan)
 {
-       return &chan->rawreadformat;
+       return chan->rawreadformat;
 }
 struct ast_format *ast_channel_rawwriteformat(struct ast_channel *chan)
 {
-       return &chan->rawwriteformat;
+       return chan->rawwriteformat;
 }
 struct ast_format *ast_channel_readformat(struct ast_channel *chan)
 {
-       return &chan->readformat;
+       return chan->readformat;
 }
 struct ast_format *ast_channel_writeformat(struct ast_channel *chan)
 {
-       return &chan->writeformat;
+       return chan->writeformat;
+}
+struct ast_hangup_handler_list *ast_channel_hangup_handlers(struct ast_channel *chan)
+{
+       return &chan->hangup_handlers;
 }
 struct ast_datastore_list *ast_channel_datastores(struct ast_channel *chan)
 {
@@ -915,6 +1017,14 @@ struct ast_party_connected_line *ast_channel_connected(struct ast_channel *chan)
 {
        return &chan->connected;
 }
+struct ast_party_connected_line *ast_channel_connected_indicated(struct ast_channel *chan)
+{
+       return &chan->connected_indicated;
+}
+struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
+{
+       return ast_party_id_merge(&chan->connected.id, &chan->connected.priv);
+}
 struct ast_party_dialed *ast_channel_dialed(struct ast_channel *chan)
 {
        return &chan->dialed;
@@ -923,6 +1033,18 @@ struct ast_party_redirecting *ast_channel_redirecting(struct ast_channel *chan)
 {
        return &chan->redirecting;
 }
+struct ast_party_id ast_channel_redirecting_effective_orig(struct ast_channel *chan)
+{
+       return ast_party_id_merge(&chan->redirecting.orig, &chan->redirecting.priv_orig);
+}
+struct ast_party_id ast_channel_redirecting_effective_from(struct ast_channel *chan)
+{
+       return ast_party_id_merge(&chan->redirecting.from, &chan->redirecting.priv_from);
+}
+struct ast_party_id ast_channel_redirecting_effective_to(struct ast_channel *chan)
+{
+       return ast_party_id_merge(&chan->redirecting.to, &chan->redirecting.priv_to);
+}
 struct timeval *ast_channel_dtmf_tv(struct ast_channel *chan)
 {
        return &chan->dtmf_tv;
@@ -971,6 +1093,24 @@ void ast_channel_varshead_set(struct ast_channel *chan, struct varshead *value)
 {
        chan->varshead = *value;
 }
+struct timeval ast_channel_creationtime(struct ast_channel *chan)
+{
+       return chan->creationtime;
+}
+void ast_channel_creationtime_set(struct ast_channel *chan, struct timeval *value)
+{
+       chan->creationtime = *value;
+}
+
+struct timeval ast_channel_answertime(struct ast_channel *chan)
+{
+       return chan->answertime;
+}
+
+void ast_channel_answertime_set(struct ast_channel *chan, struct timeval *value)
+{
+       chan->answertime = *value;
+}
 
 /* Evil softhangup accessors */
 int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
@@ -990,11 +1130,36 @@ void ast_channel_softhangup_internal_flag_clear(struct ast_channel *chan, int va
        chan ->softhangup &= ~value;
 }
 
+int ast_channel_unbridged_nolock(struct ast_channel *chan)
+{
+       return chan->unbridged;
+}
+
+int ast_channel_unbridged(struct ast_channel *chan)
+{
+       int res;
+       ast_channel_lock(chan);
+       res = ast_channel_unbridged_nolock(chan);
+       ast_channel_unlock(chan);
+       return res;
+}
+
+void ast_channel_set_unbridged_nolock(struct ast_channel *chan, int value)
+{
+       chan->unbridged = value;
+       ast_queue_frame(chan, &ast_null_frame);
+}
+
+void ast_channel_set_unbridged(struct ast_channel *chan, int value)
+{
+       ast_channel_lock(chan);
+       ast_channel_set_unbridged_nolock(chan, value);
+       ast_channel_unlock(chan);
+}
+
 void ast_channel_callid_cleanup(struct ast_channel *chan)
 {
-       if (chan->callid) {
-               chan->callid = ast_callid_unref(chan->callid);
-       }
+       chan->callid = 0;
 }
 
 /* Typedef accessors */
@@ -1014,6 +1179,24 @@ void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
 {
        chan->pickupgroup = value;
 }
+struct ast_namedgroups *ast_channel_named_callgroups(const struct ast_channel *chan)
+{
+       return chan->named_callgroups;
+}
+void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
+{
+       ast_unref_namedgroups(chan->named_callgroups);
+       chan->named_callgroups = ast_ref_namedgroups(value);
+}
+struct ast_namedgroups *ast_channel_named_pickupgroups(const struct ast_channel *chan)
+{
+       return chan->named_pickupgroups;
+}
+void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
+{
+       ast_unref_namedgroups(chan->named_pickupgroups);
+       chan->named_pickupgroups = ast_ref_namedgroups(value);
+}
 
 /* Alertpipe functions */
 int ast_channel_alert_write(struct ast_channel *chan)
@@ -1170,15 +1353,16 @@ struct ast_bridge *ast_channel_internal_bridge(const struct ast_channel *chan)
 void ast_channel_internal_bridge_set(struct ast_channel *chan, struct ast_bridge *value)
 {
        chan->bridge = value;
+       ast_channel_publish_snapshot(chan);
 }
 
-struct ast_channel *ast_channel_internal_bridged_channel(const struct ast_channel *chan)
+struct ast_bridge_channel *ast_channel_internal_bridge_channel(const struct ast_channel *chan)
 {
-       return chan->bridged_channel;
+       return chan->bridge_channel;
 }
-void ast_channel_internal_bridged_channel_set(struct ast_channel *chan, struct ast_channel *value)
+void ast_channel_internal_bridge_channel_set(struct ast_channel *chan, struct ast_bridge_channel *value)
 {
-       chan->bridged_channel = value;
+       chan->bridge_channel = value;
 }
 
 struct ast_flags *ast_channel_flags(struct ast_channel *chan)
@@ -1186,7 +1370,73 @@ struct ast_flags *ast_channel_flags(struct ast_channel *chan)
        return &chan->flags;
 }
 
-struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj), const char *file, int line, const char *function)
+static int collect_names_cb(void *obj, void *arg, int flags) {
+       struct ast_control_pvt_cause_code *cause_code = obj;
+       struct ast_str **str = arg;
+
+       ast_str_append(str, 0, "%s%s", (ast_str_strlen(*str) ? "," : ""), cause_code->chan_name);
+
+       return 0;
+}
+
+struct ast_str *ast_channel_dialed_causes_channels(const struct ast_channel *chan)
+{
+       struct ast_str *chanlist = ast_str_create(128);
+
+       if (!chanlist) {
+               return NULL;
+       }
+
+       ao2_callback(chan->dialed_causes, 0, collect_names_cb, &chanlist);
+
+       return chanlist;
+}
+
+struct ast_control_pvt_cause_code *ast_channel_dialed_causes_find(const struct ast_channel *chan, const char *chan_name)
+{
+       return ao2_find(chan->dialed_causes, chan_name, OBJ_KEY);
+}
+
+int ast_channel_dialed_causes_add(const struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
+{
+       struct ast_control_pvt_cause_code *ao2_cause_code;
+       ao2_find(chan->dialed_causes, cause_code->chan_name, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA);
+       ao2_cause_code = ao2_alloc(datalen, NULL);
+
+       if (ao2_cause_code) {
+               memcpy(ao2_cause_code, cause_code, datalen);
+               ao2_link(chan->dialed_causes, ao2_cause_code);
+               ao2_ref(ao2_cause_code, -1);
+               return 0;
+       } else {
+               return -1;
+       }
+}
+
+void ast_channel_dialed_causes_clear(const struct ast_channel *chan)
+{
+       ao2_callback(chan->dialed_causes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
+}
+
+/* \brief Hash function for pvt cause code frames */
+static int pvt_cause_hash_fn(const void *vpc, const int flags)
+{
+       const struct ast_control_pvt_cause_code *pc = vpc;
+       return ast_str_hash(ast_tech_to_upper(ast_strdupa(pc->chan_name)));
+}
+
+/* \brief Comparison function for pvt cause code frames */
+static int pvt_cause_cmp_fn(void *obj, void *vstr, int flags)
+{
+       struct ast_control_pvt_cause_code *pc = obj;
+       char *str = ast_tech_to_upper(ast_strdupa(vstr));
+       char *pc_str = ast_tech_to_upper(ast_strdupa(pc->chan_name));
+       return !strcmp(pc_str, str) ? CMP_MATCH | CMP_STOP : 0;
+}
+
+#define DIALED_CAUSES_BUCKETS 37
+
+struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj), const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *file, int line, const char *function)
 {
        struct ast_channel *tmp;
 #if defined(REF_DEBUG)
@@ -1203,12 +1453,109 @@ struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj),
                return ast_channel_unref(tmp);
        }
 
+       if (!(tmp->dialed_causes = ao2_container_alloc(DIALED_CAUSES_BUCKETS, pvt_cause_hash_fn, pvt_cause_cmp_fn))) {
+               return ast_channel_unref(tmp);
+       }
+
+       /* set the creation time in the uniqueid */
+       tmp->uniqueid.creation_time = time(NULL);
+       tmp->uniqueid.creation_unique = ast_atomic_fetchadd_int(&uniqueint, 1);
+
+       /* use provided id or default to historical {system-}time.# format */
+       if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
+               ast_copy_string(tmp->uniqueid.unique_id, assignedids->uniqueid, sizeof(tmp->uniqueid.unique_id));
+       } else if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
+               snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%li.%d",
+                       (long)(tmp->uniqueid.creation_time),
+                       tmp->uniqueid.creation_unique);
+       } else {
+               snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%s-%li.%d",
+                       ast_config_AST_SYSTEM_NAME,
+                       (long)(tmp->uniqueid.creation_time),
+                       tmp->uniqueid.creation_unique);
+       }
+
+       /* copy linked id from parent channel if known */
+       if (requestor) {
+               tmp->linkedid = requestor->linkedid;
+       } else {
+               tmp->linkedid = tmp->uniqueid;
+       }
+
        return tmp;
 }
 
+struct ast_channel *ast_channel_internal_oldest_linkedid(struct ast_channel *a, struct ast_channel *b)
+{
+       ast_assert(a->linkedid.creation_time != 0);
+       ast_assert(b->linkedid.creation_time != 0);
+
+       if (a->linkedid.creation_time < b->linkedid.creation_time) {
+               return a;
+       }
+       if (b->linkedid.creation_time < a->linkedid.creation_time) {
+               return b;
+       }
+       if (a->linkedid.creation_unique < b->linkedid.creation_unique) {
+               return a;
+       }
+       return b;
+}
+
+void ast_channel_internal_copy_linkedid(struct ast_channel *dest, struct ast_channel *source)
+{
+       if (dest->linkedid.creation_time == source->linkedid.creation_time
+               && dest->linkedid.creation_unique == source->linkedid.creation_unique
+               && !strcmp(dest->linkedid.unique_id, source->linkedid.unique_id)) {
+               return;
+       }
+       dest->linkedid = source->linkedid;
+       ast_channel_publish_snapshot(dest);
+}
+
+void ast_channel_internal_swap_uniqueid_and_linkedid(struct ast_channel *a, struct ast_channel *b)
+{
+       struct ast_channel_id temp;
+
+       temp = a->uniqueid;
+       a->uniqueid = b->uniqueid;
+       b->uniqueid = temp;
+
+       temp = a->linkedid;
+       a->linkedid = b->linkedid;
+       b->linkedid = temp;
+}
+
+void ast_channel_internal_swap_topics(struct ast_channel *a, struct ast_channel *b)
+{
+       struct stasis_cp_single *temp;
+
+       temp = a->topics;
+       a->topics = b->topics;
+       b->topics = temp;
+}
+
+void ast_channel_internal_set_fake_ids(struct ast_channel *chan, const char *uniqueid, const char *linkedid)
+{
+       ast_copy_string(chan->uniqueid.unique_id, uniqueid, sizeof(chan->uniqueid.unique_id));
+       ast_copy_string(chan->linkedid.unique_id, linkedid, sizeof(chan->linkedid.unique_id));
+}
+
 void ast_channel_internal_cleanup(struct ast_channel *chan)
 {
+       if (chan->dialed_causes) {
+               ao2_t_ref(chan->dialed_causes, -1,
+                       "done with dialed causes since the channel is going away");
+               chan->dialed_causes = NULL;
+       }
+
        ast_string_field_free_memory(chan);
+
+       chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
+       chan->endpoint_cache_forward = stasis_forward_cancel(chan->endpoint_cache_forward);
+
+       stasis_cp_single_unsubscribe(chan->topics);
+       chan->topics = NULL;
 }
 
 void ast_channel_internal_finalize(struct ast_channel *chan)
@@ -1220,3 +1567,62 @@ int ast_channel_internal_is_finalized(struct ast_channel *chan)
 {
        return chan->finalized;
 }
+
+struct stasis_topic *ast_channel_topic(struct ast_channel *chan)
+{
+       if (!chan) {
+               return ast_channel_topic_all();
+       }
+
+       return stasis_cp_single_topic(chan->topics);
+}
+
+struct stasis_topic *ast_channel_topic_cached(struct ast_channel *chan)
+{
+       if (!chan) {
+               return ast_channel_topic_all_cached();
+       }
+
+       return stasis_cp_single_topic_cached(chan->topics);
+}
+
+int ast_channel_forward_endpoint(struct ast_channel *chan,
+       struct ast_endpoint *endpoint)
+{
+       ast_assert(chan != NULL);
+       ast_assert(endpoint != NULL);
+
+       chan->endpoint_forward =
+               stasis_forward_all(ast_channel_topic(chan),
+                       ast_endpoint_topic(endpoint));
+       if (!chan->endpoint_forward) {
+               return -1;
+       }
+
+       chan->endpoint_cache_forward = stasis_forward_all(ast_channel_topic_cached(chan),
+               ast_endpoint_topic(endpoint));
+       if (!chan->endpoint_cache_forward) {
+               chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
+               return -1;
+       }
+
+       return 0;
+}
+
+int ast_channel_internal_setup_topics(struct ast_channel *chan)
+{
+       const char *topic_name = chan->uniqueid.unique_id;
+       ast_assert(chan->topics == NULL);
+
+       if (ast_strlen_zero(topic_name)) {
+               topic_name = "<dummy-channel>";
+       }
+
+       chan->topics = stasis_cp_single_create(
+               ast_channel_cache_all(), topic_name);
+       if (!chan->topics) {
+               return -1;
+       }
+
+       return 0;
+}