Sat Mar 10 01:55:20 2012

Asterisk developer's documentation


dial.c File Reference

Dialing API. More...

#include "asterisk.h"
#include <sys/time.h>
#include <signal.h>
#include "asterisk/channel.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/linkedlists.h"
#include "asterisk/dial.h"
#include "asterisk/pbx.h"
#include "asterisk/musiconhold.h"
#include "asterisk/app.h"

Go to the source code of this file.

Data Structures

struct  answer_exec_struct
 Structure for 'ANSWER_EXEC' option. More...
struct  ast_dial
 Main dialing structure. Contains global options, channels being dialed, and more! More...
struct  ast_dial_channel
 Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! More...
struct  ast_option_types
 Options structure - maps options to respective handlers (enable/disable). This list MUST be perfectly kept in order, or else madness will happen. More...

Defines

#define AST_MAX_WATCHERS   256
 Maximum number of channels we can watch at a time.
#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])
 Macro for finding the option structure to use on a dialed channel.
#define IS_CALLER(chan, owner)   (chan == owner ? 1 : 0)
 Macro that determines whether a channel is the caller or not.

Typedefs

typedef int(*) ast_dial_option_cb_disable (void *data)
 Typedef for dial option disable.
typedef void *(*) ast_dial_option_cb_enable (void *data)
 Typedef for dial option enable.

Functions

static int answer_exec_disable (void *data)
 Disable function for 'ANSWER_EXEC' option.
static void * answer_exec_enable (void *data)
 Enable function for 'ANSWER_EXEC' option.
static void answer_exec_run (struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
 Application execution function for 'ANSWER_EXEC' option.
ast_channelast_dial_answered (struct ast_dial *dial)
 Return channel that answered.
ast_channelast_dial_answered_steal (struct ast_dial *dial)
 Steal the channel that answered.
int ast_dial_append (struct ast_dial *dial, const char *tech, const char *device)
 Append a channel.
ast_dialast_dial_create (void)
 New dialing structure.
int ast_dial_destroy (struct ast_dial *dial)
 Destroys a dialing structure.
void ast_dial_hangup (struct ast_dial *dial)
 Hangup channels.
enum ast_dial_result ast_dial_join (struct ast_dial *dial)
 Cancel async thread.
int ast_dial_option_disable (struct ast_dial *dial, int num, enum ast_dial_option option)
 Disables an option per channel.
int ast_dial_option_enable (struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
 Enables an option per channel.
int ast_dial_option_global_disable (struct ast_dial *dial, enum ast_dial_option option)
 Disables an option globally.
int ast_dial_option_global_enable (struct ast_dial *dial, enum ast_dial_option option, void *data)
 Enables an option globally.
enum ast_dial_result ast_dial_run (struct ast_dial *dial, struct ast_channel *chan, int async)
 Execute dialing synchronously or asynchronously.
void ast_dial_set_global_timeout (struct ast_dial *dial, int timeout)
 Set the maximum time (globally) allowed for trying to ring phones.
void ast_dial_set_state_callback (struct ast_dial *dial, ast_dial_state_callback callback)
 Set a callback for state changes.
void ast_dial_set_timeout (struct ast_dial *dial, int num, int timeout)
 Set the maximum time (per channel) allowed for trying to ring the phone.
enum ast_dial_result ast_dial_state (struct ast_dial *dial)
 Return state of dial.
static void * async_dial (void *data)
 Dial async thread function.
static int begin_dial (struct ast_dial *dial, struct ast_channel *chan)
 Helper function that does the beginning dialing per dial structure.
static int begin_dial_channel (struct ast_dial_channel *channel, struct ast_channel *chan)
 Helper function that does the beginning dialing per-appended channel.
static struct ast_dial_channelfind_dial_channel (struct ast_dial *dial, int num)
 Helper function for finding a channel in a dial structure based on number.
static struct ast_dial_channelfind_relative_dial_channel (struct ast_dial *dial, struct ast_channel *owner)
 Helper function that finds the dialed channel based on owner.
static int handle_call_forward (struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_channel *chan)
 Helper function to handle channels that have been call forwarded.
static void handle_frame (struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
 Helper function that handles control frames WITH owner.
static void handle_frame_ownerless (struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr)
 Helper function that handles control frames WITHOUT owner.
static int handle_timeout_trip (struct ast_dial *dial, struct timeval start)
 Helper function to handle when a timeout occurs on dialing attempt.
static enum ast_dial_result monitor_dial (struct ast_dial *dial, struct ast_channel *chan)
 Helper function that basically keeps tabs on dialing attempts.
static int music_disable (void *data)
static void * music_enable (void *data)
static void set_state (struct ast_dial *dial, enum ast_dial_result state)

Variables

static struct ast_option_types option_types []
 Options structure - maps options to respective handlers (enable/disable). This list MUST be perfectly kept in order, or else madness will happen.


Detailed Description

Dialing API.

Author:
Joshua Colp <jcolp@digium.com>

Definition in file dial.c.


Define Documentation

#define AST_MAX_WATCHERS   256

Maximum number of channels we can watch at a time.

Definition at line 178 of file dial.c.

#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])

Macro for finding the option structure to use on a dialed channel.

Definition at line 181 of file dial.c.

Referenced by handle_call_forward(), and monitor_dial().

#define IS_CALLER ( chan,
owner   )     (chan == owner ? 1 : 0)

Macro that determines whether a channel is the caller or not.

Definition at line 184 of file dial.c.

Referenced by monitor_dial().


Typedef Documentation

typedef int(*) ast_dial_option_cb_disable(void *data)

Typedef for dial option disable.

Definition at line 72 of file dial.c.

typedef void*(*) ast_dial_option_cb_enable(void *data)

Typedef for dial option enable.

Definition at line 69 of file dial.c.


Function Documentation

static int answer_exec_disable ( void *  data  )  [static]

Disable function for 'ANSWER_EXEC' option.

Definition at line 107 of file dial.c.

References answer_exec_struct::args, and ast_free.

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 }

static void* answer_exec_enable ( void *  data  )  [static]

Enable function for 'ANSWER_EXEC' option.

Definition at line 81 of file dial.c.

References app, args, ast_calloc, ast_copy_string(), ast_strdup, ast_strdupa, and ast_strlen_zero().

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 }

static void answer_exec_run ( struct ast_dial dial,
struct ast_dial_channel dial_channel,
char *  app,
char *  args 
) [static]

Application execution function for 'ANSWER_EXEC' option.

Definition at line 141 of file dial.c.

References ast_hangup(), ast_mutex_lock, ast_mutex_unlock, AST_PTHREADT_STOP, ast_dial::lock, ast_dial_channel::owner, pbx_exec(), pbx_findapp(), and ast_dial::thread.

Referenced by monitor_dial().

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 }

struct ast_channel* ast_dial_answered ( struct ast_dial dial  ) 

Return channel that answered.

Note:
Returns the Asterisk channel that answered
Parameters:
dial Dialing structure

Definition at line 743 of file dial.c.

References AST_DIAL_RESULT_ANSWERED, AST_LIST_FIRST, ast_dial::channels, and ast_dial::state.

Referenced by dial_trunk(), and sla_handle_dial_state_event().

00744 {
00745    if (!dial)
00746       return NULL;
00747 
00748    return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
00749 }

struct ast_channel* ast_dial_answered_steal ( struct ast_dial dial  ) 

Steal the channel that answered.

Note:
Returns the Asterisk channel that answered and removes it from the dialing structure
Parameters:
dial Dialing structure

Definition at line 755 of file dial.c.

References AST_DIAL_RESULT_ANSWERED, AST_LIST_FIRST, ast_dial::channels, and ast_dial::state.

Referenced by do_notify().

00756 {
00757    struct ast_channel *chan = NULL;
00758 
00759    if (!dial)
00760       return NULL;
00761 
00762    if (dial->state == AST_DIAL_RESULT_ANSWERED) {
00763       chan = AST_LIST_FIRST(&dial->channels)->owner;
00764       AST_LIST_FIRST(&dial->channels)->owner = NULL;
00765    }
00766 
00767    return chan;
00768 }

int ast_dial_append ( struct ast_dial dial,
const char *  tech,
const char *  device 
)

Append a channel.

Note:
Appends a channel to a dialing structure
Returns:
Returns channel reference number on success, -1 on failure

Definition at line 218 of file dial.c.

References ast_atomic_fetchadd_int(), ast_calloc, AST_LIST_INSERT_TAIL, ast_strdup, ast_dial::channels, ast_dial_channel::list, and ast_dial::num.

Referenced by dial_trunk(), do_notify(), page_exec(), and sla_ring_station().

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 }

struct ast_dial* ast_dial_create ( void   ) 

New dialing structure.

Note:
Create a dialing structure
Returns:
Returns a calloc'd ast_dial structure, NULL on failure

Definition at line 190 of file dial.c.

References ast_calloc, AST_LIST_HEAD_INIT, ast_mutex_init, and AST_PTHREADT_NULL.

Referenced by dial_trunk(), do_notify(), page_exec(), and sla_ring_station().

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 }

int ast_dial_destroy ( struct ast_dial dial  ) 

Destroys a dialing structure.

Note:
Destroys (free's) the given ast_dial structure
Parameters:
dial Dialing structure to free
Returns:
Returns 0 on success, -1 on failure

Definition at line 855 of file dial.c.

References AST_DIAL_OPTION_MAX, ast_free, ast_hangup(), AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, ast_dial::channels, ast_dial_channel::device, ast_dial_channel::list, option_types, ast_dial_channel::options, ast_dial_channel::owner, and ast_dial_channel::tech.

Referenced by dial_trunk(), do_notify(), page_exec(), run_station(), sla_hangup_stations(), sla_ring_station(), and sla_stop_ringing_station().

00856 {
00857    int i = 0;
00858    struct ast_dial_channel *channel = NULL;
00859 
00860    if (!dial)
00861       return -1;
00862    
00863    /* Hangup and deallocate all the dialed channels */
00864    AST_LIST_LOCK(&dial->channels);
00865    AST_LIST_TRAVERSE_SAFE_BEGIN(&dial->channels, channel, list) {
00866       /* Disable any enabled options */
00867       for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
00868          if (!channel->options[i])
00869             continue;
00870          if (option_types[i].disable)
00871             option_types[i].disable(channel->options[i]);
00872          channel->options[i] = NULL;
00873       }
00874       /* Hang up channel if need be */
00875       if (channel->owner) {
00876          ast_hangup(channel->owner);
00877          channel->owner = NULL;
00878       }
00879       /* Free structure */
00880       ast_free(channel->tech);
00881       ast_free(channel->device);
00882       AST_LIST_REMOVE_CURRENT(list);
00883       ast_free(channel);
00884    }
00885    AST_LIST_TRAVERSE_SAFE_END;
00886    AST_LIST_UNLOCK(&dial->channels);
00887  
00888    /* Disable any enabled options globally */
00889    for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
00890       if (!dial->options[i])
00891          continue;
00892       if (option_types[i].disable)
00893          option_types[i].disable(dial->options[i]);
00894       dial->options[i] = NULL;
00895    }
00896 
00897    /* Lock be gone! */
00898    ast_mutex_destroy(&dial->lock);
00899 
00900    /* Free structure */
00901    ast_free(dial);
00902 
00903    return 0;
00904 }

void ast_dial_hangup ( struct ast_dial dial  ) 

Hangup channels.

Note:
Hangup all active channels
Parameters:
dial Dialing structure

Definition at line 831 of file dial.c.

References ast_hangup(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_dial::channels, ast_dial_channel::list, and ast_dial_channel::owner.

Referenced by ast_dial_run(), and page_exec().

00832 {
00833    struct ast_dial_channel *channel = NULL;
00834 
00835    if (!dial)
00836       return;
00837    
00838    AST_LIST_LOCK(&dial->channels);
00839    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00840       if (channel->owner) {
00841          ast_hangup(channel->owner);
00842          channel->owner = NULL;
00843       }
00844    }
00845    AST_LIST_UNLOCK(&dial->channels);
00846 
00847    return;
00848 }

enum ast_dial_result ast_dial_join ( struct ast_dial dial  ) 

Cancel async thread.

Note:
Cancel a running async thread
Parameters:
dial Dialing structure

Definition at line 783 of file dial.c.

References ast_channel_lock, ast_channel_unlock, AST_DIAL_RESULT_FAILED, AST_LIST_FIRST, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_mutex_lock, ast_mutex_unlock, AST_PTHREADT_NULL, AST_PTHREADT_STOP, ast_softhangup(), AST_SOFTHANGUP_EXPLICIT, ast_dial::channels, ast_dial::lock, ast_dial::state, and ast_dial::thread.

Referenced by dial_trunk(), page_exec(), run_station(), sla_hangup_stations(), sla_ring_station(), and sla_stop_ringing_station().

00784 {
00785    pthread_t thread;
00786 
00787    /* If the dial structure is not running in async, return failed */
00788    if (dial->thread == AST_PTHREADT_NULL)
00789       return AST_DIAL_RESULT_FAILED;
00790 
00791    /* Record thread */
00792    thread = dial->thread;
00793 
00794    /* Boom, commence locking */
00795    ast_mutex_lock(&dial->lock);
00796 
00797    /* Stop the thread */
00798    dial->thread = AST_PTHREADT_STOP;
00799 
00800    /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
00801    AST_LIST_LOCK(&dial->channels);
00802    if (AST_LIST_FIRST(&dial->channels)->is_running_app) {
00803       struct ast_channel *chan = AST_LIST_FIRST(&dial->channels)->owner;
00804       if (chan) {
00805          ast_channel_lock(chan);
00806          ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
00807          ast_channel_unlock(chan);
00808       }
00809    } else {
00810       /* Now we signal it with SIGURG so it will break out of it's waitfor */
00811       pthread_kill(thread, SIGURG);
00812    }
00813    AST_LIST_UNLOCK(&dial->channels);
00814 
00815    /* Yay done with it */
00816    ast_mutex_unlock(&dial->lock);
00817 
00818    /* Finally wait for the thread to exit */
00819    pthread_join(thread, NULL);
00820 
00821    /* Yay thread is all gone */
00822    dial->thread = AST_PTHREADT_NULL;
00823 
00824    return dial->state;
00825 }

int ast_dial_option_disable ( struct ast_dial dial,
int  num,
enum ast_dial_option  option 
)

Disables an option per channel.

Parameters:
dial Dial structure
num Channel number to disable option on
option Option to disable
Returns:
Returns 0 on success, -1 on failure

Definition at line 1007 of file dial.c.

References AST_LIST_EMPTY, ast_dial::channels, ast_option_types::disable, find_dial_channel(), option_types, and ast_dial_channel::options.

01008 {
01009    struct ast_dial_channel *channel = NULL;
01010 
01011    /* Ensure we have required arguments */
01012    if (!dial || AST_LIST_EMPTY(&dial->channels))
01013       return -1;
01014 
01015    if (!(channel = find_dial_channel(dial, num)))
01016       return -1;
01017 
01018    /* If the option is not enabled, return failure */
01019    if (!channel->options[option])
01020       return -1;
01021 
01022    /* Execute callback of option to disable it if it exists */
01023    if (option_types[option].disable)
01024       option_types[option].disable(channel->options[option]);
01025 
01026    /* Finally disable the option on the structure */
01027    channel->options[option] = NULL;
01028 
01029    return 0;
01030 }

int ast_dial_option_enable ( struct ast_dial dial,
int  num,
enum ast_dial_option  option,
void *  data 
)

Enables an option per channel.

Parameters:
dial Dial structure
num Channel number to enable option on
option Option to enable
data Data to pass to this option (not always needed)
Returns:
Returns 0 on success, -1 on failure

Definition at line 955 of file dial.c.

References AST_LIST_EMPTY, ast_dial::channels, ast_option_types::enable, find_dial_channel(), option_types, and ast_dial_channel::options.

00956 {
00957    struct ast_dial_channel *channel = NULL;
00958 
00959    /* Ensure we have required arguments */
00960    if (!dial || AST_LIST_EMPTY(&dial->channels))
00961       return -1;
00962 
00963    if (!(channel = find_dial_channel(dial, num)))
00964       return -1;
00965 
00966    /* If the option is already enabled, return failure */
00967    if (channel->options[option])
00968       return -1;
00969 
00970    /* Execute enable callback if it exists, if not simply make sure the value is set */
00971    if (option_types[option].enable)
00972       channel->options[option] = option_types[option].enable(data);
00973    else
00974       channel->options[option] = (void*)1;
00975 
00976    return 0;
00977 }

int ast_dial_option_global_disable ( struct ast_dial dial,
enum ast_dial_option  option 
)

Disables an option globally.

Parameters:
dial Dial structure to disable option on
option Option to disable
Returns:
Returns 0 on success, -1 on failure

Definition at line 984 of file dial.c.

References ast_option_types::disable, option_types, and ast_dial::options.

00985 {
00986    /* If the option is not enabled, return failure */
00987    if (!dial->options[option]) {
00988       return -1;
00989    }
00990 
00991    /* Execute callback of option to disable if it exists */
00992    if (option_types[option].disable)
00993       option_types[option].disable(dial->options[option]);
00994 
00995    /* Finally disable option on the structure */
00996    dial->options[option] = NULL;
00997 
00998    return 0;
00999 }

int ast_dial_option_global_enable ( struct ast_dial dial,
enum ast_dial_option  option,
void *  data 
)

Enables an option globally.

Parameters:
dial Dial structure to enable option on
option Option to enable
data Data to pass to this option (not always needed)
Returns:
Returns 0 on success, -1 on failure

Definition at line 912 of file dial.c.

References ast_option_types::enable, option_types, and ast_dial::options.

Referenced by do_notify(), and page_exec().

00913 {
00914    /* If the option is already enabled, return failure */
00915    if (dial->options[option])
00916       return -1;
00917 
00918    /* Execute enable callback if it exists, if not simply make sure the value is set */
00919    if (option_types[option].enable)
00920       dial->options[option] = option_types[option].enable(data);
00921    else
00922       dial->options[option] = (void*)1;
00923 
00924    return 0;
00925 }

enum ast_dial_result ast_dial_run ( struct ast_dial dial,
struct ast_channel chan,
int  async 
)

Execute dialing synchronously or asynchronously.

Note:
Dials channels in a dial structure.
Returns:
Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).

Definition at line 703 of file dial.c.

References ast_debug, ast_dial_hangup(), AST_DIAL_RESULT_FAILED, AST_DIAL_RESULT_INVALID, AST_DIAL_RESULT_TRYING, AST_LIST_EMPTY, ast_pthread_create, async_dial(), begin_dial(), ast_dial::channels, monitor_dial(), ast_dial::state, and ast_dial::thread.

Referenced by dial_trunk(), do_notify(), page_exec(), and sla_ring_station().

00704 {
00705    enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
00706 
00707    /* Ensure required arguments are passed */
00708    if (!dial || (!chan && !async)) {
00709       ast_debug(1, "invalid #1\n");
00710       return AST_DIAL_RESULT_INVALID;
00711    }
00712 
00713    /* If there are no channels to dial we can't very well try to dial them */
00714    if (AST_LIST_EMPTY(&dial->channels)) {
00715       ast_debug(1, "invalid #2\n");
00716       return AST_DIAL_RESULT_INVALID;
00717    }
00718 
00719    /* Dial each requested channel */
00720    if (!begin_dial(dial, chan))
00721       return AST_DIAL_RESULT_FAILED;
00722 
00723    /* If we are running async spawn a thread and send it away... otherwise block here */
00724    if (async) {
00725       dial->state = AST_DIAL_RESULT_TRYING;
00726       /* Try to create a thread */
00727       if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
00728          /* Failed to create the thread - hangup all dialed channels and return failed */
00729          ast_dial_hangup(dial);
00730          res = AST_DIAL_RESULT_FAILED;
00731       }
00732    } else {
00733       res = monitor_dial(dial, chan);
00734    }
00735 
00736    return res;
00737 }

void ast_dial_set_global_timeout ( struct ast_dial dial,
int  timeout 
)

Set the maximum time (globally) allowed for trying to ring phones.

Parameters:
dial The dial structure to apply the time limit to
timeout Maximum time allowed
Returns:
nothing

Definition at line 1042 of file dial.c.

References ast_dial::actual_timeout, and ast_dial::timeout.

Referenced by do_notify(), and page_exec().

01043 {
01044    dial->timeout = timeout;
01045 
01046    if (dial->timeout > 0 && (dial->actual_timeout > dial->timeout || dial->actual_timeout == -1))
01047       dial->actual_timeout = dial->timeout;
01048 
01049    return;
01050 }

void ast_dial_set_state_callback ( struct ast_dial dial,
ast_dial_state_callback  callback 
)

Set a callback for state changes.

Parameters:
dial The dial structure to watch for state changes
callback the callback
Returns:
nothing

Definition at line 1032 of file dial.c.

References ast_dial::state_callback.

Referenced by sla_ring_station().

01033 {
01034    dial->state_callback = callback;
01035 }

void ast_dial_set_timeout ( struct ast_dial dial,
int  num,
int  timeout 
)

Set the maximum time (per channel) allowed for trying to ring the phone.

Parameters:
dial The dial structure the channel belongs to
num Channel number to set timeout on
timeout Maximum time allowed
Returns:
nothing

Definition at line 1058 of file dial.c.

References ast_dial::actual_timeout, find_dial_channel(), and ast_dial_channel::timeout.

01059 {
01060    struct ast_dial_channel *channel = NULL;
01061 
01062    if (!(channel = find_dial_channel(dial, num)))
01063       return;
01064 
01065    channel->timeout = timeout;
01066 
01067    if (channel->timeout > 0 && (dial->actual_timeout > channel->timeout || dial->actual_timeout == -1))
01068       dial->actual_timeout = channel->timeout;
01069 
01070    return;
01071 }

enum ast_dial_result ast_dial_state ( struct ast_dial dial  ) 

Return state of dial.

Note:
Returns the state of the dial attempt
Parameters:
dial Dialing structure

Definition at line 774 of file dial.c.

References ast_dial::state.

Referenced by dial_trunk(), and sla_handle_dial_state_event().

00775 {
00776    return dial->state;
00777 }

static void* async_dial ( void *  data  )  [static]

Dial async thread function.

Definition at line 689 of file dial.c.

References monitor_dial().

Referenced by ast_dial_run().

00690 {
00691    struct ast_dial *dial = data;
00692 
00693    /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
00694    monitor_dial(dial, NULL);
00695 
00696    return NULL;
00697 }

static int begin_dial ( struct ast_dial dial,
struct ast_channel chan 
) [static]

Helper function that does the beginning dialing per dial structure.

Definition at line 300 of file dial.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, begin_dial_channel(), ast_dial::channels, and ast_dial_channel::list.

Referenced by ast_dial_run().

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 }

static int begin_dial_channel ( struct ast_dial_channel channel,
struct ast_channel chan 
) [static]

Helper function that does the beginning dialing per-appended channel.

Definition at line 247 of file dial.c.

References accountcode, ast_channel::adsicpe, ast_channel::appl, ast_call(), ast_channel_datastore_inherit(), ast_channel_inherit_variables(), ast_connected_line_copy_from_caller(), ast_copy_string(), AST_FORMAT_AUDIO_MASK, ast_hangup(), AST_MAX_EXTENSION, ast_party_redirecting_copy(), ast_poll_channel_add(), ast_request(), ast_string_field_set, ast_strlen_zero(), ast_verb, ast_dial_channel::cause, ast_channel::connected, ast_channel::data, ast_dial_channel::device, ast_channel::dialed, language, musicclass, ast_channel::musicclass, ast_channel::nativeformats, ast_dial_channel::owner, ast_channel::redirecting, ast_dial_channel::tech, ast_channel::transfercapability, ast_party_dialed::transit_network_select, and ast_channel::whentohangup.

Referenced by begin_dial(), and handle_call_forward().

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 }

static struct ast_dial_channel* find_dial_channel ( struct ast_dial dial,
int  num 
) [static]

Helper function for finding a channel in a dial structure based on number.

Definition at line 929 of file dial.c.

References AST_LIST_LAST, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_dial::channels, ast_dial_channel::list, and ast_dial_channel::num.

Referenced by ast_dial_option_disable(), ast_dial_option_enable(), and ast_dial_set_timeout().

00930 {
00931    struct ast_dial_channel *channel = AST_LIST_LAST(&dial->channels);
00932 
00933    /* We can try to predict programmer behavior, the last channel they added is probably the one they wanted to modify */
00934    if (channel->num == num)
00935       return channel;
00936 
00937    /* Hrm not at the end... looking through the list it is! */
00938    AST_LIST_LOCK(&dial->channels);
00939    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00940       if (channel->num == num)
00941          break;
00942    }
00943    AST_LIST_UNLOCK(&dial->channels);
00944    
00945    return channel;
00946 }

static struct ast_dial_channel* find_relative_dial_channel ( struct ast_dial dial,
struct ast_channel owner 
) [static]

Helper function that finds the dialed channel based on owner.

Definition at line 356 of file dial.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_dial::channels, ast_dial_channel::list, and ast_dial_channel::owner.

Referenced by monitor_dial().

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 }

static int handle_call_forward ( struct ast_dial dial,
struct ast_dial_channel channel,
struct ast_channel chan 
) [static]

Helper function to handle channels that have been call forwarded.

Definition at line 317 of file dial.c.

References AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, ast_free, ast_hangup(), AST_LIST_UNLOCK, ast_strdup, ast_strdupa, begin_dial_channel(), ast_channel::call_forward, ast_dial::channels, ast_dial_channel::device, FIND_RELATIVE_OPTION, ast_dial_channel::owner, and ast_dial_channel::tech.

Referenced by monitor_dial().

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 }

static void handle_frame ( struct ast_dial dial,
struct ast_dial_channel channel,
struct ast_frame fr,
struct ast_channel chan 
) [static]

Helper function that handles control frames WITH owner.

Definition at line 379 of file dial.c.

References ast_channel_connected_line_macro(), ast_channel_redirecting_macro(), AST_CONTROL_ANSWER, AST_CONTROL_BUSY, AST_CONTROL_CONGESTION, AST_CONTROL_CONNECTED_LINE, AST_CONTROL_FLASH, AST_CONTROL_HOLD, AST_CONTROL_INCOMPLETE, AST_CONTROL_OFFHOOK, AST_CONTROL_PROCEEDING, AST_CONTROL_PROGRESS, AST_CONTROL_REDIRECTING, AST_CONTROL_RINGING, AST_CONTROL_SRCUPDATE, AST_CONTROL_UNHOLD, AST_CONTROL_VIDUPDATE, AST_DIAL_OPTION_MUSIC, AST_DIAL_RESULT_ANSWERED, AST_DIAL_RESULT_PROCEEDING, AST_DIAL_RESULT_PROGRESS, AST_DIAL_RESULT_RINGING, AST_FRAME_CONTROL, ast_hangup(), ast_indicate(), ast_indicate_data(), AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, ast_verb, ast_verbose, ast_dial::channels, ast_frame::data, ast_frame::datalen, ast_channel::exten, ast_frame::frametype, ast_frame_subclass::integer, ast_dial_channel::list, ast_channel::name, option_verbose, ast_dial::options, ast_dial_channel::owner, ast_frame::ptr, set_state(), ast_frame::subclass, and VERBOSE_PREFIX_3.

Referenced by monitor_dial(), and socket_read().

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_INCOMPLETE:
00402          ast_verb(3, "%s dialed Incomplete extension %s\n", channel->owner->name, channel->owner->exten);
00403          ast_indicate(chan, AST_CONTROL_INCOMPLETE);
00404          break;
00405       case AST_CONTROL_RINGING:
00406          ast_verb(3, "%s is ringing\n", channel->owner->name);
00407          if (!dial->options[AST_DIAL_OPTION_MUSIC])
00408             ast_indicate(chan, AST_CONTROL_RINGING);
00409          set_state(dial, AST_DIAL_RESULT_RINGING);
00410          break;
00411       case AST_CONTROL_PROGRESS:
00412          ast_verb(3, "%s is making progress, passing it to %s\n", channel->owner->name, chan->name);
00413          ast_indicate(chan, AST_CONTROL_PROGRESS);
00414          set_state(dial, AST_DIAL_RESULT_PROGRESS);
00415          break;
00416       case AST_CONTROL_VIDUPDATE:
00417          ast_verb(3, "%s requested a video update, passing it to %s\n", channel->owner->name, chan->name);
00418          ast_indicate(chan, AST_CONTROL_VIDUPDATE);
00419          break;
00420       case AST_CONTROL_SRCUPDATE:
00421          if (option_verbose > 2)
00422             ast_verbose (VERBOSE_PREFIX_3 "%s requested a source update, passing it to %s\n", channel->owner->name, chan->name);
00423          ast_indicate(chan, AST_CONTROL_SRCUPDATE);
00424          break;
00425       case AST_CONTROL_CONNECTED_LINE:
00426          ast_verb(3, "%s connected line has changed, passing it to %s\n", channel->owner->name, chan->name);
00427          if (ast_channel_connected_line_macro(channel->owner, chan, fr, 1, 1)) {
00428             ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, fr->data.ptr, fr->datalen);
00429          }
00430          break;
00431       case AST_CONTROL_REDIRECTING:
00432          ast_verb(3, "%s redirecting info has changed, passing it to %s\n", channel->owner->name, chan->name);
00433          if (ast_channel_redirecting_macro(channel->owner, chan, fr, 1, 1)) {
00434             ast_indicate_data(chan, AST_CONTROL_REDIRECTING, fr->data.ptr, fr->datalen);
00435          }
00436          break;
00437       case AST_CONTROL_PROCEEDING:
00438          ast_verb(3, "%s is proceeding, passing it to %s\n", channel->owner->name, chan->name);
00439          ast_indicate(chan, AST_CONTROL_PROCEEDING);
00440          set_state(dial, AST_DIAL_RESULT_PROCEEDING);
00441          break;
00442       case AST_CONTROL_HOLD:
00443          ast_verb(3, "Call on %s placed on hold\n", chan->name);
00444          ast_indicate(chan, AST_CONTROL_HOLD);
00445          break;
00446       case AST_CONTROL_UNHOLD:
00447          ast_verb(3, "Call on %s left from hold\n", chan->name);
00448          ast_indicate(chan, AST_CONTROL_UNHOLD);
00449          break;
00450       case AST_CONTROL_OFFHOOK:
00451       case AST_CONTROL_FLASH:
00452          break;
00453       case -1:
00454          /* Prod the channel */
00455          ast_indicate(chan, -1);
00456          break;
00457       default:
00458          break;
00459       }
00460    }
00461 
00462    return;
00463 }

static void handle_frame_ownerless ( struct ast_dial dial,
struct ast_dial_channel channel,
struct ast_frame fr 
) [static]

Helper function that handles control frames WITHOUT owner.

Definition at line 466 of file dial.c.

References AST_CONTROL_ANSWER, AST_CONTROL_BUSY, AST_CONTROL_CONGESTION, AST_CONTROL_PROCEEDING, AST_CONTROL_PROGRESS, AST_CONTROL_RINGING, AST_DIAL_RESULT_ANSWERED, AST_DIAL_RESULT_PROCEEDING, AST_DIAL_RESULT_PROGRESS, AST_DIAL_RESULT_RINGING, AST_FRAME_CONTROL, ast_hangup(), AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, ast_verb, ast_dial::channels, ast_frame::frametype, ast_frame_subclass::integer, ast_dial_channel::list, ast_channel::name, ast_dial_channel::owner, set_state(), and ast_frame::subclass.

Referenced by monitor_dial().

00467 {
00468    /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
00469    if (fr->frametype != AST_FRAME_CONTROL)
00470       return;
00471 
00472    switch (fr->subclass.integer) {
00473    case AST_CONTROL_ANSWER:
00474       ast_verb(3, "%s answered\n", channel->owner->name);
00475       AST_LIST_LOCK(&dial->channels);
00476       AST_LIST_REMOVE(&dial->channels, channel, list);
00477       AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
00478       AST_LIST_UNLOCK(&dial->channels);
00479       set_state(dial, AST_DIAL_RESULT_ANSWERED);
00480       break;
00481    case AST_CONTROL_BUSY:
00482       ast_verb(3, "%s is busy\n", channel->owner->name);
00483       ast_hangup(channel->owner);
00484       channel->owner = NULL;
00485       break;
00486    case AST_CONTROL_CONGESTION:
00487       ast_verb(3, "%s is circuit-busy\n", channel->owner->name);
00488       ast_hangup(channel->owner);
00489       channel->owner = NULL;
00490       break;
00491    case AST_CONTROL_RINGING:
00492       ast_verb(3, "%s is ringing\n", channel->owner->name);
00493       set_state(dial, AST_DIAL_RESULT_RINGING);
00494       break;
00495    case AST_CONTROL_PROGRESS:
00496       ast_verb(3, "%s is making progress\n", channel->owner->name);
00497       set_state(dial, AST_DIAL_RESULT_PROGRESS);
00498       break;
00499    case AST_CONTROL_PROCEEDING:
00500       ast_verb(3, "%s is proceeding\n", channel->owner->name);
00501       set_state(dial, AST_DIAL_RESULT_PROCEEDING);
00502       break;
00503    default:
00504       break;
00505    }
00506 
00507    return;
00508 }

static int handle_timeout_trip ( struct ast_dial dial,
struct timeval  start 
) [static]

Helper function to handle when a timeout occurs on dialing attempt.

Definition at line 511 of file dial.c.

References AST_DIAL_RESULT_TIMEOUT, ast_hangup(), AST_LIST_TRAVERSE, ast_tvdiff_ms(), ast_tvnow(), ast_dial::channels, ast_dial_channel::list, ast_dial_channel::owner, set_state(), ast_dial::state, ast_dial_channel::timeout, and ast_dial::timeout.

Referenced by monitor_dial().

00512 {
00513    struct ast_dial_channel *channel = NULL;
00514    int diff = ast_tvdiff_ms(ast_tvnow(), start), lowest_timeout = -1, new_timeout = -1;
00515 
00516    /* If the global dial timeout tripped switch the state to timeout so our channel loop will drop every channel */
00517    if (diff >= dial->timeout) {
00518       set_state(dial, AST_DIAL_RESULT_TIMEOUT);
00519       new_timeout = 0;
00520    }
00521 
00522    /* Go through dropping out channels that have met their timeout */
00523    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00524       if (dial->state == AST_DIAL_RESULT_TIMEOUT || diff >= channel->timeout) {
00525          ast_hangup(channel->owner);
00526          channel->owner = NULL;
00527       } else if ((lowest_timeout == -1) || (lowest_timeout > channel->timeout)) {
00528          lowest_timeout = channel->timeout;
00529       }
00530    }
00531 
00532    /* Calculate the new timeout using the lowest timeout found */
00533    if (lowest_timeout >= 0)
00534       new_timeout = lowest_timeout - diff;
00535 
00536    return new_timeout;
00537 }

static enum ast_dial_result monitor_dial ( struct ast_dial dial,
struct ast_channel chan 
) [static]

Helper function that basically keeps tabs on dialing attempts.

Definition at line 540 of file dial.c.

References ast_dial::actual_timeout, answer_exec_run(), answer_exec_struct::app, answer_exec_struct::args, AST_CONTROL_RINGING, AST_DIAL_OPTION_ANSWER_EXEC, AST_DIAL_OPTION_MUSIC, AST_DIAL_OPTION_RINGING, AST_DIAL_RESULT_ANSWERED, AST_DIAL_RESULT_HANGUP, AST_DIAL_RESULT_RINGING, AST_DIAL_RESULT_TIMEOUT, AST_DIAL_RESULT_TRYING, AST_DIAL_RESULT_UNANSWERED, ast_frfree, ast_hangup(), ast_indicate(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, AST_MAX_WATCHERS, ast_moh_start(), ast_moh_stop(), ast_poll_channel_del(), AST_PTHREADT_STOP, ast_read(), ast_strdupa, ast_string_field_set, ast_strlen_zero(), ast_tvnow(), ast_waitfor_n(), ast_channel::call_forward, ast_dial::channels, find_relative_dial_channel(), FIND_RELATIVE_OPTION, handle_call_forward(), handle_frame(), handle_frame_ownerless(), handle_timeout_trip(), IS_CALLER, ast_dial_channel::is_running_app, musicclass, ast_channel::musicclass, ast_dial::options, ast_dial_channel::owner, set_state(), ast_dial::state, and ast_dial::thread.

Referenced by ast_dial_run(), and async_dial().

00541 {
00542    int timeout = -1;
00543    struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
00544    struct ast_dial_channel *channel = NULL;
00545    struct answer_exec_struct *answer_exec = NULL;
00546    struct timeval start;
00547 
00548    set_state(dial, AST_DIAL_RESULT_TRYING);
00549 
00550    /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
00551    if (dial->options[AST_DIAL_OPTION_RINGING]) {
00552       set_state(dial, AST_DIAL_RESULT_RINGING);
00553       if (chan)
00554          ast_indicate(chan, AST_CONTROL_RINGING);
00555    } else if (chan && dial->options[AST_DIAL_OPTION_MUSIC] && 
00556       !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
00557       char *original_moh = ast_strdupa(chan->musicclass);
00558       ast_indicate(chan, -1);
00559       ast_string_field_set(chan, musicclass, dial->options[AST_DIAL_OPTION_MUSIC]);
00560       ast_moh_start(chan, dial->options[AST_DIAL_OPTION_MUSIC], NULL);
00561       ast_string_field_set(chan, musicclass, original_moh);
00562    }
00563 
00564    /* Record start time for timeout purposes */
00565    start = ast_tvnow();
00566 
00567    /* We actually figured out the maximum timeout we can do as they were added, so we can directly access the info */
00568    timeout = dial->actual_timeout;
00569 
00570    /* Go into an infinite loop while we are trying */
00571    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)) {
00572       int pos = 0, count = 0;
00573       struct ast_frame *fr = NULL;
00574 
00575       /* Set up channel structure array */
00576       pos = count = 0;
00577       if (chan)
00578          cs[pos++] = chan;
00579 
00580       /* Add channels we are attempting to dial */
00581       AST_LIST_LOCK(&dial->channels);
00582       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00583          if (channel->owner) {
00584             cs[pos++] = channel->owner;
00585             count++;
00586          }
00587       }
00588       AST_LIST_UNLOCK(&dial->channels);
00589 
00590       /* If we have no outbound channels in progress, switch state to unanswered and stop */
00591       if (!count) {
00592          set_state(dial, AST_DIAL_RESULT_UNANSWERED);
00593          break;
00594       }
00595 
00596       /* Just to be safe... */
00597       if (dial->thread == AST_PTHREADT_STOP)
00598          break;
00599 
00600       /* Wait for frames from channels */
00601       who = ast_waitfor_n(cs, pos, &timeout);
00602 
00603       /* Check to see if our thread is being cancelled */
00604       if (dial->thread == AST_PTHREADT_STOP)
00605          break;
00606 
00607       /* If the timeout no longer exists OR if we got no channel it basically means the timeout was tripped, so handle it */
00608       if (!timeout || !who) {
00609          timeout = handle_timeout_trip(dial, start);
00610          continue;
00611       }
00612 
00613       /* Find relative dial channel */
00614       if (!chan || !IS_CALLER(chan, who))
00615          channel = find_relative_dial_channel(dial, who);
00616 
00617       /* See if this channel has been forwarded elsewhere */
00618       if (!ast_strlen_zero(who->call_forward)) {
00619          handle_call_forward(dial, channel, chan);
00620          continue;
00621       }
00622 
00623       /* Attempt to read in a frame */
00624       if (!(fr = ast_read(who))) {
00625          /* If this is the caller then we switch state to hangup and stop */
00626          if (chan && IS_CALLER(chan, who)) {
00627             set_state(dial, AST_DIAL_RESULT_HANGUP);
00628             break;
00629          }
00630          if (chan)
00631             ast_poll_channel_del(chan, channel->owner);
00632          ast_hangup(who);
00633          channel->owner = NULL;
00634          continue;
00635       }
00636 
00637       /* Process the frame */
00638       if (chan)
00639          handle_frame(dial, channel, fr, chan);
00640       else
00641          handle_frame_ownerless(dial, channel, fr);
00642 
00643       /* Free the received frame and start all over */
00644       ast_frfree(fr);
00645    }
00646 
00647    /* Do post-processing from loop */
00648    if (dial->state == AST_DIAL_RESULT_ANSWERED) {
00649       /* Hangup everything except that which answered */
00650       AST_LIST_LOCK(&dial->channels);
00651       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00652          if (!channel->owner || channel->owner == who)
00653             continue;
00654          if (chan)
00655             ast_poll_channel_del(chan, channel->owner);
00656          ast_hangup(channel->owner);
00657          channel->owner = NULL;
00658       }
00659       AST_LIST_UNLOCK(&dial->channels);
00660       /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
00661       if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC))) {
00662          channel->is_running_app = 1;
00663          answer_exec_run(dial, channel, answer_exec->app, answer_exec->args);
00664          channel->is_running_app = 0;
00665       }
00666 
00667       if (chan && dial->options[AST_DIAL_OPTION_MUSIC] && 
00668          !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
00669          ast_moh_stop(chan);
00670       }
00671    } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
00672       /* Hangup everything */
00673       AST_LIST_LOCK(&dial->channels);
00674       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00675          if (!channel->owner)
00676             continue;
00677          if (chan)
00678             ast_poll_channel_del(chan, channel->owner);
00679          ast_hangup(channel->owner);
00680          channel->owner = NULL;
00681       }
00682       AST_LIST_UNLOCK(&dial->channels);
00683    }
00684 
00685    return dial->state;
00686 }

static int music_disable ( void *  data  )  [static]

Definition at line 130 of file dial.c.

References ast_free.

00131 {
00132    if (!data)
00133       return -1;
00134 
00135    ast_free(data);
00136 
00137    return 0;
00138 }

static void* music_enable ( void *  data  )  [static]

Definition at line 125 of file dial.c.

References ast_strdup.

00126 {
00127    return ast_strdup(data);
00128 }

static void set_state ( struct ast_dial dial,
enum ast_dial_result  state 
) [static]

Definition at line 370 of file dial.c.

References ast_dial::state, and ast_dial::state_callback.

00371 {
00372    dial->state = state;
00373 
00374    if (dial->state_callback)
00375       dial->state_callback(dial);
00376 }


Variable Documentation

struct ast_option_types option_types[] [static]

Options structure - maps options to respective handlers (enable/disable). This list MUST be perfectly kept in order, or else madness will happen.

Referenced by ast_dial_destroy(), ast_dial_option_disable(), ast_dial_option_enable(), ast_dial_option_global_disable(), and ast_dial_option_global_enable().


Generated on Sat Mar 10 01:55:20 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7