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