2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Channel Management
23 * \author Mark Spencer <markster@digium.com>
27 <support_level>core</support_level>
32 #include "asterisk/_private.h"
38 #include "asterisk/paths.h" /* use ast_config_AST_SYSTEM_NAME */
40 #include "asterisk/pbx.h"
41 #include "asterisk/frame.h"
42 #include "asterisk/mod_format.h"
43 #include "asterisk/sched.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/musiconhold.h"
46 #include "asterisk/say.h"
47 #include "asterisk/file.h"
48 #include "asterisk/cli.h"
49 #include "asterisk/translate.h"
50 #include "asterisk/manager.h"
51 #include "asterisk/chanvars.h"
52 #include "asterisk/linkedlists.h"
53 #include "asterisk/indications.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"
71 #include "asterisk/features.h"
72 #include "asterisk/bridge.h"
73 #include "asterisk/test.h"
74 #include "asterisk/stasis_channels.h"
75 #include "asterisk/max_forwards.h"
76 #include "asterisk/stream.h"
81 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
84 #endif /* defined(HAVE_PRI) */
85 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
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 */
95 unsigned long global_fin, global_fout;
97 AST_THREADSTORAGE(state2str_threadbuf);
98 #define STATE2STR_BUFSIZE 32
100 /*! Default amount of time to use when emulating a digit as a begin and end
102 #define AST_DEFAULT_EMULATE_DTMF_DURATION 100
104 #define DEFAULT_AMA_FLAGS AST_AMA_DOCUMENTATION
106 /*! Minimum amount of time between the end of the last digit and the beginning
107 * of a new one - 45ms */
108 #define AST_MIN_DTMF_GAP 45
110 /*! \brief List of channel drivers */
112 const struct ast_channel_tech *tech;
113 AST_LIST_ENTRY(chanlist) list;
116 /*! \brief the list of registered channel types */
117 static AST_RWLIST_HEAD_STATIC(backends, chanlist);
120 #define NUM_CHANNEL_BUCKETS 61
122 #define NUM_CHANNEL_BUCKETS 1567
125 /*! \brief All active channels on the system */
126 static struct ao2_container *channels;
128 /*! \brief map AST_CAUSE's to readable string representations
138 static const struct causes_map causes[] = {
139 { AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
140 { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
141 { AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
142 { AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
143 { AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
144 { AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
145 { AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
146 { AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
147 { AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
148 { AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
149 { AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
150 { AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
151 { AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
152 { AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
153 { AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
154 { AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
155 { AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
156 { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
157 { AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
158 { AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
159 { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
160 { AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
161 { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
162 { AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
163 { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
164 { AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
165 { AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
166 { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
167 { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
168 { AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
169 { AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
170 { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
171 { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
172 { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
173 { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
174 { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
175 { AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
176 { AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
177 { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
178 { AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
179 { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
180 { AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
181 { AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
182 { AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
183 { AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
184 { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
185 { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
186 { AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
187 { AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
190 struct ast_variable *ast_channeltype_list(void)
193 struct ast_variable *var = NULL, *prev = NULL;
195 AST_RWLIST_RDLOCK(&backends);
196 AST_RWLIST_TRAVERSE(&backends, cl, list) {
198 if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
201 var = ast_variable_new(cl->tech->type, cl->tech->description, "");
205 AST_RWLIST_UNLOCK(&backends);
210 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
211 static const char *party_number_ton2str(int ton)
213 #if defined(HAVE_PRI)
214 switch ((ton >> 4) & 0x07) {
215 case PRI_TON_INTERNATIONAL:
216 return "International";
217 case PRI_TON_NATIONAL:
219 case PRI_TON_NET_SPECIFIC:
220 return "Network Specific";
221 case PRI_TON_SUBSCRIBER:
223 case PRI_TON_ABBREVIATED:
224 return "Abbreviated";
225 case PRI_TON_RESERVED:
227 case PRI_TON_UNKNOWN:
231 #endif /* defined(HAVE_PRI) */
234 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
236 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
237 static const char *party_number_plan2str(int plan)
239 #if defined(HAVE_PRI)
240 switch (plan & 0x0F) {
242 case PRI_NPI_UNKNOWN:
244 case PRI_NPI_E163_E164:
245 return "Public (E.163/E.164)";
247 return "Data (X.121)";
249 return "Telex (F.69)";
250 case PRI_NPI_NATIONAL:
251 return "National Standard";
252 case PRI_NPI_PRIVATE:
254 case PRI_NPI_RESERVED:
257 #endif /* defined(HAVE_PRI) */
260 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
262 /*! \brief Show channel types - CLI command */
263 static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
265 #define FORMAT "%-15.15s %-40.40s %-13.13s %-13.13s %-13.13s %-13.13s\n"
271 e->command = "core show channeltypes";
273 "Usage: core show channeltypes\n"
274 " Lists available channel types registered in your\n"
275 " Asterisk server.\n";
282 return CLI_SHOWUSAGE;
284 ast_cli(a->fd, FORMAT, "Type", "Description", "Devicestate", "Presencestate", "Indications", "Transfer");
285 ast_cli(a->fd, FORMAT, "-------------", "-------------", "-------------", "-------------", "-------------", "-------------");
287 AST_RWLIST_RDLOCK(&backends);
288 AST_RWLIST_TRAVERSE(&backends, cl, list) {
289 ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
290 (cl->tech->devicestate) ? "yes" : "no",
291 (cl->tech->presencestate) ? "yes" : "no",
292 (cl->tech->indicate) ? "yes" : "no",
293 (cl->tech->transfer) ? "yes" : "no");
296 AST_RWLIST_UNLOCK(&backends);
298 ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
305 static char *complete_channeltypes(struct ast_cli_args *a)
315 wordlen = strlen(a->word);
317 AST_RWLIST_RDLOCK(&backends);
318 AST_RWLIST_TRAVERSE(&backends, cl, list) {
319 if (!strncasecmp(a->word, cl->tech->type, wordlen) && ++which > a->n) {
320 ret = ast_strdup(cl->tech->type);
324 AST_RWLIST_UNLOCK(&backends);
329 /*! \brief Show details about a channel driver - CLI command */
330 static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
332 struct chanlist *cl = NULL;
333 struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
337 e->command = "core show channeltype";
339 "Usage: core show channeltype <name>\n"
340 " Show details about the specified channel type, <name>.\n";
343 return complete_channeltypes(a);
347 return CLI_SHOWUSAGE;
349 AST_RWLIST_RDLOCK(&backends);
351 AST_RWLIST_TRAVERSE(&backends, cl, list) {
352 if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
358 ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
359 AST_RWLIST_UNLOCK(&backends);
364 "-- Info about channel driver: %s --\n"
365 " Device State: %s\n"
366 "Presence State: %s\n"
369 " Capabilities: %s\n"
373 " Image Support: %s\n"
374 " Text Support: %s\n",
376 (cl->tech->devicestate) ? "yes" : "no",
377 (cl->tech->presencestate) ? "yes" : "no",
378 (cl->tech->indicate) ? "yes" : "no",
379 (cl->tech->transfer) ? "yes" : "no",
380 ast_format_cap_get_names(cl->tech->capabilities, &codec_buf),
381 (cl->tech->send_digit_begin) ? "yes" : "no",
382 (cl->tech->send_digit_end) ? "yes" : "no",
383 (cl->tech->send_html) ? "yes" : "no",
384 (cl->tech->send_image) ? "yes" : "no",
385 (cl->tech->send_text) ? "yes" : "no"
389 AST_RWLIST_UNLOCK(&backends);
394 static struct ast_cli_entry cli_channel[] = {
395 AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
396 AST_CLI_DEFINE(handle_cli_core_show_channeltype, "Give more details on that channel type")
399 static struct ast_frame *kill_read(struct ast_channel *chan)
401 /* Hangup channel. */
405 static struct ast_frame *kill_exception(struct ast_channel *chan)
407 /* Hangup channel. */
411 static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
413 /* Hangup channel. */
417 static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
419 /* No problem fixing up the channel. */
423 static int kill_hangup(struct ast_channel *chan)
425 ast_channel_tech_pvt_set(chan, NULL);
430 * \brief Kill the channel channel driver technology descriptor.
433 * The purpose of this channel technology is to encourage the
434 * channel to hangup as quickly as possible.
436 * \note Used by DTMF atxfer and zombie channels.
438 const struct ast_channel_tech ast_kill_tech = {
440 .description = "Kill channel (should not see this)",
442 .exception = kill_exception,
445 .hangup = kill_hangup,
448 /*! \brief Checks to see if a channel is needing hang up */
449 int ast_check_hangup(struct ast_channel *chan)
451 if (ast_channel_softhangup_internal_flag(chan)) /* yes if soft hangup flag set */
453 if (ast_tvzero(*ast_channel_whentohangup(chan))) /* no if no hangup scheduled */
455 if (ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()) > 0) /* no if hangup time has not come yet. */
457 ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()));
458 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(chan));
459 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT); /* record event */
463 int ast_check_hangup_locked(struct ast_channel *chan)
466 ast_channel_lock(chan);
467 res = ast_check_hangup(chan);
468 ast_channel_unlock(chan);
472 void ast_channel_softhangup_withcause_locked(struct ast_channel *chan, int causecode)
474 ast_channel_lock(chan);
477 ast_debug(1, "Setting hangupcause of channel %s to %d (is %d now)\n",
478 ast_channel_name(chan), causecode, ast_channel_hangupcause(chan));
480 ast_channel_hangupcause_set(chan, causecode);
483 ast_softhangup_nolock(chan, AST_SOFTHANGUP_EXPLICIT);
485 ast_channel_unlock(chan);
488 static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
490 struct ast_channel *chan = obj;
492 ast_softhangup(chan, AST_SOFTHANGUP_SHUTDOWN);
497 void ast_softhangup_all(void)
499 ao2_callback(channels, OBJ_NODATA | OBJ_MULTIPLE, ast_channel_softhangup_cb, NULL);
502 /*! \brief returns number of active/allocated channels */
503 int ast_active_channels(void)
505 return channels ? ao2_container_count(channels) : 0;
508 int ast_undestroyed_channels(void)
510 return ast_atomic_fetchadd_int(&chancount, 0);
513 /*! \brief Set when to hangup channel */
514 void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
516 if (ast_tvzero(offset)) {
517 ast_channel_whentohangup_set(chan, &offset);
519 struct timeval tv = ast_tvadd(offset, ast_tvnow());
520 ast_channel_whentohangup_set(chan, &tv);
522 ast_queue_frame(chan, &ast_null_frame);
526 void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
528 struct timeval when = { offset, };
529 ast_channel_setwhentohangup_tv(chan, when);
532 /*! \brief Compare a offset with when to hangup channel */
533 int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
535 struct timeval whentohangup;
537 if (ast_tvzero(*ast_channel_whentohangup(chan)))
538 return ast_tvzero(offset) ? 0 : -1;
540 if (ast_tvzero(offset))
543 whentohangup = ast_tvadd(offset, ast_tvnow());
545 return ast_tvdiff_ms(whentohangup, *ast_channel_whentohangup(chan));
548 int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
550 struct timeval when = { offset, };
551 return ast_channel_cmpwhentohangup_tv(chan, when);
554 /*! \brief Register a new telephony channel in Asterisk */
555 int ast_channel_register(const struct ast_channel_tech *tech)
557 struct chanlist *chan;
559 AST_RWLIST_WRLOCK(&backends);
561 AST_RWLIST_TRAVERSE(&backends, chan, list) {
562 if (!strcasecmp(tech->type, chan->tech->type)) {
563 ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
564 AST_RWLIST_UNLOCK(&backends);
569 if (!(chan = ast_calloc(1, sizeof(*chan)))) {
570 AST_RWLIST_UNLOCK(&backends);
574 AST_RWLIST_INSERT_HEAD(&backends, chan, list);
576 ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
578 ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
580 AST_RWLIST_UNLOCK(&backends);
585 /*! \brief Unregister channel driver */
586 void ast_channel_unregister(const struct ast_channel_tech *tech)
588 struct chanlist *chan;
590 ast_debug(1, "Unregistering channel type '%s'\n", tech->type);
592 AST_RWLIST_WRLOCK(&backends);
594 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&backends, chan, list) {
595 if (chan->tech == tech) {
596 AST_LIST_REMOVE_CURRENT(list);
598 ast_verb(2, "Unregistered channel type '%s'\n", tech->type);
602 AST_LIST_TRAVERSE_SAFE_END;
604 AST_RWLIST_UNLOCK(&backends);
607 /*! \brief Get handle to channel driver based on name */
608 const struct ast_channel_tech *ast_get_channel_tech(const char *name)
610 struct chanlist *chanls;
611 const struct ast_channel_tech *ret = NULL;
613 AST_RWLIST_RDLOCK(&backends);
615 AST_RWLIST_TRAVERSE(&backends, chanls, list) {
616 if (!strcasecmp(name, chanls->tech->type)) {
622 AST_RWLIST_UNLOCK(&backends);
627 /*! \brief Gives the string form of a given hangup cause */
628 const char *ast_cause2str(int cause)
632 for (x = 0; x < ARRAY_LEN(causes); x++) {
633 if (causes[x].cause == cause)
634 return causes[x].desc;
640 /*! \brief Convert a symbolic hangup cause to number */
641 int ast_str2cause(const char *name)
645 for (x = 0; x < ARRAY_LEN(causes); x++)
646 if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
647 return causes[x].cause;
652 static struct stasis_message *create_channel_snapshot_message(struct ast_channel *channel)
654 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
656 if (!ast_channel_snapshot_type()) {
660 ast_channel_lock(channel);
661 snapshot = ast_channel_snapshot_create(channel);
662 ast_channel_unlock(channel);
667 return stasis_message_create(ast_channel_snapshot_type(), snapshot);
670 static void publish_cache_clear(struct ast_channel *chan)
672 RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
673 RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
675 clear_msg = create_channel_snapshot_message(chan);
680 message = stasis_cache_clear_create(clear_msg);
681 stasis_publish(ast_channel_topic(chan), message);
684 /*! \brief Gives the string form of a given channel state.
686 * \note This function is not reentrant.
690 const char *ast_state2str(enum ast_channel_state state)
697 case AST_STATE_RESERVED:
699 case AST_STATE_OFFHOOK:
701 case AST_STATE_DIALING:
705 case AST_STATE_RINGING:
711 case AST_STATE_DIALING_OFFHOOK:
712 return "Dialing Offhook";
713 case AST_STATE_PRERING:
718 if (!(buf = ast_threadstorage_get(&state2str_threadbuf, STATE2STR_BUFSIZE)))
720 snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%u)", state);
725 /*! \brief Gives the string form of a given transfer capability */
726 char *ast_transfercapability2str(int transfercapability)
728 switch (transfercapability) {
729 case AST_TRANS_CAP_SPEECH:
731 case AST_TRANS_CAP_DIGITAL:
733 case AST_TRANS_CAP_RESTRICTED_DIGITAL:
734 return "RESTRICTED_DIGITAL";
735 case AST_TRANS_CAP_3_1K_AUDIO:
737 case AST_TRANS_CAP_DIGITAL_W_TONES:
738 return "DIGITAL_W_TONES";
739 case AST_TRANS_CAP_VIDEO:
746 /*! \brief Channel technology used to extract a channel from a running application. The
747 * channel created with this technology will be immediately hung up - most external
748 * applications won't ever want to see this.
750 static const struct ast_channel_tech surrogate_tech = {
752 .description = "Surrogate channel used to pull channel from an application",
753 .properties = AST_CHAN_TP_INTERNAL,
756 static const struct ast_channel_tech null_tech = {
758 .description = "Null channel (should not see this)",
761 static void ast_channel_destructor(void *obj);
762 static void ast_dummy_channel_destructor(void *obj);
763 static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags);
765 static int does_id_conflict(const char *uniqueid)
767 struct ast_channel *conflict;
770 if (ast_strlen_zero(uniqueid)) {
774 conflict = ast_channel_callback(ast_channel_by_uniqueid_cb, (char *) uniqueid, &length, OBJ_NOLOCK);
776 ast_log(LOG_ERROR, "Channel Unique ID '%s' already in use by channel %s(%p)\n",
777 uniqueid, ast_channel_name(conflict), conflict);
778 ast_channel_unref(conflict);
785 /*! \brief Create a new channel structure */
786 static struct ast_channel * attribute_malloc __attribute__((format(printf, 15, 0)))
787 __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
788 const char *acctcode, const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
789 const struct ast_channel *requestor, enum ama_flags amaflag, struct ast_endpoint *endpoint,
790 const char *file, int line,
791 const char *function, const char *name_fmt, va_list ap)
793 struct ast_channel *tmp;
794 struct varshead *headp;
795 char *tech = "", *tech2 = NULL;
796 struct ast_format_cap *nativeformats;
797 struct ast_sched_context *schedctx;
798 struct ast_timer *timer;
800 const struct ast_channel_tech *channel_tech;
801 struct ast_stream_topology *topology;
803 /* If shutting down, don't allocate any new channels */
804 if (ast_shutting_down()) {
805 ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
809 tmp = __ast_channel_internal_alloc(ast_channel_destructor, assignedids, requestor,
810 file, line, function);
812 /* Channel structure allocation failure. */
816 ast_channel_stage_snapshot(tmp);
818 if (!(nativeformats = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
820 * Aborting the channel creation. We do not need to complete staging
821 * the channel snapshot because the channel has not been finalized or
822 * linked into the channels container yet. Nobody else knows about
823 * this channel nor will anybody ever know about it.
825 return ast_channel_unref(tmp);
827 ast_format_cap_append(nativeformats, ast_format_none, 0);
828 ast_channel_nativeformats_set(tmp, nativeformats);
829 ao2_ref(nativeformats, -1);
831 ast_channel_set_rawwriteformat(tmp, ast_format_none);
832 ast_channel_set_rawreadformat(tmp, ast_format_none);
833 ast_channel_set_writeformat(tmp, ast_format_none);
834 ast_channel_set_readformat(tmp, ast_format_none);
837 * Init file descriptors to unopened state so
838 * the destructor can know not to close them.
840 ast_channel_timingfd_set(tmp, -1);
841 ast_channel_internal_alertpipe_clear(tmp);
842 ast_channel_internal_fd_clear_all(tmp);
844 if (!(schedctx = ast_sched_context_create())) {
845 ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
846 /* See earlier channel creation abort comment above. */
847 return ast_channel_unref(tmp);
849 ast_channel_sched_set(tmp, schedctx);
851 ast_party_dialed_init(ast_channel_dialed(tmp));
852 ast_party_caller_init(ast_channel_caller(tmp));
853 ast_party_connected_line_init(ast_channel_connected(tmp));
854 ast_party_connected_line_init(ast_channel_connected_indicated(tmp));
855 ast_party_redirecting_init(ast_channel_redirecting(tmp));
858 ast_channel_caller(tmp)->id.name.valid = 1;
859 ast_channel_caller(tmp)->id.name.str = ast_strdup(cid_name);
860 if (!ast_channel_caller(tmp)->id.name.str) {
861 /* See earlier channel creation abort comment above. */
862 return ast_channel_unref(tmp);
866 ast_channel_caller(tmp)->id.number.valid = 1;
867 ast_channel_caller(tmp)->id.number.str = ast_strdup(cid_num);
868 if (!ast_channel_caller(tmp)->id.number.str) {
869 /* See earlier channel creation abort comment above. */
870 return ast_channel_unref(tmp);
874 if ((timer = ast_timer_open())) {
875 ast_channel_timer_set(tmp, timer);
876 if (strcmp(ast_timer_get_name(ast_channel_timer(tmp)), "timerfd")) {
879 ast_channel_timingfd_set(tmp, ast_timer_fd(ast_channel_timer(tmp)));
882 if (needqueue && ast_channel_internal_alertpipe_init(tmp)) {
883 /* See earlier channel creation abort comment above. */
884 return ast_channel_unref(tmp);
887 if (!(topology = ast_stream_topology_alloc())) {
888 return ast_channel_unref(tmp);
890 ast_channel_internal_set_stream_topology(tmp, topology);
892 /* Always watch the alertpipe */
893 ast_channel_set_fd(tmp, AST_ALERT_FD, ast_channel_internal_alert_readfd(tmp));
894 /* And timing pipe */
895 ast_channel_set_fd(tmp, AST_TIMING_FD, ast_channel_timingfd(tmp));
898 ast_channel_state_set(tmp, state);
899 ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
901 ast_channel_streamid_set(tmp, -1);
902 ast_channel_vstreamid_set(tmp, -1);
904 ast_channel_fin_set(tmp, global_fin);
905 ast_channel_fout_set(tmp, global_fout);
908 ast_channel_creationtime_set(tmp, &now);
910 ast_channel_internal_setup_topics(tmp);
912 if (!ast_strlen_zero(name_fmt)) {
913 char *slash, *slash2;
914 /* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call.
915 * And they all use slightly different formats for their name string.
916 * This means, to set the name here, we have to accept variable args, and call the string_field_build from here.
917 * This means, that the stringfields must have a routine that takes the va_lists directly, and
918 * uses them to build the string, instead of forming the va_lists internally from the vararg ... list.
919 * This new function was written so this can be accomplished.
921 ast_channel_name_build_va(tmp, name_fmt, ap);
922 tech = ast_strdupa(ast_channel_name(tmp));
923 if ((slash = strchr(tech, '/'))) {
924 if ((slash2 = strchr(slash + 1, '/'))) {
932 * Start the string with '-' so it becomes an empty string
935 ast_channel_name_set(tmp, "-**Unknown**");
938 if (amaflag != AST_AMA_NONE) {
939 ast_channel_amaflags_set(tmp, amaflag);
941 ast_channel_amaflags_set(tmp, DEFAULT_AMA_FLAGS);
944 if (!ast_strlen_zero(acctcode)) {
945 ast_channel_accountcode_set(tmp, acctcode);
947 ast_channel_language_set(tmp, ast_defaultlanguage);
949 ast_channel_context_set(tmp, S_OR(context, "default"));
950 ast_channel_exten_set(tmp, S_OR(exten, "s"));
951 ast_channel_priority_set(tmp, 1);
953 headp = ast_channel_varshead(tmp);
954 AST_LIST_HEAD_INIT_NOLOCK(headp);
956 ast_pbx_hangup_handler_init(tmp);
957 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
958 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_autochans(tmp));
960 channel_tech = ast_get_channel_tech(tech);
961 if (!channel_tech && !ast_strlen_zero(tech2)) {
962 channel_tech = ast_get_channel_tech(tech2);
965 ast_channel_tech_set(tmp, channel_tech);
967 ast_channel_tech_set(tmp, &null_tech);
970 /* You might scream "locking inversion" at seeing this but it is actually perfectly fine.
971 * Since the channel was just created nothing can know about it yet or even acquire it.
973 ast_channel_lock(tmp);
977 if (assignedids && (does_id_conflict(assignedids->uniqueid) || does_id_conflict(assignedids->uniqueid2))) {
978 ast_channel_internal_errno_set(AST_CHANNEL_ERROR_ID_EXISTS);
979 ao2_unlock(channels);
980 ast_channel_unlock(tmp);
981 /* See earlier channel creation abort comment above. */
982 return ast_channel_unref(tmp);
985 /* Finalize and link into the channels container. */
986 ast_channel_internal_finalize(tmp);
987 ast_atomic_fetchadd_int(&chancount, +1);
988 ao2_link_flags(channels, tmp, OBJ_NOLOCK);
990 ao2_unlock(channels);
993 ast_endpoint_add_channel(endpoint, tmp);
997 * And now, since the channel structure is built, and has its name, let
998 * the world know of its existance
1000 ast_channel_stage_snapshot_done(tmp);
1004 struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
1005 const char *cid_name, const char *acctcode,
1006 const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
1007 const struct ast_channel *requestor, enum ama_flags amaflag,
1008 struct ast_endpoint *endpoint,
1009 const char *file, int line, const char *function,
1010 const char *name_fmt, ...)
1013 struct ast_channel *result;
1015 va_start(ap, name_fmt);
1016 result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
1017 assignedids, requestor, amaflag, endpoint, file, line, function, name_fmt, ap);
1023 /* only do the minimum amount of work needed here to make a channel
1024 * structure that can be used to expand channel vars */
1025 struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
1027 struct ast_channel *tmp;
1028 struct varshead *headp;
1030 tmp = __ast_channel_internal_alloc(ast_dummy_channel_destructor, NULL, NULL,
1031 file, line, function);
1033 /* Dummy channel structure allocation failure. */
1037 ast_pbx_hangup_handler_init(tmp);
1038 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
1041 * Init file descriptors to unopened state just in case
1042 * autoservice is called on the channel or something tries to
1043 * read a frame from it.
1045 ast_channel_timingfd_set(tmp, -1);
1046 ast_channel_internal_alertpipe_clear(tmp);
1047 ast_channel_internal_fd_clear_all(tmp);
1049 ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
1051 ast_channel_internal_setup_topics(tmp);
1053 headp = ast_channel_varshead(tmp);
1054 AST_LIST_HEAD_INIT_NOLOCK(headp);
1059 static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
1061 struct ast_frame *f;
1062 struct ast_frame *cur;
1063 unsigned int new_frames = 0;
1064 unsigned int new_voice_frames = 0;
1065 unsigned int queued_frames = 0;
1066 unsigned int queued_voice_frames = 0;
1067 AST_LIST_HEAD_NOLOCK(,ast_frame) frames;
1069 ast_channel_lock(chan);
1072 * Check the last frame on the queue if we are queuing the new
1075 cur = AST_LIST_LAST(ast_channel_readq(chan));
1076 if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
1077 switch (cur->subclass.integer) {
1078 case AST_CONTROL_END_OF_Q:
1079 if (fin->frametype == AST_FRAME_CONTROL
1080 && fin->subclass.integer == AST_CONTROL_HANGUP) {
1082 * Destroy the end-of-Q marker frame so we can queue the hangup
1083 * frame in its place.
1085 AST_LIST_REMOVE(ast_channel_readq(chan), cur, frame_list);
1089 * This has degenerated to a normal queue append anyway. Since
1090 * we just destroyed the last frame in the queue we must make
1091 * sure that "after" is NULL or bad things will happen.
1097 case AST_CONTROL_HANGUP:
1098 /* Don't queue anything. */
1099 ast_channel_unlock(chan);
1106 /* Build copies of all the new frames and count them */
1107 AST_LIST_HEAD_INIT_NOLOCK(&frames);
1108 for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
1109 if (!(f = ast_frdup(cur))) {
1110 if (AST_LIST_FIRST(&frames)) {
1111 ast_frfree(AST_LIST_FIRST(&frames));
1113 ast_channel_unlock(chan);
1117 AST_LIST_INSERT_TAIL(&frames, f, frame_list);
1119 if (f->frametype == AST_FRAME_VOICE) {
1124 /* Count how many frames exist on the queue */
1125 AST_LIST_TRAVERSE(ast_channel_readq(chan), cur, frame_list) {
1127 if (cur->frametype == AST_FRAME_VOICE) {
1128 queued_voice_frames++;
1132 if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
1134 ast_log(LOG_WARNING, "Exceptionally long %squeue length queuing to %s\n", queued_frames + new_frames > 128 ? "" : "voice ", ast_channel_name(chan));
1135 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), cur, frame_list) {
1136 /* Save the most recent frame */
1137 if (!AST_LIST_NEXT(cur, frame_list)) {
1139 } else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
1143 AST_LIST_REMOVE_CURRENT(frame_list);
1146 /* Read from the alert pipe for each flushed frame. */
1147 ast_channel_internal_alert_read(chan);
1150 AST_LIST_TRAVERSE_SAFE_END;
1154 AST_LIST_INSERT_LIST_AFTER(ast_channel_readq(chan), &frames, after, frame_list);
1157 AST_LIST_APPEND_LIST(&frames, ast_channel_readq(chan), frame_list);
1158 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_readq(chan));
1160 AST_LIST_APPEND_LIST(ast_channel_readq(chan), &frames, frame_list);
1163 if (ast_channel_alert_writable(chan)) {
1164 /* Write to the alert pipe for each added frame */
1165 while (new_frames--) {
1166 if (ast_channel_alert_write(chan)) {
1167 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %u): %s!\n",
1168 ast_channel_name(chan), queued_frames, strerror(errno));
1172 } else if (ast_channel_timingfd(chan) > -1) {
1173 ast_timer_enable_continuous(ast_channel_timer(chan));
1174 } else if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
1175 pthread_kill(ast_channel_blocker(chan), SIGURG);
1178 ast_channel_unlock(chan);
1183 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
1185 return __ast_queue_frame(chan, fin, 0, NULL);
1188 int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
1190 return __ast_queue_frame(chan, fin, 1, NULL);
1193 /*! \brief Queue a hangup frame for channel */
1194 int ast_queue_hangup(struct ast_channel *chan)
1196 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1199 /* Yeah, let's not change a lock-critical value without locking */
1200 ast_channel_lock(chan);
1201 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1202 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), NULL);
1204 res = ast_queue_frame(chan, &f);
1205 ast_channel_unlock(chan);
1209 /*! \brief Queue a hangup frame for channel */
1210 int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
1212 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1213 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1217 f.data.uint32 = cause;
1220 /* Yeah, let's not change a lock-critical value without locking */
1221 ast_channel_lock(chan);
1222 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1224 f.data.uint32 = ast_channel_hangupcause(chan);
1226 blob = ast_json_pack("{s: i}",
1228 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
1230 res = ast_queue_frame(chan, &f);
1231 ast_channel_unlock(chan);
1235 int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
1237 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HOLD };
1238 struct ast_json *blob = NULL;
1241 if (!ast_strlen_zero(musicclass)) {
1242 f.data.ptr = (void *) musicclass;
1243 f.datalen = strlen(musicclass) + 1;
1245 blob = ast_json_pack("{s: s}",
1246 "musicclass", musicclass);
1249 ast_channel_publish_cached_blob(chan, ast_channel_hold_type(), blob);
1251 res = ast_queue_frame(chan, &f);
1253 ast_json_unref(blob);
1258 int ast_queue_unhold(struct ast_channel *chan)
1260 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_UNHOLD };
1263 ast_channel_publish_cached_blob(chan, ast_channel_unhold_type(), NULL);
1265 res = ast_queue_frame(chan, &f);
1270 /*! \brief Queue a control frame */
1271 int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
1273 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
1274 return ast_queue_frame(chan, &f);
1277 /*! \brief Queue a control frame with payload */
1278 int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control,
1279 const void *data, size_t datalen)
1281 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
1282 return ast_queue_frame(chan, &f);
1285 /*! \brief Set defer DTMF flag on channel */
1286 int ast_channel_defer_dtmf(struct ast_channel *chan)
1291 ast_channel_lock(chan);
1292 pre = ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1293 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1294 ast_channel_unlock(chan);
1299 /*! \brief Unset defer DTMF flag on channel */
1300 void ast_channel_undefer_dtmf(struct ast_channel *chan)
1303 ast_channel_clear_flag(chan, AST_FLAG_DEFER_DTMF);
1307 struct ast_channel *ast_channel_callback(ao2_callback_data_fn *cb_fn, void *arg,
1308 void *data, int ao2_flags)
1310 return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
1313 static int ast_channel_by_name_cb(void *obj, void *arg, void *data, int flags)
1315 struct ast_channel *chan = obj;
1316 const char *name = arg;
1317 size_t name_len = *(size_t *) data;
1318 int ret = CMP_MATCH;
1320 if (ast_strlen_zero(name)) {
1321 ast_log(LOG_ERROR, "BUG! Must supply a channel name or partial name to match!\n");
1325 ast_channel_lock(chan);
1326 if ((!name_len && strcasecmp(ast_channel_name(chan), name))
1327 || (name_len && strncasecmp(ast_channel_name(chan), name, name_len))) {
1328 ret = 0; /* name match failed, keep looking */
1330 ast_channel_unlock(chan);
1335 static int ast_channel_by_exten_cb(void *obj, void *arg, void *data, int flags)
1337 struct ast_channel *chan = obj;
1338 char *context = arg;
1340 int ret = CMP_MATCH;
1342 if (ast_strlen_zero(exten) || ast_strlen_zero(context)) {
1343 ast_log(LOG_ERROR, "BUG! Must have a context and extension to match!\n");
1347 ast_channel_lock(chan);
1348 if (strcasecmp(ast_channel_context(chan), context) && strcasecmp(ast_channel_macrocontext(chan), context)) {
1349 ret = 0; /* Context match failed, continue */
1350 } else if (strcasecmp(ast_channel_exten(chan), exten) && strcasecmp(ast_channel_macroexten(chan), exten)) {
1351 ret = 0; /* Extension match failed, continue */
1353 ast_channel_unlock(chan);
1358 static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags)
1360 struct ast_channel *chan = obj;
1361 char *uniqueid = arg;
1362 size_t id_len = *(size_t *) data;
1363 int ret = CMP_MATCH;
1365 if (ast_strlen_zero(uniqueid)) {
1366 ast_log(LOG_ERROR, "BUG! Must supply a uniqueid or partial uniqueid to match!\n");
1370 ast_channel_lock(chan);
1371 if ((!id_len && strcasecmp(ast_channel_uniqueid(chan), uniqueid))
1372 || (id_len && strncasecmp(ast_channel_uniqueid(chan), uniqueid, id_len))) {
1373 ret = 0; /* uniqueid match failed, keep looking */
1375 ast_channel_unlock(chan);
1380 struct ast_channel_iterator {
1381 /* storage for non-dynamically allocated iterator */
1382 struct ao2_iterator simple_iterator;
1383 /* pointer to the actual iterator (simple_iterator or a dynamically
1384 * allocated iterator)
1386 struct ao2_iterator *active_iterator;
1389 struct ast_channel_iterator *ast_channel_iterator_destroy(struct ast_channel_iterator *i)
1391 ao2_iterator_destroy(i->active_iterator);
1397 struct ast_channel_iterator *ast_channel_iterator_by_exten_new(const char *exten, const char *context)
1399 struct ast_channel_iterator *i;
1400 char *l_exten = (char *) exten;
1401 char *l_context = (char *) context;
1403 if (!(i = ast_calloc(1, sizeof(*i)))) {
1407 i->active_iterator = (void *) ast_channel_callback(ast_channel_by_exten_cb,
1408 l_context, l_exten, OBJ_MULTIPLE);
1409 if (!i->active_iterator) {
1417 struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, size_t name_len)
1419 struct ast_channel_iterator *i;
1420 char *l_name = (char *) name;
1422 if (!(i = ast_calloc(1, sizeof(*i)))) {
1426 i->active_iterator = (void *) ast_channel_callback(ast_channel_by_name_cb,
1428 OBJ_MULTIPLE | (name_len == 0 /* match the whole word, so optimize */ ? OBJ_KEY : 0));
1429 if (!i->active_iterator) {
1437 struct ast_channel_iterator *ast_channel_iterator_all_new(void)
1439 struct ast_channel_iterator *i;
1441 if (!(i = ast_calloc(1, sizeof(*i)))) {
1445 i->simple_iterator = ao2_iterator_init(channels, 0);
1446 i->active_iterator = &i->simple_iterator;
1451 struct ast_channel *ast_channel_iterator_next(struct ast_channel_iterator *i)
1453 return ao2_iterator_next(i->active_iterator);
1456 /* Legacy function, not currently used for lookups, but we need a cmp_fn */
1457 static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
1459 ast_log(LOG_ERROR, "BUG! Should never be called!\n");
1463 struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
1465 struct ast_channel *chan;
1466 char *l_name = (char *) name;
1468 chan = ast_channel_callback(ast_channel_by_name_cb, l_name, &name_len,
1469 (name_len == 0) /* optimize if it is a complete name match */ ? OBJ_KEY : 0);
1474 if (ast_strlen_zero(l_name)) {
1475 /* We didn't have a name to search for so quit. */
1479 /* Now try a search for uniqueid. */
1480 return ast_channel_callback(ast_channel_by_uniqueid_cb, l_name, &name_len, 0);
1483 struct ast_channel *ast_channel_get_by_name(const char *name)
1485 return ast_channel_get_by_name_prefix(name, 0);
1488 struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
1490 char *l_exten = (char *) exten;
1491 char *l_context = (char *) context;
1493 return ast_channel_callback(ast_channel_by_exten_cb, l_context, l_exten, 0);
1496 int ast_is_deferrable_frame(const struct ast_frame *frame)
1498 /* Do not add a default entry in this switch statement. Each new
1499 * frame type should be addressed directly as to whether it should
1500 * be queued up or not.
1502 switch (frame->frametype) {
1503 case AST_FRAME_BRIDGE_ACTION:
1504 case AST_FRAME_BRIDGE_ACTION_SYNC:
1505 case AST_FRAME_CONTROL:
1506 case AST_FRAME_TEXT:
1507 case AST_FRAME_IMAGE:
1508 case AST_FRAME_HTML:
1511 case AST_FRAME_DTMF_END:
1512 case AST_FRAME_DTMF_BEGIN:
1513 case AST_FRAME_VOICE:
1514 case AST_FRAME_VIDEO:
1515 case AST_FRAME_NULL:
1518 case AST_FRAME_MODEM:
1519 case AST_FRAME_RTCP:
1525 /*! \brief Wait, look for hangups and condition arg */
1526 int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data)
1528 struct ast_frame *f;
1529 struct ast_silence_generator *silgen = NULL;
1531 struct timeval start;
1533 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
1535 AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
1537 /* If no other generator is present, start silencegen while waiting */
1538 if (ast_opt_transmit_silence && !ast_channel_generatordata(chan)) {
1539 silgen = ast_channel_start_silence_generator(chan);
1542 start = ast_tvnow();
1543 while ((ms = ast_remaining_ms(start, timeout_ms))) {
1544 struct ast_frame *dup_f = NULL;
1546 if (cond && ((*cond)(data) == 0)) {
1549 ms = ast_waitfor(chan, ms);
1561 if (!ast_is_deferrable_frame(f)) {
1566 if ((dup_f = ast_frisolate(f))) {
1570 AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
1575 /* stop silgen if present */
1577 ast_channel_stop_silence_generator(chan, silgen);
1580 /* We need to free all the deferred frames, but we only need to
1581 * queue the deferred frames if there was no error and no
1582 * hangup was received
1584 ast_channel_lock(chan);
1585 while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
1587 ast_queue_frame_head(chan, f);
1591 ast_channel_unlock(chan);
1596 /*! \brief Wait, look for hangups */
1597 int ast_safe_sleep(struct ast_channel *chan, int ms)
1599 return ast_safe_sleep_conditional(chan, ms, NULL, NULL);
1602 struct ast_channel *ast_channel_release(struct ast_channel *chan)
1604 /* Safe, even if already unlinked. */
1605 ao2_unlink(channels, chan);
1606 return ast_channel_unref(chan);
1609 void ast_party_name_init(struct ast_party_name *init)
1612 init->char_set = AST_PARTY_CHAR_SET_ISO8859_1;
1613 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1617 void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
1620 /* Don't copy to self */
1624 ast_free(dest->str);
1625 dest->str = ast_strdup(src->str);
1626 dest->char_set = src->char_set;
1627 dest->presentation = src->presentation;
1628 dest->valid = src->valid;
1631 void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
1634 init->char_set = guide->char_set;
1635 init->presentation = guide->presentation;
1636 init->valid = guide->valid;
1639 void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
1642 /* Don't set to self */
1646 if (src->str && src->str != dest->str) {
1647 ast_free(dest->str);
1648 dest->str = ast_strdup(src->str);
1651 dest->char_set = src->char_set;
1652 dest->presentation = src->presentation;
1653 dest->valid = src->valid;
1656 void ast_party_name_free(struct ast_party_name *doomed)
1658 ast_free(doomed->str);
1662 void ast_party_number_init(struct ast_party_number *init)
1665 init->plan = 0;/* Unknown */
1666 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1670 void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
1673 /* Don't copy to self */
1677 ast_free(dest->str);
1678 dest->str = ast_strdup(src->str);
1679 dest->plan = src->plan;
1680 dest->presentation = src->presentation;
1681 dest->valid = src->valid;
1684 void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
1687 init->plan = guide->plan;
1688 init->presentation = guide->presentation;
1689 init->valid = guide->valid;
1692 void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
1695 /* Don't set to self */
1699 if (src->str && src->str != dest->str) {
1700 ast_free(dest->str);
1701 dest->str = ast_strdup(src->str);
1704 dest->plan = src->plan;
1705 dest->presentation = src->presentation;
1706 dest->valid = src->valid;
1709 void ast_party_number_free(struct ast_party_number *doomed)
1711 ast_free(doomed->str);
1715 void ast_party_subaddress_init(struct ast_party_subaddress *init)
1719 init->odd_even_indicator = 0;
1723 void ast_party_subaddress_copy(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1726 /* Don't copy to self */
1730 ast_free(dest->str);
1731 dest->str = ast_strdup(src->str);
1732 dest->type = src->type;
1733 dest->odd_even_indicator = src->odd_even_indicator;
1734 dest->valid = src->valid;
1737 void ast_party_subaddress_set_init(struct ast_party_subaddress *init, const struct ast_party_subaddress *guide)
1740 init->type = guide->type;
1741 init->odd_even_indicator = guide->odd_even_indicator;
1742 init->valid = guide->valid;
1745 void ast_party_subaddress_set(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1748 /* Don't set to self */
1752 if (src->str && src->str != dest->str) {
1753 ast_free(dest->str);
1754 dest->str = ast_strdup(src->str);
1757 dest->type = src->type;
1758 dest->odd_even_indicator = src->odd_even_indicator;
1759 dest->valid = src->valid;
1762 void ast_party_subaddress_free(struct ast_party_subaddress *doomed)
1764 ast_free(doomed->str);
1768 void ast_set_party_id_all(struct ast_set_party_id *update_id)
1770 update_id->name = 1;
1771 update_id->number = 1;
1772 update_id->subaddress = 1;
1775 void ast_party_id_init(struct ast_party_id *init)
1777 ast_party_name_init(&init->name);
1778 ast_party_number_init(&init->number);
1779 ast_party_subaddress_init(&init->subaddress);
1783 void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
1786 /* Don't copy to self */
1790 ast_party_name_copy(&dest->name, &src->name);
1791 ast_party_number_copy(&dest->number, &src->number);
1792 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1794 ast_free(dest->tag);
1795 dest->tag = ast_strdup(src->tag);
1798 void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
1800 ast_party_name_set_init(&init->name, &guide->name);
1801 ast_party_number_set_init(&init->number, &guide->number);
1802 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
1806 void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
1809 /* Don't set to self */
1813 if (!update || update->name) {
1814 ast_party_name_set(&dest->name, &src->name);
1816 if (!update || update->number) {
1817 ast_party_number_set(&dest->number, &src->number);
1819 if (!update || update->subaddress) {
1820 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
1823 if (src->tag && src->tag != dest->tag) {
1824 ast_free(dest->tag);
1825 dest->tag = ast_strdup(src->tag);
1829 void ast_party_id_free(struct ast_party_id *doomed)
1831 ast_party_name_free(&doomed->name);
1832 ast_party_number_free(&doomed->number);
1833 ast_party_subaddress_free(&doomed->subaddress);
1835 ast_free(doomed->tag);
1839 int ast_party_id_presentation(const struct ast_party_id *id)
1841 int number_priority;
1843 int number_screening;
1847 /* Determine name presentation priority. */
1848 if (!id->name.valid) {
1849 name_value = AST_PRES_UNAVAILABLE;
1852 name_value = id->name.presentation & AST_PRES_RESTRICTION;
1853 switch (name_value) {
1854 case AST_PRES_RESTRICTED:
1857 case AST_PRES_ALLOWED:
1860 case AST_PRES_UNAVAILABLE:
1864 name_value = AST_PRES_UNAVAILABLE;
1870 /* Determine number presentation priority. */
1871 if (!id->number.valid) {
1872 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1873 number_value = AST_PRES_UNAVAILABLE;
1874 number_priority = 3;
1876 number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
1877 number_value = id->number.presentation & AST_PRES_RESTRICTION;
1878 switch (number_value) {
1879 case AST_PRES_RESTRICTED:
1880 number_priority = 0;
1882 case AST_PRES_ALLOWED:
1883 number_priority = 1;
1885 case AST_PRES_UNAVAILABLE:
1886 number_priority = 2;
1889 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1890 number_value = AST_PRES_UNAVAILABLE;
1891 number_priority = 3;
1896 /* Select the wining presentation value. */
1897 if (name_priority < number_priority) {
1898 number_value = name_value;
1900 if (number_value == AST_PRES_UNAVAILABLE) {
1901 return AST_PRES_NUMBER_NOT_AVAILABLE;
1904 return number_value | number_screening;
1907 void ast_party_id_invalidate(struct ast_party_id *id)
1910 id->number.valid = 0;
1911 id->subaddress.valid = 0;
1914 void ast_party_id_reset(struct ast_party_id *id)
1916 ast_party_id_free(id);
1917 ast_party_id_init(id);
1920 struct ast_party_id ast_party_id_merge(struct ast_party_id *base, struct ast_party_id *overlay)
1922 struct ast_party_id merged;
1925 if (overlay->name.valid) {
1926 merged.name = overlay->name;
1928 if (overlay->number.valid) {
1929 merged.number = overlay->number;
1931 if (overlay->subaddress.valid) {
1932 merged.subaddress = overlay->subaddress;
1934 /* Note the actual structure is returned and not a pointer to it! */
1938 void ast_party_id_merge_copy(struct ast_party_id *dest, struct ast_party_id *base, struct ast_party_id *overlay)
1940 struct ast_party_id merged;
1942 merged = ast_party_id_merge(base, overlay);
1943 ast_party_id_copy(dest, &merged);
1946 void ast_party_dialed_init(struct ast_party_dialed *init)
1948 init->number.str = NULL;
1949 init->number.plan = 0;/* Unknown */
1950 ast_party_subaddress_init(&init->subaddress);
1951 init->transit_network_select = 0;
1954 void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1957 /* Don't copy to self */
1961 ast_free(dest->number.str);
1962 dest->number.str = ast_strdup(src->number.str);
1963 dest->number.plan = src->number.plan;
1964 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1965 dest->transit_network_select = src->transit_network_select;
1968 void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
1970 init->number.str = NULL;
1971 init->number.plan = guide->number.plan;
1972 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
1973 init->transit_network_select = guide->transit_network_select;
1976 void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1978 if (src->number.str && src->number.str != dest->number.str) {
1979 ast_free(dest->number.str);
1980 dest->number.str = ast_strdup(src->number.str);
1982 dest->number.plan = src->number.plan;
1984 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
1986 dest->transit_network_select = src->transit_network_select;
1989 void ast_party_dialed_free(struct ast_party_dialed *doomed)
1991 ast_free(doomed->number.str);
1992 doomed->number.str = NULL;
1993 ast_party_subaddress_free(&doomed->subaddress);
1996 void ast_party_caller_init(struct ast_party_caller *init)
1998 ast_party_id_init(&init->id);
1999 ast_party_id_init(&init->ani);
2000 ast_party_id_init(&init->priv);
2004 void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
2007 /* Don't copy to self */
2011 ast_party_id_copy(&dest->id, &src->id);
2012 ast_party_id_copy(&dest->ani, &src->ani);
2013 ast_party_id_copy(&dest->priv, &src->priv);
2014 dest->ani2 = src->ani2;
2017 void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
2019 ast_party_id_set_init(&init->id, &guide->id);
2020 ast_party_id_set_init(&init->ani, &guide->ani);
2021 ast_party_id_set_init(&init->priv, &guide->priv);
2022 init->ani2 = guide->ani2;
2025 void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
2027 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2028 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2029 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2030 dest->ani2 = src->ani2;
2033 void ast_party_caller_free(struct ast_party_caller *doomed)
2035 ast_party_id_free(&doomed->id);
2036 ast_party_id_free(&doomed->ani);
2037 ast_party_id_free(&doomed->priv);
2040 void ast_party_connected_line_init(struct ast_party_connected_line *init)
2042 ast_party_id_init(&init->id);
2043 ast_party_id_init(&init->ani);
2044 ast_party_id_init(&init->priv);
2046 init->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2049 void ast_party_connected_line_copy(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
2052 /* Don't copy to self */
2056 ast_party_id_copy(&dest->id, &src->id);
2057 ast_party_id_copy(&dest->ani, &src->ani);
2058 ast_party_id_copy(&dest->priv, &src->priv);
2059 dest->ani2 = src->ani2;
2060 dest->source = src->source;
2063 void ast_party_connected_line_set_init(struct ast_party_connected_line *init, const struct ast_party_connected_line *guide)
2065 ast_party_id_set_init(&init->id, &guide->id);
2066 ast_party_id_set_init(&init->ani, &guide->ani);
2067 ast_party_id_set_init(&init->priv, &guide->priv);
2068 init->ani2 = guide->ani2;
2069 init->source = guide->source;
2072 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)
2074 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2075 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2076 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2077 dest->ani2 = src->ani2;
2078 dest->source = src->source;
2081 void ast_party_connected_line_collect_caller(struct ast_party_connected_line *connected, struct ast_party_caller *caller)
2083 connected->id = caller->id;
2084 connected->ani = caller->ani;
2085 connected->priv = caller->priv;
2086 connected->ani2 = caller->ani2;
2087 connected->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2090 void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
2092 ast_party_id_free(&doomed->id);
2093 ast_party_id_free(&doomed->ani);
2094 ast_party_id_free(&doomed->priv);
2097 void ast_party_redirecting_reason_init(struct ast_party_redirecting_reason *init)
2100 init->code = AST_REDIRECTING_REASON_UNKNOWN;
2103 void ast_party_redirecting_reason_copy(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2109 ast_free(dest->str);
2110 dest->str = ast_strdup(src->str);
2111 dest->code = src->code;
2114 void ast_party_redirecting_reason_set_init(struct ast_party_redirecting_reason *init, const struct ast_party_redirecting_reason *guide)
2117 init->code = guide->code;
2120 void ast_party_redirecting_reason_set(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2126 if (src->str && src->str != dest->str) {
2127 ast_free(dest->str);
2128 dest->str = ast_strdup(src->str);
2131 dest->code = src->code;
2134 void ast_party_redirecting_reason_free(struct ast_party_redirecting_reason *doomed)
2136 ast_free(doomed->str);
2140 void ast_party_redirecting_init(struct ast_party_redirecting *init)
2142 ast_party_id_init(&init->orig);
2143 ast_party_id_init(&init->from);
2144 ast_party_id_init(&init->to);
2145 ast_party_id_init(&init->priv_orig);
2146 ast_party_id_init(&init->priv_from);
2147 ast_party_id_init(&init->priv_to);
2148 ast_party_redirecting_reason_init(&init->reason);
2149 ast_party_redirecting_reason_init(&init->orig_reason);
2153 void ast_party_redirecting_copy(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src)
2156 /* Don't copy to self */
2160 ast_party_id_copy(&dest->orig, &src->orig);
2161 ast_party_id_copy(&dest->from, &src->from);
2162 ast_party_id_copy(&dest->to, &src->to);
2163 ast_party_id_copy(&dest->priv_orig, &src->priv_orig);
2164 ast_party_id_copy(&dest->priv_from, &src->priv_from);
2165 ast_party_id_copy(&dest->priv_to, &src->priv_to);
2166 ast_party_redirecting_reason_copy(&dest->reason, &src->reason);
2167 ast_party_redirecting_reason_copy(&dest->orig_reason, &src->orig_reason);
2168 dest->count = src->count;
2171 void ast_party_redirecting_set_init(struct ast_party_redirecting *init, const struct ast_party_redirecting *guide)
2173 ast_party_id_set_init(&init->orig, &guide->orig);
2174 ast_party_id_set_init(&init->from, &guide->from);
2175 ast_party_id_set_init(&init->to, &guide->to);
2176 ast_party_id_set_init(&init->priv_orig, &guide->priv_orig);
2177 ast_party_id_set_init(&init->priv_from, &guide->priv_from);
2178 ast_party_id_set_init(&init->priv_to, &guide->priv_to);
2179 ast_party_redirecting_reason_set_init(&init->reason, &guide->reason);
2180 ast_party_redirecting_reason_set_init(&init->orig_reason, &guide->orig_reason);
2181 init->count = guide->count;
2184 void ast_party_redirecting_set(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src, const struct ast_set_party_redirecting *update)
2186 ast_party_id_set(&dest->orig, &src->orig, update ? &update->orig : NULL);
2187 ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
2188 ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
2189 ast_party_id_set(&dest->priv_orig, &src->priv_orig, update ? &update->priv_orig : NULL);
2190 ast_party_id_set(&dest->priv_from, &src->priv_from, update ? &update->priv_from : NULL);
2191 ast_party_id_set(&dest->priv_to, &src->priv_to, update ? &update->priv_to : NULL);
2192 ast_party_redirecting_reason_set(&dest->reason, &src->reason);
2193 ast_party_redirecting_reason_set(&dest->orig_reason, &src->orig_reason);
2194 dest->count = src->count;
2197 void ast_party_redirecting_free(struct ast_party_redirecting *doomed)
2199 ast_party_id_free(&doomed->orig);
2200 ast_party_id_free(&doomed->from);
2201 ast_party_id_free(&doomed->to);
2202 ast_party_id_free(&doomed->priv_orig);
2203 ast_party_id_free(&doomed->priv_from);
2204 ast_party_id_free(&doomed->priv_to);
2205 ast_party_redirecting_reason_free(&doomed->reason);
2206 ast_party_redirecting_reason_free(&doomed->orig_reason);
2209 /*! \brief Free a channel structure */
2210 static void ast_channel_destructor(void *obj)
2212 struct ast_channel *chan = obj;
2213 struct ast_var_t *vardata;
2214 struct ast_frame *f;
2215 struct varshead *headp;
2216 struct ast_datastore *datastore;
2217 char device_name[AST_CHANNEL_NAME];
2220 /* Stop monitoring */
2221 if (ast_channel_monitor(chan)) {
2222 ast_channel_monitor(chan)->stop(chan, 0);
2225 /* If there is native format music-on-hold state, free it */
2226 if (ast_channel_music_state(chan)) {
2227 ast_moh_cleanup(chan);
2230 ast_pbx_hangup_handler_destroy(chan);
2232 /* Things that may possibly raise Stasis messages shouldn't occur after this point */
2233 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEAD);
2235 if (ast_channel_internal_is_finalized(chan)) {
2236 /* A channel snapshot should not be in the process of being staged now. */
2237 ast_assert(!ast_test_flag(ast_channel_flags(chan), AST_FLAG_SNAPSHOT_STAGE));
2239 ast_channel_lock(chan);
2240 ast_channel_publish_snapshot(chan);
2241 ast_channel_unlock(chan);
2242 publish_cache_clear(chan);
2245 ast_channel_lock(chan);
2247 /* Get rid of each of the data stores on the channel */
2248 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry)))
2249 /* Free the data store */
2250 ast_datastore_free(datastore);
2252 /* While the channel is locked, take the reference to its callid while we tear down the call. */
2253 callid = ast_channel_callid(chan);
2254 ast_channel_callid_cleanup(chan);
2256 ast_channel_unlock(chan);
2258 /* Lock and unlock the channel just to be sure nobody has it locked still
2259 due to a reference that was stored in a datastore. (i.e. app_chanspy) */
2260 ast_channel_lock(chan);
2261 ast_channel_unlock(chan);
2263 if (ast_channel_tech_pvt(chan)) {
2264 ast_log_callid(LOG_WARNING, callid, "Channel '%s' may not have been hung up properly\n", ast_channel_name(chan));
2265 ast_free(ast_channel_tech_pvt(chan));
2268 if (ast_channel_sched(chan)) {
2269 ast_sched_context_destroy(ast_channel_sched(chan));
2272 if (ast_channel_internal_is_finalized(chan)) {
2275 ast_copy_string(device_name, ast_channel_name(chan), sizeof(device_name));
2276 if ((dashptr = strrchr(device_name, '-'))) {
2280 device_name[0] = '\0';
2283 /* Free translators */
2284 if (ast_channel_readtrans(chan))
2285 ast_translator_free_path(ast_channel_readtrans(chan));
2286 if (ast_channel_writetrans(chan))
2287 ast_translator_free_path(ast_channel_writetrans(chan));
2288 if (ast_channel_pbx(chan))
2289 ast_log_callid(LOG_WARNING, callid, "PBX may not have been terminated properly on '%s'\n", ast_channel_name(chan));
2292 ast_channel_set_oldwriteformat(chan, NULL);
2293 ast_channel_set_rawreadformat(chan, NULL);
2294 ast_channel_set_rawwriteformat(chan, NULL);
2295 ast_channel_set_readformat(chan, NULL);
2296 ast_channel_set_writeformat(chan, NULL);
2298 ast_party_dialed_free(ast_channel_dialed(chan));
2299 ast_party_caller_free(ast_channel_caller(chan));
2300 ast_party_connected_line_free(ast_channel_connected(chan));
2301 ast_party_connected_line_free(ast_channel_connected_indicated(chan));
2302 ast_party_redirecting_free(ast_channel_redirecting(chan));
2304 /* Close pipes if appropriate */
2305 ast_channel_internal_alertpipe_close(chan);
2306 if (ast_channel_timer(chan)) {
2307 ast_timer_close(ast_channel_timer(chan));
2308 ast_channel_timer_set(chan, NULL);
2310 while ((f = AST_LIST_REMOVE_HEAD(ast_channel_readq(chan), frame_list)))
2313 /* loop over the variables list, freeing all data and deleting list items */
2314 /* no need to lock the list, as the channel is already locked */
2315 headp = ast_channel_varshead(chan);
2316 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2317 ast_var_delete(vardata);
2319 ast_app_group_discard(chan);
2321 /* Destroy the jitterbuffer */
2322 ast_jb_destroy(chan);
2324 if (ast_channel_cdr(chan)) {
2325 ast_cdr_free(ast_channel_cdr(chan));
2326 ast_channel_cdr_set(chan, NULL);
2329 if (ast_channel_zone(chan)) {
2330 ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan)));
2333 ast_channel_internal_cleanup(chan);
2335 if (device_name[0]) {
2337 * We have a device name to notify of a new state.
2339 * Queue an unknown state, because, while we know that this particular
2340 * instance is dead, we don't know the state of all other possible
2343 ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE) ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), device_name);
2346 ast_channel_nativeformats_set(chan, NULL);
2348 ast_channel_named_callgroups_set(chan, NULL);
2349 ast_channel_named_pickupgroups_set(chan, NULL);
2351 ast_atomic_fetchadd_int(&chancount, -1);
2354 /*! \brief Free a dummy channel structure */
2355 static void ast_dummy_channel_destructor(void *obj)
2357 struct ast_channel *chan = obj;
2358 struct ast_datastore *datastore;
2359 struct ast_var_t *vardata;
2360 struct varshead *headp;
2362 ast_pbx_hangup_handler_destroy(chan);
2364 /* Get rid of each of the data stores on the channel */
2365 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) {
2366 /* Free the data store */
2367 ast_datastore_free(datastore);
2370 ast_party_dialed_free(ast_channel_dialed(chan));
2371 ast_party_caller_free(ast_channel_caller(chan));
2372 ast_party_connected_line_free(ast_channel_connected(chan));
2373 ast_party_connected_line_free(ast_channel_connected_indicated(chan));
2374 ast_party_redirecting_free(ast_channel_redirecting(chan));
2376 /* loop over the variables list, freeing all data and deleting list items */
2377 /* no need to lock the list, as the channel is already locked */
2378 headp = ast_channel_varshead(chan);
2379 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2380 ast_var_delete(vardata);
2382 if (ast_channel_cdr(chan)) {
2383 ast_cdr_free(ast_channel_cdr(chan));
2384 ast_channel_cdr_set(chan, NULL);
2387 ast_channel_internal_cleanup(chan);
2390 struct ast_datastore *ast_channel_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
2392 return ast_datastore_alloc(info, uid);
2395 int ast_channel_datastore_free(struct ast_datastore *datastore)
2397 return ast_datastore_free(datastore);
2400 int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to)
2402 struct ast_datastore *datastore = NULL, *datastore2;
2404 AST_LIST_TRAVERSE(ast_channel_datastores(from), datastore, entry) {
2405 if (datastore->inheritance > 0) {
2406 datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
2408 datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
2409 datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
2410 AST_LIST_INSERT_TAIL(ast_channel_datastores(to), datastore2, entry);
2417 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
2421 AST_LIST_INSERT_HEAD(ast_channel_datastores(chan), datastore, entry);
2426 int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
2428 return AST_LIST_REMOVE(ast_channel_datastores(chan), datastore, entry) ? 0 : -1;
2431 struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
2433 struct ast_datastore *datastore = NULL;
2438 AST_LIST_TRAVERSE(ast_channel_datastores(chan), datastore, entry) {
2439 if (datastore->info != info) {
2444 /* matched by type only */
2448 if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
2449 /* Matched by type AND uid */
2457 /*! Set the file descriptor on the channel */
2458 void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
2460 ast_channel_internal_fd_set(chan, which, fd);
2464 void ast_channel_clear_softhangup(struct ast_channel *chan, int flag)
2466 ast_channel_lock(chan);
2468 ast_channel_softhangup_internal_flag_clear(chan, flag);
2470 if (!ast_channel_softhangup_internal_flag(chan)) {
2471 struct ast_frame *fr;
2473 /* If we have completely cleared the softhangup flag,
2474 * then we need to fully abort the hangup process. This requires
2475 * pulling the END_OF_Q frame out of the channel frame queue if it
2476 * still happens to be there. */
2478 fr = AST_LIST_LAST(ast_channel_readq(chan));
2479 if (fr && fr->frametype == AST_FRAME_CONTROL &&
2480 fr->subclass.integer == AST_CONTROL_END_OF_Q) {
2481 AST_LIST_REMOVE(ast_channel_readq(chan), fr, frame_list);
2486 ast_channel_unlock(chan);
2489 /*! \brief Softly hangup a channel, don't lock */
2490 int ast_softhangup_nolock(struct ast_channel *chan, int cause)
2492 ast_debug(1, "Soft-Hanging (%#04x) up channel '%s'\n", (unsigned)cause, ast_channel_name(chan));
2493 /* Inform channel driver that we need to be hung up, if it cares */
2494 ast_channel_softhangup_internal_flag_add(chan, cause);
2495 ast_queue_frame(chan, &ast_null_frame);
2496 /* Interrupt any poll call or such */
2497 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING))
2498 pthread_kill(ast_channel_blocker(chan), SIGURG);
2502 /*! \brief Softly hangup a channel, lock */
2503 int ast_softhangup(struct ast_channel *chan, int cause)
2505 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2508 ast_channel_lock(chan);
2509 res = ast_softhangup_nolock(chan, cause);
2510 blob = ast_json_pack("{s: i, s: b}",
2513 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
2514 ast_channel_unlock(chan);
2519 static void free_translation(struct ast_channel *clonechan)
2521 if (ast_channel_writetrans(clonechan)) {
2522 ast_translator_free_path(ast_channel_writetrans(clonechan));
2524 if (ast_channel_readtrans(clonechan)) {
2525 ast_translator_free_path(ast_channel_readtrans(clonechan));
2527 ast_channel_writetrans_set(clonechan, NULL);
2528 ast_channel_readtrans_set(clonechan, NULL);
2531 void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
2533 RAII_VAR(struct ast_channel *, bridge, ast_channel_bridge_peer(chan), ast_channel_cleanup);
2535 ast_channel_lock(chan);
2536 if (force || ast_strlen_zero(ast_channel_hangupsource(chan))) {
2537 ast_channel_hangupsource_set(chan, source);
2539 ast_channel_unlock(chan);
2542 ast_channel_lock(bridge);
2543 if (force || ast_strlen_zero(ast_channel_hangupsource(bridge))) {
2544 ast_channel_hangupsource_set(bridge, source);
2546 ast_channel_unlock(bridge);
2550 int ast_channel_has_audio_frame_or_monitor(struct ast_channel *chan)
2552 return ast_channel_monitor(chan)
2553 || !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
2554 || !ast_framehook_list_contains_no_active(ast_channel_framehooks(chan));
2557 int ast_channel_has_hook_requiring_audio(struct ast_channel *chan)
2559 return ast_channel_monitor(chan)
2560 || !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
2561 || !ast_framehook_list_contains_no_active_of_type(ast_channel_framehooks(chan), AST_FRAME_VOICE);
2564 static void destroy_hooks(struct ast_channel *chan)
2566 if (ast_channel_audiohooks(chan)) {
2567 ast_audiohook_detach_list(ast_channel_audiohooks(chan));
2568 ast_channel_audiohooks_set(chan, NULL);
2571 ast_framehook_list_destroy(chan);
2574 /*! \brief Hangup a channel */
2575 void ast_hangup(struct ast_channel *chan)
2577 /* Be NULL safe for RAII_VAR() usage. */
2582 ast_autoservice_stop(chan);
2584 ast_channel_lock(chan);
2586 while (ast_channel_masq(chan) || ast_channel_masqr(chan)) {
2587 CHANNEL_DEADLOCK_AVOIDANCE(chan);
2590 /* Mark as a zombie so a masquerade cannot be setup on this channel. */
2591 ast_set_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE);
2593 ast_channel_unlock(chan);
2596 * XXX if running the hangup handlers here causes problems
2597 * because the handlers take too long to execute, we could move
2598 * the meat of this function into another thread. A thread
2599 * where channels go to die.
2601 * If this is done, ast_autoservice_chan_hangup_peer() will no
2604 ast_pbx_hangup_handler_run(chan);
2605 ao2_unlink(channels, chan);
2606 ast_channel_lock(chan);
2608 destroy_hooks(chan);
2610 free_translation(chan);
2611 /* Close audio stream */
2612 if (ast_channel_stream(chan)) {
2613 ast_closestream(ast_channel_stream(chan));
2614 ast_channel_stream_set(chan, NULL);
2616 /* Close video stream */
2617 if (ast_channel_vstream(chan)) {
2618 ast_closestream(ast_channel_vstream(chan));
2619 ast_channel_vstream_set(chan, NULL);
2621 if (ast_channel_sched(chan)) {
2622 ast_sched_context_destroy(ast_channel_sched(chan));
2623 ast_channel_sched_set(chan, NULL);
2626 if (ast_channel_generatordata(chan)) { /* Clear any tone stuff remaining */
2627 if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2628 ast_channel_generator(chan)->release(chan, ast_channel_generatordata(chan));
2631 ast_channel_generatordata_set(chan, NULL);
2632 ast_channel_generator_set(chan, NULL);
2634 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
2635 ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
2636 "is blocked by thread %ld in procedure %s! Expect a failure\n",
2637 (long) pthread_self(), ast_channel_name(chan), (long)ast_channel_blocker(chan), ast_channel_blockproc(chan));
2638 ast_assert(ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING) == 0);
2641 ast_debug(1, "Hanging up channel '%s'\n", ast_channel_name(chan));
2642 if (ast_channel_tech(chan)->hangup) {
2643 ast_channel_tech(chan)->hangup(chan);
2646 ast_channel_unlock(chan);
2650 ast_channel_unref(chan);
2655 * \brief Set channel answered time if not already set.
2658 * \param chan Channel to set answered time.
2662 static void set_channel_answer_time(struct ast_channel *chan)
2664 if (ast_tvzero(ast_channel_answertime(chan))) {
2665 struct timeval answertime;
2667 answertime = ast_tvnow();
2668 ast_channel_answertime_set(chan, &answertime);
2672 int ast_raw_answer(struct ast_channel *chan)
2676 ast_channel_lock(chan);
2678 /* You can't answer an outbound call */
2679 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING)) {
2680 ast_channel_unlock(chan);
2684 /* Stop if we're a zombie or need a soft hangup */
2685 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
2686 ast_channel_unlock(chan);
2691 * Mark when incoming channel answered so we can know how
2692 * long the channel has been up.
2694 set_channel_answer_time(chan);
2696 ast_channel_unlock(chan);
2698 switch (ast_channel_state(chan)) {
2699 case AST_STATE_RINGING:
2700 case AST_STATE_RING:
2701 ast_channel_lock(chan);
2702 if (ast_channel_tech(chan)->answer) {
2703 res = ast_channel_tech(chan)->answer(chan);
2705 ast_setstate(chan, AST_STATE_UP);
2706 ast_channel_unlock(chan);
2714 ast_indicate(chan, -1);
2719 int __ast_answer(struct ast_channel *chan, unsigned int delay)
2722 enum ast_channel_state old_state;
2724 old_state = ast_channel_state(chan);
2725 if ((res = ast_raw_answer(chan))) {
2729 switch (old_state) {
2730 case AST_STATE_RINGING:
2731 case AST_STATE_RING:
2732 /* wait for media to start flowing, but don't wait any longer
2733 * than 'delay' or 500 milliseconds, whichever is longer
2736 AST_LIST_HEAD_NOLOCK(, ast_frame) frames;
2737 struct ast_frame *cur;
2738 struct ast_frame *new_frame;
2739 int timeout_ms = MAX(delay, 500);
2740 unsigned int done = 0;
2741 struct timeval start;
2743 AST_LIST_HEAD_INIT_NOLOCK(&frames);
2745 start = ast_tvnow();
2747 int ms = ast_remaining_ms(start, timeout_ms);
2748 ms = ast_waitfor(chan, ms);
2750 ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", ast_channel_name(chan), strerror(errno));
2755 ast_debug(2, "Didn't receive a media frame from %s within %u ms of answering. Continuing anyway\n", ast_channel_name(chan), MAX(delay, 500));
2758 cur = ast_read(chan);
2759 if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
2760 (cur->subclass.integer == AST_CONTROL_HANGUP))) {
2765 ast_debug(2, "Hangup of channel %s detected in answer routine\n", ast_channel_name(chan));
2769 if ((new_frame = ast_frisolate(cur)) != cur) {
2773 AST_LIST_INSERT_HEAD(&frames, new_frame, frame_list);
2775 /* if a specific delay period was requested, continue
2776 * until that delay has passed. don't stop just because
2777 * incoming media has arrived.
2783 switch (new_frame->frametype) {
2784 /* all of these frametypes qualify as 'media' */
2785 case AST_FRAME_VOICE:
2786 case AST_FRAME_VIDEO:
2787 case AST_FRAME_TEXT:
2788 case AST_FRAME_DTMF_BEGIN:
2789 case AST_FRAME_DTMF_END:
2790 case AST_FRAME_IMAGE:
2791 case AST_FRAME_HTML:
2792 case AST_FRAME_MODEM:
2793 case AST_FRAME_RTCP:
2796 case AST_FRAME_CONTROL:
2798 case AST_FRAME_BRIDGE_ACTION:
2799 case AST_FRAME_BRIDGE_ACTION_SYNC:
2800 case AST_FRAME_NULL:
2810 ast_channel_lock(chan);
2811 while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
2813 ast_queue_frame_head(chan, cur);
2817 ast_channel_unlock(chan);
2827 int ast_answer(struct ast_channel *chan)
2829 return __ast_answer(chan, 0);
2832 inline int ast_auto_answer(struct ast_channel *chan)
2834 if (ast_channel_state(chan) == AST_STATE_UP) {
2835 /* Already answered */
2838 return ast_answer(chan);
2841 int ast_channel_get_duration(struct ast_channel *chan)
2843 ast_assert(NULL != chan);
2845 if (ast_tvzero(ast_channel_creationtime(chan))) {
2848 return (ast_tvdiff_ms(ast_tvnow(), ast_channel_creationtime(chan)) / 1000);
2851 int ast_channel_get_up_time(struct ast_channel *chan)
2853 ast_assert(NULL != chan);
2855 if (ast_tvzero(ast_channel_answertime(chan))) {
2858 return (ast_tvdiff_ms(ast_tvnow(), ast_channel_answertime(chan)) / 1000);
2861 static void deactivate_generator_nolock(struct ast_channel *chan)
2863 if (ast_channel_generatordata(chan)) {
2864 struct ast_generator *generator = ast_channel_generator(chan);
2866 if (generator && generator->release) {
2867 generator->release(chan, ast_channel_generatordata(chan));
2869 ast_channel_generatordata_set(chan, NULL);
2870 ast_channel_generator_set(chan, NULL);
2871 ast_channel_set_fd(chan, AST_GENERATOR_FD, -1);
2872 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
2873 ast_settimeout(chan, 0, NULL, NULL);
2877 void ast_deactivate_generator(struct ast_channel *chan)
2879 ast_channel_lock(chan);
2880 deactivate_generator_nolock(chan);
2881 ast_channel_unlock(chan);
2884 static void generator_write_format_change(struct ast_channel *chan)
2886 struct ast_generator *generator;
2888 ast_channel_lock(chan);
2889 generator = ast_channel_generator(chan);
2890 if (generator && generator->write_format_change) {
2891 generator->write_format_change(chan, ast_channel_generatordata(chan));
2893 ast_channel_unlock(chan);
2896 static int generator_force(const void *data)
2898 /* Called if generator doesn't have data */
2901 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
2902 struct ast_channel *chan = (struct ast_channel *)data;
2904 ast_channel_lock(chan);
2905 tmp = ast_channel_generatordata(chan);
2906 ast_channel_generatordata_set(chan, NULL);
2907 if (ast_channel_generator(chan))
2908 generate = ast_channel_generator(chan)->generate;
2909 ast_channel_unlock(chan);
2911 if (!tmp || !generate) {
2915 res = generate(chan, tmp, 0, ast_format_get_sample_rate(ast_channel_writeformat(chan)) / 50);
2917 ast_channel_lock(chan);
2918 if (ast_channel_generator(chan) && generate == ast_channel_generator(chan)->generate) {
2919 ast_channel_generatordata_set(chan, tmp);
2921 ast_channel_unlock(chan);
2924 ast_debug(1, "Auto-deactivating generator\n");
2925 ast_deactivate_generator(chan);
2931 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
2934 void *generatordata = NULL;
2936 ast_channel_lock(chan);
2937 if (ast_channel_generatordata(chan)) {
2938 struct ast_generator *generator_old = ast_channel_generator(chan);
2940 if (generator_old && generator_old->release) {
2941 generator_old->release(chan, ast_channel_generatordata(chan));
2944 if (gen->alloc && !(generatordata = gen->alloc(chan, params))) {
2947 ast_channel_generatordata_set(chan, generatordata);
2949 ast_settimeout(chan, 50, generator_force, chan);
2950 ast_channel_generator_set(chan, gen);
2952 ast_channel_unlock(chan);
2959 /*! \brief Wait for x amount of time on a file descriptor to have input. */
2960 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
2963 ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
2967 /*! \brief Wait for x amount of time on a file descriptor to have input. */
2968 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
2969 int *exception, int *outfd, int *ms)
2971 struct timeval start = { 0 , 0 };
2972 struct pollfd *pfds = NULL;
2977 struct timeval now = { 0, 0 };
2978 struct timeval whentohangup = { 0, 0 }, diff;
2979 struct ast_channel *winner = NULL;
2992 for (x = 0; x < n; x++) {
2993 ast_channel_lock(c[x]);
2994 if (!ast_tvzero(*ast_channel_whentohangup(c[x]))) {
2995 if (ast_tvzero(whentohangup))
2997 diff = ast_tvsub(*ast_channel_whentohangup(c[x]), now);
2998 if (diff.tv_sec < 0 || ast_tvzero(diff)) {
2999 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3000 /* Should already be hungup */
3001 ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3002 ast_channel_unlock(c[x]);
3005 if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
3006 whentohangup = diff;
3008 sz += ast_channel_fd_count(c[x]);
3009 ast_channel_unlock(c[x]);
3016 pfds = ast_alloca(sizeof(*pfds) * sz);
3017 fdmap = ast_alloca(sizeof(*fdmap) * sz);
3019 /* Wait full interval */
3021 /* INT_MAX, not LONG_MAX, because it matters on 64-bit */
3022 if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
3023 rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000; /* timeout in milliseconds */
3024 if (*ms >= 0 && *ms < rms) { /* original *ms still smaller */
3027 } else if (!ast_tvzero(whentohangup) && rms < 0) {
3028 /* Tiny corner case... call would need to last >24 days */
3032 * Build the pollfd array, putting the channels' fds first,
3033 * followed by individual fds. Order is important because
3034 * individual fd's must have priority over channel fds.
3037 for (x = 0; x < n; x++) {
3038 ast_channel_lock(c[x]);
3039 for (y = 0; y < ast_channel_fd_count(c[x]); y++) {
3040 fdmap[max].fdno = y; /* fd y is linked to this pfds */
3041 fdmap[max].chan = x; /* channel x is linked to this pfds */
3042 max += ast_add_fd(&pfds[max], ast_channel_fd(c[x], y));
3044 CHECK_BLOCKING(c[x]);
3045 ast_channel_unlock(c[x]);
3047 /* Add the individual fds */
3048 for (x = 0; x < nfds; x++) {
3049 fdmap[max].chan = -1;
3050 max += ast_add_fd(&pfds[max], fds[x]);
3054 start = ast_tvnow();
3057 if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */
3060 if (kbrms > 600000) {
3063 res = ast_poll(pfds, max, kbrms);
3067 } while (!res && (rms > 0));
3069 res = ast_poll(pfds, max, rms);
3071 for (x = 0; x < n; x++) {
3072 ast_channel_lock(c[x]);
3073 ast_clear_flag(ast_channel_flags(c[x]), AST_FLAG_BLOCKING);
3074 ast_channel_unlock(c[x]);
3076 if (res < 0) { /* Simulate a timeout if we were interrupted */
3077 if (errno != EINTR) {
3082 if (!ast_tvzero(whentohangup)) { /* if we have a timeout, check who expired */
3084 for (x = 0; x < n; x++) {
3085 if (!ast_tvzero(*ast_channel_whentohangup(c[x])) && ast_tvcmp(*ast_channel_whentohangup(c[x]), now) <= 0) {
3086 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3087 ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3088 if (winner == NULL) {
3094 if (res == 0) { /* no fd ready, reset timeout and done */
3095 *ms = 0; /* XXX use 0 since we may not have an exact timeout. */
3099 * Then check if any channel or fd has a pending event.
3100 * Remember to check channels first and fds last, as they
3101 * must have priority on setting 'winner'
3103 for (x = 0; x < max; x++) {
3104 res = pfds[x].revents;
3108 if (fdmap[x].chan >= 0) { /* this is a channel */
3109 winner = c[fdmap[x].chan]; /* override previous winners */
3110 ast_channel_lock(winner);
3111 if (res & POLLPRI) {
3112 ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3114 ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3116 ast_channel_fdno_set(winner, fdmap[x].fdno);
3117 ast_channel_unlock(winner);
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);
3137 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
3139 return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
3142 int ast_waitfor(struct ast_channel *c, int ms)
3147 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3150 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3155 int ast_waitfordigit(struct ast_channel *c, int ms)
3157 return ast_waitfordigit_full(c, ms, -1, -1);
3160 int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
3162 return ast_settimeout_full(c, rate, func, data, 0);
3165 int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data, unsigned int is_ao2_obj)
3168 unsigned int real_rate = rate, max_rate;
3170 ast_channel_lock(c);
3172 if (ast_channel_timingfd(c) == -1) {
3173 ast_channel_unlock(c);
3182 if (rate && rate > (max_rate = ast_timer_get_max_rate(ast_channel_timer(c)))) {
3183 real_rate = max_rate;
3186 ast_debug(1, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
3188 res = ast_timer_set_rate(ast_channel_timer(c), real_rate);
3190 if (ast_channel_timingdata(c) && ast_test_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
3191 ao2_ref(ast_channel_timingdata(c), -1);
3194 ast_channel_timingfunc_set(c, func);
3195 ast_channel_timingdata_set(c, data);
3197 if (data && is_ao2_obj) {
3199 ast_set_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
3201 ast_clear_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
3204 if (func == NULL && rate == 0 && ast_channel_fdno(c) == AST_TIMING_FD) {
3205 /* Clearing the timing func and setting the rate to 0
3206 * means that we don't want to be reading from the timingfd
3207 * any more. Setting c->fdno to -1 means we won't have any
3208 * errant reads from the timingfd, meaning we won't potentially
3209 * miss any important frames.
3211 ast_channel_fdno_set(c, -1);
3214 ast_channel_unlock(c);
3219 int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, int cmdfd)
3221 struct timeval start = ast_tvnow();
3224 /* Stop if we're a zombie or need a soft hangup */
3225 if (ast_test_flag(ast_channel_flags(c), AST_FLAG_ZOMBIE) || ast_check_hangup(c))
3228 /* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
3229 ast_channel_set_flag(c, AST_FLAG_END_DTMF_ONLY);
3231 /* Wait for a digit, no more than timeout_ms milliseconds total.
3232 * Or, wait indefinitely if timeout_ms is <0.
3234 while ((ms = ast_remaining_ms(start, timeout_ms))) {
3235 struct ast_channel *rchan;
3239 /* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,
3240 * it is unhelpful if it waited less than a millisecond.
3242 rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
3244 if (!rchan && outfd < 0 && ms) {
3245 if (errno == 0 || errno == EINTR)
3247 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
3248 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
3250 } else if (outfd > -1) {
3251 /* The FD we were watching has something waiting */
3252 ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
3253 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
3257 struct ast_frame *f = ast_read(c);
3261 switch (f->frametype) {
3262 case AST_FRAME_DTMF_BEGIN:
3264 case AST_FRAME_DTMF_END:
3265 res = f->subclass.integer;
3267 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
3269 case AST_FRAME_CONTROL:
3270 switch (f->subclass.integer) {
3271 case AST_CONTROL_HANGUP:
3273 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
3275 case AST_CONTROL_STREAM_STOP:
3276 case AST_CONTROL_STREAM_SUSPEND:
3277 case AST_CONTROL_STREAM_RESTART:
3278 case AST_CONTROL_STREAM_REVERSE:
3279 case AST_CONTROL_STREAM_FORWARD:
3280 /* Fall-through and treat as if it were a DTMF signal. Items
3281 * that perform stream control will handle this. */
3282 res = f->subclass.integer;
3284 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
3286 case AST_CONTROL_PVT_CAUSE_CODE:
3287 case AST_CONTROL_RINGING:
3288 case AST_CONTROL_ANSWER:
3289 case AST_CONTROL_SRCUPDATE:
3290 case AST_CONTROL_SRCCHANGE:
3291 case AST_CONTROL_CONNECTED_LINE:
3292 case AST_CONTROL_REDIRECTING:
3293 case AST_CONTROL_UPDATE_RTP_PEER:
3294 case AST_CONTROL_HOLD:
3295 case AST_CONTROL_UNHOLD:
3300 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
3304 case AST_FRAME_VOICE:
3305 /* Write audio if appropriate */
3307 if (write(audiofd, f->data.ptr, f->datalen) < 0) {
3308 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3319 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
3321 return 0; /* Time is up */
3324 enum DtmfDirection {
3329 static const char *dtmf_direction_to_string(enum DtmfDirection direction)
3331 switch (direction) {
3341 static void send_dtmf_begin_event(struct ast_channel *chan,
3342 enum DtmfDirection direction, const char digit)
3344 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3345 char digit_str[] = { digit, '\0' };
3347 blob = ast_json_pack("{ s: s, s: s }",
3349 "direction", dtmf_direction_to_string(direction));
3354 ast_channel_publish_cached_blob(chan, ast_channel_dtmf_begin_type(), blob);
3357 static void send_dtmf_end_event(struct ast_channel *chan,
3358 enum DtmfDirection direction, const char digit, long duration_ms)
3360 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3361 char digit_str[] = { digit, '\0' };
3363 blob = ast_json_pack("{ s: s, s: s, s: i }",
3365 "direction", dtmf_direction_to_string(direction),
3366 "duration_ms", duration_ms);
3371 ast_channel_publish_cached_blob(chan, ast_channel_dtmf_end_type(), blob);
3374 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
3376 struct ast_generator *generator;
3381 generator = ast_channel_generator(chan);
3383 || !generator->generate
3384 || f->frametype != AST_FRAME_VOICE
3385 || !ast_channel_generatordata(chan)
3386 || ast_channel_timingfunc(chan)) {
3391 * We must generate frames in phase locked mode since
3392 * we have no internal timer available.
3394 if (ast_format_cmp(f->subclass.format, ast_channel_writeformat(chan)) == AST_FORMAT_CMP_NOT_EQUAL) {
3396 factor = ((float) ast_format_get_sample_rate(ast_channel_writeformat(chan))) / ((float) ast_format_get_sample_rate(f->subclass.format));
3397 samples = (int) (((float) f->samples) * factor);
3399 samples = f->samples;
3402 gendata = ast_channel_generatordata(chan);
3403 ast_channel_generatordata_set(chan, NULL); /* reset, to let writes go through */
3406 * This unlock is here based on two assumptions that hold true at
3407 * this point in the code. 1) this function is only called from
3408 * within __ast_read() and 2) all generators call ast_write() in
3409 * their generate callback.
3411 * The reason this is added is so that when ast_write is called,
3412 * the lock that occurs there will not recursively lock the
3413 * channel. Doing this will allow deadlock avoidance to work in
3416 ast_channel_unlock(chan);
3417 res = generator->generate(chan, gendata, f->datalen, samples);
3418 ast_channel_lock(chan);
3419 if (generator == ast_channel_generator(chan)) {
3420 ast_channel_generatordata_set(chan, gendata);
3422 ast_debug(1, "Auto-deactivating generator\n");
3423 ast_deactivate_generator(chan);
3428 static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
3430 struct ast_frame *fr = ast_channel_dtmff(chan);
3432 fr->frametype = AST_FRAME_DTMF_END;
3433 fr->subclass.integer = f->subclass.integer;
3436 /* The only time this function will be called is for a frame that just came
3437 * out of the channel driver. So, we want to stick it on the tail of the
3440 ast_queue_frame(chan, fr);
3444 * \brief Determine whether or not we should ignore DTMF in the readq
3446 static inline int should_skip_dtmf(struct ast_channel *chan)
3448 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_EMULATE_DTMF)) {
3449 /* We're in the middle of emulating a digit, or DTMF has been
3450 * explicitly deferred. Skip this digit, then. */
3454 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3455 ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
3456 /* We're not in the middle of a digit, but it hasn't been long enough
3457 * since the last digit, so we'll have to skip DTMF for now. */
3465 * \brief calculates the number of samples to jump forward with in a monitor stream.
3467 * \note When using ast_seekstream() with the read and write streams of a monitor,
3468 * the number of samples to seek forward must be of the same sample rate as the stream
3469 * or else the jump will not be calculated correctly.
3471 * \retval number of samples to seek forward after rate conversion.
3473 static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
3475 int diff = sample_rate - seek_rate;
3478 samples = samples / (float) (sample_rate / seek_rate);
3479 } else if (diff < 0) {
3480 samples = samples * (float) (seek_rate / sample_rate);
3486 static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio, int dropnondefault)
3488 struct ast_frame *f = NULL; /* the return value */
3491 struct ast_stream *stream = NULL, *default_stream = NULL;
3493 /* this function is very long so make sure there is only one return
3494 * point at the end (there are only two exceptions to this).
3496 ast_channel_lock(chan);
3498 /* Stop if we're a zombie or need a soft hangup */
3499 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
3500 if (ast_channel_generator(chan))
3501 ast_deactivate_generator(chan);
3504 * It is possible for chan->_softhangup to be set and there
3505 * still be control frames that need to be read. Instead of
3506 * just going to 'done' in the case of ast_check_hangup(), we
3507 * need to queue the end-of-Q frame so that it can mark the end
3508 * of the read queue. If there are frames to be read,
3509 * ast_queue_control() will be called repeatedly, but will only
3510 * queue the first end-of-Q frame.
3512 if (ast_channel_softhangup_internal_flag(chan)) {
3513 ast_queue_control(chan, AST_CONTROL_END_OF_Q);
3520 * The ast_waitfor() code records which of the channel's file
3521 * descriptors reported that data is available. In theory,
3522 * ast_read() should only be called after ast_waitfor() reports
3523 * that a channel has data available for reading. However,
3524 * there still may be some edge cases throughout the code where
3525 * ast_read() is called improperly. This can potentially cause
3526 * problems, so if this is a developer build, make a lot of
3527 * noise if this happens so that it can be addressed.
3529 * One of the potential problems is blocking on a dead channel.
3531 if (ast_channel_fdno(chan) == -1) {
3533 "ast_read() on chan '%s' called with no recorded file descriptor.\n",
3534 ast_channel_name(chan));
3539 prestate = ast_channel_state(chan);
3541 if (ast_channel_timingfd(chan) > -1 && ast_channel_fdno(chan) == AST_TIMING_FD) {
3542 enum ast_timer_event res;
3544 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3546 res = ast_timer_get_event(ast_channel_timer(chan));
3549 case AST_TIMING_EVENT_EXPIRED:
3550 if (ast_timer_ack(ast_channel_timer(chan), 1) < 0) {
3551 ast_log(LOG_ERROR, "Failed to acknoweldge timer in ast_read\n");
3555 if (ast_channel_timingfunc(chan)) {
3556 /* save a copy of func/data before unlocking the channel */
3557 ast_timing_func_t func = ast_channel_timingfunc(chan);
3558 void *data = ast_channel_timingdata(chan);
3560 if (data && ast_test_flag(ast_channel_flags(chan), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
3564 ast_channel_fdno_set(chan, -1);
3565 ast_channel_unlock(chan);
3571 ast_timer_set_rate(ast_channel_timer(chan), 0);
3572 ast_channel_fdno_set(chan, -1);
3573 ast_channel_unlock(chan);
3576 /* cannot 'goto done' because the channel is already unlocked */
3577 return &ast_null_frame;
3579 case AST_TIMING_EVENT_CONTINUOUS:
3580 if (AST_LIST_EMPTY(ast_channel_readq(chan)) ||
3581 !AST_LIST_NEXT(AST_LIST_FIRST(ast_channel_readq(chan)), frame_list)) {
3582 ast_timer_disable_continuous(ast_channel_timer(chan));
3587 } else if (ast_channel_fd_isset(chan, AST_GENERATOR_FD) && ast_channel_fdno(chan) == AST_GENERATOR_FD) {
3588 /* if the AST_GENERATOR_FD is set, call the generator with args
3589 * set to -1 so it can do whatever it needs to.
3591 void *tmp = ast_channel_generatordata(chan);
3592 ast_channel_generatordata_set(chan, NULL); /* reset to let ast_write get through */
3593 ast_channel_generator(chan)->generate(chan, tmp, -1, -1);
3594 ast_channel_generatordata_set(chan, tmp);
3595 f = &ast_null_frame;
3596 ast_channel_fdno_set(chan, -1);
3598 } else if (ast_channel_fd_isset(chan, AST_JITTERBUFFER_FD) && ast_channel_fdno(chan) == AST_JITTERBUFFER_FD) {
3599 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3602 /* Read and ignore anything on the alertpipe, but read only
3603 one sizeof(blah) per frame that we send from it */
3604 if (ast_channel_internal_alert_read(chan) == AST_ALERT_READ_FATAL) {
3605 f = &ast_null_frame;
3609 /* Check for pending read queue */
3610 if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
3611 int skip_dtmf = should_skip_dtmf(chan);
3613 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), f, frame_list) {
3614 /* We have to be picky about which frame we pull off of the readq because
3615 * there are cases where we want to leave DTMF frames on the queue until
3616 * some later time. */
3618 if ( (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) && skip_dtmf) {
3622 AST_LIST_REMOVE_CURRENT(frame_list);
3625 AST_LIST_TRAVERSE_SAFE_END;
3628 /* There were no acceptable frames on the readq. */
3629 f = &ast_null_frame;
3630 ast_channel_alert_write(chan);
3633 /* Interpret hangup and end-of-Q frames to return NULL */
3634 /* XXX why not the same for frames from the channel ? */
3635 if (f->frametype == AST_FRAME_CONTROL) {
3636 switch (f->subclass.integer) {
3637 case AST_CONTROL_HANGUP:
3638 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
3639 cause = f->data.uint32;
3641 case AST_CONTROL_END_OF_Q:
3648 } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
3649 if (ast_channel_tech(chan) && ast_channel_tech(chan)->read_stream) {
3650 stream = ast_stream_topology_get_stream(ast_channel_get_stream_topology(chan), f->stream_num);
3651 default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3653 /* Since this channel driver does not support multistream determine the default stream this frame
3654 * originated from and update the frame to include it.
3656 stream = default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3657 f->stream_num = ast_stream_get_position(stream);
3661 ast_channel_blocker_set(chan, pthread_self());
3662 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION)) {
3663 if (ast_channel_tech(chan)->exception)
3664 f = ast_channel_tech(chan)->exception(chan);
3666 ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", ast_channel_name(chan));
3667 f = &ast_null_frame;
3669 /* Clear the exception flag */
3670 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3671 } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read_stream) {
3672 f = ast_channel_tech(chan)->read_stream(chan);
3674 /* This channel driver supports multistream so the stream_num on the frame is valid, the only
3675 * thing different is that we need to find the default stream so we know whether to invoke the
3676 * default stream logic or not (such as transcoding).
3678 if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
3679 stream = ast_stream_topology_get_stream(ast_channel_get_stream_topology(chan), f->stream_num);
3680 default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3682 } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read) {
3683 f = ast_channel_tech(chan)->read(chan);
3685 /* Since this channel driver does not support multistream determine the default stream this frame
3686 * originated from and update the frame to include it.
3688 if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
3689 stream = default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3690 f->stream_num = ast_stream_get_position(stream);
3694 ast_log(LOG_WARNING, "No read routine on channel %s\n", ast_channel_name(chan));
3697 if (stream == default_stream) {
3698 /* Perform the framehook read event here. After the frame enters the framehook list
3699 * there is no telling what will happen, <insert mad scientist laugh here>!!! */
3700 f = ast_framehook_list_read_event(ast_channel_framehooks(chan), f);
3704 * Reset the recorded file descriptor that triggered this read so that we can
3705 * easily detect when ast_read() is called without properly using ast_waitfor().
3707 ast_channel_fdno_set(chan, -1);
3710 struct ast_frame *readq_tail = AST_LIST_LAST(ast_channel_readq(chan));
3711 struct ast_control_read_action_payload *read_action_payload;
3712 struct ast_party_connected_line connected;
3715 /* if the channel driver returned more than one frame, stuff the excess
3716 into the readq for the next ast_read call
3718 if (AST_LIST_NEXT(f, frame_list)) {
3719 ast_queue_frame(chan, AST_LIST_NEXT(f, frame_list));
3720 ast_frfree(AST_LIST_NEXT(f, frame_list));
3721 AST_LIST_NEXT(f, frame_list) = NULL;
3724 if (dropnondefault && stream != default_stream) {
3725 /* If the frame originates from a non-default stream and the caller can not handle other streams
3726 * absorb the frame and replace it with a null one instead.
3729 f = &ast_null_frame;
3732 switch (f->frametype) {
3733 case AST_FRAME_CONTROL:
3734 if (f->subclass.integer == AST_CONTROL_ANSWER) {
3735 if (prestate == AST_STATE_UP && ast_channel_is_bridged(chan)) {
3736 ast_debug(1, "Dropping duplicate answer!\n");
3738 f = &ast_null_frame;
3741 * Mark when outgoing channel answered so we can know how
3742 * long the channel has been up.
3744 set_channel_answer_time(chan);
3746 ast_setstate(chan, AST_STATE_UP);
3748 } else if (f->subclass.integer == AST_CONTROL_READ_ACTION) {
3749 read_action_payload = f->data.ptr;
3750 switch (read_action_payload->action) {
3751 case AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO:
3752 ast_party_connected_line_init(&connected);
3753 ast_party_connected_line_copy(&connected, ast_channel_connected(chan));
3754 if (ast_connected_line_parse_data(read_action_payload->payload,
3755 read_action_payload->payload_size, &connected)) {
3756 ast_party_connected_line_free(&connected);
3759 ast_channel_unlock(chan);
3760 if (ast_channel_connected_line_sub(NULL, chan, &connected, 0) &&
3761 ast_channel_connected_line_macro(NULL, chan, &connected, 1, 0)) {
3762 ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE,
3763 read_action_payload->payload,
3764 read_action_payload->payload_size);
3766 ast_party_connected_line_free(&connected);
3767 ast_channel_lock(chan);
3771 f = &ast_null_frame;
3772 } else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_REQUEST_CHANGE && dropnondefault) {
3773 /* The caller of this function is incapable of handling streams so we don't accept the change request
3774 * and stick to the streams currently on the channel.
3776 ast_channel_stream_topology_changed(chan, ast_channel_get_stream_topology(chan));
3778 f = &ast_null_frame;
3779 } else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_CHANGED && dropnondefault) {
3780 /* The caller of this function is incapable of handling streams so we absord the notification that the
3781 * stream topology has changed.
3784 f = &ast_null_frame;
3787 case AST_FRAME_DTMF_END:
3788 send_dtmf_end_event(chan, DTMF_RECEIVED, f->subclass.integer, f->len);
3789 ast_log(LOG_DTMF, "DTMF end '%c' received on %s, duration %ld ms\n", f->subclass.integer, ast_channel_name(chan), f->len);
3790 /* Queue it up if DTMF is deferred, or if DTMF emulation is forced. */
3791 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF) || ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF)) {
3792 queue_dtmf_readq(chan, f);
3794 f = &ast_null_frame;
3795 } else if (!ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF | AST_FLAG_END_DTMF_ONLY)) {
3796 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3797 ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
3798 /* If it hasn't been long enough, defer this digit */
3799 queue_dtmf_readq(chan, f);
3801 f = &ast_null_frame;
3803 /* There was no begin, turn this into a begin and send the end later */