Asterisk - The Open Source Telephony Project  21.4.1
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"

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

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
Returns
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(), 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",
251  DEVICE_STATE_SCHEME_STASIS);
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 
265  ast_db_del(DEVICE_STATE_FAMILY, name);
266 
267  /* send state change for delete */
270  DEVICE_STATE_SCHEME_STASIS, name);
271 
272  return STASIS_DEVICE_STATE_OK;
273 }
int ast_device_state_clear_cache(const char *device)
Clear the device from the stasis cache.
Definition: devicestate.c:688
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
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: main/db.c:476
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.
Return values
NULLon 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(), 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:612
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
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(), STASIS_DEVICE_STATE_MISSING, STASIS_DEVICE_STATE_NOT_CONTROLLED, STASIS_DEVICE_STATE_OK, and STASIS_DEVICE_STATE_UNKNOWN.

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 
237  ast_db_put(DEVICE_STATE_FAMILY, name, value);
239  DEVICE_STATE_SCHEME_STASIS, name);
240 
241  return STASIS_DEVICE_STATE_OK;
242 }
ast_device_state
Device States.
Definition: devicestate.h:52
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 ast_debug(level,...)
Log a DEBUG message.
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:342
struct ast_json* stasis_app_device_states_to_json ( void  )

Convert device states to json array.

Returns
JSON representation.
Return values
NULLon error.

Definition at line 168 of file res_stasis_device_state.c.

References ast_db_freetree(), ast_db_gettree(), ast_json_array_append(), ast_json_array_create(), and stasis_app_device_state_to_json().

Referenced by ast_ari_device_states_list().

169 {
170  struct ast_json *array = ast_json_array_create();
171  struct ast_db_entry *tree;
172  struct ast_db_entry *entry;
173 
174  tree = ast_db_gettree(DEVICE_STATE_FAMILY, NULL);
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
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
Definition: main/db.c:677
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:362
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:378
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:610
Definition: astdb.h:31
Abstract JSON element (object, array, string, int, ...).
Definition: search.h:40
struct ast_json * stasis_app_device_state_to_json(const char *name, enum ast_device_state state)
Convert device state to json.