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