Thu Jul 9 13:41:16 2009

Asterisk developer's documentation


devicestate.h File Reference

Device state management. More...

Go to the source code of this file.

Enumerations

enum  ast_device_state {
  AST_DEVICE_UNKNOWN, AST_DEVICE_NOT_INUSE, AST_DEVICE_INUSE, AST_DEVICE_BUSY,
  AST_DEVICE_INVALID, AST_DEVICE_UNAVAILABLE, AST_DEVICE_RINGING, AST_DEVICE_RINGINUSE,
  AST_DEVICE_ONHOLD
}
 Device States. More...

Functions

enum ast_device_state ast_device_state (const char *device)
 Asks a channel for device state.
int ast_device_state_changed (const char *fmt,...)
 Tells Asterisk the State for Device is changed.
int ast_device_state_changed_literal (const char *device)
 Tells Asterisk the State for Device is changed.
int ast_devstate_changed (enum ast_device_state state, const char *fmt,...)
 Tells Asterisk the State for Device is changed.
int ast_devstate_changed_literal (enum ast_device_state state, const char *device)
 Tells Asterisk the State for Device is changed.
int ast_devstate_prov_add (const char *label, ast_devstate_prov_cb_type callback)
 Add device state provider.
int ast_devstate_prov_del (const char *label)
 Remove device state provider.
const char * ast_devstate_str (enum ast_device_state devstate)
 Convert device state to text string that is easier to parse.
enum ast_device_state ast_devstate_val (const char *val)
 Convert device state from text to integer value.
enum ast_device_state ast_parse_device_state (const char *device)
 Search the Channels by Name.
const char * devstate2str (enum ast_device_state devstate)
 Convert device state to text string for output.

Variables

enum ast_device_state(*) ast_devstate_prov_cb_type (const char *data)
 Devicestate provider call back.


Detailed Description

Device state management.

To subscribe to device state changes, use the generic ast_event_subscribe method. For an example, see apps/app_queue.c.

Todo:
Currently, when the state of a device changes, the device state provider calls one of the functions defined here to queue an object to say that the state of a device has changed. However, this does not include the new state. Another thread processes these device state change objects and calls the device state provider's callback to figure out what the new state is. It would make a lot more sense for the new state to be included in the original function call that says the state of a device has changed. However, it will take a lot of work to change this.

Definition in file devicestate.h.


Enumeration Type Documentation

enum ast_device_state

Device States.

Note:
The order of these states may not change because they are included in Asterisk events which may be transmitted across the network to other servers.
Enumerator:
AST_DEVICE_UNKNOWN  Device is valid but channel didn't know state
AST_DEVICE_NOT_INUSE  Device is not used
AST_DEVICE_INUSE  Device is in use
AST_DEVICE_BUSY  Device is busy
AST_DEVICE_INVALID  Device is invalid
AST_DEVICE_UNAVAILABLE  Device is unavailable
AST_DEVICE_RINGING  Device is ringing
AST_DEVICE_RINGINUSE  Device is ringing *and* in use
AST_DEVICE_ONHOLD  Device is on hold

Definition at line 49 of file devicestate.h.

00049                       {
00050    AST_DEVICE_UNKNOWN,      /*!< Device is valid but channel didn't know state */
00051    AST_DEVICE_NOT_INUSE,    /*!< Device is not used */
00052    AST_DEVICE_INUSE,        /*!< Device is in use */
00053    AST_DEVICE_BUSY,         /*!< Device is busy */
00054    AST_DEVICE_INVALID,      /*!< Device is invalid */
00055    AST_DEVICE_UNAVAILABLE,  /*!< Device is unavailable */
00056    AST_DEVICE_RINGING,      /*!< Device is ringing */
00057    AST_DEVICE_RINGINUSE,    /*!< Device is ringing *and* in use */
00058    AST_DEVICE_ONHOLD,       /*!< Device is on hold */
00059 };


Function Documentation

enum ast_device_state ast_device_state ( const char *  device  ) 

Asks a channel for device state.

Parameters:
device like a dial string
Asks a channel for device state, data is normally a number from a dial string used by the low level module Tries the channel device state callback if not supported search in the active channels list for the device.

Return values:
an AST_DEVICE_??? state
-1 on failure

Definition at line 344 of file devicestate.c.

References _ast_device_state().

Referenced by _ast_device_state(), ast_extension_state2(), ast_parse_device_state(), chanavail_exec(), create_queue_member(), device_state_cb(), devstate_cached(), devstate_read(), devstate_write(), do_state_change(), handle_cli_devstate_change(), page_exec(), ring_entry(), skinny_extensionstate_cb(), sla_state(), and transmit_state_notify().

00345 {
00346    /* This function is called from elsewhere in the code to find out the
00347     * current state of a device.  Check the cache, first. */
00348 
00349    return _ast_device_state(device, 1);
00350 }

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

Tells Asterisk the State for Device is changed.

Parameters:
fmt device name like a dial string with format parameters
Asterisk polls the new extension states and calls the registered callbacks for the changed extensions

Return values:
0 on success
-1 on failure
Note:
This is deprecated in favor of ast_devstate_changed()

Definition at line 488 of file devicestate.c.

References AST_DEVICE_UNKNOWN, ast_devstate_changed_literal(), AST_MAX_EXTENSION, and buf.

Referenced by __expire_registry(), __iax2_poke_noanswer(), agent_call(), agent_hangup(), agent_logoff_maintenance(), agent_read(), expire_register(), handle_offhook_message(), handle_onhook_message(), handle_response_peerpoke(), handle_soft_key_event_message(), handle_stimulus_message(), login_exec(), reg_source_db(), register_verify(), reload_agents(), sip_peer_hold(), sip_poke_noanswer(), skinny_unregister(), socket_process(), update_call_counter(), and update_registry().

00489 {
00490    char buf[AST_MAX_EXTENSION];
00491    va_list ap;
00492 
00493    va_start(ap, fmt);
00494    vsnprintf(buf, sizeof(buf), fmt, ap);
00495    va_end(ap);
00496 
00497    return ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, buf);
00498 }

int ast_device_state_changed_literal ( const char *  device  ) 

Tells Asterisk the State for Device is changed.

Parameters:
device device name like a dial string
Asterisk polls the new extension states and calls the registered callbacks for the changed extensions

Return values:
0 on success
-1 on failure
Note:
This is deprecated in favor of ast_devstate_changed_literal()

Definition at line 470 of file devicestate.c.

References AST_DEVICE_UNKNOWN, and ast_devstate_changed_literal().

Referenced by ast_channel_free(), ast_setstate(), and dahdi_new().

00471 {
00472    return ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, dev);
00473 }

int ast_devstate_changed ( enum ast_device_state  state,
const char *  fmt,
  ... 
)

Tells Asterisk the State for Device is changed.

Parameters:
state the new state of the device
fmt device name like a dial string with format parameters
The new state of the device will be sent off to any subscribers of device states. It will also be stored in the internal event cache.

Return values:
0 on success
-1 on failure

Definition at line 475 of file devicestate.c.

References ast_devstate_changed_literal(), AST_MAX_EXTENSION, and buf.

Referenced by conf_run(), devstate_write(), handle_cli_devstate_change(), load_module(), notify_metermaids(), sla_change_trunk_state(), sla_handle_hold_event(), and sla_station_exec().

00476 {
00477    char buf[AST_MAX_EXTENSION];
00478    va_list ap;
00479 
00480    va_start(ap, fmt);
00481    vsnprintf(buf, sizeof(buf), fmt, ap);
00482    va_end(ap);
00483 
00484    return ast_devstate_changed_literal(state, buf);
00485 }

int ast_devstate_changed_literal ( enum ast_device_state  state,
const char *  device 
)

Tells Asterisk the State for Device is changed.

Parameters:
state the new state of the device
device device name like a dial string with format parameters
The new state of the device will be sent off to any subscribers of device states. It will also be stored in the internal event cache.

Return values:
0 on success
-1 on failure

Definition at line 446 of file devicestate.c.

References ast_calloc, ast_cond_signal(), ast_debug, AST_DEVICE_UNKNOWN, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_UNLOCK, AST_PTHREADT_NULL, CACHE_ON, change_pending, change_thread, devstate_event(), do_state_change(), and devstate_prov::list.

Referenced by ast_device_state_changed(), ast_device_state_changed_literal(), and ast_devstate_changed().

00447 {
00448    struct state_change *change;
00449 
00450    ast_debug(3, "Notification of state change to be queued on device/channel %s\n", device);
00451 
00452    if (state != AST_DEVICE_UNKNOWN) {
00453       devstate_event(device, state, CACHE_ON);
00454    } else if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
00455       /* we could not allocate a change struct, or */
00456       /* there is no background thread, so process the change now */
00457       do_state_change(device);
00458    } else {
00459       /* queue the change */
00460       strcpy(change->device, device);
00461       AST_LIST_LOCK(&state_changes);
00462       AST_LIST_INSERT_TAIL(&state_changes, change, list);
00463       ast_cond_signal(&change_pending);
00464       AST_LIST_UNLOCK(&state_changes);
00465    }
00466 
00467    return 1;
00468 }

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:
0 success
-1 failure

Definition at line 353 of file devicestate.c.

References ast_calloc, ast_copy_string(), AST_RWLIST_INSERT_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and devstate_prov::list.

Referenced by ast_features_init(), and load_module().

00354 {
00355    struct devstate_prov *devprov;
00356 
00357    if (!callback || !(devprov = ast_calloc(1, sizeof(*devprov))))
00358       return -1;
00359 
00360    devprov->callback = callback;
00361    ast_copy_string(devprov->label, label, sizeof(devprov->label));
00362 
00363    AST_RWLIST_WRLOCK(&devstate_provs);
00364    AST_RWLIST_INSERT_HEAD(&devstate_provs, devprov, list);
00365    AST_RWLIST_UNLOCK(&devstate_provs);
00366 
00367    return 0;
00368 }

int ast_devstate_prov_del ( const char *  label  ) 

Remove device state provider.

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

Definition at line 371 of file devicestate.c.

References ast_free, AST_RWLIST_REMOVE_CURRENT, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, devstate_prov::label, and devstate_prov::list.

Referenced by unload_module().

00372 {
00373    struct devstate_prov *devcb;
00374    int res = -1;
00375 
00376    AST_RWLIST_WRLOCK(&devstate_provs);
00377    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&devstate_provs, devcb, list) {
00378       if (!strcasecmp(devcb->label, label)) {
00379          AST_RWLIST_REMOVE_CURRENT(list);
00380          ast_free(devcb);
00381          res = 0;
00382          break;
00383       }
00384    }
00385    AST_RWLIST_TRAVERSE_SAFE_END;
00386    AST_RWLIST_UNLOCK(&devstate_provs);
00387 
00388    return res;
00389 }

const char* ast_devstate_str ( enum ast_device_state  devstate  ) 

Convert device state to text string that is easier to parse.

Parameters:
devstate Current device state

Definition at line 183 of file devicestate.c.

References AST_DEVICE_BUSY, AST_DEVICE_INUSE, AST_DEVICE_INVALID, AST_DEVICE_NOT_INUSE, AST_DEVICE_ONHOLD, AST_DEVICE_RINGING, AST_DEVICE_RINGINUSE, AST_DEVICE_UNAVAILABLE, and AST_DEVICE_UNKNOWN.

Referenced by devstate_read().

00184 {
00185    const char *res = "UNKNOWN";
00186 
00187    switch (state) {
00188    case AST_DEVICE_UNKNOWN:
00189       break;
00190    case AST_DEVICE_NOT_INUSE:
00191       res = "NOT_INUSE";
00192       break;
00193    case AST_DEVICE_INUSE:
00194       res = "INUSE";
00195       break;
00196    case AST_DEVICE_BUSY:
00197       res = "BUSY";
00198       break;
00199    case AST_DEVICE_INVALID:
00200       res = "INVALID";
00201       break;
00202    case AST_DEVICE_UNAVAILABLE:
00203       res = "UNAVAILABLE";
00204       break;
00205    case AST_DEVICE_RINGING:
00206       res = "RINGING";
00207       break;
00208    case AST_DEVICE_RINGINUSE:
00209       res = "RINGINUSE";
00210       break;
00211    case AST_DEVICE_ONHOLD:
00212       res = "ONHOLD";
00213       break;
00214    }
00215 
00216    return res;
00217 }

enum ast_device_state ast_devstate_val ( const char *  val  ) 

Convert device state from text to integer value.

Parameters:
val The text representing the device state. Valid values are anything that comes after AST_DEVICE_ in one of the defined values.
Returns:
The AST_DEVICE_ integer value

Definition at line 219 of file devicestate.c.

References AST_DEVICE_BUSY, AST_DEVICE_INUSE, AST_DEVICE_INVALID, AST_DEVICE_NOT_INUSE, AST_DEVICE_ONHOLD, AST_DEVICE_RINGING, AST_DEVICE_RINGINUSE, AST_DEVICE_UNAVAILABLE, and AST_DEVICE_UNKNOWN.

Referenced by custom_devstate_callback(), devstate_write(), handle_cli_devstate_change(), and load_module().

00220 {
00221    if (!strcasecmp(val, "NOT_INUSE"))
00222       return AST_DEVICE_NOT_INUSE;
00223    else if (!strcasecmp(val, "INUSE"))
00224       return AST_DEVICE_INUSE;
00225    else if (!strcasecmp(val, "BUSY"))
00226       return AST_DEVICE_BUSY;
00227    else if (!strcasecmp(val, "INVALID"))
00228       return AST_DEVICE_INVALID;
00229    else if (!strcasecmp(val, "UNAVAILABLE"))
00230       return AST_DEVICE_UNAVAILABLE;
00231    else if (!strcasecmp(val, "RINGING"))
00232       return AST_DEVICE_RINGING;
00233    else if (!strcasecmp(val, "RINGINUSE"))
00234       return AST_DEVICE_RINGINUSE;
00235    else if (!strcasecmp(val, "ONHOLD"))
00236       return AST_DEVICE_ONHOLD;
00237 
00238    return AST_DEVICE_UNKNOWN;
00239 }

enum ast_device_state ast_parse_device_state ( const char *  device  ) 

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 246 of file devicestate.c.

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

Referenced by _ast_device_state(), and chanavail_exec().

00247 {
00248    struct ast_channel *chan;
00249    char match[AST_CHANNEL_NAME];
00250    enum ast_device_state res;
00251 
00252    ast_copy_string(match, device, sizeof(match)-1);
00253    strcat(match, "-");
00254    chan = ast_get_channel_by_name_prefix_locked(match, strlen(match));
00255 
00256    if (!chan)
00257       return AST_DEVICE_UNKNOWN;
00258 
00259    if (chan->_state == AST_STATE_RINGING)
00260       res = AST_DEVICE_RINGING;
00261    else
00262       res = AST_DEVICE_INUSE;
00263    
00264    ast_channel_unlock(chan);
00265 
00266    return res;
00267 }

const char* devstate2str ( enum ast_device_state  devstate  ) 

Convert device state to text string for output.

Parameters:
devstate Current device state

Definition at line 178 of file devicestate.c.

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

00179 {
00180    return devstatestring[devstate];
00181 }


Variable Documentation

enum ast_device_state(*) ast_devstate_prov_cb_type(const char *data)

Devicestate provider call back.

Definition at line 62 of file devicestate.h.


Generated on Thu Jul 9 13:41:16 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7