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_NOLOCK(, 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) \
184 /*! \brief Maximum number of channels we can watch at a time */
185 #define AST_MAX_WATCHERS 256
187 /*! \brief Macro for finding the option structure to use on a dialed channel */
188 #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])
190 /*! \brief Macro that determines whether a channel is the caller or not */
191 #define IS_CALLER(chan, owner) (chan == owner ? 1 : 0)
193 /*! \brief New dialing structure
194 * \note Create a dialing structure
195 * \return Returns a calloc'd ast_dial structure, NULL on failure
197 struct ast_dial *ast_dial_create(void)
199 struct ast_dial *dial = NULL;
201 /* Allocate new memory for structure */
202 if (!(dial = ast_calloc(1, sizeof(*dial))))
205 /* Initialize list of channels */
206 AST_LIST_HEAD_INIT_NOLOCK(&dial->channels);
208 /* Initialize thread to NULL */
209 dial->thread = AST_PTHREADT_NULL;
211 /* No timeout exists... yet */
213 dial->actual_timeout = -1;
215 /* Can't forget about the lock */
216 ast_mutex_init(&dial->lock);
221 /*! \brief Append a channel
222 * \note Appends a channel to a dialing structure
223 * \return Returns channel reference number on success, -1 on failure
225 int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device)
227 struct ast_dial_channel *channel = NULL;
229 /* Make sure we have required arguments */
230 if (!dial || !tech || !device)
233 /* Allocate new memory for dialed channel structure */
234 if (!(channel = ast_calloc(1, sizeof(*channel))))
237 /* Record technology and device for when we actually dial */
238 channel->tech = ast_strdup(tech);
239 channel->device = ast_strdup(device);
241 /* Grab reference number from dial structure */
242 channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
244 /* No timeout exists... yet */
245 channel->timeout = -1;
247 /* Insert into channels list */
248 AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
253 /*! \brief Helper function that does the beginning dialing per-appended channel */
254 static int begin_dial_channel(struct ast_dial_channel *channel, struct ast_channel *chan)
256 char numsubst[AST_MAX_EXTENSION];
259 /* Copy device string over */
260 ast_copy_string(numsubst, channel->device, sizeof(numsubst));
262 /* If we fail to create our owner channel bail out */
263 if (!(channel->owner = ast_request(channel->tech, chan ? chan->nativeformats : AST_FORMAT_AUDIO_MASK, numsubst, &channel->cause)))
266 channel->owner->appl = "AppDial2";
267 channel->owner->data = "(Outgoing Line)";
268 channel->owner->whentohangup = 0;
270 /* Inherit everything from he who spawned this dial */
272 ast_channel_inherit_variables(chan, channel->owner);
274 /* Copy over callerid information */
275 S_REPLACE(channel->owner->cid.cid_num, ast_strdup(chan->cid.cid_num));
276 S_REPLACE(channel->owner->cid.cid_name, ast_strdup(chan->cid.cid_name));
277 S_REPLACE(channel->owner->cid.cid_ani, ast_strdup(chan->cid.cid_ani));
278 S_REPLACE(channel->owner->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
280 ast_string_field_set(channel->owner, language, chan->language);
281 ast_string_field_set(channel->owner, accountcode, chan->accountcode);
282 channel->owner->cdrflags = chan->cdrflags;
283 if (ast_strlen_zero(channel->owner->musicclass))
284 ast_string_field_set(channel->owner, musicclass, chan->musicclass);
286 channel->owner->cid.cid_pres = chan->cid.cid_pres;
287 channel->owner->cid.cid_ton = chan->cid.cid_ton;
288 channel->owner->cid.cid_tns = chan->cid.cid_tns;
289 channel->owner->adsicpe = chan->adsicpe;
290 channel->owner->transfercapability = chan->transfercapability;
293 /* Attempt to actually call this device */
294 if ((res = ast_call(channel->owner, numsubst, 0))) {
296 ast_hangup(channel->owner);
297 channel->owner = NULL;
300 ast_poll_channel_add(chan, channel->owner);
302 ast_verb(3, "Called %s\n", numsubst);
308 /*! \brief Helper function that does the beginning dialing per dial structure */
309 static int begin_dial(struct ast_dial *dial, struct ast_channel *chan)
311 struct ast_dial_channel *channel = NULL;
314 /* Iterate through channel list, requesting and calling each one */
315 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
316 success += begin_dial_channel(channel, chan);
319 /* If number of failures matches the number of channels, then this truly failed */
323 /*! \brief Helper function to handle channels that have been call forwarded */
324 static int handle_call_forward(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_channel *chan)
326 struct ast_channel *original = channel->owner;
327 char *tmp = ast_strdupa(channel->owner->call_forward);
328 char *tech = "Local", *device = tmp, *stuff;
330 /* If call forwarding is disabled just drop the original channel and don't attempt to dial the new one */
331 if (FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING)) {
332 ast_hangup(original);
333 channel->owner = NULL;
337 /* Figure out the new destination */
338 if ((stuff = strchr(tmp, '/'))) {
344 /* Drop old destination information */
345 ast_free(channel->tech);
346 ast_free(channel->device);
348 /* Update the dial channel with the new destination information */
349 channel->tech = ast_strdup(tech);
350 channel->device = ast_strdup(device);
352 /* Finally give it a go... send it out into the world */
353 begin_dial_channel(channel, chan);
355 /* Drop the original channel */
356 ast_hangup(original);
361 /*! \brief Helper function that finds the dialed channel based on owner */
362 static struct ast_dial_channel *find_relative_dial_channel(struct ast_dial *dial, struct ast_channel *owner)
364 struct ast_dial_channel *channel = NULL;
366 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
367 if (channel->owner == owner)
374 static void set_state(struct ast_dial *dial, enum ast_dial_result state)
378 if (dial->state_callback)
379 dial->state_callback(dial);
382 /*! \brief Helper function that handles control frames WITH owner */
383 static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
385 if (fr->frametype == AST_FRAME_CONTROL) {
386 switch (fr->subclass) {
387 case AST_CONTROL_ANSWER:
388 ast_verb(3, "%s answered %s\n", channel->owner->name, chan->name);
389 AST_LIST_REMOVE(&dial->channels, channel, list);
390 AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
391 set_state(dial, AST_DIAL_RESULT_ANSWERED);
393 case AST_CONTROL_BUSY:
394 ast_verb(3, "%s is busy\n", channel->owner->name);
395 ast_hangup(channel->owner);
396 channel->owner = NULL;
398 case AST_CONTROL_CONGESTION:
399 ast_verb(3, "%s is circuit-busy\n", channel->owner->name);
400 ast_hangup(channel->owner);
401 channel->owner = NULL;
403 case AST_CONTROL_RINGING:
404 ast_verb(3, "%s is ringing\n", channel->owner->name);
405 if (!dial->options[AST_DIAL_OPTION_MUSIC])
406 ast_indicate(chan, AST_CONTROL_RINGING);
407 set_state(dial, AST_DIAL_RESULT_RINGING);
409 case AST_CONTROL_PROGRESS:
410 ast_verb(3, "%s is making progress, passing it to %s\n", channel->owner->name, chan->name);
411 ast_indicate(chan, AST_CONTROL_PROGRESS);
412 set_state(dial, AST_DIAL_RESULT_PROGRESS);
414 case AST_CONTROL_VIDUPDATE:
415 ast_verb(3, "%s requested a video update, passing it to %s\n", channel->owner->name, chan->name);
416 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
418 case AST_CONTROL_PROCEEDING:
419 ast_verb(3, "%s is proceeding, passing it to %s\n", channel->owner->name, chan->name);
420 ast_indicate(chan, AST_CONTROL_PROCEEDING);
421 set_state(dial, AST_DIAL_RESULT_PROCEEDING);
423 case AST_CONTROL_HOLD:
424 ast_verb(3, "Call on %s placed on hold\n", chan->name);
425 ast_indicate(chan, AST_CONTROL_HOLD);
427 case AST_CONTROL_UNHOLD:
428 ast_verb(3, "Call on %s left from hold\n", chan->name);
429 ast_indicate(chan, AST_CONTROL_UNHOLD);
431 case AST_CONTROL_OFFHOOK:
432 case AST_CONTROL_FLASH:
435 /* Prod the channel */
436 ast_indicate(chan, -1);
446 /*! \brief Helper function that handles control frames WITHOUT owner */
447 static void handle_frame_ownerless(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr)
449 /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
450 if (fr->frametype != AST_FRAME_CONTROL)
453 switch (fr->subclass) {
454 case AST_CONTROL_ANSWER:
455 ast_verb(3, "%s answered\n", channel->owner->name);
456 AST_LIST_REMOVE(&dial->channels, channel, list);
457 AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
458 set_state(dial, AST_DIAL_RESULT_ANSWERED);
460 case AST_CONTROL_BUSY:
461 ast_verb(3, "%s is busy\n", channel->owner->name);
462 ast_hangup(channel->owner);
463 channel->owner = NULL;
465 case AST_CONTROL_CONGESTION:
466 ast_verb(3, "%s is circuit-busy\n", channel->owner->name);
467 ast_hangup(channel->owner);
468 channel->owner = NULL;
470 case AST_CONTROL_RINGING:
471 ast_verb(3, "%s is ringing\n", channel->owner->name);
472 set_state(dial, AST_DIAL_RESULT_RINGING);
474 case AST_CONTROL_PROGRESS:
475 ast_verb(3, "%s is making progress\n", channel->owner->name);
476 set_state(dial, AST_DIAL_RESULT_PROGRESS);
478 case AST_CONTROL_PROCEEDING:
479 ast_verb(3, "%s is proceeding\n", channel->owner->name);
480 set_state(dial, AST_DIAL_RESULT_PROCEEDING);
489 /*! \brief Helper function to handle when a timeout occurs on dialing attempt */
490 static int handle_timeout_trip(struct ast_dial *dial, struct timeval start)
492 struct ast_dial_channel *channel = NULL;
493 int diff = ast_tvdiff_ms(ast_tvnow(), start), lowest_timeout = -1, new_timeout = -1;
495 /* If the global dial timeout tripped switch the state to timeout so our channel loop will drop every channel */
496 if (diff >= dial->timeout) {
497 set_state(dial, AST_DIAL_RESULT_TIMEOUT);
501 /* Go through dropping out channels that have met their timeout */
502 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
503 if (dial->state == AST_DIAL_RESULT_TIMEOUT || diff >= channel->timeout) {
504 ast_hangup(channel->owner);
505 channel->owner = NULL;
506 } else if ((lowest_timeout == -1) || (lowest_timeout > channel->timeout)) {
507 lowest_timeout = channel->timeout;
511 /* Calculate the new timeout using the lowest timeout found */
512 if (lowest_timeout >= 0)
513 new_timeout = lowest_timeout - diff;
518 /*! \brief Helper function that basically keeps tabs on dialing attempts */
519 static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
522 struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
523 struct ast_dial_channel *channel = NULL;
524 struct answer_exec_struct *answer_exec = NULL;
525 struct timeval start;
527 set_state(dial, AST_DIAL_RESULT_TRYING);
529 /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
530 if (dial->options[AST_DIAL_OPTION_RINGING]) {
531 set_state(dial, AST_DIAL_RESULT_RINGING);
533 ast_indicate(chan, AST_CONTROL_RINGING);
534 } else if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
535 !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
536 char *original_moh = ast_strdupa(chan->musicclass);
537 ast_indicate(chan, -1);
538 ast_string_field_set(chan, musicclass, dial->options[AST_DIAL_OPTION_MUSIC]);
539 ast_moh_start(chan, dial->options[AST_DIAL_OPTION_MUSIC], NULL);
540 ast_string_field_set(chan, musicclass, original_moh);
543 /* Record start time for timeout purposes */
546 /* We actually figured out the maximum timeout we can do as they were added, so we can directly access the info */
547 timeout = dial->actual_timeout;
549 /* Go into an infinite loop while we are trying */
550 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)) {
551 int pos = 0, count = 0;
552 struct ast_frame *fr = NULL;
554 /* Set up channel structure array */
559 /* Add channels we are attempting to dial */
560 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
561 if (channel->owner) {
562 cs[pos++] = channel->owner;
567 /* If we have no outbound channels in progress, switch state to unanswered and stop */
569 set_state(dial, AST_DIAL_RESULT_UNANSWERED);
573 /* Just to be safe... */
574 if (dial->thread == AST_PTHREADT_STOP)
577 /* Wait for frames from channels */
578 who = ast_waitfor_n(cs, pos, &timeout);
580 /* Check to see if our thread is being cancelled */
581 if (dial->thread == AST_PTHREADT_STOP)
584 /* If the timeout no longer exists OR if we got no channel it basically means the timeout was tripped, so handle it */
585 if (!timeout || !who) {
586 timeout = handle_timeout_trip(dial, start);
590 /* Find relative dial channel */
591 if (!chan || !IS_CALLER(chan, who))
592 channel = find_relative_dial_channel(dial, who);
594 /* See if this channel has been forwarded elsewhere */
595 if (!ast_strlen_zero(who->call_forward)) {
596 handle_call_forward(dial, channel, chan);
600 /* Attempt to read in a frame */
601 if (!(fr = ast_read(who))) {
602 /* If this is the caller then we switch state to hangup and stop */
603 if (chan && IS_CALLER(chan, who)) {
604 set_state(dial, AST_DIAL_RESULT_HANGUP);
608 ast_poll_channel_del(chan, channel->owner);
610 channel->owner = NULL;
614 /* Process the frame */
616 handle_frame(dial, channel, fr, chan);
618 handle_frame_ownerless(dial, channel, fr);
620 /* Free the received frame and start all over */
624 /* Do post-processing from loop */
625 if (dial->state == AST_DIAL_RESULT_ANSWERED) {
626 /* Hangup everything except that which answered */
627 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
628 if (!channel->owner || channel->owner == who)
631 ast_poll_channel_del(chan, channel->owner);
632 ast_hangup(channel->owner);
633 channel->owner = NULL;
635 /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
636 if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC))) {
637 channel->is_running_app = 1;
638 answer_exec_run(dial, channel, answer_exec->app, answer_exec->args);
639 channel->is_running_app = 0;
642 if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
643 !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
646 } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
647 /* Hangup everything */
648 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
652 ast_poll_channel_del(chan, channel->owner);
653 ast_hangup(channel->owner);
654 channel->owner = NULL;
661 /*! \brief Dial async thread function */
662 static void *async_dial(void *data)
664 struct ast_dial *dial = data;
666 /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
667 monitor_dial(dial, NULL);
672 /*! \brief Execute dialing synchronously or asynchronously
673 * \note Dials channels in a dial structure.
674 * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
676 enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
678 enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
680 /* Ensure required arguments are passed */
681 if (!dial || (!chan && !async)) {
682 ast_debug(1, "invalid #1\n");
683 return AST_DIAL_RESULT_INVALID;
686 /* If there are no channels to dial we can't very well try to dial them */
687 if (AST_LIST_EMPTY(&dial->channels)) {
688 ast_debug(1, "invalid #2\n");
689 return AST_DIAL_RESULT_INVALID;
692 /* Dial each requested channel */
693 if (!begin_dial(dial, chan))
694 return AST_DIAL_RESULT_FAILED;
696 /* If we are running async spawn a thread and send it away... otherwise block here */
698 dial->state = AST_DIAL_RESULT_TRYING;
699 /* Try to create a thread */
700 if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
701 /* Failed to create the thread - hangup all dialed channels and return failed */
702 ast_dial_hangup(dial);
703 res = AST_DIAL_RESULT_FAILED;
706 res = monitor_dial(dial, chan);
712 /*! \brief Return channel that answered
713 * \note Returns the Asterisk channel that answered
714 * \param dial Dialing structure
716 struct ast_channel *ast_dial_answered(struct ast_dial *dial)
721 return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
724 /*! \brief Return state of dial
725 * \note Returns the state of the dial attempt
726 * \param dial Dialing structure
728 enum ast_dial_result ast_dial_state(struct ast_dial *dial)
733 /*! \brief Cancel async thread
734 * \note Cancel a running async thread
735 * \param dial Dialing structure
737 enum ast_dial_result ast_dial_join(struct ast_dial *dial)
741 /* If the dial structure is not running in async, return failed */
742 if (dial->thread == AST_PTHREADT_NULL)
743 return AST_DIAL_RESULT_FAILED;
746 thread = dial->thread;
748 /* Boom, commence locking */
749 ast_mutex_lock(&dial->lock);
751 /* Stop the thread */
752 dial->thread = AST_PTHREADT_STOP;
754 /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
755 if (AST_LIST_FIRST(&dial->channels)->is_running_app) {
756 struct ast_channel *chan = AST_LIST_FIRST(&dial->channels)->owner;
757 ast_channel_lock(chan);
758 ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
759 ast_channel_unlock(chan);
761 /* Now we signal it with SIGURG so it will break out of it's waitfor */
762 pthread_kill(thread, SIGURG);
765 /* Yay done with it */
766 ast_mutex_unlock(&dial->lock);
768 /* Finally wait for the thread to exit */
769 pthread_join(thread, NULL);
771 /* Yay thread is all gone */
772 dial->thread = AST_PTHREADT_NULL;
777 /*! \brief Hangup channels
778 * \note Hangup all active channels
779 * \param dial Dialing structure
781 void ast_dial_hangup(struct ast_dial *dial)
783 struct ast_dial_channel *channel = NULL;
788 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
789 if (channel->owner) {
790 ast_hangup(channel->owner);
791 channel->owner = NULL;
798 /*! \brief Destroys a dialing structure
799 * \note Destroys (free's) the given ast_dial structure
800 * \param dial Dialing structure to free
801 * \return Returns 0 on success, -1 on failure
803 int ast_dial_destroy(struct ast_dial *dial)
806 struct ast_dial_channel *channel = NULL;
811 /* Hangup and deallocate all the dialed channels */
812 AST_LIST_TRAVERSE_SAFE_BEGIN(&dial->channels, channel, list) {
813 /* Disable any enabled options */
814 for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
815 if (!channel->options[i])
817 if (option_types[i].disable)
818 option_types[i].disable(channel->options[i]);
819 channel->options[i] = NULL;
821 /* Hang up channel if need be */
822 if (channel->owner) {
823 ast_hangup(channel->owner);
824 channel->owner = NULL;
827 ast_free(channel->tech);
828 ast_free(channel->device);
829 AST_LIST_REMOVE_CURRENT(list);
832 AST_LIST_TRAVERSE_SAFE_END;
834 /* Disable any enabled options globally */
835 for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
836 if (!dial->options[i])
838 if (option_types[i].disable)
839 option_types[i].disable(dial->options[i]);
840 dial->options[i] = NULL;
844 ast_mutex_destroy(&dial->lock);
852 /*! \brief Enables an option globally
853 * \param dial Dial structure to enable option on
854 * \param option Option to enable
855 * \param data Data to pass to this option (not always needed)
856 * \return Returns 0 on success, -1 on failure
858 int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
860 /* If the option is already enabled, return failure */
861 if (dial->options[option])
864 /* Execute enable callback if it exists, if not simply make sure the value is set */
865 if (option_types[option].enable)
866 dial->options[option] = option_types[option].enable(data);
868 dial->options[option] = (void*)1;
873 /*! \brief Helper function for finding a channel in a dial structure based on number
875 static struct ast_dial_channel *find_dial_channel(struct ast_dial *dial, int num)
877 struct ast_dial_channel *channel = AST_LIST_LAST(&dial->channels);
879 /* We can try to predict programmer behavior, the last channel they added is probably the one they wanted to modify */
880 if (channel->num == num)
883 /* Hrm not at the end... looking through the list it is! */
884 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
885 if (channel->num == num)
892 /*! \brief Enables an option per channel
893 * \param dial Dial structure
894 * \param num Channel number to enable option on
895 * \param option Option to enable
896 * \param data Data to pass to this option (not always needed)
897 * \return Returns 0 on success, -1 on failure
899 int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
901 struct ast_dial_channel *channel = NULL;
903 /* Ensure we have required arguments */
904 if (!dial || AST_LIST_EMPTY(&dial->channels))
907 if (!(channel = find_dial_channel(dial, num)))
910 /* If the option is already enabled, return failure */
911 if (channel->options[option])
914 /* Execute enable callback if it exists, if not simply make sure the value is set */
915 if (option_types[option].enable)
916 channel->options[option] = option_types[option].enable(data);
918 channel->options[option] = (void*)1;
923 /*! \brief Disables an option globally
924 * \param dial Dial structure to disable option on
925 * \param option Option to disable
926 * \return Returns 0 on success, -1 on failure
928 int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
930 /* If the option is not enabled, return failure */
931 if (!dial->options[option])
934 /* Execute callback of option to disable if it exists */
935 if (option_types[option].disable)
936 option_types[option].disable(dial->options[option]);
938 /* Finally disable option on the structure */
939 dial->options[option] = NULL;
944 /*! \brief Disables an option per channel
945 * \param dial Dial structure
946 * \param num Channel number to disable option on
947 * \param option Option to disable
948 * \return Returns 0 on success, -1 on failure
950 int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
952 struct ast_dial_channel *channel = NULL;
954 /* Ensure we have required arguments */
955 if (!dial || AST_LIST_EMPTY(&dial->channels))
958 if (!(channel = find_dial_channel(dial, num)))
961 /* If the option is not enabled, return failure */
962 if (!channel->options[option])
965 /* Execute callback of option to disable it if it exists */
966 if (option_types[option].disable)
967 option_types[option].disable(channel->options[option]);
969 /* Finally disable the option on the structure */
970 channel->options[option] = NULL;
975 void ast_dial_set_state_callback(struct ast_dial *dial, ast_dial_state_callback callback)
977 dial->state_callback = callback;
980 /*! \brief Set the maximum time (globally) allowed for trying to ring phones
981 * \param dial The dial structure to apply the time limit to
982 * \param timeout Maximum time allowed
985 void ast_dial_set_global_timeout(struct ast_dial *dial, int timeout)
987 dial->timeout = timeout;
989 if (dial->timeout > 0 && dial->actual_timeout > dial->timeout)
990 dial->actual_timeout = dial->timeout;
995 /*! \brief Set the maximum time (per channel) allowed for trying to ring the phone
996 * \param dial The dial structure the channel belongs to
997 * \param num Channel number to set timeout on
998 * \param timeout Maximum time allowed
1001 void ast_dial_set_timeout(struct ast_dial *dial, int num, int timeout)
1003 struct ast_dial_channel *channel = NULL;
1005 if (!(channel = find_dial_channel(dial, num)))
1008 channel->timeout = timeout;
1010 if (channel->timeout > 0 && dial->actual_timeout > channel->timeout)
1011 dial->actual_timeout = channel->timeout;