2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, 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 Management
23 * \author Mark Spencer <markster@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/_private.h"
40 #include "asterisk/paths.h" /* use ast_config_AST_SYSTEM_NAME */
42 #include "asterisk/pbx.h"
43 #include "asterisk/frame.h"
44 #include "asterisk/mod_format.h"
45 #include "asterisk/sched.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/musiconhold.h"
48 #include "asterisk/say.h"
49 #include "asterisk/file.h"
50 #include "asterisk/cli.h"
51 #include "asterisk/translate.h"
52 #include "asterisk/manager.h"
53 #include "asterisk/chanvars.h"
54 #include "asterisk/linkedlists.h"
55 #include "asterisk/indications.h"
56 #include "asterisk/monitor.h"
57 #include "asterisk/causes.h"
58 #include "asterisk/callerid.h"
59 #include "asterisk/utils.h"
60 #include "asterisk/lock.h"
61 #include "asterisk/app.h"
62 #include "asterisk/transcap.h"
63 #include "asterisk/devicestate.h"
64 #include "asterisk/threadstorage.h"
65 #include "asterisk/slinfactory.h"
66 #include "asterisk/audiohook.h"
67 #include "asterisk/framehook.h"
68 #include "asterisk/timing.h"
69 #include "asterisk/autochan.h"
70 #include "asterisk/stringfields.h"
71 #include "asterisk/global_datastores.h"
72 #include "asterisk/data.h"
73 #include "asterisk/channel_internal.h"
74 #include "asterisk/features.h"
75 #include "asterisk/bridge.h"
76 #include "asterisk/test.h"
77 #include "asterisk/stasis_channels.h"
83 #include <sys/epoll.h>
86 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
89 #endif /* defined(HAVE_PRI) */
90 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
92 struct ast_epoll_data {
93 struct ast_channel *chan;
97 /* uncomment if you have problems with 'monitoring' synchronized files */
99 #define MONITOR_CONSTANT_DELAY
100 #define MONITOR_DELAY 150 * 8 /*!< 150 ms of MONITORING DELAY */
103 /*! \brief Prevent new channel allocation if shutting down. */
104 static int shutting_down;
106 static int chancount;
108 unsigned long global_fin, global_fout;
110 AST_THREADSTORAGE(state2str_threadbuf);
111 #define STATE2STR_BUFSIZE 32
113 /*! Default amount of time to use when emulating a digit as a begin and end
115 #define AST_DEFAULT_EMULATE_DTMF_DURATION 100
117 #define DEFAULT_AMA_FLAGS AST_AMA_DOCUMENTATION
119 /*! Minimum amount of time between the end of the last digit and the beginning
120 * of a new one - 45ms */
121 #define AST_MIN_DTMF_GAP 45
123 /*! \brief List of channel drivers */
125 const struct ast_channel_tech *tech;
126 AST_LIST_ENTRY(chanlist) list;
129 /*! \brief the list of registered channel types */
130 static AST_RWLIST_HEAD_STATIC(backends, chanlist);
133 #define NUM_CHANNEL_BUCKETS 61
135 #define NUM_CHANNEL_BUCKETS 1567
138 /*! \brief All active channels on the system */
139 static struct ao2_container *channels;
141 /*! \brief map AST_CAUSE's to readable string representations
151 static const struct causes_map causes[] = {
152 { AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
153 { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
154 { AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
155 { AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
156 { AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
157 { AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
158 { AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
159 { AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
160 { AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
161 { AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
162 { AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
163 { AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
164 { AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
165 { AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
166 { AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
167 { AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
168 { AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
169 { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
170 { AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
171 { AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
172 { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
173 { AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
174 { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
175 { AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
176 { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
177 { AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
178 { AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
179 { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
180 { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
181 { AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
182 { AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
183 { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
184 { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
185 { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
186 { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
187 { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
188 { AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
189 { AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
190 { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
191 { AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
192 { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
193 { AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
194 { AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
195 { AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
196 { AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
197 { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
198 { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
199 { AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
200 { AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
203 struct ast_variable *ast_channeltype_list(void)
206 struct ast_variable *var = NULL, *prev = NULL;
208 AST_RWLIST_RDLOCK(&backends);
209 AST_RWLIST_TRAVERSE(&backends, cl, list) {
211 if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
214 var = ast_variable_new(cl->tech->type, cl->tech->description, "");
218 AST_RWLIST_UNLOCK(&backends);
223 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
224 static const char *party_number_ton2str(int ton)
226 #if defined(HAVE_PRI)
227 switch ((ton >> 4) & 0x07) {
228 case PRI_TON_INTERNATIONAL:
229 return "International";
230 case PRI_TON_NATIONAL:
232 case PRI_TON_NET_SPECIFIC:
233 return "Network Specific";
234 case PRI_TON_SUBSCRIBER:
236 case PRI_TON_ABBREVIATED:
237 return "Abbreviated";
238 case PRI_TON_RESERVED:
240 case PRI_TON_UNKNOWN:
244 #endif /* defined(HAVE_PRI) */
247 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
249 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
250 static const char *party_number_plan2str(int plan)
252 #if defined(HAVE_PRI)
253 switch (plan & 0x0F) {
255 case PRI_NPI_UNKNOWN:
257 case PRI_NPI_E163_E164:
258 return "Public (E.163/E.164)";
260 return "Data (X.121)";
262 return "Telex (F.69)";
263 case PRI_NPI_NATIONAL:
264 return "National Standard";
265 case PRI_NPI_PRIVATE:
267 case PRI_NPI_RESERVED:
270 #endif /* defined(HAVE_PRI) */
273 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
275 /*! \brief Show channel types - CLI command */
276 static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
278 #define FORMAT "%-15.15s %-40.40s %-13.13s %-13.13s %-13.13s %-13.13s\n"
284 e->command = "core show channeltypes";
286 "Usage: core show channeltypes\n"
287 " Lists available channel types registered in your\n"
288 " Asterisk server.\n";
295 return CLI_SHOWUSAGE;
297 ast_cli(a->fd, FORMAT, "Type", "Description", "Devicestate", "Presencestate", "Indications", "Transfer");
298 ast_cli(a->fd, FORMAT, "-------------", "-------------", "-------------", "-------------", "-------------", "-------------");
300 AST_RWLIST_RDLOCK(&backends);
301 AST_RWLIST_TRAVERSE(&backends, cl, list) {
302 ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
303 (cl->tech->devicestate) ? "yes" : "no",
304 (cl->tech->presencestate) ? "yes" : "no",
305 (cl->tech->indicate) ? "yes" : "no",
306 (cl->tech->transfer) ? "yes" : "no");
309 AST_RWLIST_UNLOCK(&backends);
311 ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
318 static char *complete_channeltypes(struct ast_cli_args *a)
328 wordlen = strlen(a->word);
330 AST_RWLIST_RDLOCK(&backends);
331 AST_RWLIST_TRAVERSE(&backends, cl, list) {
332 if (!strncasecmp(a->word, cl->tech->type, wordlen) && ++which > a->n) {
333 ret = ast_strdup(cl->tech->type);
337 AST_RWLIST_UNLOCK(&backends);
342 /*! \brief Show details about a channel driver - CLI command */
343 static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
345 struct chanlist *cl = NULL;
346 struct ast_str *codec_buf = ast_str_alloca(256);
350 e->command = "core show channeltype";
352 "Usage: core show channeltype <name>\n"
353 " Show details about the specified channel type, <name>.\n";
356 return complete_channeltypes(a);
360 return CLI_SHOWUSAGE;
362 AST_RWLIST_RDLOCK(&backends);
364 AST_RWLIST_TRAVERSE(&backends, cl, list) {
365 if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
371 ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
372 AST_RWLIST_UNLOCK(&backends);
377 "-- Info about channel driver: %s --\n"
378 " Device State: %s\n"
379 "Presence State: %s\n"
382 " Capabilities: %s\n"
386 " Image Support: %s\n"
387 " Text Support: %s\n",
389 (cl->tech->devicestate) ? "yes" : "no",
390 (cl->tech->presencestate) ? "yes" : "no",
391 (cl->tech->indicate) ? "yes" : "no",
392 (cl->tech->transfer) ? "yes" : "no",
393 ast_format_cap_get_names(cl->tech->capabilities, &codec_buf),
394 (cl->tech->send_digit_begin) ? "yes" : "no",
395 (cl->tech->send_digit_end) ? "yes" : "no",
396 (cl->tech->send_html) ? "yes" : "no",
397 (cl->tech->send_image) ? "yes" : "no",
398 (cl->tech->send_text) ? "yes" : "no"
402 AST_RWLIST_UNLOCK(&backends);
407 static struct ast_cli_entry cli_channel[] = {
408 AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
409 AST_CLI_DEFINE(handle_cli_core_show_channeltype, "Give more details on that channel type")
412 static struct ast_frame *kill_read(struct ast_channel *chan)
414 /* Hangup channel. */
418 static struct ast_frame *kill_exception(struct ast_channel *chan)
420 /* Hangup channel. */
424 static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
426 /* Hangup channel. */
430 static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
432 /* No problem fixing up the channel. */
436 static int kill_hangup(struct ast_channel *chan)
438 ast_channel_tech_pvt_set(chan, NULL);
443 * \brief Kill the channel channel driver technology descriptor.
446 * The purpose of this channel technology is to encourage the
447 * channel to hangup as quickly as possible.
449 * \note Used by DTMF atxfer and zombie channels.
451 const struct ast_channel_tech ast_kill_tech = {
453 .description = "Kill channel (should not see this)",
455 .exception = kill_exception,
458 .hangup = kill_hangup,
461 /*! \brief Checks to see if a channel is needing hang up */
462 int ast_check_hangup(struct ast_channel *chan)
464 if (ast_channel_softhangup_internal_flag(chan)) /* yes if soft hangup flag set */
466 if (ast_tvzero(*ast_channel_whentohangup(chan))) /* no if no hangup scheduled */
468 if (ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()) > 0) /* no if hangup time has not come yet. */
470 ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()));
471 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(chan));
472 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT); /* record event */
476 int ast_check_hangup_locked(struct ast_channel *chan)
479 ast_channel_lock(chan);
480 res = ast_check_hangup(chan);
481 ast_channel_unlock(chan);
485 void ast_channel_softhangup_withcause_locked(struct ast_channel *chan, int causecode)
487 ast_channel_lock(chan);
490 ast_debug(1, "Setting hangupcause of channel %s to %d (is %d now)\n",
491 ast_channel_name(chan), causecode, ast_channel_hangupcause(chan));
493 ast_channel_hangupcause_set(chan, causecode);
496 ast_softhangup_nolock(chan, AST_SOFTHANGUP_EXPLICIT);
498 ast_channel_unlock(chan);
501 static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
503 struct ast_channel *chan = obj;
505 ast_softhangup(chan, AST_SOFTHANGUP_SHUTDOWN);
510 void ast_begin_shutdown(int hangup)
515 ao2_callback(channels, OBJ_NODATA | OBJ_MULTIPLE, ast_channel_softhangup_cb, NULL);
519 /*! \brief returns number of active/allocated channels */
520 int ast_active_channels(void)
522 return channels ? ao2_container_count(channels) : 0;
525 int ast_undestroyed_channels(void)
527 return ast_atomic_fetchadd_int(&chancount, 0);
530 /*! \brief Cancel a shutdown in progress */
531 void ast_cancel_shutdown(void)
536 /*! \brief Returns non-zero if Asterisk is being shut down */
537 int ast_shutting_down(void)
539 return shutting_down;
542 /*! \brief Set when to hangup channel */
543 void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
545 if (ast_tvzero(offset)) {
546 ast_channel_whentohangup_set(chan, &offset);
548 struct timeval tv = ast_tvadd(offset, ast_tvnow());
549 ast_channel_whentohangup_set(chan, &tv);
551 ast_queue_frame(chan, &ast_null_frame);
555 void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
557 struct timeval when = { offset, };
558 ast_channel_setwhentohangup_tv(chan, when);
561 /*! \brief Compare a offset with when to hangup channel */
562 int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
564 struct timeval whentohangup;
566 if (ast_tvzero(*ast_channel_whentohangup(chan)))
567 return ast_tvzero(offset) ? 0 : -1;
569 if (ast_tvzero(offset))
572 whentohangup = ast_tvadd(offset, ast_tvnow());
574 return ast_tvdiff_ms(whentohangup, *ast_channel_whentohangup(chan));
577 int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
579 struct timeval when = { offset, };
580 return ast_channel_cmpwhentohangup_tv(chan, when);
583 /*! \brief Register a new telephony channel in Asterisk */
584 int ast_channel_register(const struct ast_channel_tech *tech)
586 struct chanlist *chan;
588 AST_RWLIST_WRLOCK(&backends);
590 AST_RWLIST_TRAVERSE(&backends, chan, list) {
591 if (!strcasecmp(tech->type, chan->tech->type)) {
592 ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
593 AST_RWLIST_UNLOCK(&backends);
598 if (!(chan = ast_calloc(1, sizeof(*chan)))) {
599 AST_RWLIST_UNLOCK(&backends);
603 AST_RWLIST_INSERT_HEAD(&backends, chan, list);
605 ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
607 ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
609 AST_RWLIST_UNLOCK(&backends);
614 /*! \brief Unregister channel driver */
615 void ast_channel_unregister(const struct ast_channel_tech *tech)
617 struct chanlist *chan;
619 ast_debug(1, "Unregistering channel type '%s'\n", tech->type);
621 AST_RWLIST_WRLOCK(&backends);
623 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&backends, chan, list) {
624 if (chan->tech == tech) {
625 AST_LIST_REMOVE_CURRENT(list);
627 ast_verb(2, "Unregistered channel type '%s'\n", tech->type);
631 AST_LIST_TRAVERSE_SAFE_END;
633 AST_RWLIST_UNLOCK(&backends);
636 /*! \brief Get handle to channel driver based on name */
637 const struct ast_channel_tech *ast_get_channel_tech(const char *name)
639 struct chanlist *chanls;
640 const struct ast_channel_tech *ret = NULL;
642 AST_RWLIST_RDLOCK(&backends);
644 AST_RWLIST_TRAVERSE(&backends, chanls, list) {
645 if (!strcasecmp(name, chanls->tech->type)) {
651 AST_RWLIST_UNLOCK(&backends);
656 /*! \brief Gives the string form of a given hangup cause */
657 const char *ast_cause2str(int cause)
661 for (x = 0; x < ARRAY_LEN(causes); x++) {
662 if (causes[x].cause == cause)
663 return causes[x].desc;
669 /*! \brief Convert a symbolic hangup cause to number */
670 int ast_str2cause(const char *name)
674 for (x = 0; x < ARRAY_LEN(causes); x++)
675 if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
676 return causes[x].cause;
681 static struct stasis_message *create_channel_snapshot_message(struct ast_channel *channel)
683 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
685 if (!ast_channel_snapshot_type()) {
689 ast_channel_lock(channel);
690 snapshot = ast_channel_snapshot_create(channel);
691 ast_channel_unlock(channel);
696 return stasis_message_create(ast_channel_snapshot_type(), snapshot);
699 static void publish_cache_clear(struct ast_channel *chan)
701 RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
702 RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
704 clear_msg = create_channel_snapshot_message(chan);
709 message = stasis_cache_clear_create(clear_msg);
710 stasis_publish(ast_channel_topic(chan), message);
713 /*! \brief Gives the string form of a given channel state.
715 * \note This function is not reentrant.
719 const char *ast_state2str(enum ast_channel_state state)
726 case AST_STATE_RESERVED:
728 case AST_STATE_OFFHOOK:
730 case AST_STATE_DIALING:
734 case AST_STATE_RINGING:
740 case AST_STATE_DIALING_OFFHOOK:
741 return "Dialing Offhook";
742 case AST_STATE_PRERING:
747 if (!(buf = ast_threadstorage_get(&state2str_threadbuf, STATE2STR_BUFSIZE)))
749 snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%u)", state);
754 /*! \brief Gives the string form of a given transfer capability */
755 char *ast_transfercapability2str(int transfercapability)
757 switch (transfercapability) {
758 case AST_TRANS_CAP_SPEECH:
760 case AST_TRANS_CAP_DIGITAL:
762 case AST_TRANS_CAP_RESTRICTED_DIGITAL:
763 return "RESTRICTED_DIGITAL";
764 case AST_TRANS_CAP_3_1K_AUDIO:
766 case AST_TRANS_CAP_DIGITAL_W_TONES:
767 return "DIGITAL_W_TONES";
768 case AST_TRANS_CAP_VIDEO:
775 /*! \brief Channel technology used to extract a channel from a running application. The
776 * channel created with this technology will be immediately hung up - most external
777 * applications won't ever want to see this.
779 static const struct ast_channel_tech surrogate_tech = {
781 .description = "Surrogate channel used to pull channel from an application",
782 .properties = AST_CHAN_TP_INTERNAL,
785 static const struct ast_channel_tech null_tech = {
787 .description = "Null channel (should not see this)",
790 static void ast_channel_destructor(void *obj);
791 static void ast_dummy_channel_destructor(void *obj);
793 /*! \brief Create a new channel structure */
794 static struct ast_channel * attribute_malloc __attribute__((format(printf, 15, 0)))
795 __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
796 const char *acctcode, const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
797 const struct ast_channel *requestor, enum ama_flags amaflag, struct ast_endpoint *endpoint,
798 const char *file, int line,
799 const char *function, const char *name_fmt, va_list ap)
801 struct ast_channel *tmp;
802 struct varshead *headp;
803 char *tech = "", *tech2 = NULL;
804 struct ast_format_cap *nativeformats;
805 struct ast_sched_context *schedctx;
806 struct ast_timer *timer;
808 const struct ast_channel_tech *channel_tech;
810 /* If shutting down, don't allocate any new channels */
811 if (ast_shutting_down()) {
812 ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
816 if (!(tmp = ast_channel_internal_alloc(ast_channel_destructor, assignedids, requestor))) {
817 /* Channel structure allocation failure. */
821 ast_channel_stage_snapshot(tmp);
823 if (!(nativeformats = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
824 /* format capabilities structure allocation failure */
825 return ast_channel_unref(tmp);
827 ast_format_cap_append(nativeformats, ast_format_none, 0);
828 ast_channel_nativeformats_set(tmp, nativeformats);
829 ao2_ref(nativeformats, -1);
831 ast_channel_set_rawwriteformat(tmp, ast_format_none);
832 ast_channel_set_rawreadformat(tmp, ast_format_none);
833 ast_channel_set_writeformat(tmp, ast_format_none);
834 ast_channel_set_readformat(tmp, ast_format_none);
837 * Init file descriptors to unopened state so
838 * the destructor can know not to close them.
840 ast_channel_timingfd_set(tmp, -1);
841 ast_channel_internal_alertpipe_clear(tmp);
842 ast_channel_internal_fd_clear_all(tmp);
845 ast_channel_epfd_set(tmp, epoll_create(25));
848 if (!(schedctx = ast_sched_context_create())) {
849 ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
850 return ast_channel_unref(tmp);
852 ast_channel_sched_set(tmp, schedctx);
854 ast_party_dialed_init(ast_channel_dialed(tmp));
855 ast_party_caller_init(ast_channel_caller(tmp));
856 ast_party_connected_line_init(ast_channel_connected(tmp));
857 ast_party_connected_line_init(ast_channel_connected_indicated(tmp));
858 ast_party_redirecting_init(ast_channel_redirecting(tmp));
861 ast_channel_caller(tmp)->id.name.valid = 1;
862 ast_channel_caller(tmp)->id.name.str = ast_strdup(cid_name);
863 if (!ast_channel_caller(tmp)->id.name.str) {
864 return ast_channel_unref(tmp);
868 ast_channel_caller(tmp)->id.number.valid = 1;
869 ast_channel_caller(tmp)->id.number.str = ast_strdup(cid_num);
870 if (!ast_channel_caller(tmp)->id.number.str) {
871 return ast_channel_unref(tmp);
875 if ((timer = ast_timer_open())) {
876 ast_channel_timer_set(tmp, timer);
877 if (strcmp(ast_timer_get_name(ast_channel_timer(tmp)), "timerfd")) {
880 ast_channel_timingfd_set(tmp, ast_timer_fd(ast_channel_timer(tmp)));
883 if (needqueue && ast_channel_internal_alertpipe_init(tmp)) {
884 return ast_channel_unref(tmp);
887 /* Always watch the alertpipe */
888 ast_channel_set_fd(tmp, AST_ALERT_FD, ast_channel_internal_alert_readfd(tmp));
889 /* And timing pipe */
890 ast_channel_set_fd(tmp, AST_TIMING_FD, ast_channel_timingfd(tmp));
893 ast_channel_state_set(tmp, state);
894 ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
896 ast_channel_streamid_set(tmp, -1);
898 ast_channel_fin_set(tmp, global_fin);
899 ast_channel_fout_set(tmp, global_fout);
902 ast_channel_creationtime_set(tmp, &now);
904 ast_channel_internal_setup_topics(tmp);
906 if (!ast_strlen_zero(name_fmt)) {
907 char *slash, *slash2;
908 /* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call.
909 * And they all use slightly different formats for their name string.
910 * This means, to set the name here, we have to accept variable args, and call the string_field_build from here.
911 * This means, that the stringfields must have a routine that takes the va_lists directly, and
912 * uses them to build the string, instead of forming the va_lists internally from the vararg ... list.
913 * This new function was written so this can be accomplished.
915 ast_channel_name_build_va(tmp, name_fmt, ap);
916 tech = ast_strdupa(ast_channel_name(tmp));
917 if ((slash = strchr(tech, '/'))) {
918 if ((slash2 = strchr(slash + 1, '/'))) {
926 * Start the string with '-' so it becomes an empty string
929 ast_channel_name_set(tmp, "-**Unknown**");
932 if (amaflag != AST_AMA_NONE) {
933 ast_channel_amaflags_set(tmp, amaflag);
935 ast_channel_amaflags_set(tmp, DEFAULT_AMA_FLAGS);
938 if (!ast_strlen_zero(acctcode)) {
939 ast_channel_accountcode_set(tmp, acctcode);
941 ast_channel_language_set(tmp, ast_defaultlanguage);
943 ast_channel_context_set(tmp, S_OR(context, "default"));
944 ast_channel_exten_set(tmp, S_OR(exten, "s"));
945 ast_channel_priority_set(tmp, 1);
947 headp = ast_channel_varshead(tmp);
948 AST_LIST_HEAD_INIT_NOLOCK(headp);
950 ast_pbx_hangup_handler_init(tmp);
951 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
952 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_autochans(tmp));
954 channel_tech = ast_get_channel_tech(tech);
955 if (!channel_tech && !ast_strlen_zero(tech2)) {
956 channel_tech = ast_get_channel_tech(tech2);
959 ast_channel_tech_set(tmp, channel_tech);
961 ast_channel_tech_set(tmp, &null_tech);
964 ast_channel_internal_finalize(tmp);
966 ast_atomic_fetchadd_int(&chancount, +1);
968 /* You might scream "locking inversion" at seeing this but it is actually perfectly fine.
969 * Since the channel was just created nothing can know about it yet or even acquire it.
971 ast_channel_lock(tmp);
973 ao2_link(channels, tmp);
976 ast_endpoint_add_channel(endpoint, tmp);
980 * And now, since the channel structure is built, and has its name, let
981 * the world know of its existance
983 ast_channel_stage_snapshot_done(tmp);
987 struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
988 const char *cid_name, const char *acctcode,
989 const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
990 const struct ast_channel *requestor, enum ama_flags amaflag,
991 struct ast_endpoint *endpoint,
992 const char *file, int line, const char *function,
993 const char *name_fmt, ...)
996 struct ast_channel *result;
998 va_start(ap, name_fmt);
999 result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
1000 assignedids, requestor, amaflag, endpoint, file, line, function, name_fmt, ap);
1006 /* only do the minimum amount of work needed here to make a channel
1007 * structure that can be used to expand channel vars */
1008 #if defined(REF_DEBUG) || defined(__AST_DEBUG_MALLOC)
1009 struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
1011 struct ast_channel *ast_dummy_channel_alloc(void)
1014 struct ast_channel *tmp;
1015 struct varshead *headp;
1017 if (!(tmp = ast_channel_internal_alloc(ast_dummy_channel_destructor, NULL, NULL))) {
1018 /* Dummy channel structure allocation failure. */
1022 ast_pbx_hangup_handler_init(tmp);
1023 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
1026 * Init file descriptors to unopened state just in case
1027 * autoservice is called on the channel or something tries to
1028 * read a frame from it.
1030 ast_channel_timingfd_set(tmp, -1);
1031 ast_channel_internal_alertpipe_clear(tmp);
1032 ast_channel_internal_fd_clear_all(tmp);
1034 ast_channel_epfd_set(tmp, -1);
1037 ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
1039 ast_channel_internal_setup_topics(tmp);
1041 headp = ast_channel_varshead(tmp);
1042 AST_LIST_HEAD_INIT_NOLOCK(headp);
1047 static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
1049 struct ast_frame *f;
1050 struct ast_frame *cur;
1051 unsigned int new_frames = 0;
1052 unsigned int new_voice_frames = 0;
1053 unsigned int queued_frames = 0;
1054 unsigned int queued_voice_frames = 0;
1055 AST_LIST_HEAD_NOLOCK(,ast_frame) frames;
1057 ast_channel_lock(chan);
1060 * Check the last frame on the queue if we are queuing the new
1063 cur = AST_LIST_LAST(ast_channel_readq(chan));
1064 if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
1065 switch (cur->subclass.integer) {
1066 case AST_CONTROL_END_OF_Q:
1067 if (fin->frametype == AST_FRAME_CONTROL
1068 && fin->subclass.integer == AST_CONTROL_HANGUP) {
1070 * Destroy the end-of-Q marker frame so we can queue the hangup
1071 * frame in its place.
1073 AST_LIST_REMOVE(ast_channel_readq(chan), cur, frame_list);
1077 * This has degenerated to a normal queue append anyway. Since
1078 * we just destroyed the last frame in the queue we must make
1079 * sure that "after" is NULL or bad things will happen.
1085 case AST_CONTROL_HANGUP:
1086 /* Don't queue anything. */
1087 ast_channel_unlock(chan);
1094 /* Build copies of all the new frames and count them */
1095 AST_LIST_HEAD_INIT_NOLOCK(&frames);
1096 for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
1097 if (!(f = ast_frdup(cur))) {
1098 if (AST_LIST_FIRST(&frames)) {
1099 ast_frfree(AST_LIST_FIRST(&frames));
1101 ast_channel_unlock(chan);
1105 AST_LIST_INSERT_TAIL(&frames, f, frame_list);
1107 if (f->frametype == AST_FRAME_VOICE) {
1112 /* Count how many frames exist on the queue */
1113 AST_LIST_TRAVERSE(ast_channel_readq(chan), cur, frame_list) {
1115 if (cur->frametype == AST_FRAME_VOICE) {
1116 queued_voice_frames++;
1120 if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
1122 ast_log(LOG_WARNING, "Exceptionally long %squeue length queuing to %s\n", queued_frames + new_frames > 128 ? "" : "voice ", ast_channel_name(chan));
1123 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), cur, frame_list) {
1124 /* Save the most recent frame */
1125 if (!AST_LIST_NEXT(cur, frame_list)) {
1127 } else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
1131 AST_LIST_REMOVE_CURRENT(frame_list);
1135 AST_LIST_TRAVERSE_SAFE_END;
1139 AST_LIST_INSERT_LIST_AFTER(ast_channel_readq(chan), &frames, after, frame_list);
1142 AST_LIST_APPEND_LIST(&frames, ast_channel_readq(chan), frame_list);
1143 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_readq(chan));
1145 AST_LIST_APPEND_LIST(ast_channel_readq(chan), &frames, frame_list);
1148 if (ast_channel_alert_writable(chan)) {
1149 if (ast_channel_alert_write(chan)) {
1150 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %u): %s!\n",
1151 ast_channel_name(chan), queued_frames, strerror(errno));
1153 } else if (ast_channel_timingfd(chan) > -1) {
1154 ast_timer_enable_continuous(ast_channel_timer(chan));
1155 } else if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
1156 pthread_kill(ast_channel_blocker(chan), SIGURG);
1159 ast_channel_unlock(chan);
1164 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
1166 return __ast_queue_frame(chan, fin, 0, NULL);
1169 int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
1171 return __ast_queue_frame(chan, fin, 1, NULL);
1174 /*! \brief Queue a hangup frame for channel */
1175 int ast_queue_hangup(struct ast_channel *chan)
1177 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1180 /* Yeah, let's not change a lock-critical value without locking */
1181 ast_channel_lock(chan);
1182 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1183 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), NULL);
1185 res = ast_queue_frame(chan, &f);
1186 ast_channel_unlock(chan);
1190 /*! \brief Queue a hangup frame for channel */
1191 int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
1193 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1194 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1198 f.data.uint32 = cause;
1201 /* Yeah, let's not change a lock-critical value without locking */
1202 ast_channel_lock(chan);
1203 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1205 f.data.uint32 = ast_channel_hangupcause(chan);
1207 blob = ast_json_pack("{s: i}",
1209 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
1211 res = ast_queue_frame(chan, &f);
1212 ast_channel_unlock(chan);
1216 int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
1218 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1219 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HOLD };
1222 if (!ast_strlen_zero(musicclass)) {
1223 f.data.ptr = (void *) musicclass;
1224 f.datalen = strlen(musicclass) + 1;
1226 blob = ast_json_pack("{s: s}",
1227 "musicclass", musicclass);
1230 ast_channel_publish_cached_blob(chan, ast_channel_hold_type(), blob);
1232 res = ast_queue_frame(chan, &f);
1237 int ast_queue_unhold(struct ast_channel *chan)
1239 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_UNHOLD };
1242 ast_channel_publish_cached_blob(chan, ast_channel_unhold_type(), NULL);
1244 res = ast_queue_frame(chan, &f);
1249 /*! \brief Queue a control frame */
1250 int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
1252 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
1253 return ast_queue_frame(chan, &f);
1256 /*! \brief Queue a control frame with payload */
1257 int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control,
1258 const void *data, size_t datalen)
1260 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
1261 return ast_queue_frame(chan, &f);
1264 /*! \brief Set defer DTMF flag on channel */
1265 int ast_channel_defer_dtmf(struct ast_channel *chan)
1270 pre = ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1271 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1276 /*! \brief Unset defer DTMF flag on channel */
1277 void ast_channel_undefer_dtmf(struct ast_channel *chan)
1280 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1283 struct ast_channel *ast_channel_callback(ao2_callback_data_fn *cb_fn, void *arg,
1284 void *data, int ao2_flags)
1286 return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
1289 static int ast_channel_by_name_cb(void *obj, void *arg, void *data, int flags)
1291 struct ast_channel *chan = obj;
1292 const char *name = arg;
1293 size_t name_len = *(size_t *) data;
1294 int ret = CMP_MATCH;
1296 if (ast_strlen_zero(name)) {
1297 ast_log(LOG_ERROR, "BUG! Must supply a channel name or partial name to match!\n");
1301 ast_channel_lock(chan);
1302 if ((!name_len && strcasecmp(ast_channel_name(chan), name))
1303 || (name_len && strncasecmp(ast_channel_name(chan), name, name_len))) {
1304 ret = 0; /* name match failed, keep looking */
1306 ast_channel_unlock(chan);
1311 static int ast_channel_by_exten_cb(void *obj, void *arg, void *data, int flags)
1313 struct ast_channel *chan = obj;
1314 char *context = arg;
1316 int ret = CMP_MATCH;
1318 if (ast_strlen_zero(exten) || ast_strlen_zero(context)) {
1319 ast_log(LOG_ERROR, "BUG! Must have a context and extension to match!\n");
1323 ast_channel_lock(chan);
1324 if (strcasecmp(ast_channel_context(chan), context) && strcasecmp(ast_channel_macrocontext(chan), context)) {
1325 ret = 0; /* Context match failed, continue */
1326 } else if (strcasecmp(ast_channel_exten(chan), exten) && strcasecmp(ast_channel_macroexten(chan), exten)) {
1327 ret = 0; /* Extension match failed, continue */
1329 ast_channel_unlock(chan);
1334 static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags)
1336 struct ast_channel *chan = obj;
1337 char *uniqueid = arg;
1338 size_t id_len = *(size_t *) data;
1339 int ret = CMP_MATCH;
1341 if (ast_strlen_zero(uniqueid)) {
1342 ast_log(LOG_ERROR, "BUG! Must supply a uniqueid or partial uniqueid to match!\n");
1346 ast_channel_lock(chan);
1347 if ((!id_len && strcasecmp(ast_channel_uniqueid(chan), uniqueid))
1348 || (id_len && strncasecmp(ast_channel_uniqueid(chan), uniqueid, id_len))) {
1349 ret = 0; /* uniqueid match failed, keep looking */
1351 ast_channel_unlock(chan);
1356 struct ast_channel_iterator {
1357 /* storage for non-dynamically allocated iterator */
1358 struct ao2_iterator simple_iterator;
1359 /* pointer to the actual iterator (simple_iterator or a dynamically
1360 * allocated iterator)
1362 struct ao2_iterator *active_iterator;
1365 struct ast_channel_iterator *ast_channel_iterator_destroy(struct ast_channel_iterator *i)
1367 ao2_iterator_destroy(i->active_iterator);
1373 struct ast_channel_iterator *ast_channel_iterator_by_exten_new(const char *exten, const char *context)
1375 struct ast_channel_iterator *i;
1376 char *l_exten = (char *) exten;
1377 char *l_context = (char *) context;
1379 if (!(i = ast_calloc(1, sizeof(*i)))) {
1383 i->active_iterator = (void *) ast_channel_callback(ast_channel_by_exten_cb,
1384 l_context, l_exten, OBJ_MULTIPLE);
1385 if (!i->active_iterator) {
1393 struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, size_t name_len)
1395 struct ast_channel_iterator *i;
1396 char *l_name = (char *) name;
1398 if (!(i = ast_calloc(1, sizeof(*i)))) {
1402 i->active_iterator = (void *) ast_channel_callback(ast_channel_by_name_cb,
1404 OBJ_MULTIPLE | (name_len == 0 /* match the whole word, so optimize */ ? OBJ_KEY : 0));
1405 if (!i->active_iterator) {
1413 struct ast_channel_iterator *ast_channel_iterator_all_new(void)
1415 struct ast_channel_iterator *i;
1417 if (!(i = ast_calloc(1, sizeof(*i)))) {
1421 i->simple_iterator = ao2_iterator_init(channels, 0);
1422 i->active_iterator = &i->simple_iterator;
1427 struct ast_channel *ast_channel_iterator_next(struct ast_channel_iterator *i)
1429 return ao2_iterator_next(i->active_iterator);
1432 /* Legacy function, not currently used for lookups, but we need a cmp_fn */
1433 static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
1435 ast_log(LOG_ERROR, "BUG! Should never be called!\n");
1439 struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
1441 struct ast_channel *chan;
1442 char *l_name = (char *) name;
1444 chan = ast_channel_callback(ast_channel_by_name_cb, l_name, &name_len,
1445 (name_len == 0) /* optimize if it is a complete name match */ ? OBJ_KEY : 0);
1450 if (ast_strlen_zero(l_name)) {
1451 /* We didn't have a name to search for so quit. */
1455 /* Now try a search for uniqueid. */
1456 return ast_channel_callback(ast_channel_by_uniqueid_cb, l_name, &name_len, 0);
1459 struct ast_channel *ast_channel_get_by_name(const char *name)
1461 return ast_channel_get_by_name_prefix(name, 0);
1464 struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
1466 char *l_exten = (char *) exten;
1467 char *l_context = (char *) context;
1469 return ast_channel_callback(ast_channel_by_exten_cb, l_context, l_exten, 0);
1472 int ast_is_deferrable_frame(const struct ast_frame *frame)
1474 /* Do not add a default entry in this switch statement. Each new
1475 * frame type should be addressed directly as to whether it should
1476 * be queued up or not.
1478 switch (frame->frametype) {
1479 case AST_FRAME_BRIDGE_ACTION:
1480 case AST_FRAME_BRIDGE_ACTION_SYNC:
1481 case AST_FRAME_CONTROL:
1482 case AST_FRAME_TEXT:
1483 case AST_FRAME_IMAGE:
1484 case AST_FRAME_HTML:
1487 case AST_FRAME_DTMF_END:
1488 case AST_FRAME_DTMF_BEGIN:
1489 case AST_FRAME_VOICE:
1490 case AST_FRAME_VIDEO:
1491 case AST_FRAME_NULL:
1494 case AST_FRAME_MODEM:
1500 /*! \brief Wait, look for hangups and condition arg */
1501 int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data)
1503 struct ast_frame *f;
1504 struct ast_silence_generator *silgen = NULL;
1506 struct timeval start;
1508 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
1510 AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
1512 /* If no other generator is present, start silencegen while waiting */
1513 if (ast_opt_transmit_silence && !ast_channel_generatordata(chan)) {
1514 silgen = ast_channel_start_silence_generator(chan);
1517 start = ast_tvnow();
1518 while ((ms = ast_remaining_ms(start, timeout_ms))) {
1519 struct ast_frame *dup_f = NULL;
1521 if (cond && ((*cond)(data) == 0)) {
1524 ms = ast_waitfor(chan, ms);
1536 if (!ast_is_deferrable_frame(f)) {
1541 if ((dup_f = ast_frisolate(f))) {
1545 AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
1550 /* stop silgen if present */
1552 ast_channel_stop_silence_generator(chan, silgen);
1555 /* We need to free all the deferred frames, but we only need to
1556 * queue the deferred frames if there was no error and no
1557 * hangup was received
1559 ast_channel_lock(chan);
1560 while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
1562 ast_queue_frame_head(chan, f);
1566 ast_channel_unlock(chan);
1571 /*! \brief Wait, look for hangups */
1572 int ast_safe_sleep(struct ast_channel *chan, int ms)
1574 return ast_safe_sleep_conditional(chan, ms, NULL, NULL);
1577 struct ast_channel *ast_channel_release(struct ast_channel *chan)
1579 /* Safe, even if already unlinked. */
1580 ao2_unlink(channels, chan);
1581 return ast_channel_unref(chan);
1584 void ast_party_name_init(struct ast_party_name *init)
1587 init->char_set = AST_PARTY_CHAR_SET_ISO8859_1;
1588 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1592 void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
1595 /* Don't copy to self */
1599 ast_free(dest->str);
1600 dest->str = ast_strdup(src->str);
1601 dest->char_set = src->char_set;
1602 dest->presentation = src->presentation;
1603 dest->valid = src->valid;
1606 void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
1609 init->char_set = guide->char_set;
1610 init->presentation = guide->presentation;
1611 init->valid = guide->valid;
1614 void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
1617 /* Don't set to self */
1621 if (src->str && src->str != dest->str) {
1622 ast_free(dest->str);
1623 dest->str = ast_strdup(src->str);
1626 dest->char_set = src->char_set;
1627 dest->presentation = src->presentation;
1628 dest->valid = src->valid;
1631 void ast_party_name_free(struct ast_party_name *doomed)
1633 ast_free(doomed->str);
1637 void ast_party_number_init(struct ast_party_number *init)
1640 init->plan = 0;/* Unknown */
1641 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1645 void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
1648 /* Don't copy to self */
1652 ast_free(dest->str);
1653 dest->str = ast_strdup(src->str);
1654 dest->plan = src->plan;
1655 dest->presentation = src->presentation;
1656 dest->valid = src->valid;
1659 void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
1662 init->plan = guide->plan;
1663 init->presentation = guide->presentation;
1664 init->valid = guide->valid;
1667 void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
1670 /* Don't set to self */
1674 if (src->str && src->str != dest->str) {
1675 ast_free(dest->str);
1676 dest->str = ast_strdup(src->str);
1679 dest->plan = src->plan;
1680 dest->presentation = src->presentation;
1681 dest->valid = src->valid;
1684 void ast_party_number_free(struct ast_party_number *doomed)
1686 ast_free(doomed->str);
1690 void ast_party_subaddress_init(struct ast_party_subaddress *init)
1694 init->odd_even_indicator = 0;
1698 void ast_party_subaddress_copy(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1701 /* Don't copy to self */
1705 ast_free(dest->str);
1706 dest->str = ast_strdup(src->str);
1707 dest->type = src->type;
1708 dest->odd_even_indicator = src->odd_even_indicator;
1709 dest->valid = src->valid;
1712 void ast_party_subaddress_set_init(struct ast_party_subaddress *init, const struct ast_party_subaddress *guide)
1715 init->type = guide->type;
1716 init->odd_even_indicator = guide->odd_even_indicator;
1717 init->valid = guide->valid;
1720 void ast_party_subaddress_set(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1723 /* Don't set to self */
1727 if (src->str && src->str != dest->str) {
1728 ast_free(dest->str);
1729 dest->str = ast_strdup(src->str);
1732 dest->type = src->type;
1733 dest->odd_even_indicator = src->odd_even_indicator;
1734 dest->valid = src->valid;
1737 void ast_party_subaddress_free(struct ast_party_subaddress *doomed)
1739 ast_free(doomed->str);
1743 void ast_set_party_id_all(struct ast_set_party_id *update_id)
1745 update_id->name = 1;
1746 update_id->number = 1;
1747 update_id->subaddress = 1;
1750 void ast_party_id_init(struct ast_party_id *init)
1752 ast_party_name_init(&init->name);
1753 ast_party_number_init(&init->number);
1754 ast_party_subaddress_init(&init->subaddress);
1758 void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
1761 /* Don't copy to self */
1765 ast_party_name_copy(&dest->name, &src->name);
1766 ast_party_number_copy(&dest->number, &src->number);
1767 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1769 ast_free(dest->tag);
1770 dest->tag = ast_strdup(src->tag);
1773 void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
1775 ast_party_name_set_init(&init->name, &guide->name);
1776 ast_party_number_set_init(&init->number, &guide->number);
1777 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
1781 void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
1784 /* Don't set to self */
1788 if (!update || update->name) {
1789 ast_party_name_set(&dest->name, &src->name);
1791 if (!update || update->number) {
1792 ast_party_number_set(&dest->number, &src->number);
1794 if (!update || update->subaddress) {
1795 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
1798 if (src->tag && src->tag != dest->tag) {
1799 ast_free(dest->tag);
1800 dest->tag = ast_strdup(src->tag);
1804 void ast_party_id_free(struct ast_party_id *doomed)
1806 ast_party_name_free(&doomed->name);
1807 ast_party_number_free(&doomed->number);
1808 ast_party_subaddress_free(&doomed->subaddress);
1810 ast_free(doomed->tag);
1814 int ast_party_id_presentation(const struct ast_party_id *id)
1816 int number_priority;
1818 int number_screening;
1822 /* Determine name presentation priority. */
1823 if (!id->name.valid) {
1824 name_value = AST_PRES_UNAVAILABLE;
1827 name_value = id->name.presentation & AST_PRES_RESTRICTION;
1828 switch (name_value) {
1829 case AST_PRES_RESTRICTED:
1832 case AST_PRES_ALLOWED:
1835 case AST_PRES_UNAVAILABLE:
1839 name_value = AST_PRES_UNAVAILABLE;
1845 /* Determine number presentation priority. */
1846 if (!id->number.valid) {
1847 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1848 number_value = AST_PRES_UNAVAILABLE;
1849 number_priority = 3;
1851 number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
1852 number_value = id->number.presentation & AST_PRES_RESTRICTION;
1853 switch (number_value) {
1854 case AST_PRES_RESTRICTED:
1855 number_priority = 0;
1857 case AST_PRES_ALLOWED:
1858 number_priority = 1;
1860 case AST_PRES_UNAVAILABLE:
1861 number_priority = 2;
1864 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1865 number_value = AST_PRES_UNAVAILABLE;
1866 number_priority = 3;
1871 /* Select the wining presentation value. */
1872 if (name_priority < number_priority) {
1873 number_value = name_value;
1875 if (number_value == AST_PRES_UNAVAILABLE) {
1876 return AST_PRES_NUMBER_NOT_AVAILABLE;
1879 return number_value | number_screening;
1882 void ast_party_id_invalidate(struct ast_party_id *id)
1885 id->number.valid = 0;
1886 id->subaddress.valid = 0;
1889 void ast_party_id_reset(struct ast_party_id *id)
1891 ast_party_id_free(id);
1892 ast_party_id_init(id);
1895 struct ast_party_id ast_party_id_merge(struct ast_party_id *base, struct ast_party_id *overlay)
1897 struct ast_party_id merged;
1900 if (overlay->name.valid) {
1901 merged.name = overlay->name;
1903 if (overlay->number.valid) {
1904 merged.number = overlay->number;
1906 if (overlay->subaddress.valid) {
1907 merged.subaddress = overlay->subaddress;
1909 /* Note the actual structure is returned and not a pointer to it! */
1913 void ast_party_id_merge_copy(struct ast_party_id *dest, struct ast_party_id *base, struct ast_party_id *overlay)
1915 struct ast_party_id merged;
1917 merged = ast_party_id_merge(base, overlay);
1918 ast_party_id_copy(dest, &merged);
1921 void ast_party_dialed_init(struct ast_party_dialed *init)
1923 init->number.str = NULL;
1924 init->number.plan = 0;/* Unknown */
1925 ast_party_subaddress_init(&init->subaddress);
1926 init->transit_network_select = 0;
1929 void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1932 /* Don't copy to self */
1936 ast_free(dest->number.str);
1937 dest->number.str = ast_strdup(src->number.str);
1938 dest->number.plan = src->number.plan;
1939 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1940 dest->transit_network_select = src->transit_network_select;
1943 void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
1945 init->number.str = NULL;
1946 init->number.plan = guide->number.plan;
1947 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
1948 init->transit_network_select = guide->transit_network_select;
1951 void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1953 if (src->number.str && src->number.str != dest->number.str) {
1954 ast_free(dest->number.str);
1955 dest->number.str = ast_strdup(src->number.str);
1957 dest->number.plan = src->number.plan;
1959 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
1961 dest->transit_network_select = src->transit_network_select;
1964 void ast_party_dialed_free(struct ast_party_dialed *doomed)
1966 ast_free(doomed->number.str);
1967 doomed->number.str = NULL;
1968 ast_party_subaddress_free(&doomed->subaddress);
1971 void ast_party_caller_init(struct ast_party_caller *init)
1973 ast_party_id_init(&init->id);
1974 ast_party_id_init(&init->ani);
1975 ast_party_id_init(&init->priv);
1979 void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
1982 /* Don't copy to self */
1986 ast_party_id_copy(&dest->id, &src->id);
1987 ast_party_id_copy(&dest->ani, &src->ani);
1988 ast_party_id_copy(&dest->priv, &src->priv);
1989 dest->ani2 = src->ani2;
1992 void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
1994 ast_party_id_set_init(&init->id, &guide->id);
1995 ast_party_id_set_init(&init->ani, &guide->ani);
1996 ast_party_id_set_init(&init->priv, &guide->priv);
1997 init->ani2 = guide->ani2;
2000 void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
2002 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2003 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2004 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2005 dest->ani2 = src->ani2;
2008 void ast_party_caller_free(struct ast_party_caller *doomed)
2010 ast_party_id_free(&doomed->id);
2011 ast_party_id_free(&doomed->ani);
2012 ast_party_id_free(&doomed->priv);
2015 void ast_party_connected_line_init(struct ast_party_connected_line *init)
2017 ast_party_id_init(&init->id);
2018 ast_party_id_init(&init->ani);
2019 ast_party_id_init(&init->priv);
2021 init->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2024 void ast_party_connected_line_copy(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
2027 /* Don't copy to self */
2031 ast_party_id_copy(&dest->id, &src->id);
2032 ast_party_id_copy(&dest->ani, &src->ani);
2033 ast_party_id_copy(&dest->priv, &src->priv);
2034 dest->ani2 = src->ani2;
2035 dest->source = src->source;
2038 void ast_party_connected_line_set_init(struct ast_party_connected_line *init, const struct ast_party_connected_line *guide)
2040 ast_party_id_set_init(&init->id, &guide->id);
2041 ast_party_id_set_init(&init->ani, &guide->ani);
2042 ast_party_id_set_init(&init->priv, &guide->priv);
2043 init->ani2 = guide->ani2;
2044 init->source = guide->source;
2047 void ast_party_connected_line_set(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src, const struct ast_set_party_connected_line *update)
2049 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2050 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2051 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2052 dest->ani2 = src->ani2;
2053 dest->source = src->source;
2056 void ast_party_connected_line_collect_caller(struct ast_party_connected_line *connected, struct ast_party_caller *caller)
2058 connected->id = caller->id;
2059 connected->ani = caller->ani;
2060 connected->priv = caller->priv;
2061 connected->ani2 = caller->ani2;
2062 connected->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2065 void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
2067 ast_party_id_free(&doomed->id);
2068 ast_party_id_free(&doomed->ani);
2069 ast_party_id_free(&doomed->priv);
2072 void ast_party_redirecting_reason_init(struct ast_party_redirecting_reason *init)
2075 init->code = AST_REDIRECTING_REASON_UNKNOWN;
2078 void ast_party_redirecting_reason_copy(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2084 ast_free(dest->str);
2085 dest->str = ast_strdup(src->str);
2086 dest->code = src->code;
2089 void ast_party_redirecting_reason_set_init(struct ast_party_redirecting_reason *init, const struct ast_party_redirecting_reason *guide)
2092 init->code = guide->code;
2095 void ast_party_redirecting_reason_set(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2101 if (src->str && src->str != dest->str) {
2102 ast_free(dest->str);
2103 dest->str = ast_strdup(src->str);
2106 dest->code = src->code;
2109 void ast_party_redirecting_reason_free(struct ast_party_redirecting_reason *doomed)
2111 ast_free(doomed->str);
2115 void ast_party_redirecting_init(struct ast_party_redirecting *init)
2117 ast_party_id_init(&init->orig);
2118 ast_party_id_init(&init->from);
2119 ast_party_id_init(&init->to);
2120 ast_party_id_init(&init->priv_orig);
2121 ast_party_id_init(&init->priv_from);
2122 ast_party_id_init(&init->priv_to);
2123 ast_party_redirecting_reason_init(&init->reason);
2124 ast_party_redirecting_reason_init(&init->orig_reason);
2128 void ast_party_redirecting_copy(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src)
2131 /* Don't copy to self */
2135 ast_party_id_copy(&dest->orig, &src->orig);
2136 ast_party_id_copy(&dest->from, &src->from);
2137 ast_party_id_copy(&dest->to, &src->to);
2138 ast_party_id_copy(&dest->priv_orig, &src->priv_orig);
2139 ast_party_id_copy(&dest->priv_from, &src->priv_from);
2140 ast_party_id_copy(&dest->priv_to, &src->priv_to);
2141 ast_party_redirecting_reason_copy(&dest->reason, &src->reason);
2142 ast_party_redirecting_reason_copy(&dest->orig_reason, &src->orig_reason);
2143 dest->count = src->count;
2146 void ast_party_redirecting_set_init(struct ast_party_redirecting *init, const struct ast_party_redirecting *guide)
2148 ast_party_id_set_init(&init->orig, &guide->orig);
2149 ast_party_id_set_init(&init->from, &guide->from);
2150 ast_party_id_set_init(&init->to, &guide->to);
2151 ast_party_id_set_init(&init->priv_orig, &guide->priv_orig);
2152 ast_party_id_set_init(&init->priv_from, &guide->priv_from);
2153 ast_party_id_set_init(&init->priv_to, &guide->priv_to);
2154 ast_party_redirecting_reason_set_init(&init->reason, &guide->reason);
2155 ast_party_redirecting_reason_set_init(&init->orig_reason, &guide->orig_reason);
2156 init->count = guide->count;
2159 void ast_party_redirecting_set(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src, const struct ast_set_party_redirecting *update)
2161 ast_party_id_set(&dest->orig, &src->orig, update ? &update->orig : NULL);
2162 ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
2163 ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
2164 ast_party_id_set(&dest->priv_orig, &src->priv_orig, update ? &update->priv_orig : NULL);
2165 ast_party_id_set(&dest->priv_from, &src->priv_from, update ? &update->priv_from : NULL);
2166 ast_party_id_set(&dest->priv_to, &src->priv_to, update ? &update->priv_to : NULL);
2167 ast_party_redirecting_reason_set(&dest->reason, &src->reason);
2168 ast_party_redirecting_reason_set(&dest->orig_reason, &src->orig_reason);
2169 dest->count = src->count;
2172 void ast_party_redirecting_free(struct ast_party_redirecting *doomed)
2174 ast_party_id_free(&doomed->orig);
2175 ast_party_id_free(&doomed->from);
2176 ast_party_id_free(&doomed->to);
2177 ast_party_id_free(&doomed->priv_orig);
2178 ast_party_id_free(&doomed->priv_from);
2179 ast_party_id_free(&doomed->priv_to);
2180 ast_party_redirecting_reason_free(&doomed->reason);
2181 ast_party_redirecting_reason_free(&doomed->orig_reason);
2184 /*! \brief Free a channel structure */
2185 static void ast_channel_destructor(void *obj)
2187 struct ast_channel *chan = obj;
2191 struct ast_var_t *vardata;
2192 struct ast_frame *f;
2193 struct varshead *headp;
2194 struct ast_datastore *datastore;
2195 char device_name[AST_CHANNEL_NAME];
2196 struct ast_callid *callid;
2198 /* Stop monitoring */
2199 if (ast_channel_monitor(chan)) {
2200 ast_channel_monitor(chan)->stop(chan, 0);
2203 /* If there is native format music-on-hold state, free it */
2204 if (ast_channel_music_state(chan)) {
2205 ast_moh_cleanup(chan);
2208 ast_pbx_hangup_handler_destroy(chan);
2210 /* Things that may possibly raise Stasis messages shouldn't occur after this point */
2211 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEAD);
2213 if (ast_channel_internal_is_finalized(chan)) {
2214 /* A channel snapshot should not be in the process of being staged now. */
2215 ast_assert(!ast_test_flag(ast_channel_flags(chan), AST_FLAG_SNAPSHOT_STAGE));
2217 ast_channel_lock(chan);
2218 ast_channel_publish_snapshot(chan);
2219 ast_channel_unlock(chan);
2220 publish_cache_clear(chan);
2223 ast_channel_lock(chan);
2225 /* Get rid of each of the data stores on the channel */
2226 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry)))
2227 /* Free the data store */
2228 ast_datastore_free(datastore);
2230 /* While the channel is locked, take the reference to its callid while we tear down the call. */
2231 callid = ast_channel_callid(chan);
2232 ast_channel_callid_cleanup(chan);
2234 ast_channel_unlock(chan);
2236 /* Lock and unlock the channel just to be sure nobody has it locked still
2237 due to a reference that was stored in a datastore. (i.e. app_chanspy) */
2238 ast_channel_lock(chan);
2239 ast_channel_unlock(chan);
2241 if (ast_channel_tech_pvt(chan)) {
2242 ast_log_callid(LOG_WARNING, callid, "Channel '%s' may not have been hung up properly\n", ast_channel_name(chan));
2243 ast_free(ast_channel_tech_pvt(chan));
2246 if (ast_channel_sched(chan)) {
2247 ast_sched_context_destroy(ast_channel_sched(chan));
2250 if (ast_channel_internal_is_finalized(chan)) {
2253 ast_copy_string(device_name, ast_channel_name(chan), sizeof(device_name));
2254 if ((dashptr = strrchr(device_name, '-'))) {
2258 device_name[0] = '\0';
2261 /* Free translators */
2262 if (ast_channel_readtrans(chan))
2263 ast_translator_free_path(ast_channel_readtrans(chan));
2264 if (ast_channel_writetrans(chan))
2265 ast_translator_free_path(ast_channel_writetrans(chan));
2266 if (ast_channel_pbx(chan))
2267 ast_log_callid(LOG_WARNING, callid, "PBX may not have been terminated properly on '%s'\n", ast_channel_name(chan));
2270 ast_channel_set_oldwriteformat(chan, NULL);
2271 ast_channel_set_rawreadformat(chan, NULL);
2272 ast_channel_set_rawwriteformat(chan, NULL);
2273 ast_channel_set_readformat(chan, NULL);
2274 ast_channel_set_writeformat(chan, NULL);
2276 ast_party_dialed_free(ast_channel_dialed(chan));
2277 ast_party_caller_free(ast_channel_caller(chan));
2278 ast_party_connected_line_free(ast_channel_connected(chan));
2279 ast_party_connected_line_free(ast_channel_connected_indicated(chan));
2280 ast_party_redirecting_free(ast_channel_redirecting(chan));
2282 /* Close pipes if appropriate */
2283 ast_channel_internal_alertpipe_close(chan);
2284 if (ast_channel_timer(chan)) {
2285 ast_timer_close(ast_channel_timer(chan));
2286 ast_channel_timer_set(chan, NULL);
2289 for (i = 0; i < AST_MAX_FDS; i++) {
2290 if (ast_channel_internal_epfd_data(chan, i)) {
2291 ast_free(ast_channel_internal_epfd_data(chan, i));
2294 close(ast_channel_epfd(chan));
2296 while ((f = AST_LIST_REMOVE_HEAD(ast_channel_readq(chan), frame_list)))
2299 /* loop over the variables list, freeing all data and deleting list items */
2300 /* no need to lock the list, as the channel is already locked */
2301 headp = ast_channel_varshead(chan);
2302 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2303 ast_var_delete(vardata);
2305 ast_app_group_discard(chan);
2307 /* Destroy the jitterbuffer */
2308 ast_jb_destroy(chan);
2310 if (ast_channel_cdr(chan)) {
2311 ast_cdr_free(ast_channel_cdr(chan));
2312 ast_channel_cdr_set(chan, NULL);
2315 if (ast_channel_zone(chan)) {
2316 ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan)));
2319 ast_channel_internal_cleanup(chan);
2321 if (device_name[0]) {
2323 * We have a device name to notify of a new state.
2325 * Queue an unknown state, because, while we know that this particular
2326 * instance is dead, we don't know the state of all other possible
2329 ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE) ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), device_name);
2332 ast_channel_nativeformats_set(chan, NULL);
2334 ast_callid_unref(callid);
2337 ast_channel_named_callgroups_set(chan, NULL);
2338 ast_channel_named_pickupgroups_set(chan, NULL);
2340 ast_atomic_fetchadd_int(&chancount, -1);
2343 /*! \brief Free a dummy channel structure */
2344 static void ast_dummy_channel_destructor(void *obj)
2346 struct ast_channel *chan = obj;
2347 struct ast_datastore *datastore;
2348 struct ast_var_t *vardata;
2349 struct varshead *headp;
2351 ast_pbx_hangup_handler_destroy(chan);
2353 /* Get rid of each of the data stores on the channel */
2354 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) {
2355 /* Free the data store */
2356 ast_datastore_free(datastore);
2359 ast_party_dialed_free(ast_channel_dialed(chan));
2360 ast_party_caller_free(ast_channel_caller(chan));
2361 ast_party_connected_line_free(ast_channel_connected(chan));
2362 ast_party_connected_line_free(ast_channel_connected_indicated(chan));
2363 ast_party_redirecting_free(ast_channel_redirecting(chan));
2365 /* loop over the variables list, freeing all data and deleting list items */
2366 /* no need to lock the list, as the channel is already locked */
2367 headp = ast_channel_varshead(chan);
2368 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2369 ast_var_delete(vardata);
2371 if (ast_channel_cdr(chan)) {
2372 ast_cdr_free(ast_channel_cdr(chan));
2373 ast_channel_cdr_set(chan, NULL);
2376 ast_channel_internal_cleanup(chan);
2379 struct ast_datastore *ast_channel_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
2381 return ast_datastore_alloc(info, uid);
2384 int ast_channel_datastore_free(struct ast_datastore *datastore)
2386 return ast_datastore_free(datastore);
2389 int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to)
2391 struct ast_datastore *datastore = NULL, *datastore2;
2393 AST_LIST_TRAVERSE(ast_channel_datastores(from), datastore, entry) {
2394 if (datastore->inheritance > 0) {
2395 datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
2397 datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
2398 datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
2399 AST_LIST_INSERT_TAIL(ast_channel_datastores(to), datastore2, entry);
2406 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
2410 AST_LIST_INSERT_HEAD(ast_channel_datastores(chan), datastore, entry);
2415 int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
2417 return AST_LIST_REMOVE(ast_channel_datastores(chan), datastore, entry) ? 0 : -1;
2420 struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
2422 struct ast_datastore *datastore = NULL;
2427 AST_LIST_TRAVERSE(ast_channel_datastores(chan), datastore, entry) {
2428 if (datastore->info != info) {
2433 /* matched by type only */
2437 if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
2438 /* Matched by type AND uid */
2446 /*! Set the file descriptor on the channel */
2447 void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
2450 struct epoll_event ev;
2451 struct ast_epoll_data *aed = NULL;
2453 if (ast_channel_fd_isset(chan, which)) {
2454 epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_DEL, ast_channel_fd(chan, which), &ev);
2455 aed = ast_channel_internal_epfd_data(chan, which);
2458 /* If this new fd is valid, add it to the epoll */
2460 if (!aed && (!(aed = ast_calloc(1, sizeof(*aed)))))
2463 ast_channel_internal_epfd_data_set(chan, which, aed);
2467 ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2469 epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_ADD, fd, &ev);
2471 /* We don't have to keep around this epoll data structure now */
2473 ast_channel_epfd_data_set(chan, which, NULL);
2476 ast_channel_internal_fd_set(chan, which, fd);
2480 /*! Add a channel to an optimized waitfor */
2481 void ast_poll_channel_add(struct ast_channel *chan0, struct ast_channel *chan1)
2484 struct epoll_event ev;
2487 if (ast_channel_epfd(chan0) == -1)
2490 /* Iterate through the file descriptors on chan1, adding them to chan0 */
2491 for (i = 0; i < AST_MAX_FDS; i++) {
2492 if (!ast_channel_fd_isset(chan1, i)) {
2495 ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2496 ev.data.ptr = ast_channel_internal_epfd_data(chan1, i);
2497 epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_ADD, ast_channel_fd(chan1, i), &ev);
2504 /*! Delete a channel from an optimized waitfor */
2505 void ast_poll_channel_del(struct ast_channel *chan0, struct ast_channel *chan1)
2508 struct epoll_event ev;
2511 if (ast_channel_epfd(chan0) == -1)
2514 for (i = 0; i < AST_MAX_FDS; i++) {
2515 if (!ast_channel_fd_isset(chan1, i)) {
2518 epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_DEL, ast_channel_fd(chan1, i), &ev);
2525 void ast_channel_clear_softhangup(struct ast_channel *chan, int flag)
2527 ast_channel_lock(chan);
2529 ast_channel_softhangup_internal_flag_clear(chan, flag);
2531 if (!ast_channel_softhangup_internal_flag(chan)) {
2532 struct ast_frame *fr;
2534 /* If we have completely cleared the softhangup flag,
2535 * then we need to fully abort the hangup process. This requires
2536 * pulling the END_OF_Q frame out of the channel frame queue if it
2537 * still happens to be there. */
2539 fr = AST_LIST_LAST(ast_channel_readq(chan));
2540 if (fr && fr->frametype == AST_FRAME_CONTROL &&
2541 fr->subclass.integer == AST_CONTROL_END_OF_Q) {
2542 AST_LIST_REMOVE(ast_channel_readq(chan), fr, frame_list);
2547 ast_channel_unlock(chan);
2550 /*! \brief Softly hangup a channel, don't lock */
2551 int ast_softhangup_nolock(struct ast_channel *chan, int cause)
2553 ast_debug(1, "Soft-Hanging (%#04x) up channel '%s'\n", (unsigned)cause, ast_channel_name(chan));
2554 /* Inform channel driver that we need to be hung up, if it cares */
2555 ast_channel_softhangup_internal_flag_add(chan, cause);
2556 ast_queue_frame(chan, &ast_null_frame);
2557 /* Interrupt any poll call or such */
2558 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING))
2559 pthread_kill(ast_channel_blocker(chan), SIGURG);
2563 /*! \brief Softly hangup a channel, lock */
2564 int ast_softhangup(struct ast_channel *chan, int cause)
2566 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2569 ast_channel_lock(chan);
2570 res = ast_softhangup_nolock(chan, cause);
2571 blob = ast_json_pack("{s: i, s: b}",
2574 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
2575 ast_channel_unlock(chan);
2580 static void free_translation(struct ast_channel *clonechan)
2582 if (ast_channel_writetrans(clonechan)) {
2583 ast_translator_free_path(ast_channel_writetrans(clonechan));
2585 if (ast_channel_readtrans(clonechan)) {
2586 ast_translator_free_path(ast_channel_readtrans(clonechan));
2588 ast_channel_writetrans_set(clonechan, NULL);
2589 ast_channel_readtrans_set(clonechan, NULL);
2592 void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
2594 RAII_VAR(struct ast_channel *, bridge, ast_channel_bridge_peer(chan), ast_channel_cleanup);
2596 ast_channel_lock(chan);
2597 if (force || ast_strlen_zero(ast_channel_hangupsource(chan))) {
2598 ast_channel_hangupsource_set(chan, source);
2600 ast_channel_unlock(chan);
2603 ast_channel_lock(bridge);
2604 if (force || ast_strlen_zero(ast_channel_hangupsource(bridge))) {
2605 ast_channel_hangupsource_set(bridge, source);
2607 ast_channel_unlock(bridge);
2611 int ast_channel_has_audio_frame_or_monitor(struct ast_channel *chan)
2613 return ast_channel_monitor(chan)
2614 || !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
2615 || !ast_framehook_list_contains_no_active(ast_channel_framehooks(chan));
2618 int ast_channel_has_hook_requiring_audio(struct ast_channel *chan)
2620 return ast_channel_monitor(chan)
2621 || !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
2622 || !ast_framehook_list_contains_no_active_of_type(ast_channel_framehooks(chan), AST_FRAME_VOICE);
2625 static void destroy_hooks(struct ast_channel *chan)
2627 if (ast_channel_audiohooks(chan)) {
2628 ast_audiohook_detach_list(ast_channel_audiohooks(chan));
2629 ast_channel_audiohooks_set(chan, NULL);
2632 ast_framehook_list_destroy(chan);
2635 /*! \brief Hangup a channel */
2636 void ast_hangup(struct ast_channel *chan)
2638 /* Be NULL safe for RAII_VAR() usage. */
2643 ast_autoservice_stop(chan);
2645 ast_channel_lock(chan);
2647 while (ast_channel_masq(chan) || ast_channel_masqr(chan)) {
2648 CHANNEL_DEADLOCK_AVOIDANCE(chan);
2651 /* Mark as a zombie so a masquerade cannot be setup on this channel. */
2652 ast_set_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE);
2654 ast_channel_unlock(chan);
2657 * XXX if running the hangup handlers here causes problems
2658 * because the handlers take too long to execute, we could move
2659 * the meat of this function into another thread. A thread
2660 * where channels go to die.
2662 * If this is done, ast_autoservice_chan_hangup_peer() will no
2665 ast_pbx_hangup_handler_run(chan);
2666 ao2_unlink(channels, chan);
2667 ast_channel_lock(chan);
2669 destroy_hooks(chan);
2671 free_translation(chan);
2672 /* Close audio stream */
2673 if (ast_channel_stream(chan)) {
2674 ast_closestream(ast_channel_stream(chan));
2675 ast_channel_stream_set(chan, NULL);
2677 /* Close video stream */
2678 if (ast_channel_vstream(chan)) {
2679 ast_closestream(ast_channel_vstream(chan));
2680 ast_channel_vstream_set(chan, NULL);
2682 if (ast_channel_sched(chan)) {
2683 ast_sched_context_destroy(ast_channel_sched(chan));
2684 ast_channel_sched_set(chan, NULL);
2687 if (ast_channel_generatordata(chan)) { /* Clear any tone stuff remaining */
2688 if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2689 ast_channel_generator(chan)->release(chan, ast_channel_generatordata(chan));
2692 ast_channel_generatordata_set(chan, NULL);
2693 ast_channel_generator_set(chan, NULL);
2695 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
2696 ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
2697 "is blocked by thread %ld in procedure %s! Expect a failure\n",
2698 (long) pthread_self(), ast_channel_name(chan), (long)ast_channel_blocker(chan), ast_channel_blockproc(chan));
2699 ast_assert(ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING) == 0);
2702 ast_debug(1, "Hanging up channel '%s'\n", ast_channel_name(chan));
2703 if (ast_channel_tech(chan)->hangup) {
2704 ast_channel_tech(chan)->hangup(chan);
2707 ast_channel_unlock(chan);
2711 ast_channel_unref(chan);
2714 int ast_raw_answer(struct ast_channel *chan)
2717 struct timeval answertime;
2719 ast_channel_lock(chan);
2721 /* You can't answer an outbound call */
2722 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING)) {
2723 ast_channel_unlock(chan);
2727 /* Stop if we're a zombie or need a soft hangup */
2728 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
2729 ast_channel_unlock(chan);
2733 answertime = ast_tvnow();
2734 ast_channel_answertime_set(chan, &answertime);
2736 ast_channel_unlock(chan);
2738 switch (ast_channel_state(chan)) {
2739 case AST_STATE_RINGING:
2740 case AST_STATE_RING:
2741 ast_channel_lock(chan);
2742 if (ast_channel_tech(chan)->answer) {
2743 res = ast_channel_tech(chan)->answer(chan);
2745 ast_setstate(chan, AST_STATE_UP);
2746 ast_channel_unlock(chan);
2754 ast_indicate(chan, -1);
2759 int __ast_answer(struct ast_channel *chan, unsigned int delay)
2762 enum ast_channel_state old_state;
2764 old_state = ast_channel_state(chan);
2765 if ((res = ast_raw_answer(chan))) {
2769 switch (old_state) {
2770 case AST_STATE_RINGING:
2771 case AST_STATE_RING:
2772 /* wait for media to start flowing, but don't wait any longer
2773 * than 'delay' or 500 milliseconds, whichever is longer
2776 AST_LIST_HEAD_NOLOCK(, ast_frame) frames;
2777 struct ast_frame *cur;
2778 struct ast_frame *new_frame;
2779 int timeout_ms = MAX(delay, 500);
2780 unsigned int done = 0;
2781 struct timeval start;
2783 AST_LIST_HEAD_INIT_NOLOCK(&frames);
2785 start = ast_tvnow();
2787 int ms = ast_remaining_ms(start, timeout_ms);
2788 ms = ast_waitfor(chan, ms);
2790 ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", ast_channel_name(chan), strerror(errno));
2795 ast_debug(2, "Didn't receive a media frame from %s within %u ms of answering. Continuing anyway\n", ast_channel_name(chan), MAX(delay, 500));
2798 cur = ast_read(chan);
2799 if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
2800 (cur->subclass.integer == AST_CONTROL_HANGUP))) {
2805 ast_debug(2, "Hangup of channel %s detected in answer routine\n", ast_channel_name(chan));
2809 if ((new_frame = ast_frisolate(cur)) != cur) {
2813 AST_LIST_INSERT_HEAD(&frames, new_frame, frame_list);
2815 /* if a specific delay period was requested, continue
2816 * until that delay has passed. don't stop just because
2817 * incoming media has arrived.
2823 switch (new_frame->frametype) {
2824 /* all of these frametypes qualify as 'media' */
2825 case AST_FRAME_VOICE:
2826 case AST_FRAME_VIDEO:
2827 case AST_FRAME_TEXT:
2828 case AST_FRAME_DTMF_BEGIN:
2829 case AST_FRAME_DTMF_END:
2830 case AST_FRAME_IMAGE:
2831 case AST_FRAME_HTML:
2832 case AST_FRAME_MODEM:
2835 case AST_FRAME_CONTROL:
2837 case AST_FRAME_BRIDGE_ACTION:
2838 case AST_FRAME_BRIDGE_ACTION_SYNC:
2839 case AST_FRAME_NULL:
2850 ast_channel_lock(chan);
2851 while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
2852 ast_queue_frame_head(chan, cur);
2855 ast_channel_unlock(chan);
2866 int ast_answer(struct ast_channel *chan)
2868 return __ast_answer(chan, 0);
2871 inline int ast_auto_answer(struct ast_channel *chan)
2873 if (ast_channel_state(chan) == AST_STATE_UP) {
2874 /* Already answered */
2877 return ast_answer(chan);
2880 int ast_channel_get_duration(struct ast_channel *chan)
2882 ast_assert(NULL != chan);
2884 if (ast_tvzero(ast_channel_creationtime(chan))) {
2887 return (ast_tvdiff_ms(ast_tvnow(), ast_channel_creationtime(chan)) / 1000);
2890 int ast_channel_get_up_time(struct ast_channel *chan)
2892 ast_assert(NULL != chan);
2894 if (ast_tvzero(ast_channel_answertime(chan))) {
2897 return (ast_tvdiff_ms(ast_tvnow(), ast_channel_answertime(chan)) / 1000);
2900 static void deactivate_generator_nolock(struct ast_channel *chan)
2902 if (ast_channel_generatordata(chan)) {
2903 struct ast_generator *generator = ast_channel_generator(chan);
2905 if (generator && generator->release) {
2906 generator->release(chan, ast_channel_generatordata(chan));
2908 ast_channel_generatordata_set(chan, NULL);
2909 ast_channel_generator_set(chan, NULL);
2910 ast_channel_set_fd(chan, AST_GENERATOR_FD, -1);
2911 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
2912 ast_settimeout(chan, 0, NULL, NULL);
2916 void ast_deactivate_generator(struct ast_channel *chan)
2918 ast_channel_lock(chan);
2919 deactivate_generator_nolock(chan);
2920 ast_channel_unlock(chan);
2923 static void generator_write_format_change(struct ast_channel *chan)
2925 struct ast_generator *generator;
2927 ast_channel_lock(chan);
2928 generator = ast_channel_generator(chan);
2929 if (generator && generator->write_format_change) {
2930 generator->write_format_change(chan, ast_channel_generatordata(chan));
2932 ast_channel_unlock(chan);
2935 static int generator_force(const void *data)
2937 /* Called if generator doesn't have data */
2940 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
2941 struct ast_channel *chan = (struct ast_channel *)data;
2943 ast_channel_lock(chan);
2944 tmp = ast_channel_generatordata(chan);
2945 ast_channel_generatordata_set(chan, NULL);
2946 if (ast_channel_generator(chan))
2947 generate = ast_channel_generator(chan)->generate;
2948 ast_channel_unlock(chan);
2950 if (!tmp || !generate) {
2954 res = generate(chan, tmp, 0, ast_format_get_sample_rate(ast_channel_writeformat(chan)) / 50);
2956 ast_channel_lock(chan);
2957 if (ast_channel_generator(chan) && generate == ast_channel_generator(chan)->generate) {
2958 ast_channel_generatordata_set(chan, tmp);
2960 ast_channel_unlock(chan);
2963 ast_debug(1, "Auto-deactivating generator\n");
2964 ast_deactivate_generator(chan);
2970 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
2973 void *generatordata = NULL;
2975 ast_channel_lock(chan);
2976 if (ast_channel_generatordata(chan)) {
2977 struct ast_generator *generator_old = ast_channel_generator(chan);
2979 if (generator_old && generator_old->release) {
2980 generator_old->release(chan, ast_channel_generatordata(chan));
2983 if (gen->alloc && !(generatordata = gen->alloc(chan, params))) {
2986 ast_channel_generatordata_set(chan, generatordata);
2988 ast_settimeout(chan, 50, generator_force, chan);
2989 ast_channel_generator_set(chan, gen);
2991 ast_channel_unlock(chan);
2998 /*! \brief Wait for x amount of time on a file descriptor to have input. */
2999 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
3002 ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
3006 /*! \brief Wait for x amount of time on a file descriptor to have input. */
3008 static struct ast_channel *ast_waitfor_nandfds_classic(struct ast_channel **c, int n, int *fds, int nfds,
3009 int *exception, int *outfd, int *ms)
3011 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3012 int *exception, int *outfd, int *ms)
3015 struct timeval start = { 0 , 0 };
3016 struct pollfd *pfds = NULL;
3021 struct timeval now = { 0, 0 };
3022 struct timeval whentohangup = { 0, 0 }, diff;
3023 struct ast_channel *winner = NULL;
3036 if ((sz = n * AST_MAX_FDS + nfds)) {
3037 pfds = ast_alloca(sizeof(*pfds) * sz);
3038 fdmap = ast_alloca(sizeof(*fdmap) * sz);
3040 /* nothing to allocate and no FDs to check */
3044 for (x = 0; x < n; x++) {
3045 ast_channel_lock(c[x]);
3046 if (!ast_tvzero(*ast_channel_whentohangup(c[x]))) {
3047 if (ast_tvzero(whentohangup))
3049 diff = ast_tvsub(*ast_channel_whentohangup(c[x]), now);
3050 if (diff.tv_sec < 0 || ast_tvzero(diff)) {
3051 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3052 /* Should already be hungup */
3053 ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3054 ast_channel_unlock(c[x]);
3057 if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
3058 whentohangup = diff;
3060 ast_channel_unlock(c[x]);
3062 /* Wait full interval */
3064 /* INT_MAX, not LONG_MAX, because it matters on 64-bit */
3065 if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
3066 rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000; /* timeout in milliseconds */
3067 if (*ms >= 0 && *ms < rms) { /* original *ms still smaller */
3070 } else if (!ast_tvzero(whentohangup) && rms < 0) {
3071 /* Tiny corner case... call would need to last >24 days */
3075 * Build the pollfd array, putting the channels' fds first,
3076 * followed by individual fds. Order is important because
3077 * individual fd's must have priority over channel fds.
3080 for (x = 0; x < n; x++) {
3081 for (y = 0; y < AST_MAX_FDS; y++) {
3082 fdmap[max].fdno = y; /* fd y is linked to this pfds */
3083 fdmap[max].chan = x; /* channel x is linked to this pfds */
3084 max += ast_add_fd(&pfds[max], ast_channel_fd(c[x], y));
3086 CHECK_BLOCKING(c[x]);
3088 /* Add the individual fds */
3089 for (x = 0; x < nfds; x++) {
3090 fdmap[max].chan = -1;
3091 max += ast_add_fd(&pfds[max], fds[x]);
3095 start = ast_tvnow();
3098 if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */
3101 if (kbrms > 600000) {
3104 res = ast_poll(pfds, max, kbrms);
3108 } while (!res && (rms > 0));
3110 res = ast_poll(pfds, max, rms);
3112 for (x = 0; x < n; x++) {
3113 ast_clear_flag(ast_channel_flags(c[x]), AST_FLAG_BLOCKING);
3115 if (res < 0) { /* Simulate a timeout if we were interrupted */
3116 if (errno != EINTR) {
3121 if (!ast_tvzero(whentohangup)) { /* if we have a timeout, check who expired */
3123 for (x = 0; x < n; x++) {
3124 if (!ast_tvzero(*ast_channel_whentohangup(c[x])) && ast_tvcmp(*ast_channel_whentohangup(c[x]), now) <= 0) {
3125 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3126 ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3127 if (winner == NULL) {
3133 if (res == 0) { /* no fd ready, reset timeout and done */
3134 *ms = 0; /* XXX use 0 since we may not have an exact timeout. */
3138 * Then check if any channel or fd has a pending event.
3139 * Remember to check channels first and fds last, as they
3140 * must have priority on setting 'winner'
3142 for (x = 0; x < max; x++) {
3143 res = pfds[x].revents;
3147 if (fdmap[x].chan >= 0) { /* this is a channel */
3148 winner = c[fdmap[x].chan]; /* override previous winners */
3149 if (res & POLLPRI) {
3150 ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3152 ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3154 ast_channel_fdno_set(winner, fdmap[x].fdno);
3155 } else { /* this is an fd */
3157 *outfd = pfds[x].fd;
3160 *exception = (res & POLLPRI) ? -1 : 0;
3166 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3175 static struct ast_channel *ast_waitfor_nandfds_simple(struct ast_channel *chan, int *ms)
3177 struct timeval start = { 0 , 0 };
3179 struct epoll_event ev[1];
3180 long diff, rms = *ms;
3181 struct ast_channel *winner = NULL;
3182 struct ast_epoll_data *aed = NULL;
3184 ast_channel_lock(chan);
3185 /* Figure out their timeout */
3186 if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
3187 if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow())) < 0) {
3188 /* They should already be hungup! */
3189 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
3190 ast_channel_unlock(chan);
3193 /* If this value is smaller then the current one... make it priority */
3199 ast_channel_unlock(chan);
3201 /* Time to make this channel block... */
3202 CHECK_BLOCKING(chan);
3205 start = ast_tvnow();
3208 /* We don't have to add any file descriptors... they are already added, we just have to wait! */
3209 res = epoll_wait(ast_channel_epfd(chan), ev, 1, rms);
3212 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
3214 /* Simulate a timeout if we were interrupted */
3216 if (errno != EINTR) {
3222 /* If this channel has a timeout see if it expired */
3223 if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
3224 if (ast_tvdiff_ms(ast_tvnow(), *ast_channel_whentohangup(chan)) >= 0) {
3225 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
3230 /* No fd ready, reset timeout and be done for now */
3236 /* See what events are pending */
3237 aed = ev[0].data.ptr;
3238 ast_channel_fdno_set(chan, aed->which);
3239 if (ev[0].events & EPOLLPRI) {
3240 ast_set_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3242 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3246 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3255 static struct ast_channel *ast_waitfor_nandfds_complex(struct ast_channel **c, int n, int *ms)
3257 struct timeval start = { 0 , 0 };
3259 struct epoll_event ev[25] = { { 0, } };
3260 struct timeval now = { 0, 0 };
3261 long whentohangup = 0, diff = 0, rms = *ms;
3262 struct ast_channel *winner = NULL;
3264 for (i = 0; i < n; i++) {
3265 ast_channel_lock(c[i]);
3266 if (!ast_tvzero(*ast_channel_whentohangup(c[i]))) {
3267 if (whentohangup == 0) {
3270 if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(c[i]), now)) < 0) {
3271 ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
3272 ast_channel_unlock(c[i]);
3275 if (!whentohangup || whentohangup > diff) {
3276 whentohangup = diff;
3279 ast_channel_unlock(c[i]);
3280 CHECK_BLOCKING(c[i]);
3286 if (*ms >= 0 && *ms < rms) {
3292 start = ast_tvnow();
3295 res = epoll_wait(ast_channel_epfd(c[0]), ev, 25, rms);
3297 for (i = 0; i < n; i++) {
3298 ast_clear_flag(ast_channel_flags(c[i]), AST_FLAG_BLOCKING);
3302 if (errno != EINTR) {
3310 for (i = 0; i < n; i++) {
3311 if (!ast_tvzero(*ast_channel_whentohangup(c[i])) && ast_tvdiff_ms(now, *ast_channel_whentohangup(c[i])) >= 0) {
3312 ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
3325 for (i = 0; i < res; i++) {
3326 struct ast_epoll_data *aed = ev[i].data.ptr;
3328 if (!ev[i].events || !aed) {
3333 if (ev[i].events & EPOLLPRI) {
3334 ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3336 ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3338 ast_channel_fdno_set(winner, aed->which);
3342 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3351 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3352 int *exception, int *outfd, int *ms)
3354 /* Clear all provided values in one place. */
3362 /* If no epoll file descriptor is available resort to classic nandfds */
3363 if (!n || nfds || ast_channel_epfd(c[0]) == -1) {
3364 return ast_waitfor_nandfds_classic(c, n, fds, nfds, exception, outfd, ms);
3365 } else if (!nfds && n == 1) {
3366 return ast_waitfor_nandfds_simple(c[0], ms);
3368 return ast_waitfor_nandfds_complex(c, n, ms);
3373 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
3375 return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
3378 int ast_waitfor(struct ast_channel *c, int ms)
3383 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3386 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3391 int ast_waitfordigit(struct ast_channel *c, int ms)
3393 return ast_waitfordigit_full(c, ms, -1, -1);
3396 int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
3398 return ast_settimeout_full(c, rate, func, data, 0);
3401 int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data, unsigned int is_ao2_obj)
3404 unsigned int real_rate = rate, max_rate;
3406 ast_channel_lock(c);
3408 if (ast_channel_timingfd(c) == -1) {
3409 ast_channel_unlock(c);
3418 if (rate && rate > (max_rate = ast_timer_get_max_rate(ast_channel_timer(c)))) {
3419 real_rate = max_rate;
3422 ast_debug(1, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
3424 res = ast_timer_set_rate(ast_channel_timer(c), real_rate);
3426 if (ast_channel_timingdata(c) && ast_test_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
3427 ao2_ref(ast_channel_timingdata(c), -1);
3430 ast_channel_timingfunc_set(c, func);
3431 ast_channel_timingdata_set(c, data);
3433 if (data && is_ao2_obj) {
3435 ast_set_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
3437 ast_clear_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
3440 if (func == NULL && rate == 0 && ast_channel_fdno(c) == AST_TIMING_FD) {
3441 /* Clearing the timing func and setting the rate to 0
3442 * means that we don't want to be reading from the timingfd
3443 * any more. Setting c->fdno to -1 means we won't have any
3444 * errant reads from the timingfd, meaning we won't potentially
3445 * miss any important frames.
3447 ast_channel_fdno_set(c, -1);
3450 ast_channel_unlock(c);
3455 int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, int cmdfd)
3457 struct timeval start = ast_tvnow();
3460 /* Stop if we're a zombie or need a soft hangup */
3461 if (ast_test_flag(ast_channel_flags(c), AST_FLAG_ZOMBIE) || ast_check_hangup(c))
3464 /* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
3465 ast_set_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3467 /* Wait for a digit, no more than timeout_ms milliseconds total.
3468 * Or, wait indefinitely if timeout_ms is <0.
3470 while ((ms = ast_remaining_ms(start, timeout_ms))) {
3471 struct ast_channel *rchan;
3475 /* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,
3476 * it is unhelpful if it waited less than a millisecond.
3478 rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
3480 if (!rchan && outfd < 0 && ms) {
3481 if (errno == 0 || errno == EINTR)
3483 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
3484 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3486 } else if (outfd > -1) {
3487 /* The FD we were watching has something waiting */
3488 ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
3489 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3493 struct ast_frame *f = ast_read(c);
3497 switch (f->frametype) {
3498 case AST_FRAME_DTMF_BEGIN:
3500 case AST_FRAME_DTMF_END:
3501 res = f->subclass.integer;
3503 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3505 case AST_FRAME_CONTROL:
3506 switch (f->subclass.integer) {
3507 case AST_CONTROL_HANGUP:
3509 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3511 case AST_CONTROL_STREAM_STOP:
3512 case AST_CONTROL_STREAM_SUSPEND:
3513 case AST_CONTROL_STREAM_RESTART:
3514 case AST_CONTROL_STREAM_REVERSE:
3515 case AST_CONTROL_STREAM_FORWARD:
3516 /* Fall-through and treat as if it were a DTMF signal. Items
3517 * that perform stream control will handle this. */
3518 res = f->subclass.integer;
3520 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3522 case AST_CONTROL_PVT_CAUSE_CODE:
3523 case AST_CONTROL_RINGING:
3524 case AST_CONTROL_ANSWER:
3525 case AST_CONTROL_SRCUPDATE:
3526 case AST_CONTROL_SRCCHANGE:
3527 case AST_CONTROL_CONNECTED_LINE:
3528 case AST_CONTROL_REDIRECTING:
3529 case AST_CONTROL_UPDATE_RTP_PEER:
3530 case AST_CONTROL_HOLD:
3531 case AST_CONTROL_UNHOLD:
3536 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
3540 case AST_FRAME_VOICE:
3541 /* Write audio if appropriate */
3543 if (write(audiofd, f->data.ptr, f->datalen) < 0) {
3544 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3555 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3557 return 0; /* Time is up */
3560 enum DtmfDirection {
3565 static const char *dtmf_direction_to_string(enum DtmfDirection direction)
3567 switch (direction) {
3577 static void send_dtmf_begin_event(struct ast_channel *chan,
3578 enum DtmfDirection direction, const char digit)
3580 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3581 char digit_str[] = { digit, '\0' };
3583 blob = ast_json_pack("{ s: s, s: s }",
3585 "direction", dtmf_direction_to_string(direction));
3590 ast_channel_publish_cached_blob(chan, ast_channel_dtmf_begin_type(), blob);
3593 static void send_dtmf_end_event(struct ast_channel *chan,
3594 enum DtmfDirection direction, const char digit, long duration_ms)
3596 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3597 char digit_str[] = { digit, '\0' };
3599 blob = ast_json_pack("{ s: s, s: s, s: i }",
3601 "direction", dtmf_direction_to_string(direction),
3602 "duration_ms", duration_ms);
3607 ast_channel_publish_cached_blob(chan, ast_channel_dtmf_end_type(), blob);
3610 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
3612 struct ast_generator *generator;
3617 generator = ast_channel_generator(chan);
3619 || !generator->generate
3620 || f->frametype != AST_FRAME_VOICE
3621 || !ast_channel_generatordata(chan)
3622 || ast_channel_timingfunc(chan)) {
3627 * We must generate frames in phase locked mode since
3628 * we have no internal timer available.
3630 if (ast_format_cmp(f->subclass.format, ast_channel_writeformat(chan)) == AST_FORMAT_CMP_NOT_EQUAL) {
3632 factor = ((float) ast_format_get_sample_rate(ast_channel_writeformat(chan))) / ((float) ast_format_get_sample_rate(f->subclass.format));
3633 samples = (int) (((float) f->samples) * factor);
3635 samples = f->samples;
3638 gendata = ast_channel_generatordata(chan);
3639 ast_channel_generatordata_set(chan, NULL); /* reset, to let writes go through */
3642 * This unlock is here based on two assumptions that hold true at
3643 * this point in the code. 1) this function is only called from
3644 * within __ast_read() and 2) all generators call ast_write() in
3645 * their generate callback.
3647 * The reason this is added is so that when ast_write is called,
3648 * the lock that occurs there will not recursively lock the
3649 * channel. Doing this will allow deadlock avoidance to work in
3652 ast_channel_unlock(chan);
3653 res = generator->generate(chan, gendata, f->datalen, samples);
3654 ast_channel_lock(chan);
3655 if (generator == ast_channel_generator(chan)) {
3656 ast_channel_generatordata_set(chan, gendata);
3658 ast_debug(1, "Auto-deactivating generator\n");
3659 ast_deactivate_generator(chan);
3664 static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
3666 struct ast_frame *fr = ast_channel_dtmff(chan);
3668 fr->frametype = AST_FRAME_DTMF_END;
3669 fr->subclass.integer = f->subclass.integer;
3672 /* The only time this function will be called is for a frame that just came
3673 * out of the channel driver. So, we want to stick it on the tail of the
3676 ast_queue_frame(chan, fr);
3680 * \brief Determine whether or not we should ignore DTMF in the readq
3682 static inline int should_skip_dtmf(struct ast_channel *chan)
3684 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_EMULATE_DTMF)) {
3685 /* We're in the middle of emulating a digit, or DTMF has been
3686 * explicitly deferred. Skip this digit, then. */
3690 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3691 ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
3692 /* We're not in the middle of a digit, but it hasn't been long enough
3693 * since the last digit, so we'll have to skip DTMF for now. */
3701 * \brief calculates the number of samples to jump forward with in a monitor stream.
3703 * \note When using ast_seekstream() with the read and write streams of a monitor,
3704 * the number of samples to seek forward must be of the same sample rate as the stream
3705 * or else the jump will not be calculated correctly.
3707 * \retval number of samples to seek forward after rate conversion.
3709 static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
3711 int diff = sample_rate - seek_rate;
3714 samples = samples / (float) (sample_rate / seek_rate);
3715 } else if (diff < 0) {
3716 samples = samples * (float) (seek_rate / sample_rate);
3722 static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
3724 struct ast_frame *f = NULL; /* the return value */
3728 /* this function is very long so make sure there is only one return
3729 * point at the end (there are only two exceptions to this).
3731 ast_channel_lock(chan);
3733 /* Stop if we're a zombie or need a soft hangup */
3734 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
3735 if (ast_channel_generator(chan))
3736 ast_deactivate_generator(chan);
3739 * It is possible for chan->_softhangup to be set and there
3740 * still be control frames that need to be read. Instead of
3741 * just going to 'done' in the case of ast_check_hangup(), we
3742 * need to queue the end-of-Q frame so that it can mark the end
3743 * of the read queue. If there are frames to be read,
3744 * ast_queue_control() will be called repeatedly, but will only
3745 * queue the first end-of-Q frame.
3747 if (ast_channel_softhangup_internal_flag(chan)) {
3748 ast_queue_control(chan, AST_CONTROL_END_OF_Q);
3755 * The ast_waitfor() code records which of the channel's file
3756 * descriptors reported that data is available. In theory,
3757 * ast_read() should only be called after ast_waitfor() reports
3758 * that a channel has data available for reading. However,
3759 * there still may be some edge cases throughout the code where
3760 * ast_read() is called improperly. This can potentially cause
3761 * problems, so if this is a developer build, make a lot of
3762 * noise if this happens so that it can be addressed.
3764 * One of the potential problems is blocking on a dead channel.
3766 if (ast_channel_fdno(chan) == -1) {
3768 "ast_read() on chan '%s' called with no recorded file descriptor.\n",
3769 ast_channel_name(chan));
3774 prestate = ast_channel_state(chan);
3776 if (ast_channel_timingfd(chan) > -1 && ast_channel_fdno(chan) == AST_TIMING_FD) {
3777 enum ast_timer_event res;
3779 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3781 res = ast_timer_get_event(ast_channel_timer(chan));
3784 case AST_TIMING_EVENT_EXPIRED:
3785 if (ast_timer_ack(ast_channel_timer(chan), 1) < 0) {
3786 ast_log(LOG_ERROR, "Failed to acknoweldge timer in ast_read\n");
3790 if (ast_channel_timingfunc(chan)) {
3791 /* save a copy of func/data before unlocking the channel */
3792 ast_timing_func_t func = ast_channel_timingfunc(chan);
3793 void *data = ast_channel_timingdata(chan);
3795 if (data && ast_test_flag(ast_channel_flags(chan), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
3799 ast_channel_fdno_set(chan, -1);
3800 ast_channel_unlock(chan);
3806 ast_timer_set_rate(ast_channel_timer(chan), 0);
3807 ast_channel_fdno_set(chan, -1);
3808 ast_channel_unlock(chan);
3811 /* cannot 'goto done' because the channel is already unlocked */
3812 return &ast_null_frame;
3814 case AST_TIMING_EVENT_CONTINUOUS:
3815 if (AST_LIST_EMPTY(ast_channel_readq(chan)) ||
3816 !AST_LIST_NEXT(AST_LIST_FIRST(ast_channel_readq(chan)), frame_list)) {
3817 ast_timer_disable_continuous(ast_channel_timer(chan));
3822 } else if (ast_channel_fd_isset(chan, AST_GENERATOR_FD) && ast_channel_fdno(chan) == AST_GENERATOR_FD) {
3823 /* if the AST_GENERATOR_FD is set, call the generator with args
3824 * set to -1 so it can do whatever it needs to.
3826 void *tmp = ast_channel_generatordata(chan);
3827 ast_channel_generatordata_set(chan, NULL); /* reset to let ast_write get through */
3828 ast_channel_generator(chan)->generate(chan, tmp, -1, -1);
3829 ast_channel_generatordata_set(chan, tmp);
3830 f = &ast_null_frame;
3831 ast_channel_fdno_set(chan, -1);
3833 } else if (ast_channel_fd_isset(chan, AST_JITTERBUFFER_FD) && ast_channel_fdno(chan) == AST_JITTERBUFFER_FD) {
3834 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3837 /* Read and ignore anything on the alertpipe, but read only
3838 one sizeof(blah) per frame that we send from it */
3839 if (ast_channel_internal_alert_read(chan) == AST_ALERT_READ_FATAL) {
3840 f = &ast_null_frame;
3844 /* Check for pending read queue */
3845 if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
3846 int skip_dtmf = should_skip_dtmf(chan);
3848 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), f, frame_list) {
3849 /* We have to be picky about which frame we pull off of the readq because
3850 * there are cases where we want to leave DTMF frames on the queue until
3851 * some later time. */
3853 if ( (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) && skip_dtmf) {
3857 AST_LIST_REMOVE_CURRENT(frame_list);
3860 AST_LIST_TRAVERSE_SAFE_END;
3863 /* There were no acceptable frames on the readq. */
3864 f = &ast_null_frame;
3865 ast_channel_alert_write(chan);
3868 /* Interpret hangup and end-of-Q frames to return NULL */
3869 /* XXX why not the same for frames from the channel ? */
3870 if (f->frametype == AST_FRAME_CONTROL) {
3871 switch (f->subclass.integer) {
3872 case AST_CONTROL_HANGUP:
3873 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
3874 cause = f->data.uint32;
3876 case AST_CONTROL_END_OF_Q:
3885 ast_channel_blocker_set(chan, pthread_self());
3886 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION)) {
3887 if (ast_channel_tech(chan)->exception)
3888 f = ast_channel_tech(chan)->exception(chan);
3890 ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", ast_channel_name(chan));
3891 f = &ast_null_frame;
3893 /* Clear the exception flag */
3894 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3895 } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read)