2005-07-08 21:15:41 +00:00
|
|
|
/*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2005-07-08 21:15:41 +00:00
|
|
|
*
|
2008-06-10 15:12:17 +00:00
|
|
|
* Copyright (C) 1999 - 2008, Digium, Inc.
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
|
|
|
* Mark Spencer <markster@digium.com>
|
2008-06-10 15:12:17 +00:00
|
|
|
* Russell Bryant <russell@digium.com>
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
2005-07-08 21:15:41 +00:00
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
2005-09-14 20:46:50 +00:00
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-10-24 20:12:06 +00:00
|
|
|
* \brief Device state management
|
2005-12-30 21:18:06 +00:00
|
|
|
*
|
|
|
|
* \author Mark Spencer <markster@digium.com>
|
2008-06-10 15:12:17 +00:00
|
|
|
* \author Russell Bryant <russell@digium.com>
|
2006-11-30 18:22:10 +00:00
|
|
|
*
|
|
|
|
* \arg \ref AstExtState
|
2005-07-08 21:15:41 +00:00
|
|
|
*/
|
2007-08-10 16:24:11 +00:00
|
|
|
|
2006-11-30 17:15:54 +00:00
|
|
|
/*! \page AstExtState Extension and device states in Asterisk
|
2008-06-10 15:12:17 +00:00
|
|
|
*
|
|
|
|
* (Note that these descriptions of device states and extension
|
|
|
|
* states have not been updated to the way things work
|
|
|
|
* in Asterisk 1.6.)
|
2006-11-30 17:15:54 +00:00
|
|
|
*
|
|
|
|
* Asterisk has an internal system that reports states
|
|
|
|
* for an extension. By using the dialplan priority -1,
|
|
|
|
* also called a \b hint, a connection can be made from an
|
|
|
|
* extension to one or many devices. The state of the extension
|
|
|
|
* now depends on the combined state of the devices.
|
|
|
|
*
|
|
|
|
* The device state is basically based on the current calls.
|
|
|
|
* If the devicestate engine can find a call from or to the
|
|
|
|
* device, it's in use.
|
|
|
|
*
|
|
|
|
* Some channel drivers implement a callback function for
|
|
|
|
* a better level of reporting device states. The SIP channel
|
|
|
|
* has a complicated system for this, which is improved
|
|
|
|
* by adding call limits to the configuration.
|
|
|
|
*
|
|
|
|
* Functions that want to check the status of an extension
|
|
|
|
* register themself as a \b watcher.
|
|
|
|
* Watchers in this system can subscribe either to all extensions
|
|
|
|
* or just a specific extensions.
|
|
|
|
*
|
|
|
|
* For non-device related states, there's an API called
|
2007-05-23 02:55:22 +00:00
|
|
|
* devicestate providers. This is an extendible system for
|
2006-11-30 17:15:54 +00:00
|
|
|
* delivering state information from outside sources or
|
|
|
|
* functions within Asterisk. Currently we have providers
|
|
|
|
* for app_meetme.c - the conference bridge - and call
|
|
|
|
* parking (metermaids).
|
|
|
|
*
|
|
|
|
* There are manly three subscribers to extension states
|
|
|
|
* within Asterisk:
|
|
|
|
* - AMI, the manager interface
|
|
|
|
* - app_queue.c - the Queue dialplan application
|
|
|
|
* - SIP subscriptions, a.k.a. "blinking lamps" or
|
|
|
|
* "buddy lists"
|
|
|
|
*
|
2006-11-30 18:22:10 +00:00
|
|
|
* The CLI command "show hints" show last known state
|
|
|
|
*
|
2007-05-23 02:55:22 +00:00
|
|
|
* \note None of these handle user states, like an IM presence
|
2006-11-30 17:15:54 +00:00
|
|
|
* system. res_jabber.c can subscribe and watch such states
|
|
|
|
* in jabber/xmpp based systems.
|
|
|
|
*
|
2006-11-30 20:34:23 +00:00
|
|
|
* \section AstDevStateArch Architecture for devicestates
|
2006-11-30 18:22:10 +00:00
|
|
|
*
|
|
|
|
* When a channel driver or asterisk app changes state for
|
|
|
|
* a watched object, it alerts the core. The core queues
|
|
|
|
* a change. When the change is processed, there's a query
|
|
|
|
* sent to the channel driver/provider if there's a function
|
|
|
|
* to handle that, otherwise a channel walk is issued to find
|
|
|
|
* a channel that involves the object.
|
|
|
|
*
|
|
|
|
* The changes are queued and processed by a separate thread.
|
|
|
|
* This thread calls the watchers subscribing to status
|
|
|
|
* changes for the object. For manager, this results
|
|
|
|
* in events. For SIP, NOTIFY requests.
|
|
|
|
*
|
2006-11-30 18:25:51 +00:00
|
|
|
* - Device states
|
|
|
|
* \arg \ref devicestate.c
|
|
|
|
* \arg \ref devicestate.h
|
2006-11-30 20:34:23 +00:00
|
|
|
*
|
|
|
|
* \section AstExtStateArch Architecture for extension states
|
|
|
|
*
|
|
|
|
* Hints are connected to extension. If an extension changes state
|
|
|
|
* it checks the hint devices. If there is a hint, the callbacks into
|
|
|
|
* device states are checked. The aggregated state is set for the hint
|
|
|
|
* and reported back.
|
|
|
|
*
|
2006-11-30 18:25:51 +00:00
|
|
|
* - Extension states
|
2007-07-23 14:32:04 +00:00
|
|
|
* \arg \ref AstENUM ast_extension_states
|
2006-11-30 18:25:51 +00:00
|
|
|
* \arg \ref pbx.c
|
|
|
|
* \arg \ref pbx.h
|
2006-11-30 20:34:23 +00:00
|
|
|
* - Structures
|
2007-06-07 23:07:25 +00:00
|
|
|
* - \ref ast_state_cb struct. Callbacks for watchers
|
2006-11-30 20:34:23 +00:00
|
|
|
* - Callback ast_state_cb_type
|
2007-06-07 23:07:25 +00:00
|
|
|
* - \ref ast_hint struct.
|
2006-11-30 20:34:23 +00:00
|
|
|
* - Functions
|
|
|
|
* - ast_extension_state_add()
|
|
|
|
* - ast_extension_state_del()
|
|
|
|
* - ast_get_hint()
|
2006-11-30 17:15:54 +00:00
|
|
|
*
|
|
|
|
*/
|
2005-07-08 21:15:41 +00:00
|
|
|
|
2006-06-07 18:54:56 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2007-11-20 22:18:21 +00:00
|
|
|
#include "asterisk/_private.h"
|
2005-07-08 21:15:41 +00:00
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
#include "asterisk/lock.h"
|
|
|
|
#include "asterisk/linkedlists.h"
|
|
|
|
#include "asterisk/devicestate.h"
|
|
|
|
#include "asterisk/pbx.h"
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
#include "asterisk/app.h"
|
Merge major changes to the way device state is passed around Asterisk. The two
places that cared about device states were app_queue and the hint code in pbx.c.
The changes include converting it to use the Asterisk event system, as well as
other efficiency improvements.
* app_queue: This module used to register a callback into devicestate.c to
monitor device state changes. Now, it is just a subscriber to Asterisk
events with the type, device state.
* pbx.c hints: Previously, the device state processing thread in devicestate.c
would call ast_hint_state_changed() each time the state of a device changed.
Then, that code would go looking for all the hints that monitor that device,
and call their callbacks. All of this blocked the device state processing
thread. Now, the hint code is a subscriber of Asterisk events with the
type, device state. Furthermore, when this code receives a device state
change event, it queues it up to be processed by another thread so that it
doesn't block one of the event processing threads.
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@66958 f38db490-d61c-443f-a65b-d21fe96a405b
2007-06-01 23:34:43 +00:00
|
|
|
#include "asterisk/event.h"
|
2005-07-08 21:15:41 +00:00
|
|
|
|
2006-04-04 18:24:41 +00:00
|
|
|
/*! \brief Device state strings for printing */
|
2008-11-04 18:47:20 +00:00
|
|
|
static const char *devstatestring[][2] = {
|
|
|
|
{ /* 0 AST_DEVICE_UNKNOWN */ "Unknown", "UNKNOWN" }, /*!< Valid, but unknown state */
|
|
|
|
{ /* 1 AST_DEVICE_NOT_INUSE */ "Not in use", "NOT_INUSE" }, /*!< Not used */
|
|
|
|
{ /* 2 AST_DEVICE IN USE */ "In use", "INUSE" }, /*!< In use */
|
|
|
|
{ /* 3 AST_DEVICE_BUSY */ "Busy", "BUSY" }, /*!< Busy */
|
|
|
|
{ /* 4 AST_DEVICE_INVALID */ "Invalid", "INVALID" }, /*!< Invalid - not known to Asterisk */
|
|
|
|
{ /* 5 AST_DEVICE_UNAVAILABLE */ "Unavailable", "UNAVAILABLE" }, /*!< Unavailable (not registered) */
|
|
|
|
{ /* 6 AST_DEVICE_RINGING */ "Ringing", "RINGING" }, /*!< Ring, ring, ring */
|
|
|
|
{ /* 7 AST_DEVICE_RINGINUSE */ "Ring+Inuse", "RINGINUSE" }, /*!< Ring and in use */
|
|
|
|
{ /* 8 AST_DEVICE_ONHOLD */ "On Hold" "ONHOLD" }, /*!< On Hold */
|
2005-07-20 00:34:01 +00:00
|
|
|
};
|
|
|
|
|
2008-07-25 21:20:03 +00:00
|
|
|
/*!\brief Mapping for channel states to device states */
|
|
|
|
static const struct chan2dev {
|
|
|
|
enum ast_channel_state chan;
|
|
|
|
enum ast_device_state dev;
|
|
|
|
} chan2dev[] = {
|
|
|
|
{ AST_STATE_DOWN, AST_DEVICE_NOT_INUSE },
|
|
|
|
{ AST_STATE_RESERVED, AST_DEVICE_INUSE },
|
|
|
|
{ AST_STATE_OFFHOOK, AST_DEVICE_INUSE },
|
|
|
|
{ AST_STATE_DIALING, AST_DEVICE_INUSE },
|
|
|
|
{ AST_STATE_RING, AST_DEVICE_INUSE },
|
|
|
|
{ AST_STATE_RINGING, AST_DEVICE_RINGING },
|
|
|
|
{ AST_STATE_UP, AST_DEVICE_INUSE },
|
|
|
|
{ AST_STATE_BUSY, AST_DEVICE_BUSY },
|
|
|
|
{ AST_STATE_DIALING_OFFHOOK, AST_DEVICE_INUSE },
|
|
|
|
{ AST_STATE_PRERING, AST_DEVICE_RINGING },
|
|
|
|
{ -100, -100 },
|
|
|
|
};
|
|
|
|
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
/*! \brief A device state provider (not a channel) */
|
|
|
|
struct devstate_prov {
|
|
|
|
char label[40];
|
|
|
|
ast_devstate_prov_cb_type callback;
|
2007-02-02 00:39:01 +00:00
|
|
|
AST_RWLIST_ENTRY(devstate_prov) list;
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*! \brief A list of providers */
|
2007-02-02 00:39:01 +00:00
|
|
|
static AST_RWLIST_HEAD_STATIC(devstate_provs, devstate_prov);
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
|
2005-07-08 21:15:41 +00:00
|
|
|
struct state_change {
|
|
|
|
AST_LIST_ENTRY(state_change) list;
|
|
|
|
char device[1];
|
|
|
|
};
|
|
|
|
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
/*! \brief The state change queue. State changes are queued
|
|
|
|
for processing by a separate thread */
|
2005-07-08 21:15:41 +00:00
|
|
|
static AST_LIST_HEAD_STATIC(state_changes, state_change);
|
|
|
|
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
/*! \brief The device state change notification thread */
|
2005-07-08 21:15:41 +00:00
|
|
|
static pthread_t change_thread = AST_PTHREADT_NULL;
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
|
|
|
|
/*! \brief Flag for the queue */
|
2005-10-28 16:35:43 +00:00
|
|
|
static ast_cond_t change_pending;
|
2005-07-08 21:15:41 +00:00
|
|
|
|
2008-06-10 15:12:17 +00:00
|
|
|
struct devstate_change {
|
|
|
|
AST_LIST_ENTRY(devstate_change) entry;
|
|
|
|
uint32_t state;
|
|
|
|
struct ast_eid eid;
|
|
|
|
char device[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct {
|
|
|
|
pthread_t thread;
|
|
|
|
struct ast_event_sub *event_sub;
|
|
|
|
ast_cond_t cond;
|
|
|
|
ast_mutex_t lock;
|
|
|
|
AST_LIST_HEAD_NOLOCK(, devstate_change) devstate_change_q;
|
|
|
|
} devstate_collector = {
|
|
|
|
.thread = AST_PTHREADT_NULL,
|
|
|
|
};
|
|
|
|
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
/* Forward declarations */
|
|
|
|
static int getproviderstate(const char *provider, const char *address);
|
|
|
|
|
2006-01-20 00:18:42 +00:00
|
|
|
/*! \brief Find devicestate as text message for output */
|
2008-11-04 18:47:20 +00:00
|
|
|
const char *ast_devstate2str(enum ast_device_state devstate)
|
|
|
|
{
|
|
|
|
return devstatestring[devstate][0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deprecated interface (not prefixed with ast_) */
|
2007-02-13 22:02:20 +00:00
|
|
|
const char *devstate2str(enum ast_device_state devstate)
|
2005-07-20 00:34:01 +00:00
|
|
|
{
|
2008-11-04 18:47:20 +00:00
|
|
|
return devstatestring[devstate][0];
|
2005-07-20 00:34:01 +00:00
|
|
|
}
|
|
|
|
|
2008-07-25 21:20:03 +00:00
|
|
|
enum ast_device_state ast_state_chan2dev(enum ast_channel_state chanstate)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
chanstate &= 0xFFFF;
|
|
|
|
for (i = 0; chan2dev[i].chan != -100; i++) {
|
|
|
|
if (chan2dev[i].chan == chanstate) {
|
|
|
|
return chan2dev[i].dev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AST_DEVICE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2008-11-04 18:47:20 +00:00
|
|
|
/* Parseable */
|
2007-02-13 22:02:20 +00:00
|
|
|
const char *ast_devstate_str(enum ast_device_state state)
|
|
|
|
{
|
2008-11-04 18:47:20 +00:00
|
|
|
return devstatestring[state][1];
|
2007-02-13 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum ast_device_state ast_devstate_val(const char *val)
|
|
|
|
{
|
|
|
|
if (!strcasecmp(val, "NOT_INUSE"))
|
|
|
|
return AST_DEVICE_NOT_INUSE;
|
|
|
|
else if (!strcasecmp(val, "INUSE"))
|
|
|
|
return AST_DEVICE_INUSE;
|
|
|
|
else if (!strcasecmp(val, "BUSY"))
|
|
|
|
return AST_DEVICE_BUSY;
|
|
|
|
else if (!strcasecmp(val, "INVALID"))
|
|
|
|
return AST_DEVICE_INVALID;
|
|
|
|
else if (!strcasecmp(val, "UNAVAILABLE"))
|
|
|
|
return AST_DEVICE_UNAVAILABLE;
|
|
|
|
else if (!strcasecmp(val, "RINGING"))
|
|
|
|
return AST_DEVICE_RINGING;
|
|
|
|
else if (!strcasecmp(val, "RINGINUSE"))
|
|
|
|
return AST_DEVICE_RINGINUSE;
|
|
|
|
else if (!strcasecmp(val, "ONHOLD"))
|
|
|
|
return AST_DEVICE_ONHOLD;
|
|
|
|
|
|
|
|
return AST_DEVICE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2006-04-04 18:24:41 +00:00
|
|
|
/*! \brief Find out if device is active in a call or not
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
\note find channels with the device's name in it
|
|
|
|
This function is only used for channels that does not implement
|
|
|
|
devicestate natively
|
2006-04-04 18:24:41 +00:00
|
|
|
*/
|
2007-02-13 22:02:20 +00:00
|
|
|
enum ast_device_state ast_parse_device_state(const char *device)
|
2005-07-08 21:15:41 +00:00
|
|
|
{
|
|
|
|
struct ast_channel *chan;
|
|
|
|
char match[AST_CHANNEL_NAME];
|
2007-02-13 22:02:20 +00:00
|
|
|
enum ast_device_state res;
|
2005-07-08 21:15:41 +00:00
|
|
|
|
|
|
|
ast_copy_string(match, device, sizeof(match)-1);
|
|
|
|
strcat(match, "-");
|
|
|
|
chan = ast_get_channel_by_name_prefix_locked(match, strlen(match));
|
|
|
|
|
|
|
|
if (!chan)
|
|
|
|
return AST_DEVICE_UNKNOWN;
|
|
|
|
|
|
|
|
if (chan->_state == AST_STATE_RINGING)
|
|
|
|
res = AST_DEVICE_RINGING;
|
|
|
|
else
|
|
|
|
res = AST_DEVICE_INUSE;
|
|
|
|
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_unlock(chan);
|
2005-07-08 21:15:41 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2007-08-10 16:24:11 +00:00
|
|
|
static enum ast_device_state devstate_cached(const char *device)
|
|
|
|
{
|
|
|
|
enum ast_device_state res = AST_DEVICE_UNKNOWN;
|
|
|
|
struct ast_event *event;
|
|
|
|
|
2008-07-25 12:42:25 +00:00
|
|
|
event = ast_event_get_cached(AST_EVENT_DEVICE_STATE,
|
2007-08-10 16:24:11 +00:00
|
|
|
AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
|
|
|
|
AST_EVENT_IE_END);
|
|
|
|
|
|
|
|
if (!event)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
res = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
|
|
|
|
|
|
|
|
ast_event_destroy(event);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-01-20 00:18:42 +00:00
|
|
|
/*! \brief Check device state through channel specific function or generic function */
|
2008-07-26 15:15:14 +00:00
|
|
|
static enum ast_device_state _ast_device_state(const char *device, int check_cache)
|
2005-07-08 21:15:41 +00:00
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
char *number;
|
|
|
|
const struct ast_channel_tech *chan_tech;
|
2007-08-10 16:24:11 +00:00
|
|
|
enum ast_device_state res;
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
/*! \brief Channel driver that provides device state */
|
|
|
|
char *tech;
|
|
|
|
/*! \brief Another provider of device state */
|
|
|
|
char *provider = NULL;
|
2007-08-10 16:24:11 +00:00
|
|
|
|
|
|
|
/* If the last known state is cached, just return that */
|
2008-07-26 15:16:20 +00:00
|
|
|
if (check_cache) {
|
|
|
|
res = devstate_cached(device);
|
|
|
|
if (res != AST_DEVICE_UNKNOWN) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2007-08-10 16:24:11 +00:00
|
|
|
|
2005-07-08 21:15:41 +00:00
|
|
|
buf = ast_strdupa(device);
|
|
|
|
tech = strsep(&buf, "/");
|
2007-02-02 00:58:09 +00:00
|
|
|
if (!(number = buf)) {
|
|
|
|
if (!(provider = strsep(&tech, ":")))
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
return AST_DEVICE_INVALID;
|
|
|
|
/* We have a provider */
|
|
|
|
number = tech;
|
|
|
|
tech = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (provider) {
|
2007-06-14 19:39:12 +00:00
|
|
|
ast_debug(3, "Checking if I can find provider for \"%s\" - number: %s\n", provider, number);
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
return getproviderstate(provider, number);
|
|
|
|
}
|
2007-02-02 00:58:09 +00:00
|
|
|
|
2007-06-14 19:39:12 +00:00
|
|
|
ast_debug(4, "No provider found, checking channel drivers for %s - %s\n", tech, number);
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
|
2007-02-02 00:58:09 +00:00
|
|
|
if (!(chan_tech = ast_get_channel_tech(tech)))
|
2005-07-08 21:15:41 +00:00
|
|
|
return AST_DEVICE_INVALID;
|
|
|
|
|
2007-02-02 00:58:09 +00:00
|
|
|
if (!(chan_tech->devicestate)) /* Does the channel driver support device state notification? */
|
|
|
|
return ast_parse_device_state(device); /* No, try the generic function */
|
|
|
|
|
|
|
|
res = chan_tech->devicestate(number);
|
|
|
|
|
|
|
|
if (res != AST_DEVICE_UNKNOWN)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
res = ast_parse_device_state(device);
|
|
|
|
|
|
|
|
if (res == AST_DEVICE_UNKNOWN)
|
|
|
|
return AST_DEVICE_NOT_INUSE;
|
|
|
|
|
|
|
|
return res;
|
2005-07-08 21:15:41 +00:00
|
|
|
}
|
|
|
|
|
2008-07-26 15:15:14 +00:00
|
|
|
enum ast_device_state ast_device_state(const char *device)
|
|
|
|
{
|
|
|
|
/* This function is called from elsewhere in the code to find out the
|
|
|
|
* current state of a device. Check the cache, first. */
|
|
|
|
|
|
|
|
return _ast_device_state(device, 1);
|
|
|
|
}
|
|
|
|
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
/*! \brief Add device state provider */
|
|
|
|
int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback)
|
|
|
|
{
|
|
|
|
struct devstate_prov *devprov;
|
|
|
|
|
|
|
|
if (!callback || !(devprov = ast_calloc(1, sizeof(*devprov))))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
devprov->callback = callback;
|
|
|
|
ast_copy_string(devprov->label, label, sizeof(devprov->label));
|
|
|
|
|
2007-02-02 00:39:01 +00:00
|
|
|
AST_RWLIST_WRLOCK(&devstate_provs);
|
|
|
|
AST_RWLIST_INSERT_HEAD(&devstate_provs, devprov, list);
|
|
|
|
AST_RWLIST_UNLOCK(&devstate_provs);
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Remove device state provider */
|
2007-02-13 22:02:20 +00:00
|
|
|
int ast_devstate_prov_del(const char *label)
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
{
|
|
|
|
struct devstate_prov *devcb;
|
2007-02-13 22:02:20 +00:00
|
|
|
int res = -1;
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
|
2007-02-02 00:39:01 +00:00
|
|
|
AST_RWLIST_WRLOCK(&devstate_provs);
|
|
|
|
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&devstate_provs, devcb, list) {
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
if (!strcasecmp(devcb->label, label)) {
|
2007-11-08 05:28:47 +00:00
|
|
|
AST_RWLIST_REMOVE_CURRENT(list);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(devcb);
|
2007-02-13 22:02:20 +00:00
|
|
|
res = 0;
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-02-02 00:39:01 +00:00
|
|
|
AST_RWLIST_TRAVERSE_SAFE_END;
|
|
|
|
AST_RWLIST_UNLOCK(&devstate_provs);
|
2007-02-13 22:02:20 +00:00
|
|
|
|
|
|
|
return res;
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Get provider device state */
|
|
|
|
static int getproviderstate(const char *provider, const char *address)
|
|
|
|
{
|
|
|
|
struct devstate_prov *devprov;
|
|
|
|
int res = AST_DEVICE_INVALID;
|
|
|
|
|
2007-02-02 00:39:01 +00:00
|
|
|
AST_RWLIST_RDLOCK(&devstate_provs);
|
|
|
|
AST_RWLIST_TRAVERSE(&devstate_provs, devprov, list) {
|
2007-06-14 19:39:12 +00:00
|
|
|
ast_debug(5, "Checking provider %s with %s\n", devprov->label, provider);
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
|
|
|
|
if (!strcasecmp(devprov->label, provider)) {
|
|
|
|
res = devprov->callback(address);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-02-02 00:39:01 +00:00
|
|
|
AST_RWLIST_UNLOCK(&devstate_provs);
|
2008-06-10 15:12:17 +00:00
|
|
|
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-07-26 14:46:13 +00:00
|
|
|
static void devstate_event(const char *device, enum ast_device_state state)
|
2005-07-08 21:15:41 +00:00
|
|
|
{
|
Merge major changes to the way device state is passed around Asterisk. The two
places that cared about device states were app_queue and the hint code in pbx.c.
The changes include converting it to use the Asterisk event system, as well as
other efficiency improvements.
* app_queue: This module used to register a callback into devicestate.c to
monitor device state changes. Now, it is just a subscriber to Asterisk
events with the type, device state.
* pbx.c hints: Previously, the device state processing thread in devicestate.c
would call ast_hint_state_changed() each time the state of a device changed.
Then, that code would go looking for all the hints that monitor that device,
and call their callbacks. All of this blocked the device state processing
thread. Now, the hint code is a subscriber of Asterisk events with the
type, device state. Furthermore, when this code receives a device state
change event, it queues it up to be processed by another thread so that it
doesn't block one of the event processing threads.
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@66958 f38db490-d61c-443f-a65b-d21fe96a405b
2007-06-01 23:34:43 +00:00
|
|
|
struct ast_event *event;
|
2005-07-08 21:15:41 +00:00
|
|
|
|
2008-07-26 14:46:13 +00:00
|
|
|
ast_debug(3, "device '%s' state '%d'\n", device, state);
|
2008-06-10 15:12:17 +00:00
|
|
|
|
|
|
|
if (!(event = ast_event_new(AST_EVENT_DEVICE_STATE_CHANGE,
|
Merge major changes to the way device state is passed around Asterisk. The two
places that cared about device states were app_queue and the hint code in pbx.c.
The changes include converting it to use the Asterisk event system, as well as
other efficiency improvements.
* app_queue: This module used to register a callback into devicestate.c to
monitor device state changes. Now, it is just a subscriber to Asterisk
events with the type, device state.
* pbx.c hints: Previously, the device state processing thread in devicestate.c
would call ast_hint_state_changed() each time the state of a device changed.
Then, that code would go looking for all the hints that monitor that device,
and call their callbacks. All of this blocked the device state processing
thread. Now, the hint code is a subscriber of Asterisk events with the
type, device state. Furthermore, when this code receives a device state
change event, it queues it up to be processed by another thread so that it
doesn't block one of the event processing threads.
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@66958 f38db490-d61c-443f-a65b-d21fe96a405b
2007-06-01 23:34:43 +00:00
|
|
|
AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
|
|
|
|
AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
|
|
|
|
AST_EVENT_IE_END))) {
|
|
|
|
return;
|
|
|
|
}
|
2005-07-08 21:15:41 +00:00
|
|
|
|
2008-07-26 14:46:13 +00:00
|
|
|
/* Cache this event, replacing an event in the cache with the same
|
|
|
|
* device name if it exists. */
|
|
|
|
ast_event_queue_and_cache(event,
|
|
|
|
AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR,
|
|
|
|
AST_EVENT_IE_EID, AST_EVENT_IE_PLTYPE_RAW, sizeof(struct ast_eid),
|
|
|
|
AST_EVENT_IE_END);
|
2005-07-08 21:15:41 +00:00
|
|
|
}
|
|
|
|
|
2007-08-10 16:24:11 +00:00
|
|
|
/*! Called by the state change thread to find out what the state is, and then
|
|
|
|
* to queue up the state change event */
|
|
|
|
static void do_state_change(const char *device)
|
|
|
|
{
|
|
|
|
enum ast_device_state state;
|
|
|
|
|
2008-07-26 15:15:14 +00:00
|
|
|
state = _ast_device_state(device, 0);
|
2007-08-10 16:24:11 +00:00
|
|
|
|
2008-11-04 18:47:20 +00:00
|
|
|
ast_debug(3, "Changing state for %s - state %d (%s)\n", device, state, ast_devstate2str(state));
|
2007-08-10 16:24:11 +00:00
|
|
|
|
2008-07-26 14:46:13 +00:00
|
|
|
devstate_event(device, state);
|
2007-08-10 16:24:11 +00:00
|
|
|
}
|
|
|
|
|
2008-07-25 17:24:43 +00:00
|
|
|
int ast_devstate_changed_literal(enum ast_device_state state, const char *device)
|
2005-07-08 21:15:41 +00:00
|
|
|
{
|
2006-02-09 16:59:50 +00:00
|
|
|
struct state_change *change;
|
2005-07-08 21:15:41 +00:00
|
|
|
|
2008-07-26 14:57:50 +00:00
|
|
|
/*
|
|
|
|
* If we know the state change (how nice of the caller of this function!)
|
|
|
|
* then we can just generate a device state event.
|
|
|
|
*
|
|
|
|
* Otherwise, we do the following:
|
|
|
|
* - Queue an event up to another thread that the state has changed
|
|
|
|
* - In the processing thread, it calls the callback provided by the
|
|
|
|
* device state provider (which may or may not be a channel driver)
|
|
|
|
* to determine the state.
|
|
|
|
* - If the device state provider does not know the state, or this is
|
|
|
|
* for a channel and the channel driver does not implement a device
|
|
|
|
* state callback, then we will look through the channel list to
|
|
|
|
* see if we can determine a state based on active calls.
|
|
|
|
* - Once a state has been determined, a device state event is generated.
|
|
|
|
*/
|
2007-05-14 18:39:16 +00:00
|
|
|
|
2007-08-10 16:24:11 +00:00
|
|
|
if (state != AST_DEVICE_UNKNOWN) {
|
2008-07-26 14:46:13 +00:00
|
|
|
devstate_event(device, state);
|
2007-08-10 16:24:11 +00:00
|
|
|
} else if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
|
2005-07-08 21:15:41 +00:00
|
|
|
/* we could not allocate a change struct, or */
|
|
|
|
/* there is no background thread, so process the change now */
|
|
|
|
do_state_change(device);
|
|
|
|
} else {
|
|
|
|
/* queue the change */
|
|
|
|
strcpy(change->device, device);
|
|
|
|
AST_LIST_LOCK(&state_changes);
|
|
|
|
AST_LIST_INSERT_TAIL(&state_changes, change, list);
|
2007-02-02 00:49:14 +00:00
|
|
|
ast_cond_signal(&change_pending);
|
2005-07-10 13:06:54 +00:00
|
|
|
AST_LIST_UNLOCK(&state_changes);
|
2005-07-08 21:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-10-17 15:41:55 +00:00
|
|
|
int ast_device_state_changed_literal(const char *dev)
|
|
|
|
{
|
2008-07-25 17:24:43 +00:00
|
|
|
return ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, dev);
|
2007-08-10 16:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ast_devstate_changed(enum ast_device_state state, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char buf[AST_MAX_EXTENSION];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2008-07-25 17:24:43 +00:00
|
|
|
return ast_devstate_changed_literal(state, buf);
|
2005-10-17 15:41:55 +00:00
|
|
|
}
|
|
|
|
|
2006-01-20 00:18:42 +00:00
|
|
|
/*! \brief Accept change notification, add it to change queue */
|
2005-10-17 15:41:55 +00:00
|
|
|
int ast_device_state_changed(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char buf[AST_MAX_EXTENSION];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
va_end(ap);
|
2007-08-10 16:24:11 +00:00
|
|
|
|
2008-07-25 17:24:43 +00:00
|
|
|
return ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, buf);
|
2005-10-17 15:41:55 +00:00
|
|
|
}
|
|
|
|
|
2006-01-20 00:18:42 +00:00
|
|
|
/*! \brief Go through the dev state change queue and update changes in the dev state thread */
|
2005-07-20 00:34:01 +00:00
|
|
|
static void *do_devstate_changes(void *data)
|
2005-07-08 21:15:41 +00:00
|
|
|
{
|
2007-02-02 00:49:14 +00:00
|
|
|
struct state_change *next, *current;
|
2005-07-08 21:15:41 +00:00
|
|
|
|
2007-01-23 00:11:32 +00:00
|
|
|
for (;;) {
|
2007-02-02 00:49:14 +00:00
|
|
|
/* This basically pops off any state change entries, resets the list back to NULL, unlocks, and processes each state change */
|
|
|
|
AST_LIST_LOCK(&state_changes);
|
|
|
|
if (AST_LIST_EMPTY(&state_changes))
|
2005-10-28 16:35:43 +00:00
|
|
|
ast_cond_wait(&change_pending, &state_changes.lock);
|
2007-02-02 00:49:14 +00:00
|
|
|
next = AST_LIST_FIRST(&state_changes);
|
|
|
|
AST_LIST_HEAD_INIT_NOLOCK(&state_changes);
|
|
|
|
AST_LIST_UNLOCK(&state_changes);
|
|
|
|
|
|
|
|
/* Process each state change */
|
|
|
|
while ((current = next)) {
|
|
|
|
next = AST_LIST_NEXT(current, list);
|
|
|
|
do_state_change(current->device);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(current);
|
2005-07-08 21:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-06-10 15:12:17 +00:00
|
|
|
static void destroy_devstate_change(struct devstate_change *sc)
|
|
|
|
{
|
|
|
|
ast_free(sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_SERVERS 64
|
|
|
|
struct change_collection {
|
|
|
|
struct devstate_change states[MAX_SERVERS];
|
|
|
|
size_t num_states;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void devstate_cache_cb(const struct ast_event *event, void *data)
|
|
|
|
{
|
|
|
|
struct change_collection *collection = data;
|
|
|
|
int i;
|
|
|
|
const struct ast_eid *eid;
|
|
|
|
|
|
|
|
if (collection->num_states == ARRAY_LEN(collection->states)) {
|
|
|
|
ast_log(LOG_ERROR, "More per-server state values than we have room for (MAX_SERVERS is %d)\n",
|
|
|
|
MAX_SERVERS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(eid = ast_event_get_ie_raw(event, AST_EVENT_IE_EID))) {
|
|
|
|
ast_log(LOG_ERROR, "Device state change event with no EID\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = collection->num_states;
|
|
|
|
|
|
|
|
collection->states[i].state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
|
|
|
|
collection->states[i].eid = *eid;
|
|
|
|
|
|
|
|
collection->num_states++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void process_collection(const char *device, struct change_collection *collection)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct ast_devstate_aggregate agg;
|
|
|
|
enum ast_device_state state;
|
|
|
|
struct ast_event *event;
|
|
|
|
|
|
|
|
ast_devstate_aggregate_init(&agg);
|
|
|
|
|
|
|
|
for (i = 0; i < collection->num_states; i++) {
|
|
|
|
ast_debug(1, "Adding per-server state of '%s' for '%s'\n",
|
2008-11-04 18:47:20 +00:00
|
|
|
ast_devstate2str(collection->states[i].state), device);
|
2008-06-10 15:12:17 +00:00
|
|
|
ast_devstate_aggregate_add(&agg, collection->states[i].state);
|
|
|
|
}
|
|
|
|
|
|
|
|
state = ast_devstate_aggregate_result(&agg);
|
|
|
|
|
|
|
|
ast_debug(1, "Aggregate devstate result is %d\n", state);
|
|
|
|
|
|
|
|
event = ast_event_get_cached(AST_EVENT_DEVICE_STATE,
|
|
|
|
AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
|
|
|
|
AST_EVENT_IE_END);
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
enum ast_device_state old_state;
|
|
|
|
|
|
|
|
old_state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
|
|
|
|
|
|
|
|
ast_event_destroy(event);
|
|
|
|
|
|
|
|
if (state == old_state) {
|
|
|
|
/* No change since last reported device state */
|
|
|
|
ast_debug(1, "Aggregate state for device '%s' has not changed from '%s'\n",
|
2008-11-04 18:47:20 +00:00
|
|
|
device, ast_devstate2str(state));
|
2008-06-10 15:12:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_debug(1, "Aggregate state for device '%s' has changed to '%s'\n",
|
2008-11-04 18:47:20 +00:00
|
|
|
device, ast_devstate2str(state));
|
2008-06-10 15:12:17 +00:00
|
|
|
|
|
|
|
event = ast_event_new(AST_EVENT_DEVICE_STATE,
|
|
|
|
AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
|
|
|
|
AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
|
|
|
|
AST_EVENT_IE_END);
|
|
|
|
|
|
|
|
if (!event)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ast_event_queue_and_cache(event,
|
|
|
|
AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR,
|
|
|
|
AST_EVENT_IE_END);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_devstate_change(struct devstate_change *sc)
|
|
|
|
{
|
|
|
|
struct ast_event_sub *tmp_sub;
|
|
|
|
struct change_collection collection = {
|
|
|
|
.num_states = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
ast_debug(1, "Processing device state change for '%s'\n", sc->device);
|
|
|
|
|
|
|
|
if (!(tmp_sub = ast_event_subscribe_new(AST_EVENT_DEVICE_STATE_CHANGE, devstate_cache_cb, &collection))) {
|
|
|
|
ast_log(LOG_ERROR, "Failed to create subscription\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ast_event_sub_append_ie_str(tmp_sub, AST_EVENT_IE_DEVICE, sc->device)) {
|
|
|
|
ast_log(LOG_ERROR, "Failed to append device IE\n");
|
|
|
|
ast_event_sub_destroy(tmp_sub);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Populate the collection of device states from the cache */
|
|
|
|
ast_event_dump_cache(tmp_sub);
|
|
|
|
|
|
|
|
process_collection(sc->device, &collection);
|
|
|
|
|
|
|
|
ast_event_sub_destroy(tmp_sub);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *run_devstate_collector(void *data)
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
struct devstate_change *sc;
|
|
|
|
|
|
|
|
ast_mutex_lock(&devstate_collector.lock);
|
|
|
|
while (!(sc = AST_LIST_REMOVE_HEAD(&devstate_collector.devstate_change_q, entry)))
|
|
|
|
ast_cond_wait(&devstate_collector.cond, &devstate_collector.lock);
|
|
|
|
ast_mutex_unlock(&devstate_collector.lock);
|
|
|
|
|
|
|
|
handle_devstate_change(sc);
|
|
|
|
|
|
|
|
destroy_devstate_change(sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void devstate_change_collector_cb(const struct ast_event *event, void *data)
|
|
|
|
{
|
|
|
|
struct devstate_change *sc;
|
|
|
|
const char *device;
|
|
|
|
const struct ast_eid *eid;
|
|
|
|
uint32_t state;
|
|
|
|
|
|
|
|
device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
|
|
|
|
eid = ast_event_get_ie_raw(event, AST_EVENT_IE_EID);
|
|
|
|
state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
|
|
|
|
|
|
|
|
if (ast_strlen_zero(device) || !eid) {
|
|
|
|
ast_log(LOG_ERROR, "Invalid device state change event received\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(sc = ast_calloc(1, sizeof(*sc) + strlen(device))))
|
|
|
|
return;
|
|
|
|
|
|
|
|
strcpy(sc->device, device);
|
|
|
|
sc->eid = *eid;
|
|
|
|
sc->state = state;
|
|
|
|
|
|
|
|
ast_mutex_lock(&devstate_collector.lock);
|
|
|
|
AST_LIST_INSERT_TAIL(&devstate_collector.devstate_change_q, sc, entry);
|
|
|
|
ast_cond_signal(&devstate_collector.cond);
|
|
|
|
ast_mutex_unlock(&devstate_collector.lock);
|
|
|
|
}
|
|
|
|
|
2006-01-20 00:18:42 +00:00
|
|
|
/*! \brief Initialize the device state engine in separate thread */
|
2005-07-08 21:15:41 +00:00
|
|
|
int ast_device_state_engine_init(void)
|
|
|
|
{
|
2008-06-10 15:12:17 +00:00
|
|
|
devstate_collector.event_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE_CHANGE,
|
|
|
|
devstate_change_collector_cb, NULL, AST_EVENT_IE_END);
|
|
|
|
|
|
|
|
if (!devstate_collector.event_sub) {
|
|
|
|
ast_log(LOG_ERROR, "Failed to create subscription for the device state change collector\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_mutex_init(&devstate_collector.lock);
|
|
|
|
ast_cond_init(&devstate_collector.cond, NULL);
|
|
|
|
if (ast_pthread_create_background(&devstate_collector.thread, NULL, run_devstate_collector, NULL) < 0) {
|
|
|
|
ast_log(LOG_ERROR, "Unable to start device state collector thread.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-10-28 16:35:43 +00:00
|
|
|
ast_cond_init(&change_pending, NULL);
|
2006-10-04 19:51:38 +00:00
|
|
|
if (ast_pthread_create_background(&change_thread, NULL, do_devstate_changes, NULL) < 0) {
|
2005-07-08 21:15:41 +00:00
|
|
|
ast_log(LOG_ERROR, "Unable to start device state change thread.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-10 14:06:29 +00:00
|
|
|
|
|
|
|
void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg)
|
|
|
|
{
|
|
|
|
memset(agg, 0, sizeof(*agg));
|
|
|
|
|
|
|
|
agg->all_unavail = 1;
|
|
|
|
agg->all_busy = 1;
|
|
|
|
agg->all_free = 1;
|
|
|
|
agg->all_on_hold = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ast_devstate_aggregate_add(struct ast_devstate_aggregate *agg, enum ast_device_state state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case AST_DEVICE_NOT_INUSE:
|
|
|
|
agg->all_unavail = 0;
|
|
|
|
agg->all_busy = 0;
|
|
|
|
agg->all_on_hold = 0;
|
|
|
|
break;
|
|
|
|
case AST_DEVICE_INUSE:
|
|
|
|
agg->in_use = 1;
|
|
|
|
agg->all_busy = 0;
|
|
|
|
agg->all_unavail = 0;
|
|
|
|
agg->all_free = 0;
|
|
|
|
agg->all_on_hold = 0;
|
|
|
|
break;
|
|
|
|
case AST_DEVICE_RINGING:
|
|
|
|
agg->ring = 1;
|
|
|
|
agg->all_busy = 0;
|
|
|
|
agg->all_unavail = 0;
|
|
|
|
agg->all_free = 0;
|
|
|
|
agg->all_on_hold = 0;
|
|
|
|
break;
|
|
|
|
case AST_DEVICE_RINGINUSE:
|
|
|
|
agg->in_use = 1;
|
|
|
|
agg->ring = 1;
|
|
|
|
agg->all_busy = 0;
|
|
|
|
agg->all_unavail = 0;
|
|
|
|
agg->all_free = 0;
|
|
|
|
agg->all_on_hold = 0;
|
|
|
|
break;
|
|
|
|
case AST_DEVICE_ONHOLD:
|
|
|
|
agg->all_unavail = 0;
|
|
|
|
agg->all_free = 0;
|
|
|
|
break;
|
|
|
|
case AST_DEVICE_BUSY:
|
|
|
|
agg->all_unavail = 0;
|
|
|
|
agg->all_free = 0;
|
|
|
|
agg->all_on_hold = 0;
|
|
|
|
agg->busy = 1;
|
|
|
|
break;
|
|
|
|
case AST_DEVICE_UNAVAILABLE:
|
|
|
|
case AST_DEVICE_INVALID:
|
|
|
|
agg->all_busy = 0;
|
|
|
|
agg->all_free = 0;
|
|
|
|
agg->all_on_hold = 0;
|
|
|
|
break;
|
|
|
|
case AST_DEVICE_UNKNOWN:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg)
|
|
|
|
{
|
|
|
|
if (agg->all_free)
|
|
|
|
return AST_DEVICE_NOT_INUSE;
|
|
|
|
|
|
|
|
if (agg->all_on_hold)
|
|
|
|
return AST_DEVICE_ONHOLD;
|
|
|
|
|
|
|
|
if (agg->all_busy)
|
|
|
|
return AST_DEVICE_BUSY;
|
|
|
|
|
|
|
|
if (agg->all_unavail)
|
|
|
|
return AST_DEVICE_UNAVAILABLE;
|
|
|
|
|
|
|
|
if (agg->ring)
|
|
|
|
return agg->in_use ? AST_DEVICE_RINGINUSE : AST_DEVICE_RINGING;
|
|
|
|
|
|
|
|
if (agg->in_use)
|
|
|
|
return AST_DEVICE_INUSE;
|
|
|
|
|
|
|
|
if (agg->busy)
|
|
|
|
return AST_DEVICE_BUSY;
|
|
|
|
|
|
|
|
return AST_DEVICE_NOT_INUSE;
|
|
|
|
}
|
|
|
|
|