Sat Aug 6 00:39:55 2011

Asterisk developer's documentation


dial.c File Reference

Dialing API. More...

#include "asterisk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/options.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/linkedlists.h"
#include "asterisk/dial.h"
#include "asterisk/pbx.h"

Go to the source code of this file.

Data Structures

struct  answer_exec_struct
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.
#define S_REPLACE(s, new_val)

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)
static void * answer_exec_enable (void *data)
static void answer_exec_run (struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
ast_channelast_dial_answered (struct ast_dial *dial)
 Return 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_state_callback (struct ast_dial *dial, ast_dial_state_callback callback)
 Set a callback for state changes.
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.
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 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 enum ast_dial_result monitor_dial (struct ast_dial *dial, struct ast_channel *chan)
 Helper function that basically keeps tabs on dialing attempts.
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 171 of file dial.c.

Referenced by monitor_dial(), and wait_for_answer().

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

Referenced by 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 177 of file dial.c.

Referenced by monitor_dial().

#define S_REPLACE ( s,
new_val   ) 

Value:

do {                            \
                if (s)                  \
                        free(s);        \
                s = (new_val);          \
        } while (0)

Definition at line 163 of file dial.c.

Referenced by begin_dial().


Typedef Documentation

typedef int(*) ast_dial_option_cb_disable(void *data)

Typedef for dial option disable.

Definition at line 74 of file dial.c.

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

Typedef for dial option enable.

Definition at line 71 of file dial.c.


Function Documentation

static int answer_exec_disable ( void *  data  )  [static]

Definition at line 109 of file dial.c.

References answer_exec_struct::args, and free.

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

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

Definition at line 83 of file dial.c.

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

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

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

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

00129 {
00130    struct ast_channel *chan = dial_channel->owner;
00131    struct ast_app *ast_app = pbx_findapp(app);
00132 
00133    /* If the application was not found, return immediately */
00134    if (!ast_app)
00135       return;
00136 
00137    /* All is well... execute the application */
00138    pbx_exec(chan, ast_app, args);
00139 
00140    /* If another thread is not taking over hang up the channel */
00141    ast_mutex_lock(&dial->lock);
00142    if (dial->thread != AST_PTHREADT_STOP) {
00143       ast_hangup(chan);
00144       dial_channel->owner = NULL;
00145    }
00146    ast_mutex_unlock(&dial->lock);
00147 
00148    return;
00149 }

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

00620 {
00621    if (!dial)
00622       return NULL;
00623 
00624    return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
00625 }

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

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

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

00208 {
00209    struct ast_dial_channel *channel = NULL;
00210 
00211    /* Make sure we have required arguments */
00212    if (!dial || !tech || !device)
00213       return -1;
00214 
00215    /* Allocate new memory for dialed channel structure */
00216    if (!(channel = ast_calloc(1, sizeof(*channel))))
00217       return -1;
00218 
00219    /* Record technology and device for when we actually dial */
00220    channel->tech = tech;
00221    channel->device = device;
00222 
00223    /* Grab reference number from dial structure */
00224    channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
00225 
00226    /* Insert into channels list */
00227    AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
00228 
00229    return channel->num;
00230 }

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

References ast_calloc, AST_LIST_HEAD_INIT, ast_mutex_init(), and AST_PTHREADT_NULL.

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

00184 {
00185    struct ast_dial *dial = NULL;
00186 
00187    /* Allocate new memory for structure */
00188    if (!(dial = ast_calloc(1, sizeof(*dial))))
00189       return NULL;
00190 
00191    /* Initialize list of channels */
00192    AST_LIST_HEAD_INIT(&dial->channels);
00193 
00194    /* Initialize thread to NULL */
00195    dial->thread = AST_PTHREADT_NULL;
00196 
00197    /* Can't forget about the lock */
00198    ast_mutex_init(&dial->lock);
00199 
00200    return dial;
00201 }

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

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

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

00713 {
00714    int i = 0;
00715    struct ast_dial_channel *channel = NULL;
00716 
00717    if (!dial)
00718       return -1;
00719    
00720    /* Hangup and deallocate all the dialed channels */
00721    AST_LIST_LOCK(&dial->channels);
00722    AST_LIST_TRAVERSE_SAFE_BEGIN(&dial->channels, channel, list) {
00723       /* Disable any enabled options */
00724       for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
00725          if (!channel->options[i])
00726             continue;
00727          if (option_types[i].disable)
00728             option_types[i].disable(channel->options[i]);
00729          channel->options[i] = NULL;
00730       }
00731       /* Hang up channel if need be */
00732       if (channel->owner) {
00733          ast_hangup(channel->owner);
00734          channel->owner = NULL;
00735       }
00736       /* Free structure */
00737       AST_LIST_REMOVE_CURRENT(&dial->channels, list);
00738       free(channel);
00739    }
00740    AST_LIST_TRAVERSE_SAFE_END;
00741    AST_LIST_UNLOCK(&dial->channels);
00742        
00743    /* Disable any enabled options globally */
00744    for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
00745       if (!dial->options[i])
00746          continue;
00747       if (option_types[i].disable)
00748          option_types[i].disable(dial->options[i]);
00749       dial->options[i] = NULL;
00750    }
00751 
00752    /* Lock be gone! */
00753    ast_mutex_destroy(&dial->lock);
00754 
00755    /* Free structure */
00756    free(dial);
00757 
00758    return 0;
00759 }

void ast_dial_hangup ( struct ast_dial dial  ) 

Hangup channels.

Note:
Hangup all active channels
Parameters:
dial Dialing structure

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

00689 {
00690    struct ast_dial_channel *channel = NULL;
00691 
00692    if (!dial)
00693       return;
00694    
00695    AST_LIST_LOCK(&dial->channels);
00696    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00697       if (channel->owner) {
00698          ast_hangup(channel->owner);
00699          channel->owner = NULL;
00700       }
00701    }
00702    AST_LIST_UNLOCK(&dial->channels);
00703 
00704    return;
00705 }

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

00641 {
00642    pthread_t thread;
00643 
00644    /* If the dial structure is not running in async, return failed */
00645    if (dial->thread == AST_PTHREADT_NULL)
00646       return AST_DIAL_RESULT_FAILED;
00647 
00648    /* Record thread */
00649    thread = dial->thread;
00650 
00651    /* Boom, commence locking */
00652    ast_mutex_lock(&dial->lock);
00653 
00654    /* Stop the thread */
00655    dial->thread = AST_PTHREADT_STOP;
00656 
00657    /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
00658    AST_LIST_LOCK(&dial->channels);
00659    if (AST_LIST_FIRST(&dial->channels)->is_running_app) {
00660       struct ast_channel *chan = AST_LIST_FIRST(&dial->channels)->owner;
00661       if (chan) {
00662          ast_channel_lock(chan);
00663          ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
00664          ast_channel_unlock(chan);
00665       }
00666    } else {
00667       /* Now we signal it with SIGURG so it will break out of it's waitfor */
00668       pthread_kill(thread, SIGURG);
00669    }
00670    AST_LIST_UNLOCK(&dial->channels);
00671 
00672    /* Yay done with it */
00673    ast_mutex_unlock(&dial->lock);
00674 
00675    /* Finally wait for the thread to exit */
00676    pthread_join(thread, NULL);
00677 
00678    /* Yay thread is all gone */
00679    dial->thread = AST_PTHREADT_NULL;
00680 
00681    return dial->state;
00682 }

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

References AST_LIST_EMPTY, AST_LIST_LAST, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_dial::channels, ast_option_types::disable, ast_dial_channel::list, ast_dial_channel::num, option_types, and ast_dial_channel::options.

00854 {
00855    struct ast_dial_channel *channel = NULL;
00856 
00857    /* Ensure we have required arguments */
00858    if (!dial || AST_LIST_EMPTY(&dial->channels))
00859       return -1;
00860 
00861    /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
00862    AST_LIST_LOCK(&dial->channels);
00863    if (AST_LIST_LAST(&dial->channels)->num != num) {
00864       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00865          if (channel->num == num)
00866             break;
00867       }
00868    } else {
00869       channel = AST_LIST_LAST(&dial->channels);
00870    }
00871    AST_LIST_UNLOCK(&dial->channels);
00872 
00873    /* If none found, return failure */
00874    if (!channel)
00875       return -1;
00876 
00877    /* If the option is not enabled, return failure */
00878    if (!channel->options[option])
00879       return -1;
00880 
00881    /* Execute callback of option to disable it if it exists */
00882    if (option_types[option].disable)
00883       option_types[option].disable(channel->options[option]);
00884 
00885    /* Finally disable the option on the structure */
00886    channel->options[option] = NULL;
00887 
00888    return 0;
00889 }

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

References AST_LIST_EMPTY, AST_LIST_LAST, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_dial::channels, ast_option_types::enable, ast_dial_channel::list, ast_dial_channel::num, option_types, and ast_dial_channel::options.

00790 {
00791    struct ast_dial_channel *channel = NULL;
00792 
00793    /* Ensure we have required arguments */
00794    if (!dial || AST_LIST_EMPTY(&dial->channels))
00795       return -1;
00796    
00797    /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
00798    AST_LIST_LOCK(&dial->channels);
00799    if (AST_LIST_LAST(&dial->channels)->num != num) {
00800       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00801          if (channel->num == num)
00802             break;
00803       }
00804    } else {
00805       channel = AST_LIST_LAST(&dial->channels);
00806    }
00807    AST_LIST_UNLOCK(&dial->channels);
00808 
00809    /* If none found, return failure */
00810    if (!channel)
00811       return -1;
00812 
00813    /* If the option is already enabled, return failure */
00814    if (channel->options[option])
00815       return -1;
00816 
00817         /* Execute enable callback if it exists, if not simply make sure the value is set */
00818    if (option_types[option].enable)
00819       channel->options[option] = option_types[option].enable(data);
00820    else
00821       channel->options[option] = (void*)1;
00822 
00823    return 0;
00824 }

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

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

00832 {
00833         /* If the option is not enabled, return failure */
00834         if (!dial->options[option])
00835                 return -1;
00836 
00837    /* Execute callback of option to disable if it exists */
00838    if (option_types[option].disable)
00839       option_types[option].disable(dial->options[option]);
00840 
00841    /* Finally disable option on the structure */
00842    dial->options[option] = NULL;
00843 
00844         return 0;
00845 }

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

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

Referenced by page_exec().

00768 {
00769    /* If the option is already enabled, return failure */
00770    if (dial->options[option])
00771       return -1;
00772 
00773    /* Execute enable callback if it exists, if not simply make sure the value is set */
00774    if (option_types[option].enable)
00775       dial->options[option] = option_types[option].enable(data);
00776    else
00777       dial->options[option] = (void*)1;
00778 
00779    return 0;
00780 }

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

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

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

00580 {
00581    enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
00582 
00583    /* Ensure required arguments are passed */
00584    if (!dial || (!chan && !async)) {
00585       ast_log(LOG_DEBUG, "invalid #1\n");
00586       return AST_DIAL_RESULT_INVALID;
00587    }
00588 
00589    /* If there are no channels to dial we can't very well try to dial them */
00590    if (AST_LIST_EMPTY(&dial->channels)) {
00591       ast_log(LOG_DEBUG, "invalid #2\n");
00592       return AST_DIAL_RESULT_INVALID;
00593    }
00594 
00595    /* Dial each requested channel */
00596    if (!begin_dial(dial, chan))
00597       return AST_DIAL_RESULT_FAILED;
00598 
00599    /* If we are running async spawn a thread and send it away... otherwise block here */
00600    if (async) {
00601       dial->state = AST_DIAL_RESULT_TRYING;
00602       /* Try to create a thread */
00603       if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
00604          /* Failed to create the thread - hangup all dialed channels and return failed */
00605          ast_dial_hangup(dial);
00606          res = AST_DIAL_RESULT_FAILED;
00607       }
00608    } else {
00609       res = monitor_dial(dial, chan);
00610    }
00611 
00612    return res;
00613 }

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

References ast_dial::state_callback.

Referenced by sla_ring_station().

00892 {
00893    dial->state_callback = callback;
00894 }

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

References ast_dial::state.

Referenced by dial_trunk(), and sla_handle_dial_state_event().

00632 {
00633    return dial->state;
00634 }

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

Dial async thread function.

Definition at line 565 of file dial.c.

References monitor_dial().

Referenced by ast_dial_run().

00566 {
00567    struct ast_dial *dial = data;
00568 
00569    /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
00570    monitor_dial(dial, NULL);
00571 
00572    return NULL;
00573 }

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

Helper function that does the beginning dialing.

Definition at line 233 of file dial.c.

References accountcode, ast_channel::adsicpe, ast_channel::appl, ast_call(), ast_channel_inherit_variables(), ast_copy_string(), AST_FORMAT_AUDIO_MASK, ast_hangup(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, AST_MAX_EXTENSION, ast_request(), ast_strdup, ast_string_field_set, ast_strlen_zero(), ast_verbose(), ast_dial_channel::cause, ast_channel::cdrflags, ast_dial::channels, ast_channel::cid, ast_callerid::cid_ani, ast_callerid::cid_name, ast_callerid::cid_num, ast_callerid::cid_pres, ast_callerid::cid_rdnis, ast_callerid::cid_tns, ast_callerid::cid_ton, ast_channel::data, ast_dial_channel::device, language, ast_dial_channel::list, musicclass, ast_channel::musicclass, ast_channel::nativeformats, option_verbose, ast_dial_channel::owner, S_REPLACE, ast_dial_channel::tech, ast_channel::transfercapability, VERBOSE_PREFIX_3, and ast_channel::whentohangup.

Referenced by ast_dial_run().

00234 {
00235    struct ast_dial_channel *channel = NULL;
00236    int success = 0, res = 0;
00237 
00238    /* Iterate through channel list, requesting and calling each one */
00239    AST_LIST_LOCK(&dial->channels);
00240    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00241       char numsubst[AST_MAX_EXTENSION];
00242 
00243       /* Copy device string over */
00244       ast_copy_string(numsubst, channel->device, sizeof(numsubst));
00245 
00246       /* Request that the channel be created */
00247       if (!(channel->owner = ast_request(channel->tech, 
00248          chan ? chan->nativeformats : AST_FORMAT_AUDIO_MASK, numsubst, &channel->cause))) {
00249          continue;
00250       }
00251 
00252       channel->owner->appl = "AppDial2";
00253                 channel->owner->data = "(Outgoing Line)";
00254                 channel->owner->whentohangup = 0;
00255 
00256       /* Inherit everything from he who spawned this Dial */
00257       if (chan) {
00258          ast_channel_inherit_variables(chan, channel->owner);
00259 
00260          /* Copy over callerid information */
00261          S_REPLACE(channel->owner->cid.cid_num, ast_strdup(chan->cid.cid_num));
00262          S_REPLACE(channel->owner->cid.cid_name, ast_strdup(chan->cid.cid_name));
00263          S_REPLACE(channel->owner->cid.cid_ani, ast_strdup(chan->cid.cid_ani));
00264          S_REPLACE(channel->owner->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
00265    
00266          ast_string_field_set(channel->owner, language, chan->language);
00267          ast_string_field_set(channel->owner, accountcode, chan->accountcode);
00268          channel->owner->cdrflags = chan->cdrflags;
00269          if (ast_strlen_zero(channel->owner->musicclass))
00270             ast_string_field_set(channel->owner, musicclass, chan->musicclass);
00271    
00272          channel->owner->cid.cid_pres = chan->cid.cid_pres;
00273          channel->owner->cid.cid_ton = chan->cid.cid_ton;
00274          channel->owner->cid.cid_tns = chan->cid.cid_tns;
00275          channel->owner->adsicpe = chan->adsicpe;
00276          channel->owner->transfercapability = chan->transfercapability;
00277       }
00278 
00279       /* Actually call the device */
00280       if ((res = ast_call(channel->owner, numsubst, 0))) {
00281          ast_hangup(channel->owner);
00282          channel->owner = NULL;
00283       } else {
00284          success++;
00285          if (option_verbose > 2)
00286             ast_verbose(VERBOSE_PREFIX_3 "Called %s\n", numsubst);
00287       }
00288    }
00289    AST_LIST_UNLOCK(&dial->channels);
00290 
00291    /* If number of failures matches the number of channels, then this truly failed */
00292    return success;
00293 }

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

00297 {
00298    struct ast_dial_channel *channel = NULL;
00299 
00300    AST_LIST_LOCK(&dial->channels);
00301    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00302       if (channel->owner == owner)
00303          break;
00304    }
00305    AST_LIST_UNLOCK(&dial->channels);
00306 
00307    return channel;
00308 }

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

References AST_CONTROL_ANSWER, AST_CONTROL_BUSY, AST_CONTROL_CONGESTION, AST_CONTROL_FLASH, AST_CONTROL_HOLD, AST_CONTROL_OFFHOOK, AST_CONTROL_PROCEEDING, AST_CONTROL_PROGRESS, AST_CONTROL_RINGING, AST_CONTROL_SRCUPDATE, AST_CONTROL_UNHOLD, AST_CONTROL_VIDUPDATE, AST_DIAL_RESULT_ANSWERED, AST_DIAL_RESULT_PROCEEDING, AST_DIAL_RESULT_PROGRESS, AST_DIAL_RESULT_RINGING, AST_FRAME_CONTROL, ast_hangup(), ast_indicate(), AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, ast_verbose(), ast_dial::channels, ast_frame::frametype, ast_dial_channel::list, ast_channel::name, option_verbose, ast_dial_channel::owner, set_state(), ast_frame::subclass, and VERBOSE_PREFIX_3.

Referenced by monitor_dial(), and socket_read().

00320 {
00321    if (fr->frametype == AST_FRAME_CONTROL) {
00322       switch (fr->subclass) {
00323       case AST_CONTROL_ANSWER:
00324          if (option_verbose > 2)
00325             ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", channel->owner->name, chan->name);
00326          AST_LIST_LOCK(&dial->channels);
00327          AST_LIST_REMOVE(&dial->channels, channel, list);
00328          AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
00329          AST_LIST_UNLOCK(&dial->channels);
00330          set_state(dial, AST_DIAL_RESULT_ANSWERED);
00331          break;
00332       case AST_CONTROL_BUSY:
00333          if (option_verbose > 2)
00334             ast_verbose(VERBOSE_PREFIX_3 "%s is busy\n", channel->owner->name);
00335          ast_hangup(channel->owner);
00336          channel->owner = NULL;
00337          break;
00338       case AST_CONTROL_CONGESTION:
00339          if (option_verbose > 2)
00340             ast_verbose(VERBOSE_PREFIX_3 "%s is circuit-busy\n", channel->owner->name);
00341          ast_hangup(channel->owner);
00342          channel->owner = NULL;
00343          break;
00344       case AST_CONTROL_RINGING:
00345          if (option_verbose > 2)
00346             ast_verbose(VERBOSE_PREFIX_3 "%s is ringing\n", channel->owner->name);
00347          ast_indicate(chan, AST_CONTROL_RINGING);
00348          set_state(dial, AST_DIAL_RESULT_RINGING);
00349          break;
00350       case AST_CONTROL_PROGRESS:
00351          if (option_verbose > 2)
00352             ast_verbose (VERBOSE_PREFIX_3 "%s is making progress, passing it to %s\n", channel->owner->name, chan->name);
00353          ast_indicate(chan, AST_CONTROL_PROGRESS);
00354          set_state(dial, AST_DIAL_RESULT_PROGRESS);
00355          break;
00356       case AST_CONTROL_VIDUPDATE:
00357          if (option_verbose > 2)
00358             ast_verbose (VERBOSE_PREFIX_3 "%s requested a video update, passing it to %s\n", channel->owner->name, chan->name);
00359          ast_indicate(chan, AST_CONTROL_VIDUPDATE);
00360          break;
00361       case AST_CONTROL_SRCUPDATE:
00362          if (option_verbose > 2)
00363             ast_verbose (VERBOSE_PREFIX_3 "%s requested a source update, passing it to %s\n", channel->owner->name, chan->name);
00364          ast_indicate(chan, AST_CONTROL_SRCUPDATE);
00365          break;
00366       case AST_CONTROL_PROCEEDING:
00367          if (option_verbose > 2)
00368             ast_verbose (VERBOSE_PREFIX_3 "%s is proceeding, passing it to %s\n", channel->owner->name, chan->name);
00369          ast_indicate(chan, AST_CONTROL_PROCEEDING);
00370          set_state(dial, AST_DIAL_RESULT_PROCEEDING);
00371          break;
00372       case AST_CONTROL_HOLD:
00373          if (option_verbose > 2)
00374             ast_verbose(VERBOSE_PREFIX_3 "Call on %s placed on hold\n", chan->name);
00375          ast_indicate(chan, AST_CONTROL_HOLD);
00376          break;
00377       case AST_CONTROL_UNHOLD:
00378          if (option_verbose > 2)
00379             ast_verbose(VERBOSE_PREFIX_3 "Call on %s left from hold\n", chan->name);
00380          ast_indicate(chan, AST_CONTROL_UNHOLD);
00381          break;
00382       case AST_CONTROL_OFFHOOK:
00383       case AST_CONTROL_FLASH:
00384          break;
00385       case -1:
00386          /* Prod the channel */
00387          ast_indicate(chan, -1);
00388          break;
00389       default:
00390          break;
00391       }
00392    }
00393 
00394    return;
00395 }

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 398 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_verbose(), ast_dial::channels, ast_frame::frametype, ast_dial_channel::list, ast_channel::name, option_verbose, ast_dial_channel::owner, set_state(), ast_frame::subclass, and VERBOSE_PREFIX_3.

Referenced by monitor_dial().

00399 {
00400    /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
00401    if (fr->frametype != AST_FRAME_CONTROL)
00402       return;
00403 
00404    switch (fr->subclass) {
00405    case AST_CONTROL_ANSWER:
00406       if (option_verbose > 2)
00407          ast_verbose( VERBOSE_PREFIX_3 "%s answered\n", channel->owner->name);
00408       AST_LIST_LOCK(&dial->channels);
00409       AST_LIST_REMOVE(&dial->channels, channel, list);
00410       AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
00411       AST_LIST_UNLOCK(&dial->channels);
00412       set_state(dial, AST_DIAL_RESULT_ANSWERED);
00413       break;
00414    case AST_CONTROL_BUSY:
00415       if (option_verbose > 2)
00416          ast_verbose(VERBOSE_PREFIX_3 "%s is busy\n", channel->owner->name);
00417       ast_hangup(channel->owner);
00418       channel->owner = NULL;
00419       break;
00420    case AST_CONTROL_CONGESTION:
00421       if (option_verbose > 2)
00422          ast_verbose(VERBOSE_PREFIX_3 "%s is circuit-busy\n", channel->owner->name);
00423       ast_hangup(channel->owner);
00424       channel->owner = NULL;
00425       break;
00426    case AST_CONTROL_RINGING:
00427       if (option_verbose > 2)
00428          ast_verbose(VERBOSE_PREFIX_3 "%s is ringing\n", channel->owner->name);
00429       set_state(dial, AST_DIAL_RESULT_RINGING);
00430       break;
00431    case AST_CONTROL_PROGRESS:
00432       if (option_verbose > 2)
00433          ast_verbose (VERBOSE_PREFIX_3 "%s is making progress\n", channel->owner->name);
00434       set_state(dial, AST_DIAL_RESULT_PROGRESS);
00435       break;
00436    case AST_CONTROL_PROCEEDING:
00437       if (option_verbose > 2)
00438          ast_verbose (VERBOSE_PREFIX_3 "%s is proceeding\n", channel->owner->name);
00439       set_state(dial, AST_DIAL_RESULT_PROCEEDING);
00440       break;
00441    default:
00442       break;
00443    }
00444 
00445    return;
00446 }

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

References answer_exec_run(), answer_exec_struct::app, answer_exec_struct::args, AST_CONTROL_RINGING, AST_DIAL_OPTION_ANSWER_EXEC, 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_PTHREADT_STOP, ast_read(), ast_waitfor_n(), ast_dial::channels, find_relative_dial_channel(), FIND_RELATIVE_OPTION, handle_frame(), handle_frame_ownerless(), IS_CALLER, ast_dial_channel::is_running_app, ast_dial::options, ast_dial_channel::owner, set_state(), ast_dial::state, and ast_dial::thread.

Referenced by ast_dial_run(), and async_dial().

00450 {
00451    int timeout = -1, count = 0;
00452    struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
00453    struct ast_dial_channel *channel = NULL;
00454    struct answer_exec_struct *answer_exec = NULL;
00455 
00456    set_state(dial, AST_DIAL_RESULT_TRYING);
00457 
00458    /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
00459    if (dial->options[AST_DIAL_OPTION_RINGING]) {
00460       set_state(dial, AST_DIAL_RESULT_RINGING);
00461       if (chan)
00462          ast_indicate(chan, AST_CONTROL_RINGING);
00463    }
00464 
00465    /* Go into an infinite loop while we are trying */
00466    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)) {
00467       int pos = 0;
00468       struct ast_frame *fr = NULL;
00469 
00470       /* Set up channel structure array */
00471       pos = count = 0;
00472       if (chan)
00473          cs[pos++] = chan;
00474 
00475       /* Add channels we are attempting to dial */
00476       AST_LIST_LOCK(&dial->channels);
00477       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00478          if (channel->owner) {
00479             cs[pos++] = channel->owner;
00480             count++;
00481          }
00482       }
00483       AST_LIST_UNLOCK(&dial->channels);
00484 
00485       /* If we have no outbound channels in progress, switch state to unanswered and stop */
00486       if (!count) {
00487          set_state(dial, AST_DIAL_RESULT_UNANSWERED);
00488          break;
00489       }
00490 
00491       /* Just to be safe... */
00492       if (dial->thread == AST_PTHREADT_STOP)
00493          break;
00494 
00495       /* Wait for frames from channels */
00496       who = ast_waitfor_n(cs, pos, &timeout);
00497 
00498       /* Check to see if our thread is being cancelled */
00499       if (dial->thread == AST_PTHREADT_STOP)
00500          break;
00501 
00502       /* If we are not being cancelled and we have no channel, then timeout was tripped */
00503       if (!who)
00504          continue;
00505 
00506       /* Find relative dial channel */
00507       if (!chan || !IS_CALLER(chan, who))
00508          channel = find_relative_dial_channel(dial, who);
00509 
00510       /* Attempt to read in a frame */
00511       if (!(fr = ast_read(who))) {
00512          /* If this is the caller then we switch state to hangup and stop */
00513          if (chan && IS_CALLER(chan, who)) {
00514             set_state(dial, AST_DIAL_RESULT_HANGUP);
00515             break;
00516          }
00517          ast_hangup(who);
00518          channel->owner = NULL;
00519          continue;
00520       }
00521 
00522       /* Process the frame */
00523       if (chan)
00524          handle_frame(dial, channel, fr, chan);
00525       else
00526          handle_frame_ownerless(dial, channel, fr);
00527 
00528       /* Free the received frame and start all over */
00529       ast_frfree(fr);
00530    }
00531 
00532    /* Do post-processing from loop */
00533    if (dial->state == AST_DIAL_RESULT_ANSWERED) {
00534       /* Hangup everything except that which answered */
00535       AST_LIST_LOCK(&dial->channels);
00536       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00537          if (!channel->owner || channel->owner == who)
00538             continue;
00539          ast_hangup(channel->owner);
00540          channel->owner = NULL;
00541       }
00542       AST_LIST_UNLOCK(&dial->channels);
00543       /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
00544       if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC))) {
00545          channel->is_running_app = 1;
00546          answer_exec_run(dial, channel, answer_exec->app, answer_exec->args);
00547          channel->is_running_app = 0;
00548       }
00549    } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
00550       /* Hangup everything */
00551       AST_LIST_LOCK(&dial->channels);
00552       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00553          if (!channel->owner)
00554             continue;
00555          ast_hangup(channel->owner);
00556          channel->owner = NULL;
00557       }
00558       AST_LIST_UNLOCK(&dial->channels);
00559    }
00560 
00561    return dial->state;
00562 }

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

Definition at line 310 of file dial.c.

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

Referenced by handle_frame(), handle_frame_ownerless(), and monitor_dial().

00311 {
00312    dial->state = state;
00313 
00314    if (dial->state_callback)
00315       dial->state_callback(dial);
00316 }


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 Aug 6 00:39:55 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7