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