Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Functions | Variables
presencestate.c File Reference

Presence state management. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/linkedlists.h"
#include "asterisk/presencestate.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/test.h"

Go to the source code of this file.

Data Structures

struct  presence_state_provider
 A presence state provider. More...
 
struct  presence_state_providers
 A list of providers. More...
 

Functions

enum ast_presence_state ast_presence_state (const char *presence_provider, char **subtype, char **message)
 Asks a presence state provider for the current presence state. More...
 
const char * ast_presence_state2str (enum ast_presence_state state)
 Convert presence state to text string for output. More...
 
struct stasis_cacheast_presence_state_cache (void)
 Backend cache for ast_presence_state_topic_cached() More...
 
int ast_presence_state_changed (enum ast_presence_state state, const char *subtype, const char *message, const char *fmt,...)
 Notify the world that a presence provider state changed. More...
 
int ast_presence_state_changed_literal (enum ast_presence_state state, const char *subtype, const char *message, const char *presence_provider)
 Notify the world that a presence provider state changed. More...
 
int ast_presence_state_engine_init (void)
 
static enum ast_presence_state ast_presence_state_helper (const char *presence_provider, char **subtype, char **message, int check_cache)
 
enum ast_presence_state ast_presence_state_nocache (const char *presence_provider, char **subtype, char **message)
 Asks a presence state provider for the current presence state, bypassing the event cache. More...
 
int ast_presence_state_prov_add (const char *label, ast_presence_state_prov_cb_type callback)
 Add presence state provider. More...
 
int ast_presence_state_prov_del (const char *label)
 Remove presence state provider. More...
 
struct stasis_topicast_presence_state_topic_all (void)
 Get presence state topic. More...
 
struct stasis_topicast_presence_state_topic_cached (void)
 Get caching presence state topic. More...
 
enum ast_presence_state ast_presence_state_val (const char *val)
 Convert presence state from text to integer value. More...
 
static void do_presence_state_change (const char *provider)
 
static struct ast_presence_state_messagepresence_state_alloc (const char *provider, enum ast_presence_state state, const char *subtype, const char *message)
 
static enum ast_presence_state presence_state_cached (const char *presence_provider, char **subtype, char **message)
 
static void presence_state_dtor (void *obj)
 
static void presence_state_engine_cleanup (void)
 
static void presence_state_event (const char *provider, enum ast_presence_state state, const char *subtype, const char *message)
 
static const char * presence_state_get_id (struct stasis_message *msg)
 
static struct ast_manager_event_blobpresence_state_to_ami (struct stasis_message *msg)
 
 STASIS_MESSAGE_TYPE_DEFN (ast_presence_state_message_type,.to_ami=presence_state_to_ami,)
 

Variables

struct stasis_cachepresence_state_cache
 
static struct presence_state_providers presence_state_providers = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
 
struct stasis_topicpresence_state_topic_all
 
struct stasis_caching_topicpresence_state_topic_cached
 
struct {
   enum ast_presence_state   state
 
   const char *   string
 
state2string []
 Device state strings for printing.
 

Detailed Description

Presence state management.

Definition in file presencestate.c.

Function Documentation

enum ast_presence_state ast_presence_state ( const char *  presence_provider,
char **  subtype,
char **  message 
)

Asks a presence state provider for the current presence state.

Parameters
presence_providerThe presence provider to retrieve the state from.
subtypeThe output paramenter to store the subtype string in. Must be freed if returned
messageThe output paramenter to store the message string in. Must be freed if returned
Return values
presencestate value on success,
-1on failure.

Definition at line 221 of file presencestate.c.

222 {
223  return ast_presence_state_helper(presence_provider, subtype, message, 1);
224 }
const char* ast_presence_state2str ( enum ast_presence_state  state)

Convert presence state to text string for output.

Parameters
stateCurrent presence state

Definition at line 103 of file presencestate.c.

References state2string.

Referenced by handle_show_hint(), and handle_show_hints().

104 {
105  int i;
106  for (i = 0; i < ARRAY_LEN(state2string); i++) {
107  if (state == state2string[i].state) {
108  return state2string[i].string;
109  }
110  }
111  return "";
112 }
static const struct @386 state2string[]
Device state strings for printing.
struct stasis_cache* ast_presence_state_cache ( void  )

Backend cache for ast_presence_state_topic_cached()

Return values
Cacheof ast_presence_state_message.
Since
12

Definition at line 370 of file presencestate.c.

371 {
372  return presence_state_cache;
373 }
int ast_presence_state_changed ( enum ast_presence_state  state,
const char *  subtype,
const char *  message,
const char *  fmt,
  ... 
)

Notify the world that a presence provider state changed.

Parameters
statethe new presence state
subtypethe new presence subtype
messagethe new presence message
fmtPresence entity whose state has changed

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

Return values
0Success
-1Failure

Definition at line 350 of file presencestate.c.

References AST_MAX_EXTENSION, and ast_presence_state_changed_literal().

354 {
355  char buf[AST_MAX_EXTENSION];
356  va_list ap;
357 
358  va_start(ap, fmt);
359  vsnprintf(buf, sizeof(buf), fmt, ap);
360  va_end(ap);
361 
362  return ast_presence_state_changed_literal(state, subtype, message, buf);
363 }
#define AST_MAX_EXTENSION
Definition: channel.h:134
int ast_presence_state_changed_literal(enum ast_presence_state state, const char *subtype, const char *message, const char *presence_provider)
Notify the world that a presence provider state changed.
int ast_presence_state_changed_literal ( enum ast_presence_state  state,
const char *  subtype,
const char *  message,
const char *  presence_provider 
)

Notify the world that a presence provider state changed.

Parameters
statethe new presence state
subtypethe new presence subtype
messagethe new presence message
presence_providerPresence entity whose state has changed

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

Return values
0Success
-1Failure

Definition at line 336 of file presencestate.c.

Referenced by ast_presence_state_changed().

340 {
341  if (state == AST_PRESENCE_NOT_SET) {
342  do_presence_state_change(presence_provider);
343  } else {
344  presence_state_event(presence_provider, state, subtype, message);
345  }
346 
347  return 0;
348 }
enum ast_presence_state ast_presence_state_nocache ( const char *  presence_provider,
char **  subtype,
char **  message 
)

Asks a presence state provider for the current presence state, bypassing the event cache.

Some presence state providers may perform transformations on presence data when it is requested (such as a base64 decode). In such instances, use of the event cache is not suitable and should be bypassed.

Parameters
presence_providerThe presence provider to retrieve the state from.
subtypeThe output paramenter to store the subtype string in. Must be freed if returned
messageThe output paramenter to store the message string in. Must be freed if returned
Return values
presencestate value on success,
-1on failure.

Definition at line 226 of file presencestate.c.

227 {
228  return ast_presence_state_helper(presence_provider, subtype, message, 0);
229 }
int ast_presence_state_prov_add ( const char *  label,
ast_presence_state_prov_cb_type  callback 
)

Add presence state provider.

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

Definition at line 231 of file presencestate.c.

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

232 {
233  struct presence_state_provider *provider;
234 
235  if (!callback || !(provider = ast_calloc(1, sizeof(*provider)))) {
236  return -1;
237  }
238 
239  provider->callback = callback;
240  ast_copy_string(provider->label, label, sizeof(provider->label));
241 
243  AST_RWLIST_INSERT_HEAD(&presence_state_providers, provider, list);
245 
246  return 0;
247 }
A list of providers.
A presence state provider.
Definition: presencestate.c:94
#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_presence_state_prov_del ( const char *  label)

Remove presence state provider.

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

Definition at line 248 of file presencestate.c.

References AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

249 {
250  struct presence_state_provider *provider;
251  int res = -1;
252 
254  AST_RWLIST_TRAVERSE_SAFE_BEGIN(&presence_state_providers, provider, list) {
255  if (!strcasecmp(provider->label, label)) {
256  AST_RWLIST_REMOVE_CURRENT(list);
257  ast_free(provider);
258  res = 0;
259  break;
260  }
261  }
262  AST_RWLIST_TRAVERSE_SAFE_END;
264 
265  return res;
266 }
A list of providers.
A presence state provider.
Definition: presencestate.c:94
#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
struct stasis_topic* ast_presence_state_topic_all ( void  )

Get presence state topic.

Return values
Stasistopic for presence state messages
Since
12

Definition at line 365 of file presencestate.c.

Referenced by load_pbx(), and publish_hint_change().

366 {
367  return presence_state_topic_all;
368 }
struct stasis_topic* ast_presence_state_topic_cached ( void  )

Get caching presence state topic.

Return values
CachingStasis topic for presence state messages
Since
12

Definition at line 375 of file presencestate.c.

References stasis_caching_get_topic().

376 {
377  return stasis_caching_get_topic(presence_state_topic_cached);
378 }
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
enum ast_presence_state ast_presence_state_val ( const char *  val)

Convert presence state from text to integer value.

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

Definition at line 114 of file presencestate.c.

References state2string.

115 {
116  int i;
117  for (i = 0; i < ARRAY_LEN(state2string); i++) {
118  if (!strcasecmp(val, state2string[i].string)) {
119  return state2string[i].state;
120  }
121  }
122  return AST_PRESENCE_INVALID;
123 }
static const struct @386 state2string[]
Device state strings for printing.