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