aboutsummaryrefslogtreecommitdiffstats
path: root/main/devicestate.c
blob: 1601ee12c37fa9e6917bba3840dfaa63bd81c50e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 1999 - 2008, Digium, Inc.
 *
 * Mark Spencer <markster@digium.com>
 * Russell Bryant <russell@digium.com>
 *
 * 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.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Device state management
 *
 * \author Mark Spencer <markster@digium.com> 
 * \author Russell Bryant <russell@digium.com>
 *
 *	\arg \ref AstExtState
 */

/*! \page AstExtState Extension and device states in Asterisk
 *
 * (Note that these descriptions of device states and extension
 * states have not been updated to the way things work
 * in Asterisk 1.6.)
 *
 *	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
 *	devicestate providers. This is an extendible system for
 *	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"
 *
 *	The CLI command "show hints" show last known state
 *
 *	\note None of these handle user states, like an IM presence
 *	system. res_jabber.c can subscribe and watch such states
 *	in jabber/xmpp based systems.
 *
 *	\section AstDevStateArch Architecture for devicestates
 *
 *	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.
 *
 *	- Device states
 *		\arg \ref devicestate.c 
 *		\arg \ref devicestate.h 
 *
 *	\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.
 *
 *	- Extension states
 *		\arg \ref AstENUM ast_extension_states
 *		\arg \ref pbx.c 
 *		\arg \ref pbx.h 
 *	- Structures
 *		- \ref ast_state_cb struct.  Callbacks for watchers
 *		- Callback ast_state_cb_type
 *		- \ref ast_hint struct.
 * 	- Functions
 *		- ast_extension_state_add()
 *		- ast_extension_state_del()
 *		- ast_get_hint()
 *	
 */

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "asterisk/_private.h"
#include "asterisk/channel.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/linkedlists.h"
#include "asterisk/devicestate.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/event.h"

/*! \brief Device state strings for printing */
static const char *devstatestring[] = {
	/* 0 AST_DEVICE_UNKNOWN */	"Unknown",	/*!< Valid, but unknown state */
	/* 1 AST_DEVICE_NOT_INUSE */	"Not in use",	/*!< Not used */
	/* 2 AST_DEVICE IN USE */	"In use",	/*!< In use */
	/* 3 AST_DEVICE_BUSY */		"Busy",		/*!< Busy */
	/* 4 AST_DEVICE_INVALID */	"Invalid",	/*!< Invalid - not known to Asterisk */
	/* 5 AST_DEVICE_UNAVAILABLE */	"Unavailable",	/*!< Unavailable (not registred) */
	/* 6 AST_DEVICE_RINGING */	"Ringing",	/*!< Ring, ring, ring */
	/* 7 AST_DEVICE_RINGINUSE */	"Ring+Inuse",	/*!< Ring and in use */
	/* 8 AST_DEVICE_ONHOLD */	"On Hold"	/*!< On Hold */
};

/*!\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 },
};

/*! \brief  A device state provider (not a channel) */
struct devstate_prov {
	char label[40];
	ast_devstate_prov_cb_type callback;
	AST_RWLIST_ENTRY(devstate_prov) list;
};

/*! \brief A list of providers */
static AST_RWLIST_HEAD_STATIC(devstate_provs, devstate_prov);

struct state_change {
	AST_LIST_ENTRY(state_change) list;
	char device[1];
};

/*! \brief The state change queue. State changes are queued
	for processing by a separate thread */
static AST_LIST_HEAD_STATIC(state_changes, state_change);

/*! \brief The device state change notification thread */
static pthread_t change_thread = AST_PTHREADT_NULL;

/*! \brief Flag for the queue */
static ast_cond_t change_pending;

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,
};

/* Forward declarations */
static int getproviderstate(const char *provider, const char *address);

/*! \brief Find devicestate as text message for output */
const char *devstate2str(enum ast_device_state devstate) 
{
	return devstatestring[devstate];
}

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;
}

const char *ast_devstate_str(enum ast_device_state state)
{
	const char *res = "UNKNOWN";

	switch (state) {
	case AST_DEVICE_UNKNOWN:
		break;
	case AST_DEVICE_NOT_INUSE:
		res = "NOT_INUSE";
		break;
	case AST_DEVICE_INUSE:
		res = "INUSE";
		break;
	case AST_DEVICE_BUSY:
		res = "BUSY";
		break;
	case AST_DEVICE_INVALID:
		res = "INVALID";
		break;
	case AST_DEVICE_UNAVAILABLE:
		res = "UNAVAILABLE";
		break;
	case AST_DEVICE_RINGING:
		res = "RINGING";
		break;
	case AST_DEVICE_RINGINUSE:
		res = "RINGINUSE";
		break;
	case AST_DEVICE_ONHOLD:
		res = "ONHOLD";
		break;
	}

	return res;
}

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;
}

/*! \brief Find out if device is active in a call or not 
	\note find channels with the device's name in it
	This function is only used for channels that does not implement 
	devicestate natively
*/
enum ast_device_state ast_parse_device_state(const char *device)
{
	struct ast_channel *chan;
	char match[AST_CHANNEL_NAME];
	enum ast_device_state res;

	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;
	
	ast_channel_unlock(chan);

	return res;
}

static enum ast_device_state devstate_cached(const char *device)
{
	enum ast_device_state res = AST_DEVICE_UNKNOWN;
	struct ast_event *event;

	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)
		return res;

	res = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);

	ast_event_destroy(event);

	return res;
}

/*! \brief Check device state through channel specific function or generic function */
static enum ast_device_state _ast_device_state(const char *device, int check_cache)
{
	char *buf;
	char *number;
	const struct ast_channel_tech *chan_tech;
	enum ast_device_state res;
	/*! \brief Channel driver that provides device state */
	char *tech;
	/*! \brief Another provider of device state */
	char *provider = NULL;

	/* If the last known state is cached, just return that */
	if (check_cache) {
		res = devstate_cached(device);
		if (res != AST_DEVICE_UNKNOWN) {
			return res;
		}
	}

	buf = ast_strdupa(device);
	tech = strsep(&buf, "/");
	if (!(number = buf)) {
		if (!(provider = strsep(&tech, ":")))
			return AST_DEVICE_INVALID;
		/* We have a provider */
		number = tech;
		tech = NULL;
	}

	if (provider)  {
		ast_debug(3, "Checking if I can find provider for \"%s\" - number: %s\n", provider, number);
		return getproviderstate(provider, number);
	}

	ast_debug(4, "No provider found, checking channel drivers for %s - %s\n", tech, number);

	if (!(chan_tech = ast_get_channel_tech(tech)))
		return AST_DEVICE_INVALID;

	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;
}

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);
}

/*! \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));

	AST_RWLIST_WRLOCK(&devstate_provs);
	AST_RWLIST_INSERT_HEAD(&devstate_provs, devprov, list);
	AST_RWLIST_UNLOCK(&devstate_provs);

	return 0;
}

/*! \brief Remove device state provider */
int ast_devstate_prov_del(const char *label)
{
	struct devstate_prov *devcb;
	int res = -1;

	AST_RWLIST_WRLOCK(&devstate_provs);
	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&devstate_provs, devcb, list) {
		if (!strcasecmp(devcb->label, label)) {
			AST_RWLIST_REMOVE_CURRENT(list);
			ast_free(devcb);
			res = 0;
			break;
		}
	}
	AST_RWLIST_TRAVERSE_SAFE_END;
	AST_RWLIST_UNLOCK(&devstate_provs);

	return res;
}

/*! \brief Get provider device state */
static int getproviderstate(const char *provider, const char *address)
{
	struct devstate_prov *devprov;
	int res = AST_DEVICE_INVALID;

	AST_RWLIST_RDLOCK(&devstate_provs);
	AST_RWLIST_TRAVERSE(&devstate_provs, devprov, list) {
		ast_debug(5, "Checking provider %s with %s\n", devprov->label, provider);

		if (!strcasecmp(devprov->label, provider)) {
			res = devprov->callback(address);
			break;
		}
	}
	AST_RWLIST_UNLOCK(&devstate_provs);

	return res;
}

static void devstate_event(const char *device, enum ast_device_state state)
{
	struct ast_event *event;

	ast_debug(3, "device '%s' state '%d'\n", device, state);

	if (!(event = ast_event_new(AST_EVENT_DEVICE_STATE_CHANGE,
			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;
	}

	/* 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);
}

/*! 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;

	state = _ast_device_state(device, 0);

	ast_debug(3, "Changing state for %s - state %d (%s)\n", device, state, devstate2str(state));

	devstate_event(device, state);
}

int ast_devstate_changed_literal(enum ast_device_state state, const char *device)
{
	struct state_change *change;

	/* 
	 * 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.
	 */

	if (state != AST_DEVICE_UNKNOWN) {
		devstate_event(device, state);
	} else if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
		/* 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);
		ast_cond_signal(&change_pending);
		AST_LIST_UNLOCK(&state_changes);
	}

	return 1;
}

int ast_device_state_changed_literal(const char *dev)
{
	return ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, dev);
}

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);

	return ast_devstate_changed_literal(state, buf);
}

/*! \brief Accept change notification, add it to change queue */
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);

	return ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, buf);
}

/*! \brief Go through the dev state change queue and update changes in the dev state thread */
static void *do_devstate_changes(void *data)
{
	struct state_change *next, *current;

	for (;;) {
		/* 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))
			ast_cond_wait(&change_pending, &state_changes.lock);
		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);
			ast_free(current);
		}
	}

	return NULL;
}

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", 
			devstate2str(collection->states[i].state), device);
		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",
				device, devstate2str(state));
			return;
		}
	}

	ast_debug(1, "Aggregate state for device '%s' has changed to '%s'\n",
		device, devstate2str(state));

	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);
}

/*! \brief Initialize the device state engine in separate thread */
int ast_device_state_engine_init(void)
{
	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;
	}

	ast_cond_init(&change_pending, NULL);
	if (ast_pthread_create_background(&change_thread, NULL, do_devstate_changes, NULL) < 0) {
		ast_log(LOG_ERROR, "Unable to start device state change thread.\n");
		return -1;
	}

	return 0;
}

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;
}