channel: Add support for writing to a specific stream.
[asterisk/asterisk.git] / main / channel_internal_api.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Channel Accessor API
22  *
23  * This file is intended to be the only file that ever accesses the
24  * internals of an ast_channel. All other files should use the
25  * accessor functions defined here.
26  *
27  * \author Terry Wilson
28  */
29
30 /*** MODULEINFO
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 #include <unistd.h>
37 #include <fcntl.h>
38
39 #include "asterisk/paths.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/channel_internal.h"
42 #include "asterisk/data.h"
43 #include "asterisk/endpoints.h"
44 #include "asterisk/indications.h"
45 #include "asterisk/stasis_cache_pattern.h"
46 #include "asterisk/stasis_channels.h"
47 #include "asterisk/stasis_endpoints.h"
48 #include "asterisk/stringfields.h"
49 #include "asterisk/stream.h"
50 #include "asterisk/test.h"
51
52 /*!
53  * \brief Channel UniqueId structure
54  * \note channel creation time used for determining LinkedId Propagation
55  */
56 struct ast_channel_id {
57         time_t creation_time;                           /*!< Creation time */
58         int creation_unique;                            /*!< sub-second unique value */
59         char unique_id[AST_MAX_UNIQUEID];       /*!< Unique Identifier */
60 };
61
62 /*!
63  * \brief Main Channel structure associated with a channel.
64  *
65  * \note When adding fields to this structure, it is important to add the field
66  *       'in position' with like-aligned fields, so as to keep the compiler from
67  *       having to add padding to align fields. The structure's fields are sorted
68  *       in this order: pointers, structures, long, int/enum, short, char. This
69  *       is especially important on 64-bit architectures, where mixing 4-byte
70  *       and 8-byte fields causes 4 bytes of padding to be added before many
71  *       8-byte fields.
72  */
73 struct ast_channel {
74         const struct ast_channel_tech *tech;            /*!< Technology (point to channel driver) */
75         void *tech_pvt;                                 /*!< Private data used by the technology driver */
76         void *music_state;                              /*!< Music State*/
77         void *generatordata;                            /*!< Current generator data if there is any */
78         struct ast_generator *generator;                /*!< Current active data generator */
79         struct ast_channel *masq;                       /*!< Channel that will masquerade as us */
80         struct ast_channel *masqr;                      /*!< Who we are masquerading as */
81         const char *blockproc;                          /*!< Procedure causing blocking */
82         const char *appl;                               /*!< Current application */
83         const char *data;                               /*!< Data passed to current application */
84         struct ast_sched_context *sched;                /*!< Schedule context */
85         struct ast_filestream *stream;                  /*!< Stream itself. */
86         struct ast_filestream *vstream;                 /*!< Video Stream itself. */
87         ast_timing_func_t timingfunc;
88         void *timingdata;
89         struct ast_pbx *pbx;                            /*!< PBX private structure for this channel */
90         struct ast_trans_pvt *writetrans;               /*!< Write translation path */
91         struct ast_trans_pvt *readtrans;                /*!< Read translation path */
92         struct ast_audiohook_list *audiohooks;
93         struct ast_framehook_list *framehooks;
94         struct ast_cdr *cdr;                            /*!< Call Detail Record */
95         struct ast_tone_zone *zone;                     /*!< Tone zone as set in indications.conf or
96                                                          *   in the CHANNEL dialplan function */
97         struct ast_channel_monitor *monitor;            /*!< Channel monitoring */
98         ast_callid callid;                      /*!< Bound call identifier pointer */
99 #ifdef HAVE_EPOLL
100         struct ast_epoll_data *epfd_data[AST_MAX_FDS];
101 #endif
102         struct ao2_container *dialed_causes;            /*!< Contains tech-specific and Asterisk cause data from dialed channels */
103
104         AST_DECLARE_STRING_FIELDS(
105                 AST_STRING_FIELD(name);         /*!< ASCII unique channel name */
106                 AST_STRING_FIELD(language);     /*!< Language requested for voice prompts */
107                 AST_STRING_FIELD(musicclass);   /*!< Default music class */
108                 AST_STRING_FIELD(latest_musicclass);   /*!< Latest active music class */
109                 AST_STRING_FIELD(accountcode);  /*!< Account code for billing */
110                 AST_STRING_FIELD(peeraccount);  /*!< Peer account code for billing */
111                 AST_STRING_FIELD(userfield);    /*!< Userfield for CEL billing */
112                 AST_STRING_FIELD(call_forward); /*!< Where to forward to if asked to dial on this interface */
113                 AST_STRING_FIELD(parkinglot);   /*! Default parking lot, if empty, default parking lot  */
114                 AST_STRING_FIELD(hangupsource); /*! Who is responsible for hanging up this channel */
115                 AST_STRING_FIELD(dialcontext);  /*!< Dial: Extension context that we were called from */
116         );
117
118         struct ast_channel_id uniqueid;         /*!< Unique Channel Identifier - can be specified on creation */
119         struct ast_channel_id linkedid;         /*!< Linked Channel Identifier - oldest propagated when bridged */
120
121         struct timeval whentohangup; /*!< Non-zero, set to actual time when channel is to be hung up */
122         pthread_t blocker;           /*!< If anyone is blocking, this is them */
123
124         /*!
125          * \brief Dialed/Called information.
126          * \note Set on incoming channels to indicate the originally dialed party.
127          * \note Dialed Number Identifier (DNID)
128          */
129         struct ast_party_dialed dialed;
130
131         /*!
132          * \brief Channel Caller ID information.
133          * \note The caller id information is the caller id of this
134          * channel when it is used to initiate a call.
135          */
136         struct ast_party_caller caller;
137
138         /*!
139          * \brief Channel Connected Line ID information.
140          * \note The connected line information identifies the channel
141          * connected/bridged to this channel.
142          */
143         struct ast_party_connected_line connected;
144
145         /*!
146          * \brief Channel Connected Line ID information that was last indicated.
147          */
148         struct ast_party_connected_line connected_indicated;
149
150         /*! \brief Redirecting/Diversion information */
151         struct ast_party_redirecting redirecting;
152
153         struct ast_frame dtmff;                         /*!< DTMF frame */
154         struct varshead varshead;                       /*!< A linked list for channel variables. See \ref AstChanVar */
155         ast_group_t callgroup;                          /*!< Call group for call pickups */
156         ast_group_t pickupgroup;                        /*!< Pickup group - which calls groups can be picked up? */
157         struct ast_namedgroups *named_callgroups;       /*!< Named call group for call pickups */
158         struct ast_namedgroups *named_pickupgroups;     /*!< Named pickup group - which call groups can be picked up? */
159         struct timeval creationtime;                    /*!< The time of channel creation */
160         struct timeval answertime;                              /*!< The time the channel was answered */
161         struct ast_readq_list readq;
162         struct ast_jb jb;                               /*!< The jitterbuffer state */
163         struct timeval dtmf_tv;                         /*!< The time that an in process digit began, or the last digit ended */
164         struct ast_hangup_handler_list hangup_handlers;/*!< Hangup handlers on the channel. */
165         struct ast_datastore_list datastores; /*!< Data stores on the channel */
166         struct ast_autochan_list autochans; /*!< Autochans on the channel */
167         unsigned long insmpl;                           /*!< Track the read/written samples for monitor use */
168         unsigned long outsmpl;                          /*!< Track the read/written samples for monitor use */
169
170         int fds[AST_MAX_FDS];                           /*!< File descriptors for channel -- Drivers will poll on
171                                                          *   these file descriptors, so at least one must be non -1.
172                                                          *   See \arg \ref AstFileDesc */
173         int softhangup;                         /*!< Whether or not we have been hung up...  Do not set this value
174                                                          *   directly, use ast_softhangup() */
175         int fdno;                                       /*!< Which fd had an event detected on */
176         int streamid;                                   /*!< For streaming playback, the schedule ID */
177         int vstreamid;                                  /*!< For streaming video playback, the schedule ID */
178         struct ast_format *oldwriteformat;  /*!< Original writer format */
179         int timingfd;                                   /*!< Timing fd */
180         enum ast_channel_state state;                   /*!< State of line -- Don't write directly, use ast_setstate() */
181         int rings;                                      /*!< Number of rings so far */
182         int priority;                                   /*!< Dialplan: Current extension priority */
183         int macropriority;                              /*!< Macro: Current non-macro priority. See app_macro.c */
184         int amaflags;                                   /*!< Set BEFORE PBX is started to determine AMA flags */
185         enum ast_channel_adsicpe adsicpe;               /*!< Whether or not ADSI is detected on CPE */
186         unsigned int fin;                               /*!< Frames in counters. The high bit is a debug mask, so
187                                                          *   the counter is only in the remaining bits */
188         unsigned int fout;                              /*!< Frames out counters. The high bit is a debug mask, so
189                                                          *   the counter is only in the remaining bits */
190         int hangupcause;                                /*!< Why is the channel hanged up. See causes.h */
191         unsigned int finalized:1;       /*!< Whether or not the channel has been successfully allocated */
192         struct ast_flags flags;                         /*!< channel flags of AST_FLAG_ type */
193         int alertpipe[2];
194         struct ast_format_cap *nativeformats;         /*!< Kinds of data this channel can natively handle */
195         struct ast_format *readformat;            /*!< Requested read format (after translation) */
196         struct ast_format *writeformat;           /*!< Requested write format (before translation) */
197         struct ast_format *rawreadformat;         /*!< Raw read format (before translation) */
198         struct ast_format *rawwriteformat;        /*!< Raw write format (after translation) */
199         unsigned int emulate_dtmf_duration;             /*!< Number of ms left to emulate DTMF for */
200 #ifdef HAVE_EPOLL
201         int epfd;
202 #endif
203         int visible_indication;                         /*!< Indication currently playing on the channel */
204         int hold_state;                                                 /*!< Current Hold/Unhold state */
205
206         unsigned short transfercapability;              /*!< ISDN Transfer Capability - AST_FLAG_DIGITAL is not enough */
207
208         struct ast_bridge *bridge;                      /*!< Bridge this channel is participating in */
209         struct ast_bridge_channel *bridge_channel;/*!< The bridge_channel this channel is linked with. */
210         struct ast_timer *timer;                        /*!< timer object that provided timingfd */
211
212         char context[AST_MAX_CONTEXT];                  /*!< Dialplan: Current extension context */
213         char exten[AST_MAX_EXTENSION];                  /*!< Dialplan: Current extension number */
214         char macrocontext[AST_MAX_CONTEXT];             /*!< Macro: Current non-macro context. See app_macro.c */
215         char macroexten[AST_MAX_EXTENSION];             /*!< Macro: Current non-macro extension. See app_macro.c */
216         char unbridged;                                                 /*!< non-zero if the bridge core needs to re-evaluate the current
217                                                                                          bridging technology which is in use by this channel's bridge. */
218         char is_t38_active;                                             /*!< non-zero if T.38 is active on this channel. */
219         char dtmf_digit_to_emulate;                     /*!< Digit being emulated */
220         char sending_dtmf_digit;                        /*!< Digit this channel is currently sending out. (zero if not sending) */
221         struct timeval sending_dtmf_tv;         /*!< The time this channel started sending the current digit. (Invalid if sending_dtmf_digit is zero.) */
222         struct stasis_cp_single *topics;                /*!< Topic for all channel's events */
223         struct stasis_forward *endpoint_forward;        /*!< Subscription for event forwarding to endpoint's topic */
224         struct stasis_forward *endpoint_cache_forward; /*!< Subscription for cache updates to endpoint's topic */
225         struct ast_stream_topology *stream_topology; /*!< Stream topology */
226         struct ast_stream *default_streams[AST_MEDIA_TYPE_END]; /*!< Default streams indexed by media type */
227 };
228
229 /*! \brief The monotonically increasing integer counter for channel uniqueids */
230 static int uniqueint;
231
232 /* AST_DATA definitions, which will probably have to be re-thought since the channel will be opaque */
233
234 #if 0   /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
235 #define DATA_EXPORT_CALLERID(MEMBER)                            \
236         MEMBER(ast_callerid, cid_dnid, AST_DATA_STRING)         \
237         MEMBER(ast_callerid, cid_num, AST_DATA_STRING)          \
238         MEMBER(ast_callerid, cid_name, AST_DATA_STRING)         \
239         MEMBER(ast_callerid, cid_ani, AST_DATA_STRING)          \
240         MEMBER(ast_callerid, cid_pres, AST_DATA_INTEGER)        \
241         MEMBER(ast_callerid, cid_ani2, AST_DATA_INTEGER)        \
242         MEMBER(ast_callerid, cid_tag, AST_DATA_STRING)
243
244 AST_DATA_STRUCTURE(ast_callerid, DATA_EXPORT_CALLERID);
245 #endif
246
247 #define DATA_EXPORT_CHANNEL(MEMBER)                                             \
248         MEMBER(ast_channel, blockproc, AST_DATA_STRING)                         \
249         MEMBER(ast_channel, appl, AST_DATA_STRING)                              \
250         MEMBER(ast_channel, data, AST_DATA_STRING)                              \
251         MEMBER(ast_channel, name, AST_DATA_STRING) \
252         MEMBER(ast_channel, language, AST_DATA_STRING)                          \
253         MEMBER(ast_channel, musicclass, AST_DATA_STRING)                        \
254         MEMBER(ast_channel, accountcode, AST_DATA_STRING)                       \
255         MEMBER(ast_channel, peeraccount, AST_DATA_STRING)                       \
256         MEMBER(ast_channel, userfield, AST_DATA_STRING)                         \
257         MEMBER(ast_channel, call_forward, AST_DATA_STRING)                      \
258         MEMBER(ast_channel, parkinglot, AST_DATA_STRING)                        \
259         MEMBER(ast_channel, hangupsource, AST_DATA_STRING)                      \
260         MEMBER(ast_channel, dialcontext, AST_DATA_STRING)                       \
261         MEMBER(ast_channel, rings, AST_DATA_INTEGER)                            \
262         MEMBER(ast_channel, priority, AST_DATA_INTEGER)                         \
263         MEMBER(ast_channel, macropriority, AST_DATA_INTEGER)                    \
264         MEMBER(ast_channel, adsicpe, AST_DATA_INTEGER)                          \
265         MEMBER(ast_channel, fin, AST_DATA_UNSIGNED_INTEGER)                     \
266         MEMBER(ast_channel, fout, AST_DATA_UNSIGNED_INTEGER)                    \
267         MEMBER(ast_channel, emulate_dtmf_duration, AST_DATA_UNSIGNED_INTEGER)   \
268         MEMBER(ast_channel, visible_indication, AST_DATA_INTEGER)               \
269         MEMBER(ast_channel, context, AST_DATA_STRING)                           \
270         MEMBER(ast_channel, exten, AST_DATA_STRING)                             \
271         MEMBER(ast_channel, macrocontext, AST_DATA_STRING)                      \
272         MEMBER(ast_channel, macroexten, AST_DATA_STRING)
273
274 AST_DATA_STRUCTURE(ast_channel, DATA_EXPORT_CHANNEL);
275
276 static void channel_data_add_flags(struct ast_data *tree,
277         struct ast_channel *chan)
278 {
279         ast_data_add_bool(tree, "DEFER_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF));
280         ast_data_add_bool(tree, "WRITE_INT", ast_test_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT));
281         ast_data_add_bool(tree, "BLOCKING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING));
282         ast_data_add_bool(tree, "ZOMBIE", ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE));
283         ast_data_add_bool(tree, "EXCEPTION", ast_test_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION));
284         ast_data_add_bool(tree, "MOH", ast_test_flag(ast_channel_flags(chan), AST_FLAG_MOH));
285         ast_data_add_bool(tree, "SPYING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_SPYING));
286         ast_data_add_bool(tree, "IN_AUTOLOOP", ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP));
287         ast_data_add_bool(tree, "OUTGOING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING));
288         ast_data_add_bool(tree, "IN_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF));
289         ast_data_add_bool(tree, "EMULATE_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF));
290         ast_data_add_bool(tree, "END_DTMF_ONLY", ast_test_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY));
291         ast_data_add_bool(tree, "MASQ_NOSTREAM", ast_test_flag(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM));
292         ast_data_add_bool(tree, "BRIDGE_HANGUP_RUN", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_RUN));
293         ast_data_add_bool(tree, "DISABLE_WORKAROUNDS", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_WORKAROUNDS));
294         ast_data_add_bool(tree, "DISABLE_DEVSTATE_CACHE", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE));
295         ast_data_add_bool(tree, "BRIDGE_DUAL_REDIRECT_WAIT", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT));
296         ast_data_add_bool(tree, "ORIGINATED", ast_test_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED));
297         ast_data_add_bool(tree, "DEAD", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEAD));
298 }
299
300 int ast_channel_data_add_structure(struct ast_data *tree,
301         struct ast_channel *chan, int add_bridged)
302 {
303         struct ast_data *data_bridged;
304         struct ast_data *data_cdr;
305         struct ast_data *data_flags;
306         struct ast_data *data_zones;
307         struct ast_data *enum_node;
308         struct ast_data *data_softhangup;
309 #if 0   /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
310         struct ast_data *data_callerid;
311         char value_str[100];
312 #endif
313
314         if (!tree) {
315                 return -1;
316         }
317
318         ast_data_add_structure(ast_channel, tree, chan);
319
320         if (add_bridged) {
321                 RAII_VAR(struct ast_channel *, bc, ast_channel_bridge_peer(chan), ast_channel_cleanup);
322                 if (bc) {
323                         data_bridged = ast_data_add_node(tree, "bridged");
324                         if (!data_bridged) {
325                                 return -1;
326                         }
327                         ast_channel_data_add_structure(data_bridged, bc, 0);
328                 }
329         }
330
331         ast_data_add_str(tree, "uniqueid", ast_channel_uniqueid(chan));
332         ast_data_add_str(tree, "linkedid", ast_channel_linkedid(chan));
333
334         ast_data_add_codec(tree, "oldwriteformat", ast_channel_oldwriteformat(chan));
335         ast_data_add_codec(tree, "readformat", ast_channel_readformat(chan));
336         ast_data_add_codec(tree, "writeformat", ast_channel_writeformat(chan));
337         ast_data_add_codec(tree, "rawreadformat", ast_channel_rawreadformat(chan));
338         ast_data_add_codec(tree, "rawwriteformat", ast_channel_rawwriteformat(chan));
339         ast_data_add_codecs(tree, "nativeformats", ast_channel_nativeformats(chan));
340
341         /* state */
342         enum_node = ast_data_add_node(tree, "state");
343         if (!enum_node) {
344                 return -1;
345         }
346         ast_data_add_str(enum_node, "text", ast_state2str(ast_channel_state(chan)));
347         ast_data_add_int(enum_node, "value", ast_channel_state(chan));
348
349         /* hangupcause */
350         enum_node = ast_data_add_node(tree, "hangupcause");
351         if (!enum_node) {
352                 return -1;
353         }
354         ast_data_add_str(enum_node, "text", ast_cause2str(ast_channel_hangupcause(chan)));
355         ast_data_add_int(enum_node, "value", ast_channel_hangupcause(chan));
356
357         /* amaflags */
358         enum_node = ast_data_add_node(tree, "amaflags");
359         if (!enum_node) {
360                 return -1;
361         }
362         ast_data_add_str(enum_node, "text", ast_channel_amaflags2string(ast_channel_amaflags(chan)));
363         ast_data_add_int(enum_node, "value", ast_channel_amaflags(chan));
364
365         /* transfercapability */
366         enum_node = ast_data_add_node(tree, "transfercapability");
367         if (!enum_node) {
368                 return -1;
369         }
370         ast_data_add_str(enum_node, "text", ast_transfercapability2str(ast_channel_transfercapability(chan)));
371         ast_data_add_int(enum_node, "value", ast_channel_transfercapability(chan));
372
373         /* _softphangup */
374         data_softhangup = ast_data_add_node(tree, "softhangup");
375         if (!data_softhangup) {
376                 return -1;
377         }
378         ast_data_add_bool(data_softhangup, "dev", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_DEV);
379         ast_data_add_bool(data_softhangup, "asyncgoto", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO);
380         ast_data_add_bool(data_softhangup, "shutdown", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_SHUTDOWN);
381         ast_data_add_bool(data_softhangup, "timeout", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_TIMEOUT);
382         ast_data_add_bool(data_softhangup, "appunload", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_APPUNLOAD);
383         ast_data_add_bool(data_softhangup, "explicit", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_EXPLICIT);
384
385         /* channel flags */
386         data_flags = ast_data_add_node(tree, "flags");
387         if (!data_flags) {
388                 return -1;
389         }
390         channel_data_add_flags(data_flags, chan);
391
392         ast_data_add_uint(tree, "timetohangup", ast_channel_whentohangup(chan)->tv_sec);
393
394 #if 0   /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
395         /* callerid */
396         data_callerid = ast_data_add_node(tree, "callerid");
397         if (!data_callerid) {
398                 return -1;
399         }
400         ast_data_add_structure(ast_callerid, data_callerid, &(chan->cid));
401         /* insert the callerid ton */
402         enum_node = ast_data_add_node(data_callerid, "cid_ton");
403         if (!enum_node) {
404                 return -1;
405         }
406         ast_data_add_int(enum_node, "value", chan->cid.cid_ton);
407         snprintf(value_str, sizeof(value_str), "TON: %s/Plan: %s",
408                 party_number_ton2str(chan->cid.cid_ton),
409                 party_number_plan2str(chan->cid.cid_ton));
410         ast_data_add_str(enum_node, "text", value_str);
411 #endif
412
413         /* tone zone */
414         if (ast_channel_zone(chan)) {
415                 data_zones = ast_data_add_node(tree, "zone");
416                 if (!data_zones) {
417                         return -1;
418                 }
419                 ast_tone_zone_data_add_structure(data_zones, ast_channel_zone(chan));
420         }
421
422         /* insert cdr */
423         data_cdr = ast_data_add_node(tree, "cdr");
424         if (!data_cdr) {
425                 return -1;
426         }
427
428         return 0;
429 }
430
431 int ast_channel_data_cmp_structure(const struct ast_data_search *tree,
432         struct ast_channel *chan, const char *structure_name)
433 {
434         return ast_data_search_cmp_structure(tree, ast_channel, chan, structure_name);
435 }
436
437 /* ACCESSORS */
438
439 #define DEFINE_STRINGFIELD_SETTERS_FOR(field, publish, assert_on_null) \
440 void ast_channel_##field##_set(struct ast_channel *chan, const char *value) \
441 { \
442         if ((assert_on_null)) ast_assert(!ast_strlen_zero(value)); \
443         if (!strcmp(value, chan->field)) return; \
444         ast_string_field_set(chan, field, value); \
445         if (publish && ast_channel_internal_is_finalized(chan)) ast_channel_publish_snapshot(chan); \
446 } \
447   \
448 void ast_channel_##field##_build_va(struct ast_channel *chan, const char *fmt, va_list ap) \
449 { \
450         ast_string_field_build_va(chan, field, fmt, ap); \
451         if (publish && ast_channel_internal_is_finalized(chan)) ast_channel_publish_snapshot(chan); \
452 } \
453 void ast_channel_##field##_build(struct ast_channel *chan, const char *fmt, ...) \
454 { \
455         va_list ap; \
456         va_start(ap, fmt); \
457         ast_channel_##field##_build_va(chan, fmt, ap); \
458         va_end(ap); \
459 }
460
461 DEFINE_STRINGFIELD_SETTERS_FOR(name, 0, 1);
462 DEFINE_STRINGFIELD_SETTERS_FOR(language, 1, 0);
463 DEFINE_STRINGFIELD_SETTERS_FOR(musicclass, 0, 0);
464 DEFINE_STRINGFIELD_SETTERS_FOR(latest_musicclass, 0, 0);
465 DEFINE_STRINGFIELD_SETTERS_FOR(accountcode, 1, 0);
466 DEFINE_STRINGFIELD_SETTERS_FOR(peeraccount, 1, 0);
467 DEFINE_STRINGFIELD_SETTERS_FOR(userfield, 0, 0);
468 DEFINE_STRINGFIELD_SETTERS_FOR(call_forward, 0, 0);
469 DEFINE_STRINGFIELD_SETTERS_FOR(parkinglot, 0, 0);
470 DEFINE_STRINGFIELD_SETTERS_FOR(hangupsource, 0, 0);
471 DEFINE_STRINGFIELD_SETTERS_FOR(dialcontext, 0, 0);
472
473 #define DEFINE_STRINGFIELD_GETTER_FOR(field) const char *ast_channel_##field(const struct ast_channel *chan) \
474 { \
475         return chan->field; \
476 }
477
478 DEFINE_STRINGFIELD_GETTER_FOR(name);
479 DEFINE_STRINGFIELD_GETTER_FOR(language);
480 DEFINE_STRINGFIELD_GETTER_FOR(musicclass);
481 DEFINE_STRINGFIELD_GETTER_FOR(latest_musicclass);
482 DEFINE_STRINGFIELD_GETTER_FOR(accountcode);
483 DEFINE_STRINGFIELD_GETTER_FOR(peeraccount);
484 DEFINE_STRINGFIELD_GETTER_FOR(userfield);
485 DEFINE_STRINGFIELD_GETTER_FOR(call_forward);
486 DEFINE_STRINGFIELD_GETTER_FOR(parkinglot);
487 DEFINE_STRINGFIELD_GETTER_FOR(hangupsource);
488 DEFINE_STRINGFIELD_GETTER_FOR(dialcontext);
489
490 const char *ast_channel_uniqueid(const struct ast_channel *chan)
491 {
492         ast_assert(chan->uniqueid.unique_id[0] != '\0');
493         return chan->uniqueid.unique_id;
494 }
495
496 const char *ast_channel_linkedid(const struct ast_channel *chan)
497 {
498         ast_assert(chan->linkedid.unique_id[0] != '\0');
499         return chan->linkedid.unique_id;
500 }
501
502 const char *ast_channel_appl(const struct ast_channel *chan)
503 {
504         return chan->appl;
505 }
506 void ast_channel_appl_set(struct ast_channel *chan, const char *value)
507 {
508         chan->appl = value;
509 }
510 const char *ast_channel_blockproc(const struct ast_channel *chan)
511 {
512         return chan->blockproc;
513 }
514 void ast_channel_blockproc_set(struct ast_channel *chan, const char *value)
515 {
516         chan->blockproc = value;
517 }
518 const char *ast_channel_data(const struct ast_channel *chan)
519 {
520         return chan->data;
521 }
522 void ast_channel_data_set(struct ast_channel *chan, const char *value)
523 {
524         chan->data = value;
525 }
526
527 const char *ast_channel_context(const struct ast_channel *chan)
528 {
529         return chan->context;
530 }
531 void ast_channel_context_set(struct ast_channel *chan, const char *value)
532 {
533         ast_copy_string(chan->context, value, sizeof(chan->context));
534 }
535 const char *ast_channel_exten(const struct ast_channel *chan)
536 {
537         return chan->exten;
538 }
539 void ast_channel_exten_set(struct ast_channel *chan, const char *value)
540 {
541         ast_copy_string(chan->exten, value, sizeof(chan->exten));
542 }
543 const char *ast_channel_macrocontext(const struct ast_channel *chan)
544 {
545         return chan->macrocontext;
546 }
547 void ast_channel_macrocontext_set(struct ast_channel *chan, const char *value)
548 {
549         ast_copy_string(chan->macrocontext, value, sizeof(chan->macrocontext));
550 }
551 const char *ast_channel_macroexten(const struct ast_channel *chan)
552 {
553         return chan->macroexten;
554 }
555 void ast_channel_macroexten_set(struct ast_channel *chan, const char *value)
556 {
557         ast_copy_string(chan->macroexten, value, sizeof(chan->macroexten));
558 }
559
560 char ast_channel_dtmf_digit_to_emulate(const struct ast_channel *chan)
561 {
562         return chan->dtmf_digit_to_emulate;
563 }
564 void ast_channel_dtmf_digit_to_emulate_set(struct ast_channel *chan, char value)
565 {
566         chan->dtmf_digit_to_emulate = value;
567 }
568
569 char ast_channel_sending_dtmf_digit(const struct ast_channel *chan)
570 {
571         return chan->sending_dtmf_digit;
572 }
573 void ast_channel_sending_dtmf_digit_set(struct ast_channel *chan, char value)
574 {
575         chan->sending_dtmf_digit = value;
576 }
577
578 struct timeval ast_channel_sending_dtmf_tv(const struct ast_channel *chan)
579 {
580         return chan->sending_dtmf_tv;
581 }
582 void ast_channel_sending_dtmf_tv_set(struct ast_channel *chan, struct timeval value)
583 {
584         chan->sending_dtmf_tv = value;
585 }
586
587 enum ama_flags ast_channel_amaflags(const struct ast_channel *chan)
588 {
589         return chan->amaflags;
590 }
591
592 void ast_channel_amaflags_set(struct ast_channel *chan, enum ama_flags value)
593 {
594         if (chan->amaflags == value) {
595                 return;
596         }
597         chan->amaflags = value;
598         ast_channel_publish_snapshot(chan);
599 }
600
601 #ifdef HAVE_EPOLL
602 int ast_channel_epfd(const struct ast_channel *chan)
603 {
604         return chan->epfd;
605 }
606 void ast_channel_epfd_set(struct ast_channel *chan, int value)
607 {
608         chan->epfd = value;
609 }
610 #endif
611 int ast_channel_fdno(const struct ast_channel *chan)
612 {
613         return chan->fdno;
614 }
615 void ast_channel_fdno_set(struct ast_channel *chan, int value)
616 {
617         chan->fdno = value;
618 }
619 int ast_channel_hangupcause(const struct ast_channel *chan)
620 {
621         return chan->hangupcause;
622 }
623 void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
624 {
625         chan->hangupcause = value;
626 }
627 int ast_channel_macropriority(const struct ast_channel *chan)
628 {
629         return chan->macropriority;
630 }
631 void ast_channel_macropriority_set(struct ast_channel *chan, int value)
632 {
633         chan->macropriority = value;
634 }
635 int ast_channel_priority(const struct ast_channel *chan)
636 {
637         return chan->priority;
638 }
639 void ast_channel_priority_set(struct ast_channel *chan, int value)
640 {
641         chan->priority = value;
642 }
643 int ast_channel_rings(const struct ast_channel *chan)
644 {
645         return chan->rings;
646 }
647 void ast_channel_rings_set(struct ast_channel *chan, int value)
648 {
649         chan->rings = value;
650 }
651 int ast_channel_streamid(const struct ast_channel *chan)
652 {
653         return chan->streamid;
654 }
655 void ast_channel_streamid_set(struct ast_channel *chan, int value)
656 {
657         chan->streamid = value;
658 }
659 int ast_channel_timingfd(const struct ast_channel *chan)
660 {
661         return chan->timingfd;
662 }
663 void ast_channel_timingfd_set(struct ast_channel *chan, int value)
664 {
665         chan->timingfd = value;
666 }
667 int ast_channel_visible_indication(const struct ast_channel *chan)
668 {
669         return chan->visible_indication;
670 }
671 void ast_channel_visible_indication_set(struct ast_channel *chan, int value)
672 {
673         chan->visible_indication = value;
674 }
675 int ast_channel_hold_state(const struct ast_channel *chan)
676 {
677         return chan->hold_state;
678 }
679 void ast_channel_hold_state_set(struct ast_channel *chan, int value)
680 {
681         chan->hold_state = value;
682 }
683 int ast_channel_vstreamid(const struct ast_channel *chan)
684 {
685         return chan->vstreamid;
686 }
687 void ast_channel_vstreamid_set(struct ast_channel *chan, int value)
688 {
689         chan->vstreamid = value;
690 }
691 unsigned short ast_channel_transfercapability(const struct ast_channel *chan)
692 {
693         return chan->transfercapability;
694 }
695 void ast_channel_transfercapability_set(struct ast_channel *chan, unsigned short value)
696 {
697         chan->transfercapability = value;
698 }
699 unsigned int ast_channel_emulate_dtmf_duration(const struct ast_channel *chan)
700 {
701         return chan->emulate_dtmf_duration;
702 }
703 void ast_channel_emulate_dtmf_duration_set(struct ast_channel *chan, unsigned int value)
704 {
705         chan->emulate_dtmf_duration = value;
706 }
707 unsigned int ast_channel_fin(const struct ast_channel *chan)
708 {
709         return chan->fin;
710 }
711 void ast_channel_fin_set(struct ast_channel *chan, unsigned int value)
712 {
713         chan->fin = value;
714 }
715 unsigned int ast_channel_fout(const struct ast_channel *chan)
716 {
717         return chan->fout;
718 }
719 void ast_channel_fout_set(struct ast_channel *chan, unsigned int value)
720 {
721         chan->fout = value;
722 }
723 unsigned long ast_channel_insmpl(const struct ast_channel *chan)
724 {
725         return chan->insmpl;
726 }
727 void ast_channel_insmpl_set(struct ast_channel *chan, unsigned long value)
728 {
729         chan->insmpl = value;
730 }
731 unsigned long ast_channel_outsmpl(const struct ast_channel *chan)
732 {
733         return chan->outsmpl;
734 }
735 void ast_channel_outsmpl_set(struct ast_channel *chan, unsigned long value)
736 {
737         chan->outsmpl = value;
738 }
739 void *ast_channel_generatordata(const struct ast_channel *chan)
740 {
741         return chan->generatordata;
742 }
743 void ast_channel_generatordata_set(struct ast_channel *chan, void *value)
744 {
745         chan->generatordata = value;
746 }
747 void *ast_channel_music_state(const struct ast_channel *chan)
748 {
749         return chan->music_state;
750 }
751 void ast_channel_music_state_set(struct ast_channel *chan, void *value)
752 {
753         chan->music_state = value;
754 }
755 void *ast_channel_tech_pvt(const struct ast_channel *chan)
756 {
757         return chan->tech_pvt;
758 }
759 void ast_channel_tech_pvt_set(struct ast_channel *chan, void *value)
760 {
761         chan->tech_pvt = value;
762 }
763 void *ast_channel_timingdata(const struct ast_channel *chan)
764 {
765         return chan->timingdata;
766 }
767 void ast_channel_timingdata_set(struct ast_channel *chan, void *value)
768 {
769         chan->timingdata = value;
770 }
771 struct ast_audiohook_list *ast_channel_audiohooks(const struct ast_channel *chan)
772 {
773         return chan->audiohooks;
774 }
775 void ast_channel_audiohooks_set(struct ast_channel *chan, struct ast_audiohook_list *value)
776 {
777         chan->audiohooks = value;
778 }
779 struct ast_cdr *ast_channel_cdr(const struct ast_channel *chan)
780 {
781         return chan->cdr;
782 }
783 void ast_channel_cdr_set(struct ast_channel *chan, struct ast_cdr *value)
784 {
785         chan->cdr = value;
786 }
787 struct ast_channel *ast_channel_masq(const struct ast_channel *chan)
788 {
789         return chan->masq;
790 }
791 void ast_channel_masq_set(struct ast_channel *chan, struct ast_channel *value)
792 {
793         chan->masq = value;
794 }
795 struct ast_channel *ast_channel_masqr(const struct ast_channel *chan)
796 {
797         return chan->masqr;
798 }
799 void ast_channel_masqr_set(struct ast_channel *chan, struct ast_channel *value)
800 {
801         chan->masqr = value;
802 }
803 struct ast_channel_monitor *ast_channel_monitor(const struct ast_channel *chan)
804 {
805         return chan->monitor;
806 }
807 void ast_channel_monitor_set(struct ast_channel *chan, struct ast_channel_monitor *value)
808 {
809         chan->monitor = value;
810 }
811 struct ast_filestream *ast_channel_stream(const struct ast_channel *chan)
812 {
813         return chan->stream;
814 }
815 void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
816 {
817         chan->stream = value;
818 }
819 struct ast_filestream *ast_channel_vstream(const struct ast_channel *chan)
820 {
821         return chan->vstream;
822 }
823 void ast_channel_vstream_set(struct ast_channel *chan, struct ast_filestream *value)
824 {
825         chan->vstream = value;
826 }
827 struct ast_format_cap *ast_channel_nativeformats(const struct ast_channel *chan)
828 {
829         return chan->nativeformats;
830 }
831
832 static void channel_set_default_streams(struct ast_channel *chan)
833 {
834         enum ast_media_type type;
835
836         ast_assert(chan != NULL);
837
838         for (type = AST_MEDIA_TYPE_UNKNOWN; type < AST_MEDIA_TYPE_END; type++) {
839                 if (chan->stream_topology) {
840                         chan->default_streams[type] =
841                                 ast_stream_topology_get_first_stream_by_type(chan->stream_topology, type);
842                 } else {
843                         chan->default_streams[type] = NULL;
844                 }
845         }
846 }
847
848 void ast_channel_internal_set_stream_topology(struct ast_channel *chan,
849         struct ast_stream_topology *topology)
850 {
851         ast_stream_topology_free(chan->stream_topology);
852         chan->stream_topology = topology;
853         channel_set_default_streams(chan);
854 }
855
856 void ast_channel_nativeformats_set(struct ast_channel *chan,
857         struct ast_format_cap *value)
858 {
859         ast_assert(chan != NULL);
860
861         ao2_replace(chan->nativeformats, value);
862
863         /* If chan->stream_topology is NULL, the channel is being destroyed
864          * and topology is destroyed.
865          */
866         if (!chan->stream_topology) {
867                 return;
868         }
869
870         if (!chan->tech || !(chan->tech->properties & AST_CHAN_TP_MULTISTREAM) || !value) {
871                 struct ast_stream_topology *new_topology;
872
873                 if (!value) {
874                         new_topology = ast_stream_topology_alloc();
875                 } else {
876                         new_topology = ast_stream_topology_create_from_format_cap(value);
877                 }
878                 ast_channel_internal_set_stream_topology(chan, new_topology);
879         }
880 }
881
882 struct ast_framehook_list *ast_channel_framehooks(const struct ast_channel *chan)
883 {
884         return chan->framehooks;
885 }
886 void ast_channel_framehooks_set(struct ast_channel *chan, struct ast_framehook_list *value)
887 {
888         chan->framehooks = value;
889 }
890 struct ast_generator *ast_channel_generator(const struct ast_channel *chan)
891 {
892         return chan->generator;
893 }
894 void ast_channel_generator_set(struct ast_channel *chan, struct ast_generator *value)
895 {
896         chan->generator = value;
897 }
898 struct ast_pbx *ast_channel_pbx(const struct ast_channel *chan)
899 {
900         return chan->pbx;
901 }
902 void ast_channel_pbx_set(struct ast_channel *chan, struct ast_pbx *value)
903 {
904         chan->pbx = value;
905 }
906 struct ast_sched_context *ast_channel_sched(const struct ast_channel *chan)
907 {
908         return chan->sched;
909 }
910 void ast_channel_sched_set(struct ast_channel *chan, struct ast_sched_context *value)
911 {
912         chan->sched = value;
913 }
914 struct ast_timer *ast_channel_timer(const struct ast_channel *chan)
915 {
916         return chan->timer;
917 }
918 void ast_channel_timer_set(struct ast_channel *chan, struct ast_timer *value)
919 {
920         chan->timer = value;
921 }
922 struct ast_tone_zone *ast_channel_zone(const struct ast_channel *chan)
923 {
924         return chan->zone;
925 }
926 void ast_channel_zone_set(struct ast_channel *chan, struct ast_tone_zone *value)
927 {
928         chan->zone = value;
929 }
930 struct ast_trans_pvt *ast_channel_readtrans(const struct ast_channel *chan)
931 {
932         return chan->readtrans;
933 }
934 void ast_channel_readtrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
935 {
936         chan->readtrans = value;
937 }
938 struct ast_trans_pvt *ast_channel_writetrans(const struct ast_channel *chan)
939 {
940         return chan->writetrans;
941 }
942 void ast_channel_writetrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
943 {
944         chan->writetrans = value;
945 }
946 const struct ast_channel_tech *ast_channel_tech(const struct ast_channel *chan)
947 {
948         return chan->tech;
949 }
950 void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
951 {
952         chan->tech = value;
953 }
954 enum ast_channel_adsicpe ast_channel_adsicpe(const struct ast_channel *chan)
955 {
956         return chan->adsicpe;
957 }
958 void ast_channel_adsicpe_set(struct ast_channel *chan, enum ast_channel_adsicpe value)
959 {
960         chan->adsicpe = value;
961 }
962 enum ast_channel_state ast_channel_state(const struct ast_channel *chan)
963 {
964         return chan->state;
965 }
966 ast_callid ast_channel_callid(const struct ast_channel *chan)
967 {
968         return chan->callid;
969 }
970 void ast_channel_callid_set(struct ast_channel *chan, ast_callid callid)
971 {
972         char call_identifier_from[AST_CALLID_BUFFER_LENGTH];
973         char call_identifier_to[AST_CALLID_BUFFER_LENGTH];
974         call_identifier_from[0] = '\0';
975         ast_callid_strnprint(call_identifier_to, sizeof(call_identifier_to), callid);
976         if (chan->callid) {
977                 ast_callid_strnprint(call_identifier_from, sizeof(call_identifier_from), chan->callid);
978                 ast_debug(3, "Channel Call ID changing from %s to %s\n", call_identifier_from, call_identifier_to);
979         }
980
981         chan->callid = callid;
982
983         ast_test_suite_event_notify("CallIDChange",
984                 "State: CallIDChange\r\n"
985                 "Channel: %s\r\n"
986                 "CallID: %s\r\n"
987                 "PriorCallID: %s",
988                 ast_channel_name(chan),
989                 call_identifier_to,
990                 call_identifier_from);
991 }
992
993 void ast_channel_state_set(struct ast_channel *chan, enum ast_channel_state value)
994 {
995         chan->state = value;
996 }
997 void ast_channel_set_oldwriteformat(struct ast_channel *chan, struct ast_format *format)
998 {
999         ao2_replace(chan->oldwriteformat, format);
1000 }
1001 void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
1002 {
1003         ao2_replace(chan->rawreadformat, format);
1004 }
1005 void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
1006 {
1007         ao2_replace(chan->rawwriteformat, format);
1008 }
1009 void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
1010 {
1011         ao2_replace(chan->readformat, format);
1012 }
1013 void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
1014 {
1015         ao2_replace(chan->writeformat, format);
1016 }
1017 struct ast_format *ast_channel_oldwriteformat(struct ast_channel *chan)
1018 {
1019         return chan->oldwriteformat;
1020 }
1021 struct ast_format *ast_channel_rawreadformat(struct ast_channel *chan)
1022 {
1023         return chan->rawreadformat;
1024 }
1025 struct ast_format *ast_channel_rawwriteformat(struct ast_channel *chan)
1026 {
1027         return chan->rawwriteformat;
1028 }
1029 struct ast_format *ast_channel_readformat(struct ast_channel *chan)
1030 {
1031         return chan->readformat;
1032 }
1033 struct ast_format *ast_channel_writeformat(struct ast_channel *chan)
1034 {
1035         return chan->writeformat;
1036 }
1037 struct ast_hangup_handler_list *ast_channel_hangup_handlers(struct ast_channel *chan)
1038 {
1039         return &chan->hangup_handlers;
1040 }
1041 struct ast_datastore_list *ast_channel_datastores(struct ast_channel *chan)
1042 {
1043         return &chan->datastores;
1044 }
1045 struct ast_autochan_list *ast_channel_autochans(struct ast_channel *chan)
1046 {
1047         return &chan->autochans;
1048 }
1049 struct ast_readq_list *ast_channel_readq(struct ast_channel *chan)
1050 {
1051         return &chan->readq;
1052 }
1053 struct ast_frame *ast_channel_dtmff(struct ast_channel *chan)
1054 {
1055         return &chan->dtmff;
1056 }
1057 struct ast_jb *ast_channel_jb(struct ast_channel *chan)
1058 {
1059         return &chan->jb;
1060 }
1061 struct ast_party_caller *ast_channel_caller(struct ast_channel *chan)
1062 {
1063         return &chan->caller;
1064 }
1065 struct ast_party_connected_line *ast_channel_connected(struct ast_channel *chan)
1066 {
1067         return &chan->connected;
1068 }
1069 struct ast_party_connected_line *ast_channel_connected_indicated(struct ast_channel *chan)
1070 {
1071         return &chan->connected_indicated;
1072 }
1073 struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
1074 {
1075         return ast_party_id_merge(&chan->connected.id, &chan->connected.priv);
1076 }
1077 struct ast_party_dialed *ast_channel_dialed(struct ast_channel *chan)
1078 {
1079         return &chan->dialed;
1080 }
1081 struct ast_party_redirecting *ast_channel_redirecting(struct ast_channel *chan)
1082 {
1083         return &chan->redirecting;
1084 }
1085 struct ast_party_id ast_channel_redirecting_effective_orig(struct ast_channel *chan)
1086 {
1087         return ast_party_id_merge(&chan->redirecting.orig, &chan->redirecting.priv_orig);
1088 }
1089 struct ast_party_id ast_channel_redirecting_effective_from(struct ast_channel *chan)
1090 {
1091         return ast_party_id_merge(&chan->redirecting.from, &chan->redirecting.priv_from);
1092 }
1093 struct ast_party_id ast_channel_redirecting_effective_to(struct ast_channel *chan)
1094 {
1095         return ast_party_id_merge(&chan->redirecting.to, &chan->redirecting.priv_to);
1096 }
1097 struct timeval *ast_channel_dtmf_tv(struct ast_channel *chan)
1098 {
1099         return &chan->dtmf_tv;
1100 }
1101 struct timeval *ast_channel_whentohangup(struct ast_channel *chan)
1102 {
1103         return &chan->whentohangup;
1104 }
1105 struct varshead *ast_channel_varshead(struct ast_channel *chan)
1106 {
1107         return &chan->varshead;
1108 }
1109 void ast_channel_dtmff_set(struct ast_channel *chan, struct ast_frame *value)
1110 {
1111         chan->dtmff = *value;
1112 }
1113 void ast_channel_jb_set(struct ast_channel *chan, struct ast_jb *value)
1114 {
1115         chan->jb = *value;
1116 }
1117 void ast_channel_caller_set(struct ast_channel *chan, struct ast_party_caller *value)
1118 {
1119         chan->caller = *value;
1120 }
1121 void ast_channel_connected_set(struct ast_channel *chan, struct ast_party_connected_line *value)
1122 {
1123         chan->connected = *value;
1124 }
1125 void ast_channel_dialed_set(struct ast_channel *chan, struct ast_party_dialed *value)
1126 {
1127         chan->dialed = *value;
1128 }
1129 void ast_channel_redirecting_set(struct ast_channel *chan, struct ast_party_redirecting *value)
1130 {
1131         chan->redirecting = *value;
1132 }
1133 void ast_channel_dtmf_tv_set(struct ast_channel *chan, struct timeval *value)
1134 {
1135         chan->dtmf_tv = *value;
1136 }
1137 void ast_channel_whentohangup_set(struct ast_channel *chan, struct timeval *value)
1138 {
1139         chan->whentohangup = *value;
1140 }
1141 void ast_channel_varshead_set(struct ast_channel *chan, struct varshead *value)
1142 {
1143         chan->varshead = *value;
1144 }
1145 struct timeval ast_channel_creationtime(struct ast_channel *chan)
1146 {
1147         return chan->creationtime;
1148 }
1149 void ast_channel_creationtime_set(struct ast_channel *chan, struct timeval *value)
1150 {
1151         chan->creationtime = *value;
1152 }
1153
1154 struct timeval ast_channel_answertime(struct ast_channel *chan)
1155 {
1156         return chan->answertime;
1157 }
1158
1159 void ast_channel_answertime_set(struct ast_channel *chan, struct timeval *value)
1160 {
1161         chan->answertime = *value;
1162 }
1163
1164 /* Evil softhangup accessors */
1165 int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
1166 {
1167         return chan->softhangup;
1168 }
1169 void ast_channel_softhangup_internal_flag_set(struct ast_channel *chan, int value)
1170 {
1171         chan->softhangup = value;
1172 }
1173 void ast_channel_softhangup_internal_flag_add(struct ast_channel *chan, int value)
1174 {
1175         chan->softhangup |= value;
1176 }
1177 void ast_channel_softhangup_internal_flag_clear(struct ast_channel *chan, int value)
1178 {
1179         chan ->softhangup &= ~value;
1180 }
1181
1182 int ast_channel_unbridged_nolock(struct ast_channel *chan)
1183 {
1184         return chan->unbridged;
1185 }
1186
1187 int ast_channel_unbridged(struct ast_channel *chan)
1188 {
1189         int res;
1190         ast_channel_lock(chan);
1191         res = ast_channel_unbridged_nolock(chan);
1192         ast_channel_unlock(chan);
1193         return res;
1194 }
1195
1196 void ast_channel_set_unbridged_nolock(struct ast_channel *chan, int value)
1197 {
1198         chan->unbridged = !!value;
1199         ast_queue_frame(chan, &ast_null_frame);
1200 }
1201
1202 void ast_channel_set_unbridged(struct ast_channel *chan, int value)
1203 {
1204         ast_channel_lock(chan);
1205         ast_channel_set_unbridged_nolock(chan, value);
1206         ast_channel_unlock(chan);
1207 }
1208
1209 int ast_channel_is_t38_active_nolock(struct ast_channel *chan)
1210 {
1211         return chan->is_t38_active;
1212 }
1213
1214 int ast_channel_is_t38_active(struct ast_channel *chan)
1215 {
1216         int res;
1217
1218         ast_channel_lock(chan);
1219         res = ast_channel_is_t38_active_nolock(chan);
1220         ast_channel_unlock(chan);
1221         return res;
1222 }
1223
1224 void ast_channel_set_is_t38_active_nolock(struct ast_channel *chan, int is_t38_active)
1225 {
1226         chan->is_t38_active = !!is_t38_active;
1227 }
1228
1229 void ast_channel_set_is_t38_active(struct ast_channel *chan, int is_t38_active)
1230 {
1231         ast_channel_lock(chan);
1232         ast_channel_set_is_t38_active_nolock(chan, is_t38_active);
1233         ast_channel_unlock(chan);
1234 }
1235
1236 void ast_channel_callid_cleanup(struct ast_channel *chan)
1237 {
1238         chan->callid = 0;
1239 }
1240
1241 /* Typedef accessors */
1242 ast_group_t ast_channel_callgroup(const struct ast_channel *chan)
1243 {
1244         return chan->callgroup;
1245 }
1246 void ast_channel_callgroup_set(struct ast_channel *chan, ast_group_t value)
1247 {
1248         chan->callgroup = value;
1249 }
1250 ast_group_t ast_channel_pickupgroup(const struct ast_channel *chan)
1251 {
1252         return chan->pickupgroup;
1253 }
1254 void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
1255 {
1256         chan->pickupgroup = value;
1257 }
1258 struct ast_namedgroups *ast_channel_named_callgroups(const struct ast_channel *chan)
1259 {
1260         return chan->named_callgroups;
1261 }
1262 void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
1263 {
1264         ast_unref_namedgroups(chan->named_callgroups);
1265         chan->named_callgroups = ast_ref_namedgroups(value);
1266 }
1267 struct ast_namedgroups *ast_channel_named_pickupgroups(const struct ast_channel *chan)
1268 {
1269         return chan->named_pickupgroups;
1270 }
1271 void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
1272 {
1273         ast_unref_namedgroups(chan->named_pickupgroups);
1274         chan->named_pickupgroups = ast_ref_namedgroups(value);
1275 }
1276
1277 /* Alertpipe functions */
1278 int ast_channel_alert_write(struct ast_channel *chan)
1279 {
1280         char blah = 0x7F;
1281
1282         if (!ast_channel_alert_writable(chan)) {
1283                 errno = EBADF;
1284                 return 0;
1285         }
1286         /* preset errno in case returned size does not match */
1287         errno = EPIPE;
1288         return write(chan->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah);
1289 }
1290
1291 static int channel_internal_alert_check_nonblock(struct ast_channel *chan)
1292 {
1293         int flags;
1294
1295         flags = fcntl(chan->alertpipe[0], F_GETFL);
1296         /* For some odd reason, the alertpipe occasionally loses nonblocking status,
1297          * which immediately causes a deadlock scenario.  Detect and prevent this. */
1298         if ((flags & O_NONBLOCK) == 0) {
1299                 ast_log(LOG_ERROR, "Alertpipe on channel %s lost O_NONBLOCK?!!\n", ast_channel_name(chan));
1300                 if (fcntl(chan->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
1301                         ast_log(LOG_WARNING, "Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1302                         return -1;
1303                 }
1304         }
1305         return 0;
1306 }
1307
1308 ast_alert_status_t ast_channel_internal_alert_flush(struct ast_channel *chan)
1309 {
1310         int bytes_read;
1311         char blah[100];
1312
1313         if (!ast_channel_internal_alert_readable(chan)) {
1314                 return AST_ALERT_NOT_READABLE;
1315         }
1316         if (channel_internal_alert_check_nonblock(chan)) {
1317                 return AST_ALERT_READ_FATAL;
1318         }
1319
1320         /* Read the alertpipe until it is exhausted. */
1321         for (;;) {
1322                 bytes_read = read(chan->alertpipe[0], blah, sizeof(blah));
1323                 if (bytes_read < 0) {
1324                         if (errno == EINTR) {
1325                                 continue;
1326                         }
1327                         if (errno == EAGAIN || errno == EWOULDBLOCK) {
1328                                 /*
1329                                  * Would block so nothing left to read.
1330                                  * This is the normal loop exit.
1331                                  */
1332                                 break;
1333                         }
1334                         ast_log(LOG_WARNING, "read() failed flushing alertpipe: %s\n",
1335                                 strerror(errno));
1336                         return AST_ALERT_READ_FAIL;
1337                 }
1338                 if (!bytes_read) {
1339                         /* Read nothing so we are done */
1340                         break;
1341                 }
1342         }
1343
1344         return AST_ALERT_READ_SUCCESS;
1345 }
1346
1347 ast_alert_status_t ast_channel_internal_alert_read(struct ast_channel *chan)
1348 {
1349         char blah;
1350
1351         if (!ast_channel_internal_alert_readable(chan)) {
1352                 return AST_ALERT_NOT_READABLE;
1353         }
1354         if (channel_internal_alert_check_nonblock(chan)) {
1355                 return AST_ALERT_READ_FATAL;
1356         }
1357
1358         if (read(chan->alertpipe[0], &blah, sizeof(blah)) < 0) {
1359                 if (errno != EINTR && errno != EAGAIN) {
1360                         ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
1361                         return AST_ALERT_READ_FAIL;
1362                 }
1363         }
1364
1365         return AST_ALERT_READ_SUCCESS;
1366 }
1367
1368 int ast_channel_alert_writable(struct ast_channel *chan)
1369 {
1370         return chan->alertpipe[1] > -1;
1371 }
1372
1373 int ast_channel_internal_alert_readable(struct ast_channel *chan)
1374 {
1375         return chan->alertpipe[0] > -1;
1376 }
1377
1378 void ast_channel_internal_alertpipe_clear(struct ast_channel *chan)
1379 {
1380         chan->alertpipe[0] = chan->alertpipe[1] = -1;
1381 }
1382
1383 void ast_channel_internal_alertpipe_close(struct ast_channel *chan)
1384 {
1385         if (ast_channel_internal_alert_readable(chan)) {
1386                 close(chan->alertpipe[0]);
1387                 chan->alertpipe[0] = -1;
1388         }
1389         if (ast_channel_alert_writable(chan)) {
1390                 close(chan->alertpipe[1]);
1391                 chan->alertpipe[1] = -1;
1392         }
1393 }
1394
1395 int ast_channel_internal_alertpipe_init(struct ast_channel *chan)
1396 {
1397         if (pipe(chan->alertpipe)) {
1398                 ast_log(LOG_WARNING, "Channel allocation failed: Can't create alert pipe! Try increasing max file descriptors with ulimit -n\n");
1399                 return -1;
1400         } else {
1401                 int flags = fcntl(chan->alertpipe[0], F_GETFL);
1402                 if (fcntl(chan->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
1403                         ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1404                         return -1;
1405                 }
1406                 flags = fcntl(chan->alertpipe[1], F_GETFL);
1407                 if (fcntl(chan->alertpipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
1408                         ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1409                         return -1;
1410                 }
1411         }
1412         return 0;
1413 }
1414
1415 int ast_channel_internal_alert_readfd(struct ast_channel *chan)
1416 {
1417         return chan->alertpipe[0];
1418 }
1419
1420 void ast_channel_internal_alertpipe_swap(struct ast_channel *chan1, struct ast_channel *chan2)
1421 {
1422         int i;
1423         for (i = 0; i < ARRAY_LEN(chan1->alertpipe); i++) {
1424                 SWAP(chan1->alertpipe[i], chan2->alertpipe[i]);
1425         }
1426 }
1427
1428 /* file descriptor array accessors */
1429 void ast_channel_internal_fd_set(struct ast_channel *chan, int which, int value)
1430 {
1431         chan->fds[which] = value;
1432 }
1433 void ast_channel_internal_fd_clear(struct ast_channel *chan, int which)
1434 {
1435         ast_channel_internal_fd_set(chan, which, -1);
1436 }
1437 void ast_channel_internal_fd_clear_all(struct ast_channel *chan)
1438 {
1439         int i;
1440         for (i = 0; i < AST_MAX_FDS; i++) {
1441                 ast_channel_internal_fd_clear(chan, i);
1442         }
1443 }
1444 int ast_channel_fd(const struct ast_channel *chan, int which)
1445 {
1446         return chan->fds[which];
1447 }
1448 int ast_channel_fd_isset(const struct ast_channel *chan, int which)
1449 {
1450         return ast_channel_fd(chan, which) > -1;
1451 }
1452
1453 #ifdef HAVE_EPOLL
1454 struct ast_epoll_data *ast_channel_internal_epfd_data(const struct ast_channel *chan, int which)
1455 {
1456         return chan->epfd_data[which];
1457 }
1458 void ast_channel_internal_epfd_data_set(struct ast_channel *chan, int which , struct ast_epoll_data *value)
1459 {
1460         chan->epfd_data[which] = value;
1461 }
1462 #endif
1463
1464 pthread_t ast_channel_blocker(const struct ast_channel *chan)
1465 {
1466         return chan->blocker;
1467 }
1468 void ast_channel_blocker_set(struct ast_channel *chan, pthread_t value)
1469 {
1470         chan->blocker = value;
1471 }
1472
1473 ast_timing_func_t ast_channel_timingfunc(const struct ast_channel *chan)
1474 {
1475         return chan->timingfunc;
1476 }
1477 void ast_channel_timingfunc_set(struct ast_channel *chan, ast_timing_func_t value)
1478 {
1479         chan->timingfunc = value;
1480 }
1481
1482 struct ast_bridge *ast_channel_internal_bridge(const struct ast_channel *chan)
1483 {
1484         return chan->bridge;
1485 }
1486 void ast_channel_internal_bridge_set(struct ast_channel *chan, struct ast_bridge *value)
1487 {
1488         chan->bridge = value;
1489         ast_channel_publish_snapshot(chan);
1490 }
1491
1492 struct ast_bridge_channel *ast_channel_internal_bridge_channel(const struct ast_channel *chan)
1493 {
1494         return chan->bridge_channel;
1495 }
1496 void ast_channel_internal_bridge_channel_set(struct ast_channel *chan, struct ast_bridge_channel *value)
1497 {
1498         chan->bridge_channel = value;
1499 }
1500
1501 struct ast_flags *ast_channel_flags(struct ast_channel *chan)
1502 {
1503         return &chan->flags;
1504 }
1505
1506 static int collect_names_cb(void *obj, void *arg, int flags) {
1507         struct ast_control_pvt_cause_code *cause_code = obj;
1508         struct ast_str **str = arg;
1509
1510         ast_str_append(str, 0, "%s%s", (ast_str_strlen(*str) ? "," : ""), cause_code->chan_name);
1511
1512         return 0;
1513 }
1514
1515 struct ast_str *ast_channel_dialed_causes_channels(const struct ast_channel *chan)
1516 {
1517         struct ast_str *chanlist = ast_str_create(128);
1518
1519         if (!chanlist) {
1520                 return NULL;
1521         }
1522
1523         ao2_callback(chan->dialed_causes, 0, collect_names_cb, &chanlist);
1524
1525         return chanlist;
1526 }
1527
1528 struct ast_control_pvt_cause_code *ast_channel_dialed_causes_find(const struct ast_channel *chan, const char *chan_name)
1529 {
1530         return ao2_find(chan->dialed_causes, chan_name, OBJ_KEY);
1531 }
1532
1533 int ast_channel_dialed_causes_add(const struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
1534 {
1535         struct ast_control_pvt_cause_code *ao2_cause_code;
1536         ao2_find(chan->dialed_causes, cause_code->chan_name, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA);
1537         ao2_cause_code = ao2_alloc(datalen, NULL);
1538
1539         if (ao2_cause_code) {
1540                 memcpy(ao2_cause_code, cause_code, datalen);
1541                 ao2_link(chan->dialed_causes, ao2_cause_code);
1542                 ao2_ref(ao2_cause_code, -1);
1543                 return 0;
1544         } else {
1545                 return -1;
1546         }
1547 }
1548
1549 void ast_channel_dialed_causes_clear(const struct ast_channel *chan)
1550 {
1551         ao2_callback(chan->dialed_causes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
1552 }
1553
1554 /* \brief Hash function for pvt cause code frames */
1555 static int pvt_cause_hash_fn(const void *vpc, const int flags)
1556 {
1557         const struct ast_control_pvt_cause_code *pc = vpc;
1558         return ast_str_hash(ast_tech_to_upper(ast_strdupa(pc->chan_name)));
1559 }
1560
1561 /* \brief Comparison function for pvt cause code frames */
1562 static int pvt_cause_cmp_fn(void *obj, void *vstr, int flags)
1563 {
1564         struct ast_control_pvt_cause_code *pc = obj;
1565         char *str = ast_tech_to_upper(ast_strdupa(vstr));
1566         char *pc_str = ast_tech_to_upper(ast_strdupa(pc->chan_name));
1567         return !strcmp(pc_str, str) ? CMP_MATCH | CMP_STOP : 0;
1568 }
1569
1570 #define DIALED_CAUSES_BUCKETS 37
1571
1572 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)
1573 {
1574         struct ast_channel *tmp;
1575
1576         tmp = __ao2_alloc(sizeof(*tmp), destructor,
1577                 AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function);
1578
1579         if (!tmp) {
1580                 return NULL;
1581         }
1582
1583         if ((ast_string_field_init(tmp, 128))) {
1584                 return ast_channel_unref(tmp);
1585         }
1586
1587         if (!(tmp->dialed_causes = ao2_container_alloc(DIALED_CAUSES_BUCKETS, pvt_cause_hash_fn, pvt_cause_cmp_fn))) {
1588                 return ast_channel_unref(tmp);
1589         }
1590
1591         /* set the creation time in the uniqueid */
1592         tmp->uniqueid.creation_time = time(NULL);
1593         tmp->uniqueid.creation_unique = ast_atomic_fetchadd_int(&uniqueint, 1);
1594
1595         /* use provided id or default to historical {system-}time.# format */
1596         if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
1597                 ast_copy_string(tmp->uniqueid.unique_id, assignedids->uniqueid, sizeof(tmp->uniqueid.unique_id));
1598         } else if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
1599                 snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%li.%d",
1600                         (long)(tmp->uniqueid.creation_time),
1601                         tmp->uniqueid.creation_unique);
1602         } else {
1603                 snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%s-%li.%d",
1604                         ast_config_AST_SYSTEM_NAME,
1605                         (long)(tmp->uniqueid.creation_time),
1606                         tmp->uniqueid.creation_unique);
1607         }
1608
1609         /* copy linked id from parent channel if known */
1610         if (requestor) {
1611                 tmp->linkedid = requestor->linkedid;
1612         } else {
1613                 tmp->linkedid = tmp->uniqueid;
1614         }
1615
1616         return tmp;
1617 }
1618
1619 struct ast_channel *ast_channel_internal_oldest_linkedid(struct ast_channel *a, struct ast_channel *b)
1620 {
1621         ast_assert(a->linkedid.creation_time != 0);
1622         ast_assert(b->linkedid.creation_time != 0);
1623
1624         if (a->linkedid.creation_time < b->linkedid.creation_time) {
1625                 return a;
1626         }
1627         if (b->linkedid.creation_time < a->linkedid.creation_time) {
1628                 return b;
1629         }
1630         if (a->linkedid.creation_unique < b->linkedid.creation_unique) {
1631                 return a;
1632         }
1633         return b;
1634 }
1635
1636 void ast_channel_internal_copy_linkedid(struct ast_channel *dest, struct ast_channel *source)
1637 {
1638         if (dest->linkedid.creation_time == source->linkedid.creation_time
1639                 && dest->linkedid.creation_unique == source->linkedid.creation_unique
1640                 && !strcmp(dest->linkedid.unique_id, source->linkedid.unique_id)) {
1641                 return;
1642         }
1643         dest->linkedid = source->linkedid;
1644         ast_channel_publish_snapshot(dest);
1645 }
1646
1647 void ast_channel_internal_swap_uniqueid_and_linkedid(struct ast_channel *a, struct ast_channel *b)
1648 {
1649         struct ast_channel_id temp;
1650
1651         temp = a->uniqueid;
1652         a->uniqueid = b->uniqueid;
1653         b->uniqueid = temp;
1654
1655         temp = a->linkedid;
1656         a->linkedid = b->linkedid;
1657         b->linkedid = temp;
1658 }
1659
1660 void ast_channel_internal_swap_topics(struct ast_channel *a, struct ast_channel *b)
1661 {
1662         struct stasis_cp_single *temp;
1663
1664         temp = a->topics;
1665         a->topics = b->topics;
1666         b->topics = temp;
1667 }
1668
1669 void ast_channel_internal_set_fake_ids(struct ast_channel *chan, const char *uniqueid, const char *linkedid)
1670 {
1671         ast_copy_string(chan->uniqueid.unique_id, uniqueid, sizeof(chan->uniqueid.unique_id));
1672         ast_copy_string(chan->linkedid.unique_id, linkedid, sizeof(chan->linkedid.unique_id));
1673 }
1674
1675 void ast_channel_internal_cleanup(struct ast_channel *chan)
1676 {
1677         if (chan->dialed_causes) {
1678                 ao2_t_ref(chan->dialed_causes, -1,
1679                         "done with dialed causes since the channel is going away");
1680                 chan->dialed_causes = NULL;
1681         }
1682
1683         ast_string_field_free_memory(chan);
1684
1685         chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
1686         chan->endpoint_cache_forward = stasis_forward_cancel(chan->endpoint_cache_forward);
1687
1688         stasis_cp_single_unsubscribe(chan->topics);
1689         chan->topics = NULL;
1690
1691         ast_channel_internal_set_stream_topology(chan, NULL);
1692 }
1693
1694 void ast_channel_internal_finalize(struct ast_channel *chan)
1695 {
1696         chan->finalized = 1;
1697 }
1698
1699 int ast_channel_internal_is_finalized(struct ast_channel *chan)
1700 {
1701         return chan->finalized;
1702 }
1703
1704 struct stasis_topic *ast_channel_topic(struct ast_channel *chan)
1705 {
1706         if (!chan) {
1707                 return ast_channel_topic_all();
1708         }
1709
1710         return stasis_cp_single_topic(chan->topics);
1711 }
1712
1713 struct stasis_topic *ast_channel_topic_cached(struct ast_channel *chan)
1714 {
1715         if (!chan) {
1716                 return ast_channel_topic_all_cached();
1717         }
1718
1719         return stasis_cp_single_topic_cached(chan->topics);
1720 }
1721
1722 int ast_channel_forward_endpoint(struct ast_channel *chan,
1723         struct ast_endpoint *endpoint)
1724 {
1725         ast_assert(chan != NULL);
1726         ast_assert(endpoint != NULL);
1727
1728         chan->endpoint_forward =
1729                 stasis_forward_all(ast_channel_topic(chan),
1730                         ast_endpoint_topic(endpoint));
1731         if (!chan->endpoint_forward) {
1732                 return -1;
1733         }
1734
1735         chan->endpoint_cache_forward = stasis_forward_all(ast_channel_topic_cached(chan),
1736                 ast_endpoint_topic(endpoint));
1737         if (!chan->endpoint_cache_forward) {
1738                 chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
1739                 return -1;
1740         }
1741
1742         return 0;
1743 }
1744
1745 int ast_channel_internal_setup_topics(struct ast_channel *chan)
1746 {
1747         const char *topic_name = chan->uniqueid.unique_id;
1748         ast_assert(chan->topics == NULL);
1749
1750         if (ast_strlen_zero(topic_name)) {
1751                 topic_name = "<dummy-channel>";
1752         }
1753
1754         chan->topics = stasis_cp_single_create(
1755                 ast_channel_cache_all(), topic_name);
1756         if (!chan->topics) {
1757                 return -1;
1758         }
1759
1760         return 0;
1761 }
1762
1763 AST_THREADSTORAGE(channel_errno);
1764
1765 void ast_channel_internal_errno_set(enum ast_channel_error error)
1766 {
1767         enum ast_channel_error *error_code = ast_threadstorage_get(&channel_errno, sizeof(*error_code));
1768         if (!error_code) {
1769                 return;
1770         }
1771
1772         *error_code = error;
1773 }
1774
1775 enum ast_channel_error ast_channel_internal_errno(void)
1776 {
1777         enum ast_channel_error *error_code = ast_threadstorage_get(&channel_errno, sizeof(*error_code));
1778         if (!error_code) {
1779                 return AST_CHANNEL_ERROR_UNKNOWN;
1780         }
1781
1782         return *error_code;
1783 }
1784
1785 struct ast_stream_topology *ast_channel_get_stream_topology(
1786         const struct ast_channel *chan)
1787 {
1788         ast_assert(chan != NULL);
1789
1790         return chan->stream_topology;
1791 }
1792
1793 struct ast_stream_topology *ast_channel_set_stream_topology(struct ast_channel *chan,
1794         struct ast_stream_topology *topology)
1795 {
1796         struct ast_stream_topology *new_topology;
1797
1798         ast_assert(chan != NULL);
1799
1800         /* A non-MULTISTREAM channel can't manipulate topology directly */
1801         ast_assert(chan->tech != NULL && (chan->tech->properties & AST_CHAN_TP_MULTISTREAM));
1802
1803         /* Unless the channel is being destroyed, we always want a topology on
1804          * it even if its empty.
1805          */
1806         if (!topology) {
1807                 new_topology = ast_stream_topology_alloc();
1808         } else {
1809                 new_topology = topology;
1810         }
1811
1812         if (new_topology) {
1813                 ast_channel_internal_set_stream_topology(chan, new_topology);
1814         }
1815
1816         return new_topology;
1817 }
1818
1819 struct ast_stream *ast_channel_get_default_stream(struct ast_channel *chan,
1820         enum ast_media_type type)
1821 {
1822         ast_assert(chan != NULL);
1823         ast_assert(type < AST_MEDIA_TYPE_END);
1824
1825         return chan->default_streams[type];
1826 }
1827
1828 void ast_channel_internal_swap_stream_topology(struct ast_channel *chan1,
1829         struct ast_channel *chan2)
1830 {
1831         struct ast_stream_topology *tmp_topology;
1832
1833         ast_assert(chan1 != NULL && chan2 != NULL);
1834
1835         tmp_topology = chan1->stream_topology;
1836         chan1->stream_topology = chan2->stream_topology;
1837         chan2->stream_topology = tmp_topology;
1838
1839         channel_set_default_streams(chan1);
1840         channel_set_default_streams(chan2);
1841 }