2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2007, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
23 * \author Joshua Colp <jcolp@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/channel.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/linkedlists.h"
41 #include "asterisk/dial.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/musiconhold.h"
44 #include "asterisk/app.h"
45 #include "asterisk/causes.h"
46 #include "asterisk/stasis_channels.h"
48 /*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
50 int num; /*!< Current number to give to next dialed channel */
51 int timeout; /*!< Maximum time allowed for dial attempts */
52 int actual_timeout; /*!< Actual timeout based on all factors (ie: channels) */
53 enum ast_dial_result state; /*!< Status of dial */
54 void *options[AST_DIAL_OPTION_MAX]; /*!< Global options */
55 ast_dial_state_callback state_callback; /*!< Status callback */
56 void *user_data; /*!< Attached user data */
57 AST_LIST_HEAD(, ast_dial_channel) channels; /*!< Channels being dialed */
58 pthread_t thread; /*!< Thread (if running in async) */
59 struct ast_callid *callid; /*!< callid pointer (if running in async) */
60 ast_mutex_t lock; /*! Lock to protect the thread information above */
63 /*! \brief Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! */
64 struct ast_dial_channel {
65 int num; /*!< Unique number for dialed channel */
66 int timeout; /*!< Maximum time allowed for attempt */
67 char *tech; /*!< Technology being dialed */
68 char *device; /*!< Device being dialed */
69 void *options[AST_DIAL_OPTION_MAX]; /*!< Channel specific options */
70 int cause; /*!< Cause code in case of failure */
71 unsigned int is_running_app:1; /*!< Is this running an application? */
72 struct ast_channel *owner; /*!< Asterisk channel */
73 AST_LIST_ENTRY(ast_dial_channel) list; /*!< Linked list information */
76 /*! \brief Typedef for dial option enable */
77 typedef void *(*ast_dial_option_cb_enable)(void *data);
79 /*! \brief Typedef for dial option disable */
80 typedef int (*ast_dial_option_cb_disable)(void *data);
82 /*! \brief Structure for 'ANSWER_EXEC' option */
83 struct answer_exec_struct {
84 char app[AST_MAX_APP]; /*!< Application name */
85 char *args; /*!< Application arguments */
88 /*! \brief Enable function for 'ANSWER_EXEC' option */
89 static void *answer_exec_enable(void *data)
91 struct answer_exec_struct *answer_exec = NULL;
92 char *app = ast_strdupa((char*)data), *args = NULL;
94 /* Not giving any data to this option is bad, mmmk? */
95 if (ast_strlen_zero(app))
98 /* Create new data structure */
99 if (!(answer_exec = ast_calloc(1, sizeof(*answer_exec))))
102 /* Parse out application and arguments */
103 if ((args = strchr(app, ','))) {
105 answer_exec->args = ast_strdup(args);
108 /* Copy application name */
109 ast_copy_string(answer_exec->app, app, sizeof(answer_exec->app));
114 /*! \brief Disable function for 'ANSWER_EXEC' option */
115 static int answer_exec_disable(void *data)
117 struct answer_exec_struct *answer_exec = data;
119 /* Make sure we have a value */
123 /* If arguments are present, free them too */
124 if (answer_exec->args)
125 ast_free(answer_exec->args);
127 /* This is simple - just free the structure */
128 ast_free(answer_exec);
133 static void *music_enable(void *data)
135 return ast_strdup(data);
138 static int music_disable(void *data)
148 static void *predial_enable(void *data)
150 return ast_strdup(data);
153 static int predial_disable(void *data)
164 /*! \brief Application execution function for 'ANSWER_EXEC' option */
165 static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
167 struct ast_channel *chan = dial_channel->owner;
168 struct ast_app *ast_app = pbx_findapp(app);
170 /* If the application was not found, return immediately */
174 /* All is well... execute the application */
175 pbx_exec(chan, ast_app, args);
177 /* If another thread is not taking over hang up the channel */
178 ast_mutex_lock(&dial->lock);
179 if (dial->thread != AST_PTHREADT_STOP) {
181 dial_channel->owner = NULL;
183 ast_mutex_unlock(&dial->lock);
188 struct ast_option_types {
189 enum ast_dial_option option;
190 ast_dial_option_cb_enable enable;
191 ast_dial_option_cb_disable disable;
195 * \brief Map options to respective handlers (enable/disable).
197 * \note This list MUST be perfectly kept in order with enum
198 * ast_dial_option, or else madness will happen.
200 static const struct ast_option_types option_types[] = {
201 { AST_DIAL_OPTION_RINGING, NULL, NULL }, /*!< Always indicate ringing to caller */
202 { AST_DIAL_OPTION_ANSWER_EXEC, answer_exec_enable, answer_exec_disable }, /*!< Execute application upon answer in async mode */
203 { AST_DIAL_OPTION_MUSIC, music_enable, music_disable }, /*!< Play music to the caller instead of ringing */
204 { AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL, NULL }, /*!< Disable call forwarding on channels */
205 { AST_DIAL_OPTION_PREDIAL, predial_enable, predial_disable }, /*!< Execute a subroutine on the outbound channels prior to dialing */
206 { AST_DIAL_OPTION_MAX, NULL, NULL }, /*!< Terminator of list */
209 /*! \brief Maximum number of channels we can watch at a time */
210 #define AST_MAX_WATCHERS 256
212 /*! \brief Macro for finding the option structure to use on a dialed channel */
213 #define FIND_RELATIVE_OPTION(dial, dial_channel, ast_dial_option) (dial_channel->options[ast_dial_option] ? dial_channel->options[ast_dial_option] : dial->options[ast_dial_option])
215 /*! \brief Macro that determines whether a channel is the caller or not */
216 #define IS_CALLER(chan, owner) (chan == owner ? 1 : 0)
218 /*! \brief New dialing structure
219 * \note Create a dialing structure
220 * \return Returns a calloc'd ast_dial structure, NULL on failure
222 struct ast_dial *ast_dial_create(void)
224 struct ast_dial *dial = NULL;
226 /* Allocate new memory for structure */
227 if (!(dial = ast_calloc(1, sizeof(*dial))))
230 /* Initialize list of channels */
231 AST_LIST_HEAD_INIT(&dial->channels);
233 /* Initialize thread to NULL */
234 dial->thread = AST_PTHREADT_NULL;
236 /* No timeout exists... yet */
238 dial->actual_timeout = -1;
240 /* Can't forget about the lock */
241 ast_mutex_init(&dial->lock);
246 /*! \brief Append a channel
247 * \note Appends a channel to a dialing structure
248 * \return Returns channel reference number on success, -1 on failure
250 int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device)
252 struct ast_dial_channel *channel = NULL;
254 /* Make sure we have required arguments */
255 if (!dial || !tech || !device)
258 /* Allocate new memory for dialed channel structure */
259 if (!(channel = ast_calloc(1, sizeof(*channel))))
262 /* Record technology and device for when we actually dial */
263 channel->tech = ast_strdup(tech);
264 channel->device = ast_strdup(device);
266 /* Grab reference number from dial structure */
267 channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
269 /* No timeout exists... yet */
270 channel->timeout = -1;
272 /* Insert into channels list */
273 AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
278 /*! \brief Helper function that requests all channels */
279 static int begin_dial_prerun(struct ast_dial_channel *channel, struct ast_channel *chan, struct ast_format_cap *cap, const char *predial_string)
281 char numsubst[AST_MAX_EXTENSION];
282 struct ast_format_cap *cap_all_audio = NULL;
283 struct ast_format_cap *cap_request;
285 /* Copy device string over */
286 ast_copy_string(numsubst, channel->device, sizeof(numsubst));
288 if (!ast_format_cap_is_empty(cap)) {
291 cap_request = ast_channel_nativeformats(chan);
293 cap_all_audio = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
294 ast_format_cap_add_all_by_type(cap_all_audio, AST_FORMAT_TYPE_AUDIO);
295 cap_request = cap_all_audio;
298 /* If we fail to create our owner channel bail out */
299 if (!(channel->owner = ast_request(channel->tech, cap_request, chan, numsubst, &channel->cause))) {
300 cap_all_audio = ast_format_cap_destroy(cap_all_audio);
304 cap_all_audio = ast_format_cap_destroy(cap_all_audio);
306 ast_channel_stage_snapshot(channel->owner);
308 ast_channel_appl_set(channel->owner, "AppDial2");
309 ast_channel_data_set(channel->owner, "(Outgoing Line)");
310 ast_publish_channel_state(channel->owner);
311 memset(ast_channel_whentohangup(channel->owner), 0, sizeof(*ast_channel_whentohangup(channel->owner)));
313 /* Inherit everything from he who spawned this dial */
315 ast_channel_inherit_variables(chan, channel->owner);
316 ast_channel_datastore_inherit(chan, channel->owner);
318 /* Copy over callerid information */
319 ast_party_redirecting_copy(ast_channel_redirecting(channel->owner), ast_channel_redirecting(chan));
321 ast_channel_dialed(channel->owner)->transit_network_select = ast_channel_dialed(chan)->transit_network_select;
323 ast_connected_line_copy_from_caller(ast_channel_connected(channel->owner), ast_channel_caller(chan));
325 ast_channel_language_set(channel->owner, ast_channel_language(chan));
326 ast_channel_accountcode_set(channel->owner, ast_channel_accountcode(chan));
327 if (ast_strlen_zero(ast_channel_musicclass(channel->owner)))
328 ast_channel_musicclass_set(channel->owner, ast_channel_musicclass(chan));
330 ast_channel_adsicpe_set(channel->owner, ast_channel_adsicpe(chan));
331 ast_channel_transfercapability_set(channel->owner, ast_channel_transfercapability(chan));
334 ast_channel_stage_snapshot_done(channel->owner);
336 if (!ast_strlen_zero(predial_string)) {
337 const char *predial_callee = ast_app_expand_sub_args(chan, predial_string);
338 if (!predial_callee) {
339 ast_log(LOG_ERROR, "Could not expand subroutine arguments in predial request '%s'\n", predial_string);
341 ast_autoservice_start(chan);
342 ast_pre_call(channel->owner, predial_callee);
343 ast_autoservice_stop(chan);
344 ast_free((char *) predial_callee);
350 int ast_dial_prerun(struct ast_dial *dial, struct ast_channel *chan, struct ast_format_cap *cap)
352 struct ast_dial_channel *channel;
354 char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
356 if (!ast_strlen_zero(predial_string)) {
357 ast_replace_subargument_delimiter(predial_string);
360 AST_LIST_LOCK(&dial->channels);
361 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
362 if ((res = begin_dial_prerun(channel, chan, cap, predial_string))) {
366 AST_LIST_UNLOCK(&dial->channels);
371 /*! \brief Helper function that does the beginning dialing per-appended channel */
372 static int begin_dial_channel(struct ast_dial_channel *channel, struct ast_channel *chan, int async, const char *predial_string)
374 char numsubst[AST_MAX_EXTENSION];
377 /* If no owner channel exists yet execute pre-run */
378 if (!channel->owner && begin_dial_prerun(channel, chan, NULL, predial_string)) {
382 /* Copy device string over */
383 ast_copy_string(numsubst, channel->device, sizeof(numsubst));
385 /* Attempt to actually call this device */
386 if ((res = ast_call(channel->owner, numsubst, 0))) {
388 ast_hangup(channel->owner);
389 channel->owner = NULL;
392 ast_poll_channel_add(chan, channel->owner);
394 ast_channel_publish_dial(async ? NULL : chan, channel->owner, channel->device, NULL);
396 ast_verb(3, "Called %s\n", numsubst);
402 /*! \brief Helper function that does the beginning dialing per dial structure */
403 static int begin_dial(struct ast_dial *dial, struct ast_channel *chan, int async)
405 struct ast_dial_channel *channel = NULL;
407 char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
409 if (!ast_strlen_zero(predial_string)) {
410 ast_replace_subargument_delimiter(predial_string);
413 /* Iterate through channel list, requesting and calling each one */
414 AST_LIST_LOCK(&dial->channels);
415 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
416 success += begin_dial_channel(channel, chan, async, predial_string);
418 AST_LIST_UNLOCK(&dial->channels);
420 /* If number of failures matches the number of channels, then this truly failed */
424 /*! \brief Helper function to handle channels that have been call forwarded */
425 static int handle_call_forward(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_channel *chan)
427 struct ast_channel *original = channel->owner;
428 char *tmp = ast_strdupa(ast_channel_call_forward(channel->owner));
429 char *tech = "Local", *device = tmp, *stuff;
430 char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
432 if (!ast_strlen_zero(predial_string)) {
433 ast_replace_subargument_delimiter(predial_string);
436 /* If call forwarding is disabled just drop the original channel and don't attempt to dial the new one */
437 if (FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING)) {
438 ast_hangup(original);
439 channel->owner = NULL;
443 /* Figure out the new destination */
444 if ((stuff = strchr(tmp, '/'))) {
449 const char *forward_context;
450 char destination[AST_MAX_CONTEXT + AST_MAX_EXTENSION + 1];
452 ast_channel_lock(original);
453 forward_context = pbx_builtin_getvar_helper(original, "FORWARD_CONTEXT");
454 snprintf(destination, sizeof(destination), "%s@%s", tmp, S_OR(forward_context, ast_channel_context(original)));
455 ast_channel_unlock(original);
456 device = ast_strdupa(destination);
459 /* Drop old destination information */
460 ast_free(channel->tech);
461 ast_free(channel->device);
463 /* Update the dial channel with the new destination information */
464 channel->tech = ast_strdup(tech);
465 channel->device = ast_strdup(device);
466 AST_LIST_UNLOCK(&dial->channels);
469 /* Drop the original channel */
470 ast_hangup(original);
471 channel->owner = NULL;
473 /* Finally give it a go... send it out into the world */
474 begin_dial_channel(channel, chan, chan ? 0 : 1, predial_string);
479 /*! \brief Helper function that finds the dialed channel based on owner */
480 static struct ast_dial_channel *find_relative_dial_channel(struct ast_dial *dial, struct ast_channel *owner)
482 struct ast_dial_channel *channel = NULL;
484 AST_LIST_LOCK(&dial->channels);
485 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
486 if (channel->owner == owner)
489 AST_LIST_UNLOCK(&dial->channels);
494 static void set_state(struct ast_dial *dial, enum ast_dial_result state)
498 if (dial->state_callback)
499 dial->state_callback(dial);
502 /*! \brief Helper function that handles control frames WITH owner */
503 static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
505 if (fr->frametype == AST_FRAME_CONTROL) {
506 switch (fr->subclass.integer) {
507 case AST_CONTROL_ANSWER:
508 ast_verb(3, "%s answered %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
509 AST_LIST_LOCK(&dial->channels);
510 AST_LIST_REMOVE(&dial->channels, channel, list);
511 AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
512 AST_LIST_UNLOCK(&dial->channels);
513 ast_channel_publish_dial(chan, channel->owner, channel->device, "ANSWER");
514 set_state(dial, AST_DIAL_RESULT_ANSWERED);
516 case AST_CONTROL_BUSY:
517 ast_verb(3, "%s is busy\n", ast_channel_name(channel->owner));
518 ast_channel_publish_dial(chan, channel->owner, channel->device, "BUSY");
519 ast_hangup(channel->owner);
520 channel->owner = NULL;
522 case AST_CONTROL_CONGESTION:
523 ast_verb(3, "%s is circuit-busy\n", ast_channel_name(channel->owner));
524 ast_channel_publish_dial(chan, channel->owner, channel->device, "CONGESTION");
525 ast_hangup(channel->owner);
526 channel->owner = NULL;
528 case AST_CONTROL_INCOMPLETE:
529 ast_verb(3, "%s dialed Incomplete extension %s\n", ast_channel_name(channel->owner), ast_channel_exten(channel->owner));
530 ast_indicate(chan, AST_CONTROL_INCOMPLETE);
532 case AST_CONTROL_RINGING:
533 ast_verb(3, "%s is ringing\n", ast_channel_name(channel->owner));
534 if (!dial->options[AST_DIAL_OPTION_MUSIC])
535 ast_indicate(chan, AST_CONTROL_RINGING);
536 set_state(dial, AST_DIAL_RESULT_RINGING);
538 case AST_CONTROL_PROGRESS:
539 ast_verb(3, "%s is making progress, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
540 ast_indicate(chan, AST_CONTROL_PROGRESS);
541 set_state(dial, AST_DIAL_RESULT_PROGRESS);
543 case AST_CONTROL_VIDUPDATE:
544 ast_verb(3, "%s requested a video update, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
545 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
547 case AST_CONTROL_SRCUPDATE:
548 ast_verb(3, "%s requested a source update, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
549 ast_indicate(chan, AST_CONTROL_SRCUPDATE);
551 case AST_CONTROL_CONNECTED_LINE:
552 ast_verb(3, "%s connected line has changed, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
553 if (ast_channel_connected_line_sub(channel->owner, chan, fr, 1) &&
554 ast_channel_connected_line_macro(channel->owner, chan, fr, 1, 1)) {
555 ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, fr->data.ptr, fr->datalen);
558 case AST_CONTROL_REDIRECTING:
559 ast_verb(3, "%s redirecting info has changed, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
560 if (ast_channel_redirecting_sub(channel->owner, chan, fr, 1) &&
561 ast_channel_redirecting_macro(channel->owner, chan, fr, 1, 1)) {
562 ast_indicate_data(chan, AST_CONTROL_REDIRECTING, fr->data.ptr, fr->datalen);
565 case AST_CONTROL_PROCEEDING:
566 ast_verb(3, "%s is proceeding, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
567 ast_indicate(chan, AST_CONTROL_PROCEEDING);
568 set_state(dial, AST_DIAL_RESULT_PROCEEDING);
570 case AST_CONTROL_HOLD:
571 ast_verb(3, "Call on %s placed on hold\n", ast_channel_name(chan));
572 ast_indicate(chan, AST_CONTROL_HOLD);
574 case AST_CONTROL_UNHOLD:
575 ast_verb(3, "Call on %s left from hold\n", ast_channel_name(chan));
576 ast_indicate(chan, AST_CONTROL_UNHOLD);
578 case AST_CONTROL_OFFHOOK:
579 case AST_CONTROL_FLASH:
581 case AST_CONTROL_PVT_CAUSE_CODE:
582 ast_indicate_data(chan, AST_CONTROL_PVT_CAUSE_CODE, fr->data.ptr, fr->datalen);
585 /* Prod the channel */
586 ast_indicate(chan, -1);
596 /*! \brief Helper function that handles control frames WITHOUT owner */
597 static void handle_frame_ownerless(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr)
599 /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
600 if (fr->frametype != AST_FRAME_CONTROL)
603 switch (fr->subclass.integer) {
604 case AST_CONTROL_ANSWER:
605 ast_verb(3, "%s answered\n", ast_channel_name(channel->owner));
606 AST_LIST_LOCK(&dial->channels);
607 AST_LIST_REMOVE(&dial->channels, channel, list);
608 AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
609 AST_LIST_UNLOCK(&dial->channels);
610 ast_channel_publish_dial(NULL, channel->owner, channel->device, "ANSWER");
611 set_state(dial, AST_DIAL_RESULT_ANSWERED);
613 case AST_CONTROL_BUSY:
614 ast_verb(3, "%s is busy\n", ast_channel_name(channel->owner));
615 ast_channel_publish_dial(NULL, channel->owner, channel->device, "BUSY");
616 ast_hangup(channel->owner);
617 channel->owner = NULL;
619 case AST_CONTROL_CONGESTION:
620 ast_verb(3, "%s is circuit-busy\n", ast_channel_name(channel->owner));
621 ast_channel_publish_dial(NULL, channel->owner, channel->device, "CONGESTION");
622 ast_hangup(channel->owner);
623 channel->owner = NULL;
625 case AST_CONTROL_RINGING:
626 ast_verb(3, "%s is ringing\n", ast_channel_name(channel->owner));
627 set_state(dial, AST_DIAL_RESULT_RINGING);
629 case AST_CONTROL_PROGRESS:
630 ast_verb(3, "%s is making progress\n", ast_channel_name(channel->owner));
631 set_state(dial, AST_DIAL_RESULT_PROGRESS);
633 case AST_CONTROL_PROCEEDING:
634 ast_verb(3, "%s is proceeding\n", ast_channel_name(channel->owner));
635 set_state(dial, AST_DIAL_RESULT_PROCEEDING);
644 /*! \brief Helper function to handle when a timeout occurs on dialing attempt */
645 static int handle_timeout_trip(struct ast_dial *dial, struct timeval start)
647 struct ast_dial_channel *channel = NULL;
648 int diff = ast_tvdiff_ms(ast_tvnow(), start), lowest_timeout = -1, new_timeout = -1;
650 /* If there is no difference yet return the dial timeout so we can go again, we were likely interrupted */
652 return dial->timeout;
655 /* If the global dial timeout tripped switch the state to timeout so our channel loop will drop every channel */
656 if (diff >= dial->timeout) {
657 set_state(dial, AST_DIAL_RESULT_TIMEOUT);
661 /* Go through dropping out channels that have met their timeout */
662 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
663 if (dial->state == AST_DIAL_RESULT_TIMEOUT || diff >= channel->timeout) {
664 ast_hangup(channel->owner);
665 channel->owner = NULL;
666 } else if ((lowest_timeout == -1) || (lowest_timeout > channel->timeout)) {
667 lowest_timeout = channel->timeout;
671 /* Calculate the new timeout using the lowest timeout found */
672 if (lowest_timeout >= 0)
673 new_timeout = lowest_timeout - diff;
678 const char *ast_hangup_cause_to_dial_status(int hangup_cause)
680 switch(hangup_cause) {
683 case AST_CAUSE_CONGESTION:
685 case AST_CAUSE_NO_ROUTE_DESTINATION:
686 case AST_CAUSE_UNREGISTERED:
687 return "CHANUNAVAIL";
688 case AST_CAUSE_NO_ANSWER:
694 /*! \brief Helper function that basically keeps tabs on dialing attempts */
695 static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
698 struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
699 struct ast_dial_channel *channel = NULL;
700 struct answer_exec_struct *answer_exec = NULL;
701 struct timeval start;
703 set_state(dial, AST_DIAL_RESULT_TRYING);
705 /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
706 if (dial->options[AST_DIAL_OPTION_RINGING]) {
707 set_state(dial, AST_DIAL_RESULT_RINGING);
709 ast_indicate(chan, AST_CONTROL_RINGING);
710 } else if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
711 !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
712 char *original_moh = ast_strdupa(ast_channel_musicclass(chan));
713 ast_indicate(chan, -1);
714 ast_channel_musicclass_set(chan, dial->options[AST_DIAL_OPTION_MUSIC]);
715 ast_moh_start(chan, dial->options[AST_DIAL_OPTION_MUSIC], NULL);
716 ast_channel_musicclass_set(chan, original_moh);
719 /* Record start time for timeout purposes */
722 /* We actually figured out the maximum timeout we can do as they were added, so we can directly access the info */
723 timeout = dial->actual_timeout;
725 /* Go into an infinite loop while we are trying */
726 while ((dial->state != AST_DIAL_RESULT_UNANSWERED) && (dial->state != AST_DIAL_RESULT_ANSWERED) && (dial->state != AST_DIAL_RESULT_HANGUP) && (dial->state != AST_DIAL_RESULT_TIMEOUT)) {
727 int pos = 0, count = 0;
728 struct ast_frame *fr = NULL;
730 /* Set up channel structure array */
735 /* Add channels we are attempting to dial */
736 AST_LIST_LOCK(&dial->channels);
737 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
738 if (channel->owner) {
739 cs[pos++] = channel->owner;
743 AST_LIST_UNLOCK(&dial->channels);
745 /* If we have no outbound channels in progress, switch state to unanswered and stop */
747 set_state(dial, AST_DIAL_RESULT_UNANSWERED);
751 /* Just to be safe... */
752 if (dial->thread == AST_PTHREADT_STOP)
755 /* Wait for frames from channels */
756 who = ast_waitfor_n(cs, pos, &timeout);
758 /* Check to see if our thread is being canceled */
759 if (dial->thread == AST_PTHREADT_STOP)
762 /* If the timeout no longer exists OR if we got no channel it basically means the timeout was tripped, so handle it */
763 if (!timeout || !who) {
764 timeout = handle_timeout_trip(dial, start);
768 /* Find relative dial channel */
769 if (!chan || !IS_CALLER(chan, who))
770 channel = find_relative_dial_channel(dial, who);
772 /* See if this channel has been forwarded elsewhere */
773 if (!ast_strlen_zero(ast_channel_call_forward(who))) {
774 handle_call_forward(dial, channel, chan);
778 /* Attempt to read in a frame */
779 if (!(fr = ast_read(who))) {
780 /* If this is the caller then we switch state to hangup and stop */
781 if (chan && IS_CALLER(chan, who)) {
782 set_state(dial, AST_DIAL_RESULT_HANGUP);
786 ast_poll_channel_del(chan, channel->owner);
787 ast_channel_publish_dial(chan, who, channel->device, ast_hangup_cause_to_dial_status(ast_channel_hangupcause(who)));
789 channel->owner = NULL;
793 /* Process the frame */
795 handle_frame(dial, channel, fr, chan);
797 handle_frame_ownerless(dial, channel, fr);
799 /* Free the received frame and start all over */
803 /* Do post-processing from loop */
804 if (dial->state == AST_DIAL_RESULT_ANSWERED) {
805 /* Hangup everything except that which answered */
806 AST_LIST_LOCK(&dial->channels);
807 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
808 if (!channel->owner || channel->owner == who)
811 ast_poll_channel_del(chan, channel->owner);
812 ast_channel_publish_dial(chan, channel->owner, channel->device, "CANCEL");
813 ast_hangup(channel->owner);
814 channel->owner = NULL;
816 AST_LIST_UNLOCK(&dial->channels);
817 /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
818 if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC))) {
819 channel->is_running_app = 1;
820 answer_exec_run(dial, channel, answer_exec->app, answer_exec->args);
821 channel->is_running_app = 0;
824 if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
825 !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
828 } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
829 /* Hangup everything */
830 AST_LIST_LOCK(&dial->channels);
831 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
835 ast_poll_channel_del(chan, channel->owner);
836 ast_channel_publish_dial(chan, channel->owner, channel->device, "CANCEL");
837 ast_hangup(channel->owner);
838 channel->owner = NULL;
840 AST_LIST_UNLOCK(&dial->channels);
846 /*! \brief Dial async thread function */
847 static void *async_dial(void *data)
849 struct ast_dial *dial = data;
851 ast_callid_threadassoc_add(dial->callid);
854 /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
855 monitor_dial(dial, NULL);
860 /*! \brief Execute dialing synchronously or asynchronously
861 * \note Dials channels in a dial structure.
862 * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
864 enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
866 enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
868 /* Ensure required arguments are passed */
870 ast_debug(1, "invalid #1\n");
871 return AST_DIAL_RESULT_INVALID;
874 /* If there are no channels to dial we can't very well try to dial them */
875 if (AST_LIST_EMPTY(&dial->channels)) {
876 ast_debug(1, "invalid #2\n");
877 return AST_DIAL_RESULT_INVALID;
880 /* Dial each requested channel */
881 if (!begin_dial(dial, chan, async))
882 return AST_DIAL_RESULT_FAILED;
884 /* If we are running async spawn a thread and send it away... otherwise block here */
886 /* reference be released at dial destruction if it isn't NULL */
887 dial->callid = ast_read_threadstorage_callid();
888 dial->state = AST_DIAL_RESULT_TRYING;
889 /* Try to create a thread */
890 if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
891 /* Failed to create the thread - hangup all dialed channels and return failed */
892 ast_dial_hangup(dial);
893 res = AST_DIAL_RESULT_FAILED;
896 res = monitor_dial(dial, chan);
902 /*! \brief Return channel that answered
903 * \note Returns the Asterisk channel that answered
904 * \param dial Dialing structure
906 struct ast_channel *ast_dial_answered(struct ast_dial *dial)
911 return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
914 /*! \brief Steal the channel that answered
915 * \note Returns the Asterisk channel that answered and removes it from the dialing structure
916 * \param dial Dialing structure
918 struct ast_channel *ast_dial_answered_steal(struct ast_dial *dial)
920 struct ast_channel *chan = NULL;
925 if (dial->state == AST_DIAL_RESULT_ANSWERED) {
926 chan = AST_LIST_FIRST(&dial->channels)->owner;
927 AST_LIST_FIRST(&dial->channels)->owner = NULL;
933 /*! \brief Return state of dial
934 * \note Returns the state of the dial attempt
935 * \param dial Dialing structure
937 enum ast_dial_result ast_dial_state(struct ast_dial *dial)
942 /*! \brief Cancel async thread
943 * \note Cancel a running async thread
944 * \param dial Dialing structure
946 enum ast_dial_result ast_dial_join(struct ast_dial *dial)
950 /* If the dial structure is not running in async, return failed */
951 if (dial->thread == AST_PTHREADT_NULL)
952 return AST_DIAL_RESULT_FAILED;
955 thread = dial->thread;
957 /* Boom, commence locking */
958 ast_mutex_lock(&dial->lock);
960 /* Stop the thread */
961 dial->thread = AST_PTHREADT_STOP;
963 /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
964 AST_LIST_LOCK(&dial->channels);
965 if (AST_LIST_FIRST(&dial->channels)->is_running_app) {
966 struct ast_channel *chan = AST_LIST_FIRST(&dial->channels)->owner;
968 ast_channel_lock(chan);
969 ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
970 ast_channel_unlock(chan);
973 /* Now we signal it with SIGURG so it will break out of it's waitfor */
974 pthread_kill(thread, SIGURG);
976 AST_LIST_UNLOCK(&dial->channels);
978 /* Yay done with it */
979 ast_mutex_unlock(&dial->lock);
981 /* Finally wait for the thread to exit */
982 pthread_join(thread, NULL);
984 /* Yay thread is all gone */
985 dial->thread = AST_PTHREADT_NULL;
990 /*! \brief Hangup channels
991 * \note Hangup all active channels
992 * \param dial Dialing structure
994 void ast_dial_hangup(struct ast_dial *dial)
996 struct ast_dial_channel *channel = NULL;
1001 AST_LIST_LOCK(&dial->channels);
1002 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
1003 ast_hangup(channel->owner);
1004 channel->owner = NULL;
1006 AST_LIST_UNLOCK(&dial->channels);
1011 /*! \brief Destroys a dialing structure
1012 * \note Destroys (free's) the given ast_dial structure
1013 * \param dial Dialing structure to free
1014 * \return Returns 0 on success, -1 on failure
1016 int ast_dial_destroy(struct ast_dial *dial)
1019 struct ast_dial_channel *channel = NULL;
1024 /* Hangup and deallocate all the dialed channels */
1025 AST_LIST_LOCK(&dial->channels);
1026 AST_LIST_TRAVERSE_SAFE_BEGIN(&dial->channels, channel, list) {
1027 /* Disable any enabled options */
1028 for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
1029 if (!channel->options[i])
1031 if (option_types[i].disable)
1032 option_types[i].disable(channel->options[i]);
1033 channel->options[i] = NULL;
1036 /* Hang up channel if need be */
1037 ast_hangup(channel->owner);
1038 channel->owner = NULL;
1040 /* Free structure */
1041 ast_free(channel->tech);
1042 ast_free(channel->device);
1043 AST_LIST_REMOVE_CURRENT(list);
1046 AST_LIST_TRAVERSE_SAFE_END;
1047 AST_LIST_UNLOCK(&dial->channels);
1049 /* Disable any enabled options globally */
1050 for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
1051 if (!dial->options[i])
1053 if (option_types[i].disable)
1054 option_types[i].disable(dial->options[i]);
1055 dial->options[i] = NULL;
1059 ast_mutex_destroy(&dial->lock);
1061 /* Get rid of the reference to the ast_callid */
1063 ast_callid_unref(dial->callid);
1066 /* Free structure */
1072 /*! \brief Enables an option globally
1073 * \param dial Dial structure to enable option on
1074 * \param option Option to enable
1075 * \param data Data to pass to this option (not always needed)
1076 * \return Returns 0 on success, -1 on failure
1078 int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
1080 /* If the option is already enabled, return failure */
1081 if (dial->options[option])
1084 /* Execute enable callback if it exists, if not simply make sure the value is set */
1085 if (option_types[option].enable)
1086 dial->options[option] = option_types[option].enable(data);
1088 dial->options[option] = (void*)1;
1093 /*! \brief Helper function for finding a channel in a dial structure based on number
1095 static struct ast_dial_channel *find_dial_channel(struct ast_dial *dial, int num)
1097 struct ast_dial_channel *channel = AST_LIST_LAST(&dial->channels);
1099 /* We can try to predict programmer behavior, the last channel they added is probably the one they wanted to modify */
1100 if (channel->num == num)
1103 /* Hrm not at the end... looking through the list it is! */
1104 AST_LIST_LOCK(&dial->channels);
1105 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
1106 if (channel->num == num)
1109 AST_LIST_UNLOCK(&dial->channels);
1114 /*! \brief Enables an option per channel
1115 * \param dial Dial structure
1116 * \param num Channel number to enable option on
1117 * \param option Option to enable
1118 * \param data Data to pass to this option (not always needed)
1119 * \return Returns 0 on success, -1 on failure
1121 int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
1123 struct ast_dial_channel *channel = NULL;
1125 /* Ensure we have required arguments */
1126 if (!dial || AST_LIST_EMPTY(&dial->channels))
1129 if (!(channel = find_dial_channel(dial, num)))
1132 /* If the option is already enabled, return failure */
1133 if (channel->options[option])
1136 /* Execute enable callback if it exists, if not simply make sure the value is set */
1137 if (option_types[option].enable)
1138 channel->options[option] = option_types[option].enable(data);
1140 channel->options[option] = (void*)1;
1145 /*! \brief Disables an option globally
1146 * \param dial Dial structure to disable option on
1147 * \param option Option to disable
1148 * \return Returns 0 on success, -1 on failure
1150 int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
1152 /* If the option is not enabled, return failure */
1153 if (!dial->options[option]) {
1157 /* Execute callback of option to disable if it exists */
1158 if (option_types[option].disable)
1159 option_types[option].disable(dial->options[option]);
1161 /* Finally disable option on the structure */
1162 dial->options[option] = NULL;
1167 /*! \brief Disables an option per channel
1168 * \param dial Dial structure
1169 * \param num Channel number to disable option on
1170 * \param option Option to disable
1171 * \return Returns 0 on success, -1 on failure
1173 int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
1175 struct ast_dial_channel *channel = NULL;
1177 /* Ensure we have required arguments */
1178 if (!dial || AST_LIST_EMPTY(&dial->channels))
1181 if (!(channel = find_dial_channel(dial, num)))
1184 /* If the option is not enabled, return failure */
1185 if (!channel->options[option])
1188 /* Execute callback of option to disable it if it exists */
1189 if (option_types[option].disable)
1190 option_types[option].disable(channel->options[option]);
1192 /* Finally disable the option on the structure */
1193 channel->options[option] = NULL;
1198 int ast_dial_reason(struct ast_dial *dial, int num)
1200 struct ast_dial_channel *channel;
1202 if (!dial || AST_LIST_EMPTY(&dial->channels) || !(channel = find_dial_channel(dial, num))) {
1206 return channel->cause;
1209 struct ast_channel *ast_dial_get_channel(struct ast_dial *dial, int num)
1211 struct ast_dial_channel *channel;
1213 if (!dial || AST_LIST_EMPTY(&dial->channels) || !(channel = find_dial_channel(dial, num))) {
1217 return channel->owner;
1220 void ast_dial_set_state_callback(struct ast_dial *dial, ast_dial_state_callback callback)
1222 dial->state_callback = callback;
1225 void ast_dial_set_user_data(struct ast_dial *dial, void *user_data)
1227 dial->user_data = user_data;
1230 void *ast_dial_get_user_data(struct ast_dial *dial)
1232 return dial->user_data;
1235 /*! \brief Set the maximum time (globally) allowed for trying to ring phones
1236 * \param dial The dial structure to apply the time limit to
1237 * \param timeout Maximum time allowed
1240 void ast_dial_set_global_timeout(struct ast_dial *dial, int timeout)
1242 dial->timeout = timeout;
1244 if (dial->timeout > 0 && (dial->actual_timeout > dial->timeout || dial->actual_timeout == -1))
1245 dial->actual_timeout = dial->timeout;
1250 /*! \brief Set the maximum time (per channel) allowed for trying to ring the phone
1251 * \param dial The dial structure the channel belongs to
1252 * \param num Channel number to set timeout on
1253 * \param timeout Maximum time allowed
1256 void ast_dial_set_timeout(struct ast_dial *dial, int num, int timeout)
1258 struct ast_dial_channel *channel = NULL;
1260 if (!(channel = find_dial_channel(dial, num)))
1263 channel->timeout = timeout;
1265 if (channel->timeout > 0 && (dial->actual_timeout > channel->timeout || dial->actual_timeout == -1))
1266 dial->actual_timeout = channel->timeout;