Fri Aug 24 02:25:36 2007

Asterisk developer's documentation


devicestate.c File Reference

Device state management. More...

#include "asterisk.h"
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "asterisk/channel.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/linkedlists.h"
#include "asterisk/logger.h"
#include "asterisk/devicestate.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/options.h"

Include dependency graph for devicestate.c:

Go to the source code of this file.

Data Structures

struct  devstate_cb
 A device state watcher (callback). More...
struct  devstate_prov
 A device state provider (not a channel). More...
struct  state_change

Functions

static int __ast_device_state_changed_literal (char *buf)
int ast_device_state (const char *device)
 Asks a channel for device state.
int ast_device_state_changed (const char *fmt,...)
 Accept change notification, add it to change queue.
int ast_device_state_changed_literal (const char *dev)
 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.
static AST_LIST_HEAD_STATIC (state_changes, state_change)
 The state change queue. State changes are queued for processing by a separate thread.
static AST_LIST_HEAD_STATIC (devstate_cbs, devstate_cb)
 A device state watcher list.
static AST_LIST_HEAD_STATIC (devstate_provs, devstate_prov)
 A list of providers.
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.
static void * do_devstate_changes (void *data)
 Go through the dev state change queue and update changes in the dev state thread.
static void do_state_change (const char *device)
 Notify callback watchers of change, and notify PBX core for hint updates Normally executed within a separate thread.
static int getproviderstate (const char *provider, const char *address)
 Get provider device state.

Variables

static ast_cond_t change_pending
 Flag for the queue.
static pthread_t change_thread = AST_PTHREADT_NULL
 The device state change notification thread.
static const char * devstatestring []
 Device state strings for printing.


Detailed Description

Device state management.

Author:
Mark Spencer <markster@digium.com>

Definition in file devicestate.c.


Function Documentation

static int __ast_device_state_changed_literal ( char *  buf  )  [static]

Definition at line 381 of file devicestate.c.

References ast_calloc, ast_cond_signal(), AST_LIST_FIRST, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), AST_PTHREADT_NULL, do_state_change(), LOG_DEBUG, and option_debug.

Referenced by ast_device_state_changed(), and ast_device_state_changed_literal().

00382 {
00383    char *device;
00384    struct state_change *change;
00385    char *tmp = NULL;
00386 
00387    if (option_debug > 2)
00388       ast_log(LOG_DEBUG, "Notification of state change to be queued on device/channel %s\n", buf);
00389 
00390    device = buf;
00391    
00392    tmp = strrchr(device, '-');
00393    if (tmp)
00394       *tmp = '\0';
00395    
00396    if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
00397       /* we could not allocate a change struct, or */
00398       /* there is no background thread, so process the change now */
00399       do_state_change(device);
00400    } else {
00401       /* queue the change */
00402       strcpy(change->device, device);
00403       AST_LIST_LOCK(&state_changes);
00404       AST_LIST_INSERT_TAIL(&state_changes, change, list);
00405       if (AST_LIST_FIRST(&state_changes) == change)
00406          /* the list was empty, signal the thread */
00407          ast_cond_signal(&change_pending);
00408       AST_LIST_UNLOCK(&state_changes);
00409    }
00410 
00411    return 1;
00412 }

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 216 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(), do_state_change(), and transmit_state_notify().

00217 {
00218    char *buf;
00219    char *number;
00220    const struct ast_channel_tech *chan_tech;
00221    int res = 0;
00222    /*! \brief Channel driver that provides device state */
00223    char *tech;
00224    /*! \brief Another provider of device state */
00225    char *provider = NULL;
00226    
00227    buf = ast_strdupa(device);
00228    tech = strsep(&buf, "/");
00229    number = buf;
00230    if (!number) {
00231       provider = strsep(&tech, ":");
00232       if (!provider)
00233          return AST_DEVICE_INVALID;
00234       /* We have a provider */
00235       number = tech;
00236       tech = NULL;
00237    }
00238 
00239    if (provider)  {
00240       if(option_debug > 2)
00241          ast_log(LOG_DEBUG, "Checking if I can find provider for \"%s\" - number: %s\n", provider, number);
00242       return getproviderstate(provider, number);
00243    }
00244    if (option_debug > 3)
00245       ast_log(LOG_DEBUG, "No provider found, checking channel drivers for %s - %s\n", tech, number);
00246 
00247    chan_tech = ast_get_channel_tech(tech);
00248    if (!chan_tech)
00249       return AST_DEVICE_INVALID;
00250 
00251    if (!chan_tech->devicestate)  /* Does the channel driver support device state notification? */
00252       return ast_parse_device_state(device); /* No, try the generic function */
00253    else {
00254       res = chan_tech->devicestate(number);  /* Ask the channel driver for device state */
00255       if (res == AST_DEVICE_UNKNOWN) {
00256          res = ast_parse_device_state(device);
00257          /* at this point we know the device exists, but the channel driver
00258             could not give us a state; if there is no channel state available,
00259             it must be 'not in use'
00260          */
00261          if (res == AST_DEVICE_UNKNOWN)
00262             res = AST_DEVICE_NOT_INUSE;
00263          return res;
00264       } else
00265          return res;
00266    }
00267 }

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

Accept change notification, add it to change queue.

Definition at line 422 of file devicestate.c.

References __ast_device_state_changed_literal(), and AST_MAX_EXTENSION.

Referenced by __expire_registry(), __iax2_poke_noanswer(), __login_exec(), action_agent_callback_login(), agent_hangup(), agent_logoff_maintenance(), conf_run(), expire_register(), handle_response_peerpoke(), notify_metermaids(), reg_source_db(), register_verify(), reload_agents(), sip_peer_hold(), sip_poke_noanswer(), sla_change_trunk_state(), sla_handle_hold_event(), sla_station_exec(), socket_process(), update_call_counter(), and update_registry().

00423 {
00424    char buf[AST_MAX_EXTENSION];
00425    va_list ap;
00426 
00427    va_start(ap, fmt);
00428    vsnprintf(buf, sizeof(buf), fmt, ap);
00429    va_end(ap);
00430    return __ast_device_state_changed_literal(buf);
00431 }

int ast_device_state_changed_literal ( const char *  device  ) 

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

References __ast_device_state_changed_literal(), and ast_strdupa.

Referenced by ast_channel_free(), and ast_setstate().

00415 {
00416    char *buf;
00417    buf = ast_strdupa(dev);
00418    return __ast_device_state_changed_literal(buf);
00419 }

int ast_device_state_engine_init ( void   ) 

Initialize the device state engine in separate thread.

Definition at line 459 of file devicestate.c.

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

Referenced by main().

00460 {
00461    ast_cond_init(&change_pending, NULL);
00462    if (ast_pthread_create_background(&change_thread, NULL, do_devstate_changes, NULL) < 0) {
00463       ast_log(LOG_ERROR, "Unable to start device state change thread.\n");
00464       return -1;
00465    }
00466 
00467    return 0;
00468 }

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

References ast_calloc, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, and AST_LIST_UNLOCK.

Referenced by load_module().

00328 {
00329    struct devstate_cb *devcb;
00330 
00331    if (!callback || !(devcb = ast_calloc(1, sizeof(*devcb))))
00332       return -1;
00333 
00334    devcb->data = data;
00335    devcb->callback = callback;
00336 
00337    AST_LIST_LOCK(&devstate_cbs);
00338    AST_LIST_INSERT_HEAD(&devstate_cbs, devcb, list);
00339    AST_LIST_UNLOCK(&devstate_cbs);
00340 
00341    return 0;
00342 }

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

00346 {
00347    struct devstate_cb *devcb;
00348 
00349    AST_LIST_LOCK(&devstate_cbs);
00350    AST_LIST_TRAVERSE_SAFE_BEGIN(&devstate_cbs, devcb, list) {
00351       if ((devcb->callback == callback) && (devcb->data == data)) {
00352          AST_LIST_REMOVE_CURRENT(&devstate_cbs, list);
00353          free(devcb);
00354          break;
00355       }
00356    }
00357    AST_LIST_TRAVERSE_SAFE_END;
00358    AST_LIST_UNLOCK(&devstate_cbs);
00359 }

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

References ast_calloc, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, and AST_LIST_UNLOCK.

Referenced by load_module().

00271 {
00272    struct devstate_prov *devprov;
00273 
00274    if (!callback || !(devprov = ast_calloc(1, sizeof(*devprov))))
00275       return -1;
00276 
00277    devprov->callback = callback;
00278    ast_copy_string(devprov->label, label, sizeof(devprov->label));
00279 
00280    AST_LIST_LOCK(&devstate_provs);
00281    AST_LIST_INSERT_HEAD(&devstate_provs, devprov, list);
00282    AST_LIST_UNLOCK(&devstate_provs);
00283 
00284    return 0;
00285 }

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

00289 {
00290    struct devstate_prov *devcb;
00291 
00292    AST_LIST_LOCK(&devstate_provs);
00293    AST_LIST_TRAVERSE_SAFE_BEGIN(&devstate_provs, devcb, list) {
00294       if (!strcasecmp(devcb->label, label)) {
00295          AST_LIST_REMOVE_CURRENT(&devstate_provs, list);
00296          free(devcb);
00297          break;
00298       }
00299    }
00300    AST_LIST_TRAVERSE_SAFE_END;
00301    AST_LIST_UNLOCK(&devstate_provs);
00302 }

static AST_LIST_HEAD_STATIC ( state_changes  ,
state_change   
) [static]

The state change queue. State changes are queued for processing by a separate thread.

static AST_LIST_HEAD_STATIC ( devstate_cbs  ,
devstate_cb   
) [static]

A device state watcher list.

static AST_LIST_HEAD_STATIC ( devstate_provs  ,
devstate_prov   
) [static]

A list of providers.

int 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 192 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().

00193 {
00194    struct ast_channel *chan;
00195    char match[AST_CHANNEL_NAME];
00196    int res;
00197 
00198    ast_copy_string(match, device, sizeof(match)-1);
00199    strcat(match, "-");
00200    chan = ast_get_channel_by_name_prefix_locked(match, strlen(match));
00201 
00202    if (!chan)
00203       return AST_DEVICE_UNKNOWN;
00204 
00205    if (chan->_state == AST_STATE_RINGING)
00206       res = AST_DEVICE_RINGING;
00207    else
00208       res = AST_DEVICE_INUSE;
00209    
00210    ast_channel_unlock(chan);
00211 
00212    return res;
00213 }

const char* devstate2str ( int  devstate  ) 

Convert device state to text string for output.

Parameters:
devstate Current device state

Definition at line 182 of file devicestate.c.

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

00183 {
00184    return devstatestring[devstate];
00185 }

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

Go through the dev state change queue and update changes in the dev state thread.

Definition at line 434 of file devicestate.c.

References ast_cond_wait(), AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, do_state_change(), and free.

Referenced by ast_device_state_engine_init().

00435 {
00436    struct state_change *cur;
00437 
00438    AST_LIST_LOCK(&state_changes);
00439    for(;;) {
00440       /* the list lock will _always_ be held at this point in the loop */
00441       cur = AST_LIST_REMOVE_HEAD(&state_changes, list);
00442       if (cur) {
00443          /* we got an entry, so unlock the list while we process it */
00444          AST_LIST_UNLOCK(&state_changes);
00445          do_state_change(cur->device);
00446          free(cur);
00447          AST_LIST_LOCK(&state_changes);
00448       } else {
00449          /* there was no entry, so atomically unlock the list and wait for
00450             the condition to be signalled (returns with the lock held) */
00451          ast_cond_wait(&change_pending, &state_changes.lock);
00452       }
00453    }
00454 
00455    return NULL;
00456 }

static void do_state_change ( const char *  device  )  [static]

Notify callback watchers of change, and notify PBX core for hint updates Normally executed within a separate thread.

Definition at line 364 of file devicestate.c.

References ast_device_state(), ast_hint_state_changed(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), devstate_cb::callback, devstate_cb::data, devstate2str(), LOG_DEBUG, and option_debug.

Referenced by __ast_device_state_changed_literal(), and do_devstate_changes().

00365 {
00366    int state;
00367    struct devstate_cb *devcb;
00368 
00369    state = ast_device_state(device);
00370    if (option_debug > 2)
00371       ast_log(LOG_DEBUG, "Changing state for %s - state %d (%s)\n", device, state, devstate2str(state));
00372 
00373    AST_LIST_LOCK(&devstate_cbs);
00374    AST_LIST_TRAVERSE(&devstate_cbs, devcb, list)
00375       devcb->callback(device, state, devcb->data);
00376    AST_LIST_UNLOCK(&devstate_cbs);
00377 
00378    ast_hint_state_changed(device);
00379 }

static int getproviderstate ( const char *  provider,
const char *  address 
) [static]

Get provider device state.

Definition at line 305 of file devicestate.c.

References AST_DEVICE_INVALID, AST_LIST_LOCK, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_log(), devstate_prov::callback, devstate_prov::label, LOG_DEBUG, and option_debug.

Referenced by ast_device_state().

00306 {
00307    struct devstate_prov *devprov;
00308    int res = AST_DEVICE_INVALID;
00309 
00310 
00311    AST_LIST_LOCK(&devstate_provs);
00312    AST_LIST_TRAVERSE_SAFE_BEGIN(&devstate_provs, devprov, list) {
00313       if(option_debug > 4)
00314          ast_log(LOG_DEBUG, "Checking provider %s with %s\n", devprov->label, provider);
00315 
00316       if (!strcasecmp(devprov->label, provider)) {
00317          res = devprov->callback(address);
00318          break;
00319       }
00320    }
00321    AST_LIST_TRAVERSE_SAFE_END;
00322    AST_LIST_UNLOCK(&devstate_provs);
00323    return res;
00324 }


Variable Documentation

ast_cond_t change_pending [static]

Flag for the queue.

Definition at line 176 of file devicestate.c.

pthread_t change_thread = AST_PTHREADT_NULL [static]

The device state change notification thread.

Definition at line 173 of file devicestate.c.

const char* devstatestring[] [static]

Device state strings for printing.

Definition at line 131 of file devicestate.c.


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