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