[asterisk-commits] kharwell: branch kharwell/pimp_sip_state r388722 - in /team/kharwell/pimp_sip...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue May 14 15:29:14 CDT 2013


Author: kharwell
Date: Tue May 14 15:29:13 2013
New Revision: 388722

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=388722
Log:
changed xpidf provider to use pjxpidf.  updated pubsub to check for function callback before calling

Modified:
    team/kharwell/pimp_sip_state/res/res_sip_exten_state.c
    team/kharwell/pimp_sip_state/res/res_sip_providers/res_sip_pidf.c
    team/kharwell/pimp_sip_state/res/res_sip_pubsub.c

Modified: team/kharwell/pimp_sip_state/res/res_sip_exten_state.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/pimp_sip_state/res/res_sip_exten_state.c?view=diff&rev=388722&r1=388721&r2=388722
==============================================================================
--- team/kharwell/pimp_sip_state/res/res_sip_exten_state.c (original)
+++ team/kharwell/pimp_sip_state/res/res_sip_exten_state.c Tue May 14 15:29:13 2013
@@ -486,14 +486,11 @@
 	ast_copy_pj_str(exten_state_sub->exten, &sip_uri->user, sizeof(exten_state_sub->exten));
 
 	if ((exten_state_sub->id = ast_extension_state_add(exten_state_sub->context, exten_state_sub->exten,
-						state_changed, exten_state_sub)) < 0) {
+							   state_changed, exten_state_sub)) < 0) {
 		ast_log(LOG_WARNING, "Unable to subscribe extension %s\n",
 			exten_state_sub->exten);
 		return NULL;
 	}
-
-	/* bump the ref since ast_extension_state_add holds a reference */
-	ao2_ref(exten_state_sub, +1);
 
 	if (add_datastore(exten_state_sub)) {
 		ast_log(LOG_WARNING, "Unable to add to subscription datastore.\n");
@@ -507,6 +504,8 @@
 	}
 
 	send_notify(exten_state_sub, NULL, PJSIP_EVSUB_STATE_ACTIVE);
+	/* bump the ref since ast_extension_state_add holds a reference */
+	ao2_ref(exten_state_sub, +1);
 	return exten_state_sub->sip_sub;
 }
 

Modified: team/kharwell/pimp_sip_state/res/res_sip_providers/res_sip_pidf.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/pimp_sip_state/res/res_sip_providers/res_sip_pidf.c?view=diff&rev=388722&r1=388721&r2=388722
==============================================================================
--- team/kharwell/pimp_sip_state/res/res_sip_providers/res_sip_pidf.c (original)
+++ team/kharwell/pimp_sip_state/res/res_sip_providers/res_sip_pidf.c Tue May 14 15:29:13 2013
@@ -25,11 +25,15 @@
  ***/
 
 #include "asterisk.h"
+
+#include <pjsip.h>
+#include <pjsip_simple.h>
+#include <pjlib.h>
+
+#include "asterisk/res_sip.h"
 #include "asterisk/res_sip_exten_state.h"
 #include "include/res_sip_providers.h"
 
-#include <pjsip_simple.h>
-#include <pjlib.h>
 
 enum state {
 	NOTIFY_OPEN,
@@ -83,57 +87,20 @@
 	}
 }
 
-/*! Factory used to initialize memory allocation pool. */
-static pj_caching_pool *caching_pool = NULL;
-
-/*! Pool used for memory allocation. */
-static pj_pool_t *pool = NULL;
-
-static void pool_destructor(void *obj)
-{
-	struct pj_caching_pool *cp = obj;
-	if (cp) {
-		pj_caching_pool_destroy(cp);
-		caching_pool = NULL;
-	}
-}
-
-static void pool_create(void)
-{
-	caching_pool = ao2_alloc(sizeof(*caching_pool), pool_destructor);
-
-	pj_caching_pool_init(caching_pool,
-			     &pj_pool_factory_default_policy, 0);
-
-	if (!(pool = pj_pool_create(&caching_pool->factory, "pidf",
-				    1024, 1024, NULL))) {
-		ast_log(LOG_WARNING, "Failed to create memory pool for PIDF\n");
-		pj_caching_pool_destroy(caching_pool);
-	}
-}
-
-static void pool_ref(int ref)
-{
-	if (!caching_pool) {
-		pool_create();
-	} else {
-		ao2_ref(caching_pool, ref);
-	}
-}
-
-static void add_attr(pj_xml_node *node, const pj_str_t* name,
-		      const pj_str_t* value)
+static pj_xml_attr *create_attr(pj_pool_t *pool, pj_xml_node *node,
+				const char *name, const char *value)
 {
 	pj_xml_attr *attr = PJ_POOL_ALLOC_T(pool, pj_xml_attr);
 
-	pj_strdup(pool, &attr->name, name);
-	pj_strdup(pool, &attr->value, value);
-	attr->value = *value;
+	pj_strdup2(pool, &attr->name, name);
+	pj_strdup2(pool, &attr->value, value);
 
 	pj_xml_add_attr(node, attr);
-}
-
-static pj_xml_node *create_node(const char* name)
+	return attr;
+}
+
+static pj_xml_node *create_node(pj_pool_t *pool, pj_xml_node *parent,
+				const char* name)
 {
 	pj_xml_node *node = PJ_POOL_ALLOC_T(pool, pj_xml_node);
 
@@ -145,7 +112,26 @@
 	node->content.ptr = NULL;
 	node->content.slen = 0;
 
+	pj_xml_add_node(parent, node);
 	return node;
+}
+
+static pj_xml_attr *find_node_attr(pj_pool_t* pool, pj_xml_node *parent,
+				   const char *node_name, const char *attr_name)
+{
+	pj_str_t name;
+	pj_xml_node *node;
+	pj_xml_attr *attr;
+
+	if (!(node = pj_xml_find_node(parent, pj_cstr(&name, node_name)))) {
+		node = create_node(pool, parent, node_name);
+	}
+
+	if (!(attr = pj_xml_find_attr(node, pj_cstr(&name, attr_name), NULL))) {
+		attr = create_attr(pool, node, attr_name, "");
+	}
+
+	return attr;
 }
 
 /*!
@@ -158,33 +144,36 @@
  * supports this format.
 
  */
-static void add_non_standard(pj_xml_node *node, const char *pidfstate)
-{
-	static const pj_str_t XMLNS_PP = { "xmlns:pp", 8 };
-	static const pj_str_t XMLNS_PERSON = { "urn:ietf:params:xml:ns:pidf:person", 34 };
-
-	static const pj_str_t XMLNS_ES = { "xmlns:es", 8 };
-	static const pj_str_t XMLNS_RPID_STATUS = { "urn:ietf:params:xml:ns:pidf:rpid:status:rpid-status", 51 };
-
-	static const pj_str_t XMLNS_EP = { "xmlns:ep", 8 };
-	static const pj_str_t XMLNS_RPID_PERSON = { "urn:ietf:params:xml:ns:pidf:rpid:rpid-person", 44 };
-
-	pj_xml_node *person = create_node("pp:person");
-	pj_xml_node *status = create_node("status");
+static void add_non_standard(pj_pool_t *pool, pj_xml_node *node, const char *pidfstate)
+{
+	static const char *XMLNS_PP = "xmlns:pp";
+	static const char *XMLNS_PERSON = "urn:ietf:params:xml:ns:pidf:person";
+
+	static const char *XMLNS_ES = "xmlns:es";
+	static const char *XMLNS_RPID_STATUS = "urn:ietf:params:xml:ns:pidf:rpid:status:rpid-status";
+
+	static const char *XMLNS_EP = "xmlns:ep";
+	static const char *XMLNS_RPID_PERSON = "urn:ietf:params:xml:ns:pidf:rpid:rpid-person";
+
+	pj_xml_node *person = create_node(pool, node, "pp:person");
+	pj_xml_node *status = create_node(pool, person, "status");
 
 	if (pidfstate[0] != '-') {
-		pj_xml_node *activities = create_node("ep:activities");
+		pj_xml_node *activities = create_node(pool, status, "ep:activities");
 		pj_strdup2(pool, &activities->content, "ep:");
 		pj_strcat2(&activities->content, pidfstate);
-		pj_xml_add_node(status, activities);
-	}
-
-	add_attr(node, &XMLNS_PP, &XMLNS_PERSON);
-	add_attr(node, &XMLNS_ES, &XMLNS_RPID_STATUS);
-	add_attr(node, &XMLNS_EP, &XMLNS_RPID_PERSON);
-
-	pj_xml_add_node(person, status);
-	pj_xml_add_node(node, person);
+	}
+
+	create_attr(pool, node, XMLNS_PP, XMLNS_PERSON);
+	create_attr(pool, node, XMLNS_ES, XMLNS_RPID_STATUS);
+	create_attr(pool, node, XMLNS_EP, XMLNS_RPID_PERSON);
+}
+
+static void release_pool(void *obj)
+{
+	pj_pool_t *pool = obj;
+
+	pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
 }
 
 static int pidf_xml_create_body(struct ast_sip_exten_state_data *data, const char *local,
@@ -192,12 +181,14 @@
 {
 	pjpidf_pres *pres;
 	pjpidf_tuple *tuple;
-
 	pj_str_t entity, note, id, contact, priority;
-
 	char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
 	int local_state, size;
 
+	RAII_VAR(pj_pool_t *, pool,
+		 pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
+					 "pidf", 1024, 1024), release_pool);
+
 	exten_state_to_str(data->exten_state, &statestring, &pidfstate,
 			   &pidfnote, &local_state);
 
@@ -206,7 +197,7 @@
 		return -1;
 	}
 
-	add_non_standard(pres, pidfstate);
+	add_non_standard(pool, pres, pidfstate);
 
 	if (!pjpidf_pres_add_note(pool, pres, pj_cstr(&note, pidfnote))) {
 		ast_log(LOG_WARNING, "Unable to add note to PIDF presence\n");
@@ -228,7 +219,6 @@
 		ast_log(LOG_WARNING, "PIDF body text too large\n");
 		return -1;
 	}
-
 	*(ast_str_buffer(*body_text) + size) = '\0';
 	ast_str_update(*body_text);
 
@@ -244,39 +234,62 @@
 
 void ast_sip_register_pidf_xml_provider(void)
 {
-	pool_ref(+1);
 	ast_sip_register_exten_state_provider(&pidf_xml_provider);
 }
 
 void ast_sip_unregister_pidf_xml_provider(void)
 {
 	ast_sip_unregister_exten_state_provider(&pidf_xml_provider);
-	pool_ref(-1);
 }
 
 static int xpidf_xml_create_body(struct ast_sip_exten_state_data *data, const char *local,
 				 const char *remote, struct ast_str **body_text)
 {
+	pjxpidf_pres *pres;
+	pj_xml_attr *attr;
+	pj_str_t name;
 	char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
-	int local_state;
+	int local_state, size;
+
+	RAII_VAR(pj_pool_t *, pool,
+		 pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
+					 "pidf", 1024, 1024), release_pool);
 
 	exten_state_to_str(data->exten_state, &statestring, &pidfstate,
 			   &pidfnote, &local_state);
 
-	ast_str_append(body_text, 0,
-		       "<?xml version=\"1.0\"?>\n"
-		       "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n"
-		       "<presence>\n");
-	ast_str_append(body_text, 0, "<presentity uri=\"%s;method=SUBSCRIBE\" />\n", local);
-	ast_str_append(body_text, 0, "<atom id=\"%s\">\n", data->exten);
-	ast_str_append(body_text, 0, "<address uri=\"%s;user=ip\" priority=\"0.800000\">\n", remote);
-	ast_str_append(body_text, 0, "<status status=\"%s\" />\n",
-		       (local_state ==  NOTIFY_OPEN) ? "open" :
-		       (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
-	ast_str_append(body_text, 0, "<msnsubstatus substatus=\"%s\" />\n",
-		       (local_state == NOTIFY_OPEN) ? "online" :
-		       (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
-	ast_str_append(body_text, 0, "</address>\n</atom>\n</presence>\n");
+	if (!(pres = pjxpidf_create(pool, pj_cstr(&name, local)))) {
+		ast_log(LOG_WARNING, "Unable to create PIDF presence\n");
+		return -1;
+	}
+
+	attr = find_node_attr(pool, pres, "atom", "id");
+	pj_strdup2(pool, &attr->value, data->exten);
+
+	attr = find_node_attr(pool, pres, "address", "uri");
+	pj_strdup2(pool, &attr->value, remote);
+	pj_strcat2(&attr->value, ";user=ip");
+	create_attr(pool, pj_xml_find_node(pres, pj_cstr(&name, "address")),
+		    "priority", "0.80000");
+
+	attr = find_node_attr(pool, pres, "status", "status");
+	pj_strdup2(pool, &attr->value,
+		   (local_state ==  NOTIFY_OPEN) ? "open" :
+		   (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
+
+	attr = find_node_attr(pool, pres, "msnsubstatus", "substatus");
+	pj_strdup2(pool, &attr->value,
+		   (local_state == NOTIFY_OPEN) ? "online" :
+		   (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
+
+	if (!(size = pjxpidf_print(pres, ast_str_buffer(*body_text),
+				  ast_str_size(*body_text)))) {
+		ast_log(LOG_WARNING, "XPIDF body text too large\n");
+		return -1;
+	}
+
+	*(ast_str_buffer(*body_text) + size) = '\0';
+	ast_str_update(*body_text);
 
 	return 0;
 }
@@ -290,14 +303,12 @@
 
 void ast_sip_register_xpidf_xml_provider(void)
 {
-	pool_ref(+1);
 	ast_sip_register_exten_state_provider(&xpidf_xml_provider);
 }
 
 void ast_sip_unregister_xpidf_xml_provider(void)
 {
 	ast_sip_unregister_exten_state_provider(&xpidf_xml_provider);
-	pool_ref(-1);
 }
 
 static struct ast_sip_exten_state_provider cpim_pidf_xml_provider = {
@@ -309,12 +320,10 @@
 
 void ast_sip_register_cpim_pidf_xml_provider(void)
 {
-	pool_ref(+1);
 	ast_sip_register_exten_state_provider(&cpim_pidf_xml_provider);
 }
 
 void ast_sip_unregister_cpim_pidf_xml_provider(void)
 {
 	ast_sip_unregister_exten_state_provider(&cpim_pidf_xml_provider);
-	pool_ref(-1);
-}
+}

Modified: team/kharwell/pimp_sip_state/res/res_sip_pubsub.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/pimp_sip_state/res/res_sip_pubsub.c?view=diff&rev=388722&r1=388721&r2=388722
==============================================================================
--- team/kharwell/pimp_sip_state/res/res_sip_pubsub.c (original)
+++ team/kharwell/pimp_sip_state/res/res_sip_pubsub.c Tue May 14 15:29:13 2013
@@ -514,7 +514,8 @@
 		return;
 	}
 
-	if (tsx->role == PJSIP_ROLE_UAC && event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
+	if (sub->handler->notify_response && tsx->role == PJSIP_ROLE_UAC &&
+	    event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
 		sub->handler->notify_response(sub, event->body.tsx_state.src.rdata);
 	}
 }
@@ -598,7 +599,7 @@
 		.status_code = 200,
 	};
 
-	if (!sub|| !sub->handler->notify_request) {
+	if (!sub || !sub->handler->notify_request) {
 		return;
 	}
 




More information about the asterisk-commits mailing list