Add an option to the dial API for playing music instead of ringing to the caller.
[asterisk/asterisk.git] / main / dial.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2007, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Dialing API
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
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>
37
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"
46 #include "asterisk/musiconhold.h"
47
48 /*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
49 struct ast_dial {
50         int num;                                           /*! Current number to give to next dialed channel */
51         enum ast_dial_result state;                       /*! Status of dial */
52         void *options[AST_DIAL_OPTION_MAX];                /*! Global options */
53         ast_dial_state_callback state_callback;          /*! Status callback */
54         AST_LIST_HEAD_NOLOCK(, ast_dial_channel) channels; /*! Channels being dialed */
55         pthread_t thread;                                  /*! Thread (if running in async) */
56 };
57
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         struct ast_channel *owner;             /*! Asterisk channel */
66         AST_LIST_ENTRY(ast_dial_channel) list; /*! Linked list information */
67 };
68
69 /*! \brief Typedef for dial option enable */
70 typedef void *(*ast_dial_option_cb_enable)(void *data);
71
72 /*! \brief Typedef for dial option disable */
73 typedef int (*ast_dial_option_cb_disable)(void *data);
74
75 /* Structure for 'ANSWER_EXEC' option */
76 struct answer_exec_struct {
77         char app[AST_MAX_APP]; /* Application name */
78         char *args;            /* Application arguments */
79 };
80
81 /* Enable function for 'ANSWER_EXEC' option */
82 static void *answer_exec_enable(void *data)
83 {
84         struct answer_exec_struct *answer_exec = NULL;
85         char *app = ast_strdupa((char*)data), *args = NULL;
86
87         /* Not giving any data to this option is bad, mmmk? */
88         if (ast_strlen_zero(app))
89                 return NULL;
90
91         /* Create new data structure */
92         if (!(answer_exec = ast_calloc(1, sizeof(*answer_exec))))
93                 return NULL;
94         
95         /* Parse out application and arguments */
96         if ((args = strchr(app, '|'))) {
97                 *args++ = '\0';
98                 answer_exec->args = ast_strdup(args);
99         }
100
101         /* Copy application name */
102         ast_copy_string(answer_exec->app, app, sizeof(answer_exec->app));
103
104         return answer_exec;
105 }
106
107 /* Disable function for 'ANSWER_EXEC' option */
108 static int answer_exec_disable(void *data)
109 {
110         struct answer_exec_struct *answer_exec = data;
111
112         /* Make sure we have a value */
113         if (!answer_exec)
114                 return -1;
115
116         /* If arguments are present, free them too */
117         if (answer_exec->args)
118                 free(answer_exec->args);
119
120         /* This is simple - just free the structure */
121         free(answer_exec);
122
123         return 0;
124 }
125
126 static void *music_enable(void *data)
127 {
128         return ast_strdup(data);
129 }
130
131 static int music_disable(void *data)
132 {
133         if (!data)
134                 return -1;
135
136         free(data);
137
138         return 0;
139 }
140
141 /* Application execution function for 'ANSWER_EXEC' option */
142 static void answer_exec_run(struct ast_channel *chan, char *app, char *args)
143 {
144         struct ast_app *ast_app = pbx_findapp(app);
145
146         /* If the application was not found, return immediately */
147         if (!ast_app)
148                 return;
149
150         /* All is well... execute the application */
151         pbx_exec(chan, ast_app, args);
152
153         return;
154 }
155
156 /*! \brief Options structure - maps options to respective handlers (enable/disable). This list MUST be perfectly kept in order, or else madness will happen. */
157 static const struct ast_option_types {
158         enum ast_dial_option option;
159         ast_dial_option_cb_enable enable;
160         ast_dial_option_cb_disable disable;
161 } option_types[] = {
162         { AST_DIAL_OPTION_RINGING, NULL, NULL },                                  /*!< Always indicate ringing to caller */
163         { AST_DIAL_OPTION_ANSWER_EXEC, answer_exec_enable, answer_exec_disable }, /*!< Execute application upon answer in async mode */
164         { AST_DIAL_OPTION_MUSIC, music_enable, music_disable },                   /*!< Play music to the caller instead of ringing */
165         { AST_DIAL_OPTION_MAX, NULL, NULL },                                      /*!< Terminator of list */
166 };
167
168 /* free the buffer if allocated, and set the pointer to the second arg */
169 #define S_REPLACE(s, new_val)           \
170         do {                            \
171                 if (s)                  \
172                         free(s);        \
173                 s = (new_val);          \
174         } while (0)
175
176 /*! \brief Maximum number of channels we can watch at a time */
177 #define AST_MAX_WATCHERS 256
178
179 /*! \brief Macro for finding the option structure to use on a dialed channel */
180 #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])
181
182 /*! \brief Macro that determines whether a channel is the caller or not */
183 #define IS_CALLER(chan, owner) (chan == owner ? 1 : 0)
184
185 /*! \brief New dialing structure
186  * \note Create a dialing structure
187  * \return Returns a calloc'd ast_dial structure, NULL on failure
188  */
189 struct ast_dial *ast_dial_create(void)
190 {
191         struct ast_dial *dial = NULL;
192
193         /* Allocate new memory for structure */
194         if (!(dial = ast_calloc(1, sizeof(*dial))))
195                 return NULL;
196
197         /* Initialize list of channels */
198         AST_LIST_HEAD_INIT_NOLOCK(&dial->channels);
199
200         /* Initialize thread to NULL */
201         dial->thread = AST_PTHREADT_NULL;
202
203         return dial;
204 }
205
206 /*! \brief Append a channel
207  * \note Appends a channel to a dialing structure
208  * \return Returns channel reference number on success, -1 on failure
209  */
210 int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device)
211 {
212         struct ast_dial_channel *channel = NULL;
213
214         /* Make sure we have required arguments */
215         if (!dial || !tech || !device)
216                 return -1;
217
218         /* Allocate new memory for dialed channel structure */
219         if (!(channel = ast_calloc(1, sizeof(*channel))))
220                 return -1;
221
222         /* Record technology and device for when we actually dial */
223         channel->tech = tech;
224         channel->device = device;
225
226         /* Grab reference number from dial structure */
227         channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
228
229         /* Insert into channels list */
230         AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
231
232         return channel->num;
233 }
234
235 /*! \brief Helper function that does the beginning dialing */
236 static int begin_dial(struct ast_dial *dial, struct ast_channel *chan)
237 {
238         struct ast_dial_channel *channel = NULL;
239         int success = 0, res = 0;
240
241         /* Iterate through channel list, requesting and calling each one */
242         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
243                 char numsubst[AST_MAX_EXTENSION];
244
245                 /* Copy device string over */
246                 ast_copy_string(numsubst, channel->device, sizeof(numsubst));
247
248                 /* Request that the channel be created */
249                 if (!(channel->owner = ast_request(channel->tech, 
250                         chan ? chan->nativeformats : AST_FORMAT_AUDIO_MASK, numsubst, &channel->cause))) {
251                         continue;
252                 }
253
254                 channel->owner->appl = "AppDial2";
255                 channel->owner->data = "(Outgoing Line)";
256                 channel->owner->whentohangup = 0;
257
258                 /* Inherit everything from he who spawned this Dial */
259                 if (chan) {
260                         ast_channel_inherit_variables(chan, channel->owner);
261
262                         /* Copy over callerid information */
263                         S_REPLACE(channel->owner->cid.cid_num, ast_strdup(chan->cid.cid_num));
264                         S_REPLACE(channel->owner->cid.cid_name, ast_strdup(chan->cid.cid_name));
265                         S_REPLACE(channel->owner->cid.cid_ani, ast_strdup(chan->cid.cid_ani));
266                         S_REPLACE(channel->owner->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
267         
268                         ast_string_field_set(channel->owner, language, chan->language);
269                         ast_string_field_set(channel->owner, accountcode, chan->accountcode);
270                         channel->owner->cdrflags = chan->cdrflags;
271                         if (ast_strlen_zero(channel->owner->musicclass))
272                                 ast_string_field_set(channel->owner, musicclass, chan->musicclass);
273         
274                         channel->owner->cid.cid_pres = chan->cid.cid_pres;
275                         channel->owner->cid.cid_ton = chan->cid.cid_ton;
276                         channel->owner->cid.cid_tns = chan->cid.cid_tns;
277                         channel->owner->adsicpe = chan->adsicpe;
278                         channel->owner->transfercapability = chan->transfercapability;
279                 }
280
281                 /* Actually call the device */
282                 if ((res = ast_call(channel->owner, numsubst, 0))) {
283                         ast_hangup(channel->owner);
284                         channel->owner = NULL;
285                 } else {
286                         success++;
287                         if (option_verbose > 2)
288                                 ast_verbose(VERBOSE_PREFIX_3 "Called %s\n", numsubst);
289                 }
290         }
291
292         /* If number of failures matches the number of channels, then this truly failed */
293         return success;
294 }
295
296 /*! \brief Helper function that finds the dialed channel based on owner */
297 static struct ast_dial_channel *find_relative_dial_channel(struct ast_dial *dial, struct ast_channel *owner)
298 {
299         struct ast_dial_channel *channel = NULL;
300
301         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
302                 if (channel->owner == owner)
303                         break;
304         }
305
306         return channel;
307 }
308
309 static void set_state(struct ast_dial *dial, enum ast_dial_result state)
310 {
311         dial->state = state;
312
313         if (dial->state_callback)
314                 dial->state_callback(dial);
315 }
316
317 /*! \brief Helper function that handles control frames WITH owner */
318 static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
319 {
320         if (fr->frametype == AST_FRAME_CONTROL) {
321                 switch (fr->subclass) {
322                 case AST_CONTROL_ANSWER:
323                         if (option_verbose > 2)
324                                 ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", channel->owner->name, chan->name);
325                         AST_LIST_REMOVE(&dial->channels, channel, list);
326                         AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
327                         set_state(dial, AST_DIAL_RESULT_ANSWERED);
328                         break;
329                 case AST_CONTROL_BUSY:
330                         if (option_verbose > 2)
331                                 ast_verbose(VERBOSE_PREFIX_3 "%s is busy\n", channel->owner->name);
332                         ast_hangup(channel->owner);
333                         channel->owner = NULL;
334                         break;
335                 case AST_CONTROL_CONGESTION:
336                         if (option_verbose > 2)
337                                 ast_verbose(VERBOSE_PREFIX_3 "%s is circuit-busy\n", channel->owner->name);
338                         ast_hangup(channel->owner);
339                         channel->owner = NULL;
340                         break;
341                 case AST_CONTROL_RINGING:
342                         if (option_verbose > 2)
343                                 ast_verbose(VERBOSE_PREFIX_3 "%s is ringing\n", channel->owner->name);
344                         if (!dial->options[AST_DIAL_OPTION_MUSIC])
345                                 ast_indicate(chan, AST_CONTROL_RINGING);
346                         break;
347                 case AST_CONTROL_PROGRESS:
348                         if (option_verbose > 2)
349                                 ast_verbose (VERBOSE_PREFIX_3 "%s is making progress, passing it to %s\n", channel->owner->name, chan->name);
350                         ast_indicate(chan, AST_CONTROL_PROGRESS);
351                         break;
352                 case AST_CONTROL_VIDUPDATE:
353                         if (option_verbose > 2)
354                                 ast_verbose (VERBOSE_PREFIX_3 "%s requested a video update, passing it to %s\n", channel->owner->name, chan->name);
355                         ast_indicate(chan, AST_CONTROL_VIDUPDATE);
356                         break;
357                 case AST_CONTROL_PROCEEDING:
358                         if (option_verbose > 2)
359                                 ast_verbose (VERBOSE_PREFIX_3 "%s is proceeding, passing it to %s\n", channel->owner->name, chan->name);
360                         ast_indicate(chan, AST_CONTROL_PROCEEDING);
361                         break;
362                 case AST_CONTROL_HOLD:
363                         if (option_verbose > 2)
364                                 ast_verbose(VERBOSE_PREFIX_3 "Call on %s placed on hold\n", chan->name);
365                         ast_indicate(chan, AST_CONTROL_HOLD);
366                         break;
367                 case AST_CONTROL_UNHOLD:
368                         if (option_verbose > 2)
369                                 ast_verbose(VERBOSE_PREFIX_3 "Call on %s left from hold\n", chan->name);
370                         ast_indicate(chan, AST_CONTROL_UNHOLD);
371                         break;
372                 case AST_CONTROL_OFFHOOK:
373                 case AST_CONTROL_FLASH:
374                         break;
375                 case -1:
376                         /* Prod the channel */
377                         ast_indicate(chan, -1);
378                         break;
379                 default:
380                         break;
381                 }
382         }
383
384         return;
385 }
386
387 /*! \brief Helper function that handles control frames WITHOUT owner */
388 static void handle_frame_ownerless(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr)
389 {
390         /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
391         if (fr->frametype != AST_FRAME_CONTROL)
392                 return;
393
394         switch (fr->subclass) {
395         case AST_CONTROL_ANSWER:
396                 if (option_verbose > 2)
397                         ast_verbose( VERBOSE_PREFIX_3 "%s answered\n", channel->owner->name);
398                 AST_LIST_REMOVE(&dial->channels, channel, list);
399                 AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
400                 set_state(dial, AST_DIAL_RESULT_ANSWERED);
401                 break;
402         case AST_CONTROL_BUSY:
403                 if (option_verbose > 2)
404                         ast_verbose(VERBOSE_PREFIX_3 "%s is busy\n", channel->owner->name);
405                 ast_hangup(channel->owner);
406                 channel->owner = NULL;
407                 break;
408         case AST_CONTROL_CONGESTION:
409                 if (option_verbose > 2)
410                         ast_verbose(VERBOSE_PREFIX_3 "%s is circuit-busy\n", channel->owner->name);
411                 ast_hangup(channel->owner);
412                 channel->owner = NULL;
413                 break;
414         case AST_CONTROL_RINGING:
415                 if (option_verbose > 2)
416                         ast_verbose(VERBOSE_PREFIX_3 "%s is ringing\n", channel->owner->name);
417                 set_state(dial, AST_DIAL_RESULT_RINGING);
418                 break;
419         case AST_CONTROL_PROGRESS:
420                 if (option_verbose > 2)
421                         ast_verbose (VERBOSE_PREFIX_3 "%s is making progress\n", channel->owner->name);
422                 set_state(dial, AST_DIAL_RESULT_PROGRESS);
423                 break;
424         case AST_CONTROL_PROCEEDING:
425                 if (option_verbose > 2)
426                         ast_verbose (VERBOSE_PREFIX_3 "%s is proceeding\n", channel->owner->name);
427                 set_state(dial, AST_DIAL_RESULT_PROCEEDING);
428                 break;
429         default:
430                 break;
431         }
432
433         return;
434 }
435
436 /*! \brief Helper function that basically keeps tabs on dialing attempts */
437 static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
438 {
439         int timeout = -1, count = 0;
440         struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
441         struct ast_dial_channel *channel = NULL;
442         struct answer_exec_struct *answer_exec = NULL;
443
444         set_state(dial, AST_DIAL_RESULT_TRYING);
445
446         /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
447         if (dial->options[AST_DIAL_OPTION_RINGING]) {
448                 set_state(dial, AST_DIAL_RESULT_RINGING);
449                 if (chan)
450                         ast_indicate(chan, AST_CONTROL_RINGING);
451         } else if (chan && dial->options[AST_DIAL_OPTION_MUSIC] && 
452                 !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
453                 char *original_moh = ast_strdupa(chan->musicclass);
454                 ast_indicate(chan, -1);
455                 ast_string_field_set(chan, musicclass, dial->options[AST_DIAL_OPTION_MUSIC]);
456                 ast_moh_start(chan, dial->options[AST_DIAL_OPTION_MUSIC], NULL);
457                 ast_string_field_set(chan, musicclass, original_moh);
458         }
459
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)) {
462                 int pos = 0;
463                 struct ast_frame *fr = NULL;
464
465                 /* Set up channel structure array */
466                 pos = count = 0;
467                 if (chan)
468                         cs[pos++] = chan;
469
470                 /* Add channels we are attempting to dial */
471                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
472                         if (channel->owner) {
473                                 cs[pos++] = channel->owner;
474                                 count++;
475                         }
476                 }
477
478                 /* If we have no outbound channels in progress, switch state to unanswered and stop */
479                 if (!count) {
480                         set_state(dial, AST_DIAL_RESULT_UNANSWERED);
481                         break;
482                 }
483
484                 /* Just to be safe... */
485                 if (dial->thread == AST_PTHREADT_STOP)
486                         break;
487
488                 /* Wait for frames from channels */
489                 who = ast_waitfor_n(cs, pos, &timeout);
490
491                 /* Check to see if our thread is being cancelled */
492                 if (dial->thread == AST_PTHREADT_STOP)
493                         break;
494
495                 /* If we are not being cancelled and we have no channel, then timeout was tripped */
496                 if (!who)
497                         continue;
498
499                 /* Find relative dial channel */
500                 if (!chan || !IS_CALLER(chan, who))
501                         channel = find_relative_dial_channel(dial, who);
502
503                 /* Attempt to read in a frame */
504                 if (!(fr = ast_read(who))) {
505                         /* If this is the caller then we switch state to hangup and stop */
506                         if (chan && IS_CALLER(chan, who)) {
507                                 set_state(dial, AST_DIAL_RESULT_HANGUP);
508                                 break;
509                         }
510                         ast_hangup(who);
511                         channel->owner = NULL;
512                         continue;
513                 }
514
515                 /* Process the frame */
516                 if (chan)
517                         handle_frame(dial, channel, fr, chan);
518                 else
519                         handle_frame_ownerless(dial, channel, fr);
520
521                 /* Free the received frame and start all over */
522                 ast_frfree(fr);
523         }
524
525         /* Do post-processing from loop */
526         if (dial->state == AST_DIAL_RESULT_ANSWERED) {
527                 /* Hangup everything except that which answered */
528                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
529                         if (!channel->owner || channel->owner == who)
530                                 continue;
531                         ast_hangup(channel->owner);
532                         channel->owner = NULL;
533                 }
534                 /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
535                 if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC)))
536                         answer_exec_run(who, answer_exec->app, answer_exec->args);
537
538                 if (chan && dial->options[AST_DIAL_OPTION_MUSIC] && 
539                         !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
540                         ast_moh_stop(chan);
541                 }
542         } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
543                 /* Hangup everything */
544                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
545                         if (!channel->owner)
546                                 continue;
547                         ast_hangup(channel->owner);
548                         channel->owner = NULL;
549                 }
550         }
551
552         return dial->state;
553 }
554
555 /*! \brief Dial async thread function */
556 static void *async_dial(void *data)
557 {
558         struct ast_dial *dial = data;
559
560         /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
561         monitor_dial(dial, NULL);
562
563         return NULL;
564 }
565
566 /*! \brief Execute dialing synchronously or asynchronously
567  * \note Dials channels in a dial structure.
568  * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
569  */
570 enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
571 {
572         enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
573
574         /* Ensure required arguments are passed */
575         if (!dial || (!chan && !async)) {
576                 ast_log(LOG_DEBUG, "invalid #1\n");
577                 return AST_DIAL_RESULT_INVALID;
578         }
579
580         /* If there are no channels to dial we can't very well try to dial them */
581         if (AST_LIST_EMPTY(&dial->channels)) {
582                 ast_log(LOG_DEBUG, "invalid #2\n");
583                 return AST_DIAL_RESULT_INVALID;
584         }
585
586         /* Dial each requested channel */
587         if (!begin_dial(dial, chan))
588                 return AST_DIAL_RESULT_FAILED;
589
590         /* If we are running async spawn a thread and send it away... otherwise block here */
591         if (async) {
592                 dial->state = AST_DIAL_RESULT_TRYING;
593                 /* Try to create a thread */
594                 if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
595                         /* Failed to create the thread - hangup all dialed channels and return failed */
596                         ast_dial_hangup(dial);
597                         res = AST_DIAL_RESULT_FAILED;
598                 }
599         } else {
600                 res = monitor_dial(dial, chan);
601         }
602
603         return res;
604 }
605
606 /*! \brief Return channel that answered
607  * \note Returns the Asterisk channel that answered
608  * \param dial Dialing structure
609  */
610 struct ast_channel *ast_dial_answered(struct ast_dial *dial)
611 {
612         if (!dial)
613                 return NULL;
614
615         return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
616 }
617
618 /*! \brief Return state of dial
619  * \note Returns the state of the dial attempt
620  * \param dial Dialing structure
621  */
622 enum ast_dial_result ast_dial_state(struct ast_dial *dial)
623 {
624         return dial->state;
625 }
626
627 /*! \brief Cancel async thread
628  * \note Cancel a running async thread
629  * \param dial Dialing structure
630  */
631 enum ast_dial_result ast_dial_join(struct ast_dial *dial)
632 {
633         pthread_t thread;
634
635         /* If the dial structure is not running in async, return failed */
636         if (dial->thread == AST_PTHREADT_NULL)
637                 return AST_DIAL_RESULT_FAILED;
638
639         /* Record thread */
640         thread = dial->thread;
641
642         /* Stop the thread */
643         dial->thread = AST_PTHREADT_STOP;
644
645         /* Now we signal it with SIGURG so it will break out of it's waitfor */
646         pthread_kill(thread, SIGURG);
647
648         /* Finally wait for the thread to exit */
649         pthread_join(thread, NULL);
650
651         /* Yay thread is all gone */
652         dial->thread = AST_PTHREADT_NULL;
653
654         return dial->state;
655 }
656
657 /*! \brief Hangup channels
658  * \note Hangup all active channels
659  * \param dial Dialing structure
660  */
661 void ast_dial_hangup(struct ast_dial *dial)
662 {
663         struct ast_dial_channel *channel = NULL;
664
665         if (!dial)
666                 return;
667         
668         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
669                 if (channel->owner) {
670                         ast_hangup(channel->owner);
671                         channel->owner = NULL;
672                 }
673         }
674
675         return;
676 }
677
678 /*! \brief Destroys a dialing structure
679  * \note Destroys (free's) the given ast_dial structure
680  * \param dial Dialing structure to free
681  * \return Returns 0 on success, -1 on failure
682  */
683 int ast_dial_destroy(struct ast_dial *dial)
684 {
685         int i = 0;
686         struct ast_dial_channel *channel = NULL;
687
688         if (!dial)
689                 return -1;
690         
691         /* Hangup and deallocate all the dialed channels */
692         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
693                 /* Disable any enabled options */
694                 for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
695                         if (!channel->options[i])
696                                 continue;
697                         if (option_types[i].disable)
698                                 option_types[i].disable(channel->options[i]);
699                         channel->options[i] = NULL;
700                 }
701                 /* Hang up channel if need be */
702                 if (channel->owner) {
703                         ast_hangup(channel->owner);
704                         channel->owner = NULL;
705                 }
706                 /* Free structure */
707                 free(channel);
708         }
709        
710         /* Disable any enabled options globally */
711         for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
712                 if (!dial->options[i])
713                         continue;
714                 if (option_types[i].disable)
715                         option_types[i].disable(dial->options[i]);
716                 dial->options[i] = NULL;
717         }
718
719         /* Free structure */
720         free(dial);
721
722         return 0;
723 }
724
725 /*! \brief Enables an option globally
726  * \param dial Dial structure to enable option on
727  * \param option Option to enable
728  * \param data Data to pass to this option (not always needed)
729  * \return Returns 0 on success, -1 on failure
730  */
731 int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
732 {
733         /* If the option is already enabled, return failure */
734         if (dial->options[option])
735                 return -1;
736
737         /* Execute enable callback if it exists, if not simply make sure the value is set */
738         if (option_types[option].enable)
739                 dial->options[option] = option_types[option].enable(data);
740         else
741                 dial->options[option] = (void*)1;
742
743         return 0;
744 }
745
746 /*! \brief Enables an option per channel
747  * \param dial Dial structure
748  * \param num Channel number to enable option on
749  * \param option Option to enable
750  * \param data Data to pass to this option (not always needed)
751  * \return Returns 0 on success, -1 on failure
752  */
753 int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
754 {
755         struct ast_dial_channel *channel = NULL;
756
757         /* Ensure we have required arguments */
758         if (!dial || AST_LIST_EMPTY(&dial->channels))
759                 return -1;
760         
761         /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
762         if (AST_LIST_LAST(&dial->channels)->num != num) {
763                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
764                         if (channel->num == num)
765                                 break;
766                 }
767         } else {
768                 channel = AST_LIST_LAST(&dial->channels);
769         }
770
771         /* If none found, return failure */
772         if (!channel)
773                 return -1;
774
775         /* If the option is already enabled, return failure */
776         if (channel->options[option])
777                 return -1;
778
779         /* Execute enable callback if it exists, if not simply make sure the value is set */
780         if (option_types[option].enable)
781                 channel->options[option] = option_types[option].enable(data);
782         else
783                 channel->options[option] = (void*)1;
784
785         return 0;
786 }
787
788 /*! \brief Disables an option globally
789  * \param dial Dial structure to disable option on
790  * \param option Option to disable
791  * \return Returns 0 on success, -1 on failure
792  */
793 int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
794 {
795         /* If the option is not enabled, return failure */
796         if (!dial->options[option])
797                 return -1;
798
799         /* Execute callback of option to disable if it exists */
800         if (option_types[option].disable)
801                 option_types[option].disable(dial->options[option]);
802
803         /* Finally disable option on the structure */
804         dial->options[option] = NULL;
805
806         return 0;
807 }
808
809 /*! \brief Disables an option per channel
810  * \param dial Dial structure
811  * \param num Channel number to disable option on
812  * \param option Option to disable
813  * \return Returns 0 on success, -1 on failure
814  */
815 int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
816 {
817         struct ast_dial_channel *channel = NULL;
818
819         /* Ensure we have required arguments */
820         if (!dial || AST_LIST_EMPTY(&dial->channels))
821                 return -1;
822
823         /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
824         if (AST_LIST_LAST(&dial->channels)->num != num) {
825                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
826                         if (channel->num == num)
827                                 break;
828                 }
829         } else {
830                 channel = AST_LIST_LAST(&dial->channels);
831         }
832
833         /* If none found, return failure */
834         if (!channel)
835                 return -1;
836
837         /* If the option is not enabled, return failure */
838         if (!channel->options[option])
839                 return -1;
840
841         /* Execute callback of option to disable it if it exists */
842         if (option_types[option].disable)
843                 option_types[option].disable(channel->options[option]);
844
845         /* Finally disable the option on the structure */
846         channel->options[option] = NULL;
847
848         return 0;
849 }
850
851 void ast_dial_set_state_callback(struct ast_dial *dial, ast_dial_state_callback callback)
852 {
853         dial->state_callback = callback;
854 }