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_REGISTER_FILE()
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"
78 #include "asterisk/max_forwards.h"
84 #include <sys/epoll.h>
87 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
90 #endif /* defined(HAVE_PRI) */
91 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
93 struct ast_epoll_data {
94 struct ast_channel *chan;
98 /* uncomment if you have problems with 'monitoring' synchronized files */
100 #define MONITOR_CONSTANT_DELAY
101 #define MONITOR_DELAY 150 * 8 /*!< 150 ms of MONITORING DELAY */
104 static int chancount;
106 unsigned long global_fin, global_fout;
108 AST_THREADSTORAGE(state2str_threadbuf);
109 #define STATE2STR_BUFSIZE 32
111 /*! Default amount of time to use when emulating a digit as a begin and end
113 #define AST_DEFAULT_EMULATE_DTMF_DURATION 100
115 #define DEFAULT_AMA_FLAGS AST_AMA_DOCUMENTATION
117 /*! Minimum amount of time between the end of the last digit and the beginning
118 * of a new one - 45ms */
119 #define AST_MIN_DTMF_GAP 45
121 /*! \brief List of channel drivers */
123 const struct ast_channel_tech *tech;
124 AST_LIST_ENTRY(chanlist) list;
127 /*! \brief the list of registered channel types */
128 static AST_RWLIST_HEAD_STATIC(backends, chanlist);
131 #define NUM_CHANNEL_BUCKETS 61
133 #define NUM_CHANNEL_BUCKETS 1567
136 /*! \brief All active channels on the system */
137 static struct ao2_container *channels;
139 /*! \brief map AST_CAUSE's to readable string representations
149 static const struct causes_map causes[] = {
150 { AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
151 { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
152 { AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
153 { AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
154 { AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
155 { AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
156 { AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
157 { AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
158 { AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
159 { AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
160 { AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
161 { AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
162 { AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
163 { AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
164 { AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
165 { AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
166 { AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
167 { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
168 { AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
169 { AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
170 { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
171 { AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
172 { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
173 { AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
174 { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
175 { AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
176 { AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
177 { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
178 { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
179 { AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
180 { AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
181 { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
182 { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
183 { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
184 { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
185 { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
186 { AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
187 { AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
188 { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
189 { AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
190 { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
191 { AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
192 { AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
193 { AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
194 { AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
195 { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
196 { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
197 { AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
198 { AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
201 struct ast_variable *ast_channeltype_list(void)
204 struct ast_variable *var = NULL, *prev = NULL;
206 AST_RWLIST_RDLOCK(&backends);
207 AST_RWLIST_TRAVERSE(&backends, cl, list) {
209 if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
212 var = ast_variable_new(cl->tech->type, cl->tech->description, "");
216 AST_RWLIST_UNLOCK(&backends);
221 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
222 static const char *party_number_ton2str(int ton)
224 #if defined(HAVE_PRI)
225 switch ((ton >> 4) & 0x07) {
226 case PRI_TON_INTERNATIONAL:
227 return "International";
228 case PRI_TON_NATIONAL:
230 case PRI_TON_NET_SPECIFIC:
231 return "Network Specific";
232 case PRI_TON_SUBSCRIBER:
234 case PRI_TON_ABBREVIATED:
235 return "Abbreviated";
236 case PRI_TON_RESERVED:
238 case PRI_TON_UNKNOWN:
242 #endif /* defined(HAVE_PRI) */
245 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
247 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
248 static const char *party_number_plan2str(int plan)
250 #if defined(HAVE_PRI)
251 switch (plan & 0x0F) {
253 case PRI_NPI_UNKNOWN:
255 case PRI_NPI_E163_E164:
256 return "Public (E.163/E.164)";
258 return "Data (X.121)";
260 return "Telex (F.69)";
261 case PRI_NPI_NATIONAL:
262 return "National Standard";
263 case PRI_NPI_PRIVATE:
265 case PRI_NPI_RESERVED:
268 #endif /* defined(HAVE_PRI) */
271 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
273 /*! \brief Show channel types - CLI command */
274 static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
276 #define FORMAT "%-15.15s %-40.40s %-13.13s %-13.13s %-13.13s %-13.13s\n"
282 e->command = "core show channeltypes";
284 "Usage: core show channeltypes\n"
285 " Lists available channel types registered in your\n"
286 " Asterisk server.\n";
293 return CLI_SHOWUSAGE;
295 ast_cli(a->fd, FORMAT, "Type", "Description", "Devicestate", "Presencestate", "Indications", "Transfer");
296 ast_cli(a->fd, FORMAT, "-------------", "-------------", "-------------", "-------------", "-------------", "-------------");
298 AST_RWLIST_RDLOCK(&backends);
299 AST_RWLIST_TRAVERSE(&backends, cl, list) {
300 ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
301 (cl->tech->devicestate) ? "yes" : "no",
302 (cl->tech->presencestate) ? "yes" : "no",
303 (cl->tech->indicate) ? "yes" : "no",
304 (cl->tech->transfer) ? "yes" : "no");
307 AST_RWLIST_UNLOCK(&backends);
309 ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
316 static char *complete_channeltypes(struct ast_cli_args *a)
326 wordlen = strlen(a->word);
328 AST_RWLIST_RDLOCK(&backends);
329 AST_RWLIST_TRAVERSE(&backends, cl, list) {
330 if (!strncasecmp(a->word, cl->tech->type, wordlen) && ++which > a->n) {
331 ret = ast_strdup(cl->tech->type);
335 AST_RWLIST_UNLOCK(&backends);
340 /*! \brief Show details about a channel driver - CLI command */
341 static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
343 struct chanlist *cl = NULL;
344 struct ast_str *codec_buf = ast_str_alloca(256);
348 e->command = "core show channeltype";
350 "Usage: core show channeltype <name>\n"
351 " Show details about the specified channel type, <name>.\n";
354 return complete_channeltypes(a);
358 return CLI_SHOWUSAGE;
360 AST_RWLIST_RDLOCK(&backends);
362 AST_RWLIST_TRAVERSE(&backends, cl, list) {
363 if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
369 ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
370 AST_RWLIST_UNLOCK(&backends);
375 "-- Info about channel driver: %s --\n"
376 " Device State: %s\n"
377 "Presence State: %s\n"
380 " Capabilities: %s\n"
384 " Image Support: %s\n"
385 " Text Support: %s\n",
387 (cl->tech->devicestate) ? "yes" : "no",
388 (cl->tech->presencestate) ? "yes" : "no",
389 (cl->tech->indicate) ? "yes" : "no",
390 (cl->tech->transfer) ? "yes" : "no",
391 ast_format_cap_get_names(cl->tech->capabilities, &codec_buf),
392 (cl->tech->send_digit_begin) ? "yes" : "no",
393 (cl->tech->send_digit_end) ? "yes" : "no",
394 (cl->tech->send_html) ? "yes" : "no",
395 (cl->tech->send_image) ? "yes" : "no",
396 (cl->tech->send_text) ? "yes" : "no"
400 AST_RWLIST_UNLOCK(&backends);
405 static struct ast_cli_entry cli_channel[] = {
406 AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
407 AST_CLI_DEFINE(handle_cli_core_show_channeltype, "Give more details on that channel type")
410 static struct ast_frame *kill_read(struct ast_channel *chan)
412 /* Hangup channel. */
416 static struct ast_frame *kill_exception(struct ast_channel *chan)
418 /* Hangup channel. */
422 static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
424 /* Hangup channel. */
428 static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
430 /* No problem fixing up the channel. */
434 static int kill_hangup(struct ast_channel *chan)
436 ast_channel_tech_pvt_set(chan, NULL);
441 * \brief Kill the channel channel driver technology descriptor.
444 * The purpose of this channel technology is to encourage the
445 * channel to hangup as quickly as possible.
447 * \note Used by DTMF atxfer and zombie channels.
449 const struct ast_channel_tech ast_kill_tech = {
451 .description = "Kill channel (should not see this)",
453 .exception = kill_exception,
456 .hangup = kill_hangup,
459 /*! \brief Checks to see if a channel is needing hang up */
460 int ast_check_hangup(struct ast_channel *chan)
462 if (ast_channel_softhangup_internal_flag(chan)) /* yes if soft hangup flag set */
464 if (ast_tvzero(*ast_channel_whentohangup(chan))) /* no if no hangup scheduled */
466 if (ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()) > 0) /* no if hangup time has not come yet. */
468 ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()));
469 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(chan));
470 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT); /* record event */
474 int ast_check_hangup_locked(struct ast_channel *chan)
477 ast_channel_lock(chan);
478 res = ast_check_hangup(chan);
479 ast_channel_unlock(chan);
483 void ast_channel_softhangup_withcause_locked(struct ast_channel *chan, int causecode)
485 ast_channel_lock(chan);
488 ast_debug(1, "Setting hangupcause of channel %s to %d (is %d now)\n",
489 ast_channel_name(chan), causecode, ast_channel_hangupcause(chan));
491 ast_channel_hangupcause_set(chan, causecode);
494 ast_softhangup_nolock(chan, AST_SOFTHANGUP_EXPLICIT);
496 ast_channel_unlock(chan);
499 static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
501 struct ast_channel *chan = obj;
503 ast_softhangup(chan, AST_SOFTHANGUP_SHUTDOWN);
508 void ast_softhangup_all(void)
510 ao2_callback(channels, OBJ_NODATA | OBJ_MULTIPLE, ast_channel_softhangup_cb, NULL);
513 /*! \brief returns number of active/allocated channels */
514 int ast_active_channels(void)
516 return channels ? ao2_container_count(channels) : 0;
519 int ast_undestroyed_channels(void)
521 return ast_atomic_fetchadd_int(&chancount, 0);
524 /*! \brief Set when to hangup channel */
525 void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
527 if (ast_tvzero(offset)) {
528 ast_channel_whentohangup_set(chan, &offset);
530 struct timeval tv = ast_tvadd(offset, ast_tvnow());
531 ast_channel_whentohangup_set(chan, &tv);
533 ast_queue_frame(chan, &ast_null_frame);
537 void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
539 struct timeval when = { offset, };
540 ast_channel_setwhentohangup_tv(chan, when);
543 /*! \brief Compare a offset with when to hangup channel */
544 int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
546 struct timeval whentohangup;
548 if (ast_tvzero(*ast_channel_whentohangup(chan)))
549 return ast_tvzero(offset) ? 0 : -1;
551 if (ast_tvzero(offset))
554 whentohangup = ast_tvadd(offset, ast_tvnow());
556 return ast_tvdiff_ms(whentohangup, *ast_channel_whentohangup(chan));
559 int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
561 struct timeval when = { offset, };
562 return ast_channel_cmpwhentohangup_tv(chan, when);
565 /*! \brief Register a new telephony channel in Asterisk */
566 int ast_channel_register(const struct ast_channel_tech *tech)
568 struct chanlist *chan;
570 AST_RWLIST_WRLOCK(&backends);
572 AST_RWLIST_TRAVERSE(&backends, chan, list) {
573 if (!strcasecmp(tech->type, chan->tech->type)) {
574 ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
575 AST_RWLIST_UNLOCK(&backends);
580 if (!(chan = ast_calloc(1, sizeof(*chan)))) {
581 AST_RWLIST_UNLOCK(&backends);
585 AST_RWLIST_INSERT_HEAD(&backends, chan, list);
587 ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
589 ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
591 AST_RWLIST_UNLOCK(&backends);
596 /*! \brief Unregister channel driver */
597 void ast_channel_unregister(const struct ast_channel_tech *tech)
599 struct chanlist *chan;
601 ast_debug(1, "Unregistering channel type '%s'\n", tech->type);
603 AST_RWLIST_WRLOCK(&backends);
605 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&backends, chan, list) {
606 if (chan->tech == tech) {
607 AST_LIST_REMOVE_CURRENT(list);
609 ast_verb(2, "Unregistered channel type '%s'\n", tech->type);
613 AST_LIST_TRAVERSE_SAFE_END;
615 AST_RWLIST_UNLOCK(&backends);
618 /*! \brief Get handle to channel driver based on name */
619 const struct ast_channel_tech *ast_get_channel_tech(const char *name)
621 struct chanlist *chanls;
622 const struct ast_channel_tech *ret = NULL;
624 AST_RWLIST_RDLOCK(&backends);
626 AST_RWLIST_TRAVERSE(&backends, chanls, list) {
627 if (!strcasecmp(name, chanls->tech->type)) {
633 AST_RWLIST_UNLOCK(&backends);
638 /*! \brief Gives the string form of a given hangup cause */
639 const char *ast_cause2str(int cause)
643 for (x = 0; x < ARRAY_LEN(causes); x++) {
644 if (causes[x].cause == cause)
645 return causes[x].desc;
651 /*! \brief Convert a symbolic hangup cause to number */
652 int ast_str2cause(const char *name)
656 for (x = 0; x < ARRAY_LEN(causes); x++)
657 if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
658 return causes[x].cause;
663 static struct stasis_message *create_channel_snapshot_message(struct ast_channel *channel)
665 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
667 if (!ast_channel_snapshot_type()) {
671 ast_channel_lock(channel);
672 snapshot = ast_channel_snapshot_create(channel);
673 ast_channel_unlock(channel);
678 return stasis_message_create(ast_channel_snapshot_type(), snapshot);
681 static void publish_cache_clear(struct ast_channel *chan)
683 RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
684 RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
686 clear_msg = create_channel_snapshot_message(chan);
691 message = stasis_cache_clear_create(clear_msg);
692 stasis_publish(ast_channel_topic(chan), message);
695 /*! \brief Gives the string form of a given channel state.
697 * \note This function is not reentrant.
701 const char *ast_state2str(enum ast_channel_state state)
708 case AST_STATE_RESERVED:
710 case AST_STATE_OFFHOOK:
712 case AST_STATE_DIALING:
716 case AST_STATE_RINGING:
722 case AST_STATE_DIALING_OFFHOOK:
723 return "Dialing Offhook";
724 case AST_STATE_PRERING:
729 if (!(buf = ast_threadstorage_get(&state2str_threadbuf, STATE2STR_BUFSIZE)))
731 snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%u)", state);
736 /*! \brief Gives the string form of a given transfer capability */
737 char *ast_transfercapability2str(int transfercapability)
739 switch (transfercapability) {
740 case AST_TRANS_CAP_SPEECH:
742 case AST_TRANS_CAP_DIGITAL:
744 case AST_TRANS_CAP_RESTRICTED_DIGITAL:
745 return "RESTRICTED_DIGITAL";
746 case AST_TRANS_CAP_3_1K_AUDIO:
748 case AST_TRANS_CAP_DIGITAL_W_TONES:
749 return "DIGITAL_W_TONES";
750 case AST_TRANS_CAP_VIDEO:
757 /*! \brief Channel technology used to extract a channel from a running application. The
758 * channel created with this technology will be immediately hung up - most external
759 * applications won't ever want to see this.
761 static const struct ast_channel_tech surrogate_tech = {
763 .description = "Surrogate channel used to pull channel from an application",
764 .properties = AST_CHAN_TP_INTERNAL,
767 static const struct ast_channel_tech null_tech = {
769 .description = "Null channel (should not see this)",
772 static void ast_channel_destructor(void *obj);
773 static void ast_dummy_channel_destructor(void *obj);
775 /*! \brief Create a new channel structure */
776 static struct ast_channel * attribute_malloc __attribute__((format(printf, 15, 0)))
777 __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
778 const char *acctcode, const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
779 const struct ast_channel *requestor, enum ama_flags amaflag, struct ast_endpoint *endpoint,
780 const char *file, int line,
781 const char *function, const char *name_fmt, va_list ap)
783 struct ast_channel *tmp;
784 struct varshead *headp;
785 char *tech = "", *tech2 = NULL;
786 struct ast_format_cap *nativeformats;
787 struct ast_sched_context *schedctx;
788 struct ast_timer *timer;
790 const struct ast_channel_tech *channel_tech;
792 /* If shutting down, don't allocate any new channels */
793 if (ast_shutting_down()) {
794 ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
798 if (!(tmp = ast_channel_internal_alloc(ast_channel_destructor, assignedids, requestor))) {
799 /* Channel structure allocation failure. */
803 ast_channel_stage_snapshot(tmp);
805 if (!(nativeformats = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
806 /* format capabilities structure allocation failure */
807 return ast_channel_unref(tmp);
809 ast_format_cap_append(nativeformats, ast_format_none, 0);
810 ast_channel_nativeformats_set(tmp, nativeformats);
811 ao2_ref(nativeformats, -1);
813 ast_channel_set_rawwriteformat(tmp, ast_format_none);
814 ast_channel_set_rawreadformat(tmp, ast_format_none);
815 ast_channel_set_writeformat(tmp, ast_format_none);
816 ast_channel_set_readformat(tmp, ast_format_none);
819 * Init file descriptors to unopened state so
820 * the destructor can know not to close them.
822 ast_channel_timingfd_set(tmp, -1);
823 ast_channel_internal_alertpipe_clear(tmp);
824 ast_channel_internal_fd_clear_all(tmp);
827 ast_channel_epfd_set(tmp, epoll_create(25));
830 if (!(schedctx = ast_sched_context_create())) {
831 ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
832 return ast_channel_unref(tmp);
834 ast_channel_sched_set(tmp, schedctx);
836 ast_party_dialed_init(ast_channel_dialed(tmp));
837 ast_party_caller_init(ast_channel_caller(tmp));
838 ast_party_connected_line_init(ast_channel_connected(tmp));
839 ast_party_connected_line_init(ast_channel_connected_indicated(tmp));
840 ast_party_redirecting_init(ast_channel_redirecting(tmp));
843 ast_channel_caller(tmp)->id.name.valid = 1;
844 ast_channel_caller(tmp)->id.name.str = ast_strdup(cid_name);
845 if (!ast_channel_caller(tmp)->id.name.str) {
846 return ast_channel_unref(tmp);
850 ast_channel_caller(tmp)->id.number.valid = 1;
851 ast_channel_caller(tmp)->id.number.str = ast_strdup(cid_num);
852 if (!ast_channel_caller(tmp)->id.number.str) {
853 return ast_channel_unref(tmp);
857 if ((timer = ast_timer_open())) {
858 ast_channel_timer_set(tmp, timer);
859 if (strcmp(ast_timer_get_name(ast_channel_timer(tmp)), "timerfd")) {
862 ast_channel_timingfd_set(tmp, ast_timer_fd(ast_channel_timer(tmp)));
865 if (needqueue && ast_channel_internal_alertpipe_init(tmp)) {
866 return ast_channel_unref(tmp);
869 /* Always watch the alertpipe */
870 ast_channel_set_fd(tmp, AST_ALERT_FD, ast_channel_internal_alert_readfd(tmp));
871 /* And timing pipe */
872 ast_channel_set_fd(tmp, AST_TIMING_FD, ast_channel_timingfd(tmp));
875 ast_channel_state_set(tmp, state);
876 ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
878 ast_channel_streamid_set(tmp, -1);
880 ast_channel_fin_set(tmp, global_fin);
881 ast_channel_fout_set(tmp, global_fout);
884 ast_channel_creationtime_set(tmp, &now);
886 ast_channel_internal_setup_topics(tmp);
888 if (!ast_strlen_zero(name_fmt)) {
889 char *slash, *slash2;
890 /* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call.
891 * And they all use slightly different formats for their name string.
892 * This means, to set the name here, we have to accept variable args, and call the string_field_build from here.
893 * This means, that the stringfields must have a routine that takes the va_lists directly, and
894 * uses them to build the string, instead of forming the va_lists internally from the vararg ... list.
895 * This new function was written so this can be accomplished.
897 ast_channel_name_build_va(tmp, name_fmt, ap);
898 tech = ast_strdupa(ast_channel_name(tmp));
899 if ((slash = strchr(tech, '/'))) {
900 if ((slash2 = strchr(slash + 1, '/'))) {
908 * Start the string with '-' so it becomes an empty string
911 ast_channel_name_set(tmp, "-**Unknown**");
914 if (amaflag != AST_AMA_NONE) {
915 ast_channel_amaflags_set(tmp, amaflag);
917 ast_channel_amaflags_set(tmp, DEFAULT_AMA_FLAGS);
920 if (!ast_strlen_zero(acctcode)) {
921 ast_channel_accountcode_set(tmp, acctcode);
923 ast_channel_language_set(tmp, ast_defaultlanguage);
925 ast_channel_context_set(tmp, S_OR(context, "default"));
926 ast_channel_exten_set(tmp, S_OR(exten, "s"));
927 ast_channel_priority_set(tmp, 1);
929 headp = ast_channel_varshead(tmp);
930 AST_LIST_HEAD_INIT_NOLOCK(headp);
932 ast_pbx_hangup_handler_init(tmp);
933 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
934 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_autochans(tmp));
936 channel_tech = ast_get_channel_tech(tech);
937 if (!channel_tech && !ast_strlen_zero(tech2)) {
938 channel_tech = ast_get_channel_tech(tech2);
941 ast_channel_tech_set(tmp, channel_tech);
943 ast_channel_tech_set(tmp, &null_tech);
946 ast_channel_internal_finalize(tmp);
948 ast_atomic_fetchadd_int(&chancount, +1);
950 /* You might scream "locking inversion" at seeing this but it is actually perfectly fine.
951 * Since the channel was just created nothing can know about it yet or even acquire it.
953 ast_channel_lock(tmp);
955 ao2_link(channels, tmp);
958 ast_endpoint_add_channel(endpoint, tmp);
962 * And now, since the channel structure is built, and has its name, let
963 * the world know of its existance
965 ast_channel_stage_snapshot_done(tmp);
969 struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
970 const char *cid_name, const char *acctcode,
971 const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
972 const struct ast_channel *requestor, enum ama_flags amaflag,
973 struct ast_endpoint *endpoint,
974 const char *file, int line, const char *function,
975 const char *name_fmt, ...)
978 struct ast_channel *result;
980 va_start(ap, name_fmt);
981 result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
982 assignedids, requestor, amaflag, endpoint, file, line, function, name_fmt, ap);
988 /* only do the minimum amount of work needed here to make a channel
989 * structure that can be used to expand channel vars */
990 #if defined(REF_DEBUG) || defined(__AST_DEBUG_MALLOC)
991 struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
993 struct ast_channel *ast_dummy_channel_alloc(void)
996 struct ast_channel *tmp;
997 struct varshead *headp;
999 if (!(tmp = ast_channel_internal_alloc(ast_dummy_channel_destructor, NULL, NULL))) {
1000 /* Dummy channel structure allocation failure. */
1004 ast_pbx_hangup_handler_init(tmp);
1005 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
1008 * Init file descriptors to unopened state just in case
1009 * autoservice is called on the channel or something tries to
1010 * read a frame from it.
1012 ast_channel_timingfd_set(tmp, -1);
1013 ast_channel_internal_alertpipe_clear(tmp);
1014 ast_channel_internal_fd_clear_all(tmp);
1016 ast_channel_epfd_set(tmp, -1);
1019 ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
1021 ast_channel_internal_setup_topics(tmp);
1023 headp = ast_channel_varshead(tmp);
1024 AST_LIST_HEAD_INIT_NOLOCK(headp);
1029 static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
1031 struct ast_frame *f;
1032 struct ast_frame *cur;
1033 unsigned int new_frames = 0;
1034 unsigned int new_voice_frames = 0;
1035 unsigned int queued_frames = 0;
1036 unsigned int queued_voice_frames = 0;
1037 AST_LIST_HEAD_NOLOCK(,ast_frame) frames;
1039 ast_channel_lock(chan);
1042 * Check the last frame on the queue if we are queuing the new
1045 cur = AST_LIST_LAST(ast_channel_readq(chan));
1046 if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
1047 switch (cur->subclass.integer) {
1048 case AST_CONTROL_END_OF_Q:
1049 if (fin->frametype == AST_FRAME_CONTROL
1050 && fin->subclass.integer == AST_CONTROL_HANGUP) {
1052 * Destroy the end-of-Q marker frame so we can queue the hangup
1053 * frame in its place.
1055 AST_LIST_REMOVE(ast_channel_readq(chan), cur, frame_list);
1059 * This has degenerated to a normal queue append anyway. Since
1060 * we just destroyed the last frame in the queue we must make
1061 * sure that "after" is NULL or bad things will happen.
1067 case AST_CONTROL_HANGUP:
1068 /* Don't queue anything. */
1069 ast_channel_unlock(chan);
1076 /* Build copies of all the new frames and count them */
1077 AST_LIST_HEAD_INIT_NOLOCK(&frames);
1078 for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
1079 if (!(f = ast_frdup(cur))) {
1080 if (AST_LIST_FIRST(&frames)) {
1081 ast_frfree(AST_LIST_FIRST(&frames));
1083 ast_channel_unlock(chan);
1087 AST_LIST_INSERT_TAIL(&frames, f, frame_list);
1089 if (f->frametype == AST_FRAME_VOICE) {
1094 /* Count how many frames exist on the queue */
1095 AST_LIST_TRAVERSE(ast_channel_readq(chan), cur, frame_list) {
1097 if (cur->frametype == AST_FRAME_VOICE) {
1098 queued_voice_frames++;
1102 if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
1104 ast_log(LOG_WARNING, "Exceptionally long %squeue length queuing to %s\n", queued_frames + new_frames > 128 ? "" : "voice ", ast_channel_name(chan));
1105 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), cur, frame_list) {
1106 /* Save the most recent frame */
1107 if (!AST_LIST_NEXT(cur, frame_list)) {
1109 } else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
1113 AST_LIST_REMOVE_CURRENT(frame_list);
1117 AST_LIST_TRAVERSE_SAFE_END;
1121 AST_LIST_INSERT_LIST_AFTER(ast_channel_readq(chan), &frames, after, frame_list);
1124 AST_LIST_APPEND_LIST(&frames, ast_channel_readq(chan), frame_list);
1125 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_readq(chan));
1127 AST_LIST_APPEND_LIST(ast_channel_readq(chan), &frames, frame_list);
1130 if (ast_channel_alert_writable(chan)) {
1131 if (ast_channel_alert_write(chan)) {
1132 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %u): %s!\n",
1133 ast_channel_name(chan), queued_frames, strerror(errno));
1135 } else if (ast_channel_timingfd(chan) > -1) {
1136 ast_timer_enable_continuous(ast_channel_timer(chan));
1137 } else if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
1138 pthread_kill(ast_channel_blocker(chan), SIGURG);
1141 ast_channel_unlock(chan);
1146 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
1148 return __ast_queue_frame(chan, fin, 0, NULL);
1151 int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
1153 return __ast_queue_frame(chan, fin, 1, NULL);
1156 /*! \brief Queue a hangup frame for channel */
1157 int ast_queue_hangup(struct ast_channel *chan)
1159 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1162 /* Yeah, let's not change a lock-critical value without locking */
1163 ast_channel_lock(chan);
1164 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1165 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), NULL);
1167 res = ast_queue_frame(chan, &f);
1168 ast_channel_unlock(chan);
1172 /*! \brief Queue a hangup frame for channel */
1173 int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
1175 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1176 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1180 f.data.uint32 = cause;
1183 /* Yeah, let's not change a lock-critical value without locking */
1184 ast_channel_lock(chan);
1185 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1187 f.data.uint32 = ast_channel_hangupcause(chan);
1189 blob = ast_json_pack("{s: i}",
1191 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
1193 res = ast_queue_frame(chan, &f);
1194 ast_channel_unlock(chan);
1198 int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
1200 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HOLD };
1201 struct ast_json *blob = NULL;
1204 if (!ast_strlen_zero(musicclass)) {
1205 f.data.ptr = (void *) musicclass;
1206 f.datalen = strlen(musicclass) + 1;
1208 blob = ast_json_pack("{s: s}",
1209 "musicclass", musicclass);
1212 ast_channel_publish_cached_blob(chan, ast_channel_hold_type(), blob);
1214 res = ast_queue_frame(chan, &f);
1216 ast_json_unref(blob);
1221 int ast_queue_unhold(struct ast_channel *chan)
1223 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_UNHOLD };
1226 ast_channel_publish_cached_blob(chan, ast_channel_unhold_type(), NULL);
1228 res = ast_queue_frame(chan, &f);
1233 /*! \brief Queue a control frame */
1234 int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
1236 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
1237 return ast_queue_frame(chan, &f);
1240 /*! \brief Queue a control frame with payload */
1241 int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control,
1242 const void *data, size_t datalen)
1244 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
1245 return ast_queue_frame(chan, &f);
1248 /*! \brief Set defer DTMF flag on channel */
1249 int ast_channel_defer_dtmf(struct ast_channel *chan)
1254 pre = ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1255 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1260 /*! \brief Unset defer DTMF flag on channel */
1261 void ast_channel_undefer_dtmf(struct ast_channel *chan)
1264 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1267 struct ast_channel *ast_channel_callback(ao2_callback_data_fn *cb_fn, void *arg,
1268 void *data, int ao2_flags)
1270 return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
1273 static int ast_channel_by_name_cb(void *obj, void *arg, void *data, int flags)
1275 struct ast_channel *chan = obj;
1276 const char *name = arg;
1277 size_t name_len = *(size_t *) data;
1278 int ret = CMP_MATCH;
1280 if (ast_strlen_zero(name)) {
1281 ast_log(LOG_ERROR, "BUG! Must supply a channel name or partial name to match!\n");
1285 ast_channel_lock(chan);
1286 if ((!name_len && strcasecmp(ast_channel_name(chan), name))
1287 || (name_len && strncasecmp(ast_channel_name(chan), name, name_len))) {
1288 ret = 0; /* name match failed, keep looking */
1290 ast_channel_unlock(chan);
1295 static int ast_channel_by_exten_cb(void *obj, void *arg, void *data, int flags)
1297 struct ast_channel *chan = obj;
1298 char *context = arg;
1300 int ret = CMP_MATCH;
1302 if (ast_strlen_zero(exten) || ast_strlen_zero(context)) {
1303 ast_log(LOG_ERROR, "BUG! Must have a context and extension to match!\n");
1307 ast_channel_lock(chan);
1308 if (strcasecmp(ast_channel_context(chan), context) && strcasecmp(ast_channel_macrocontext(chan), context)) {
1309 ret = 0; /* Context match failed, continue */
1310 } else if (strcasecmp(ast_channel_exten(chan), exten) && strcasecmp(ast_channel_macroexten(chan), exten)) {
1311 ret = 0; /* Extension match failed, continue */
1313 ast_channel_unlock(chan);
1318 static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags)
1320 struct ast_channel *chan = obj;
1321 char *uniqueid = arg;
1322 size_t id_len = *(size_t *) data;
1323 int ret = CMP_MATCH;
1325 if (ast_strlen_zero(uniqueid)) {
1326 ast_log(LOG_ERROR, "BUG! Must supply a uniqueid or partial uniqueid to match!\n");
1330 ast_channel_lock(chan);
1331 if ((!id_len && strcasecmp(ast_channel_uniqueid(chan), uniqueid))
1332 || (id_len && strncasecmp(ast_channel_uniqueid(chan), uniqueid, id_len))) {
1333 ret = 0; /* uniqueid match failed, keep looking */
1335 ast_channel_unlock(chan);
1340 struct ast_channel_iterator {
1341 /* storage for non-dynamically allocated iterator */
1342 struct ao2_iterator simple_iterator;
1343 /* pointer to the actual iterator (simple_iterator or a dynamically
1344 * allocated iterator)
1346 struct ao2_iterator *active_iterator;
1349 struct ast_channel_iterator *ast_channel_iterator_destroy(struct ast_channel_iterator *i)
1351 ao2_iterator_destroy(i->active_iterator);
1357 struct ast_channel_iterator *ast_channel_iterator_by_exten_new(const char *exten, const char *context)
1359 struct ast_channel_iterator *i;
1360 char *l_exten = (char *) exten;
1361 char *l_context = (char *) context;
1363 if (!(i = ast_calloc(1, sizeof(*i)))) {
1367 i->active_iterator = (void *) ast_channel_callback(ast_channel_by_exten_cb,
1368 l_context, l_exten, OBJ_MULTIPLE);
1369 if (!i->active_iterator) {
1377 struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, size_t name_len)
1379 struct ast_channel_iterator *i;
1380 char *l_name = (char *) name;
1382 if (!(i = ast_calloc(1, sizeof(*i)))) {
1386 i->active_iterator = (void *) ast_channel_callback(ast_channel_by_name_cb,
1388 OBJ_MULTIPLE | (name_len == 0 /* match the whole word, so optimize */ ? OBJ_KEY : 0));
1389 if (!i->active_iterator) {
1397 struct ast_channel_iterator *ast_channel_iterator_all_new(void)
1399 struct ast_channel_iterator *i;
1401 if (!(i = ast_calloc(1, sizeof(*i)))) {
1405 i->simple_iterator = ao2_iterator_init(channels, 0);
1406 i->active_iterator = &i->simple_iterator;
1411 struct ast_channel *ast_channel_iterator_next(struct ast_channel_iterator *i)
1413 return ao2_iterator_next(i->active_iterator);
1416 /* Legacy function, not currently used for lookups, but we need a cmp_fn */
1417 static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
1419 ast_log(LOG_ERROR, "BUG! Should never be called!\n");
1423 struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
1425 struct ast_channel *chan;
1426 char *l_name = (char *) name;
1428 chan = ast_channel_callback(ast_channel_by_name_cb, l_name, &name_len,
1429 (name_len == 0) /* optimize if it is a complete name match */ ? OBJ_KEY : 0);
1434 if (ast_strlen_zero(l_name)) {
1435 /* We didn't have a name to search for so quit. */
1439 /* Now try a search for uniqueid. */
1440 return ast_channel_callback(ast_channel_by_uniqueid_cb, l_name, &name_len, 0);
1443 struct ast_channel *ast_channel_get_by_name(const char *name)
1445 return ast_channel_get_by_name_prefix(name, 0);
1448 struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
1450 char *l_exten = (char *) exten;
1451 char *l_context = (char *) context;
1453 return ast_channel_callback(ast_channel_by_exten_cb, l_context, l_exten, 0);
1456 int ast_is_deferrable_frame(const struct ast_frame *frame)
1458 /* Do not add a default entry in this switch statement. Each new
1459 * frame type should be addressed directly as to whether it should
1460 * be queued up or not.
1462 switch (frame->frametype) {
1463 case AST_FRAME_BRIDGE_ACTION:
1464 case AST_FRAME_BRIDGE_ACTION_SYNC:
1465 case AST_FRAME_CONTROL:
1466 case AST_FRAME_TEXT:
1467 case AST_FRAME_IMAGE:
1468 case AST_FRAME_HTML:
1471 case AST_FRAME_DTMF_END:
1472 case AST_FRAME_DTMF_BEGIN:
1473 case AST_FRAME_VOICE:
1474 case AST_FRAME_VIDEO:
1475 case AST_FRAME_NULL:
1478 case AST_FRAME_MODEM:
1484 /*! \brief Wait, look for hangups and condition arg */
1485 int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data)
1487 struct ast_frame *f;
1488 struct ast_silence_generator *silgen = NULL;
1490 struct timeval start;
1492 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
1494 AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
1496 /* If no other generator is present, start silencegen while waiting */
1497 if (ast_opt_transmit_silence && !ast_channel_generatordata(chan)) {
1498 silgen = ast_channel_start_silence_generator(chan);
1501 start = ast_tvnow();
1502 while ((ms = ast_remaining_ms(start, timeout_ms))) {
1503 struct ast_frame *dup_f = NULL;
1505 if (cond && ((*cond)(data) == 0)) {
1508 ms = ast_waitfor(chan, ms);
1520 if (!ast_is_deferrable_frame(f)) {
1525 if ((dup_f = ast_frisolate(f))) {
1529 AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
1534 /* stop silgen if present */
1536 ast_channel_stop_silence_generator(chan, silgen);
1539 /* We need to free all the deferred frames, but we only need to
1540 * queue the deferred frames if there was no error and no
1541 * hangup was received
1543 ast_channel_lock(chan);
1544 while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
1546 ast_queue_frame_head(chan, f);
1550 ast_channel_unlock(chan);
1555 /*! \brief Wait, look for hangups */
1556 int ast_safe_sleep(struct ast_channel *chan, int ms)
1558 return ast_safe_sleep_conditional(chan, ms, NULL, NULL);
1561 struct ast_channel *ast_channel_release(struct ast_channel *chan)
1563 /* Safe, even if already unlinked. */
1564 ao2_unlink(channels, chan);
1565 return ast_channel_unref(chan);
1568 void ast_party_name_init(struct ast_party_name *init)
1571 init->char_set = AST_PARTY_CHAR_SET_ISO8859_1;
1572 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1576 void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
1579 /* Don't copy to self */
1583 ast_free(dest->str);
1584 dest->str = ast_strdup(src->str);
1585 dest->char_set = src->char_set;
1586 dest->presentation = src->presentation;
1587 dest->valid = src->valid;
1590 void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
1593 init->char_set = guide->char_set;
1594 init->presentation = guide->presentation;
1595 init->valid = guide->valid;
1598 void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
1601 /* Don't set to self */
1605 if (src->str && src->str != dest->str) {
1606 ast_free(dest->str);
1607 dest->str = ast_strdup(src->str);
1610 dest->char_set = src->char_set;
1611 dest->presentation = src->presentation;
1612 dest->valid = src->valid;
1615 void ast_party_name_free(struct ast_party_name *doomed)
1617 ast_free(doomed->str);
1621 void ast_party_number_init(struct ast_party_number *init)
1624 init->plan = 0;/* Unknown */
1625 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1629 void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
1632 /* Don't copy to self */
1636 ast_free(dest->str);
1637 dest->str = ast_strdup(src->str);
1638 dest->plan = src->plan;
1639 dest->presentation = src->presentation;
1640 dest->valid = src->valid;
1643 void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
1646 init->plan = guide->plan;
1647 init->presentation = guide->presentation;
1648 init->valid = guide->valid;
1651 void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
1654 /* Don't set to self */
1658 if (src->str && src->str != dest->str) {
1659 ast_free(dest->str);
1660 dest->str = ast_strdup(src->str);
1663 dest->plan = src->plan;
1664 dest->presentation = src->presentation;
1665 dest->valid = src->valid;
1668 void ast_party_number_free(struct ast_party_number *doomed)
1670 ast_free(doomed->str);
1674 void ast_party_subaddress_init(struct ast_party_subaddress *init)
1678 init->odd_even_indicator = 0;
1682 void ast_party_subaddress_copy(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1685 /* Don't copy to self */
1689 ast_free(dest->str);
1690 dest->str = ast_strdup(src->str);
1691 dest->type = src->type;
1692 dest->odd_even_indicator = src->odd_even_indicator;
1693 dest->valid = src->valid;
1696 void ast_party_subaddress_set_init(struct ast_party_subaddress *init, const struct ast_party_subaddress *guide)
1699 init->type = guide->type;
1700 init->odd_even_indicator = guide->odd_even_indicator;
1701 init->valid = guide->valid;
1704 void ast_party_subaddress_set(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1707 /* Don't set to self */
1711 if (src->str && src->str != dest->str) {
1712 ast_free(dest->str);
1713 dest->str = ast_strdup(src->str);
1716 dest->type = src->type;
1717 dest->odd_even_indicator = src->odd_even_indicator;
1718 dest->valid = src->valid;
1721 void ast_party_subaddress_free(struct ast_party_subaddress *doomed)
1723 ast_free(doomed->str);
1727 void ast_set_party_id_all(struct ast_set_party_id *update_id)
1729 update_id->name = 1;
1730 update_id->number = 1;
1731 update_id->subaddress = 1;
1734 void ast_party_id_init(struct ast_party_id *init)
1736 ast_party_name_init(&init->name);
1737 ast_party_number_init(&init->number);
1738 ast_party_subaddress_init(&init->subaddress);
1742 void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
1745 /* Don't copy to self */
1749 ast_party_name_copy(&dest->name, &src->name);
1750 ast_party_number_copy(&dest->number, &src->number);
1751 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1753 ast_free(dest->tag);
1754 dest->tag = ast_strdup(src->tag);
1757 void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
1759 ast_party_name_set_init(&init->name, &guide->name);
1760 ast_party_number_set_init(&init->number, &guide->number);
1761 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
1765 void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
1768 /* Don't set to self */
1772 if (!update || update->name) {
1773 ast_party_name_set(&dest->name, &src->name);
1775 if (!update || update->number) {
1776 ast_party_number_set(&dest->number, &src->number);
1778 if (!update || update->subaddress) {
1779 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
1782 if (src->tag && src->tag != dest->tag) {
1783 ast_free(dest->tag);
1784 dest->tag = ast_strdup(src->tag);
1788 void ast_party_id_free(struct ast_party_id *doomed)
1790 ast_party_name_free(&doomed->name);
1791 ast_party_number_free(&doomed->number);
1792 ast_party_subaddress_free(&doomed->subaddress);
1794 ast_free(doomed->tag);
1798 int ast_party_id_presentation(const struct ast_party_id *id)
1800 int number_priority;
1802 int number_screening;
1806 /* Determine name presentation priority. */
1807 if (!id->name.valid) {
1808 name_value = AST_PRES_UNAVAILABLE;
1811 name_value = id->name.presentation & AST_PRES_RESTRICTION;
1812 switch (name_value) {
1813 case AST_PRES_RESTRICTED:
1816 case AST_PRES_ALLOWED:
1819 case AST_PRES_UNAVAILABLE:
1823 name_value = AST_PRES_UNAVAILABLE;
1829 /* Determine number presentation priority. */
1830 if (!id->number.valid) {
1831 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1832 number_value = AST_PRES_UNAVAILABLE;
1833 number_priority = 3;
1835 number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
1836 number_value = id->number.presentation & AST_PRES_RESTRICTION;
1837 switch (number_value) {
1838 case AST_PRES_RESTRICTED:
1839 number_priority = 0;
1841 case AST_PRES_ALLOWED:
1842 number_priority = 1;
1844 case AST_PRES_UNAVAILABLE:
1845 number_priority = 2;
1848 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1849 number_value = AST_PRES_UNAVAILABLE;
1850 number_priority = 3;
1855 /* Select the wining presentation value. */
1856 if (name_priority < number_priority) {
1857 number_value = name_value;
1859 if (number_value == AST_PRES_UNAVAILABLE) {
1860 return AST_PRES_NUMBER_NOT_AVAILABLE;
1863 return number_value | number_screening;
1866 void ast_party_id_invalidate(struct ast_party_id *id)
1869 id->number.valid = 0;
1870 id->subaddress.valid = 0;
1873 void ast_party_id_reset(struct ast_party_id *id)
1875 ast_party_id_free(id);
1876 ast_party_id_init(id);
1879 struct ast_party_id ast_party_id_merge(struct ast_party_id *base, struct ast_party_id *overlay)
1881 struct ast_party_id merged;
1884 if (overlay->name.valid) {
1885 merged.name = overlay->name;
1887 if (overlay->number.valid) {
1888 merged.number = overlay->number;
1890 if (overlay->subaddress.valid) {
1891 merged.subaddress = overlay->subaddress;
1893 /* Note the actual structure is returned and not a pointer to it! */
1897 void ast_party_id_merge_copy(struct ast_party_id *dest, struct ast_party_id *base, struct ast_party_id *overlay)
1899 struct ast_party_id merged;
1901 merged = ast_party_id_merge(base, overlay);
1902 ast_party_id_copy(dest, &merged);
1905 void ast_party_dialed_init(struct ast_party_dialed *init)
1907 init->number.str = NULL;
1908 init->number.plan = 0;/* Unknown */
1909 ast_party_subaddress_init(&init->subaddress);
1910 init->transit_network_select = 0;
1913 void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1916 /* Don't copy to self */
1920 ast_free(dest->number.str);
1921 dest->number.str = ast_strdup(src->number.str);
1922 dest->number.plan = src->number.plan;
1923 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1924 dest->transit_network_select = src->transit_network_select;
1927 void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
1929 init->number.str = NULL;
1930 init->number.plan = guide->number.plan;
1931 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
1932 init->transit_network_select = guide->transit_network_select;
1935 void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1937 if (src->number.str && src->number.str != dest->number.str) {
1938 ast_free(dest->number.str);
1939 dest->number.str = ast_strdup(src->number.str);
1941 dest->number.plan = src->number.plan;
1943 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
1945 dest->transit_network_select = src->transit_network_select;
1948 void ast_party_dialed_free(struct ast_party_dialed *doomed)
1950 ast_free(doomed->number.str);
1951 doomed->number.str = NULL;
1952 ast_party_subaddress_free(&doomed->subaddress);
1955 void ast_party_caller_init(struct ast_party_caller *init)
1957 ast_party_id_init(&init->id);
1958 ast_party_id_init(&init->ani);
1959 ast_party_id_init(&init->priv);
1963 void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
1966 /* Don't copy to self */
1970 ast_party_id_copy(&dest->id, &src->id);
1971 ast_party_id_copy(&dest->ani, &src->ani);
1972 ast_party_id_copy(&dest->priv, &src->priv);
1973 dest->ani2 = src->ani2;
1976 void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
1978 ast_party_id_set_init(&init->id, &guide->id);
1979 ast_party_id_set_init(&init->ani, &guide->ani);
1980 ast_party_id_set_init(&init->priv, &guide->priv);
1981 init->ani2 = guide->ani2;
1984 void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
1986 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
1987 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
1988 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
1989 dest->ani2 = src->ani2;
1992 void ast_party_caller_free(struct ast_party_caller *doomed)
1994 ast_party_id_free(&doomed->id);
1995 ast_party_id_free(&doomed->ani);
1996 ast_party_id_free(&doomed->priv);
1999 void ast_party_connected_line_init(struct ast_party_connected_line *init)
2001 ast_party_id_init(&init->id);
2002 ast_party_id_init(&init->ani);
2003 ast_party_id_init(&init->priv);
2005 init->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2008 void ast_party_connected_line_copy(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
2011 /* Don't copy to self */
2015 ast_party_id_copy(&dest->id, &src->id);
2016 ast_party_id_copy(&dest->ani, &src->ani);
2017 ast_party_id_copy(&dest->priv, &src->priv);
2018 dest->ani2 = src->ani2;
2019 dest->source = src->source;
2022 void ast_party_connected_line_set_init(struct ast_party_connected_line *init, const struct ast_party_connected_line *guide)
2024 ast_party_id_set_init(&init->id, &guide->id);
2025 ast_party_id_set_init(&init->ani, &guide->ani);
2026 ast_party_id_set_init(&init->priv, &guide->priv);
2027 init->ani2 = guide->ani2;
2028 init->source = guide->source;
2031 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)
2033 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2034 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2035 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2036 dest->ani2 = src->ani2;
2037 dest->source = src->source;
2040 void ast_party_connected_line_collect_caller(struct ast_party_connected_line *connected, struct ast_party_caller *caller)
2042 connected->id = caller->id;
2043 connected->ani = caller->ani;
2044 connected->priv = caller->priv;
2045 connected->ani2 = caller->ani2;
2046 connected->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2049 void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
2051 ast_party_id_free(&doomed->id);
2052 ast_party_id_free(&doomed->ani);
2053 ast_party_id_free(&doomed->priv);
2056 void ast_party_redirecting_reason_init(struct ast_party_redirecting_reason *init)
2059 init->code = AST_REDIRECTING_REASON_UNKNOWN;
2062 void ast_party_redirecting_reason_copy(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2068 ast_free(dest->str);
2069 dest->str = ast_strdup(src->str);
2070 dest->code = src->code;
2073 void ast_party_redirecting_reason_set_init(struct ast_party_redirecting_reason *init, const struct ast_party_redirecting_reason *guide)
2076 init->code = guide->code;
2079 void ast_party_redirecting_reason_set(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2085 if (src->str && src->str != dest->str) {
2086 ast_free(dest->str);
2087 dest->str = ast_strdup(src->str);
2090 dest->code = src->code;
2093 void ast_party_redirecting_reason_free(struct ast_party_redirecting_reason *doomed)
2095 ast_free(doomed->str);
2099 void ast_party_redirecting_init(struct ast_party_redirecting *init)
2101 ast_party_id_init(&init->orig);
2102 ast_party_id_init(&init->from);
2103 ast_party_id_init(&init->to);
2104 ast_party_id_init(&init->priv_orig);
2105 ast_party_id_init(&init->priv_from);
2106 ast_party_id_init(&init->priv_to);
2107 ast_party_redirecting_reason_init(&init->reason);
2108 ast_party_redirecting_reason_init(&init->orig_reason);
2112 void ast_party_redirecting_copy(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src)
2115 /* Don't copy to self */
2119 ast_party_id_copy(&dest->orig, &src->orig);
2120 ast_party_id_copy(&dest->from, &src->from);
2121 ast_party_id_copy(&dest->to, &src->to);
2122 ast_party_id_copy(&dest->priv_orig, &src->priv_orig);
2123 ast_party_id_copy(&dest->priv_from, &src->priv_from);
2124 ast_party_id_copy(&dest->priv_to, &src->priv_to);
2125 ast_party_redirecting_reason_copy(&dest->reason, &src->reason);
2126 ast_party_redirecting_reason_copy(&dest->orig_reason, &src->orig_reason);
2127 dest->count = src->count;
2130 void ast_party_redirecting_set_init(struct ast_party_redirecting *init, const struct ast_party_redirecting *guide)
2132 ast_party_id_set_init(&init->orig, &guide->orig);
2133 ast_party_id_set_init(&init->from, &guide->from);
2134 ast_party_id_set_init(&init->to, &guide->to);
2135 ast_party_id_set_init(&init->priv_orig, &guide->priv_orig);
2136 ast_party_id_set_init(&init->priv_from, &guide->priv_from);
2137 ast_party_id_set_init(&init->priv_to, &guide->priv_to);
2138 ast_party_redirecting_reason_set_init(&init->reason, &guide->reason);
2139 ast_party_redirecting_reason_set_init(&init->orig_reason, &guide->orig_reason);
2140 init->count = guide->count;
2143 void ast_party_redirecting_set(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src, const struct ast_set_party_redirecting *update)
2145 ast_party_id_set(&dest->orig, &src->orig, update ? &update->orig : NULL);
2146 ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
2147 ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
2148 ast_party_id_set(&dest->priv_orig, &src->priv_orig, update ? &update->priv_orig : NULL);
2149 ast_party_id_set(&dest->priv_from, &src->priv_from, update ? &update->priv_from : NULL);
2150 ast_party_id_set(&dest->priv_to, &src->priv_to, update ? &update->priv_to : NULL);
2151 ast_party_redirecting_reason_set(&dest->reason, &src->reason);
2152 ast_party_redirecting_reason_set(&dest->orig_reason, &src->orig_reason);
2153 dest->count = src->count;
2156 void ast_party_redirecting_free(struct ast_party_redirecting *doomed)
2158 ast_party_id_free(&doomed->orig);
2159 ast_party_id_free(&doomed->from);
2160 ast_party_id_free(&doomed->to);
2161 ast_party_id_free(&doomed->priv_orig);
2162 ast_party_id_free(&doomed->priv_from);
2163 ast_party_id_free(&doomed->priv_to);
2164 ast_party_redirecting_reason_free(&doomed->reason);
2165 ast_party_redirecting_reason_free(&doomed->orig_reason);
2168 /*! \brief Free a channel structure */
2169 static void ast_channel_destructor(void *obj)
2171 struct ast_channel *chan = obj;
2175 struct ast_var_t *vardata;
2176 struct ast_frame *f;
2177 struct varshead *headp;
2178 struct ast_datastore *datastore;
2179 char device_name[AST_CHANNEL_NAME];
2182 /* Stop monitoring */
2183 if (ast_channel_monitor(chan)) {
2184 ast_channel_monitor(chan)->stop(chan, 0);
2187 /* If there is native format music-on-hold state, free it */
2188 if (ast_channel_music_state(chan)) {
2189 ast_moh_cleanup(chan);
2192 ast_pbx_hangup_handler_destroy(chan);
2194 /* Things that may possibly raise Stasis messages shouldn't occur after this point */
2195 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEAD);
2197 if (ast_channel_internal_is_finalized(chan)) {
2198 /* A channel snapshot should not be in the process of being staged now. */
2199 ast_assert(!ast_test_flag(ast_channel_flags(chan), AST_FLAG_SNAPSHOT_STAGE));
2201 ast_channel_lock(chan);
2202 ast_channel_publish_snapshot(chan);
2203 ast_channel_unlock(chan);
2204 publish_cache_clear(chan);
2207 ast_channel_lock(chan);
2209 /* Get rid of each of the data stores on the channel */
2210 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry)))
2211 /* Free the data store */
2212 ast_datastore_free(datastore);
2214 /* While the channel is locked, take the reference to its callid while we tear down the call. */
2215 callid = ast_channel_callid(chan);
2216 ast_channel_callid_cleanup(chan);
2218 ast_channel_unlock(chan);
2220 /* Lock and unlock the channel just to be sure nobody has it locked still
2221 due to a reference that was stored in a datastore. (i.e. app_chanspy) */
2222 ast_channel_lock(chan);
2223 ast_channel_unlock(chan);
2225 if (ast_channel_tech_pvt(chan)) {
2226 ast_log_callid(LOG_WARNING, callid, "Channel '%s' may not have been hung up properly\n", ast_channel_name(chan));
2227 ast_free(ast_channel_tech_pvt(chan));
2230 if (ast_channel_sched(chan)) {
2231 ast_sched_context_destroy(ast_channel_sched(chan));
2234 if (ast_channel_internal_is_finalized(chan)) {
2237 ast_copy_string(device_name, ast_channel_name(chan), sizeof(device_name));
2238 if ((dashptr = strrchr(device_name, '-'))) {
2242 device_name[0] = '\0';
2245 /* Free translators */
2246 if (ast_channel_readtrans(chan))
2247 ast_translator_free_path(ast_channel_readtrans(chan));
2248 if (ast_channel_writetrans(chan))
2249 ast_translator_free_path(ast_channel_writetrans(chan));
2250 if (ast_channel_pbx(chan))
2251 ast_log_callid(LOG_WARNING, callid, "PBX may not have been terminated properly on '%s'\n", ast_channel_name(chan));
2254 ast_channel_set_oldwriteformat(chan, NULL);
2255 ast_channel_set_rawreadformat(chan, NULL);
2256 ast_channel_set_rawwriteformat(chan, NULL);
2257 ast_channel_set_readformat(chan, NULL);
2258 ast_channel_set_writeformat(chan, NULL);
2260 ast_party_dialed_free(ast_channel_dialed(chan));
2261 ast_party_caller_free(ast_channel_caller(chan));
2262 ast_party_connected_line_free(ast_channel_connected(chan));
2263 ast_party_connected_line_free(ast_channel_connected_indicated(chan));
2264 ast_party_redirecting_free(ast_channel_redirecting(chan));
2266 /* Close pipes if appropriate */
2267 ast_channel_internal_alertpipe_close(chan);
2268 if (ast_channel_timer(chan)) {
2269 ast_timer_close(ast_channel_timer(chan));
2270 ast_channel_timer_set(chan, NULL);
2273 for (i = 0; i < AST_MAX_FDS; i++) {
2274 if (ast_channel_internal_epfd_data(chan, i)) {
2275 ast_free(ast_channel_internal_epfd_data(chan, i));
2278 close(ast_channel_epfd(chan));
2280 while ((f = AST_LIST_REMOVE_HEAD(ast_channel_readq(chan), frame_list)))
2283 /* loop over the variables list, freeing all data and deleting list items */
2284 /* no need to lock the list, as the channel is already locked */
2285 headp = ast_channel_varshead(chan);
2286 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2287 ast_var_delete(vardata);
2289 ast_app_group_discard(chan);
2291 /* Destroy the jitterbuffer */
2292 ast_jb_destroy(chan);
2294 if (ast_channel_cdr(chan)) {
2295 ast_cdr_free(ast_channel_cdr(chan));
2296 ast_channel_cdr_set(chan, NULL);
2299 if (ast_channel_zone(chan)) {
2300 ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan)));
2303 ast_channel_internal_cleanup(chan);
2305 if (device_name[0]) {
2307 * We have a device name to notify of a new state.
2309 * Queue an unknown state, because, while we know that this particular
2310 * instance is dead, we don't know the state of all other possible
2313 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);
2316 ast_channel_nativeformats_set(chan, NULL);
2318 ast_channel_named_callgroups_set(chan, NULL);
2319 ast_channel_named_pickupgroups_set(chan, NULL);
2321 ast_atomic_fetchadd_int(&chancount, -1);
2324 /*! \brief Free a dummy channel structure */
2325 static void ast_dummy_channel_destructor(void *obj)
2327 struct ast_channel *chan = obj;
2328 struct ast_datastore *datastore;
2329 struct ast_var_t *vardata;
2330 struct varshead *headp;
2332 ast_pbx_hangup_handler_destroy(chan);
2334 /* Get rid of each of the data stores on the channel */
2335 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) {
2336 /* Free the data store */
2337 ast_datastore_free(datastore);
2340 ast_party_dialed_free(ast_channel_dialed(chan));
2341 ast_party_caller_free(ast_channel_caller(chan));
2342 ast_party_connected_line_free(ast_channel_connected(chan));
2343 ast_party_connected_line_free(ast_channel_connected_indicated(chan));
2344 ast_party_redirecting_free(ast_channel_redirecting(chan));
2346 /* loop over the variables list, freeing all data and deleting list items */
2347 /* no need to lock the list, as the channel is already locked */
2348 headp = ast_channel_varshead(chan);
2349 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2350 ast_var_delete(vardata);
2352 if (ast_channel_cdr(chan)) {
2353 ast_cdr_free(ast_channel_cdr(chan));
2354 ast_channel_cdr_set(chan, NULL);
2357 ast_channel_internal_cleanup(chan);
2360 struct ast_datastore *ast_channel_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
2362 return ast_datastore_alloc(info, uid);
2365 int ast_channel_datastore_free(struct ast_datastore *datastore)
2367 return ast_datastore_free(datastore);
2370 int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to)
2372 struct ast_datastore *datastore = NULL, *datastore2;
2374 AST_LIST_TRAVERSE(ast_channel_datastores(from), datastore, entry) {
2375 if (datastore->inheritance > 0) {
2376 datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
2378 datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
2379 datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
2380 AST_LIST_INSERT_TAIL(ast_channel_datastores(to), datastore2, entry);
2387 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
2391 AST_LIST_INSERT_HEAD(ast_channel_datastores(chan), datastore, entry);
2396 int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
2398 return AST_LIST_REMOVE(ast_channel_datastores(chan), datastore, entry) ? 0 : -1;
2401 struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
2403 struct ast_datastore *datastore = NULL;
2408 AST_LIST_TRAVERSE(ast_channel_datastores(chan), datastore, entry) {
2409 if (datastore->info != info) {
2414 /* matched by type only */
2418 if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
2419 /* Matched by type AND uid */
2427 /*! Set the file descriptor on the channel */
2428 void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
2431 struct epoll_event ev;
2432 struct ast_epoll_data *aed = NULL;
2434 if (ast_channel_fd_isset(chan, which)) {
2435 epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_DEL, ast_channel_fd(chan, which), &ev);
2436 aed = ast_channel_internal_epfd_data(chan, which);
2439 /* If this new fd is valid, add it to the epoll */
2441 if (!aed && (!(aed = ast_calloc(1, sizeof(*aed)))))
2444 ast_channel_internal_epfd_data_set(chan, which, aed);
2448 ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2450 epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_ADD, fd, &ev);
2452 /* We don't have to keep around this epoll data structure now */
2454 ast_channel_epfd_data_set(chan, which, NULL);
2457 ast_channel_internal_fd_set(chan, which, fd);
2461 /*! Add a channel to an optimized waitfor */
2462 void ast_poll_channel_add(struct ast_channel *chan0, struct ast_channel *chan1)
2465 struct epoll_event ev;
2468 if (ast_channel_epfd(chan0) == -1)
2471 /* Iterate through the file descriptors on chan1, adding them to chan0 */
2472 for (i = 0; i < AST_MAX_FDS; i++) {
2473 if (!ast_channel_fd_isset(chan1, i)) {
2476 ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2477 ev.data.ptr = ast_channel_internal_epfd_data(chan1, i);
2478 epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_ADD, ast_channel_fd(chan1, i), &ev);
2485 /*! Delete a channel from an optimized waitfor */
2486 void ast_poll_channel_del(struct ast_channel *chan0, struct ast_channel *chan1)
2489 struct epoll_event ev;
2492 if (ast_channel_epfd(chan0) == -1)
2495 for (i = 0; i < AST_MAX_FDS; i++) {
2496 if (!ast_channel_fd_isset(chan1, i)) {
2499 epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_DEL, ast_channel_fd(chan1, i), &ev);
2506 void ast_channel_clear_softhangup(struct ast_channel *chan, int flag)
2508 ast_channel_lock(chan);
2510 ast_channel_softhangup_internal_flag_clear(chan, flag);
2512 if (!ast_channel_softhangup_internal_flag(chan)) {
2513 struct ast_frame *fr;
2515 /* If we have completely cleared the softhangup flag,
2516 * then we need to fully abort the hangup process. This requires
2517 * pulling the END_OF_Q frame out of the channel frame queue if it
2518 * still happens to be there. */
2520 fr = AST_LIST_LAST(ast_channel_readq(chan));
2521 if (fr && fr->frametype == AST_FRAME_CONTROL &&
2522 fr->subclass.integer == AST_CONTROL_END_OF_Q) {
2523 AST_LIST_REMOVE(ast_channel_readq(chan), fr, frame_list);
2528 ast_channel_unlock(chan);
2531 /*! \brief Softly hangup a channel, don't lock */
2532 int ast_softhangup_nolock(struct ast_channel *chan, int cause)
2534 ast_debug(1, "Soft-Hanging (%#04x) up channel '%s'\n", (unsigned)cause, ast_channel_name(chan));
2535 /* Inform channel driver that we need to be hung up, if it cares */
2536 ast_channel_softhangup_internal_flag_add(chan, cause);
2537 ast_queue_frame(chan, &ast_null_frame);
2538 /* Interrupt any poll call or such */
2539 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING))
2540 pthread_kill(ast_channel_blocker(chan), SIGURG);
2544 /*! \brief Softly hangup a channel, lock */
2545 int ast_softhangup(struct ast_channel *chan, int cause)
2547 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2550 ast_channel_lock(chan);
2551 res = ast_softhangup_nolock(chan, cause);
2552 blob = ast_json_pack("{s: i, s: b}",
2555 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
2556 ast_channel_unlock(chan);
2561 static void free_translation(struct ast_channel *clonechan)
2563 if (ast_channel_writetrans(clonechan)) {
2564 ast_translator_free_path(ast_channel_writetrans(clonechan));
2566 if (ast_channel_readtrans(clonechan)) {
2567 ast_translator_free_path(ast_channel_readtrans(clonechan));
2569 ast_channel_writetrans_set(clonechan, NULL);
2570 ast_channel_readtrans_set(clonechan, NULL);
2573 void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
2575 RAII_VAR(struct ast_channel *, bridge, ast_channel_bridge_peer(chan), ast_channel_cleanup);
2577 ast_channel_lock(chan);
2578 if (force || ast_strlen_zero(ast_channel_hangupsource(chan))) {
2579 ast_channel_hangupsource_set(chan, source);
2581 ast_channel_unlock(chan);
2584 ast_channel_lock(bridge);
2585 if (force || ast_strlen_zero(ast_channel_hangupsource(bridge))) {
2586 ast_channel_hangupsource_set(bridge, source);
2588 ast_channel_unlock(bridge);
2592 int ast_channel_has_audio_frame_or_monitor(struct ast_channel *chan)
2594 return ast_channel_monitor(chan)
2595 || !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
2596 || !ast_framehook_list_contains_no_active(ast_channel_framehooks(chan));
2599 int ast_channel_has_hook_requiring_audio(struct ast_channel *chan)
2601 return ast_channel_monitor(chan)
2602 || !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
2603 || !ast_framehook_list_contains_no_active_of_type(ast_channel_framehooks(chan), AST_FRAME_VOICE);
2606 static void destroy_hooks(struct ast_channel *chan)
2608 if (ast_channel_audiohooks(chan)) {
2609 ast_audiohook_detach_list(ast_channel_audiohooks(chan));
2610 ast_channel_audiohooks_set(chan, NULL);
2613 ast_framehook_list_destroy(chan);
2616 /*! \brief Hangup a channel */
2617 void ast_hangup(struct ast_channel *chan)
2619 /* Be NULL safe for RAII_VAR() usage. */
2624 ast_autoservice_stop(chan);
2626 ast_channel_lock(chan);
2628 while (ast_channel_masq(chan) || ast_channel_masqr(chan)) {
2629 CHANNEL_DEADLOCK_AVOIDANCE(chan);
2632 /* Mark as a zombie so a masquerade cannot be setup on this channel. */
2633 ast_set_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE);
2635 ast_channel_unlock(chan);
2638 * XXX if running the hangup handlers here causes problems
2639 * because the handlers take too long to execute, we could move
2640 * the meat of this function into another thread. A thread
2641 * where channels go to die.
2643 * If this is done, ast_autoservice_chan_hangup_peer() will no
2646 ast_pbx_hangup_handler_run(chan);
2647 ao2_unlink(channels, chan);
2648 ast_channel_lock(chan);
2650 destroy_hooks(chan);
2652 free_translation(chan);
2653 /* Close audio stream */
2654 if (ast_channel_stream(chan)) {
2655 ast_closestream(ast_channel_stream(chan));
2656 ast_channel_stream_set(chan, NULL);
2658 /* Close video stream */
2659 if (ast_channel_vstream(chan)) {
2660 ast_closestream(ast_channel_vstream(chan));
2661 ast_channel_vstream_set(chan, NULL);
2663 if (ast_channel_sched(chan)) {
2664 ast_sched_context_destroy(ast_channel_sched(chan));
2665 ast_channel_sched_set(chan, NULL);
2668 if (ast_channel_generatordata(chan)) { /* Clear any tone stuff remaining */
2669 if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2670 ast_channel_generator(chan)->release(chan, ast_channel_generatordata(chan));
2673 ast_channel_generatordata_set(chan, NULL);
2674 ast_channel_generator_set(chan, NULL);
2676 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
2677 ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
2678 "is blocked by thread %ld in procedure %s! Expect a failure\n",
2679 (long) pthread_self(), ast_channel_name(chan), (long)ast_channel_blocker(chan), ast_channel_blockproc(chan));
2680 ast_assert(ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING) == 0);
2683 ast_debug(1, "Hanging up channel '%s'\n", ast_channel_name(chan));
2684 if (ast_channel_tech(chan)->hangup) {
2685 ast_channel_tech(chan)->hangup(chan);
2688 ast_channel_unlock(chan);
2692 ast_channel_unref(chan);
2695 int ast_raw_answer(struct ast_channel *chan)
2698 struct timeval answertime;
2700 ast_channel_lock(chan);
2702 /* You can't answer an outbound call */
2703 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING)) {
2704 ast_channel_unlock(chan);
2708 /* Stop if we're a zombie or need a soft hangup */
2709 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
2710 ast_channel_unlock(chan);
2714 answertime = ast_tvnow();
2715 ast_channel_answertime_set(chan, &answertime);
2717 ast_channel_unlock(chan);
2719 switch (ast_channel_state(chan)) {
2720 case AST_STATE_RINGING:
2721 case AST_STATE_RING:
2722 ast_channel_lock(chan);
2723 if (ast_channel_tech(chan)->answer) {
2724 res = ast_channel_tech(chan)->answer(chan);
2726 ast_setstate(chan, AST_STATE_UP);
2727 ast_channel_unlock(chan);
2735 ast_indicate(chan, -1);
2740 int __ast_answer(struct ast_channel *chan, unsigned int delay)
2743 enum ast_channel_state old_state;
2745 old_state = ast_channel_state(chan);
2746 if ((res = ast_raw_answer(chan))) {
2750 switch (old_state) {
2751 case AST_STATE_RINGING:
2752 case AST_STATE_RING:
2753 /* wait for media to start flowing, but don't wait any longer
2754 * than 'delay' or 500 milliseconds, whichever is longer
2757 AST_LIST_HEAD_NOLOCK(, ast_frame) frames;
2758 struct ast_frame *cur;
2759 struct ast_frame *new_frame;
2760 int timeout_ms = MAX(delay, 500);
2761 unsigned int done = 0;
2762 struct timeval start;
2764 AST_LIST_HEAD_INIT_NOLOCK(&frames);
2766 start = ast_tvnow();
2768 int ms = ast_remaining_ms(start, timeout_ms);
2769 ms = ast_waitfor(chan, ms);
2771 ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", ast_channel_name(chan), strerror(errno));
2776 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));
2779 cur = ast_read(chan);
2780 if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
2781 (cur->subclass.integer == AST_CONTROL_HANGUP))) {
2786 ast_debug(2, "Hangup of channel %s detected in answer routine\n", ast_channel_name(chan));
2790 if ((new_frame = ast_frisolate(cur)) != cur) {
2794 AST_LIST_INSERT_HEAD(&frames, new_frame, frame_list);
2796 /* if a specific delay period was requested, continue
2797 * until that delay has passed. don't stop just because
2798 * incoming media has arrived.
2804 switch (new_frame->frametype) {
2805 /* all of these frametypes qualify as 'media' */
2806 case AST_FRAME_VOICE:
2807 case AST_FRAME_VIDEO:
2808 case AST_FRAME_TEXT:
2809 case AST_FRAME_DTMF_BEGIN:
2810 case AST_FRAME_DTMF_END:
2811 case AST_FRAME_IMAGE:
2812 case AST_FRAME_HTML:
2813 case AST_FRAME_MODEM:
2816 case AST_FRAME_CONTROL:
2818 case AST_FRAME_BRIDGE_ACTION:
2819 case AST_FRAME_BRIDGE_ACTION_SYNC:
2820 case AST_FRAME_NULL:
2830 ast_channel_lock(chan);
2831 while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
2833 ast_queue_frame_head(chan, cur);
2837 ast_channel_unlock(chan);
2847 int ast_answer(struct ast_channel *chan)
2849 return __ast_answer(chan, 0);
2852 inline int ast_auto_answer(struct ast_channel *chan)
2854 if (ast_channel_state(chan) == AST_STATE_UP) {
2855 /* Already answered */
2858 return ast_answer(chan);
2861 int ast_channel_get_duration(struct ast_channel *chan)
2863 ast_assert(NULL != chan);
2865 if (ast_tvzero(ast_channel_creationtime(chan))) {
2868 return (ast_tvdiff_ms(ast_tvnow(), ast_channel_creationtime(chan)) / 1000);
2871 int ast_channel_get_up_time(struct ast_channel *chan)
2873 ast_assert(NULL != chan);
2875 if (ast_tvzero(ast_channel_answertime(chan))) {
2878 return (ast_tvdiff_ms(ast_tvnow(), ast_channel_answertime(chan)) / 1000);
2881 static void deactivate_generator_nolock(struct ast_channel *chan)
2883 if (ast_channel_generatordata(chan)) {
2884 struct ast_generator *generator = ast_channel_generator(chan);
2886 if (generator && generator->release) {
2887 generator->release(chan, ast_channel_generatordata(chan));
2889 ast_channel_generatordata_set(chan, NULL);
2890 ast_channel_generator_set(chan, NULL);
2891 ast_channel_set_fd(chan, AST_GENERATOR_FD, -1);
2892 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
2893 ast_settimeout(chan, 0, NULL, NULL);
2897 void ast_deactivate_generator(struct ast_channel *chan)
2899 ast_channel_lock(chan);
2900 deactivate_generator_nolock(chan);
2901 ast_channel_unlock(chan);
2904 static void generator_write_format_change(struct ast_channel *chan)
2906 struct ast_generator *generator;
2908 ast_channel_lock(chan);
2909 generator = ast_channel_generator(chan);
2910 if (generator && generator->write_format_change) {
2911 generator->write_format_change(chan, ast_channel_generatordata(chan));
2913 ast_channel_unlock(chan);
2916 static int generator_force(const void *data)
2918 /* Called if generator doesn't have data */
2921 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
2922 struct ast_channel *chan = (struct ast_channel *)data;
2924 ast_channel_lock(chan);
2925 tmp = ast_channel_generatordata(chan);
2926 ast_channel_generatordata_set(chan, NULL);
2927 if (ast_channel_generator(chan))
2928 generate = ast_channel_generator(chan)->generate;
2929 ast_channel_unlock(chan);
2931 if (!tmp || !generate) {
2935 res = generate(chan, tmp, 0, ast_format_get_sample_rate(ast_channel_writeformat(chan)) / 50);
2937 ast_channel_lock(chan);
2938 if (ast_channel_generator(chan) && generate == ast_channel_generator(chan)->generate) {
2939 ast_channel_generatordata_set(chan, tmp);
2941 ast_channel_unlock(chan);
2944 ast_debug(1, "Auto-deactivating generator\n");
2945 ast_deactivate_generator(chan);
2951 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
2954 void *generatordata = NULL;
2956 ast_channel_lock(chan);
2957 if (ast_channel_generatordata(chan)) {
2958 struct ast_generator *generator_old = ast_channel_generator(chan);
2960 if (generator_old && generator_old->release) {
2961 generator_old->release(chan, ast_channel_generatordata(chan));
2964 if (gen->alloc && !(generatordata = gen->alloc(chan, params))) {
2967 ast_channel_generatordata_set(chan, generatordata);
2969 ast_settimeout(chan, 50, generator_force, chan);
2970 ast_channel_generator_set(chan, gen);
2972 ast_channel_unlock(chan);
2979 /*! \brief Wait for x amount of time on a file descriptor to have input. */
2980 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
2983 ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
2987 /*! \brief Wait for x amount of time on a file descriptor to have input. */
2989 static struct ast_channel *ast_waitfor_nandfds_classic(struct ast_channel **c, int n, int *fds, int nfds,
2990 int *exception, int *outfd, int *ms)
2992 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
2993 int *exception, int *outfd, int *ms)
2996 struct timeval start = { 0 , 0 };
2997 struct pollfd *pfds = NULL;
3002 struct timeval now = { 0, 0 };
3003 struct timeval whentohangup = { 0, 0 }, diff;
3004 struct ast_channel *winner = NULL;
3017 if ((sz = n * AST_MAX_FDS + nfds)) {
3018 pfds = ast_alloca(sizeof(*pfds) * sz);
3019 fdmap = ast_alloca(sizeof(*fdmap) * sz);
3021 /* nothing to allocate and no FDs to check */
3025 for (x = 0; x < n; x++) {
3026 ast_channel_lock(c[x]);
3027 if (!ast_tvzero(*ast_channel_whentohangup(c[x]))) {
3028 if (ast_tvzero(whentohangup))
3030 diff = ast_tvsub(*ast_channel_whentohangup(c[x]), now);
3031 if (diff.tv_sec < 0 || ast_tvzero(diff)) {
3032 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3033 /* Should already be hungup */
3034 ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3035 ast_channel_unlock(c[x]);
3038 if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
3039 whentohangup = diff;
3041 ast_channel_unlock(c[x]);
3043 /* Wait full interval */
3045 /* INT_MAX, not LONG_MAX, because it matters on 64-bit */
3046 if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
3047 rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000; /* timeout in milliseconds */
3048 if (*ms >= 0 && *ms < rms) { /* original *ms still smaller */
3051 } else if (!ast_tvzero(whentohangup) && rms < 0) {
3052 /* Tiny corner case... call would need to last >24 days */
3056 * Build the pollfd array, putting the channels' fds first,
3057 * followed by individual fds. Order is important because
3058 * individual fd's must have priority over channel fds.
3061 for (x = 0; x < n; x++) {
3062 for (y = 0; y < AST_MAX_FDS; y++) {
3063 fdmap[max].fdno = y; /* fd y is linked to this pfds */
3064 fdmap[max].chan = x; /* channel x is linked to this pfds */
3065 max += ast_add_fd(&pfds[max], ast_channel_fd(c[x], y));
3067 CHECK_BLOCKING(c[x]);
3069 /* Add the individual fds */
3070 for (x = 0; x < nfds; x++) {
3071 fdmap[max].chan = -1;
3072 max += ast_add_fd(&pfds[max], fds[x]);
3076 start = ast_tvnow();
3079 if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */
3082 if (kbrms > 600000) {
3085 res = ast_poll(pfds, max, kbrms);
3089 } while (!res && (rms > 0));
3091 res = ast_poll(pfds, max, rms);
3093 for (x = 0; x < n; x++) {
3094 ast_clear_flag(ast_channel_flags(c[x]), AST_FLAG_BLOCKING);
3096 if (res < 0) { /* Simulate a timeout if we were interrupted */
3097 if (errno != EINTR) {
3102 if (!ast_tvzero(whentohangup)) { /* if we have a timeout, check who expired */
3104 for (x = 0; x < n; x++) {
3105 if (!ast_tvzero(*ast_channel_whentohangup(c[x])) && ast_tvcmp(*ast_channel_whentohangup(c[x]), now) <= 0) {
3106 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3107 ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3108 if (winner == NULL) {
3114 if (res == 0) { /* no fd ready, reset timeout and done */
3115 *ms = 0; /* XXX use 0 since we may not have an exact timeout. */
3119 * Then check if any channel or fd has a pending event.
3120 * Remember to check channels first and fds last, as they
3121 * must have priority on setting 'winner'
3123 for (x = 0; x < max; x++) {
3124 res = pfds[x].revents;
3128 if (fdmap[x].chan >= 0) { /* this is a channel */
3129 winner = c[fdmap[x].chan]; /* override previous winners */
3130 if (res & POLLPRI) {
3131 ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3133 ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3135 ast_channel_fdno_set(winner, fdmap[x].fdno);
3136 } else { /* this is an fd */
3138 *outfd = pfds[x].fd;
3141 *exception = (res & POLLPRI) ? -1 : 0;
3147 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3156 static struct ast_channel *ast_waitfor_nandfds_simple(struct ast_channel *chan, int *ms)
3158 struct timeval start = { 0 , 0 };
3160 struct epoll_event ev[1];
3161 long diff, rms = *ms;
3162 struct ast_channel *winner = NULL;
3163 struct ast_epoll_data *aed = NULL;
3165 ast_channel_lock(chan);
3166 /* Figure out their timeout */
3167 if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
3168 if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow())) < 0) {
3169 /* They should already be hungup! */
3170 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
3171 ast_channel_unlock(chan);
3174 /* If this value is smaller then the current one... make it priority */
3180 ast_channel_unlock(chan);
3182 /* Time to make this channel block... */
3183 CHECK_BLOCKING(chan);
3186 start = ast_tvnow();
3189 /* We don't have to add any file descriptors... they are already added, we just have to wait! */
3190 res = epoll_wait(ast_channel_epfd(chan), ev, 1, rms);
3193 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
3195 /* Simulate a timeout if we were interrupted */
3197 if (errno != EINTR) {
3203 /* If this channel has a timeout see if it expired */
3204 if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
3205 if (ast_tvdiff_ms(ast_tvnow(), *ast_channel_whentohangup(chan)) >= 0) {
3206 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
3211 /* No fd ready, reset timeout and be done for now */
3217 /* See what events are pending */
3218 aed = ev[0].data.ptr;
3219 ast_channel_fdno_set(chan, aed->which);
3220 if (ev[0].events & EPOLLPRI) {
3221 ast_set_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3223 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3227 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3236 static struct ast_channel *ast_waitfor_nandfds_complex(struct ast_channel **c, int n, int *ms)
3238 struct timeval start = { 0 , 0 };
3240 struct epoll_event ev[25] = { { 0, } };
3241 struct timeval now = { 0, 0 };
3242 long whentohangup = 0, diff = 0, rms = *ms;
3243 struct ast_channel *winner = NULL;
3245 for (i = 0; i < n; i++) {
3246 ast_channel_lock(c[i]);
3247 if (!ast_tvzero(*ast_channel_whentohangup(c[i]))) {
3248 if (whentohangup == 0) {
3251 if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(c[i]), now)) < 0) {
3252 ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
3253 ast_channel_unlock(c[i]);
3256 if (!whentohangup || whentohangup > diff) {
3257 whentohangup = diff;
3260 ast_channel_unlock(c[i]);
3261 CHECK_BLOCKING(c[i]);
3267 if (*ms >= 0 && *ms < rms) {
3273 start = ast_tvnow();
3276 res = epoll_wait(ast_channel_epfd(c[0]), ev, 25, rms);
3278 for (i = 0; i < n; i++) {
3279 ast_clear_flag(ast_channel_flags(c[i]), AST_FLAG_BLOCKING);
3283 if (errno != EINTR) {
3291 for (i = 0; i < n; i++) {
3292 if (!ast_tvzero(*ast_channel_whentohangup(c[i])) && ast_tvdiff_ms(now, *ast_channel_whentohangup(c[i])) >= 0) {
3293 ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
3306 for (i = 0; i < res; i++) {
3307 struct ast_epoll_data *aed = ev[i].data.ptr;
3309 if (!ev[i].events || !aed) {
3314 if (ev[i].events & EPOLLPRI) {
3315 ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3317 ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3319 ast_channel_fdno_set(winner, aed->which);
3323 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3332 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3333 int *exception, int *outfd, int *ms)
3335 /* Clear all provided values in one place. */
3343 /* If no epoll file descriptor is available resort to classic nandfds */
3344 if (!n || nfds || ast_channel_epfd(c[0]) == -1) {
3345 return ast_waitfor_nandfds_classic(c, n, fds, nfds, exception, outfd, ms);
3346 } else if (!nfds && n == 1) {
3347 return ast_waitfor_nandfds_simple(c[0], ms);
3349 return ast_waitfor_nandfds_complex(c, n, ms);
3354 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
3356 return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
3359 int ast_waitfor(struct ast_channel *c, int ms)
3364 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3367 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3372 int ast_waitfordigit(struct ast_channel *c, int ms)
3374 return ast_waitfordigit_full(c, ms, -1, -1);
3377 int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
3379 return ast_settimeout_full(c, rate, func, data, 0);
3382 int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data, unsigned int is_ao2_obj)
3385 unsigned int real_rate = rate, max_rate;
3387 ast_channel_lock(c);
3389 if (ast_channel_timingfd(c) == -1) {
3390 ast_channel_unlock(c);
3399 if (rate && rate > (max_rate = ast_timer_get_max_rate(ast_channel_timer(c)))) {
3400 real_rate = max_rate;
3403 ast_debug(1, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
3405 res = ast_timer_set_rate(ast_channel_timer(c), real_rate);
3407 if (ast_channel_timingdata(c) && ast_test_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
3408 ao2_ref(ast_channel_timingdata(c), -1);
3411 ast_channel_timingfunc_set(c, func);
3412 ast_channel_timingdata_set(c, data);
3414 if (data && is_ao2_obj) {
3416 ast_set_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
3418 ast_clear_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
3421 if (func == NULL && rate == 0 && ast_channel_fdno(c) == AST_TIMING_FD) {
3422 /* Clearing the timing func and setting the rate to 0
3423 * means that we don't want to be reading from the timingfd
3424 * any more. Setting c->fdno to -1 means we won't have any
3425 * errant reads from the timingfd, meaning we won't potentially
3426 * miss any important frames.
3428 ast_channel_fdno_set(c, -1);
3431 ast_channel_unlock(c);
3436 int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, int cmdfd)
3438 struct timeval start = ast_tvnow();
3441 /* Stop if we're a zombie or need a soft hangup */
3442 if (ast_test_flag(ast_channel_flags(c), AST_FLAG_ZOMBIE) || ast_check_hangup(c))
3445 /* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
3446 ast_set_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3448 /* Wait for a digit, no more than timeout_ms milliseconds total.
3449 * Or, wait indefinitely if timeout_ms is <0.
3451 while ((ms = ast_remaining_ms(start, timeout_ms))) {
3452 struct ast_channel *rchan;
3456 /* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,
3457 * it is unhelpful if it waited less than a millisecond.
3459 rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
3461 if (!rchan && outfd < 0 && ms) {
3462 if (errno == 0 || errno == EINTR)
3464 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
3465 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3467 } else if (outfd > -1) {
3468 /* The FD we were watching has something waiting */
3469 ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
3470 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3474 struct ast_frame *f = ast_read(c);
3478 switch (f->frametype) {
3479 case AST_FRAME_DTMF_BEGIN:
3481 case AST_FRAME_DTMF_END:
3482 res = f->subclass.integer;
3484 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3486 case AST_FRAME_CONTROL:
3487 switch (f->subclass.integer) {
3488 case AST_CONTROL_HANGUP:
3490 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3492 case AST_CONTROL_STREAM_STOP:
3493 case AST_CONTROL_STREAM_SUSPEND:
3494 case AST_CONTROL_STREAM_RESTART:
3495 case AST_CONTROL_STREAM_REVERSE:
3496 case AST_CONTROL_STREAM_FORWARD:
3497 /* Fall-through and treat as if it were a DTMF signal. Items
3498 * that perform stream control will handle this. */
3499 res = f->subclass.integer;
3501 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3503 case AST_CONTROL_PVT_CAUSE_CODE:
3504 case AST_CONTROL_RINGING:
3505 case AST_CONTROL_ANSWER:
3506 case AST_CONTROL_SRCUPDATE:
3507 case AST_CONTROL_SRCCHANGE:
3508 case AST_CONTROL_CONNECTED_LINE:
3509 case AST_CONTROL_REDIRECTING:
3510 case AST_CONTROL_UPDATE_RTP_PEER:
3511 case AST_CONTROL_HOLD:
3512 case AST_CONTROL_UNHOLD:
3517 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
3521 case AST_FRAME_VOICE:
3522 /* Write audio if appropriate */
3524 if (write(audiofd, f->data.ptr, f->datalen) < 0) {
3525 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3536 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3538 return 0; /* Time is up */
3541 enum DtmfDirection {
3546 static const char *dtmf_direction_to_string(enum DtmfDirection direction)
3548 switch (direction) {
3558 static void send_dtmf_begin_event(struct ast_channel *chan,
3559 enum DtmfDirection direction, const char digit)
3561 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3562 char digit_str[] = { digit, '\0' };
3564 blob = ast_json_pack("{ s: s, s: s }",
3566 "direction", dtmf_direction_to_string(direction));
3571 ast_channel_publish_cached_blob(chan, ast_channel_dtmf_begin_type(), blob);
3574 static void send_dtmf_end_event(struct ast_channel *chan,
3575 enum DtmfDirection direction, const char digit, long duration_ms)
3577 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3578 char digit_str[] = { digit, '\0' };
3580 blob = ast_json_pack("{ s: s, s: s, s: i }",
3582 "direction", dtmf_direction_to_string(direction),
3583 "duration_ms", duration_ms);
3588 ast_channel_publish_cached_blob(chan, ast_channel_dtmf_end_type(), blob);
3591 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
3593 struct ast_generator *generator;
3598 generator = ast_channel_generator(chan);
3600 || !generator->generate
3601 || f->frametype != AST_FRAME_VOICE
3602 || !ast_channel_generatordata(chan)
3603 || ast_channel_timingfunc(chan)) {
3608 * We must generate frames in phase locked mode since
3609 * we have no internal timer available.
3611 if (ast_format_cmp(f->subclass.format, ast_channel_writeformat(chan)) == AST_FORMAT_CMP_NOT_EQUAL) {
3613 factor = ((float) ast_format_get_sample_rate(ast_channel_writeformat(chan))) / ((float) ast_format_get_sample_rate(f->subclass.format));
3614 samples = (int) (((float) f->samples) * factor);
3616 samples = f->samples;
3619 gendata = ast_channel_generatordata(chan);
3620 ast_channel_generatordata_set(chan, NULL); /* reset, to let writes go through */
3623 * This unlock is here based on two assumptions that hold true at
3624 * this point in the code. 1) this function is only called from
3625 * within __ast_read() and 2) all generators call ast_write() in
3626 * their generate callback.
3628 * The reason this is added is so that when ast_write is called,
3629 * the lock that occurs there will not recursively lock the
3630 * channel. Doing this will allow deadlock avoidance to work in
3633 ast_channel_unlock(chan);
3634 res = generator->generate(chan, gendata, f->datalen, samples);
3635 ast_channel_lock(chan);
3636 if (generator == ast_channel_generator(chan)) {
3637 ast_channel_generatordata_set(chan, gendata);
3639 ast_debug(1, "Auto-deactivating generator\n");
3640 ast_deactivate_generator(chan);
3645 static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
3647 struct ast_frame *fr = ast_channel_dtmff(chan);
3649 fr->frametype = AST_FRAME_DTMF_END;
3650 fr->subclass.integer = f->subclass.integer;
3653 /* The only time this function will be called is for a frame that just came
3654 * out of the channel driver. So, we want to stick it on the tail of the
3657 ast_queue_frame(chan, fr);
3661 * \brief Determine whether or not we should ignore DTMF in the readq
3663 static inline int should_skip_dtmf(struct ast_channel *chan)
3665 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_EMULATE_DTMF)) {
3666 /* We're in the middle of emulating a digit, or DTMF has been
3667 * explicitly deferred. Skip this digit, then. */
3671 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3672 ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
3673 /* We're not in the middle of a digit, but it hasn't been long enough
3674 * since the last digit, so we'll have to skip DTMF for now. */
3682 * \brief calculates the number of samples to jump forward with in a monitor stream.
3684 * \note When using ast_seekstream() with the read and write streams of a monitor,
3685 * the number of samples to seek forward must be of the same sample rate as the stream
3686 * or else the jump will not be calculated correctly.
3688 * \retval number of samples to seek forward after rate conversion.
3690 static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
3692 int diff = sample_rate - seek_rate;
3695 samples = samples / (float) (sample_rate / seek_rate);
3696 } else if (diff < 0) {
3697 samples = samples * (float) (seek_rate / sample_rate);
3703 static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
3705 struct ast_frame *f = NULL; /* the return value */
3709 /* this function is very long so make sure there is only one return
3710 * point at the end (there are only two exceptions to this).
3712 ast_channel_lock(chan);
3714 /* Stop if we're a zombie or need a soft hangup */
3715 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
3716 if (ast_channel_generator(chan))
3717 ast_deactivate_generator(chan);
3720 * It is possible for chan->_softhangup to be set and there
3721 * still be control frames that need to be read. Instead of
3722 * just going to 'done' in the case of ast_check_hangup(), we
3723 * need to queue the end-of-Q frame so that it can mark the end
3724 * of the read queue. If there are frames to be read,
3725 * ast_queue_control() will be called repeatedly, but will only
3726 * queue the first end-of-Q frame.
3728 if (ast_channel_softhangup_internal_flag(chan)) {
3729 ast_queue_control(chan, AST_CONTROL_END_OF_Q);
3736 * The ast_waitfor() code records which of the channel's file
3737 * descriptors reported that data is available. In theory,
3738 * ast_read() should only be called after ast_waitfor() reports
3739 * that a channel has data available for reading. However,
3740 * there still may be some edge cases throughout the code where
3741 * ast_read() is called improperly. This can potentially cause
3742 * problems, so if this is a developer build, make a lot of
3743 * noise if this happens so that it can be addressed.
3745 * One of the potential problems is blocking on a dead channel.
3747 if (ast_channel_fdno(chan) == -1) {
3749 "ast_read() on chan '%s' called with no recorded file descriptor.\n",
3750 ast_channel_name(chan));
3755 prestate = ast_channel_state(chan);
3757 if (ast_channel_timingfd(chan) > -1 && ast_channel_fdno(chan) == AST_TIMING_FD) {
3758 enum ast_timer_event res;
3760 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3762 res = ast_timer_get_event(ast_channel_timer(chan));
3765 case AST_TIMING_EVENT_EXPIRED:
3766 if (ast_timer_ack(ast_channel_timer(chan), 1) < 0) {
3767 ast_log(LOG_ERROR, "Failed to acknoweldge timer in ast_read\n");
3771 if (ast_channel_timingfunc(chan)) {
3772 /* save a copy of func/data before unlocking the channel */
3773 ast_timing_func_t func = ast_channel_timingfunc(chan);
3774 void *data = ast_channel_timingdata(chan);
3776 if (data && ast_test_flag(ast_channel_flags(chan), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
3780 ast_channel_fdno_set(chan, -1);
3781 ast_channel_unlock(chan);
3787 ast_timer_set_rate(ast_channel_timer(chan), 0);
3788 ast_channel_fdno_set(chan, -1);
3789 ast_channel_unlock(chan);
3792 /* cannot 'goto done' because the channel is already unlocked */
3793 return &ast_null_frame;
3795 case AST_TIMING_EVENT_CONTINUOUS:
3796 if (AST_LIST_EMPTY(ast_channel_readq(chan)) ||
3797 !AST_LIST_NEXT(AST_LIST_FIRST(ast_channel_readq(chan)), frame_list)) {
3798 ast_timer_disable_continuous(ast_channel_timer(chan));
3803 } else if (ast_channel_fd_isset(chan, AST_GENERATOR_FD) && ast_channel_fdno(chan) == AST_GENERATOR_FD) {
3804 /* if the AST_GENERATOR_FD is set, call the generator with args
3805 * set to -1 so it can do whatever it needs to.
3807 void *tmp = ast_channel_generatordata(chan);
3808 ast_channel_generatordata_set(chan, NULL); /* reset to let ast_write get through */
3809 ast_channel_generator(chan)->generate(chan, tmp, -1, -1);
3810 ast_channel_generatordata_set(chan, tmp);
3811 f = &ast_null_frame;
3812 ast_channel_fdno_set(chan, -1);
3814 } else if (ast_channel_fd_isset(chan, AST_JITTERBUFFER_FD) && ast_channel_fdno(chan) == AST_JITTERBUFFER_FD) {
3815 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3818 /* Read and ignore anything on the alertpipe, but read only
3819 one sizeof(blah) per frame that we send from it */
3820 if (ast_channel_internal_alert_read(chan) == AST_ALERT_READ_FATAL) {
3821 f = &ast_null_frame;
3825 /* Check for pending read queue */
3826 if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
3827 int skip_dtmf = should_skip_dtmf(chan);
3829 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), f, frame_list) {
3830 /* We have to be picky about which frame we pull off of the readq because
3831 * there are cases where we want to leave DTMF frames on the queue until
3832 * some later time. */
3834 if ( (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) && skip_dtmf) {
3838 AST_LIST_REMOVE_CURRENT(frame_list);
3841 AST_LIST_TRAVERSE_SAFE_END;
3844 /* There were no acceptable frames on the readq. */
3845 f = &ast_null_frame;
3846 ast_channel_alert_write(chan);
3849 /* Interpret hangup and end-of-Q frames to return NULL */
3850 /* XXX why not the same for frames from the channel ? */
3851 if (f->frametype == AST_FRAME_CONTROL) {
3852 switch (f->subclass.integer) {
3853 case AST_CONTROL_HANGUP:
3854 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
3855 cause = f->data.uint32;
3857 case AST_CONTROL_END_OF_Q:
3866 ast_channel_blocker_set(chan, pthread_self());
3867 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION)) {
3868 if (ast_channel_tech(chan)->exception)
3869 f = ast_channel_tech(chan)->exception(chan);
3871 ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", ast_channel_name(chan));
3872 f = &ast_null_frame;
3874 /* Clear the exception flag */