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