[asterisk-commits] file: branch 12 r418116 - in /branches/12: include/asterisk/ res/ res/res_pjsip/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Jul 7 11:05:51 CDT 2014


Author: file
Date: Mon Jul  7 11:05:44 2014
New Revision: 418116

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=418116
Log:
res_pjsip_dialog_info_body_generator: Add dialog-info+xml support for presence.

This module implements dialog-info+xml for the purposes of presence. This means
that phones such as Grandstreams can now subscribe to receive presence information
for an extension.

ASTERISK-21443 #close
Reported by: Matt Jordan

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

Added:
    branches/12/res/res_pjsip_dialog_info_body_generator.c   (with props)
Modified:
    branches/12/include/asterisk/res_pjsip_body_generator_types.h
    branches/12/include/asterisk/res_pjsip_presence_xml.h
    branches/12/res/res_pjsip/presence_xml.c
    branches/12/res/res_pjsip_exten_state.c

Modified: branches/12/include/asterisk/res_pjsip_body_generator_types.h
URL: http://svnview.digium.com/svn/asterisk/branches/12/include/asterisk/res_pjsip_body_generator_types.h?view=diff&rev=418116&r1=418115&r2=418116
==============================================================================
--- branches/12/include/asterisk/res_pjsip_body_generator_types.h (original)
+++ branches/12/include/asterisk/res_pjsip_body_generator_types.h Mon Jul  7 11:05:44 2014
@@ -28,6 +28,7 @@
  * \li application/pidf+xml
  * \li application/xpidf+xml
  * \li application/cpim-pidf+xml
+ * \li application/dialog-info+xml
  */
 struct ast_sip_exten_state_data {
 	/*! The extension of the current state change */
@@ -48,6 +49,8 @@
 	char local[PJSIP_MAX_URL_SIZE];
 	/*! Remote dialog URI */
 	char remote[PJSIP_MAX_URL_SIZE];
+	/*! Optional subscription */
+	struct ast_sip_subscription *sub;
 	/*! Allocation pool */
 	pj_pool_t *pool;
 };

Modified: branches/12/include/asterisk/res_pjsip_presence_xml.h
URL: http://svnview.digium.com/svn/asterisk/branches/12/include/asterisk/res_pjsip_presence_xml.h?view=diff&rev=418116&r1=418115&r2=418116
==============================================================================
--- branches/12/include/asterisk/res_pjsip_presence_xml.h (original)
+++ branches/12/include/asterisk/res_pjsip_presence_xml.h Mon Jul  7 11:05:44 2014
@@ -72,7 +72,7 @@
  * \brief Create XML node
  *
  * \param pool Allocation pool
- * \param parent Node that will be parent to the created node
+ * \param parent Optional node that will be parent to the created node
  * \param name The name for the new node
  * \return The created node
  */

Modified: branches/12/res/res_pjsip/presence_xml.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_pjsip/presence_xml.c?view=diff&rev=418116&r1=418115&r2=418116
==============================================================================
--- branches/12/res/res_pjsip/presence_xml.c (original)
+++ branches/12/res/res_pjsip/presence_xml.c Mon Jul  7 11:05:44 2014
@@ -152,7 +152,10 @@
 	node->content.ptr = NULL;
 	node->content.slen = 0;
 
-	pj_xml_add_node(parent, node);
+	if (parent) {
+		pj_xml_add_node(parent, node);
+	}
+
 	return node;
 }
 

Added: branches/12/res/res_pjsip_dialog_info_body_generator.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_pjsip_dialog_info_body_generator.c?view=auto&rev=418116
==============================================================================
--- branches/12/res/res_pjsip_dialog_info_body_generator.c (added)
+++ branches/12/res/res_pjsip_dialog_info_body_generator.c Mon Jul  7 11:05:44 2014
@@ -1,0 +1,215 @@
+/*
+ * asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2014, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at 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>
+	<depend>res_pjsip_exten_state</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/res_pjsip.h"
+#include "asterisk/res_pjsip_pubsub.h"
+#include "asterisk/res_pjsip_presence_xml.h"
+#include "asterisk/res_pjsip_body_generator_types.h"
+
+/*! \brief Structure which contains dialog-info+xml state information */
+struct dialog_info_xml_state {
+	/*! \brief Version to place into the next NOTIFY */
+	unsigned int version;
+};
+
+/*! \brief Destructor for dialog-info+xml information */
+static void dialog_info_xml_state_destroy(void *obj)
+{
+	ast_free(obj);
+}
+
+/*! \brief Datastore for attaching dialog-info+xml state information */
+static const struct ast_datastore_info dialog_info_xml_datastore = {
+	.type = "dialog-info+xml",
+	.destroy = dialog_info_xml_state_destroy,
+};
+
+static void *dialog_info_allocate_body(void *data)
+{
+	struct ast_sip_exten_state_data *state_data = data;
+
+	return ast_sip_presence_xml_create_node(state_data->pool, NULL, "dialog-info");
+}
+
+static struct ast_datastore *dialog_info_xml_state_find_or_create(struct ast_sip_subscription *sub)
+{
+	struct ast_datastore *datastore = ast_sip_subscription_get_datastore(sub, "dialog-info+xml");
+
+	if (datastore) {
+		return datastore;
+	}
+
+	datastore = ast_sip_subscription_alloc_datastore(&dialog_info_xml_datastore, "dialog-info+xml");
+	if (!datastore) {
+		return NULL;
+	}
+	datastore->data = ast_calloc(1, sizeof(struct dialog_info_xml_state));
+	if (!datastore->data || ast_sip_subscription_add_datastore(sub, datastore)) {
+		ao2_ref(datastore, -1);
+		return NULL;
+	}
+
+	return datastore;
+}
+
+static unsigned int dialog_info_xml_get_version(struct ast_sip_subscription *sub, unsigned int *version)
+{
+	struct ast_datastore *datastore = dialog_info_xml_state_find_or_create(sub);
+	struct dialog_info_xml_state *state;
+
+	if (!datastore) {
+		return -1;
+	}
+
+	state = datastore->data;
+	*version = state->version++;
+	ao2_ref(datastore, -1);
+
+	return 0;
+}
+
+static int dialog_info_generate_body_content(void *body, void *data)
+{
+	pj_xml_node *dialog_info = body, *dialog, *state;
+	struct ast_sip_exten_state_data *state_data = data;
+	char *local = ast_strdupa(state_data->local), *stripped, *statestring = NULL;
+	char *pidfstate = NULL, *pidfnote = NULL;
+	enum ast_sip_pidf_state local_state;
+	unsigned int version;
+	char version_str[32], sanitized[PJSIP_MAX_URL_SIZE];
+
+	if (!local || !state_data->sub) {
+		return -1;
+	}
+
+	if (dialog_info_xml_get_version(state_data->sub, &version)) {
+		ast_log(LOG_WARNING, "dialog-info+xml version could not be retrieved from datastore\n");
+		return -1;
+	}
+
+	stripped = ast_strip_quoted(local, "<", ">");
+	ast_sip_sanitize_xml(stripped, sanitized, sizeof(sanitized));
+
+	ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
+			&pidfstate, &pidfnote, &local_state);
+
+	ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "xmlns", "urn:ietf:params:xml:ns:dialog-info");
+
+	snprintf(version_str, sizeof(version_str), "%u", version);
+	ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "version", version_str);
+
+	ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "state", "full");
+	ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "entity", sanitized);
+
+	dialog = ast_sip_presence_xml_create_node(state_data->pool, dialog_info, "dialog");
+	ast_sip_presence_xml_create_attr(state_data->pool, dialog, "id", state_data->exten);
+
+	state = ast_sip_presence_xml_create_node(state_data->pool, dialog, "state");
+	pj_strdup2(state_data->pool, &state->content, statestring);
+
+	if (state_data->exten_state == AST_EXTENSION_ONHOLD) {
+		pj_xml_node *local_node, *target, *param;
+
+		local_node = ast_sip_presence_xml_create_node(state_data->pool, dialog, "local");
+		target = ast_sip_presence_xml_create_node(state_data->pool, local_node, "target");
+		ast_sip_presence_xml_create_attr(state_data->pool, target, "uri", sanitized);
+		param = ast_sip_presence_xml_create_node(state_data->pool, target, "param");
+		ast_sip_presence_xml_create_attr(state_data->pool, param, "pname", "+sip.rendering");
+		ast_sip_presence_xml_create_attr(state_data->pool, param, "pvalue", "no");
+	}
+
+	return 0;
+}
+
+/* The maximum number of times the ast_str() for the body text can grow before we declare an XML body
+ * too large to send.
+ */
+#define MAX_STRING_GROWTHS 3
+
+/* When having pj_xml_print add the XML prolog to the output body the function will return 39
+ * instead of -1 if the rest of the document can not be printed into the body.
+ */
+#define XML_PROLOG 39
+
+static void dialog_info_to_string(void *body, struct ast_str **str)
+{
+	pj_xml_node *dialog_info = body;
+	int growths = 0;
+	int size;
+
+	do {
+		size = pj_xml_print(dialog_info, ast_str_buffer(*str), ast_str_size(*str), PJ_TRUE);
+		if (size == XML_PROLOG) {
+			ast_str_make_space(str, ast_str_size(*str) * 2);
+			++growths;
+		}
+	} while (size == XML_PROLOG && growths < MAX_STRING_GROWTHS);
+
+	if (size == XML_PROLOG) {
+		ast_log(LOG_WARNING, "dialog-info+xml body text too large\n");
+		return;
+	}
+
+	*(ast_str_buffer(*str) + size) = '\0';
+	ast_str_update(*str);
+}
+
+static struct ast_sip_pubsub_body_generator dialog_info_body_generator = {
+	.type = "application",
+	.subtype = "dialog-info+xml",
+	.allocate_body = dialog_info_allocate_body,
+	.generate_body_content = dialog_info_generate_body_content,
+	.to_string = dialog_info_to_string,
+	/* No need for a destroy_body callback since we use a pool */
+};
+
+static int load_module(void)
+{
+	if (ast_sip_pubsub_register_body_generator(&dialog_info_body_generator)) {
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+	ast_sip_pubsub_unregister_body_generator(&dialog_info_body_generator);
+	return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State Dialog Info+XML Provider",
+		.load = load_module,
+		.unload = unload_module,
+		.load_pri = AST_MODPRI_CHANNEL_DEPEND,
+);

Propchange: branches/12/res/res_pjsip_dialog_info_body_generator.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: branches/12/res/res_pjsip_dialog_info_body_generator.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: branches/12/res/res_pjsip_dialog_info_body_generator.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: branches/12/res/res_pjsip_exten_state.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_pjsip_exten_state.c?view=diff&rev=418116&r1=418115&r2=418116
==============================================================================
--- branches/12/res/res_pjsip_exten_state.c (original)
+++ branches/12/res/res_pjsip_exten_state.c Mon Jul  7 11:05:44 2014
@@ -66,6 +66,7 @@
 };
 
 #define DEFAULT_PRESENCE_BODY "application/pidf+xml"
+#define DEFAULT_DIALOG_BODY "application/dialog-info+xml"
 
 static void subscription_shutdown(struct ast_sip_subscription *sub);
 static struct ast_sip_subscription *new_subscribe(struct ast_sip_endpoint *endpoint,
@@ -90,6 +91,18 @@
 	.to_ami = to_ami,
 };
 
+struct ast_sip_subscription_handler dialog_handler = {
+	.event_name = "dialog",
+	.accept = { DEFAULT_DIALOG_BODY, },
+	.default_accept = DEFAULT_DIALOG_BODY,
+	.subscription_shutdown = subscription_shutdown,
+	.new_subscribe = new_subscribe,
+	.resubscribe = resubscribe,
+	.subscription_timeout = subscription_timeout,
+	.subscription_terminated = subscription_terminated,
+	.to_ami = to_ami,
+};
+
 static void exten_state_subscription_destructor(void *obj)
 {
 	struct exten_state_subscription *sub = obj;
@@ -217,6 +230,7 @@
 							  exten_state_sub->exten, &subtype, &message),
 		.presence_subtype = subtype,
 		.presence_message = message,
+		.sub = exten_state_sub->sip_sub,
 		.user_agent = exten_state_sub->user_agent
 	};
 
@@ -313,6 +327,8 @@
 	task_data->exten_state_data.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
 			"exten_state", 1024, 1024);
 
+	task_data->exten_state_data.sub = task_data->exten_state_sub->sip_sub;
+
 	create_send_notify(task_data->exten_state_sub, task_data->evsub_state ==
 			   PJSIP_EVSUB_STATE_TERMINATED ? "noresource" : NULL,
 			   task_data->evsub_state, &task_data->exten_state_data);
@@ -511,11 +527,20 @@
 			presence_handler.event_name);
 		return AST_MODULE_LOAD_DECLINE;
 	}
+
+	if (ast_sip_register_subscription_handler(&dialog_handler)) {
+		ast_log(LOG_WARNING, "Unable to register subscription handler %s\n",
+			dialog_handler.event_name);
+		ast_sip_unregister_subscription_handler(&presence_handler);
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
 	return AST_MODULE_LOAD_SUCCESS;
 }
 
 static int unload_module(void)
 {
+	ast_sip_unregister_subscription_handler(&dialog_handler);
 	ast_sip_unregister_subscription_handler(&presence_handler);
 	return 0;
 }




More information about the asterisk-commits mailing list