Thu Oct 8 21:57:48 2009

Asterisk developer's documentation


devicestate.h File Reference

Device state management. More...

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

Go to the source code of this file.

Defines

#define AST_DEVICE_BUSY   3
#define AST_DEVICE_INUSE   2
#define AST_DEVICE_INVALID   4
#define AST_DEVICE_NOT_INUSE   1
#define AST_DEVICE_ONHOLD   8
#define AST_DEVICE_RINGING   6
#define AST_DEVICE_RINGINUSE   7
#define AST_DEVICE_UNAVAILABLE   5
#define AST_DEVICE_UNKNOWN   0

Typedefs

typedef int(* ast_devstate_cb_type )(const char *dev, int state, void *data, char *cid_num, char *cid_name)
 Devicestate watcher call back.
typedef int(* ast_devstate_prov_cb_type )(const char *data)
 Devicestate provider call back.

Functions

int ast_device_state (const char *device)
 Asks a channel for device state.
int ast_device_state_changed (const char *fmt,...) __attribute__((format(printf
 Tells Asterisk the State for Device is changed.
int int ast_device_state_changed_literal (const char *device, const char *cid_num, const char *cid_name)
 Tells Asterisk the State for Device is changed.
int ast_device_state_engine_init (void)
 Initialize the device state engine in separate thread.
int ast_devstate_add (ast_devstate_cb_type callback, void *data)
 Registers a device state change callback.
void ast_devstate_del (ast_devstate_cb_type callback, void *data)
 Unregisters a device state change callback.
int ast_devstate_prov_add (const char *label, ast_devstate_prov_cb_type callback)
 Add device state provider.
void ast_devstate_prov_del (const char *label)
 Remove device state provider.
int ast_parse_device_state (const char *device)
 Search the Channels by Name.
const char * devstate2str (int devstate)
 Convert device state to text string for output.


Detailed Description

Device state management.

Definition in file devicestate.h.


Define Documentation

#define AST_DEVICE_BUSY   3

Device is busy

Definition at line 37 of file devicestate.h.

Referenced by agent_devicestate(), ast_devstate_str(), ast_devstate_val(), ast_extension_state2(), and sip_devicestate().

#define AST_DEVICE_INUSE   2

#define AST_DEVICE_INVALID   4

#define AST_DEVICE_NOT_INUSE   1

#define AST_DEVICE_ONHOLD   8

Device is on hold

Definition at line 47 of file devicestate.h.

Referenced by ast_devstate_str(), ast_devstate_val(), ast_extension_state2(), sip_devicestate(), and sla_state().

#define AST_DEVICE_RINGING   6

#define AST_DEVICE_RINGINUSE   7

Device is ringing *and* in use

Definition at line 45 of file devicestate.h.

Referenced by ast_devstate_str(), ast_devstate_val(), ast_extension_state2(), and sip_devicestate().

#define AST_DEVICE_UNAVAILABLE   5

#define AST_DEVICE_UNKNOWN   0


Typedef Documentation

typedef int(* ast_devstate_cb_type)(const char *dev, int state, void *data, char *cid_num, char *cid_name)

Devicestate watcher call back.

Definition at line 50 of file devicestate.h.

typedef int(* ast_devstate_prov_cb_type)(const char *data)

Devicestate provider call back.

Definition at line 53 of file devicestate.h.


Function Documentation

int ast_device_state ( const char *  device  ) 

Asks a channel for device state.

Parameters:
device like a dialstring Asks a channel for device state, data is normaly a number from dialstring used by the low level module Trys the channel devicestate callback if not supported search in the active channels list for the device. Returns an AST_DEVICE_??? state -1 on failure

Channel driver that provides device state

Another provider of device state

Definition at line 134 of file devicestate.c.

References AST_DEVICE_INVALID, AST_DEVICE_NOT_INUSE, AST_DEVICE_UNKNOWN, ast_get_channel_tech(), ast_log(), ast_parse_device_state(), ast_strdupa, ast_channel_tech::devicestate, getproviderstate(), LOG_DEBUG, option_debug, and strsep().

Referenced by ast_extension_state2(), chanavail_exec(), create_queue_member(), devstate_read(), do_state_change(), reload_queues(), ring_entry(), and transmit_state_notify().

00135 {
00136    char *buf;
00137    char *number;
00138    const struct ast_channel_tech *chan_tech;
00139    int res = 0;
00140    /*! \brief Channel driver that provides device state */
00141    char *tech;
00142    /*! \brief Another provider of device state */
00143    char *provider = NULL;
00144    
00145    buf = ast_strdupa(device);
00146    tech = strsep(&buf, "/");
00147    number = buf;
00148    if (!number) {
00149       provider = strsep(&tech, ":");
00150       if (!provider)
00151          return AST_DEVICE_INVALID;
00152       /* We have a provider */
00153       number = tech;
00154       tech = NULL;
00155    }
00156 
00157    if (provider)  {
00158       if(option_debug > 2)
00159          ast_log(LOG_DEBUG, "Checking if I can find provider for \"%s\" - number: %s\n", provider, number);
00160       return getproviderstate(provider, number);
00161    }
00162    if (option_debug > 3)
00163       ast_log(LOG_DEBUG, "No provider found, checking channel drivers for %s - %s\n", tech, number);
00164 
00165    chan_tech = ast_get_channel_tech(tech);
00166    if (!chan_tech)
00167       return AST_DEVICE_INVALID;
00168 
00169    if (!chan_tech->devicestate)  /* Does the channel driver support device state notification? */
00170       return ast_parse_device_state(device); /* No, try the generic function */
00171    else {
00172       res = chan_tech->devicestate(number);  /* Ask the channel driver for device state */
00173       if (res == AST_DEVICE_UNKNOWN) {
00174          res = ast_parse_device_state(device);
00175          /* at this point we know the device exists, but the channel driver
00176             could not give us a state; if there is no channel state available,
00177             it must be 'not in use'
00178          */
00179          if (res == AST_DEVICE_UNKNOWN)
00180             res = AST_DEVICE_NOT_INUSE;
00181          return res;
00182       } else
00183          return res;
00184    }
00185 }

int ast_device_state_changed ( const char *  fmt,
  ... 
)

Tells Asterisk the State for Device is changed.

Parameters:
fmt devicename like a dialstring with format parameters Asterisk polls the new extensionstates and calls the registered callbacks for the changed extensions Returns 0 on success, -1 on failure

int int ast_device_state_changed_literal ( const char *  device,
const char *  cid_num,
const char *  cid_name 
)

Tells Asterisk the State for Device is changed.

Parameters:
device devicename like a dialstring Asterisk polls the new extensionstates and calls the registered callbacks for the changed extensions Returns 0 on success, -1 on failure

Definition at line 346 of file devicestate.c.

References __ast_device_state_changed_literal(), and ast_strdupa.

Referenced by action_devstate(), ast_channel_free(), ast_setstate_and_callerid(), devstate_cli(), and devstate_exec().

00347 {
00348    char *buf;
00349    char *buf2 = NULL;
00350    char *buf3 = NULL;
00351    buf = ast_strdupa(dev);
00352    if (cid_num)
00353        buf2 = ast_strdupa(cid_num);
00354    if (cid_name)
00355        buf3 = ast_strdupa(cid_name);
00356    return __ast_device_state_changed_literal(buf, 0, buf2, buf3);
00357 }

int ast_device_state_engine_init ( void   ) 

Initialize the device state engine in separate thread.

Definition at line 397 of file devicestate.c.

References ast_cond_init(), ast_log(), ast_pthread_create_background, do_devstate_changes(), and LOG_ERROR.

Referenced by main().

00398 {
00399    ast_cond_init(&change_pending, NULL);
00400    if (ast_pthread_create_background(&change_thread, NULL, do_devstate_changes, NULL) < 0) {
00401       ast_log(LOG_ERROR, "Unable to start device state change thread.\n");
00402       return -1;
00403    }
00404 
00405    return 0;
00406 }

int ast_devstate_add ( ast_devstate_cb_type  callback,
void *  data 
)

Registers a device state change callback.

Parameters:
callback Callback
data to pass to callback The callback is called if the state for extension is changed Return -1 on failure, ID on success

Definition at line 245 of file devicestate.c.

References ast_calloc, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_UNLOCK, devstate_cb::callback, and devstate_cb::data.

Referenced by load_module().

00246 {
00247    struct devstate_cb *devcb;
00248 
00249    if (!callback || !(devcb = ast_calloc(1, sizeof(*devcb))))
00250       return -1;
00251 
00252    devcb->data = data;
00253    devcb->callback = callback;
00254 
00255    AST_LIST_LOCK(&devstate_cbs);
00256    AST_LIST_INSERT_HEAD(&devstate_cbs, devcb, list);
00257    AST_LIST_UNLOCK(&devstate_cbs);
00258 
00259    return 0;
00260 }

void ast_devstate_del ( ast_devstate_cb_type  callback,
void *  data 
)

Unregisters a device state change callback.

Parameters:
callback Callback
data to pass to callback The callback is called if the state for extension is changed Return -1 on failure, ID on success

Definition at line 263 of file devicestate.c.

References AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, devstate_cb::callback, devstate_cb::data, and free.

Referenced by unload_module().

00264 {
00265    struct devstate_cb *devcb;
00266 
00267    AST_LIST_LOCK(&devstate_cbs);
00268    AST_LIST_TRAVERSE_SAFE_BEGIN(&devstate_cbs, devcb, list) {
00269       if ((devcb->callback == callback) && (devcb->data == data)) {
00270          AST_LIST_REMOVE_CURRENT(&devstate_cbs, list);
00271          free(devcb);
00272          break;
00273       }
00274    }
00275    AST_LIST_TRAVERSE_SAFE_END;
00276    AST_LIST_UNLOCK(&devstate_cbs);
00277 }

int ast_devstate_prov_add ( const char *  label,
ast_devstate_prov_cb_type  callback 
)

Add device state provider.

Parameters:
label to use in hint, like label:object
callback Callback
Return values:
-1 failure
0 success

Definition at line 188 of file devicestate.c.

References ast_calloc, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_UNLOCK, devstate_prov::callback, and devstate_prov::label.

Referenced by load_module().

00189 {
00190    struct devstate_prov *devprov;
00191 
00192    if (!callback || !(devprov = ast_calloc(1, sizeof(*devprov))))
00193       return -1;
00194 
00195    devprov->callback = callback;
00196    ast_copy_string(devprov->label, label, sizeof(devprov->label));
00197 
00198    AST_LIST_LOCK(&devstate_provs);
00199    AST_LIST_INSERT_HEAD(&devstate_provs, devprov, list);
00200    AST_LIST_UNLOCK(&devstate_provs);
00201 
00202    return 0;
00203 }

void ast_devstate_prov_del ( const char *  label  ) 

Remove device state provider.

Parameters:
label to use in hint, like label:object
Returns:
nothing

Definition at line 206 of file devicestate.c.

References AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, free, and devstate_prov::label.

Referenced by unload_module().

00207 {
00208    struct devstate_prov *devcb;
00209 
00210    AST_LIST_LOCK(&devstate_provs);
00211    AST_LIST_TRAVERSE_SAFE_BEGIN(&devstate_provs, devcb, list) {
00212       if (!strcasecmp(devcb->label, label)) {
00213          AST_LIST_REMOVE_CURRENT(&devstate_provs, list);
00214          free(devcb);
00215          break;
00216       }
00217    }
00218    AST_LIST_TRAVERSE_SAFE_END;
00219    AST_LIST_UNLOCK(&devstate_provs);
00220 }

int ast_parse_device_state ( const char *  device  ) 

Search the Channels by Name.

Parameters:
device like a dialstring Search the Device in active channels by compare the channelname against the devicename. Compared are only the first chars to the first '-' char. Returns an AST_DEVICE_UNKNOWN if no channel found or AST_DEVICE_INUSE if a channel is found
Search the Channels by Name.

Note:
find channels with the device's name in it This function is only used for channels that does not implement devicestate natively

Definition at line 110 of file devicestate.c.

References ast_channel::_state, AST_CHANNEL_NAME, ast_channel_unlock, AST_DEVICE_INUSE, AST_DEVICE_RINGING, AST_DEVICE_UNKNOWN, ast_get_channel_by_name_prefix_locked(), AST_STATE_RINGING, and match().

Referenced by ast_device_state().

00111 {
00112    struct ast_channel *chan;
00113    char match[AST_CHANNEL_NAME];
00114    int res;
00115 
00116    ast_copy_string(match, device, sizeof(match)-1);
00117    strcat(match, "-");
00118    chan = ast_get_channel_by_name_prefix_locked(match, strlen(match));
00119 
00120    if (!chan)
00121       return AST_DEVICE_UNKNOWN;
00122 
00123    if (chan->_state == AST_STATE_RINGING)
00124       res = AST_DEVICE_RINGING;
00125    else
00126       res = AST_DEVICE_INUSE;
00127    
00128    ast_channel_unlock(chan);
00129 
00130    return res;
00131 }

const char* devstate2str ( int  devstate  ) 

Convert device state to text string for output.

Parameters:
devstate Current device state

Definition at line 100 of file devicestate.c.

Referenced by __queues_show(), do_state_change(), and handle_statechange().

00101 {
00102    return devstatestring[devstate];
00103 }


Generated on Thu Oct 8 21:57:48 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.8