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_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 case AST_CONTROL_PROCEEDING
:
366 if (option_verbose
> 2)
367 ast_verbose (VERBOSE_PREFIX_3
"%s is proceeding, passing it to %s\n", channel
->owner
->name
, chan
->name
);
368 ast_indicate(chan
, AST_CONTROL_PROCEEDING
);
369 set_state(dial
, AST_DIAL_RESULT_PROCEEDING
);
371 case AST_CONTROL_HOLD
:
372 if (option_verbose
> 2)
373 ast_verbose(VERBOSE_PREFIX_3
"Call on %s placed on hold\n", chan
->name
);
374 ast_indicate(chan
, AST_CONTROL_HOLD
);
376 case AST_CONTROL_UNHOLD
:
377 if (option_verbose
> 2)
378 ast_verbose(VERBOSE_PREFIX_3
"Call on %s left from hold\n", chan
->name
);
379 ast_indicate(chan
, AST_CONTROL_UNHOLD
);
381 case AST_CONTROL_OFFHOOK
:
382 case AST_CONTROL_FLASH
:
385 /* Prod the channel */
386 ast_indicate(chan
, -1);
396 /*! \brief Helper function that handles control frames WITHOUT owner */
397 static void handle_frame_ownerless(struct ast_dial
*dial
, struct ast_dial_channel
*channel
, struct ast_frame
*fr
)
399 /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
400 if (fr
->frametype
!= AST_FRAME_CONTROL
)
403 switch (fr
->subclass
) {
404 case AST_CONTROL_ANSWER
:
405 if (option_verbose
> 2)
406 ast_verbose( VERBOSE_PREFIX_3
"%s answered\n", channel
->owner
->name
);
407 AST_LIST_LOCK(&dial
->channels
);
408 AST_LIST_REMOVE(&dial
->channels
, channel
, list
);
409 AST_LIST_INSERT_HEAD(&dial
->channels
, channel
, list
);
410 AST_LIST_UNLOCK(&dial
->channels
);
411 set_state(dial
, AST_DIAL_RESULT_ANSWERED
);
413 case AST_CONTROL_BUSY
:
414 if (option_verbose
> 2)
415 ast_verbose(VERBOSE_PREFIX_3
"%s is busy\n", channel
->owner
->name
);
416 ast_hangup(channel
->owner
);
417 channel
->owner
= NULL
;
419 case AST_CONTROL_CONGESTION
:
420 if (option_verbose
> 2)
421 ast_verbose(VERBOSE_PREFIX_3
"%s is circuit-busy\n", channel
->owner
->name
);
422 ast_hangup(channel
->owner
);
423 channel
->owner
= NULL
;
425 case AST_CONTROL_RINGING
:
426 if (option_verbose
> 2)
427 ast_verbose(VERBOSE_PREFIX_3
"%s is ringing\n", channel
->owner
->name
);
428 set_state(dial
, AST_DIAL_RESULT_RINGING
);
430 case AST_CONTROL_PROGRESS
:
431 if (option_verbose
> 2)
432 ast_verbose (VERBOSE_PREFIX_3
"%s is making progress\n", channel
->owner
->name
);
433 set_state(dial
, AST_DIAL_RESULT_PROGRESS
);
435 case AST_CONTROL_PROCEEDING
:
436 if (option_verbose
> 2)
437 ast_verbose (VERBOSE_PREFIX_3
"%s is proceeding\n", channel
->owner
->name
);
438 set_state(dial
, AST_DIAL_RESULT_PROCEEDING
);
447 /*! \brief Helper function that basically keeps tabs on dialing attempts */
448 static enum ast_dial_result
monitor_dial(struct ast_dial
*dial
, struct ast_channel
*chan
)
450 int timeout
= -1, count
= 0;
451 struct ast_channel
*cs
[AST_MAX_WATCHERS
], *who
= NULL
;
452 struct ast_dial_channel
*channel
= NULL
;
453 struct answer_exec_struct
*answer_exec
= NULL
;
455 set_state(dial
, AST_DIAL_RESULT_TRYING
);
457 /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
458 if (dial
->options
[AST_DIAL_OPTION_RINGING
]) {
459 set_state(dial
, AST_DIAL_RESULT_RINGING
);
461 ast_indicate(chan
, AST_CONTROL_RINGING
);
464 /* Go into an infinite loop while we are trying */
465 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 struct ast_frame
*fr
= NULL
;
469 /* Set up channel structure array */
474 /* Add channels we are attempting to dial */
475 AST_LIST_LOCK(&dial
->channels
);
476 AST_LIST_TRAVERSE(&dial
->channels
, channel
, list
) {
477 if (channel
->owner
) {
478 cs
[pos
++] = channel
->owner
;
482 AST_LIST_UNLOCK(&dial
->channels
);
484 /* If we have no outbound channels in progress, switch state to unanswered and stop */
486 set_state(dial
, AST_DIAL_RESULT_UNANSWERED
);
490 /* Just to be safe... */
491 if (dial
->thread
== AST_PTHREADT_STOP
)
494 /* Wait for frames from channels */
495 who
= ast_waitfor_n(cs
, pos
, &timeout
);
497 /* Check to see if our thread is being cancelled */
498 if (dial
->thread
== AST_PTHREADT_STOP
)
501 /* If we are not being cancelled and we have no channel, then timeout was tripped */
505 /* Find relative dial channel */
506 if (!chan
|| !IS_CALLER(chan
, who
))
507 channel
= find_relative_dial_channel(dial
, who
);
509 /* Attempt to read in a frame */
510 if (!(fr
= ast_read(who
))) {
511 /* If this is the caller then we switch state to hangup and stop */
512 if (chan
&& IS_CALLER(chan
, who
)) {
513 set_state(dial
, AST_DIAL_RESULT_HANGUP
);
517 channel
->owner
= NULL
;
521 /* Process the frame */
523 handle_frame(dial
, channel
, fr
, chan
);
525 handle_frame_ownerless(dial
, channel
, fr
);
527 /* Free the received frame and start all over */
531 /* Do post-processing from loop */
532 if (dial
->state
== AST_DIAL_RESULT_ANSWERED
) {
533 /* Hangup everything except that which answered */
534 AST_LIST_LOCK(&dial
->channels
);
535 AST_LIST_TRAVERSE(&dial
->channels
, channel
, list
) {
536 if (!channel
->owner
|| channel
->owner
== who
)
538 ast_hangup(channel
->owner
);
539 channel
->owner
= NULL
;
541 AST_LIST_UNLOCK(&dial
->channels
);
542 /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
543 if ((channel
= find_relative_dial_channel(dial
, who
)) && (answer_exec
= FIND_RELATIVE_OPTION(dial
, channel
, AST_DIAL_OPTION_ANSWER_EXEC
))) {
544 channel
->is_running_app
= 1;
545 answer_exec_run(dial
, channel
, answer_exec
->app
, answer_exec
->args
);
546 channel
->is_running_app
= 0;
548 } else if (dial
->state
== AST_DIAL_RESULT_HANGUP
) {
549 /* Hangup everything */
550 AST_LIST_LOCK(&dial
->channels
);
551 AST_LIST_TRAVERSE(&dial
->channels
, channel
, list
) {
554 ast_hangup(channel
->owner
);
555 channel
->owner
= NULL
;
557 AST_LIST_UNLOCK(&dial
->channels
);
563 /*! \brief Dial async thread function */
564 static void *async_dial(void *data
)
566 struct ast_dial
*dial
= data
;
568 /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
569 monitor_dial(dial
, NULL
);
574 /*! \brief Execute dialing synchronously or asynchronously
575 * \note Dials channels in a dial structure.
576 * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
578 enum ast_dial_result
ast_dial_run(struct ast_dial
*dial
, struct ast_channel
*chan
, int async
)
580 enum ast_dial_result res
= AST_DIAL_RESULT_TRYING
;
582 /* Ensure required arguments are passed */
583 if (!dial
|| (!chan
&& !async
)) {
584 ast_log(LOG_DEBUG
, "invalid #1\n");
585 return AST_DIAL_RESULT_INVALID
;
588 /* If there are no channels to dial we can't very well try to dial them */
589 if (AST_LIST_EMPTY(&dial
->channels
)) {
590 ast_log(LOG_DEBUG
, "invalid #2\n");
591 return AST_DIAL_RESULT_INVALID
;
594 /* Dial each requested channel */
595 if (!begin_dial(dial
, chan
))
596 return AST_DIAL_RESULT_FAILED
;
598 /* If we are running async spawn a thread and send it away... otherwise block here */
600 dial
->state
= AST_DIAL_RESULT_TRYING
;
601 /* Try to create a thread */
602 if (ast_pthread_create(&dial
->thread
, NULL
, async_dial
, dial
)) {
603 /* Failed to create the thread - hangup all dialed channels and return failed */
604 ast_dial_hangup(dial
);
605 res
= AST_DIAL_RESULT_FAILED
;
608 res
= monitor_dial(dial
, chan
);
614 /*! \brief Return channel that answered
615 * \note Returns the Asterisk channel that answered
616 * \param dial Dialing structure
618 struct ast_channel
*ast_dial_answered(struct ast_dial
*dial
)
623 return ((dial
->state
== AST_DIAL_RESULT_ANSWERED
) ? AST_LIST_FIRST(&dial
->channels
)->owner
: NULL
);
626 /*! \brief Return state of dial
627 * \note Returns the state of the dial attempt
628 * \param dial Dialing structure
630 enum ast_dial_result
ast_dial_state(struct ast_dial
*dial
)
635 /*! \brief Cancel async thread
636 * \note Cancel a running async thread
637 * \param dial Dialing structure
639 enum ast_dial_result
ast_dial_join(struct ast_dial
*dial
)
643 /* If the dial structure is not running in async, return failed */
644 if (dial
->thread
== AST_PTHREADT_NULL
)
645 return AST_DIAL_RESULT_FAILED
;
648 thread
= dial
->thread
;
650 /* Boom, commence locking */
651 ast_mutex_lock(&dial
->lock
);
653 /* Stop the thread */
654 dial
->thread
= AST_PTHREADT_STOP
;
656 /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
657 AST_LIST_LOCK(&dial
->channels
);
658 if (AST_LIST_FIRST(&dial
->channels
)->is_running_app
) {
659 struct ast_channel
*chan
= AST_LIST_FIRST(&dial
->channels
)->owner
;
661 ast_channel_lock(chan
);
662 ast_softhangup(chan
, AST_SOFTHANGUP_EXPLICIT
);
663 ast_channel_unlock(chan
);
666 /* Now we signal it with SIGURG so it will break out of it's waitfor */
667 pthread_kill(thread
, SIGURG
);
669 AST_LIST_UNLOCK(&dial
->channels
);
671 /* Yay done with it */
672 ast_mutex_unlock(&dial
->lock
);
674 /* Finally wait for the thread to exit */
675 pthread_join(thread
, NULL
);
677 /* Yay thread is all gone */
678 dial
->thread
= AST_PTHREADT_NULL
;
683 /*! \brief Hangup channels
684 * \note Hangup all active channels
685 * \param dial Dialing structure
687 void ast_dial_hangup(struct ast_dial
*dial
)
689 struct ast_dial_channel
*channel
= NULL
;
694 AST_LIST_LOCK(&dial
->channels
);
695 AST_LIST_TRAVERSE(&dial
->channels
, channel
, list
) {
696 if (channel
->owner
) {
697 ast_hangup(channel
->owner
);
698 channel
->owner
= NULL
;
701 AST_LIST_UNLOCK(&dial
->channels
);
706 /*! \brief Destroys a dialing structure
707 * \note Destroys (free's) the given ast_dial structure
708 * \param dial Dialing structure to free
709 * \return Returns 0 on success, -1 on failure
711 int ast_dial_destroy(struct ast_dial
*dial
)
714 struct ast_dial_channel
*channel
= NULL
;
719 /* Hangup and deallocate all the dialed channels */
720 AST_LIST_LOCK(&dial
->channels
);
721 AST_LIST_TRAVERSE_SAFE_BEGIN(&dial
->channels
, channel
, list
) {
722 /* Disable any enabled options */
723 for (i
= 0; i
< AST_DIAL_OPTION_MAX
; i
++) {
724 if (!channel
->options
[i
])
726 if (option_types
[i
].disable
)
727 option_types
[i
].disable(channel
->options
[i
]);
728 channel
->options
[i
] = NULL
;
730 /* Hang up channel if need be */
731 if (channel
->owner
) {
732 ast_hangup(channel
->owner
);
733 channel
->owner
= NULL
;
736 AST_LIST_REMOVE_CURRENT(&dial
->channels
, list
);
739 AST_LIST_TRAVERSE_SAFE_END
;
740 AST_LIST_UNLOCK(&dial
->channels
);
742 /* Disable any enabled options globally */
743 for (i
= 0; i
< AST_DIAL_OPTION_MAX
; i
++) {
744 if (!dial
->options
[i
])
746 if (option_types
[i
].disable
)
747 option_types
[i
].disable(dial
->options
[i
]);
748 dial
->options
[i
] = NULL
;
752 ast_mutex_destroy(&dial
->lock
);
760 /*! \brief Enables an option globally
761 * \param dial Dial structure to enable option on
762 * \param option Option to enable
763 * \param data Data to pass to this option (not always needed)
764 * \return Returns 0 on success, -1 on failure
766 int ast_dial_option_global_enable(struct ast_dial
*dial
, enum ast_dial_option option
, void *data
)
768 /* If the option is already enabled, return failure */
769 if (dial
->options
[option
])
772 /* Execute enable callback if it exists, if not simply make sure the value is set */
773 if (option_types
[option
].enable
)
774 dial
->options
[option
] = option_types
[option
].enable(data
);
776 dial
->options
[option
] = (void*)1;
781 /*! \brief Enables an option per channel
782 * \param dial Dial structure
783 * \param num Channel number to enable option on
784 * \param option Option to enable
785 * \param data Data to pass to this option (not always needed)
786 * \return Returns 0 on success, -1 on failure
788 int ast_dial_option_enable(struct ast_dial
*dial
, int num
, enum ast_dial_option option
, void *data
)
790 struct ast_dial_channel
*channel
= NULL
;
792 /* Ensure we have required arguments */
793 if (!dial
|| AST_LIST_EMPTY(&dial
->channels
))
796 /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
797 AST_LIST_LOCK(&dial
->channels
);
798 if (AST_LIST_LAST(&dial
->channels
)->num
!= num
) {
799 AST_LIST_TRAVERSE(&dial
->channels
, channel
, list
) {
800 if (channel
->num
== num
)
804 channel
= AST_LIST_LAST(&dial
->channels
);
806 AST_LIST_UNLOCK(&dial
->channels
);
808 /* If none found, return failure */
812 /* If the option is already enabled, return failure */
813 if (channel
->options
[option
])
816 /* Execute enable callback if it exists, if not simply make sure the value is set */
817 if (option_types
[option
].enable
)
818 channel
->options
[option
] = option_types
[option
].enable(data
);
820 channel
->options
[option
] = (void*)1;
825 /*! \brief Disables an option globally
826 * \param dial Dial structure to disable option on
827 * \param option Option to disable
828 * \return Returns 0 on success, -1 on failure
830 int ast_dial_option_global_disable(struct ast_dial
*dial
, enum ast_dial_option option
)
832 /* If the option is not enabled, return failure */
833 if (!dial
->options
[option
])
836 /* Execute callback of option to disable if it exists */
837 if (option_types
[option
].disable
)
838 option_types
[option
].disable(dial
->options
[option
]);
840 /* Finally disable option on the structure */
841 dial
->options
[option
] = NULL
;
846 /*! \brief Disables an option per channel
847 * \param dial Dial structure
848 * \param num Channel number to disable option on
849 * \param option Option to disable
850 * \return Returns 0 on success, -1 on failure
852 int ast_dial_option_disable(struct ast_dial
*dial
, int num
, enum ast_dial_option option
)
854 struct ast_dial_channel
*channel
= NULL
;
856 /* Ensure we have required arguments */
857 if (!dial
|| AST_LIST_EMPTY(&dial
->channels
))
860 /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
861 AST_LIST_LOCK(&dial
->channels
);
862 if (AST_LIST_LAST(&dial
->channels
)->num
!= num
) {
863 AST_LIST_TRAVERSE(&dial
->channels
, channel
, list
) {
864 if (channel
->num
== num
)
868 channel
= AST_LIST_LAST(&dial
->channels
);
870 AST_LIST_UNLOCK(&dial
->channels
);
872 /* If none found, return failure */
876 /* If the option is not enabled, return failure */
877 if (!channel
->options
[option
])
880 /* Execute callback of option to disable it if it exists */
881 if (option_types
[option
].disable
)
882 option_types
[option
].disable(channel
->options
[option
]);
884 /* Finally disable the option on the structure */
885 channel
->options
[option
] = NULL
;
890 void ast_dial_set_state_callback(struct ast_dial
*dial
, ast_dial_state_callback callback
)
892 dial
->state_callback
= callback
;