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