[asterisk-commits] mmichelson: branch mmichelson/subscription_abstraction r416436 - /team/mmiche...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Jun 16 16:54:14 CDT 2014


Author: mmichelson
Date: Mon Jun 16 16:54:08 2014
New Revision: 416436

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=416436
Log:
Get res_pjsip_exten_state compiling.

Also fixed a memory leak I noticed in res_pjsip_mwi.c


Modified:
    team/mmichelson/subscription_abstraction/res/res_pjsip_exten_state.c
    team/mmichelson/subscription_abstraction/res/res_pjsip_mwi.c

Modified: team/mmichelson/subscription_abstraction/res/res_pjsip_exten_state.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/subscription_abstraction/res/res_pjsip_exten_state.c?view=diff&rev=416436&r1=416435&r2=416436
==============================================================================
--- team/mmichelson/subscription_abstraction/res/res_pjsip_exten_state.c (original)
+++ team/mmichelson/subscription_abstraction/res/res_pjsip_exten_state.c Mon Jun 16 16:54:08 2014
@@ -68,26 +68,24 @@
 #define DEFAULT_PRESENCE_BODY "application/pidf+xml"
 
 static void subscription_shutdown(struct ast_sip_subscription *sub);
-static struct ast_sip_subscription *new_subscribe(struct ast_sip_endpoint *endpoint,
-						  pjsip_rx_data *rdata);
-static void resubscribe(struct ast_sip_subscription *sub, pjsip_rx_data *rdata,
-			struct ast_sip_subscription_response_data *response_data);
-static void subscription_timeout(struct ast_sip_subscription *sub);
-static void subscription_terminated(struct ast_sip_subscription *sub,
-				    pjsip_rx_data *rdata);
+static int new_subscribe(struct ast_sip_endpoint *endpoint, const char *resource);
+static int notify_required(struct ast_sip_subscription *sub,
+		enum ast_sip_subscription_notify_reason reason);
 static void to_ami(struct ast_sip_subscription *sub,
 		   struct ast_str **buf);
+
+struct ast_sip_notifier presence_notifier = {
+	.default_accept = DEFAULT_PRESENCE_BODY,
+	.new_subscribe = new_subscribe,
+	.notify_required = notify_required,
+};
 
 struct ast_sip_subscription_handler presence_handler = {
 	.event_name = "presence",
 	.accept = { DEFAULT_PRESENCE_BODY, },
-	.default_accept = DEFAULT_PRESENCE_BODY,
 	.subscription_shutdown = subscription_shutdown,
-	.new_subscribe = new_subscribe,
-	.resubscribe = resubscribe,
-	.subscription_timeout = subscription_timeout,
-	.subscription_terminated = subscription_terminated,
 	.to_ami = to_ami,
+	.notifier = &presence_notifier,
 };
 
 static void exten_state_subscription_destructor(void *obj)
@@ -98,25 +96,6 @@
 	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
@@ -131,86 +110,35 @@
  * Creates the underlying SIP subscription for the given request. First makes
  * sure that there are registered handler and provider objects available.
  */
-static struct exten_state_subscription *exten_state_subscription_alloc(
-	struct ast_sip_endpoint *endpoint, enum ast_sip_subscription_role role, pjsip_rx_data *rdata)
-{
-	RAII_VAR(struct exten_state_subscription *, exten_state_sub,
-		 ao2_alloc(sizeof(*exten_state_sub), exten_state_subscription_destructor), ao2_cleanup);
-
+static struct exten_state_subscription *exten_state_subscription_alloc(struct ast_sip_endpoint *endpoint)
+{
+	struct exten_state_subscription * exten_state_sub;
+	
+	exten_state_sub = ao2_alloc(sizeof(*exten_state_sub), exten_state_subscription_destructor);
 	if (!exten_state_sub) {
 		return NULL;
 	}
 
-	if (!(exten_state_sub->sip_sub = ast_sip_create_subscription(
-		      &presence_handler, role, endpoint, rdata))) {
-		ast_log(LOG_WARNING, "Unable to create SIP subscription for endpoint %s\n",
-			ast_sorcery_object_get_id(endpoint));
-		return NULL;
-	}
-
 	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);
+	/* XXX TODO Previously, user agent was reaped from the rdata here and stored on the
+	 * exten_state_sub. This was useful for determining if the phone being communicated with
+	 * is a Digium phone.
+	 *
+	 * This needs to be made available somehow. Maybe it could just be on the base
+	 * ast_sip_subscription structure? Seems a bit specialized though.
+	 */
 	return exten_state_sub;
 }
 
 /*!
  * \internal
- * \brief Create and send a NOTIFY request to the subscriber.
- */
-static void create_send_notify(struct exten_state_subscription *exten_state_sub, const char *reason,
-			       pjsip_evsub_state evsub_state, struct ast_sip_exten_state_data *exten_state_data)
-{
-	RAII_VAR(struct ast_str *, body_text, ast_str_create(BODY_SIZE), ast_free_ptr);
-	pj_str_t reason_str;
-	const pj_str_t *reason_str_ptr = NULL;
-	pjsip_tx_data *tdata;
-	struct ast_sip_body body;
-
-	body.type = ast_sip_subscription_get_body_type(exten_state_sub->sip_sub);
-	body.subtype = ast_sip_subscription_get_body_subtype(exten_state_sub->sip_sub);
-
-	if (ast_sip_pubsub_generate_body_content(body.type, body.subtype,
-				exten_state_data, &body_text)) {
-		ast_log(LOG_ERROR, "Unable to create body on NOTIFY request\n");
-		return;
-	}
-
-	body.body_text = ast_str_buffer(body_text);
-
-	if (reason) {
-		pj_cstr(&reason_str, reason);
-		reason_str_ptr = &reason_str;
-	}
-
-	if (pjsip_evsub_notify(ast_sip_subscription_get_evsub(exten_state_sub->sip_sub),
-			      evsub_state, NULL, reason_str_ptr, &tdata) != PJ_SUCCESS) {
-		ast_log(LOG_WARNING, "Unable to create NOTIFY request\n");
-		return;
-	}
-
-	if (ast_sip_add_body(tdata, &body)) {
-		ast_log(LOG_WARNING, "Unable to add body to NOTIFY request\n");
-		pjsip_tx_data_dec_ref(tdata);
-		return;
-	}
-
-	if (ast_sip_subscription_send_request(exten_state_sub->sip_sub, tdata) != PJ_SUCCESS) {
-		ast_log(LOG_WARNING, "Unable to send NOTIFY request\n");
-	}
-}
-
-/*!
- * \internal
  * \brief Get device state information and send notification to the subscriber.
  */
-static void send_notify(struct exten_state_subscription *exten_state_sub, const char *reason,
-	pjsip_evsub_state evsub_state)
+static void send_notify(struct exten_state_subscription *exten_state_sub)
 {
 	RAII_VAR(struct ao2_container*, info, NULL, ao2_cleanup);
 	char *subtype = NULL, *message = NULL;
-	pjsip_dialog *dlg;
 	struct ast_sip_exten_state_data exten_state_data = {
 		.exten = exten_state_sub->exten,
 		.presence_state = ast_hint_presence_state(NULL, exten_state_sub->context,
@@ -220,11 +148,10 @@
 		.user_agent = exten_state_sub->user_agent
 	};
 
-	dlg = ast_sip_subscription_get_dlg(exten_state_sub->sip_sub);
-	ast_copy_pj_str(exten_state_data.local, &dlg->local.info_str,
-			sizeof(exten_state_data.local));
-	ast_copy_pj_str(exten_state_data.remote, &dlg->remote.info_str,
-			sizeof(exten_state_data.remote));
+	ast_sip_subscription_get_local_uri(exten_state_sub->sip_sub,
+			exten_state_data.local, sizeof(exten_state_data.local));
+	ast_sip_subscription_get_remote_uri(exten_state_sub->sip_sub,
+			exten_state_data.remote, sizeof(exten_state_data.remote));
 
 	if ((exten_state_data.exten_state = ast_extension_state_extended(
 		     NULL, exten_state_sub->context, exten_state_sub->exten, &info)) < 0) {
@@ -238,14 +165,13 @@
 			"exten_state", 1024, 1024);
 
 	exten_state_data.device_state_info = info;
-	create_send_notify(exten_state_sub, reason, evsub_state, &exten_state_data);
+	ast_sip_subscription_notify(exten_state_sub->sip_sub, &exten_state_data);
 	pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), exten_state_data.pool);
 }
 
 struct notify_task_data {
 	struct ast_sip_exten_state_data exten_state_data;
 	struct exten_state_subscription *exten_state_sub;
-	pjsip_evsub_state evsub_state;
 };
 
 static void notify_task_data_destructor(void *obj)
@@ -264,14 +190,12 @@
 {
 	struct notify_task_data *task_data =
 		ao2_alloc(sizeof(*task_data), notify_task_data_destructor);
-	struct pjsip_dialog *dlg;
 
 	if (!task_data) {
 		ast_log(LOG_WARNING, "Unable to create notify task data\n");
 		return NULL;
 	}
 
-	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;
@@ -289,15 +213,13 @@
 		ao2_ref(task_data->exten_state_data.device_state_info, +1);
 	}
 
-	dlg = ast_sip_subscription_get_dlg(exten_state_sub->sip_sub);
-	ast_copy_pj_str(task_data->exten_state_data.local, &dlg->local.info_str,
-			sizeof(task_data->exten_state_data.local));
-	ast_copy_pj_str(task_data->exten_state_data.remote, &dlg->remote.info_str,
-			sizeof(task_data->exten_state_data.remote));
+	ast_sip_subscription_get_local_uri(exten_state_sub->sip_sub,
+			task_data->exten_state_data.local, sizeof(task_data->exten_state_data.local));
+	ast_sip_subscription_get_remote_uri(exten_state_sub->sip_sub,
+			task_data->exten_state_data.remote, sizeof(task_data->exten_state_data.remote));
 
 	if ((info->exten_state == AST_EXTENSION_DEACTIVATED) ||
 	    (info->exten_state == AST_EXTENSION_REMOVED)) {
-		task_data->evsub_state = PJSIP_EVSUB_STATE_TERMINATED;
 		ast_log(LOG_WARNING, "Watcher for hint %s %s\n", exten, info->exten_state
 			 == AST_EXTENSION_REMOVED ? "removed" : "deactivated");
 	}
@@ -313,9 +235,7 @@
 	task_data->exten_state_data.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
 			"exten_state", 1024, 1024);
 
-	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);
+	ast_sip_subscription_notify(task_data->exten_state_sub->sip_sub, &task_data->exten_state_data);
 
 	pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(),
 			task_data->exten_state_data.pool);
@@ -407,24 +327,30 @@
 	ao2_cleanup(exten_state_sub);
 }
 
-static struct ast_sip_subscription *new_subscribe(struct ast_sip_endpoint *endpoint,
-						  pjsip_rx_data *rdata)
-{
-	pjsip_uri *uri = rdata->msg_info.msg->line.req.uri;
-	pjsip_sip_uri *sip_uri = pjsip_uri_get_uri(uri);
-	RAII_VAR(struct exten_state_subscription *, exten_state_sub, NULL, ao2_cleanup);
-
-	if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri)) {
-		ast_log(LOG_WARNING, "Attempt to SUBSCRIBE to a non-SIP URI\n");
-		return NULL;
-	}
-
-	if (!(exten_state_sub = exten_state_subscription_alloc(endpoint, AST_SIP_NOTIFIER, rdata))) {
-		return NULL;
+static int new_subscribe(struct ast_sip_endpoint *endpoint,
+		const char *resource)
+{
+	if (!ast_exists_extension(NULL, endpoint->context, resource, PRIORITY_HINT, NULL)) {
+		ast_log(LOG_WARNING, "Extension %s does not exist or has no associated hint\n", resource);
+		return 404;
+	}
+
+	return 200;
+}
+
+static int initial_subscribe(struct ast_sip_subscription *sip_sub)
+{
+	struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sip_sub);
+	const char *resource = ast_sip_subscription_get_resource_name(sip_sub);
+	struct exten_state_subscription *exten_state_sub;
+
+	if (!(exten_state_sub = exten_state_subscription_alloc(endpoint))) {
+		ao2_cleanup(endpoint);
+		return -1;
 	}
 
 	ast_copy_string(exten_state_sub->context, endpoint->context, sizeof(exten_state_sub->context));
-	ast_copy_pj_str(exten_state_sub->exten, &sip_uri->user, sizeof(exten_state_sub->exten));
+	ast_copy_string(exten_state_sub->exten, resource, sizeof(exten_state_sub->exten));
 
 	if ((exten_state_sub->id = ast_extension_state_add_destroy_extended(
 		     exten_state_sub->context, exten_state_sub->exten,
@@ -432,8 +358,9 @@
 		ast_log(LOG_WARNING, "Unable to subscribe endpoint '%s' to extension '%s@%s'\n",
 			ast_sorcery_object_get_id(endpoint), exten_state_sub->exten,
 			exten_state_sub->context);
-		pjsip_evsub_terminate(ast_sip_subscription_get_evsub(exten_state_sub->sip_sub), PJ_FALSE);
-		return NULL;
+		ao2_cleanup(endpoint);
+		ao2_cleanup(exten_state_sub);
+		return -1;
 	}
 
 	/* bump the ref since ast_extension_state_add holds a reference */
@@ -441,55 +368,39 @@
 
 	if (add_datastore(exten_state_sub)) {
 		ast_log(LOG_WARNING, "Unable to add to subscription datastore.\n");
-		pjsip_evsub_terminate(ast_sip_subscription_get_evsub(exten_state_sub->sip_sub), PJ_FALSE);
-		return NULL;
-	}
-
-	if (ast_sip_subscription_accept(exten_state_sub->sip_sub, rdata, 200)) {
-		ast_log(LOG_WARNING, "Unable to accept the incoming extension state subscription.\n");
-		pjsip_evsub_terminate(ast_sip_subscription_get_evsub(exten_state_sub->sip_sub), PJ_FALSE);
-		return NULL;
-	}
-
-	send_notify(exten_state_sub, NULL, PJSIP_EVSUB_STATE_ACTIVE);
-	return exten_state_sub->sip_sub;
-}
-
-static void resubscribe(struct ast_sip_subscription *sub, pjsip_rx_data *rdata,
-			struct ast_sip_subscription_response_data *response_data)
-{
-	struct exten_state_subscription *exten_state_sub = get_exten_state_sub(sub);
-
-	if (!exten_state_sub) {
-		return;
-	}
-
-	send_notify(exten_state_sub, NULL, PJSIP_EVSUB_STATE_ACTIVE);
-}
-
-static void subscription_timeout(struct ast_sip_subscription *sub)
-{
-	struct exten_state_subscription *exten_state_sub = get_exten_state_sub(sub);
-
-	if (!exten_state_sub) {
-		return;
-	}
-
-	ast_verbose(VERBOSE_PREFIX_3 "Subscription has timed out.\n");
-	send_notify(exten_state_sub, "timeout", PJSIP_EVSUB_STATE_TERMINATED);
-}
-
-static void subscription_terminated(struct ast_sip_subscription *sub,
-				    pjsip_rx_data *rdata)
-{
-	struct exten_state_subscription *exten_state_sub = get_exten_state_sub(sub);
-
-	if (!exten_state_sub) {
-		return;
-	}
-
-	ast_verbose(VERBOSE_PREFIX_3 "Subscription has been terminated.\n");
-	send_notify(exten_state_sub, NULL, PJSIP_EVSUB_STATE_TERMINATED);
+		ao2_cleanup(endpoint);
+		ao2_cleanup(exten_state_sub);
+		return -1;
+	}
+
+	send_notify(exten_state_sub);
+	ao2_cleanup(exten_state_sub);
+	ao2_cleanup(endpoint);
+	return 0;
+}
+
+static int notify_required(struct ast_sip_subscription *sub,
+		enum ast_sip_subscription_notify_reason reason)
+{
+	struct exten_state_subscription *exten_state_sub;
+
+	switch (reason) {
+	case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED:
+		return initial_subscribe(sub);
+	case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_RENEWED:
+	case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_TERMINATED:
+	case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_OTHER:
+		exten_state_sub = get_exten_state_sub(sub);
+
+		if (!exten_state_sub) {
+			return -1;
+		}
+
+		send_notify(exten_state_sub);
+		break;
+	}
+
+	return 0;
 }
 
 static void to_ami(struct ast_sip_subscription *sub,

Modified: team/mmichelson/subscription_abstraction/res/res_pjsip_mwi.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/subscription_abstraction/res/res_pjsip_mwi.c?view=diff&rev=416436&r1=416435&r2=416436
==============================================================================
--- team/mmichelson/subscription_abstraction/res/res_pjsip_mwi.c (original)
+++ team/mmichelson/subscription_abstraction/res/res_pjsip_mwi.c Mon Jun 16 16:54:08 2014
@@ -690,12 +690,14 @@
 	}
 
 	if (!sub) {
+		ao2_cleanup(endpoint);
 		return -1;
 	}
 
 	send_mwi_notify(sub);
+
 	ao2_cleanup(sub);
-
+	ao2_cleanup(endpoint);
 	return 0;
 }
 




More information about the asterisk-commits mailing list