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