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$")
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! */
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
))
92 /* Create new data structure */
93 if (!(answer_exec
= ast_calloc(1, sizeof(*answer_exec
))))
96 /* Parse out application and arguments */
97 if ((args
= strchr(app
, '|'))) {
99 answer_exec
->args
= ast_strdup(args
);
102 /* Copy application name */
103 ast_copy_string(answer_exec
->app
, app
, sizeof(answer_exec
->app
));
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 */
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 */
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 */
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
) {
144 dial_channel
->owner
= NULL
;
146 ast_mutex_unlock(&dial
->lock
);
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
;
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) \
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
))))
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
);
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
)
215 /* Allocate new memory for dialed channel structure */
216 if (!(channel
= ast_calloc(1, sizeof(*channel
))))
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
);
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
))) {
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 */
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
;
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 */
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
)
305 AST_LIST_UNLOCK(&dial
->channels
);
310 static void set_state(struct ast_dial
*dial
, enum ast_dial_result 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
);
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
;
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
;
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
);
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
);
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
);
361 case AST_CONTROL_PROCEEDING
:
362 if (option_verbose
> 2)
363 ast_verbose (VERBOSE_PREFIX_3
"%s is proceeding, passing it to %s\n", channel
->owner
->name
, chan
->name
);
364 ast_indicate(chan
, AST_CONTROL_PROCEEDING
);
365 set_state(dial
, AST_DIAL_RESULT_PROCEEDING
);
367 case AST_CONTROL_HOLD
:
368 if (option_verbose
> 2)
369 ast_verbose(VERBOSE_PREFIX_3
"Call on %s placed on hold\n", chan
->name
);
370 ast_indicate(chan
, AST_CONTROL_HOLD
);
372 case AST_CONTROL_UNHOLD
:
373 if (option_verbose
> 2)
374 ast_verbose(VERBOSE_PREFIX_3
"Call on %s left from hold\n", chan
->name
);
375 ast_indicate(chan
, AST_CONTROL_UNHOLD
);
377 case AST_CONTROL_OFFHOOK
:
378 case AST_CONTROL_FLASH
:
381 /* Prod the channel */
382 ast_indicate(chan
, -1);
392 /*! \brief Helper function that handles control frames WITHOUT owner */
393 static void handle_frame_ownerless(struct ast_dial
*dial
, struct ast_dial_channel
*channel
, struct ast_frame
*fr
)
395 /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
396 if (fr
->frametype
!= AST_FRAME_CONTROL
)
399 switch (fr
->subclass
) {
400 case AST_CONTROL_ANSWER
:
401 if (option_verbose
> 2)
402 ast_verbose( VERBOSE_PREFIX_3
"%s answered\n", channel
->owner
->name
);
403 AST_LIST_LOCK(&dial
->channels
);
404 AST_LIST_REMOVE(&dial
->channels
, channel
, list
);
405 AST_LIST_INSERT_HEAD(&dial
->channels
, channel
, list
);
406 AST_LIST_UNLOCK(&dial
->channels
);
407 set_state(dial
, AST_DIAL_RESULT_ANSWERED
);
409 case AST_CONTROL_BUSY
:
410 if (option_verbose
> 2)
411 ast_verbose(VERBOSE_PREFIX_3
"%s is busy\n", channel
->owner
->name
);
412 ast_hangup(channel
->owner
);
413 channel
->owner
= NULL
;
415 case AST_CONTROL_CONGESTION
:
416 if (option_verbose
> 2)
417 ast_verbose(VERBOSE_PREFIX_3
"%s is circuit-busy\n", channel
->owner
->name
);
418 ast_hangup(channel
->owner
);
419 channel
->owner
= NULL
;
421 case AST_CONTROL_RINGING
:
422 if (option_verbose
> 2)
423 ast_verbose(VERBOSE_PREFIX_3
"%s is ringing\n", channel
->owner
->name
);
424 set_state(dial
, AST_DIAL_RESULT_RINGING
);
426 case AST_CONTROL_PROGRESS
:
427 if (option_verbose
> 2)
428 ast_verbose (VERBOSE_PREFIX_3
"%s is making progress\n", channel
->owner
->name
);
429 set_state(dial
, AST_DIAL_RESULT_PROGRESS
);
431 case AST_CONTROL_PROCEEDING
:
432 if (option_verbose
> 2)
433 ast_verbose (VERBOSE_PREFIX_3
"%s is proceeding\n", channel
->owner
->name
);
434 set_state(dial
, AST_DIAL_RESULT_PROCEEDING
);
443 /*! \brief Helper function that basically keeps tabs on dialing attempts */
444 static enum ast_dial_result
monitor_dial(struct ast_dial
*dial
, struct ast_channel
*chan
)
446 int timeout
= -1, count
= 0;
447 struct ast_channel
*cs
[AST_MAX_WATCHERS
], *who
= NULL
;
448 struct ast_dial_channel
*channel
= NULL
;
449 struct answer_exec_struct
*answer_exec
= NULL
;
451 set_state(dial
, AST_DIAL_RESULT_TRYING
);
453 /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
454 if (dial
->options
[AST_DIAL_OPTION_RINGING
]) {
455 set_state(dial
, AST_DIAL_RESULT_RINGING
);
457 ast_indicate(chan
, AST_CONTROL_RINGING
);
460 /* Go into an infinite loop while we are trying */
461 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
)) {
463 struct ast_frame
*fr
= NULL
;
465 /* Set up channel structure array */
470 /* Add channels we are attempting to dial */
471 AST_LIST_LOCK(&dial
->channels
);
472 AST_LIST_TRAVERSE(&dial
->channels
, channel
, list
) {
473 if (channel
->owner
) {
474 cs
[pos
++] = channel
->owner
;
478 AST_LIST_UNLOCK(&dial
->channels
);
480 /* If we have no outbound channels in progress, switch state to unanswered and stop */
482 set_state(dial
, AST_DIAL_RESULT_UNANSWERED
);
486 /* Just to be safe... */
487 if (dial
->thread
== AST_PTHREADT_STOP
)
490 /* Wait for frames from channels */
491 who
= ast_waitfor_n(cs
, pos
, &timeout
);
493 /* Check to see if our thread is being cancelled */
494 if (dial
->thread
== AST_PTHREADT_STOP
)
497 /* If we are not being cancelled and we have no channel, then timeout was tripped */
501 /* Find relative dial channel */
502 if (!chan
|| !IS_CALLER(chan
, who
))
503 channel
= find_relative_dial_channel(dial
, who
);
505 /* Attempt to read in a frame */
506 if (!(fr
= ast_read(who
))) {
507 /* If this is the caller then we switch state to hangup and stop */
508 if (chan
&& IS_CALLER(chan
, who
)) {
509 set_state(dial
, AST_DIAL_RESULT_HANGUP
);
513 channel
->owner
= NULL
;
517 /* Process the frame */
519 handle_frame(dial
, channel
, fr
, chan
);
521 handle_frame_ownerless(dial
, channel
, fr
);
523 /* Free the received frame and start all over */
527 /* Do post-processing from loop */
528 if (dial
->state
== AST_DIAL_RESULT_ANSWERED
) {
529 /* Hangup everything except that which answered */
530 AST_LIST_LOCK(&dial
->channels
);
531 AST_LIST_TRAVERSE(&dial
->channels
, channel
, list
) {
532 if (!channel
->owner
|| channel
->owner
== who
)
534 ast_hangup(channel
->owner
);
535 channel
->owner
= NULL
;
537 AST_LIST_UNLOCK(&dial
->channels
);
538 /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
539 if ((channel
= find_relative_dial_channel(dial
, who
)) && (answer_exec
= FIND_RELATIVE_OPTION(dial
, channel
, AST_DIAL_OPTION_ANSWER_EXEC
))) {
540 channel
->is_running_app
= 1;
541 answer_exec_run(dial
, channel
, answer_exec
->app
, answer_exec
->args
);
542 channel
->is_running_app
= 0;
544 } else if (dial
->state
== AST_DIAL_RESULT_HANGUP
) {
545 /* Hangup everything */
546 AST_LIST_LOCK(&dial
->channels
);
547 AST_LIST_TRAVERSE(&dial
->channels
, channel
, list
) {
550 ast_hangup(channel
->owner
);
551 channel
->owner
= NULL
;
553 AST_LIST_UNLOCK(&dial
->channels
);
559 /*! \brief Dial async thread function */
560 static void *async_dial(void *data
)
562 struct ast_dial
*dial
= data
;
564 /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
565 monitor_dial(dial
, NULL
);
570 /*! \brief Execute dialing synchronously or asynchronously
571 * \note Dials channels in a dial structure.
572 * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
574 enum ast_dial_result
ast_dial_run(struct ast_dial
*dial
, struct ast_channel
*chan
, int async
)
576 enum ast_dial_result res
= AST_DIAL_RESULT_TRYING
;
578 /* Ensure required arguments are passed */
579 if (!dial
|| (!chan
&& !async
)) {
580 ast_log(LOG_DEBUG
, "invalid #1\n");
581 return AST_DIAL_RESULT_INVALID
;
584 /* If there are no channels to dial we can't very well try to dial them */
585 if (AST_LIST_EMPTY(&dial
->channels
)) {
586 ast_log(LOG_DEBUG
, "invalid #2\n");
587 return AST_DIAL_RESULT_INVALID
;
590 /* Dial each requested channel */
591 if (!begin_dial(dial
, chan
))
592 return AST_DIAL_RESULT_FAILED
;
594 /* If we are running async spawn a thread and send it away... otherwise block here */
596 dial
->state
= AST_DIAL_RESULT_TRYING
;
597 /* Try to create a thread */
598 if (ast_pthread_create(&dial
->thread
, NULL
, async_dial
, dial
)) {
599 /* Failed to create the thread - hangup all dialed channels and return failed */
600 ast_dial_hangup(dial
);
601 res
= AST_DIAL_RESULT_FAILED
;
604 res
= monitor_dial(dial
, chan
);
610 /*! \brief Return channel that answered
611 * \note Returns the Asterisk channel that answered
612 * \param dial Dialing structure
614 struct ast_channel
*ast_dial_answered(struct ast_dial
*dial
)
619 return ((dial
->state
== AST_DIAL_RESULT_ANSWERED
) ? AST_LIST_FIRST(&dial
->channels
)->owner
: NULL
);
622 /*! \brief Return state of dial
623 * \note Returns the state of the dial attempt
624 * \param dial Dialing structure
626 enum ast_dial_result
ast_dial_state(struct ast_dial
*dial
)
631 /*! \brief Cancel async thread
632 * \note Cancel a running async thread
633 * \param dial Dialing structure
635 enum ast_dial_result
ast_dial_join(struct ast_dial
*dial
)
639 /* If the dial structure is not running in async, return failed */
640 if (dial
->thread
== AST_PTHREADT_NULL
)
641 return AST_DIAL_RESULT_FAILED
;
644 thread
= dial
->thread
;
646 /* Boom, commence locking */
647 ast_mutex_lock(&dial
->lock
);
649 /* Stop the thread */
650 dial
->thread
= AST_PTHREADT_STOP
;
652 /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
653 AST_LIST_LOCK(&dial
->channels
);
654 if (AST_LIST_FIRST(&dial
->channels
)->is_running_app
) {
655 struct ast_channel
*chan
= AST_LIST_FIRST(&dial
->channels
)->owner
;
657 ast_channel_lock(chan
);
658 ast_softhangup(chan
, AST_SOFTHANGUP_EXPLICIT
);
659 ast_channel_unlock(chan
);
662 /* Now we signal it with SIGURG so it will break out of it's waitfor */
663 pthread_kill(thread
, SIGURG
);
665 AST_LIST_UNLOCK(&dial
->channels
);
667 /* Yay done with it */
668 ast_mutex_unlock(&dial
->lock
);
670 /* Finally wait for the thread to exit */
671 pthread_join(thread
, NULL
);
673 /* Yay thread is all gone */
674 dial
->thread
= AST_PTHREADT_NULL
;
679 /*! \brief Hangup channels
680 * \note Hangup all active channels
681 * \param dial Dialing structure
683 void ast_dial_hangup(struct ast_dial
*dial
)
685 struct ast_dial_channel
*channel
= NULL
;
690 AST_LIST_LOCK(&dial
->channels
);
691 AST_LIST_TRAVERSE(&dial
->channels
, channel
, list
) {
692 if (channel
->owner
) {
693 ast_hangup(channel
->owner
);
694 channel
->owner
= NULL
;
697 AST_LIST_UNLOCK(&dial
->channels
);
702 /*! \brief Destroys a dialing structure
703 * \note Destroys (free's) the given ast_dial structure
704 * \param dial Dialing structure to free
705 * \return Returns 0 on success, -1 on failure
707 int ast_dial_destroy(struct ast_dial
*dial
)
710 struct ast_dial_channel
*channel
= NULL
;
715 /* Hangup and deallocate all the dialed channels */
716 AST_LIST_LOCK(&dial
->channels
);
717 AST_LIST_TRAVERSE_SAFE_BEGIN(&dial
->channels
, channel
, list
) {
718 /* Disable any enabled options */
719 for (i
= 0; i
< AST_DIAL_OPTION_MAX
; i
++) {
720 if (!channel
->options
[i
])
722 if (option_types
[i
].disable
)
723 option_types
[i
].disable(channel
->options
[i
]);
724 channel
->options
[i
] = NULL
;
726 /* Hang up channel if need be */
727 if (channel
->owner
) {
728 ast_hangup(channel
->owner
);
729 channel
->owner
= NULL
;
732 AST_LIST_REMOVE_CURRENT(&dial
->channels
, list
);
735 AST_LIST_TRAVERSE_SAFE_END
;
736 AST_LIST_UNLOCK(&dial
->channels
);
738 /* Disable any enabled options globally */
739 for (i
= 0; i
< AST_DIAL_OPTION_MAX
; i
++) {
740 if (!dial
->options
[i
])
742 if (option_types
[i
].disable
)
743 option_types
[i
].disable(dial
->options
[i
]);
744 dial
->options
[i
] = NULL
;
748 ast_mutex_destroy(&dial
->lock
);
756 /*! \brief Enables an option globally
757 * \param dial Dial structure to enable option on
758 * \param option Option to enable
759 * \param data Data to pass to this option (not always needed)
760 * \return Returns 0 on success, -1 on failure
762 int ast_dial_option_global_enable(struct ast_dial
*dial
, enum ast_dial_option option
, void *data
)
764 /* If the option is already enabled, return failure */
765 if (dial
->options
[option
])
768 /* Execute enable callback if it exists, if not simply make sure the value is set */
769 if (option_types
[option
].enable
)
770 dial
->options
[option
] = option_types
[option
].enable(data
);
772 dial
->options
[option
] = (void*)1;
777 /*! \brief Enables an option per channel
778 * \param dial Dial structure
779 * \param num Channel number to enable option on
780 * \param option Option to enable
781 * \param data Data to pass to this option (not always needed)
782 * \return Returns 0 on success, -1 on failure
784 int ast_dial_option_enable(struct ast_dial
*dial
, int num
, enum ast_dial_option option
, void *data
)
786 struct ast_dial_channel
*channel
= NULL
;
788 /* Ensure we have required arguments */
789 if (!dial
|| AST_LIST_EMPTY(&dial
->channels
))
792 /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
793 AST_LIST_LOCK(&dial
->channels
);
794 if (AST_LIST_LAST(&dial
->channels
)->num
!= num
) {
795 AST_LIST_TRAVERSE(&dial
->channels
, channel
, list
) {
796 if (channel
->num
== num
)
800 channel
= AST_LIST_LAST(&dial
->channels
);
802 AST_LIST_UNLOCK(&dial
->channels
);
804 /* If none found, return failure */
808 /* If the option is already enabled, return failure */
809 if (channel
->options
[option
])
812 /* Execute enable callback if it exists, if not simply make sure the value is set */
813 if (option_types
[option
].enable
)
814 channel
->options
[option
] = option_types
[option
].enable(data
);
816 channel
->options
[option
] = (void*)1;
821 /*! \brief Disables an option globally
822 * \param dial Dial structure to disable option on
823 * \param option Option to disable
824 * \return Returns 0 on success, -1 on failure
826 int ast_dial_option_global_disable(struct ast_dial
*dial
, enum ast_dial_option option
)
828 /* If the option is not enabled, return failure */
829 if (!dial
->options
[option
])
832 /* Execute callback of option to disable if it exists */
833 if (option_types
[option
].disable
)
834 option_types
[option
].disable(dial
->options
[option
]);
836 /* Finally disable option on the structure */
837 dial
->options
[option
] = NULL
;
842 /*! \brief Disables an option per channel
843 * \param dial Dial structure
844 * \param num Channel number to disable option on
845 * \param option Option to disable
846 * \return Returns 0 on success, -1 on failure
848 int ast_dial_option_disable(struct ast_dial
*dial
, int num
, enum ast_dial_option option
)
850 struct ast_dial_channel
*channel
= NULL
;
852 /* Ensure we have required arguments */
853 if (!dial
|| AST_LIST_EMPTY(&dial
->channels
))
856 /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
857 AST_LIST_LOCK(&dial
->channels
);
858 if (AST_LIST_LAST(&dial
->channels
)->num
!= num
) {
859 AST_LIST_TRAVERSE(&dial
->channels
, channel
, list
) {
860 if (channel
->num
== num
)
864 channel
= AST_LIST_LAST(&dial
->channels
);
866 AST_LIST_UNLOCK(&dial
->channels
);
868 /* If none found, return failure */
872 /* If the option is not enabled, return failure */
873 if (!channel
->options
[option
])
876 /* Execute callback of option to disable it if it exists */
877 if (option_types
[option
].disable
)
878 option_types
[option
].disable(channel
->options
[option
]);
880 /* Finally disable the option on the structure */
881 channel
->options
[option
] = NULL
;
886 void ast_dial_set_state_callback(struct ast_dial
*dial
, ast_dial_state_callback callback
)
888 dial
->state_callback
= callback
;