Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
devicestate.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2008, Digium, Inc.
5 *
6 * Mark Spencer <markster@digium.com>
7 * Russell Bryant <russell@digium.com>
8 *
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
14 *
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
18 */
19
20/*! \file
21 *
22 * \brief Device state management
23 *
24 * \author Mark Spencer <markster@digium.com>
25 * \author Russell Bryant <russell@digium.com>
26 *
27 * \arg \ref AstExtState
28 */
29
30/*! \page AstExtState Extension and device states in Asterisk
31 *
32 * (Note that these descriptions of device states and extension
33 * states have not been updated to the way things work
34 * in Asterisk 1.6.)
35 *
36 * Asterisk has an internal system that reports states
37 * for an extension. By using the dialplan priority -1,
38 * also called a \b hint, a connection can be made from an
39 * extension to one or many devices. The state of the extension
40 * now depends on the combined state of the devices.
41 *
42 * The device state is basically based on the current calls.
43 * If the devicestate engine can find a call from or to the
44 * device, it's in use.
45 *
46 * Some channel drivers implement a callback function for
47 * a better level of reporting device states. The SIP channel
48 * has a complicated system for this, which is improved
49 * by adding call limits to the configuration.
50 *
51 * Functions that want to check the status of an extension
52 * register themself as a \b watcher.
53 * Watchers in this system can subscribe either to all extensions
54 * or just a specific extensions.
55 *
56 * For non-device related states, there's an API called
57 * devicestate providers. This is an extendible system for
58 * delivering state information from outside sources or
59 * functions within Asterisk. Currently we have providers
60 * for app_meetme.c - the conference bridge - and call
61 * parking (metermaids).
62 *
63 * There are manly three subscribers to extension states
64 * within Asterisk:
65 * - AMI, the manager interface
66 * - app_queue.c - the Queue dialplan application
67 * - SIP subscriptions, a.k.a. "blinking lamps" or
68 * "buddy lists"
69 *
70 * The CLI command "show hints" show last known state
71 *
72 * \note None of these handle user states, like an IM presence
73 * system. res_xmpp.c can subscribe and watch such states
74 * in jabber/xmpp based systems.
75 *
76 * \section AstDevStateArch Architecture for devicestates
77 *
78 * When a channel driver or asterisk app changes state for
79 * a watched object, it alerts the core. The core queues
80 * a change. When the change is processed, there's a query
81 * sent to the channel driver/provider if there's a function
82 * to handle that, otherwise a channel walk is issued to find
83 * a channel that involves the object.
84 *
85 * The changes are queued and processed by a separate thread.
86 * This thread calls the watchers subscribing to status
87 * changes for the object. For manager, this results
88 * in events. For SIP, NOTIFY requests.
89 *
90 * - Device states
91 * \arg \ref devicestate.c
92 * \arg \ref devicestate.h
93 *
94 * \section AstExtStateArch Architecture for extension states
95 *
96 * Hints are connected to extension. If an extension changes state
97 * it checks the hint devices. If there is a hint, the callbacks into
98 * device states are checked. The aggregated state is set for the hint
99 * and reported back.
100 *
101 * - Extension states
102 * \arg \ref ast_extension_states "AstENUM"
103 * \arg \ref pbx.c
104 * \arg \ref pbx.h
105 * - Structures
106 * - \ref ast_state_cb struct. Callbacks for watchers
107 * - Callback ast_state_cb_type
108 * - \ref ast_hint struct.
109 * - Functions
110 * - ast_extension_state_add()
111 * - ast_extension_state_del()
112 * - ast_get_hint()
113 *
114 */
115
116/*** MODULEINFO
117 <support_level>core</support_level>
118 ***/
119
120/*** DOCUMENTATION
121 <managerEvent language="en_US" name="DeviceStateChange">
122 <managerEventInstance class="EVENT_FLAG_CALL">
123 <since>
124 <version>13.0.0</version>
125 </since>
126 <synopsis>Raised when a device state changes</synopsis>
127 <syntax>
128 <parameter name="Device">
129 <para>The device whose state has changed</para>
130 </parameter>
131 <parameter name="State">
132 <para>The new state of the device</para>
133 </parameter>
134 </syntax>
135 <description>
136 <para>This differs from the <literal>ExtensionStatus</literal>
137 event because this event is raised for all device state changes,
138 not only for changes that affect dialplan hints.</para>
139 </description>
140 <see-also>
141 <ref type="managerEvent">ExtensionStatus</ref>
142 </see-also>
143 </managerEventInstance>
144 </managerEvent>
145***/
146
147#include "asterisk.h"
148
149#include "asterisk/_private.h"
150#include "asterisk/channel.h"
151#include "asterisk/utils.h"
152#include "asterisk/lock.h"
153#include "asterisk/linkedlists.h"
154#include "asterisk/devicestate.h"
155#include "asterisk/pbx.h"
156#include "asterisk/app.h"
157#include "asterisk/astobj2.h"
158#include "asterisk/stasis.h"
159#include "asterisk/devicestate.h"
160
161#define DEVSTATE_TOPIC_BUCKETS 57
162
163/*! \brief Device state strings for printing */
164static const char * const devstatestring[][2] = {
165 { /* 0 AST_DEVICE_UNKNOWN */ "Unknown", "UNKNOWN" }, /*!< Valid, but unknown state */
166 { /* 1 AST_DEVICE_NOT_INUSE */ "Not in use", "NOT_INUSE" }, /*!< Not used */
167 { /* 2 AST_DEVICE IN USE */ "In use", "INUSE" }, /*!< In use */
168 { /* 3 AST_DEVICE_BUSY */ "Busy", "BUSY" }, /*!< Busy */
169 { /* 4 AST_DEVICE_INVALID */ "Invalid", "INVALID" }, /*!< Invalid - not known to Asterisk */
170 { /* 5 AST_DEVICE_UNAVAILABLE */ "Unavailable", "UNAVAILABLE" }, /*!< Unavailable (not registered) */
171 { /* 6 AST_DEVICE_RINGING */ "Ringing", "RINGING" }, /*!< Ring, ring, ring */
172 { /* 7 AST_DEVICE_RINGINUSE */ "Ring+Inuse", "RINGINUSE" }, /*!< Ring and in use */
173 { /* 8 AST_DEVICE_ONHOLD */ "On Hold", "ONHOLD" }, /*!< On Hold */
174};
175
176/*!\brief Mapping for channel states to device states */
177static const struct chan2dev {
180} chan2dev[] = {
185 { AST_STATE_RING, AST_DEVICE_INUSE }, /* Audible ringback tone */
186 { AST_STATE_RINGING, AST_DEVICE_RINGING }, /* Actual ringing */
192
193/*! \brief A device state provider (not a channel) */
195 char label[40];
198};
199
200/*! \brief A list of providers */
202
206 char device[1];
207};
208
209/*! \brief The state change queue. State changes are queued
210 for processing by a separate thread */
212
213/*! \brief The device state change notification thread */
215
216/*! \brief Flag for the queue */
218static volatile int shuttingdown;
219
221
226
227static struct ast_manager_event_blob *devstate_to_ami(struct stasis_message *msg);
228static struct ast_event *devstate_to_event(struct stasis_message *msg);
229
230
233 .to_event = devstate_to_event,
234);
235
236/* Forward declarations */
237static int getproviderstate(const char *provider, const char *address);
238
239/*! \brief Find devicestate as text message for output */
240const char *ast_devstate2str(enum ast_device_state devstate)
241{
242 return devstatestring[devstate][0];
243}
244
246{
247 int i;
248 chanstate &= 0xFFFF;
249 for (i = 0; i < ARRAY_LEN(chan2dev); i++) {
250 if (chan2dev[i].chan == chanstate) {
251 return chan2dev[i].dev;
252 }
253 }
254 return AST_DEVICE_UNKNOWN;
255}
256
257/* Parseable */
259{
260 return devstatestring[state][1];
261}
262
264{
265 if (!strcasecmp(val, "NOT_INUSE"))
267 else if (!strcasecmp(val, "INUSE"))
268 return AST_DEVICE_INUSE;
269 else if (!strcasecmp(val, "BUSY"))
270 return AST_DEVICE_BUSY;
271 else if (!strcasecmp(val, "INVALID"))
272 return AST_DEVICE_INVALID;
273 else if (!strcasecmp(val, "UNAVAILABLE"))
275 else if (!strcasecmp(val, "RINGING"))
276 return AST_DEVICE_RINGING;
277 else if (!strcasecmp(val, "RINGINUSE"))
279 else if (!strcasecmp(val, "ONHOLD"))
280 return AST_DEVICE_ONHOLD;
281
282 return AST_DEVICE_UNKNOWN;
283}
284
285/*! \brief Find out if device is active in a call or not
286 \note find channels with the device's name in it
287 This function is only used for channels that does not implement
288 devicestate natively
289*/
291{
292 struct ast_channel *chan;
294 enum ast_device_state res;
295
296 snprintf(match, sizeof(match), "%s-", device);
297
298 if (!(chan = ast_channel_get_by_name_prefix(match, strlen(match)))) {
299 return AST_DEVICE_UNKNOWN;
300 }
301
303 res = AST_DEVICE_ONHOLD;
304 } else {
306 }
307 ast_channel_unref(chan);
308
309 return res;
310}
311
312static enum ast_device_state devstate_cached(const char *device)
313{
314 struct stasis_message *cached_msg;
315 struct ast_device_state_message *device_state;
317
320 if (!cached_msg) {
321 return AST_DEVICE_UNKNOWN;
322 }
323 device_state = stasis_message_data(cached_msg);
324 state = device_state->state;
325 ao2_cleanup(cached_msg);
326
327 return state;
328}
329
330/*! \brief Check device state through channel specific function or generic function */
331static enum ast_device_state _ast_device_state(const char *device, int check_cache)
332{
333 char *number;
334 const struct ast_channel_tech *chan_tech;
335 enum ast_device_state res;
336 /*! \brief Channel driver that provides device state */
337 char *tech;
338
339 /* If the last known state is cached, just return that */
340 if (check_cache) {
341 res = devstate_cached(device);
342 if (res != AST_DEVICE_UNKNOWN) {
343 return res;
344 }
345 }
346
347 number = ast_strdupa(device);
348 tech = strsep(&number, "/");
349 if (!number) {
350 /*! \brief Another provider of device state */
351 char *provider;
352
353 provider = strsep(&tech, ":");
354 if (!tech) {
355 return AST_DEVICE_INVALID;
356 }
357 /* We have a provider */
358 number = tech;
359
360 ast_debug(3, "Checking if I can find provider for \"%s\" - number: %s\n", provider, number);
362 }
363
364 ast_debug(4, "No provider found, checking channel drivers for %s - %s\n", tech, number);
365
366 chan_tech = ast_get_channel_tech(tech);
367 if (!chan_tech) {
368 return AST_DEVICE_INVALID;
369 }
370
371 /* Does the channel driver support device state notification? */
372 if (!chan_tech->devicestate) {
373 /* No, try the generic function */
374 return ast_parse_device_state(device);
375 }
376
377 res = chan_tech->devicestate(number);
378 if (res == AST_DEVICE_UNKNOWN) {
379 res = ast_parse_device_state(device);
380 }
381
382 return res;
383}
384
385enum ast_device_state ast_device_state(const char *device)
386{
387 /* This function is called from elsewhere in the code to find out the
388 * current state of a device. Check the cache, first. */
389
390 return _ast_device_state(device, 1);
391}
392
393/*! \brief Add device state provider */
394int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback)
395{
396 struct devstate_prov *devcb;
397 struct devstate_prov *devprov;
398
399 if (!callback || !(devprov = ast_calloc(1, sizeof(*devprov))))
400 return -1;
401
402 devprov->callback = callback;
403 ast_copy_string(devprov->label, label, sizeof(devprov->label));
404
406 AST_RWLIST_TRAVERSE(&devstate_provs, devcb, list) {
407 if (!strcasecmp(devcb->label, label)) {
408 ast_log(LOG_WARNING, "Device state provider '%s' already registered\n", label);
409 ast_free(devprov);
411 return -1;
412 }
413 }
414 AST_RWLIST_INSERT_HEAD(&devstate_provs, devprov, list);
416
417 return 0;
418}
419
420/*! \brief Remove device state provider */
422{
423 struct devstate_prov *devcb;
424 int res = -1;
425
428 if (!strcasecmp(devcb->label, label)) {
430 ast_free(devcb);
431 res = 0;
432 break;
433 }
434 }
437
438 return res;
439}
440
441/*! \brief Get provider device state */
442static int getproviderstate(const char *provider, const char *address)
443{
444 struct devstate_prov *devprov;
445 int res = AST_DEVICE_INVALID;
446
448 AST_RWLIST_TRAVERSE(&devstate_provs, devprov, list) {
449 ast_debug(5, "Checking provider %s with %s\n", devprov->label, provider);
450
451 if (!strcasecmp(devprov->label, provider)) {
452 res = devprov->callback(address);
453 break;
454 }
455 }
457
458 return res;
459}
460
461/*! Called by the state change thread to find out what the state is, and then
462 * to queue up the state change event */
463static void do_state_change(const char *device, enum ast_devstate_cache cachable)
464{
466
467 state = _ast_device_state(device, 0);
468
469 ast_debug(3, "Changing state for %s - state %u (%s)\n", device, state, ast_devstate2str(state));
470
471 ast_publish_device_state(device, state, cachable);
472}
473
474int ast_devstate_changed_literal(enum ast_device_state state, enum ast_devstate_cache cachable, const char *device)
475{
476 struct state_change *change;
477
478 /*
479 * If we know the state change (how nice of the caller of this function!)
480 * then we can just generate a device state event.
481 *
482 * Otherwise, we do the following:
483 * - Queue an event up to another thread that the state has changed
484 * - In the processing thread, it calls the callback provided by the
485 * device state provider (which may or may not be a channel driver)
486 * to determine the state.
487 * - If the device state provider does not know the state, or this is
488 * for a channel and the channel driver does not implement a device
489 * state callback, then we will look through the channel list to
490 * see if we can determine a state based on active calls.
491 * - Once a state has been determined, a device state event is generated.
492 */
493
494 if (state != AST_DEVICE_UNKNOWN) {
496 } else if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
497 /* we could not allocate a change struct, or */
498 /* there is no background thread, so process the change now */
500 } else {
501 /* queue the change */
502 strcpy(change->device, device);
503 change->cachable = cachable;
508 }
509
510 return 0;
511}
512
514{
516 va_list ap;
517
518 va_start(ap, fmt);
519 vsnprintf(buf, sizeof(buf), fmt, ap);
520 va_end(ap);
521
523}
524
525/*! \brief Go through the dev state change queue and update changes in the dev state thread */
526static void *do_devstate_changes(void *data)
527{
528 struct state_change *next, *current;
529
530 while (!shuttingdown) {
531 /* This basically pops off any state change entries, resets the list back to NULL, unlocks, and processes each state change */
538
539 /* Process each state change */
540 while ((current = next)) {
542 do_state_change(current->device, current->cachable);
544 }
545 }
546
547 return NULL;
548}
549
551{
552 struct ast_device_state_message *new_device_state;
553 char *pos;
554 size_t stuff_len;
555
557
558 stuff_len = strlen(device) + 1;
559 if (eid) {
560 stuff_len += sizeof(*eid);
561 }
562 new_device_state = ao2_alloc_options(sizeof(*new_device_state) + stuff_len, NULL,
564 if (!new_device_state) {
565 return NULL;
566 }
567
568 if (eid) {
569 /* non-aggregate device state. */
570 new_device_state->stuff[0] = *eid;
571 new_device_state->eid = &new_device_state->stuff[0];
572 pos = (char *) &new_device_state->stuff[1];
573 } else {
574 pos = (char *) &new_device_state->stuff[0];
575 }
576
577 strcpy(pos, device);/* Safe */
578 new_device_state->device = pos;
579
580 new_device_state->state = state;
581 new_device_state->cachable = cachable;
582
583 return new_device_state;
584}
585
586static void devstate_change_cb(void *data, struct stasis_subscription *sub, struct stasis_message *msg)
587{
588 struct ast_device_state_message *device_state;
589
591 return;
592 }
593
594 device_state = stasis_message_data(msg);
595 if (device_state->cachable == AST_DEVSTATE_CACHABLE || !device_state->eid) {
596 /* Ignore cacheable and aggregate messages. */
597 return;
598 }
599
600 /*
601 * Non-cacheable device state aggregates are just the
602 * device state republished as the aggregate.
603 */
604 ast_publish_device_state_full(device_state->device, device_state->state,
605 device_state->cachable, NULL);
606}
607
609{
610 shuttingdown = 1;
614
616 pthread_join(change_thread, NULL);
617 }
618}
619
620/*! \brief Initialize the device state engine in separate thread */
622{
625 ast_log(LOG_ERROR, "Unable to start device state change thread.\n");
626 return -1;
627 }
629
630 return 0;
631}
632
634{
635 memset(agg, 0, sizeof(*agg));
637}
638
640{
641 static enum ast_device_state state_order[] = {
642 1, /* AST_DEVICE_UNKNOWN */
643 3, /* AST_DEVICE_NOT_INUSE */
644 6, /* AST_DEVICE_INUSE */
645 7, /* AST_DEVICE_BUSY */
646 0, /* AST_DEVICE_INVALID */
647 2, /* AST_DEVICE_UNAVAILABLE */
648 5, /* AST_DEVICE_RINGING */
649 8, /* AST_DEVICE_RINGINUSE */
650 4, /* AST_DEVICE_ONHOLD */
651 };
652
653 if (state == AST_DEVICE_RINGING) {
654 agg->ringing = 1;
656 agg->inuse = 1;
657 }
658
659 if (agg->ringing && agg->inuse) {
661 } else if (state_order[state] > state_order[agg->state]) {
662 agg->state = state;
663 }
664}
665
667{
668 return agg->state;
669}
670
672{
674}
675
677{
678 return device_state_cache;
679}
680
682{
684}
685
686struct stasis_topic *ast_device_state_topic(const char *device)
687{
689}
690
691int ast_device_state_clear_cache(const char *device)
692{
693 struct stasis_message *cached_msg;
694 struct stasis_message *msg;
695
698 if (!cached_msg) {
699 /* nothing to clear */
700 return -1;
701 }
702
703 msg = stasis_cache_clear_create(cached_msg);
704 if (msg) {
706 }
707 ao2_cleanup(msg);
708 ao2_cleanup(cached_msg);
709 return 0;
710}
711
713 const char *device,
715 enum ast_devstate_cache cachable,
716 struct ast_eid *eid)
717{
718 RAII_VAR(struct ast_device_state_message *, device_state, NULL, ao2_cleanup);
720 struct stasis_topic *topic;
721
722 ast_assert(!ast_strlen_zero(device));
723
725 return -1;
726 }
727
728 device_state = device_state_alloc(device, state, cachable, eid);
729 if (!device_state) {
730 return -1;
731 }
732
734 eid);
735 if (!message) {
736 return -1;
737 }
738
739 /* When a device state is to be cached it is likely that something
740 * external will either be monitoring it or will want to pull the
741 * information from the cache, so we always publish to the device
742 * specific topic. Cachable updates traditionally come from such things
743 * as a SIP or PJSIP device.
744 * When a device state is not to be cached we only publish to its
745 * specific topic if something has already created the topic. Publishing
746 * to its topic otherwise would create the topic, which may not be
747 * necessary as it could be an ephemeral device. Uncachable updates
748 * traditionally come from such things as Local channels.
749 */
751 topic = ast_device_state_topic(device);
752 } else {
754 }
755
756 if (!topic) {
757 return -1;
758 }
759
760 stasis_publish(topic, message);
761 return 0;
762}
763
764static const char *device_state_get_id(struct stasis_message *message)
765{
766 struct ast_device_state_message *device_state;
767
769 return NULL;
770 }
771
772 device_state = stasis_message_data(message);
773 if (device_state->cachable == AST_DEVSTATE_NOT_CACHABLE) {
774 return NULL;
775 }
776
777 return device_state->device;
778}
779
780/*!
781 * \internal
782 * \brief Callback to publish the aggregate device state cache entry message.
783 * \since 12.2.0
784 *
785 * \param cache_topic Caching topic the aggregate message may be published over.
786 * \param aggregate The aggregate snapshot message to publish.
787 */
788static void device_state_aggregate_publish(struct stasis_topic *cache_topic, struct stasis_message *aggregate)
789{
790 const char *device;
791 struct stasis_topic *device_specific_topic;
792
793 device = device_state_get_id(aggregate);
794 if (!device) {
795 return;
796 }
797 device_specific_topic = ast_device_state_topic(device);
798 if (!device_specific_topic) {
799 return;
800 }
801
802 stasis_publish(device_specific_topic, aggregate);
803}
804
805/*!
806 * \internal
807 * \brief Callback to calculate the aggregate device state cache entry.
808 * \since 12.2.0
809 *
810 * \param entry Cache entry to calculate a new aggregate snapshot.
811 * \param new_snapshot The snapshot that is being updated.
812 *
813 * \note Return a ref bumped pointer from stasis_cache_entry_get_aggregate()
814 * if a new aggregate could not be calculated because of error.
815 *
816 * \return New aggregate-snapshot calculated on success.
817 * Caller has a reference on return.
818 */
819static struct stasis_message *device_state_aggregate_calc(struct stasis_cache_entry *entry, struct stasis_message *new_snapshot)
820{
821 struct stasis_message *aggregate_snapshot;
822 struct stasis_message *snapshot;
823 struct ast_device_state_message *device_state;
824 const char *device = NULL;
825 struct ast_devstate_aggregate aggregate;
826 int idx;
827
829 return NULL;
830 }
831
832 /* Determine the new aggregate device state. */
833 ast_devstate_aggregate_init(&aggregate);
834 snapshot = stasis_cache_entry_get_local(entry);
835 if (snapshot) {
836 device_state = stasis_message_data(snapshot);
837 device = device_state->device;
838 ast_devstate_aggregate_add(&aggregate, device_state->state);
839 }
840 for (idx = 0; ; ++idx) {
841 snapshot = stasis_cache_entry_get_remote(entry, idx);
842 if (!snapshot) {
843 break;
844 }
845
846 device_state = stasis_message_data(snapshot);
847 device = device_state->device;
848 ast_devstate_aggregate_add(&aggregate, device_state->state);
849 }
850
851 if (!device) {
852 /* There are no device states cached. Delete the aggregate. */
853 return NULL;
854 }
855
856 snapshot = stasis_cache_entry_get_aggregate(entry);
857 if (snapshot) {
858 device_state = stasis_message_data(snapshot);
859 if (device_state->state == ast_devstate_aggregate_result(&aggregate)) {
860 /* Aggregate device state did not change. */
861 return ao2_bump(snapshot);
862 }
863 }
864
865 device_state = device_state_alloc(device, ast_devstate_aggregate_result(&aggregate),
867 if (!device_state) {
868 /* Bummer. We have to keep the old aggregate snapshot. */
869 return ao2_bump(snapshot);
870 }
872 device_state, NULL);
873 ao2_cleanup(device_state);
874 if (!aggregate_snapshot) {
875 /* Bummer. We have to keep the old aggregate snapshot. */
876 return ao2_bump(snapshot);
877 }
878
879 return aggregate_snapshot;
880}
881
882static void devstate_cleanup(void)
883{
886
889
892
895
897}
898
900{
902
904 return -1;
905 }
906 device_state_topic_all = stasis_topic_create("devicestate:all");
908 return -1;
909 }
912 return -1;
913 }
916 if (!device_state_cache) {
917 return -1;
918 }
922 return -1;
923 }
926
930 ast_log(LOG_ERROR, "Failed to create subscription creating uncached device state aggregate events.\n");
931 return -1;
932 }
935
936 return 0;
937}
938
940{
941 struct ast_device_state_message *dev_state;
942
943 dev_state = stasis_message_data(msg);
944
945 /* Ignore non-aggregate states */
946 if (dev_state->eid) {
947 return NULL;
948 }
949
950 return ast_manager_event_blob_create(EVENT_FLAG_CALL, "DeviceStateChange",
951 "Device: %s\r\n"
952 "State: %s\r\n",
953 dev_state->device, ast_devstate_str(dev_state->state));
954}
955
956/*! \brief Convert a \ref stasis_message to a \ref ast_event */
958{
959 struct ast_event *event;
960 struct ast_device_state_message *device_state;
961
962 if (!message) {
963 return NULL;
964 }
965
966 device_state = stasis_message_data(message);
967
968 if (device_state->eid) {
973 AST_EVENT_IE_EID, AST_EVENT_IE_PLTYPE_RAW, device_state->eid, sizeof(*device_state->eid),
975 } else {
981 }
982
983 return event;
984}
Prototypes for public functions only of internal interest,.
char * strsep(char **str, const char *delims)
Asterisk main include file. File version handling, generic pbx functions.
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
@ AO2_ALLOC_OPT_LOCK_NOLOCK
Definition: astobj2.h:367
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:404
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
static struct prometheus_metrics_provider provider
Definition: bridges.c:201
enum cc_state state
Definition: ccss.c:399
static int match(struct ast_sockaddr *addr, unsigned short callno, unsigned short dcallno, const struct chan_iax2_pvt *cur, int check_dcallno)
Definition: chan_iax2.c:2387
General Asterisk PBX channel definitions.
struct ast_channel * ast_channel_get_by_name_prefix(const char *name, size_t name_len)
Find a channel by a name prefix.
Definition: channel.c:1461
#define AST_CHANNEL_NAME
Definition: channel.h:173
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:3006
int ast_channel_hold_state(const struct ast_channel *chan)
const struct ast_channel_tech * ast_get_channel_tech(const char *name)
Get a channel technology structure by name.
Definition: channel.c:592
#define AST_MAX_EXTENSION
Definition: channel.h:134
ast_channel_state
ast_channel states
Definition: channelstate.h:35
@ AST_STATE_RING
Definition: channelstate.h:40
@ AST_STATE_DIALING_OFFHOOK
Definition: channelstate.h:44
@ AST_STATE_RINGING
Definition: channelstate.h:41
@ AST_STATE_PRERING
Definition: channelstate.h:45
@ AST_STATE_DOWN
Definition: channelstate.h:36
@ AST_STATE_OFFHOOK
Definition: channelstate.h:38
@ AST_STATE_BUSY
Definition: channelstate.h:43
@ AST_STATE_DIALING
Definition: channelstate.h:39
@ AST_STATE_UP
Definition: channelstate.h:42
@ AST_STATE_RESERVED
Definition: channelstate.h:37
static void devstate_change_cb(void *data, struct stasis_subscription *sub, struct stasis_message *msg)
Definition: devicestate.c:586
static volatile int shuttingdown
Definition: devicestate.c:218
int ast_device_state_engine_init(void)
Initialize the device state engine in separate thread.
Definition: devicestate.c:621
int ast_devstate_prov_del(const char *label)
Remove device state provider.
Definition: devicestate.c:421
static void device_state_engine_cleanup(void)
Definition: devicestate.c:608
void ast_devstate_aggregate_add(struct ast_devstate_aggregate *agg, enum ast_device_state state)
Add a device state to the aggregate device state.
Definition: devicestate.c:639
enum ast_device_state ast_device_state(const char *device)
Asks a channel for device state.
Definition: devicestate.c:385
static enum ast_device_state devstate_cached(const char *device)
Definition: devicestate.c:312
struct stasis_cache * ast_device_state_cache(void)
Backend cache for ast_device_state_topic_cached()
Definition: devicestate.c:676
static const char *const devstatestring[][2]
Device state strings for printing.
Definition: devicestate.c:164
int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt,...)
Tells Asterisk the State for Device is changed.
Definition: devicestate.c:513
void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg)
Initialize aggregate device state.
Definition: devicestate.c:633
struct stasis_topic * ast_device_state_topic_cached(void)
Get the Stasis caching topic for device state messages.
Definition: devicestate.c:681
const char * ast_devstate_str(enum ast_device_state state)
Convert device state to text string that is easier to parse.
Definition: devicestate.c:258
static pthread_t change_thread
The device state change notification thread.
Definition: devicestate.c:214
static void devstate_cleanup(void)
Definition: devicestate.c:882
STASIS_MESSAGE_TYPE_DEFN(ast_device_state_message_type,.to_ami=devstate_to_ami,.to_event=devstate_to_event,)
static void * do_devstate_changes(void *data)
Go through the dev state change queue and update changes in the dev state thread.
Definition: devicestate.c:526
static int getproviderstate(const char *provider, const char *address)
Get provider device state.
Definition: devicestate.c:442
struct stasis_topic * ast_device_state_topic_all(void)
Get the Stasis topic for device state messages.
Definition: devicestate.c:671
enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg)
Get the aggregate device state result.
Definition: devicestate.c:666
static struct stasis_message * device_state_aggregate_calc(struct stasis_cache_entry *entry, struct stasis_message *new_snapshot)
Definition: devicestate.c:819
static struct ast_manager_event_blob * devstate_to_ami(struct stasis_message *msg)
Definition: devicestate.c:939
enum ast_device_state ast_state_chan2dev(enum ast_channel_state chanstate)
Convert channel state to devicestate.
Definition: devicestate.c:245
static struct stasis_caching_topic * device_state_topic_cached
Definition: devicestate.c:224
enum ast_device_state ast_parse_device_state(const char *device)
Find out if device is active in a call or not.
Definition: devicestate.c:290
static ast_cond_t change_pending
Flag for the queue.
Definition: devicestate.c:217
int devstate_init(void)
Initialize the device state core.
Definition: devicestate.c:899
static struct stasis_topic_pool * device_state_topic_pool
Definition: devicestate.c:225
static struct stasis_topic * device_state_topic_all
Definition: devicestate.c:222
static void device_state_aggregate_publish(struct stasis_topic *cache_topic, struct stasis_message *aggregate)
Definition: devicestate.c:788
struct stasis_subscription * devstate_message_sub
Definition: devicestate.c:220
int ast_device_state_clear_cache(const char *device)
Clear the device from the stasis cache.
Definition: devicestate.c:691
int ast_devstate_changed_literal(enum ast_device_state state, enum ast_devstate_cache cachable, const char *device)
Tells Asterisk the State for Device is changed.
Definition: devicestate.c:474
static enum ast_device_state _ast_device_state(const char *device, int check_cache)
Check device state through channel specific function or generic function.
Definition: devicestate.c:331
static void do_state_change(const char *device, enum ast_devstate_cache cachable)
Definition: devicestate.c:463
static struct stasis_cache * device_state_cache
Definition: devicestate.c:223
static const char * device_state_get_id(struct stasis_message *message)
Definition: devicestate.c:764
enum ast_device_state ast_devstate_val(const char *val)
Convert device state from text to integer value.
Definition: devicestate.c:263
struct stasis_topic * ast_device_state_topic(const char *device)
Get the Stasis topic for device state messages for a specific device.
Definition: devicestate.c:686
static struct ast_device_state_message * device_state_alloc(const char *device, enum ast_device_state state, enum ast_devstate_cache cachable, const struct ast_eid *eid)
Definition: devicestate.c:550
int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback)
Add device state provider.
Definition: devicestate.c:394
static struct ast_event * devstate_to_event(struct stasis_message *msg)
Convert a stasis_message to a ast_event.
Definition: devicestate.c:957
const char * ast_devstate2str(enum ast_device_state devstate)
Find devicestate as text message for output.
Definition: devicestate.c:240
int ast_publish_device_state_full(const char *device, enum ast_device_state state, enum ast_devstate_cache cachable, struct ast_eid *eid)
Publish a device state update with EID.
Definition: devicestate.c:712
Device state management.
#define ast_publish_device_state(device, state, cachable)
Publish a device state update.
Definition: devicestate.h:321
struct stasis_message_type * ast_device_state_message_type(void)
Get the Stasis message type for device state messages.
ast_devstate_cache
Device State Cachability.
Definition: devicestate.h:68
@ AST_DEVSTATE_CACHABLE
Definition: devicestate.h:70
@ AST_DEVSTATE_NOT_CACHABLE
Definition: devicestate.h:69
enum ast_device_state(* ast_devstate_prov_cb_type)(const char *data)
Devicestate provider call back.
Definition: devicestate.h:74
ast_device_state
Device States.
Definition: devicestate.h:52
@ AST_DEVICE_RINGINUSE
Definition: devicestate.h:60
@ AST_DEVICE_INUSE
Definition: devicestate.h:55
@ AST_DEVICE_UNKNOWN
Definition: devicestate.h:53
@ AST_DEVICE_ONHOLD
Definition: devicestate.h:61
@ AST_DEVICE_RINGING
Definition: devicestate.h:59
@ AST_DEVICE_INVALID
Definition: devicestate.h:57
@ AST_DEVICE_BUSY
Definition: devicestate.h:56
@ AST_DEVICE_NOT_INUSE
Definition: devicestate.h:54
@ AST_DEVICE_UNAVAILABLE
Definition: devicestate.h:58
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
struct ast_event * ast_event_new(enum ast_event_type event_type,...)
Create a new event.
Definition: event.c:403
@ AST_EVENT_IE_END
Definition: event_defs.h:70
@ AST_EVENT_IE_STATE
Generic State IE Used by AST_EVENT_DEVICE_STATE_CHANGE Payload type: UINT The actual state values dep...
Definition: event_defs.h:121
@ AST_EVENT_IE_DEVICE
Device Name Used by AST_EVENT_DEVICE_STATE_CHANGE Payload type: STR.
Definition: event_defs.h:113
@ AST_EVENT_IE_EID
Entity ID Used by All events Payload type: RAW This IE indicates which server the event originated fr...
Definition: event_defs.h:272
@ AST_EVENT_IE_CACHABLE
Event non-cacheability flag Used by: All events Payload type: UINT.
Definition: event_defs.h:306
@ AST_EVENT_DEVICE_STATE_CHANGE
Definition: event_defs.h:48
@ AST_EVENT_DEVICE_STATE
Definition: event_defs.h:45
@ AST_EVENT_IE_PLTYPE_RAW
Definition: event_defs.h:337
@ AST_EVENT_IE_PLTYPE_UINT
Definition: event_defs.h:333
@ AST_EVENT_IE_PLTYPE_STR
Definition: event_defs.h:335
char * address
Definition: f2c.h:59
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
@ AST_CONTROL_HOLD
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_WARNING
A set of macros to manage forward-linked lists.
#define AST_RWLIST_REMOVE_CURRENT
Definition: linkedlists.h:570
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
Definition: linkedlists.h:681
#define AST_LIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
Definition: linkedlists.h:291
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:545
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized.
Definition: linkedlists.h:333
#define AST_LIST_EMPTY(head)
Checks whether the specified list contains any entries.
Definition: linkedlists.h:450
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:410
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:617
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:40
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:494
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:718
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:140
#define AST_RWLIST_ENTRY
Definition: linkedlists.h:415
#define AST_LIST_FIRST(head)
Returns the first entry contained in a list.
Definition: linkedlists.h:421
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:439
Asterisk locking-related definitions:
#define ast_cond_wait(cond, mutex)
Definition: lock.h:209
#define AST_PTHREADT_NULL
Definition: lock.h:70
#define ast_cond_init(cond, attr)
Definition: lock.h:205
pthread_cond_t ast_cond_t
Definition: lock.h:182
#define ast_cond_signal(cond)
Definition: lock.h:207
size_t current
Definition: main/cli.c:113
struct ast_manager_event_blob * ast_manager_event_blob_create(int event_flags, const char *manager_event, const char *extra_fields_fmt,...)
Construct a ast_manager_event_blob.
Definition: manager.c:10237
#define EVENT_FLAG_CALL
Definition: manager.h:76
Core PBX routines and definitions.
struct stasis_forward * sub
Definition: res_corosync.c:240
static void to_ami(struct ast_sip_subscription *sub, struct ast_str **buf)
#define NULL
Definition: resample.c:96
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
struct stasis_message * stasis_cache_clear_create(struct stasis_message *message)
A message which instructs the caching topic to remove an entry from its cache.
Definition: stasis_cache.c:778
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
struct stasis_topic * stasis_caching_get_topic(struct stasis_caching_topic *caching_topic)
Returns the topic of cached events from a caching topics.
Definition: stasis_cache.c:85
#define STASIS_MESSAGE_TYPE_CLEANUP(name)
Boiler-plate messaging macro for cleaning up message types.
Definition: stasis.h:1515
struct stasis_topic * stasis_topic_pool_get_topic(struct stasis_topic_pool *pool, const char *topic_name)
Find or create a topic in the pool.
Definition: stasis.c:1911
@ STASIS_SUBSCRIPTION_FILTER_SELECTIVE
Definition: stasis.h:297
struct stasis_topic * stasis_topic_create(const char *name)
Create a new topic.
Definition: stasis.c:644
struct stasis_caching_topic * stasis_caching_topic_create(struct stasis_topic *original_topic, struct stasis_cache *cache)
Create a topic which monitors and caches messages from another topic.
Definition: stasis_cache.c:948
int stasis_subscription_accept_message_type(struct stasis_subscription *subscription, const struct stasis_message_type *type)
Indicate to a subscription that we are interested in a message type.
Definition: stasis.c:1050
int stasis_subscription_set_filter(struct stasis_subscription *subscription, enum stasis_subscription_message_filter filter)
Set the message type filtering level on a subscription.
Definition: stasis.c:1104
int stasis_caching_set_filter(struct stasis_caching_topic *caching_topic, enum stasis_subscription_message_filter filter)
Set the message type filtering level on a cache.
Definition: stasis_cache.c:109
struct stasis_message * stasis_cache_entry_get_aggregate(struct stasis_cache_entry *entry)
Get the aggregate cache entry snapshot.
Definition: stasis_cache.c:365
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1493
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
struct stasis_cache * stasis_cache_create_full(snapshot_get_id id_fn, cache_aggregate_calc_fn aggregate_calc_fn, cache_aggregate_publish_fn aggregate_publish_fn)
Create a cache.
Definition: stasis_cache.c:334
struct stasis_subscription * stasis_unsubscribe_and_join(struct stasis_subscription *subscription)
Cancel a subscription, blocking until the last message is processed.
Definition: stasis.c:1161
struct stasis_caching_topic * stasis_caching_unsubscribe_and_join(struct stasis_caching_topic *caching_topic)
Unsubscribes a caching topic from its upstream topic, blocking until all messages have been forwarded...
Definition: stasis_cache.c:146
int stasis_topic_pool_topic_exists(const struct stasis_topic_pool *pool, const char *topic_name)
Check if a topic exists in a pool.
Definition: stasis.c:1954
struct stasis_message * stasis_message_create_full(struct stasis_message_type *type, void *data, const struct ast_eid *eid)
Create a new message for an entity.
struct stasis_topic_pool * stasis_topic_pool_create(struct stasis_topic *pooled_topic)
Create a topic pool that routes messages from dynamically generated topics to the given topic.
Definition: stasis.c:1860
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic's subscribers.
Definition: stasis.c:1538
struct stasis_message * stasis_cache_get_by_eid(struct stasis_cache *cache, struct stasis_message_type *type, const char *id, const struct ast_eid *eid)
Retrieve an item from the cache for a specific entity.
Definition: stasis_cache.c:659
struct stasis_message * stasis_cache_entry_get_local(struct stasis_cache_entry *entry)
Get the local entity's cache entry snapshot.
Definition: stasis_cache.c:370
#define stasis_subscribe(topic, callback, data)
Definition: stasis.h:649
struct stasis_message * stasis_cache_entry_get_remote(struct stasis_cache_entry *entry, int idx)
Get a remote entity's cache entry snapshot by index.
Definition: stasis_cache.c:375
int stasis_caching_accept_message_type(struct stasis_caching_topic *caching_topic, struct stasis_message_type *type)
Indicate to a caching topic that we are interested in a message type.
Definition: stasis_cache.c:90
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Structure to describe a channel "technology", ie a channel driver See for examples:
Definition: channel.h:648
int(*const devicestate)(const char *device_number)
Definition: channel.h:695
Main Channel structure associated with a channel.
The structure that contains device state.
Definition: devicestate.h:238
enum ast_device_state state
Definition: devicestate.h:248
const struct ast_eid * eid
The EID of the server where this message originated.
Definition: devicestate.h:246
struct ast_eid stuff[0]
Definition: devicestate.h:252
enum ast_devstate_cache cachable
Definition: devicestate.h:250
You shouldn't care about the contents of this struct.
Definition: devicestate.h:228
enum ast_device_state state
Definition: devicestate.h:231
unsigned int ringing
Definition: devicestate.h:229
An Entity ID is essentially a MAC address, brief and unique.
Definition: utils.h:813
An event.
Definition: event.c:81
Struct containing info for an AMI event to send out.
Definition: manager.h:503
Mapping for channel states to device states.
Definition: devicestate.c:177
enum ast_device_state dev
Definition: devicestate.c:179
enum ast_channel_state chan
Definition: devicestate.c:178
A device state provider (not a channel)
Definition: devicestate.c:194
ast_devstate_prov_cb_type callback
Definition: devicestate.c:196
char label[40]
Definition: devicestate.c:195
A list of providers.
Definition: devicestate.c:201
Definition: astman.c:222
Number structure.
Definition: app_followme.c:157
Definition: stasis_cache.c:173
struct ast_eid eid
struct state_change::@345 list
struct state_change * next
Definition: devicestate.c:204
enum ast_devstate_cache cachable
Definition: devicestate.c:205
char device[1]
Definition: devicestate.c:206
The state change queue. State changes are queued for processing by a separate thread.
Definition: devicestate.c:211
ast_mutex_t lock
Definition: devicestate.c:211
Definition: ast_expr2.c:325
Utility functions.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
#define ast_assert(a)
Definition: utils.h:739
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:592
#define ARRAY_LEN(a)
Definition: utils.h:666
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93