Sat Apr 26 2014 22:03:05

Asterisk developer's documentation


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/event.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...
struct  state_change
struct  state_changes
 The state change queue. State changes are queued for processing by a separate thread. 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.
const char * ast_presence_state2str (enum ast_presence_state state)
 Convert presence state to text string for output.
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.
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_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.
int ast_presence_state_prov_add (const char *label, ast_presence_state_prov_cb_type callback)
 Add presence state provider.
int ast_presence_state_prov_del (const char *label)
 Remove presence state provider.
enum ast_presence_state ast_presence_state_val (const char *val)
 Convert presence state from text to integer value.
static void * do_presence_changes (void *data)
 Go through the presence state change queue and update changes in the presence state thread.
static void do_presence_state_change (const char *provider)
static enum ast_presence_state presence_state_cached (const char *presence_provider, char **subtype, char **message)
static void presence_state_event (const char *provider, enum ast_presence_state state, const char *subtype, const char *message)

Variables

static ast_cond_t change_pending
 Flag for the queue.
static pthread_t change_thread = AST_PTHREADT_NULL
 The presence state change notification thread.
static struct
presence_state_providers 
presence_state_providers
struct {
   enum ast_presence_state   state
   const char *   string
state2string []
 Device state strings for printing.
static struct state_changes state_changes

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_provider,Thepresence provider to retrieve the state from.
subtype,Theoutput paramenter to store the subtype string in. Must be freed if returned
message,Theoutput paramenter to store the message string in. Must be freed if returned
Return values:
presencestate value on success,
-1on failure.

Definition at line 169 of file presencestate.c.

References ast_presence_state_helper().

{
   return ast_presence_state_helper(presence_provider, subtype, message, 1);
}
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 81 of file presencestate.c.

References ARRAY_LEN, and state2string.

Referenced by action_presencestate(), handle_cli_presencestate_list(), hints_data_provider_get(), manager_state_cb(), presence_read(), and state_notify_build_xml().

{
   int i;
   for (i = 0; i < ARRAY_LEN(state2string); i++) {
      if (state == state2string[i].state) {
         return state2string[i].string;
      }
   }
   return "";
}
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 275 of file presencestate.c.

References AST_MAX_EXTENSION, and ast_presence_state_changed_literal().

Referenced by load_module().

{
   char buf[AST_MAX_EXTENSION];
   va_list ap;

   va_start(ap, fmt);
   vsnprintf(buf, sizeof(buf), fmt, ap);
   va_end(ap);

   return ast_presence_state_changed_literal(state, subtype, message, buf);
}
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 252 of file presencestate.c.

References ast_calloc, ast_cond_signal, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_UNLOCK, AST_PRESENCE_NOT_SET, AST_PTHREADT_NULL, change_thread, do_presence_state_change(), state_change::list, presence_state_event(), and state_change::provider.

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

{
   struct state_change *change;

   if (state != AST_PRESENCE_NOT_SET) {
      presence_state_event(presence_provider, state, subtype, message);
   } else if ((change_thread == AST_PTHREADT_NULL) ||
      !(change = ast_calloc(1, sizeof(*change) + strlen(presence_provider)))) {
      do_presence_state_change(presence_provider);
   } else {
      strcpy(change->provider, presence_provider);
      AST_LIST_LOCK(&state_changes);
      AST_LIST_INSERT_TAIL(&state_changes, change, list);
      ast_cond_signal(&change_pending);
      AST_LIST_UNLOCK(&state_changes);
   }

   return 0;
}

Definition at line 315 of file presencestate.c.

References ast_cond_init, ast_log(), ast_pthread_create_background, change_thread, do_presence_changes(), and LOG_ERROR.

Referenced by main().

{
   ast_cond_init(&change_pending, NULL);
   if (ast_pthread_create_background(&change_thread, NULL, do_presence_changes, NULL) < 0) {
      ast_log(LOG_ERROR, "Unable to start presence state change thread.\n");
      return -1;
   }

   return 0;
}
static enum ast_presence_state ast_presence_state_helper ( const char *  presence_provider,
char **  subtype,
char **  message,
int  check_cache 
) [static]

Definition at line 129 of file presencestate.c.

References ast_debug, ast_log(), AST_PRESENCE_INVALID, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, presence_state_provider::callback, presence_state_provider::label, LOG_WARNING, and presence_state_cached().

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

{
   struct presence_state_provider *provider;
   char *address;
   char *label = ast_strdupa(presence_provider);
   int res = AST_PRESENCE_INVALID;

   if (check_cache) {
      res = presence_state_cached(presence_provider, subtype, message);
      if (res != AST_PRESENCE_INVALID) {
         return res;
      }
   }

   if ((address = strchr(label, ':'))) {
      *address = '\0';
      address++;
   } else {
      ast_log(LOG_WARNING, "No label found for presence state provider: %s\n", presence_provider);
      return res;
   }

   AST_RWLIST_RDLOCK(&presence_state_providers);
   AST_RWLIST_TRAVERSE(&presence_state_providers, provider, list) {
      ast_debug(5, "Checking provider %s with %s\n", provider->label, label);

      if (!strcasecmp(provider->label, label)) {
         res = provider->callback(address, subtype, message);
         break;
      }
   }
   AST_RWLIST_UNLOCK(&presence_state_providers);

   if (!provider) {
      ast_log(LOG_WARNING, "No provider found for label %s\n", label);
   }

   return res;
}
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_provider,Thepresence provider to retrieve the state from.
subtype,Theoutput paramenter to store the subtype string in. Must be freed if returned
message,Theoutput paramenter to store the message string in. Must be freed if returned
Return values:
presencestate value on success,
-1on failure.

Definition at line 174 of file presencestate.c.

References ast_presence_state_helper().

Referenced by presence_read().

{
   return ast_presence_state_helper(presence_provider, subtype, message, 0);
}
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 179 of file presencestate.c.

References ast_calloc, ast_copy_string(), AST_RWLIST_INSERT_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, presence_state_provider::callback, and presence_state_provider::label.

Referenced by load_module().

{
   struct presence_state_provider *provider;

   if (!callback || !(provider = ast_calloc(1, sizeof(*provider)))) {
      return -1;
   }

   provider->callback = callback;
   ast_copy_string(provider->label, label, sizeof(provider->label));

   AST_RWLIST_WRLOCK(&presence_state_providers);
   AST_RWLIST_INSERT_HEAD(&presence_state_providers, provider, list);
   AST_RWLIST_UNLOCK(&presence_state_providers);

   return 0;
}
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 196 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, and presence_state_provider::label.

Referenced by unload_module().

{
   struct presence_state_provider *provider;
   int res = -1;

   AST_RWLIST_WRLOCK(&presence_state_providers);
   AST_RWLIST_TRAVERSE_SAFE_BEGIN(&presence_state_providers, provider, list) {
      if (!strcasecmp(provider->label, label)) {
         AST_RWLIST_REMOVE_CURRENT(list);
         ast_free(provider);
         res = 0;
         break;
      }
   }
   AST_RWLIST_TRAVERSE_SAFE_END;
   AST_RWLIST_UNLOCK(&presence_state_providers);

   return res;
}
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 92 of file presencestate.c.

References ARRAY_LEN, AST_PRESENCE_INVALID, and state2string.

Referenced by parse_data().

{
   int i;
   for (i = 0; i < ARRAY_LEN(state2string); i++) {
      if (!strcasecmp(val, state2string[i].string)) {
         return state2string[i].state;
      }
   }
   return AST_PRESENCE_INVALID;
}
static void* do_presence_changes ( void *  data) [static]

Go through the presence state change queue and update changes in the presence state thread.

Definition at line 291 of file presencestate.c.

References ast_cond_wait, ast_free, AST_LIST_EMPTY, AST_LIST_FIRST, AST_LIST_HEAD_INIT_NOLOCK, AST_LIST_LOCK, AST_LIST_NEXT, AST_LIST_UNLOCK, do_presence_state_change(), state_change::list, state_changes::lock, state_change::next, and state_change::provider.

Referenced by ast_presence_state_engine_init().

{
   struct state_change *next, *current;

   for (;;) {
      /* This basically pops off any state change entries, resets the list back to NULL, unlocks, and processes each state change */
      AST_LIST_LOCK(&state_changes);
      if (AST_LIST_EMPTY(&state_changes))
         ast_cond_wait(&change_pending, &state_changes.lock);
      next = AST_LIST_FIRST(&state_changes);
      AST_LIST_HEAD_INIT_NOLOCK(&state_changes);
      AST_LIST_UNLOCK(&state_changes);

      /* Process each state change */
      while ((current = next)) {
         next = AST_LIST_NEXT(current, list);
         do_presence_state_change(current->provider);
         ast_free(current);
      }
   }

   return NULL;
}
static void do_presence_state_change ( const char *  provider) [static]

Definition at line 235 of file presencestate.c.

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

Referenced by ast_presence_state_changed_literal(), and do_presence_changes().

{
   char *subtype = NULL;
   char *message = NULL;
   enum ast_presence_state state;

   state = ast_presence_state_helper(provider, &subtype, &message, 0);

   if (state < 0) {
      return;
   }

   presence_state_event(provider, state, subtype, message);
   ast_free(subtype);
   ast_free(message);
}

Variable Documentation

Flag for the queue.

Definition at line 57 of file presencestate.c.

pthread_t change_thread = AST_PTHREADT_NULL [static]

The presence state change notification thread.

Definition at line 79 of file presencestate.c.

Referenced by ast_presence_state_changed_literal(), and ast_presence_state_engine_init().

Definition at line 44 of file presencestate.c.

struct { ... } state2string[] [static]

Device state strings for printing.

Referenced by ast_presence_state2str(), and ast_presence_state_val().

struct state_changes state_changes [static]
const char* string