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/paths.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/channel_internal.h"
44 #include "asterisk/data.h"
45 #include "asterisk/endpoints.h"
46 #include "asterisk/indications.h"
47 #include "asterisk/stasis_cache_pattern.h"
48 #include "asterisk/stasis_channels.h"
49 #include "asterisk/stasis_endpoints.h"
50 #include "asterisk/stringfields.h"
51 #include "asterisk/test.h"
54 * \brief Channel UniqueId structure
55 * \note channel creation time used for determining LinkedId Propagation
57 struct ast_channel_id {
58 time_t creation_time; /*!< Creation time */
59 int creation_unique; /*!< sub-second unique value */
60 char unique_id[AST_MAX_UNIQUEID]; /*!< Unique Identifier */
64 * \brief Main Channel structure associated with a channel.
66 * \note When adding fields to this structure, it is important to add the field
67 * 'in position' with like-aligned fields, so as to keep the compiler from
68 * having to add padding to align fields. The structure's fields are sorted
69 * in this order: pointers, structures, long, int/enum, short, char. This
70 * is especially important on 64-bit architectures, where mixing 4-byte
71 * and 8-byte fields causes 4 bytes of padding to be added before many
75 const struct ast_channel_tech *tech; /*!< Technology (point to channel driver) */
76 void *tech_pvt; /*!< Private data used by the technology driver */
77 void *music_state; /*!< Music State*/
78 void *generatordata; /*!< Current generator data if there is any */
79 struct ast_generator *generator; /*!< Current active data generator */
80 struct ast_channel *masq; /*!< Channel that will masquerade as us */
81 struct ast_channel *masqr; /*!< Who we are masquerading as */
82 const char *blockproc; /*!< Procedure causing blocking */
83 const char *appl; /*!< Current application */
84 const char *data; /*!< Data passed to current application */
85 struct ast_sched_context *sched; /*!< Schedule context */
86 struct ast_filestream *stream; /*!< Stream itself. */
87 struct ast_filestream *vstream; /*!< Video Stream itself. */
88 ast_timing_func_t timingfunc;
90 struct ast_pbx *pbx; /*!< PBX private structure for this channel */
91 struct ast_trans_pvt *writetrans; /*!< Write translation path */
92 struct ast_trans_pvt *readtrans; /*!< Read translation path */
93 struct ast_audiohook_list *audiohooks;
94 struct ast_framehook_list *framehooks;
95 struct ast_cdr *cdr; /*!< Call Detail Record */
96 struct ast_tone_zone *zone; /*!< Tone zone as set in indications.conf or
97 * in the CHANNEL dialplan function */
98 struct ast_channel_monitor *monitor; /*!< Channel monitoring */
99 struct ast_callid *callid; /*!< Bound call identifier pointer */
101 struct ast_epoll_data *epfd_data[AST_MAX_FDS];
103 struct ao2_container *dialed_causes; /*!< Contains tech-specific and Asterisk cause data from dialed channels */
105 AST_DECLARE_STRING_FIELDS(
106 AST_STRING_FIELD(name); /*!< ASCII unique channel name */
107 AST_STRING_FIELD(language); /*!< Language requested for voice prompts */
108 AST_STRING_FIELD(musicclass); /*!< Default music class */
109 AST_STRING_FIELD(latest_musicclass); /*!< Latest active music class */
110 AST_STRING_FIELD(accountcode); /*!< Account code for billing */
111 AST_STRING_FIELD(peeraccount); /*!< Peer account code for billing */
112 AST_STRING_FIELD(userfield); /*!< Userfield for CEL billing */
113 AST_STRING_FIELD(call_forward); /*!< Where to forward to if asked to dial on this interface */
114 AST_STRING_FIELD(parkinglot); /*! Default parking lot, if empty, default parking lot */
115 AST_STRING_FIELD(hangupsource); /*! Who is responsible for hanging up this channel */
116 AST_STRING_FIELD(dialcontext); /*!< Dial: Extension context that we were called from */
119 struct ast_channel_id uniqueid; /*!< Unique Channel Identifier - can be specified on creation */
120 struct ast_channel_id linkedid; /*!< Linked Channel Identifier - oldest propagated when bridged */
122 struct timeval whentohangup; /*!< Non-zero, set to actual time when channel is to be hung up */
123 pthread_t blocker; /*!< If anyone is blocking, this is them */
126 * \brief Dialed/Called information.
127 * \note Set on incoming channels to indicate the originally dialed party.
128 * \note Dialed Number Identifier (DNID)
130 struct ast_party_dialed dialed;
133 * \brief Channel Caller ID information.
134 * \note The caller id information is the caller id of this
135 * channel when it is used to initiate a call.
137 struct ast_party_caller caller;
140 * \brief Channel Connected Line ID information.
141 * \note The connected line information identifies the channel
142 * connected/bridged to this channel.
144 struct ast_party_connected_line connected;
147 * \brief Channel Connected Line ID information that was last indicated.
149 struct ast_party_connected_line connected_indicated;
151 /*! \brief Redirecting/Diversion information */
152 struct ast_party_redirecting redirecting;
154 struct ast_frame dtmff; /*!< DTMF frame */
155 struct varshead varshead; /*!< A linked list for channel variables. See \ref AstChanVar */
156 ast_group_t callgroup; /*!< Call group for call pickups */
157 ast_group_t pickupgroup; /*!< Pickup group - which calls groups can be picked up? */
158 struct ast_namedgroups *named_callgroups; /*!< Named call group for call pickups */
159 struct ast_namedgroups *named_pickupgroups; /*!< Named pickup group - which call groups can be picked up? */
160 struct timeval creationtime; /*!< The time of channel creation */
161 struct timeval answertime; /*!< The time the channel was answered */
162 struct ast_readq_list readq;
163 struct ast_jb jb; /*!< The jitterbuffer state */
164 struct timeval dtmf_tv; /*!< The time that an in process digit began, or the last digit ended */
165 struct ast_hangup_handler_list hangup_handlers;/*!< Hangup handlers on the channel. */
166 struct ast_datastore_list datastores; /*!< Data stores on the channel */
167 struct ast_autochan_list autochans; /*!< Autochans on the channel */
168 unsigned long insmpl; /*!< Track the read/written samples for monitor use */
169 unsigned long outsmpl; /*!< Track the read/written samples for monitor use */
171 int fds[AST_MAX_FDS]; /*!< File descriptors for channel -- Drivers will poll on
172 * these file descriptors, so at least one must be non -1.
173 * See \arg \ref AstFileDesc */
174 int softhangup; /*!< Whether or not we have been hung up... Do not set this value
175 * directly, use ast_softhangup() */
176 int unbridged; /*!< If non-zero, the bridge core needs to re-evaluate the current
177 bridging technology which is in use by this channel's bridge. */
178 int fdno; /*!< Which fd had an event detected on */
179 int streamid; /*!< For streaming playback, the schedule ID */
180 int vstreamid; /*!< For streaming video playback, the schedule ID */
181 struct ast_format *oldwriteformat; /*!< Original writer format */
182 int timingfd; /*!< Timing fd */
183 enum ast_channel_state state; /*!< State of line -- Don't write directly, use ast_setstate() */
184 int rings; /*!< Number of rings so far */
185 int priority; /*!< Dialplan: Current extension priority */
186 int macropriority; /*!< Macro: Current non-macro priority. See app_macro.c */
187 int amaflags; /*!< Set BEFORE PBX is started to determine AMA flags */
188 enum ast_channel_adsicpe adsicpe; /*!< Whether or not ADSI is detected on CPE */
189 unsigned int fin; /*!< Frames in counters. The high bit is a debug mask, so
190 * the counter is only in the remaining bits */
191 unsigned int fout; /*!< Frames out counters. The high bit is a debug mask, so
192 * the counter is only in the remaining bits */
193 int hangupcause; /*!< Why is the channel hanged up. See causes.h */
194 unsigned int finalized:1; /*!< Whether or not the channel has been successfully allocated */
195 struct ast_flags flags; /*!< channel flags of AST_FLAG_ type */
197 struct ast_format_cap *nativeformats; /*!< Kinds of data this channel can natively handle */
198 struct ast_format *readformat; /*!< Requested read format (after translation) */
199 struct ast_format *writeformat; /*!< Requested write format (after translation) */
200 struct ast_format *rawreadformat; /*!< Raw read format (before translation) */
201 struct ast_format *rawwriteformat; /*!< Raw write format (before translation) */
202 unsigned int emulate_dtmf_duration; /*!< Number of ms left to emulate DTMF for */
206 int visible_indication; /*!< Indication currently playing on the channel */
207 int hold_state; /*!< Current Hold/Unhold state */
209 unsigned short transfercapability; /*!< ISDN Transfer Capability - AST_FLAG_DIGITAL is not enough */
211 struct ast_bridge *bridge; /*!< Bridge this channel is participating in */
212 struct ast_bridge_channel *bridge_channel;/*!< The bridge_channel this channel is linked with. */
213 struct ast_timer *timer; /*!< timer object that provided timingfd */
215 char context[AST_MAX_CONTEXT]; /*!< Dialplan: Current extension context */
216 char exten[AST_MAX_EXTENSION]; /*!< Dialplan: Current extension number */
217 char macrocontext[AST_MAX_CONTEXT]; /*!< Macro: Current non-macro context. See app_macro.c */
218 char macroexten[AST_MAX_EXTENSION]; /*!< Macro: Current non-macro extension. See app_macro.c */
219 char dtmf_digit_to_emulate; /*!< Digit being emulated */
220 char sending_dtmf_digit; /*!< Digit this channel is currently sending out. (zero if not sending) */
221 struct timeval sending_dtmf_tv; /*!< The time this channel started sending the current digit. (Invalid if sending_dtmf_digit is zero.) */
222 struct stasis_cp_single *topics; /*!< Topic for all channel's events */
223 struct stasis_forward *endpoint_forward; /*!< Subscription for event forwarding to endpoint's topic */
224 struct stasis_forward *endpoint_cache_forward; /*!< Subscription for cache updates to endpoint's topic */
227 /*! \brief The monotonically increasing integer counter for channel uniqueids */
228 static int uniqueint;
230 /* AST_DATA definitions, which will probably have to be re-thought since the channel will be opaque */
232 #if 0 /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
233 #define DATA_EXPORT_CALLERID(MEMBER) \
234 MEMBER(ast_callerid, cid_dnid, AST_DATA_STRING) \
235 MEMBER(ast_callerid, cid_num, AST_DATA_STRING) \
236 MEMBER(ast_callerid, cid_name, AST_DATA_STRING) \
237 MEMBER(ast_callerid, cid_ani, AST_DATA_STRING) \
238 MEMBER(ast_callerid, cid_pres, AST_DATA_INTEGER) \
239 MEMBER(ast_callerid, cid_ani2, AST_DATA_INTEGER) \
240 MEMBER(ast_callerid, cid_tag, AST_DATA_STRING)
242 AST_DATA_STRUCTURE(ast_callerid, DATA_EXPORT_CALLERID);
245 #define DATA_EXPORT_CHANNEL(MEMBER) \
246 MEMBER(ast_channel, blockproc, AST_DATA_STRING) \
247 MEMBER(ast_channel, appl, AST_DATA_STRING) \
248 MEMBER(ast_channel, data, AST_DATA_STRING) \
249 MEMBER(ast_channel, name, AST_DATA_STRING) \
250 MEMBER(ast_channel, language, AST_DATA_STRING) \
251 MEMBER(ast_channel, musicclass, AST_DATA_STRING) \
252 MEMBER(ast_channel, accountcode, AST_DATA_STRING) \
253 MEMBER(ast_channel, peeraccount, AST_DATA_STRING) \
254 MEMBER(ast_channel, userfield, AST_DATA_STRING) \
255 MEMBER(ast_channel, call_forward, AST_DATA_STRING) \
256 MEMBER(ast_channel, parkinglot, AST_DATA_STRING) \
257 MEMBER(ast_channel, hangupsource, AST_DATA_STRING) \
258 MEMBER(ast_channel, dialcontext, AST_DATA_STRING) \
259 MEMBER(ast_channel, rings, AST_DATA_INTEGER) \
260 MEMBER(ast_channel, priority, AST_DATA_INTEGER) \
261 MEMBER(ast_channel, macropriority, AST_DATA_INTEGER) \
262 MEMBER(ast_channel, adsicpe, AST_DATA_INTEGER) \
263 MEMBER(ast_channel, fin, AST_DATA_UNSIGNED_INTEGER) \
264 MEMBER(ast_channel, fout, AST_DATA_UNSIGNED_INTEGER) \
265 MEMBER(ast_channel, emulate_dtmf_duration, AST_DATA_UNSIGNED_INTEGER) \
266 MEMBER(ast_channel, visible_indication, AST_DATA_INTEGER) \
267 MEMBER(ast_channel, context, AST_DATA_STRING) \
268 MEMBER(ast_channel, exten, AST_DATA_STRING) \
269 MEMBER(ast_channel, macrocontext, AST_DATA_STRING) \
270 MEMBER(ast_channel, macroexten, AST_DATA_STRING)
272 AST_DATA_STRUCTURE(ast_channel, DATA_EXPORT_CHANNEL);
274 static void channel_data_add_flags(struct ast_data *tree,
275 struct ast_channel *chan)
277 ast_data_add_bool(tree, "DEFER_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF));
278 ast_data_add_bool(tree, "WRITE_INT", ast_test_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT));
279 ast_data_add_bool(tree, "BLOCKING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING));
280 ast_data_add_bool(tree, "ZOMBIE", ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE));
281 ast_data_add_bool(tree, "EXCEPTION", ast_test_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION));
282 ast_data_add_bool(tree, "MOH", ast_test_flag(ast_channel_flags(chan), AST_FLAG_MOH));
283 ast_data_add_bool(tree, "SPYING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_SPYING));
284 ast_data_add_bool(tree, "IN_AUTOLOOP", ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP));
285 ast_data_add_bool(tree, "OUTGOING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING));
286 ast_data_add_bool(tree, "IN_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF));
287 ast_data_add_bool(tree, "EMULATE_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF));
288 ast_data_add_bool(tree, "END_DTMF_ONLY", ast_test_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY));
289 ast_data_add_bool(tree, "MASQ_NOSTREAM", ast_test_flag(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM));
290 ast_data_add_bool(tree, "BRIDGE_HANGUP_RUN", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_RUN));
291 ast_data_add_bool(tree, "DISABLE_WORKAROUNDS", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_WORKAROUNDS));
292 ast_data_add_bool(tree, "DISABLE_DEVSTATE_CACHE", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE));
293 ast_data_add_bool(tree, "BRIDGE_DUAL_REDIRECT_WAIT", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT));
294 ast_data_add_bool(tree, "ORIGINATED", ast_test_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED));
295 ast_data_add_bool(tree, "DEAD", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEAD));
298 int ast_channel_data_add_structure(struct ast_data *tree,
299 struct ast_channel *chan, int add_bridged)
301 struct ast_data *data_bridged;
302 struct ast_data *data_cdr;
303 struct ast_data *data_flags;
304 struct ast_data *data_zones;
305 struct ast_data *enum_node;
306 struct ast_data *data_softhangup;
307 #if 0 /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
308 struct ast_data *data_callerid;
316 ast_data_add_structure(ast_channel, tree, chan);
319 RAII_VAR(struct ast_channel *, bc, ast_channel_bridge_peer(chan), ast_channel_cleanup);
321 data_bridged = ast_data_add_node(tree, "bridged");
325 ast_channel_data_add_structure(data_bridged, bc, 0);
329 ast_data_add_str(tree, "uniqueid", ast_channel_uniqueid(chan));
330 ast_data_add_str(tree, "linkedid", ast_channel_linkedid(chan));
332 ast_data_add_codec(tree, "oldwriteformat", ast_channel_oldwriteformat(chan));
333 ast_data_add_codec(tree, "readformat", ast_channel_readformat(chan));
334 ast_data_add_codec(tree, "writeformat", ast_channel_writeformat(chan));
335 ast_data_add_codec(tree, "rawreadformat", ast_channel_rawreadformat(chan));
336 ast_data_add_codec(tree, "rawwriteformat", ast_channel_rawwriteformat(chan));
337 ast_data_add_codecs(tree, "nativeformats", ast_channel_nativeformats(chan));
340 enum_node = ast_data_add_node(tree, "state");
344 ast_data_add_str(enum_node, "text", ast_state2str(ast_channel_state(chan)));
345 ast_data_add_int(enum_node, "value", ast_channel_state(chan));
348 enum_node = ast_data_add_node(tree, "hangupcause");
352 ast_data_add_str(enum_node, "text", ast_cause2str(ast_channel_hangupcause(chan)));
353 ast_data_add_int(enum_node, "value", ast_channel_hangupcause(chan));
356 enum_node = ast_data_add_node(tree, "amaflags");
360 ast_data_add_str(enum_node, "text", ast_channel_amaflags2string(ast_channel_amaflags(chan)));
361 ast_data_add_int(enum_node, "value", ast_channel_amaflags(chan));
363 /* transfercapability */
364 enum_node = ast_data_add_node(tree, "transfercapability");
368 ast_data_add_str(enum_node, "text", ast_transfercapability2str(ast_channel_transfercapability(chan)));
369 ast_data_add_int(enum_node, "value", ast_channel_transfercapability(chan));
372 data_softhangup = ast_data_add_node(tree, "softhangup");
373 if (!data_softhangup) {
376 ast_data_add_bool(data_softhangup, "dev", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_DEV);
377 ast_data_add_bool(data_softhangup, "asyncgoto", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO);
378 ast_data_add_bool(data_softhangup, "shutdown", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_SHUTDOWN);
379 ast_data_add_bool(data_softhangup, "timeout", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_TIMEOUT);
380 ast_data_add_bool(data_softhangup, "appunload", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_APPUNLOAD);
381 ast_data_add_bool(data_softhangup, "explicit", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_EXPLICIT);
384 data_flags = ast_data_add_node(tree, "flags");
388 channel_data_add_flags(data_flags, chan);
390 ast_data_add_uint(tree, "timetohangup", ast_channel_whentohangup(chan)->tv_sec);
392 #if 0 /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
394 data_callerid = ast_data_add_node(tree, "callerid");
395 if (!data_callerid) {
398 ast_data_add_structure(ast_callerid, data_callerid, &(chan->cid));
399 /* insert the callerid ton */
400 enum_node = ast_data_add_node(data_callerid, "cid_ton");
404 ast_data_add_int(enum_node, "value", chan->cid.cid_ton);
405 snprintf(value_str, sizeof(value_str), "TON: %s/Plan: %s",
406 party_number_ton2str(chan->cid.cid_ton),
407 party_number_plan2str(chan->cid.cid_ton));
408 ast_data_add_str(enum_node, "text", value_str);
412 if (ast_channel_zone(chan)) {
413 data_zones = ast_data_add_node(tree, "zone");
417 ast_tone_zone_data_add_structure(data_zones, ast_channel_zone(chan));
421 data_cdr = ast_data_add_node(tree, "cdr");
429 int ast_channel_data_cmp_structure(const struct ast_data_search *tree,
430 struct ast_channel *chan, const char *structure_name)
432 return ast_data_search_cmp_structure(tree, ast_channel, chan, structure_name);
437 #define DEFINE_STRINGFIELD_SETTERS_FOR(field, publish, assert_on_null) \
438 void ast_channel_##field##_set(struct ast_channel *chan, const char *value) \
440 if ((assert_on_null)) ast_assert(!ast_strlen_zero(value)); \
441 if (!strcmp(value, chan->field)) return; \
442 ast_string_field_set(chan, field, value); \
443 if (publish && ast_channel_internal_is_finalized(chan)) ast_channel_publish_snapshot(chan); \
446 void ast_channel_##field##_build_va(struct ast_channel *chan, const char *fmt, va_list ap) \
448 ast_string_field_build_va(chan, field, fmt, ap); \
449 if (publish && ast_channel_internal_is_finalized(chan)) ast_channel_publish_snapshot(chan); \
451 void ast_channel_##field##_build(struct ast_channel *chan, const char *fmt, ...) \
455 ast_channel_##field##_build_va(chan, fmt, ap); \
459 DEFINE_STRINGFIELD_SETTERS_FOR(name, 0, 1);
460 DEFINE_STRINGFIELD_SETTERS_FOR(language, 1, 0);
461 DEFINE_STRINGFIELD_SETTERS_FOR(musicclass, 0, 0);
462 DEFINE_STRINGFIELD_SETTERS_FOR(latest_musicclass, 0, 0);
463 DEFINE_STRINGFIELD_SETTERS_FOR(accountcode, 1, 0);
464 DEFINE_STRINGFIELD_SETTERS_FOR(peeraccount, 1, 0);
465 DEFINE_STRINGFIELD_SETTERS_FOR(userfield, 0, 0);
466 DEFINE_STRINGFIELD_SETTERS_FOR(call_forward, 0, 0);
467 DEFINE_STRINGFIELD_SETTERS_FOR(parkinglot, 0, 0);
468 DEFINE_STRINGFIELD_SETTERS_FOR(hangupsource, 0, 0);
469 DEFINE_STRINGFIELD_SETTERS_FOR(dialcontext, 0, 0);
471 #define DEFINE_STRINGFIELD_GETTER_FOR(field) const char *ast_channel_##field(const struct ast_channel *chan) \
473 return chan->field; \
476 DEFINE_STRINGFIELD_GETTER_FOR(name);
477 DEFINE_STRINGFIELD_GETTER_FOR(language);
478 DEFINE_STRINGFIELD_GETTER_FOR(musicclass);
479 DEFINE_STRINGFIELD_GETTER_FOR(latest_musicclass);
480 DEFINE_STRINGFIELD_GETTER_FOR(accountcode);
481 DEFINE_STRINGFIELD_GETTER_FOR(peeraccount);
482 DEFINE_STRINGFIELD_GETTER_FOR(userfield);
483 DEFINE_STRINGFIELD_GETTER_FOR(call_forward);
484 DEFINE_STRINGFIELD_GETTER_FOR(parkinglot);
485 DEFINE_STRINGFIELD_GETTER_FOR(hangupsource);
486 DEFINE_STRINGFIELD_GETTER_FOR(dialcontext);
488 const char *ast_channel_uniqueid(const struct ast_channel *chan)
490 ast_assert(chan->uniqueid.unique_id[0] != '\0');
491 return chan->uniqueid.unique_id;
494 const char *ast_channel_linkedid(const struct ast_channel *chan)
496 ast_assert(chan->linkedid.unique_id[0] != '\0');
497 return chan->linkedid.unique_id;
500 const char *ast_channel_appl(const struct ast_channel *chan)
504 void ast_channel_appl_set(struct ast_channel *chan, const char *value)
508 const char *ast_channel_blockproc(const struct ast_channel *chan)
510 return chan->blockproc;
512 void ast_channel_blockproc_set(struct ast_channel *chan, const char *value)
514 chan->blockproc = value;
516 const char *ast_channel_data(const struct ast_channel *chan)
520 void ast_channel_data_set(struct ast_channel *chan, const char *value)
525 const char *ast_channel_context(const struct ast_channel *chan)
527 return chan->context;
529 void ast_channel_context_set(struct ast_channel *chan, const char *value)
531 ast_copy_string(chan->context, value, sizeof(chan->context));
533 const char *ast_channel_exten(const struct ast_channel *chan)
537 void ast_channel_exten_set(struct ast_channel *chan, const char *value)
539 ast_copy_string(chan->exten, value, sizeof(chan->exten));
541 const char *ast_channel_macrocontext(const struct ast_channel *chan)
543 return chan->macrocontext;
545 void ast_channel_macrocontext_set(struct ast_channel *chan, const char *value)
547 ast_copy_string(chan->macrocontext, value, sizeof(chan->macrocontext));
549 const char *ast_channel_macroexten(const struct ast_channel *chan)
551 return chan->macroexten;
553 void ast_channel_macroexten_set(struct ast_channel *chan, const char *value)
555 ast_copy_string(chan->macroexten, value, sizeof(chan->macroexten));
558 char ast_channel_dtmf_digit_to_emulate(const struct ast_channel *chan)
560 return chan->dtmf_digit_to_emulate;
562 void ast_channel_dtmf_digit_to_emulate_set(struct ast_channel *chan, char value)
564 chan->dtmf_digit_to_emulate = value;
567 char ast_channel_sending_dtmf_digit(const struct ast_channel *chan)
569 return chan->sending_dtmf_digit;
571 void ast_channel_sending_dtmf_digit_set(struct ast_channel *chan, char value)
573 chan->sending_dtmf_digit = value;
576 struct timeval ast_channel_sending_dtmf_tv(const struct ast_channel *chan)
578 return chan->sending_dtmf_tv;
580 void ast_channel_sending_dtmf_tv_set(struct ast_channel *chan, struct timeval value)
582 chan->sending_dtmf_tv = value;
585 enum ama_flags ast_channel_amaflags(const struct ast_channel *chan)
587 return chan->amaflags;
590 void ast_channel_amaflags_set(struct ast_channel *chan, enum ama_flags value)
592 if (chan->amaflags == value) {
595 chan->amaflags = value;
596 ast_channel_publish_snapshot(chan);
600 int ast_channel_epfd(const struct ast_channel *chan)
604 void ast_channel_epfd_set(struct ast_channel *chan, int value)
609 int ast_channel_fdno(const struct ast_channel *chan)
613 void ast_channel_fdno_set(struct ast_channel *chan, int value)
617 int ast_channel_hangupcause(const struct ast_channel *chan)
619 return chan->hangupcause;
621 void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
623 chan->hangupcause = value;
625 int ast_channel_macropriority(const struct ast_channel *chan)
627 return chan->macropriority;
629 void ast_channel_macropriority_set(struct ast_channel *chan, int value)
631 chan->macropriority = value;
633 int ast_channel_priority(const struct ast_channel *chan)
635 return chan->priority;
637 void ast_channel_priority_set(struct ast_channel *chan, int value)
639 chan->priority = value;
641 int ast_channel_rings(const struct ast_channel *chan)
645 void ast_channel_rings_set(struct ast_channel *chan, int value)
649 int ast_channel_streamid(const struct ast_channel *chan)
651 return chan->streamid;
653 void ast_channel_streamid_set(struct ast_channel *chan, int value)
655 chan->streamid = value;
657 int ast_channel_timingfd(const struct ast_channel *chan)
659 return chan->timingfd;
661 void ast_channel_timingfd_set(struct ast_channel *chan, int value)
663 chan->timingfd = value;
665 int ast_channel_visible_indication(const struct ast_channel *chan)
667 return chan->visible_indication;
669 void ast_channel_visible_indication_set(struct ast_channel *chan, int value)
671 chan->visible_indication = value;
673 int ast_channel_hold_state(const struct ast_channel *chan)
675 return chan->hold_state;
677 void ast_channel_hold_state_set(struct ast_channel *chan, int value)
679 chan->hold_state = value;
681 int ast_channel_vstreamid(const struct ast_channel *chan)
683 return chan->vstreamid;
685 void ast_channel_vstreamid_set(struct ast_channel *chan, int value)
687 chan->vstreamid = value;
689 unsigned short ast_channel_transfercapability(const struct ast_channel *chan)
691 return chan->transfercapability;
693 void ast_channel_transfercapability_set(struct ast_channel *chan, unsigned short value)
695 chan->transfercapability = value;
697 unsigned int ast_channel_emulate_dtmf_duration(const struct ast_channel *chan)
699 return chan->emulate_dtmf_duration;
701 void ast_channel_emulate_dtmf_duration_set(struct ast_channel *chan, unsigned int value)
703 chan->emulate_dtmf_duration = value;
705 unsigned int ast_channel_fin(const struct ast_channel *chan)
709 void ast_channel_fin_set(struct ast_channel *chan, unsigned int value)
713 unsigned int ast_channel_fout(const struct ast_channel *chan)
717 void ast_channel_fout_set(struct ast_channel *chan, unsigned int value)
721 unsigned long ast_channel_insmpl(const struct ast_channel *chan)
725 void ast_channel_insmpl_set(struct ast_channel *chan, unsigned long value)
727 chan->insmpl = value;
729 unsigned long ast_channel_outsmpl(const struct ast_channel *chan)
731 return chan->outsmpl;
733 void ast_channel_outsmpl_set(struct ast_channel *chan, unsigned long value)
735 chan->outsmpl = value;
737 void *ast_channel_generatordata(const struct ast_channel *chan)
739 return chan->generatordata;
741 void ast_channel_generatordata_set(struct ast_channel *chan, void *value)
743 chan->generatordata = value;
745 void *ast_channel_music_state(const struct ast_channel *chan)
747 return chan->music_state;
749 void ast_channel_music_state_set(struct ast_channel *chan, void *value)
751 chan->music_state = value;
753 void *ast_channel_tech_pvt(const struct ast_channel *chan)
755 return chan->tech_pvt;
757 void ast_channel_tech_pvt_set(struct ast_channel *chan, void *value)
759 chan->tech_pvt = value;
761 void *ast_channel_timingdata(const struct ast_channel *chan)
763 return chan->timingdata;
765 void ast_channel_timingdata_set(struct ast_channel *chan, void *value)
767 chan->timingdata = value;
769 struct ast_audiohook_list *ast_channel_audiohooks(const struct ast_channel *chan)
771 return chan->audiohooks;
773 void ast_channel_audiohooks_set(struct ast_channel *chan, struct ast_audiohook_list *value)
775 chan->audiohooks = value;
777 struct ast_cdr *ast_channel_cdr(const struct ast_channel *chan)
781 void ast_channel_cdr_set(struct ast_channel *chan, struct ast_cdr *value)
785 struct ast_channel *ast_channel_masq(const struct ast_channel *chan)
789 void ast_channel_masq_set(struct ast_channel *chan, struct ast_channel *value)
793 struct ast_channel *ast_channel_masqr(const struct ast_channel *chan)
797 void ast_channel_masqr_set(struct ast_channel *chan, struct ast_channel *value)
801 struct ast_channel_monitor *ast_channel_monitor(const struct ast_channel *chan)
803 return chan->monitor;
805 void ast_channel_monitor_set(struct ast_channel *chan, struct ast_channel_monitor *value)
807 chan->monitor = value;
809 struct ast_filestream *ast_channel_stream(const struct ast_channel *chan)
813 void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
815 chan->stream = value;
817 struct ast_filestream *ast_channel_vstream(const struct ast_channel *chan)
819 return chan->vstream;
821 void ast_channel_vstream_set(struct ast_channel *chan, struct ast_filestream *value)
823 chan->vstream = value;
825 struct ast_format_cap *ast_channel_nativeformats(const struct ast_channel *chan)
827 return chan->nativeformats;
829 void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
831 ao2_cleanup(chan->nativeformats);
832 chan->nativeformats = ao2_bump(value);
834 struct ast_framehook_list *ast_channel_framehooks(const struct ast_channel *chan)
836 return chan->framehooks;
838 void ast_channel_framehooks_set(struct ast_channel *chan, struct ast_framehook_list *value)
840 chan->framehooks = value;
842 struct ast_generator *ast_channel_generator(const struct ast_channel *chan)
844 return chan->generator;
846 void ast_channel_generator_set(struct ast_channel *chan, struct ast_generator *value)
848 chan->generator = value;
850 struct ast_pbx *ast_channel_pbx(const struct ast_channel *chan)
854 void ast_channel_pbx_set(struct ast_channel *chan, struct ast_pbx *value)
858 struct ast_sched_context *ast_channel_sched(const struct ast_channel *chan)
862 void ast_channel_sched_set(struct ast_channel *chan, struct ast_sched_context *value)
866 struct ast_timer *ast_channel_timer(const struct ast_channel *chan)
870 void ast_channel_timer_set(struct ast_channel *chan, struct ast_timer *value)
874 struct ast_tone_zone *ast_channel_zone(const struct ast_channel *chan)
878 void ast_channel_zone_set(struct ast_channel *chan, struct ast_tone_zone *value)
882 struct ast_trans_pvt *ast_channel_readtrans(const struct ast_channel *chan)
884 return chan->readtrans;
886 void ast_channel_readtrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
888 chan->readtrans = value;
890 struct ast_trans_pvt *ast_channel_writetrans(const struct ast_channel *chan)
892 return chan->writetrans;
894 void ast_channel_writetrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
896 chan->writetrans = value;
898 const struct ast_channel_tech *ast_channel_tech(const struct ast_channel *chan)
902 void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
906 enum ast_channel_adsicpe ast_channel_adsicpe(const struct ast_channel *chan)
908 return chan->adsicpe;
910 void ast_channel_adsicpe_set(struct ast_channel *chan, enum ast_channel_adsicpe value)
912 chan->adsicpe = value;
914 enum ast_channel_state ast_channel_state(const struct ast_channel *chan)
918 struct ast_callid *ast_channel_callid(const struct ast_channel *chan)
921 ast_callid_ref(chan->callid);
926 void ast_channel_callid_set(struct ast_channel *chan, struct ast_callid *callid)
928 char call_identifier_from[AST_CALLID_BUFFER_LENGTH];
929 char call_identifier_to[AST_CALLID_BUFFER_LENGTH];
930 call_identifier_from[0] = '\0';
931 ast_callid_strnprint(call_identifier_to, sizeof(call_identifier_to), callid);
933 ast_callid_strnprint(call_identifier_from, sizeof(call_identifier_from), chan->callid);
934 ast_debug(3, "Channel Call ID changing from %s to %s\n", call_identifier_from, call_identifier_to);
935 /* unbind if already set */
936 ast_callid_unref(chan->callid);
939 chan->callid = ast_callid_ref(callid);
941 ast_test_suite_event_notify("CallIDChange",
942 "State: CallIDChange\r\n"
946 ast_channel_name(chan),
948 call_identifier_from);
951 void ast_channel_state_set(struct ast_channel *chan, enum ast_channel_state value)
955 void ast_channel_set_oldwriteformat(struct ast_channel *chan, struct ast_format *format)
957 ao2_cleanup(chan->oldwriteformat);
958 chan->oldwriteformat = ao2_bump(format);
960 void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
962 ao2_cleanup(chan->rawreadformat);
963 chan->rawreadformat = ao2_bump(format);
965 void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
967 ao2_cleanup(chan->rawwriteformat);
968 chan->rawwriteformat = ao2_bump(format);
970 void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
972 ao2_cleanup(chan->readformat);
973 chan->readformat = ao2_bump(format);
975 void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
977 ao2_cleanup(chan->writeformat);
978 chan->writeformat = ao2_bump(format);
980 struct ast_format *ast_channel_oldwriteformat(struct ast_channel *chan)
982 return chan->oldwriteformat;
984 struct ast_format *ast_channel_rawreadformat(struct ast_channel *chan)
986 return chan->rawreadformat;
988 struct ast_format *ast_channel_rawwriteformat(struct ast_channel *chan)
990 return chan->rawwriteformat;
992 struct ast_format *ast_channel_readformat(struct ast_channel *chan)
994 return chan->readformat;
996 struct ast_format *ast_channel_writeformat(struct ast_channel *chan)
998 return chan->writeformat;
1000 struct ast_hangup_handler_list *ast_channel_hangup_handlers(struct ast_channel *chan)
1002 return &chan->hangup_handlers;
1004 struct ast_datastore_list *ast_channel_datastores(struct ast_channel *chan)
1006 return &chan->datastores;
1008 struct ast_autochan_list *ast_channel_autochans(struct ast_channel *chan)
1010 return &chan->autochans;
1012 struct ast_readq_list *ast_channel_readq(struct ast_channel *chan)
1014 return &chan->readq;
1016 struct ast_frame *ast_channel_dtmff(struct ast_channel *chan)
1018 return &chan->dtmff;
1020 struct ast_jb *ast_channel_jb(struct ast_channel *chan)
1024 struct ast_party_caller *ast_channel_caller(struct ast_channel *chan)
1026 return &chan->caller;
1028 struct ast_party_connected_line *ast_channel_connected(struct ast_channel *chan)
1030 return &chan->connected;
1032 struct ast_party_connected_line *ast_channel_connected_indicated(struct ast_channel *chan)
1034 return &chan->connected_indicated;
1036 struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
1038 return ast_party_id_merge(&chan->connected.id, &chan->connected.priv);
1040 struct ast_party_dialed *ast_channel_dialed(struct ast_channel *chan)
1042 return &chan->dialed;
1044 struct ast_party_redirecting *ast_channel_redirecting(struct ast_channel *chan)
1046 return &chan->redirecting;
1048 struct ast_party_id ast_channel_redirecting_effective_orig(struct ast_channel *chan)
1050 return ast_party_id_merge(&chan->redirecting.orig, &chan->redirecting.priv_orig);
1052 struct ast_party_id ast_channel_redirecting_effective_from(struct ast_channel *chan)
1054 return ast_party_id_merge(&chan->redirecting.from, &chan->redirecting.priv_from);
1056 struct ast_party_id ast_channel_redirecting_effective_to(struct ast_channel *chan)
1058 return ast_party_id_merge(&chan->redirecting.to, &chan->redirecting.priv_to);
1060 struct timeval *ast_channel_dtmf_tv(struct ast_channel *chan)
1062 return &chan->dtmf_tv;
1064 struct timeval *ast_channel_whentohangup(struct ast_channel *chan)
1066 return &chan->whentohangup;
1068 struct varshead *ast_channel_varshead(struct ast_channel *chan)
1070 return &chan->varshead;
1072 void ast_channel_dtmff_set(struct ast_channel *chan, struct ast_frame *value)
1074 chan->dtmff = *value;
1076 void ast_channel_jb_set(struct ast_channel *chan, struct ast_jb *value)
1080 void ast_channel_caller_set(struct ast_channel *chan, struct ast_party_caller *value)
1082 chan->caller = *value;
1084 void ast_channel_connected_set(struct ast_channel *chan, struct ast_party_connected_line *value)
1086 chan->connected = *value;
1088 void ast_channel_dialed_set(struct ast_channel *chan, struct ast_party_dialed *value)
1090 chan->dialed = *value;
1092 void ast_channel_redirecting_set(struct ast_channel *chan, struct ast_party_redirecting *value)
1094 chan->redirecting = *value;
1096 void ast_channel_dtmf_tv_set(struct ast_channel *chan, struct timeval *value)
1098 chan->dtmf_tv = *value;
1100 void ast_channel_whentohangup_set(struct ast_channel *chan, struct timeval *value)
1102 chan->whentohangup = *value;
1104 void ast_channel_varshead_set(struct ast_channel *chan, struct varshead *value)
1106 chan->varshead = *value;
1108 struct timeval ast_channel_creationtime(struct ast_channel *chan)
1110 return chan->creationtime;
1112 void ast_channel_creationtime_set(struct ast_channel *chan, struct timeval *value)
1114 chan->creationtime = *value;
1117 struct timeval ast_channel_answertime(struct ast_channel *chan)
1119 return chan->answertime;
1122 void ast_channel_answertime_set(struct ast_channel *chan, struct timeval *value)
1124 chan->answertime = *value;
1127 /* Evil softhangup accessors */
1128 int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
1130 return chan->softhangup;
1132 void ast_channel_softhangup_internal_flag_set(struct ast_channel *chan, int value)
1134 chan->softhangup = value;
1136 void ast_channel_softhangup_internal_flag_add(struct ast_channel *chan, int value)
1138 chan->softhangup |= value;
1140 void ast_channel_softhangup_internal_flag_clear(struct ast_channel *chan, int value)
1142 chan ->softhangup &= ~value;
1145 int ast_channel_unbridged_nolock(struct ast_channel *chan)
1147 return chan->unbridged;
1150 int ast_channel_unbridged(struct ast_channel *chan)
1153 ast_channel_lock(chan);
1154 res = ast_channel_unbridged_nolock(chan);
1155 ast_channel_unlock(chan);
1159 void ast_channel_set_unbridged_nolock(struct ast_channel *chan, int value)
1161 chan->unbridged = value;
1162 ast_queue_frame(chan, &ast_null_frame);
1165 void ast_channel_set_unbridged(struct ast_channel *chan, int value)
1167 ast_channel_lock(chan);
1168 ast_channel_set_unbridged_nolock(chan, value);
1169 ast_channel_unlock(chan);
1172 void ast_channel_callid_cleanup(struct ast_channel *chan)
1175 chan->callid = ast_callid_unref(chan->callid);
1179 /* Typedef accessors */
1180 ast_group_t ast_channel_callgroup(const struct ast_channel *chan)
1182 return chan->callgroup;
1184 void ast_channel_callgroup_set(struct ast_channel *chan, ast_group_t value)
1186 chan->callgroup = value;
1188 ast_group_t ast_channel_pickupgroup(const struct ast_channel *chan)
1190 return chan->pickupgroup;
1192 void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
1194 chan->pickupgroup = value;
1196 struct ast_namedgroups *ast_channel_named_callgroups(const struct ast_channel *chan)
1198 return chan->named_callgroups;
1200 void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
1202 ast_unref_namedgroups(chan->named_callgroups);
1203 chan->named_callgroups = ast_ref_namedgroups(value);
1205 struct ast_namedgroups *ast_channel_named_pickupgroups(const struct ast_channel *chan)
1207 return chan->named_pickupgroups;
1209 void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
1211 ast_unref_namedgroups(chan->named_pickupgroups);
1212 chan->named_pickupgroups = ast_ref_namedgroups(value);
1215 /* Alertpipe functions */
1216 int ast_channel_alert_write(struct ast_channel *chan)
1219 return ast_channel_alert_writable(chan) && write(chan->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah);
1222 ast_alert_status_t ast_channel_internal_alert_read(struct ast_channel *chan)
1227 if (!ast_channel_internal_alert_readable(chan)) {
1228 return AST_ALERT_NOT_READABLE;
1231 flags = fcntl(chan->alertpipe[0], F_GETFL);
1232 /* For some odd reason, the alertpipe occasionally loses nonblocking status,
1233 * which immediately causes a deadlock scenario. Detect and prevent this. */
1234 if ((flags & O_NONBLOCK) == 0) {
1235 ast_log(LOG_ERROR, "Alertpipe on channel %s lost O_NONBLOCK?!!\n", ast_channel_name(chan));
1236 if (fcntl(chan->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
1237 ast_log(LOG_WARNING, "Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1238 return AST_ALERT_READ_FATAL;
1241 if (read(chan->alertpipe[0], &blah, sizeof(blah)) < 0) {
1242 if (errno != EINTR && errno != EAGAIN) {
1243 ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
1244 return AST_ALERT_READ_FAIL;
1248 return AST_ALERT_READ_SUCCESS;
1251 int ast_channel_alert_writable(struct ast_channel *chan)
1253 return chan->alertpipe[1] > -1;
1256 int ast_channel_internal_alert_readable(struct ast_channel *chan)
1258 return chan->alertpipe[0] > -1;
1261 void ast_channel_internal_alertpipe_clear(struct ast_channel *chan)
1263 chan->alertpipe[0] = chan->alertpipe[1] = -1;
1266 void ast_channel_internal_alertpipe_close(struct ast_channel *chan)
1268 if (ast_channel_internal_alert_readable(chan)) {
1269 close(chan->alertpipe[0]);
1271 if (ast_channel_alert_writable(chan)) {
1272 close(chan->alertpipe[1]);
1276 int ast_channel_internal_alertpipe_init(struct ast_channel *chan)
1278 if (pipe(chan->alertpipe)) {
1279 ast_log(LOG_WARNING, "Channel allocation failed: Can't create alert pipe! Try increasing max file descriptors with ulimit -n\n");
1282 int flags = fcntl(chan->alertpipe[0], F_GETFL);
1283 if (fcntl(chan->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
1284 ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1287 flags = fcntl(chan->alertpipe[1], F_GETFL);
1288 if (fcntl(chan->alertpipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
1289 ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
1296 int ast_channel_internal_alert_readfd(struct ast_channel *chan)
1298 return chan->alertpipe[0];
1301 void ast_channel_internal_alertpipe_swap(struct ast_channel *chan1, struct ast_channel *chan2)
1304 for (i = 0; i < ARRAY_LEN(chan1->alertpipe); i++) {
1305 SWAP(chan1->alertpipe[i], chan2->alertpipe[i]);
1309 /* file descriptor array accessors */
1310 void ast_channel_internal_fd_set(struct ast_channel *chan, int which, int value)
1312 chan->fds[which] = value;
1314 void ast_channel_internal_fd_clear(struct ast_channel *chan, int which)
1316 ast_channel_internal_fd_set(chan, which, -1);
1318 void ast_channel_internal_fd_clear_all(struct ast_channel *chan)
1321 for (i = 0; i < AST_MAX_FDS; i++) {
1322 ast_channel_internal_fd_clear(chan, i);
1325 int ast_channel_fd(const struct ast_channel *chan, int which)
1327 return chan->fds[which];
1329 int ast_channel_fd_isset(const struct ast_channel *chan, int which)
1331 return ast_channel_fd(chan, which) > -1;
1335 struct ast_epoll_data *ast_channel_internal_epfd_data(const struct ast_channel *chan, int which)
1337 return chan->epfd_data[which];
1339 void ast_channel_internal_epfd_data_set(struct ast_channel *chan, int which , struct ast_epoll_data *value)
1341 chan->epfd_data[which] = value;
1345 pthread_t ast_channel_blocker(const struct ast_channel *chan)
1347 return chan->blocker;
1349 void ast_channel_blocker_set(struct ast_channel *chan, pthread_t value)
1351 chan->blocker = value;
1354 ast_timing_func_t ast_channel_timingfunc(const struct ast_channel *chan)
1356 return chan->timingfunc;
1358 void ast_channel_timingfunc_set(struct ast_channel *chan, ast_timing_func_t value)
1360 chan->timingfunc = value;
1363 struct ast_bridge *ast_channel_internal_bridge(const struct ast_channel *chan)
1365 return chan->bridge;
1367 void ast_channel_internal_bridge_set(struct ast_channel *chan, struct ast_bridge *value)
1369 chan->bridge = value;
1370 ast_channel_publish_snapshot(chan);
1373 struct ast_bridge_channel *ast_channel_internal_bridge_channel(const struct ast_channel *chan)
1375 return chan->bridge_channel;
1377 void ast_channel_internal_bridge_channel_set(struct ast_channel *chan, struct ast_bridge_channel *value)
1379 chan->bridge_channel = value;
1382 struct ast_flags *ast_channel_flags(struct ast_channel *chan)
1384 return &chan->flags;
1387 static int collect_names_cb(void *obj, void *arg, int flags) {
1388 struct ast_control_pvt_cause_code *cause_code = obj;
1389 struct ast_str **str = arg;
1391 ast_str_append(str, 0, "%s%s", (ast_str_strlen(*str) ? "," : ""), cause_code->chan_name);
1396 struct ast_str *ast_channel_dialed_causes_channels(const struct ast_channel *chan)
1398 struct ast_str *chanlist = ast_str_create(128);
1404 ao2_callback(chan->dialed_causes, 0, collect_names_cb, &chanlist);
1409 struct ast_control_pvt_cause_code *ast_channel_dialed_causes_find(const struct ast_channel *chan, const char *chan_name)
1411 return ao2_find(chan->dialed_causes, chan_name, OBJ_KEY);
1414 int ast_channel_dialed_causes_add(const struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
1416 struct ast_control_pvt_cause_code *ao2_cause_code;
1417 ao2_find(chan->dialed_causes, cause_code->chan_name, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA);
1418 ao2_cause_code = ao2_alloc(datalen, NULL);
1420 if (ao2_cause_code) {
1421 memcpy(ao2_cause_code, cause_code, datalen);
1422 ao2_link(chan->dialed_causes, ao2_cause_code);
1423 ao2_ref(ao2_cause_code, -1);
1430 void ast_channel_dialed_causes_clear(const struct ast_channel *chan)
1432 ao2_callback(chan->dialed_causes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
1435 /* \brief Hash function for pvt cause code frames */
1436 static int pvt_cause_hash_fn(const void *vpc, const int flags)
1438 const struct ast_control_pvt_cause_code *pc = vpc;
1439 return ast_str_hash(ast_tech_to_upper(ast_strdupa(pc->chan_name)));
1442 /* \brief Comparison function for pvt cause code frames */
1443 static int pvt_cause_cmp_fn(void *obj, void *vstr, int flags)
1445 struct ast_control_pvt_cause_code *pc = obj;
1446 char *str = ast_tech_to_upper(ast_strdupa(vstr));
1447 char *pc_str = ast_tech_to_upper(ast_strdupa(pc->chan_name));
1448 return !strcmp(pc_str, str) ? CMP_MATCH | CMP_STOP : 0;
1451 #define DIALED_CAUSES_BUCKETS 37
1453 struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj), const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *file, int line, const char *function)
1455 struct ast_channel *tmp;
1456 #if defined(REF_DEBUG)
1457 tmp = __ao2_alloc_debug(sizeof(*tmp), destructor,
1458 AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function, 1);
1459 #elif defined(__AST_DEBUG_MALLOC)
1460 tmp = __ao2_alloc_debug(sizeof(*tmp), destructor,
1461 AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function, 0);
1463 tmp = ao2_alloc(sizeof(*tmp), destructor);
1466 if ((ast_string_field_init(tmp, 128))) {
1467 return ast_channel_unref(tmp);
1470 if (!(tmp->dialed_causes = ao2_container_alloc(DIALED_CAUSES_BUCKETS, pvt_cause_hash_fn, pvt_cause_cmp_fn))) {
1471 return ast_channel_unref(tmp);
1474 /* set the creation time in the uniqueid */
1475 tmp->uniqueid.creation_time = time(NULL);
1476 tmp->uniqueid.creation_unique = ast_atomic_fetchadd_int(&uniqueint, 1);
1478 /* use provided id or default to historical {system-}time.# format */
1479 if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
1480 ast_copy_string(tmp->uniqueid.unique_id, assignedids->uniqueid, sizeof(tmp->uniqueid.unique_id));
1481 } else if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
1482 snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%li.%d",
1483 (long)(tmp->uniqueid.creation_time),
1484 tmp->uniqueid.creation_unique);
1486 snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%s-%li.%d",
1487 ast_config_AST_SYSTEM_NAME,
1488 (long)(tmp->uniqueid.creation_time),
1489 tmp->uniqueid.creation_unique);
1492 /* copy linked id from parent channel if known */
1494 tmp->linkedid = requestor->linkedid;
1496 tmp->linkedid = tmp->uniqueid;
1502 struct ast_channel *ast_channel_internal_oldest_linkedid(struct ast_channel *a, struct ast_channel *b)
1504 ast_assert(a->linkedid.creation_time != 0);
1505 ast_assert(b->linkedid.creation_time != 0);
1507 if (a->linkedid.creation_time < b->linkedid.creation_time) {
1510 if (b->linkedid.creation_time < a->linkedid.creation_time) {
1513 if (a->linkedid.creation_unique < b->linkedid.creation_unique) {
1519 void ast_channel_internal_copy_linkedid(struct ast_channel *dest, struct ast_channel *source)
1521 if (dest->linkedid.creation_time == source->linkedid.creation_time
1522 && dest->linkedid.creation_unique == source->linkedid.creation_unique
1523 && !strcmp(dest->linkedid.unique_id, source->linkedid.unique_id)) {
1526 dest->linkedid = source->linkedid;
1527 ast_channel_publish_snapshot(dest);
1530 void ast_channel_internal_swap_uniqueid_and_linkedid(struct ast_channel *a, struct ast_channel *b)
1532 struct ast_channel_id temp;
1535 a->uniqueid = b->uniqueid;
1539 a->linkedid = b->linkedid;
1543 void ast_channel_internal_set_fake_ids(struct ast_channel *chan, const char *uniqueid, const char *linkedid)
1545 ast_copy_string(chan->uniqueid.unique_id, uniqueid, sizeof(chan->uniqueid.unique_id));
1546 ast_copy_string(chan->linkedid.unique_id, linkedid, sizeof(chan->linkedid.unique_id));
1549 void ast_channel_internal_cleanup(struct ast_channel *chan)
1551 if (chan->dialed_causes) {
1552 ao2_t_ref(chan->dialed_causes, -1,
1553 "done with dialed causes since the channel is going away");
1554 chan->dialed_causes = NULL;
1557 ast_string_field_free_memory(chan);
1559 chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
1560 chan->endpoint_cache_forward = stasis_forward_cancel(chan->endpoint_cache_forward);
1562 stasis_cp_single_unsubscribe(chan->topics);
1563 chan->topics = NULL;
1566 void ast_channel_internal_finalize(struct ast_channel *chan)
1568 chan->finalized = 1;
1571 int ast_channel_internal_is_finalized(struct ast_channel *chan)
1573 return chan->finalized;
1576 struct stasis_topic *ast_channel_topic(struct ast_channel *chan)
1579 return ast_channel_topic_all();
1582 return stasis_cp_single_topic(chan->topics);
1585 struct stasis_topic *ast_channel_topic_cached(struct ast_channel *chan)
1588 return ast_channel_topic_all_cached();
1591 return stasis_cp_single_topic_cached(chan->topics);
1594 int ast_channel_forward_endpoint(struct ast_channel *chan,
1595 struct ast_endpoint *endpoint)
1597 ast_assert(chan != NULL);
1598 ast_assert(endpoint != NULL);
1600 chan->endpoint_forward =
1601 stasis_forward_all(ast_channel_topic(chan),
1602 ast_endpoint_topic(endpoint));
1603 if (!chan->endpoint_forward) {
1607 chan->endpoint_cache_forward = stasis_forward_all(ast_channel_topic_cached(chan),
1608 ast_endpoint_topic(endpoint));
1609 if (!chan->endpoint_cache_forward) {
1610 chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
1617 int ast_channel_internal_setup_topics(struct ast_channel *chan)
1619 const char *topic_name = chan->uniqueid.unique_id;
1620 ast_assert(chan->topics == NULL);
1622 if (ast_strlen_zero(topic_name)) {
1623 topic_name = "<dummy-channel>";
1626 chan->topics = stasis_cp_single_create(
1627 ast_channel_cache_all(), topic_name);
1628 if (!chan->topics) {