Sat Aug 6 00:39:28 2011

Asterisk developer's documentation


dial.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2007, Digium, Inc.
00005  *
00006  * Joshua Colp <jcolp@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Dialing API
00022  *
00023  * \author Joshua Colp <jcolp@digium.com>
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 108961 $")
00029 
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <sys/time.h>
00034 #include <signal.h>
00035 #include <errno.h>
00036 #include <unistd.h>
00037 
00038 #include "asterisk/logger.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/options.h"
00041 #include "asterisk/utils.h"
00042 #include "asterisk/lock.h"
00043 #include "asterisk/linkedlists.h"
00044 #include "asterisk/dial.h"
00045 #include "asterisk/pbx.h"
00046 
00047 /*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
00048 struct ast_dial {
00049    int num;                                           /*! Current number to give to next dialed channel */
00050    enum ast_dial_result state;                       /*! Status of dial */
00051    void *options[AST_DIAL_OPTION_MAX];                /*! Global options */
00052    ast_dial_state_callback state_callback;          /*! Status callback */
00053    AST_LIST_HEAD(, ast_dial_channel) channels; /*! Channels being dialed */
00054    pthread_t thread;                                  /*! Thread (if running in async) */
00055    ast_mutex_t lock;                                  /*! Lock to protect the thread information above */
00056 };
00057 
00058 /*! \brief Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! */
00059 struct ast_dial_channel {
00060    int num;                               /*! Unique number for dialed channel */
00061    const char *tech;                      /*! Technology being dialed */
00062    const char *device;                    /*! Device being dialed */
00063    void *options[AST_DIAL_OPTION_MAX];    /*! Channel specific options */
00064    int cause;                             /*! Cause code in case of failure */
00065    int is_running_app:1;                  /*! Is this running an application? */
00066    struct ast_channel *owner;             /*! Asterisk channel */
00067    AST_LIST_ENTRY(ast_dial_channel) list; /*! Linked list information */
00068 };
00069 
00070 /*! \brief Typedef for dial option enable */
00071 typedef void *(*ast_dial_option_cb_enable)(void *data);
00072 
00073 /*! \brief Typedef for dial option disable */
00074 typedef int (*ast_dial_option_cb_disable)(void *data);
00075 
00076 /* Structure for 'ANSWER_EXEC' option */
00077 struct answer_exec_struct {
00078    char app[AST_MAX_APP]; /* Application name */
00079    char *args;            /* Application arguments */
00080 };
00081 
00082 /* Enable function for 'ANSWER_EXEC' option */
00083 static void *answer_exec_enable(void *data)
00084 {
00085    struct answer_exec_struct *answer_exec = NULL;
00086    char *app = ast_strdupa((char*)data), *args = NULL;
00087 
00088    /* Not giving any data to this option is bad, mmmk? */
00089    if (ast_strlen_zero(app))
00090       return NULL;
00091 
00092    /* Create new data structure */
00093    if (!(answer_exec = ast_calloc(1, sizeof(*answer_exec))))
00094       return NULL;
00095    
00096    /* Parse out application and arguments */
00097    if ((args = strchr(app, '|'))) {
00098       *args++ = '\0';
00099       answer_exec->args = ast_strdup(args);
00100    }
00101 
00102    /* Copy application name */
00103    ast_copy_string(answer_exec->app, app, sizeof(answer_exec->app));
00104 
00105    return answer_exec;
00106 }
00107 
00108 /* Disable function for 'ANSWER_EXEC' option */
00109 static int answer_exec_disable(void *data)
00110 {
00111    struct answer_exec_struct *answer_exec = data;
00112 
00113    /* Make sure we have a value */
00114    if (!answer_exec)
00115       return -1;
00116 
00117    /* If arguments are present, free them too */
00118    if (answer_exec->args)
00119       free(answer_exec->args);
00120 
00121    /* This is simple - just free the structure */
00122    free(answer_exec);
00123 
00124    return 0;
00125 }
00126 
00127 /* Application execution function for 'ANSWER_EXEC' option */
00128 static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
00129 {
00130    struct ast_channel *chan = dial_channel->owner;
00131    struct ast_app *ast_app = pbx_findapp(app);
00132 
00133    /* If the application was not found, return immediately */
00134    if (!ast_app)
00135       return;
00136 
00137    /* All is well... execute the application */
00138    pbx_exec(chan, ast_app, args);
00139 
00140    /* If another thread is not taking over hang up the channel */
00141    ast_mutex_lock(&dial->lock);
00142    if (dial->thread != AST_PTHREADT_STOP) {
00143       ast_hangup(chan);
00144       dial_channel->owner = NULL;
00145    }
00146    ast_mutex_unlock(&dial->lock);
00147 
00148    return;
00149 }
00150 
00151 /*! \brief Options structure - maps options to respective handlers (enable/disable). This list MUST be perfectly kept in order, or else madness will happen. */
00152 static const struct ast_option_types {
00153    enum ast_dial_option option;
00154    ast_dial_option_cb_enable enable;
00155    ast_dial_option_cb_disable disable;
00156 } option_types[] = {
00157    { AST_DIAL_OPTION_RINGING, NULL, NULL },                                  /*! Always indicate ringing to caller */
00158    { AST_DIAL_OPTION_ANSWER_EXEC, answer_exec_enable, answer_exec_disable }, /*! Execute application upon answer in async mode */
00159    { AST_DIAL_OPTION_MAX, NULL, NULL },                                      /*! Terminator of list */
00160 };
00161 
00162 /* free the buffer if allocated, and set the pointer to the second arg */
00163 #define S_REPLACE(s, new_val)           \
00164         do {                            \
00165                 if (s)                  \
00166                         free(s);        \
00167                 s = (new_val);          \
00168         } while (0)
00169 
00170 /*! \brief Maximum number of channels we can watch at a time */
00171 #define AST_MAX_WATCHERS 256
00172 
00173 /*! \brief Macro for finding the option structure to use on a dialed channel */
00174 #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])
00175 
00176 /*! \brief Macro that determines whether a channel is the caller or not */
00177 #define IS_CALLER(chan, owner) (chan == owner ? 1 : 0)
00178 
00179 /*! \brief New dialing structure
00180  * \note Create a dialing structure
00181  * \return Returns a calloc'd ast_dial structure, NULL on failure
00182  */
00183 struct ast_dial *ast_dial_create(void)
00184 {
00185    struct ast_dial *dial = NULL;
00186 
00187    /* Allocate new memory for structure */
00188    if (!(dial = ast_calloc(1, sizeof(*dial))))
00189       return NULL;
00190 
00191    /* Initialize list of channels */
00192    AST_LIST_HEAD_INIT(&dial->channels);
00193 
00194    /* Initialize thread to NULL */
00195    dial->thread = AST_PTHREADT_NULL;
00196 
00197    /* Can't forget about the lock */
00198    ast_mutex_init(&dial->lock);
00199 
00200    return dial;
00201 }
00202 
00203 /*! \brief Append a channel
00204  * \note Appends a channel to a dialing structure
00205  * \return Returns channel reference number on success, -1 on failure
00206  */
00207 int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device)
00208 {
00209    struct ast_dial_channel *channel = NULL;
00210 
00211    /* Make sure we have required arguments */
00212    if (!dial || !tech || !device)
00213       return -1;
00214 
00215    /* Allocate new memory for dialed channel structure */
00216    if (!(channel = ast_calloc(1, sizeof(*channel))))
00217       return -1;
00218 
00219    /* Record technology and device for when we actually dial */
00220    channel->tech = tech;
00221    channel->device = device;
00222 
00223    /* Grab reference number from dial structure */
00224    channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
00225 
00226    /* Insert into channels list */
00227    AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
00228 
00229    return channel->num;
00230 }
00231 
00232 /*! \brief Helper function that does the beginning dialing */
00233 static int begin_dial(struct ast_dial *dial, struct ast_channel *chan)
00234 {
00235    struct ast_dial_channel *channel = NULL;
00236    int success = 0, res = 0;
00237 
00238    /* Iterate through channel list, requesting and calling each one */
00239    AST_LIST_LOCK(&dial->channels);
00240    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00241       char numsubst[AST_MAX_EXTENSION];
00242 
00243       /* Copy device string over */
00244       ast_copy_string(numsubst, channel->device, sizeof(numsubst));
00245 
00246       /* Request that the channel be created */
00247       if (!(channel->owner = ast_request(channel->tech, 
00248          chan ? chan->nativeformats : AST_FORMAT_AUDIO_MASK, numsubst, &channel->cause))) {
00249          continue;
00250       }
00251 
00252       channel->owner->appl = "AppDial2";
00253                 channel->owner->data = "(Outgoing Line)";
00254                 channel->owner->whentohangup = 0;
00255 
00256       /* Inherit everything from he who spawned this Dial */
00257       if (chan) {
00258          ast_channel_inherit_variables(chan, channel->owner);
00259 
00260          /* Copy over callerid information */
00261          S_REPLACE(channel->owner->cid.cid_num, ast_strdup(chan->cid.cid_num));
00262          S_REPLACE(channel->owner->cid.cid_name, ast_strdup(chan->cid.cid_name));
00263          S_REPLACE(channel->owner->cid.cid_ani, ast_strdup(chan->cid.cid_ani));
00264          S_REPLACE(channel->owner->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
00265    
00266          ast_string_field_set(channel->owner, language, chan->language);
00267          ast_string_field_set(channel->owner, accountcode, chan->accountcode);
00268          channel->owner->cdrflags = chan->cdrflags;
00269          if (ast_strlen_zero(channel->owner->musicclass))
00270             ast_string_field_set(channel->owner, musicclass, chan->musicclass);
00271    
00272          channel->owner->cid.cid_pres = chan->cid.cid_pres;
00273          channel->owner->cid.cid_ton = chan->cid.cid_ton;
00274          channel->owner->cid.cid_tns = chan->cid.cid_tns;
00275          channel->owner->adsicpe = chan->adsicpe;
00276          channel->owner->transfercapability = chan->transfercapability;
00277       }
00278 
00279       /* Actually call the device */
00280       if ((res = ast_call(channel->owner, numsubst, 0))) {
00281          ast_hangup(channel->owner);
00282          channel->owner = NULL;
00283       } else {
00284          success++;
00285          if (option_verbose > 2)
00286             ast_verbose(VERBOSE_PREFIX_3 "Called %s\n", numsubst);
00287       }
00288    }
00289    AST_LIST_UNLOCK(&dial->channels);
00290 
00291    /* If number of failures matches the number of channels, then this truly failed */
00292    return success;
00293 }
00294 
00295 /*! \brief Helper function that finds the dialed channel based on owner */
00296 static struct ast_dial_channel *find_relative_dial_channel(struct ast_dial *dial, struct ast_channel *owner)
00297 {
00298    struct ast_dial_channel *channel = NULL;
00299 
00300    AST_LIST_LOCK(&dial->channels);
00301    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00302       if (channel->owner == owner)
00303          break;
00304    }
00305    AST_LIST_UNLOCK(&dial->channels);
00306 
00307    return channel;
00308 }
00309 
00310 static void set_state(struct ast_dial *dial, enum ast_dial_result state)
00311 {
00312    dial->state = state;
00313 
00314    if (dial->state_callback)
00315       dial->state_callback(dial);
00316 }
00317 
00318 /*! \brief Helper function that handles control frames WITH owner */
00319 static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
00320 {
00321    if (fr->frametype == AST_FRAME_CONTROL) {
00322       switch (fr->subclass) {
00323       case AST_CONTROL_ANSWER:
00324          if (option_verbose > 2)
00325             ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", channel->owner->name, chan->name);
00326          AST_LIST_LOCK(&dial->channels);
00327          AST_LIST_REMOVE(&dial->channels, channel, list);
00328          AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
00329          AST_LIST_UNLOCK(&dial->channels);
00330          set_state(dial, AST_DIAL_RESULT_ANSWERED);
00331          break;
00332       case AST_CONTROL_BUSY:
00333          if (option_verbose > 2)
00334             ast_verbose(VERBOSE_PREFIX_3 "%s is busy\n", channel->owner->name);
00335          ast_hangup(channel->owner);
00336          channel->owner = NULL;
00337          break;
00338       case AST_CONTROL_CONGESTION:
00339          if (option_verbose > 2)
00340             ast_verbose(VERBOSE_PREFIX_3 "%s is circuit-busy\n", channel->owner->name);
00341          ast_hangup(channel->owner);
00342          channel->owner = NULL;
00343          break;
00344       case AST_CONTROL_RINGING:
00345          if (option_verbose > 2)
00346             ast_verbose(VERBOSE_PREFIX_3 "%s is ringing\n", channel->owner->name);
00347          ast_indicate(chan, AST_CONTROL_RINGING);
00348          set_state(dial, AST_DIAL_RESULT_RINGING);
00349          break;
00350       case AST_CONTROL_PROGRESS:
00351          if (option_verbose > 2)
00352             ast_verbose (VERBOSE_PREFIX_3 "%s is making progress, passing it to %s\n", channel->owner->name, chan->name);
00353          ast_indicate(chan, AST_CONTROL_PROGRESS);
00354          set_state(dial, AST_DIAL_RESULT_PROGRESS);
00355          break;
00356       case AST_CONTROL_VIDUPDATE:
00357          if (option_verbose > 2)
00358             ast_verbose (VERBOSE_PREFIX_3 "%s requested a video update, passing it to %s\n", channel->owner->name, chan->name);
00359          ast_indicate(chan, AST_CONTROL_VIDUPDATE);
00360          break;
00361       case AST_CONTROL_SRCUPDATE:
00362          if (option_verbose > 2)
00363             ast_verbose (VERBOSE_PREFIX_3 "%s requested a source update, passing it to %s\n", channel->owner->name, chan->name);
00364          ast_indicate(chan, AST_CONTROL_SRCUPDATE);
00365          break;
00366       case AST_CONTROL_PROCEEDING:
00367          if (option_verbose > 2)
00368             ast_verbose (VERBOSE_PREFIX_3 "%s is proceeding, passing it to %s\n", channel->owner->name, chan->name);
00369          ast_indicate(chan, AST_CONTROL_PROCEEDING);
00370          set_state(dial, AST_DIAL_RESULT_PROCEEDING);
00371          break;
00372       case AST_CONTROL_HOLD:
00373          if (option_verbose > 2)
00374             ast_verbose(VERBOSE_PREFIX_3 "Call on %s placed on hold\n", chan->name);
00375          ast_indicate(chan, AST_CONTROL_HOLD);
00376          break;
00377       case AST_CONTROL_UNHOLD:
00378          if (option_verbose > 2)
00379             ast_verbose(VERBOSE_PREFIX_3 "Call on %s left from hold\n", chan->name);
00380          ast_indicate(chan, AST_CONTROL_UNHOLD);
00381          break;
00382       case AST_CONTROL_OFFHOOK:
00383       case AST_CONTROL_FLASH:
00384          break;
00385       case -1:
00386          /* Prod the channel */
00387          ast_indicate(chan, -1);
00388          break;
00389       default:
00390          break;
00391       }
00392    }
00393 
00394    return;
00395 }
00396 
00397 /*! \brief Helper function that handles control frames WITHOUT owner */
00398 static void handle_frame_ownerless(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr)
00399 {
00400    /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
00401    if (fr->frametype != AST_FRAME_CONTROL)
00402       return;
00403 
00404    switch (fr->subclass) {
00405    case AST_CONTROL_ANSWER:
00406       if (option_verbose > 2)
00407          ast_verbose( VERBOSE_PREFIX_3 "%s answered\n", channel->owner->name);
00408       AST_LIST_LOCK(&dial->channels);
00409       AST_LIST_REMOVE(&dial->channels, channel, list);
00410       AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
00411       AST_LIST_UNLOCK(&dial->channels);
00412       set_state(dial, AST_DIAL_RESULT_ANSWERED);
00413       break;
00414    case AST_CONTROL_BUSY:
00415       if (option_verbose > 2)
00416          ast_verbose(VERBOSE_PREFIX_3 "%s is busy\n", channel->owner->name);
00417       ast_hangup(channel->owner);
00418       channel->owner = NULL;
00419       break;
00420    case AST_CONTROL_CONGESTION:
00421       if (option_verbose > 2)
00422          ast_verbose(VERBOSE_PREFIX_3 "%s is circuit-busy\n", channel->owner->name);
00423       ast_hangup(channel->owner);
00424       channel->owner = NULL;
00425       break;
00426    case AST_CONTROL_RINGING:
00427       if (option_verbose > 2)
00428          ast_verbose(VERBOSE_PREFIX_3 "%s is ringing\n", channel->owner->name);
00429       set_state(dial, AST_DIAL_RESULT_RINGING);
00430       break;
00431    case AST_CONTROL_PROGRESS:
00432       if (option_verbose > 2)
00433          ast_verbose (VERBOSE_PREFIX_3 "%s is making progress\n", channel->owner->name);
00434       set_state(dial, AST_DIAL_RESULT_PROGRESS);
00435       break;
00436    case AST_CONTROL_PROCEEDING:
00437       if (option_verbose > 2)
00438          ast_verbose (VERBOSE_PREFIX_3 "%s is proceeding\n", channel->owner->name);
00439       set_state(dial, AST_DIAL_RESULT_PROCEEDING);
00440       break;
00441    default:
00442       break;
00443    }
00444 
00445    return;
00446 }
00447 
00448 /*! \brief Helper function that basically keeps tabs on dialing attempts */
00449 static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
00450 {
00451    int timeout = -1, count = 0;
00452    struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
00453    struct ast_dial_channel *channel = NULL;
00454    struct answer_exec_struct *answer_exec = NULL;
00455 
00456    set_state(dial, AST_DIAL_RESULT_TRYING);
00457 
00458    /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
00459    if (dial->options[AST_DIAL_OPTION_RINGING]) {
00460       set_state(dial, AST_DIAL_RESULT_RINGING);
00461       if (chan)
00462          ast_indicate(chan, AST_CONTROL_RINGING);
00463    }
00464 
00465    /* Go into an infinite loop while we are trying */
00466    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)) {
00467       int pos = 0;
00468       struct ast_frame *fr = NULL;
00469 
00470       /* Set up channel structure array */
00471       pos = count = 0;
00472       if (chan)
00473          cs[pos++] = chan;
00474 
00475       /* Add channels we are attempting to dial */
00476       AST_LIST_LOCK(&dial->channels);
00477       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00478          if (channel->owner) {
00479             cs[pos++] = channel->owner;
00480             count++;
00481          }
00482       }
00483       AST_LIST_UNLOCK(&dial->channels);
00484 
00485       /* If we have no outbound channels in progress, switch state to unanswered and stop */
00486       if (!count) {
00487          set_state(dial, AST_DIAL_RESULT_UNANSWERED);
00488          break;
00489       }
00490 
00491       /* Just to be safe... */
00492       if (dial->thread == AST_PTHREADT_STOP)
00493          break;
00494 
00495       /* Wait for frames from channels */
00496       who = ast_waitfor_n(cs, pos, &timeout);
00497 
00498       /* Check to see if our thread is being cancelled */
00499       if (dial->thread == AST_PTHREADT_STOP)
00500          break;
00501 
00502       /* If we are not being cancelled and we have no channel, then timeout was tripped */
00503       if (!who)
00504          continue;
00505 
00506       /* Find relative dial channel */
00507       if (!chan || !IS_CALLER(chan, who))
00508          channel = find_relative_dial_channel(dial, who);
00509 
00510       /* Attempt to read in a frame */
00511       if (!(fr = ast_read(who))) {
00512          /* If this is the caller then we switch state to hangup and stop */
00513          if (chan && IS_CALLER(chan, who)) {
00514             set_state(dial, AST_DIAL_RESULT_HANGUP);
00515             break;
00516          }
00517          ast_hangup(who);
00518          channel->owner = NULL;
00519          continue;
00520       }
00521 
00522       /* Process the frame */
00523       if (chan)
00524          handle_frame(dial, channel, fr, chan);
00525       else
00526          handle_frame_ownerless(dial, channel, fr);
00527 
00528       /* Free the received frame and start all over */
00529       ast_frfree(fr);
00530    }
00531 
00532    /* Do post-processing from loop */
00533    if (dial->state == AST_DIAL_RESULT_ANSWERED) {
00534       /* Hangup everything except that which answered */
00535       AST_LIST_LOCK(&dial->channels);
00536       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00537          if (!channel->owner || channel->owner == who)
00538             continue;
00539          ast_hangup(channel->owner);
00540          channel->owner = NULL;
00541       }
00542       AST_LIST_UNLOCK(&dial->channels);
00543       /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
00544       if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC))) {
00545          channel->is_running_app = 1;
00546          answer_exec_run(dial, channel, answer_exec->app, answer_exec->args);
00547          channel->is_running_app = 0;
00548       }
00549    } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
00550       /* Hangup everything */
00551       AST_LIST_LOCK(&dial->channels);
00552       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00553          if (!channel->owner)
00554             continue;
00555          ast_hangup(channel->owner);
00556          channel->owner = NULL;
00557       }
00558       AST_LIST_UNLOCK(&dial->channels);
00559    }
00560 
00561    return dial->state;
00562 }
00563 
00564 /*! \brief Dial async thread function */
00565 static void *async_dial(void *data)
00566 {
00567    struct ast_dial *dial = data;
00568 
00569    /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
00570    monitor_dial(dial, NULL);
00571 
00572    return NULL;
00573 }
00574 
00575 /*! \brief Execute dialing synchronously or asynchronously
00576  * \note Dials channels in a dial structure.
00577  * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
00578  */
00579 enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
00580 {
00581    enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
00582 
00583    /* Ensure required arguments are passed */
00584    if (!dial || (!chan && !async)) {
00585       ast_log(LOG_DEBUG, "invalid #1\n");
00586       return AST_DIAL_RESULT_INVALID;
00587    }
00588 
00589    /* If there are no channels to dial we can't very well try to dial them */
00590    if (AST_LIST_EMPTY(&dial->channels)) {
00591       ast_log(LOG_DEBUG, "invalid #2\n");
00592       return AST_DIAL_RESULT_INVALID;
00593    }
00594 
00595    /* Dial each requested channel */
00596    if (!begin_dial(dial, chan))
00597       return AST_DIAL_RESULT_FAILED;
00598 
00599    /* If we are running async spawn a thread and send it away... otherwise block here */
00600    if (async) {
00601       dial->state = AST_DIAL_RESULT_TRYING;
00602       /* Try to create a thread */
00603       if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
00604          /* Failed to create the thread - hangup all dialed channels and return failed */
00605          ast_dial_hangup(dial);
00606          res = AST_DIAL_RESULT_FAILED;
00607       }
00608    } else {
00609       res = monitor_dial(dial, chan);
00610    }
00611 
00612    return res;
00613 }
00614 
00615 /*! \brief Return channel that answered
00616  * \note Returns the Asterisk channel that answered
00617  * \param dial Dialing structure
00618  */
00619 struct ast_channel *ast_dial_answered(struct ast_dial *dial)
00620 {
00621    if (!dial)
00622       return NULL;
00623 
00624    return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
00625 }
00626 
00627 /*! \brief Return state of dial
00628  * \note Returns the state of the dial attempt
00629  * \param dial Dialing structure
00630  */
00631 enum ast_dial_result ast_dial_state(struct ast_dial *dial)
00632 {
00633    return dial->state;
00634 }
00635 
00636 /*! \brief Cancel async thread
00637  * \note Cancel a running async thread
00638  * \param dial Dialing structure
00639  */
00640 enum ast_dial_result ast_dial_join(struct ast_dial *dial)
00641 {
00642    pthread_t thread;
00643 
00644    /* If the dial structure is not running in async, return failed */
00645    if (dial->thread == AST_PTHREADT_NULL)
00646       return AST_DIAL_RESULT_FAILED;
00647 
00648    /* Record thread */
00649    thread = dial->thread;
00650 
00651    /* Boom, commence locking */
00652    ast_mutex_lock(&dial->lock);
00653 
00654    /* Stop the thread */
00655    dial->thread = AST_PTHREADT_STOP;
00656 
00657    /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
00658    AST_LIST_LOCK(&dial->channels);
00659    if (AST_LIST_FIRST(&dial->channels)->is_running_app) {
00660       struct ast_channel *chan = AST_LIST_FIRST(&dial->channels)->owner;
00661       if (chan) {
00662          ast_channel_lock(chan);
00663          ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
00664          ast_channel_unlock(chan);
00665       }
00666    } else {
00667       /* Now we signal it with SIGURG so it will break out of it's waitfor */
00668       pthread_kill(thread, SIGURG);
00669    }
00670    AST_LIST_UNLOCK(&dial->channels);
00671 
00672    /* Yay done with it */
00673    ast_mutex_unlock(&dial->lock);
00674 
00675    /* Finally wait for the thread to exit */
00676    pthread_join(thread, NULL);
00677 
00678    /* Yay thread is all gone */
00679    dial->thread = AST_PTHREADT_NULL;
00680 
00681    return dial->state;
00682 }
00683 
00684 /*! \brief Hangup channels
00685  * \note Hangup all active channels
00686  * \param dial Dialing structure
00687  */
00688 void ast_dial_hangup(struct ast_dial *dial)
00689 {
00690    struct ast_dial_channel *channel = NULL;
00691 
00692    if (!dial)
00693       return;
00694    
00695    AST_LIST_LOCK(&dial->channels);
00696    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00697       if (channel->owner) {
00698          ast_hangup(channel->owner);
00699          channel->owner = NULL;
00700       }
00701    }
00702    AST_LIST_UNLOCK(&dial->channels);
00703 
00704    return;
00705 }
00706 
00707 /*! \brief Destroys a dialing structure
00708  * \note Destroys (free's) the given ast_dial structure
00709  * \param dial Dialing structure to free
00710  * \return Returns 0 on success, -1 on failure
00711  */
00712 int ast_dial_destroy(struct ast_dial *dial)
00713 {
00714    int i = 0;
00715    struct ast_dial_channel *channel = NULL;
00716 
00717    if (!dial)
00718       return -1;
00719    
00720    /* Hangup and deallocate all the dialed channels */
00721    AST_LIST_LOCK(&dial->channels);
00722    AST_LIST_TRAVERSE_SAFE_BEGIN(&dial->channels, channel, list) {
00723       /* Disable any enabled options */
00724       for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
00725          if (!channel->options[i])
00726             continue;
00727          if (option_types[i].disable)
00728             option_types[i].disable(channel->options[i]);
00729          channel->options[i] = NULL;
00730       }
00731       /* Hang up channel if need be */
00732       if (channel->owner) {
00733          ast_hangup(channel->owner);
00734          channel->owner = NULL;
00735       }
00736       /* Free structure */
00737       AST_LIST_REMOVE_CURRENT(&dial->channels, list);
00738       free(channel);
00739    }
00740    AST_LIST_TRAVERSE_SAFE_END;
00741    AST_LIST_UNLOCK(&dial->channels);
00742        
00743    /* Disable any enabled options globally */
00744    for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
00745       if (!dial->options[i])
00746          continue;
00747       if (option_types[i].disable)
00748          option_types[i].disable(dial->options[i]);
00749       dial->options[i] = NULL;
00750    }
00751 
00752    /* Lock be gone! */
00753    ast_mutex_destroy(&dial->lock);
00754 
00755    /* Free structure */
00756    free(dial);
00757 
00758    return 0;
00759 }
00760 
00761 /*! \brief Enables an option globally
00762  * \param dial Dial structure to enable option on
00763  * \param option Option to enable
00764  * \param data Data to pass to this option (not always needed)
00765  * \return Returns 0 on success, -1 on failure
00766  */
00767 int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
00768 {
00769    /* If the option is already enabled, return failure */
00770    if (dial->options[option])
00771       return -1;
00772 
00773    /* Execute enable callback if it exists, if not simply make sure the value is set */
00774    if (option_types[option].enable)
00775       dial->options[option] = option_types[option].enable(data);
00776    else
00777       dial->options[option] = (void*)1;
00778 
00779    return 0;
00780 }
00781 
00782 /*! \brief Enables an option per channel
00783  * \param dial Dial structure
00784  * \param num Channel number to enable option on
00785  * \param option Option to enable
00786  * \param data Data to pass to this option (not always needed)
00787  * \return Returns 0 on success, -1 on failure
00788  */
00789 int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
00790 {
00791    struct ast_dial_channel *channel = NULL;
00792 
00793    /* Ensure we have required arguments */
00794    if (!dial || AST_LIST_EMPTY(&dial->channels))
00795       return -1;
00796    
00797    /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
00798    AST_LIST_LOCK(&dial->channels);
00799    if (AST_LIST_LAST(&dial->channels)->num != num) {
00800       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00801          if (channel->num == num)
00802             break;
00803       }
00804    } else {
00805       channel = AST_LIST_LAST(&dial->channels);
00806    }
00807    AST_LIST_UNLOCK(&dial->channels);
00808 
00809    /* If none found, return failure */
00810    if (!channel)
00811       return -1;
00812 
00813    /* If the option is already enabled, return failure */
00814    if (channel->options[option])
00815       return -1;
00816 
00817         /* Execute enable callback if it exists, if not simply make sure the value is set */
00818    if (option_types[option].enable)
00819       channel->options[option] = option_types[option].enable(data);
00820    else
00821       channel->options[option] = (void*)1;
00822 
00823    return 0;
00824 }
00825 
00826 /*! \brief Disables an option globally
00827  * \param dial Dial structure to disable option on
00828  * \param option Option to disable
00829  * \return Returns 0 on success, -1 on failure
00830  */
00831 int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
00832 {
00833         /* If the option is not enabled, return failure */
00834         if (!dial->options[option])
00835                 return -1;
00836 
00837    /* Execute callback of option to disable if it exists */
00838    if (option_types[option].disable)
00839       option_types[option].disable(dial->options[option]);
00840 
00841    /* Finally disable option on the structure */
00842    dial->options[option] = NULL;
00843 
00844         return 0;
00845 }
00846 
00847 /*! \brief Disables an option per channel
00848  * \param dial Dial structure
00849  * \param num Channel number to disable option on
00850  * \param option Option to disable
00851  * \return Returns 0 on success, -1 on failure
00852  */
00853 int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
00854 {
00855    struct ast_dial_channel *channel = NULL;
00856 
00857    /* Ensure we have required arguments */
00858    if (!dial || AST_LIST_EMPTY(&dial->channels))
00859       return -1;
00860 
00861    /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
00862    AST_LIST_LOCK(&dial->channels);
00863    if (AST_LIST_LAST(&dial->channels)->num != num) {
00864       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00865          if (channel->num == num)
00866             break;
00867       }
00868    } else {
00869       channel = AST_LIST_LAST(&dial->channels);
00870    }
00871    AST_LIST_UNLOCK(&dial->channels);
00872 
00873    /* If none found, return failure */
00874    if (!channel)
00875       return -1;
00876 
00877    /* If the option is not enabled, return failure */
00878    if (!channel->options[option])
00879       return -1;
00880 
00881    /* Execute callback of option to disable it if it exists */
00882    if (option_types[option].disable)
00883       option_types[option].disable(channel->options[option]);
00884 
00885    /* Finally disable the option on the structure */
00886    channel->options[option] = NULL;
00887 
00888    return 0;
00889 }
00890 
00891 void ast_dial_set_state_callback(struct ast_dial *dial, ast_dial_state_callback callback)
00892 {
00893    dial->state_callback = callback;
00894 }

Generated on Sat Aug 6 00:39:28 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7