Thu Apr 16 06:46:28 2015

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"

Include dependency graph for dial.c:

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

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.
struct ast_channelast_dial_answered (struct ast_dial *dial)
 Return channel that answered.
struct 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.
struct 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 []
 Map options to respective handlers (enable/disable).


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 189 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 192 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 195 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 76 of file dial.c.

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

Typedef for dial option enable.

Definition at line 73 of file dial.c.


Function Documentation

static int answer_exec_disable ( void *  data  )  [static]

Disable function for 'ANSWER_EXEC' option.

Definition at line 111 of file dial.c.

References answer_exec_struct::args, and ast_free.

00112 {
00113    struct answer_exec_struct *answer_exec = data;
00114 
00115    /* Make sure we have a value */
00116    if (!answer_exec)
00117       return -1;
00118 
00119    /* If arguments are present, free them too */
00120    if (answer_exec->args)
00121       ast_free(answer_exec->args);
00122 
00123    /* This is simple - just free the structure */
00124    ast_free(answer_exec);
00125 
00126    return 0;
00127 }

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

Enable function for 'ANSWER_EXEC' option.

Definition at line 85 of file dial.c.

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

00086 {
00087    struct answer_exec_struct *answer_exec = NULL;
00088    char *app = ast_strdupa((char*)data), *args = NULL;
00089 
00090    /* Not giving any data to this option is bad, mmmk? */
00091    if (ast_strlen_zero(app))
00092       return NULL;
00093 
00094    /* Create new data structure */
00095    if (!(answer_exec = ast_calloc(1, sizeof(*answer_exec))))
00096       return NULL;
00097    
00098    /* Parse out application and arguments */
00099    if ((args = strchr(app, ','))) {
00100       *args++ = '\0';
00101       answer_exec->args = ast_strdup(args);
00102    }
00103 
00104    /* Copy application name */
00105    ast_copy_string(answer_exec->app, app, sizeof(answer_exec->app));
00106 
00107    return answer_exec;
00108 }

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 145 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().

00146 {
00147    struct ast_channel *chan = dial_channel->owner;
00148    struct ast_app *ast_app = pbx_findapp(app);
00149 
00150    /* If the application was not found, return immediately */
00151    if (!ast_app)
00152       return;
00153 
00154    /* All is well... execute the application */
00155    pbx_exec(chan, ast_app, args);
00156 
00157    /* If another thread is not taking over hang up the channel */
00158    ast_mutex_lock(&dial->lock);
00159    if (dial->thread != AST_PTHREADT_STOP) {
00160       ast_hangup(chan);
00161       dial_channel->owner = NULL;
00162    }
00163    ast_mutex_unlock(&dial->lock);
00164 
00165    return;
00166 }

struct ast_channel* ast_dial_answered ( struct ast_dial dial  )  [read]

Return channel that answered.

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

Definition at line 754 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().

00755 {
00756    if (!dial)
00757       return NULL;
00758 
00759    return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
00760 }

struct ast_channel* ast_dial_answered_steal ( struct ast_dial dial  )  [read]

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 766 of file dial.c.

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

Referenced by do_notify().

00767 {
00768    struct ast_channel *chan = NULL;
00769 
00770    if (!dial)
00771       return NULL;
00772 
00773    if (dial->state == AST_DIAL_RESULT_ANSWERED) {
00774       chan = AST_LIST_FIRST(&dial->channels)->owner;
00775       AST_LIST_FIRST(&dial->channels)->owner = NULL;
00776    }
00777 
00778    return chan;
00779 }

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 229 of file dial.c.

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

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

00230 {
00231    struct ast_dial_channel *channel = NULL;
00232 
00233    /* Make sure we have required arguments */
00234    if (!dial || !tech || !device)
00235       return -1;
00236 
00237    /* Allocate new memory for dialed channel structure */
00238    if (!(channel = ast_calloc(1, sizeof(*channel))))
00239       return -1;
00240 
00241    /* Record technology and device for when we actually dial */
00242    channel->tech = ast_strdup(tech);
00243    channel->device = ast_strdup(device);
00244 
00245    /* Grab reference number from dial structure */
00246    channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
00247 
00248    /* No timeout exists... yet */
00249    channel->timeout = -1;
00250 
00251    /* Insert into channels list */
00252    AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
00253 
00254    return channel->num;
00255 }

struct ast_dial* ast_dial_create ( void   )  [read]

New dialing structure.

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

Definition at line 201 of file dial.c.

References ast_dial::actual_timeout, ast_calloc, AST_LIST_HEAD_INIT, ast_mutex_init, AST_PTHREADT_NULL, ast_dial::channels, ast_dial::lock, ast_dial::thread, and ast_dial::timeout.

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

00202 {
00203    struct ast_dial *dial = NULL;
00204 
00205    /* Allocate new memory for structure */
00206    if (!(dial = ast_calloc(1, sizeof(*dial))))
00207       return NULL;
00208 
00209    /* Initialize list of channels */
00210    AST_LIST_HEAD_INIT(&dial->channels);
00211 
00212    /* Initialize thread to NULL */
00213    dial->thread = AST_PTHREADT_NULL;
00214 
00215    /* No timeout exists... yet */
00216    dial->timeout = -1;
00217    dial->actual_timeout = -1;
00218 
00219    /* Can't forget about the lock */
00220    ast_mutex_init(&dial->lock);
00221 
00222    return dial;
00223 }

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 866 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_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_mutex_destroy, ast_dial::channels, ast_dial_channel::device, ast_option_types::disable, ast_dial_channel::list, ast_dial::lock, ast_dial::options, ast_dial_channel::options, ast_dial_channel::owner, and ast_dial_channel::tech.

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

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

void ast_dial_hangup ( struct ast_dial dial  ) 

Hangup channels.

Note:
Hangup all active channels
Parameters:
dial Dialing structure

Definition at line 842 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().

00843 {
00844    struct ast_dial_channel *channel = NULL;
00845 
00846    if (!dial)
00847       return;
00848    
00849    AST_LIST_LOCK(&dial->channels);
00850    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00851       if (channel->owner) {
00852          ast_hangup(channel->owner);
00853          channel->owner = NULL;
00854       }
00855    }
00856    AST_LIST_UNLOCK(&dial->channels);
00857 
00858    return;
00859 }

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 794 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_handle_dial_state_event(), sla_hangup_stations(), sla_ring_station(), and sla_stop_ringing_station().

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

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 1018 of file dial.c.

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

01019 {
01020    struct ast_dial_channel *channel = NULL;
01021 
01022    /* Ensure we have required arguments */
01023    if (!dial || AST_LIST_EMPTY(&dial->channels))
01024       return -1;
01025 
01026    if (!(channel = find_dial_channel(dial, num)))
01027       return -1;
01028 
01029    /* If the option is not enabled, return failure */
01030    if (!channel->options[option])
01031       return -1;
01032 
01033    /* Execute callback of option to disable it if it exists */
01034    if (option_types[option].disable)
01035       option_types[option].disable(channel->options[option]);
01036 
01037    /* Finally disable the option on the structure */
01038    channel->options[option] = NULL;
01039 
01040    return 0;
01041 }

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 966 of file dial.c.

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

00967 {
00968    struct ast_dial_channel *channel = NULL;
00969 
00970    /* Ensure we have required arguments */
00971    if (!dial || AST_LIST_EMPTY(&dial->channels))
00972       return -1;
00973 
00974    if (!(channel = find_dial_channel(dial, num)))
00975       return -1;
00976 
00977    /* If the option is already enabled, return failure */
00978    if (channel->options[option])
00979       return -1;
00980 
00981    /* Execute enable callback if it exists, if not simply make sure the value is set */
00982    if (option_types[option].enable)
00983       channel->options[option] = option_types[option].enable(data);
00984    else
00985       channel->options[option] = (void*)1;
00986 
00987    return 0;
00988 }

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 995 of file dial.c.

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

00996 {
00997    /* If the option is not enabled, return failure */
00998    if (!dial->options[option]) {
00999       return -1;
01000    }
01001 
01002    /* Execute callback of option to disable if it exists */
01003    if (option_types[option].disable)
01004       option_types[option].disable(dial->options[option]);
01005 
01006    /* Finally disable option on the structure */
01007    dial->options[option] = NULL;
01008 
01009    return 0;
01010 }

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 923 of file dial.c.

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

Referenced by do_notify(), and page_exec().

00924 {
00925    /* If the option is already enabled, return failure */
00926    if (dial->options[option])
00927       return -1;
00928 
00929    /* Execute enable callback if it exists, if not simply make sure the value is set */
00930    if (option_types[option].enable)
00931       dial->options[option] = option_types[option].enable(data);
00932    else
00933       dial->options[option] = (void*)1;
00934 
00935    return 0;
00936 }

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 714 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().

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

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 1053 of file dial.c.

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

Referenced by do_notify(), and page_exec().

01054 {
01055    dial->timeout = timeout;
01056 
01057    if (dial->timeout > 0 && (dial->actual_timeout > dial->timeout || dial->actual_timeout == -1))
01058       dial->actual_timeout = dial->timeout;
01059 
01060    return;
01061 }

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 1043 of file dial.c.

References ast_dial::state_callback.

Referenced by sla_ring_station().

01044 {
01045    dial->state_callback = callback;
01046 }

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 1069 of file dial.c.

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

01070 {
01071    struct ast_dial_channel *channel = NULL;
01072 
01073    if (!(channel = find_dial_channel(dial, num)))
01074       return;
01075 
01076    channel->timeout = timeout;
01077 
01078    if (channel->timeout > 0 && (dial->actual_timeout > channel->timeout || dial->actual_timeout == -1))
01079       dial->actual_timeout = channel->timeout;
01080 
01081    return;
01082 }

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 785 of file dial.c.

References ast_dial::state.

Referenced by dial_trunk(), and sla_handle_dial_state_event().

00786 {
00787    return dial->state;
00788 }

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

Dial async thread function.

Definition at line 700 of file dial.c.

References monitor_dial().

Referenced by ast_dial_run().

00701 {
00702    struct ast_dial *dial = data;
00703 
00704    /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
00705    monitor_dial(dial, NULL);
00706 
00707    return NULL;
00708 }

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 311 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().

00312 {
00313    struct ast_dial_channel *channel = NULL;
00314    int success = 0;
00315 
00316    /* Iterate through channel list, requesting and calling each one */
00317    AST_LIST_LOCK(&dial->channels);
00318    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00319       success += begin_dial_channel(channel, chan);
00320    }
00321    AST_LIST_UNLOCK(&dial->channels);
00322 
00323    /* If number of failures matches the number of channels, then this truly failed */
00324    return success;
00325 }

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 258 of file dial.c.

References ast_channel::accountcode, 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_channel::caller, ast_dial_channel::cause, ast_channel::connected, ast_channel::data, ast_dial_channel::device, ast_channel::dialed, ast_channel::language, 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().

00259 {
00260    char numsubst[AST_MAX_EXTENSION];
00261    int res = 1;
00262 
00263    /* Copy device string over */
00264    ast_copy_string(numsubst, channel->device, sizeof(numsubst));
00265 
00266    /* If we fail to create our owner channel bail out */
00267    if (!(channel->owner = ast_request(channel->tech, chan ? chan->nativeformats : AST_FORMAT_AUDIO_MASK, chan, numsubst, &channel->cause)))
00268       return -1;
00269 
00270    channel->owner->appl = "AppDial2";
00271    channel->owner->data = "(Outgoing Line)";
00272    memset(&channel->owner->whentohangup, 0, sizeof(channel->owner->whentohangup));
00273 
00274    /* Inherit everything from he who spawned this dial */
00275    if (chan) {
00276       ast_channel_inherit_variables(chan, channel->owner);
00277       ast_channel_datastore_inherit(chan, channel->owner);
00278 
00279       /* Copy over callerid information */
00280       ast_party_redirecting_copy(&channel->owner->redirecting, &chan->redirecting);
00281 
00282       channel->owner->dialed.transit_network_select = chan->dialed.transit_network_select;
00283 
00284       ast_connected_line_copy_from_caller(&channel->owner->connected, &chan->caller);
00285 
00286       ast_string_field_set(channel->owner, language, chan->language);
00287       ast_string_field_set(channel->owner, accountcode, chan->accountcode);
00288       if (ast_strlen_zero(channel->owner->musicclass))
00289          ast_string_field_set(channel->owner, musicclass, chan->musicclass);
00290 
00291       channel->owner->adsicpe = chan->adsicpe;
00292       channel->owner->transfercapability = chan->transfercapability;
00293    }
00294 
00295    /* Attempt to actually call this device */
00296    if ((res = ast_call(channel->owner, numsubst, 0))) {
00297       res = 0;
00298       ast_hangup(channel->owner);
00299       channel->owner = NULL;
00300    } else {
00301       if (chan)
00302          ast_poll_channel_add(chan, channel->owner);
00303       res = 1;
00304       ast_verb(3, "Called %s\n", numsubst);
00305    }
00306 
00307    return res;
00308 }

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

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

Definition at line 940 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().

00941 {
00942    struct ast_dial_channel *channel = AST_LIST_LAST(&dial->channels);
00943 
00944    /* We can try to predict programmer behavior, the last channel they added is probably the one they wanted to modify */
00945    if (channel->num == num)
00946       return channel;
00947 
00948    /* Hrm not at the end... looking through the list it is! */
00949    AST_LIST_LOCK(&dial->channels);
00950    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00951       if (channel->num == num)
00952          break;
00953    }
00954    AST_LIST_UNLOCK(&dial->channels);
00955    
00956    return channel;
00957 }

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

Helper function that finds the dialed channel based on owner.

Definition at line 367 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().

00368 {
00369    struct ast_dial_channel *channel = NULL;
00370 
00371    AST_LIST_LOCK(&dial->channels);
00372    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00373       if (channel->owner == owner)
00374          break;
00375    }
00376    AST_LIST_UNLOCK(&dial->channels);
00377 
00378    return channel;
00379 }

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 328 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().

00329 {
00330    struct ast_channel *original = channel->owner;
00331    char *tmp = ast_strdupa(channel->owner->call_forward);
00332    char *tech = "Local", *device = tmp, *stuff;
00333 
00334    /* If call forwarding is disabled just drop the original channel and don't attempt to dial the new one */
00335    if (FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING)) {
00336       ast_hangup(original);
00337       channel->owner = NULL;
00338       return 0;
00339    }
00340 
00341    /* Figure out the new destination */
00342    if ((stuff = strchr(tmp, '/'))) {
00343       *stuff++ = '\0';
00344       tech = tmp;
00345       device = stuff;
00346    }
00347 
00348    /* Drop old destination information */
00349    ast_free(channel->tech);
00350    ast_free(channel->device);
00351 
00352    /* Update the dial channel with the new destination information */
00353    channel->tech = ast_strdup(tech);
00354    channel->device = ast_strdup(device);
00355    AST_LIST_UNLOCK(&dial->channels);
00356 
00357    /* Finally give it a go... send it out into the world */
00358    begin_dial_channel(channel, chan);
00359 
00360    /* Drop the original channel */
00361    ast_hangup(original);
00362 
00363    return 0;
00364 }

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 390 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().

00391 {
00392    if (fr->frametype == AST_FRAME_CONTROL) {
00393       switch (fr->subclass.integer) {
00394       case AST_CONTROL_ANSWER:
00395          ast_verb(3, "%s answered %s\n", channel->owner->name, chan->name);
00396          AST_LIST_LOCK(&dial->channels);
00397          AST_LIST_REMOVE(&dial->channels, channel, list);
00398          AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
00399          AST_LIST_UNLOCK(&dial->channels);
00400          set_state(dial, AST_DIAL_RESULT_ANSWERED);
00401          break;
00402       case AST_CONTROL_BUSY:
00403          ast_verb(3, "%s is busy\n", channel->owner->name);
00404          ast_hangup(channel->owner);
00405          channel->owner = NULL;
00406          break;
00407       case AST_CONTROL_CONGESTION:
00408          ast_verb(3, "%s is circuit-busy\n", channel->owner->name);
00409          ast_hangup(channel->owner);
00410          channel->owner = NULL;
00411          break;
00412       case AST_CONTROL_INCOMPLETE:
00413          ast_verb(3, "%s dialed Incomplete extension %s\n", channel->owner->name, channel->owner->exten);
00414          ast_indicate(chan, AST_CONTROL_INCOMPLETE);
00415          break;
00416       case AST_CONTROL_RINGING:
00417          ast_verb(3, "%s is ringing\n", channel->owner->name);
00418          if (!dial->options[AST_DIAL_OPTION_MUSIC])
00419             ast_indicate(chan, AST_CONTROL_RINGING);
00420          set_state(dial, AST_DIAL_RESULT_RINGING);
00421          break;
00422       case AST_CONTROL_PROGRESS:
00423          ast_verb(3, "%s is making progress, passing it to %s\n", channel->owner->name, chan->name);
00424          ast_indicate(chan, AST_CONTROL_PROGRESS);
00425          set_state(dial, AST_DIAL_RESULT_PROGRESS);
00426          break;
00427       case AST_CONTROL_VIDUPDATE:
00428          ast_verb(3, "%s requested a video update, passing it to %s\n", channel->owner->name, chan->name);
00429          ast_indicate(chan, AST_CONTROL_VIDUPDATE);
00430          break;
00431       case AST_CONTROL_SRCUPDATE:
00432          if (option_verbose > 2)
00433             ast_verbose (VERBOSE_PREFIX_3 "%s requested a source update, passing it to %s\n", channel->owner->name, chan->name);
00434          ast_indicate(chan, AST_CONTROL_SRCUPDATE);
00435          break;
00436       case AST_CONTROL_CONNECTED_LINE:
00437          ast_verb(3, "%s connected line has changed, passing it to %s\n", channel->owner->name, chan->name);
00438          if (ast_channel_connected_line_macro(channel->owner, chan, fr, 1, 1)) {
00439             ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, fr->data.ptr, fr->datalen);
00440          }
00441          break;
00442       case AST_CONTROL_REDIRECTING:
00443          ast_verb(3, "%s redirecting info has changed, passing it to %s\n", channel->owner->name, chan->name);
00444          if (ast_channel_redirecting_macro(channel->owner, chan, fr, 1, 1)) {
00445             ast_indicate_data(chan, AST_CONTROL_REDIRECTING, fr->data.ptr, fr->datalen);
00446          }
00447          break;
00448       case AST_CONTROL_PROCEEDING:
00449          ast_verb(3, "%s is proceeding, passing it to %s\n", channel->owner->name, chan->name);
00450          ast_indicate(chan, AST_CONTROL_PROCEEDING);
00451          set_state(dial, AST_DIAL_RESULT_PROCEEDING);
00452          break;
00453       case AST_CONTROL_HOLD:
00454          ast_verb(3, "Call on %s placed on hold\n", chan->name);
00455          ast_indicate(chan, AST_CONTROL_HOLD);
00456          break;
00457       case AST_CONTROL_UNHOLD:
00458          ast_verb(3, "Call on %s left from hold\n", chan->name);
00459          ast_indicate(chan, AST_CONTROL_UNHOLD);
00460          break;
00461       case AST_CONTROL_OFFHOOK:
00462       case AST_CONTROL_FLASH:
00463          break;
00464       case -1:
00465          /* Prod the channel */
00466          ast_indicate(chan, -1);
00467          break;
00468       default:
00469          break;
00470       }
00471    }
00472 
00473    return;
00474 }

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 477 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().

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

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 522 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().

00523 {
00524    struct ast_dial_channel *channel = NULL;
00525    int diff = ast_tvdiff_ms(ast_tvnow(), start), lowest_timeout = -1, new_timeout = -1;
00526 
00527    /* If the global dial timeout tripped switch the state to timeout so our channel loop will drop every channel */
00528    if (diff >= dial->timeout) {
00529       set_state(dial, AST_DIAL_RESULT_TIMEOUT);
00530       new_timeout = 0;
00531    }
00532 
00533    /* Go through dropping out channels that have met their timeout */
00534    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00535       if (dial->state == AST_DIAL_RESULT_TIMEOUT || diff >= channel->timeout) {
00536          ast_hangup(channel->owner);
00537          channel->owner = NULL;
00538       } else if ((lowest_timeout == -1) || (lowest_timeout > channel->timeout)) {
00539          lowest_timeout = channel->timeout;
00540       }
00541    }
00542 
00543    /* Calculate the new timeout using the lowest timeout found */
00544    if (lowest_timeout >= 0)
00545       new_timeout = lowest_timeout - diff;
00546 
00547    return new_timeout;
00548 }

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 551 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().

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

static int music_disable ( void *  data  )  [static]

Definition at line 134 of file dial.c.

References ast_free.

00135 {
00136    if (!data)
00137       return -1;
00138 
00139    ast_free(data);
00140 
00141    return 0;
00142 }

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

Definition at line 129 of file dial.c.

References ast_strdup.

00130 {
00131    return ast_strdup(data);
00132 }

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

Definition at line 381 of file dial.c.

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

00382 {
00383    dial->state = state;
00384 
00385    if (dial->state_callback)
00386       dial->state_callback(dial);
00387 }


Variable Documentation

struct ast_option_types option_types[] [static]

Map options to respective handlers (enable/disable).

Note:
This list MUST be perfectly kept in order with enum ast_dial_option, or else madness will happen.

Definition at line 180 of file dial.c.


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