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