Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | 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"
Include dependency graph for presencestate.c:

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

Macros

#define TEST_CATEGORY   "/main/presence/"
 

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...
 
 AST_TEST_DEFINE (test_presence_chan)
 
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)
 
static int presence_test_presencestate (const char *label, char **subtype, char **message)
 
 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
 
static int presence_test_alice_state = AST_PRESENCE_UNAVAILABLE
 
static int presence_test_bob_state = AST_PRESENCE_UNAVAILABLE
 
static struct ast_channel_tech presence_test_tech
 
struct {
   enum ast_presence_state   state
 
   const char *   string
 
state2string []
 Device state strings for printing. More...
 

Detailed Description

Presence state management.

Definition in file presencestate.c.

Macro Definition Documentation

◆ TEST_CATEGORY

#define TEST_CATEGORY   "/main/presence/"

Definition at line 393 of file presencestate.c.

Referenced by AST_TEST_DEFINE().

Function Documentation

◆ ast_presence_state()

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.

References ast_presence_state_helper().

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

◆ ast_presence_state2str()

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 ARRAY_LEN, and state2string.

Referenced by action_presencestate(), AST_TEST_DEFINE(), exten_state_publisher_state_cb(), handle_cli_presencestate_list(), handle_show_hint(), handle_show_hints(), manager_state_cb(), pidf_supplement_body(), presence_change_common(), presence_read(), presence_state_to_ami(), and state_notify_build_xml().

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 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static const struct @413 state2string[]
Device state strings for printing.

◆ ast_presence_state_cache()

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.

References presence_state_cache.

Referenced by action_presencestatelist(), and presence_state_cached().

371 {
372  return presence_state_cache;
373 }
struct stasis_cache * presence_state_cache
Definition: presencestate.c:90

◆ ast_presence_state_changed()

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, ast_presence_state_changed_literal(), and buf.

Referenced by load_module().

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 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_MAX_EXTENSION
Definition: channel.h:135
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.

◆ ast_presence_state_changed_literal()

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.

References AST_PRESENCE_NOT_SET, do_presence_state_change(), and presence_state_event().

Referenced by ast_presence_state_changed(), handle_cli_presencestate_change(), and presence_write().

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 }
static void do_presence_state_change(const char *provider)
static void presence_state_event(const char *provider, enum ast_presence_state state, const char *subtype, const char *message)

◆ ast_presence_state_engine_init()

int ast_presence_state_engine_init ( void  )

Definition at line 495 of file presencestate.c.

References ast_presence_state_message_type(), ast_register_cleanup(), AST_TEST_REGISTER, presence_state_engine_cleanup(), presence_state_get_id(), stasis_cache_create(), stasis_caching_accept_message_type(), stasis_caching_set_filter(), stasis_caching_topic_create(), STASIS_MESSAGE_TYPE_INIT, STASIS_SUBSCRIPTION_FILTER_SELECTIVE, and stasis_topic_create().

Referenced by asterisk_daemon().

496 {
498 
500  return -1;
501  }
502 
503  presence_state_topic_all = stasis_topic_create("presence_state:all");
505  return -1;
506  }
507 
509  if (!presence_state_cache) {
510  return -1;
511  }
512 
515  return -1;
516  }
519 
520  AST_TEST_REGISTER(test_presence_chan);
521 
522  return 0;
523 }
struct stasis_topic * presence_state_topic_all
Definition: presencestate.c:89
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 const char * presence_state_get_id(struct stasis_message *msg)
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1501
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
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
struct stasis_topic * stasis_topic_create(const char *name)
Create a new topic.
Definition: stasis.c:618
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_cache * presence_state_cache
Definition: presencestate.c:90
struct stasis_cache * stasis_cache_create(snapshot_get_id id_fn)
Create a cache.
Definition: stasis_cache.c:360
struct stasis_message_type * ast_presence_state_message_type(void)
Get presence state message type.
struct stasis_caching_topic * presence_state_topic_cached
Definition: presencestate.c:91
static void presence_state_engine_cleanup(void)

◆ ast_presence_state_helper()

static enum ast_presence_state ast_presence_state_helper ( const char *  presence_provider,
char **  subtype,
char **  message,
int  check_cache 
)
static

Definition at line 146 of file presencestate.c.

References ast_debug, ast_free, ast_get_channel_tech(), ast_log, AST_PRESENCE_AVAILABLE, AST_PRESENCE_AWAY, AST_PRESENCE_CHAT, AST_PRESENCE_DND, AST_PRESENCE_INVALID, AST_PRESENCE_NOT_SET, AST_PRESENCE_UNAVAILABLE, AST_PRESENCE_XA, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_strdupa, presence_state_provider::callback, presence_state_provider::label, LOG_WARNING, NULL, presence_state_cached(), ast_channel_tech::presencestate, provider, state, and strsep().

Referenced by ast_presence_state(), ast_presence_state_nocache(), and do_presence_state_change().

147 {
148  char *labels = ast_strdupa(presence_provider);
149  char *label;
151  enum ast_presence_state state_order[] = {
152  [AST_PRESENCE_INVALID] = 0,
153  [AST_PRESENCE_NOT_SET] = 1,
156  [AST_PRESENCE_CHAT] = 4,
157  [AST_PRESENCE_AWAY] = 5,
158  [AST_PRESENCE_XA] = 6,
159  [AST_PRESENCE_DND] = 7
160  };
161 
162  *subtype = NULL;
163  *message = NULL;
164 
165  while ((label = strsep(&labels, "&"))) {
166  enum ast_presence_state next_state = AST_PRESENCE_INVALID;
167  char *next_subtype = NULL;
168  char *next_message = NULL;
169 
170  if (check_cache) {
171  next_state = presence_state_cached(label, &next_subtype, &next_message);
172  }
173 
174  if (next_state == AST_PRESENCE_INVALID) {
176  const struct ast_channel_tech *chan_tech;
177  char *address;
178 
179  if ((address = strchr(label, '/'))) {
180  *address++ = '\0';
181 
182  if ((chan_tech = ast_get_channel_tech(label)) && chan_tech->presencestate) {
183  next_state = chan_tech->presencestate(address, &next_subtype, &next_message);
184  }
185  } else if ((address = strchr(label, ':'))) {
186  *address++ = '\0';
187 
190  ast_debug(5, "Checking provider %s with %s\n", provider->label, label);
191 
192  if (!strcasecmp(provider->label, label)) {
193  next_state = provider->callback(address, &next_subtype, &next_message);
194  break;
195  }
196  }
198 
199  if (!provider) {
200  ast_log(LOG_WARNING, "No provider found for label: %s\n", label);
201  }
202  } else {
203  ast_log(LOG_WARNING, "No label found for presence state provider: %s\n", label);
204  }
205  }
206 
207  if (state_order[next_state] > state_order[state]) {
208  state = next_state;
209 
210  ast_free(*subtype);
211  ast_free(*message);
212 
213  *subtype = next_subtype;
214  *message = next_message;
215  }
216  }
217 
218  return state;
219 }
int(*const presencestate)(const char *presence_provider, char **subtype, char **message)
Definition: channel.h:677
const struct ast_channel_tech * ast_get_channel_tech(const char *name)
Get a channel technology structure by name.
Definition: channel.c:592
ast_presence_state_prov_cb_type callback
Definition: presencestate.c:96
char * address
Definition: f2c.h:59
A list of providers.
A presence state provider.
Definition: presencestate.c:94
#define LOG_WARNING
Definition: logger.h:274
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
#define NULL
Definition: resample.c:96
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
ast_presence_state
Definition: presencestate.h:26
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
Structure to describe a channel "technology", ie a channel driver See for examples: ...
Definition: channel.h:629
#define ast_free(a)
Definition: astmm.h:182
char * strsep(char **str, const char *delims)
static enum ast_presence_state presence_state_cached(const char *presence_provider, char **subtype, char **message)
static struct prometheus_metrics_provider provider
Definition: bridges.c:178
enum ast_presence_state state
Definition: presencestate.c:72

◆ ast_presence_state_nocache()

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.

References ast_presence_state_helper().

Referenced by AST_TEST_DEFINE(), and presence_read().

227 {
228  return ast_presence_state_helper(presence_provider, subtype, message, 0);
229 }
static enum ast_presence_state ast_presence_state_helper(const char *presence_provider, char **subtype, char **message, int check_cache)

◆ ast_presence_state_prov_add()

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_INSERT_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, presence_state_provider::callback, presence_state_provider::label, and provider.

Referenced by load_module().

232 {
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 
245 
246  return 0;
247 }
ast_presence_state_prov_cb_type callback
Definition: presencestate.c:96
A list of providers.
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
A presence state provider.
Definition: presencestate.c:94
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:717
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
static struct prometheus_metrics_provider provider
Definition: bridges.c:178

◆ ast_presence_state_prov_del()

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_free, AST_RWLIST_REMOVE_CURRENT, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, presence_state_provider::label, and provider.

Referenced by unload_module().

249 {
251  int res = -1;
252 
255  if (!strcasecmp(provider->label, label)) {
257  ast_free(provider);
258  res = 0;
259  break;
260  }
261  }
264 
265  return res;
266 }
A list of providers.
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
A presence state provider.
Definition: presencestate.c:94
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
#define AST_RWLIST_REMOVE_CURRENT
Definition: linkedlists.h:569
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:544
#define ast_free(a)
Definition: astmm.h:182
static struct prometheus_metrics_provider provider
Definition: bridges.c:178
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:616

◆ ast_presence_state_topic_all()

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.

References presence_state_topic_all.

Referenced by load_module(), load_pbx(), presence_change_common(), presence_state_event(), and publish_hint_change().

366 {
368 }
struct stasis_topic * presence_state_topic_all
Definition: presencestate.c:89

◆ ast_presence_state_topic_cached()

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 {
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
struct stasis_caching_topic * presence_state_topic_cached
Definition: presencestate.c:91

◆ ast_presence_state_val()

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 ARRAY_LEN, AST_PRESENCE_INVALID, and state2string.

Referenced by parse_data().

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 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
Definition: ast_expr2.c:325
static const struct @413 state2string[]
Device state strings for printing.

◆ AST_TEST_DEFINE()

AST_TEST_DEFINE ( test_presence_chan  )

Definition at line 415 of file presencestate.c.

References ast_channel_register(), ast_channel_unregister(), ast_free, ast_log, AST_PRESENCE_AVAILABLE, AST_PRESENCE_DND, ast_presence_state2str(), ast_presence_state_nocache(), AST_PRESENCE_UNAVAILABLE, AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, sip_to_pjsip::info(), LOG_WARNING, NULL, presence_test_alice_state, presence_test_bob_state, provider, state, TEST_CATEGORY, TEST_EXECUTE, TEST_INIT, and ast_channel_tech::type.

416 {
417  int res = AST_TEST_FAIL;
418  char provider[80];
420  char *subtype = NULL, *message = NULL;
421 
422  switch (cmd) {
423  case TEST_INIT:
424  info->name = "channel_presence";
425  info->category = TEST_CATEGORY;
426  info->summary = "Channel presence state tests";
427  info->description = "Creates test channel technology and then test the presence state callback";
428  return AST_TEST_NOT_RUN;
429  case TEST_EXECUTE:
430  break;
431  }
432 
434  ast_log(LOG_WARNING, "Unable to register channel type '%s'\n", presence_test_tech.type);
435  goto presence_test_cleanup;
436  }
437 
438  /* Check Alice's state */
439  snprintf(provider, sizeof(provider), "%s/Alice", presence_test_tech.type);
440 
442  state = ast_presence_state_nocache(provider, &subtype, &message);
443 
444  if (state != presence_test_alice_state) {
445  ast_log(LOG_WARNING, "Presence state of '%s' returned '%s' instead of the expected value '%s'\n",
447  goto presence_test_cleanup;
448  }
449 
450  /* Check Alice's and Bob's state, Alice's should win as DND > AVAILABLE */
451  snprintf(provider, sizeof(provider), "%s/Alice&%s/Bob", presence_test_tech.type, presence_test_tech.type);
452 
455  state = ast_presence_state_nocache(provider, &subtype, &message);
456 
457  if (state != presence_test_alice_state) {
458  ast_log(LOG_WARNING, "Presence state of '%s' returned '%s' instead of the expected value '%s'\n",
460  goto presence_test_cleanup;
461  }
462 
463  /* Check Alice's and Bob's state, Bob's should now win as AVAILABLE < UNAVAILABLE */
465  state = ast_presence_state_nocache(provider, &subtype, &message);
466 
467  if (state != presence_test_bob_state) {
468  ast_log(LOG_WARNING, "Presence state of '%s' returned '%s' instead of the expected value '%s'\n",
470  goto presence_test_cleanup;
471  }
472 
473  res = AST_TEST_PASS;
474 
475 presence_test_cleanup:
477  ast_free(subtype);
478  ast_free(message);
479 
480  return res;
481 }
const char *const type
Definition: channel.h:630
static struct ast_channel_tech presence_test_tech
void ast_channel_unregister(const struct ast_channel_tech *tech)
Unregister a channel technology.
Definition: channel.c:570
#define LOG_WARNING
Definition: logger.h:274
int ast_channel_register(const struct ast_channel_tech *tech)
Register a channel technology (a new channel driver) Called by a channel module to register the kind ...
Definition: channel.c:539
#define NULL
Definition: resample.c:96
#define ast_log
Definition: astobj2.c:42
ast_presence_state
Definition: presencestate.h:26
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...
#define TEST_CATEGORY
def info(msg)
#define ast_free(a)
Definition: astmm.h:182
const char * ast_presence_state2str(enum ast_presence_state state)
Convert presence state to text string for output.
static int presence_test_alice_state
static int presence_test_bob_state
static struct prometheus_metrics_provider provider
Definition: bridges.c:178
enum ast_presence_state state
Definition: presencestate.c:72

◆ do_presence_state_change()

static void do_presence_state_change ( const char *  provider)
static

Definition at line 319 of file presencestate.c.

References ast_free, AST_PRESENCE_INVALID, ast_presence_state_helper(), NULL, presence_state_event(), and state.

Referenced by ast_presence_state_changed_literal().

320 {
321  char *subtype = NULL;
322  char *message = NULL;
324 
325  state = ast_presence_state_helper(provider, &subtype, &message, 0);
326 
327  if (state == AST_PRESENCE_INVALID) {
328  return;
329  }
330 
331  presence_state_event(provider, state, subtype, message);
332  ast_free(subtype);
333  ast_free(message);
334 }
static enum ast_presence_state ast_presence_state_helper(const char *presence_provider, char **subtype, char **message, int check_cache)
#define NULL
Definition: resample.c:96
ast_presence_state
Definition: presencestate.h:26
#define ast_free(a)
Definition: astmm.h:182
static void presence_state_event(const char *provider, enum ast_presence_state state, const char *subtype, const char *message)
static struct prometheus_metrics_provider provider
Definition: bridges.c:178
enum ast_presence_state state
Definition: presencestate.c:72

◆ presence_state_alloc()

static struct ast_presence_state_message* presence_state_alloc ( const char *  provider,
enum ast_presence_state  state,
const char *  subtype,
const char *  message 
)
static

Definition at line 274 of file presencestate.c.

References ao2_alloc, ao2_cleanup, ao2_ref, ast_string_field_init, ast_string_field_set, NULL, presence_state_dtor(), RAII_VAR, S_OR, and state.

Referenced by presence_state_event().

278 {
279  RAII_VAR(struct ast_presence_state_message *, presence_state, ao2_alloc(sizeof(*presence_state), presence_state_dtor), ao2_cleanup);
280 
281  if (!presence_state || ast_string_field_init(presence_state, 256)) {
282  return NULL;
283  }
284 
285  presence_state->state = state;
286  ast_string_field_set(presence_state, provider, provider);
287  ast_string_field_set(presence_state, subtype, S_OR(subtype, ""));
288  ast_string_field_set(presence_state, message, S_OR(message, ""));
289 
290  ao2_ref(presence_state, +1);
291  return presence_state;
292 }
static void presence_state_dtor(void *obj)
Stasis message payload representing a presence state update.
#define NULL
Definition: resample.c:96
#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:911
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:353
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
static struct prometheus_metrics_provider provider
Definition: bridges.c:178
enum ast_presence_state state
Definition: presencestate.c:72
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:514

◆ presence_state_cached()

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

Definition at line 125 of file presencestate.c.

References ao2_cleanup, AST_PRESENCE_INVALID, ast_presence_state_cache(), ast_presence_state_message_type(), ast_strdup, ast_strlen_zero, NULL, RAII_VAR, stasis_cache_get(), stasis_message_data(), and ast_presence_state_message::state.

Referenced by ast_presence_state_helper().

126 {
128  RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
129  struct ast_presence_state_message *presence_state;
130 
132 
133  if (!msg) {
134  return res;
135  }
136 
137  presence_state = stasis_message_data(msg);
138  res = presence_state->state;
139 
140  *subtype = !ast_strlen_zero(presence_state->subtype) ? ast_strdup(presence_state->subtype) : NULL;
141  *message = !ast_strlen_zero(presence_state->message) ? ast_strdup(presence_state->message) : NULL;
142 
143  return res;
144 }
Stasis message payload representing a presence state update.
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
#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:911
ast_presence_state
Definition: presencestate.h:26
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
struct stasis_cache * ast_presence_state_cache(void)
Backend cache for ast_presence_state_topic_cached()
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
struct stasis_message_type * ast_presence_state_message_type(void)
Get presence state message type.
enum ast_presence_state state
struct stasis_message * stasis_cache_get(struct stasis_cache *cache, struct stasis_message_type *type, const char *id)
Retrieve an item from the cache for the ast_eid_default entity.
Definition: stasis_cache.c:686

◆ presence_state_dtor()

static void presence_state_dtor ( void *  obj)
static

Definition at line 268 of file presencestate.c.

References ast_string_field_free_memory.

Referenced by presence_state_alloc().

269 {
270  struct ast_presence_state_message *presence_state = obj;
271  ast_string_field_free_memory(presence_state);
272 }
Stasis message payload representing a presence state update.
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:368

◆ presence_state_engine_cleanup()

static void presence_state_engine_cleanup ( void  )
static

Definition at line 484 of file presencestate.c.

References ao2_cleanup, ast_presence_state_message_type(), AST_TEST_UNREGISTER, NULL, stasis_caching_unsubscribe_and_join(), and STASIS_MESSAGE_TYPE_CLEANUP.

Referenced by ast_presence_state_engine_init().

485 {
492  AST_TEST_UNREGISTER(test_presence_chan);
493 }
struct stasis_topic * presence_state_topic_all
Definition: presencestate.c:89
#define STASIS_MESSAGE_TYPE_CLEANUP(name)
Boiler-plate messaging macro for cleaning up message types.
Definition: stasis.h:1523
#define NULL
Definition: resample.c:96
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
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
struct stasis_cache * presence_state_cache
Definition: presencestate.c:90
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
struct stasis_message_type * ast_presence_state_message_type(void)
Get presence state message type.
struct stasis_caching_topic * presence_state_topic_cached
Definition: presencestate.c:91

◆ presence_state_event()

static void presence_state_event ( const char *  provider,
enum ast_presence_state  state,
const char *  subtype,
const char *  message 
)
static

Definition at line 294 of file presencestate.c.

References ao2_cleanup, ast_presence_state_message_type(), ast_presence_state_topic_all(), NULL, presence_state_alloc(), RAII_VAR, stasis_message_create(), and stasis_publish().

Referenced by ast_presence_state_changed_literal(), and do_presence_state_change().

298 {
299  RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
300  RAII_VAR(struct ast_presence_state_message *, presence_state, NULL, ao2_cleanup);
301 
303  return;
304  }
305 
306  presence_state = presence_state_alloc(provider, state, subtype, message);
307  if (!presence_state) {
308  return;
309  }
310 
312  if (!msg) {
313  return;
314  }
315 
317 }
Stasis message payload representing a presence state update.
static struct ast_presence_state_message * presence_state_alloc(const char *provider, enum ast_presence_state state, const char *subtype, const char *message)
#define NULL
Definition: resample.c:96
struct stasis_topic * ast_presence_state_topic_all(void)
Get presence state topic.
#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:911
struct stasis_message * stasis_message_create(struct stasis_message_type *type, void *data)
Create a new message.
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic&#39;s subscribers.
Definition: stasis.c:1511
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
struct stasis_message_type * ast_presence_state_message_type(void)
Get presence state message type.
static struct prometheus_metrics_provider provider
Definition: bridges.c:178

◆ presence_state_get_id()

static const char* presence_state_get_id ( struct stasis_message msg)
static

Definition at line 380 of file presencestate.c.

References ast_presence_state_message_type(), NULL, stasis_message_data(), and stasis_message_type().

Referenced by ast_presence_state_engine_init().

381 {
382  struct ast_presence_state_message *presence_state = stasis_message_data(msg);
383 
385  return NULL;
386  }
387 
388  return presence_state->provider;
389 }
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
Stasis message payload representing a presence state update.
#define NULL
Definition: resample.c:96
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
struct stasis_message_type * ast_presence_state_message_type(void)
Get presence state message type.

◆ presence_state_to_ami()

static struct ast_manager_event_blob * presence_state_to_ami ( struct stasis_message msg)
static

Definition at line 525 of file presencestate.c.

References ast_escape_c_alloc(), ast_free, ast_manager_event_blob_create(), ast_presence_state2str(), EVENT_FLAG_CALL, stasis_message_data(), and ast_presence_state_message::state.

526 {
527  struct ast_presence_state_message *presence_state = stasis_message_data(msg);
528  struct ast_manager_event_blob *res;
529 
530  char *subtype = ast_escape_c_alloc(presence_state->subtype);
531  char *message = ast_escape_c_alloc(presence_state->message);
532 
533  res = ast_manager_event_blob_create(EVENT_FLAG_CALL, "PresenceStateChange",
534  "Presentity: %s\r\n"
535  "Status: %s\r\n"
536  "Subtype: %s\r\n"
537  "Message: %s\r\n",
538  presence_state->provider,
539  ast_presence_state2str(presence_state->state),
540  subtype ?: "",
541  message ?: "");
542 
543  ast_free(subtype);
544  ast_free(message);
545 
546  return res;
547 }
Struct containing info for an AMI event to send out.
Definition: manager.h:491
#define EVENT_FLAG_CALL
Definition: manager.h:72
Stasis message payload representing a presence state update.
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:9727
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
#define ast_free(a)
Definition: astmm.h:182
char * ast_escape_c_alloc(const char *s)
Escape standard &#39;C&#39; sequences in the given string.
Definition: main/utils.c:1892
const char * ast_presence_state2str(enum ast_presence_state state)
Convert presence state to text string for output.
enum ast_presence_state state

◆ presence_test_presencestate()

static int presence_test_presencestate ( const char *  label,
char **  subtype,
char **  message 
)
static

Definition at line 398 of file presencestate.c.

References AST_PRESENCE_UNAVAILABLE, presence_test_alice_state, and presence_test_bob_state.

399 {
400  if (!strcmp(label, "Alice")) {
402  } else if (!strcmp(label, "Bob")) {
404  } else {
406  }
407 }
static int presence_test_alice_state
static int presence_test_bob_state

◆ STASIS_MESSAGE_TYPE_DEFN()

STASIS_MESSAGE_TYPE_DEFN ( ast_presence_state_message_type  ,
to_ami = presence_state_to_ami 
)

Variable Documentation

◆ presence_state_cache

struct stasis_cache* presence_state_cache

Definition at line 90 of file presencestate.c.

Referenced by ast_presence_state_cache().

◆ presence_state_providers

struct presence_state_providers presence_state_providers = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
static

◆ presence_state_topic_all

struct stasis_topic* presence_state_topic_all

Definition at line 89 of file presencestate.c.

Referenced by ast_presence_state_topic_all().

◆ presence_state_topic_cached

struct stasis_caching_topic* presence_state_topic_cached

Definition at line 91 of file presencestate.c.

◆ presence_test_alice_state

int presence_test_alice_state = AST_PRESENCE_UNAVAILABLE
static

Definition at line 395 of file presencestate.c.

Referenced by AST_TEST_DEFINE(), and presence_test_presencestate().

◆ presence_test_bob_state

int presence_test_bob_state = AST_PRESENCE_UNAVAILABLE
static

Definition at line 396 of file presencestate.c.

Referenced by AST_TEST_DEFINE(), and presence_test_presencestate().

◆ presence_test_tech

struct ast_channel_tech presence_test_tech
static
Initial value:
= {
.type = "PresenceTestChannel",
.description = "Presence test technology",
.presencestate = presence_test_presencestate,
}
static int presence_test_presencestate(const char *label, char **subtype, char **message)

Definition at line 409 of file presencestate.c.

◆ state

◆ state2string

const { ... } state2string[]

Device state strings for printing.

Referenced by ast_presence_state2str(), and ast_presence_state_val().

◆ string

const char* string