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