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>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/channel.h"
34 #include "asterisk/utils.h"
35 #include "asterisk/lock.h"
36 #include "asterisk/linkedlists.h"
37 #include "asterisk/dial.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/musiconhold.h"
41 /*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
43 int num; /*!< Current number to give to next dialed channel */
44 int timeout; /*!< Maximum time allowed for dial attempts */
45 int actual_timeout; /*!< Actual timeout based on all factors (ie: channels) */
46 enum ast_dial_result state; /*!< Status of dial */
47 void *options[AST_DIAL_OPTION_MAX]; /*!< Global options */
48 ast_dial_state_callback state_callback; /*!< Status callback */
49 AST_LIST_HEAD(, ast_dial_channel) channels; /*!< Channels being dialed */
50 pthread_t thread; /*!< Thread (if running in async) */
51 ast_mutex_t lock; /*! Lock to protect the thread information above */
54 /*! \brief Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! */
55 struct ast_dial_channel {
56 int num; /*!< Unique number for dialed channel */
57 int timeout; /*!< Maximum time allowed for attempt */
58 char *tech; /*!< Technology being dialed */
59 char *device; /*!< Device being dialed */
60 void *options[AST_DIAL_OPTION_MAX]; /*!< Channel specific options */
61 int cause; /*!< Cause code in case of failure */
62 int is_running_app:1; /*!< Is this running an application? */
63 struct ast_channel *owner; /*!< Asterisk channel */
64 AST_LIST_ENTRY(ast_dial_channel) list; /*!< Linked list information */
67 /*! \brief Typedef for dial option enable */
68 typedef void *(*ast_dial_option_cb_enable)(void *data);
70 /*! \brief Typedef for dial option disable */
71 typedef int (*ast_dial_option_cb_disable)(void *data);
73 /*! \brief Structure for 'ANSWER_EXEC' option */
74 struct answer_exec_struct {
75 char app[AST_MAX_APP]; /*!< Application name */
76 char *args; /*!< Application arguments */
79 /*! \brief Enable function for 'ANSWER_EXEC' option */
80 static void *answer_exec_enable(void *data)
82 struct answer_exec_struct *answer_exec = NULL;
83 char *app = ast_strdupa((char*)data), *args = NULL;
85 /* Not giving any data to this option is bad, mmmk? */
86 if (ast_strlen_zero(app))
89 /* Create new data structure */
90 if (!(answer_exec = ast_calloc(1, sizeof(*answer_exec))))
93 /* Parse out application and arguments */
94 if ((args = strchr(app, ','))) {
96 answer_exec->args = ast_strdup(args);
99 /* Copy application name */
100 ast_copy_string(answer_exec->app, app, sizeof(answer_exec->app));
105 /*! \brief Disable function for 'ANSWER_EXEC' option */
106 static int answer_exec_disable(void *data)
108 struct answer_exec_struct *answer_exec = data;
110 /* Make sure we have a value */
114 /* If arguments are present, free them too */
115 if (answer_exec->args)
116 ast_free(answer_exec->args);
118 /* This is simple - just free the structure */
119 ast_free(answer_exec);
124 static void *music_enable(void *data)
126 return ast_strdup(data);
129 static int music_disable(void *data)
139 /*! \brief Application execution function for 'ANSWER_EXEC' option */
140 static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
142 struct ast_channel *chan = dial_channel->owner;
143 struct ast_app *ast_app = pbx_findapp(app);
145 /* If the application was not found, return immediately */
149 /* All is well... execute the application */
150 pbx_exec(chan, ast_app, args);
152 /* If another thread is not taking over hang up the channel */
153 ast_mutex_lock(&dial->lock);
154 if (dial->thread != AST_PTHREADT_STOP) {
156 dial_channel->owner = NULL;
158 ast_mutex_unlock(&dial->lock);
163 /*! \brief Options structure - maps options to respective handlers (enable/disable). This list MUST be perfectly kept in order, or else madness will happen. */
164 static const struct ast_option_types {
165 enum ast_dial_option option;
166 ast_dial_option_cb_enable enable;
167 ast_dial_option_cb_disable disable;
169 { AST_DIAL_OPTION_RINGING, NULL, NULL }, /*!< Always indicate ringing to caller */
170 { AST_DIAL_OPTION_ANSWER_EXEC, answer_exec_enable, answer_exec_disable }, /*!< Execute application upon answer in async mode */
171 { AST_DIAL_OPTION_MUSIC, music_enable, music_disable }, /*!< Play music to the caller instead of ringing */
172 { AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL, NULL }, /*!< Disable call forwarding on channels */
173 { AST_DIAL_OPTION_MAX, NULL, NULL }, /*!< Terminator of list */
176 /*! \brief free the buffer if allocated, and set the pointer to the second arg */
177 #define S_REPLACE(s, new_val) \
185 /*! \brief Maximum number of channels we can watch at a time */
186 #define AST_MAX_WATCHERS 256
188 /*! \brief Macro for finding the option structure to use on a dialed channel */
189 #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])
191 /*! \brief Macro that determines whether a channel is the caller or not */
192 #define IS_CALLER(chan, owner) (chan == owner ? 1 : 0)
194 /*! \brief New dialing structure
195 * \note Create a dialing structure
196 * \return Returns a calloc'd ast_dial structure, NULL on failure
198 struct ast_dial *ast_dial_create(void)
200 struct ast_dial *dial = NULL;
202 /* Allocate new memory for structure */
203 if (!(dial = ast_calloc(1, sizeof(*dial))))
206 /* Initialize list of channels */
207 AST_LIST_HEAD_INIT(&dial->channels);
209 /* Initialize thread to NULL */
210 dial->thread = AST_PTHREADT_NULL;
212 /* No timeout exists... yet */
214 dial->actual_timeout = -1;
216 /* Can't forget about the lock */
217 ast_mutex_init(&dial->lock);
222 /*! \brief Append a channel
223 * \note Appends a channel to a dialing structure
224 * \return Returns channel reference number on success, -1 on failure
226 int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device)
228 struct ast_dial_channel *channel = NULL;
230 /* Make sure we have required arguments */
231 if (!dial || !tech || !device)
234 /* Allocate new memory for dialed channel structure */
235 if (!(channel = ast_calloc(1, sizeof(*channel))))
238 /* Record technology and device for when we actually dial */
239 channel->tech = ast_strdup(tech);
240 channel->device = ast_strdup(device);
242 /* Grab reference number from dial structure */
243 channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
245 /* No timeout exists... yet */
246 channel->timeout = -1;
248 /* Insert into channels list */
249 AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
254 /*! \brief Helper function that does the beginning dialing per-appended channel */
255 static int begin_dial_channel(struct ast_dial_channel *channel, struct ast_channel *chan)
257 char numsubst[AST_MAX_EXTENSION];
260 /* Copy device string over */
261 ast_copy_string(numsubst, channel->device, sizeof(numsubst));
263 /* If we fail to create our owner channel bail out */
264 if (!(channel->owner = ast_request(channel->tech, chan ? chan->nativeformats : AST_FORMAT_AUDIO_MASK, numsubst, &channel->cause)))
267 channel->owner->appl = "AppDial2";
268 channel->owner->data = "(Outgoing Line)";
269 memset(&channel->owner->whentohangup, 0, sizeof(channel->owner->whentohangup));
271 /* Inherit everything from he who spawned this dial */
273 ast_channel_inherit_variables(chan, channel->owner);
275 /* Copy over callerid information */
276 S_REPLACE(channel->owner->cid.cid_num, ast_strdup(chan->cid.cid_num));
277 S_REPLACE(channel->owner->cid.cid_name, ast_strdup(chan->cid.cid_name));
278 S_REPLACE(channel->owner->cid.cid_ani, ast_strdup(chan->cid.cid_ani));
279 S_REPLACE(channel->owner->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
281 ast_string_field_set(channel->owner, language, chan->language);
282 ast_string_field_set(channel->owner, accountcode, chan->accountcode);
283 channel->owner->cdrflags = chan->cdrflags;
284 if (ast_strlen_zero(channel->owner->musicclass))
285 ast_string_field_set(channel->owner, musicclass, chan->musicclass);
287 channel->owner->cid.cid_pres = chan->cid.cid_pres;
288 channel->owner->cid.cid_ton = chan->cid.cid_ton;
289 channel->owner->cid.cid_tns = chan->cid.cid_tns;
290 channel->owner->adsicpe = chan->adsicpe;
291 channel->owner->transfercapability = chan->transfercapability;
294 /* Attempt to actually call this device */
295 if ((res = ast_call(channel->owner, numsubst, 0))) {
297 ast_hangup(channel->owner);
298 channel->owner = NULL;
301 ast_poll_channel_add(chan, channel->owner);
303 ast_verb(3, "Called %s\n", numsubst);
309 /*! \brief Helper function that does the beginning dialing per dial structure */
310 static int begin_dial(struct ast_dial *dial, struct ast_channel *chan)
312 struct ast_dial_channel *channel = NULL;
315 /* Iterate through channel list, requesting and calling each one */
316 AST_LIST_LOCK(&dial->channels);
317 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
318 success += begin_dial_channel(channel, chan);
320 AST_LIST_UNLOCK(&dial->channels);
322 /* If number of failures matches the number of channels, then this truly failed */
326 /*! \brief Helper function to handle channels that have been call forwarded */
327 static int handle_call_forward(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_channel *chan)
329 struct ast_channel *original = channel->owner;
330 char *tmp = ast_strdupa(channel->owner->call_forward);
331 char *tech = "Local", *device = tmp, *stuff;
333 /* If call forwarding is disabled just drop the original channel and don't attempt to dial the new one */
334 if (FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING)) {
335 ast_hangup(original);
336 channel->owner = NULL;
340 /* Figure out the new destination */
341 if ((stuff = strchr(tmp, '/'))) {
347 /* Drop old destination information */
348 ast_free(channel->tech);
349 ast_free(channel->device);
351 /* Update the dial channel with the new destination information */
352 channel->tech = ast_strdup(tech);
353 channel->device = ast_strdup(device);
354 AST_LIST_UNLOCK(&dial->channels);
356 /* Finally give it a go... send it out into the world */
357 begin_dial_channel(channel, chan);
359 /* Drop the original channel */
360 ast_hangup(original);
365 /*! \brief Helper function that finds the dialed channel based on owner */
366 static struct ast_dial_channel *find_relative_dial_channel(struct ast_dial *dial, struct ast_channel *owner)
368 struct ast_dial_channel *channel = NULL;
370 AST_LIST_LOCK(&dial->channels);
371 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
372 if (channel->owner == owner)
375 AST_LIST_UNLOCK(&dial->channels);
380 static void set_state(struct ast_dial *dial, enum ast_dial_result state)
384 if (dial->state_callback)
385 dial->state_callback(dial);
388 /*! \brief Helper function that handles control frames WITH owner */
389 static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
391 if (fr->frametype == AST_FRAME_CONTROL) {
392 switch (fr->subclass) {
393 case AST_CONTROL_ANSWER:
394 ast_verb(3, "%s answered %s\n", channel->owner->name, chan->name);
395 AST_LIST_LOCK(&dial->channels);
396 AST_LIST_REMOVE(&dial->channels, channel, list);
397 AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
398 AST_LIST_UNLOCK(&dial->channels);
399 set_state(dial, AST_DIAL_RESULT_ANSWERED);
401 case AST_CONTROL_BUSY:
402 ast_verb(3, "%s is busy\n", channel->owner->name);
403 ast_hangup(channel->owner);
404 channel->owner = NULL;
406 case AST_CONTROL_CONGESTION:
407 ast_verb(3, "%s is circuit-busy\n", channel->owner->name);
408 ast_hangup(channel->owner);
409 channel->owner = NULL;
411 case AST_CONTROL_RINGING:
412 ast_verb(3, "%s is ringing\n", channel->owner->name);
413 if (!dial->options[AST_DIAL_OPTION_MUSIC])
414 ast_indicate(chan, AST_CONTROL_RINGING);
415 set_state(dial, AST_DIAL_RESULT_RINGING);
417 case AST_CONTROL_PROGRESS:
418 ast_verb(3, "%s is making progress, passing it to %s\n", channel->owner->name, chan->name);
419 ast_indicate(chan, AST_CONTROL_PROGRESS);
420 set_state(dial, AST_DIAL_RESULT_PROGRESS);
422 case AST_CONTROL_VIDUPDATE:
423 ast_verb(3, "%s requested a video update, passing it to %s\n", channel->owner->name, chan->name);
424 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
426 case AST_CONTROL_SRCUPDATE:
427 if (option_verbose > 2)
428 ast_verbose (VERBOSE_PREFIX_3 "%s requested a source update, passing it to %s\n", channel->owner->name, chan->name);
429 ast_indicate(chan, AST_CONTROL_SRCUPDATE);
431 case AST_CONTROL_PROCEEDING:
432 ast_verb(3, "%s is proceeding, passing it to %s\n", channel->owner->name, chan->name);
433 ast_indicate(chan, AST_CONTROL_PROCEEDING);
434 set_state(dial, AST_DIAL_RESULT_PROCEEDING);
436 case AST_CONTROL_HOLD:
437 ast_verb(3, "Call on %s placed on hold\n", chan->name);
438 ast_indicate(chan, AST_CONTROL_HOLD);
440 case AST_CONTROL_UNHOLD:
441 ast_verb(3, "Call on %s left from hold\n", chan->name);
442 ast_indicate(chan, AST_CONTROL_UNHOLD);
444 case AST_CONTROL_OFFHOOK:
445 case AST_CONTROL_FLASH:
448 /* Prod the channel */
449 ast_indicate(chan, -1);
459 /*! \brief Helper function that handles control frames WITHOUT owner */
460 static void handle_frame_ownerless(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr)
462 /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
463 if (fr->frametype != AST_FRAME_CONTROL)
466 switch (fr->subclass) {
467 case AST_CONTROL_ANSWER:
468 ast_verb(3, "%s answered\n", channel->owner->name);
469 AST_LIST_LOCK(&dial->channels);
470 AST_LIST_REMOVE(&dial->channels, channel, list);
471 AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
472 AST_LIST_UNLOCK(&dial->channels);
473 set_state(dial, AST_DIAL_RESULT_ANSWERED);
475 case AST_CONTROL_BUSY:
476 ast_verb(3, "%s is busy\n", channel->owner->name);
477 ast_hangup(channel->owner);
478 channel->owner = NULL;
480 case AST_CONTROL_CONGESTION:
481 ast_verb(3, "%s is circuit-busy\n", channel->owner->name);
482 ast_hangup(channel->owner);
483 channel->owner = NULL;
485 case AST_CONTROL_RINGING:
486 ast_verb(3, "%s is ringing\n", channel->owner->name);
487 set_state(dial, AST_DIAL_RESULT_RINGING);
489 case AST_CONTROL_PROGRESS:
490 ast_verb(3, "%s is making progress\n", channel->owner->name);
491 set_state(dial, AST_DIAL_RESULT_PROGRESS);
493 case AST_CONTROL_PROCEEDING:
494 ast_verb(3, "%s is proceeding\n", channel->owner->name);
495 set_state(dial, AST_DIAL_RESULT_PROCEEDING);
504 /*! \brief Helper function to handle when a timeout occurs on dialing attempt */
505 static int handle_timeout_trip(struct ast_dial *dial, struct timeval start)
507 struct ast_dial_channel *channel = NULL;
508 int diff = ast_tvdiff_ms(ast_tvnow(), start), lowest_timeout = -1, new_timeout = -1;
510 /* If the global dial timeout tripped switch the state to timeout so our channel loop will drop every channel */
511 if (diff >= dial->timeout) {
512 set_state(dial, AST_DIAL_RESULT_TIMEOUT);
516 /* Go through dropping out channels that have met their timeout */
517 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
518 if (dial->state == AST_DIAL_RESULT_TIMEOUT || diff >= channel->timeout) {
519 ast_hangup(channel->owner);
520 channel->owner = NULL;
521 } else if ((lowest_timeout == -1) || (lowest_timeout > channel->timeout)) {
522 lowest_timeout = channel->timeout;
526 /* Calculate the new timeout using the lowest timeout found */
527 if (lowest_timeout >= 0)
528 new_timeout = lowest_timeout - diff;
533 /*! \brief Helper function that basically keeps tabs on dialing attempts */
534 static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
537 struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
538 struct ast_dial_channel *channel = NULL;
539 struct answer_exec_struct *answer_exec = NULL;
540 struct timeval start;
542 set_state(dial, AST_DIAL_RESULT_TRYING);
544 /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
545 if (dial->options[AST_DIAL_OPTION_RINGING]) {
546 set_state(dial, AST_DIAL_RESULT_RINGING);
548 ast_indicate(chan, AST_CONTROL_RINGING);
549 } else if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
550 !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
551 char *original_moh = ast_strdupa(chan->musicclass);
552 ast_indicate(chan, -1);
553 ast_string_field_set(chan, musicclass, dial->options[AST_DIAL_OPTION_MUSIC]);
554 ast_moh_start(chan, dial->options[AST_DIAL_OPTION_MUSIC], NULL);
555 ast_string_field_set(chan, musicclass, original_moh);
558 /* Record start time for timeout purposes */
561 /* We actually figured out the maximum timeout we can do as they were added, so we can directly access the info */
562 timeout = dial->actual_timeout;
564 /* Go into an infinite loop while we are trying */
565 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)) {
566 int pos = 0, count = 0;
567 struct ast_frame *fr = NULL;
569 /* Set up channel structure array */
574 /* Add channels we are attempting to dial */
575 AST_LIST_LOCK(&dial->channels);
576 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
577 if (channel->owner) {
578 cs[pos++] = channel->owner;
582 AST_LIST_UNLOCK(&dial->channels);
584 /* If we have no outbound channels in progress, switch state to unanswered and stop */
586 set_state(dial, AST_DIAL_RESULT_UNANSWERED);
590 /* Just to be safe... */
591 if (dial->thread == AST_PTHREADT_STOP)
594 /* Wait for frames from channels */
595 who = ast_waitfor_n(cs, pos, &timeout);
597 /* Check to see if our thread is being cancelled */
598 if (dial->thread == AST_PTHREADT_STOP)
601 /* If the timeout no longer exists OR if we got no channel it basically means the timeout was tripped, so handle it */
602 if (!timeout || !who) {
603 timeout = handle_timeout_trip(dial, start);
607 /* Find relative dial channel */
608 if (!chan || !IS_CALLER(chan, who))
609 channel = find_relative_dial_channel(dial, who);
611 /* See if this channel has been forwarded elsewhere */
612 if (!ast_strlen_zero(who->call_forward)) {
613 handle_call_forward(dial, channel, chan);
617 /* Attempt to read in a frame */
618 if (!(fr = ast_read(who))) {
619 /* If this is the caller then we switch state to hangup and stop */
620 if (chan && IS_CALLER(chan, who)) {
621 set_state(dial, AST_DIAL_RESULT_HANGUP);
625 ast_poll_channel_del(chan, channel->owner);
627 channel->owner = NULL;
631 /* Process the frame */
633 handle_frame(dial, channel, fr, chan);
635 handle_frame_ownerless(dial, channel, fr);
637 /* Free the received frame and start all over */
641 /* Do post-processing from loop */
642 if (dial->state == AST_DIAL_RESULT_ANSWERED) {
643 /* Hangup everything except that which answered */
644 AST_LIST_LOCK(&dial->channels);
645 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
646 if (!channel->owner || channel->owner == who)
649 ast_poll_channel_del(chan, channel->owner);
650 ast_hangup(channel->owner);
651 channel->owner = NULL;
653 AST_LIST_UNLOCK(&dial->channels);
654 /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
655 if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC))) {
656 channel->is_running_app = 1;
657 answer_exec_run(dial, channel, answer_exec->app, answer_exec->args);
658 channel->is_running_app = 0;
661 if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
662 !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
665 } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
666 /* Hangup everything */
667 AST_LIST_LOCK(&dial->channels);
668 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
672 ast_poll_channel_del(chan, channel->owner);
673 ast_hangup(channel->owner);
674 channel->owner = NULL;
676 AST_LIST_UNLOCK(&dial->channels);
682 /*! \brief Dial async thread function */
683 static void *async_dial(void *data)
685 struct ast_dial *dial = data;
687 /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
688 monitor_dial(dial, NULL);
693 /*! \brief Execute dialing synchronously or asynchronously
694 * \note Dials channels in a dial structure.
695 * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
697 enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
699 enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
701 /* Ensure required arguments are passed */
702 if (!dial || (!chan && !async)) {
703 ast_debug(1, "invalid #1\n");
704 return AST_DIAL_RESULT_INVALID;
707 /* If there are no channels to dial we can't very well try to dial them */
708 if (AST_LIST_EMPTY(&dial->channels)) {
709 ast_debug(1, "invalid #2\n");
710 return AST_DIAL_RESULT_INVALID;
713 /* Dial each requested channel */
714 if (!begin_dial(dial, chan))
715 return AST_DIAL_RESULT_FAILED;
717 /* If we are running async spawn a thread and send it away... otherwise block here */
719 dial->state = AST_DIAL_RESULT_TRYING;
720 /* Try to create a thread */
721 if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
722 /* Failed to create the thread - hangup all dialed channels and return failed */
723 ast_dial_hangup(dial);
724 res = AST_DIAL_RESULT_FAILED;
727 res = monitor_dial(dial, chan);
733 /*! \brief Return channel that answered
734 * \note Returns the Asterisk channel that answered
735 * \param dial Dialing structure
737 struct ast_channel *ast_dial_answered(struct ast_dial *dial)
742 return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
745 /*! \brief Steal the channel that answered
746 * \note Returns the Asterisk channel that answered and removes it from the dialing structure
747 * \param dial Dialing structure
749 struct ast_channel *ast_dial_answered_steal(struct ast_dial *dial)
751 struct ast_channel *chan = NULL;
756 if (dial->state == AST_DIAL_RESULT_ANSWERED) {
757 chan = AST_LIST_FIRST(&dial->channels)->owner;
758 AST_LIST_FIRST(&dial->channels)->owner = NULL;
764 /*! \brief Return state of dial
765 * \note Returns the state of the dial attempt
766 * \param dial Dialing structure
768 enum ast_dial_result ast_dial_state(struct ast_dial *dial)
773 /*! \brief Cancel async thread
774 * \note Cancel a running async thread
775 * \param dial Dialing structure
777 enum ast_dial_result ast_dial_join(struct ast_dial *dial)
781 /* If the dial structure is not running in async, return failed */
782 if (dial->thread == AST_PTHREADT_NULL)
783 return AST_DIAL_RESULT_FAILED;
786 thread = dial->thread;
788 /* Boom, commence locking */
789 ast_mutex_lock(&dial->lock);
791 /* Stop the thread */
792 dial->thread = AST_PTHREADT_STOP;
794 /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
795 AST_LIST_LOCK(&dial->channels);
796 if (AST_LIST_FIRST(&dial->channels)->is_running_app) {
797 struct ast_channel *chan = AST_LIST_FIRST(&dial->channels)->owner;
799 ast_channel_lock(chan);
800 ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
801 ast_channel_unlock(chan);
804 /* Now we signal it with SIGURG so it will break out of it's waitfor */
805 pthread_kill(thread, SIGURG);
807 AST_LIST_UNLOCK(&dial->channels);
809 /* Yay done with it */
810 ast_mutex_unlock(&dial->lock);
812 /* Finally wait for the thread to exit */
813 pthread_join(thread, NULL);
815 /* Yay thread is all gone */
816 dial->thread = AST_PTHREADT_NULL;
821 /*! \brief Hangup channels
822 * \note Hangup all active channels
823 * \param dial Dialing structure
825 void ast_dial_hangup(struct ast_dial *dial)
827 struct ast_dial_channel *channel = NULL;
832 AST_LIST_LOCK(&dial->channels);
833 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
834 if (channel->owner) {
835 ast_hangup(channel->owner);
836 channel->owner = NULL;
839 AST_LIST_UNLOCK(&dial->channels);
844 /*! \brief Destroys a dialing structure
845 * \note Destroys (free's) the given ast_dial structure
846 * \param dial Dialing structure to free
847 * \return Returns 0 on success, -1 on failure
849 int ast_dial_destroy(struct ast_dial *dial)
852 struct ast_dial_channel *channel = NULL;
857 /* Hangup and deallocate all the dialed channels */
858 AST_LIST_LOCK(&dial->channels);
859 AST_LIST_TRAVERSE_SAFE_BEGIN(&dial->channels, channel, list) {
860 /* Disable any enabled options */
861 for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
862 if (!channel->options[i])
864 if (option_types[i].disable)
865 option_types[i].disable(channel->options[i]);
866 channel->options[i] = NULL;
868 /* Hang up channel if need be */
869 if (channel->owner) {
870 ast_hangup(channel->owner);
871 channel->owner = NULL;
874 ast_free(channel->tech);
875 ast_free(channel->device);
876 AST_LIST_REMOVE_CURRENT(list);
879 AST_LIST_TRAVERSE_SAFE_END;
880 AST_LIST_UNLOCK(&dial->channels);
882 /* Disable any enabled options globally */
883 for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
884 if (!dial->options[i])
886 if (option_types[i].disable)
887 option_types[i].disable(dial->options[i]);
888 dial->options[i] = NULL;
892 ast_mutex_destroy(&dial->lock);
900 /*! \brief Enables an option globally
901 * \param dial Dial structure to enable option on
902 * \param option Option to enable
903 * \param data Data to pass to this option (not always needed)
904 * \return Returns 0 on success, -1 on failure
906 int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
908 /* If the option is already enabled, return failure */
909 if (dial->options[option])
912 /* Execute enable callback if it exists, if not simply make sure the value is set */
913 if (option_types[option].enable)
914 dial->options[option] = option_types[option].enable(data);
916 dial->options[option] = (void*)1;
921 /*! \brief Helper function for finding a channel in a dial structure based on number
923 static struct ast_dial_channel *find_dial_channel(struct ast_dial *dial, int num)
925 struct ast_dial_channel *channel = AST_LIST_LAST(&dial->channels);
927 /* We can try to predict programmer behavior, the last channel they added is probably the one they wanted to modify */
928 if (channel->num == num)
931 /* Hrm not at the end... looking through the list it is! */
932 AST_LIST_LOCK(&dial->channels);
933 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
934 if (channel->num == num)
937 AST_LIST_UNLOCK(&dial->channels);
942 /*! \brief Enables an option per channel
943 * \param dial Dial structure
944 * \param num Channel number to enable option on
945 * \param option Option to enable
946 * \param data Data to pass to this option (not always needed)
947 * \return Returns 0 on success, -1 on failure
949 int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
951 struct ast_dial_channel *channel = NULL;
953 /* Ensure we have required arguments */
954 if (!dial || AST_LIST_EMPTY(&dial->channels))
957 if (!(channel = find_dial_channel(dial, num)))
960 /* If the option is already enabled, return failure */
961 if (channel->options[option])
964 /* Execute enable callback if it exists, if not simply make sure the value is set */
965 if (option_types[option].enable)
966 channel->options[option] = option_types[option].enable(data);
968 channel->options[option] = (void*)1;
973 /*! \brief Disables an option globally
974 * \param dial Dial structure to disable option on
975 * \param option Option to disable
976 * \return Returns 0 on success, -1 on failure
978 int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
980 /* If the option is not enabled, return failure */
981 if (!dial->options[option]) {
985 /* Execute callback of option to disable if it exists */
986 if (option_types[option].disable)
987 option_types[option].disable(dial->options[option]);
989 /* Finally disable option on the structure */
990 dial->options[option] = NULL;
995 /*! \brief Disables an option per channel
996 * \param dial Dial structure
997 * \param num Channel number to disable option on
998 * \param option Option to disable
999 * \return Returns 0 on success, -1 on failure
1001 int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
1003 struct ast_dial_channel *channel = NULL;
1005 /* Ensure we have required arguments */
1006 if (!dial || AST_LIST_EMPTY(&dial->channels))
1009 if (!(channel = find_dial_channel(dial, num)))
1012 /* If the option is not enabled, return failure */
1013 if (!channel->options[option])
1016 /* Execute callback of option to disable it if it exists */
1017 if (option_types[option].disable)
1018 option_types[option].disable(channel->options[option]);
1020 /* Finally disable the option on the structure */
1021 channel->options[option] = NULL;
1026 void ast_dial_set_state_callback(struct ast_dial *dial, ast_dial_state_callback callback)
1028 dial->state_callback = callback;
1031 /*! \brief Set the maximum time (globally) allowed for trying to ring phones
1032 * \param dial The dial structure to apply the time limit to
1033 * \param timeout Maximum time allowed
1036 void ast_dial_set_global_timeout(struct ast_dial *dial, int timeout)
1038 dial->timeout = timeout;
1040 if (dial->timeout > 0 && dial->actual_timeout > dial->timeout)
1041 dial->actual_timeout = dial->timeout;
1046 /*! \brief Set the maximum time (per channel) allowed for trying to ring the phone
1047 * \param dial The dial structure the channel belongs to
1048 * \param num Channel number to set timeout on
1049 * \param timeout Maximum time allowed
1052 void ast_dial_set_timeout(struct ast_dial *dial, int num, int timeout)
1054 struct ast_dial_channel *channel = NULL;
1056 if (!(channel = find_dial_channel(dial, num)))
1059 channel->timeout = timeout;
1061 if (channel->timeout > 0 && dial->actual_timeout > channel->timeout)
1062 dial->actual_timeout = channel->timeout;