stream: Make ast_stream_topology_create_from_format_cap() allow NULL cap.
[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                 new_topology = ast_stream_topology_create_from_format_cap(value);
859                 ast_channel_internal_set_stream_topology(chan, new_topology);
860         }
861 }
862
863 struct ast_framehook_list *ast_channel_framehooks(const struct ast_channel *chan)
864 {
865         return chan->framehooks;
866 }
867 void ast_channel_framehooks_set(struct ast_channel *chan, struct ast_framehook_list *value)
868 {
869         chan->framehooks = value;
870 }
871 struct ast_generator *ast_channel_generator(const struct ast_channel *chan)
872 {
873         return chan->generator;
874 }
875 void ast_channel_generator_set(struct ast_channel *chan, struct ast_generator *value)
876 {
877         chan->generator = value;
878 }
879 struct ast_pbx *ast_channel_pbx(const struct ast_channel *chan)
880 {
881         return chan->pbx;
882 }
883 void ast_channel_pbx_set(struct ast_channel *chan, struct ast_pbx *value)
884 {
885         chan->pbx = value;
886 }
887 struct ast_sched_context *ast_channel_sched(const struct ast_channel *chan)
888 {
889         return chan->sched;
890 }
891 void ast_channel_sched_set(struct ast_channel *chan, struct ast_sched_context *value)
892 {
893         chan->sched = value;
894 }
895 struct ast_timer *ast_channel_timer(const struct ast_channel *chan)
896 {
897         return chan->timer;
898 }
899 void ast_channel_timer_set(struct ast_channel *chan, struct ast_timer *value)
900 {
901         chan->timer = value;
902 }
903 struct ast_tone_zone *ast_channel_zone(const struct ast_channel *chan)
904 {
905         return chan->zone;
906 }
907 void ast_channel_zone_set(struct ast_channel *chan, struct ast_tone_zone *value)
908 {
909         chan->zone = value;
910 }
911 struct ast_trans_pvt *ast_channel_readtrans(const struct ast_channel *chan)
912 {
913         return chan->readtrans;
914 }
915 void ast_channel_readtrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
916 {
917         chan->readtrans = value;
918 }
919 struct ast_trans_pvt *ast_channel_writetrans(const struct ast_channel *chan)
920 {
921         return chan->writetrans;
922 }
923 void ast_channel_writetrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
924 {
925         chan->writetrans = value;
926 }
927 const struct ast_channel_tech *ast_channel_tech(const struct ast_channel *chan)
928 {
929         return chan->tech;
930 }
931 void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
932 {
933         if (value->read_stream || value->write_stream) {
934                 ast_assert(value->read_stream && value->write_stream);
935         }
936
937         chan->tech = value;
938 }
939 enum ast_channel_adsicpe ast_channel_adsicpe(const struct ast_channel *chan)
940 {
941         return chan->adsicpe;
942 }
943 void ast_channel_adsicpe_set(struct ast_channel *chan, enum ast_channel_adsicpe value)
944 {
945         chan->adsicpe = value;
946 }
947 enum ast_channel_state ast_channel_state(const struct ast_channel *chan)
948 {
949         return chan->state;
950 }
951 ast_callid ast_channel_callid(const struct ast_channel *chan)
952 {
953         return chan->callid;
954 }
955 void ast_channel_callid_set(struct ast_channel *chan, ast_callid callid)
956 {
957         char call_identifier_from[AST_CALLID_BUFFER_LENGTH];
958         char call_identifier_to[AST_CALLID_BUFFER_LENGTH];
959         call_identifier_from[0] = '\0';
960         ast_callid_strnprint(call_identifier_to, sizeof(call_identifier_to), callid);
961         if (chan->callid) {
962                 ast_callid_strnprint(call_identifier_from, sizeof(call_identifier_from), chan->callid);
963                 ast_debug(3, "Channel Call ID changing from %s to %s\n", call_identifier_from, call_identifier_to);
964         }
965
966         chan->callid = callid;
967
968         ast_test_suite_event_notify("CallIDChange",
969                 "State: CallIDChange\r\n"
970                 "Channel: %s\r\n"
971                 "CallID: %s\r\n"
972                 "PriorCallID: %s",
973                 ast_channel_name(chan),
974                 call_identifier_to,
975                 call_identifier_from);
976 }
977
978 void ast_channel_state_set(struct ast_channel *chan, enum ast_channel_state value)
979 {
980         chan->state = value;
981 }
982 void ast_channel_set_oldwriteformat(struct ast_channel *chan, struct ast_format *format)
983 {
984         ao2_replace(chan->oldwriteformat, format);
985 }
986 void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
987 {
988         ao2_replace(chan->rawreadformat, format);
989 }
990 void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
991 {
992         ao2_replace(chan->rawwriteformat, format);
993 }
994 void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
995 {
996         ao2_replace(chan->readformat, format);
997 }
998 void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
999 {
1000         ao2_replace(chan->writeformat, format);
1001 }
1002 struct ast_format *ast_channel_oldwriteformat(struct ast_channel *chan)
1003 {
1004         return chan->oldwriteformat;
1005 }
1006 struct ast_format *ast_channel_rawreadformat(struct ast_channel *chan)
1007 {
1008         return chan->rawreadformat;
1009 }
1010 struct ast_format *ast_channel_rawwriteformat(struct ast_channel *chan)
1011 {
1012         return chan->rawwriteformat;
1013 }
1014 struct ast_format *ast_channel_readformat(struct ast_channel *chan)
1015 {
1016         return chan->readformat;
1017 }
1018 struct ast_format *ast_channel_writeformat(struct ast_channel *chan)
1019 {
1020         return chan->writeformat;
1021 }
1022 struct ast_hangup_handler_list *ast_channel_hangup_handlers(struct ast_channel *chan)
1023 {
1024         return &chan->hangup_handlers;
1025 }
1026 struct ast_datastore_list *ast_channel_datastores(struct ast_channel *chan)
1027 {
1028         return &chan->datastores;
1029 }
1030 struct ast_autochan_list *ast_channel_autochans(struct ast_channel *chan)
1031 {
1032         return &chan->autochans;
1033 }
1034 struct ast_readq_list *ast_channel_readq(struct ast_channel *chan)
1035 {
1036         return &chan->readq;
1037 }
1038 struct ast_frame *ast_channel_dtmff(struct ast_channel *chan)
1039 {
1040         return &chan->dtmff;
1041 }
1042 struct ast_jb *ast_channel_jb(struct ast_channel *chan)
1043 {
1044         return &chan->jb;
1045 }
1046 struct ast_party_caller *ast_channel_caller(struct ast_channel *chan)
1047 {
1048         return &chan->caller;
1049 }
1050 struct ast_party_connected_line *ast_channel_connected(struct ast_channel *chan)
1051 {
1052         return &chan->connected;
1053 }
1054 struct ast_party_connected_line *ast_channel_connected_indicated(struct ast_channel *chan)
1055 {
1056         return &chan->connected_indicated;
1057 }
1058 struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
1059 {
1060         return ast_party_id_merge(&chan->connected.id, &chan->connected.priv);
1061 }
1062 struct ast_party_dialed *ast_channel_dialed(struct ast_channel *chan)
1063 {
1064         return &chan->dialed;
1065 }
1066 struct ast_party_redirecting *ast_channel_redirecting(struct ast_channel *chan)
1067 {
1068         return &chan->redirecting;
1069 }
1070 struct ast_party_id ast_channel_redirecting_effective_orig(struct ast_channel *chan)
1071 {
1072         return ast_party_id_merge(&chan->redirecting.orig, &chan->redirecting.priv_orig);
1073 }
1074 struct ast_party_id ast_channel_redirecting_effective_from(struct ast_channel *chan)
1075 {
1076         return ast_party_id_merge(&chan->redirecting.from, &chan->redirecting.priv_from);
1077 }
1078 struct ast_party_id ast_channel_redirecting_effective_to(struct ast_channel *chan)
1079 {
1080         return ast_party_id_merge(&chan->redirecting.to, &chan->redirecting.priv_to);
1081 }
1082 struct timeval *ast_channel_dtmf_tv(struct ast_channel *chan)
1083 {
1084         return &chan->dtmf_tv;
1085 }
1086 struct timeval *ast_channel_whentohangup(struct ast_channel *chan)
1087 {
1088         return &chan->whentohangup;
1089 }
1090 struct varshead *ast_channel_varshead(struct ast_channel *chan)
1091 {
1092         return &chan->varshead;
1093 }
1094 void ast_channel_dtmff_set(struct ast_channel *chan, struct ast_frame *value)
1095 {
1096         chan->dtmff = *value;
1097 }
1098 void ast_channel_jb_set(struct ast_channel *chan, struct ast_jb *value)
1099 {
1100         chan->jb = *value;
1101 }
1102 void ast_channel_caller_set(struct ast_channel *chan, struct ast_party_caller *value)
1103 {
1104         chan->caller = *value;
1105 }
1106 void ast_channel_connected_set(struct ast_channel *chan, struct ast_party_connected_line *value)
1107 {
1108         chan->connected = *value;
1109 }
1110 void ast_channel_dialed_set(struct ast_channel *chan, struct ast_party_dialed *value)
1111 {
1112         chan->dialed = *value;
1113 }
1114 void ast_channel_redirecting_set(struct ast_channel *chan, struct ast_party_redirecting *value)
1115 {
1116         chan->redirecting = *value;
1117 }
1118 void ast_channel_dtmf_tv_set(struct ast_channel *chan, struct timeval *value)
1119 {
1120         chan->dtmf_tv = *value;
1121 }
1122 void ast_channel_whentohangup_set(struct ast_channel *chan, struct timeval *value)
1123 {
1124         chan->whentohangup = *value;
1125 }
1126 void ast_channel_varshead_set(struct ast_channel *chan, struct varshead *value)
1127 {
1128         chan->varshead = *value;
1129 }
1130 struct timeval ast_channel_creationtime(struct ast_channel *chan)
1131 {
1132         return chan->creationtime;
1133 }
1134 void ast_channel_creationtime_set(struct ast_channel *chan, struct timeval *value)
1135 {
1136         chan->creationtime = *value;
1137 }
1138
1139 struct timeval ast_channel_answertime(struct ast_channel *chan)
1140 {
1141         return chan->answertime;
1142 }
1143
1144 void ast_channel_answertime_set(struct ast_channel *chan, struct timeval *value)
1145 {
1146         chan->answertime = *value;
1147 }
1148
1149 /* Evil softhangup accessors */
1150 int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
1151 {
1152         return chan->softhangup;
1153 }
1154 void ast_channel_softhangup_internal_flag_set(struct ast_channel *chan, int value)
1155 {
1156         chan->softhangup = value;
1157 }
1158 void ast_channel_softhangup_internal_flag_add(struct ast_channel *chan, int value)
1159 {
1160         chan->softhangup |= value;
1161 }
1162 void ast_channel_softhangup_internal_flag_clear(struct ast_channel *chan, int value)
1163 {
1164         chan ->softhangup &= ~value;
1165 }
1166
1167 int ast_channel_unbridged_nolock(struct ast_channel *chan)
1168 {
1169         return chan->unbridged;
1170 }
1171
1172 int ast_channel_unbridged(struct ast_channel *chan)
1173 {
1174         int res;
1175         ast_channel_lock(chan);
1176         res = ast_channel_unbridged_nolock(chan);
1177         ast_channel_unlock(chan);
1178         return res;
1179 }
1180
1181 void ast_channel_set_unbridged_nolock(struct ast_channel *chan, int value)
1182 {
1183         chan->unbridged = !!value;
1184         ast_queue_frame(chan, &ast_null_frame);
1185 }
1186
1187 void ast_channel_set_unbridged(struct ast_channel *chan, int value)
1188 {
1189         ast_channel_lock(chan);
1190         ast_channel_set_unbridged_nolock(chan, value);
1191         ast_channel_unlock(chan);
1192 }
1193
1194 int ast_channel_is_t38_active_nolock(struct ast_channel *chan)
1195 {
1196         return chan->is_t38_active;
1197 }
1198
1199 int ast_channel_is_t38_active(struct ast_channel *chan)
1200 {
1201         int res;
1202
1203         ast_channel_lock(chan);
1204         res = ast_channel_is_t38_active_nolock(chan);
1205         ast_channel_unlock(chan);
1206         return res;
1207 }
1208
1209 void ast_channel_set_is_t38_active_nolock(struct ast_channel *chan, int is_t38_active)
1210 {
1211         chan->is_t38_active = !!is_t38_active;
1212 }
1213
1214 void ast_channel_set_is_t38_active(struct ast_channel *chan, int is_t38_active)
1215 {
1216         ast_channel_lock(chan);
1217         ast_channel_set_is_t38_active_nolock(chan, is_t38_active);
1218         ast_channel_unlock(chan);
1219 }
1220
1221 void ast_channel_callid_cleanup(struct ast_channel *chan)
1222 {
1223         chan->callid = 0;
1224 }
1225
1226 /* Typedef accessors */
1227 ast_group_t ast_channel_callgroup(const struct ast_channel *chan)
1228 {
1229         return chan->callgroup;
1230 }
1231 void ast_channel_callgroup_set(struct ast_channel *chan, ast_group_t value)
1232 {
1233         chan->callgroup = value;
1234 }
1235 ast_group_t ast_channel_pickupgroup(const struct ast_channel *chan)
1236 {
1237         return chan->pickupgroup;
1238 }
1239 void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
1240 {
1241         chan->pickupgroup = value;
1242 }
1243 struct ast_namedgroups *ast_channel_named_callgroups(const struct ast_channel *chan)
1244 {
1245         return chan->named_callgroups;
1246 }
1247 void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
1248 {
1249         ast_unref_namedgroups(chan->named_callgroups);
1250         chan->named_callgroups = ast_ref_namedgroups(value);
1251 }
1252 struct ast_namedgroups *ast_channel_named_pickupgroups(const struct ast_channel *chan)
1253 {
1254         return chan->named_pickupgroups;
1255 }
1256 void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
1257 {
1258         ast_unref_namedgroups(chan->named_pickupgroups);
1259         chan->named_pickupgroups = ast_ref_namedgroups(value);
1260 }
1261
1262 /* Alertpipe functions */
1263 int ast_channel_alert_write(struct ast_channel *chan)
1264 {
1265         return ast_alertpipe_write(chan->alertpipe);
1266 }
1267
1268 ast_alert_status_t ast_channel_internal_alert_flush(struct ast_channel *chan)
1269 {
1270         return ast_alertpipe_flush(chan->alertpipe);
1271 }
1272
1273 ast_alert_status_t ast_channel_internal_alert_read(struct ast_channel *chan)
1274 {
1275         return ast_alertpipe_read(chan->alertpipe);
1276 }
1277
1278 int ast_channel_alert_writable(struct ast_channel *chan)
1279 {
1280         return ast_alertpipe_writable(chan->alertpipe);
1281 }
1282
1283 int ast_channel_internal_alert_readable(struct ast_channel *chan)
1284 {
1285         return ast_alertpipe_readable(chan->alertpipe);
1286 }
1287
1288 void ast_channel_internal_alertpipe_clear(struct ast_channel *chan)
1289 {
1290         ast_alertpipe_clear(chan->alertpipe);
1291 }
1292
1293 void ast_channel_internal_alertpipe_close(struct ast_channel *chan)
1294 {
1295         ast_alertpipe_close(chan->alertpipe);
1296 }
1297
1298 int ast_channel_internal_alertpipe_init(struct ast_channel *chan)
1299 {
1300         return ast_alertpipe_init(chan->alertpipe);
1301 }
1302
1303 int ast_channel_internal_alert_readfd(struct ast_channel *chan)
1304 {
1305         return ast_alertpipe_readfd(chan->alertpipe);
1306 }
1307
1308 void ast_channel_internal_alertpipe_swap(struct ast_channel *chan1, struct ast_channel *chan2)
1309 {
1310         ast_alertpipe_swap(chan1->alertpipe, chan2->alertpipe);
1311 }
1312
1313 /* file descriptor array accessors */
1314 void ast_channel_internal_fd_set(struct ast_channel *chan, int which, int value)
1315 {
1316         int pos;
1317
1318         /* This ensures that if the vector has to grow with unused positions they will be
1319          * initialized to -1.
1320          */
1321         for (pos = AST_VECTOR_SIZE(&chan->fds); pos < which; pos++) {
1322                 AST_VECTOR_REPLACE(&chan->fds, pos, -1);
1323         }
1324
1325         AST_VECTOR_REPLACE(&chan->fds, which, value);
1326 }
1327 void ast_channel_internal_fd_clear(struct ast_channel *chan, int which)
1328 {
1329         if (which >= AST_VECTOR_SIZE(&chan->fds)) {
1330                 return;
1331         }
1332
1333         AST_VECTOR_REPLACE(&chan->fds, which, -1);
1334 }
1335 void ast_channel_internal_fd_clear_all(struct ast_channel *chan)
1336 {
1337         AST_VECTOR_RESET(&chan->fds, AST_VECTOR_ELEM_CLEANUP_NOOP);
1338 }
1339 int ast_channel_fd(const struct ast_channel *chan, int which)
1340 {
1341         return (which >= AST_VECTOR_SIZE(&chan->fds)) ? -1 : AST_VECTOR_GET(&chan->fds, which);
1342 }
1343 int ast_channel_fd_isset(const struct ast_channel *chan, int which)
1344 {
1345         return ast_channel_fd(chan, which) > -1;
1346 }
1347
1348 int ast_channel_fd_count(const struct ast_channel *chan)
1349 {
1350         return AST_VECTOR_SIZE(&chan->fds);
1351 }
1352
1353 int ast_channel_fd_add(struct ast_channel *chan, int value)
1354 {
1355         int pos = AST_EXTENDED_FDS;
1356
1357         while (ast_channel_fd_isset(chan, pos)) {
1358                 pos += 1;
1359         }
1360
1361         AST_VECTOR_REPLACE(&chan->fds, pos, value);
1362
1363         return pos;
1364 }
1365
1366 pthread_t ast_channel_blocker(const struct ast_channel *chan)
1367 {
1368         return chan->blocker;
1369 }
1370 void ast_channel_blocker_set(struct ast_channel *chan, pthread_t value)
1371 {
1372         chan->blocker = value;
1373 }
1374
1375 ast_timing_func_t ast_channel_timingfunc(const struct ast_channel *chan)
1376 {
1377         return chan->timingfunc;
1378 }
1379 void ast_channel_timingfunc_set(struct ast_channel *chan, ast_timing_func_t value)
1380 {
1381         chan->timingfunc = value;
1382 }
1383
1384 struct ast_bridge *ast_channel_internal_bridge(const struct ast_channel *chan)
1385 {
1386         return chan->bridge;
1387 }
1388 void ast_channel_internal_bridge_set(struct ast_channel *chan, struct ast_bridge *value)
1389 {
1390         chan->bridge = value;
1391         ast_channel_publish_snapshot(chan);
1392 }
1393
1394 struct ast_bridge_channel *ast_channel_internal_bridge_channel(const struct ast_channel *chan)
1395 {
1396         return chan->bridge_channel;
1397 }
1398 void ast_channel_internal_bridge_channel_set(struct ast_channel *chan, struct ast_bridge_channel *value)
1399 {
1400         chan->bridge_channel = value;
1401 }
1402
1403 struct ast_flags *ast_channel_flags(struct ast_channel *chan)
1404 {
1405         return &chan->flags;
1406 }
1407
1408 static int collect_names_cb(void *obj, void *arg, int flags) {
1409         struct ast_control_pvt_cause_code *cause_code = obj;
1410         struct ast_str **str = arg;
1411
1412         ast_str_append(str, 0, "%s%s", (ast_str_strlen(*str) ? "," : ""), cause_code->chan_name);
1413
1414         return 0;
1415 }
1416
1417 struct ast_str *ast_channel_dialed_causes_channels(const struct ast_channel *chan)
1418 {
1419         struct ast_str *chanlist = ast_str_create(128);
1420
1421         if (!chanlist) {
1422                 return NULL;
1423         }
1424
1425         ao2_callback(chan->dialed_causes, 0, collect_names_cb, &chanlist);
1426
1427         return chanlist;
1428 }
1429
1430 struct ast_control_pvt_cause_code *ast_channel_dialed_causes_find(const struct ast_channel *chan, const char *chan_name)
1431 {
1432         return ao2_find(chan->dialed_causes, chan_name, OBJ_KEY);
1433 }
1434
1435 int ast_channel_dialed_causes_add(const struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
1436 {
1437         struct ast_control_pvt_cause_code *ao2_cause_code;
1438         ao2_find(chan->dialed_causes, cause_code->chan_name, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA);
1439         ao2_cause_code = ao2_alloc(datalen, NULL);
1440
1441         if (ao2_cause_code) {
1442                 memcpy(ao2_cause_code, cause_code, datalen);
1443                 ao2_link(chan->dialed_causes, ao2_cause_code);
1444                 ao2_ref(ao2_cause_code, -1);
1445                 return 0;
1446         } else {
1447                 return -1;
1448         }
1449 }
1450
1451 void ast_channel_dialed_causes_clear(const struct ast_channel *chan)
1452 {
1453         ao2_callback(chan->dialed_causes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
1454 }
1455
1456 /* \brief Hash function for pvt cause code frames */
1457 static int pvt_cause_hash_fn(const void *vpc, const int flags)
1458 {
1459         const struct ast_control_pvt_cause_code *pc = vpc;
1460         return ast_str_hash(ast_tech_to_upper(ast_strdupa(pc->chan_name)));
1461 }
1462
1463 /* \brief Comparison function for pvt cause code frames */
1464 static int pvt_cause_cmp_fn(void *obj, void *vstr, int flags)
1465 {
1466         struct ast_control_pvt_cause_code *pc = obj;
1467         char *str = ast_tech_to_upper(ast_strdupa(vstr));
1468         char *pc_str = ast_tech_to_upper(ast_strdupa(pc->chan_name));
1469         return !strcmp(pc_str, str) ? CMP_MATCH | CMP_STOP : 0;
1470 }
1471
1472 #define DIALED_CAUSES_BUCKETS 37
1473
1474 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)
1475 {
1476         struct ast_channel *tmp;
1477
1478         tmp = __ao2_alloc(sizeof(*tmp), destructor,
1479                 AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function);
1480
1481         if (!tmp) {
1482                 return NULL;
1483         }
1484
1485         if ((ast_string_field_init(tmp, 128))) {
1486                 return ast_channel_unref(tmp);
1487         }
1488
1489         if (!(tmp->dialed_causes = ao2_container_alloc(DIALED_CAUSES_BUCKETS, pvt_cause_hash_fn, pvt_cause_cmp_fn))) {
1490                 return ast_channel_unref(tmp);
1491         }
1492
1493         /* set the creation time in the uniqueid */
1494         tmp->uniqueid.creation_time = time(NULL);
1495         tmp->uniqueid.creation_unique = ast_atomic_fetchadd_int(&uniqueint, 1);
1496
1497         /* use provided id or default to historical {system-}time.# format */
1498         if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
1499                 ast_copy_string(tmp->uniqueid.unique_id, assignedids->uniqueid, sizeof(tmp->uniqueid.unique_id));
1500         } else if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
1501                 snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%li.%d",
1502                         (long)(tmp->uniqueid.creation_time),
1503                         tmp->uniqueid.creation_unique);
1504         } else {
1505                 snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%s-%li.%d",
1506                         ast_config_AST_SYSTEM_NAME,
1507                         (long)(tmp->uniqueid.creation_time),
1508                         tmp->uniqueid.creation_unique);
1509         }
1510
1511         /* copy linked id from parent channel if known */
1512         if (requestor) {
1513                 tmp->linkedid = requestor->linkedid;
1514         } else {
1515                 tmp->linkedid = tmp->uniqueid;
1516         }
1517
1518         AST_VECTOR_INIT(&tmp->fds, AST_MAX_FDS);
1519
1520         return tmp;
1521 }
1522
1523 struct ast_channel *ast_channel_internal_oldest_linkedid(struct ast_channel *a, struct ast_channel *b)
1524 {
1525         ast_assert(a->linkedid.creation_time != 0);
1526         ast_assert(b->linkedid.creation_time != 0);
1527
1528         if (a->linkedid.creation_time < b->linkedid.creation_time) {
1529                 return a;
1530         }
1531         if (b->linkedid.creation_time < a->linkedid.creation_time) {
1532                 return b;
1533         }
1534         if (a->linkedid.creation_unique < b->linkedid.creation_unique) {
1535                 return a;
1536         }
1537         return b;
1538 }
1539
1540 void ast_channel_internal_copy_linkedid(struct ast_channel *dest, struct ast_channel *source)
1541 {
1542         if (dest->linkedid.creation_time == source->linkedid.creation_time
1543                 && dest->linkedid.creation_unique == source->linkedid.creation_unique
1544                 && !strcmp(dest->linkedid.unique_id, source->linkedid.unique_id)) {
1545                 return;
1546         }
1547         dest->linkedid = source->linkedid;
1548         ast_channel_publish_snapshot(dest);
1549 }
1550
1551 void ast_channel_internal_swap_uniqueid_and_linkedid(struct ast_channel *a, struct ast_channel *b)
1552 {
1553         struct ast_channel_id temp;
1554
1555         temp = a->uniqueid;
1556         a->uniqueid = b->uniqueid;
1557         b->uniqueid = temp;
1558
1559         temp = a->linkedid;
1560         a->linkedid = b->linkedid;
1561         b->linkedid = temp;
1562 }
1563
1564 void ast_channel_internal_swap_topics(struct ast_channel *a, struct ast_channel *b)
1565 {
1566         struct stasis_cp_single *temp;
1567
1568         temp = a->topics;
1569         a->topics = b->topics;
1570         b->topics = temp;
1571 }
1572
1573 void ast_channel_internal_set_fake_ids(struct ast_channel *chan, const char *uniqueid, const char *linkedid)
1574 {
1575         ast_copy_string(chan->uniqueid.unique_id, uniqueid, sizeof(chan->uniqueid.unique_id));
1576         ast_copy_string(chan->linkedid.unique_id, linkedid, sizeof(chan->linkedid.unique_id));
1577 }
1578
1579 void ast_channel_internal_cleanup(struct ast_channel *chan)
1580 {
1581         if (chan->dialed_causes) {
1582                 ao2_t_ref(chan->dialed_causes, -1,
1583                         "done with dialed causes since the channel is going away");
1584                 chan->dialed_causes = NULL;
1585         }
1586
1587         ast_string_field_free_memory(chan);
1588
1589         chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
1590         chan->endpoint_cache_forward = stasis_forward_cancel(chan->endpoint_cache_forward);
1591
1592         stasis_cp_single_unsubscribe(chan->topics);
1593         chan->topics = NULL;
1594
1595         ast_channel_internal_set_stream_topology(chan, NULL);
1596
1597         AST_VECTOR_FREE(&chan->fds);
1598 }
1599
1600 void ast_channel_internal_finalize(struct ast_channel *chan)
1601 {
1602         chan->finalized = 1;
1603 }
1604
1605 int ast_channel_internal_is_finalized(struct ast_channel *chan)
1606 {
1607         return chan->finalized;
1608 }
1609
1610 struct stasis_topic *ast_channel_topic(struct ast_channel *chan)
1611 {
1612         if (!chan) {
1613                 return ast_channel_topic_all();
1614         }
1615
1616         return stasis_cp_single_topic(chan->topics);
1617 }
1618
1619 struct stasis_topic *ast_channel_topic_cached(struct ast_channel *chan)
1620 {
1621         if (!chan) {
1622                 return ast_channel_topic_all_cached();
1623         }
1624
1625         return stasis_cp_single_topic_cached(chan->topics);
1626 }
1627
1628 int ast_channel_forward_endpoint(struct ast_channel *chan,
1629         struct ast_endpoint *endpoint)
1630 {
1631         ast_assert(chan != NULL);
1632         ast_assert(endpoint != NULL);
1633
1634         chan->endpoint_forward =
1635                 stasis_forward_all(ast_channel_topic(chan),
1636                         ast_endpoint_topic(endpoint));
1637         if (!chan->endpoint_forward) {
1638                 return -1;
1639         }
1640
1641         chan->endpoint_cache_forward = stasis_forward_all(ast_channel_topic_cached(chan),
1642                 ast_endpoint_topic(endpoint));
1643         if (!chan->endpoint_cache_forward) {
1644                 chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
1645                 return -1;
1646         }
1647
1648         return 0;
1649 }
1650
1651 int ast_channel_internal_setup_topics(struct ast_channel *chan)
1652 {
1653         const char *topic_name = chan->uniqueid.unique_id;
1654         ast_assert(chan->topics == NULL);
1655
1656         if (ast_strlen_zero(topic_name)) {
1657                 topic_name = "<dummy-channel>";
1658         }
1659
1660         chan->topics = stasis_cp_single_create(
1661                 ast_channel_cache_all(), topic_name);
1662         if (!chan->topics) {
1663                 return -1;
1664         }
1665
1666         return 0;
1667 }
1668
1669 AST_THREADSTORAGE(channel_errno);
1670
1671 void ast_channel_internal_errno_set(enum ast_channel_error error)
1672 {
1673         enum ast_channel_error *error_code = ast_threadstorage_get(&channel_errno, sizeof(*error_code));
1674         if (!error_code) {
1675                 return;
1676         }
1677
1678         *error_code = error;
1679 }
1680
1681 enum ast_channel_error ast_channel_internal_errno(void)
1682 {
1683         enum ast_channel_error *error_code = ast_threadstorage_get(&channel_errno, sizeof(*error_code));
1684         if (!error_code) {
1685                 return AST_CHANNEL_ERROR_UNKNOWN;
1686         }
1687
1688         return *error_code;
1689 }
1690
1691 struct ast_stream_topology *ast_channel_get_stream_topology(
1692         const struct ast_channel *chan)
1693 {
1694         ast_assert(chan != NULL);
1695
1696         return chan->stream_topology;
1697 }
1698
1699 struct ast_stream_topology *ast_channel_set_stream_topology(struct ast_channel *chan,
1700         struct ast_stream_topology *topology)
1701 {
1702         struct ast_stream_topology *new_topology;
1703
1704         ast_assert(chan != NULL);
1705
1706         /* A non-MULTISTREAM channel can't manipulate topology directly */
1707         ast_assert(ast_channel_is_multistream(chan));
1708
1709         /* Unless the channel is being destroyed, we always want a topology on
1710          * it even if its empty.
1711          */
1712         if (!topology) {
1713                 new_topology = ast_stream_topology_alloc();
1714         } else {
1715                 new_topology = topology;
1716         }
1717
1718         if (new_topology) {
1719                 ast_channel_internal_set_stream_topology(chan, new_topology);
1720         }
1721
1722         return new_topology;
1723 }
1724
1725 struct ast_stream *ast_channel_get_default_stream(struct ast_channel *chan,
1726         enum ast_media_type type)
1727 {
1728         ast_assert(chan != NULL);
1729         ast_assert(type < AST_MEDIA_TYPE_END);
1730
1731         return chan->default_streams[type];
1732 }
1733
1734 void ast_channel_internal_swap_stream_topology(struct ast_channel *chan1,
1735         struct ast_channel *chan2)
1736 {
1737         struct ast_stream_topology *tmp_topology;
1738
1739         ast_assert(chan1 != NULL && chan2 != NULL);
1740
1741         tmp_topology = chan1->stream_topology;
1742         chan1->stream_topology = chan2->stream_topology;
1743         chan2->stream_topology = tmp_topology;
1744
1745         channel_set_default_streams(chan1);
1746         channel_set_default_streams(chan2);
1747 }
1748
1749 int ast_channel_is_multistream(struct ast_channel *chan)
1750 {
1751         return (chan->tech && chan->tech->read_stream && chan->tech->write_stream);
1752 }