[asterisk-commits] kharwell: branch group/pimp_my_sip r392029 - in /team/group/pimp_my_sip: incl...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Mon Jun 17 10:41:26 CDT 2013
Author: kharwell
Date: Mon Jun 17 10:41:21 2013
New Revision: 392029
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=392029
Log:
New SIP channel driver - extension state notifications
Added support for extension state notifications in the new SIP channel driver.
A simple API was developed so that specific extension state providers can attach
and create the relevant NOTIFY request body, according to their registered type,
for the outgoing request. The framework maintains the list of providers and
looks up the appropriate provider upon a state change for an associated
registered subscription. For instance, a provider can be registered for a
particular event/media type tuple (ex: presence and application/xml+pidf). When
a subscription comes in for a given event/media type it is the responsibility of
the registered provider to create that NOTIFY body.
Included in the changes is also an implementation of providers for the following
types: event=presence; media_type=application/pidf+xml, application/xml+pidf,
and application/cpim+pidf+xml.
(closes issue ASTERISK-21441)
Reported by: Matt Jordan
(closes issue ASTERISK-21442)
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/2523/
Added:
team/group/pimp_my_sip/include/asterisk/res_sip_exten_state.h (with props)
team/group/pimp_my_sip/res/res_sip_exten_state.c (with props)
team/group/pimp_my_sip/res/res_sip_exten_state.exports.in (with props)
team/group/pimp_my_sip/res/res_sip_pidf.c (with props)
Modified:
team/group/pimp_my_sip/include/asterisk/res_sip_pubsub.h
team/group/pimp_my_sip/main/pbx.c
team/group/pimp_my_sip/res/res_sip_pubsub.c
team/group/pimp_my_sip/res/res_sip_pubsub.exports.in
Added: team/group/pimp_my_sip/include/asterisk/res_sip_exten_state.h
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/include/asterisk/res_sip_exten_state.h?view=auto&rev=392029
==============================================================================
--- team/group/pimp_my_sip/include/asterisk/res_sip_exten_state.h (added)
+++ team/group/pimp_my_sip/include/asterisk/res_sip_exten_state.h Mon Jun 17 10:41:21 2013
@@ -1,0 +1,94 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Kevin Harwell <kharwell 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.
+ */
+
+#ifndef _RES_SIP_EXTEN_STATE_H
+#define _RES_SIP_EXTEN_STATE_H
+
+#include "asterisk/stringfields.h"
+#include "asterisk/linkedlists.h"
+
+#include "asterisk/pbx.h"
+#include "asterisk/presencestate.h"
+
+
+/*!
+ * \brief Contains information pertaining to extension/device state changes.
+ */
+struct ast_sip_exten_state_data {
+ /*! The extension of the current state change */
+ const char *exten;
+ /*! The extension state of the change */
+ enum ast_extension_states exten_state;
+ /*! The presence state of the change */
+ enum ast_presence_state presence_state;
+ /*! Current device state information */
+ struct ao2_container *device_state_info;
+};
+
+/*!
+ * \brief Extension state provider.
+ */
+struct ast_sip_exten_state_provider {
+ /*! The name of the event this provider registers for */
+ const char *event_name;
+ /*! Type of the body, ex: "application" */
+ const char *type;
+ /*! Subtype of the body, ex: "pidf+xml" */
+ const char *subtype;
+ /*! Type/Subtype together - ex: application/pidf+xml */
+ const char *body_type;
+ /*! Subscription handler to be used and associated with provider */
+ struct ast_sip_subscription_handler *handler;
+
+ /*!
+ * \brief Create the body text of a NOTIFY request.
+ *
+ * Implementors use this to create body information within the given
+ * ast_str. That information is then added to the NOTIFY request.
+ *
+ * \param data Current extension state changes
+ * \param local URI of the dialog's local party, e.g. 'from'
+ * \param remote URI of the dialog's remote party, e.g. 'to'
+ * \param body_text Out parameter used to populate the NOTIFY msg body
+ * \retval 0 Successfully created the body's text
+ * \retval -1 Failed to create the body's text
+ */
+ int (*create_body)(struct ast_sip_exten_state_data *data, const char *local,
+ const char *remote, struct ast_str **body_text);
+
+ /*! Next item in the list */
+ AST_LIST_ENTRY(ast_sip_exten_state_provider) next;
+};
+
+/*!
+ * \brief Registers an extension state provider.
+ *
+ * \param obj An extension state provider
+ * \retval 0 Successfully registered the extension state provider
+ * \retval -1 Failed to register the extension state provider
+ */
+int ast_sip_register_exten_state_provider(struct ast_sip_exten_state_provider *obj);
+
+/*!
+ * \brief Unregisters an extension state provider.
+ *
+ * \param obj An extension state provider
+ */
+void ast_sip_unregister_exten_state_provider(struct ast_sip_exten_state_provider *obj);
+
+#endif
Propchange: team/group/pimp_my_sip/include/asterisk/res_sip_exten_state.h
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: team/group/pimp_my_sip/include/asterisk/res_sip_exten_state.h
------------------------------------------------------------------------------
svn:keywords = Author Date Id Rev URL
Propchange: team/group/pimp_my_sip/include/asterisk/res_sip_exten_state.h
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: team/group/pimp_my_sip/include/asterisk/res_sip_pubsub.h
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/include/asterisk/res_sip_pubsub.h?view=diff&rev=392029&r1=392028&r2=392029
==============================================================================
--- team/group/pimp_my_sip/include/asterisk/res_sip_pubsub.h (original)
+++ team/group/pimp_my_sip/include/asterisk/res_sip_pubsub.h Mon Jun 17 10:41:21 2013
@@ -261,7 +261,22 @@
* \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 Send a request created via a PJSIP evsub method
*
Modified: team/group/pimp_my_sip/main/pbx.c
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/main/pbx.c?view=diff&rev=392029&r1=392028&r2=392029
==============================================================================
--- team/group/pimp_my_sip/main/pbx.c (original)
+++ team/group/pimp_my_sip/main/pbx.c Mon Jun 17 10:41:21 2013
@@ -11814,7 +11814,7 @@
hintdevices = ao2_container_alloc(HASH_EXTENHINT_SIZE, hintdevice_hash_cb, hintdevice_cmp_multiple);
statecbs = ao2_container_alloc(1, NULL, statecbs_cmp);
- ast_register_atexit(pbx_shutdown);
+ ast_register_cleanup(pbx_shutdown);
return (hints && hintdevices && statecbs) ? 0 : -1;
}
Added: team/group/pimp_my_sip/res/res_sip_exten_state.c
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip_exten_state.c?view=auto&rev=392029
==============================================================================
--- team/group/pimp_my_sip/res/res_sip_exten_state.c (added)
+++ team/group/pimp_my_sip/res/res_sip_exten_state.c Mon Jun 17 10:41:21 2013
@@ -1,0 +1,617 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Kevin Harwell <kharwell 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_sip</depend>
+ <depend>res_sip_pubsub</depend>
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include <pjsip.h>
+#include <pjsip_simple.h>
+#include <pjlib.h>
+
+#include "asterisk/res_sip.h"
+#include "asterisk/res_sip_pubsub.h"
+#include "asterisk/res_sip_exten_state.h"
+#include "asterisk/module.h"
+#include "asterisk/logger.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/sorcery.h"
+#include "asterisk/app.h"
+
+#define BODY_SIZE 1024
+#define EVENT_TYPE_SIZE 50
+
+AST_RWLIST_HEAD_STATIC(providers, ast_sip_exten_state_provider);
+
+/*!
+ * \internal
+ * \brief Find a provider based on the given accept body type.
+ */
+static struct ast_sip_exten_state_provider *provider_by_type(const char *type)
+{
+ struct ast_sip_exten_state_provider *i;
+ SCOPED_LOCK(lock, &providers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
+ AST_RWLIST_TRAVERSE_SAFE_BEGIN(&providers, i, next) {
+ if (!strcmp(i->body_type, type)) {
+ return i;
+ }
+ }
+ AST_RWLIST_TRAVERSE_SAFE_END;
+ return NULL;
+}
+
+/*!
+ * \internal
+ * \brief Find a provider based on the given accept body types.
+ */
+static struct ast_sip_exten_state_provider *provider_by_types(const char *event_name,
+ char **types, int count)
+{
+ int i;
+ struct ast_sip_exten_state_provider *res;
+ for (i = 0; i < count; ++i) {
+ if ((res = provider_by_type(types[i])) &&
+ !strcmp(event_name, res->event_name)) {
+ return res;
+ }
+ }
+ return NULL;
+}
+
+/*!
+ * \brief A subscription for extension state
+ *
+ * This structure acts as the owner for the underlying SIP subscription. It
+ * also keeps a pointer to an associated "provider" so when a state changes
+ * a notify data creator is quickly accessible.
+ */
+struct exten_state_subscription {
+ /*! Watcher id when registering for extension state changes */
+ int id;
+ /*! The SIP subscription */
+ struct ast_sip_subscription *sip_sub;
+ /*! The name of the event the subscribed to */
+ char event_name[EVENT_TYPE_SIZE];
+ /*! The number of body types */
+ int body_types_count;
+ /*! The subscription body types */
+ char **body_types;
+ /*! Context in which subscription looks for updates */
+ char context[AST_MAX_CONTEXT];
+ /*! Extension within the context to receive updates from */
+ char exten[AST_MAX_EXTENSION];
+ /*! The last known extension state */
+ enum ast_extension_states last_exten_state;
+};
+
+static void exten_state_subscription_destructor(void *obj)
+{
+ struct exten_state_subscription *sub = obj;
+ int i;
+
+ for (i = 0; i < sub->body_types_count; ++i) {
+ ast_free(sub->body_types[i]);
+ }
+
+ ast_free(sub->body_types);
+ ao2_cleanup(sub->sip_sub);
+}
+
+/*!
+ * \internal
+ * \brief Copies the body types the message wishes to subscribe to.
+ */
+static void copy_body_types(pjsip_rx_data *rdata,
+ struct exten_state_subscription *exten_state_sub)
+{
+ int i;
+ pjsip_accept_hdr *hdr = (pjsip_accept_hdr*)
+ pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, NULL);
+
+ exten_state_sub->body_types_count = hdr->count;
+ exten_state_sub->body_types = ast_malloc(hdr->count * sizeof(char*));
+
+ for (i = 0; i < hdr->count; ++i) {
+ exten_state_sub->body_types[i] =
+ ast_malloc(hdr->values[i].slen * sizeof(char*) + 1);
+
+ ast_copy_string(exten_state_sub->body_types[i],
+ pj_strbuf(&hdr->values[i]), hdr->values[i].slen + 1);
+ }
+}
+
+/*!
+ * \internal
+ * \brief Initialize the last extension state to something outside
+ * its usual states.
+ */
+#define INITIAL_LAST_EXTEN_STATE -3
+
+/*!
+ * \internal
+ * \brief Allocates an exten_state_subscription object.
+ *
+ * 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)
+{
+ static const pj_str_t event_name = { "Event", 5 };
+ pjsip_event_hdr *hdr = (pjsip_event_hdr*)pjsip_msg_find_hdr_by_name(
+ rdata->msg_info.msg, &event_name, NULL);
+
+ struct ast_sip_exten_state_provider *provider;
+ RAII_VAR(struct exten_state_subscription *, exten_state_sub,
+ ao2_alloc(sizeof(*exten_state_sub), exten_state_subscription_destructor), ao2_cleanup);
+
+ if (!exten_state_sub) {
+ return NULL;
+ }
+
+ ast_copy_pj_str(exten_state_sub->event_name, &hdr->event_type,
+ sizeof(exten_state_sub->event_name));
+
+ copy_body_types(rdata, exten_state_sub);
+ if (!(provider = provider_by_types(exten_state_sub->event_name,
+ exten_state_sub->body_types,
+ exten_state_sub->body_types_count))) {
+ ast_log(LOG_WARNING, "Unable to locate subscription handler\n");
+ return NULL;
+ }
+
+ if (!(exten_state_sub->sip_sub = ast_sip_create_subscription(
+ provider->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;
+
+ ao2_ref(exten_state_sub, +1);
+ 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;
+ pjsip_dialog *dlg;
+ char local[PJSIP_MAX_URL_SIZE], remote[PJSIP_MAX_URL_SIZE];
+ struct ast_sip_body body;
+
+ struct ast_sip_exten_state_provider *provider = provider_by_types(
+ exten_state_sub->event_name, exten_state_sub->body_types,
+ exten_state_sub->body_types_count);
+
+ if (!provider) {
+ ast_log(LOG_ERROR, "Unable to locate provider for subscription\n");
+ return;
+ }
+
+ body.type = provider->type;
+ body.subtype = provider->subtype;
+
+ dlg = ast_sip_subscription_get_dlg(exten_state_sub->sip_sub);
+ ast_copy_pj_str(local, &dlg->local.info_str, sizeof(local));
+ ast_copy_pj_str(remote, &dlg->remote.info_str, sizeof(remote));
+
+ if (provider->create_body(exten_state_data, local, remote, &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");
+ pjsip_tx_data_dec_ref(tdata);
+ }
+}
+
+/*!
+ * \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)
+{
+ RAII_VAR(struct ao2_container*, info, NULL, ao2_cleanup);
+ char *subtype = NULL, *message = NULL;
+
+ 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,
+ exten_state_sub->exten, &subtype, &message),
+ };
+
+ if ((exten_state_data.exten_state = ast_extension_state_extended(
+ NULL, exten_state_sub->context, exten_state_sub->exten, &info)) < 0) {
+
+ ast_log(LOG_WARNING, "Unable to get device hint/info for extension %s\n",
+ exten_state_sub->exten);
+ return;
+ }
+
+ exten_state_data.device_state_info = info;
+ create_send_notify(exten_state_sub, reason, evsub_state, &exten_state_data);
+}
+
+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)
+{
+ struct notify_task_data *task_data = obj;
+
+ ao2_ref(task_data->exten_state_sub, -1);
+ ao2_cleanup(task_data->exten_state_data.device_state_info);
+}
+
+static struct notify_task_data *alloc_notify_task_data(char *exten, struct exten_state_subscription *exten_state_sub,
+ struct ast_state_cb_info *info)
+{
+ struct notify_task_data *task_data =
+ ao2_alloc(sizeof(*task_data), notify_task_data_destructor);
+
+ 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;
+ 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.device_state_info = info->device_state_info;
+
+ if (task_data->exten_state_data.device_state_info) {
+ ao2_ref(task_data->exten_state_data.device_state_info, +1);
+ }
+
+ 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");
+ }
+
+ return task_data;
+}
+
+static int notify_task(void *obj)
+{
+ RAII_VAR(struct notify_task_data *, task_data, obj, ao2_cleanup);
+
+ 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);
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief Callback for exten/device state changes.
+ *
+ * Upon state change, send the appropriate notification to the subscriber.
+ */
+static int state_changed(char *context, char *exten,
+ struct ast_state_cb_info *info, void *data)
+{
+ struct notify_task_data *task_data;
+ struct exten_state_subscription *exten_state_sub = data;
+
+ if (exten_state_sub->last_exten_state == info->exten_state) {
+ return 0;
+ }
+
+ if (!(task_data = alloc_notify_task_data(exten, exten_state_sub, info))) {
+ return -1;
+ }
+
+ /* safe to push this async since we copy the data from info and
+ add a ref for the device state info */
+ if (ast_sip_push_task(ast_sip_subscription_get_serializer(task_data->exten_state_sub->sip_sub),
+ notify_task, task_data)) {
+ ao2_cleanup(task_data);
+ return -1;
+ }
+ return 0;
+}
+
+static void state_changed_destroy(int id, void *data)
+{
+ struct exten_state_subscription *exten_state_sub = data;
+ ao2_cleanup(exten_state_sub);
+}
+
+static struct ast_datastore_info ds_info = { };
+static const char ds_name[] = "exten state datastore";
+
+/*!
+ * \internal
+ * \brief Add a datastore for exten exten_state_subscription.
+ *
+ * Adds the exten_state_subscription wrapper object to a datastore so it can be retrieved
+ * later based upon its association with the ast_sip_subscription.
+ */
+static int add_datastore(struct exten_state_subscription *exten_state_sub)
+{
+ RAII_VAR(struct ast_datastore *, datastore,
+ ast_sip_subscription_alloc_datastore(&ds_info, ds_name), ao2_cleanup);
+
+ if (!datastore) {
+ return -1;
+ }
+
+ datastore->data = exten_state_sub;
+ ast_sip_subscription_add_datastore(exten_state_sub->sip_sub, datastore);
+ ao2_ref(exten_state_sub, +1);
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief Get the exten_state_subscription object associated with the given
+ * ast_sip_subscription in the datastore.
+ */
+static struct exten_state_subscription *get_exten_state_sub(
+ struct ast_sip_subscription *sub)
+{
+ RAII_VAR(struct ast_datastore *, datastore,
+ ast_sip_subscription_get_datastore(sub, ds_name), ao2_cleanup);
+
+ return datastore ? datastore->data : NULL;
+}
+
+static void subscription_shutdown(struct ast_sip_subscription *sub)
+{
+ struct exten_state_subscription *exten_state_sub = get_exten_state_sub(sub);
+
+ if (!exten_state_sub) {
+ return;
+ }
+
+ ast_extension_state_del(exten_state_sub->id, state_changed);
+ ast_sip_subscription_remove_datastore(exten_state_sub->sip_sub, ds_name);
+ /* remove data store reference */
+ 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;
+ }
+
+ 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));
+
+ if ((exten_state_sub->id = ast_extension_state_add_destroy_extended(
+ exten_state_sub->context, exten_state_sub->exten,
+ state_changed, state_changed_destroy, 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");
+ return NULL;
+ }
+
+ if (pjsip_evsub_accept(ast_sip_subscription_get_evsub(exten_state_sub->sip_sub),
+ rdata, 200, NULL) != PJ_SUCCESS) {
+ ast_log(LOG_WARNING, "Unable to accept the incoming extension state subscription.\n");
+ 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);
+}
+
+/*!
+ * \internal
+ * \brief Create and register a subscription handler.
+ *
+ * Creates a subscription handler that can be registered with the pub/sub
+ * framework for the given event_name and accept value.
+ */
+static struct ast_sip_subscription_handler *create_and_register_handler(
+ const char *event_name, const char *accept)
+{
+ struct ast_sip_subscription_handler *handler =
+ ao2_alloc(sizeof(*handler), NULL);
+
+ if (!handler) {
+ return NULL;
+ }
+
+ handler->event_name = event_name;
+ handler->accept[0] = accept;
+
+ handler->subscription_shutdown = subscription_shutdown;
+ handler->new_subscribe = new_subscribe;
+ handler->resubscribe = resubscribe;
+ handler->subscription_timeout = subscription_timeout;
+ handler->subscription_terminated = subscription_terminated;
+
+ if (ast_sip_register_subscription_handler(handler)) {
+ ast_log(LOG_WARNING, "Unable to register subscription handler %s\n",
+ handler->event_name);
+ ao2_cleanup(handler);
+ return NULL;
+ }
+
+ return handler;
+}
+
+int ast_sip_register_exten_state_provider(struct ast_sip_exten_state_provider *obj)
+{
+ if (ast_strlen_zero(obj->type)) {
+ ast_log(LOG_WARNING, "Type not specified on provider for event %s\n",
+ obj->event_name);
+ return -1;
+ }
+
+ if (ast_strlen_zero(obj->subtype)) {
+ ast_log(LOG_WARNING, "Subtype not specified on provider for event %s\n",
+ obj->event_name);
+ return -1;
+ }
+
+ if (!obj->create_body) {
+ ast_log(LOG_WARNING, "Body handler not specified on provide for event %s\n",
+ obj->event_name);
+ return -1;
+ }
+
+ if (!(obj->handler = create_and_register_handler(obj->event_name, obj->body_type))) {
+ ast_log(LOG_WARNING, "Handler could not be registered for provider event %s\n",
+ obj->event_name);
+ return -1;
+ }
+
+ /* scope to avoid mix declarations */
+ {
+ SCOPED_LOCK(lock, &providers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
+ AST_RWLIST_INSERT_TAIL(&providers, obj, next);
+ ast_module_ref(ast_module_info->self);
+ }
+
+ return 0;
+}
+
+void ast_sip_unregister_exten_state_provider(struct ast_sip_exten_state_provider *obj)
+{
+ struct ast_sip_exten_state_provider *i;
+ SCOPED_LOCK(lock, &providers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
+ AST_RWLIST_TRAVERSE_SAFE_BEGIN(&providers, i, next) {
+ if (i == obj) {
+ ast_sip_unregister_subscription_handler(i->handler);
+ ao2_cleanup(i->handler);
+ AST_RWLIST_REMOVE_CURRENT(next);
+ ast_module_unref(ast_module_info->self);
+ break;
+ }
+ }
+ AST_RWLIST_TRAVERSE_SAFE_END;
+}
+
+static int load_module(void)
+{
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+ return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "SIP Extension State Notifications",
+ .load = load_module,
+ .unload = unload_module,
+ .load_pri = AST_MODPRI_CHANNEL_DEPEND,
+);
Propchange: team/group/pimp_my_sip/res/res_sip_exten_state.c
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: team/group/pimp_my_sip/res/res_sip_exten_state.c
------------------------------------------------------------------------------
svn:keywords = Author Date Id Rev URL
Propchange: team/group/pimp_my_sip/res/res_sip_exten_state.c
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: team/group/pimp_my_sip/res/res_sip_exten_state.exports.in
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip_exten_state.exports.in?view=auto&rev=392029
==============================================================================
--- team/group/pimp_my_sip/res/res_sip_exten_state.exports.in (added)
+++ team/group/pimp_my_sip/res/res_sip_exten_state.exports.in Mon Jun 17 10:41:21 2013
@@ -1,0 +1,7 @@
+{
+ global:
+ LINKER_SYMBOL_PREFIXast_sip_register_exten_state_provider;
+ LINKER_SYMBOL_PREFIXast_sip_unregister_exten_state_provider;
+ local:
+ *;
+};
Propchange: team/group/pimp_my_sip/res/res_sip_exten_state.exports.in
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: team/group/pimp_my_sip/res/res_sip_exten_state.exports.in
------------------------------------------------------------------------------
svn:keywords = Author Date Id Rev URL
Propchange: team/group/pimp_my_sip/res/res_sip_exten_state.exports.in
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: team/group/pimp_my_sip/res/res_sip_pidf.c
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip_pidf.c?view=auto&rev=392029
==============================================================================
--- team/group/pimp_my_sip/res/res_sip_pidf.c (added)
+++ team/group/pimp_my_sip/res/res_sip_pidf.c Mon Jun 17 10:41:21 2013
@@ -1,0 +1,341 @@
+/*
+ * asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Kevin Harwell <kharwell 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_sip</depend>
+ <depend>res_sip_pubsub</depend>
+ <depend>res_sip_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_sip.h"
+#include "asterisk/res_sip_exten_state.h"
+
+enum state {
+ NOTIFY_OPEN,
+ NOTIFY_INUSE,
+ NOTIFY_CLOSED
+};
+
+static void exten_state_to_str(int state, char **statestring, char **pidfstate,
+ char **pidfnote, int *local_state)
+{
+ switch (state) {
+ case AST_EXTENSION_RINGING:
+ *statestring = "early";
+ *local_state = NOTIFY_INUSE;
+ *pidfstate = "busy";
+ *pidfnote = "Ringing";
+ break;
+ case AST_EXTENSION_INUSE:
+ *statestring = "confirmed";
+ *local_state = NOTIFY_INUSE;
+ *pidfstate = "busy";
+ *pidfnote = "On the phone";
+ break;
+ case AST_EXTENSION_BUSY:
+ *statestring = "confirmed";
+ *local_state = NOTIFY_CLOSED;
+ *pidfstate = "busy";
+ *pidfnote = "On the phone";
+ break;
+ case AST_EXTENSION_UNAVAILABLE:
+ *statestring = "terminated";
+ *local_state = NOTIFY_CLOSED;
+ *pidfstate = "away";
+ *pidfnote = "Unavailable";
+ break;
+ case AST_EXTENSION_ONHOLD:
+ *statestring = "confirmed";
+ *local_state = NOTIFY_CLOSED;
+ *pidfstate = "busy";
+ *pidfnote = "On hold";
+ break;
+ case AST_EXTENSION_NOT_INUSE:
+ default:
+ /* Default setting */
+ *statestring = "terminated";
+ *local_state = NOTIFY_OPEN;
+ *pidfstate = "--";
+ *pidfnote ="Ready";
+
+ break;
+ }
+}
+
+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_strdup2(pool, &attr->name, name);
+ pj_strdup2(pool, &attr->value, value);
+
+ pj_xml_add_attr(node, attr);
+ 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);
+
+ pj_list_init(&node->attr_head);
+ pj_list_init(&node->node_head);
+
+ pj_strdup2(pool, &node->name, name);
+
+ 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;
+}
+
+/*!
+ * \internal
+ * \brief Adds non standard elements to the xml body
+ *
+ * This is some code that was part of the original chan_sip implementation
+ * that is not part of the RFC 3863 definition, but we are keeping available
+ * for backward compatability. The original comment stated that Eyebeam
+ * supports this format.
+
+ */
+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(pool, status, "ep:activities");
+ pj_strdup2(pool, &activities->content, "ep:");
+ pj_strcat2(&activities->content, pidfstate);
+ }
+
+ 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,
+ const char *remote, struct ast_str **body_text)
+{
+ 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);
+
+ if (!(pres = pjpidf_create(pool, pj_cstr(&entity, local)))) {
+ ast_log(LOG_WARNING, "Unable to create PIDF presence\n");
+ return -1;
+ }
+
+ add_non_standard(pool, pres, pidfstate);
+
+ if (!pjpidf_pres_add_note(pool, pres, pj_cstr(¬e, pidfnote))) {
+ ast_log(LOG_WARNING, "Unable to add note to PIDF presence\n");
+ return -1;
+ }
+
+ if (!(tuple = pjpidf_pres_add_tuple(pool, pres, pj_cstr(&id, data->exten)))) {
+ ast_log(LOG_WARNING, "Unable to create PIDF tuple\n");
+ return -1;
+ }
+
+ pjpidf_tuple_set_contact(pool, tuple, pj_cstr(&contact, remote));
+ pjpidf_tuple_set_contact_prio(pool, tuple, pj_cstr(&priority, "1"));
+ pjpidf_status_set_basic_open(pjpidf_tuple_get_status(tuple),
+ (pidfstate[0] == 'b') || (local_state != NOTIFY_CLOSED));
+
+ if (!(size = pjpidf_print(pres, ast_str_buffer(*body_text),
+ ast_str_size(*body_text)))) {
+ ast_log(LOG_WARNING, "PIDF body text too large\n");
+ return -1;
+ }
+ *(ast_str_buffer(*body_text) + size) = '\0';
+ ast_str_update(*body_text);
+
+ return 0;
+}
+
+static struct ast_sip_exten_state_provider pidf_xml_provider = {
+ .event_name = "presence",
+ .type = "application",
+ .subtype = "pidf+xml",
+ .body_type = "application/pidf+xml",
+ .create_body = pidf_xml_create_body
+};
+
+static int xpidf_xml_create_body(struct ast_sip_exten_state_data *data, const char *local,
+ const char *remote, struct ast_str **body_text)
+{
+ static pj_str_t STR_ADDR_PARAM = { ";user=ip", 8 };
+ pjxpidf_pres *pres;
+ pj_xml_attr *attr;
+ pj_str_t name, uri;
+ 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);
+
+ 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");
+
+ uri.ptr = (char*) pj_pool_alloc(pool, strlen(remote) + STR_ADDR_PARAM.slen);
+ pj_strcpy2( &uri, remote);
+ pj_strcat( &uri, &STR_ADDR_PARAM);
+ pj_strdup(pool, &attr->value, &uri);
+
+ 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;
+}
+
+static struct ast_sip_exten_state_provider xpidf_xml_provider = {
+ .event_name = "presence",
+ .type = "application",
+ .subtype = "xpidf+xml",
+ .body_type = "application/xpidf+xml",
+ .create_body = xpidf_xml_create_body
+};
+
+static struct ast_sip_exten_state_provider cpim_pidf_xml_provider = {
+ .event_name = "presence",
+ .type = "application",
+ .subtype = "cpim-pidf+xml",
+ .body_type = "application/cpim-pidf+xml",
+ .create_body = xpidf_xml_create_body,
+};
+
+static int load_module(void)
+{
+ if (ast_sip_register_exten_state_provider(&pidf_xml_provider)) {
+ ast_log(LOG_WARNING, "Unable to load provider event_name=%s, body_type=%s",
+ pidf_xml_provider.event_name, pidf_xml_provider.body_type);
+ }
+
+ if (ast_sip_register_exten_state_provider(&xpidf_xml_provider)) {
+ ast_log(LOG_WARNING, "Unable to load provider event_name=%s, body_type=%s",
+ xpidf_xml_provider.event_name, xpidf_xml_provider.body_type);
+ }
+
+ if (ast_sip_register_exten_state_provider(&cpim_pidf_xml_provider)) {
+ ast_log(LOG_WARNING, "Unable to load provider event_name=%s, body_type=%s",
+ cpim_pidf_xml_provider.event_name, cpim_pidf_xml_provider.body_type);
+ }
+
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+ ast_sip_unregister_exten_state_provider(&cpim_pidf_xml_provider);
+ ast_sip_unregister_exten_state_provider(&xpidf_xml_provider);
+ ast_sip_unregister_exten_state_provider(&pidf_xml_provider);
+
+ return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SIP Extension State PIDF Provider",
+ .load = load_module,
+ .unload = unload_module,
+ .load_pri = AST_MODPRI_CHANNEL_DEPEND,
+);
Propchange: team/group/pimp_my_sip/res/res_sip_pidf.c
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: team/group/pimp_my_sip/res/res_sip_pidf.c
------------------------------------------------------------------------------
svn:keywords = Author Date Id Rev URL
Propchange: team/group/pimp_my_sip/res/res_sip_pidf.c
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: team/group/pimp_my_sip/res/res_sip_pubsub.c
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip_pubsub.c?view=diff&rev=392029&r1=392028&r2=392029
==============================================================================
[... 74 lines stripped ...]
More information about the asterisk-commits
mailing list