Asterisk - The Open Source Telephony Project  GIT-master-a24979a
channel.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18 
19 /*! \file
20  *
21  * \brief Channel Management
22  *
23  * \author Mark Spencer <markster@digium.com>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 #include "asterisk/_private.h"
33 
34 #include <sys/time.h>
35 #include <signal.h>
36 #include <math.h>
37 
38 #include "asterisk/paths.h" /* use ast_config_AST_SYSTEM_NAME */
39 
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"
70 #include "asterisk/features.h"
71 #include "asterisk/bridge.h"
72 #include "asterisk/test.h"
74 #include "asterisk/max_forwards.h"
75 #include "asterisk/stream.h"
76 #include "asterisk/message.h"
77 
78 /*** DOCUMENTATION
79  ***/
80 
81 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
82 #if defined(HAVE_PRI)
83 #include "libpri.h"
84 #endif /* defined(HAVE_PRI) */
85 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
86 
87 /* uncomment if you have problems with 'monitoring' synchronized files */
88 #if 0
89 #define MONITOR_CONSTANT_DELAY
90 #define MONITOR_DELAY 150 * 8 /*!< 150 ms of MONITORING DELAY */
91 #endif
92 
93 static int chancount;
94 
95 unsigned long global_fin, global_fout;
96 
98 #define STATE2STR_BUFSIZE 32
99 
100 /*! Default amount of time to use when emulating a DTMF digit as a begin and end
101  * 100ms */
102 #define AST_DEFAULT_EMULATE_DTMF_DURATION 100
103 
104 /*! Default amount of time to use when emulating an MF digit as a begin and end
105  * 55ms */
106 #define DEFAULT_EMULATE_MF_DURATION 55
107 
108 #define DEFAULT_AMA_FLAGS AST_AMA_DOCUMENTATION
109 
110 /*! Minimum amount of time between the end of the last digit and the beginning
111  * of a new one - 45ms */
112 #define AST_MIN_DTMF_GAP 45
113 
114 /*! \brief List of channel drivers */
115 struct chanlist {
116  const struct ast_channel_tech *tech;
118 };
119 
120 /*! \brief the list of registered channel types */
122 
123 /*! \brief All active channels on the system */
124 static struct ao2_container *channels;
125 
126 /*! \brief map AST_CAUSE's to readable string representations
127  *
128  * \ref causes.h
129 */
130 struct causes_map {
131  int cause;
132  const char *name;
133  const char *desc;
134 };
135 
136 static const struct causes_map causes[] = {
137  { AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
138  { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
139  { AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
140  { AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
141  { AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
142  { AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
143  { AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
144  { AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
145  { AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
146  { AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
147  { AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
148  { AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
149  { AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
150  { AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
151  { AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
152  { AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
153  { AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
154  { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
155  { AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
156  { AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
157  { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
158  { AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
159  { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
160  { AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
161  { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
162  { AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
163  { AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
164  { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
165  { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
166  { AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
167  { AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
168  { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
169  { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
170  { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
171  { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
172  { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
173  { AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
174  { AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
175  { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
176  { AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
177  { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
178  { AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
179  { AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
180  { AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
181  { AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
182  { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
183  { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
184  { AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
185  { AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
186 };
187 
189 {
190  struct chanlist *cl;
191  struct ast_variable *var = NULL, *prev = NULL;
192 
194  AST_RWLIST_TRAVERSE(&backends, cl, list) {
195  if (prev) {
196  if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
197  prev = prev->next;
198  } else {
199  var = ast_variable_new(cl->tech->type, cl->tech->description, "");
200  prev = var;
201  }
202  }
204 
205  return var;
206 }
207 
208 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
209 static const char *party_number_ton2str(int ton)
210 {
211 #if defined(HAVE_PRI)
212  switch ((ton >> 4) & 0x07) {
213  case PRI_TON_INTERNATIONAL:
214  return "International";
215  case PRI_TON_NATIONAL:
216  return "National";
217  case PRI_TON_NET_SPECIFIC:
218  return "Network Specific";
219  case PRI_TON_SUBSCRIBER:
220  return "Subscriber";
221  case PRI_TON_ABBREVIATED:
222  return "Abbreviated";
223  case PRI_TON_RESERVED:
224  return "Reserved";
225  case PRI_TON_UNKNOWN:
226  default:
227  break;
228  }
229 #endif /* defined(HAVE_PRI) */
230  return "Unknown";
231 }
232 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
233 
234 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
235 static const char *party_number_plan2str(int plan)
236 {
237 #if defined(HAVE_PRI)
238  switch (plan & 0x0F) {
239  default:
240  case PRI_NPI_UNKNOWN:
241  break;
242  case PRI_NPI_E163_E164:
243  return "Public (E.163/E.164)";
244  case PRI_NPI_X121:
245  return "Data (X.121)";
246  case PRI_NPI_F69:
247  return "Telex (F.69)";
248  case PRI_NPI_NATIONAL:
249  return "National Standard";
250  case PRI_NPI_PRIVATE:
251  return "Private";
252  case PRI_NPI_RESERVED:
253  return "Reserved";
254  }
255 #endif /* defined(HAVE_PRI) */
256  return "Unknown";
257 }
258 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
259 
260 /*! \brief Show channel types - CLI command */
261 static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
262 {
263 #define FORMAT "%-15.15s %-40.40s %-13.13s %-13.13s %-13.13s %-13.13s\n"
264  struct chanlist *cl;
265  int count_chan = 0;
266 
267  switch (cmd) {
268  case CLI_INIT:
269  e->command = "core show channeltypes";
270  e->usage =
271  "Usage: core show channeltypes\n"
272  " Lists available channel types registered in your\n"
273  " Asterisk server.\n";
274  return NULL;
275  case CLI_GENERATE:
276  return NULL;
277  }
278 
279  if (a->argc != 3)
280  return CLI_SHOWUSAGE;
281 
282  ast_cli(a->fd, FORMAT, "Type", "Description", "Devicestate", "Presencestate", "Indications", "Transfer");
283  ast_cli(a->fd, FORMAT, "-------------", "-------------", "-------------", "-------------", "-------------", "-------------");
284 
287  ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
288  (cl->tech->devicestate) ? "yes" : "no",
289  (cl->tech->presencestate) ? "yes" : "no",
290  (cl->tech->indicate) ? "yes" : "no",
291  (cl->tech->transfer) ? "yes" : "no");
292  count_chan++;
293  }
295 
296  ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
297 
298  return CLI_SUCCESS;
299 
300 #undef FORMAT
301 }
302 
303 static char *complete_channeltypes(struct ast_cli_args *a)
304 {
305  struct chanlist *cl;
306  int wordlen;
307 
308  if (a->pos != 3) {
309  return NULL;
310  }
311 
312  wordlen = strlen(a->word);
313 
316  if (!strncasecmp(a->word, cl->tech->type, wordlen)) {
318  }
319  }
321 
322  return NULL;
323 }
324 
325 /*! \brief Show details about a channel driver - CLI command */
326 static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
327 {
328  struct chanlist *cl = NULL;
329  struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
330 
331  switch (cmd) {
332  case CLI_INIT:
333  e->command = "core show channeltype";
334  e->usage =
335  "Usage: core show channeltype <name>\n"
336  " Show details about the specified channel type, <name>.\n";
337  return NULL;
338  case CLI_GENERATE:
339  return complete_channeltypes(a);
340  }
341 
342  if (a->argc != 4)
343  return CLI_SHOWUSAGE;
344 
346 
347  AST_RWLIST_TRAVERSE(&backends, cl, list) {
348  if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
349  break;
350  }
351 
352 
353  if (!cl) {
354  ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
356  return CLI_FAILURE;
357  }
358 
359  ast_cli(a->fd,
360  "-- Info about channel driver: %s --\n"
361  " Device State: %s\n"
362  "Presence State: %s\n"
363  " Indication: %s\n"
364  " Transfer : %s\n"
365  " Capabilities: %s\n"
366  " Digit Begin: %s\n"
367  " Digit End: %s\n"
368  " Send HTML : %s\n"
369  " Image Support: %s\n"
370  " Text Support: %s\n",
371  cl->tech->type,
372  (cl->tech->devicestate) ? "yes" : "no",
373  (cl->tech->presencestate) ? "yes" : "no",
374  (cl->tech->indicate) ? "yes" : "no",
375  (cl->tech->transfer) ? "yes" : "no",
376  ast_format_cap_get_names(cl->tech->capabilities, &codec_buf),
377  (cl->tech->send_digit_begin) ? "yes" : "no",
378  (cl->tech->send_digit_end) ? "yes" : "no",
379  (cl->tech->send_html) ? "yes" : "no",
380  (cl->tech->send_image) ? "yes" : "no",
381  (cl->tech->send_text) ? "yes" : "no"
382 
383  );
384 
386 
387  return CLI_SUCCESS;
388 }
389 
390 static struct ast_cli_entry cli_channel[] = {
391  AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
392  AST_CLI_DEFINE(handle_cli_core_show_channeltype, "Give more details on that channel type")
393 };
394 
395 static struct ast_frame *kill_read(struct ast_channel *chan)
396 {
397  /* Hangup channel. */
398  return NULL;
399 }
400 
401 static struct ast_frame *kill_exception(struct ast_channel *chan)
402 {
403  /* Hangup channel. */
404  return NULL;
405 }
406 
407 static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
408 {
409  /* Hangup channel. */
410  return -1;
411 }
412 
413 static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
414 {
415  /* No problem fixing up the channel. */
416  return 0;
417 }
418 
419 static int kill_hangup(struct ast_channel *chan)
420 {
422  return 0;
423 }
424 
425 /*!
426  * \brief Kill the channel channel driver technology descriptor.
427  *
428  * \details
429  * The purpose of this channel technology is to encourage the
430  * channel to hangup as quickly as possible.
431  *
432  * \note Used by DTMF atxfer and zombie channels.
433  */
434 const struct ast_channel_tech ast_kill_tech = {
435  .type = "Kill",
436  .description = "Kill channel (should not see this)",
437  .read = kill_read,
438  .exception = kill_exception,
439  .write = kill_write,
440  .fixup = kill_fixup,
441  .hangup = kill_hangup,
442 };
443 
444 /*! \brief Checks to see if a channel is needing hang up */
445 int ast_check_hangup(struct ast_channel *chan)
446 {
447  if (ast_channel_softhangup_internal_flag(chan)) /* yes if soft hangup flag set */
448  return 1;
449  if (ast_tvzero(*ast_channel_whentohangup(chan))) /* no if no hangup scheduled */
450  return 0;
451  if (ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()) > 0) /* no if hangup time has not come yet. */
452  return 0;
453  ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()));
454  ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(chan));
456  return 1;
457 }
458 
460 {
461  int res;
462  ast_channel_lock(chan);
463  res = ast_check_hangup(chan);
464  ast_channel_unlock(chan);
465  return res;
466 }
467 
468 void ast_channel_softhangup_withcause_locked(struct ast_channel *chan, int causecode)
469 {
470  ast_channel_lock(chan);
471 
472  if (causecode > 0) {
473  ast_debug(1, "Setting hangupcause of channel %s to %d (is %d now)\n",
474  ast_channel_name(chan), causecode, ast_channel_hangupcause(chan));
475 
476  ast_channel_hangupcause_set(chan, causecode);
477  }
478 
480 
481  ast_channel_unlock(chan);
482 }
483 
484 static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
485 {
486  struct ast_channel *chan = obj;
487 
489 
490  return 0;
491 }
492 
494 {
496 }
497 
498 /*! \brief returns number of active/allocated channels */
500 {
501  return channels ? ao2_container_count(channels) : 0;
502 }
503 
505 {
507 }
508 
509 /*! \brief Set when to hangup channel */
510 void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
511 {
512  if (ast_tvzero(offset)) {
513  ast_channel_whentohangup_set(chan, &offset);
514  } else {
515  struct timeval tv = ast_tvadd(offset, ast_tvnow());
516  ast_channel_whentohangup_set(chan, &tv);
517  }
519  return;
520 }
521 
522 /*! \brief Compare a offset with when to hangup channel */
523 int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
524 {
525  struct timeval whentohangup;
526 
528  return ast_tvzero(offset) ? 0 : -1;
529 
530  if (ast_tvzero(offset))
531  return 1;
532 
533  whentohangup = ast_tvadd(offset, ast_tvnow());
534 
535  return ast_tvdiff_ms(whentohangup, *ast_channel_whentohangup(chan));
536 }
537 
538 /*! \brief Register a new telephony channel in Asterisk */
539 int ast_channel_register(const struct ast_channel_tech *tech)
540 {
541  struct chanlist *chan;
542 
544 
546  if (!strcasecmp(tech->type, chan->tech->type)) {
547  ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
549  return -1;
550  }
551  }
552 
553  if (!(chan = ast_calloc(1, sizeof(*chan)))) {
555  return -1;
556  }
557  chan->tech = tech;
559 
560  ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
561 
562  ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
563 
565 
566  return 0;
567 }
568 
569 /*! \brief Unregister channel driver */
571 {
572  struct chanlist *chan;
573 
574  ast_debug(1, "Unregistering channel type '%s'\n", tech->type);
575 
577 
579  if (chan->tech == tech) {
581  ast_free(chan);
582  ast_verb(2, "Unregistered channel type '%s'\n", tech->type);
583  break;
584  }
585  }
587 
589 }
590 
591 /*! \brief Get handle to channel driver based on name */
592 const struct ast_channel_tech *ast_get_channel_tech(const char *name)
593 {
594  struct chanlist *chanls;
595  const struct ast_channel_tech *ret = NULL;
596 
598 
599  AST_RWLIST_TRAVERSE(&backends, chanls, list) {
600  if (!strcasecmp(name, chanls->tech->type)) {
601  ret = chanls->tech;
602  break;
603  }
604  }
605 
607 
608  return ret;
609 }
610 
611 /*! \brief Gives the string form of a given hangup cause */
612 const char *ast_cause2str(int cause)
613 {
614  int x;
615 
616  for (x = 0; x < ARRAY_LEN(causes); x++) {
617  if (causes[x].cause == cause)
618  return causes[x].desc;
619  }
620 
621  return "Unknown";
622 }
623 
624 /*! \brief Convert a symbolic hangup cause to number */
625 int ast_str2cause(const char *name)
626 {
627  int x;
628 
629  for (x = 0; x < ARRAY_LEN(causes); x++)
630  if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
631  return causes[x].cause;
632 
633  return -1;
634 }
635 
637 {
638  char *buf;
639 
640  switch (state) {
641  case AST_STATE_DOWN:
642  return "Down";
643  case AST_STATE_RESERVED:
644  return "Rsrvd";
645  case AST_STATE_OFFHOOK:
646  return "OffHook";
647  case AST_STATE_DIALING:
648  return "Dialing";
649  case AST_STATE_RING:
650  return "Ring";
651  case AST_STATE_RINGING:
652  return "Ringing";
653  case AST_STATE_UP:
654  return "Up";
655  case AST_STATE_BUSY:
656  return "Busy";
658  return "Dialing Offhook";
659  case AST_STATE_PRERING:
660  return "Pre-ring";
661  case AST_STATE_MUTE:
662  return "Mute";
663  default:
665  return "Unknown";
666  snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%u)", state);
667  return buf;
668  }
669 }
670 
671 /*! \brief Gives the string form of a given transfer capability */
672 char *ast_transfercapability2str(int transfercapability)
673 {
674  switch (transfercapability) {
676  return "SPEECH";
678  return "DIGITAL";
680  return "RESTRICTED_DIGITAL";
682  return "3K1AUDIO";
684  return "DIGITAL_W_TONES";
685  case AST_TRANS_CAP_VIDEO:
686  return "VIDEO";
687  default:
688  return "UNKNOWN";
689  }
690 }
691 
692 /*! \brief Channel technology used to extract a channel from a running application. The
693  * channel created with this technology will be immediately hung up - most external
694  * applications won't ever want to see this.
695  */
696 static const struct ast_channel_tech surrogate_tech = {
697  .type = "Surrogate",
698  .description = "Surrogate channel used to pull channel from an application",
699  .properties = AST_CHAN_TP_INTERNAL,
700 };
701 
702 static const struct ast_channel_tech null_tech = {
703  .type = "NULL",
704  .description = "Null channel (should not see this)",
705 };
706 
707 static void ast_channel_destructor(void *obj);
708 static void ast_dummy_channel_destructor(void *obj);
709 static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags);
710 
711 static int does_id_conflict(const char *uniqueid)
712 {
713  struct ast_channel *conflict;
714  size_t length = 0;
715 
716  if (ast_strlen_zero(uniqueid)) {
717  return 0;
718  }
719 
720  conflict = ast_channel_callback(ast_channel_by_uniqueid_cb, (char *) uniqueid, &length, OBJ_NOLOCK);
721  if (conflict) {
722  ast_log(LOG_ERROR, "Channel Unique ID '%s' already in use by channel %s(%p)\n",
723  uniqueid, ast_channel_name(conflict), conflict);
724  ast_channel_unref(conflict);
725  return 1;
726  }
727 
728  return 0;
729 }
730 
731 /*! \brief Create a new channel structure */
732 static struct ast_channel *__attribute__((format(printf, 15, 0)))
733 __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
734  const char *acctcode, const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
735  const struct ast_channel *requestor, enum ama_flags amaflag, struct ast_endpoint *endpoint,
736  const char *file, int line,
737  const char *function, const char *name_fmt, va_list ap)
738 {
739  struct ast_channel *tmp;
740  struct varshead *headp;
741  char *tech = "", *tech2 = NULL;
742  struct ast_format_cap *nativeformats;
743  struct ast_sched_context *schedctx;
744  struct ast_timer *timer;
745  struct timeval now;
746  const struct ast_channel_tech *channel_tech;
747  struct ast_stream_topology *topology;
748 
749  /* If shutting down, don't allocate any new channels */
750  if (ast_shutting_down()) {
751  ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
752  return NULL;
753  }
754 
755  tmp = __ast_channel_internal_alloc(ast_channel_destructor, assignedids, requestor,
756  file, line, function);
757  if (!tmp) {
758  /* Channel structure allocation failure. */
759  return NULL;
760  }
761 
763 
764  /*
765  * Init file descriptors to unopened state so
766  * the destructor can know not to close them.
767  */
771 
773  if (!nativeformats) {
774  /*
775  * Aborting the channel creation. We do not need to complete staging
776  * the channel snapshot because the channel has not been finalized or
777  * linked into the channels container yet. Nobody else knows about
778  * this channel nor will anybody ever know about it.
779  */
780  return ast_channel_unref(tmp);
781  }
782  ast_format_cap_append(nativeformats, ast_format_none, 0);
783  ast_channel_nativeformats_set(tmp, nativeformats);
784  ao2_ref(nativeformats, -1);
785 
790 
791  if (!(schedctx = ast_sched_context_create())) {
792  ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
793  /* See earlier channel creation abort comment above. */
794  return ast_channel_unref(tmp);
795  }
796  ast_channel_sched_set(tmp, schedctx);
797 
803 
804  if (cid_name) {
807  if (!ast_channel_caller(tmp)->id.name.str) {
808  /* See earlier channel creation abort comment above. */
809  return ast_channel_unref(tmp);
810  }
811  }
812  if (cid_num) {
815  if (!ast_channel_caller(tmp)->id.number.str) {
816  /* See earlier channel creation abort comment above. */
817  return ast_channel_unref(tmp);
818  }
819  }
820 
821  if ((timer = ast_timer_open())) {
823  if (strcmp(ast_timer_get_name(ast_channel_timer(tmp)), "timerfd")) {
824  needqueue = 0;
825  }
827  }
828 
829  if (needqueue && ast_channel_internal_alertpipe_init(tmp)) {
830  /* See earlier channel creation abort comment above. */
831  return ast_channel_unref(tmp);
832  }
833 
834  if (!(topology = ast_stream_topology_alloc())) {
835  return ast_channel_unref(tmp);
836  }
838 
839  /* Always watch the alertpipe */
841  /* And timing pipe */
843 
844  /* Initial state */
847 
850 
853 
854  now = ast_tvnow();
856 
858 
859  if (!ast_strlen_zero(name_fmt)) {
860  char *slash, *slash2;
861  /* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call.
862  * And they all use slightly different formats for their name string.
863  * This means, to set the name here, we have to accept variable args, and call the string_field_build from here.
864  * This means, that the stringfields must have a routine that takes the va_lists directly, and
865  * uses them to build the string, instead of forming the va_lists internally from the vararg ... list.
866  * This new function was written so this can be accomplished.
867  */
868  ast_channel_name_build_va(tmp, name_fmt, ap);
870  if ((slash = strchr(tech, '/'))) {
871  if ((slash2 = strchr(slash + 1, '/'))) {
872  tech2 = slash + 1;
873  *slash2 = '\0';
874  }
875  *slash = '\0';
876  }
877  } else {
878  /*
879  * Start the string with '-' so it becomes an empty string
880  * in the destructor.
881  */
882  ast_channel_name_set(tmp, "-**Unknown**");
883  }
884 
885  if (amaflag != AST_AMA_NONE) {
886  ast_channel_amaflags_set(tmp, amaflag);
887  } else {
889  }
890 
891  if (!ast_strlen_zero(acctcode)) {
892  ast_channel_accountcode_set(tmp, acctcode);
893  }
894  ast_channel_language_set(tmp, ast_defaultlanguage);
895 
896  ast_channel_context_set(tmp, S_OR(context, "default"));
899 
900  headp = ast_channel_varshead(tmp);
902 
906 
907  channel_tech = ast_get_channel_tech(tech);
908  if (!channel_tech && !ast_strlen_zero(tech2)) {
909  channel_tech = ast_get_channel_tech(tech2);
910  }
911  if (channel_tech) {
912  ast_channel_tech_set(tmp, channel_tech);
913  } else {
915  }
916 
917  /* You might scream "locking inversion" at seeing this but it is actually perfectly fine.
918  * Since the channel was just created nothing can know about it yet or even acquire it.
919  */
921 
923 
924  if (assignedids && (does_id_conflict(assignedids->uniqueid) || does_id_conflict(assignedids->uniqueid2))) {
928  /* See earlier channel creation abort comment above. */
929  return ast_channel_unref(tmp);
930  }
931 
932  /* Finalize and link into the channels container. */
936 
938 
939  if (endpoint) {
940  ast_endpoint_add_channel(endpoint, tmp);
941  }
942 
943  /*
944  * And now, since the channel structure is built, and has its name, let
945  * the world know of its existance
946  */
948 
949  ast_debug(1, "Channel %p '%s' allocated\n", tmp, ast_channel_name(tmp));
950 
951  return tmp;
952 }
953 
954 struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
955  const char *cid_name, const char *acctcode,
956  const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
957  const struct ast_channel *requestor, enum ama_flags amaflag,
958  struct ast_endpoint *endpoint,
959  const char *file, int line, const char *function,
960  const char *name_fmt, ...)
961 {
962  va_list ap;
963  struct ast_channel *result;
964 
965  va_start(ap, name_fmt);
966  result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
967  assignedids, requestor, amaflag, endpoint, file, line, function, name_fmt, ap);
968  va_end(ap);
969 
970  return result;
971 }
972 
973 /* only do the minimum amount of work needed here to make a channel
974  * structure that can be used to expand channel vars */
975 struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
976 {
977  struct ast_channel *tmp;
978  struct varshead *headp;
979 
981  file, line, function);
982  if (!tmp) {
983  /* Dummy channel structure allocation failure. */
984  return NULL;
985  }
986 
989 
990  /*
991  * Init file descriptors to unopened state just in case
992  * autoservice is called on the channel or something tries to
993  * read a frame from it.
994  */
998 
1000 
1002 
1003  headp = ast_channel_varshead(tmp);
1005 
1006  return tmp;
1007 }
1008 
1009 static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
1010 {
1011  struct ast_frame *f;
1012  struct ast_frame *cur;
1013  unsigned int new_frames = 0;
1014  unsigned int new_voice_frames = 0;
1015  unsigned int queued_frames = 0;
1016  unsigned int queued_voice_frames = 0;
1018 
1019  ast_channel_lock(chan);
1020 
1021  /*
1022  * Check the last frame on the queue if we are queuing the new
1023  * frames after it.
1024  */
1025  cur = AST_LIST_LAST(ast_channel_readq(chan));
1026  if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
1027  switch (cur->subclass.integer) {
1028  case AST_CONTROL_END_OF_Q:
1029  if (fin->frametype == AST_FRAME_CONTROL
1030  && fin->subclass.integer == AST_CONTROL_HANGUP) {
1031  /*
1032  * Destroy the end-of-Q marker frame so we can queue the hangup
1033  * frame in its place.
1034  */
1036  ast_frfree(cur);
1037 
1038  /*
1039  * This has degenerated to a normal queue append anyway. Since
1040  * we just destroyed the last frame in the queue we must make
1041  * sure that "after" is NULL or bad things will happen.
1042  */
1043  after = NULL;
1044  break;
1045  }
1046  /* Fall through */
1047  case AST_CONTROL_HANGUP:
1048  /* Don't queue anything. */
1049  ast_channel_unlock(chan);
1050  return 0;
1051  default:
1052  break;
1053  }
1054  }
1055 
1056  /* Build copies of all the new frames and count them */
1058  for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
1059  if (!(f = ast_frdup(cur))) {
1060  if (AST_LIST_FIRST(&frames)) {
1062  }
1063  ast_channel_unlock(chan);
1064  return -1;
1065  }
1066 
1068  new_frames++;
1069  if (f->frametype == AST_FRAME_VOICE) {
1070  new_voice_frames++;
1071  }
1072  }
1073 
1074  /* Count how many frames exist on the queue */
1076  queued_frames++;
1077  if (cur->frametype == AST_FRAME_VOICE) {
1078  queued_voice_frames++;
1079  }
1080  }
1081 
1082  if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
1083  int count = 0;
1084  ast_log(LOG_WARNING, "Exceptionally long %squeue length queuing to %s\n", queued_frames + new_frames > 128 ? "" : "voice ", ast_channel_name(chan));
1086  /* Save the most recent frame */
1087  if (!AST_LIST_NEXT(cur, frame_list)) {
1088  break;
1089  } else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
1090  if (++count > 64) {
1091  break;
1092  }
1094  ast_frfree(cur);
1095 
1096  /* Read from the alert pipe for each flushed frame. */
1098  }
1099  }
1101  }
1102 
1103  if (after) {
1105  } else {
1106  if (head) {
1109  }
1111  }
1112 
1113  if (ast_channel_alert_writable(chan)) {
1114  /* Write to the alert pipe for each added frame */
1115  while (new_frames--) {
1116  if (ast_channel_alert_write(chan)) {
1117  ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %u): %s!\n",
1118  ast_channel_name(chan), queued_frames, strerror(errno));
1119  break;
1120  }
1121  }
1122  } else if (ast_channel_timingfd(chan) > -1) {
1124  } else if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
1125  pthread_kill(ast_channel_blocker(chan), SIGURG);
1126  }
1127 
1128  ast_channel_unlock(chan);
1129 
1130  return 0;
1131 }
1132 
1133 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
1134 {
1135  return __ast_queue_frame(chan, fin, 0, NULL);
1136 }
1137 
1138 int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
1139 {
1140  return __ast_queue_frame(chan, fin, 1, NULL);
1141 }
1142 
1143 /*! \brief Queue a hangup frame for channel */
1145 {
1146  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1147  int res;
1148 
1149  /* Yeah, let's not change a lock-critical value without locking */
1150  ast_channel_lock(chan);
1153 
1154  res = ast_queue_frame(chan, &f);
1155  ast_channel_unlock(chan);
1156  return res;
1157 }
1158 
1159 /*! \brief Queue a hangup frame for channel */
1160 int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
1161 {
1162  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1163  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1164  int res;
1165 
1166  if (cause >= 0) {
1167  f.data.uint32 = cause;
1168  }
1169 
1170  /* Yeah, let's not change a lock-critical value without locking */
1171  ast_channel_lock(chan);
1173  if (cause < 0) {
1175  }
1176  blob = ast_json_pack("{s: i}",
1177  "cause", cause);
1179 
1180  res = ast_queue_frame(chan, &f);
1181  ast_channel_unlock(chan);
1182  return res;
1183 }
1184 
1185 int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
1186 {
1187  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HOLD };
1188  struct ast_json *blob = NULL;
1189  int res;
1190 
1191  if (!ast_strlen_zero(musicclass)) {
1192  f.data.ptr = (void *) musicclass;
1193  f.datalen = strlen(musicclass) + 1;
1194 
1195  blob = ast_json_pack("{s: s}",
1196  "musicclass", musicclass);
1197  }
1198 
1199  ast_channel_lock(chan);
1201  ast_channel_unlock(chan);
1202 
1203  res = ast_queue_frame(chan, &f);
1204 
1205  ast_json_unref(blob);
1206 
1207  return res;
1208 }
1209 
1211 {
1212  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_UNHOLD };
1213  int res;
1214 
1215  ast_channel_lock(chan);
1217  ast_channel_unlock(chan);
1218 
1219  res = ast_queue_frame(chan, &f);
1220 
1221  return res;
1222 }
1223 
1224 /*! \brief Queue a control frame */
1226 {
1227  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
1228  return ast_queue_frame(chan, &f);
1229 }
1230 
1231 /*! \brief Queue a control frame with payload */
1233  const void *data, size_t datalen)
1234 {
1235  struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
1236  return ast_queue_frame(chan, &f);
1237 }
1238 
1239 /*! \brief Queue an ANSWER control frame with topology */
1240 int ast_queue_answer(struct ast_channel *chan, const struct ast_stream_topology *topology)
1241 {
1242  struct ast_frame f = {
1244  .subclass.integer = AST_CONTROL_ANSWER,
1245  .subclass.topology = (struct ast_stream_topology *)topology,
1246  };
1247  return ast_queue_frame(chan, &f);
1248 }
1249 
1250 /*! \brief Set defer DTMF flag on channel */
1252 {
1253  int pre = 0;
1254 
1255  if (chan) {
1256  ast_channel_lock(chan);
1259  ast_channel_unlock(chan);
1260  }
1261  return pre;
1262 }
1263 
1264 /*! \brief Unset defer DTMF flag on channel */
1266 {
1267  if (chan) {
1269  }
1270 }
1271 
1273  void *data, int ao2_flags)
1274 {
1275  return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
1276 }
1277 
1278 static int ast_channel_by_name_cb(void *obj, void *arg, void *data, int flags)
1279 {
1280  struct ast_channel *chan = obj;
1281  const char *name = arg;
1282  size_t name_len = *(size_t *) data;
1283  int ret = CMP_MATCH;
1284 
1285  if (ast_strlen_zero(name)) {
1286  ast_log(LOG_ERROR, "BUG! Must supply a channel name or partial name to match!\n");
1287  return CMP_STOP;
1288  }
1289 
1290  ast_channel_lock(chan);
1291  if ((!name_len && strcasecmp(ast_channel_name(chan), name))
1292  || (name_len && strncasecmp(ast_channel_name(chan), name, name_len))) {
1293  ret = 0; /* name match failed, keep looking */
1294  }
1295  ast_channel_unlock(chan);
1296 
1297  return ret;
1298 }
1299 
1300 static int ast_channel_by_exten_cb(void *obj, void *arg, void *data, int flags)
1301 {
1302  struct ast_channel *chan = obj;
1303  char *context = arg;
1304  char *exten = data;
1305  int ret = CMP_MATCH;
1306 
1308  ast_log(LOG_ERROR, "BUG! Must have a context and extension to match!\n");
1309  return CMP_STOP;
1310  }
1311 
1312  ast_channel_lock(chan);
1313  if (strcasecmp(ast_channel_context(chan), context) && strcasecmp(ast_channel_macrocontext(chan), context)) {
1314  ret = 0; /* Context match failed, continue */
1315  } else if (strcasecmp(ast_channel_exten(chan), exten) && strcasecmp(ast_channel_macroexten(chan), exten)) {
1316  ret = 0; /* Extension match failed, continue */
1317  }
1318  ast_channel_unlock(chan);
1319 
1320  return ret;
1321 }
1322 
1323 static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags)
1324 {
1325  struct ast_channel *chan = obj;
1326  char *uniqueid = arg;
1327  size_t id_len = *(size_t *) data;
1328  int ret = CMP_MATCH;
1329 
1330  if (ast_strlen_zero(uniqueid)) {
1331  ast_log(LOG_ERROR, "BUG! Must supply a uniqueid or partial uniqueid to match!\n");
1332  return CMP_STOP;
1333  }
1334 
1335  ast_channel_lock(chan);
1336  if ((!id_len && strcasecmp(ast_channel_uniqueid(chan), uniqueid))
1337  || (id_len && strncasecmp(ast_channel_uniqueid(chan), uniqueid, id_len))) {
1338  ret = 0; /* uniqueid match failed, keep looking */
1339  }
1340  ast_channel_unlock(chan);
1341 
1342  return ret;
1343 }
1344 
1346  /* storage for non-dynamically allocated iterator */
1348  /* pointer to the actual iterator (simple_iterator or a dynamically
1349  * allocated iterator)
1350  */
1352 };
1353 
1355 {
1357  ast_free(i);
1358 
1359  return NULL;
1360 }
1361 
1363 {
1364  struct ast_channel_iterator *i;
1365  char *l_exten = (char *) exten;
1366  char *l_context = (char *) context;
1367 
1368  if (!(i = ast_calloc(1, sizeof(*i)))) {
1369  return NULL;
1370  }
1371 
1373  l_context, l_exten, OBJ_MULTIPLE);
1374  if (!i->active_iterator) {
1375  ast_free(i);
1376  return NULL;
1377  }
1378 
1379  return i;
1380 }
1381 
1382 struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, size_t name_len)
1383 {
1384  struct ast_channel_iterator *i;
1385  char *l_name = (char *) name;
1386 
1387  if (!(i = ast_calloc(1, sizeof(*i)))) {
1388  return NULL;
1389  }
1390 
1392  l_name, &name_len,
1393  OBJ_MULTIPLE | (name_len == 0 /* match the whole word, so optimize */ ? OBJ_KEY : 0));
1394  if (!i->active_iterator) {
1395  ast_free(i);
1396  return NULL;
1397  }
1398 
1399  return i;
1400 }
1401 
1403 {
1404  struct ast_channel_iterator *i;
1405 
1406  if (!(i = ast_calloc(1, sizeof(*i)))) {
1407  return NULL;
1408  }
1409 
1412 
1413  return i;
1414 }
1415 
1417 {
1419 }
1420 
1421 /* Legacy function, not currently used for lookups, but we need a cmp_fn */
1422 static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
1423 {
1424  ast_log(LOG_ERROR, "BUG! Should never be called!\n");
1425  return CMP_STOP;
1426 }
1427 
1428 struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
1429 {
1430  struct ast_channel *chan;
1431  char *l_name = (char *) name;
1432 
1433  if (ast_strlen_zero(l_name)) {
1434  /* We didn't have a name to search for so quit. */
1435  return NULL;
1436  }
1437 
1438  chan = ast_channel_callback(ast_channel_by_name_cb, l_name, &name_len,
1439  (name_len == 0) /* optimize if it is a complete name match */ ? OBJ_KEY : 0);
1440  if (chan) {
1441  return chan;
1442  }
1443 
1444  /* Now try a search for uniqueid. */
1445  return ast_channel_callback(ast_channel_by_uniqueid_cb, l_name, &name_len, 0);
1446 }
1447 
1449 {
1451 }
1452 
1453 struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
1454 {
1455  char *l_exten = (char *) exten;
1456  char *l_context = (char *) context;
1457 
1458  return ast_channel_callback(ast_channel_by_exten_cb, l_context, l_exten, 0);
1459 }
1460 
1461 int ast_is_deferrable_frame(const struct ast_frame *frame)
1462 {
1463  /* Do not add a default entry in this switch statement. Each new
1464  * frame type should be addressed directly as to whether it should
1465  * be queued up or not.
1466  */
1467  switch (frame->frametype) {
1470  case AST_FRAME_CONTROL:
1471  case AST_FRAME_TEXT:
1472  case AST_FRAME_TEXT_DATA:
1473  case AST_FRAME_IMAGE:
1474  case AST_FRAME_HTML:
1475  return 1;
1476 
1477  case AST_FRAME_DTMF_END:
1478  case AST_FRAME_DTMF_BEGIN:
1479  case AST_FRAME_VOICE:
1480  case AST_FRAME_VIDEO:
1481  case AST_FRAME_NULL:
1482  case AST_FRAME_IAX:
1483  case AST_FRAME_CNG:
1484  case AST_FRAME_MODEM:
1485  case AST_FRAME_RTCP:
1486  return 0;
1487  }
1488  return 0;
1489 }
1490 
1491 /*! \brief Wait, look for hangups and condition arg */
1492 static int safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data, unsigned int generate_silence)
1493 {
1494  struct ast_frame *f;
1495  struct ast_silence_generator *silgen = NULL;
1496  int res = 0;
1497  struct timeval start;
1498  int ms;
1499  AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
1500 
1501  AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
1502 
1503  /* If no other generator is present, start silencegen while waiting */
1504  if (generate_silence && ast_opt_transmit_silence && !ast_channel_generatordata(chan)) {
1505  silgen = ast_channel_start_silence_generator(chan);
1506  }
1507 
1508  start = ast_tvnow();
1509  while ((ms = ast_remaining_ms(start, timeout_ms))) {
1510  struct ast_frame *dup_f = NULL;
1511 
1512  if (cond && ((*cond)(data) == 0)) {
1513  break;
1514  }
1515  ms = ast_waitfor(chan, ms);
1516  if (ms < 0) {
1517  res = -1;
1518  break;
1519  }
1520  if (ms > 0) {
1521  f = ast_read(chan);
1522  if (!f) {
1523  res = -1;
1524  break;
1525  }
1526 
1527  if (!ast_is_deferrable_frame(f)) {
1528  ast_frfree(f);
1529  continue;
1530  }
1531 
1532  if ((dup_f = ast_frisolate(f))) {
1533  if (dup_f != f) {
1534  ast_frfree(f);
1535  }
1536  AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
1537  }
1538  }
1539  }
1540 
1541  /* stop silgen if present */
1542  if (silgen) {
1543  ast_channel_stop_silence_generator(chan, silgen);
1544  }
1545 
1546  /* We need to free all the deferred frames, but we only need to
1547  * queue the deferred frames if there was no error and no
1548  * hangup was received
1549  */
1550  ast_channel_lock(chan);
1551  while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
1552  if (!res) {
1553  ast_queue_frame_head(chan, f);
1554  }
1555  ast_frfree(f);
1556  }
1557  ast_channel_unlock(chan);
1558 
1559  return res;
1560 }
1561 
1562 int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data)
1563 {
1564  return safe_sleep_conditional(chan, timeout_ms, cond, data, 1);
1565 }
1566 
1567 /*! \brief Wait, look for hangups */
1568 int ast_safe_sleep(struct ast_channel *chan, int ms)
1569 {
1570  return safe_sleep_conditional(chan, ms, NULL, NULL, 1);
1571 }
1572 
1574 {
1575  return safe_sleep_conditional(chan, ms, NULL, NULL, 0);
1576 }
1577 
1579 {
1580  /* Safe, even if already unlinked. */
1581  ao2_unlink(channels, chan);
1582  return ast_channel_unref(chan);
1583 }
1584 
1586 {
1587  init->str = NULL;
1590  init->valid = 0;
1591 }
1592 
1593 void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
1594 {
1595  if (dest == src) {
1596  /* Don't copy to self */
1597  return;
1598  }
1599 
1600  ast_free(dest->str);
1601  dest->str = ast_strdup(src->str);
1602  dest->char_set = src->char_set;
1603  dest->presentation = src->presentation;
1604  dest->valid = src->valid;
1605 }
1606 
1607 void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
1608 {
1609  init->str = NULL;
1610  init->char_set = guide->char_set;
1611  init->presentation = guide->presentation;
1612  init->valid = guide->valid;
1613 }
1614 
1615 void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
1616 {
1617  if (dest == src) {
1618  /* Don't set to self */
1619  return;
1620  }
1621 
1622  if (src->str && src->str != dest->str) {
1623  ast_free(dest->str);
1624  dest->str = ast_strdup(src->str);
1625  }
1626 
1627  dest->char_set = src->char_set;
1628  dest->presentation = src->presentation;
1629  dest->valid = src->valid;
1630 }
1631 
1633 {
1634  ast_free(doomed->str);
1635  doomed->str = NULL;
1636 }
1637 
1639 {
1640  init->str = NULL;
1641  init->plan = 0;/* Unknown */
1643  init->valid = 0;
1644 }
1645 
1646 void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
1647 {
1648  if (dest == src) {
1649  /* Don't copy to self */
1650  return;
1651  }
1652 
1653  ast_free(dest->str);
1654  dest->str = ast_strdup(src->str);
1655  dest->plan = src->plan;
1656  dest->presentation = src->presentation;
1657  dest->valid = src->valid;
1658 }
1659 
1660 void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
1661 {
1662  init->str = NULL;
1663  init->plan = guide->plan;
1664  init->presentation = guide->presentation;
1665  init->valid = guide->valid;
1666 }
1667 
1668 void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
1669 {
1670  if (dest == src) {
1671  /* Don't set to self */
1672  return;
1673  }
1674 
1675  if (src->str && src->str != dest->str) {
1676  ast_free(dest->str);
1677  dest->str = ast_strdup(src->str);
1678  }
1679 
1680  dest->plan = src->plan;
1681  dest->presentation = src->presentation;
1682  dest->valid = src->valid;
1683 }
1684 
1686 {
1687  ast_free(doomed->str);
1688  doomed->str = NULL;
1689 }
1690 
1692 {
1693  init->str = NULL;
1694  init->type = 0;
1695  init->odd_even_indicator = 0;
1696  init->valid = 0;
1697 }
1698 
1700 {
1701  if (dest == src) {
1702  /* Don't copy to self */
1703  return;
1704  }
1705 
1706  ast_free(dest->str);
1707  dest->str = ast_strdup(src->str);
1708  dest->type = src->type;
1710  dest->valid = src->valid;
1711 }
1712 
1714 {
1715  init->str = NULL;
1716  init->type = guide->type;
1717  init->odd_even_indicator = guide->odd_even_indicator;
1718  init->valid = guide->valid;
1719 }
1720 
1722 {
1723  if (dest == src) {
1724  /* Don't set to self */
1725  return;
1726  }
1727 
1728  if (src->str && src->str != dest->str) {
1729  ast_free(dest->str);
1730  dest->str = ast_strdup(src->str);
1731  }
1732 
1733  dest->type = src->type;
1735  dest->valid = src->valid;
1736 }
1737 
1739 {
1740  ast_free(doomed->str);
1741  doomed->str = NULL;
1742 }
1743 
1745 {
1746  update_id->name = 1;
1747  update_id->number = 1;
1748  update_id->subaddress = 1;
1749 }
1750 
1752 {
1753  ast_party_name_init(&init->name);
1754  ast_party_number_init(&init->number);
1756  init->tag = NULL;
1757 }
1758 
1759 void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
1760 {
1761  if (dest == src) {
1762  /* Don't copy to self */
1763  return;
1764  }
1765 
1766  ast_party_name_copy(&dest->name, &src->name);
1767  ast_party_number_copy(&dest->number, &src->number);
1769 
1770  ast_free(dest->tag);
1771  dest->tag = ast_strdup(src->tag);
1772 }
1773 
1774 void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
1775 {
1776  ast_party_name_set_init(&init->name, &guide->name);
1777  ast_party_number_set_init(&init->number, &guide->number);
1779  init->tag = NULL;
1780 }
1781 
1782 void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
1783 {
1784  if (dest == src) {
1785  /* Don't set to self */
1786  return;
1787  }
1788 
1789  if (!update || update->name) {
1790  ast_party_name_set(&dest->name, &src->name);
1791  }
1792  if (!update || update->number) {
1793  ast_party_number_set(&dest->number, &src->number);
1794  }
1795  if (!update || update->subaddress) {
1797  }
1798 
1799  if (src->tag && src->tag != dest->tag) {
1800  ast_free(dest->tag);
1801  dest->tag = ast_strdup(src->tag);
1802  }
1803 }
1804 
1805 void ast_party_id_free(struct ast_party_id *doomed)
1806 {
1807  ast_party_name_free(&doomed->name);
1808  ast_party_number_free(&doomed->number);
1810 
1811  ast_free(doomed->tag);
1812  doomed->tag = NULL;
1813 }
1814 
1816 {
1817  int number_priority;
1818  int number_value;
1819  int number_screening;
1820  int name_priority;
1821  int name_value;
1822 
1823  /* Determine name presentation priority. */
1824  if (!id->name.valid) {
1825  name_value = AST_PRES_UNAVAILABLE;
1826  name_priority = 3;
1827  } else {
1828  name_value = id->name.presentation & AST_PRES_RESTRICTION;
1829  switch (name_value) {
1830  case AST_PRES_RESTRICTED:
1831  name_priority = 0;
1832  break;
1833  case AST_PRES_ALLOWED:
1834  name_priority = 1;
1835  break;
1836  case AST_PRES_UNAVAILABLE:
1837  name_priority = 2;
1838  break;
1839  default:
1840  name_value = AST_PRES_UNAVAILABLE;
1841  name_priority = 3;
1842  break;
1843  }
1844  }
1845 
1846  /* Determine number presentation priority. */
1847  if (!id->number.valid) {
1848  number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1849  number_value = AST_PRES_UNAVAILABLE;
1850  number_priority = 3;
1851  } else {
1852  number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
1853  number_value = id->number.presentation & AST_PRES_RESTRICTION;
1854  switch (number_value) {
1855  case AST_PRES_RESTRICTED:
1856  number_priority = 0;
1857  break;
1858  case AST_PRES_ALLOWED:
1859  number_priority = 1;
1860  break;
1861  case AST_PRES_UNAVAILABLE:
1862  number_priority = 2;
1863  break;
1864  default:
1865  number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1866  number_value = AST_PRES_UNAVAILABLE;
1867  number_priority = 3;
1868  break;
1869  }
1870  }
1871 
1872  /* Select the wining presentation value. */
1873  if (name_priority < number_priority) {
1874  number_value = name_value;
1875  }
1876  if (number_value == AST_PRES_UNAVAILABLE) {
1878  }
1879 
1880  return number_value | number_screening;
1881 }
1882 
1884 {
1885  id->name.valid = 0;
1886  id->number.valid = 0;
1887  id->subaddress.valid = 0;
1888 }
1889 
1891 {
1892  ast_party_id_free(id);
1893  ast_party_id_init(id);
1894 }
1895 
1896 struct ast_party_id ast_party_id_merge(struct ast_party_id *base, struct ast_party_id *overlay)
1897 {
1898  struct ast_party_id merged;
1899 
1900  merged = *base;
1901  if (overlay->name.valid) {
1902  merged.name = overlay->name;
1903  }
1904  if (overlay->number.valid) {
1905  merged.number = overlay->number;
1906  }
1907  if (overlay->subaddress.valid) {
1908  merged.subaddress = overlay->subaddress;
1909  }
1910  /* Note the actual structure is returned and not a pointer to it! */
1911  return merged;
1912 }
1913 
1914 void ast_party_id_merge_copy(struct ast_party_id *dest, struct ast_party_id *base, struct ast_party_id *overlay)
1915 {
1916  struct ast_party_id merged;
1917 
1918  merged = ast_party_id_merge(base, overlay);
1919  ast_party_id_copy(dest, &merged);
1920 }
1921 
1923 {
1924  init->number.str = NULL;
1925  init->number.plan = 0;/* Unknown */
1927  init->transit_network_select = 0;
1928 }
1929 
1930 void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1931 {
1932  if (dest == src) {
1933  /* Don't copy to self */
1934  return;
1935  }
1936 
1937  ast_free(dest->number.str);
1938  dest->number.str = ast_strdup(src->number.str);
1939  dest->number.plan = src->number.plan;
1942 }
1943 
1944 void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
1945 {
1946  init->number.str = NULL;
1947  init->number.plan = guide->number.plan;
1950 }
1951 
1952 void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1953 {
1954  if (src->number.str && src->number.str != dest->number.str) {
1955  ast_free(dest->number.str);
1956  dest->number.str = ast_strdup(src->number.str);
1957  }
1958  dest->number.plan = src->number.plan;
1959 
1961 
1963 }
1964 
1966 {
1967  ast_free(doomed->number.str);
1968  doomed->number.str = NULL;
1970 }
1971 
1973 {
1974  ast_party_id_init(&init->id);
1975  ast_party_id_init(&init->ani);
1976  ast_party_id_init(&init->priv);
1977  init->ani2 = 0;
1978 }
1979 
1980 void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
1981 {
1982  if (dest == src) {
1983  /* Don't copy to self */
1984  return;
1985  }
1986 
1987  ast_party_id_copy(&dest->id, &src->id);
1988  ast_party_id_copy(&dest->ani, &src->ani);
1989  ast_party_id_copy(&dest->priv, &src->priv);
1990  dest->ani2 = src->ani2;
1991 }
1992 
1993 void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
1994 {
1995  ast_party_id_set_init(&init->id, &guide->id);
1996  ast_party_id_set_init(&init->ani, &guide->ani);
1997  ast_party_id_set_init(&init->priv, &guide->priv);
1998  init->ani2 = guide->ani2;
1999 }
2000 
2001 void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
2002 {
2003  ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2004  ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2005  ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2006  dest->ani2 = src->ani2;
2007 }
2008 
2010 {
2011  ast_party_id_free(&doomed->id);
2012  ast_party_id_free(&doomed->ani);
2013  ast_party_id_free(&doomed->priv);
2014 }
2015 
2017 {
2018  ast_party_id_init(&init->id);
2019  ast_party_id_init(&init->ani);
2020  ast_party_id_init(&init->priv);
2021  init->ani2 = 0;
2023 }
2024 
2026 {
2027  if (dest == src) {
2028  /* Don't copy to self */
2029  return;
2030  }
2031 
2032  ast_party_id_copy(&dest->id, &src->id);
2033  ast_party_id_copy(&dest->ani, &src->ani);
2034  ast_party_id_copy(&dest->priv, &src->priv);
2035  dest->ani2 = src->ani2;
2036  dest->source = src->source;
2037 }
2038 
2040 {
2041  ast_party_id_set_init(&init->id, &guide->id);
2042  ast_party_id_set_init(&init->ani, &guide->ani);
2043  ast_party_id_set_init(&init->priv, &guide->priv);
2044  init->ani2 = guide->ani2;
2045  init->source = guide->source;
2046 }
2047 
2049 {
2050  ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2051  ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2052  ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2053  dest->ani2 = src->ani2;
2054  dest->source = src->source;
2055 }
2056 
2058 {
2059  connected->id = caller->id;
2060  connected->ani = caller->ani;
2061  connected->priv = caller->priv;
2062  connected->ani2 = caller->ani2;
2064 }
2065 
2067 {
2068  ast_party_id_free(&doomed->id);
2069  ast_party_id_free(&doomed->ani);
2070  ast_party_id_free(&doomed->priv);
2071 }
2072 
2074 {
2075  init->str = NULL;
2077 }
2078 
2080 {
2081  if (dest == src) {
2082  return;
2083  }
2084 
2085  ast_free(dest->str);
2086  dest->str = ast_strdup(src->str);
2087  dest->code = src->code;
2088 }
2089 
2091 {
2092  init->str = NULL;
2093  init->code = guide->code;
2094 }
2095 
2097 {
2098  if (dest == src) {
2099  return;
2100  }
2101 
2102  if (src->str && src->str != dest->str) {
2103  ast_free(dest->str);
2104  dest->str = ast_strdup(src->str);
2105  }
2106 
2107  dest->code = src->code;
2108 }
2109 
2111 {
2112  ast_free(doomed->str);
2113 }
2114 
2115 
2117 {
2118  ast_party_id_init(&init->orig);
2119  ast_party_id_init(&init->from);
2120  ast_party_id_init(&init->to);
2121  ast_party_id_init(&init->priv_orig);
2122  ast_party_id_init(&init->priv_from);
2123  ast_party_id_init(&init->priv_to);
2126  init->count = 0;
2127 }
2128 
2130 {
2131  if (dest == src) {
2132  /* Don't copy to self */
2133  return;
2134  }
2135 
2136  ast_party_id_copy(&dest->orig, &src->orig);
2137  ast_party_id_copy(&dest->from, &src->from);
2138  ast_party_id_copy(&dest->to, &src->to);
2139  ast_party_id_copy(&dest->priv_orig, &src->priv_orig);
2140  ast_party_id_copy(&dest->priv_from, &src->priv_from);
2141  ast_party_id_copy(&dest->priv_to, &src->priv_to);
2144  dest->count = src->count;
2145 }
2146 
2148 {
2149  ast_party_id_set_init(&init->orig, &guide->orig);
2150  ast_party_id_set_init(&init->from, &guide->from);
2151  ast_party_id_set_init(&init->to, &guide->to);
2152  ast_party_id_set_init(&init->priv_orig, &guide->priv_orig);
2153  ast_party_id_set_init(&init->priv_from, &guide->priv_from);
2154  ast_party_id_set_init(&init->priv_to, &guide->priv_to);
2157  init->count = guide->count;
2158 }
2159 
2161 {
2162  ast_party_id_set(&dest->orig, &src->orig, update ? &update->orig : NULL);
2163  ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
2164  ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
2165  ast_party_id_set(&dest->priv_orig, &src->priv_orig, update ? &update->priv_orig : NULL);
2166  ast_party_id_set(&dest->priv_from, &src->priv_from, update ? &update->priv_from : NULL);
2167  ast_party_id_set(&dest->priv_to, &src->priv_to, update ? &update->priv_to : NULL);
2170  dest->count = src->count;
2171 }
2172 
2174 {
2175  ast_party_id_free(&doomed->orig);
2176  ast_party_id_free(&doomed->from);
2177  ast_party_id_free(&doomed->to);
2178  ast_party_id_free(&doomed->priv_orig);
2179  ast_party_id_free(&doomed->priv_from);
2180  ast_party_id_free(&doomed->priv_to);
2183 }
2184 
2185 /*! \brief Free a channel structure */
2186 static void ast_channel_destructor(void *obj)
2187 {
2188  struct ast_channel *chan = obj;
2189  struct ast_var_t *vardata;
2190  struct ast_frame *f;
2191  struct varshead *headp;
2192  struct ast_datastore *datastore;
2193  char device_name[AST_CHANNEL_NAME];
2194  ast_callid callid;
2195 
2196  ast_debug(1, "Channel %p '%s' destroying\n", chan, ast_channel_name(chan));
2197 
2198  /* Stop monitoring */
2199  if (ast_channel_monitor(chan)) {
2200  ast_channel_monitor(chan)->stop(chan, 0);
2201  }
2202 
2203  /* If there is native format music-on-hold state, free it */
2204  if (ast_channel_music_state(chan)) {
2205  ast_moh_cleanup(chan);
2206  }
2207 
2209 
2210  /* Things that may possibly raise Stasis messages shouldn't occur after this point */
2212 
2214  /* A channel snapshot should not be in the process of being staged now. */
2216 
2217  ast_channel_lock(chan);
2219  ast_channel_unlock(chan);
2220  }
2221 
2222  ast_channel_lock(chan);
2223 
2224  /* Get rid of each of the data stores on the channel */
2225  while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry)))
2226  /* Free the data store */
2227  ast_datastore_free(datastore);
2228 
2229  /* While the channel is locked, take the reference to its callid while we tear down the call. */
2230  callid = ast_channel_callid(chan);
2232 
2233  ast_channel_unlock(chan);
2234 
2235  /* Lock and unlock the channel just to be sure nobody has it locked still
2236  due to a reference that was stored in a datastore. (i.e. app_chanspy) */
2237  ast_channel_lock(chan);
2238  ast_channel_unlock(chan);
2239 
2240  if (ast_channel_tech_pvt(chan)) {
2241  ast_log_callid(LOG_WARNING, callid, "Channel '%s' may not have been hung up properly\n", ast_channel_name(chan));
2243  }
2244 
2245  if (ast_channel_sched(chan)) {
2247  }
2248 
2250  char *dashptr;
2251 
2252  ast_copy_string(device_name, ast_channel_name(chan), sizeof(device_name));
2253  if ((dashptr = strrchr(device_name, '-'))) {
2254  *dashptr = '\0';
2255  }
2256  } else {
2257  device_name[0] = '\0';
2258  }
2259 
2260  /* Free translators */
2261  if (ast_channel_readtrans(chan))
2263  if (ast_channel_writetrans(chan))
2265  if (ast_channel_pbx(chan))
2266  ast_log_callid(LOG_WARNING, callid, "PBX may not have been terminated properly on '%s'\n", ast_channel_name(chan));
2267 
2268  /* Free formats */
2274 
2280 
2281  /* Close pipes if appropriate */
2283  if (ast_channel_timer(chan)) {
2285  ast_channel_timer_set(chan, NULL);
2286  }
2287  while ((f = AST_LIST_REMOVE_HEAD(ast_channel_readq(chan), frame_list)))
2288  ast_frfree(f);
2289 
2290  /* loop over the variables list, freeing all data and deleting list items */
2291  /* no need to lock the list, as the channel is already locked */
2292  headp = ast_channel_varshead(chan);
2293  while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2294  ast_var_delete(vardata);
2295 
2296  ast_app_group_discard(chan);
2297 
2298  /* Destroy the jitterbuffer */
2299  ast_jb_destroy(chan);
2300 
2301  if (ast_channel_cdr(chan)) {
2303  ast_channel_cdr_set(chan, NULL);
2304  }
2305 
2306  if (ast_channel_zone(chan)) {
2308  }
2309 
2311 
2312  if (device_name[0]) {
2313  /*
2314  * We have a device name to notify of a new state.
2315  *
2316  * Queue an unknown state, because, while we know that this particular
2317  * instance is dead, we don't know the state of all other possible
2318  * instances.
2319  */
2321  }
2322 
2324 
2327 
2329 }
2330 
2331 /*! \brief Free a dummy channel structure */
2332 static void ast_dummy_channel_destructor(void *obj)
2333 {
2334  struct ast_channel *chan = obj;
2335  struct ast_datastore *datastore;
2336  struct ast_var_t *vardata;
2337  struct varshead *headp;
2338 
2340 
2341  /* Get rid of each of the data stores on the channel */
2342  while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) {
2343  /* Free the data store */
2344  ast_datastore_free(datastore);
2345  }
2346 
2352 
2353  /* loop over the variables list, freeing all data and deleting list items */
2354  /* no need to lock the list, as the channel is already locked */
2355  headp = ast_channel_varshead(chan);
2356  while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2357  ast_var_delete(vardata);
2358 
2359  if (ast_channel_cdr(chan)) {
2361  ast_channel_cdr_set(chan, NULL);
2362  }
2363 
2365 }
2366 
2368 {
2369  struct ast_datastore *datastore = NULL, *datastore2;
2370 
2371  AST_LIST_TRAVERSE(ast_channel_datastores(from), datastore, entry) {
2372  if (datastore->inheritance > 0) {
2373  datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
2374  if (datastore2) {
2375  datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
2376  datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
2378  }
2379  }
2380  }
2381  return 0;
2382 }
2383 
2384 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
2385 {
2386  int res = 0;
2387 
2389 
2390  return res;
2391 }
2392 
2393 int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
2394 {
2395  return AST_LIST_REMOVE(ast_channel_datastores(chan), datastore, entry) ? 0 : -1;
2396 }
2397 
2398 struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
2399 {
2400  struct ast_datastore *datastore = NULL;
2401 
2402  if (info == NULL)
2403  return NULL;
2404 
2405  AST_LIST_TRAVERSE(ast_channel_datastores(chan), datastore, entry) {
2406  if (datastore->info != info) {
2407  continue;
2408  }
2409 
2410  if (uid == NULL) {
2411  /* matched by type only */
2412  break;
2413  }
2414 
2415  if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
2416  /* Matched by type AND uid */
2417  break;
2418  }
2419  }
2420 
2421  return datastore;
2422 }
2423 
2424 /*! Set the file descriptor on the channel */
2425 void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
2426 {
2427  ast_channel_internal_fd_set(chan, which, fd);
2428  return;
2429 }
2430 
2432 {
2433  ast_channel_lock(chan);
2434 
2436 
2438  struct ast_frame *fr;
2439 
2440  /* If we have completely cleared the softhangup flag,
2441  * then we need to fully abort the hangup process. This requires
2442  * pulling the END_OF_Q frame out of the channel frame queue if it
2443  * still happens to be there. */
2444 
2445  fr = AST_LIST_LAST(ast_channel_readq(chan));
2446  if (fr && fr->frametype == AST_FRAME_CONTROL &&
2449  ast_frfree(fr);
2450  }
2451  }
2452 
2453  ast_channel_unlock(chan);
2454 }
2455 
2456 /*! \brief Softly hangup a channel, don't lock */
2457 int ast_softhangup_nolock(struct ast_channel *chan, int cause)
2458 {
2459  ast_debug(1, "Soft-Hanging (%#04x) up channel '%s'\n", (unsigned)cause, ast_channel_name(chan));
2460  /* Inform channel driver that we need to be hung up, if it cares */
2463  /* Interrupt any poll call or such */
2465  pthread_kill(ast_channel_blocker(chan), SIGURG);
2466  return 0;
2467 }
2468 
2469 /*! \brief Softly hangup a channel, lock */
2470 int ast_softhangup(struct ast_channel *chan, int cause)
2471 {
2472  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2473  int res;
2474 
2475  ast_channel_lock(chan);
2476  res = ast_softhangup_nolock(chan, cause);
2477  blob = ast_json_pack("{s: i, s: b}",
2478  "cause", cause,
2479  "soft", 1);
2481  ast_channel_unlock(chan);
2482 
2483  return res;
2484 }
2485 
2486 static void free_translation(struct ast_channel *clonechan)
2487 {
2488  if (ast_channel_writetrans(clonechan)) {
2490  }
2491  if (ast_channel_readtrans(clonechan)) {
2493  }
2494  ast_channel_writetrans_set(clonechan, NULL);
2495  ast_channel_readtrans_set(clonechan, NULL);
2496 }
2497 
2498 void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
2499 {
2501 
2502  ast_channel_lock(chan);
2503  if (force || ast_strlen_zero(ast_channel_hangupsource(chan))) {
2504  ast_channel_hangupsource_set(chan, source);
2505  }
2506  ast_channel_unlock(chan);
2507 
2508  if (bridge) {
2509  ast_channel_lock(bridge);
2510  if (force || ast_strlen_zero(ast_channel_hangupsource(bridge))) {
2511  ast_channel_hangupsource_set(bridge, source);
2512  }
2513  ast_channel_unlock(bridge);
2514  }
2515 }
2516 
2518 {
2519  return ast_channel_monitor(chan)
2522 }
2523 
2525 {
2526  return ast_channel_monitor(chan)
2529 }
2530 
2531 static void destroy_hooks(struct ast_channel *chan)
2532 {
2533  if (ast_channel_audiohooks(chan)) {
2536  }
2537 
2539 }
2540 
2541 /*! \brief Hangup a channel */
2542 void ast_hangup(struct ast_channel *chan)
2543 {
2544  /* Be NULL safe for RAII_VAR() usage. */
2545  if (!chan) {
2546  return;
2547  }
2548 
2549  ast_debug(1, "Channel %p '%s' hanging up. Refs: %d\n", chan, ast_channel_name(chan),
2550  ao2_ref(chan, 0));
2551 
2552  ast_autoservice_stop(chan);
2553 
2554  ast_channel_lock(chan);
2555 
2556  while (ast_channel_masq(chan) || ast_channel_masqr(chan)) {
2558  }
2559 
2560  /* Mark as a zombie so a masquerade cannot be setup on this channel. */
2562 
2563  ast_channel_unlock(chan);
2564 
2565  /*
2566  * XXX if running the hangup handlers here causes problems
2567  * because the handlers take too long to execute, we could move
2568  * the meat of this function into another thread. A thread
2569  * where channels go to die.
2570  *
2571  * If this is done, ast_autoservice_chan_hangup_peer() will no
2572  * longer be needed.
2573  */
2575  ao2_unlink(channels, chan);
2576  ast_channel_lock(chan);
2577 
2578  destroy_hooks(chan);
2579 
2580  free_translation(chan);
2581  /* Close audio stream */
2582  if (ast_channel_stream(chan)) {
2585  }
2586  /* Close video stream */
2587  if (ast_channel_vstream(chan)) {
2590  }
2591  if (ast_channel_sched(chan)) {
2593  ast_channel_sched_set(chan, NULL);
2594  }
2595 
2596  if (ast_channel_generatordata(chan)) { /* Clear any tone stuff remaining */
2597  if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2599  }
2600  }
2603 
2605  ast_log(LOG_WARNING, "Hard hangup called by thread LWP %d on %s, while blocked by thread LWP %d in procedure %s! Expect a failure\n",
2607  ast_channel_blockproc(chan));
2608  ast_assert(0);
2609  }
2610 
2611  if (ast_channel_tech(chan)->hangup) {
2612  ast_channel_tech(chan)->hangup(chan);
2613  }
2614 
2615  ast_channel_unlock(chan);
2616 
2617  ast_cc_offer(chan);
2618 
2619  ast_channel_unref(chan);
2620 }
2621 
2622 /*!
2623  * \internal
2624  * \brief Set channel answered time if not already set.
2625  * \since 13.11.0
2626  *
2627  * \param chan Channel to set answered time.
2628  */
2629 static void set_channel_answer_time(struct ast_channel *chan)
2630 {
2631  if (ast_tvzero(ast_channel_answertime(chan))) {
2632  struct timeval answertime;
2633 
2634  answertime = ast_tvnow();
2635  ast_channel_answertime_set(chan, &answertime);
2636  }
2637 }
2638 
2639 
2641 {
2642  int res = 0;
2643  SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2644 
2645  ast_channel_lock(chan);
2646 
2647  /* You can't answer an outbound call */
2649  ast_channel_unlock(chan);
2650  return 0;
2651  }
2652 
2653  /* Stop if we're a zombie or need a soft hangup */
2655  ast_channel_unlock(chan);
2656  return -1;
2657  }
2658 
2659  /*
2660  * Mark when incoming channel answered so we can know how
2661  * long the channel has been up.
2662  */
2664 
2665  ast_channel_unlock(chan);
2666 
2667  switch (ast_channel_state(chan)) {
2668  case AST_STATE_RINGING:
2669  case AST_STATE_RING:
2670  ast_channel_lock(chan);
2671  if (ast_channel_tech(chan)->answer_with_stream_topology) {
2672  res = ast_channel_tech(chan)->answer_with_stream_topology(chan, topology);
2673 
2674  } else if (ast_channel_tech(chan)->answer) {
2675  res = ast_channel_tech(chan)->answer(chan);
2676  }
2677  ast_setstate(chan, AST_STATE_UP);
2678  ast_channel_unlock(chan);
2679  break;
2680  case AST_STATE_UP:
2681  break;
2682  default:
2683  break;
2684  }
2685 
2686  ast_indicate(chan, -1);
2687 
2688  return res;
2689 }
2690 
2691 int ast_raw_answer(struct ast_channel *chan)
2692 {
2694 }
2695 
2696 int __ast_answer(struct ast_channel *chan, unsigned int delay)
2697 {
2698  int res = 0;
2699  enum ast_channel_state old_state;
2700  SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2701 
2702  old_state = ast_channel_state(chan);
2703  if ((res = ast_raw_answer(chan))) {
2704  return res;
2705  }
2706 
2707  switch (old_state) {
2708  case AST_STATE_RINGING:
2709  case AST_STATE_RING:
2710  /* wait for media to start flowing, but don't wait any longer
2711  * than 'delay' or 500 milliseconds, whichever is longer
2712  */
2713  do {
2715  struct ast_frame *cur;
2716  struct ast_frame *new_frame;
2717  int timeout_ms = MAX(delay, 500);
2718  unsigned int done = 0;
2719  struct timeval start;
2720 
2722 
2723  start = ast_tvnow();
2724  for (;;) {
2725  int ms = ast_remaining_ms(start, timeout_ms);
2726  ms = ast_waitfor(chan, ms);
2727  if (ms < 0) {
2728  ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", ast_channel_name(chan), strerror(errno));
2729  res = -1;
2730  break;
2731  }
2732  if (ms == 0) {
2733  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));
2734  break;
2735  }
2736  cur = ast_read(chan);
2737  if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
2738  (cur->subclass.integer == AST_CONTROL_HANGUP))) {
2739  if (cur) {
2740  ast_frfree(cur);
2741  }
2742  res = -1;
2743  ast_debug(2, "Hangup of channel %s detected in answer routine\n", ast_channel_name(chan));
2744  break;
2745  }
2746 
2747  if ((new_frame = ast_frisolate(cur)) != cur) {
2748  ast_frfree(cur);
2749  }
2750 
2751  AST_LIST_INSERT_HEAD(&frames, new_frame, frame_list);
2752 
2753  /* if a specific delay period was requested, continue
2754  * until that delay has passed. don't stop just because
2755  * incoming media has arrived.
2756  */
2757  if (delay) {
2758  continue;
2759  }
2760 
2761  switch (new_frame->frametype) {
2762  /* all of these frametypes qualify as 'media' */
2763  case AST_FRAME_VOICE:
2764  case AST_FRAME_VIDEO:
2765  case AST_FRAME_TEXT:
2766  case AST_FRAME_TEXT_DATA:
2767  case AST_FRAME_DTMF_BEGIN:
2768  case AST_FRAME_DTMF_END:
2769  case AST_FRAME_IMAGE:
2770  case AST_FRAME_HTML:
2771  case AST_FRAME_MODEM:
2772  case AST_FRAME_RTCP:
2773  done = 1;
2774  break;
2775  case AST_FRAME_CONTROL:
2776  case AST_FRAME_IAX:
2779  case AST_FRAME_NULL:
2780  case AST_FRAME_CNG:
2781  break;
2782  }
2783 
2784  if (done) {
2785  break;
2786  }
2787  }
2788 
2789  ast_channel_lock(chan);
2790  while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
2791  if (res == 0) {
2792  ast_queue_frame_head(chan, cur);
2793  }
2794  ast_frfree(cur);
2795  }
2796  ast_channel_unlock(chan);
2797  } while (0);
2798  break;
2799  default:
2800  break;
2801  }
2802 
2803  return res;
2804 }
2805 
2806 int ast_answer(struct ast_channel *chan)
2807 {
2808  SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2809  return __ast_answer(chan, 0);
2810 }
2811 
2812 inline int ast_auto_answer(struct ast_channel *chan)
2813 {
2814  if (ast_channel_state(chan) == AST_STATE_UP) {
2815  /* Already answered */
2816  return 0;
2817  }
2818  return ast_answer(chan);
2819 }
2820 
2822 {
2823  ast_assert(NULL != chan);
2824 
2825  if (ast_tvzero(ast_channel_creationtime(chan))) {
2826  return 0;
2827  }
2829 }
2830 
2832 {
2833  return (ast_channel_get_duration_ms(chan) / 1000);
2834 }
2835 
2837 {
2838  ast_assert(NULL != chan);
2839 
2840  if (ast_tvzero(ast_channel_answertime(chan))) {
2841  return 0;
2842  }
2844 }
2845 
2847 {
2848  return (ast_channel_get_up_time_ms(chan) / 1000);
2849 }
2850 
2851 /*!
2852  * \brief Determine whether or not we have to trigger dtmf emulating using 50 fps timer events
2853  * especially when no voice frames are received during dtmf processing (direct media or muted
2854  * sender case using SIP INFO)
2855  */
2856 static inline int should_trigger_dtmf_emulating(struct ast_channel *chan)
2857 {
2859  /* We're in the middle of emulating a digit, or DTMF has been
2860  * explicitly deferred. Trigger dtmf with periodic 50 pfs timer events, then. */
2861  return 1;
2862  }
2863 
2864  if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
2866  /*
2867  * We're not in the middle of a digit, but it hasn't been long enough
2868  * since the last digit, so we'll have to trigger DTMF further on.
2869  * Using 2 times AST_MIN_DTMF_GAP to trigger readq reading for possible
2870  * buffered next dtmf event
2871  */
2872  return 1;
2873  }
2874 
2875  return 0;
2876 }
2877 
2878 static void deactivate_generator_nolock(struct ast_channel *chan)
2879 {
2880  if (ast_channel_generatordata(chan)) {
2881  struct ast_generator *generator = ast_channel_generator(chan);
2882 
2883  if (generator && generator->release) {
2884  generator->release(chan, ast_channel_generatordata(chan));
2885  }
2890  ast_settimeout(chan, 0, NULL, NULL);
2891  }
2892 }
2893 
2895 {
2896  ast_channel_lock(chan);
2898  if (should_trigger_dtmf_emulating(chan)) {
2899  /* if in the middle of dtmf emulation keep 50 tick per sec timer on rolling */
2901  }
2902  ast_channel_unlock(chan);
2903 }
2904 
2906 {
2907  struct ast_generator *generator;
2908 
2909  ast_channel_lock(chan);
2910  generator = ast_channel_generator(chan);
2911  if (generator && generator->write_format_change) {
2912  generator->write_format_change(chan, ast_channel_generatordata(chan));
2913  }
2914  ast_channel_unlock(chan);
2915 }
2916 
2917 static int generator_force(const void *data)
2918 {
2919  /* Called if generator doesn't have data */
2920  void *tmp;
2921  int res;
2922  int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
2923  struct ast_channel *chan = (struct ast_channel *)data;
2924 
2925  ast_channel_lock(chan);
2928  if (ast_channel_generator(chan))
2929  generate = ast_channel_generator(chan)->generate;
2930  ast_channel_unlock(chan);
2931 
2932  if (!tmp || !generate) {
2933  return 0;
2934  }
2935 
2936  res = generate(chan, tmp, 0, ast_format_get_sample_rate(ast_channel_writeformat(chan)) / 50);
2937 
2938  ast_channel_lock(chan);
2939  if (ast_channel_generator(chan) && generate == ast_channel_generator(chan)->generate) {
2941  }
2942  ast_channel_unlock(chan);
2943 
2944  if (res) {
2945  ast_debug(1, "Auto-deactivating generator\n");
2947  }
2948 
2949  return 0;
2950 }
2951 
2952 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
2953 {
2954  int res = 0;
2955  void *generatordata = NULL;
2956 
2957  ast_channel_lock(chan);
2958  if (ast_channel_generatordata(chan)) {
2959  struct ast_generator *generator_old = ast_channel_generator(chan);
2960 
2961  if (generator_old && generator_old->release) {
2962  generator_old->release(chan, ast_channel_generatordata(chan));
2963  }
2964  }
2965  if (gen->alloc && !(generatordata = gen->alloc(chan, params))) {
2966  res = -1;
2967  }
2968  ast_channel_generatordata_set(chan, generatordata);
2969  if (!res) {
2970  ast_settimeout(chan, 50, generator_force, chan);
2972  }
2973  ast_channel_unlock(chan);
2974 
2975  ast_prod(chan);
2976 
2977  return res;
2978 }
2979 
2980 /*! \brief Wait for x amount of time on a file descriptor to have input. */
2981 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
2982 {
2983  int winner = -1;
2984  ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
2985  return winner;
2986 }
2987 
2988 /*! \brief Wait for x amount of time on a file descriptor to have input. */
2989 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
2990  int *exception, int *outfd, int *ms)
2991 {
2992  struct timeval start = { 0 , 0 };
2993  struct pollfd *pfds = NULL;
2994  int res;
2995  long rms;
2996  int x, y, max;
2997  int sz = nfds;
2998  struct timeval now = { 0, 0 };
2999  struct timeval whentohangup = { 0, 0 }, diff;
3000  struct ast_channel *winner = NULL;
3001  struct fdmap {
3002  int chan;
3003  int fdno;
3004  } *fdmap = NULL;
3005 
3006  if (outfd) {
3007  *outfd = -99999;
3008  }
3009  if (exception) {
3010  *exception = 0;
3011  }
3012 
3013  for (x = 0; x < n; x++) {
3014  ast_channel_lock(c[x]);
3015  if (!ast_tvzero(*ast_channel_whentohangup(c[x]))) {
3016  if (ast_tvzero(whentohangup))
3017  now = ast_tvnow();
3018  diff = ast_tvsub(*ast_channel_whentohangup(c[x]), now);
3019  if (diff.tv_sec < 0 || ast_tvzero(diff)) {
3020  ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3021  /* Should already be hungup */
3023  ast_channel_unlock(c[x]);
3024  return c[x];
3025  }
3026  if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
3027  whentohangup = diff;
3028  }
3029  sz += ast_channel_fd_count(c[x]);
3030  ast_channel_unlock(c[x]);
3031  }
3032 
3033  if (!sz) {
3034  return NULL;
3035  }
3036 
3037  pfds = ast_alloca(sizeof(*pfds) * sz);
3038  fdmap = ast_alloca(sizeof(*fdmap) * sz);
3039 
3040  /* Wait full interval */
3041  rms = *ms;
3042  /* INT_MAX, not LONG_MAX, because it matters on 64-bit */
3043  if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
3044  rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000; /* timeout in milliseconds */
3045  if (*ms >= 0 && *ms < rms) { /* original *ms still smaller */
3046  rms = *ms;
3047  }
3048  } else if (!ast_tvzero(whentohangup) && rms < 0) {
3049  /* Tiny corner case... call would need to last >24 days */
3050  rms = INT_MAX;
3051  }
3052  /*
3053  * Build the pollfd array, putting the channels' fds first,
3054  * followed by individual fds. Order is important because
3055  * individual fd's must have priority over channel fds.
3056  */
3057  max = 0;
3058  for (x = 0; x < n; x++) {
3059  ast_channel_lock(c[x]);
3060  for (y = 0; y < ast_channel_fd_count(c[x]); y++) {
3061  fdmap[max].fdno = y; /* fd y is linked to this pfds */
3062  fdmap[max].chan = x; /* channel x is linked to this pfds */
3063  max += ast_add_fd(&pfds[max], ast_channel_fd(c[x], y));
3064  }
3065  CHECK_BLOCKING(c[x]);
3066  ast_channel_unlock(c[x]);
3067  }
3068  /* Add the individual fds */
3069  for (x = 0; x < nfds; x++) {
3070  fdmap[max].chan = -1;
3071  max += ast_add_fd(&pfds[max], fds[x]);
3072  }
3073 
3074  if (*ms > 0) {
3075  start = ast_tvnow();
3076  }
3077 
3078  if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */
3079  do {
3080  int kbrms = rms;
3081  if (kbrms > 600000) {
3082  kbrms = 600000;
3083  }
3084  res = ast_poll(pfds, max, kbrms);
3085  if (!res) {
3086  rms -= kbrms;
3087  }
3088  } while (!res && (rms > 0));
3089  } else {
3090  res = ast_poll(pfds, max, rms);
3091  }
3092  for (x = 0; x < n; x++) {
3093  ast_channel_lock(c[x]);
3095  ast_channel_unlock(c[x]);
3096  }
3097  if (res < 0) { /* Simulate a timeout if we were interrupted */
3098  if (errno != EINTR) {
3099  *ms = -1;
3100  }
3101  return NULL;
3102  }
3103  if (!ast_tvzero(whentohangup)) { /* if we have a timeout, check who expired */
3104  now = ast_tvnow();
3105  for (x = 0; x < n; x++) {
3106  if (!ast_tvzero(*ast_channel_whentohangup(c[x])) && ast_tvcmp(*ast_channel_whentohangup(c[x]), now) <= 0) {
3107  ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3109  if (winner == NULL) {
3110  winner = c[x];
3111  }
3112  }
3113  }
3114  }
3115  if (res == 0) { /* no fd ready, reset timeout and done */
3116  *ms = 0; /* XXX use 0 since we may not have an exact timeout. */
3117  return winner;
3118  }
3119  /*
3120  * Then check if any channel or fd has a pending event.
3121  * Remember to check channels first and fds last, as they
3122  * must have priority on setting 'winner'
3123  */
3124  for (x = 0; x < max; x++) {
3125  res = pfds[x].revents;
3126  if (res == 0) {
3127  continue;
3128  }
3129  if (fdmap[x].chan >= 0) { /* this is a channel */
3130  winner = c[fdmap[x].chan]; /* override previous winners */
3131  ast_channel_lock(winner);
3132  if (res & POLLPRI) {
3134  } else {
3136  }
3137  ast_channel_fdno_set(winner, fdmap[x].fdno);
3138  ast_channel_unlock(winner);
3139  } else { /* this is an fd */
3140  if (outfd) {
3141  *outfd = pfds[x].fd;
3142  }
3143  if (exception) {
3144  *exception = (res & POLLPRI) ? -1 : 0;
3145  }
3146  winner = NULL;
3147  }
3148  }
3149  if (*ms > 0) {
3150  *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3151  if (*ms < 0) {
3152  *ms = 0;
3153  }
3154  }
3155  return winner;
3156 }
3157 
3158 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
3159 {
3160  return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
3161 }
3162 
3163 int ast_waitfor(struct ast_channel *c, int ms)
3164 {
3165  if (ms < 0) {
3166  do {
3167  ms = 100000;
3168  ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3169  } while (!ms);
3170  } else {
3171  ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3172  }
3173  return ms;
3174 }
3175 
3176 int ast_waitfordigit(struct ast_channel *c, int ms)
3177 {
3178  return ast_waitfordigit_full(c, ms, NULL, -1, -1);
3179 }
3180 
3181 int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
3182 {
3183  return ast_settimeout_full(c, rate, func, data, 0);
3184 }
3185 
3186 int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data, unsigned int is_ao2_obj)
3187 {
3188  int res;
3189  unsigned int real_rate = rate, max_rate;
3190 
3192 
3193  if (ast_channel_timingfd(c) == -1) {
3195  return -1;
3196  }
3197 
3198  if (!func) {
3199  rate = 0;
3200  data = NULL;
3201  }
3202 
3203  if (rate && rate > (max_rate = ast_timer_get_max_rate(ast_channel_timer(c)))) {
3204  real_rate = max_rate;
3205  }
3206 
3207  ast_debug(3, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
3208 
3209  res = ast_timer_set_rate(ast_channel_timer(c), real_rate);
3210 
3213  }
3214 
3217 
3218  if (data && is_ao2_obj) {
3219  ao2_ref(data, 1);
3221  } else {
3223  }
3224 
3225  if (func == NULL && rate == 0 && ast_channel_fdno(c) == AST_TIMING_FD) {
3226  /* Clearing the timing func and setting the rate to 0
3227  * means that we don't want to be reading from the timingfd
3228  * any more. Setting c->fdno to -1 means we won't have any
3229  * errant reads from the timingfd, meaning we won't potentially
3230  * miss any important frames.
3231  */
3232  ast_channel_fdno_set(c, -1);
3233  }
3234 
3236 
3237  return res;
3238 }
3239 
3240 int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, const char *breakon, int audiofd, int cmdfd)
3241 {
3242  struct timeval start = ast_tvnow();
3243  int ms;
3244 
3245  /* Stop if we're a zombie or need a soft hangup */
3247  return -1;
3248 
3249  /* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
3251 
3252  /* Wait for a digit, no more than timeout_ms milliseconds total.
3253  * Or, wait indefinitely if timeout_ms is <0.
3254  */
3255  while ((ms = ast_remaining_ms(start, timeout_ms))) {
3256  struct ast_channel *rchan;
3257  int outfd = -1;
3258 
3259  errno = 0;
3260  /* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,
3261  * it is unhelpful if it waited less than a millisecond.
3262  */
3263  rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
3264 
3265  if (!rchan && outfd < 0 && ms) {
3266  if (errno == 0 || errno == EINTR)
3267  continue;
3268  ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
3270  return -1;
3271  } else if (outfd > -1) {
3272  /* The FD we were watching has something waiting */
3273  ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
3275  return 1;
3276  } else if (rchan) {
3277  int res;
3278  struct ast_frame *f = ast_read(c);
3279 
3280  if (!f) {
3282 
3283  return -1;
3284  }
3285 
3286  switch (f->frametype) {
3287  case AST_FRAME_DTMF_BEGIN:
3288  break;
3289  case AST_FRAME_DTMF_END:
3290  res = f->subclass.integer;
3291  if (!breakon || strchr(breakon, res)) {
3292  ast_frfree(f);
3294  return res;
3295  }
3296  break;
3297  case AST_FRAME_CONTROL:
3298  switch (f->subclass.integer) {
3299  case AST_CONTROL_HANGUP:
3300  ast_frfree(f);
3302  return -1;
3308  /* Fall-through and treat as if it were a DTMF signal. Items
3309  * that perform stream control will handle this. */
3310  res = f->subclass.integer;
3311  ast_frfree(f);
3313  return res;
3315  case AST_CONTROL_RINGING:
3316  case AST_CONTROL_ANSWER:
3317  case AST_CONTROL_SRCUPDATE:
3318  case AST_CONTROL_SRCCHANGE:
3322  case AST_CONTROL_HOLD:
3323  case AST_CONTROL_UNHOLD:
3324  case AST_CONTROL_FLASH:
3325  case -1:
3326  /* Unimportant */
3327  break;
3328  default:
3329  ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
3330  break;
3331  }
3332  break;
3333  case AST_FRAME_VOICE:
3334  /* Write audio if appropriate */
3335  if (audiofd > -1) {
3336  if (write(audiofd, f->data.ptr, f->datalen) < 0) {
3337  ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3338  }
3339  }
3340  default:
3341  /* Ignore */
3342  break;
3343  }
3344  ast_frfree(f);
3345  }
3346  }
3347 
3349 
3350  return 0; /* Time is up */
3351 }
3352 
3355  DTMF_SENT
3356 };
3357 
3359 {
3360  switch (direction) {
3361  case DTMF_RECEIVED:
3362  return "Received";
3363  case DTMF_SENT:
3364  return "Sent";
3365  }
3366 
3367  return "?";
3368 }
3369 
3370 static void send_dtmf_begin_event(struct ast_channel *chan,
3371  enum DtmfDirection direction, const char digit)
3372 {
3373  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3374  char digit_str[] = { digit, '\0' };
3375 
3376  blob = ast_json_pack("{ s: s, s: s }",
3377  "digit", digit_str,
3378  "direction", dtmf_direction_to_string(direction));
3379  if (!blob) {
3380  return;
3381  }
3382 
3384 }
3385 
3386 static void send_dtmf_end_event(struct ast_channel *chan,
3387  enum DtmfDirection direction, const char digit, long duration_ms)
3388 {
3389  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3390  char digit_str[] = { digit, '\0' };
3391 
3392  blob = ast_json_pack("{ s: s, s: s, s: I }",
3393  "digit", digit_str,
3394  "direction", dtmf_direction_to_string(direction),
3395  "duration_ms", (ast_json_int_t)duration_ms);
3396  if (!blob) {
3397  return;
3398  }
3399 
3401 }
3402 
3403 static void send_flash_event(struct ast_channel *chan)
3404 {
3406 }
3407 
3408 static void send_wink_event(struct ast_channel *chan)
3409 {
3411 }
3412 
3413 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
3414 {
3415  struct ast_generator *generator;
3416  void *gendata;
3417  int res;
3418  int samples;
3419 
3420  generator = ast_channel_generator(chan);
3421  if (!generator
3422  || !generator->generate
3423  || f->frametype != AST_FRAME_VOICE
3424  || !ast_channel_generatordata(chan)
3425  || ast_channel_timingfunc(chan)) {
3426  return;
3427  }
3428 
3429  /*
3430  * We must generate frames in phase locked mode since
3431  * we have no internal timer available.
3432  */
3434  float factor;
3436  samples = (int) (((float) f->samples) * factor);
3437  } else {
3438  samples = f->samples;
3439  }
3440 
3441  gendata = ast_channel_generatordata(chan);
3442  ast_channel_generatordata_set(chan, NULL); /* reset, to let writes go through */
3443 
3444  /*
3445  * This unlock is here based on two assumptions that hold true at
3446  * this point in the code. 1) this function is only called from
3447  * within __ast_read() and 2) all generators call ast_write() in
3448  * their generate callback.
3449  *
3450  * The reason this is added is so that when ast_write is called,
3451  * the lock that occurs there will not recursively lock the
3452  * channel. Doing this will allow deadlock avoidance to work in
3453  * deeper functions.
3454  */
3455  ast_channel_unlock(chan);
3456  res = generator->generate(chan, gendata, f->datalen, samples);
3457  ast_channel_lock(chan);
3458  if (generator == ast_channel_generator(chan)) {
3459  ast_channel_generatordata_set(chan, gendata);
3460  if (res) {
3461  ast_debug(1, "Auto-deactivating generator\n");
3463  }
3464  }
3465 }
3466 
3467 static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
3468 {
3469  struct ast_frame *fr = ast_channel_dtmff(chan);
3470 
3472  fr->subclass.integer = f->subclass.integer;
3473  fr->len = f->len;
3474 
3475  /* The only time this function will be called is for a frame that just came
3476  * out of the channel driver. So, we want to stick it on the tail of the
3477  * readq. */
3478 
3479  ast_queue_frame(chan, fr);
3480 }
3481 
3482 /*!
3483  * \brief Determine whether or not we should ignore DTMF in the readq
3484  */
3485 static inline int should_skip_dtmf(struct ast_channel *chan)
3486 {
3488  /* We're in the middle of emulating a digit, or DTMF has been
3489  * explicitly deferred. Skip this digit, then. */
3490  return 1;
3491  }
3492 
3493  if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3495  /* We're not in the middle of a digit, but it hasn't been long enough
3496  * since the last digit, so we'll have to skip DTMF for now. */
3497  return 1;
3498  }
3499 
3500  return 0;
3501 }
3502 
3503 /*!
3504  * \brief calculates the number of samples to jump forward with in a monitor stream.
3505 
3506  * \note When using ast_seekstream() with the read and write streams of a monitor,
3507  * the number of samples to seek forward must be of the same sample rate as the stream
3508  * or else the jump will not be calculated correctly.
3509  *
3510  * \retval number of samples to seek forward after rate conversion.
3511  */
3512 static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
3513 {
3514  int diff = sample_rate - seek_rate;
3515 
3516  if (diff > 0) {
3517  samples = samples / (float) (sample_rate / seek_rate);
3518  } else if (diff < 0) {
3519  samples = samples * (float) (seek_rate / sample_rate);
3520  }
3521 
3522  return samples;
3523 }
3524 
3525 static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio, int dropnondefault)
3526 {
3527  struct ast_frame *f = NULL; /* the return value */
3528  int prestate;
3529  int cause = 0;
3530  struct ast_stream *stream = NULL, *default_stream = NULL;
3531 
3532  /* this function is very long so make sure there is only one return
3533  * point at the end (there are only two exceptions to this).
3534  */
3535  ast_channel_lock(chan);
3536 
3537  /* Stop if we're a zombie or need a soft hangup */
3539  if (ast_channel_generator(chan))
3541 
3542  /*
3543  * It is possible for chan->_softhangup to be set and there
3544  * still be control frames that need to be read. Instead of
3545  * just going to 'done' in the case of ast_check_hangup(), we
3546  * need to queue the end-of-Q frame so that it can mark the end
3547  * of the read queue. If there are frames to be read,
3548  * ast_queue_control() will be called repeatedly, but will only
3549  * queue the first end-of-Q frame.
3550  */
3553  } else {
3554  goto done;
3555  }
3556  } else {
3557 #ifdef AST_DEVMODE
3558  /*
3559  * The ast_waitfor() code records which of the channel's file
3560  * descriptors reported that data is available. In theory,
3561  * ast_read() should only be called after ast_waitfor() reports
3562  * that a channel has data available for reading. However,
3563  * there still may be some edge cases throughout the code where
3564  * ast_read() is called improperly. This can potentially cause
3565  * problems, so if this is a developer build, make a lot of
3566  * noise if this happens so that it can be addressed.
3567  *
3568  * One of the potential problems is blocking on a dead channel.
3569  */
3570  if (ast_channel_fdno(chan) == -1) {
3572  "ast_read() on chan '%s' called with no recorded file descriptor.\n",
3573  ast_channel_name(chan));
3574  }
3575 #endif
3576  }
3577 
3578  prestate = ast_channel_state(chan);
3579 
3580  if (ast_channel_timingfd(chan) > -1 && ast_channel_fdno(chan) == AST_TIMING_FD) {
3581  enum ast_timer_event res;
3582  int trigger_dtmf_emulating = should_trigger_dtmf_emulating(chan);
3583 
3585 
3587 
3588  switch (res) {
3590  if (ast_timer_ack(ast_channel_timer(chan), 1) < 0) {
3591  ast_log(LOG_ERROR, "Failed to acknowledge timer in ast_read\n");
3592  goto done;
3593  }
3594 
3595  if (ast_channel_timingfunc(chan)) {
3596  /* save a copy of func/data before unlocking the channel */
3598  void *data = ast_channel_timingdata(chan);
3599  int got_ref = 0;
3601  ao2_ref(data, 1);
3602  got_ref = 1;
3603  }
3604  ast_channel_fdno_set(chan, -1);
3605  ast_channel_unlock(chan);
3606  func(data);
3607  if (got_ref) {
3608  ao2_ref(data, -1);
3609  }
3610 
3611  if (trigger_dtmf_emulating) {
3612  /*
3613  * Since we're breaking out of this switch block and not
3614  * returning, we need to re-lock the channel.
3615  */
3616  ast_channel_lock(chan);
3617  /* generate null frame to trigger dtmf emulating */
3618  f = &ast_null_frame;
3619  break;
3620  }
3621  } else if (trigger_dtmf_emulating) {
3622  /* generate null frame to trigger dtmf emulating */
3623  f = &ast_null_frame;
3624  break;
3625  } else {
3627  /* generate very last null frame to trigger dtmf emulating */
3628  f = &ast_null_frame;
3629  break;
3630  }
3631 
3632  /* cannot 'goto done' because the channel is already unlocked */
3633  return &ast_null_frame;
3634 
3636  if (AST_LIST_EMPTY(ast_channel_readq(chan)) ||
3639  }
3640  break;
3641  }
3642 
3644  /* if the AST_GENERATOR_FD is set, call the generator with args
3645  * set to -1 so it can do whatever it needs to.
3646  */
3647  void *tmp = ast_channel_generatordata(chan);
3648  ast_channel_generatordata_set(chan, NULL); /* reset to let ast_write get through */
3649  ast_channel_generator(chan)->generate(chan, tmp, -1, -1);
3651  f = &ast_null_frame;
3652  ast_channel_fdno_set(chan, -1);
3653  goto done;
3656  }
3657 
3658  /* Read and ignore anything on the alertpipe, but read only
3659  one sizeof(blah) per frame that we send from it */
3661  f = &ast_null_frame;
3662  goto done;
3663  }
3664 
3665  /* Check for pending read queue */
3666  if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
3667  int skipped_dtmf_frame = 0;
3668  int skip_dtmf = should_skip_dtmf(chan);
3669 
3671  /* We have to be picky about which frame we pull off of the readq because
3672  * there are cases where we want to leave DTMF frames on the queue until
3673  * some later time. */
3674 
3675  if ( (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) && skip_dtmf) {
3676  skipped_dtmf_frame = 1;
3677  continue;
3678  }
3679 
3681  break;
3682  }
3684 
3685  if (!f) {
3686  /* There were no acceptable frames on the readq. */
3687  f = &ast_null_frame;
3688  if (!skipped_dtmf_frame) {
3689  /*
3690  * Do not trigger alert pipe if only buffered dtmf begin or end frames
3691  * are left in the readq.
3692  */
3694  } else {
3695  /*
3696  * Safely disable continous timer events if only buffered dtmf begin or end
3697  * frames are left in the readq.
3698  */
3700  }
3701  }
3702 
3703  /* Interpret hangup and end-of-Q frames to return NULL */
3704  /* XXX why not the same for frames from the channel ? */
3705  if (f->frametype == AST_FRAME_CONTROL) {
3706  switch (f->subclass.integer) {
3707  case AST_CONTROL_HANGUP:
3709  cause = f->data.uint32;
3710  /* Fall through */
3711  case AST_CONTROL_END_OF_Q:
3712  ast_frfree(f);
3713  f = NULL;
3714  break;
3715  default:
3716  break;
3717  }
3718  } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
3719  if (ast_channel_tech(chan) && ast_channel_tech(chan)->read_stream) {
3722  } else {
3723  /* Since this channel driver does not support multistream determine the default stream this frame
3724  * originated from and update the frame to include it.
3725  */
3726  stream = default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3727  /* In order to allow media to be passed up the underlying media type has to have a format negotiated on
3728  * the channel itself. In cases where this hasn't happened the channel driver is incorrectly passing up
3729  * a frame for a format that has not been negotiated. If this occurs just drop the frame as we have no
3730  * stream that it came from.
3731  */
3732  if (!stream) {
3733  ast_frfree(f);
3734  f = &ast_null_frame;
3735  } else {
3736  f->stream_num = ast_stream_get_position(stream);
3737  }
3738  }
3739  }
3740  } else {
3742  if (ast_channel_tech(chan)->exception)
3743  f = ast_channel_tech(chan)->exception(chan);
3744  else {
3745  ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", ast_channel_name(chan));
3746  f = &ast_null_frame;
3747  }
3748  /* Clear the exception flag */
3750  } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read_stream) {
3751  f = ast_channel_tech(chan)->read_stream(chan);
3752 
3753  /* This channel driver supports multistream so the stream_num on the frame is valid, the only
3754  * thing different is that we need to find the default stream so we know whether to invoke the
3755  * default stream logic or not (such as transcoding).
3756  */
3757  if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
3760  }
3761  } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read) {
3762  f = ast_channel_tech(chan)->read(chan);
3763 
3764  /* Since this channel driver does not support multistream determine the default stream this frame
3765  * originated from and update the frame to include it.
3766  */
3767  if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
3768  stream = default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3769  if (!stream) {
3770  ast_frfree(f);
3771  f = &ast_null_frame;
3772  } else {
3773  f->stream_num = ast_stream_get_position(stream);
3774  }
3775  }
3776  }
3777  else
3778  ast_log(LOG_WARNING, "No read routine on channel %s\n", ast_channel_name(chan));
3779  }
3780 
3781  if (stream == default_stream) {
3782  /* Perform the framehook read event here. After the frame enters the framehook list
3783  * there is no telling what will happen, <insert mad scientist laugh here>!!! */
3785  }
3786 
3787  /*
3788  * Reset the recorded file descriptor that triggered this read so that we can
3789  * easily detect when ast_read() is called without properly using ast_waitfor().
3790  */
3791  ast_channel_fdno_set(chan, -1);
3792 
3793  if (f) {
3794  struct ast_frame *readq_tail = AST_LIST_LAST(ast_channel_readq(chan));
3795  struct ast_control_read_action_payload *read_action_payload;
3797  int hooked = 0;
3798 
3799  /* if the channel driver returned more than one frame, stuff the excess
3800  into the readq for the next ast_read call
3801  */
3802  if (AST_LIST_NEXT(f, frame_list)) {
3806  }
3807 
3808  if (dropnondefault && stream != default_stream) {
3809  /* If the frame originates from a non-default stream and the caller can not handle other streams
3810  * absorb the frame and replace it with a null one instead.
3811  */
3812  ast_frfree(f);
3813  f = &ast_null_frame;
3814  }
3815 
3816  switch (f->frametype) {
3817  case AST_FRAME_CONTROL:
3818  if (f->subclass.integer == AST_CONTROL_ANSWER) {
3819  if (prestate == AST_STATE_UP && ast_channel_is_bridged(chan)) {
3820  ast_debug(1, "Dropping duplicate answer!\n");
3821  ast_frfree(f);
3822  f = &ast_null_frame;
3823  } else {
3824  /*
3825  * Mark when outgoing channel answered so we can know how
3826  * long the channel has been up.
3827  */
3829 
3830  ast_setstate(chan, AST_STATE_UP);
3831  }
3832  } else if (f->subclass.integer == AST_CONTROL_READ_ACTION) {
3833  read_action_payload = f->data.ptr;
3834  switch (read_action_payload->action) {
3838  if (ast_connected_line_parse_data(read_action_payload->payload,
3839  read_action_payload->payload_size, &connected)) {
3841  break;
3842  }
3843  ast_channel_unlock(chan);
3844  if (ast_channel_connected_line_sub(NULL, chan, &connected, 0) &&
3847  read_action_payload->payload,
3848  read_action_payload->payload_size);
3849  }
3851  ast_channel_lock(chan);
3852  break;
3854  ast_channel_unlock(chan);
3855  ast_sendtext(chan, (const char *)read_action_payload->payload);
3856  ast_channel_lock(chan);
3857  break;
3859  ast_channel_unlock(chan);
3860  ast_sendtext_data(chan, (struct ast_msg_data *)read_action_payload->payload);
3861  ast_channel_lock(chan);
3862  break;
3863  }
3864  ast_frfree(f);
3865  f = &ast_null_frame;
3866  } else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_REQUEST_CHANGE && dropnondefault) {
3867  /* The caller of this function is incapable of handling streams so we don't accept the change request
3868  * and stick to the streams currently on the channel.
3869  */
3871  ast_frfree(f);
3872  f = &ast_null_frame;
3873  } else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_CHANGED && dropnondefault) {
3874  /* The caller of this function is incapable of handling streams so we absorb the notification that the
3875  * stream topology has changed.
3876  */
3877  ast_frfree(f);
3878  f = &ast_null_frame;
3879  } else if (f->subclass.integer == AST_CONTROL_FLASH) {
3880  send_flash_event(chan);
3881  } else if (f->subclass.integer == AST_CONTROL_WINK) {
3882  send_wink_event(chan);
3883  }
3884  break;
3885  case AST_FRAME_DTMF_END:
3887  ast_log(LOG_DTMF, "DTMF end '%c' received on %s, duration %ld ms\n", f->subclass.integer, ast_channel_name(chan), f->len);
3888  /* Queue it up if DTMF is deferred, or if DTMF emulation is forced. */
3890  queue_dtmf_readq(chan, f);
3891  ast_frfree(f);
3892  f = &ast_null_frame;
3894  if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3896  /* If it hasn't been long enough, defer this digit */
3897  queue_dtmf_readq(chan, f);
3898  ast_frfree(f);
3899  f = &ast_null_frame;
3900  } else {
3901  /* There was no begin, turn this into a begin and send the end later */
3902  struct timeval tv = ast_tvnow();
3906  ast_channel_dtmf_tv_set(chan, &tv);
3907  if (f->len) {
3908  if (f->len > option_dtmfminduration)
3910  else
3912  } else
3914  ast_log(LOG_DTMF, "DTMF begin emulation of '%c' with duration %u queued on %s\n", f->subclass.integer, ast_channel_emulate_dtmf_duration(chan), ast_channel_name(chan));
3915 
3916  /*
3917  * Start generating 50 fps timer events (null frames) for dtmf emulating
3918  * independently from any existing incoming voice frames.
3919  * If channel generator is already activated in regular mode use these
3920  * timer events to generate null frames.
3921  */
3922  if (!ast_channel_generator(chan)) {
3924  }
3925  }
3926  if (ast_channel_audiohooks(chan)) {
3927  struct ast_frame *old_frame = f;
3928  /*!
3929  * \todo XXX It is possible to write a digit to the audiohook twice
3930  * if the digit was originally read while the channel was in autoservice. */
3932  if (old_frame != f)
3933  ast_frfree(old_frame);
3934  }
3935  } else {
3936  struct timeval now = ast_tvnow();
3938  ast_log(LOG_DTMF, "DTMF end accepted with begin '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3940  if (!f->len)
3941  f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
3942 
3943  /* detect tones that were received on
3944  * the wire with durations shorter than
3945  * option_dtmfminduration and set f->len
3946  * to the actual duration of the DTMF
3947  * frames on the wire. This will cause
3948  * dtmf emulation to be triggered later
3949  * on.
3950  */
3952  f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
3953  ast_log(LOG_DTMF, "DTMF end '%c' detected to have actual duration %ld on the wire, emulation will be triggered on %s\n", f->subclass.integer, f->len, ast_channel_name(chan));
3954  }
3955  } else if (!f->len) {
3956  ast_log(LOG_DTMF, "DTMF end accepted without begin '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3958  }
3960  ast_log(LOG_DTMF, "DTMF end '%c' has duration %ld but want minimum %u, emulating on %s\n", f->subclass.integer, f->len, option_dtmfminduration, ast_channel_name(chan));
3964  ast_frfree(f);
3965  f = &ast_null_frame;
3966 
3967  /* Start generating 50 fps timer events (null frames) for dtmf emulating
3968  * independently from any existing incoming voice frames.
3969  * If channel generator is already activated in regular mode use these
3970  * timer events to generate null frames.
3971  */
3972  if (!ast_channel_generator(chan)) {
3974  }
3975  } else {
3976  ast_log(LOG_DTMF, "DTMF end passthrough '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3977  if (f->len < option_dtmfminduration) {
3979  }
3980  ast_channel_dtmf_tv_set(chan, &now);
3981 
3982  /* Start generating 50 fps timer events (null frames) for dtmf emulating
3983  * independently from any existing incoming voice frames.
3984  * If channel generator is already activated in regular mode use these
3985  * timer events to generate null frames.
3986  */
3987  if (!ast_channel_generator(chan)) {
3989  }
3990  }
3991  if (ast_channel_audiohooks(chan)) {
3992  struct ast_frame *old_frame = f;
3994  if (old_frame != f)
3995  ast_frfree(old_frame);
3996  }
3997  }
3998  break;
3999  case AST_FRAME_DTMF_BEGIN:
4001  ast_log(LOG_DTMF, "DTMF begin '%c' received on %s\n", f->subclass.integer, ast_channel_name(chan));
4003  (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
4005  ast_log(LOG_DTMF, "DTMF begin ignored '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
4006  ast_frfree(f);
4007  f = &ast_null_frame;
4008  } else {
4009  struct timeval now = ast_tvnow();
4011  ast_channel_dtmf_tv_set(chan, &now);
4012  ast_log(LOG_DTMF, "DTMF begin passthrough '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
4013  }
4014  break;
4015  case AST_FRAME_NULL:
4016  /* The EMULATE_DTMF flag must be cleared here as opposed to when the duration
4017  * is reached , because we want to make sure we pass at least one
4018  * voice frame through before starting the next digit, to ensure a gap
4019  * between DTMF digits. */
4021  struct timeval now = ast_tvnow();
4022  if (!ast_channel_emulate_dtmf_duration(chan)) {
4025  } else if (ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan)) >= ast_channel_emulate_dtmf_duration(chan)) {
4027  ast_frfree(f);
4028  f = ast_channel_dtmff(chan);
4031  f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
4032  ast_channel_dtmf_tv_set(chan, &now);
4035  ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, ast_channel_name(chan));
4036  if (ast_channel_audiohooks(chan)) {
4037  struct ast_frame *old_frame = f;
4039  if (old_frame != f) {
4040  ast_frfree(old_frame);
4041  }
4042  }
4043 
4044  /* Start generating 50 fps timer events (null frames) for dtmf emulating
4045  * independently from any existing incoming voice frames.
4046  * If channel generator is already activated in regular mode use these
4047  * timer events to generate null frames.
4048  */
4049  if (!ast_channel_generator(chan)) {
4051  }
4052  }
4053  }
4054  break;
4055  case AST_FRAME_VOICE:
4056  /* If media was received from a non-default stream don't perform any actions, let it just go through */
4057  if (stream != default_stream) {
4058  break;
4059  }
4060 
4061  /* The EMULATE_DTMF flag must be cleared here as opposed to when the duration
4062  * is reached , because we want to make sure we pass at least one
4063  * voice frame through before starting the next digit, to ensure a gap
4064  * between DTMF digits. */
4068  }
4069 
4070  if (dropaudio || ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF)) {
4071  if (dropaudio)
4072  ast_read_generator_actions(chan, f);
4073  ast_frfree(f);
4074  f = &ast_null_frame;
4075  }
4076 
4078  struct timeval now = ast_tvnow();
4081  ast_frfree(f);
4082  f = ast_channel_dtmff(chan);
4085  f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
4086  ast_channel_dtmf_tv_set(chan, &now);
4087  if (ast_channel_audiohooks(chan)) {
4088  struct ast_frame *old_frame = f;
4090  if (old_frame != f)
4091  ast_frfree(old_frame);
4092  }
4093  ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, ast_channel_name(chan));
4094  } else {
4095  /* Drop voice frames while we're still in the middle of the digit */
4096  ast_frfree(f);
4097  f = &ast_null_frame;
4098  }
4099  break;
4100  }
4101  if (f->frametype != AST_FRAME_VOICE) {
4102  break;
4103  }
4106  struct ast_format *core_format;
4107 
4108  /*
4109  * Note: This frame may not be one of the current native
4110  * formats. We may have gotten it out of the read queue from
4111  * a previous multi-frame translation, from a framehook
4112  * injected frame, or the device we're talking to isn't
4113  * respecting negotiated formats. Regardless we will accept
4114  * all frames.
4115  *
4116  * Update the read translation path to handle the new format
4117  * that just came in. If the core wants slinear we need to
4118  * setup a new translation path because the core is usually
4119  * doing something with the audio itself and may not handle
4120  * any other format. e.g., Softmix bridge, holding bridge
4121  * announcer channel, recording, AMD... Otherwise, we'll
4122  * setup to pass the frame as is to the core. In this case
4123  * the core doesn't care. The channel is likely in
4124  * autoservice, safesleep, or the channel is in a bridge.
4125  * Let the bridge technology deal with format compatibility
4126  * between the channels in the bridge.
4127  *
4128  * Beware of the transcode_via_slin and genericplc options as
4129  * they force any transcoding to go through slin on a bridge.
4130  * Unfortunately transcode_via_slin is enabled by default and
4131  * genericplc is enabled in the codecs.conf.sample file.
4132  *
4133  * XXX Only updating translation to slinear frames has some
4134  * corner cases if slinear is one of the native formats and
4135  * there are different sample rates involved. We might wind
4136  * up with conflicting translation paths between channels
4137  * where the read translation path on this channel reduces
4138  * the sample rate followed by a write translation path on
4139  * the peer channel that increases the sample rate.
4140  */
4141  core_format = ast_channel_readformat(chan);
4142  if (!ast_format_cache_is_slinear(core_format)) {
4143  core_format = f->subclass.format;
4144  }
4145  if (ast_set_read_format_path(chan, f->subclass.format, core_format)) {
4146  /* Drop frame. We couldn't make it compatible with the core. */
4147  ast_frfree(f);
4148  f = &ast_null_frame;
4149  break;
4150  }
4151  }
4152  /*
4153  * Send frame to audiohooks if present, if frametype is linear, to preserve
4154  * functional compatibility with previous behavior. If not linear, hold off
4155  * until transcoding is done where we are more likely to have a linear frame
4156  */
4158  /* Place hooked after declaration */
4159  struct ast_frame *old_frame = f;
4160  hooked = 1;
4161 
4163  if (old_frame != f) {
4164  ast_frfree(old_frame);
4165  }
4166  }
4167 
4168  if (ast_channel_monitor(chan) && ast_channel_monitor(chan)->read_stream) {
4169  /* XXX what does this do ? */
4170 #ifndef MONITOR_CONSTANT_DELAY
4171  int jump = ast_channel_outsmpl(chan) - ast_channel_insmpl(chan) - 4 * f->samples;
4172  if (jump >= 0) {
4175  ast_format_get_sample_rate(ast_channel_monitor(chan)->read_stream->fmt->format));
4176  if (ast_seekstream(ast_channel_monitor(chan)->read_stream, jump, SEEK_FORCECUR) == -1) {
4177  ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
4178  }
4180  } else {
4182  }
4183 #else
4184  int jump = calc_monitor_jump((ast_channel_outsmpl(chan) - ast_channel_insmpl(chan)),
4186  ast_format_get_sample_rate(ast_channel_monitor(chan)->read_stream->fmt->format));
4187  if (jump - MONITOR_DELAY >= 0) {
4188  if (ast_seekstream(ast_channel_monitor(chan)->read_stream, jump - f->samples, SEEK_FORCECUR) == -1) {
4189  ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
4190  }
4192  } else {
4193  ast_channel_insmpl(chan) += f->samples;
4194  }
4195 #endif
4197  if (ast_writestream(ast_channel_monitor(chan)->read_stream, f) < 0)
4198  ast_log(LOG_WARNING, "Failed to write data to channel monitor read stream\n");
4199  }
4200  }
4201 
4202  if (ast_channel_readtrans(chan)
4204  f = ast_translate(ast_channel_readtrans(chan), f, 1);
4205  if (!f) {
4206  f = &ast_null_frame;
4207  }
4208  }
4209 
4210  /* Second chance at hooking a linear frame, also the last chance */
4211  if (ast_channel_audiohooks(chan) && !hooked) {
4212  struct ast_frame *old_frame = f;
4213 
4215  if (old_frame != f) {
4216  ast_frfree(old_frame);
4217  }
4218  }
4219 
4220  /*
4221  * It is possible for the translation process on the channel to have
4222  * produced multiple frames from the single input frame we passed it; if
4223  * this happens, queue the additional frames *before* the frames we may
4224  * have queued earlier. if the readq was empty, put them at the head of
4225  * the queue, and if it was not, put them just after the frame that was
4226  * at the end of the queue.
4227  */
4228  if (AST_LIST_NEXT(f, frame_list)) {
4229  struct ast_frame *cur, *multi_frame = AST_LIST_NEXT(f, frame_list);
4230 
4231  /* Mark these frames as being re-queued */
4232  for (cur = multi_frame; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
4234  }
4235 
4236  if (!readq_tail) {
4237  ast_queue_frame_head(chan, multi_frame);
4238  } else {
4239  __ast_queue_frame(chan, multi_frame, 0, readq_tail);
4240  }
4241  ast_frfree(multi_frame);
4243  }
4244 
4245  /*
4246  * Run generator sitting on the line if timing device not available
4247  * and synchronous generation of outgoing frames is necessary
4248  */
4249  ast_read_generator_actions(chan, f);
4250  break;
4251  case AST_FRAME_RTCP:
4252  /* Incoming RTCP feedback needs to get to the translator for
4253  * outgoing media, which means we treat it as an ast_write */
4254  if (ast_channel_writetrans(chan)) {
4256  }
4257  break;
4258  default:
4259  /* Just pass it on! */
4260  break;
4261  }
4262  } else {
4263  /* Make sure we always return NULL in the future */
4266  }
4267  if (cause)
4268  ast_channel_hangupcause_set(chan, cause);
4269  if (ast_channel_generator(chan))
4271  /* We no longer End the CDR here */
4272  }
4273 
4274  /* High bit prints debugging */
4275  if (ast_channel_fin(chan) & DEBUGCHAN_FLAG)
4276  ast_frame_dump(ast_channel_name(chan), f, "<<");
4278 
4279 done:
4281  ast_channel_generator(chan)->digit(chan, f->subclass.integer);
4282 
4284  /* The list gets recreated if audiohooks are added again later */
4287  }
4288  ast_channel_unlock(chan);
4289  return f;
4290 }
4291 
4292 struct ast_frame *ast_read(struct ast_channel *chan)
4293 {
4294  return __ast_read(chan, 0, 1);
4295 }
4296 
4298 {
4299  return __ast_read(chan, 0, 0);
4300 }
4301 
4303 {
4304  return __ast_read(chan, 1, 1);
4305 }
4306 
4308 {
4309  return __ast_read(chan, 1, 0);
4310 }
4311 
4312 int ast_indicate(struct ast_channel *chan, int condition)
4313 {
4314  return ast_indicate_data(chan, condition, NULL, 0);
4315 }
4316 
4318 {
4319  /* Don't include a default case here so that we get compiler warnings
4320  * when a new type is added. */
4321 
4322  switch (condition) {
4323  case AST_CONTROL_PROGRESS:
4325  case AST_CONTROL_VIDUPDATE:
4326  case AST_CONTROL_SRCUPDATE:
4327  case AST_CONTROL_SRCCHANGE:
4328  case AST_CONTROL_RADIO_KEY:
4330  case AST_CONTROL_OPTION:
4331  case AST_CONTROL_WINK:
4332  case AST_CONTROL_FLASH:
4333  case AST_CONTROL_OFFHOOK:
4335  case AST_CONTROL_ANSWER:
4336  case AST_CONTROL_HANGUP:
4339  case AST_CONTROL_TRANSFER:
4341  case _XXX_AST_CONTROL_T38:
4342  case AST_CONTROL_CC:
4344  case AST_CONTROL_AOC:
4345  case AST_CONTROL_END_OF_Q:
4346  case AST_CONTROL_MCID:
4362  break;
4363 
4366  case AST_CONTROL_BUSY:
4367  case AST_CONTROL_RINGING:
4368  case AST_CONTROL_RING:
4369  case AST_CONTROL_HOLD:
4370  /* You can hear these */
4371  return 1;
4372 
4373  case AST_CONTROL_UNHOLD:
4374  /* This is a special case. You stop hearing this. */
4375  break;
4376  }
4377 
4378  return 0;
4379 }
4380 
4381 void ast_channel_hangupcause_hash_set(struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
4382 {
4383  char causevar[256];
4384 
4385  if (ast_channel_dialed_causes_add(chan, cause_code, datalen)) {
4386  ast_log(LOG_WARNING, "Unable to store hangup cause for %s on %s\n", cause_code->chan_name, ast_channel_name(chan));
4387  }
4388 
4389  if (cause_code->emulate_sip_cause) {
4390  snprintf(causevar, sizeof(causevar), "HASH(SIP_CAUSE,%s)", cause_code->chan_name);
4391  ast_func_write(chan, causevar, cause_code->code);
4392  }
4393 }
4394 
4395 enum ama_flags ast_channel_string2amaflag(const char *flag)
4396 {
4397  if (!strcasecmp(flag, "default"))
4398  return DEFAULT_AMA_FLAGS;
4399  if (!strcasecmp(flag, "omit"))
4400  return AST_AMA_OMIT;
4401  if (!strcasecmp(flag, "billing"))
4402  return AST_AMA_BILLING;
4403  if (!strcasecmp(flag, "documentation"))
4404  return AST_AMA_DOCUMENTATION;
4405  return AST_AMA_NONE;
4406 }
4407 
4409 {
4410  switch (flag) {
4411  case AST_AMA_OMIT:
4412  return "OMIT";
4413  case AST_AMA_BILLING:
4414  return "BILLING";
4415  case AST_AMA_DOCUMENTATION:
4416  return "DOCUMENTATION";
4417  default:
4418  return "Unknown";
4419  }
4420 }
4421 
4422 /*!
4423  * \internal
4424  * \brief Preprocess connected line update.
4425  * \since 12.0.0
4426  *
4427  * \param chan channel to change the indication
4428  * \param data pointer to payload data
4429  * \param datalen size of payload data
4430  *
4431  * \note This function assumes chan is locked.
4432  *
4433  * \retval 0 keep going.
4434  * \retval -1 quit now.
4435  */
4436 static int indicate_connected_line(struct ast_channel *chan, const void *data, size_t datalen)
4437 {
4438  struct ast_party_connected_line *chan_connected = ast_channel_connected(chan);
4439  struct ast_party_connected_line *chan_indicated = ast_channel_connected_indicated(chan);
4441  unsigned char current[1024];
4442  unsigned char proposed[1024];
4443  int current_size;
4444  int proposed_size;
4445  int res;
4446 
4447  ast_party_connected_line_set_init(&connected, chan_connected);
4448  res = ast_connected_line_parse_data(data, datalen, &connected);
4449  if (!res) {
4451  }
4453  if (res) {
4454  return -1;
4455  }
4456 
4457  current_size = ast_connected_line_build_data(current, sizeof(current),
4458  chan_indicated, NULL);
4459  proposed_size = ast_connected_line_build_data(proposed, sizeof(proposed),
4460  chan_connected, NULL);
4461  if (current_size == -1 || proposed_size == -1) {
4462  return -1;
4463  }
4464 
4465  if (current_size == proposed_size && !memcmp(current, proposed, current_size)) {
4466  ast_debug(1, "%s: Dropping redundant connected line update \"%s\" <%s>.\n",
4467  ast_channel_name(chan),
4468  S_COR(chan_connected->id.name.valid, chan_connected->id.name.str, ""),
4469  S_COR(chan_connected->id.number.valid, chan_connected->id.number.str, ""));
4470  return -1;
4471  }
4472 
4473  ast_party_connected_line_copy(chan_indicated, chan_connected);
4474  return 0;
4475 }
4476 
4477 /*!
4478  * \internal
4479  * \brief Preprocess redirecting update.
4480  * \since 12.0.0
4481  *
4482  * \param chan channel to change the indication
4483  * \param data pointer to payload data
4484  * \param datalen size of payload data
4485  *
4486  * \note This function assumes chan is locked.
4487  *
4488  * \retval 0 keep going.
4489  * \retval -1 quit now.
4490  */
4491 static int indicate_redirecting(struct ast_channel *chan, const void *data, size_t datalen)
4492 {
4493  struct ast_party_redirecting redirecting;
4494  int res;
4495 
4497  res = ast_redirecting_parse_data(data, datalen, &redirecting);
4498  if (!res) {
4499  ast_channel_set_redirecting(chan, &redirecting, NULL);
4500  }
4501  ast_party_redirecting_free(&redirecting);
4502  return res ? -1 : 0;
4503 }
4504 
4505 static int indicate_data_internal(struct ast_channel *chan, int _condition, const void *data, size_t datalen)
4506 {
4507  /* By using an enum, we'll get compiler warnings for values not handled
4508  * in switch statements. */
4509  enum ast_control_frame_type condition = _condition;
4510  struct ast_tone_zone_sound *ts = NULL;
4511  const struct ast_control_t38_parameters *t38_parameters;
4512  int res;
4513 
4514  switch (condition) {
4516  if (indicate_connected_line(chan, data, datalen)) {
4517  res = 0;
4518  return res;
4519  }
4520  break;
4522  if (indicate_redirecting(chan, data, datalen)) {
4523  res = 0;
4524  return res;
4525  }
4526  break;
4527  case AST_CONTROL_HOLD:
4528  case AST_CONTROL_UNHOLD:
4529  ast_channel_hold_state_set(chan, _condition);
4530  break;
4532  t38_parameters = data;
4533  switch (t38_parameters->request_response) {
4535  case AST_T38_NEGOTIATED:
4537  break;
4539  case AST_T38_TERMINATED:
4540  case AST_T38_REFUSED:
4542  break;
4543  default:
4544  break;
4545  }
4546  break;
4547  default:
4548  break;
4549  }
4550 
4551  if (is_visible_indication(condition)) {
4552  /* A new visible indication is requested. */
4553  ast_channel_visible_indication_set(chan, _condition);
4554  } else if (condition == AST_CONTROL_UNHOLD || _condition < 0) {
4555  /* Visible indication is cleared/stopped. */
4557  }
4558 
4559  if (ast_channel_tech(chan)->indicate) {
4560  /* See if the channel driver can handle this condition. */
4561  res = ast_channel_tech(chan)->indicate(chan, _condition, data, datalen);
4562  } else {
4563  res = -1;
4564  }
4565 
4566  if (!res) {
4567  /* The channel driver successfully handled this indication */
4568  res = 0;
4569  return res;
4570  }
4571 
4572  /* The channel driver does not support this indication, let's fake
4573  * it by doing our own tone generation if applicable. */
4574 
4575  /*!\note If we compare the enumeration type, which does not have any
4576  * negative constants, the compiler may optimize this code away.
4577  * Therefore, we must perform an integer comparison here. */
4578  if (_condition < 0) {
4579  /* Stop any tones that are playing */
4580  ast_playtones_stop(chan);
4581  res = 0;
4582  return res;
4583  }
4584 
4585  /* Handle conditions that we have tones for. */
4586  switch (condition) {
4587  case _XXX_AST_CONTROL_T38:
4588  /* deprecated T.38 control frame */
4589  res = -1;
4590  return res;
4592  /* there is no way to provide 'default' behavior for these
4593  * control frames, so we need to return failure, but there
4594  * is also no value in the log message below being emitted
4595  * since failure to handle these frames is not an 'error'
4596  * so just return right now. in addition, we want to return
4597  * whatever value the channel driver returned, in case it
4598  * has some meaning.*/
4599  return res;
4600  case AST_CONTROL_RINGING:
4601  ts = ast_get_indication_tone(ast_channel_zone(chan), "ring");
4602  /* It is common practice for channel drivers to return -1 if trying
4603  * to indicate ringing on a channel which is up. The idea is to let the
4604  * core generate the ringing inband. However, we don't want the
4605  * warning message about not being able to handle the specific indication
4606  * to print nor do we want ast_indicate_data to return an "error" for this
4607  * condition
4608  */
4609  if (ast_channel_state(chan) == AST_STATE_UP) {
4610  res = 0;
4611  }
4612  break;
4613  case AST_CONTROL_BUSY:
4614  ts = ast_get_indication_tone(ast_channel_zone(chan), "busy");
4615  break;
4618  ts = ast_get_indication_tone(ast_channel_zone(chan), "congestion");
4619  break;
4621  ast_channel_hangupcause_hash_set(chan, data, datalen);
4622  res = 0;
4623  break;
4624  case AST_CONTROL_PROGRESS:
4626  case AST_CONTROL_VIDUPDATE:
4627  case AST_CONTROL_SRCUPDATE:
4628  case AST_CONTROL_SRCCHANGE:
4629  case AST_CONTROL_RADIO_KEY: