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