Let's also include aclocal.m4
[asterisk-bristuff.git] / main / dial.c
blob401e5af6ce7c7f45b897ebdb43758ed5877caf0d
1 /*
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.
19 /*! \file
21 * \brief Dialing API
23 * \author Joshua Colp <jcolp@digium.com>
26 #include "asterisk.h"
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/time.h>
34 #include <signal.h>
35 #include <errno.h>
36 #include <unistd.h>
38 #include "asterisk/logger.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/options.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/lock.h"
43 #include "asterisk/linkedlists.h"
44 #include "asterisk/dial.h"
45 #include "asterisk/pbx.h"
47 /*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
48 struct ast_dial {
49 int num; /*! Current number to give to next dialed channel */
50 enum ast_dial_result state; /*! Status of dial */
51 void *options[AST_DIAL_OPTION_MAX]; /*! Global options */
52 ast_dial_state_callback state_callback; /*! Status callback */
53 AST_LIST_HEAD(, ast_dial_channel) channels; /*! Channels being dialed */
54 pthread_t thread; /*! Thread (if running in async) */
55 ast_mutex_t lock; /*! Lock to protect the thread information above */
58 /*! \brief Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! */
59 struct ast_dial_channel {
60 int num; /*! Unique number for dialed channel */
61 const char *tech; /*! Technology being dialed */
62 const char *device; /*! Device being dialed */
63 void *options[AST_DIAL_OPTION_MAX]; /*! Channel specific options */
64 int cause; /*! Cause code in case of failure */
65 int is_running_app:1; /*! Is this running an application? */
66 struct ast_channel *owner; /*! Asterisk channel */
67 AST_LIST_ENTRY(ast_dial_channel) list; /*! Linked list information */
70 /*! \brief Typedef for dial option enable */
71 typedef void *(*ast_dial_option_cb_enable)(void *data);
73 /*! \brief Typedef for dial option disable */
74 typedef int (*ast_dial_option_cb_disable)(void *data);
76 /* Structure for 'ANSWER_EXEC' option */
77 struct answer_exec_struct {
78 char app[AST_MAX_APP]; /* Application name */
79 char *args; /* Application arguments */
82 /* Enable function for 'ANSWER_EXEC' option */
83 static void *answer_exec_enable(void *data)
85 struct answer_exec_struct *answer_exec = NULL;
86 char *app = ast_strdupa((char*)data), *args = NULL;
88 /* Not giving any data to this option is bad, mmmk? */
89 if (ast_strlen_zero(app))
90 return NULL;
92 /* Create new data structure */
93 if (!(answer_exec = ast_calloc(1, sizeof(*answer_exec))))
94 return NULL;
96 /* Parse out application and arguments */
97 if ((args = strchr(app, '|'))) {
98 *args++ = '\0';
99 answer_exec->args = ast_strdup(args);
102 /* Copy application name */
103 ast_copy_string(answer_exec->app, app, sizeof(answer_exec->app));
105 return answer_exec;
108 /* Disable function for 'ANSWER_EXEC' option */
109 static int answer_exec_disable(void *data)
111 struct answer_exec_struct *answer_exec = data;
113 /* Make sure we have a value */
114 if (!answer_exec)
115 return -1;
117 /* If arguments are present, free them too */
118 if (answer_exec->args)
119 free(answer_exec->args);
121 /* This is simple - just free the structure */
122 free(answer_exec);
124 return 0;
127 /* Application execution function for 'ANSWER_EXEC' option */
128 static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
130 struct ast_channel *chan = dial_channel->owner;
131 struct ast_app *ast_app = pbx_findapp(app);
133 /* If the application was not found, return immediately */
134 if (!ast_app)
135 return;
137 /* All is well... execute the application */
138 pbx_exec(chan, ast_app, args);
140 /* If another thread is not taking over hang up the channel */
141 ast_mutex_lock(&dial->lock);
142 if (dial->thread != AST_PTHREADT_STOP) {
143 ast_hangup(chan);
144 dial_channel->owner = NULL;
146 ast_mutex_unlock(&dial->lock);
148 return;
151 /*! \brief Options structure - maps options to respective handlers (enable/disable). This list MUST be perfectly kept in order, or else madness will happen. */
152 static const struct ast_option_types {
153 enum ast_dial_option option;
154 ast_dial_option_cb_enable enable;
155 ast_dial_option_cb_disable disable;
156 } option_types[] = {
157 { AST_DIAL_OPTION_RINGING, NULL, NULL }, /*! Always indicate ringing to caller */
158 { AST_DIAL_OPTION_ANSWER_EXEC, answer_exec_enable, answer_exec_disable }, /*! Execute application upon answer in async mode */
159 { AST_DIAL_OPTION_MAX, NULL, NULL }, /*! Terminator of list */
162 /* free the buffer if allocated, and set the pointer to the second arg */
163 #define S_REPLACE(s, new_val) \
164 do { \
165 if (s) \
166 free(s); \
167 s = (new_val); \
168 } while (0)
170 /*! \brief Maximum number of channels we can watch at a time */
171 #define AST_MAX_WATCHERS 256
173 /*! \brief Macro for finding the option structure to use on a dialed channel */
174 #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])
176 /*! \brief Macro that determines whether a channel is the caller or not */
177 #define IS_CALLER(chan, owner) (chan == owner ? 1 : 0)
179 /*! \brief New dialing structure
180 * \note Create a dialing structure
181 * \return Returns a calloc'd ast_dial structure, NULL on failure
183 struct ast_dial *ast_dial_create(void)
185 struct ast_dial *dial = NULL;
187 /* Allocate new memory for structure */
188 if (!(dial = ast_calloc(1, sizeof(*dial))))
189 return NULL;
191 /* Initialize list of channels */
192 AST_LIST_HEAD_INIT(&dial->channels);
194 /* Initialize thread to NULL */
195 dial->thread = AST_PTHREADT_NULL;
197 /* Can't forget about the lock */
198 ast_mutex_init(&dial->lock);
200 return dial;
203 /*! \brief Append a channel
204 * \note Appends a channel to a dialing structure
205 * \return Returns channel reference number on success, -1 on failure
207 int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device)
209 struct ast_dial_channel *channel = NULL;
211 /* Make sure we have required arguments */
212 if (!dial || !tech || !device)
213 return -1;
215 /* Allocate new memory for dialed channel structure */
216 if (!(channel = ast_calloc(1, sizeof(*channel))))
217 return -1;
219 /* Record technology and device for when we actually dial */
220 channel->tech = tech;
221 channel->device = device;
223 /* Grab reference number from dial structure */
224 channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
226 /* Insert into channels list */
227 AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
229 return channel->num;
232 /*! \brief Helper function that does the beginning dialing */
233 static int begin_dial(struct ast_dial *dial, struct ast_channel *chan)
235 struct ast_dial_channel *channel = NULL;
236 int success = 0, res = 0;
238 /* Iterate through channel list, requesting and calling each one */
239 AST_LIST_LOCK(&dial->channels);
240 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
241 char numsubst[AST_MAX_EXTENSION];
243 /* Copy device string over */
244 ast_copy_string(numsubst, channel->device, sizeof(numsubst));
246 /* Request that the channel be created */
247 if (!(channel->owner = ast_request(channel->tech,
248 chan ? chan->nativeformats : AST_FORMAT_AUDIO_MASK, numsubst, &channel->cause))) {
249 continue;
252 channel->owner->appl = "AppDial2";
253 channel->owner->data = "(Outgoing Line)";
254 channel->owner->whentohangup = 0;
256 /* Inherit everything from he who spawned this Dial */
257 if (chan) {
258 ast_channel_inherit_variables(chan, channel->owner);
260 /* Copy over callerid information */
261 S_REPLACE(channel->owner->cid.cid_num, ast_strdup(chan->cid.cid_num));
262 S_REPLACE(channel->owner->cid.cid_name, ast_strdup(chan->cid.cid_name));
263 S_REPLACE(channel->owner->cid.cid_ani, ast_strdup(chan->cid.cid_ani));
264 S_REPLACE(channel->owner->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
266 ast_string_field_set(channel->owner, language, chan->language);
267 ast_string_field_set(channel->owner, accountcode, chan->accountcode);
268 channel->owner->cdrflags = chan->cdrflags;
269 if (ast_strlen_zero(channel->owner->musicclass))
270 ast_string_field_set(channel->owner, musicclass, chan->musicclass);
272 channel->owner->cid.cid_pres = chan->cid.cid_pres;
273 channel->owner->cid.cid_ton = chan->cid.cid_ton;
274 channel->owner->cid.cid_tns = chan->cid.cid_tns;
275 channel->owner->adsicpe = chan->adsicpe;
276 channel->owner->transfercapability = chan->transfercapability;
279 /* Actually call the device */
280 if ((res = ast_call(channel->owner, numsubst, 0))) {
281 ast_hangup(channel->owner);
282 channel->owner = NULL;
283 } else {
284 success++;
285 if (option_verbose > 2)
286 ast_verbose(VERBOSE_PREFIX_3 "Called %s\n", numsubst);
289 AST_LIST_UNLOCK(&dial->channels);
291 /* If number of failures matches the number of channels, then this truly failed */
292 return success;
295 /*! \brief Helper function that finds the dialed channel based on owner */
296 static struct ast_dial_channel *find_relative_dial_channel(struct ast_dial *dial, struct ast_channel *owner)
298 struct ast_dial_channel *channel = NULL;
300 AST_LIST_LOCK(&dial->channels);
301 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
302 if (channel->owner == owner)
303 break;
305 AST_LIST_UNLOCK(&dial->channels);
307 return channel;
310 static void set_state(struct ast_dial *dial, enum ast_dial_result state)
312 dial->state = state;
314 if (dial->state_callback)
315 dial->state_callback(dial);
318 /*! \brief Helper function that handles control frames WITH owner */
319 static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
321 if (fr->frametype == AST_FRAME_CONTROL) {
322 switch (fr->subclass) {
323 case AST_CONTROL_ANSWER:
324 if (option_verbose > 2)
325 ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", channel->owner->name, chan->name);
326 AST_LIST_LOCK(&dial->channels);
327 AST_LIST_REMOVE(&dial->channels, channel, list);
328 AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
329 AST_LIST_UNLOCK(&dial->channels);
330 set_state(dial, AST_DIAL_RESULT_ANSWERED);
331 break;
332 case AST_CONTROL_BUSY:
333 if (option_verbose > 2)
334 ast_verbose(VERBOSE_PREFIX_3 "%s is busy\n", channel->owner->name);
335 ast_hangup(channel->owner);
336 channel->owner = NULL;
337 break;
338 case AST_CONTROL_CONGESTION:
339 if (option_verbose > 2)
340 ast_verbose(VERBOSE_PREFIX_3 "%s is circuit-busy\n", channel->owner->name);
341 ast_hangup(channel->owner);
342 channel->owner = NULL;
343 break;
344 case AST_CONTROL_RINGING:
345 if (option_verbose > 2)
346 ast_verbose(VERBOSE_PREFIX_3 "%s is ringing\n", channel->owner->name);
347 ast_indicate(chan, AST_CONTROL_RINGING);
348 set_state(dial, AST_DIAL_RESULT_RINGING);
349 break;
350 case AST_CONTROL_PROGRESS:
351 if (option_verbose > 2)
352 ast_verbose (VERBOSE_PREFIX_3 "%s is making progress, passing it to %s\n", channel->owner->name, chan->name);
353 ast_indicate(chan, AST_CONTROL_PROGRESS);
354 set_state(dial, AST_DIAL_RESULT_PROGRESS);
355 break;
356 case AST_CONTROL_VIDUPDATE:
357 if (option_verbose > 2)
358 ast_verbose (VERBOSE_PREFIX_3 "%s requested a video update, passing it to %s\n", channel->owner->name, chan->name);
359 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
360 break;
361 case AST_CONTROL_SRCUPDATE:
362 if (option_verbose > 2)
363 ast_verbose (VERBOSE_PREFIX_3 "%s requested a source update, passing it to %s\n", channel->owner->name, chan->name);
364 ast_indicate(chan, AST_CONTROL_SRCUPDATE);
365 break;
366 case AST_CONTROL_PROCEEDING:
367 if (option_verbose > 2)
368 ast_verbose (VERBOSE_PREFIX_3 "%s is proceeding, passing it to %s\n", channel->owner->name, chan->name);
369 ast_indicate(chan, AST_CONTROL_PROCEEDING);
370 set_state(dial, AST_DIAL_RESULT_PROCEEDING);
371 break;
372 case AST_CONTROL_HOLD:
373 if (option_verbose > 2)
374 ast_verbose(VERBOSE_PREFIX_3 "Call on %s placed on hold\n", chan->name);
375 ast_indicate(chan, AST_CONTROL_HOLD);
376 break;
377 case AST_CONTROL_UNHOLD:
378 if (option_verbose > 2)
379 ast_verbose(VERBOSE_PREFIX_3 "Call on %s left from hold\n", chan->name);
380 ast_indicate(chan, AST_CONTROL_UNHOLD);
381 break;
382 case AST_CONTROL_OFFHOOK:
383 case AST_CONTROL_FLASH:
384 break;
385 case -1:
386 /* Prod the channel */
387 ast_indicate(chan, -1);
388 break;
389 default:
390 break;
394 return;
397 /*! \brief Helper function that handles control frames WITHOUT owner */
398 static void handle_frame_ownerless(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr)
400 /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
401 if (fr->frametype != AST_FRAME_CONTROL)
402 return;
404 switch (fr->subclass) {
405 case AST_CONTROL_ANSWER:
406 if (option_verbose > 2)
407 ast_verbose( VERBOSE_PREFIX_3 "%s answered\n", channel->owner->name);
408 AST_LIST_LOCK(&dial->channels);
409 AST_LIST_REMOVE(&dial->channels, channel, list);
410 AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
411 AST_LIST_UNLOCK(&dial->channels);
412 set_state(dial, AST_DIAL_RESULT_ANSWERED);
413 break;
414 case AST_CONTROL_BUSY:
415 if (option_verbose > 2)
416 ast_verbose(VERBOSE_PREFIX_3 "%s is busy\n", channel->owner->name);
417 ast_hangup(channel->owner);
418 channel->owner = NULL;
419 break;
420 case AST_CONTROL_CONGESTION:
421 if (option_verbose > 2)
422 ast_verbose(VERBOSE_PREFIX_3 "%s is circuit-busy\n", channel->owner->name);
423 ast_hangup(channel->owner);
424 channel->owner = NULL;
425 break;
426 case AST_CONTROL_RINGING:
427 if (option_verbose > 2)
428 ast_verbose(VERBOSE_PREFIX_3 "%s is ringing\n", channel->owner->name);
429 set_state(dial, AST_DIAL_RESULT_RINGING);
430 break;
431 case AST_CONTROL_PROGRESS:
432 if (option_verbose > 2)
433 ast_verbose (VERBOSE_PREFIX_3 "%s is making progress\n", channel->owner->name);
434 set_state(dial, AST_DIAL_RESULT_PROGRESS);
435 break;
436 case AST_CONTROL_PROCEEDING:
437 if (option_verbose > 2)
438 ast_verbose (VERBOSE_PREFIX_3 "%s is proceeding\n", channel->owner->name);
439 set_state(dial, AST_DIAL_RESULT_PROCEEDING);
440 break;
441 default:
442 break;
445 return;
448 /*! \brief Helper function that basically keeps tabs on dialing attempts */
449 static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
451 int timeout = -1, count = 0;
452 struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
453 struct ast_dial_channel *channel = NULL;
454 struct answer_exec_struct *answer_exec = NULL;
456 set_state(dial, AST_DIAL_RESULT_TRYING);
458 /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
459 if (dial->options[AST_DIAL_OPTION_RINGING]) {
460 set_state(dial, AST_DIAL_RESULT_RINGING);
461 if (chan)
462 ast_indicate(chan, AST_CONTROL_RINGING);
465 /* Go into an infinite loop while we are trying */
466 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)) {
467 int pos = 0;
468 struct ast_frame *fr = NULL;
470 /* Set up channel structure array */
471 pos = count = 0;
472 if (chan)
473 cs[pos++] = chan;
475 /* Add channels we are attempting to dial */
476 AST_LIST_LOCK(&dial->channels);
477 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
478 if (channel->owner) {
479 cs[pos++] = channel->owner;
480 count++;
483 AST_LIST_UNLOCK(&dial->channels);
485 /* If we have no outbound channels in progress, switch state to unanswered and stop */
486 if (!count) {
487 set_state(dial, AST_DIAL_RESULT_UNANSWERED);
488 break;
491 /* Just to be safe... */
492 if (dial->thread == AST_PTHREADT_STOP)
493 break;
495 /* Wait for frames from channels */
496 who = ast_waitfor_n(cs, pos, &timeout);
498 /* Check to see if our thread is being cancelled */
499 if (dial->thread == AST_PTHREADT_STOP)
500 break;
502 /* If we are not being cancelled and we have no channel, then timeout was tripped */
503 if (!who)
504 continue;
506 /* Find relative dial channel */
507 if (!chan || !IS_CALLER(chan, who))
508 channel = find_relative_dial_channel(dial, who);
510 /* Attempt to read in a frame */
511 if (!(fr = ast_read(who))) {
512 /* If this is the caller then we switch state to hangup and stop */
513 if (chan && IS_CALLER(chan, who)) {
514 set_state(dial, AST_DIAL_RESULT_HANGUP);
515 break;
517 ast_hangup(who);
518 channel->owner = NULL;
519 continue;
522 /* Process the frame */
523 if (chan)
524 handle_frame(dial, channel, fr, chan);
525 else
526 handle_frame_ownerless(dial, channel, fr);
528 /* Free the received frame and start all over */
529 ast_frfree(fr);
532 /* Do post-processing from loop */
533 if (dial->state == AST_DIAL_RESULT_ANSWERED) {
534 /* Hangup everything except that which answered */
535 AST_LIST_LOCK(&dial->channels);
536 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
537 if (!channel->owner || channel->owner == who)
538 continue;
539 ast_hangup(channel->owner);
540 channel->owner = NULL;
542 AST_LIST_UNLOCK(&dial->channels);
543 /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
544 if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC))) {
545 channel->is_running_app = 1;
546 answer_exec_run(dial, channel, answer_exec->app, answer_exec->args);
547 channel->is_running_app = 0;
549 } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
550 /* Hangup everything */
551 AST_LIST_LOCK(&dial->channels);
552 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
553 if (!channel->owner)
554 continue;
555 ast_hangup(channel->owner);
556 channel->owner = NULL;
558 AST_LIST_UNLOCK(&dial->channels);
561 return dial->state;
564 /*! \brief Dial async thread function */
565 static void *async_dial(void *data)
567 struct ast_dial *dial = data;
569 /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
570 monitor_dial(dial, NULL);
572 return NULL;
575 /*! \brief Execute dialing synchronously or asynchronously
576 * \note Dials channels in a dial structure.
577 * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
579 enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
581 enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
583 /* Ensure required arguments are passed */
584 if (!dial || (!chan && !async)) {
585 ast_log(LOG_DEBUG, "invalid #1\n");
586 return AST_DIAL_RESULT_INVALID;
589 /* If there are no channels to dial we can't very well try to dial them */
590 if (AST_LIST_EMPTY(&dial->channels)) {
591 ast_log(LOG_DEBUG, "invalid #2\n");
592 return AST_DIAL_RESULT_INVALID;
595 /* Dial each requested channel */
596 if (!begin_dial(dial, chan))
597 return AST_DIAL_RESULT_FAILED;
599 /* If we are running async spawn a thread and send it away... otherwise block here */
600 if (async) {
601 dial->state = AST_DIAL_RESULT_TRYING;
602 /* Try to create a thread */
603 if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
604 /* Failed to create the thread - hangup all dialed channels and return failed */
605 ast_dial_hangup(dial);
606 res = AST_DIAL_RESULT_FAILED;
608 } else {
609 res = monitor_dial(dial, chan);
612 return res;
615 /*! \brief Return channel that answered
616 * \note Returns the Asterisk channel that answered
617 * \param dial Dialing structure
619 struct ast_channel *ast_dial_answered(struct ast_dial *dial)
621 if (!dial)
622 return NULL;
624 return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
627 /*! \brief Return state of dial
628 * \note Returns the state of the dial attempt
629 * \param dial Dialing structure
631 enum ast_dial_result ast_dial_state(struct ast_dial *dial)
633 return dial->state;
636 /*! \brief Cancel async thread
637 * \note Cancel a running async thread
638 * \param dial Dialing structure
640 enum ast_dial_result ast_dial_join(struct ast_dial *dial)
642 pthread_t thread;
644 /* If the dial structure is not running in async, return failed */
645 if (dial->thread == AST_PTHREADT_NULL)
646 return AST_DIAL_RESULT_FAILED;
648 /* Record thread */
649 thread = dial->thread;
651 /* Boom, commence locking */
652 ast_mutex_lock(&dial->lock);
654 /* Stop the thread */
655 dial->thread = AST_PTHREADT_STOP;
657 /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
658 AST_LIST_LOCK(&dial->channels);
659 if (AST_LIST_FIRST(&dial->channels)->is_running_app) {
660 struct ast_channel *chan = AST_LIST_FIRST(&dial->channels)->owner;
661 if (chan) {
662 ast_channel_lock(chan);
663 ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
664 ast_channel_unlock(chan);
666 } else {
667 /* Now we signal it with SIGURG so it will break out of it's waitfor */
668 pthread_kill(thread, SIGURG);
670 AST_LIST_UNLOCK(&dial->channels);
672 /* Yay done with it */
673 ast_mutex_unlock(&dial->lock);
675 /* Finally wait for the thread to exit */
676 pthread_join(thread, NULL);
678 /* Yay thread is all gone */
679 dial->thread = AST_PTHREADT_NULL;
681 return dial->state;
684 /*! \brief Hangup channels
685 * \note Hangup all active channels
686 * \param dial Dialing structure
688 void ast_dial_hangup(struct ast_dial *dial)
690 struct ast_dial_channel *channel = NULL;
692 if (!dial)
693 return;
695 AST_LIST_LOCK(&dial->channels);
696 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
697 if (channel->owner) {
698 ast_hangup(channel->owner);
699 channel->owner = NULL;
702 AST_LIST_UNLOCK(&dial->channels);
704 return;
707 /*! \brief Destroys a dialing structure
708 * \note Destroys (free's) the given ast_dial structure
709 * \param dial Dialing structure to free
710 * \return Returns 0 on success, -1 on failure
712 int ast_dial_destroy(struct ast_dial *dial)
714 int i = 0;
715 struct ast_dial_channel *channel = NULL;
717 if (!dial)
718 return -1;
720 /* Hangup and deallocate all the dialed channels */
721 AST_LIST_LOCK(&dial->channels);
722 AST_LIST_TRAVERSE_SAFE_BEGIN(&dial->channels, channel, list) {
723 /* Disable any enabled options */
724 for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
725 if (!channel->options[i])
726 continue;
727 if (option_types[i].disable)
728 option_types[i].disable(channel->options[i]);
729 channel->options[i] = NULL;
731 /* Hang up channel if need be */
732 if (channel->owner) {
733 ast_hangup(channel->owner);
734 channel->owner = NULL;
736 /* Free structure */
737 AST_LIST_REMOVE_CURRENT(&dial->channels, list);
738 free(channel);
740 AST_LIST_TRAVERSE_SAFE_END;
741 AST_LIST_UNLOCK(&dial->channels);
743 /* Disable any enabled options globally */
744 for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
745 if (!dial->options[i])
746 continue;
747 if (option_types[i].disable)
748 option_types[i].disable(dial->options[i]);
749 dial->options[i] = NULL;
752 /* Lock be gone! */
753 ast_mutex_destroy(&dial->lock);
755 /* Free structure */
756 free(dial);
758 return 0;
761 /*! \brief Enables an option globally
762 * \param dial Dial structure to enable option on
763 * \param option Option to enable
764 * \param data Data to pass to this option (not always needed)
765 * \return Returns 0 on success, -1 on failure
767 int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
769 /* If the option is already enabled, return failure */
770 if (dial->options[option])
771 return -1;
773 /* Execute enable callback if it exists, if not simply make sure the value is set */
774 if (option_types[option].enable)
775 dial->options[option] = option_types[option].enable(data);
776 else
777 dial->options[option] = (void*)1;
779 return 0;
782 /*! \brief Enables an option per channel
783 * \param dial Dial structure
784 * \param num Channel number to enable option on
785 * \param option Option to enable
786 * \param data Data to pass to this option (not always needed)
787 * \return Returns 0 on success, -1 on failure
789 int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
791 struct ast_dial_channel *channel = NULL;
793 /* Ensure we have required arguments */
794 if (!dial || AST_LIST_EMPTY(&dial->channels))
795 return -1;
797 /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
798 AST_LIST_LOCK(&dial->channels);
799 if (AST_LIST_LAST(&dial->channels)->num != num) {
800 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
801 if (channel->num == num)
802 break;
804 } else {
805 channel = AST_LIST_LAST(&dial->channels);
807 AST_LIST_UNLOCK(&dial->channels);
809 /* If none found, return failure */
810 if (!channel)
811 return -1;
813 /* If the option is already enabled, return failure */
814 if (channel->options[option])
815 return -1;
817 /* Execute enable callback if it exists, if not simply make sure the value is set */
818 if (option_types[option].enable)
819 channel->options[option] = option_types[option].enable(data);
820 else
821 channel->options[option] = (void*)1;
823 return 0;
826 /*! \brief Disables an option globally
827 * \param dial Dial structure to disable option on
828 * \param option Option to disable
829 * \return Returns 0 on success, -1 on failure
831 int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
833 /* If the option is not enabled, return failure */
834 if (!dial->options[option])
835 return -1;
837 /* Execute callback of option to disable if it exists */
838 if (option_types[option].disable)
839 option_types[option].disable(dial->options[option]);
841 /* Finally disable option on the structure */
842 dial->options[option] = NULL;
844 return 0;
847 /*! \brief Disables an option per channel
848 * \param dial Dial structure
849 * \param num Channel number to disable option on
850 * \param option Option to disable
851 * \return Returns 0 on success, -1 on failure
853 int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
855 struct ast_dial_channel *channel = NULL;
857 /* Ensure we have required arguments */
858 if (!dial || AST_LIST_EMPTY(&dial->channels))
859 return -1;
861 /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
862 AST_LIST_LOCK(&dial->channels);
863 if (AST_LIST_LAST(&dial->channels)->num != num) {
864 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
865 if (channel->num == num)
866 break;
868 } else {
869 channel = AST_LIST_LAST(&dial->channels);
871 AST_LIST_UNLOCK(&dial->channels);
873 /* If none found, return failure */
874 if (!channel)
875 return -1;
877 /* If the option is not enabled, return failure */
878 if (!channel->options[option])
879 return -1;
881 /* Execute callback of option to disable it if it exists */
882 if (option_types[option].disable)
883 option_types[option].disable(channel->options[option]);
885 /* Finally disable the option on the structure */
886 channel->options[option] = NULL;
888 return 0;
891 void ast_dial_set_state_callback(struct ast_dial *dial, ast_dial_state_callback callback)
893 dial->state_callback = callback;