Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Macros | Typedefs | Enumerations | Functions
devicestate.h File Reference

Device state management. More...

#include "asterisk/channelstate.h"
#include "asterisk/utils.h"

Go to the source code of this file.

Data Structures

struct  ast_device_state_message
 The structure that contains device state. More...
 
struct  ast_devstate_aggregate
 You shouldn't care about the contents of this struct. More...
 

Macros

#define ast_publish_device_state(device, state, cachable)   ast_publish_device_state_full(device, state, cachable, &ast_eid_default)
 Publish a device state update. More...
 

Typedefs

typedef enum ast_device_state(* ast_devstate_prov_cb_type) (const char *data)
 Devicestate provider call back.
 

Enumerations

enum  ast_device_state {
  AST_DEVICE_UNKNOWN, AST_DEVICE_NOT_INUSE, AST_DEVICE_INUSE, AST_DEVICE_BUSY,
  AST_DEVICE_INVALID, AST_DEVICE_UNAVAILABLE, AST_DEVICE_RINGING, AST_DEVICE_RINGINUSE,
  AST_DEVICE_ONHOLD, AST_DEVICE_TOTAL
}
 Device States. More...
 
enum  ast_devstate_cache { AST_DEVSTATE_NOT_CACHABLE, AST_DEVSTATE_CACHABLE }
 Device State Cachability. More...
 

Functions

enum ast_device_state ast_device_state (const char *device)
 Asks a channel for device state. More...
 
struct stasis_cacheast_device_state_cache (void)
 Backend cache for ast_device_state_topic_cached() More...
 
int ast_device_state_clear_cache (const char *device)
 Clear the device from the stasis cache. More...
 
struct stasis_message_typeast_device_state_message_type (void)
 Get the Stasis message type for device state messages. More...
 
struct stasis_topicast_device_state_topic (const char *device)
 Get the Stasis topic for device state messages for a specific device. More...
 
struct stasis_topicast_device_state_topic_all (void)
 Get the Stasis topic for device state messages. More...
 
struct stasis_topicast_device_state_topic_cached (void)
 Get the Stasis caching topic for device state messages. More...
 
const char * ast_devstate2str (enum ast_device_state devstate) attribute_pure
 Convert device state to text string for output. More...
 
void ast_devstate_aggregate_add (struct ast_devstate_aggregate *agg, enum ast_device_state state)
 Add a device state to the aggregate device state. More...
 
void ast_devstate_aggregate_init (struct ast_devstate_aggregate *agg)
 Initialize aggregate device state. More...
 
enum ast_device_state ast_devstate_aggregate_result (struct ast_devstate_aggregate *agg)
 Get the aggregate device state result. More...
 
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. More...
 
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. More...
 
int ast_devstate_prov_add (const char *label, ast_devstate_prov_cb_type callback)
 Add device state provider. More...
 
int ast_devstate_prov_del (const char *label)
 Remove device state provider. More...
 
const char * ast_devstate_str (enum ast_device_state devstate) attribute_pure
 Convert device state to text string that is easier to parse. More...
 
enum ast_device_state ast_devstate_val (const char *val)
 Convert device state from text to integer value. More...
 
enum ast_device_state ast_parse_device_state (const char *device)
 Search the Channels by Name. More...
 
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. More...
 
enum ast_device_state ast_state_chan2dev (enum ast_channel_state chanstate)
 Convert channel state to devicestate. More...
 
int devstate_init (void)
 Initialize the device state core. More...
 

Detailed Description

Device state management.

To subscribe to device state changes, use the stasis_subscribe method. For an example, see apps/app_queue.c.

Todo:
Currently, when the state of a device changes, the device state provider calls one of the functions defined here to queue an object to say that the state of a device has changed. However, this does not include the new state. Another thread processes these device state change objects and calls the device state provider's callback to figure out what the new state is. It would make a lot more sense for the new state to be included in the original function call that says the state of a device has changed. However, it will take a lot of work to change this.

Definition in file devicestate.h.

Macro Definition Documentation

#define ast_publish_device_state (   device,
  state,
  cachable 
)    ast_publish_device_state_full(device, state, cachable, &ast_eid_default)

Publish a device state update.

Parameters
[in]deviceThe device name
[in]stateThe state of the device
[in]cachableWhether the device state can be cached
Return values
0Success
-1Failure
Since
12

Definition at line 321 of file devicestate.h.

Referenced by ast_devstate_changed_literal(), and do_state_change().

Enumeration Type Documentation

Device States.

Note
The order of these states may not change because they are included in Asterisk events which may be transmitted across the network to other servers.
Enumerator
AST_DEVICE_UNKNOWN 

Device is valid but channel didn't know state

AST_DEVICE_NOT_INUSE 

Device is not used

AST_DEVICE_INUSE 

Device is in use

AST_DEVICE_BUSY 

Device is busy

AST_DEVICE_INVALID 

Device is invalid

AST_DEVICE_UNAVAILABLE 

Device is unavailable

AST_DEVICE_RINGING 

Device is ringing

AST_DEVICE_RINGINUSE 

Device is ringing and in use

AST_DEVICE_ONHOLD 

Device is on hold

AST_DEVICE_TOTAL 

Total num of device states, used for testing

Definition at line 52 of file devicestate.h.

52  {
53  AST_DEVICE_UNKNOWN, /*!< Device is valid but channel didn't know state */
54  AST_DEVICE_NOT_INUSE, /*!< Device is not used */
55  AST_DEVICE_INUSE, /*!< Device is in use */
56  AST_DEVICE_BUSY, /*!< Device is busy */
57  AST_DEVICE_INVALID, /*!< Device is invalid */
58  AST_DEVICE_UNAVAILABLE, /*!< Device is unavailable */
59  AST_DEVICE_RINGING, /*!< Device is ringing */
60  AST_DEVICE_RINGINUSE, /*!< Device is ringing *and* in use */
61  AST_DEVICE_ONHOLD, /*!< Device is on hold */
62  AST_DEVICE_TOTAL, /*!< Total num of device states, used for testing */
63 };

Device State Cachability.

Note
This is used to define the cacheability of a device state when set.
Enumerator
AST_DEVSTATE_NOT_CACHABLE 

This device state is not cachable

AST_DEVSTATE_CACHABLE 

This device state is cachable

Definition at line 68 of file devicestate.h.

68  {
69  AST_DEVSTATE_NOT_CACHABLE, /*!< This device state is not cachable */
70  AST_DEVSTATE_CACHABLE, /*!< This device state is cachable */
71 };

Function Documentation

enum ast_device_state ast_device_state ( const char *  device)

Asks a channel for device state.

Parameters
devicelike a dial string

Asks a channel for device state, data is normally a number from a dial string used by the low level module Tries the channel device state callback if not supported search in the active channels list for the device.

Returns
an AST_DEVICE_??? state

Definition at line 382 of file devicestate.c.

References _ast_device_state().

383 {
384  /* This function is called from elsewhere in the code to find out the
385  * current state of a device. Check the cache, first. */
386 
387  return _ast_device_state(device, 1);
388 }
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:328
struct stasis_cache* ast_device_state_cache ( void  )

Backend cache for ast_device_state_topic_cached()

Returns
Cache of ast_device_state_message.
Since
12

Definition at line 673 of file devicestate.c.

Referenced by ast_device_state_clear_cache(), and xmpp_init_event_distribution().

674 {
675  return device_state_cache;
676 }
int ast_device_state_clear_cache ( const char *  device)

Clear the device from the stasis cache.

Parameters
deviceThe device to clear
Return values
0if successful
-1nothing to clear
Since
12

Definition at line 688 of file devicestate.c.

References ast_device_state_cache(), ast_device_state_message_type(), ast_device_state_topic(), ast_eid_default, stasis_cache_clear_create(), stasis_cache_get_by_eid(), and stasis_publish().

Referenced by stasis_app_device_state_delete().

689 {
690  struct stasis_message *cached_msg;
691  struct stasis_message *msg;
692 
695  if (!cached_msg) {
696  /* nothing to clear */
697  return -1;
698  }
699 
700  msg = stasis_cache_clear_create(cached_msg);
701  if (msg) {
703  }
704  ao2_cleanup(msg);
705  ao2_cleanup(cached_msg);
706  return 0;
707 }
struct stasis_cache * ast_device_state_cache(void)
Backend cache for ast_device_state_topic_cached()
Definition: devicestate.c:673
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 * 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_type * ast_device_state_message_type(void)
Get the Stasis message type for device state messages.
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic's subscribers.
Definition: stasis.c:1511
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93
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:683
struct stasis_message_type* ast_device_state_message_type ( void  )

Get the Stasis message type for device state messages.

Returns
The message type for device state messages
Return values
NULLif it has not been allocated
Since
12

Referenced by ast_device_state_clear_cache(), ast_publish_device_state_full(), device_state_cb(), devstate_init(), load_module(), load_pbx(), xmpp_init_event_distribution(), and xmpp_pubsub_devstate_cb().

struct stasis_topic* ast_device_state_topic ( const char *  device)

Get the Stasis topic for device state messages for a specific device.

Parameters
deviceThe device for which to get the topic
Returns
The topic structure for MWI messages for a given device
Return values
NULLif it failed to be found or allocated
Since
12

Definition at line 683 of file devicestate.c.

References stasis_topic_pool_get_topic().

Referenced by ast_device_state_clear_cache(), and ast_publish_device_state_full().

684 {
685  return stasis_topic_pool_get_topic(device_state_topic_pool, device);
686 }
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:1884
struct stasis_topic* ast_device_state_topic_all ( void  )

Get the Stasis topic for device state messages.

Returns
The topic for device state messages
Return values
NULLif it has not been allocated
Since
12

Definition at line 668 of file devicestate.c.

Referenced by ast_publish_device_state_full(), devstate_init(), load_module(), load_pbx(), publish_hint_change(), publish_hint_remove(), and xmpp_init_event_distribution().

669 {
670  return device_state_topic_all;
671 }
struct stasis_topic* ast_device_state_topic_cached ( void  )

Get the Stasis caching topic for device state messages.

Returns
The caching topic for device state messages
Return values
NULLif it has not been allocated
Since
12

Definition at line 678 of file devicestate.c.

References stasis_caching_get_topic().

679 {
680  return stasis_caching_get_topic(device_state_topic_cached);
681 }
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
const char* ast_devstate2str ( enum ast_device_state  devstate)

Convert device state to text string for output.

Parameters
devstateCurrent device state

Convert device state to text string for output.

Definition at line 237 of file devicestate.c.

References devstatestring.

Referenced by device_state_cb(), do_state_change(), parking_notify_metermaids(), and print_queue().

238 {
239  return devstatestring[devstate][0];
240 }
static const char *const devstatestring[][2]
Device state strings for printing.
Definition: devicestate.c:161
void ast_devstate_aggregate_add ( struct ast_devstate_aggregate agg,
enum ast_device_state  state 
)

Add a device state to the aggregate device state.

Parameters
[in]aggthe state object
[in]statethe state to add
Since
1.6.1

Definition at line 636 of file devicestate.c.

References AST_DEVICE_BUSY, AST_DEVICE_INUSE, AST_DEVICE_ONHOLD, AST_DEVICE_RINGING, AST_DEVICE_RINGINUSE, and ast_device_state_message::state.

Referenced by chan_pjsip_devicestate().

637 {
638  static enum ast_device_state state_order[] = {
639  1, /* AST_DEVICE_UNKNOWN */
640  3, /* AST_DEVICE_NOT_INUSE */
641  6, /* AST_DEVICE_INUSE */
642  7, /* AST_DEVICE_BUSY */
643  0, /* AST_DEVICE_INVALID */
644  2, /* AST_DEVICE_UNAVAILABLE */
645  5, /* AST_DEVICE_RINGING */
646  8, /* AST_DEVICE_RINGINUSE */
647  4, /* AST_DEVICE_ONHOLD */
648  };
649 
650  if (state == AST_DEVICE_RINGING) {
651  agg->ringing = 1;
653  agg->inuse = 1;
654  }
655 
656  if (agg->ringing && agg->inuse) {
657  agg->state = AST_DEVICE_RINGINUSE;
658  } else if (state_order[state] > state_order[agg->state]) {
659  agg->state = state;
660  }
661 }
ast_device_state
Device States.
Definition: devicestate.h:52
void ast_devstate_aggregate_init ( struct ast_devstate_aggregate agg)

Initialize aggregate device state.

Parameters
[in]aggthe state object
Since
1.6.1

Definition at line 630 of file devicestate.c.

References AST_DEVICE_INVALID.

Referenced by chan_pjsip_devicestate().

631 {
632  memset(agg, 0, sizeof(*agg));
633  agg->state = AST_DEVICE_INVALID;
634 }
enum ast_device_state ast_devstate_aggregate_result ( struct ast_devstate_aggregate agg)

Get the aggregate device state result.

Parameters
[in]aggthe state object
Returns
the aggregate device state after adding some number of device states.
Since
1.6.1

Definition at line 663 of file devicestate.c.

Referenced by chan_pjsip_devicestate().

664 {
665  return agg->state;
666 }
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.

Parameters
statethe new state of the device
cachablewhether this device state is cachable
fmtdevice name like a dial string with format parameters

The new state of the device will be sent off to any subscribers of device states. It will also be stored in the internal event cache.

Return values
0on success
-1on failure

Definition at line 510 of file devicestate.c.

References ast_devstate_changed_literal(), and AST_MAX_EXTENSION.

Referenced by add_to_queue(), chan_pjsip_indicate(), conf_handle_first_join(), device_state_cb(), init_queue(), leave_queue(), parking_notify_metermaids(), register_verify(), remove_from_queue(), rt_handle_member_record(), stasis_app_device_state_delete(), stasis_app_device_state_update(), update_registry(), and xmpp_pak_presence().

511 {
512  char buf[AST_MAX_EXTENSION];
513  va_list ap;
514 
515  va_start(ap, fmt);
516  vsnprintf(buf, sizeof(buf), fmt, ap);
517  va_end(ap);
518 
519  return ast_devstate_changed_literal(state, cachable, buf);
520 }
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:471
#define AST_MAX_EXTENSION
Definition: channel.h:134
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.

Parameters
statethe new state of the device
cachablewhether this device state is cachable
devicedevice name like a dial string with format parameters

The new state of the device will be sent off to any subscribers of device states. It will also be stored in the internal event cache.

Return values
0on success
-1on failure

Definition at line 471 of file devicestate.c.

References ast_calloc, AST_DEVICE_UNKNOWN, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_publish_device_state, change_pending, change_thread, and do_state_change().

Referenced by ast_channel_destructor(), ast_devstate_changed(), ast_setstate(), and chan_pjsip_indicate().

472 {
473  struct state_change *change;
474 
475  /*
476  * If we know the state change (how nice of the caller of this function!)
477  * then we can just generate a device state event.
478  *
479  * Otherwise, we do the following:
480  * - Queue an event up to another thread that the state has changed
481  * - In the processing thread, it calls the callback provided by the
482  * device state provider (which may or may not be a channel driver)
483  * to determine the state.
484  * - If the device state provider does not know the state, or this is
485  * for a channel and the channel driver does not implement a device
486  * state callback, then we will look through the channel list to
487  * see if we can determine a state based on active calls.
488  * - Once a state has been determined, a device state event is generated.
489  */
490 
491  if (state != AST_DEVICE_UNKNOWN) {
492  ast_publish_device_state(device, state, cachable);
493  } else if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
494  /* we could not allocate a change struct, or */
495  /* there is no background thread, so process the change now */
496  do_state_change(device, cachable);
497  } else {
498  /* queue the change */
499  strcpy(change->device, device);
500  change->cachable = cachable;
502  AST_LIST_INSERT_TAIL(&state_changes, change, list);
503  ast_cond_signal(&change_pending);
505  }
506 
507  return 0;
508 }
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:40
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:140
static void do_state_change(const char *device, enum ast_devstate_cache cachable)
Definition: devicestate.c:460
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
static pthread_t change_thread
The device state change notification thread.
Definition: devicestate.c:211
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
static ast_cond_t change_pending
Flag for the queue.
Definition: devicestate.c:214
The state change queue. State changes are queued for processing by a separate thread.
Definition: devicestate.c:208
#define ast_publish_device_state(device, state, cachable)
Publish a device state update.
Definition: devicestate.h:321
int ast_devstate_prov_add ( const char *  label,
ast_devstate_prov_cb_type  callback 
)

Add device state provider.

Parameters
labelto use in hint, like label:object
callbackCallback
Return values
0success
-1failure

Definition at line 391 of file devicestate.c.

References ast_calloc, ast_copy_string(), AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

Referenced by load_module(), and load_parking_devstate().

392 {
393  struct devstate_prov *devcb;
394  struct devstate_prov *devprov;
395 
396  if (!callback || !(devprov = ast_calloc(1, sizeof(*devprov))))
397  return -1;
398 
399  devprov->callback = callback;
400  ast_copy_string(devprov->label, label, sizeof(devprov->label));
401 
403  AST_RWLIST_TRAVERSE(&devstate_provs, devcb, list) {
404  if (!strcasecmp(devcb->label, label)) {
405  ast_log(LOG_WARNING, "Device state provider '%s' already registered\n", label);
406  ast_free(devprov);
408  return -1;
409  }
410  }
411  AST_RWLIST_INSERT_HEAD(&devstate_provs, devprov, list);
413 
414  return 0;
415 }
A list of providers.
Definition: devicestate.c:198
A device state provider (not a channel)
Definition: devicestate.c:191
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
int ast_devstate_prov_del ( const char *  label)

Remove device state provider.

Parameters
labelto use in hint, like label:object
Return values
-1on failure
0on success

Definition at line 418 of file devicestate.c.

References AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

Referenced by unload_parking_devstate().

419 {
420  struct devstate_prov *devcb;
421  int res = -1;
422 
424  AST_RWLIST_TRAVERSE_SAFE_BEGIN(&devstate_provs, devcb, list) {
425  if (!strcasecmp(devcb->label, label)) {
426  AST_RWLIST_REMOVE_CURRENT(list);
427  ast_free(devcb);
428  res = 0;
429  break;
430  }
431  }
432  AST_RWLIST_TRAVERSE_SAFE_END;
434 
435  return res;
436 }
A list of providers.
Definition: devicestate.c:198
A device state provider (not a channel)
Definition: devicestate.c:191
#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
const char* ast_devstate_str ( enum ast_device_state  devstate)

Convert device state to text string that is easier to parse.

Parameters
devstateCurrent device state

Definition at line 255 of file devicestate.c.

References devstatestring.

Referenced by stasis_app_device_state_to_json(), and xmpp_pubsub_devstate_cb().

256 {
257  return devstatestring[state][1];
258 }
static const char *const devstatestring[][2]
Device state strings for printing.
Definition: devicestate.c:161
enum ast_device_state ast_devstate_val ( const char *  val)

Convert device state from text to integer value.

Parameters
valThe text representing the device state. Valid values are anything that comes after AST_DEVICE_ in one of the defined values.
Returns
The AST_DEVICE_ integer value

Definition at line 260 of file devicestate.c.

References AST_DEVICE_BUSY, AST_DEVICE_INUSE, AST_DEVICE_INVALID, AST_DEVICE_NOT_INUSE, AST_DEVICE_ONHOLD, AST_DEVICE_RINGING, AST_DEVICE_RINGINUSE, AST_DEVICE_UNAVAILABLE, and AST_DEVICE_UNKNOWN.

Referenced by stasis_app_device_state_update(), and xmpp_pubsub_handle_event().

261 {
262  if (!strcasecmp(val, "NOT_INUSE"))
263  return AST_DEVICE_NOT_INUSE;
264  else if (!strcasecmp(val, "INUSE"))
265  return AST_DEVICE_INUSE;
266  else if (!strcasecmp(val, "BUSY"))
267  return AST_DEVICE_BUSY;
268  else if (!strcasecmp(val, "INVALID"))
269  return AST_DEVICE_INVALID;
270  else if (!strcasecmp(val, "UNAVAILABLE"))
271  return AST_DEVICE_UNAVAILABLE;
272  else if (!strcasecmp(val, "RINGING"))
273  return AST_DEVICE_RINGING;
274  else if (!strcasecmp(val, "RINGINUSE"))
275  return AST_DEVICE_RINGINUSE;
276  else if (!strcasecmp(val, "ONHOLD"))
277  return AST_DEVICE_ONHOLD;
278 
279  return AST_DEVICE_UNKNOWN;
280 }
enum ast_device_state ast_parse_device_state ( const char *  device)

Search the Channels by Name.

Parameters
devicelike a dial string

Search the Device in active channels by compare the channel name against the device name. Compared are only the first chars to the first '-' char.

Return values
AST_DEVICE_UNKNOWNif no channel found
AST_DEVICE_INUSEif a channel is found

Search the Channels by Name.

Note
find channels with the device's name in it This function is only used for channels that does not implement devicestate natively

Definition at line 287 of file devicestate.c.

References ast_channel_get_by_name_prefix(), AST_CHANNEL_NAME, ast_channel_unref, AST_CONTROL_HOLD, AST_DEVICE_ONHOLD, AST_DEVICE_UNKNOWN, and ast_state_chan2dev().

Referenced by _ast_device_state().

288 {
289  struct ast_channel *chan;
290  char match[AST_CHANNEL_NAME];
291  enum ast_device_state res;
292 
293  snprintf(match, sizeof(match), "%s-", device);
294 
295  if (!(chan = ast_channel_get_by_name_prefix(match, strlen(match)))) {
296  return AST_DEVICE_UNKNOWN;
297  }
298 
299  if (ast_channel_hold_state(chan) == AST_CONTROL_HOLD) {
300  res = AST_DEVICE_ONHOLD;
301  } else {
303  }
304  ast_channel_unref(chan);
305 
306  return res;
307 }
Main Channel structure associated with a channel.
ast_device_state
Device States.
Definition: devicestate.h:52
enum ast_device_state ast_state_chan2dev(enum ast_channel_state chanstate)
Convert channel state to devicestate.
Definition: devicestate.c:242
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2958
ast_channel_state
ast_channel states
Definition: channelstate.h:35
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:1434
#define AST_CHANNEL_NAME
Definition: channel.h:171
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.

Parameters
[in]deviceThe device name
[in]stateThe state of the device
[in]cachableWhether the device state can be cached
[in]eidThe EID of the server that originally published the message
Return values
0Success
-1Failure
Since
12

Definition at line 709 of file devicestate.c.

References ast_device_state_message_type(), ast_device_state_topic(), ast_device_state_topic_all(), RAII_VAR, stasis_message_create_full(), stasis_publish(), and stasis_topic_pool_topic_exists().

Referenced by publish_device_state_to_stasis(), and xmpp_pubsub_handle_event().

714 {
715  RAII_VAR(struct ast_device_state_message *, device_state, NULL, ao2_cleanup);
716  RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
717  struct stasis_topic *topic;
718 
719  ast_assert(!ast_strlen_zero(device));
720 
722  return -1;
723  }
724 
725  device_state = device_state_alloc(device, state, cachable, eid);
726  if (!device_state) {
727  return -1;
728  }
729 
731  eid);
732  if (!message) {
733  return -1;
734  }
735 
736  /* When a device state is to be cached it is likely that something
737  * external will either be monitoring it or will want to pull the
738  * information from the cache, so we always publish to the device
739  * specific topic. Cachable updates traditionally come from such things
740  * as a SIP or PJSIP device.
741  * When a device state is not to be cached we only publish to its
742  * specific topic if something has already created the topic. Publishing
743  * to its topic otherwise would create the topic, which may not be
744  * necessary as it could be an ephemeral device. Uncachable updates
745  * traditionally come from such things as Local channels.
746  */
747  if (cachable || stasis_topic_pool_topic_exists(device_state_topic_pool, device)) {
748  topic = ast_device_state_topic(device);
749  } else {
750  topic = ast_device_state_topic_all();
751  }
752 
753  if (!topic) {
754  return -1;
755  }
756 
757  stasis_publish(topic, message);
758  return 0;
759 }
struct stasis_message_type * ast_device_state_message_type(void)
Get the Stasis message type for device state messages.
struct stasis_topic * ast_device_state_topic_all(void)
Get the Stasis topic for device state messages.
Definition: devicestate.c:668
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:1927
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic's subscribers.
Definition: stasis.c:1511
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:683
The structure that contains device state.
Definition: devicestate.h:238
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.
#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
enum ast_device_state ast_state_chan2dev ( enum ast_channel_state  chanstate)

Convert channel state to devicestate.

Parameters
chanstateCurrent channel state
Since
1.6.1

Definition at line 242 of file devicestate.c.

References AST_DEVICE_UNKNOWN.

Referenced by ast_parse_device_state(), and chan_pjsip_devicestate().

243 {
244  int i;
245  chanstate &= 0xFFFF;
246  for (i = 0; i < ARRAY_LEN(chan2dev); i++) {
247  if (chan2dev[i].chan == chanstate) {
248  return chan2dev[i].dev;
249  }
250  }
251  return AST_DEVICE_UNKNOWN;
252 }
Mapping for channel states to device states.
Definition: devicestate.c:174
int devstate_init ( void  )

Initialize the device state core.

Return values
0Success
-1Failure
Since
12

Definition at line 896 of file devicestate.c.

References ast_device_state_message_type(), ast_device_state_topic_all(), ast_register_cleanup(), stasis_cache_create_full(), stasis_caching_accept_message_type(), stasis_caching_set_filter(), stasis_caching_topic_create(), STASIS_MESSAGE_TYPE_INIT, stasis_subscription_accept_message_type(), STASIS_SUBSCRIPTION_FILTER_SELECTIVE, stasis_subscription_set_filter(), stasis_topic_create(), and stasis_topic_pool_create().

897 {
898  ast_register_cleanup(devstate_cleanup);
899 
901  return -1;
902  }
903  device_state_topic_all = stasis_topic_create("devicestate:all");
904  if (!device_state_topic_all) {
905  return -1;
906  }
907  device_state_topic_pool = stasis_topic_pool_create(ast_device_state_topic_all());
908  if (!device_state_topic_pool) {
909  return -1;
910  }
911  device_state_cache = stasis_cache_create_full(device_state_get_id,
912  device_state_aggregate_calc, device_state_aggregate_publish);
913  if (!device_state_cache) {
914  return -1;
915  }
916  device_state_topic_cached = stasis_caching_topic_create(ast_device_state_topic_all(),
917  device_state_cache);
918  if (!device_state_topic_cached) {
919  return -1;
920  }
923 
924  devstate_message_sub = stasis_subscribe(ast_device_state_topic_all(),
925  devstate_change_cb, NULL);
926  if (!devstate_message_sub) {
927  ast_log(LOG_ERROR, "Failed to create subscription creating uncached device state aggregate events.\n");
928  return -1;
929  }
932 
933  return 0;
934 }
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
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1493
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_set_filter(struct stasis_subscription *subscription, enum stasis_subscription_message_filter filter)
Set the message type filtering level on a subscription.
Definition: stasis.c:1077
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:1833
struct stasis_message_type * ast_device_state_message_type(void)
Get the Stasis message type for device state messages.
struct stasis_topic * ast_device_state_topic_all(void)
Get the Stasis topic for device state messages.
Definition: devicestate.c:668
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
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_topic * stasis_topic_create(const char *name)
Create a new topic.
Definition: stasis.c:617
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
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:1023