res_pjsip_exten_state: Presence for digium phones
authorKevin Harwell <kharwell@digium.com>
Tue, 25 Feb 2014 17:51:51 +0000 (17:51 +0000)
committerKevin Harwell <kharwell@digium.com>
Tue, 25 Feb 2014 17:51:51 +0000 (17:51 +0000)
Added presence support for digium phones.

Review: https://reviewboard.asterisk.org/r/3239/
........

Merged revisions 408882 from http://svn.asterisk.org/svn/asterisk/branches/12

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@408883 65c4cc65-6c06-0410-ace0-fbb531ad65f3

include/asterisk/res_pjsip_body_generator_types.h
res/res_pjsip_exten_state.c
res/res_pjsip_pidf_digium_body_supplement.c [new file with mode: 0644]

index f27e896..09f234a 100644 (file)
@@ -36,6 +36,12 @@ struct ast_sip_exten_state_data {
        enum ast_extension_states exten_state;
        /*! The presence state of the change */
        enum ast_presence_state presence_state;
+       /*! The presence subtype of the change */
+       char *presence_subtype;
+       /*! The presence message of the change */
+       char *presence_message;
+       /*! Subscriber user agent */
+       char *user_agent;
        /*! Current device state information */
        struct ao2_container *device_state_info;
        /*! Local dialog URI */
index de3c0bf..7742de4 100644 (file)
@@ -57,8 +57,12 @@ struct exten_state_subscription {
        char context[AST_MAX_CONTEXT];
        /*! Extension within the context to receive updates from */
        char exten[AST_MAX_EXTENSION];
+       /*! The subscription's user agent */
+       char *user_agent;
        /*! The last known extension state */
        enum ast_extension_states last_exten_state;
+       /*! The last known presence state */
+       enum ast_presence_state last_presence_state;
 };
 
 #define DEFAULT_PRESENCE_BODY "application/pidf+xml"
@@ -90,9 +94,29 @@ static void exten_state_subscription_destructor(void *obj)
 {
        struct exten_state_subscription *sub = obj;
 
+       ast_free(sub->user_agent);
        ao2_cleanup(sub->sip_sub);
 }
 
+static char *get_user_agent(pjsip_rx_data *rdata)
+{
+       static const pj_str_t USER_AGENT = { "User-Agent", 10 };
+
+       size_t size;
+       char *user_agent = NULL;
+       pjsip_user_agent_hdr *user_agent_hdr = pjsip_msg_find_hdr_by_name(
+               rdata->msg_info.msg, &USER_AGENT, NULL);
+
+       if (!user_agent_hdr) {
+               return NULL;
+       }
+
+       size = pj_strlen(&user_agent_hdr->hvalue) + 1;
+       user_agent = ast_malloc(size);
+       ast_copy_pj_str(user_agent, &user_agent_hdr->hvalue, size);
+       return ast_str_to_lower(user_agent);
+}
+
 /*!
  * \internal
  * \brief Initialize the last extension state to something outside
@@ -125,7 +149,8 @@ static struct exten_state_subscription *exten_state_subscription_alloc(
        }
 
        exten_state_sub->last_exten_state = INITIAL_LAST_EXTEN_STATE;
-
+       exten_state_sub->last_presence_state = AST_PRESENCE_NOT_SET;
+       exten_state_sub->user_agent = get_user_agent(rdata);
        ao2_ref(exten_state_sub, +1);
        return exten_state_sub;
 }
@@ -190,6 +215,9 @@ static void send_notify(struct exten_state_subscription *exten_state_sub, const
                .exten = exten_state_sub->exten,
                .presence_state = ast_hint_presence_state(NULL, exten_state_sub->context,
                                                          exten_state_sub->exten, &subtype, &message),
+               .presence_subtype = subtype,
+               .presence_message = message,
+               .user_agent = exten_state_sub->user_agent
        };
 
        dlg = ast_sip_subscription_get_dlg(exten_state_sub->sip_sub);
@@ -226,6 +254,9 @@ static void notify_task_data_destructor(void *obj)
 
        ao2_ref(task_data->exten_state_sub, -1);
        ao2_cleanup(task_data->exten_state_data.device_state_info);
+       ast_free(task_data->exten_state_data.presence_subtype);
+       ast_free(task_data->exten_state_data.presence_message);
+       ast_free(task_data->exten_state_data.user_agent);
 }
 
 static struct notify_task_data *alloc_notify_task_data(char *exten, struct exten_state_subscription *exten_state_sub,
@@ -243,11 +274,15 @@ static struct notify_task_data *alloc_notify_task_data(char *exten, struct exten
        task_data->evsub_state = PJSIP_EVSUB_STATE_ACTIVE;
        task_data->exten_state_sub = exten_state_sub;
        task_data->exten_state_sub->last_exten_state = info->exten_state;
+       task_data->exten_state_sub->last_presence_state = info->presence_state;
        ao2_ref(task_data->exten_state_sub, +1);
 
        task_data->exten_state_data.exten = exten_state_sub->exten;
        task_data->exten_state_data.exten_state = info->exten_state;
        task_data->exten_state_data.presence_state = info->presence_state;
+       task_data->exten_state_data.presence_subtype = ast_strdup(info->presence_subtype);
+       task_data->exten_state_data.presence_message = ast_strdup(info->presence_message);
+       task_data->exten_state_data.user_agent = ast_strdup(exten_state_sub->user_agent);
        task_data->exten_state_data.device_state_info = info->device_state_info;
 
        if (task_data->exten_state_data.device_state_info) {
@@ -299,7 +334,8 @@ static int state_changed(char *context, char *exten,
        struct notify_task_data *task_data;
        struct exten_state_subscription *exten_state_sub = data;
 
-       if (exten_state_sub->last_exten_state == info->exten_state) {
+       if (exten_state_sub->last_exten_state == info->exten_state &&
+               exten_state_sub->last_presence_state == info->presence_state) {
                return 0;
        }
 
diff --git a/res/res_pjsip_pidf_digium_body_supplement.c b/res/res_pjsip_pidf_digium_body_supplement.c
new file mode 100644 (file)
index 0000000..d1b2c6b
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2014, Digium, Inc.
+ *
+ * Kevin Harwell <kharwell@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.
+ */
+
+/*** MODULEINFO
+       <depend>pjproject</depend>
+       <depend>res_pjsip</depend>
+       <depend>res_pjsip_pubsub</depend>
+       <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include <pjsip.h>
+#include <pjsip_simple.h>
+#include <pjlib.h>
+
+#include "asterisk/module.h"
+#include "asterisk/presencestate.h"
+#include "asterisk/res_pjsip.h"
+#include "asterisk/res_pjsip_pubsub.h"
+#include "asterisk/res_pjsip_presence_xml.h"
+#include "asterisk/res_pjsip_body_generator_types.h"
+
+static int pidf_supplement_body(void *body, void *data)
+{
+       struct ast_sip_exten_state_data *state_data = data;
+       pj_xml_node *node;
+
+       if (ast_strlen_zero(state_data->user_agent) ||
+           !strstr(state_data->user_agent, "digium")) {
+               /* not a digium phone */
+               return 0;
+       }
+
+       if (!(node = ast_sip_presence_xml_create_node(
+                     state_data->pool, body, "tuple"))) {
+               ast_log(LOG_WARNING, "Unable to create PIDF tuple\n");
+               return -1;
+       }
+
+       ast_sip_presence_xml_create_attr(
+               state_data->pool, node, "id", "digium-presence");
+
+       if (!(node = ast_sip_presence_xml_create_node(
+                     state_data->pool, node, "status"))) {
+               ast_log(LOG_WARNING, "Unable to create PIDF tuple status\n");
+               return -1;
+       }
+
+       if (!(node = ast_sip_presence_xml_create_node(
+                     state_data->pool, node, "digium_presence"))) {
+               ast_log(LOG_WARNING, "Unable to create digium presence\n");
+               return -1;
+       }
+
+       if (!ast_strlen_zero(state_data->presence_message)) {
+                       pj_strdup2(state_data->pool, &node->content,
+                                  state_data->presence_message);
+       }
+
+       ast_sip_presence_xml_create_attr(
+               state_data->pool, node, "type", ast_presence_state2str(
+                       state_data->presence_state));
+
+       if (!ast_strlen_zero(state_data->presence_subtype)) {
+               ast_sip_presence_xml_create_attr(
+                       state_data->pool, node, "subtype",
+                       state_data->presence_subtype);
+       }
+
+       return 0;
+}
+
+static struct ast_sip_pubsub_body_supplement pidf_supplement = {
+       .type = "application",
+       .subtype = "pidf+xml",
+       .supplement_body = pidf_supplement_body,
+};
+
+static int load_module(void)
+{
+       if (ast_sip_pubsub_register_body_supplement(&pidf_supplement)) {
+               return AST_MODULE_LOAD_DECLINE;
+       }
+       return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+       ast_sip_pubsub_unregister_body_supplement(&pidf_supplement);
+       return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP PIDF Digium presence supplement",
+               .load = load_module,
+               .unload = unload_module,
+               .load_pri = AST_MODPRI_CHANNEL_DEPEND,
+);