Wed Aug 15 01:24:20 2007

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

Generated on Wed Aug 15 01:24:20 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.3