Mon Oct 8 12:39:02 2012

Asterisk developer's documentation


devicestate.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2008, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  * Russell Bryant <russell@digium.com>
00008  *
00009  * See http://www.asterisk.org for more information about
00010  * the Asterisk project. Please do not directly contact
00011  * any of the maintainers of this project for assistance;
00012  * the project provides a web site, mailing lists and IRC
00013  * channels for your use.
00014  *
00015  * This program is free software, distributed under the terms of
00016  * the GNU General Public License Version 2. See the LICENSE file
00017  * at the top of the source tree.
00018  */
00019 
00020 /*! \file
00021  *
00022  * \brief Device state management
00023  *
00024  * \author Mark Spencer <markster@digium.com> 
00025  * \author Russell Bryant <russell@digium.com>
00026  *
00027  * \arg \ref AstExtState
00028  */
00029 
00030 /*! \page AstExtState Extension and device states in Asterisk
00031  *
00032  * (Note that these descriptions of device states and extension
00033  * states have not been updated to the way things work
00034  * in Asterisk 1.6.)
00035  *
00036  * Asterisk has an internal system that reports states
00037  * for an extension. By using the dialplan priority -1,
00038  * also called a \b hint, a connection can be made from an
00039  * extension to one or many devices. The state of the extension
00040  * now depends on the combined state of the devices.
00041  *
00042  * The device state is basically based on the current calls.
00043  * If the devicestate engine can find a call from or to the
00044  * device, it's in use.
00045  * 
00046  * Some channel drivers implement a callback function for 
00047  * a better level of reporting device states. The SIP channel
00048  * has a complicated system for this, which is improved 
00049  * by adding call limits to the configuration.
00050  * 
00051  * Functions that want to check the status of an extension
00052  * register themself as a \b watcher.
00053  * Watchers in this system can subscribe either to all extensions
00054  * or just a specific extensions.
00055  *
00056  * For non-device related states, there's an API called
00057  * devicestate providers. This is an extendible system for
00058  * delivering state information from outside sources or
00059  * functions within Asterisk. Currently we have providers
00060  * for app_meetme.c - the conference bridge - and call
00061  * parking (metermaids).
00062  *
00063  * There are manly three subscribers to extension states 
00064  * within Asterisk:
00065  * - AMI, the manager interface
00066  * - app_queue.c - the Queue dialplan application
00067  * - SIP subscriptions, a.k.a. "blinking lamps" or 
00068  *   "buddy lists"
00069  *
00070  * The CLI command "show hints" show last known state
00071  *
00072  * \note None of these handle user states, like an IM presence
00073  * system. res_jabber.c can subscribe and watch such states
00074  * in jabber/xmpp based systems.
00075  *
00076  * \section AstDevStateArch Architecture for devicestates
00077  *
00078  * When a channel driver or asterisk app changes state for 
00079  * a watched object, it alerts the core. The core queues
00080  * a change. When the change is processed, there's a query
00081  * sent to the channel driver/provider if there's a function
00082  * to handle that, otherwise a channel walk is issued to find
00083  * a channel that involves the object.
00084  * 
00085  * The changes are queued and processed by a separate thread.
00086  * This thread calls the watchers subscribing to status 
00087  * changes for the object. For manager, this results 
00088  * in events. For SIP, NOTIFY requests.
00089  *
00090  * - Device states
00091  *    \arg \ref devicestate.c 
00092  *    \arg \ref devicestate.h 
00093  *
00094  * \section AstExtStateArch Architecture for extension states
00095  * 
00096  * Hints are connected to extension. If an extension changes state
00097  * it checks the hint devices. If there is a hint, the callbacks into
00098  * device states are checked. The aggregated state is set for the hint
00099  * and reported back.
00100  *
00101  * - Extension states
00102  *    \arg \ref AstENUM ast_extension_states
00103  *    \arg \ref pbx.c 
00104  *    \arg \ref pbx.h 
00105  * - Structures
00106  *    - \ref ast_state_cb struct.  Callbacks for watchers
00107  *    - Callback ast_state_cb_type
00108  *    - \ref ast_hint struct.
00109  *    - Functions
00110  *    - ast_extension_state_add()
00111  *    - ast_extension_state_del()
00112  *    - ast_get_hint()
00113  * 
00114  */
00115 
00116 /*** MODULEINFO
00117    <support_level>core</support_level>
00118  ***/
00119 
00120 #include "asterisk.h"
00121 
00122 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
00123 
00124 #include "asterisk/_private.h"
00125 #include "asterisk/channel.h"
00126 #include "asterisk/utils.h"
00127 #include "asterisk/lock.h"
00128 #include "asterisk/linkedlists.h"
00129 #include "asterisk/devicestate.h"
00130 #include "asterisk/pbx.h"
00131 #include "asterisk/app.h"
00132 #include "asterisk/event.h"
00133 
00134 /*! \brief Device state strings for printing */
00135 static const char * const devstatestring[][2] = {
00136    { /* 0 AST_DEVICE_UNKNOWN */     "Unknown",     "UNKNOWN"     }, /*!< Valid, but unknown state */
00137    { /* 1 AST_DEVICE_NOT_INUSE */   "Not in use",  "NOT_INUSE"   }, /*!< Not used */
00138    { /* 2 AST_DEVICE IN USE */      "In use",      "INUSE"       }, /*!< In use */
00139    { /* 3 AST_DEVICE_BUSY */        "Busy",        "BUSY"        }, /*!< Busy */
00140    { /* 4 AST_DEVICE_INVALID */     "Invalid",     "INVALID"     }, /*!< Invalid - not known to Asterisk */
00141    { /* 5 AST_DEVICE_UNAVAILABLE */ "Unavailable", "UNAVAILABLE" }, /*!< Unavailable (not registered) */
00142    { /* 6 AST_DEVICE_RINGING */     "Ringing",     "RINGING"     }, /*!< Ring, ring, ring */
00143    { /* 7 AST_DEVICE_RINGINUSE */   "Ring+Inuse",  "RINGINUSE"   }, /*!< Ring and in use */
00144    { /* 8 AST_DEVICE_ONHOLD */      "On Hold",      "ONHOLD"      }, /*!< On Hold */
00145 };
00146 
00147 /*!\brief Mapping for channel states to device states */
00148 static const struct chan2dev {
00149    enum ast_channel_state chan;
00150    enum ast_device_state dev;
00151 } chan2dev[] = {
00152    { AST_STATE_DOWN,            AST_DEVICE_NOT_INUSE },
00153    { AST_STATE_RESERVED,        AST_DEVICE_INUSE },
00154    { AST_STATE_OFFHOOK,         AST_DEVICE_INUSE },
00155    { AST_STATE_DIALING,         AST_DEVICE_INUSE },
00156    { AST_STATE_RING,            AST_DEVICE_INUSE },
00157    { AST_STATE_RINGING,         AST_DEVICE_RINGING },
00158    { AST_STATE_UP,              AST_DEVICE_INUSE },
00159    { AST_STATE_BUSY,            AST_DEVICE_BUSY },
00160    { AST_STATE_DIALING_OFFHOOK, AST_DEVICE_INUSE },
00161    { AST_STATE_PRERING,         AST_DEVICE_RINGING },
00162    { -100,                      -100 },
00163 };
00164 
00165 /*! \brief  A device state provider (not a channel) */
00166 struct devstate_prov {
00167    char label[40];
00168    ast_devstate_prov_cb_type callback;
00169    AST_RWLIST_ENTRY(devstate_prov) list;
00170 };
00171 
00172 /*! \brief A list of providers */
00173 static AST_RWLIST_HEAD_STATIC(devstate_provs, devstate_prov);
00174 
00175 struct state_change {
00176    AST_LIST_ENTRY(state_change) list;
00177    char device[1];
00178 };
00179 
00180 /*! \brief The state change queue. State changes are queued
00181    for processing by a separate thread */
00182 static AST_LIST_HEAD_STATIC(state_changes, state_change);
00183 
00184 /*! \brief The device state change notification thread */
00185 static pthread_t change_thread = AST_PTHREADT_NULL;
00186 
00187 /*! \brief Flag for the queue */
00188 static ast_cond_t change_pending;
00189 
00190 struct devstate_change {
00191    AST_LIST_ENTRY(devstate_change) entry;
00192    uint32_t state;
00193    struct ast_eid eid;
00194    char device[1];
00195 };
00196 
00197 static struct {
00198    pthread_t thread;
00199    struct ast_event_sub *event_sub;
00200    ast_cond_t cond;
00201    ast_mutex_t lock;
00202    AST_LIST_HEAD_NOLOCK(, devstate_change) devstate_change_q;
00203    unsigned int enabled:1;
00204 } devstate_collector = {
00205    .thread = AST_PTHREADT_NULL,
00206    .enabled = 0,
00207 };
00208 
00209 /* Forward declarations */
00210 static int getproviderstate(const char *provider, const char *address);
00211 
00212 /*! \brief Find devicestate as text message for output */
00213 const char *ast_devstate2str(enum ast_device_state devstate) 
00214 {
00215    return devstatestring[devstate][0];
00216 }
00217 
00218 /* Deprecated interface (not prefixed with ast_) */
00219 const char *devstate2str(enum ast_device_state devstate) 
00220 {
00221    return devstatestring[devstate][0];
00222 }
00223 
00224 enum ast_device_state ast_state_chan2dev(enum ast_channel_state chanstate)
00225 {
00226    int i;
00227    chanstate &= 0xFFFF;
00228    for (i = 0; chan2dev[i].chan != -100; i++) {
00229       if (chan2dev[i].chan == chanstate) {
00230          return chan2dev[i].dev;
00231       }
00232    }
00233    return AST_DEVICE_UNKNOWN;
00234 }
00235 
00236 /* Parseable */
00237 const char *ast_devstate_str(enum ast_device_state state)
00238 {
00239    return devstatestring[state][1];
00240 }
00241 
00242 enum ast_device_state ast_devstate_val(const char *val)
00243 {
00244    if (!strcasecmp(val, "NOT_INUSE"))
00245       return AST_DEVICE_NOT_INUSE;
00246    else if (!strcasecmp(val, "INUSE"))
00247       return AST_DEVICE_INUSE;
00248    else if (!strcasecmp(val, "BUSY"))
00249       return AST_DEVICE_BUSY;
00250    else if (!strcasecmp(val, "INVALID"))
00251       return AST_DEVICE_INVALID;
00252    else if (!strcasecmp(val, "UNAVAILABLE"))
00253       return AST_DEVICE_UNAVAILABLE;
00254    else if (!strcasecmp(val, "RINGING"))
00255       return AST_DEVICE_RINGING;
00256    else if (!strcasecmp(val, "RINGINUSE"))
00257       return AST_DEVICE_RINGINUSE;
00258    else if (!strcasecmp(val, "ONHOLD"))
00259       return AST_DEVICE_ONHOLD;
00260 
00261    return AST_DEVICE_UNKNOWN;
00262 }
00263 
00264 /*! \brief Find out if device is active in a call or not 
00265    \note find channels with the device's name in it
00266    This function is only used for channels that does not implement 
00267    devicestate natively
00268 */
00269 enum ast_device_state ast_parse_device_state(const char *device)
00270 {
00271    struct ast_channel *chan;
00272    char match[AST_CHANNEL_NAME];
00273    enum ast_device_state res;
00274 
00275    snprintf(match, sizeof(match), "%s-", device);
00276 
00277    if (!(chan = ast_channel_get_by_name_prefix(match, strlen(match)))) {
00278       return AST_DEVICE_UNKNOWN;
00279    }
00280 
00281    res = (chan->_state == AST_STATE_RINGING) ? AST_DEVICE_RINGING : AST_DEVICE_INUSE;
00282    
00283    chan = ast_channel_unref(chan);
00284 
00285    return res;
00286 }
00287 
00288 static enum ast_device_state devstate_cached(const char *device)
00289 {
00290    enum ast_device_state res = AST_DEVICE_UNKNOWN;
00291    struct ast_event *event;
00292 
00293    event = ast_event_get_cached(AST_EVENT_DEVICE_STATE,
00294       AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
00295       AST_EVENT_IE_END);
00296 
00297    if (!event)
00298       return res;
00299 
00300    res = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
00301 
00302    ast_event_destroy(event);
00303 
00304    return res;
00305 }
00306 
00307 /*! \brief Check device state through channel specific function or generic function */
00308 static enum ast_device_state _ast_device_state(const char *device, int check_cache)
00309 {
00310    char *buf;
00311    char *number;
00312    const struct ast_channel_tech *chan_tech;
00313    enum ast_device_state res;
00314    /*! \brief Channel driver that provides device state */
00315    char *tech;
00316    /*! \brief Another provider of device state */
00317    char *provider = NULL;
00318 
00319    /* If the last known state is cached, just return that */
00320    if (check_cache) {
00321       res = devstate_cached(device);
00322       if (res != AST_DEVICE_UNKNOWN) {
00323          return res;
00324       }
00325    }
00326 
00327    buf = ast_strdupa(device);
00328    tech = strsep(&buf, "/");
00329    if (!(number = buf)) {
00330       provider = strsep(&tech, ":");
00331       if (!tech) {
00332          return AST_DEVICE_INVALID;
00333       }
00334       /* We have a provider */
00335       number = tech;
00336       tech = NULL;
00337 
00338       ast_debug(3, "Checking if I can find provider for \"%s\" - number: %s\n", provider, number);
00339       return getproviderstate(provider, number);
00340    }
00341 
00342    ast_debug(4, "No provider found, checking channel drivers for %s - %s\n", tech, number);
00343 
00344    if (!(chan_tech = ast_get_channel_tech(tech)))
00345       return AST_DEVICE_INVALID;
00346 
00347    if (!(chan_tech->devicestate)) /* Does the channel driver support device state notification? */
00348       return ast_parse_device_state(device); /* No, try the generic function */
00349 
00350    res = chan_tech->devicestate(number);
00351 
00352    if (res != AST_DEVICE_UNKNOWN)
00353       return res;
00354 
00355    res = ast_parse_device_state(device);
00356 
00357    return res;
00358 }
00359 
00360 enum ast_device_state ast_device_state(const char *device)
00361 {
00362    /* This function is called from elsewhere in the code to find out the
00363     * current state of a device.  Check the cache, first. */
00364 
00365    return _ast_device_state(device, 1);
00366 }
00367 
00368 /*! \brief Add device state provider */
00369 int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback)
00370 {
00371    struct devstate_prov *devprov;
00372 
00373    if (!callback || !(devprov = ast_calloc(1, sizeof(*devprov))))
00374       return -1;
00375 
00376    devprov->callback = callback;
00377    ast_copy_string(devprov->label, label, sizeof(devprov->label));
00378 
00379    AST_RWLIST_WRLOCK(&devstate_provs);
00380    AST_RWLIST_INSERT_HEAD(&devstate_provs, devprov, list);
00381    AST_RWLIST_UNLOCK(&devstate_provs);
00382 
00383    return 0;
00384 }
00385 
00386 /*! \brief Remove device state provider */
00387 int ast_devstate_prov_del(const char *label)
00388 {
00389    struct devstate_prov *devcb;
00390    int res = -1;
00391 
00392    AST_RWLIST_WRLOCK(&devstate_provs);
00393    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&devstate_provs, devcb, list) {
00394       if (!strcasecmp(devcb->label, label)) {
00395          AST_RWLIST_REMOVE_CURRENT(list);
00396          ast_free(devcb);
00397          res = 0;
00398          break;
00399       }
00400    }
00401    AST_RWLIST_TRAVERSE_SAFE_END;
00402    AST_RWLIST_UNLOCK(&devstate_provs);
00403 
00404    return res;
00405 }
00406 
00407 /*! \brief Get provider device state */
00408 static int getproviderstate(const char *provider, const char *address)
00409 {
00410    struct devstate_prov *devprov;
00411    int res = AST_DEVICE_INVALID;
00412 
00413    AST_RWLIST_RDLOCK(&devstate_provs);
00414    AST_RWLIST_TRAVERSE(&devstate_provs, devprov, list) {
00415       ast_debug(5, "Checking provider %s with %s\n", devprov->label, provider);
00416 
00417       if (!strcasecmp(devprov->label, provider)) {
00418          res = devprov->callback(address);
00419          break;
00420       }
00421    }
00422    AST_RWLIST_UNLOCK(&devstate_provs);
00423 
00424    return res;
00425 }
00426 
00427 static void devstate_event(const char *device, enum ast_device_state state)
00428 {
00429    struct ast_event *event;
00430    enum ast_event_type event_type;
00431 
00432    if (devstate_collector.enabled) {
00433       /* Distributed device state is enabled, so this state change is a change
00434        * for a single server, not the real state. */
00435       event_type = AST_EVENT_DEVICE_STATE_CHANGE;
00436    } else {
00437       event_type = AST_EVENT_DEVICE_STATE;
00438    }
00439 
00440    ast_debug(3, "device '%s' state '%d'\n", device, state);
00441 
00442    if (!(event = ast_event_new(event_type,
00443          AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
00444          AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
00445          AST_EVENT_IE_END))) {
00446       return;
00447    }
00448 
00449    ast_event_queue_and_cache(event);
00450 }
00451 
00452 /*! Called by the state change thread to find out what the state is, and then
00453  *  to queue up the state change event */
00454 static void do_state_change(const char *device)
00455 {
00456    enum ast_device_state state;
00457 
00458    state = _ast_device_state(device, 0);
00459 
00460    ast_debug(3, "Changing state for %s - state %d (%s)\n", device, state, ast_devstate2str(state));
00461 
00462    devstate_event(device, state);
00463 }
00464 
00465 int ast_devstate_changed_literal(enum ast_device_state state, const char *device)
00466 {
00467    struct state_change *change;
00468 
00469    /* 
00470     * If we know the state change (how nice of the caller of this function!)
00471     * then we can just generate a device state event. 
00472     *
00473     * Otherwise, we do the following:
00474     *   - Queue an event up to another thread that the state has changed
00475     *   - In the processing thread, it calls the callback provided by the
00476     *     device state provider (which may or may not be a channel driver)
00477     *     to determine the state.
00478     *   - If the device state provider does not know the state, or this is
00479     *     for a channel and the channel driver does not implement a device
00480     *     state callback, then we will look through the channel list to
00481     *     see if we can determine a state based on active calls.
00482     *   - Once a state has been determined, a device state event is generated.
00483     */
00484 
00485    if (state != AST_DEVICE_UNKNOWN) {
00486       devstate_event(device, state);
00487    } else if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
00488       /* we could not allocate a change struct, or */
00489       /* there is no background thread, so process the change now */
00490       do_state_change(device);
00491    } else {
00492       /* queue the change */
00493       strcpy(change->device, device);
00494       AST_LIST_LOCK(&state_changes);
00495       AST_LIST_INSERT_TAIL(&state_changes, change, list);
00496       ast_cond_signal(&change_pending);
00497       AST_LIST_UNLOCK(&state_changes);
00498    }
00499 
00500    return 1;
00501 }
00502 
00503 int ast_device_state_changed_literal(const char *dev)
00504 {
00505    return ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, dev);
00506 }
00507 
00508 int ast_devstate_changed(enum ast_device_state state, const char *fmt, ...) 
00509 {
00510    char buf[AST_MAX_EXTENSION];
00511    va_list ap;
00512 
00513    va_start(ap, fmt);
00514    vsnprintf(buf, sizeof(buf), fmt, ap);
00515    va_end(ap);
00516 
00517    return ast_devstate_changed_literal(state, buf);
00518 }
00519 
00520 int ast_device_state_changed(const char *fmt, ...) 
00521 {
00522    char buf[AST_MAX_EXTENSION];
00523    va_list ap;
00524 
00525    va_start(ap, fmt);
00526    vsnprintf(buf, sizeof(buf), fmt, ap);
00527    va_end(ap);
00528 
00529    return ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, buf);
00530 }
00531 
00532 /*! \brief Go through the dev state change queue and update changes in the dev state thread */
00533 static void *do_devstate_changes(void *data)
00534 {
00535    struct state_change *next, *current;
00536 
00537    for (;;) {
00538       /* This basically pops off any state change entries, resets the list back to NULL, unlocks, and processes each state change */
00539       AST_LIST_LOCK(&state_changes);
00540       if (AST_LIST_EMPTY(&state_changes))
00541          ast_cond_wait(&change_pending, &state_changes.lock);
00542       next = AST_LIST_FIRST(&state_changes);
00543       AST_LIST_HEAD_INIT_NOLOCK(&state_changes);
00544       AST_LIST_UNLOCK(&state_changes);
00545 
00546       /* Process each state change */
00547       while ((current = next)) {
00548          next = AST_LIST_NEXT(current, list);
00549          do_state_change(current->device);
00550          ast_free(current);
00551       }
00552    }
00553 
00554    return NULL;
00555 }
00556 
00557 static void destroy_devstate_change(struct devstate_change *sc)
00558 {
00559    ast_free(sc);
00560 }
00561 
00562 #define MAX_SERVERS 64
00563 struct change_collection {
00564    struct devstate_change states[MAX_SERVERS];
00565    size_t num_states;
00566 };
00567 
00568 static void devstate_cache_cb(const struct ast_event *event, void *data)
00569 {
00570    struct change_collection *collection = data;
00571    int i;
00572    const struct ast_eid *eid;
00573 
00574    if (collection->num_states == ARRAY_LEN(collection->states)) {
00575       ast_log(LOG_ERROR, "More per-server state values than we have room for (MAX_SERVERS is %d)\n",
00576          MAX_SERVERS);
00577       return;
00578    }
00579 
00580    if (!(eid = ast_event_get_ie_raw(event, AST_EVENT_IE_EID))) {
00581       ast_log(LOG_ERROR, "Device state change event with no EID\n");
00582       return;
00583    }
00584 
00585    i = collection->num_states;
00586 
00587    collection->states[i].state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
00588    collection->states[i].eid = *eid;
00589 
00590    collection->num_states++;
00591 }
00592 
00593 static void process_collection(const char *device, struct change_collection *collection)
00594 {
00595    int i;
00596    struct ast_devstate_aggregate agg;
00597    enum ast_device_state state;
00598    struct ast_event *event;
00599 
00600    ast_devstate_aggregate_init(&agg);
00601 
00602    for (i = 0; i < collection->num_states; i++) {
00603       ast_debug(1, "Adding per-server state of '%s' for '%s'\n", 
00604          ast_devstate2str(collection->states[i].state), device);
00605       ast_devstate_aggregate_add(&agg, collection->states[i].state);
00606    }
00607 
00608    state = ast_devstate_aggregate_result(&agg);
00609 
00610    ast_debug(1, "Aggregate devstate result is '%s' for '%s'\n",
00611       ast_devstate2str(state), device);
00612 
00613    event = ast_event_get_cached(AST_EVENT_DEVICE_STATE,
00614       AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
00615       AST_EVENT_IE_END);
00616    
00617    if (event) {
00618       enum ast_device_state old_state;
00619 
00620       old_state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
00621       
00622       ast_event_destroy(event);
00623 
00624       if (state == old_state) {
00625          /* No change since last reported device state */
00626          ast_debug(1, "Aggregate state for device '%s' has not changed from '%s'\n",
00627             device, ast_devstate2str(state));
00628          return;
00629       }
00630    }
00631 
00632    ast_debug(1, "Aggregate state for device '%s' has changed to '%s'\n",
00633       device, ast_devstate2str(state));
00634 
00635    event = ast_event_new(AST_EVENT_DEVICE_STATE,
00636       AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
00637       AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
00638       AST_EVENT_IE_END);
00639 
00640    if (!event) {
00641       return;
00642    }
00643 
00644    ast_event_queue_and_cache(event);
00645 }
00646 
00647 static void handle_devstate_change(struct devstate_change *sc)
00648 {
00649    struct ast_event_sub *tmp_sub;
00650    struct change_collection collection = {
00651       .num_states = 0,
00652    };
00653 
00654    ast_debug(1, "Processing device state change for '%s'\n", sc->device);
00655 
00656    if (!(tmp_sub = ast_event_subscribe_new(AST_EVENT_DEVICE_STATE_CHANGE, devstate_cache_cb, &collection))) {
00657       ast_log(LOG_ERROR, "Failed to create subscription\n");
00658       return;
00659    }
00660 
00661    if (ast_event_sub_append_ie_str(tmp_sub, AST_EVENT_IE_DEVICE, sc->device)) {
00662       ast_log(LOG_ERROR, "Failed to append device IE\n");
00663       ast_event_sub_destroy(tmp_sub);
00664       return;
00665    }
00666 
00667    /* Populate the collection of device states from the cache */
00668    ast_event_dump_cache(tmp_sub);
00669 
00670    process_collection(sc->device, &collection);
00671 
00672    ast_event_sub_destroy(tmp_sub);
00673 }
00674 
00675 static void *run_devstate_collector(void *data)
00676 {
00677    for (;;) {
00678       struct devstate_change *sc;
00679 
00680       ast_mutex_lock(&devstate_collector.lock);
00681       while (!(sc = AST_LIST_REMOVE_HEAD(&devstate_collector.devstate_change_q, entry)))
00682          ast_cond_wait(&devstate_collector.cond, &devstate_collector.lock);
00683       ast_mutex_unlock(&devstate_collector.lock);
00684 
00685       handle_devstate_change(sc);
00686 
00687       destroy_devstate_change(sc);
00688    }
00689 
00690    return NULL;
00691 }
00692 
00693 static void devstate_change_collector_cb(const struct ast_event *event, void *data)
00694 {
00695    struct devstate_change *sc;
00696    const char *device;
00697    const struct ast_eid *eid;
00698    uint32_t state;
00699 
00700    device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
00701    eid = ast_event_get_ie_raw(event, AST_EVENT_IE_EID);
00702    state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
00703 
00704    if (ast_strlen_zero(device) || !eid) {
00705       ast_log(LOG_ERROR, "Invalid device state change event received\n");
00706       return;
00707    }
00708 
00709    if (!(sc = ast_calloc(1, sizeof(*sc) + strlen(device))))
00710       return;
00711 
00712    strcpy(sc->device, device);
00713    sc->eid = *eid;
00714    sc->state = state;
00715 
00716    ast_mutex_lock(&devstate_collector.lock);
00717    AST_LIST_INSERT_TAIL(&devstate_collector.devstate_change_q, sc, entry);
00718    ast_cond_signal(&devstate_collector.cond);
00719    ast_mutex_unlock(&devstate_collector.lock);
00720 }
00721 
00722 /*! \brief Initialize the device state engine in separate thread */
00723 int ast_device_state_engine_init(void)
00724 {
00725    ast_cond_init(&change_pending, NULL);
00726    if (ast_pthread_create_background(&change_thread, NULL, do_devstate_changes, NULL) < 0) {
00727       ast_log(LOG_ERROR, "Unable to start device state change thread.\n");
00728       return -1;
00729    }
00730 
00731    return 0;
00732 }
00733 
00734 void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg)
00735 {
00736    memset(agg, 0, sizeof(*agg));
00737    agg->state = AST_DEVICE_INVALID;
00738 }
00739 
00740 void ast_devstate_aggregate_add(struct ast_devstate_aggregate *agg, enum ast_device_state state)
00741 {
00742    static enum ast_device_state state_order[] = {
00743       1, /* AST_DEVICE_UNKNOWN */
00744       3, /* AST_DEVICE_NOT_INUSE */
00745       6, /* AST_DEVICE_INUSE */
00746       7, /* AST_DEVICE_BUSY */
00747       0, /* AST_DEVICE_INVALID */
00748       2, /* AST_DEVICE_UNAVAILABLE */
00749       5, /* AST_DEVICE_RINGING */
00750       8, /* AST_DEVICE_RINGINUSE */
00751       4, /* AST_DEVICE_ONHOLD */
00752    };
00753 
00754    if (state == AST_DEVICE_RINGING) {
00755       agg->ringing = 1;
00756    } else if (state == AST_DEVICE_INUSE || state == AST_DEVICE_ONHOLD || state == AST_DEVICE_BUSY) {
00757       agg->inuse = 1;
00758    }
00759 
00760    if (agg->ringing && agg->inuse) {
00761       agg->state = AST_DEVICE_RINGINUSE;
00762    } else if (state_order[state] > state_order[agg->state]) {
00763       agg->state = state;
00764    }
00765 }
00766 
00767 enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg)
00768 {
00769    return agg->state;
00770 }
00771 
00772 int ast_enable_distributed_devstate(void)
00773 {
00774    if (devstate_collector.enabled) {
00775       return 0;
00776    }
00777 
00778    devstate_collector.event_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE_CHANGE,
00779       devstate_change_collector_cb, "devicestate_engine_enable_distributed", NULL, AST_EVENT_IE_END);
00780 
00781    if (!devstate_collector.event_sub) {
00782       ast_log(LOG_ERROR, "Failed to create subscription for the device state change collector\n");
00783       return -1;
00784    }
00785 
00786    ast_mutex_init(&devstate_collector.lock);
00787    ast_cond_init(&devstate_collector.cond, NULL);
00788    if (ast_pthread_create_background(&devstate_collector.thread, NULL, run_devstate_collector, NULL) < 0) {
00789       ast_log(LOG_ERROR, "Unable to start device state collector thread.\n");
00790       return -1;
00791    }
00792 
00793    devstate_collector.enabled = 1;
00794 
00795    return 0;
00796 }

Generated on Mon Oct 8 12:39:02 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7