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>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/_private.h"
36 #include "asterisk/paths.h" /* use ast_config_AST_SYSTEM_NAME */
38 #include "asterisk/pbx.h"
39 #include "asterisk/frame.h"
40 #include "asterisk/mod_format.h"
41 #include "asterisk/sched.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/musiconhold.h"
44 #include "asterisk/say.h"
45 #include "asterisk/file.h"
46 #include "asterisk/cli.h"
47 #include "asterisk/translate.h"
48 #include "asterisk/manager.h"
49 #include "asterisk/cel.h"
50 #include "asterisk/chanvars.h"
51 #include "asterisk/linkedlists.h"
52 #include "asterisk/indications.h"
53 #include "asterisk/monitor.h"
54 #include "asterisk/causes.h"
55 #include "asterisk/callerid.h"
56 #include "asterisk/utils.h"
57 #include "asterisk/lock.h"
58 #include "asterisk/app.h"
59 #include "asterisk/transcap.h"
60 #include "asterisk/devicestate.h"
61 #include "asterisk/threadstorage.h"
62 #include "asterisk/slinfactory.h"
63 #include "asterisk/audiohook.h"
64 #include "asterisk/framehook.h"
65 #include "asterisk/timing.h"
66 #include "asterisk/autochan.h"
67 #include "asterisk/stringfields.h"
68 #include "asterisk/global_datastores.h"
69 #include "asterisk/data.h"
70 #include "asterisk/channel_internal.h"
73 #include <sys/epoll.h>
76 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
79 #endif /* defined(HAVE_PRI) */
80 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
82 struct ast_epoll_data {
83 struct ast_channel *chan;
87 /* uncomment if you have problems with 'monitoring' synchronized files */
89 #define MONITOR_CONSTANT_DELAY
90 #define MONITOR_DELAY 150 * 8 /*!< 150 ms of MONITORING DELAY */
93 /*! \brief Prevent new channel allocation if shutting down. */
94 static int shutting_down;
99 unsigned long global_fin, global_fout;
101 AST_THREADSTORAGE(state2str_threadbuf);
102 #define STATE2STR_BUFSIZE 32
104 /*! Default amount of time to use when emulating a digit as a begin and end
106 #define AST_DEFAULT_EMULATE_DTMF_DURATION 100
108 /*! Minimum amount of time between the end of the last digit and the beginning
109 * of a new one - 45ms */
110 #define AST_MIN_DTMF_GAP 45
112 /*! \brief List of channel drivers */
114 const struct ast_channel_tech *tech;
115 AST_LIST_ENTRY(chanlist) list;
119 /*! \brief Structure to hold channel context backtrace data */
120 struct ast_chan_trace_data {
122 AST_LIST_HEAD_NOLOCK(, ast_chan_trace) trace;
125 /*! \brief Structure to save contexts where an ast_chan has been into */
126 struct ast_chan_trace {
127 char context[AST_MAX_CONTEXT];
128 char exten[AST_MAX_EXTENSION];
130 AST_LIST_ENTRY(ast_chan_trace) entry;
134 /*! \brief the list of registered channel types */
135 static AST_RWLIST_HEAD_STATIC(backends, chanlist);
138 #define NUM_CHANNEL_BUCKETS 61
140 #define NUM_CHANNEL_BUCKETS 1567
143 /*! \brief All active channels on the system */
144 static struct ao2_container *channels;
146 /*! \brief map AST_CAUSE's to readable string representations
150 static const struct {
155 { AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
156 { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
157 { AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
158 { AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
159 { AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
160 { AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
161 { AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
162 { AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
163 { AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
164 { AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
165 { AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
166 { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
167 { AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
168 { AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
169 { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
170 { AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
171 { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
172 { AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
173 { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
174 { AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
175 { AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
176 { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
177 { AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
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 "%-10.10s %-40.40s %-12.12s %-12.12s %-12.12s\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", "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->indicate) ? "yes" : "no",
303 (cl->tech->transfer) ? "yes" : "no");
306 AST_RWLIST_UNLOCK(&backends);
308 ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
315 static char *complete_channeltypes(struct ast_cli_args *a)
325 wordlen = strlen(a->word);
327 AST_RWLIST_RDLOCK(&backends);
328 AST_RWLIST_TRAVERSE(&backends, cl, list) {
329 if (!strncasecmp(a->word, cl->tech->type, wordlen) && ++which > a->n) {
330 ret = ast_strdup(cl->tech->type);
334 AST_RWLIST_UNLOCK(&backends);
339 /*! \brief Show details about a channel driver - CLI command */
340 static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
342 struct chanlist *cl = NULL;
347 e->command = "core show channeltype";
349 "Usage: core show channeltype <name>\n"
350 " Show details about the specified channel type, <name>.\n";
353 return complete_channeltypes(a);
357 return CLI_SHOWUSAGE;
359 AST_RWLIST_RDLOCK(&backends);
361 AST_RWLIST_TRAVERSE(&backends, cl, list) {
362 if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
368 ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
369 AST_RWLIST_UNLOCK(&backends);
374 "-- Info about channel driver: %s --\n"
375 " Device State: %s\n"
378 " Capabilities: %s\n"
382 " Image Support: %s\n"
383 " Text Support: %s\n",
385 (cl->tech->devicestate) ? "yes" : "no",
386 (cl->tech->indicate) ? "yes" : "no",
387 (cl->tech->transfer) ? "yes" : "no",
388 ast_getformatname_multiple(buf, sizeof(buf), cl->tech->capabilities),
389 (cl->tech->send_digit_begin) ? "yes" : "no",
390 (cl->tech->send_digit_end) ? "yes" : "no",
391 (cl->tech->send_html) ? "yes" : "no",
392 (cl->tech->send_image) ? "yes" : "no",
393 (cl->tech->send_text) ? "yes" : "no"
397 AST_RWLIST_UNLOCK(&backends);
402 static struct ast_cli_entry cli_channel[] = {
403 AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
404 AST_CLI_DEFINE(handle_cli_core_show_channeltype, "Give more details on that channel type")
407 static struct ast_frame *kill_read(struct ast_channel *chan)
409 /* Hangup channel. */
413 static struct ast_frame *kill_exception(struct ast_channel *chan)
415 /* Hangup channel. */
419 static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
421 /* Hangup channel. */
425 static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
427 /* No problem fixing up the channel. */
431 static int kill_hangup(struct ast_channel *chan)
433 ast_channel_tech_pvt_set(chan, NULL);
438 * \brief Kill the channel channel driver technology descriptor.
441 * The purpose of this channel technology is to encourage the
442 * channel to hangup as quickly as possible.
444 * \note Used by DTMF atxfer and zombie channels.
446 const struct ast_channel_tech ast_kill_tech = {
448 .description = "Kill channel (should not see this)",
450 .exception = kill_exception,
453 .hangup = kill_hangup,
457 /*! \brief Destructor for the channel trace datastore */
458 static void ast_chan_trace_destroy_cb(void *data)
460 struct ast_chan_trace *trace;
461 struct ast_chan_trace_data *traced = data;
462 while ((trace = AST_LIST_REMOVE_HEAD(&traced->trace, entry))) {
468 /*! \brief Datastore to put the linked list of ast_chan_trace and trace status */
469 static const struct ast_datastore_info ast_chan_trace_datastore_info = {
471 .destroy = ast_chan_trace_destroy_cb
474 /*! \brief Put the channel backtrace in a string */
475 int ast_channel_trace_serialize(struct ast_channel *chan, struct ast_str **buf)
478 struct ast_chan_trace *trace;
479 struct ast_chan_trace_data *traced;
480 struct ast_datastore *store;
482 ast_channel_lock(chan);
483 store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
485 ast_channel_unlock(chan);
488 traced = store->data;
490 AST_LIST_TRAVERSE(&traced->trace, trace, entry) {
491 if (ast_str_append(buf, 0, "[%d] => %s, %s, %d\n", total, trace->context, trace->exten, trace->priority) < 0) {
492 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
498 ast_channel_unlock(chan);
502 /* !\brief Whether or not context tracing is enabled */
503 int ast_channel_trace_is_enabled(struct ast_channel *chan)
505 struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
508 return ((struct ast_chan_trace_data *)store->data)->enabled;
511 /*! \brief Update the context backtrace data if tracing is enabled */
512 static int ast_channel_trace_data_update(struct ast_channel *chan, struct ast_chan_trace_data *traced)
514 struct ast_chan_trace *trace;
515 if (!traced->enabled)
517 /* If the last saved context does not match the current one
518 OR we have not saved any context so far, then save the current context */
519 if ((!AST_LIST_EMPTY(&traced->trace) && strcasecmp(AST_LIST_FIRST(&traced->trace)->context, ast_channel_context(chan))) ||
520 (AST_LIST_EMPTY(&traced->trace))) {
521 /* Just do some debug logging */
522 if (AST_LIST_EMPTY(&traced->trace))
523 ast_debug(1, "Setting initial trace context to %s\n", ast_channel_context(chan));
525 ast_debug(1, "Changing trace context from %s to %s\n", AST_LIST_FIRST(&traced->trace)->context, ast_channel_context(chan));
526 /* alloc or bail out */
527 trace = ast_malloc(sizeof(*trace));
530 /* save the current location and store it in the trace list */
531 ast_copy_string(trace->context, ast_channel_context(chan), sizeof(trace->context));
532 ast_copy_string(trace->exten, ast_channel_exten(chan), sizeof(trace->exten));
533 trace->priority = ast_channel_priority(chan);
534 AST_LIST_INSERT_HEAD(&traced->trace, trace, entry);
539 /*! \brief Update the context backtrace if tracing is enabled */
540 int ast_channel_trace_update(struct ast_channel *chan)
542 struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
545 return ast_channel_trace_data_update(chan, store->data);
548 /*! \brief Enable context tracing in the channel */
549 int ast_channel_trace_enable(struct ast_channel *chan)
551 struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
552 struct ast_chan_trace_data *traced;
554 store = ast_datastore_alloc(&ast_chan_trace_datastore_info, "ChanTrace");
557 traced = ast_calloc(1, sizeof(*traced));
559 ast_datastore_free(store);
562 store->data = traced;
563 AST_LIST_HEAD_INIT_NOLOCK(&traced->trace);
564 ast_channel_datastore_add(chan, store);
566 ((struct ast_chan_trace_data *)store->data)->enabled = 1;
567 ast_channel_trace_data_update(chan, store->data);
571 /*! \brief Disable context tracing in the channel */
572 int ast_channel_trace_disable(struct ast_channel *chan)
574 struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
577 ((struct ast_chan_trace_data *)store->data)->enabled = 0;
580 #endif /* CHANNEL_TRACE */
582 /*! \brief Checks to see if a channel is needing hang up */
583 int ast_check_hangup(struct ast_channel *chan)
585 if (ast_channel_softhangup_internal_flag(chan)) /* yes if soft hangup flag set */
587 if (ast_tvzero(*ast_channel_whentohangup(chan))) /* no if no hangup scheduled */
589 if (ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()) > 0) /* no if hangup time has not come yet. */
591 ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()));
592 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT); /* record event */
596 int ast_check_hangup_locked(struct ast_channel *chan)
599 ast_channel_lock(chan);
600 res = ast_check_hangup(chan);
601 ast_channel_unlock(chan);
605 int ast_channel_softhangup_withcause_locked(void *obj, int causecode)
607 struct ast_channel *chan = obj;
609 ast_channel_lock(chan);
612 ast_debug(1, "Setting hangupcause of channel %s to %d (is %d now)\n",
613 ast_channel_name(chan), causecode, ast_channel_hangupcause(chan));
615 ast_channel_hangupcause_set(chan, causecode);
618 ast_softhangup_nolock(chan, AST_SOFTHANGUP_EXPLICIT);
620 ast_channel_unlock(chan);
625 static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
627 struct ast_channel *chan = obj;
629 ast_softhangup(chan, AST_SOFTHANGUP_SHUTDOWN);
634 void ast_begin_shutdown(int hangup)
639 ao2_callback(channels, OBJ_NODATA | OBJ_MULTIPLE, ast_channel_softhangup_cb, NULL);
643 /*! \brief returns number of active/allocated channels */
644 int ast_active_channels(void)
646 return channels ? ao2_container_count(channels) : 0;
649 int ast_undestroyed_channels(void)
651 return ast_atomic_fetchadd_int(&chancount, 0);
654 /*! \brief Cancel a shutdown in progress */
655 void ast_cancel_shutdown(void)
660 /*! \brief Returns non-zero if Asterisk is being shut down */
661 int ast_shutting_down(void)
663 return shutting_down;
666 /*! \brief Set when to hangup channel */
667 void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
669 if (ast_tvzero(offset)) {
670 ast_channel_whentohangup_set(chan, &offset);
672 struct timeval tv = ast_tvadd(offset, ast_tvnow());
673 ast_channel_whentohangup_set(chan, &tv);
675 ast_queue_frame(chan, &ast_null_frame);
679 void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
681 struct timeval when = { offset, };
682 ast_channel_setwhentohangup_tv(chan, when);
685 /*! \brief Compare a offset with when to hangup channel */
686 int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
688 struct timeval whentohangup;
690 if (ast_tvzero(*ast_channel_whentohangup(chan)))
691 return ast_tvzero(offset) ? 0 : -1;
693 if (ast_tvzero(offset))
696 whentohangup = ast_tvadd(offset, ast_tvnow());
698 return ast_tvdiff_ms(whentohangup, *ast_channel_whentohangup(chan));
701 int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
703 struct timeval when = { offset, };
704 return ast_channel_cmpwhentohangup_tv(chan, when);
707 /*! \brief Register a new telephony channel in Asterisk */
708 int ast_channel_register(const struct ast_channel_tech *tech)
710 struct chanlist *chan;
712 AST_RWLIST_WRLOCK(&backends);
714 AST_RWLIST_TRAVERSE(&backends, chan, list) {
715 if (!strcasecmp(tech->type, chan->tech->type)) {
716 ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
717 AST_RWLIST_UNLOCK(&backends);
722 if (!(chan = ast_calloc(1, sizeof(*chan)))) {
723 AST_RWLIST_UNLOCK(&backends);
727 AST_RWLIST_INSERT_HEAD(&backends, chan, list);
729 ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
731 ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
733 AST_RWLIST_UNLOCK(&backends);
738 /*! \brief Unregister channel driver */
739 void ast_channel_unregister(const struct ast_channel_tech *tech)
741 struct chanlist *chan;
743 ast_debug(1, "Unregistering channel type '%s'\n", tech->type);
745 AST_RWLIST_WRLOCK(&backends);
747 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&backends, chan, list) {
748 if (chan->tech == tech) {
749 AST_LIST_REMOVE_CURRENT(list);
751 ast_verb(2, "Unregistered channel type '%s'\n", tech->type);
755 AST_LIST_TRAVERSE_SAFE_END;
757 AST_RWLIST_UNLOCK(&backends);
760 /*! \brief Get handle to channel driver based on name */
761 const struct ast_channel_tech *ast_get_channel_tech(const char *name)
763 struct chanlist *chanls;
764 const struct ast_channel_tech *ret = NULL;
766 AST_RWLIST_RDLOCK(&backends);
768 AST_RWLIST_TRAVERSE(&backends, chanls, list) {
769 if (!strcasecmp(name, chanls->tech->type)) {
775 AST_RWLIST_UNLOCK(&backends);
780 /*! \brief Gives the string form of a given hangup cause */
781 const char *ast_cause2str(int cause)
785 for (x = 0; x < ARRAY_LEN(causes); x++) {
786 if (causes[x].cause == cause)
787 return causes[x].desc;
793 /*! \brief Convert a symbolic hangup cause to number */
794 int ast_str2cause(const char *name)
798 for (x = 0; x < ARRAY_LEN(causes); x++)
799 if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
800 return causes[x].cause;
805 /*! \brief Gives the string form of a given channel state.
806 \note This function is not reentrant.
808 const char *ast_state2str(enum ast_channel_state state)
815 case AST_STATE_RESERVED:
817 case AST_STATE_OFFHOOK:
819 case AST_STATE_DIALING:
823 case AST_STATE_RINGING:
829 case AST_STATE_DIALING_OFFHOOK:
830 return "Dialing Offhook";
831 case AST_STATE_PRERING:
834 if (!(buf = ast_threadstorage_get(&state2str_threadbuf, STATE2STR_BUFSIZE)))
836 snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%d)", state);
841 /*! \brief Gives the string form of a given transfer capability */
842 char *ast_transfercapability2str(int transfercapability)
844 switch (transfercapability) {
845 case AST_TRANS_CAP_SPEECH:
847 case AST_TRANS_CAP_DIGITAL:
849 case AST_TRANS_CAP_RESTRICTED_DIGITAL:
850 return "RESTRICTED_DIGITAL";
851 case AST_TRANS_CAP_3_1K_AUDIO:
853 case AST_TRANS_CAP_DIGITAL_W_TONES:
854 return "DIGITAL_W_TONES";
855 case AST_TRANS_CAP_VIDEO:
862 /*! \brief Pick the best audio codec */
863 struct ast_format *ast_best_codec(struct ast_format_cap *cap, struct ast_format *result)
865 /* This just our opinion, expressed in code. We are asked to choose
866 the best codec to use, given no information */
867 static const enum ast_format_id prefs[] =
869 /*! Okay, ulaw is used by all telephony equipment, so start with it */
871 /*! Unless of course, you're a silly European, so then prefer ALAW */
877 /*! G.722 is better then all below, but not as common as the above... so give ulaw and alaw priority */
879 /*! Okay, well, signed linear is easy to translate into other stuff */
880 AST_FORMAT_SLINEAR192,
881 AST_FORMAT_SLINEAR96,
882 AST_FORMAT_SLINEAR48,
883 AST_FORMAT_SLINEAR44,
884 AST_FORMAT_SLINEAR32,
885 AST_FORMAT_SLINEAR24,
886 AST_FORMAT_SLINEAR16,
887 AST_FORMAT_SLINEAR12,
889 /*! G.726 is standard ADPCM, in RFC3551 packing order */
891 /*! G.726 is standard ADPCM, in AAL2 packing order */
892 AST_FORMAT_G726_AAL2,
893 /*! ADPCM has great sound quality and is still pretty easy to translate */
895 /*! Okay, we're down to vocoders now, so pick GSM because it's small and easier to
896 translate and sounds pretty good */
898 /*! iLBC is not too bad */
900 /*! Speex is free, but computationally more expensive than GSM */
904 /*! SILK is pretty awesome. */
906 /*! CELT supports crazy high sample rates */
908 /*! Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
911 /*! G.729a is faster than 723 and slightly less expensive */
913 /*! Down to G.723.1 which is proprietary but at least designed for voice */
919 /* Find the first preferred codec in the format given */
920 for (x = 0; x < ARRAY_LEN(prefs); x++) {
921 if (ast_format_cap_best_byid(cap, prefs[x], result)) {
926 ast_format_clear(result);
927 ast_log(LOG_WARNING, "Don't know any of %s formats\n", ast_getformatname_multiple(buf, sizeof(buf), cap));
932 static const struct ast_channel_tech null_tech = {
934 .description = "Null channel (should not see this)",
937 static void ast_channel_destructor(void *obj);
938 static void ast_dummy_channel_destructor(void *obj);
940 /*! \brief Create a new channel structure */
941 static struct ast_channel * attribute_malloc __attribute__((format(printf, 13, 0)))
942 __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
943 const char *acctcode, const char *exten, const char *context,
944 const char *linkedid, const int amaflag, const char *file, int line,
945 const char *function, const char *name_fmt, va_list ap)
947 struct ast_channel *tmp;
948 struct varshead *headp;
949 char *tech = "", *tech2 = NULL;
950 struct ast_format_cap *nativeformats;
951 struct ast_sched_context *schedctx;
952 struct ast_timer *timer;
954 /* If shutting down, don't allocate any new channels */
955 if (ast_shutting_down()) {
956 ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
960 if (!(tmp = ast_channel_internal_alloc(ast_channel_destructor))) {
961 /* Channel structure allocation failure. */
964 if (!(nativeformats = ast_format_cap_alloc())) {
966 /* format capabilities structure allocation failure */
969 ast_channel_nativeformats_set(tmp, nativeformats);
972 * Init file descriptors to unopened state so
973 * the destructor can know not to close them.
975 ast_channel_timingfd_set(tmp, -1);
976 ast_channel_internal_alertpipe_clear(tmp);
977 ast_channel_internal_fd_clear_all(tmp);
980 ast_channel_epfd(tmp) = epoll_create(25);
983 if (!(schedctx = ast_sched_context_create())) {
984 ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
985 return ast_channel_unref(tmp);
987 ast_channel_sched_set(tmp, schedctx);
989 ast_party_dialed_init(ast_channel_dialed(tmp));
990 ast_party_caller_init(ast_channel_caller(tmp));
991 ast_party_connected_line_init(ast_channel_connected(tmp));
992 ast_party_redirecting_init(ast_channel_redirecting(tmp));
995 ast_channel_caller(tmp)->id.name.valid = 1;
996 ast_channel_caller(tmp)->id.name.str = ast_strdup(cid_name);
997 if (!ast_channel_caller(tmp)->id.name.str) {
998 return ast_channel_unref(tmp);
1002 ast_channel_caller(tmp)->id.number.valid = 1;
1003 ast_channel_caller(tmp)->id.number.str = ast_strdup(cid_num);
1004 if (!ast_channel_caller(tmp)->id.number.str) {
1005 return ast_channel_unref(tmp);
1009 if ((timer = ast_timer_open())) {
1010 ast_channel_timer_set(tmp, timer);
1011 if (strcmp(ast_timer_get_name(ast_channel_timer(tmp)), "timerfd")) {
1014 ast_channel_timingfd_set(tmp, ast_timer_fd(ast_channel_timer(tmp)));
1018 * This is the last place the channel constructor can fail.
1020 * The destructor takes advantage of this fact to ensure that the
1021 * AST_CEL_CHANNEL_END is not posted if we have not posted the
1022 * AST_CEL_CHANNEL_START yet.
1025 if (needqueue && ast_channel_internal_alertpipe_init(tmp)) {
1026 return ast_channel_unref(tmp);
1029 /* Always watch the alertpipe */
1030 ast_channel_set_fd(tmp, AST_ALERT_FD, ast_channel_internal_alert_readfd(tmp));
1031 /* And timing pipe */
1032 ast_channel_set_fd(tmp, AST_TIMING_FD, ast_channel_timingfd(tmp));
1035 ast_channel_state_set(tmp, state);
1037 ast_channel_streamid_set(tmp, -1);
1039 ast_channel_fin_set(tmp, global_fin);
1040 ast_channel_fout_set(tmp, global_fout);
1042 if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
1043 ast_channel_uniqueid_build(tmp, "%li.%d", (long) time(NULL),
1044 ast_atomic_fetchadd_int(&uniqueint, 1));
1046 ast_channel_uniqueid_build(tmp, "%s-%li.%d", ast_config_AST_SYSTEM_NAME,
1047 (long) time(NULL), ast_atomic_fetchadd_int(&uniqueint, 1));
1050 if (!ast_strlen_zero(linkedid)) {
1051 ast_channel_linkedid_set(tmp, linkedid);
1053 ast_channel_linkedid_set(tmp, ast_channel_uniqueid(tmp));
1056 if (!ast_strlen_zero(name_fmt)) {
1057 char *slash, *slash2;
1058 /* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call.
1059 * And they all use slightly different formats for their name string.
1060 * This means, to set the name here, we have to accept variable args, and call the string_field_build from here.
1061 * This means, that the stringfields must have a routine that takes the va_lists directly, and
1062 * uses them to build the string, instead of forming the va_lists internally from the vararg ... list.
1063 * This new function was written so this can be accomplished.
1065 ast_channel_name_build_va(tmp, name_fmt, ap);
1066 tech = ast_strdupa(ast_channel_name(tmp));
1067 if ((slash = strchr(tech, '/'))) {
1068 if ((slash2 = strchr(slash + 1, '/'))) {
1076 * Start the string with '-' so it becomes an empty string
1077 * in the destructor.
1079 ast_channel_name_set(tmp, "-**Unknown**");
1082 /* Reminder for the future: under what conditions do we NOT want to track cdrs on channels? */
1084 /* These 4 variables need to be set up for the cdr_init() to work right */
1086 ast_channel_amaflags_set(tmp, amaflag);
1088 ast_channel_amaflags_set(tmp, ast_default_amaflags);
1091 if (!ast_strlen_zero(acctcode))
1092 ast_channel_accountcode_set(tmp, acctcode);
1094 ast_channel_accountcode_set(tmp, ast_default_accountcode);
1096 ast_channel_context_set(tmp, S_OR(context, "default"));
1097 ast_channel_exten_set(tmp, S_OR(exten, "s"));
1098 ast_channel_priority_set(tmp, 1);
1100 ast_channel_cdr_set(tmp, ast_cdr_alloc());
1101 ast_cdr_init(ast_channel_cdr(tmp), tmp);
1102 ast_cdr_start(ast_channel_cdr(tmp));
1104 ast_atomic_fetchadd_int(&chancount, +1);
1105 ast_cel_report_event(tmp, AST_CEL_CHANNEL_START, NULL, NULL, NULL);
1107 headp = ast_channel_varshead(tmp);
1108 AST_LIST_HEAD_INIT_NOLOCK(headp);
1110 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
1112 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_autochans(tmp));
1114 ast_channel_language_set(tmp, defaultlanguage);
1116 ast_channel_tech_set(tmp, &null_tech);
1118 ao2_link(channels, tmp);
1121 * And now, since the channel structure is built, and has its name, let's
1122 * call the manager event generator with this Newchannel event. This is the
1123 * proper and correct place to make this call, but you sure do have to pass
1124 * a lot of data into this func to do it here!
1126 if (ast_get_channel_tech(tech) || (tech2 && ast_get_channel_tech(tech2))) {
1127 ast_manager_event(tmp, EVENT_FLAG_CALL, "Newchannel",
1129 "ChannelState: %d\r\n"
1130 "ChannelStateDesc: %s\r\n"
1131 "CallerIDNum: %s\r\n"
1132 "CallerIDName: %s\r\n"
1133 "AccountCode: %s\r\n"
1137 ast_channel_name(tmp),
1139 ast_state2str(state),
1142 ast_channel_accountcode(tmp),
1145 ast_channel_uniqueid(tmp));
1148 ast_channel_internal_finalize(tmp);
1152 struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
1153 const char *cid_name, const char *acctcode,
1154 const char *exten, const char *context,
1155 const char *linkedid, const int amaflag,
1156 const char *file, int line, const char *function,
1157 const char *name_fmt, ...)
1160 struct ast_channel *result;
1162 va_start(ap, name_fmt);
1163 result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
1164 linkedid, amaflag, file, line, function, name_fmt, ap);
1170 /* only do the minimum amount of work needed here to make a channel
1171 * structure that can be used to expand channel vars */
1172 #if defined(REF_DEBUG) || defined(__AST_DEBUG_MALLOC)
1173 struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
1175 struct ast_channel *ast_dummy_channel_alloc(void)
1178 struct ast_channel *tmp;
1179 struct varshead *headp;
1181 if (!(tmp = ast_channel_internal_alloc(ast_dummy_channel_destructor))) {
1182 /* Dummy channel structure allocation failure. */
1186 headp = ast_channel_varshead(tmp);
1187 AST_LIST_HEAD_INIT_NOLOCK(headp);
1192 static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
1194 struct ast_frame *f;
1195 struct ast_frame *cur;
1196 unsigned int new_frames = 0;
1197 unsigned int new_voice_frames = 0;
1198 unsigned int queued_frames = 0;
1199 unsigned int queued_voice_frames = 0;
1200 AST_LIST_HEAD_NOLOCK(,ast_frame) frames;
1202 ast_channel_lock(chan);
1205 * Check the last frame on the queue if we are queuing the new
1208 cur = AST_LIST_LAST(ast_channel_readq(chan));
1209 if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
1210 switch (cur->subclass.integer) {
1211 case AST_CONTROL_END_OF_Q:
1212 if (fin->frametype == AST_FRAME_CONTROL
1213 && fin->subclass.integer == AST_CONTROL_HANGUP) {
1215 * Destroy the end-of-Q marker frame so we can queue the hangup
1216 * frame in its place.
1218 AST_LIST_REMOVE(ast_channel_readq(chan), cur, frame_list);
1222 * This has degenerated to a normal queue append anyway. Since
1223 * we just destroyed the last frame in the queue we must make
1224 * sure that "after" is NULL or bad things will happen.
1230 case AST_CONTROL_HANGUP:
1231 /* Don't queue anything. */
1232 ast_channel_unlock(chan);
1239 /* Build copies of all the new frames and count them */
1240 AST_LIST_HEAD_INIT_NOLOCK(&frames);
1241 for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
1242 if (!(f = ast_frdup(cur))) {
1243 if (AST_LIST_FIRST(&frames)) {
1244 ast_frfree(AST_LIST_FIRST(&frames));
1246 ast_channel_unlock(chan);
1250 AST_LIST_INSERT_TAIL(&frames, f, frame_list);
1252 if (f->frametype == AST_FRAME_VOICE) {
1257 /* Count how many frames exist on the queue */
1258 AST_LIST_TRAVERSE(ast_channel_readq(chan), cur, frame_list) {
1260 if (cur->frametype == AST_FRAME_VOICE) {
1261 queued_voice_frames++;
1265 if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
1267 ast_log(LOG_WARNING, "Exceptionally long %squeue length queuing to %s\n", queued_frames + new_frames > 128 ? "" : "voice ", ast_channel_name(chan));
1268 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), cur, frame_list) {
1269 /* Save the most recent frame */
1270 if (!AST_LIST_NEXT(cur, frame_list)) {
1272 } else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
1276 AST_LIST_REMOVE_CURRENT(frame_list);
1280 AST_LIST_TRAVERSE_SAFE_END;
1284 AST_LIST_INSERT_LIST_AFTER(ast_channel_readq(chan), &frames, after, frame_list);
1287 AST_LIST_APPEND_LIST(&frames, ast_channel_readq(chan), frame_list);
1288 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_readq(chan));
1290 AST_LIST_APPEND_LIST(ast_channel_readq(chan), &frames, frame_list);
1293 if (ast_channel_alert_writable(chan)) {
1294 if (ast_channel_alert_write(chan)) {
1295 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %d): %s!\n",
1296 ast_channel_name(chan), queued_frames, strerror(errno));
1298 } else if (ast_channel_timingfd(chan) > -1) {
1299 ast_timer_enable_continuous(ast_channel_timer(chan));
1300 } else if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
1301 pthread_kill(ast_channel_blocker(chan), SIGURG);
1304 ast_channel_unlock(chan);
1309 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
1311 return __ast_queue_frame(chan, fin, 0, NULL);
1314 int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
1316 return __ast_queue_frame(chan, fin, 1, NULL);
1319 /*! \brief Queue a hangup frame for channel */
1320 int ast_queue_hangup(struct ast_channel *chan)
1322 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1325 /* Yeah, let's not change a lock-critical value without locking */
1326 ast_channel_lock(chan);
1327 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1329 manager_event(EVENT_FLAG_CALL, "HangupRequest",
1332 ast_channel_name(chan),
1333 ast_channel_uniqueid(chan));
1335 res = ast_queue_frame(chan, &f);
1336 ast_channel_unlock(chan);
1340 /*! \brief Queue a hangup frame for channel */
1341 int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
1343 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1347 f.data.uint32 = cause;
1350 /* Yeah, let's not change a lock-critical value without locking */
1351 ast_channel_lock(chan);
1352 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1354 f.data.uint32 = ast_channel_hangupcause(chan);
1357 manager_event(EVENT_FLAG_CALL, "HangupRequest",
1361 ast_channel_name(chan),
1362 ast_channel_uniqueid(chan),
1365 res = ast_queue_frame(chan, &f);
1366 ast_channel_unlock(chan);
1370 /*! \brief Queue a control frame */
1371 int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
1373 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
1374 return ast_queue_frame(chan, &f);
1377 /*! \brief Queue a control frame with payload */
1378 int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control,
1379 const void *data, size_t datalen)
1381 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
1382 return ast_queue_frame(chan, &f);
1385 /*! \brief Set defer DTMF flag on channel */
1386 int ast_channel_defer_dtmf(struct ast_channel *chan)
1391 pre = ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1392 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1397 /*! \brief Unset defer DTMF flag on channel */
1398 void ast_channel_undefer_dtmf(struct ast_channel *chan)
1401 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1404 struct ast_channel *ast_channel_callback(ao2_callback_data_fn *cb_fn, void *arg,
1405 void *data, int ao2_flags)
1407 return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
1410 static int ast_channel_by_name_cb(void *obj, void *arg, void *data, int flags)
1412 struct ast_channel *chan = obj;
1413 const char *name = arg;
1414 size_t name_len = *(size_t *) data;
1415 int ret = CMP_MATCH;
1417 if (ast_strlen_zero(name)) {
1418 ast_log(LOG_ERROR, "BUG! Must supply a channel name or partial name to match!\n");
1422 ast_channel_lock(chan);
1423 if ((!name_len && strcasecmp(ast_channel_name(chan), name))
1424 || (name_len && strncasecmp(ast_channel_name(chan), name, name_len))) {
1425 ret = 0; /* name match failed, keep looking */
1427 ast_channel_unlock(chan);
1432 static int ast_channel_by_exten_cb(void *obj, void *arg, void *data, int flags)
1434 struct ast_channel *chan = obj;
1435 char *context = arg;
1437 int ret = CMP_MATCH;
1439 if (ast_strlen_zero(exten) || ast_strlen_zero(context)) {
1440 ast_log(LOG_ERROR, "BUG! Must have a context and extension to match!\n");
1444 ast_channel_lock(chan);
1445 if (strcasecmp(ast_channel_context(chan), context) && strcasecmp(ast_channel_macrocontext(chan), context)) {
1446 ret = 0; /* Context match failed, continue */
1447 } else if (strcasecmp(ast_channel_exten(chan), exten) && strcasecmp(ast_channel_macroexten(chan), exten)) {
1448 ret = 0; /* Extension match failed, continue */
1450 ast_channel_unlock(chan);
1455 static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags)
1457 struct ast_channel *chan = obj;
1458 char *uniqueid = arg;
1459 size_t id_len = *(size_t *) data;
1460 int ret = CMP_MATCH;
1462 if (ast_strlen_zero(uniqueid)) {
1463 ast_log(LOG_ERROR, "BUG! Must supply a uniqueid or partial uniqueid to match!\n");
1467 ast_channel_lock(chan);
1468 if ((!id_len && strcasecmp(ast_channel_uniqueid(chan), uniqueid))
1469 || (id_len && strncasecmp(ast_channel_uniqueid(chan), uniqueid, id_len))) {
1470 ret = 0; /* uniqueid match failed, keep looking */
1472 ast_channel_unlock(chan);
1477 struct ast_channel_iterator {
1478 /* storage for non-dynamically allocated iterator */
1479 struct ao2_iterator simple_iterator;
1480 /* pointer to the actual iterator (simple_iterator or a dynamically
1481 * allocated iterator)
1483 struct ao2_iterator *active_iterator;
1486 struct ast_channel_iterator *ast_channel_iterator_destroy(struct ast_channel_iterator *i)
1488 ao2_iterator_destroy(i->active_iterator);
1494 struct ast_channel_iterator *ast_channel_iterator_by_exten_new(const char *exten, const char *context)
1496 struct ast_channel_iterator *i;
1497 char *l_exten = (char *) exten;
1498 char *l_context = (char *) context;
1500 if (!(i = ast_calloc(1, sizeof(*i)))) {
1504 i->active_iterator = (void *) ast_channel_callback(ast_channel_by_exten_cb,
1505 l_context, l_exten, OBJ_MULTIPLE);
1506 if (!i->active_iterator) {
1514 struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, size_t name_len)
1516 struct ast_channel_iterator *i;
1517 char *l_name = (char *) name;
1519 if (!(i = ast_calloc(1, sizeof(*i)))) {
1523 i->active_iterator = (void *) ast_channel_callback(ast_channel_by_name_cb,
1525 OBJ_MULTIPLE | (name_len == 0 /* match the whole word, so optimize */ ? OBJ_KEY : 0));
1526 if (!i->active_iterator) {
1534 struct ast_channel_iterator *ast_channel_iterator_all_new(void)
1536 struct ast_channel_iterator *i;
1538 if (!(i = ast_calloc(1, sizeof(*i)))) {
1542 i->simple_iterator = ao2_iterator_init(channels, 0);
1543 i->active_iterator = &i->simple_iterator;
1548 struct ast_channel *ast_channel_iterator_next(struct ast_channel_iterator *i)
1550 return ao2_iterator_next(i->active_iterator);
1553 /* Legacy function, not currently used for lookups, but we need a cmp_fn */
1554 static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
1556 ast_log(LOG_ERROR, "BUG! Should never be called!\n");
1560 struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
1562 struct ast_channel *chan;
1563 char *l_name = (char *) name;
1565 chan = ast_channel_callback(ast_channel_by_name_cb, l_name, &name_len,
1566 (name_len == 0) /* optimize if it is a complete name match */ ? OBJ_KEY : 0);
1571 if (ast_strlen_zero(l_name)) {
1572 /* We didn't have a name to search for so quit. */
1576 /* Now try a search for uniqueid. */
1577 return ast_channel_callback(ast_channel_by_uniqueid_cb, l_name, &name_len, 0);
1580 struct ast_channel *ast_channel_get_by_name(const char *name)
1582 return ast_channel_get_by_name_prefix(name, 0);
1585 struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
1587 char *l_exten = (char *) exten;
1588 char *l_context = (char *) context;
1590 return ast_channel_callback(ast_channel_by_exten_cb, l_context, l_exten, 0);
1593 int ast_is_deferrable_frame(const struct ast_frame *frame)
1595 /* Do not add a default entry in this switch statement. Each new
1596 * frame type should be addressed directly as to whether it should
1597 * be queued up or not.
1599 switch (frame->frametype) {
1600 case AST_FRAME_CONTROL:
1601 case AST_FRAME_TEXT:
1602 case AST_FRAME_IMAGE:
1603 case AST_FRAME_HTML:
1606 case AST_FRAME_DTMF_END:
1607 case AST_FRAME_DTMF_BEGIN:
1608 case AST_FRAME_VOICE:
1609 case AST_FRAME_VIDEO:
1610 case AST_FRAME_NULL:
1613 case AST_FRAME_MODEM:
1619 /*! \brief Wait, look for hangups and condition arg */
1620 int ast_safe_sleep_conditional(struct ast_channel *chan, int ms, int (*cond)(void*), void *data)
1622 struct ast_frame *f;
1623 struct ast_silence_generator *silgen = NULL;
1625 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
1627 AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
1629 /* If no other generator is present, start silencegen while waiting */
1630 if (ast_opt_transmit_silence && !ast_channel_generatordata(chan)) {
1631 silgen = ast_channel_start_silence_generator(chan);
1635 struct ast_frame *dup_f = NULL;
1636 if (cond && ((*cond)(data) == 0)) {
1639 ms = ast_waitfor(chan, ms);
1651 if (!ast_is_deferrable_frame(f)) {
1656 if ((dup_f = ast_frisolate(f))) {
1660 AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
1665 /* stop silgen if present */
1667 ast_channel_stop_silence_generator(chan, silgen);
1670 /* We need to free all the deferred frames, but we only need to
1671 * queue the deferred frames if there was no error and no
1672 * hangup was received
1674 ast_channel_lock(chan);
1675 while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
1677 ast_queue_frame_head(chan, f);
1681 ast_channel_unlock(chan);
1686 /*! \brief Wait, look for hangups */
1687 int ast_safe_sleep(struct ast_channel *chan, int ms)
1689 return ast_safe_sleep_conditional(chan, ms, NULL, NULL);
1692 struct ast_channel *ast_channel_release(struct ast_channel *chan)
1694 /* Safe, even if already unlinked. */
1695 ao2_unlink(channels, chan);
1696 return ast_channel_unref(chan);
1699 void ast_party_name_init(struct ast_party_name *init)
1702 init->char_set = AST_PARTY_CHAR_SET_ISO8859_1;
1703 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1707 void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
1710 /* Don't copy to self */
1714 ast_free(dest->str);
1715 dest->str = ast_strdup(src->str);
1716 dest->char_set = src->char_set;
1717 dest->presentation = src->presentation;
1718 dest->valid = src->valid;
1721 void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
1724 init->char_set = guide->char_set;
1725 init->presentation = guide->presentation;
1726 init->valid = guide->valid;
1729 void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
1732 /* Don't set to self */
1736 if (src->str && src->str != dest->str) {
1737 ast_free(dest->str);
1738 dest->str = ast_strdup(src->str);
1741 dest->char_set = src->char_set;
1742 dest->presentation = src->presentation;
1743 dest->valid = src->valid;
1746 void ast_party_name_free(struct ast_party_name *doomed)
1748 ast_free(doomed->str);
1752 void ast_party_number_init(struct ast_party_number *init)
1755 init->plan = 0;/* Unknown */
1756 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1760 void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
1763 /* Don't copy to self */
1767 ast_free(dest->str);
1768 dest->str = ast_strdup(src->str);
1769 dest->plan = src->plan;
1770 dest->presentation = src->presentation;
1771 dest->valid = src->valid;
1774 void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
1777 init->plan = guide->plan;
1778 init->presentation = guide->presentation;
1779 init->valid = guide->valid;
1782 void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
1785 /* Don't set to self */
1789 if (src->str && src->str != dest->str) {
1790 ast_free(dest->str);
1791 dest->str = ast_strdup(src->str);
1794 dest->plan = src->plan;
1795 dest->presentation = src->presentation;
1796 dest->valid = src->valid;
1799 void ast_party_number_free(struct ast_party_number *doomed)
1801 ast_free(doomed->str);
1805 void ast_party_subaddress_init(struct ast_party_subaddress *init)
1809 init->odd_even_indicator = 0;
1813 void ast_party_subaddress_copy(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1816 /* Don't copy to self */
1820 ast_free(dest->str);
1821 dest->str = ast_strdup(src->str);
1822 dest->type = src->type;
1823 dest->odd_even_indicator = src->odd_even_indicator;
1824 dest->valid = src->valid;
1827 void ast_party_subaddress_set_init(struct ast_party_subaddress *init, const struct ast_party_subaddress *guide)
1830 init->type = guide->type;
1831 init->odd_even_indicator = guide->odd_even_indicator;
1832 init->valid = guide->valid;
1835 void ast_party_subaddress_set(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1838 /* Don't set to self */
1842 if (src->str && src->str != dest->str) {
1843 ast_free(dest->str);
1844 dest->str = ast_strdup(src->str);
1847 dest->type = src->type;
1848 dest->odd_even_indicator = src->odd_even_indicator;
1849 dest->valid = src->valid;
1852 void ast_party_subaddress_free(struct ast_party_subaddress *doomed)
1854 ast_free(doomed->str);
1858 void ast_party_id_init(struct ast_party_id *init)
1860 ast_party_name_init(&init->name);
1861 ast_party_number_init(&init->number);
1862 ast_party_subaddress_init(&init->subaddress);
1866 void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
1869 /* Don't copy to self */
1873 ast_party_name_copy(&dest->name, &src->name);
1874 ast_party_number_copy(&dest->number, &src->number);
1875 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1877 ast_free(dest->tag);
1878 dest->tag = ast_strdup(src->tag);
1881 void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
1883 ast_party_name_set_init(&init->name, &guide->name);
1884 ast_party_number_set_init(&init->number, &guide->number);
1885 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
1889 void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
1892 /* Don't set to self */
1896 if (!update || update->name) {
1897 ast_party_name_set(&dest->name, &src->name);
1899 if (!update || update->number) {
1900 ast_party_number_set(&dest->number, &src->number);
1902 if (!update || update->subaddress) {
1903 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
1906 if (src->tag && src->tag != dest->tag) {
1907 ast_free(dest->tag);
1908 dest->tag = ast_strdup(src->tag);
1912 void ast_party_id_free(struct ast_party_id *doomed)
1914 ast_party_name_free(&doomed->name);
1915 ast_party_number_free(&doomed->number);
1916 ast_party_subaddress_free(&doomed->subaddress);
1918 ast_free(doomed->tag);
1922 int ast_party_id_presentation(const struct ast_party_id *id)
1924 int number_priority;
1926 int number_screening;
1930 /* Determine name presentation priority. */
1931 if (!id->name.valid) {
1932 name_value = AST_PRES_UNAVAILABLE;
1935 name_value = id->name.presentation & AST_PRES_RESTRICTION;
1936 switch (name_value) {
1937 case AST_PRES_RESTRICTED:
1940 case AST_PRES_ALLOWED:
1943 case AST_PRES_UNAVAILABLE:
1947 name_value = AST_PRES_UNAVAILABLE;
1953 /* Determine number presentation priority. */
1954 if (!id->number.valid) {
1955 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1956 number_value = AST_PRES_UNAVAILABLE;
1957 number_priority = 3;
1959 number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
1960 number_value = id->number.presentation & AST_PRES_RESTRICTION;
1961 switch (number_value) {
1962 case AST_PRES_RESTRICTED:
1963 number_priority = 0;
1965 case AST_PRES_ALLOWED:
1966 number_priority = 1;
1968 case AST_PRES_UNAVAILABLE:
1969 number_priority = 2;
1972 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1973 number_value = AST_PRES_UNAVAILABLE;
1974 number_priority = 3;
1979 /* Select the wining presentation value. */
1980 if (name_priority < number_priority) {
1981 number_value = name_value;
1983 if (number_value == AST_PRES_UNAVAILABLE) {
1984 return AST_PRES_NUMBER_NOT_AVAILABLE;
1987 return number_value | number_screening;
1990 void ast_party_dialed_init(struct ast_party_dialed *init)
1992 init->number.str = NULL;
1993 init->number.plan = 0;/* Unknown */
1994 ast_party_subaddress_init(&init->subaddress);
1995 init->transit_network_select = 0;
1998 void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
2001 /* Don't copy to self */
2005 ast_free(dest->number.str);
2006 dest->number.str = ast_strdup(src->number.str);
2007 dest->number.plan = src->number.plan;
2008 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
2009 dest->transit_network_select = src->transit_network_select;
2012 void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
2014 init->number.str = NULL;
2015 init->number.plan = guide->number.plan;
2016 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
2017 init->transit_network_select = guide->transit_network_select;
2020 void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
2022 if (src->number.str && src->number.str != dest->number.str) {
2023 ast_free(dest->number.str);
2024 dest->number.str = ast_strdup(src->number.str);
2026 dest->number.plan = src->number.plan;
2028 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
2030 dest->transit_network_select = src->transit_network_select;
2033 void ast_party_dialed_free(struct ast_party_dialed *doomed)
2035 ast_free(doomed->number.str);
2036 doomed->number.str = NULL;
2037 ast_party_subaddress_free(&doomed->subaddress);
2040 void ast_party_caller_init(struct ast_party_caller *init)
2042 ast_party_id_init(&init->id);
2043 ast_party_id_init(&init->ani);
2047 void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
2050 /* Don't copy to self */
2054 ast_party_id_copy(&dest->id, &src->id);
2055 ast_party_id_copy(&dest->ani, &src->ani);
2056 dest->ani2 = src->ani2;
2059 void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
2061 ast_party_id_set_init(&init->id, &guide->id);
2062 ast_party_id_set_init(&init->ani, &guide->ani);
2063 init->ani2 = guide->ani2;
2066 void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
2068 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2069 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2070 dest->ani2 = src->ani2;
2073 void ast_party_caller_free(struct ast_party_caller *doomed)
2075 ast_party_id_free(&doomed->id);
2076 ast_party_id_free(&doomed->ani);
2079 void ast_party_connected_line_init(struct ast_party_connected_line *init)
2081 ast_party_id_init(&init->id);
2082 ast_party_id_init(&init->ani);
2084 init->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2087 void ast_party_connected_line_copy(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
2090 /* Don't copy to self */
2094 ast_party_id_copy(&dest->id, &src->id);
2095 ast_party_id_copy(&dest->ani, &src->ani);
2096 dest->ani2 = src->ani2;
2097 dest->source = src->source;
2100 void ast_party_connected_line_set_init(struct ast_party_connected_line *init, const struct ast_party_connected_line *guide)
2102 ast_party_id_set_init(&init->id, &guide->id);
2103 ast_party_id_set_init(&init->ani, &guide->ani);
2104 init->ani2 = guide->ani2;
2105 init->source = guide->source;
2108 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)
2110 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2111 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2112 dest->ani2 = src->ani2;
2113 dest->source = src->source;
2116 void ast_party_connected_line_collect_caller(struct ast_party_connected_line *connected, struct ast_party_caller *caller)
2118 connected->id = caller->id;
2119 connected->ani = caller->ani;
2120 connected->ani2 = caller->ani2;
2121 connected->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2124 void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
2126 ast_party_id_free(&doomed->id);
2127 ast_party_id_free(&doomed->ani);
2130 void ast_party_redirecting_init(struct ast_party_redirecting *init)
2132 ast_party_id_init(&init->orig);
2133 ast_party_id_init(&init->from);
2134 ast_party_id_init(&init->to);
2136 init->reason = AST_REDIRECTING_REASON_UNKNOWN;
2137 init->orig_reason = AST_REDIRECTING_REASON_UNKNOWN;
2140 void ast_party_redirecting_copy(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src)
2143 /* Don't copy to self */
2147 ast_party_id_copy(&dest->orig, &src->orig);
2148 ast_party_id_copy(&dest->from, &src->from);
2149 ast_party_id_copy(&dest->to, &src->to);
2150 dest->count = src->count;
2151 dest->reason = src->reason;
2152 dest->orig_reason = src->orig_reason;
2155 void ast_party_redirecting_set_init(struct ast_party_redirecting *init, const struct ast_party_redirecting *guide)
2157 ast_party_id_set_init(&init->orig, &guide->orig);
2158 ast_party_id_set_init(&init->from, &guide->from);
2159 ast_party_id_set_init(&init->to, &guide->to);
2160 init->count = guide->count;
2161 init->reason = guide->reason;
2162 init->orig_reason = guide->orig_reason;
2165 void ast_party_redirecting_set(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src, const struct ast_set_party_redirecting *update)
2167 ast_party_id_set(&dest->orig, &src->orig, update ? &update->orig : NULL);
2168 ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
2169 ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
2170 dest->count = src->count;
2171 dest->reason = src->reason;
2172 dest->orig_reason = src->orig_reason;
2175 void ast_party_redirecting_free(struct ast_party_redirecting *doomed)
2177 ast_party_id_free(&doomed->orig);
2178 ast_party_id_free(&doomed->from);
2179 ast_party_id_free(&doomed->to);
2182 /*! \brief Free a channel structure */
2183 static void ast_channel_destructor(void *obj)
2185 struct ast_channel *chan = obj;
2189 struct ast_var_t *vardata;
2190 struct ast_frame *f;
2191 struct varshead *headp;
2192 struct ast_datastore *datastore;
2193 char device_name[AST_CHANNEL_NAME];
2194 struct ast_callid *callid;
2196 if (ast_channel_internal_is_finalized(chan)) {
2197 ast_cel_report_event(chan, AST_CEL_CHANNEL_END, NULL, NULL, NULL);
2198 ast_cel_check_retire_linkedid(chan);
2201 /* Get rid of each of the data stores on the channel */
2202 ast_channel_lock(chan);
2203 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry)))
2204 /* Free the data store */
2205 ast_datastore_free(datastore);
2207 /* While the channel is locked, take the reference to its callid while we tear down the call. */
2208 callid = ast_channel_callid(chan);
2209 ast_channel_callid_cleanup(chan);
2211 ast_channel_unlock(chan);
2213 /* Lock and unlock the channel just to be sure nobody has it locked still
2214 due to a reference that was stored in a datastore. (i.e. app_chanspy) */
2215 ast_channel_lock(chan);
2216 ast_channel_unlock(chan);
2218 if (ast_channel_tech_pvt(chan)) {
2219 ast_log_callid(LOG_WARNING, callid, "Channel '%s' may not have been hung up properly\n", ast_channel_name(chan));
2220 ast_free(ast_channel_tech_pvt(chan));
2223 if (ast_channel_sched(chan)) {
2224 ast_sched_context_destroy(ast_channel_sched(chan));
2227 if (ast_channel_internal_is_finalized(chan)) {
2230 ast_copy_string(device_name, ast_channel_name(chan), sizeof(device_name));
2231 if ((dashptr = strrchr(device_name, '-'))) {
2235 device_name[0] = '\0';
2238 /* Stop monitoring */
2239 if (ast_channel_monitor(chan))
2240 ast_channel_monitor(chan)->stop( chan, 0 );
2242 /* If there is native format music-on-hold state, free it */
2243 if (ast_channel_music_state(chan))
2244 ast_moh_cleanup(chan);
2246 /* Free translators */
2247 if (ast_channel_readtrans(chan))
2248 ast_translator_free_path(ast_channel_readtrans(chan));
2249 if (ast_channel_writetrans(chan))
2250 ast_translator_free_path(ast_channel_writetrans(chan));
2251 if (ast_channel_pbx(chan))
2252 ast_log_callid(LOG_WARNING, callid, "PBX may not have been terminated properly on '%s'\n", ast_channel_name(chan));
2254 ast_party_dialed_free(ast_channel_dialed(chan));
2255 ast_party_caller_free(ast_channel_caller(chan));
2256 ast_party_connected_line_free(ast_channel_connected(chan));
2257 ast_party_redirecting_free(ast_channel_redirecting(chan));
2259 /* Close pipes if appropriate */
2260 ast_channel_internal_alertpipe_close(chan);
2261 if (ast_channel_timer(chan)) {
2262 ast_timer_close(ast_channel_timer(chan));
2265 for (i = 0; i < AST_MAX_FDS; i++) {
2266 if (ast_channel_internal_epfd_data(chan, i)) {
2267 ast_free(ast_channel_internal_epfd_data(chan, i));
2270 close(ast_channel_epfd(chan));
2272 while ((f = AST_LIST_REMOVE_HEAD(ast_channel_readq(chan), frame_list)))
2275 /* loop over the variables list, freeing all data and deleting list items */
2276 /* no need to lock the list, as the channel is already locked */
2277 headp = ast_channel_varshead(chan);
2278 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2279 ast_var_delete(vardata);
2281 ast_app_group_discard(chan);
2283 /* Destroy the jitterbuffer */
2284 ast_jb_destroy(chan);
2286 if (ast_channel_cdr(chan)) {
2287 ast_cdr_discard(ast_channel_cdr(chan));
2288 ast_channel_cdr_set(chan, NULL);
2291 if (ast_channel_zone(chan)) {
2292 ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan)));
2295 ast_channel_internal_cleanup(chan);
2297 if (device_name[0]) {
2299 * We have a device name to notify of a new state.
2301 * Queue an unknown state, because, while we know that this particular
2302 * instance is dead, we don't know the state of all other possible
2305 ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, device_name);
2308 ast_channel_nativeformats_set(chan, ast_format_cap_destroy(ast_channel_nativeformats(chan)));
2310 ast_callid_unref(callid);
2312 ast_atomic_fetchadd_int(&chancount, -1);
2315 /*! \brief Free a dummy channel structure */
2316 static void ast_dummy_channel_destructor(void *obj)
2318 struct ast_channel *chan = obj;
2319 struct ast_var_t *vardata;
2320 struct varshead *headp;
2322 headp = ast_channel_varshead(chan);
2324 ast_party_dialed_free(ast_channel_dialed(chan));
2325 ast_party_caller_free(ast_channel_caller(chan));
2326 ast_party_connected_line_free(ast_channel_connected(chan));
2327 ast_party_redirecting_free(ast_channel_redirecting(chan));
2329 /* loop over the variables list, freeing all data and deleting list items */
2330 /* no need to lock the list, as the channel is already locked */
2331 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2332 ast_var_delete(vardata);
2334 if (ast_channel_cdr(chan)) {
2335 ast_cdr_discard(ast_channel_cdr(chan));
2336 ast_channel_cdr_set(chan, NULL);
2339 ast_channel_internal_cleanup(chan);
2342 struct ast_datastore *ast_channel_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
2344 return ast_datastore_alloc(info, uid);
2347 int ast_channel_datastore_free(struct ast_datastore *datastore)
2349 return ast_datastore_free(datastore);
2352 int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to)
2354 struct ast_datastore *datastore = NULL, *datastore2;
2356 AST_LIST_TRAVERSE(ast_channel_datastores(from), datastore, entry) {
2357 if (datastore->inheritance > 0) {
2358 datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
2360 datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
2361 datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
2362 AST_LIST_INSERT_TAIL(ast_channel_datastores(to), datastore2, entry);
2369 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
2373 AST_LIST_INSERT_HEAD(ast_channel_datastores(chan), datastore, entry);
2378 int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
2380 return AST_LIST_REMOVE(ast_channel_datastores(chan), datastore, entry) ? 0 : -1;
2383 struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
2385 struct ast_datastore *datastore = NULL;
2390 AST_LIST_TRAVERSE(ast_channel_datastores(chan), datastore, entry) {
2391 if (datastore->info != info) {
2396 /* matched by type only */
2400 if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
2401 /* Matched by type AND uid */
2409 /*! Set the file descriptor on the channel */
2410 void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
2413 struct epoll_event ev;
2414 struct ast_epoll_data *aed = NULL;
2416 if (ast_channel_fd_isset(chan, which)) {
2417 epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_DEL, ast_channel_fd(chan, which), &ev);
2418 aed = ast_channel_internal_epfd_data(chan, which);
2421 /* If this new fd is valid, add it to the epoll */
2423 if (!aed && (!(aed = ast_calloc(1, sizeof(*aed)))))
2426 ast_channel_internal_epfd_data_set(chan, which, aed);
2430 ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2432 epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_ADD, fd, &ev);
2434 /* We don't have to keep around this epoll data structure now */
2436 ast_channel_epfd_data_set(chan, which, NULL);
2439 ast_channel_internal_fd_set(chan, which, fd);
2443 /*! Add a channel to an optimized waitfor */
2444 void ast_poll_channel_add(struct ast_channel *chan0, struct ast_channel *chan1)
2447 struct epoll_event ev;
2450 if (ast_channel_epfd(chan0) == -1)
2453 /* Iterate through the file descriptors on chan1, adding them to chan0 */
2454 for (i = 0; i < AST_MAX_FDS; i++) {
2455 if (!ast_channel_fd_isset(chan1, i)) {
2458 ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2459 ev.data.ptr = ast_channel_internal_epfd_data(chan1, i);
2460 epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_ADD, ast_channel_fd(chan1, i), &ev);
2467 /*! Delete a channel from an optimized waitfor */
2468 void ast_poll_channel_del(struct ast_channel *chan0, struct ast_channel *chan1)
2471 struct epoll_event ev;
2474 if (ast_channel_epfd(chan0) == -1)
2477 for (i = 0; i < AST_MAX_FDS; i++) {
2478 if (!ast_channel_fd_isset(chan1, i)) {
2481 epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_DEL, ast_channel_fd(chan1, i), &ev);
2488 void ast_channel_clear_softhangup(struct ast_channel *chan, int flag)
2490 ast_channel_lock(chan);
2492 ast_channel_softhangup_internal_flag_clear(chan, flag);
2494 if (!ast_channel_softhangup_internal_flag(chan)) {
2495 struct ast_frame *fr;
2497 /* If we have completely cleared the softhangup flag,
2498 * then we need to fully abort the hangup process. This requires
2499 * pulling the END_OF_Q frame out of the channel frame queue if it
2500 * still happens to be there. */
2502 fr = AST_LIST_LAST(ast_channel_readq(chan));
2503 if (fr && fr->frametype == AST_FRAME_CONTROL &&
2504 fr->subclass.integer == AST_CONTROL_END_OF_Q) {
2505 AST_LIST_REMOVE(ast_channel_readq(chan), fr, frame_list);
2510 ast_channel_unlock(chan);
2513 /*! \brief Softly hangup a channel, don't lock */
2514 int ast_softhangup_nolock(struct ast_channel *chan, int cause)
2516 ast_debug(1, "Soft-Hanging up channel '%s'\n", ast_channel_name(chan));
2517 /* Inform channel driver that we need to be hung up, if it cares */
2518 ast_channel_softhangup_internal_flag_add(chan, cause);
2519 ast_queue_frame(chan, &ast_null_frame);
2520 /* Interrupt any poll call or such */
2521 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING))
2522 pthread_kill(ast_channel_blocker(chan), SIGURG);
2526 /*! \brief Softly hangup a channel, lock */
2527 int ast_softhangup(struct ast_channel *chan, int cause)
2531 ast_channel_lock(chan);
2532 res = ast_softhangup_nolock(chan, cause);
2533 manager_event(EVENT_FLAG_CALL, "SoftHangupRequest",
2537 ast_channel_name(chan),
2538 ast_channel_uniqueid(chan),
2540 ast_channel_unlock(chan);
2545 static void free_translation(struct ast_channel *clonechan)
2547 if (ast_channel_writetrans(clonechan))
2548 ast_translator_free_path(ast_channel_writetrans(clonechan));
2549 if (ast_channel_readtrans(clonechan))
2550 ast_translator_free_path(ast_channel_readtrans(clonechan));
2551 ast_channel_writetrans_set(clonechan, NULL);
2552 ast_channel_readtrans_set(clonechan, NULL);
2553 if (ast_format_cap_is_empty(ast_channel_nativeformats(clonechan))) {
2554 ast_format_clear(ast_channel_rawwriteformat(clonechan));
2555 ast_format_clear(ast_channel_rawreadformat(clonechan));
2557 struct ast_format tmpfmt;
2558 ast_best_codec(ast_channel_nativeformats(clonechan), &tmpfmt);
2559 ast_format_copy(ast_channel_rawwriteformat(clonechan), &tmpfmt);
2560 ast_format_copy(ast_channel_rawreadformat(clonechan), &tmpfmt);
2564 void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
2566 struct ast_channel *bridge;
2568 ast_channel_lock(chan);
2569 if (force || ast_strlen_zero(ast_channel_hangupsource(chan))) {
2570 ast_channel_hangupsource_set(chan, source);
2572 bridge = ast_bridged_channel(chan);
2573 ast_channel_unlock(chan);
2575 if (bridge && (force || ast_strlen_zero(ast_channel_hangupsource(bridge)))) {
2576 ast_channel_lock(bridge);
2577 ast_channel_hangupsource_set(chan, source);
2578 ast_channel_unlock(bridge);
2582 static void destroy_hooks(struct ast_channel *chan)
2584 if (ast_channel_audiohooks(chan)) {
2585 ast_audiohook_detach_list(ast_channel_audiohooks(chan));
2586 ast_channel_audiohooks_set(chan, NULL);
2589 ast_framehook_list_destroy(chan);
2592 /*! \brief Hangup a channel */
2593 int ast_hangup(struct ast_channel *chan)
2595 char extra_str[64]; /* used for cel logging below */
2598 ast_autoservice_stop(chan);
2600 ast_channel_lock(chan);
2603 * Do the masquerade if someone is setup to masquerade into us.
2605 * NOTE: We must hold the channel lock after testing for a
2606 * pending masquerade and setting the channel as a zombie to
2607 * prevent __ast_channel_masquerade() from setting up a
2608 * masquerade with a dead channel.
2610 while (ast_channel_masq(chan)) {
2611 ast_channel_unlock(chan);
2612 ast_do_masquerade(chan);
2613 ast_channel_lock(chan);
2616 if (ast_channel_masqr(chan)) {
2618 * This channel is one which will be masqueraded into something.
2619 * Mark it as a zombie already so ast_do_masquerade() will know
2622 ast_set_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE);
2623 destroy_hooks(chan);
2624 ast_channel_unlock(chan);
2628 /* Mark as a zombie so a masquerade cannot be setup on this channel. */
2629 if (!(was_zombie = ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE))) {
2630 ast_set_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE);
2633 ast_channel_unlock(chan);
2634 ao2_unlink(channels, chan);
2635 ast_channel_lock(chan);
2637 destroy_hooks(chan);
2639 free_translation(chan);
2640 /* Close audio stream */
2641 if (ast_channel_stream(chan)) {
2642 ast_closestream(ast_channel_stream(chan));
2643 ast_channel_stream_set(chan, NULL);
2645 /* Close video stream */
2646 if (ast_channel_vstream(chan)) {
2647 ast_closestream(ast_channel_vstream(chan));
2648 ast_channel_vstream_set(chan, NULL);
2650 if (ast_channel_sched(chan)) {
2651 ast_sched_context_destroy(ast_channel_sched(chan));
2652 ast_channel_sched_set(chan, NULL);
2655 if (ast_channel_generatordata(chan)) { /* Clear any tone stuff remaining */
2656 if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2657 ast_channel_generator(chan)->release(chan, ast_channel_generatordata(chan));
2660 ast_channel_generatordata_set(chan, NULL);
2661 ast_channel_generator_set(chan, NULL);
2663 snprintf(extra_str, sizeof(extra_str), "%d,%s,%s", ast_channel_hangupcause(chan), ast_channel_hangupsource(chan), S_OR(pbx_builtin_getvar_helper(chan, "DIALSTATUS"), ""));
2664 ast_cel_report_event(chan, AST_CEL_HANGUP, NULL, extra_str, NULL);
2666 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
2667 ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
2668 "is blocked by thread %ld in procedure %s! Expect a failure\n",
2669 (long) pthread_self(), ast_channel_name(chan), (long)ast_channel_blocker(chan), ast_channel_blockproc(chan));
2670 ast_assert(ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING) == 0);
2673 ast_debug(1, "Hanging up channel '%s'\n", ast_channel_name(chan));
2675 if (ast_channel_tech(chan)->hangup) {
2676 ast_channel_tech(chan)->hangup(chan);
2679 ast_debug(1, "Hanging up zombie '%s'\n", ast_channel_name(chan));
2682 ast_channel_unlock(chan);
2685 ast_manager_event(chan, EVENT_FLAG_CALL, "Hangup",
2688 "CallerIDNum: %s\r\n"
2689 "CallerIDName: %s\r\n"
2690 "ConnectedLineNum: %s\r\n"
2691 "ConnectedLineName: %s\r\n"
2693 "Cause-txt: %s\r\n",
2694 ast_channel_name(chan),
2695 ast_channel_uniqueid(chan),
2696 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, "<unknown>"),
2697 S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, "<unknown>"),
2698 S_COR(ast_channel_connected(chan)->id.number.valid, ast_channel_connected(chan)->id.number.str, "<unknown>"),
2699 S_COR(ast_channel_connected(chan)->id.name.valid, ast_channel_connected(chan)->id.name.str, "<unknown>"),
2700 ast_channel_hangupcause(chan),
2701 ast_cause2str(ast_channel_hangupcause(chan))
2704 if (ast_channel_cdr(chan) && !ast_test_flag(ast_channel_cdr(chan), AST_CDR_FLAG_BRIDGED) &&
2705 !ast_test_flag(ast_channel_cdr(chan), AST_CDR_FLAG_POST_DISABLED) &&
2706 (ast_channel_cdr(chan)->disposition != AST_CDR_NULL || ast_test_flag(ast_channel_cdr(chan), AST_CDR_FLAG_DIALED))) {
2707 ast_channel_lock(chan);
2708 ast_cdr_end(ast_channel_cdr(chan));
2709 ast_cdr_detach(ast_channel_cdr(chan));
2710 ast_channel_cdr_set(chan, NULL);
2711 ast_channel_unlock(chan);
2714 ast_channel_unref(chan);
2719 int ast_raw_answer(struct ast_channel *chan, int cdr_answer)
2723 ast_channel_lock(chan);
2725 /* You can't answer an outbound call */
2726 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING)) {
2727 ast_channel_unlock(chan);
2731 /* Stop if we're a zombie or need a soft hangup */
2732 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
2733 ast_channel_unlock(chan);
2737 ast_channel_unlock(chan);
2739 switch (ast_channel_state(chan)) {
2740 case AST_STATE_RINGING:
2741 case AST_STATE_RING:
2742 ast_channel_lock(chan);
2743 if (ast_channel_tech(chan)->answer) {
2744 res = ast_channel_tech(chan)->answer(chan);
2746 ast_setstate(chan, AST_STATE_UP);
2748 ast_cdr_answer(ast_channel_cdr(chan));
2750 ast_cel_report_event(chan, AST_CEL_ANSWER, NULL, NULL, NULL);
2751 ast_channel_unlock(chan);
2754 ast_cel_report_event(chan, AST_CEL_ANSWER, NULL, NULL, NULL);
2755 /* Calling ast_cdr_answer when it it has previously been called
2756 * is essentially a no-op, so it is safe.
2759 ast_cdr_answer(ast_channel_cdr(chan));
2766 ast_indicate(chan, -1);
2771 int __ast_answer(struct ast_channel *chan, unsigned int delay, int cdr_answer)
2774 enum ast_channel_state old_state;
2776 old_state = ast_channel_state(chan);
2777 if ((res = ast_raw_answer(chan, cdr_answer))) {
2781 switch (old_state) {
2782 case AST_STATE_RINGING:
2783 case AST_STATE_RING:
2784 /* wait for media to start flowing, but don't wait any longer
2785 * than 'delay' or 500 milliseconds, whichever is longer
2788 AST_LIST_HEAD_NOLOCK(, ast_frame) frames;
2789 struct ast_frame *cur, *new;
2790 int ms = MAX(delay, 500);
2791 unsigned int done = 0;
2793 AST_LIST_HEAD_INIT_NOLOCK(&frames);
2796 ms = ast_waitfor(chan, ms);
2798 ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", ast_channel_name(chan), strerror(errno));
2803 ast_debug(2, "Didn't receive a media frame from %s within %d ms of answering. Continuing anyway\n", ast_channel_name(chan), MAX(delay, 500));
2806 cur = ast_read(chan);
2807 if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
2808 (cur->subclass.integer == AST_CONTROL_HANGUP))) {
2813 ast_debug(2, "Hangup of channel %s detected in answer routine\n", ast_channel_name(chan));
2817 if ((new = ast_frisolate(cur)) != cur) {
2821 AST_LIST_INSERT_HEAD(&frames, new, frame_list);
2823 /* if a specific delay period was requested, continue
2824 * until that delay has passed. don't stop just because
2825 * incoming media has arrived.
2831 switch (new->frametype) {
2832 /* all of these frametypes qualify as 'media' */
2833 case AST_FRAME_VOICE:
2834 case AST_FRAME_VIDEO:
2835 case AST_FRAME_TEXT:
2836 case AST_FRAME_DTMF_BEGIN:
2837 case AST_FRAME_DTMF_END:
2838 case AST_FRAME_IMAGE:
2839 case AST_FRAME_HTML:
2840 case AST_FRAME_MODEM:
2843 case AST_FRAME_CONTROL:
2845 case AST_FRAME_NULL:
2856 ast_channel_lock(chan);
2857 while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
2858 ast_queue_frame_head(chan, cur);
2861 ast_channel_unlock(chan);
2872 int ast_answer(struct ast_channel *chan)
2874 return __ast_answer(chan, 0, 1);
2877 void ast_deactivate_generator(struct ast_channel *chan)
2879 ast_channel_lock(chan);
2880 if (ast_channel_generatordata(chan)) {
2881 if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2882 ast_channel_generator(chan)->release(chan, ast_channel_generatordata(chan));
2884 ast_channel_generatordata_set(chan, NULL);
2885 ast_channel_generator_set(chan, NULL);
2886 ast_channel_set_fd(chan, AST_GENERATOR_FD, -1);
2887 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
2888 ast_settimeout(chan, 0, NULL, NULL);
2890 ast_channel_unlock(chan);
2893 static void generator_write_format_change(struct ast_channel *chan)
2895 ast_channel_lock(chan);
2896 if (ast_channel_generator(chan) && ast_channel_generator(chan)->write_format_change) {
2897 ast_channel_generator(chan)->write_format_change(chan, ast_channel_generatordata(chan));
2899 ast_channel_unlock(chan);
2902 static int generator_force(const void *data)
2904 /* Called if generator doesn't have data */
2907 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
2908 struct ast_channel *chan = (struct ast_channel *)data;
2910 ast_channel_lock(chan);
2911 tmp = ast_channel_generatordata(chan);
2912 ast_channel_generatordata_set(chan, NULL);
2913 if (ast_channel_generator(chan))
2914 generate = ast_channel_generator(chan)->generate;
2915 ast_channel_unlock(chan);
2917 if (!tmp || !generate)
2920 res = generate(chan, tmp, 0, ast_format_rate(ast_channel_writeformat(chan)) / 50);
2922 ast_channel_generatordata_set(chan, tmp);
2925 ast_debug(1, "Auto-deactivating generator\n");
2926 ast_deactivate_generator(chan);
2932 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
2935 void *generatordata = NULL;
2937 ast_channel_lock(chan);
2938 if (ast_channel_generatordata(chan)) {
2939 if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2940 ast_channel_generator(chan)->release(chan, ast_channel_generatordata(chan));
2943 if (gen->alloc && !(generatordata = gen->alloc(chan, params))) {
2946 ast_channel_generatordata_set(chan, generatordata);
2948 ast_settimeout(chan, 50, generator_force, chan);
2949 ast_channel_generator_set(chan, gen);
2951 ast_channel_unlock(chan);
2958 /*! \brief Wait for x amount of time on a file descriptor to have input. */
2959 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
2962 ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
2966 /*! \brief Wait for x amount of time on a file descriptor to have input. */
2968 static struct ast_channel *ast_waitfor_nandfds_classic(struct ast_channel **c, int n, int *fds, int nfds,
2969 int *exception, int *outfd, int *ms)
2971 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
2972 int *exception, int *outfd, int *ms)
2975 struct timeval start = { 0 , 0 };
2976 struct pollfd *pfds = NULL;
2981 struct timeval now = { 0, 0 };
2982 struct timeval whentohangup = { 0, 0 }, diff;
2983 struct ast_channel *winner = NULL;
2996 if ((sz = n * AST_MAX_FDS + nfds)) {
2997 pfds = alloca(sizeof(*pfds) * sz);
2998 fdmap = alloca(sizeof(*fdmap) * sz);
3000 /* nothing to allocate and no FDs to check */
3004 /* Perform any pending masquerades */
3005 for (x = 0; x < n; x++) {
3006 while (ast_channel_masq(c[x])) {
3007 ast_do_masquerade(c[x]);
3010 ast_channel_lock(c[x]);
3011 if (!ast_tvzero(*ast_channel_whentohangup(c[x]))) {
3012 if (ast_tvzero(whentohangup))
3014 diff = ast_tvsub(*ast_channel_whentohangup(c[x]), now);
3015 if (diff.tv_sec < 0 || ast_tvzero(diff)) {
3016 /* Should already be hungup */
3017 ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3018 ast_channel_unlock(c[x]);
3021 if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
3022 whentohangup = diff;
3024 ast_channel_unlock(c[x]);
3026 /* Wait full interval */
3028 /* INT_MAX, not LONG_MAX, because it matters on 64-bit */
3029 if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
3030 rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000; /* timeout in milliseconds */
3031 if (*ms >= 0 && *ms < rms) { /* original *ms still smaller */
3034 } else if (!ast_tvzero(whentohangup) && rms < 0) {
3035 /* Tiny corner case... call would need to last >24 days */
3039 * Build the pollfd array, putting the channels' fds first,
3040 * followed by individual fds. Order is important because
3041 * individual fd's must have priority over channel fds.
3044 for (x = 0; x < n; x++) {
3045 for (y = 0; y < AST_MAX_FDS; y++) {
3046 fdmap[max].fdno = y; /* fd y is linked to this pfds */
3047 fdmap[max].chan = x; /* channel x is linked to this pfds */
3048 max += ast_add_fd(&pfds[max], ast_channel_fd(c[x], y));
3050 CHECK_BLOCKING(c[x]);
3052 /* Add the individual fds */
3053 for (x = 0; x < nfds; x++) {
3054 fdmap[max].chan = -1;
3055 max += ast_add_fd(&pfds[max], fds[x]);
3059 start = ast_tvnow();
3062 if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */
3065 if (kbrms > 600000) {
3068 res = ast_poll(pfds, max, kbrms);
3072 } while (!res && (rms > 0));
3074 res = ast_poll(pfds, max, rms);
3076 for (x = 0; x < n; x++) {
3077 ast_clear_flag(ast_channel_flags(c[x]), AST_FLAG_BLOCKING);
3079 if (res < 0) { /* Simulate a timeout if we were interrupted */
3080 if (errno != EINTR) {
3085 if (!ast_tvzero(whentohangup)) { /* if we have a timeout, check who expired */
3087 for (x = 0; x < n; x++) {
3088 if (!ast_tvzero(*ast_channel_whentohangup(c[x])) && ast_tvcmp(*ast_channel_whentohangup(c[x]), now) <= 0) {
3089 ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3090 if (winner == NULL) {
3096 if (res == 0) { /* no fd ready, reset timeout and done */
3097 *ms = 0; /* XXX use 0 since we may not have an exact timeout. */
3101 * Then check if any channel or fd has a pending event.
3102 * Remember to check channels first and fds last, as they
3103 * must have priority on setting 'winner'
3105 for (x = 0; x < max; x++) {
3106 res = pfds[x].revents;
3110 if (fdmap[x].chan >= 0) { /* this is a channel */
3111 winner = c[fdmap[x].chan]; /* override previous winners */
3112 if (res & POLLPRI) {
3113 ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3115 ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3117 ast_channel_fdno_set(winner, fdmap[x].fdno);
3118 } else { /* this is an fd */
3120 *outfd = pfds[x].fd;
3123 *exception = (res & POLLPRI) ? -1 : 0;
3129 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3138 static struct ast_channel *ast_waitfor_nandfds_simple(struct ast_channel *chan, int *ms)
3140 struct timeval start = { 0 , 0 };
3142 struct epoll_event ev[1];
3143 long diff, rms = *ms;
3144 struct ast_channel *winner = NULL;
3145 struct ast_epoll_data *aed = NULL;
3148 /* See if this channel needs to be masqueraded */
3149 while (ast_channel_masq(chan)) {
3150 ast_do_masquerade(chan);
3153 ast_channel_lock(chan);
3154 /* Figure out their timeout */
3155 if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
3156 if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow())) < 0) {
3157 /* They should already be hungup! */
3158 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
3159 ast_channel_unlock(chan);
3162 /* If this value is smaller then the current one... make it priority */
3168 ast_channel_unlock(chan);
3170 /* Time to make this channel block... */
3171 CHECK_BLOCKING(chan);
3174 start = ast_tvnow();
3177 /* We don't have to add any file descriptors... they are already added, we just have to wait! */
3178 res = epoll_wait(ast_channel_epfd(chan), ev, 1, rms);
3181 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
3183 /* Simulate a timeout if we were interrupted */
3185 if (errno != EINTR) {
3191 /* If this channel has a timeout see if it expired */
3192 if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
3193 if (ast_tvdiff_ms(ast_tvnow(), *ast_channel_whentohangup(chan)) >= 0) {
3194 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
3199 /* No fd ready, reset timeout and be done for now */
3205 /* See what events are pending */
3206 aed = ev[0].data.ptr;
3207 ast_channel_fdno_set(chan, aed->which);
3208 if (ev[0].events & EPOLLPRI) {
3209 ast_set_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3211 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3215 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3224 static struct ast_channel *ast_waitfor_nandfds_complex(struct ast_channel **c, int n, int *ms)
3226 struct timeval start = { 0 , 0 };
3228 struct epoll_event ev[25] = { { 0, } };
3229 struct timeval now = { 0, 0 };
3230 long whentohangup = 0, diff = 0, rms = *ms;
3231 struct ast_channel *winner = NULL;
3233 for (i = 0; i < n; i++) {
3234 while (ast_channel_masq(c[i])) {
3235 ast_do_masquerade(c[i]);
3238 ast_channel_lock(c[i]);
3239 if (!ast_tvzero(*ast_channel_whentohangup(c[i]))) {
3240 if (whentohangup == 0) {
3243 if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(c[i]), now)) < 0) {
3244 ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
3245 ast_channel_unlock(c[i]);
3248 if (!whentohangup || whentohangup > diff) {
3249 whentohangup = diff;
3252 ast_channel_unlock(c[i]);
3253 CHECK_BLOCKING(c[i]);
3259 if (*ms >= 0 && *ms < rms) {
3265 start = ast_tvnow();
3268 res = epoll_wait(ast_channel_epfd(c[0]), ev, 25, rms);
3270 for (i = 0; i < n; i++) {
3271 ast_clear_flag(ast_channel_flags(c[i]), AST_FLAG_BLOCKING);
3275 if (errno != EINTR) {
3283 for (i = 0; i < n; i++) {
3284 if (!ast_tvzero(*ast_channel_whentohangup(c[i])) && ast_tvdiff_ms(now, *ast_channel_whentohangup(c[i])) >= 0) {
3285 ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
3298 for (i = 0; i < res; i++) {
3299 struct ast_epoll_data *aed = ev[i].data.ptr;
3301 if (!ev[i].events || !aed) {
3306 if (ev[i].events & EPOLLPRI) {
3307 ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3309 ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3311 ast_channel_fdno_set(winner, aed->which);
3315 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3324 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3325 int *exception, int *outfd, int *ms)
3327 /* Clear all provided values in one place. */
3335 /* If no epoll file descriptor is available resort to classic nandfds */
3336 if (!n || nfds || ast_channel_epfd(c[0]) == -1) {
3337 return ast_waitfor_nandfds_classic(c, n, fds, nfds, exception, outfd, ms);
3338 } else if (!nfds && n == 1) {
3339 return ast_waitfor_nandfds_simple(c[0], ms);
3341 return ast_waitfor_nandfds_complex(c, n, ms);
3346 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
3348 return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
3351 int ast_waitfor(struct ast_channel *c, int ms)
3353 int oldms = ms; /* -1 if no timeout */
3355 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3356 if ((ms < 0) && (oldms < 0)) {
3362 /* XXX never to be called with ms = -1 */
3363 int ast_waitfordigit(struct ast_channel *c, int ms)
3365 return ast_waitfordigit_full(c, ms, -1, -1);
3368 int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
3371 unsigned int real_rate = rate, max_rate;
3373 ast_channel_lock(c);
3375 if (ast_channel_timingfd(c) == -1) {
3376 ast_channel_unlock(c);
3385 if (rate && rate > (max_rate = ast_timer_get_max_rate(ast_channel_timer(c)))) {
3386 real_rate = max_rate;
3389 ast_debug(1, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
3391 res = ast_timer_set_rate(ast_channel_timer(c), real_rate);
3393 ast_channel_timingfunc_set(c, func);
3394 ast_channel_timingdata_set(c, data);
3396 if (func == NULL && rate == 0 && ast_channel_fdno(c) == AST_TIMING_FD) {
3397 /* Clearing the timing func and setting the rate to 0
3398 * means that we don't want to be reading from the timingfd
3399 * any more. Setting c->fdno to -1 means we won't have any
3400 * errant reads from the timingfd, meaning we won't potentially
3401 * miss any important frames.
3403 ast_channel_fdno_set(c, -1);
3406 ast_channel_unlock(c);
3411 int ast_waitfordigit_full(struct ast_channel *c, int ms, int audiofd, int cmdfd)
3413 /* Stop if we're a zombie or need a soft hangup */
3414 if (ast_test_flag(ast_channel_flags(c), AST_FLAG_ZOMBIE) || ast_check_hangup(c))
3417 /* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
3418 ast_set_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3420 /* Wait for a digit, no more than ms milliseconds total. */
3423 struct ast_channel *rchan;
3427 rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
3429 if (!rchan && outfd < 0 && ms) {
3430 if (errno == 0 || errno == EINTR)
3432 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
3433 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3435 } else if (outfd > -1) {
3436 /* The FD we were watching has something waiting */
3437 ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
3438 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3442 struct ast_frame *f = ast_read(c);
3446 switch (f->frametype) {
3447 case AST_FRAME_DTMF_BEGIN:
3449 case AST_FRAME_DTMF_END:
3450 res = f->subclass.integer;
3452 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3454 case AST_FRAME_CONTROL:
3455 switch (f->subclass.integer) {
3456 case AST_CONTROL_HANGUP:
3458 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3460 case AST_CONTROL_PVT_CAUSE_CODE:
3461 case AST_CONTROL_RINGING:
3462 case AST_CONTROL_ANSWER:
3463 case AST_CONTROL_SRCUPDATE:
3464 case AST_CONTROL_SRCCHANGE:
3465 case AST_CONTROL_CONNECTED_LINE:
3466 case AST_CONTROL_REDIRECTING:
3467 case AST_CONTROL_UPDATE_RTP_PEER:
3468 case AST_CONTROL_HOLD:
3469 case AST_CONTROL_UNHOLD:
3474 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
3478 case AST_FRAME_VOICE:
3479 /* Write audio if appropriate */
3481 if (write(audiofd, f->data.ptr, f->datalen) < 0) {
3482 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3493 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3495 return 0; /* Time is up */
3498 static void send_dtmf_event(struct ast_channel *chan, const char *direction, const char digit, const char *begin, const char *end)
3500 ast_manager_event(chan, EVENT_FLAG_DTMF,
3508 ast_channel_name(chan), ast_channel_uniqueid(chan), digit, direction, begin, end);
3511 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
3513 if (ast_channel_generator(chan) && ast_channel_generator(chan)->generate && ast_channel_generatordata(chan) && !ast_internal_timing_enabled(chan)) {
3514 void *tmp = ast_channel_generatordata(chan);
3515 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = ast_channel_generator(chan)->generate;
3519 if (ast_channel_timingfunc(chan)) {
3520 ast_debug(1, "Generator got voice, switching to phase locked mode\n");
3521 ast_settimeout(chan, 0, NULL, NULL);
3524 ast_channel_generatordata_set(chan, NULL); /* reset, to let writes go through */
3526 if (ast_format_cmp(&f->subclass.format, ast_channel_writeformat(chan)) == AST_FORMAT_CMP_NOT_EQUAL) {
3528 factor = ((float) ast_format_rate(ast_channel_writeformat(chan))) / ((float) ast_format_rate(&f->subclass.format));
3529 samples = (int) ( ((float) f->samples) * factor );
3531 samples = f->samples;
3534 /* This unlock is here based on two assumptions that hold true at this point in the
3535 * code. 1) this function is only called from within __ast_read() and 2) all generators
3536 * call ast_write() in their generate callback.
3538 * The reason this is added is so that when ast_write is called, the lock that occurs
3539 * there will not recursively lock the channel. Doing this will cause intended deadlock
3540 * avoidance not to work in deeper functions
3542 ast_channel_unlock(chan);
3543 res = generate(chan, tmp, f->datalen, samples);
3544 ast_channel_lock(chan);
3545 ast_channel_generatordata_set(chan, tmp);
3547 ast_debug(1, "Auto-deactivating generator\n");
3548 ast_deactivate_generator(chan);
3551 } else if (f->frametype == AST_FRAME_CNG) {
3552 if (ast_channel_generator(chan) && !ast_channel_timingfunc(chan) && (ast_channel_timingfd(chan) > -1)) {
3553 ast_debug(1, "Generator got CNG, switching to timed mode\n");
3554 ast_settimeout(chan, 50, generator_force, chan);
3559 static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
3561 struct ast_frame *fr = ast_channel_dtmff(chan);
3563 fr->frametype = AST_FRAME_DTMF_END;
3564 fr->subclass.integer = f->subclass.integer;
3567 /* The only time this function will be called is for a frame that just came
3568 * out of the channel driver. So, we want to stick it on the tail of the
3571 ast_queue_frame(chan, fr);
3575 * \brief Determine whether or not we should ignore DTMF in the readq
3577 static inline int should_skip_dtmf(struct ast_channel *chan)
3579 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_EMULATE_DTMF)) {
3580 /* We're in the middle of emulating a digit, or DTMF has been
3581 * explicitly deferred. Skip this digit, then. */
3585 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3586 ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
3587 /* We're not in the middle of a digit, but it hasn't been long enough
3588 * since the last digit, so we'll have to skip DTMF for now. */
3596 * \brief calculates the number of samples to jump forward with in a monitor stream.
3598 * \note When using ast_seekstream() with the read and write streams of a monitor,
3599 * the number of samples to seek forward must be of the same sample rate as the stream
3600 * or else the jump will not be calculated correctly.
3602 * \retval number of samples to seek forward after rate conversion.
3604 static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
3606 int diff = sample_rate - seek_rate;
3609 samples = samples / (float) (sample_rate / seek_rate);
3610 } else if (diff < 0) {
3611 samples = samples * (float) (seek_rate / sample_rate);
3617 static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
3619 struct ast_frame *f = NULL; /* the return value */
3623 /* this function is very long so make sure there is only one return
3624 * point at the end (there are only two exceptions to this).
3627 if (ast_channel_masq(chan)) {
3628 ast_do_masquerade(chan);
3629 return &ast_null_frame;
3632 /* if here, no masq has happened, lock the channel and proceed */
3633 ast_channel_lock(chan);
3635 /* Stop if we're a zombie or need a soft hangup */
3636 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
3637 if (ast_channel_generator(chan))
3638 ast_deactivate_generator(chan);
3641 * It is possible for chan->_softhangup to be set and there
3642 * still be control frames that need to be read. Instead of
3643 * just going to 'done' in the case of ast_check_hangup(), we
3644 * need to queue the end-of-Q frame so that it can mark the end
3645 * of the read queue. If there are frames to be read,
3646 * ast_queue_control() will be called repeatedly, but will only
3647 * queue the first end-of-Q frame.
3649 if (ast_channel_softhangup_internal_flag(chan)) {
3650 ast_queue_control(chan, AST_CONTROL_END_OF_Q);
3657 * The ast_waitfor() code records which of the channel's file
3658 * descriptors reported that data is available. In theory,
3659 * ast_read() should only be called after ast_waitfor() reports
3660 * that a channel has data available for reading. However,
3661 * there still may be some edge cases throughout the code where
3662 * ast_read() is called improperly. This can potentially cause
3663 * problems, so if this is a developer build, make a lot of
3664 * noise if this happens so that it can be addressed.
3666 * One of the potential problems is blocking on a dead channel.
3668 if (ast_channel_fdno(chan) == -1) {
3670 "ast_read() on chan '%s' called with no recorded file descriptor.\n",
3671 ast_channel_name(chan));
3676 prestate = ast_channel_state(chan);
3678 if (ast_channel_timingfd(chan) > -1 && ast_channel_fdno(chan) == AST_TIMING_FD) {
3679 enum ast_timer_event res;
3681 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3683 res = ast_timer_get_event(ast_channel_timer(chan));
3686 case AST_TIMING_EVENT_EXPIRED:
3687 ast_timer_ack(ast_channel_timer(chan), 1);
3689 if (ast_channel_timingfunc(chan)) {
3690 /* save a copy of func/data before unlocking the channel */
3691 ast_timing_func_t func = ast_channel_timingfunc(chan);
3692 void *data = ast_channel_timingdata(chan);
3693 ast_channel_fdno_set(chan, -1);
3694 ast_channel_unlock(chan);
3697 ast_timer_set_rate(ast_channel_timer(chan), 0);
3698 ast_channel_fdno_set(chan, -1);
3699 ast_channel_unlock(chan);
3702 /* cannot 'goto done' because the channel is already unlocked */
3703 return &ast_null_frame;
3705 case AST_TIMING_EVENT_CONTINUOUS:
3706 if (AST_LIST_EMPTY(ast_channel_readq(chan)) ||
3707 !AST_LIST_NEXT(AST_LIST_FIRST(ast_channel_readq(chan)), frame_list)) {
3708 ast_timer_disable_continuous(ast_channel_timer(chan));
3713 } else if (ast_channel_fd_isset(chan, AST_GENERATOR_FD) && ast_channel_fdno(chan) == AST_GENERATOR_FD) {
3714 /* if the AST_GENERATOR_FD is set, call the generator with args
3715 * set to -1 so it can do whatever it needs to.
3717 void *tmp = ast_channel_generatordata(chan);
3718 ast_channel_generatordata_set(chan, NULL); /* reset to let ast_write get through */
3719 ast_channel_generator(chan)->generate(chan, tmp, -1, -1);
3720 ast_channel_generatordata_set(chan, tmp);
3721 f = &ast_null_frame;
3722 ast_channel_fdno_set(chan, -1);
3724 } else if (ast_channel_fd_isset(chan, AST_JITTERBUFFER_FD) && ast_channel_fdno(chan) == AST_JITTERBUFFER_FD) {
3725 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3728 /* Read and ignore anything on the alertpipe, but read only
3729 one sizeof(blah) per frame that we send from it */
3730 if (ast_channel_internal_alert_read(chan) == AST_ALERT_READ_FATAL) {
3731 f = &ast_null_frame;
3735 /* Check for pending read queue */
3736 if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
3737 int skip_dtmf = should_skip_dtmf(chan);
3739 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), f, frame_list) {
3740 /* We have to be picky about which frame we pull off of the readq because
3741 * there are cases where we want to leave DTMF frames on the queue until
3742 * some later time. */
3744 if ( (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) && skip_dtmf) {
3748 AST_LIST_REMOVE_CURRENT(frame_list);
3751 AST_LIST_TRAVERSE_SAFE_END;
3754 /* There were no acceptable frames on the readq. */
3755 f = &ast_null_frame;
3756 ast_channel_alert_write(chan);
3759 /* Interpret hangup and end-of-Q frames to return NULL */
3760 /* XXX why not the same for frames from the channel ? */
3761 if (f->frametype == AST_FRAME_CONTROL) {
3762 switch (f->subclass.integer) {
3763 case AST_CONTROL_HANGUP:
3764 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
3765 cause = f->data.uint32;
3767 case AST_CONTROL_END_OF_Q:
3776 ast_channel_blocker_set(chan, pthread_self());
3777 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION)) {
3778 if (ast_channel_tech(chan)->exception)
3779 f = ast_channel_tech(chan)->exception(chan);
3781 ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", ast_channel_name(chan));
3782 f = &ast_null_frame;
3784 /* Clear the exception flag */
3785 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3786 } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read)
3787 f = ast_channel_tech(chan)->read(chan);
3789 ast_log(LOG_WARNING, "No read routine on channel %s\n", ast_channel_name(chan));
3792 /* Perform the framehook read event here. After the frame enters the framehook list
3793 * there is no telling what will happen, <insert mad scientist laugh here>!!! */
3794 f = ast_framehook_list_read_event(ast_channel_framehooks(chan), f);
3797 * Reset the recorded file descriptor that triggered this read so that we can
3798 * easily detect when ast_read() is called without properly using ast_waitfor().
3800 ast_channel_fdno_set(chan, -1);
3803 struct ast_frame *readq_tail = AST_LIST_LAST(ast_channel_readq(chan));
3804 struct ast_control_read_action_payload *read_action_payload;
3805 struct ast_party_connected_line connected;
3807 /* if the channel driver returned more than one frame, stuff the excess
3808 into the readq for the next ast_read call
3810 if (AST_LIST_NEXT(f, frame_list)) {
3811 ast_queue_frame(chan, AST_LIST_NEXT(f, frame_list));
3812 ast_frfree(AST_LIST_NEXT(f, frame_list));
3813 AST_LIST_NEXT(f, frame_list) = NULL;
3816 switch (f->frametype) {
3817 case AST_FRAME_CONTROL:
3818 if (f->subclass.integer == AST_CONTROL_ANSWER) {
3819 if (!ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING)) {
3820 ast_debug(1, "Ignoring answer on an inbound call!\n");
3822 f = &ast_null_frame;
3823 } else if (prestate == AST_STATE_UP && ast_bridged_channel(chan)) {
3824 ast_debug(1, "Dropping duplicate answer!\n");
3826 f = &ast_null_frame;
3828 /* Answer the CDR */
3829 ast_setstate(chan, AST_STATE_UP);
3830 /* removed a call to ast_cdr_answer(chan->cdr) from here. */
3831 ast_cel_report_event(chan, AST_CEL_ANSWER, NULL, NULL, NULL);
3833 } else if (f->subclass.integer == AST_CONTROL_READ_ACTION) {
3834 read_action_payload = f->data.ptr;