Fri Aug 24 02:25:10 2007

Asterisk developer's documentation


dial.h File Reference

Dialing API. More...

This graph shows which files directly or indirectly include this file:

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_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.
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.


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_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_MAX,         /*!< End terminator -- must always remain last */
00043 };

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 46 of file dial.h.

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


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

References AST_DIAL_RESULT_ANSWERED, AST_LIST_FIRST, and ast_dial::state.

Referenced by dial_trunk(), and sla_handle_dial_state_event().

00584 {
00585    if (!dial)
00586       return NULL;
00587 
00588    return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
00589 }

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

References ast_calloc, AST_LIST_INSERT_TAIL, and ast_dial::num.

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

00194 {
00195    struct ast_dial_channel *channel = NULL;
00196 
00197    /* Make sure we have required arguments */
00198    if (!dial || !tech || !device)
00199       return -1;
00200 
00201    /* Allocate new memory for dialed channel structure */
00202    if (!(channel = ast_calloc(1, sizeof(*channel))))
00203       return -1;
00204 
00205    /* Record technology and device for when we actually dial */
00206    channel->tech = tech;
00207    channel->device = device;
00208 
00209    /* Grab reference number from dial structure */
00210    channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
00211 
00212    /* Insert into channels list */
00213    AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
00214 
00215    return channel->num;
00216 }

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

References ast_calloc, AST_LIST_HEAD_INIT_NOLOCK, and AST_PTHREADT_NULL.

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

00173 {
00174    struct ast_dial *dial = NULL;
00175 
00176    /* Allocate new memory for structure */
00177    if (!(dial = ast_calloc(1, sizeof(*dial))))
00178       return NULL;
00179 
00180    /* Initialize list of channels */
00181    AST_LIST_HEAD_INIT_NOLOCK(&dial->channels);
00182 
00183    /* Initialize thread to NULL */
00184    dial->thread = AST_PTHREADT_NULL;
00185 
00186    return dial;
00187 }

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

References AST_DIAL_OPTION_MAX, ast_hangup(), AST_LIST_TRAVERSE, free, 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().

00657 {
00658    int i = 0;
00659    struct ast_dial_channel *channel = NULL;
00660 
00661    if (!dial)
00662       return -1;
00663    
00664    /* Hangup and deallocate all the dialed channels */
00665    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00666       /* Disable any enabled options */
00667       for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
00668          if (!channel->options[i])
00669             continue;
00670          if (option_types[i].disable)
00671             option_types[i].disable(channel->options[i]);
00672          channel->options[i] = NULL;
00673       }
00674       /* Hang up channel if need be */
00675       if (channel->owner) {
00676          ast_hangup(channel->owner);
00677          channel->owner = NULL;
00678       }
00679       /* Free structure */
00680       free(channel);
00681    }
00682        
00683    /* Disable any enabled options globally */
00684    for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
00685       if (!dial->options[i])
00686          continue;
00687       if (option_types[i].disable)
00688          option_types[i].disable(dial->options[i]);
00689       dial->options[i] = NULL;
00690    }
00691 
00692    /* Free structure */
00693    free(dial);
00694 
00695    return 0;
00696 }

void ast_dial_hangup ( struct ast_dial dial  ) 

Hangup channels.

Note:
Hangup all active channels
Parameters:
dial Dialing structure

Definition at line 634 of file dial.c.

References ast_hangup(), AST_LIST_TRAVERSE, and ast_dial_channel::owner.

Referenced by ast_dial_run(), and page_exec().

00635 {
00636    struct ast_dial_channel *channel = NULL;
00637 
00638    if (!dial)
00639       return;
00640    
00641    AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00642       if (channel->owner) {
00643          ast_hangup(channel->owner);
00644          channel->owner = NULL;
00645       }
00646    }
00647 
00648    return;
00649 }

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

References AST_DIAL_RESULT_FAILED, AST_PTHREADT_NULL, AST_PTHREADT_STOP, and ast_dial::state.

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

00605 {
00606    pthread_t thread;
00607 
00608    /* If the dial structure is not running in async, return failed */
00609    if (dial->thread == AST_PTHREADT_NULL)
00610       return AST_DIAL_RESULT_FAILED;
00611 
00612    /* Record thread */
00613    thread = dial->thread;
00614 
00615    /* Stop the thread */
00616    dial->thread = AST_PTHREADT_STOP;
00617 
00618    /* Now we signal it with SIGURG so it will break out of it's waitfor */
00619    pthread_kill(thread, SIGURG);
00620 
00621    /* Finally wait for the thread to exit */
00622    pthread_join(thread, NULL);
00623 
00624    /* Yay thread is all gone */
00625    dial->thread = AST_PTHREADT_NULL;
00626 
00627    return dial->state;
00628 }

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

References AST_LIST_EMPTY, AST_LIST_LAST, AST_LIST_TRAVERSE, ast_option_types::disable, ast_dial_channel::num, option_types, and ast_dial_channel::options.

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

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

References AST_LIST_EMPTY, AST_LIST_LAST, AST_LIST_TRAVERSE, ast_option_types::enable, ast_dial_channel::num, option_types, and ast_dial_channel::options.

00727 {
00728    struct ast_dial_channel *channel = NULL;
00729 
00730    /* Ensure we have required arguments */
00731    if (!dial || AST_LIST_EMPTY(&dial->channels))
00732       return -1;
00733    
00734    /* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
00735    if (AST_LIST_LAST(&dial->channels)->num != num) {
00736       AST_LIST_TRAVERSE(&dial->channels, channel, list) {
00737          if (channel->num == num)
00738             break;
00739       }
00740    } else {
00741       channel = AST_LIST_LAST(&dial->channels);
00742    }
00743 
00744    /* If none found, return failure */
00745    if (!channel)
00746       return -1;
00747 
00748    /* If the option is already enabled, return failure */
00749    if (channel->options[option])
00750       return -1;
00751 
00752         /* Execute enable callback if it exists, if not simply make sure the value is set */
00753    if (option_types[option].enable)
00754       channel->options[option] = option_types[option].enable(data);
00755    else
00756       channel->options[option] = (void*)1;
00757 
00758    return 0;
00759 }

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

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

00767 {
00768         /* If the option is not enabled, return failure */
00769         if (!dial->options[option])
00770                 return -1;
00771 
00772    /* Execute callback of option to disable if it exists */
00773    if (option_types[option].disable)
00774       option_types[option].disable(dial->options[option]);
00775 
00776    /* Finally disable option on the structure */
00777    dial->options[option] = NULL;
00778 
00779         return 0;
00780 }

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

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

Referenced by page_exec().

00705 {
00706    /* If the option is already enabled, return failure */
00707    if (dial->options[option])
00708       return -1;
00709 
00710    /* Execute enable callback if it exists, if not simply make sure the value is set */
00711    if (option_types[option].enable)
00712       dial->options[option] = option_types[option].enable(data);
00713    else
00714       dial->options[option] = (void*)1;
00715 
00716    return 0;
00717 }

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 543 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(), LOG_DEBUG, monitor_dial(), and ast_dial::state.

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

00544 {
00545    enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
00546 
00547    /* Ensure required arguments are passed */
00548    if (!dial || (!chan && !async)) {
00549       ast_log(LOG_DEBUG, "invalid #1\n");
00550       return AST_DIAL_RESULT_INVALID;
00551    }
00552 
00553    /* If there are no channels to dial we can't very well try to dial them */
00554    if (AST_LIST_EMPTY(&dial->channels)) {
00555       ast_log(LOG_DEBUG, "invalid #2\n");
00556       return AST_DIAL_RESULT_INVALID;
00557    }
00558 
00559    /* Dial each requested channel */
00560    if (!begin_dial(dial, chan))
00561       return AST_DIAL_RESULT_FAILED;
00562 
00563    /* If we are running async spawn a thread and send it away... otherwise block here */
00564    if (async) {
00565       dial->state = AST_DIAL_RESULT_TRYING;
00566       /* Try to create a thread */
00567       if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
00568          /* Failed to create the thread - hangup all dialed channels and return failed */
00569          ast_dial_hangup(dial);
00570          res = AST_DIAL_RESULT_FAILED;
00571       }
00572    } else {
00573       res = monitor_dial(dial, chan);
00574    }
00575 
00576    return res;
00577 }

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

References ast_dial::state_callback.

Referenced by sla_ring_station().

00825 {
00826    dial->state_callback = callback;
00827 }

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

References ast_dial::state.

Referenced by dial_trunk(), and sla_handle_dial_state_event().

00596 {
00597    return dial->state;
00598 }


Generated on Fri Aug 24 02:25:11 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1