Mon Jun 27 16:50:53 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: 276347 $")
00029 
00030 #include <sys/time.h>
00031 #include <signal.h>
00032 
00033 #include "asterisk/channel.h"
00034 #include "asterisk/utils.h"
00035 #include "asterisk/lock.h"
00036 #include "asterisk/linkedlists.h"
00037 #include "asterisk/dial.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/musiconhold.h"
00040 #include "asterisk/app.h"
00041 
00042 /*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
00043 struct ast_dial {
00044    int num;                                           /*!< Current number to give to next dialed channel */
00045    int timeout;                                       /*!< Maximum time allowed for dial attempts */
00046    int actual_timeout;                                /*!< Actual timeout based on all factors (ie: channels) */
00047    enum ast_dial_result state;                        /*!< Status of dial */
00048    void *options[AST_DIAL_OPTION_MAX];                /*!< Global options */
00049    ast_dial_state_callback state_callback;            /*!< Status callback */
00050    AST_LIST_HEAD(, ast_dial_channel) channels; /*!< Channels being dialed */
00051    pthread_t thread;                                  /*!< Thread (if running in async) */
00052    ast_mutex_t lock;                                  /*! Lock to protect the thread information above */
00053 };
00054 
00055 /*! \brief Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! */
00056 struct ast_dial_channel {
00057    int num;          /*!< Unique number for dialed channel */
00058    int timeout;            /*!< Maximum time allowed for attempt */
00059    char *tech;          /*!< Technology being dialed */
00060    char *device;           /*!< Device being dialed */
00061    void *options[AST_DIAL_OPTION_MAX]; /*!< Channel specific options */
00062    int cause;           /*!< Cause code in case of failure */
00063    unsigned int is_running_app:1;      /*!< Is this running an application? */
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 /*! \brief 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 /*! \brief 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 /*! \brief 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       ast_free(answer_exec->args);
00118 
00119    /* This is simple - just free the structure */
00120    ast_free(answer_exec);
00121 
00122    return 0;
00123 }
00124 
00125 static void *music_enable(void *data)
00126 {
00127    return ast_strdup(data);
00128 }
00129 
00130 static int music_disable(void *data)
00131 {
00132    if (!data)
00133       return -1;
00134 
00135    ast_free(data);
00136 
00137    return 0;
00138 }
00139 
00140 /*! \brief Application execution function for 'ANSWER_EXEC' option */
00141 static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
00142 {
00143    struct ast_channel *chan = dial_channel->owner;
00144    struct ast_app *ast_app = pbx_findapp(app);
00145 
00146    /* If the application was not found, return immediately */
00147    if (!ast_app)
00148       return;
00149 
00150    /* All is well... execute the application */
00151    pbx_exec(chan, ast_app, args);
00152 
00153    /* If another thread is not taking over hang up the channel */
00154    ast_mutex_lock(&dial->lock);
00155    if (dial->thread != AST_PTHREADT_STOP) {
00156       ast_hangup(chan);
00157       dial_channel->owner = NULL;
00158    }
00159    ast_mutex_unlock(&dial->lock);
00160 
00161    return;
00162 }
00163 
00164 /*! \brief Options structure - maps options to respective handlers (enable/disable). This list MUST be perfectly kept in order, or else madness will happen. */
00165 static const struct ast_option_types {
00166    enum ast_dial_option option;
00167    ast_dial_option_cb_enable enable;
00168    ast_dial_option_cb_disable disable;
00169 } option_types[] = {
00170    { AST_DIAL_OPTION_RINGING, NULL, NULL },                                  /*!< Always indicate ringing to caller */
00171    { AST_DIAL_OPTION_ANSWER_EXEC, answer_exec_enable, answer_exec_disable }, /*!< Execute application upon answer in async mode */
00172    { AST_DIAL_OPTION_MUSIC, music_enable, music_disable },                   /*!< Play music to the caller instead of ringing */
00173    { AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL, NULL },                  /*!< Disable call forwarding on channels */
00174    { AST_DIAL_OPTION_MAX, NULL, NULL },                                      /*!< Terminator of list */
00175 };
00176 
00177 /*! \brief Maximum number of channels we can watch at a time */
00178 #define AST_MAX_WATCHERS 256
00179 
00180 /*! \brief Macro for finding the option structure to use on a dialed channel */
00181 #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])
00182 
00183 /*! \brief Macro that determines whether a channel is the caller or not */
00184 #define IS_CALLER(chan, owner) (chan == owner ? 1 : 0)
00185 
00186 /*! \brief New dialing structure
00187  * \note Create a dialing structure
00188  * \return Returns a calloc'd ast_dial structure, NULL on failure
00189  */
00190 struct ast_dial *ast_dial_create(void)
00191 {
00192    struct ast_dial *dial = NULL;
00193 
00194    /* Allocate new memory for structure */
00195    if (!(dial = ast_calloc(1, sizeof(*dial))))
00196       return NULL;
00197 
00198    /* Initialize list of channels */
00199    AST_LIST_HEAD_INIT(&dial->channels);
00200 
00201    /* Initialize thread to NULL */
00202    dial->thread = AST_PTHREADT_NULL;
00203 
00204    /* No timeout exists... yet */
00205    dial->timeout = -1;
00206    dial->actual_timeout = -1;
00207 
00208    /* Can't forget about the lock */
00209    ast_mutex_init(&dial->lock);
00210 
00211    return dial;
00212 }
00213 
00214 /*! \brief Append a channel
00215  * \note Appends a channel to a dialing structure
00216  * \return Returns channel reference number on success, -1 on failure
00217  */
00218 int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device)
00219 {
00220    struct ast_dial_channel *channel = NULL;
00221 
00222    /* Make sure we have required arguments */
00223    if (!dial || !tech || !device)
00224       return -1;
00225 
00226    /* Allocate new memory for dialed channel structure */
00227    if (!(channel = ast_calloc(1, sizeof(*channel))))
00228       return -1;
00229 
00230    /* Record technology and device for when we actually dial */
00231    channel->tech = ast_strdup(tech);
00232    channel->device = ast_strdup(device);
00233 
00234    /* Grab reference number from dial structure */
00235    channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
00236 
00237    /* No timeout exists... yet */
00238    channel->timeout = -1;
00239 
00240    /* Insert into channels list */
00241    AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
00242 
00243    return channel->num;
00244 }
00245 
00246 /*! \brief Helper function that does the beginning dialing per-appended channel */
00247 static int begin_dial_channel(struct ast_dial_channel *channel, struct ast_channel *chan)
00248 {
00249    char numsubst[AST_MAX_EXTENSION];
00250    int res = 1;
00251 
00252    /* Copy device string over */
00253    ast_copy_string(numsubst, channel->device, sizeof(numsubst));
00254 
00255    /* If we fail to create our owner channel bail out */
00256    if (!(channel->owner = ast_request(channel->tech, chan ? chan->nativeformats : AST_FORMAT_AUDIO_MASK, chan, numsubst, &channel->cause)))
00257       return -1;
00258 
00259    channel->owner->appl = "AppDial2";
00260    channel->owner->data = "(Outgoing Line)";
00261    memset(&channel->owner->whentohangup, 0, sizeof(channel->owner->whentohangup));
00262 
00263    /* Inherit everything from he who spawned this dial */
00264    if (chan) {
00265       ast_channel_inherit_variables(chan, channel->owner);
00266       ast_channel_datastore_inherit(chan, channel->owner);
00267 
00268       /* Copy over callerid information */
00269       ast_party_redirecting_copy(&channel->owner->redirecting, &chan->redirecting);
00270 
00271       channel->owner->dialed.transit_network_select = chan->dialed.transit_network_select;
00272 
00273       ast_connected_line_copy_from_caller(&channel->owner->connected, &chan->caller);
00274 
00275       ast_string_field_set(channel->owner, language, chan->language);
00276       ast_string_field_set(channel->owner, accountcode, chan->accountcode);
00277       if (ast_strlen_zero(channel->owner->musicclass))
00278          ast_string_field_set(channel->owner, musicclass, chan->musicclass);
00279 
00280       channel->owner->adsicpe = chan->adsicpe;
00281       channel->owner->transfercapability = chan->transfercapability;
00282    }
00283 
00284    /* Attempt to actually call this device */
00285    if ((res = ast_call(channel->owner, numsubst, 0))) {
00286       res = 0;
00287       ast_hangup(channel->owner);
00288       channel->owner = NULL;
00289    } else {
00290       if (chan)
00291          ast_poll_channel_add(chan, channel->owner);
00292       res = 1;
00293       ast_verb(3, "Called %s\n", numsubst);
00294    }
00295 
00296    return res;
00297 }
00298 
00299 /*! \brief Helper function that does the beginning dialing per dial structure */
00300 static int begin_dial(struct ast_dial *dial, struct ast_channel *chan)
00301 {
00302    struct ast_dial_channel *channel = NULL;
00303    int success = 0;
00304 
00305    /* Iterate through channel list, requesting and calling each one */
00306    AST_LIST_LOCK(&dial->channels);
00307    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00308       success += begin_dial_channel(channel, chan);
00309    }
00310    AST_LIST_UNLOCK(&dial->channels);
00311 
00312    /* If number of failures matches the number of channels, then this truly failed */
00313    return success;
00314 }
00315 
00316 /*! \brief Helper function to handle channels that have been call forwarded */
00317 static int handle_call_forward(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_channel *chan)
00318 {
00319    struct ast_channel *original = channel->owner;
00320    char *tmp = ast_strdupa(channel->owner->call_forward);
00321    char *tech = "Local", *device = tmp, *stuff;
00322 
00323    /* If call forwarding is disabled just drop the original channel and don't attempt to dial the new one */
00324    if (FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING)) {
00325       ast_hangup(original);
00326       channel->owner = NULL;
00327       return 0;
00328    }
00329 
00330    /* Figure out the new destination */
00331    if ((stuff = strchr(tmp, '/'))) {
00332       *stuff++ = '\0';
00333       tech = tmp;
00334       device = stuff;
00335    }
00336 
00337    /* Drop old destination information */
00338    ast_free(channel->tech);
00339    ast_free(channel->device);
00340 
00341    /* Update the dial channel with the new destination information */
00342    channel->tech = ast_strdup(tech);
00343    channel->device = ast_strdup(device);
00344    AST_LIST_UNLOCK(&dial->channels);
00345 
00346    /* Finally give it a go... send it out into the world */
00347    begin_dial_channel(channel, chan);
00348 
00349    /* Drop the original channel */
00350    ast_hangup(original);
00351 
00352    return 0;
00353 }
00354 
00355 /*! \brief Helper function that finds the dialed channel based on owner */
00356 static struct ast_dial_channel *find_relative_dial_channel(struct ast_dial *dial, struct ast_channel *owner)
00357 {
00358    struct ast_dial_channel *channel = NULL;
00359 
00360    AST_LIST_LOCK(&dial->channels);
00361    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00362       if (channel->owner == owner)
00363          break;
00364    }
00365    AST_LIST_UNLOCK(&dial->channels);
00366 
00367    return channel;
00368 }
00369 
00370 static void set_state(struct ast_dial *dial, enum ast_dial_result state)
00371 {
00372    dial->state = state;
00373 
00374    if (dial->state_callback)
00375       dial->state_callback(dial);
00376 }
00377 
00378 /*! \brief Helper function that handles control frames WITH owner */
00379 static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
00380 {
00381    if (fr->frametype == AST_FRAME_CONTROL) {
00382       switch (fr->subclass.integer) {
00383       case AST_CONTROL_ANSWER:
00384          ast_verb(3, "%s answered %s\n", channel->owner->name, chan->name);
00385          AST_LIST_LOCK(&dial->channels);
00386          AST_LIST_REMOVE(&dial->channels, channel, list);
00387          AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
00388          AST_LIST_UNLOCK(&dial->channels);
00389          set_state(dial, AST_DIAL_RESULT_ANSWERED);
00390          break;
00391       case AST_CONTROL_BUSY:
00392          ast_verb(3, "%s is busy\n", channel->owner->name);
00393          ast_hangup(channel->owner);
00394          channel->owner = NULL;
00395          break;
00396       case AST_CONTROL_CONGESTION:
00397          ast_verb(3, "%s is circuit-busy\n", channel->owner->name);
00398          ast_hangup(channel->owner);
00399          channel->owner = NULL;
00400          break;
00401       case AST_CONTROL_RINGING:
00402          ast_verb(3, "%s is ringing\n", channel->owner->name);
00403          if (!dial->options[AST_DIAL_OPTION_MUSIC])
00404             ast_indicate(chan, AST_CONTROL_RINGING);
00405          set_state(dial, AST_DIAL_RESULT_RINGING);
00406          break;
00407       case AST_CONTROL_PROGRESS:
00408          ast_verb(3, "%s is making progress, passing it to %s\n", channel->owner->name, chan->name);
00409          ast_indicate(chan, AST_CONTROL_PROGRESS);
00410          set_state(dial, AST_DIAL_RESULT_PROGRESS);
00411          break;
00412       case AST_CONTROL_VIDUPDATE:
00413          ast_verb(3, "%s requested a video update, passing it to %s\n", channel->owner->name, chan->name);
00414          ast_indicate(chan, AST_CONTROL_VIDUPDATE);
00415          break;
00416       case AST_CONTROL_SRCUPDATE:
00417          if (option_verbose > 2)
00418             ast_verbose (VERBOSE_PREFIX_3 "%s requested a source update, passing it to %s\n", channel->owner->name, chan->name);
00419          ast_indicate(chan, AST_CONTROL_SRCUPDATE);
00420          break;
00421       case AST_CONTROL_CONNECTED_LINE:
00422          ast_verb(3, "%s connected line has changed, passing it to %s\n", channel->owner->name, chan->name);
00423          if (ast_channel_connected_line_macro(channel->owner, chan, fr, 1, 1)) {
00424             ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, fr->data.ptr, fr->datalen);
00425          }
00426          break;
00427       case AST_CONTROL_REDIRECTING:
00428          ast_verb(3, "%s redirecting info has changed, passing it to %s\n", channel->owner->name, chan->name);
00429          if (ast_channel_redirecting_macro(channel->owner, chan, fr, 1, 1)) {
00430             ast_indicate_data(chan, AST_CONTROL_REDIRECTING, fr->data.ptr, fr->datalen);
00431          }
00432          break;
00433       case AST_CONTROL_PROCEEDING:
00434          ast_verb(3, "%s is proceeding, passing it to %s\n", channel->owner->name, chan->name);
00435          ast_indicate(chan, AST_CONTROL_PROCEEDING);
00436          set_state(dial, AST_DIAL_RESULT_PROCEEDING);
00437          break;
00438       case AST_CONTROL_HOLD:
00439          ast_verb(3, "Call on %s placed on hold\n", chan->name);
00440          ast_indicate(chan, AST_CONTROL_HOLD);
00441          break;
00442       case AST_CONTROL_UNHOLD:
00443          ast_verb(3, "Call on %s left from hold\n", chan->name);
00444          ast_indicate(chan, AST_CONTROL_UNHOLD);
00445          break;
00446       case AST_CONTROL_OFFHOOK:
00447       case AST_CONTROL_FLASH:
00448          break;
00449       case -1:
00450          /* Prod the channel */
00451          ast_indicate(chan, -1);
00452          break;
00453       default:
00454          break;
00455       }
00456    }
00457 
00458    return;
00459 }
00460 
00461 /*! \brief Helper function that handles control frames WITHOUT owner */
00462 static void handle_frame_ownerless(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr)
00463 {
00464    /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
00465    if (fr->frametype != AST_FRAME_CONTROL)
00466       return;
00467 
00468    switch (fr->subclass.integer) {
00469    case AST_CONTROL_ANSWER:
00470       ast_verb(3, "%s answered\n", channel->owner->name);
00471       AST_LIST_LOCK(&dial->channels);
00472       AST_LIST_REMOVE(&dial->channels, channel, list);
00473       AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
00474       AST_LIST_UNLOCK(&dial->channels);
00475       set_state(dial, AST_DIAL_RESULT_ANSWERED);
00476       break;
00477    case AST_CONTROL_BUSY:
00478       ast_verb(3, "%s is busy\n", channel->owner->name);
00479       ast_hangup(channel->owner);
00480       channel->owner = NULL;
00481       break;
00482    case AST_CONTROL_CONGESTION:
00483       ast_verb(3, "%s is circuit-busy\n", channel->owner->name);
00484       ast_hangup(channel->owner);
00485       channel->owner = NULL;
00486       break;
00487    case AST_CONTROL_RINGING:
00488       ast_verb(3, "%s is ringing\n", channel->owner->name);
00489       set_state(dial, AST_DIAL_RESULT_RINGING);
00490       break;
00491    case AST_CONTROL_PROGRESS:
00492       ast_verb(3, "%s is making progress\n", channel->owner->name);
00493       set_state(dial, AST_DIAL_RESULT_PROGRESS);
00494       break;
00495    case AST_CONTROL_PROCEEDING:
00496       ast_verb(3, "%s is proceeding\n", channel->owner->name);
00497       set_state(dial, AST_DIAL_RESULT_PROCEEDING);
00498       break;
00499    default:
00500       break;
00501    }
00502 
00503    return;
00504 }
00505 
00506 /*! \brief Helper function to handle when a timeout occurs on dialing attempt */
00507 static int handle_timeout_trip(struct ast_dial *dial, struct timeval start)
00508 {
00509    struct ast_dial_channel *channel = NULL;
00510    int diff = ast_tvdiff_ms(ast_tvnow(), start), lowest_timeout = -1, new_timeout = -1;
00511 
00512    /* If the global dial timeout tripped switch the state to timeout so our channel loop will drop every channel */
00513    if (diff >= dial->timeout) {
00514       set_state(dial, AST_DIAL_RESULT_TIMEOUT);
00515       new_timeout = 0;
00516    }
00517 
00518    /* Go through dropping out channels that have met their timeout */
00519    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00520       if (dial->state == AST_DIAL_RESULT_TIMEOUT || diff >= channel->timeout) {
00521          ast_hangup(channel->owner);
00522          channel->owner = NULL;
00523       } else if ((lowest_timeout == -1) || (lowest_timeout > channel->timeout)) {
00524          lowest_timeout = channel->timeout;
00525       }
00526    }
00527 
00528    /* Calculate the new timeout using the lowest timeout found */
00529    if (lowest_timeout >= 0)
00530       new_timeout = lowest_timeout - diff;
00531 
00532    return new_timeout;
00533 }
00534 
00535 /*! \brief Helper function that basically keeps tabs on dialing attempts */
00536 static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
00537 {
00538    int timeout = -1;
00539    struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
00540    struct ast_dial_channel *channel = NULL;
00541    struct answer_exec_struct *answer_exec = NULL;
00542    struct timeval start;
00543 
00544    set_state(dial, AST_DIAL_RESULT_TRYING);
00545 
00546    /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
00547    if (dial->options[AST_DIAL_OPTION_RINGING]) {
00548       set_state(dial, AST_DIAL_RESULT_RINGING);
00549       if (chan)
00550          ast_indicate(chan, AST_CONTROL_RINGING);
00551    } else if (chan && dial->options[AST_DIAL_OPTION_MUSIC] && 
00552       !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
00553       char *original_moh = ast_strdupa(chan->musicclass);
00554       ast_indicate(chan, -1);
00555       ast_string_field_set(chan, musicclass, dial->options[AST_DIAL_OPTION_MUSIC]);
00556       ast_moh_start(chan, dial->options[AST_DIAL_OPTION_MUSIC], NULL);
00557       ast_string_field_set(chan, musicclass, original_moh);
00558    }
00559 
00560    /* Record start time for timeout purposes */
00561    start = ast_tvnow();
00562 
00563    /* We actually figured out the maximum timeout we can do as they were added, so we can directly access the info */
00564    timeout = dial->actual_timeout;
00565 
00566    /* Go into an infinite loop while we are trying */
00567    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)) {
00568       int pos = 0, count = 0;
00569       struct ast_frame *fr = NULL;
00570 
00571       /* Set up channel structure array */
00572       pos = count = 0;
00573       if (chan)
00574          cs[pos++] = chan;
00575 
00576       /* Add channels we are attempting to dial */
00577       AST_LIST_LOCK(&dial->channels);
00578       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00579          if (channel->owner) {
00580             cs[pos++] = channel->owner;
00581             count++;
00582          }
00583       }
00584       AST_LIST_UNLOCK(&dial->channels);
00585 
00586       /* If we have no outbound channels in progress, switch state to unanswered and stop */
00587       if (!count) {
00588          set_state(dial, AST_DIAL_RESULT_UNANSWERED);
00589          break;
00590       }
00591 
00592       /* Just to be safe... */
00593       if (dial->thread == AST_PTHREADT_STOP)
00594          break;
00595 
00596       /* Wait for frames from channels */
00597       who = ast_waitfor_n(cs, pos, &timeout);
00598 
00599       /* Check to see if our thread is being cancelled */
00600       if (dial->thread == AST_PTHREADT_STOP)
00601          break;
00602 
00603       /* If the timeout no longer exists OR if we got no channel it basically means the timeout was tripped, so handle it */
00604       if (!timeout || !who) {
00605          timeout = handle_timeout_trip(dial, start);
00606          continue;
00607       }
00608 
00609       /* Find relative dial channel */
00610       if (!chan || !IS_CALLER(chan, who))
00611          channel = find_relative_dial_channel(dial, who);
00612 
00613       /* See if this channel has been forwarded elsewhere */
00614       if (!ast_strlen_zero(who->call_forward)) {
00615          handle_call_forward(dial, channel, chan);
00616          continue;
00617       }
00618 
00619       /* Attempt to read in a frame */
00620       if (!(fr = ast_read(who))) {
00621          /* If this is the caller then we switch state to hangup and stop */
00622          if (chan && IS_CALLER(chan, who)) {
00623             set_state(dial, AST_DIAL_RESULT_HANGUP);
00624             break;
00625          }
00626          if (chan)
00627             ast_poll_channel_del(chan, channel->owner);
00628          ast_hangup(who);
00629          channel->owner = NULL;
00630          continue;
00631       }
00632 
00633       /* Process the frame */
00634       if (chan)
00635          handle_frame(dial, channel, fr, chan);
00636       else
00637          handle_frame_ownerless(dial, channel, fr);
00638 
00639       /* Free the received frame and start all over */
00640       ast_frfree(fr);
00641    }
00642 
00643    /* Do post-processing from loop */
00644    if (dial->state == AST_DIAL_RESULT_ANSWERED) {
00645       /* Hangup everything except that which answered */
00646       AST_LIST_LOCK(&dial->channels);
00647       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00648          if (!channel->owner || channel->owner == who)
00649             continue;
00650          if (chan)
00651             ast_poll_channel_del(chan, channel->owner);
00652          ast_hangup(channel->owner);
00653          channel->owner = NULL;
00654       }
00655       AST_LIST_UNLOCK(&dial->channels);
00656       /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
00657       if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC))) {
00658          channel->is_running_app = 1;
00659          answer_exec_run(dial, channel, answer_exec->app, answer_exec->args);
00660          channel->is_running_app = 0;
00661       }
00662 
00663       if (chan && dial->options[AST_DIAL_OPTION_MUSIC] && 
00664          !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
00665          ast_moh_stop(chan);
00666       }
00667    } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
00668       /* Hangup everything */
00669       AST_LIST_LOCK(&dial->channels);
00670       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00671          if (!channel->owner)
00672             continue;
00673          if (chan)
00674             ast_poll_channel_del(chan, channel->owner);
00675          ast_hangup(channel->owner);
00676          channel->owner = NULL;
00677       }
00678       AST_LIST_UNLOCK(&dial->channels);
00679    }
00680 
00681    return dial->state;
00682 }
00683 
00684 /*! \brief Dial async thread function */
00685 static void *async_dial(void *data)
00686 {
00687    struct ast_dial *dial = data;
00688 
00689    /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
00690    monitor_dial(dial, NULL);
00691 
00692    return NULL;
00693 }
00694 
00695 /*! \brief Execute dialing synchronously or asynchronously
00696  * \note Dials channels in a dial structure.
00697  * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
00698  */
00699 enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
00700 {
00701    enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
00702 
00703    /* Ensure required arguments are passed */
00704    if (!dial || (!chan && !async)) {
00705       ast_debug(1, "invalid #1\n");
00706       return AST_DIAL_RESULT_INVALID;
00707    }
00708 
00709    /* If there are no channels to dial we can't very well try to dial them */
00710    if (AST_LIST_EMPTY(&dial->channels)) {
00711       ast_debug(1, "invalid #2\n");
00712       return AST_DIAL_RESULT_INVALID;
00713    }
00714 
00715    /* Dial each requested channel */
00716    if (!begin_dial(dial, chan))
00717       return AST_DIAL_RESULT_FAILED;
00718 
00719    /* If we are running async spawn a thread and send it away... otherwise block here */
00720    if (async) {
00721       dial->state = AST_DIAL_RESULT_TRYING;
00722       /* Try to create a thread */
00723       if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
00724          /* Failed to create the thread - hangup all dialed channels and return failed */
00725          ast_dial_hangup(dial);
00726          res = AST_DIAL_RESULT_FAILED;
00727       }
00728    } else {
00729       res = monitor_dial(dial, chan);
00730    }
00731 
00732    return res;
00733 }
00734 
00735 /*! \brief Return channel that answered
00736  * \note Returns the Asterisk channel that answered
00737  * \param dial Dialing structure
00738  */
00739 struct ast_channel *ast_dial_answered(struct ast_dial *dial)
00740 {
00741    if (!dial)
00742       return NULL;
00743 
00744    return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
00745 }
00746 
00747 /*! \brief Steal the channel that answered
00748  * \note Returns the Asterisk channel that answered and removes it from the dialing structure
00749  * \param dial Dialing structure
00750  */
00751 struct ast_channel *ast_dial_answered_steal(struct ast_dial *dial)
00752 {
00753    struct ast_channel *chan = NULL;
00754 
00755    if (!dial)
00756       return NULL;
00757 
00758    if (dial->state == AST_DIAL_RESULT_ANSWERED) {
00759       chan = AST_LIST_FIRST(&dial->channels)->owner;
00760       AST_LIST_FIRST(&dial->channels)->owner = NULL;
00761    }
00762 
00763    return chan;
00764 }
00765 
00766 /*! \brief Return state of dial
00767  * \note Returns the state of the dial attempt
00768  * \param dial Dialing structure
00769  */
00770 enum ast_dial_result ast_dial_state(struct ast_dial *dial)
00771 {
00772    return dial->state;
00773 }
00774 
00775 /*! \brief Cancel async thread
00776  * \note Cancel a running async thread
00777  * \param dial Dialing structure
00778  */
00779 enum ast_dial_result ast_dial_join(struct ast_dial *dial)
00780 {
00781    pthread_t thread;
00782 
00783    /* If the dial structure is not running in async, return failed */
00784    if (dial->thread == AST_PTHREADT_NULL)
00785       return AST_DIAL_RESULT_FAILED;
00786 
00787    /* Record thread */
00788    thread = dial->thread;
00789 
00790    /* Boom, commence locking */
00791    ast_mutex_lock(&dial->lock);
00792 
00793    /* Stop the thread */
00794    dial->thread = AST_PTHREADT_STOP;
00795 
00796    /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
00797    AST_LIST_LOCK(&dial->channels);
00798    if (AST_LIST_FIRST(&dial->channels)->is_running_app) {
00799       struct ast_channel *chan = AST_LIST_FIRST(&dial->channels)->owner;
00800       if (chan) {
00801          ast_channel_lock(chan);
00802          ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
00803          ast_channel_unlock(chan);
00804       }
00805    } else {
00806       /* Now we signal it with SIGURG so it will break out of it's waitfor */
00807       pthread_kill(thread, SIGURG);
00808    }
00809    AST_LIST_UNLOCK(&dial->channels);
00810 
00811    /* Yay done with it */
00812    ast_mutex_unlock(&dial->lock);
00813 
00814    /* Finally wait for the thread to exit */
00815    pthread_join(thread, NULL);
00816 
00817    /* Yay thread is all gone */
00818    dial->thread = AST_PTHREADT_NULL;
00819 
00820    return dial->state;
00821 }
00822 
00823 /*! \brief Hangup channels
00824  * \note Hangup all active channels
00825  * \param dial Dialing structure
00826  */
00827 void ast_dial_hangup(struct ast_dial *dial)
00828 {
00829    struct ast_dial_channel *channel = NULL;
00830 
00831    if (!dial)
00832       return;
00833    
00834    AST_LIST_LOCK(&dial->channels);
00835    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00836       if (channel->owner) {
00837          ast_hangup(channel->owner);
00838          channel->owner = NULL;
00839       }
00840    }
00841    AST_LIST_UNLOCK(&dial->channels);
00842 
00843    return;
00844 }
00845 
00846 /*! \brief Destroys a dialing structure
00847  * \note Destroys (free's) the given ast_dial structure
00848  * \param dial Dialing structure to free
00849  * \return Returns 0 on success, -1 on failure
00850  */
00851 int ast_dial_destroy(struct ast_dial *dial)
00852 {
00853    int i = 0;
00854    struct ast_dial_channel *channel = NULL;
00855 
00856    if (!dial)
00857       return -1;
00858    
00859    /* Hangup and deallocate all the dialed channels */
00860    AST_LIST_LOCK(&dial->channels);
00861    AST_LIST_TRAVERSE_SAFE_BEGIN(&dial->channels, channel, list) {
00862       /* Disable any enabled options */
00863       for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
00864          if (!channel->options[i])
00865             continue;
00866          if (option_types[i].disable)
00867             option_types[i].disable(channel->options[i]);
00868          channel->options[i] = NULL;
00869       }
00870       /* Hang up channel if need be */
00871       if (channel->owner) {
00872          ast_hangup(channel->owner);
00873          channel->owner = NULL;
00874       }
00875       /* Free structure */
00876       ast_free(channel->tech);
00877       ast_free(channel->device);
00878       AST_LIST_REMOVE_CURRENT(list);
00879       ast_free(channel);
00880    }
00881    AST_LIST_TRAVERSE_SAFE_END;
00882    AST_LIST_UNLOCK(&dial->channels);
00883  
00884    /* Disable any enabled options globally */
00885    for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
00886       if (!dial->options[i])
00887          continue;
00888       if (option_types[i].disable)
00889          option_types[i].disable(dial->options[i]);
00890       dial->options[i] = NULL;
00891    }
00892 
00893    /* Lock be gone! */
00894    ast_mutex_destroy(&dial->lock);
00895 
00896    /* Free structure */
00897    ast_free(dial);
00898 
00899    return 0;
00900 }
00901 
00902 /*! \brief Enables an option globally
00903  * \param dial Dial structure to enable option on
00904  * \param option Option to enable
00905  * \param data Data to pass to this option (not always needed)
00906  * \return Returns 0 on success, -1 on failure
00907  */
00908 int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
00909 {
00910    /* If the option is already enabled, return failure */
00911    if (dial->options[option])
00912       return -1;
00913 
00914    /* Execute enable callback if it exists, if not simply make sure the value is set */
00915    if (option_types[option].enable)
00916       dial->options[option] = option_types[option].enable(data);
00917    else
00918       dial->options[option] = (void*)1;
00919 
00920    return 0;
00921 }
00922 
00923 /*! \brief Helper function for finding a channel in a dial structure based on number
00924  */
00925 static struct ast_dial_channel *find_dial_channel(struct ast_dial *dial, int num)
00926 {
00927    struct ast_dial_channel *channel = AST_LIST_LAST(&dial->channels);
00928 
00929    /* We can try to predict programmer behavior, the last channel they added is probably the one they wanted to modify */
00930    if (channel->num == num)
00931       return channel;
00932 
00933    /* Hrm not at the end... looking through the list it is! */
00934    AST_LIST_LOCK(&dial->channels);
00935    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00936       if (channel->num == num)
00937          break;
00938    }
00939    AST_LIST_UNLOCK(&dial->channels);
00940    
00941    return channel;
00942 }
00943 
00944 /*! \brief Enables an option per channel
00945  * \param dial Dial structure
00946  * \param num Channel number to enable option on
00947  * \param option Option to enable
00948  * \param data Data to pass to this option (not always needed)
00949  * \return Returns 0 on success, -1 on failure
00950  */
00951 int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
00952 {
00953    struct ast_dial_channel *channel = NULL;
00954 
00955    /* Ensure we have required arguments */
00956    if (!dial || AST_LIST_EMPTY(&dial->channels))
00957       return -1;
00958 
00959    if (!(channel = find_dial_channel(dial, num)))
00960       return -1;
00961 
00962    /* If the option is already enabled, return failure */
00963    if (channel->options[option])
00964       return -1;
00965 
00966    /* Execute enable callback if it exists, if not simply make sure the value is set */
00967    if (option_types[option].enable)
00968       channel->options[option] = option_types[option].enable(data);
00969    else
00970       channel->options[option] = (void*)1;
00971 
00972    return 0;
00973 }
00974 
00975 /*! \brief Disables an option globally
00976  * \param dial Dial structure to disable option on
00977  * \param option Option to disable
00978  * \return Returns 0 on success, -1 on failure
00979  */
00980 int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
00981 {
00982    /* If the option is not enabled, return failure */
00983    if (!dial->options[option]) {
00984       return -1;
00985    }
00986 
00987    /* Execute callback of option to disable if it exists */
00988    if (option_types[option].disable)
00989       option_types[option].disable(dial->options[option]);
00990 
00991    /* Finally disable option on the structure */
00992    dial->options[option] = NULL;
00993 
00994    return 0;
00995 }
00996 
00997 /*! \brief Disables an option per channel
00998  * \param dial Dial structure
00999  * \param num Channel number to disable option on
01000  * \param option Option to disable
01001  * \return Returns 0 on success, -1 on failure
01002  */
01003 int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
01004 {
01005    struct ast_dial_channel *channel = NULL;
01006 
01007    /* Ensure we have required arguments */
01008    if (!dial || AST_LIST_EMPTY(&dial->channels))
01009       return -1;
01010 
01011    if (!(channel = find_dial_channel(dial, num)))
01012       return -1;
01013 
01014    /* If the option is not enabled, return failure */
01015    if (!channel->options[option])
01016       return -1;
01017 
01018    /* Execute callback of option to disable it if it exists */
01019    if (option_types[option].disable)
01020       option_types[option].disable(channel->options[option]);
01021 
01022    /* Finally disable the option on the structure */
01023    channel->options[option] = NULL;
01024 
01025    return 0;
01026 }
01027 
01028 void ast_dial_set_state_callback(struct ast_dial *dial, ast_dial_state_callback callback)
01029 {
01030    dial->state_callback = callback;
01031 }
01032 
01033 /*! \brief Set the maximum time (globally) allowed for trying to ring phones
01034  * \param dial The dial structure to apply the time limit to
01035  * \param timeout Maximum time allowed
01036  * \return nothing
01037  */
01038 void ast_dial_set_global_timeout(struct ast_dial *dial, int timeout)
01039 {
01040    dial->timeout = timeout;
01041 
01042    if (dial->timeout > 0 && (dial->actual_timeout > dial->timeout || dial->actual_timeout == -1))
01043       dial->actual_timeout = dial->timeout;
01044 
01045    return;
01046 }
01047 
01048 /*! \brief Set the maximum time (per channel) allowed for trying to ring the phone
01049  * \param dial The dial structure the channel belongs to
01050  * \param num Channel number to set timeout on
01051  * \param timeout Maximum time allowed
01052  * \return nothing
01053  */
01054 void ast_dial_set_timeout(struct ast_dial *dial, int num, int timeout)
01055 {
01056    struct ast_dial_channel *channel = NULL;
01057 
01058    if (!(channel = find_dial_channel(dial, num)))
01059       return;
01060 
01061    channel->timeout = timeout;
01062 
01063    if (channel->timeout > 0 && (dial->actual_timeout > channel->timeout || dial->actual_timeout == -1))
01064       dial->actual_timeout = channel->timeout;
01065 
01066    return;
01067 }

Generated on Mon Jun 27 16:50:53 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7