2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2012, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Channel Accessor API
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.
27 * \author Terry Wilson
31 <support_level>core</support_level>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
49 * \brief Main Channel structure associated with a channel.
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
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;
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 */
89 struct ast_epoll_data *epfd_data[AST_MAX_FDS];
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 */
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 */
111 * \brief Dialed/Called information.
112 * \note Set on incoming channels to indicate the originally dialed party.
113 * \note Dialed Number Identifier (DNID)
115 struct ast_party_dialed dialed;
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.
122 struct ast_party_caller caller;
125 * \brief Channel Connected Line ID information.
126 * \note The connected line information identifies the channel
127 * connected/bridged to this channel.
129 struct ast_party_connected_line connected;
131 /*! \brief Redirecting/Diversion information */
132 struct ast_party_redirecting redirecting;
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 */
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 */
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 */
180 int visible_indication; /*!< Indication currently playing on the channel */
182 unsigned short transfercapability; /*!< ISDN Transfer Capability - AST_FLAG_DIGITAL is not enough */
184 struct ast_bridge *bridge; /*!< Bridge this channel is participating in */
185 struct ast_timer *timer; /*!< timer object that provided timingfd */
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 */
194 /* AST_DATA definitions, which will probably have to be re-thought since the channel will be opaque */
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)
206 AST_DATA_STRUCTURE(ast_callerid, DATA_EXPORT_CALLERID);
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)
238 AST_DATA_STRUCTURE(ast_channel, DATA_EXPORT_CHANNEL);
240 static void channel_data_add_flags(struct ast_data *tree,
241 struct ast_channel *chan)
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));
262 int ast_channel_data_add_structure(struct ast_data *tree,
263 struct ast_channel *chan, int add_bridged)
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;
281 ast_data_add_structure(ast_channel, tree, chan);
284 bc = ast_bridged_channel(chan);
286 data_bridged = ast_data_add_node(tree, "bridged");
290 ast_channel_data_add_structure(data_bridged, bc, 0);
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));
302 enum_node = ast_data_add_node(tree, "state");
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));
310 enum_node = ast_data_add_node(tree, "hangupcause");
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));
318 enum_node = ast_data_add_node(tree, "amaflags");
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));
325 /* transfercapability */
326 enum_node = ast_data_add_node(tree, "transfercapability");
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));
334 data_softhangup = ast_data_add_node(tree, "softhangup");
335 if (!data_softhangup) {
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);
347 data_flags = ast_data_add_node(tree, "flags");
351 channel_data_add_flags(data_flags, chan);
353 ast_data_add_uint(tree, "timetohangup", ast_channel_whentohangup(chan)->tv_sec);
355 #if 0 /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
357 data_callerid = ast_data_add_node(tree, "callerid");
358 if (!data_callerid) {
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");
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);
375 if (ast_channel_zone(chan)) {
376 data_zones = ast_data_add_node(tree, "zone");
380 ast_tone_zone_data_add_structure(data_zones, ast_channel_zone(chan));
384 data_cdr = ast_data_add_node(tree, "cdr");
389 ast_cdr_data_add_structure(data_cdr, ast_channel_cdr(chan), 1);
394 int ast_channel_data_cmp_structure(const struct ast_data_search *tree,
395 struct ast_channel *chan, const char *structure_name)
397 return ast_data_search_cmp_structure(tree, ast_channel, chan, structure_name);
402 #define DEFINE_STRINGFIELD_SETTERS_FOR(field) \
403 void ast_channel_##field##_set(struct ast_channel *chan, const char *value) \
405 ast_string_field_set(chan, field, value); \
408 void ast_channel_##field##_build_va(struct ast_channel *chan, const char *fmt, va_list ap) \
410 ast_string_field_build_va(chan, field, fmt, ap); \
412 void ast_channel_##field##_build(struct ast_channel *chan, const char *fmt, ...) \
416 ast_channel_##field##_build_va(chan, fmt, ap); \
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);
432 #define DEFINE_STRINGFIELD_GETTER_FOR(field) const char *ast_channel_##field(const struct ast_channel *chan) \
434 return chan->field; \
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);
450 void ast_channel_linkedid_set(struct ast_channel *chan, const char *value)
452 ast_assert(!ast_strlen_zero(value));
453 ast_string_field_set(chan, linkedid, value);
456 const char *ast_channel_appl(const struct ast_channel *chan)
460 void ast_channel_appl_set(struct ast_channel *chan, const char *value)
464 const char *ast_channel_blockproc(const struct ast_channel *chan)
466 return chan->blockproc;
468 void ast_channel_blockproc_set(struct ast_channel *chan, const char *value)
470 chan->blockproc = value;
472 const char *ast_channel_data(const struct ast_channel *chan)
476 void ast_channel_data_set(struct ast_channel *chan, const char *value)
481 const char *ast_channel_context(const struct ast_channel *chan)
483 return chan->context;
485 void ast_channel_context_set(struct ast_channel *chan, const char *value)
487 ast_copy_string(chan->context, value, sizeof(chan->context));
489 const char *ast_channel_exten(const struct ast_channel *chan)
493 void ast_channel_exten_set(struct ast_channel *chan, const char *value)
495 ast_copy_string(chan->exten, value, sizeof(chan->exten));
497 const char *ast_channel_macrocontext(const struct ast_channel *chan)
499 return chan->macrocontext;
501 void ast_channel_macrocontext_set(struct ast_channel *chan, const char *value)
503 ast_copy_string(chan->macrocontext, value, sizeof(chan->macrocontext));
505 const char *ast_channel_macroexten(const struct ast_channel *chan)
507 return chan->macroexten;
509 void ast_channel_macroexten_set(struct ast_channel *chan, const char *value)
511 ast_copy_string(chan->macroexten, value, sizeof(chan->macroexten));
514 char ast_channel_dtmf_digit_to_emulate(const struct ast_channel *chan)
516 return chan->dtmf_digit_to_emulate;
518 void ast_channel_dtmf_digit_to_emulate_set(struct ast_channel *chan, char value)
520 chan->dtmf_digit_to_emulate = value;
522 int ast_channel_amaflags(const struct ast_channel *chan)
524 return chan->amaflags;
526 void ast_channel_amaflags_set(struct ast_channel *chan, int value)
528 chan->amaflags = value;
531 int ast_channel_epfd(const struct ast_channel *chan)
535 void ast_channel_epfd_set(struct ast_channel *chan, int value)
540 int ast_channel_fdno(const struct ast_channel *chan)
544 void ast_channel_fdno_set(struct ast_channel *chan, int value)
548 int ast_channel_hangupcause(const struct ast_channel *chan)
550 return chan->hangupcause;
552 void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
554 chan->hangupcause = value;
556 int ast_channel_macropriority(const struct ast_channel *chan)
558 return chan->macropriority;
560 void ast_channel_macropriority_set(struct ast_channel *chan, int value)
562 chan->macropriority = value;
564 int ast_channel_priority(const struct ast_channel *chan)
566 return chan->priority;
568 void ast_channel_priority_set(struct ast_channel *chan, int value)
570 chan->priority = value;
572 int ast_channel_rings(const struct ast_channel *chan)
576 void ast_channel_rings_set(struct ast_channel *chan, int value)
580 int ast_channel_streamid(const struct ast_channel *chan)
582 return chan->streamid;
584 void ast_channel_streamid_set(struct ast_channel *chan, int value)
586 chan->streamid = value;
588 int ast_channel_timingfd(const struct ast_channel *chan)
590 return chan->timingfd;
592 void ast_channel_timingfd_set(struct ast_channel *chan, int value)
594 chan->timingfd = value;
596 int ast_channel_visible_indication(const struct ast_channel *chan)
598 return chan->visible_indication;
600 void ast_channel_visible_indication_set(struct ast_channel *chan, int value)
602 chan->visible_indication = value;
604 int ast_channel_vstreamid(const struct ast_channel *chan)
606 return chan->vstreamid;
608 void ast_channel_vstreamid_set(struct ast_channel *chan, int value)
610 chan->vstreamid = value;
612 unsigned short ast_channel_transfercapability(const struct ast_channel *chan)
614 return chan->transfercapability;
616 void ast_channel_transfercapability_set(struct ast_channel *chan, unsigned short value)
618 chan->transfercapability = value;
620 unsigned int ast_channel_emulate_dtmf_duration(const struct ast_channel *chan)
622 return chan->emulate_dtmf_duration;
624 void ast_channel_emulate_dtmf_duration_set(struct ast_channel *chan, unsigned int value)
626 chan->emulate_dtmf_duration = value;
628 unsigned int ast_channel_fin(const struct ast_channel *chan)
632 void ast_channel_fin_set(struct ast_channel *chan, unsigned int value)
636 unsigned int ast_channel_fout(const struct ast_channel *chan)
640 void ast_channel_fout_set(struct ast_channel *chan, unsigned int value)
644 unsigned long ast_channel_insmpl(const struct ast_channel *chan)
648 void ast_channel_insmpl_set(struct ast_channel *chan, unsigned long value)
650 chan->insmpl = value;
652 unsigned long ast_channel_outsmpl(const struct ast_channel *chan)
654 return chan->outsmpl;
656 void ast_channel_outsmpl_set(struct ast_channel *chan, unsigned long value)
658 chan->outsmpl = value;
660 void *ast_channel_generatordata(const struct ast_channel *chan)
662 return chan->generatordata;
664 void ast_channel_generatordata_set(struct ast_channel *chan, void *value)
666 chan->generatordata = value;
668 void *ast_channel_music_state(const struct ast_channel *chan)
670 return chan->music_state;
672 void ast_channel_music_state_set(struct ast_channel *chan, void *value)
674 chan->music_state = value;
676 void *ast_channel_tech_pvt(const struct ast_channel *chan)
678 return chan->tech_pvt;
680 void ast_channel_tech_pvt_set(struct ast_channel *chan, void *value)
682 chan->tech_pvt = value;
684 void *ast_channel_timingdata(const struct ast_channel *chan)
686 return chan->timingdata;
688 void ast_channel_timingdata_set(struct ast_channel *chan, void *value)
690 chan->timingdata = value;
692 struct ast_audiohook_list *ast_channel_audiohooks(const struct ast_channel *chan)
694 return chan->audiohooks;
696 void ast_channel_audiohooks_set(struct ast_channel *chan, struct ast_audiohook_list *value)
698 chan->audiohooks = value;
700 struct ast_cdr *ast_channel_cdr(const struct ast_channel *chan)
704 void ast_channel_cdr_set(struct ast_channel *chan, struct ast_cdr *value)
708 struct ast_channel *ast_channel_masq(const struct ast_channel *chan)
712 void ast_channel_masq_set(struct ast_channel *chan, struct ast_channel *value)
716 struct ast_channel *ast_channel_masqr(const struct ast_channel *chan)
720 void ast_channel_masqr_set(struct ast_channel *chan, struct ast_channel *value)
724 struct ast_channel_monitor *ast_channel_monitor(const struct ast_channel *chan)
726 return chan->monitor;
728 void ast_channel_monitor_set(struct ast_channel *chan, struct ast_channel_monitor *value)
730 chan->monitor = value;
732 struct ast_filestream *ast_channel_stream(const struct ast_channel *chan)
736 void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
738 chan->stream = value;
740 struct ast_filestream *ast_channel_vstream(const struct ast_channel *chan)
742 return chan->vstream;
744 void ast_channel_vstream_set(struct ast_channel *chan, struct ast_filestream *value)
746 chan->vstream = value;
748 struct ast_format_cap *ast_channel_nativeformats(const struct ast_channel *chan)
750 return chan->nativeformats;
752 void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
754 chan->nativeformats = value;
756 struct ast_framehook_list *ast_channel_framehooks(const struct ast_channel *chan)
758 return chan->framehooks;
760 void ast_channel_framehooks_set(struct ast_channel *chan, struct ast_framehook_list *value)
762 chan->framehooks = value;
764 struct ast_generator *ast_channel_generator(const struct ast_channel *chan)
766 return chan->generator;
768 void ast_channel_generator_set(struct ast_channel *chan, struct ast_generator *value)
770 chan->generator = value;
772 struct ast_pbx *ast_channel_pbx(const struct ast_channel *chan)
776 void ast_channel_pbx_set(struct ast_channel *chan, struct ast_pbx *value)
780 struct ast_sched_context *ast_channel_sched(const struct ast_channel *chan)
784 void ast_channel_sched_set(struct ast_channel *chan, struct ast_sched_context *value)
788 struct ast_timer *ast_channel_timer(const struct ast_channel *chan)
792 void ast_channel_timer_set(struct ast_channel *chan, struct ast_timer *value)
796 struct ast_tone_zone *ast_channel_zone(const struct ast_channel *chan)
800 void ast_channel_zone_set(struct ast_channel *chan, struct ast_tone_zone *value)
804 struct ast_trans_pvt *ast_channel_readtrans(const struct ast_channel *chan)
806 return chan->readtrans;
808 void ast_channel_readtrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
810 chan->readtrans = value;
812 struct ast_trans_pvt *ast_channel_writetrans(const struct ast_channel *chan)
814 return chan->writetrans;
816 void ast_channel_writetrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
818 chan->writetrans = value;
820 const struct ast_channel_tech *ast_channel_tech(const struct ast_channel *chan)
824 void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
828 enum ast_channel_adsicpe ast_channel_adsicpe(const struct ast_channel *chan)
830 return chan->adsicpe;
832 void ast_channel_adsicpe_set(struct ast_channel *chan, enum ast_channel_adsicpe value)
834 chan->adsicpe = value;
836 enum ast_channel_state ast_channel_state(const struct ast_channel *chan)
840 struct ast_callid *ast_channel_callid(const struct ast_channel *chan)
843 ast_callid_ref(chan->callid);
848 void ast_channel_callid_set(struct ast_channel *chan, struct ast_callid *callid)
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);
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);
861 chan->callid = ast_callid_ref(callid);
863 ast_test_suite_event_notify("CallIDChange",
864 "State: CallIDChange\r\n"
867 "PriorCallID: %s\r\n",
868 ast_channel_name(chan),
870 call_identifier_from);
873 void ast_channel_state_set(struct ast_channel *chan, enum ast_channel_state value)
877 struct ast_format *ast_channel_oldwriteformat(struct ast_channel *chan)
879 return &chan->oldwriteformat;
881 struct ast_format *ast_channel_rawreadformat(struct ast_channel *chan)
883 return &chan->rawreadformat;
885 struct ast_format *ast_channel_rawwriteformat(struct ast_channel *chan)
887 return &chan->rawwriteformat;
889 struct ast_format *ast_channel_readformat(struct ast_channel *chan)
891 return &chan->readformat;
893 struct ast_format *ast_channel_writeformat(struct ast_channel *chan)
895 return &chan->writeformat;
897 struct ast_hangup_handler_list *ast_channel_hangup_handlers(struct ast_channel *chan)
899 return &chan->hangup_handlers;
901 struct ast_datastore_list *ast_channel_datastores(struct ast_channel *chan)
903 return &chan->datastores;
905 struct ast_autochan_list *ast_channel_autochans(struct ast_channel *chan)
907 return &chan->autochans;
909 struct ast_readq_list *ast_channel_readq(struct ast_channel *chan)
913 struct ast_frame *ast_channel_dtmff(struct ast_channel *chan)
917 struct ast_jb *ast_channel_jb(struct ast_channel *chan)
921 struct ast_party_caller *ast_channel_caller(struct ast_channel *chan)
923 return &chan->caller;
925 struct ast_party_connected_line *ast_channel_connected(struct ast_channel *chan)
927 return &chan->connected;
929 struct ast_party_dialed *ast_channel_dialed(struct ast_channel *chan)
931 return &chan->dialed;
933 struct ast_party_redirecting *ast_channel_redirecting(struct ast_channel *chan)
935 return &chan->redirecting;
937 struct timeval *ast_channel_dtmf_tv(struct ast_channel *chan)
939 return &chan->dtmf_tv;
941 struct timeval *ast_channel_whentohangup(struct ast_channel *chan)
943 return &chan->whentohangup;
945 struct varshead *ast_channel_varshead(struct ast_channel *chan)
947 return &chan->varshead;
949 void ast_channel_dtmff_set(struct ast_channel *chan, struct ast_frame *value)
951 chan->dtmff = *value;
953 void ast_channel_jb_set(struct ast_channel *chan, struct ast_jb *value)
957 void ast_channel_caller_set(struct ast_channel *chan, struct ast_party_caller *value)
959 chan->caller = *value;
961 void ast_channel_connected_set(struct ast_channel *chan, struct ast_party_connected_line *value)
963 chan->connected = *value;
965 void ast_channel_dialed_set(struct ast_channel *chan, struct ast_party_dialed *value)
967 chan->dialed = *value;
969 void ast_channel_redirecting_set(struct ast_channel *chan, struct ast_party_redirecting *value)
971 chan->redirecting = *value;
973 void ast_channel_dtmf_tv_set(struct ast_channel *chan, struct timeval *value)
975 chan->dtmf_tv = *value;
977 void ast_channel_whentohangup_set(struct ast_channel *chan, struct timeval *value)
979 chan->whentohangup = *value;
981 void ast_channel_varshead_set(struct ast_channel *chan, struct varshead *value)
983 chan->varshead = *value;
986 /* Evil softhangup accessors */
987 int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
989 return chan->softhangup;
991 void ast_channel_softhangup_internal_flag_set(struct ast_channel *chan, int value)
993 chan->softhangup = value;
995 void ast_channel_softhangup_internal_flag_add(struct ast_channel *chan, int value)
997 chan->softhangup |= value;
999 void ast_channel_softhangup_internal_flag_clear(struct ast_channel *chan, int value)
1001 chan ->softhangup &= ~value;
1004 void ast_channel_callid_cleanup(struct ast_channel *chan)
1007 chan->callid = ast_callid_unref(chan->callid);
1011 /* Typedef accessors */
1012 ast_group_t ast_channel_callgroup(const struct ast_channel *chan)
1014 return chan->callgroup;
1016 void ast_channel_callgroup_set(struct ast_channel *chan, ast_group_t value)
1018 chan->callgroup = value;
1020 ast_group_t ast_channel_pickupgroup(const struct ast_channel *chan)
1022 return chan->pickupgroup;
1024 void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
1026 chan->pickupgroup = value;
1029 /* Alertpipe functions */
1030 int ast_channel_alert_write(struct ast_channel *chan)
1033 return ast_channel_alert_writable(chan) && write(chan->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah);
1036 ast_alert_status_t ast_channel_internal_alert_read(struct ast_channel *chan)
1041 if (!ast_channel_internal_alert_readable(chan)) {
1042 return AST_ALERT_NOT_READABLE;
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;
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;
1062 return AST_ALERT_READ_SUCCESS;
1065 int ast_channel_alert_writable(struct ast_channel *chan)
1067 return chan->alertpipe[1] > -1;
1070 int ast_channel_internal_alert_readable(struct ast_channel *chan)
1072 return chan->alertpipe[0] > -1;
1075 void ast_channel_internal_alertpipe_clear(struct ast_channel *chan)
1077 chan->alertpipe[0] = chan->alertpipe[1] = -1;
1080 void ast_channel_internal_alertpipe_close(struct ast_channel *chan)
1082 if (ast_channel_internal_alert_readable(chan)) {
1083 close(chan->alertpipe[0]);
1085 if (ast_channel_alert_writable(chan)) {
1086 close(chan->alertpipe[1]);
1090 int ast_channel_internal_alertpipe_init(struct ast_channel *chan)
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");
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));
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));
1110 int ast_channel_internal_alert_readfd(struct ast_channel *chan)
1112 return chan->alertpipe[0];
1115 void ast_channel_internal_alertpipe_swap(struct ast_channel *chan1, struct ast_channel *chan2)
1118 for (i = 0; i < ARRAY_LEN(chan1->alertpipe); i++) {
1119 SWAP(chan1->alertpipe[i], chan2->alertpipe[i]);
1123 /* file descriptor array accessors */
1124 void ast_channel_internal_fd_set(struct ast_channel *chan, int which, int value)
1126 chan->fds[which] = value;
1128 void ast_channel_internal_fd_clear(struct ast_channel *chan, int which)
1130 ast_channel_internal_fd_set(chan, which, -1);
1132 void ast_channel_internal_fd_clear_all(struct ast_channel *chan)
1135 for (i = 0; i < AST_MAX_FDS; i++) {
1136 ast_channel_internal_fd_clear(chan, i);
1139 int ast_channel_fd(const struct ast_channel *chan, int which)
1141 return chan->fds[which];
1143 int ast_channel_fd_isset(const struct ast_channel *chan, int which)
1145 return ast_channel_fd(chan, which) > -1;
1149 struct ast_epoll_data *ast_channel_internal_epfd_data(const struct ast_channel *chan, int which)
1151 return chan->epfd_data[which];
1153 void ast_channel_internal_epfd_data_set(struct ast_channel *chan, int which , struct ast_epoll_data *value)
1155 chan->epfd_data[which] = value;
1159 pthread_t ast_channel_blocker(const struct ast_channel *chan)
1161 return chan->blocker;
1163 void ast_channel_blocker_set(struct ast_channel *chan, pthread_t value)
1165 chan->blocker = value;
1168 ast_timing_func_t ast_channel_timingfunc(const struct ast_channel *chan)
1170 return chan->timingfunc;
1172 void ast_channel_timingfunc_set(struct ast_channel *chan, ast_timing_func_t value)
1174 chan->timingfunc = value;
1177 struct ast_bridge *ast_channel_internal_bridge(const struct ast_channel *chan)
1179 return chan->bridge;
1181 void ast_channel_internal_bridge_set(struct ast_channel *chan, struct ast_bridge *value)
1183 chan->bridge = value;
1186 struct ast_channel *ast_channel_internal_bridged_channel(const struct ast_channel *chan)
1188 return chan->bridged_channel;
1190 void ast_channel_internal_bridged_channel_set(struct ast_channel *chan, struct ast_channel *value)
1192 chan->bridged_channel = value;
1195 struct ast_flags *ast_channel_flags(struct ast_channel *chan)
1197 return &chan->flags;
1200 struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj), const char *file, int line, const char *function)
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);
1210 tmp = ao2_alloc(sizeof(*tmp), destructor);
1213 if ((ast_string_field_init(tmp, 128))) {
1214 return ast_channel_unref(tmp);
1220 void ast_channel_internal_cleanup(struct ast_channel *chan)
1222 ast_string_field_free_memory(chan);
1225 void ast_channel_internal_finalize(struct ast_channel *chan)
1227 chan->finalized = 1;
1230 int ast_channel_internal_is_finalized(struct ast_channel *chan)
1232 return chan->finalized;