Asterisk - The Open Source Telephony Project  18.5.0
stasis_app_device_state.h File Reference

Stasis Application Device State API. See StasisApplication API" for detailed documentation. More...

#include "asterisk/app.h"
#include "asterisk/stasis_app.h"
Include dependency graph for stasis_app_device_state.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

enum  stasis_device_state_result {
  STASIS_DEVICE_STATE_OK, STASIS_DEVICE_STATE_NOT_CONTROLLED, STASIS_DEVICE_STATE_MISSING, STASIS_DEVICE_STATE_UNKNOWN,
  STASIS_DEVICE_STATE_SUBSCRIBERS
}
 
struct ast_jsonstasis_app_device_state_to_json (const char *name, enum ast_device_state state)
 Convert device state to json. More...
 
struct ast_jsonstasis_app_device_states_to_json (void)
 Convert device states to json array. More...
 
enum stasis_device_state_result stasis_app_device_state_update (const char *name, const char *value)
 Changes the state of a device controlled by ARI. More...
 
enum stasis_device_state_result stasis_app_device_state_delete (const char *name)
 Delete a device controlled by ARI. More...
 

Detailed Description

Stasis Application Device State API. See StasisApplication API" for detailed documentation.

Author
Kevin Harwell kharw.nosp@m.ell@.nosp@m.digiu.nosp@m.m.co.nosp@m.m
Since
12

Definition in file stasis_app_device_state.h.

Enumeration Type Documentation

◆ stasis_device_state_result

Stasis device state application result codes

Enumerator
STASIS_DEVICE_STATE_OK 

Application controlled device state is okay

STASIS_DEVICE_STATE_NOT_CONTROLLED 

The device name is not application controlled

STASIS_DEVICE_STATE_MISSING 

The application controlled device name is missing

STASIS_DEVICE_STATE_UNKNOWN 

The application controlled device is unknown

STASIS_DEVICE_STATE_SUBSCRIBERS 

The application controlled device has subscribers

Definition at line 56 of file stasis_app_device_state.h.

56  {
57  /*! Application controlled device state is okay */
59  /*! The device name is not application controlled */
61  /*! The application controlled device name is missing */
63  /*! The application controlled device is unknown */
65  /*! The application controlled device has subscribers */
67 };

Function Documentation

◆ stasis_app_device_state_delete()

enum stasis_device_state_result stasis_app_device_state_delete ( const char *  name)

Delete a device controlled by ARI.

Parameters
namethe name of the ARI controlled device

stasis device state application result.

Definition at line 244 of file res_stasis_device_state.c.

References ast_db_del(), ast_device_state_clear_cache(), AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, ast_devstate_changed(), ast_log, ast_strlen_zero, DEVICE_STATE_FAMILY, DEVICE_STATE_SCHEME_STASIS, LOG_ERROR, name, STASIS_DEVICE_STATE_MISSING, STASIS_DEVICE_STATE_NOT_CONTROLLED, STASIS_DEVICE_STATE_OK, and STASIS_DEVICE_STATE_UNKNOWN.

Referenced by ast_ari_device_states_delete().

245 {
246  const char *full_name = name;
247  size_t size = strlen(DEVICE_STATE_SCHEME_STASIS);
248 
249  if (strncasecmp(name, DEVICE_STATE_SCHEME_STASIS, size)) {
250  ast_log(LOG_ERROR, "Can only delete '%s' device states!\n",
253  }
254 
255  name += size;
256  if (ast_strlen_zero(name)) {
257  ast_log(LOG_ERROR, "Delete requires a device name!\n");
259  }
260 
261  if (ast_device_state_clear_cache(full_name)) {
263  }
264 
266 
267  /* send state change for delete */
271 
272  return STASIS_DEVICE_STATE_OK;
273 }
#define DEVICE_STATE_SCHEME_STASIS
int ast_device_state_clear_cache(const char *device)
Clear the device from the stasis cache.
Definition: devicestate.c:688
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
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:510
#define LOG_ERROR
Definition: logger.h:285
static const char name[]
Definition: cdr_mysql.c:74
#define DEVICE_STATE_FAMILY
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: main/db.c:429

◆ stasis_app_device_state_to_json()

struct ast_json* stasis_app_device_state_to_json ( const char *  name,
enum ast_device_state  state 
)

Convert device state to json.

Parameters
namethe name of the device
statethe device state
Returns
JSON representation.
NULL on error.

Definition at line 160 of file res_stasis_device_state.c.

References ast_devstate_str(), and ast_json_pack().

Referenced by ast_ari_device_states_get(), send_device_state(), and stasis_app_device_states_to_json().

162 {
163  return ast_json_pack("{s: s, s: s}",
164  "name", name,
165  "state", ast_devstate_str(state));
166 }
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
const char * ast_devstate_str(enum ast_device_state devstate) attribute_pure
Convert device state to text string that is easier to parse.
Definition: devicestate.c:255
static const char name[]
Definition: cdr_mysql.c:74

◆ stasis_app_device_state_update()

enum stasis_device_state_result stasis_app_device_state_update ( const char *  name,
const char *  value 
)

Changes the state of a device controlled by ARI.

Note
The controlled device must be prefixed with 'Stasis:'.
Implicitly creates the device state.
Parameters
namethe name of the ARI controlled device
valuea valid device state value
Returns
a stasis device state application result.

Definition at line 211 of file res_stasis_device_state.c.

References ast_db_put(), ast_debug, AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, ast_devstate_changed(), ast_devstate_val(), ast_log, ast_strlen_zero, DEVICE_STATE_FAMILY, DEVICE_STATE_SCHEME_STASIS, LOG_ERROR, STASIS_DEVICE_STATE_MISSING, STASIS_DEVICE_STATE_NOT_CONTROLLED, STASIS_DEVICE_STATE_OK, STASIS_DEVICE_STATE_UNKNOWN, and state.

Referenced by ast_ari_device_states_update().

213 {
214  size_t size = strlen(DEVICE_STATE_SCHEME_STASIS);
215  enum ast_device_state state;
216 
217  ast_debug(3, "Updating device name = %s, value = %s", name, value);
218 
219  if (strncasecmp(name, DEVICE_STATE_SCHEME_STASIS, size)) {
220  ast_log(LOG_ERROR, "Update can only be used to set "
221  "'%s' device state!\n", DEVICE_STATE_SCHEME_STASIS);
223  }
224 
225  name += size;
226  if (ast_strlen_zero(name)) {
227  ast_log(LOG_ERROR, "Update requires custom device name!\n");
229  }
230 
231  if (!value || (state = ast_devstate_val(value)) == AST_DEVICE_UNKNOWN) {
232  ast_log(LOG_ERROR, "Unknown device state "
233  "value '%s'\n", value);
235  }
236 
240 
241  return STASIS_DEVICE_STATE_OK;
242 }
enum sip_cc_notify_state state
Definition: chan_sip.c:959
ast_device_state
Device States.
Definition: devicestate.h:52
#define DEVICE_STATE_SCHEME_STASIS
int value
Definition: syslog.c:37
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
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:510
enum ast_device_state ast_devstate_val(const char *val)
Convert device state from text to integer value.
Definition: devicestate.c:260
#define LOG_ERROR
Definition: logger.h:285
static const char name[]
Definition: cdr_mysql.c:74
#define DEVICE_STATE_FAMILY
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:327

◆ stasis_app_device_states_to_json()

struct ast_json* stasis_app_device_states_to_json ( void  )

Convert device states to json array.

Returns
JSON representation.
NULL on error.

Definition at line 168 of file res_stasis_device_state.c.

References array(), ast_db_freetree(), ast_db_gettree(), ast_json_array_append(), ast_json_array_create(), ast_strlen_zero, DEVICE_STATE_FAMILY, DEVICE_STATE_SCHEME_STASIS, DEVICE_STATE_SIZE, ast_db_entry::key, name, ast_db_entry::next, NULL, and stasis_app_device_state_to_json().

Referenced by ast_ari_device_states_list().

169 {
171  struct ast_db_entry *tree;
172  struct ast_db_entry *entry;
173 
175  for (entry = tree; entry; entry = entry->next) {
176  const char *name = strrchr(entry->key, '/');
177 
178  if (!ast_strlen_zero(name)) {
179  char device[DEVICE_STATE_SIZE];
180 
181  snprintf(device, sizeof(device), "%s%s", DEVICE_STATE_SCHEME_STASIS, ++name);
182  ast_json_array_append(array,
184  }
185  }
186  ast_db_freetree(tree);
187 
188  return array;
189 }
ast_device_state
Device States.
Definition: devicestate.h:52
#define DEVICE_STATE_SCHEME_STASIS
#define DEVICE_STATE_SIZE
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
Definition: main/db.c:598
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
struct ast_db_entry * next
Definition: astdb.h:32
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:352
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:368
struct ast_db_entry * ast_db_gettree(const char *family, const char *keytree)
Get a list of values within the astdb tree.
Definition: main/db.c:531
static int array(struct ast_channel *chan, const char *cmd, char *var, const char *value)
Definition: astdb.h:31
static const char name[]
Definition: cdr_mysql.c:74
struct ast_json * stasis_app_device_state_to_json(const char *name, enum ast_device_state state)
Convert device state to json.
#define DEVICE_STATE_FAMILY
Abstract JSON element (object, array, string, int, ...).
Definition: search.h:40
char * key
Definition: astdb.h:33