mirror of https://github.com/asterisk/asterisk
git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/10-digiumphones@361199 65c4cc65-6c06-0410-ace0-fbb531ad65f310-digiumphones
parent
c71f0ac4b1
commit
c343f0be25
@ -0,0 +1,191 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2011-2012, Digium, Inc.
|
||||
*
|
||||
* David Vossel <dvosse@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*! \file
|
||||
*
|
||||
* \brief Encode and Decode custom control frame payload types.
|
||||
*
|
||||
* \author David Vossel <dvossel@digium.com>
|
||||
*/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||
|
||||
#include "asterisk/_private.h"
|
||||
|
||||
#include "asterisk/custom_control_frame.h"
|
||||
|
||||
struct ast_custom_payload {
|
||||
enum ast_custom_payload_type type;
|
||||
/*! length of data portion only */
|
||||
size_t datalen;
|
||||
char *data;
|
||||
};
|
||||
|
||||
enum ast_custom_payload_type ast_custom_payload_type(struct ast_custom_payload *type)
|
||||
{
|
||||
return type->type;
|
||||
}
|
||||
|
||||
size_t ast_custom_payload_len(struct ast_custom_payload *type)
|
||||
{
|
||||
return type->datalen + sizeof(struct ast_custom_payload);
|
||||
}
|
||||
|
||||
struct custom_sipinfo {
|
||||
size_t num_headers;
|
||||
int content_present;
|
||||
int useragent_filter_present;
|
||||
char *data;
|
||||
};
|
||||
|
||||
struct ast_custom_payload *ast_custom_payload_sipinfo_encode(struct ast_variable *headers,
|
||||
const char *content_type,
|
||||
const char *content,
|
||||
const char *useragent_filter)
|
||||
{
|
||||
int num_headers = 0;
|
||||
int content_present = 0;
|
||||
int content_strlen = 0;
|
||||
int content_type_strlen = 0;
|
||||
int useragent_filter_present = 0;
|
||||
int useragent_filter_len = 0;
|
||||
size_t datalen = 0;
|
||||
struct ast_variable *var;
|
||||
struct ast_custom_payload *payload;
|
||||
struct custom_sipinfo *sipinfo;
|
||||
char *data;
|
||||
|
||||
datalen += sizeof(struct custom_sipinfo);
|
||||
|
||||
for (var = headers; var; var = var->next) {
|
||||
datalen += strlen(var->name) + 1;
|
||||
datalen += strlen(var->value) + 1;
|
||||
num_headers++;
|
||||
}
|
||||
|
||||
if (!ast_strlen_zero(content_type) && !ast_strlen_zero(content)) {
|
||||
content_type_strlen = strlen(content_type);
|
||||
content_strlen = strlen(content);
|
||||
datalen += content_type_strlen + 1;
|
||||
datalen += content_strlen + 1;
|
||||
content_present = 1;
|
||||
}
|
||||
|
||||
if (!ast_strlen_zero(useragent_filter)) {
|
||||
useragent_filter_len = strlen(useragent_filter);
|
||||
datalen += useragent_filter_len + 1;
|
||||
useragent_filter_present = 1;
|
||||
}
|
||||
|
||||
if (!(payload = ast_calloc(1, datalen + sizeof(*payload)))) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
payload->type = AST_CUSTOM_SIP_INFO;
|
||||
payload->datalen = datalen;
|
||||
payload->data = (char *) payload + sizeof(struct ast_custom_payload);
|
||||
sipinfo = (struct custom_sipinfo *) payload->data;
|
||||
sipinfo->num_headers = num_headers;
|
||||
sipinfo->content_present = content_present;
|
||||
sipinfo->useragent_filter_present = useragent_filter_present;
|
||||
sipinfo->data = (char *) sipinfo + sizeof(struct custom_sipinfo);
|
||||
|
||||
/* store string buffers in payload data
|
||||
* headers are put in first, followed by content type and then content body. */
|
||||
data = sipinfo->data;
|
||||
|
||||
for (var = headers; var; var = var->next) {
|
||||
int namelen = strlen(var->name);
|
||||
int vallen = strlen(var->value);
|
||||
|
||||
/*! we already know we have enough room for each of these */
|
||||
ast_copy_string(data, var->name, namelen+1);
|
||||
data += namelen + 1; /* skip over the '\0' character */
|
||||
ast_copy_string(data, var->value, vallen+1);
|
||||
data += vallen + 1; /* skip over the '\0' character */
|
||||
}
|
||||
|
||||
if (content_present) {
|
||||
ast_copy_string(data, content_type, content_type_strlen+1);
|
||||
data += content_type_strlen + 1;
|
||||
ast_copy_string(data, content, content_strlen+1);
|
||||
data += content_strlen + 1;
|
||||
}
|
||||
|
||||
if (useragent_filter_present) {
|
||||
ast_copy_string(data, useragent_filter, useragent_filter_len+1);
|
||||
}
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
int ast_custom_payload_sipinfo_decode(struct ast_custom_payload *pl,
|
||||
struct ast_variable **headers,
|
||||
char **content_type,
|
||||
char **content,
|
||||
char **useragent_filter)
|
||||
{
|
||||
struct custom_sipinfo *sipinfo;
|
||||
struct ast_variable *cur = NULL;
|
||||
char *data;
|
||||
int i;
|
||||
|
||||
*headers = NULL;
|
||||
*content_type = NULL;
|
||||
*content = NULL;
|
||||
*useragent_filter = NULL;
|
||||
|
||||
if (pl->type != AST_CUSTOM_SIP_INFO) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
sipinfo = (struct custom_sipinfo *) pl->data;
|
||||
data = sipinfo->data;
|
||||
for (i = 0; i < sipinfo->num_headers; i++) {
|
||||
const char *name;
|
||||
const char *value;
|
||||
|
||||
name = data;
|
||||
data += strlen(name) + 1;
|
||||
value = data;
|
||||
data += strlen(value) + 1;
|
||||
|
||||
if (*headers) {
|
||||
if ((cur->next = ast_variable_new(name, value, ""))) {
|
||||
cur = cur->next;
|
||||
}
|
||||
} else {
|
||||
*headers = cur = ast_variable_new(name, value, "");
|
||||
}
|
||||
}
|
||||
|
||||
if (sipinfo->content_present) {
|
||||
*content_type = ast_strdup(data);
|
||||
data += strlen(data) + 1;
|
||||
*content = ast_strdup(data);
|
||||
data += strlen(data) + 1;
|
||||
}
|
||||
|
||||
if (sipinfo->useragent_filter_present) {
|
||||
*useragent_filter = ast_strdup(data);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -0,0 +1,285 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2011-2012, Digium, Inc.
|
||||
*
|
||||
* David Vossel <dvossel@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*! \file
|
||||
*
|
||||
* \brief Presence state management
|
||||
*/
|
||||
#include "asterisk.h"
|
||||
|
||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||
|
||||
#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"
|
||||
|
||||
/*! \brief Device state strings for printing */
|
||||
static const struct {
|
||||
const char *string;
|
||||
enum ast_presence_state state;
|
||||
|
||||
} state2string[] = {
|
||||
{ "not_set", AST_PRESENCE_NOT_SET},
|
||||
{ "unavailable", AST_PRESENCE_UNAVAILABLE },
|
||||
{ "available", AST_PRESENCE_AVAILABLE},
|
||||
{ "away", AST_PRESENCE_AWAY},
|
||||
{ "xa", AST_PRESENCE_XA},
|
||||
{ "chat", AST_PRESENCE_CHAT},
|
||||
{ "dnd", AST_PRESENCE_DND},
|
||||
};
|
||||
|
||||
/*! \brief Flag for the queue */
|
||||
static ast_cond_t change_pending;
|
||||
|
||||
struct state_change {
|
||||
AST_LIST_ENTRY(state_change) list;
|
||||
char provider[1];
|
||||
};
|
||||
|
||||
/*! \brief A presence state provider */
|
||||
struct presence_state_provider {
|
||||
char label[40];
|
||||
ast_presence_state_prov_cb_type callback;
|
||||
AST_RWLIST_ENTRY(presence_state_provider) list;
|
||||
};
|
||||
|
||||
/*! \brief A list of providers */
|
||||
static AST_RWLIST_HEAD_STATIC(presence_state_providers, presence_state_provider);
|
||||
|
||||
/*! \brief The state change queue. State changes are queued
|
||||
for processing by a separate thread */
|
||||
static AST_LIST_HEAD_STATIC(state_changes, state_change);
|
||||
|
||||
/*! \brief The presence state change notification thread */
|
||||
static pthread_t change_thread = AST_PTHREADT_NULL;
|
||||
|
||||
const char *ast_presence_state2str(enum ast_presence_state state)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < ARRAY_LEN(state2string); i++) {
|
||||
if (state == state2string[i].state) {
|
||||
return state2string[i].string;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
enum ast_presence_state ast_presence_state_val(const char *val)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < ARRAY_LEN(state2string); i++) {
|
||||
if (!strcasecmp(val, state2string[i].string)) {
|
||||
return state2string[i].state;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static enum ast_presence_state presence_state_cached(const char *presence_provider, char **subtype, char **message)
|
||||
{
|
||||
enum ast_presence_state res = -1;
|
||||
struct ast_event *event;
|
||||
const char *_subtype;
|
||||
const char *_message;
|
||||
|
||||
event = ast_event_get_cached(AST_EVENT_PRESENCE_STATE,
|
||||
AST_EVENT_IE_PRESENCE_PROVIDER, AST_EVENT_IE_PLTYPE_STR, presence_provider,
|
||||
AST_EVENT_IE_END);
|
||||
|
||||
if (!event) {
|
||||
return res;
|
||||
}
|
||||
|
||||
res = ast_event_get_ie_uint(event, AST_EVENT_IE_PRESENCE_STATE);
|
||||
_subtype = ast_event_get_ie_str(event, AST_EVENT_IE_PRESENCE_SUBTYPE);
|
||||
_message = ast_event_get_ie_str(event, AST_EVENT_IE_PRESENCE_MESSAGE);
|
||||
|
||||
*subtype = !ast_strlen_zero(_subtype) ? ast_strdup(_subtype) : NULL;
|
||||
*message = !ast_strlen_zero(_message) ? ast_strdup(_message) : NULL;
|
||||
ast_event_destroy(event);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static enum ast_presence_state ast_presence_state_helper(const char *presence_provider, char **subtype, char **message, int check_cache)
|
||||
{
|
||||
struct presence_state_provider *provider;
|
||||
char *address;
|
||||
char *label = ast_strdupa(presence_provider);
|
||||
int res = -1;
|
||||
|
||||
if (check_cache) {
|
||||
res = presence_state_cached(presence_provider, subtype, message);
|
||||
if (res > 0) {
|
||||
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);
|
||||
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
enum ast_presence_state ast_presence_state(const char *presence_provider, char **subtype, char **message)
|
||||
{
|
||||
return ast_presence_state_helper(presence_provider, subtype, message, 1);
|
||||
}
|
||||
|
||||
int ast_presence_state_prov_add(const char *label, ast_presence_state_prov_cb_type callback)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
static void do_presence_state_change(const char *provider)
|
||||
{
|
||||
struct ast_event *event;
|
||||
enum ast_event_type event_type;
|
||||
char *subtype = NULL;
|
||||
char *message = NULL;
|
||||
int state;
|
||||
|
||||
state = ast_presence_state_helper(provider, &subtype, &message, 0);
|
||||
|
||||
if (state < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
event_type = AST_EVENT_PRESENCE_STATE;
|
||||
|
||||
if (!(event = ast_event_new(event_type,
|
||||
AST_EVENT_IE_PRESENCE_PROVIDER, AST_EVENT_IE_PLTYPE_STR, provider,
|
||||
AST_EVENT_IE_PRESENCE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
|
||||
AST_EVENT_IE_PRESENCE_SUBTYPE, AST_EVENT_IE_PLTYPE_STR, S_OR(subtype, ""),
|
||||
AST_EVENT_IE_PRESENCE_MESSAGE, AST_EVENT_IE_PLTYPE_STR, S_OR(message, ""),
|
||||
AST_EVENT_IE_END))) {
|
||||
return;
|
||||
}
|
||||
|
||||
ast_event_queue_and_cache(event);
|
||||
ast_free(subtype);
|
||||
ast_free(message);
|
||||
}
|
||||
|
||||
int ast_presence_state_changed(const char *presence_provider)
|
||||
{
|
||||
struct state_change *change;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/*! \brief Go through the presence state change queue and update changes in the presence state thread */
|
||||
static void *do_presence_changes(void *data)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
int ast_presence_state_engine_init(void)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue