[asterisk-commits] mmichelson: trunk r417233 - in /trunk: include/asterisk/ res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Jun 25 15:57:33 CDT 2014


Author: mmichelson
Date: Wed Jun 25 15:57:28 2014
New Revision: 417233

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=417233
Log:
Abstract PJSIP-specific elements from the pubsub API.

This helps to pave the way for RLS work that is to come.
Since this is a self-contained change and subscription
tests still pass, this work is being committed directly
to trunk instead of a working branch.

ASTERISK-23865 #close
Review: https://reviewboard.asterisk.org/r/3628


Modified:
    trunk/include/asterisk/res_pjsip_pubsub.h
    trunk/res/res_pjsip_exten_state.c
    trunk/res/res_pjsip_mwi.c
    trunk/res/res_pjsip_pidf_body_generator.c
    trunk/res/res_pjsip_pubsub.c
    trunk/res/res_pjsip_pubsub.exports.in
    trunk/res/res_pjsip_xpidf_body_generator.c

Modified: trunk/include/asterisk/res_pjsip_pubsub.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/res_pjsip_pubsub.h?view=diff&rev=417233&r1=417232&r2=417233
==============================================================================
--- trunk/include/asterisk/res_pjsip_pubsub.h (original)
+++ trunk/include/asterisk/res_pjsip_pubsub.h Wed Jun 25 15:57:28 2014
@@ -34,6 +34,15 @@
  */
 struct ast_sip_publication;
 
+enum ast_sip_publish_state {
+    /*! Publication has just been initialized */
+    AST_SIP_PUBLISH_STATE_INITIALIZED,
+    /*! Publication is currently active */
+    AST_SIP_PUBLISH_STATE_ACTIVE,
+    /*! Publication has been terminated */
+    AST_SIP_PUBLISH_STATE_TERMINATED,
+};
+
 /*!
  * \brief Callbacks that publication handlers will define
  */
@@ -47,60 +56,38 @@
 	/*!
 	 * \brief Called when a PUBLISH to establish a new publication arrives.
 	 *
-	 * \param endpoint The endpoint from whom the PUBLISH arrived
-	 * \param rdata The PUBLISH request
-	 * \retval NULL PUBLISH was not accepted
-	 * \retval non-NULL New publication
-	 *
-	 * \note The callback is expected to send a response for the PUBLISH in success cases.
-	 */
-	struct ast_sip_publication *(*new_publication)(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata);
-
-	/*!
-	 * \brief Called when a PUBLISH for an existing publication arrives.
-	 *
-	 * This PUBLISH may be intending to change state or it may be simply renewing
-	 * the publication since the publication is nearing expiration. The callback
-	 * is expected to send a response to the PUBLISH.
-	 *
-	 * \param pub The publication on which the PUBLISH arrived
-	 * \param rdata The PUBLISH request
-	 * \retval 0 Publication was accepted
-	 * \retval non-zero Publication was denied
-	 *
-	 * \note The callback is expected to send a response for the PUBLISH.
-	 */
-	int (*publish_refresh)(struct ast_sip_publication *pub, pjsip_rx_data *rdata);
-
+	 * \param endpoint The endpoint from whom the PUBLISH arrived.
+	 * \param resource The resource whose state is being published.
+	 * \return Response code for the incoming PUBLISH
+	 */
+	int (*new_publication)(struct ast_sip_endpoint *endpoint, const char *resource);
 	/*!
 	 * \brief Called when a publication has reached its expiration.
 	 */
 	void (*publish_expire)(struct ast_sip_publication *pub);
-
-	/*!
-	 * \brief Called when a PUBLISH arrives to terminate a publication.
-	 *
-	 * \param pub The publication that is terminating
-	 * \param rdata The PUBLISH request terminating the publication
-	 *
-	 * \note The callback is expected to send a response for the PUBLISH.
-	 */
-	void (*publish_termination)(struct ast_sip_publication *pub, pjsip_rx_data *rdata);
-
+	/*!
+	 * \brief Published resource has changed states.
+	 *
+	 * The state parameter can be used to take further action. For instance,
+	 * if the state is AST_SIP_PUBLISH_STATE_INITIALIZED, then this is the initial
+	 * PUBLISH request. This is a good time to set up datastores on the publication
+	 * or any other initial needs.
+	 *
+	 * AST_SIP_PUBLISH_STATE_TERMINATED is used when the remote end is terminating
+	 * its publication. This is a good opportunity to free any resources associated with
+	 * the publication.
+	 *
+	 * AST_SIP_PUBLISH_STATE_ACTIVE is used when a publication that modifies state
+	 * arrives.
+	 *
+	 * \param pub The publication whose state has changed
+	 * \param body The body of the inbound PUBLISH
+	 * \param state The state of the publication
+	 */
+	int (*publication_state_change)(struct ast_sip_publication *pub, pjsip_msg_body *body,
+			enum ast_sip_publish_state state);
 	AST_LIST_ENTRY(ast_sip_publish_handler) next;
 };
-
-/*!
- * \brief Create a new publication
- *
- * Publication handlers should call this when a PUBLISH arrives to establish a new publication.
- *
- * \param endpoint The endpoint from whom the PUBLISHes arrive
- * \param rdata The PUBLISH that established the publication
- * \retval NULL Failed to create a publication
- * \retval non-NULL The newly-created publication
- */
-struct ast_sip_publication *ast_sip_create_publication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata);
 
 /*!
  * \brief Given a publication, get the associated endpoint
@@ -110,29 +97,6 @@
  * \retval non-NULL The associated endpoint
  */
 struct ast_sip_endpoint *ast_sip_publication_get_endpoint(struct ast_sip_publication *pub);
-
-/*!
- * \brief Create a response to an inbound PUBLISH
- *
- * The created response must be sent using ast_sip_publication_send_response
- *
- * \param pub The publication
- * \param status code The status code to place in the response
- * \param rdata The request to which the response is being made
- * \param[out] tdata The created response
- */
-int ast_sip_publication_create_response(struct ast_sip_publication *pub, int status_code, pjsip_rx_data *rdata,
-	pjsip_tx_data **tdata);
-
-/*!
- * \brief Send a response for an inbound PUBLISH
- *
- * \param pub The publication
- * \param rdata The request to which the response was made
- * \param tdata The response to the request
- */
-pj_status_t ast_sip_publication_send_response(struct ast_sip_publication *pub, pjsip_rx_data *rdata,
-	pjsip_tx_data *tdata);
 
 /*!
  * \brief Register a publish handler
@@ -222,19 +186,20 @@
 };
 
 #define AST_SIP_MAX_ACCEPT 32
-
-struct ast_sip_subscription_handler {
-	/*! The name of the event this handler deals with */
-	const char *event_name;
-	/*! The types of body this handler accepts.
-	 *
-	 * \note This option has no bearing when the handler is used in the
-	 * notifier role. When in a subscriber role, this header is used to
-	 * populate the Accept: header of an outbound SUBSCRIBE request
-	 */
-	const char *accept[AST_SIP_MAX_ACCEPT];
-	/*!
-	 * \brief Default body type defined for the event package this handler handles.
+enum ast_sip_subscription_notify_reason {
+	/*! Initial NOTIFY for subscription */
+	AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED,
+	/*! Subscription has been renewed */
+	AST_SIP_SUBSCRIPTION_NOTIFY_REASON_RENEWED,
+	/*! Subscription is being terminated */
+	AST_SIP_SUBSCRIPTION_NOTIFY_REASON_TERMINATED,
+	/*! Other unspecified reason */
+	AST_SIP_SUBSCRIPTION_NOTIFY_REASON_OTHER
+};
+
+struct ast_sip_notifier {
+	/*!
+	 * \brief Default body type defined for the event package this notifier handles.
 	 *
 	 * Typically, a SUBSCRIBE request will contain one or more Accept headers that tell
 	 * what format they expect the body of NOTIFY requests to use. However, every event
@@ -243,163 +208,96 @@
 	 */
 	const char *default_accept;
 	/*!
+	 * \brief Called when a SUBSCRIBE arrives attempting to establish a new subscription.
+	 *
+	 * The notifier is expected to return the response that should be sent to the
+	 * SUBSCRIBE request.
+	 *
+	 * If a 200-class response is returned, then the notifier's notify_required
+	 * callback will immediately be called into with a reason of
+	 * AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED.
+	 *
+	 * \param endpoint The endpoint from which we received the SUBSCRIBE
+	 * \param resource The name of the resource to which the subscription is being made
+	 * \return The response code to send to the SUBSCRIBE.
+	 */
+	int (*new_subscribe)(struct ast_sip_endpoint *endpoint, const char *resource);
+	/*!
+	 * \brief The subscription is in need of a NOTIFY request.
+	 *
+	 * A reason of AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED is given immediately
+	 * after a SUBSCRIBE is accepted. This is a good opportunity for the notifier to
+	 * perform setup duties such as establishing Stasis subscriptions or adding
+	 * datastores to the subscription.
+	 *
+	 * A reason of AST_SIP_SUBSCRIPTION_NOTIFY_REASON_TERMINATED is given when the
+	 * subscriber has terminated the subscription. If there are any duties that the
+	 *
+	 *
+	 * \param sub The subscription to send the NOTIFY on.
+	 * \param reason The reason why the NOTIFY is being sent.
+	 * \retval 0 Success
+	 * \retval -1 Failure
+	 */
+	int (*notify_required)(struct ast_sip_subscription *sub, enum ast_sip_subscription_notify_reason reason);
+};
+
+struct ast_sip_subscriber {
+	/*!
+	 * \brief A NOTIFY has been received.
+	 *
+	 * The body of the NOTIFY is provided so that it may be parsed and appropriate
+	 * internal state change may be generated.
+	 *
+	 * The state can be used to determine if the subscription has been terminated
+	 * by the far end or if this is just a typical resource state change.
+	 *
+	 * \param sub The subscription on which the NOTIFY arrived
+	 * \param body The body of the NOTIFY
+	 * \param state The subscription state
+	 */
+	void (*state_change)(struct ast_sip_subscription *sub, pjsip_msg_body *body, enum pjsip_evsub_state state);
+};
+
+struct ast_sip_subscription_handler {
+	/*! The name of the event this subscriber deals with */
+	const char *event_name;
+	/*! The types of body this subscriber accepts. */
+	const char *accept[AST_SIP_MAX_ACCEPT];
+	/*!
 	 * \brief Called when a subscription is to be destroyed
-	 *
-	 * This is a subscriber and notifier callback.
 	 *
 	 * The handler is not expected to send any sort of requests or responses
 	 * during this callback. The handler MUST, however, begin the destruction
 	 * process for the subscription during this callback.
 	 */
 	void (*subscription_shutdown)(struct ast_sip_subscription *subscription);
-
-	/*!
-	 * \brief Called when a SUBSCRIBE arrives in order to create a new subscription
-	 *
-	 * This is a notifier callback.
-	 *
-	 * If the notifier wishes to accept the subscription, then it can create
-	 * a new ast_sip_subscription to do so.
-	 *
-	 * If the notifier chooses to create a new subscription, then it must accept
-	 * the incoming subscription using pjsip_evsub_accept() and it must also
-	 * send an initial NOTIFY with the current subscription state.
-	 *
-	 * \param endpoint The endpoint from which we received the SUBSCRIBE
-	 * \param rdata The SUBSCRIBE request
-	 * \retval NULL The SUBSCRIBE has not been accepted
-	 * \retval non-NULL The newly-created subscription
-	 */
-	struct ast_sip_subscription *(*new_subscribe)(struct ast_sip_endpoint *endpoint,
-			pjsip_rx_data *rdata);
-
-	/*!
-	 * \brief Called when an endpoint renews a subscription.
-	 *
-	 * This is a notifier callback.
-	 *
-	 * Because of the way that the PJSIP evsub framework works, it will automatically
-	 * send a response to the SUBSCRIBE. However, the subscription handler must send
-	 * a NOTIFY with the current subscription state when this callback is called.
-	 *
-	 * The response_data that is passed into this callback is used to craft what should
-	 * be in the response to the incoming SUBSCRIBE. It is initialized with a 200 status
-	 * code and all other parameters are empty.
-	 *
-	 * \param sub The subscription that is being renewed
-	 * \param rdata The SUBSCRIBE request in question
-	 * \param[out] response_data Data pertaining to the SIP response that should be
-	 * sent to the SUBSCRIBE
-	 */
-	void (*resubscribe)(struct ast_sip_subscription *sub,
-			pjsip_rx_data *rdata, struct ast_sip_subscription_response_data *response_data);
-
-	/*!
-	 * \brief Called when a subscription times out.
-	 *
-	 * This is a notifier callback
-	 *
-	 * This indicates that the subscription has timed out. The subscription handler is
-	 * expected to send a NOTIFY that terminates the subscription.
-	 *
-	 * \param sub The subscription that has timed out
-	 */
-	void (*subscription_timeout)(struct ast_sip_subscription *sub);
-
-	/*!
-	 * \brief Called when a subscription is terminated via a SUBSCRIBE or NOTIFY request
-	 *
-	 * This is a notifier and subscriber callback.
-	 *
-	 * The PJSIP subscription framework will automatically send the response to the
-	 * request. If a notifier receives this callback, then the subscription handler
-	 * is expected to send a final NOTIFY to terminate the subscription.
-	 *
-	 * \param sub The subscription being terminated
-	 * \param rdata The request that terminated the subscription
-	 */
-	void (*subscription_terminated)(struct ast_sip_subscription *sub, pjsip_rx_data *rdata);
-
-	/*!
-	 * \brief Called when a subscription handler's outbound NOTIFY receives a response
-	 *
-	 * This is a notifier callback.
-	 *
-	 * \param sub The subscription
-	 * \param rdata The NOTIFY response
-	 */
-	void (*notify_response)(struct ast_sip_subscription *sub, pjsip_rx_data *rdata);
-
-	/*!
-	 * \brief Called when a subscription handler receives an inbound NOTIFY
-	 *
-	 * This is a subscriber callback.
-	 *
-	 * Because of the way that the PJSIP evsub framework works, it will automatically
-	 * send a response to the NOTIFY. By default this will be a 200 OK response, but
-	 * this callback can change details of the response by returning response data
-	 * to use.
-	 *
-	 * The response_data that is passed into this callback is used to craft what should
-	 * be in the response to the incoming SUBSCRIBE. It is initialized with a 200 status
-	 * code and all other parameters are empty.
-	 *
-	 * \param sub The subscription
-	 * \param rdata The NOTIFY request
-	 * \param[out] response_data Data pertaining to the SIP response that should be
-	 * sent to the SUBSCRIBE
-	 */
-	void (*notify_request)(struct ast_sip_subscription *sub,
-			pjsip_rx_data *rdata, struct ast_sip_subscription_response_data *response_data);
-
-	/*!
-	 * \brief Called when it is time for a subscriber to resubscribe
-	 *
-	 * This is a subscriber callback.
-	 *
-	 * The subscriber can reresh the subscription using the pjsip_evsub_initiate()
-	 * function.
-	 *
-	 * \param sub The subscription to refresh
-	 * \retval 0 Success
-	 * \retval non-zero Failure
-	 */
-	int (*refresh_subscription)(struct ast_sip_subscription *sub);
-
 	/*!
 	 * \brief Converts the subscriber to AMI
-	 *
-	 * This is a subscriber callback.
 	 *
 	 * \param sub The subscription
 	 * \param buf The string to write AMI data
 	 */
 	void (*to_ami)(struct ast_sip_subscription *sub, struct ast_str **buf);
+	/*! Subscriber callbacks for this handler */
+	struct ast_sip_subscriber *subscriber;
+	/*! Notifier callbacks for this handler */
+	struct ast_sip_notifier *notifier;
 	AST_LIST_ENTRY(ast_sip_subscription_handler) next;
 };
 
 /*!
  * \brief Create a new ast_sip_subscription structure
  *
- * In most cases the pubsub core will create a general purpose subscription
- * within PJSIP. However, PJSIP provides enhanced support for the following
- * event packages:
- *
- * presence
- * message-summary
- *
- * If either of these events are handled by the subscription handler, then
- * the special-purpose event subscriptions will be created within PJSIP,
- * and it will be expected that your subscription handler make use of the
- * special PJSIP APIs.
- *
- * \param handler The subsription handler for this subscription
- * \param role Whether we are acting as subscriber or notifier for this subscription
- * \param endpoint The endpoint involved in this subscription
- * \param rdata If acting as a notifier, the SUBSCRIBE request that triggered subscription creation
+ * When a subscriber wishes to create a subscription, it may call this function
+ * to allocate resources and to send the initial SUBSCRIBE out.
+ *
+ * \param subscriber The subscriber that is making the request.
+ * \param endpoint The endpoint to whome the SUBSCRIBE will be sent.
+ * \param resource The resource to place in the SUBSCRIBE's Request-URI.
  */
 struct ast_sip_subscription *ast_sip_create_subscription(const struct ast_sip_subscription_handler *handler,
-		enum ast_sip_subscription_role role, struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata);
+		struct ast_sip_endpoint *endpoint, const char *resource);
 
 
 /*!
@@ -426,46 +324,61 @@
 struct ast_taskprocessor *ast_sip_subscription_get_serializer(struct ast_sip_subscription *sub);
 
 /*!
- * \brief Get the underlying PJSIP evsub structure
- *
- * This is useful when wishing to call PJSIP's API calls in order to
- * create SUBSCRIBEs, NOTIFIES, etc. as well as get subscription state
- *
- * This function, as well as all methods called on the pjsip_evsub should
- * be done in a SIP servant thread.
- *
- * \param sub The subscription
- * \retval NULL Failure
- * \retval non-NULL The underlying pjsip_evsub
- */
-pjsip_evsub *ast_sip_subscription_get_evsub(struct ast_sip_subscription *sub);
-
-/*!
- * \brief Get the underlying PJSIP dialog structure
- *
- * Call this function when information needs to be retrieved from the
- * underlying pjsip dialog.
- *
- * This function, as well as all methods called on the pjsip_evsub should
- * be done in a SIP servant thread.
- *
- * \param sub The subscription
- * \retval NULL Failure
- * \retval non-NULL The underlying pjsip_dialog
- */
-pjsip_dialog *ast_sip_subscription_get_dlg(struct ast_sip_subscription *sub);
-
-/*!
- * \brief Accept a subscription request
- *
- * \param sub The subscription to be accepted
- * \param rdata The received subscription request
- * \param response The response code to send
- *
+ * \brief Notify a SIP subscription of a state change.
+ *
+ * This will create a NOTIFY body to be sent out for the subscribed resource.
+ * On real subscriptions, a NOTIFY request will be generated and sent.
+ * On virtual subscriptions, the NOTIFY is saved on the virtual subscription and the
+ * parent subscription is alerted.
+ *
+ * \param sub The subscription on which a state change is occurring.
+ * \param notify_data Event package-specific data used to create the NOTIFY body.
+ * \param terminate True if this NOTIFY is intended to terminate the subscription.
  * \retval 0 Success
  * \retval non-zero Failure
  */
-int ast_sip_subscription_accept(struct ast_sip_subscription *sub, pjsip_rx_data *rdata, int response);
+int ast_sip_subscription_notify(struct ast_sip_subscription *sub, void *notify_data, int terminate);
+
+/*!
+ * \brief Retrieve the local URI for this subscription
+ *
+ * This is the local URI as determined by the underlying SIP dialog.
+ *
+ * \param sub The subscription
+ * \param[out] buf The buffer into which to store the URI.
+ * \param size The size of the buffer.
+ */
+void ast_sip_subscription_get_local_uri(struct ast_sip_subscription *sub, char *buf, size_t size);
+
+/*!
+ * \brief Retrive the remote URI for this subscription
+ *
+ * This is the remote URI as determined by the underlying SIP dialog.
+ *
+ * \param sub The subscription
+ * \param[out] buf The buffer into which to store the URI.
+ * \param size The size of the buffer.
+ */
+void ast_sip_subscription_get_remote_uri(struct ast_sip_subscription *sub, char *buf, size_t size);
+
+/*!
+ * \brief Get the name of the subscribed resource.
+ */
+const char *ast_sip_subscription_get_resource_name(struct ast_sip_subscription *sub);
+
+/*!
+ * \brief Get a header value for a subscription.
+ *
+ * For notifiers, the headers of the inbound SUBSCRIBE that started the dialog
+ * are stored on the subscription. This method allows access to the header. The
+ * return is the same as pjsip_msg_find_hdr_by_name(), meaning that it is dependent
+ * on the header being searched for.
+ *
+ * \param sub The subscription to search in.
+ * \param header The name of the header to search for.
+ * \return The discovered header, or NULL if the header cannot be found.
+ */
+void *ast_sip_subscription_get_header(const struct ast_sip_subscription *sub, const char *header);
 
 /*!
  * \brief Send a request created via a PJSIP evsub method

Modified: trunk/res/res_pjsip_exten_state.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_pjsip_exten_state.c?view=diff&rev=417233&r1=417232&r2=417233
==============================================================================
--- trunk/res/res_pjsip_exten_state.c (original)
+++ trunk/res/res_pjsip_exten_state.c Wed Jun 25 15:57:28 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,14 +96,12 @@
 	ao2_cleanup(sub->sip_sub);
 }
 
-static char *get_user_agent(pjsip_rx_data *rdata)
-{
-	static const pj_str_t USER_AGENT = { "User-Agent", 10 };
-
+static char *get_user_agent(const struct ast_sip_subscription *sip_sub)
+{
 	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);
+	pjsip_user_agent_hdr *user_agent_hdr = ast_sip_subscription_get_header(
+			sip_sub, "User-Agent");
 
 	if (!user_agent_hdr) {
 		return NULL;
@@ -132,85 +128,30 @@
  * 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);
-
+		struct ast_sip_subscription *sip_sub, 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->sip_sub = ao2_bump(sip_sub);
 	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);
+	exten_state_sub->user_agent = get_user_agent(sip_sub);
 	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 +161,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 +178,14 @@
 			"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, 0);
 	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;
+	int terminate;
 };
 
 static void notify_task_data_destructor(void *obj)
@@ -264,14 +204,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,17 +227,16 @@
 		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");
+		task_data->terminate = 1;
 	}
 
 	return task_data;
@@ -313,9 +250,8 @@
 	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,
+			task_data->terminate);
 
 	pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(),
 			task_data->exten_state_data.pool);
@@ -407,24 +343,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(sip_sub, 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,64 +374,50 @@
 		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;
+	}
+
+	/* Go ahead and cleanup the endpoint since we don't need it anymore */
+	ao2_cleanup(endpoint);
 
 	/* 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");
-		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(exten_state_sub);
+		return -1;
+	}
+
+	send_notify(exten_state_sub);
+	ao2_cleanup(exten_state_sub);
+	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: trunk/res/res_pjsip_mwi.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_pjsip_mwi.c?view=diff&rev=417233&r1=417232&r2=417233
==============================================================================
--- trunk/res/res_pjsip_mwi.c (original)
+++ trunk/res/res_pjsip_mwi.c Wed Jun 25 15:57:28 2014
@@ -49,31 +49,24 @@
 #define MWI_SUBTYPE "simple-message-summary"
 
 static void mwi_subscription_shutdown(struct ast_sip_subscription *sub);
-static struct ast_sip_subscription *mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
-		pjsip_rx_data *rdata);
-static void mwi_resubscribe(struct ast_sip_subscription *sub, pjsip_rx_data *rdata,
-		struct ast_sip_subscription_response_data *response_data);
-static void mwi_subscription_timeout(struct ast_sip_subscription *sub);
-static void mwi_subscription_terminated(struct ast_sip_subscription *sub, pjsip_rx_data *rdata);
-static void mwi_notify_response(struct ast_sip_subscription *sub, pjsip_rx_data *rdata);
-static void mwi_notify_request(struct ast_sip_subscription *sub, pjsip_rx_data *rdata,
-		struct ast_sip_subscription_response_data *response_data);
-static int mwi_refresh_subscription(struct ast_sip_subscription *sub);
 static void mwi_to_ami(struct ast_sip_subscription *sub, struct ast_str **buf);
+static int mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
+		const char *resource);
+static int mwi_notify_required(struct ast_sip_subscription *sip_sub,
+		enum ast_sip_subscription_notify_reason reason);
+
+static struct ast_sip_notifier mwi_notifier = {
+	.default_accept = MWI_TYPE"/"MWI_SUBTYPE,
+	.new_subscribe = mwi_new_subscribe,
+	.notify_required = mwi_notify_required,
+};
 
 static struct ast_sip_subscription_handler mwi_handler = {
 	.event_name = "message-summary",
 	.accept = { MWI_TYPE"/"MWI_SUBTYPE, },
-	.default_accept =  MWI_TYPE"/"MWI_SUBTYPE,
 	.subscription_shutdown = mwi_subscription_shutdown,
-	.new_subscribe = mwi_new_subscribe,
-	.resubscribe = mwi_resubscribe,
-	.subscription_timeout = mwi_subscription_timeout,
-	.subscription_terminated = mwi_subscription_terminated,
-	.notify_response = mwi_notify_response,
-	.notify_request = mwi_notify_request,
-	.refresh_subscription = mwi_refresh_subscription,
 	.to_ami = mwi_to_ami,
+	.notifier = &mwi_notifier,
 };
 
 /*!
@@ -202,7 +195,7 @@
 }
 
 static struct mwi_subscription *mwi_subscription_alloc(struct ast_sip_endpoint *endpoint,
-		enum ast_sip_subscription_role role, unsigned int is_solicited, pjsip_rx_data *rdata)
+		unsigned int is_solicited, struct ast_sip_subscription *sip_sub)
 {
 	struct mwi_subscription *sub;
 	const char *endpoint_id = ast_sorcery_object_get_id(endpoint);
@@ -216,6 +209,7 @@
 
 	/* Safe strcpy */
 	strcpy(sub->id, endpoint_id);
+
 	/* Unsolicited MWI doesn't actually result in a SIP subscription being
 	 * created. This is because a SIP subscription associates with a dialog.
 	 * Most devices expect unsolicited MWI NOTIFYs to appear out of dialog. If
@@ -224,13 +218,7 @@
 	 * state not being updated on the device
 	 */
 	if (is_solicited) {
-		sub->sip_sub = ast_sip_create_subscription(&mwi_handler,
-				role, endpoint, rdata);
-		if (!sub->sip_sub) {
-			ast_log(LOG_WARNING, "Unable to create MWI SIP subscription for endpoint %s\n", sub->id);
-			ao2_cleanup(sub);
-			return NULL;
-		}
+		sub->sip_sub = ao2_bump(sip_sub);
 	}
 
 	sub->stasis_subs = ao2_container_alloc(STASIS_BUCKETS, stasis_sub_hash, stasis_sub_cmp);
@@ -314,7 +302,6 @@
 	struct mwi_subscription *sub;
 	struct ast_sip_endpoint *endpoint;
 	pjsip_evsub_state state;
-	const char *reason;
 	const struct ast_sip_body *body;
 };
 
@@ -324,7 +311,6 @@
 	struct mwi_subscription *sub = mwi_data->sub;
 	struct ast_sip_endpoint *endpoint = mwi_data->endpoint;
 	pjsip_evsub_state state = mwi_data->state;
-	const char *reason = mwi_data->reason;
 	const struct ast_sip_body *body = mwi_data->body;
 	struct ast_sip_contact *contact = obj;
 	const char *state_name;
@@ -358,9 +344,6 @@
 
 	sub_state = pjsip_sub_state_hdr_create(tdata->pool);
 	pj_cstr(&sub_state->sub_state, state_name);
-	if (reason) {
-		pj_cstr(&sub_state->reason_param, reason);
-	}
 	pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) sub_state);
 
 	event = pjsip_event_hdr_create(tdata->pool);
@@ -374,13 +357,15 @@
 	return 0;
 }
 
-static void send_unsolicited_mwi_notify(struct mwi_subscription *sub, pjsip_evsub_state state, const char *reason,
-		struct ast_sip_body *body)
+static void send_unsolicited_mwi_notify(struct mwi_subscription *sub,
+		struct ast_sip_message_accumulator *counter)
 {
 	RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(),
 				"endpoint", sub->id), ao2_cleanup);
 	char *endpoint_aors;
 	char *aor_name;
+	struct ast_sip_body body;
+	struct ast_str *body_text;
 
 	if (!endpoint) {
 		ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because endpoint does not exist\n",
@@ -393,7 +378,27 @@
 		return;
 	}
 
+	body.type = MWI_TYPE;
+	body.subtype = MWI_SUBTYPE;
+
+	body_text = ast_str_create(64);
+
+	if (!body_text) {
+		return;
+	}
+
+	if (ast_sip_pubsub_generate_body_content(body.type, body.subtype, counter, &body_text)) {
+		ast_log(LOG_WARNING, "Unable to generate SIP MWI NOTIFY body.\n");
+		ast_free(body_text);
+		return;
+	}
+
+	body.body_text = ast_str_buffer(body_text);
+
 	endpoint_aors = ast_strdupa(endpoint->aors);
+
+	ast_debug(5, "Sending unsolicited MWI NOTIFY to endpoint %s, new messages: %d, old messages: %d\n",
+			sub->id, counter->new_msgs, counter->old_msgs);
 
 	while ((aor_name = strsep(&endpoint_aors, ","))) {

[... 1538 lines stripped ...]



More information about the asterisk-commits mailing list