CHANNEL(callid): Give dialplan access to the callid.
[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 ((!ast_channel_is_multistream(chan)) || !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         if (value->read_stream || value->write_stream) {
953                 ast_assert(value->read_stream && value->write_stream);
954         }
955
956         chan->tech = value;
957 }
958 enum ast_channel_adsicpe ast_channel_adsicpe(const struct ast_channel *chan)
959 {
960         return chan->adsicpe;
961 }
962 void ast_channel_adsicpe_set(struct ast_channel *chan, enum ast_channel_adsicpe value)
963 {
964         chan->adsicpe = value;
965 }
966 enum ast_channel_state ast_channel_state(const struct ast_channel *chan)
967 {
968         return chan->state;
969 }
970 ast_callid ast_channel_callid(const struct ast_channel *chan)
971 {
972         return chan->callid;
973 }
974 void ast_channel_callid_set(struct ast_channel *chan, ast_callid callid)
975 {
976         char call_identifier_from[AST_CALLID_BUFFER_LENGTH];
977         char call_identifier_to[AST_CALLID_BUFFER_LENGTH];
978         call_identifier_from[0] = '\0';
979         ast_callid_strnprint(call_identifier_to, sizeof(call_identifier_to), callid);
980         if (chan->callid) {
981                 ast_callid_strnprint(call_identifier_from, sizeof(call_identifier_from), chan->callid);
982                 ast_debug(3, "Channel Call ID changing from %s to %s\n", call_identifier_from, call_identifier_to);
983         }
984
985         chan->callid = callid;
986
987         ast_test_suite_event_notify("CallIDChange",
988                 "State: CallIDChange\r\n"
989                 "Channel: %s\r\n"
990                 "CallID: %s\r\n"
991                 "PriorCallID: %s",
992                 ast_channel_name(chan),
993                 call_identifier_to,
994                 call_identifier_from);
995 }
996
997 void ast_channel_state_set(struct ast_channel *chan, enum ast_channel_state value)
998 {
999         chan->state = value;
1000 }
1001 void ast_channel_set_oldwriteformat(struct ast_channel *chan, struct ast_format *format)
1002 {
1003         ao2_replace(chan->oldwriteformat, format);
1004 }
1005 void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
1006 {
1007         ao2_replace(chan->rawreadformat, format);
1008 }
1009 void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
1010 {
1011         ao2_replace(chan->rawwriteformat, format);
1012 }
1013 void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
1014 {
1015         ao2_replace(chan->readformat, format);
1016 }
1017 void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
1018 {
1019         ao2_replace(chan->writeformat, format);
1020 }
1021 struct ast_format *ast_channel_oldwriteformat(struct ast_channel *chan)
1022 {
1023         return chan->oldwriteformat;
1024 }
1025 struct ast_format *ast_channel_rawreadformat(struct ast_channel *chan)
1026 {
1027         return chan->rawreadformat;
1028 }
1029 struct ast_format *ast_channel_rawwriteformat(struct ast_channel *chan)
1030 {
1031         return chan->rawwriteformat;
1032 }
1033 struct ast_format *ast_channel_readformat(struct ast_channel *chan)
1034 {
1035         return chan->readformat;
1036 }
1037 struct ast_format *ast_channel_writeformat(struct ast_channel *chan)
1038 {
1039         return chan->writeformat;
1040 }
1041 struct ast_hangup_handler_list *ast_channel_hangup_handlers(struct ast_channel *chan)
1042 {
1043         return &chan->hangup_handlers;
1044 }
1045 struct ast_datastore_list *ast_channel_datastores(struct ast_channel *chan)
1046 {
1047         return &chan->datastores;
1048 }
1049 struct ast_autochan_list *ast_channel_autochans(struct ast_channel *chan)
1050 {
1051         return &chan->autochans;
1052 }
1053 struct ast_readq_list *ast_channel_readq(struct ast_channel *chan)
1054 {
1055         return &chan->readq;
1056 }
1057 struct ast_frame *ast_channel_dtmff(struct ast_channel *chan)
1058 {
1059         return &chan->dtmff;
1060 }
1061 struct ast_jb *ast_channel_jb(struct ast_channel *chan)
1062 {
1063         return &chan->jb;
1064 }
1065 struct ast_party_caller *ast_channel_caller(struct ast_channel *chan)
1066 {
1067         return &chan->caller;
1068 }
1069 struct ast_party_connected_line *ast_channel_connected(struct ast_channel *chan)
1070 {
1071         return &chan->connected;
1072 }
1073 struct ast_party_connected_line *ast_channel_connected_indicated(struct ast_channel *chan)
1074 {
1075         return &chan->connected_indicated;
1076 }
1077 struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
1078 {
1079         return ast_party_id_merge(&chan->connected.id, &chan->connected.priv);
1080 }
1081 struct ast_party_dialed *ast_channel_dialed(struct ast_channel *chan)
1082 {
1083         return &chan->dialed;
1084 }
1085 struct ast_party_redirecting *ast_channel_redirecting(struct ast_channel *chan)
1086 {
1087         return &chan->redirecting;
1088 }
1089 struct ast_party_id ast_channel_redirecting_effective_orig(struct ast_channel *chan)
1090 {
1091         return ast_party_id_merge(&chan->redirecting.orig, &chan->redirecting.priv_orig);
1092 }
1093 struct ast_party_id ast_channel_redirecting_effective_from(struct ast_channel *chan)
1094 {
1095         return ast_party_id_merge(&chan->redirecting.from, &chan->redirecting.priv_from);
1096 }
1097 struct ast_party_id ast_channel_redirecting_effective_to(struct ast_channel *chan)
1098 {
1099         return ast_party_id_merge(&chan->redirecting.to, &chan->redirecting.priv_to);
1100 }
1101 struct timeval *ast_channel_dtmf_tv(struct ast_channel *chan)
1102 {
1103         return &chan->dtmf_tv;
1104 }
1105 struct timeval *ast_channel_whentohangup(struct ast_channel *chan)
1106 {
1107         return &chan->whentohangup;
1108 }
1109 struct varshead *ast_channel_varshead(struct ast_channel *chan)
1110 {
1111         return &chan->varshead;
1112 }
1113 void ast_channel_dtmff_set(struct ast_channel *chan, struct ast_frame *value)
1114 {
1115         chan->dtmff = *value;
1116 }
1117 void ast_channel_jb_set(struct ast_channel *chan, struct ast_jb *value)
1118 {
1119         chan->jb = *value;
1120 }
1121 void ast_channel_caller_set(struct ast_channel *chan, struct ast_party_caller *value)
1122 {
1123         chan->caller = *value;
1124 }
1125 void ast_channel_connected_set(struct ast_channel *chan, struct ast_party_connected_line *value)
1126 {
1127         chan->connected = *value;
1128 }
1129 void ast_channel_dialed_set(struct ast_channel *chan, struct ast_party_dialed *value)
1130 {
1131         chan->dialed = *value;
1132 }
1133 void ast_channel_redirecting_set(struct ast_channel *chan, struct ast_party_redirecting *value)
1134 {
1135         chan->redirecting = *value;
1136 }
1137 void ast_channel_dtmf_tv_set(struct ast_channel *chan, struct timeval *value)
1138 {
1139         chan->dtmf_tv = *value;
1140 }
1141 void ast_channel_whentohangup_set(struct ast_channel *chan, struct timeval *value)
1142 {
1143         chan->whentohangup = *value;
1144 }
1145 void ast_channel_varshead_set(struct ast_channel *chan, struct varshead *value)
1146 {
1147         chan->varshead = *value;
1148 }
1149 struct timeval ast_channel_creationtime(struct ast_channel *chan)
1150 {
1151         return chan->creationtime;
1152 }
1153 void ast_channel_creationtime_set(struct ast_channel *chan, struct timeval *value)
1154 {
1155         chan->creationtime = *value;
1156 }
1157
1158 struct timeval ast_channel_answertime(struct ast_channel *chan)
1159 {
1160         return chan->answertime;
1161 }
1162
1163 void ast_channel_answertime_set(struct ast_channel *chan, struct timeval *value)
1164 {
1165         chan->answertime = *value;
1166 }
1167
1168 /* Evil softhangup accessors */
1169 int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
1170 {
1171         return chan->softhangup;
1172 }
1173 void ast_channel_softhangup_internal_flag_set(struct ast_channel *chan, int value)
1174 {
1175         chan->softhangup = value;
1176 }
1177 void ast_channel_softhangup_internal_flag_add(struct ast_channel *chan, int value)
1178 {
1179         chan->softhangup |= value;
1180 }
1181 void ast_channel_softhangup_internal_flag_clear(struct ast_channel *chan, int value)
1182 {
1183         chan ->softhangup &= ~value;
1184 }
1185
1186 int ast_channel_unbridged_nolock(struct ast_channel *chan)
1187 {
1188         return chan->unbridged;
1189 }
1190
1191 int ast_channel_unbridged(struct ast_channel *chan)
1192 {
1193         int res;
1194         ast_channel_lock(chan);
1195         res = ast_channel_unbridged_nolock(chan);
1196         ast_channel_unlock(chan);
1197         return res;
1198 }
1199
1200 void ast_channel_set_unbridged_nolock(struct ast_channel *chan, int value)
1201 {
1202         chan->unbridged = !!value;
1203         ast_queue_frame(chan, &ast_null_frame);
1204 }
1205
1206 void ast_channel_set_unbridged(struct ast_channel *chan, int value)
1207 {
1208         ast_channel_lock(chan);
1209         ast_channel_set_unbridged_nolock(chan, value);
1210         ast_channel_unlock(chan);
1211 }
1212
1213 int ast_channel_is_t38_active_nolock(struct ast_channel *chan)
1214 {
1215         return chan->is_t38_active;
1216 }
1217
1218 int ast_channel_is_t38_active(struct ast_channel *chan)
1219 {
1220         int res;
1221
1222         ast_channel_lock(chan);
1223         res = ast_channel_is_t38_active_nolock(chan);
1224         ast_channel_unlock(chan);
1225         return res;
1226 }
1227
1228 void ast_channel_set_is_t38_active_nolock(struct ast_channel *chan, int is_t38_active)
1229 {
1230         chan->is_t38_active = !!is_t38_active;
1231 }
1232
1233 void ast_channel_set_is_t38_active(struct ast_channel *chan, int is_t38_active)
1234 {
1235         ast_channel_lock(chan);
1236         ast_channel_set_is_t38_active_nolock(chan, is_t38_active);
1237         ast_channel_unlock(chan);
1238 }
1239
1240 void ast_channel_callid_cleanup(struct ast_channel *chan)
1241 {
1242         chan->callid = 0;
1243 }
1244
1245 /* Typedef accessors */
1246 ast_group_t ast_channel_callgroup(const struct ast_channel *chan)
1247 {
1248         return chan->callgroup;
1249 }
1250 void ast_channel_callgroup_set(struct ast_channel *chan, ast_group_t value)
1251 {
1252         chan->callgroup = value;
1253 }
1254 ast_group_t ast_channel_pickupgroup(const struct ast_channel *chan)
1255 {
1256         return chan->pickupgroup;
1257 }
1258 void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
1259 {
1260         chan->pickupgroup = value;
1261 }
1262 struct ast_namedgroups *ast_channel_named_callgroups(const struct ast_channel *chan)
1263 {
1264         return chan->named_callgroups;
1265 }
1266 void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
1267 {
1268         ast_unref_namedgroups(chan->named_callgroups);
1269         chan->named_callgroups = ast_ref_namedgroups(value);
1270 }
1271 struct ast_namedgroups *ast_channel_named_pickupgroups(const struct ast_channel *chan)
1272 {
1273         return chan->named_pickupgroups;
1274 }
1275 void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
1276 {
1277         ast_unref_namedgroups(chan->named_pickupgroups);
1278         chan->named_pickupgroups = ast_ref_namedgroups(value);
1279 }
1280
1281 /* Alertpipe functions */
1282 int ast_channel_alert_write(struct ast_channel *chan)
1283 {
1284         char blah = 0x7F;
1285
1286         if (!ast_channel_alert_writable(chan)) {
1287                 errno = EBADF;
1288                 return 0;
1289         }
1290         /* preset errno in case returned size does not match */
1291         errno = EPIPE;
1292         return write(chan->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah);
1293 }
1294
1295 static int channel_internal_alert_check_nonblock(struct ast_channel *chan)
1296 {
1297         int flags;
1298
1299         flags = fcntl(chan->alertpipe[0], F_GETFL);
1300         /* For some odd reason, the alertpipe occasionally loses nonblocking status,
1301          * which immediately causes a deadlock scenario.  Detect and prevent this. */
1302         if ((flags & O_NONBLOCK) == 0) {
1303                 ast_log(LOG_ERROR, "Alertpipe on channel %s lost O_NONBLOCK?!!\n", ast_channel_name(chan));
1304                 if (fcntl(chan->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
1305                         ast_log(LOG_WARNING, "Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1306                         return -1;
1307                 }
1308         }
1309         return 0;
1310 }
1311
1312 ast_alert_status_t ast_channel_internal_alert_flush(struct ast_channel *chan)
1313 {
1314         int bytes_read;
1315         char blah[100];
1316
1317         if (!ast_channel_internal_alert_readable(chan)) {
1318                 return AST_ALERT_NOT_READABLE;
1319         }
1320         if (channel_internal_alert_check_nonblock(chan)) {
1321                 return AST_ALERT_READ_FATAL;
1322         }
1323
1324         /* Read the alertpipe until it is exhausted. */
1325         for (;;) {
1326                 bytes_read = read(chan->alertpipe[0], blah, sizeof(blah));
1327                 if (bytes_read < 0) {
1328                         if (errno == EINTR) {
1329                                 continue;
1330                         }
1331                         if (errno == EAGAIN || errno == EWOULDBLOCK) {
1332                                 /*
1333                                  * Would block so nothing left to read.
1334                                  * This is the normal loop exit.
1335                                  */
1336                                 break;
1337                         }
1338                         ast_log(LOG_WARNING, "read() failed flushing alertpipe: %s\n",
1339                                 strerror(errno));
1340                         return AST_ALERT_READ_FAIL;
1341                 }
1342                 if (!bytes_read) {
1343                         /* Read nothing so we are done */
1344                         break;
1345                 }
1346         }
1347
1348         return AST_ALERT_READ_SUCCESS;
1349 }
1350
1351 ast_alert_status_t ast_channel_internal_alert_read(struct ast_channel *chan)
1352 {
1353         char blah;
1354
1355         if (!ast_channel_internal_alert_readable(chan)) {
1356                 return AST_ALERT_NOT_READABLE;
1357         }
1358         if (channel_internal_alert_check_nonblock(chan)) {
1359                 return AST_ALERT_READ_FATAL;
1360         }
1361
1362         if (read(chan->alertpipe[0], &blah, sizeof(blah)) < 0) {
1363                 if (errno != EINTR && errno != EAGAIN) {
1364                         ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
1365                         return AST_ALERT_READ_FAIL;
1366                 }
1367         }
1368
1369         return AST_ALERT_READ_SUCCESS;
1370 }
1371
1372 int ast_channel_alert_writable(struct ast_channel *chan)
1373 {
1374         return chan->alertpipe[1] > -1;
1375 }
1376
1377 int ast_channel_internal_alert_readable(struct ast_channel *chan)
1378 {
1379         return chan->alertpipe[0] > -1;
1380 }
1381
1382 void ast_channel_internal_alertpipe_clear(struct ast_channel *chan)
1383 {
1384         chan->alertpipe[0] = chan->alertpipe[1] = -1;
1385 }
1386
1387 void ast_channel_internal_alertpipe_close(struct ast_channel *chan)
1388 {
1389         if (ast_channel_internal_alert_readable(chan)) {
1390                 close(chan->alertpipe[0]);
1391                 chan->alertpipe[0] = -1;
1392         }
1393         if (ast_channel_alert_writable(chan)) {
1394                 close(chan->alertpipe[1]);
1395                 chan->alertpipe[1] = -1;
1396         }
1397 }
1398
1399 int ast_channel_internal_alertpipe_init(struct ast_channel *chan)
1400 {
1401         if (pipe(chan->alertpipe)) {
1402                 ast_log(LOG_WARNING, "Channel allocation failed: Can't create alert pipe! Try increasing max file descriptors with ulimit -n\n");
1403                 return -1;
1404         } else {
1405                 int flags = fcntl(chan->alertpipe[0], F_GETFL);
1406                 if (fcntl(chan->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
1407                         ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1408                         return -1;
1409                 }
1410                 flags = fcntl(chan->alertpipe[1], F_GETFL);
1411                 if (fcntl(chan->alertpipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
1412                         ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1413                         return -1;
1414                 }
1415         }
1416         return 0;
1417 }
1418
1419 int ast_channel_internal_alert_readfd(struct ast_channel *chan)
1420 {
1421         return chan->alertpipe[0];
1422 }
1423
1424 void ast_channel_internal_alertpipe_swap(struct ast_channel *chan1, struct ast_channel *chan2)
1425 {
1426         int i;
1427         for (i = 0; i < ARRAY_LEN(chan1->alertpipe); i++) {
1428                 SWAP(chan1->alertpipe[i], chan2->alertpipe[i]);
1429         }
1430 }
1431
1432 /* file descriptor array accessors */
1433 void ast_channel_internal_fd_set(struct ast_channel *chan, int which, int value)
1434 {
1435         chan->fds[which] = value;
1436 }
1437 void ast_channel_internal_fd_clear(struct ast_channel *chan, int which)
1438 {
1439         ast_channel_internal_fd_set(chan, which, -1);
1440 }
1441 void ast_channel_internal_fd_clear_all(struct ast_channel *chan)
1442 {
1443         int i;
1444         for (i = 0; i < AST_MAX_FDS; i++) {
1445                 ast_channel_internal_fd_clear(chan, i);
1446         }
1447 }
1448 int ast_channel_fd(const struct ast_channel *chan, int which)
1449 {
1450         return chan->fds[which];
1451 }
1452 int ast_channel_fd_isset(const struct ast_channel *chan, int which)
1453 {
1454         return ast_channel_fd(chan, which) > -1;
1455 }
1456
1457 #ifdef HAVE_EPOLL
1458 struct ast_epoll_data *ast_channel_internal_epfd_data(const struct ast_channel *chan, int which)
1459 {
1460         return chan->epfd_data[which];
1461 }
1462 void ast_channel_internal_epfd_data_set(struct ast_channel *chan, int which , struct ast_epoll_data *value)
1463 {
1464         chan->epfd_data[which] = value;
1465 }
1466 #endif
1467
1468 pthread_t ast_channel_blocker(const struct ast_channel *chan)
1469 {
1470         return chan->blocker;
1471 }
1472 void ast_channel_blocker_set(struct ast_channel *chan, pthread_t value)
1473 {
1474         chan->blocker = value;
1475 }
1476
1477 ast_timing_func_t ast_channel_timingfunc(const struct ast_channel *chan)
1478 {
1479         return chan->timingfunc;
1480 }
1481 void ast_channel_timingfunc_set(struct ast_channel *chan, ast_timing_func_t value)
1482 {
1483         chan->timingfunc = value;
1484 }
1485
1486 struct ast_bridge *ast_channel_internal_bridge(const struct ast_channel *chan)
1487 {
1488         return chan->bridge;
1489 }
1490 void ast_channel_internal_bridge_set(struct ast_channel *chan, struct ast_bridge *value)
1491 {
1492         chan->bridge = value;
1493         ast_channel_publish_snapshot(chan);
1494 }
1495
1496 struct ast_bridge_channel *ast_channel_internal_bridge_channel(const struct ast_channel *chan)
1497 {
1498         return chan->bridge_channel;
1499 }
1500 void ast_channel_internal_bridge_channel_set(struct ast_channel *chan, struct ast_bridge_channel *value)
1501 {
1502         chan->bridge_channel = value;
1503 }
1504
1505 struct ast_flags *ast_channel_flags(struct ast_channel *chan)
1506 {
1507         return &chan->flags;
1508 }
1509
1510 static int collect_names_cb(void *obj, void *arg, int flags) {
1511         struct ast_control_pvt_cause_code *cause_code = obj;
1512         struct ast_str **str = arg;
1513
1514         ast_str_append(str, 0, "%s%s", (ast_str_strlen(*str) ? "," : ""), cause_code->chan_name);
1515
1516         return 0;
1517 }
1518
1519 struct ast_str *ast_channel_dialed_causes_channels(const struct ast_channel *chan)
1520 {
1521         struct ast_str *chanlist = ast_str_create(128);
1522
1523         if (!chanlist) {
1524                 return NULL;
1525         }
1526
1527         ao2_callback(chan->dialed_causes, 0, collect_names_cb, &chanlist);
1528
1529         return chanlist;
1530 }
1531
1532 struct ast_control_pvt_cause_code *ast_channel_dialed_causes_find(const struct ast_channel *chan, const char *chan_name)
1533 {
1534         return ao2_find(chan->dialed_causes, chan_name, OBJ_KEY);
1535 }
1536
1537 int ast_channel_dialed_causes_add(const struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
1538 {
1539         struct ast_control_pvt_cause_code *ao2_cause_code;
1540         ao2_find(chan->dialed_causes, cause_code->chan_name, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA);
1541         ao2_cause_code = ao2_alloc(datalen, NULL);
1542
1543         if (ao2_cause_code) {
1544                 memcpy(ao2_cause_code, cause_code, datalen);
1545                 ao2_link(chan->dialed_causes, ao2_cause_code);
1546                 ao2_ref(ao2_cause_code, -1);
1547                 return 0;
1548         } else {
1549                 return -1;
1550         }
1551 }
1552
1553 void ast_channel_dialed_causes_clear(const struct ast_channel *chan)
1554 {
1555         ao2_callback(chan->dialed_causes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
1556 }
1557
1558 /* \brief Hash function for pvt cause code frames */
1559 static int pvt_cause_hash_fn(const void *vpc, const int flags)
1560 {
1561         const struct ast_control_pvt_cause_code *pc = vpc;
1562         return ast_str_hash(ast_tech_to_upper(ast_strdupa(pc->chan_name)));
1563 }
1564
1565 /* \brief Comparison function for pvt cause code frames */
1566 static int pvt_cause_cmp_fn(void *obj, void *vstr, int flags)
1567 {
1568         struct ast_control_pvt_cause_code *pc = obj;
1569         char *str = ast_tech_to_upper(ast_strdupa(vstr));
1570         char *pc_str = ast_tech_to_upper(ast_strdupa(pc->chan_name));
1571         return !strcmp(pc_str, str) ? CMP_MATCH | CMP_STOP : 0;
1572 }
1573
1574 #define DIALED_CAUSES_BUCKETS 37
1575
1576 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)
1577 {
1578         struct ast_channel *tmp;
1579
1580         tmp = __ao2_alloc(sizeof(*tmp), destructor,
1581                 AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function);
1582
1583         if (!tmp) {
1584                 return NULL;
1585         }
1586
1587         if ((ast_string_field_init(tmp, 128))) {
1588                 return ast_channel_unref(tmp);
1589         }
1590
1591         if (!(tmp->dialed_causes = ao2_container_alloc(DIALED_CAUSES_BUCKETS, pvt_cause_hash_fn, pvt_cause_cmp_fn))) {
1592                 return ast_channel_unref(tmp);
1593         }
1594
1595         /* set the creation time in the uniqueid */
1596         tmp->uniqueid.creation_time = time(NULL);
1597         tmp->uniqueid.creation_unique = ast_atomic_fetchadd_int(&uniqueint, 1);
1598
1599         /* use provided id or default to historical {system-}time.# format */
1600         if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
1601                 ast_copy_string(tmp->uniqueid.unique_id, assignedids->uniqueid, sizeof(tmp->uniqueid.unique_id));
1602         } else if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
1603                 snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%li.%d",
1604                         (long)(tmp->uniqueid.creation_time),
1605                         tmp->uniqueid.creation_unique);
1606         } else {
1607                 snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%s-%li.%d",
1608                         ast_config_AST_SYSTEM_NAME,
1609                         (long)(tmp->uniqueid.creation_time),
1610                         tmp->uniqueid.creation_unique);
1611         }
1612
1613         /* copy linked id from parent channel if known */
1614         if (requestor) {
1615                 tmp->linkedid = requestor->linkedid;
1616         } else {
1617                 tmp->linkedid = tmp->uniqueid;
1618         }
1619
1620         return tmp;
1621 }
1622
1623 struct ast_channel *ast_channel_internal_oldest_linkedid(struct ast_channel *a, struct ast_channel *b)
1624 {
1625         ast_assert(a->linkedid.creation_time != 0);
1626         ast_assert(b->linkedid.creation_time != 0);
1627
1628         if (a->linkedid.creation_time < b->linkedid.creation_time) {
1629                 return a;
1630         }
1631         if (b->linkedid.creation_time < a->linkedid.creation_time) {
1632                 return b;
1633         }
1634         if (a->linkedid.creation_unique < b->linkedid.creation_unique) {
1635                 return a;
1636         }
1637         return b;
1638 }
1639
1640 void ast_channel_internal_copy_linkedid(struct ast_channel *dest, struct ast_channel *source)
1641 {
1642         if (dest->linkedid.creation_time == source->linkedid.creation_time
1643                 && dest->linkedid.creation_unique == source->linkedid.creation_unique
1644                 && !strcmp(dest->linkedid.unique_id, source->linkedid.unique_id)) {
1645                 return;
1646         }
1647         dest->linkedid = source->linkedid;
1648         ast_channel_publish_snapshot(dest);
1649 }
1650
1651 void ast_channel_internal_swap_uniqueid_and_linkedid(struct ast_channel *a, struct ast_channel *b)
1652 {
1653         struct ast_channel_id temp;
1654
1655         temp = a->uniqueid;
1656         a->uniqueid = b->uniqueid;
1657         b->uniqueid = temp;
1658
1659         temp = a->linkedid;
1660         a->linkedid = b->linkedid;
1661         b->linkedid = temp;
1662 }
1663
1664 void ast_channel_internal_swap_topics(struct ast_channel *a, struct ast_channel *b)
1665 {
1666         struct stasis_cp_single *temp;
1667
1668         temp = a->topics;
1669         a->topics = b->topics;
1670         b->topics = temp;
1671 }
1672
1673 void ast_channel_internal_set_fake_ids(struct ast_channel *chan, const char *uniqueid, const char *linkedid)
1674 {
1675         ast_copy_string(chan->uniqueid.unique_id, uniqueid, sizeof(chan->uniqueid.unique_id));
1676         ast_copy_string(chan->linkedid.unique_id, linkedid, sizeof(chan->linkedid.unique_id));
1677 }
1678
1679 void ast_channel_internal_cleanup(struct ast_channel *chan)
1680 {
1681         if (chan->dialed_causes) {
1682                 ao2_t_ref(chan->dialed_causes, -1,
1683                         "done with dialed causes since the channel is going away");
1684                 chan->dialed_causes = NULL;
1685         }
1686
1687         ast_string_field_free_memory(chan);
1688
1689         chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
1690         chan->endpoint_cache_forward = stasis_forward_cancel(chan->endpoint_cache_forward);
1691
1692         stasis_cp_single_unsubscribe(chan->topics);
1693         chan->topics = NULL;
1694
1695         ast_channel_internal_set_stream_topology(chan, NULL);
1696 }
1697
1698 void ast_channel_internal_finalize(struct ast_channel *chan)
1699 {
1700         chan->finalized = 1;
1701 }
1702
1703 int ast_channel_internal_is_finalized(struct ast_channel *chan)
1704 {
1705         return chan->finalized;
1706 }
1707
1708 struct stasis_topic *ast_channel_topic(struct ast_channel *chan)
1709 {
1710         if (!chan) {
1711                 return ast_channel_topic_all();
1712         }
1713
1714         return stasis_cp_single_topic(chan->topics);
1715 }
1716
1717 struct stasis_topic *ast_channel_topic_cached(struct ast_channel *chan)
1718 {
1719         if (!chan) {
1720                 return ast_channel_topic_all_cached();
1721         }
1722
1723         return stasis_cp_single_topic_cached(chan->topics);
1724 }
1725
1726 int ast_channel_forward_endpoint(struct ast_channel *chan,
1727         struct ast_endpoint *endpoint)
1728 {
1729         ast_assert(chan != NULL);
1730         ast_assert(endpoint != NULL);
1731
1732         chan->endpoint_forward =
1733                 stasis_forward_all(ast_channel_topic(chan),
1734                         ast_endpoint_topic(endpoint));
1735         if (!chan->endpoint_forward) {
1736                 return -1;
1737         }
1738
1739         chan->endpoint_cache_forward = stasis_forward_all(ast_channel_topic_cached(chan),
1740                 ast_endpoint_topic(endpoint));
1741         if (!chan->endpoint_cache_forward) {
1742                 chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
1743                 return -1;
1744         }
1745
1746         return 0;
1747 }
1748
1749 int ast_channel_internal_setup_topics(struct ast_channel *chan)
1750 {
1751         const char *topic_name = chan->uniqueid.unique_id;
1752         ast_assert(chan->topics == NULL);
1753
1754         if (ast_strlen_zero(topic_name)) {
1755                 topic_name = "<dummy-channel>";
1756         }
1757
1758         chan->topics = stasis_cp_single_create(
1759                 ast_channel_cache_all(), topic_name);
1760         if (!chan->topics) {
1761                 return -1;
1762         }
1763
1764         return 0;
1765 }
1766
1767 AST_THREADSTORAGE(channel_errno);
1768
1769 void ast_channel_internal_errno_set(enum ast_channel_error error)
1770 {
1771         enum ast_channel_error *error_code = ast_threadstorage_get(&channel_errno, sizeof(*error_code));
1772         if (!error_code) {
1773                 return;
1774         }
1775
1776         *error_code = error;
1777 }
1778
1779 enum ast_channel_error ast_channel_internal_errno(void)
1780 {
1781         enum ast_channel_error *error_code = ast_threadstorage_get(&channel_errno, sizeof(*error_code));
1782         if (!error_code) {
1783                 return AST_CHANNEL_ERROR_UNKNOWN;
1784         }
1785
1786         return *error_code;
1787 }
1788
1789 struct ast_stream_topology *ast_channel_get_stream_topology(
1790         const struct ast_channel *chan)
1791 {
1792         ast_assert(chan != NULL);
1793
1794         return chan->stream_topology;
1795 }
1796
1797 struct ast_stream_topology *ast_channel_set_stream_topology(struct ast_channel *chan,
1798         struct ast_stream_topology *topology)
1799 {
1800         struct ast_stream_topology *new_topology;
1801
1802         ast_assert(chan != NULL);
1803
1804         /* A non-MULTISTREAM channel can't manipulate topology directly */
1805         ast_assert(ast_channel_is_multistream(chan));
1806
1807         /* Unless the channel is being destroyed, we always want a topology on
1808          * it even if its empty.
1809          */
1810         if (!topology) {
1811                 new_topology = ast_stream_topology_alloc();
1812         } else {
1813                 new_topology = topology;
1814         }
1815
1816         if (new_topology) {
1817                 ast_channel_internal_set_stream_topology(chan, new_topology);
1818         }
1819
1820         return new_topology;
1821 }
1822
1823 struct ast_stream *ast_channel_get_default_stream(struct ast_channel *chan,
1824         enum ast_media_type type)
1825 {
1826         ast_assert(chan != NULL);
1827         ast_assert(type < AST_MEDIA_TYPE_END);
1828
1829         return chan->default_streams[type];
1830 }
1831
1832 void ast_channel_internal_swap_stream_topology(struct ast_channel *chan1,
1833         struct ast_channel *chan2)
1834 {
1835         struct ast_stream_topology *tmp_topology;
1836
1837         ast_assert(chan1 != NULL && chan2 != NULL);
1838
1839         tmp_topology = chan1->stream_topology;
1840         chan1->stream_topology = chan2->stream_topology;
1841         chan2->stream_topology = tmp_topology;
1842
1843         channel_set_default_streams(chan1);
1844         channel_set_default_streams(chan2);
1845 }
1846
1847 int ast_channel_is_multistream(struct ast_channel *chan)
1848 {
1849         return (chan->tech && chan->tech->read_stream && chan->tech->write_stream);
1850 }