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