Do a massive conversion for using the ast_verb() macro
[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 /*! \brief 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 /*! \brief 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 /*! \brief 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                 ast_free(answer_exec->args);
119
120         /* This is simple - just free the structure */
121         ast_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         ast_free(data);
137
138         return 0;
139 }
140
141 /*! \brief 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 /*! \brief 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                         ast_verb(3, "Called %s\n", numsubst);
288                 }
289         }
290
291         /* If number of failures matches the number of channels, then this truly failed */
292         return success;
293 }
294
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)
297 {
298         struct ast_dial_channel *channel = NULL;
299
300         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
301                 if (channel->owner == owner)
302                         break;
303         }
304
305         return channel;
306 }
307
308 static void set_state(struct ast_dial *dial, enum ast_dial_result state)
309 {
310         dial->state = state;
311
312         if (dial->state_callback)
313                 dial->state_callback(dial);
314 }
315
316 /*! \brief Helper function that handles control frames WITH owner */
317 static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
318 {
319         if (fr->frametype == AST_FRAME_CONTROL) {
320                 switch (fr->subclass) {
321                 case AST_CONTROL_ANSWER:
322                         ast_verb(3, "%s answered %s\n", channel->owner->name, chan->name);
323                         AST_LIST_REMOVE(&dial->channels, channel, list);
324                         AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
325                         set_state(dial, AST_DIAL_RESULT_ANSWERED);
326                         break;
327                 case AST_CONTROL_BUSY:
328                         ast_verb(3, "%s is busy\n", channel->owner->name);
329                         ast_hangup(channel->owner);
330                         channel->owner = NULL;
331                         break;
332                 case AST_CONTROL_CONGESTION:
333                         ast_verb(3, "%s is circuit-busy\n", channel->owner->name);
334                         ast_hangup(channel->owner);
335                         channel->owner = NULL;
336                         break;
337                 case AST_CONTROL_RINGING:
338                         ast_verb(3, "%s is ringing\n", channel->owner->name);
339                         if (!dial->options[AST_DIAL_OPTION_MUSIC])
340                                 ast_indicate(chan, AST_CONTROL_RINGING);
341                         set_state(dial, AST_DIAL_RESULT_RINGING);
342                         break;
343                 case AST_CONTROL_PROGRESS:
344                         ast_verb(3, "%s is making progress, passing it to %s\n", channel->owner->name, chan->name);
345                         ast_indicate(chan, AST_CONTROL_PROGRESS);
346                         set_state(dial, AST_DIAL_RESULT_PROGRESS);
347                         break;
348                 case AST_CONTROL_VIDUPDATE:
349                         ast_verb(3, "%s requested a video update, passing it to %s\n", channel->owner->name, chan->name);
350                         ast_indicate(chan, AST_CONTROL_VIDUPDATE);
351                         break;
352                 case AST_CONTROL_PROCEEDING:
353                         ast_verb(3, "%s is proceeding, passing it to %s\n", channel->owner->name, chan->name);
354                         ast_indicate(chan, AST_CONTROL_PROCEEDING);
355                         set_state(dial, AST_DIAL_RESULT_PROCEEDING);
356                         break;
357                 case AST_CONTROL_HOLD:
358                         ast_verb(3, "Call on %s placed on hold\n", chan->name);
359                         ast_indicate(chan, AST_CONTROL_HOLD);
360                         break;
361                 case AST_CONTROL_UNHOLD:
362                         ast_verb(3, "Call on %s left from hold\n", chan->name);
363                         ast_indicate(chan, AST_CONTROL_UNHOLD);
364                         break;
365                 case AST_CONTROL_OFFHOOK:
366                 case AST_CONTROL_FLASH:
367                         break;
368                 case -1:
369                         /* Prod the channel */
370                         ast_indicate(chan, -1);
371                         break;
372                 default:
373                         break;
374                 }
375         }
376
377         return;
378 }
379
380 /*! \brief Helper function that handles control frames WITHOUT owner */
381 static void handle_frame_ownerless(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr)
382 {
383         /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
384         if (fr->frametype != AST_FRAME_CONTROL)
385                 return;
386
387         switch (fr->subclass) {
388         case AST_CONTROL_ANSWER:
389                 ast_verb(3, "%s answered\n", channel->owner->name);
390                 AST_LIST_REMOVE(&dial->channels, channel, list);
391                 AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
392                 set_state(dial, AST_DIAL_RESULT_ANSWERED);
393                 break;
394         case AST_CONTROL_BUSY:
395                 ast_verb(3, "%s is busy\n", channel->owner->name);
396                 ast_hangup(channel->owner);
397                 channel->owner = NULL;
398                 break;
399         case AST_CONTROL_CONGESTION:
400                 ast_verb(3, "%s is circuit-busy\n", channel->owner->name);
401                 ast_hangup(channel->owner);
402                 channel->owner = NULL;
403                 break;
404         case AST_CONTROL_RINGING:
405                 ast_verb(3, "%s is ringing\n", channel->owner->name);
406                 set_state(dial, AST_DIAL_RESULT_RINGING);
407                 break;
408         case AST_CONTROL_PROGRESS:
409                 ast_verb(3, "%s is making progress\n", channel->owner->name);
410                 set_state(dial, AST_DIAL_RESULT_PROGRESS);
411                 break;
412         case AST_CONTROL_PROCEEDING:
413                 ast_verb(3, "%s is proceeding\n", channel->owner->name);
414                 set_state(dial, AST_DIAL_RESULT_PROCEEDING);
415                 break;
416         default:
417                 break;
418         }
419
420         return;
421 }
422
423 /*! \brief Helper function that basically keeps tabs on dialing attempts */
424 static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
425 {
426         int timeout = -1, count = 0;
427         struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
428         struct ast_dial_channel *channel = NULL;
429         struct answer_exec_struct *answer_exec = NULL;
430
431         set_state(dial, AST_DIAL_RESULT_TRYING);
432
433         /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
434         if (dial->options[AST_DIAL_OPTION_RINGING]) {
435                 set_state(dial, AST_DIAL_RESULT_RINGING);
436                 if (chan)
437                         ast_indicate(chan, AST_CONTROL_RINGING);
438         } else if (chan && dial->options[AST_DIAL_OPTION_MUSIC] && 
439                 !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
440                 char *original_moh = ast_strdupa(chan->musicclass);
441                 ast_indicate(chan, -1);
442                 ast_string_field_set(chan, musicclass, dial->options[AST_DIAL_OPTION_MUSIC]);
443                 ast_moh_start(chan, dial->options[AST_DIAL_OPTION_MUSIC], NULL);
444                 ast_string_field_set(chan, musicclass, original_moh);
445         }
446
447         /* Go into an infinite loop while we are trying */
448         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)) {
449                 int pos = 0;
450                 struct ast_frame *fr = NULL;
451
452                 /* Set up channel structure array */
453                 pos = count = 0;
454                 if (chan)
455                         cs[pos++] = chan;
456
457                 /* Add channels we are attempting to dial */
458                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
459                         if (channel->owner) {
460                                 cs[pos++] = channel->owner;
461                                 count++;
462                         }
463                 }
464
465                 /* If we have no outbound channels in progress, switch state to unanswered and stop */
466                 if (!count) {
467                         set_state(dial, AST_DIAL_RESULT_UNANSWERED);
468                         break;
469                 }
470
471                 /* Just to be safe... */
472                 if (dial->thread == AST_PTHREADT_STOP)
473                         break;
474
475                 /* Wait for frames from channels */
476                 who = ast_waitfor_n(cs, pos, &timeout);
477
478                 /* Check to see if our thread is being cancelled */
479                 if (dial->thread == AST_PTHREADT_STOP)
480                         break;
481
482                 /* If we are not being cancelled and we have no channel, then timeout was tripped */
483                 if (!who)
484                         continue;
485
486                 /* Find relative dial channel */
487                 if (!chan || !IS_CALLER(chan, who))
488                         channel = find_relative_dial_channel(dial, who);
489
490                 /* Attempt to read in a frame */
491                 if (!(fr = ast_read(who))) {
492                         /* If this is the caller then we switch state to hangup and stop */
493                         if (chan && IS_CALLER(chan, who)) {
494                                 set_state(dial, AST_DIAL_RESULT_HANGUP);
495                                 break;
496                         }
497                         ast_hangup(who);
498                         channel->owner = NULL;
499                         continue;
500                 }
501
502                 /* Process the frame */
503                 if (chan)
504                         handle_frame(dial, channel, fr, chan);
505                 else
506                         handle_frame_ownerless(dial, channel, fr);
507
508                 /* Free the received frame and start all over */
509                 ast_frfree(fr);
510         }
511
512         /* Do post-processing from loop */
513         if (dial->state == AST_DIAL_RESULT_ANSWERED) {
514                 /* Hangup everything except that which answered */
515                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
516                         if (!channel->owner || channel->owner == who)
517                                 continue;
518                         ast_hangup(channel->owner);
519                         channel->owner = NULL;
520                 }
521                 /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
522                 if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC)))
523                         answer_exec_run(who, answer_exec->app, answer_exec->args);
524
525                 if (chan && dial->options[AST_DIAL_OPTION_MUSIC] && 
526                         !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
527                         ast_moh_stop(chan);
528                 }
529         } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
530                 /* Hangup everything */
531                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
532                         if (!channel->owner)
533                                 continue;
534                         ast_hangup(channel->owner);
535                         channel->owner = NULL;
536                 }
537         }
538
539         return dial->state;
540 }
541
542 /*! \brief Dial async thread function */
543 static void *async_dial(void *data)
544 {
545         struct ast_dial *dial = data;
546
547         /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
548         monitor_dial(dial, NULL);
549
550         return NULL;
551 }
552
553 /*! \brief Execute dialing synchronously or asynchronously
554  * \note Dials channels in a dial structure.
555  * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
556  */
557 enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
558 {
559         enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
560
561         /* Ensure required arguments are passed */
562         if (!dial || (!chan && !async)) {
563                 ast_debug(1, "invalid #1\n");
564                 return AST_DIAL_RESULT_INVALID;
565         }
566
567         /* If there are no channels to dial we can't very well try to dial them */
568         if (AST_LIST_EMPTY(&dial->channels)) {
569                 ast_debug(1, "invalid #2\n");
570                 return AST_DIAL_RESULT_INVALID;
571         }
572
573         /* Dial each requested channel */
574         if (!begin_dial(dial, chan))
575                 return AST_DIAL_RESULT_FAILED;
576
577         /* If we are running async spawn a thread and send it away... otherwise block here */
578         if (async) {
579                 dial->state = AST_DIAL_RESULT_TRYING;
580                 /* Try to create a thread */
581                 if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
582                         /* Failed to create the thread - hangup all dialed channels and return failed */
583                         ast_dial_hangup(dial);
584                         res = AST_DIAL_RESULT_FAILED;
585                 }
586         } else {
587                 res = monitor_dial(dial, chan);
588         }
589
590         return res;
591 }
592
593 /*! \brief Return channel that answered
594  * \note Returns the Asterisk channel that answered
595  * \param dial Dialing structure
596  */
597 struct ast_channel *ast_dial_answered(struct ast_dial *dial)
598 {
599         if (!dial)
600                 return NULL;
601
602         return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
603 }
604
605 /*! \brief Return state of dial
606  * \note Returns the state of the dial attempt
607  * \param dial Dialing structure
608  */
609 enum ast_dial_result ast_dial_state(struct ast_dial *dial)
610 {
611         return dial->state;
612 }
613
614 /*! \brief Cancel async thread
615  * \note Cancel a running async thread
616  * \param dial Dialing structure
617  */
618 enum ast_dial_result ast_dial_join(struct ast_dial *dial)
619 {
620         pthread_t thread;
621
622         /* If the dial structure is not running in async, return failed */
623         if (dial->thread == AST_PTHREADT_NULL)
624                 return AST_DIAL_RESULT_FAILED;
625
626         /* Record thread */
627         thread = dial->thread;
628
629         /* Stop the thread */
630         dial->thread = AST_PTHREADT_STOP;
631
632         /* Now we signal it with SIGURG so it will break out of it's waitfor */
633         pthread_kill(thread, SIGURG);
634
635         /* Finally wait for the thread to exit */
636         pthread_join(thread, NULL);
637
638         /* Yay thread is all gone */
639         dial->thread = AST_PTHREADT_NULL;
640
641         return dial->state;
642 }
643
644 /*! \brief Hangup channels
645  * \note Hangup all active channels
646  * \param dial Dialing structure
647  */
648 void ast_dial_hangup(struct ast_dial *dial)
649 {
650         struct ast_dial_channel *channel = NULL;
651
652         if (!dial)
653                 return;
654         
655         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
656                 if (channel->owner) {
657                         ast_hangup(channel->owner);
658                         channel->owner = NULL;
659                 }
660         }
661
662         return;
663 }
664
665 /*! \brief Destroys a dialing structure
666  * \note Destroys (free's) the given ast_dial structure
667  * \param dial Dialing structure to free
668  * \return Returns 0 on success, -1 on failure
669  */
670 int ast_dial_destroy(struct ast_dial *dial)
671 {
672         int i = 0;
673         struct ast_dial_channel *channel = NULL;
674
675         if (!dial)
676                 return -1;
677         
678         /* Hangup and deallocate all the dialed channels */
679         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
680                 /* Disable any enabled options */
681                 for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
682                         if (!channel->options[i])
683                                 continue;
684                         if (option_types[i].disable)
685                                 option_types[i].disable(channel->options[i]);
686                         channel->options[i] = NULL;
687                 }
688                 /* Hang up channel if need be */
689                 if (channel->owner) {
690                         ast_hangup(channel->owner);
691                         channel->owner = NULL;
692                 }
693                 /* Free structure */
694                 ast_free(channel);
695         }
696        
697         /* Disable any enabled options globally */
698         for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
699                 if (!dial->options[i])
700                         continue;
701                 if (option_types[i].disable)
702                         option_types[i].disable(dial->options[i]);
703                 dial->options[i] = NULL;
704         }
705
706         /* Free structure */
707         ast_free(dial);
708
709         return 0;
710 }
711
712 /*! \brief Enables an option globally
713  * \param dial Dial structure to enable option on
714  * \param option Option to enable
715  * \param data Data to pass to this option (not always needed)
716  * \return Returns 0 on success, -1 on failure
717  */
718 int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
719 {
720         /* If the option is already enabled, return failure */
721         if (dial->options[option])
722                 return -1;
723
724         /* Execute enable callback if it exists, if not simply make sure the value is set */
725         if (option_types[option].enable)
726                 dial->options[option] = option_types[option].enable(data);
727         else
728                 dial->options[option] = (void*)1;
729
730         return 0;
731 }
732
733 /*! \brief Enables an option per channel
734  * \param dial Dial structure
735  * \param num Channel number to enable option on
736  * \param option Option to enable
737  * \param data Data to pass to this option (not always needed)
738  * \return Returns 0 on success, -1 on failure
739  */
740 int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
741 {
742         struct ast_dial_channel *channel = NULL;
743
744         /* Ensure we have required arguments */
745         if (!dial || AST_LIST_EMPTY(&dial->channels))
746                 return -1;
747         
748         /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
749         if (AST_LIST_LAST(&dial->channels)->num != num) {
750                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
751                         if (channel->num == num)
752                                 break;
753                 }
754         } else {
755                 channel = AST_LIST_LAST(&dial->channels);
756         }
757
758         /* If none found, return failure */
759         if (!channel)
760                 return -1;
761
762         /* If the option is already enabled, return failure */
763         if (channel->options[option])
764                 return -1;
765
766         /* Execute enable callback if it exists, if not simply make sure the value is set */
767         if (option_types[option].enable)
768                 channel->options[option] = option_types[option].enable(data);
769         else
770                 channel->options[option] = (void*)1;
771
772         return 0;
773 }
774
775 /*! \brief Disables an option globally
776  * \param dial Dial structure to disable option on
777  * \param option Option to disable
778  * \return Returns 0 on success, -1 on failure
779  */
780 int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
781 {
782         /* If the option is not enabled, return failure */
783         if (!dial->options[option])
784                 return -1;
785
786         /* Execute callback of option to disable if it exists */
787         if (option_types[option].disable)
788                 option_types[option].disable(dial->options[option]);
789
790         /* Finally disable option on the structure */
791         dial->options[option] = NULL;
792
793         return 0;
794 }
795
796 /*! \brief Disables an option per channel
797  * \param dial Dial structure
798  * \param num Channel number to disable option on
799  * \param option Option to disable
800  * \return Returns 0 on success, -1 on failure
801  */
802 int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
803 {
804         struct ast_dial_channel *channel = NULL;
805
806         /* Ensure we have required arguments */
807         if (!dial || AST_LIST_EMPTY(&dial->channels))
808                 return -1;
809
810         /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
811         if (AST_LIST_LAST(&dial->channels)->num != num) {
812                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
813                         if (channel->num == num)
814                                 break;
815                 }
816         } else {
817                 channel = AST_LIST_LAST(&dial->channels);
818         }
819
820         /* If none found, return failure */
821         if (!channel)
822                 return -1;
823
824         /* If the option is not enabled, return failure */
825         if (!channel->options[option])
826                 return -1;
827
828         /* Execute callback of option to disable it if it exists */
829         if (option_types[option].disable)
830                 option_types[option].disable(channel->options[option]);
831
832         /* Finally disable the option on the structure */
833         channel->options[option] = NULL;
834
835         return 0;
836 }
837
838 void ast_dial_set_state_callback(struct ast_dial *dial, ast_dial_state_callback callback)
839 {
840         dial->state_callback = callback;
841 }