2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2007, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Device state management
24 * \author Mark Spencer <markster@digium.com>
26 * \arg \ref AstExtState
29 /*! \page AstExtState Extension and device states in Asterisk
31 * Asterisk has an internal system that reports states
32 * for an extension. By using the dialplan priority -1,
33 * also called a \b hint, a connection can be made from an
34 * extension to one or many devices. The state of the extension
35 * now depends on the combined state of the devices.
37 * The device state is basically based on the current calls.
38 * If the devicestate engine can find a call from or to the
39 * device, it's in use.
41 * Some channel drivers implement a callback function for
42 * a better level of reporting device states. The SIP channel
43 * has a complicated system for this, which is improved
44 * by adding call limits to the configuration.
46 * Functions that want to check the status of an extension
47 * register themself as a \b watcher.
48 * Watchers in this system can subscribe either to all extensions
49 * or just a specific extensions.
51 * For non-device related states, there's an API called
52 * devicestate providers. This is an extendible system for
53 * delivering state information from outside sources or
54 * functions within Asterisk. Currently we have providers
55 * for app_meetme.c - the conference bridge - and call
56 * parking (metermaids).
58 * There are manly three subscribers to extension states
60 * - AMI, the manager interface
61 * - app_queue.c - the Queue dialplan application
62 * - SIP subscriptions, a.k.a. "blinking lamps" or
65 * The CLI command "show hints" show last known state
67 * \note None of these handle user states, like an IM presence
68 * system. res_jabber.c can subscribe and watch such states
69 * in jabber/xmpp based systems.
71 * \section AstDevStateArch Architecture for devicestates
73 * When a channel driver or asterisk app changes state for
74 * a watched object, it alerts the core. The core queues
75 * a change. When the change is processed, there's a query
76 * sent to the channel driver/provider if there's a function
77 * to handle that, otherwise a channel walk is issued to find
78 * a channel that involves the object.
80 * The changes are queued and processed by a separate thread.
81 * This thread calls the watchers subscribing to status
82 * changes for the object. For manager, this results
83 * in events. For SIP, NOTIFY requests.
86 * \arg \ref devicestate.c
87 * \arg \ref devicestate.h
89 * \section AstExtStateArch Architecture for extension states
91 * Hints are connected to extension. If an extension changes state
92 * it checks the hint devices. If there is a hint, the callbacks into
93 * device states are checked. The aggregated state is set for the hint
97 * \arg \ref AstENUM ast_extension_states
101 * - \ref ast_state_cb struct. Callbacks for watchers
102 * - Callback ast_state_cb_type
103 * - \ref ast_hint struct.
105 * - ast_extension_state_add()
106 * - ast_extension_state_del()
111 #include "asterisk.h"
113 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
115 #include "asterisk/_private.h"
116 #include "asterisk/channel.h"
117 #include "asterisk/utils.h"
118 #include "asterisk/lock.h"
119 #include "asterisk/linkedlists.h"
120 #include "asterisk/devicestate.h"
121 #include "asterisk/pbx.h"
122 #include "asterisk/app.h"
123 #include "asterisk/event.h"
125 /*! \brief Device state strings for printing */
126 static const char *devstatestring[] = {
127 /* 0 AST_DEVICE_UNKNOWN */ "Unknown", /*!< Valid, but unknown state */
128 /* 1 AST_DEVICE_NOT_INUSE */ "Not in use", /*!< Not used */
129 /* 2 AST_DEVICE IN USE */ "In use", /*!< In use */
130 /* 3 AST_DEVICE_BUSY */ "Busy", /*!< Busy */
131 /* 4 AST_DEVICE_INVALID */ "Invalid", /*!< Invalid - not known to Asterisk */
132 /* 5 AST_DEVICE_UNAVAILABLE */ "Unavailable", /*!< Unavailable (not registred) */
133 /* 6 AST_DEVICE_RINGING */ "Ringing", /*!< Ring, ring, ring */
134 /* 7 AST_DEVICE_RINGINUSE */ "Ring+Inuse", /*!< Ring and in use */
135 /* 8 AST_DEVICE_ONHOLD */ "On Hold" /*!< On Hold */
138 /*! \brief A device state provider (not a channel) */
139 struct devstate_prov {
141 ast_devstate_prov_cb_type callback;
142 AST_RWLIST_ENTRY(devstate_prov) list;
145 /*! \brief A list of providers */
146 static AST_RWLIST_HEAD_STATIC(devstate_provs, devstate_prov);
148 struct state_change {
149 AST_LIST_ENTRY(state_change) list;
153 /*! \brief The state change queue. State changes are queued
154 for processing by a separate thread */
155 static AST_LIST_HEAD_STATIC(state_changes, state_change);
157 /*! \brief The device state change notification thread */
158 static pthread_t change_thread = AST_PTHREADT_NULL;
160 /*! \brief Flag for the queue */
161 static ast_cond_t change_pending;
163 /*! \brief Whether or not to cache this device state value */
164 enum devstate_cache {
165 /*! Cache this value as it is coming from a device state provider which is
166 * pushing up state change events to us as they happen */
168 /*! Don't cache this result, since it was pulled from the device state provider.
169 * We only want to cache results from device state providers that are being nice
170 * and pushing state change events up to us as they happen. */
174 /* Forward declarations */
175 static int getproviderstate(const char *provider, const char *address);
177 /*! \brief Find devicestate as text message for output */
178 const char *devstate2str(enum ast_device_state devstate)
180 return devstatestring[devstate];
183 const char *ast_devstate_str(enum ast_device_state state)
185 const char *res = "UNKNOWN";
188 case AST_DEVICE_UNKNOWN:
190 case AST_DEVICE_NOT_INUSE:
193 case AST_DEVICE_INUSE:
196 case AST_DEVICE_BUSY:
199 case AST_DEVICE_INVALID:
202 case AST_DEVICE_UNAVAILABLE:
205 case AST_DEVICE_RINGING:
208 case AST_DEVICE_RINGINUSE:
211 case AST_DEVICE_ONHOLD:
219 enum ast_device_state ast_devstate_val(const char *val)
221 if (!strcasecmp(val, "NOT_INUSE"))
222 return AST_DEVICE_NOT_INUSE;
223 else if (!strcasecmp(val, "INUSE"))
224 return AST_DEVICE_INUSE;
225 else if (!strcasecmp(val, "BUSY"))
226 return AST_DEVICE_BUSY;
227 else if (!strcasecmp(val, "INVALID"))
228 return AST_DEVICE_INVALID;
229 else if (!strcasecmp(val, "UNAVAILABLE"))
230 return AST_DEVICE_UNAVAILABLE;
231 else if (!strcasecmp(val, "RINGING"))
232 return AST_DEVICE_RINGING;
233 else if (!strcasecmp(val, "RINGINUSE"))
234 return AST_DEVICE_RINGINUSE;
235 else if (!strcasecmp(val, "ONHOLD"))
236 return AST_DEVICE_ONHOLD;
238 return AST_DEVICE_UNKNOWN;
241 /*! \brief Find out if device is active in a call or not
242 \note find channels with the device's name in it
243 This function is only used for channels that does not implement
246 enum ast_device_state ast_parse_device_state(const char *device)
248 struct ast_channel *chan;
249 char match[AST_CHANNEL_NAME];
250 enum ast_device_state res;
252 ast_copy_string(match, device, sizeof(match)-1);
254 chan = ast_get_channel_by_name_prefix_locked(match, strlen(match));
257 return AST_DEVICE_UNKNOWN;
259 if (chan->_state == AST_STATE_RINGING)
260 res = AST_DEVICE_RINGING;
262 res = AST_DEVICE_INUSE;
264 ast_channel_unlock(chan);
269 static enum ast_device_state devstate_cached(const char *device)
271 enum ast_device_state res = AST_DEVICE_UNKNOWN;
272 struct ast_event *event;
274 event = ast_event_get_cached(AST_EVENT_DEVICE_STATE,
275 AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
281 res = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
283 ast_event_destroy(event);
288 /*! \brief Check device state through channel specific function or generic function */
289 enum ast_device_state ast_device_state(const char *device)
293 const struct ast_channel_tech *chan_tech;
294 enum ast_device_state res;
295 /*! \brief Channel driver that provides device state */
297 /*! \brief Another provider of device state */
298 char *provider = NULL;
300 /* If the last known state is cached, just return that */
301 res = devstate_cached(device);
302 if (res != AST_DEVICE_UNKNOWN)
305 buf = ast_strdupa(device);
306 tech = strsep(&buf, "/");
307 if (!(number = buf)) {
308 if (!(provider = strsep(&tech, ":")))
309 return AST_DEVICE_INVALID;
310 /* We have a provider */
316 ast_debug(3, "Checking if I can find provider for \"%s\" - number: %s\n", provider, number);
317 return getproviderstate(provider, number);
320 ast_debug(4, "No provider found, checking channel drivers for %s - %s\n", tech, number);
322 if (!(chan_tech = ast_get_channel_tech(tech)))
323 return AST_DEVICE_INVALID;
325 if (!(chan_tech->devicestate)) /* Does the channel driver support device state notification? */
326 return ast_parse_device_state(device); /* No, try the generic function */
328 res = chan_tech->devicestate(number);
330 if (res != AST_DEVICE_UNKNOWN)
333 res = ast_parse_device_state(device);
335 if (res == AST_DEVICE_UNKNOWN)
336 return AST_DEVICE_NOT_INUSE;
341 /*! \brief Add device state provider */
342 int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback)
344 struct devstate_prov *devprov;
346 if (!callback || !(devprov = ast_calloc(1, sizeof(*devprov))))
349 devprov->callback = callback;
350 ast_copy_string(devprov->label, label, sizeof(devprov->label));
352 AST_RWLIST_WRLOCK(&devstate_provs);
353 AST_RWLIST_INSERT_HEAD(&devstate_provs, devprov, list);
354 AST_RWLIST_UNLOCK(&devstate_provs);
359 /*! \brief Remove device state provider */
360 int ast_devstate_prov_del(const char *label)
362 struct devstate_prov *devcb;
365 AST_RWLIST_WRLOCK(&devstate_provs);
366 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&devstate_provs, devcb, list) {
367 if (!strcasecmp(devcb->label, label)) {
368 AST_RWLIST_REMOVE_CURRENT(list);
374 AST_RWLIST_TRAVERSE_SAFE_END;
375 AST_RWLIST_UNLOCK(&devstate_provs);
380 /*! \brief Get provider device state */
381 static int getproviderstate(const char *provider, const char *address)
383 struct devstate_prov *devprov;
384 int res = AST_DEVICE_INVALID;
387 AST_RWLIST_RDLOCK(&devstate_provs);
388 AST_RWLIST_TRAVERSE(&devstate_provs, devprov, list) {
389 ast_debug(5, "Checking provider %s with %s\n", devprov->label, provider);
391 if (!strcasecmp(devprov->label, provider)) {
392 res = devprov->callback(address);
396 AST_RWLIST_UNLOCK(&devstate_provs);
400 static void devstate_event(const char *device, enum ast_device_state state, enum devstate_cache cache)
402 struct ast_event *event;
404 if (!(event = ast_event_new(AST_EVENT_DEVICE_STATE,
405 AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
406 AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
407 AST_EVENT_IE_END))) {
411 if (cache == CACHE_ON) {
412 /* Cache this event, replacing an event in the cache with the same
413 * device name if it exists. */
414 ast_event_queue_and_cache(event,
415 AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR,
418 ast_event_queue(event);
422 /*! Called by the state change thread to find out what the state is, and then
423 * to queue up the state change event */
424 static void do_state_change(const char *device)
426 enum ast_device_state state;
428 state = ast_device_state(device);
430 ast_debug(3, "Changing state for %s - state %d (%s)\n", device, state, devstate2str(state));
432 devstate_event(device, state, CACHE_OFF);
435 static int __ast_devstate_changed_literal(enum ast_device_state state, char *buf, int norecurse)
438 struct state_change *change;
441 ast_debug(3, "Notification of state change to be queued on device/channel %s\n", buf);
445 if (state != AST_DEVICE_UNKNOWN) {
446 devstate_event(device, state, CACHE_ON);
447 } else if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
448 /* we could not allocate a change struct, or */
449 /* there is no background thread, so process the change now */
450 do_state_change(device);
452 /* queue the change */
453 strcpy(change->device, device);
454 AST_LIST_LOCK(&state_changes);
455 AST_LIST_INSERT_TAIL(&state_changes, change, list);
456 ast_cond_signal(&change_pending);
457 AST_LIST_UNLOCK(&state_changes);
460 /* The problem with this API is that a device may be called with the unique
461 * identifier appended or not, but it's separated from the channel name
462 * with a '-', which is also a legitimate character in a channel name. So,
463 * we have to force both names to get their names checked for state changes
464 * to ensure that the right one gets notified. Not a huge performance hit,
465 * but it might could be fixed by an enterprising programmer in trunk.
467 if (!norecurse && (tmp = strrchr(device, '-'))) {
469 __ast_devstate_changed_literal(state, tmp, 1);
475 int ast_devstate_changed_literal(enum ast_device_state state, const char *dev)
479 buf = ast_strdupa(dev);
481 return __ast_devstate_changed_literal(state, buf, 0);
484 int ast_device_state_changed_literal(const char *dev)
488 buf = ast_strdupa(dev);
490 return __ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, buf, 0);
493 int ast_devstate_changed(enum ast_device_state state, const char *fmt, ...)
495 char buf[AST_MAX_EXTENSION];
499 vsnprintf(buf, sizeof(buf), fmt, ap);
502 return __ast_devstate_changed_literal(state, buf, 0);
505 /*! \brief Accept change notification, add it to change queue */
506 int ast_device_state_changed(const char *fmt, ...)
508 char buf[AST_MAX_EXTENSION];
512 vsnprintf(buf, sizeof(buf), fmt, ap);
515 return __ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, buf, 0);
518 /*! \brief Go through the dev state change queue and update changes in the dev state thread */
519 static void *do_devstate_changes(void *data)
521 struct state_change *next, *current;
524 /* This basically pops off any state change entries, resets the list back to NULL, unlocks, and processes each state change */
525 AST_LIST_LOCK(&state_changes);
526 if (AST_LIST_EMPTY(&state_changes))
527 ast_cond_wait(&change_pending, &state_changes.lock);
528 next = AST_LIST_FIRST(&state_changes);
529 AST_LIST_HEAD_INIT_NOLOCK(&state_changes);
530 AST_LIST_UNLOCK(&state_changes);
532 /* Process each state change */
533 while ((current = next)) {
534 next = AST_LIST_NEXT(current, list);
535 do_state_change(current->device);
543 /*! \brief Initialize the device state engine in separate thread */
544 int ast_device_state_engine_init(void)
546 ast_cond_init(&change_pending, NULL);
547 if (ast_pthread_create_background(&change_thread, NULL, do_devstate_changes, NULL) < 0) {
548 ast_log(LOG_ERROR, "Unable to start device state change thread.\n");