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 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 432834 $")
00033 
00034 #include <sys/time.h>
00035 #include <signal.h>
00036 
00037 #include "asterisk/channel.h"
00038 #include "asterisk/utils.h"
00039 #include "asterisk/lock.h"
00040 #include "asterisk/linkedlists.h"
00041 #include "asterisk/dial.h"
00042 #include "asterisk/pbx.h"
00043 #include "asterisk/musiconhold.h"
00044 #include "asterisk/app.h"
00045 #include "asterisk/causes.h"
00046 #include "asterisk/stasis_channels.h"
00047 
00048 /*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
00049 struct ast_dial {
00050    int num;                                           /*!< Current number to give to next dialed channel */
00051    int timeout;                                       /*!< Maximum time allowed for dial attempts */
00052    int actual_timeout;                                /*!< Actual timeout based on all factors (ie: channels) */
00053    enum ast_dial_result state;                        /*!< Status of dial */
00054    void *options[AST_DIAL_OPTION_MAX];                /*!< Global options */
00055    ast_dial_state_callback state_callback;            /*!< Status callback */
00056    void *user_data;                                   /*!< Attached user data */
00057    AST_LIST_HEAD(, ast_dial_channel) channels; /*!< Channels being dialed */
00058    pthread_t thread;                                  /*!< Thread (if running in async) */
00059    ast_callid callid;                                 /*!< callid (if running in async) */
00060    ast_mutex_t lock;                                  /*! Lock to protect the thread information above */
00061 };
00062 
00063 /*! \brief Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! */
00064 struct ast_dial_channel {
00065    int num;          /*!< Unique number for dialed channel */
00066    int timeout;            /*!< Maximum time allowed for attempt */
00067    char *tech;          /*!< Technology being dialed */
00068    char *device;           /*!< Device being dialed */
00069    void *options[AST_DIAL_OPTION_MAX]; /*!< Channel specific options */
00070    int cause;           /*!< Cause code in case of failure */
00071    unsigned int is_running_app:1;      /*!< Is this running an application? */
00072    char *assignedid1;            /*!< UniqueID to assign channel */
00073    char *assignedid2;            /*!< UniqueID to assign 2nd channel */
00074    struct ast_channel *owner;    /*!< Asterisk channel */
00075    AST_LIST_ENTRY(ast_dial_channel) list; /*!< Linked list information */
00076 };
00077 
00078 /*! \brief Typedef for dial option enable */
00079 typedef void *(*ast_dial_option_cb_enable)(void *data);
00080 
00081 /*! \brief Typedef for dial option disable */
00082 typedef int (*ast_dial_option_cb_disable)(void *data);
00083 
00084 /*! \brief Structure for 'ANSWER_EXEC' option */
00085 struct answer_exec_struct {
00086    char app[AST_MAX_APP]; /*!< Application name */
00087    char *args;            /*!< Application arguments */
00088 };
00089 
00090 /*! \brief Enable function for 'ANSWER_EXEC' option */
00091 static void *answer_exec_enable(void *data)
00092 {
00093    struct answer_exec_struct *answer_exec = NULL;
00094    char *app = ast_strdupa((char*)data), *args = NULL;
00095 
00096    /* Not giving any data to this option is bad, mmmk? */
00097    if (ast_strlen_zero(app))
00098       return NULL;
00099 
00100    /* Create new data structure */
00101    if (!(answer_exec = ast_calloc(1, sizeof(*answer_exec))))
00102       return NULL;
00103 
00104    /* Parse out application and arguments */
00105    if ((args = strchr(app, ','))) {
00106       *args++ = '\0';
00107       answer_exec->args = ast_strdup(args);
00108    }
00109 
00110    /* Copy application name */
00111    ast_copy_string(answer_exec->app, app, sizeof(answer_exec->app));
00112 
00113    return answer_exec;
00114 }
00115 
00116 /*! \brief Disable function for 'ANSWER_EXEC' option */
00117 static int answer_exec_disable(void *data)
00118 {
00119    struct answer_exec_struct *answer_exec = data;
00120 
00121    /* Make sure we have a value */
00122    if (!answer_exec)
00123       return -1;
00124 
00125    /* If arguments are present, free them too */
00126    if (answer_exec->args)
00127       ast_free(answer_exec->args);
00128 
00129    /* This is simple - just free the structure */
00130    ast_free(answer_exec);
00131 
00132    return 0;
00133 }
00134 
00135 static void *music_enable(void *data)
00136 {
00137    return ast_strdup(data);
00138 }
00139 
00140 static int music_disable(void *data)
00141 {
00142    if (!data)
00143       return -1;
00144 
00145    ast_free(data);
00146 
00147    return 0;
00148 }
00149 
00150 static void *predial_enable(void *data)
00151 {
00152    return ast_strdup(data);
00153 }
00154 
00155 static int predial_disable(void *data)
00156 {
00157    if (!data) {
00158       return -1;
00159    }
00160 
00161    ast_free(data);
00162 
00163    return 0;
00164 }
00165 
00166 /*! \brief Application execution function for 'ANSWER_EXEC' option */
00167 static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
00168 {
00169    struct ast_channel *chan = dial_channel->owner;
00170    struct ast_app *ast_app = pbx_findapp(app);
00171 
00172    /* If the application was not found, return immediately */
00173    if (!ast_app)
00174       return;
00175 
00176    /* All is well... execute the application */
00177    pbx_exec(chan, ast_app, args);
00178 
00179    /* If another thread is not taking over hang up the channel */
00180    ast_mutex_lock(&dial->lock);
00181    if (dial->thread != AST_PTHREADT_STOP) {
00182       ast_hangup(chan);
00183       dial_channel->owner = NULL;
00184    }
00185    ast_mutex_unlock(&dial->lock);
00186 
00187    return;
00188 }
00189 
00190 struct ast_option_types {
00191    enum ast_dial_option option;
00192    ast_dial_option_cb_enable enable;
00193    ast_dial_option_cb_disable disable;
00194 };
00195 
00196 /*!
00197  * \brief Map options to respective handlers (enable/disable).
00198  *
00199  * \note This list MUST be perfectly kept in order with enum
00200  * ast_dial_option, or else madness will happen.
00201  */
00202 static const struct ast_option_types option_types[] = {
00203    { AST_DIAL_OPTION_RINGING, NULL, NULL },                                  /*!< Always indicate ringing to caller */
00204    { AST_DIAL_OPTION_ANSWER_EXEC, answer_exec_enable, answer_exec_disable }, /*!< Execute application upon answer in async mode */
00205    { AST_DIAL_OPTION_MUSIC, music_enable, music_disable },                   /*!< Play music to the caller instead of ringing */
00206    { AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL, NULL },                  /*!< Disable call forwarding on channels */
00207    { AST_DIAL_OPTION_PREDIAL, predial_enable, predial_disable },             /*!< Execute a subroutine on the outbound channels prior to dialing */
00208    { AST_DIAL_OPTION_DIAL_REPLACES_SELF, NULL, NULL },                       /*!< The dial operation is a replacement for the requester */
00209    { AST_DIAL_OPTION_SELF_DESTROY, NULL, NULL},                              /*!< Destroy self at end of ast_dial_run */
00210    { AST_DIAL_OPTION_MAX, NULL, NULL },                                      /*!< Terminator of list */
00211 };
00212 
00213 /*! \brief Maximum number of channels we can watch at a time */
00214 #define AST_MAX_WATCHERS 256
00215 
00216 /*! \brief Macro for finding the option structure to use on a dialed channel */
00217 #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])
00218 
00219 /*! \brief Macro that determines whether a channel is the caller or not */
00220 #define IS_CALLER(chan, owner) (chan == owner ? 1 : 0)
00221 
00222 /*! \brief New dialing structure
00223  * \note Create a dialing structure
00224  * \return Returns a calloc'd ast_dial structure, NULL on failure
00225  */
00226 struct ast_dial *ast_dial_create(void)
00227 {
00228    struct ast_dial *dial = NULL;
00229 
00230    /* Allocate new memory for structure */
00231    if (!(dial = ast_calloc(1, sizeof(*dial))))
00232       return NULL;
00233 
00234    /* Initialize list of channels */
00235    AST_LIST_HEAD_INIT(&dial->channels);
00236 
00237    /* Initialize thread to NULL */
00238    dial->thread = AST_PTHREADT_NULL;
00239 
00240    /* No timeout exists... yet */
00241    dial->timeout = -1;
00242    dial->actual_timeout = -1;
00243 
00244    /* Can't forget about the lock */
00245    ast_mutex_init(&dial->lock);
00246 
00247    return dial;
00248 }
00249 
00250 /*! \brief Append a channel
00251  * \note Appends a channel to a dialing structure
00252  * \return Returns channel reference number on success, -1 on failure
00253  */
00254 int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device, const struct ast_assigned_ids *assignedids)
00255 {
00256    struct ast_dial_channel *channel = NULL;
00257 
00258    /* Make sure we have required arguments */
00259    if (!dial || !tech || !device)
00260       return -1;
00261 
00262    /* Allocate new memory for dialed channel structure */
00263    if (!(channel = ast_calloc(1, sizeof(*channel))))
00264       return -1;
00265 
00266    /* Record technology and device for when we actually dial */
00267    channel->tech = ast_strdup(tech);
00268    channel->device = ast_strdup(device);
00269 
00270    /* Store the assigned id */
00271    if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
00272       channel->assignedid1 = ast_strdup(assignedids->uniqueid);
00273 
00274       if (!ast_strlen_zero(assignedids->uniqueid2)) {
00275          channel->assignedid2 = ast_strdup(assignedids->uniqueid2);
00276       }
00277    }
00278 
00279    /* Grab reference number from dial structure */
00280    channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
00281 
00282    /* No timeout exists... yet */
00283    channel->timeout = -1;
00284 
00285    /* Insert into channels list */
00286    AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
00287 
00288    return channel->num;
00289 }
00290 
00291 /*! \brief Helper function that requests all channels */
00292 static int begin_dial_prerun(struct ast_dial_channel *channel, struct ast_channel *chan, struct ast_format_cap *cap, const char *predial_string)
00293 {
00294    char numsubst[AST_MAX_EXTENSION];
00295    struct ast_format_cap *cap_all_audio = NULL;
00296    struct ast_format_cap *cap_request;
00297    struct ast_assigned_ids assignedids = {
00298       .uniqueid = channel->assignedid1,
00299       .uniqueid2 = channel->assignedid2,
00300    };
00301 
00302    /* Copy device string over */
00303    ast_copy_string(numsubst, channel->device, sizeof(numsubst));
00304 
00305    if (cap && ast_format_cap_count(cap)) {
00306       cap_request = cap;
00307    } else if (chan) {
00308       cap_request = ast_channel_nativeformats(chan);
00309    } else {
00310       cap_all_audio = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
00311       ast_format_cap_append_by_type(cap_all_audio, AST_MEDIA_TYPE_AUDIO);
00312       cap_request = cap_all_audio;
00313    }
00314 
00315    /* If we fail to create our owner channel bail out */
00316    if (!(channel->owner = ast_request(channel->tech, cap_request, &assignedids, chan, numsubst, &channel->cause))) {
00317       ao2_cleanup(cap_all_audio);
00318       return -1;
00319    }
00320    cap_request = NULL;
00321    ao2_cleanup(cap_all_audio);
00322 
00323    if (chan) {
00324       ast_channel_lock_both(chan, channel->owner);
00325    } else {
00326       ast_channel_lock(channel->owner);
00327    }
00328 
00329    ast_channel_stage_snapshot(channel->owner);
00330 
00331    ast_channel_appl_set(channel->owner, "AppDial2");
00332    ast_channel_data_set(channel->owner, "(Outgoing Line)");
00333 
00334    memset(ast_channel_whentohangup(channel->owner), 0, sizeof(*ast_channel_whentohangup(channel->owner)));
00335 
00336    /* Inherit everything from he who spawned this dial */
00337    if (chan) {
00338       ast_channel_inherit_variables(chan, channel->owner);
00339       ast_channel_datastore_inherit(chan, channel->owner);
00340 
00341       /* Copy over callerid information */
00342       ast_party_redirecting_copy(ast_channel_redirecting(channel->owner), ast_channel_redirecting(chan));
00343 
00344       ast_channel_dialed(channel->owner)->transit_network_select = ast_channel_dialed(chan)->transit_network_select;
00345 
00346       ast_connected_line_copy_from_caller(ast_channel_connected(channel->owner), ast_channel_caller(chan));
00347 
00348       ast_channel_language_set(channel->owner, ast_channel_language(chan));
00349       if (channel->options[AST_DIAL_OPTION_DIAL_REPLACES_SELF]) {
00350          ast_channel_req_accountcodes(channel->owner, chan, AST_CHANNEL_REQUESTOR_REPLACEMENT);
00351       } else {
00352          ast_channel_req_accountcodes(channel->owner, chan, AST_CHANNEL_REQUESTOR_BRIDGE_PEER);
00353       }
00354       if (ast_strlen_zero(ast_channel_musicclass(channel->owner)))
00355          ast_channel_musicclass_set(channel->owner, ast_channel_musicclass(chan));
00356 
00357       ast_channel_adsicpe_set(channel->owner, ast_channel_adsicpe(chan));
00358       ast_channel_transfercapability_set(channel->owner, ast_channel_transfercapability(chan));
00359       ast_channel_unlock(chan);
00360    }
00361 
00362    ast_channel_stage_snapshot_done(channel->owner);
00363    ast_channel_unlock(channel->owner);
00364 
00365    if (!ast_strlen_zero(predial_string)) {
00366       const char *predial_callee = ast_app_expand_sub_args(chan, predial_string);
00367       if (!predial_callee) {
00368          ast_log(LOG_ERROR, "Could not expand subroutine arguments in predial request '%s'\n", predial_string);
00369       }
00370       ast_autoservice_start(chan);
00371       ast_pre_call(channel->owner, predial_callee);
00372       ast_autoservice_stop(chan);
00373       ast_free((char *) predial_callee);
00374    }
00375 
00376    return 0;
00377 }
00378 
00379 int ast_dial_prerun(struct ast_dial *dial, struct ast_channel *chan, struct ast_format_cap *cap)
00380 {
00381    struct ast_dial_channel *channel;
00382    int res = -1;
00383    char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
00384 
00385    if (!ast_strlen_zero(predial_string)) {
00386       ast_replace_subargument_delimiter(predial_string);
00387    }
00388 
00389    AST_LIST_LOCK(&dial->channels);
00390    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00391       if ((res = begin_dial_prerun(channel, chan, cap, predial_string))) {
00392          break;
00393       }
00394    }
00395    AST_LIST_UNLOCK(&dial->channels);
00396 
00397    return res;
00398 }
00399 
00400 /*! \brief Helper function that does the beginning dialing per-appended channel */
00401 static int begin_dial_channel(struct ast_dial_channel *channel, struct ast_channel *chan, int async, const char *predial_string)
00402 {
00403    char numsubst[AST_MAX_EXTENSION];
00404    int res = 1;
00405 
00406    /* If no owner channel exists yet execute pre-run */
00407    if (!channel->owner && begin_dial_prerun(channel, chan, NULL, predial_string)) {
00408       return 0;
00409    }
00410 
00411    /* Copy device string over */
00412    ast_copy_string(numsubst, channel->device, sizeof(numsubst));
00413 
00414    /* Attempt to actually call this device */
00415    if ((res = ast_call(channel->owner, numsubst, 0))) {
00416       res = 0;
00417       ast_hangup(channel->owner);
00418       channel->owner = NULL;
00419    } else {
00420       if (chan) {
00421          ast_poll_channel_add(chan, channel->owner);
00422       }
00423       ast_channel_publish_dial(async ? NULL : chan, channel->owner, channel->device, NULL);
00424       res = 1;
00425       ast_verb(3, "Called %s\n", numsubst);
00426    }
00427 
00428    return res;
00429 }
00430 
00431 /*! \brief Helper function that does the beginning dialing per dial structure */
00432 static int begin_dial(struct ast_dial *dial, struct ast_channel *chan, int async)
00433 {
00434    struct ast_dial_channel *channel = NULL;
00435    int success = 0;
00436    char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
00437 
00438    if (!ast_strlen_zero(predial_string)) {
00439       ast_replace_subargument_delimiter(predial_string);
00440    }
00441 
00442    /* Iterate through channel list, requesting and calling each one */
00443    AST_LIST_LOCK(&dial->channels);
00444    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00445       success += begin_dial_channel(channel, chan, async, predial_string);
00446    }
00447    AST_LIST_UNLOCK(&dial->channels);
00448 
00449    /* If number of failures matches the number of channels, then this truly failed */
00450    return success;
00451 }
00452 
00453 /*! \brief Helper function to handle channels that have been call forwarded */
00454 static int handle_call_forward(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_channel *chan)
00455 {
00456    struct ast_channel *original = channel->owner;
00457    char *tmp = ast_strdupa(ast_channel_call_forward(channel->owner));
00458    char *tech = "Local", *device = tmp, *stuff;
00459    char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
00460 
00461    if (!ast_strlen_zero(predial_string)) {
00462       ast_replace_subargument_delimiter(predial_string);
00463    }
00464 
00465    /* If call forwarding is disabled just drop the original channel and don't attempt to dial the new one */
00466    if (FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING)) {
00467       ast_hangup(original);
00468       channel->owner = NULL;
00469       return 0;
00470    }
00471 
00472    /* Figure out the new destination */
00473    if ((stuff = strchr(tmp, '/'))) {
00474       *stuff++ = '\0';
00475       tech = tmp;
00476       device = stuff;
00477    } else {
00478       const char *forward_context;
00479       char destination[AST_MAX_CONTEXT + AST_MAX_EXTENSION + 1];
00480 
00481       ast_channel_lock(original);
00482       forward_context = pbx_builtin_getvar_helper(original, "FORWARD_CONTEXT");
00483       snprintf(destination, sizeof(destination), "%s@%s", tmp, S_OR(forward_context, ast_channel_context(original)));
00484       ast_channel_unlock(original);
00485       device = ast_strdupa(destination);
00486    }
00487 
00488    /* Drop old destination information */
00489    ast_free(channel->tech);
00490    ast_free(channel->device);
00491    ast_free(channel->assignedid1);
00492    channel->assignedid1 = NULL;
00493    ast_free(channel->assignedid2);
00494    channel->assignedid2 = NULL;
00495 
00496    /* Update the dial channel with the new destination information */
00497    channel->tech = ast_strdup(tech);
00498    channel->device = ast_strdup(device);
00499    AST_LIST_UNLOCK(&dial->channels);
00500 
00501    /* Drop the original channel */
00502    channel->owner = NULL;
00503 
00504    /* Finally give it a go... send it out into the world */
00505    begin_dial_channel(channel, chan, chan ? 0 : 1, predial_string);
00506 
00507    ast_channel_publish_dial_forward(chan, original, channel->owner, NULL, "CANCEL",
00508       ast_channel_call_forward(original));
00509 
00510    ast_hangup(original);
00511 
00512    return 0;
00513 }
00514 
00515 /*! \brief Helper function that finds the dialed channel based on owner */
00516 static struct ast_dial_channel *find_relative_dial_channel(struct ast_dial *dial, struct ast_channel *owner)
00517 {
00518    struct ast_dial_channel *channel = NULL;
00519 
00520    AST_LIST_LOCK(&dial->channels);
00521    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00522       if (channel->owner == owner)
00523          break;
00524    }
00525    AST_LIST_UNLOCK(&dial->channels);
00526 
00527    return channel;
00528 }
00529 
00530 static void set_state(struct ast_dial *dial, enum ast_dial_result state)
00531 {
00532    dial->state = state;
00533 
00534    if (dial->state_callback)
00535       dial->state_callback(dial);
00536 }
00537 
00538 /*! \brief Helper function that handles control frames WITH owner */
00539 static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
00540 {
00541    if (fr->frametype == AST_FRAME_CONTROL) {
00542       switch (fr->subclass.integer) {
00543       case AST_CONTROL_ANSWER:
00544          ast_verb(3, "%s answered %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
00545          AST_LIST_LOCK(&dial->channels);
00546          AST_LIST_REMOVE(&dial->channels, channel, list);
00547          AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
00548          AST_LIST_UNLOCK(&dial->channels);
00549          ast_channel_publish_dial(chan, channel->owner, channel->device, "ANSWER");
00550          set_state(dial, AST_DIAL_RESULT_ANSWERED);
00551          break;
00552       case AST_CONTROL_BUSY:
00553          ast_verb(3, "%s is busy\n", ast_channel_name(channel->owner));
00554          ast_channel_publish_dial(chan, channel->owner, channel->device, "BUSY");
00555          ast_hangup(channel->owner);
00556          channel->cause = AST_CAUSE_USER_BUSY;
00557          channel->owner = NULL;
00558          break;
00559       case AST_CONTROL_CONGESTION:
00560          ast_verb(3, "%s is circuit-busy\n", ast_channel_name(channel->owner));
00561          ast_channel_publish_dial(chan, channel->owner, channel->device, "CONGESTION");
00562          ast_hangup(channel->owner);
00563          channel->cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
00564          channel->owner = NULL;
00565          break;
00566       case AST_CONTROL_INCOMPLETE:
00567          ast_verb(3, "%s dialed Incomplete extension %s\n", ast_channel_name(channel->owner), ast_channel_exten(channel->owner));
00568          ast_indicate(chan, AST_CONTROL_INCOMPLETE);
00569          break;
00570       case AST_CONTROL_RINGING:
00571          ast_verb(3, "%s is ringing\n", ast_channel_name(channel->owner));
00572          if (!dial->options[AST_DIAL_OPTION_MUSIC])
00573             ast_indicate(chan, AST_CONTROL_RINGING);
00574          set_state(dial, AST_DIAL_RESULT_RINGING);
00575          break;
00576       case AST_CONTROL_PROGRESS:
00577          ast_verb(3, "%s is making progress, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
00578          ast_indicate(chan, AST_CONTROL_PROGRESS);
00579          set_state(dial, AST_DIAL_RESULT_PROGRESS);
00580          break;
00581       case AST_CONTROL_VIDUPDATE:
00582          ast_verb(3, "%s requested a video update, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
00583          ast_indicate(chan, AST_CONTROL_VIDUPDATE);
00584          break;
00585       case AST_CONTROL_SRCUPDATE:
00586          ast_verb(3, "%s requested a source update, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
00587          ast_indicate(chan, AST_CONTROL_SRCUPDATE);
00588          break;
00589       case AST_CONTROL_CONNECTED_LINE:
00590          ast_verb(3, "%s connected line has changed, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
00591          if (ast_channel_connected_line_sub(channel->owner, chan, fr, 1) &&
00592             ast_channel_connected_line_macro(channel->owner, chan, fr, 1, 1)) {
00593             ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, fr->data.ptr, fr->datalen);
00594          }
00595          break;
00596       case AST_CONTROL_REDIRECTING:
00597          ast_verb(3, "%s redirecting info has changed, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
00598          if (ast_channel_redirecting_sub(channel->owner, chan, fr, 1) &&
00599             ast_channel_redirecting_macro(channel->owner, chan, fr, 1, 1)) {
00600             ast_indicate_data(chan, AST_CONTROL_REDIRECTING, fr->data.ptr, fr->datalen);
00601          }
00602          break;
00603       case AST_CONTROL_PROCEEDING:
00604          ast_verb(3, "%s is proceeding, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
00605          ast_indicate(chan, AST_CONTROL_PROCEEDING);
00606          set_state(dial, AST_DIAL_RESULT_PROCEEDING);
00607          break;
00608       case AST_CONTROL_HOLD:
00609          ast_verb(3, "Call on %s placed on hold\n", ast_channel_name(chan));
00610          ast_indicate_data(chan, AST_CONTROL_HOLD, fr->data.ptr, fr->datalen);
00611          break;
00612       case AST_CONTROL_UNHOLD:
00613          ast_verb(3, "Call on %s left from hold\n", ast_channel_name(chan));
00614          ast_indicate(chan, AST_CONTROL_UNHOLD);
00615          break;
00616       case AST_CONTROL_OFFHOOK:
00617       case AST_CONTROL_FLASH:
00618          break;
00619       case AST_CONTROL_PVT_CAUSE_CODE:
00620          ast_indicate_data(chan, AST_CONTROL_PVT_CAUSE_CODE, fr->data.ptr, fr->datalen);
00621          break;
00622       case -1:
00623          /* Prod the channel */
00624          ast_indicate(chan, -1);
00625          break;
00626       default:
00627          break;
00628       }
00629    }
00630 }
00631 
00632 /*! \brief Helper function that handles control frames WITHOUT owner */
00633 static void handle_frame_ownerless(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr)
00634 {
00635    /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
00636    if (fr->frametype != AST_FRAME_CONTROL)
00637       return;
00638 
00639    switch (fr->subclass.integer) {
00640    case AST_CONTROL_ANSWER:
00641       ast_verb(3, "%s answered\n", ast_channel_name(channel->owner));
00642       AST_LIST_LOCK(&dial->channels);
00643       AST_LIST_REMOVE(&dial->channels, channel, list);
00644       AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
00645       AST_LIST_UNLOCK(&dial->channels);
00646       ast_channel_publish_dial(NULL, channel->owner, channel->device, "ANSWER");
00647       set_state(dial, AST_DIAL_RESULT_ANSWERED);
00648       break;
00649    case AST_CONTROL_BUSY:
00650       ast_verb(3, "%s is busy\n", ast_channel_name(channel->owner));
00651       ast_channel_publish_dial(NULL, channel->owner, channel->device, "BUSY");
00652       ast_hangup(channel->owner);
00653       channel->cause = AST_CAUSE_USER_BUSY;
00654       channel->owner = NULL;
00655       break;
00656    case AST_CONTROL_CONGESTION:
00657       ast_verb(3, "%s is circuit-busy\n", ast_channel_name(channel->owner));
00658       ast_channel_publish_dial(NULL, channel->owner, channel->device, "CONGESTION");
00659       ast_hangup(channel->owner);
00660       channel->cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
00661       channel->owner = NULL;
00662       break;
00663    case AST_CONTROL_INCOMPLETE:
00664       /*
00665        * Nothing to do but abort the call since we have no
00666        * controlling channel to ask for more digits.
00667        */
00668       ast_verb(3, "%s dialed Incomplete extension %s\n",
00669          ast_channel_name(channel->owner), ast_channel_exten(channel->owner));
00670       ast_hangup(channel->owner);
00671       channel->cause = AST_CAUSE_UNALLOCATED;
00672       channel->owner = NULL;
00673       break;
00674    case AST_CONTROL_RINGING:
00675       ast_verb(3, "%s is ringing\n", ast_channel_name(channel->owner));
00676       set_state(dial, AST_DIAL_RESULT_RINGING);
00677       break;
00678    case AST_CONTROL_PROGRESS:
00679       ast_verb(3, "%s is making progress\n", ast_channel_name(channel->owner));
00680       set_state(dial, AST_DIAL_RESULT_PROGRESS);
00681       break;
00682    case AST_CONTROL_PROCEEDING:
00683       ast_verb(3, "%s is proceeding\n", ast_channel_name(channel->owner));
00684       set_state(dial, AST_DIAL_RESULT_PROCEEDING);
00685       break;
00686    default:
00687       break;
00688    }
00689 }
00690 
00691 /*! \brief Helper function to handle when a timeout occurs on dialing attempt */
00692 static int handle_timeout_trip(struct ast_dial *dial, struct timeval start)
00693 {
00694    struct ast_dial_channel *channel = NULL;
00695    int diff = ast_tvdiff_ms(ast_tvnow(), start), lowest_timeout = -1, new_timeout = -1;
00696 
00697    /* If there is no difference yet return the dial timeout so we can go again, we were likely interrupted */
00698    if (!diff) {
00699       return dial->timeout;
00700    }
00701 
00702    /* If the global dial timeout tripped switch the state to timeout so our channel loop will drop every channel */
00703    if (diff >= dial->timeout) {
00704       set_state(dial, AST_DIAL_RESULT_TIMEOUT);
00705       new_timeout = 0;
00706    }
00707 
00708    /* Go through dropping out channels that have met their timeout */
00709    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00710       if (dial->state == AST_DIAL_RESULT_TIMEOUT || diff >= channel->timeout) {
00711          ast_hangup(channel->owner);
00712          channel->cause = AST_CAUSE_NO_ANSWER;
00713          channel->owner = NULL;
00714       } else if ((lowest_timeout == -1) || (lowest_timeout > channel->timeout)) {
00715          lowest_timeout = channel->timeout;
00716       }
00717    }
00718 
00719    /* Calculate the new timeout using the lowest timeout found */
00720    if (lowest_timeout >= 0)
00721       new_timeout = lowest_timeout - diff;
00722 
00723    return new_timeout;
00724 }
00725 
00726 const char *ast_hangup_cause_to_dial_status(int hangup_cause)
00727 {
00728    switch(hangup_cause) {
00729    case AST_CAUSE_BUSY:
00730       return "BUSY";
00731    case AST_CAUSE_CONGESTION:
00732       return "CONGESTION";
00733    case AST_CAUSE_NO_ROUTE_DESTINATION:
00734    case AST_CAUSE_UNREGISTERED:
00735       return "CHANUNAVAIL";
00736    case AST_CAUSE_NO_ANSWER:
00737    default:
00738       return "NOANSWER";
00739    }
00740 }
00741 
00742 /*! \brief Helper function that basically keeps tabs on dialing attempts */
00743 static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
00744 {
00745    int timeout = -1;
00746    struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
00747    struct ast_dial_channel *channel = NULL;
00748    struct answer_exec_struct *answer_exec = NULL;
00749    struct timeval start;
00750 
00751    set_state(dial, AST_DIAL_RESULT_TRYING);
00752 
00753    /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
00754    if (dial->options[AST_DIAL_OPTION_RINGING]) {
00755       set_state(dial, AST_DIAL_RESULT_RINGING);
00756       if (chan)
00757          ast_indicate(chan, AST_CONTROL_RINGING);
00758    } else if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
00759       !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
00760       char *original_moh = ast_strdupa(ast_channel_musicclass(chan));
00761       ast_indicate(chan, -1);
00762       ast_channel_musicclass_set(chan, dial->options[AST_DIAL_OPTION_MUSIC]);
00763       ast_moh_start(chan, dial->options[AST_DIAL_OPTION_MUSIC], NULL);
00764       ast_channel_musicclass_set(chan, original_moh);
00765    }
00766 
00767    /* Record start time for timeout purposes */
00768    start = ast_tvnow();
00769 
00770    /* We actually figured out the maximum timeout we can do as they were added, so we can directly access the info */
00771    timeout = dial->actual_timeout;
00772 
00773    /* Go into an infinite loop while we are trying */
00774    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)) {
00775       int pos = 0, count = 0;
00776       struct ast_frame *fr = NULL;
00777 
00778       /* Set up channel structure array */
00779       pos = count = 0;
00780       if (chan)
00781          cs[pos++] = chan;
00782 
00783       /* Add channels we are attempting to dial */
00784       AST_LIST_LOCK(&dial->channels);
00785       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00786          if (channel->owner) {
00787             cs[pos++] = channel->owner;
00788             count++;
00789          }
00790       }
00791       AST_LIST_UNLOCK(&dial->channels);
00792 
00793       /* If we have no outbound channels in progress, switch state to unanswered and stop */
00794       if (!count) {
00795          set_state(dial, AST_DIAL_RESULT_UNANSWERED);
00796          break;
00797       }
00798 
00799       /* Just to be safe... */
00800       if (dial->thread == AST_PTHREADT_STOP)
00801          break;
00802 
00803       /* Wait for frames from channels */
00804       who = ast_waitfor_n(cs, pos, &timeout);
00805 
00806       /* Check to see if our thread is being canceled */
00807       if (dial->thread == AST_PTHREADT_STOP)
00808          break;
00809 
00810       /* If the timeout no longer exists OR if we got no channel it basically means the timeout was tripped, so handle it */
00811       if (!timeout || !who) {
00812          timeout = handle_timeout_trip(dial, start);
00813          continue;
00814       }
00815 
00816       /* Find relative dial channel */
00817       if (!chan || !IS_CALLER(chan, who))
00818          channel = find_relative_dial_channel(dial, who);
00819 
00820       /* See if this channel has been forwarded elsewhere */
00821       if (!ast_strlen_zero(ast_channel_call_forward(who))) {
00822          handle_call_forward(dial, channel, chan);
00823          continue;
00824       }
00825 
00826       /* Attempt to read in a frame */
00827       if (!(fr = ast_read(who))) {
00828          /* If this is the caller then we switch state to hangup and stop */
00829          if (chan && IS_CALLER(chan, who)) {
00830             set_state(dial, AST_DIAL_RESULT_HANGUP);
00831             break;
00832          }
00833          if (chan)
00834             ast_poll_channel_del(chan, channel->owner);
00835          ast_channel_publish_dial(chan, who, channel->device, ast_hangup_cause_to_dial_status(ast_channel_hangupcause(who)));
00836          ast_hangup(who);
00837          channel->owner = NULL;
00838          continue;
00839       }
00840 
00841       /* Process the frame */
00842       if (chan)
00843          handle_frame(dial, channel, fr, chan);
00844       else
00845          handle_frame_ownerless(dial, channel, fr);
00846 
00847       /* Free the received frame and start all over */
00848       ast_frfree(fr);
00849    }
00850 
00851    /* Do post-processing from loop */
00852    if (dial->state == AST_DIAL_RESULT_ANSWERED) {
00853       /* Hangup everything except that which answered */
00854       AST_LIST_LOCK(&dial->channels);
00855       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00856          if (!channel->owner || channel->owner == who)
00857             continue;
00858          if (chan)
00859             ast_poll_channel_del(chan, channel->owner);
00860          ast_channel_publish_dial(chan, channel->owner, channel->device, "CANCEL");
00861          ast_hangup(channel->owner);
00862          channel->cause = AST_CAUSE_ANSWERED_ELSEWHERE;
00863          channel->owner = NULL;
00864       }
00865       AST_LIST_UNLOCK(&dial->channels);
00866       /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
00867       if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC))) {
00868          channel->is_running_app = 1;
00869          answer_exec_run(dial, channel, answer_exec->app, answer_exec->args);
00870          channel->is_running_app = 0;
00871       }
00872 
00873       if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
00874          !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
00875          ast_moh_stop(chan);
00876       }
00877    } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
00878       /* Hangup everything */
00879       AST_LIST_LOCK(&dial->channels);
00880       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00881          if (!channel->owner)
00882             continue;
00883          if (chan)
00884             ast_poll_channel_del(chan, channel->owner);
00885          ast_channel_publish_dial(chan, channel->owner, channel->device, "CANCEL");
00886          ast_hangup(channel->owner);
00887          channel->cause = AST_CAUSE_NORMAL_CLEARING;
00888          channel->owner = NULL;
00889       }
00890       AST_LIST_UNLOCK(&dial->channels);
00891    }
00892 
00893    if (dial->options[AST_DIAL_OPTION_SELF_DESTROY]) {
00894       enum ast_dial_result state = dial->state;
00895 
00896       ast_dial_destroy(dial);
00897       return state;
00898    }
00899 
00900    return dial->state;
00901 }
00902 
00903 /*! \brief Dial async thread function */
00904 static void *async_dial(void *data)
00905 {
00906    struct ast_dial *dial = data;
00907    if (dial->callid) {
00908       ast_callid_threadassoc_add(dial->callid);
00909    }
00910 
00911    /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
00912    monitor_dial(dial, NULL);
00913 
00914    return NULL;
00915 }
00916 
00917 /*! \brief Execute dialing synchronously or asynchronously
00918  * \note Dials channels in a dial structure.
00919  * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
00920  */
00921 enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
00922 {
00923    enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
00924 
00925    /* Ensure required arguments are passed */
00926    if (!dial) {
00927       ast_debug(1, "invalid #1\n");
00928       return AST_DIAL_RESULT_INVALID;
00929    }
00930 
00931    /* If there are no channels to dial we can't very well try to dial them */
00932    if (AST_LIST_EMPTY(&dial->channels)) {
00933       ast_debug(1, "invalid #2\n");
00934       return AST_DIAL_RESULT_INVALID;
00935    }
00936 
00937    /* Dial each requested channel */
00938    if (!begin_dial(dial, chan, async))
00939       return AST_DIAL_RESULT_FAILED;
00940 
00941    /* If we are running async spawn a thread and send it away... otherwise block here */
00942    if (async) {
00943       /* reference be released at dial destruction if it isn't NULL */
00944       dial->callid = ast_read_threadstorage_callid();
00945       dial->state = AST_DIAL_RESULT_TRYING;
00946       /* Try to create a thread */
00947       if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
00948          /* Failed to create the thread - hangup all dialed channels and return failed */
00949          ast_dial_hangup(dial);
00950          res = AST_DIAL_RESULT_FAILED;
00951       }
00952    } else {
00953       res = monitor_dial(dial, chan);
00954    }
00955 
00956    return res;
00957 }
00958 
00959 /*! \brief Return channel that answered
00960  * \note Returns the Asterisk channel that answered
00961  * \param dial Dialing structure
00962  */
00963 struct ast_channel *ast_dial_answered(struct ast_dial *dial)
00964 {
00965    if (!dial)
00966       return NULL;
00967 
00968    return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
00969 }
00970 
00971 /*! \brief Steal the channel that answered
00972  * \note Returns the Asterisk channel that answered and removes it from the dialing structure
00973  * \param dial Dialing structure
00974  */
00975 struct ast_channel *ast_dial_answered_steal(struct ast_dial *dial)
00976 {
00977    struct ast_channel *chan = NULL;
00978 
00979    if (!dial)
00980       return NULL;
00981 
00982    if (dial->state == AST_DIAL_RESULT_ANSWERED) {
00983       chan = AST_LIST_FIRST(&dial->channels)->owner;
00984       AST_LIST_FIRST(&dial->channels)->owner = NULL;
00985    }
00986 
00987    return chan;
00988 }
00989 
00990 /*! \brief Return state of dial
00991  * \note Returns the state of the dial attempt
00992  * \param dial Dialing structure
00993  */
00994 enum ast_dial_result ast_dial_state(struct ast_dial *dial)
00995 {
00996    return dial->state;
00997 }
00998 
00999 /*! \brief Cancel async thread
01000  * \note Cancel a running async thread
01001  * \param dial Dialing structure
01002  */
01003 enum ast_dial_result ast_dial_join(struct ast_dial *dial)
01004 {
01005    pthread_t thread;
01006 
01007    /* If the dial structure is not running in async, return failed */
01008    if (dial->thread == AST_PTHREADT_NULL)
01009       return AST_DIAL_RESULT_FAILED;
01010 
01011    /* Record thread */
01012    thread = dial->thread;
01013 
01014    /* Boom, commence locking */
01015    ast_mutex_lock(&dial->lock);
01016 
01017    /* Stop the thread */
01018    dial->thread = AST_PTHREADT_STOP;
01019 
01020    /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
01021    AST_LIST_LOCK(&dial->channels);
01022    if (AST_LIST_FIRST(&dial->channels)->is_running_app) {
01023       struct ast_channel *chan = AST_LIST_FIRST(&dial->channels)->owner;
01024       if (chan) {
01025          ast_channel_lock(chan);
01026          ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
01027          ast_channel_unlock(chan);
01028       }
01029    } else {
01030       /* Now we signal it with SIGURG so it will break out of it's waitfor */
01031       pthread_kill(thread, SIGURG);
01032    }
01033    AST_LIST_UNLOCK(&dial->channels);
01034 
01035    /* Yay done with it */
01036    ast_mutex_unlock(&dial->lock);
01037 
01038    /* Finally wait for the thread to exit */
01039    pthread_join(thread, NULL);
01040 
01041    /* Yay thread is all gone */
01042    dial->thread = AST_PTHREADT_NULL;
01043 
01044    return dial->state;
01045 }
01046 
01047 /*! \brief Hangup channels
01048  * \note Hangup all active channels
01049  * \param dial Dialing structure
01050  */
01051 void ast_dial_hangup(struct ast_dial *dial)
01052 {
01053    struct ast_dial_channel *channel = NULL;
01054 
01055    if (!dial)
01056       return;
01057 
01058    AST_LIST_LOCK(&dial->channels);
01059    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
01060       ast_hangup(channel->owner);
01061       channel->owner = NULL;
01062    }
01063    AST_LIST_UNLOCK(&dial->channels);
01064 
01065    return;
01066 }
01067 
01068 /*! \brief Destroys a dialing structure
01069  * \note Destroys (free's) the given ast_dial structure
01070  * \param dial Dialing structure to free
01071  * \return Returns 0 on success, -1 on failure
01072  */
01073 int ast_dial_destroy(struct ast_dial *dial)
01074 {
01075    int i = 0;
01076    struct ast_dial_channel *channel = NULL;
01077 
01078    if (!dial)
01079       return -1;
01080 
01081    /* Hangup and deallocate all the dialed channels */
01082    AST_LIST_LOCK(&dial->channels);
01083    AST_LIST_TRAVERSE_SAFE_BEGIN(&dial->channels, channel, list) {
01084       /* Disable any enabled options */
01085       for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
01086          if (!channel->options[i])
01087             continue;
01088          if (option_types[i].disable)
01089             option_types[i].disable(channel->options[i]);
01090          channel->options[i] = NULL;
01091       }
01092 
01093       /* Hang up channel if need be */
01094       ast_hangup(channel->owner);
01095       channel->owner = NULL;
01096 
01097       /* Free structure */
01098       ast_free(channel->tech);
01099       ast_free(channel->device);
01100       ast_free(channel->assignedid1);
01101       ast_free(channel->assignedid2);
01102 
01103       AST_LIST_REMOVE_CURRENT(list);
01104       ast_free(channel);
01105    }
01106    AST_LIST_TRAVERSE_SAFE_END;
01107    AST_LIST_UNLOCK(&dial->channels);
01108 
01109    /* Disable any enabled options globally */
01110    for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
01111       if (!dial->options[i])
01112          continue;
01113       if (option_types[i].disable)
01114          option_types[i].disable(dial->options[i]);
01115       dial->options[i] = NULL;
01116    }
01117 
01118    /* Lock be gone! */
01119    ast_mutex_destroy(&dial->lock);
01120 
01121    /* Free structure */
01122    ast_free(dial);
01123 
01124    return 0;
01125 }
01126 
01127 /*! \brief Enables an option globally
01128  * \param dial Dial structure to enable option on
01129  * \param option Option to enable
01130  * \param data Data to pass to this option (not always needed)
01131  * \return Returns 0 on success, -1 on failure
01132  */
01133 int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
01134 {
01135    /* If the option is already enabled, return failure */
01136    if (dial->options[option])
01137       return -1;
01138 
01139    /* Execute enable callback if it exists, if not simply make sure the value is set */
01140    if (option_types[option].enable)
01141       dial->options[option] = option_types[option].enable(data);
01142    else
01143       dial->options[option] = (void*)1;
01144 
01145    return 0;
01146 }
01147 
01148 /*! \brief Helper function for finding a channel in a dial structure based on number
01149  */
01150 static struct ast_dial_channel *find_dial_channel(struct ast_dial *dial, int num)
01151 {
01152    struct ast_dial_channel *channel = AST_LIST_LAST(&dial->channels);
01153 
01154    /* We can try to predict programmer behavior, the last channel they added is probably the one they wanted to modify */
01155    if (channel->num == num)
01156       return channel;
01157 
01158    /* Hrm not at the end... looking through the list it is! */
01159    AST_LIST_LOCK(&dial->channels);
01160    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
01161       if (channel->num == num)
01162          break;
01163    }
01164    AST_LIST_UNLOCK(&dial->channels);
01165 
01166    return channel;
01167 }
01168 
01169 /*! \brief Enables an option per channel
01170  * \param dial Dial structure
01171  * \param num Channel number to enable option on
01172  * \param option Option to enable
01173  * \param data Data to pass to this option (not always needed)
01174  * \return Returns 0 on success, -1 on failure
01175  */
01176 int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
01177 {
01178    struct ast_dial_channel *channel = NULL;
01179 
01180    /* Ensure we have required arguments */
01181    if (!dial || AST_LIST_EMPTY(&dial->channels))
01182       return -1;
01183 
01184    if (!(channel = find_dial_channel(dial, num)))
01185       return -1;
01186 
01187    /* If the option is already enabled, return failure */
01188    if (channel->options[option])
01189       return -1;
01190 
01191    /* Execute enable callback if it exists, if not simply make sure the value is set */
01192    if (option_types[option].enable)
01193       channel->options[option] = option_types[option].enable(data);
01194    else
01195       channel->options[option] = (void*)1;
01196 
01197    return 0;
01198 }
01199 
01200 /*! \brief Disables an option globally
01201  * \param dial Dial structure to disable option on
01202  * \param option Option to disable
01203  * \return Returns 0 on success, -1 on failure
01204  */
01205 int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
01206 {
01207    /* If the option is not enabled, return failure */
01208    if (!dial->options[option]) {
01209       return -1;
01210    }
01211 
01212    /* Execute callback of option to disable if it exists */
01213    if (option_types[option].disable)
01214       option_types[option].disable(dial->options[option]);
01215 
01216    /* Finally disable option on the structure */
01217    dial->options[option] = NULL;
01218 
01219    return 0;
01220 }
01221 
01222 /*! \brief Disables an option per channel
01223  * \param dial Dial structure
01224  * \param num Channel number to disable option on
01225  * \param option Option to disable
01226  * \return Returns 0 on success, -1 on failure
01227  */
01228 int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
01229 {
01230    struct ast_dial_channel *channel = NULL;
01231 
01232    /* Ensure we have required arguments */
01233    if (!dial || AST_LIST_EMPTY(&dial->channels))
01234       return -1;
01235 
01236    if (!(channel = find_dial_channel(dial, num)))
01237       return -1;
01238 
01239    /* If the option is not enabled, return failure */
01240    if (!channel->options[option])
01241       return -1;
01242 
01243    /* Execute callback of option to disable it if it exists */
01244    if (option_types[option].disable)
01245       option_types[option].disable(channel->options[option]);
01246 
01247    /* Finally disable the option on the structure */
01248    channel->options[option] = NULL;
01249 
01250    return 0;
01251 }
01252 
01253 int ast_dial_reason(struct ast_dial *dial, int num)
01254 {
01255    struct ast_dial_channel *channel;
01256 
01257    if (!dial || AST_LIST_EMPTY(&dial->channels) || !(channel = find_dial_channel(dial, num))) {
01258       return -1;
01259    }
01260 
01261    return channel->cause;
01262 }
01263 
01264 struct ast_channel *ast_dial_get_channel(struct ast_dial *dial, int num)
01265 {
01266    struct ast_dial_channel *channel;
01267 
01268    if (!dial || AST_LIST_EMPTY(&dial->channels) || !(channel = find_dial_channel(dial, num))) {
01269       return NULL;
01270    }
01271 
01272    return channel->owner;
01273 }
01274 
01275 void ast_dial_set_state_callback(struct ast_dial *dial, ast_dial_state_callback callback)
01276 {
01277    dial->state_callback = callback;
01278 }
01279 
01280 void ast_dial_set_user_data(struct ast_dial *dial, void *user_data)
01281 {
01282    dial->user_data = user_data;
01283 }
01284 
01285 void *ast_dial_get_user_data(struct ast_dial *dial)
01286 {
01287    return dial->user_data;
01288 }
01289 
01290 /*! \brief Set the maximum time (globally) allowed for trying to ring phones
01291  * \param dial The dial structure to apply the time limit to
01292  * \param timeout Maximum time allowed
01293  * \return nothing
01294  */
01295 void ast_dial_set_global_timeout(struct ast_dial *dial, int timeout)
01296 {
01297    dial->timeout = timeout;
01298 
01299    if (dial->timeout > 0 && (dial->actual_timeout > dial->timeout || dial->actual_timeout == -1))
01300       dial->actual_timeout = dial->timeout;
01301 
01302    return;
01303 }
01304 
01305 /*! \brief Set the maximum time (per channel) allowed for trying to ring the phone
01306  * \param dial The dial structure the channel belongs to
01307  * \param num Channel number to set timeout on
01308  * \param timeout Maximum time allowed
01309  * \return nothing
01310  */
01311 void ast_dial_set_timeout(struct ast_dial *dial, int num, int timeout)
01312 {
01313    struct ast_dial_channel *channel = NULL;
01314 
01315    if (!(channel = find_dial_channel(dial, num)))
01316       return;
01317 
01318    channel->timeout = timeout;
01319 
01320    if (channel->timeout > 0 && (dial->actual_timeout > channel->timeout || dial->actual_timeout == -1))
01321       dial->actual_timeout = channel->timeout;
01322 
01323    return;
01324 }

Generated on Thu Apr 16 06:27:33 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6