Mon Jun 27 16:51:13 2011

Asterisk developer's documentation


dial.h File Reference

Dialing API. More...

Go to the source code of this file.

Typedefs

typedef void(*) ast_dial_state_callback (struct ast_dial *)

Enumerations

enum  ast_dial_option {
  AST_DIAL_OPTION_RINGING, AST_DIAL_OPTION_ANSWER_EXEC, AST_DIAL_OPTION_MUSIC, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING,
  AST_DIAL_OPTION_MAX
}
 List of options that are applicable either globally or per dialed channel. More...
enum  ast_dial_result {
  AST_DIAL_RESULT_INVALID, AST_DIAL_RESULT_FAILED, AST_DIAL_RESULT_TRYING, AST_DIAL_RESULT_RINGING,
  AST_DIAL_RESULT_PROGRESS, AST_DIAL_RESULT_PROCEEDING, AST_DIAL_RESULT_ANSWERED, AST_DIAL_RESULT_TIMEOUT,
  AST_DIAL_RESULT_HANGUP, AST_DIAL_RESULT_UNANSWERED
}
 List of return codes for dial run API calls. More...

Functions

ast_channelast_dial_answered (struct ast_dial *dial)
 Return channel that answered.
ast_channelast_dial_answered_steal (struct ast_dial *dial)
 Steal the channel that answered.
int ast_dial_append (struct ast_dial *dial, const char *tech, const char *device)
 Append a channel.
ast_dialast_dial_create (void)
 New dialing structure.
int ast_dial_destroy (struct ast_dial *dial)
 Destroys a dialing structure.
void ast_dial_hangup (struct ast_dial *dial)
 Hangup channels.
enum ast_dial_result ast_dial_join (struct ast_dial *dial)
 Cancel async thread.
int ast_dial_option_disable (struct ast_dial *dial, int num, enum ast_dial_option option)
 Disables an option per channel.
int ast_dial_option_enable (struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
 Enables an option per channel.
int ast_dial_option_global_disable (struct ast_dial *dial, enum ast_dial_option option)
 Disables an option globally.
int ast_dial_option_global_enable (struct ast_dial *dial, enum ast_dial_option option, void *data)
 Enables an option globally.
enum ast_dial_result ast_dial_run (struct ast_dial *dial, struct ast_channel *chan, int async)
 Execute dialing synchronously or asynchronously.
void ast_dial_set_global_timeout (struct ast_dial *dial, int timeout)
 Set the maximum time (globally) allowed for trying to ring phones.
void ast_dial_set_state_callback (struct ast_dial *dial, ast_dial_state_callback callback)
 Set a callback for state changes.
void ast_dial_set_timeout (struct ast_dial *dial, int num, int timeout)
 Set the maximum time (per channel) allowed for trying to ring the phone.
enum ast_dial_result ast_dial_state (struct ast_dial *dial)
 Return state of dial.


Detailed Description

Dialing API.

Definition in file dial.h.


Typedef Documentation

typedef void(*) ast_dial_state_callback(struct ast_dial *)

Definition at line 36 of file dial.h.


Enumeration Type Documentation

enum ast_dial_option

List of options that are applicable either globally or per dialed channel.

Enumerator:
AST_DIAL_OPTION_RINGING  Always indicate ringing to caller
AST_DIAL_OPTION_ANSWER_EXEC  Execute application upon answer in async mode
AST_DIAL_OPTION_MUSIC  Play music on hold instead of ringing to the calling channel
AST_DIAL_OPTION_DISABLE_CALL_FORWARDING  Disable call forwarding on channels
AST_DIAL_OPTION_MAX  End terminator -- must always remain last

Definition at line 39 of file dial.h.

00039                      {
00040    AST_DIAL_OPTION_RINGING,                 /*!< Always indicate ringing to caller */
00041    AST_DIAL_OPTION_ANSWER_EXEC,             /*!< Execute application upon answer in async mode */
00042    AST_DIAL_OPTION_MUSIC,                   /*!< Play music on hold instead of ringing to the calling channel */
00043    AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, /*!< Disable call forwarding on channels */
00044    AST_DIAL_OPTION_MAX,                     /*!< End terminator -- must always remain last */
00045 };

enum ast_dial_result

List of return codes for dial run API calls.

Enumerator:
AST_DIAL_RESULT_INVALID  Invalid options were passed to run function
AST_DIAL_RESULT_FAILED  Attempts to dial failed before reaching critical state
AST_DIAL_RESULT_TRYING  Currently trying to dial
AST_DIAL_RESULT_RINGING  Dial is presently ringing
AST_DIAL_RESULT_PROGRESS  Dial is presently progressing
AST_DIAL_RESULT_PROCEEDING  Dial is presently proceeding
AST_DIAL_RESULT_ANSWERED  A channel was answered
AST_DIAL_RESULT_TIMEOUT  Timeout was tripped, nobody answered
AST_DIAL_RESULT_HANGUP  Caller hung up
AST_DIAL_RESULT_UNANSWERED  Nobody answered

Definition at line 48 of file dial.h.

00048                      {
00049    AST_DIAL_RESULT_INVALID,     /*!< Invalid options were passed to run function */
00050    AST_DIAL_RESULT_FAILED,      /*!< Attempts to dial failed before reaching critical state */
00051    AST_DIAL_RESULT_TRYING,      /*!< Currently trying to dial */
00052    AST_DIAL_RESULT_RINGING,     /*!< Dial is presently ringing */
00053    AST_DIAL_RESULT_PROGRESS,    /*!< Dial is presently progressing */
00054    AST_DIAL_RESULT_PROCEEDING,  /*!< Dial is presently proceeding */
00055    AST_DIAL_RESULT_ANSWERED,    /*!< A channel was answered */
00056    AST_DIAL_RESULT_TIMEOUT,     /*!< Timeout was tripped, nobody answered */
00057    AST_DIAL_RESULT_HANGUP,      /*!< Caller hung up */
00058    AST_DIAL_RESULT_UNANSWERED,  /*!< Nobody answered */
00059 };


Function Documentation

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

00740 {
00741    if (!dial)
00742       return NULL;
00743 
00744    return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
00745 }

struct ast_channel* ast_dial_answered_steal ( struct ast_dial dial  ) 

Steal the channel that answered.

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

Definition at line 751 of file dial.c.

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

Referenced by do_notify().

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

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

Append a channel.

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

Definition at line 218 of file dial.c.

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

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

00219 {
00220    struct ast_dial_channel *channel = NULL;
00221 
00222    /* Make sure we have required arguments */
00223    if (!dial || !tech || !device)
00224       return -1;
00225 
00226    /* Allocate new memory for dialed channel structure */
00227    if (!(channel = ast_calloc(1, sizeof(*channel))))
00228       return -1;
00229 
00230    /* Record technology and device for when we actually dial */
00231    channel->tech = ast_strdup(tech);
00232    channel->device = ast_strdup(device);
00233 
00234    /* Grab reference number from dial structure */
00235    channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
00236 
00237    /* No timeout exists... yet */
00238    channel->timeout = -1;
00239 
00240    /* Insert into channels list */
00241    AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
00242 
00243    return channel->num;
00244 }

struct ast_dial* ast_dial_create ( void   ) 

New dialing structure.

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

Definition at line 190 of file dial.c.

References ast_calloc, AST_LIST_HEAD_INIT, ast_mutex_init, and AST_PTHREADT_NULL.

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

00191 {
00192    struct ast_dial *dial = NULL;
00193 
00194    /* Allocate new memory for structure */
00195    if (!(dial = ast_calloc(1, sizeof(*dial))))
00196       return NULL;
00197 
00198    /* Initialize list of channels */
00199    AST_LIST_HEAD_INIT(&dial->channels);
00200 
00201    /* Initialize thread to NULL */
00202    dial->thread = AST_PTHREADT_NULL;
00203 
00204    /* No timeout exists... yet */
00205    dial->timeout = -1;
00206    dial->actual_timeout = -1;
00207 
00208    /* Can't forget about the lock */
00209    ast_mutex_init(&dial->lock);
00210 
00211    return dial;
00212 }

int ast_dial_destroy ( struct ast_dial dial  ) 

Destroys a dialing structure.

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

Definition at line 851 of file dial.c.

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

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

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

void ast_dial_hangup ( struct ast_dial dial  ) 

Hangup channels.

Note:
Hangup all active channels
Parameters:
dial Dialing structure

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Referenced by do_notify(), and page_exec().

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

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

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

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

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

Referenced by do_notify(), and page_exec().

01039 {
01040    dial->timeout = timeout;
01041 
01042    if (dial->timeout > 0 && (dial->actual_timeout > dial->timeout || dial->actual_timeout == -1))
01043       dial->actual_timeout = dial->timeout;
01044 
01045    return;
01046 }

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

References ast_dial::state_callback.

Referenced by sla_ring_station().

01029 {
01030    dial->state_callback = callback;
01031 }

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

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

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

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

References ast_dial::state.

Referenced by dial_trunk(), and sla_handle_dial_state_event().

00771 {
00772    return dial->state;
00773 }


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