2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Kevin Harwell <kharwell@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <depend>res_pjsip_pubsub</depend>
23 <support_level>core</support_level>
29 #include <pjsip_simple.h>
32 #include "asterisk/res_pjsip.h"
33 #include "asterisk/res_pjsip_pubsub.h"
34 #include "asterisk/res_pjsip_body_generator_types.h"
35 #include "asterisk/module.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/astobj2.h"
38 #include "asterisk/sorcery.h"
39 #include "asterisk/app.h"
41 #define BODY_SIZE 1024
42 #define EVENT_TYPE_SIZE 50
45 * \brief A subscription for extension state
47 * This structure acts as the owner for the underlying SIP subscription. It
48 * also keeps a pointer to an associated "provider" so when a state changes
49 * a notify data creator is quickly accessible.
51 struct exten_state_subscription {
52 /*! Watcher id when registering for extension state changes */
54 /*! The SIP subscription */
55 struct ast_sip_subscription *sip_sub;
56 /*! Context in which subscription looks for updates */
57 char context[AST_MAX_CONTEXT];
58 /*! Extension within the context to receive updates from */
59 char exten[AST_MAX_EXTENSION];
60 /*! The subscription's user agent */
62 /*! The last known extension state */
63 enum ast_extension_states last_exten_state;
64 /*! The last known presence state */
65 enum ast_presence_state last_presence_state;
68 #define DEFAULT_PRESENCE_BODY "application/pidf+xml"
69 #define DEFAULT_DIALOG_BODY "application/dialog-info+xml"
71 static void subscription_shutdown(struct ast_sip_subscription *sub);
72 static int new_subscribe(struct ast_sip_endpoint *endpoint, const char *resource);
73 static int subscription_established(struct ast_sip_subscription *sub);
74 static void *get_notify_data(struct ast_sip_subscription *sub);
75 static void to_ami(struct ast_sip_subscription *sub,
76 struct ast_str **buf);
78 struct ast_sip_notifier presence_notifier = {
79 .default_accept = DEFAULT_PRESENCE_BODY,
80 .new_subscribe = new_subscribe,
81 .subscription_established = subscription_established,
82 .get_notify_data = get_notify_data,
85 struct ast_sip_notifier dialog_notifier = {
86 .default_accept = DEFAULT_DIALOG_BODY,
87 .new_subscribe = new_subscribe,
88 .subscription_established = subscription_established,
89 .get_notify_data = get_notify_data,
92 struct ast_sip_subscription_handler presence_handler = {
93 .event_name = "presence",
94 .body_type = AST_SIP_EXTEN_STATE_DATA,
95 .accept = { DEFAULT_PRESENCE_BODY, },
96 .subscription_shutdown = subscription_shutdown,
98 .notifier = &presence_notifier,
101 struct ast_sip_subscription_handler dialog_handler = {
102 .event_name = "dialog",
103 .body_type = AST_SIP_EXTEN_STATE_DATA,
104 .accept = { DEFAULT_DIALOG_BODY, },
105 .subscription_shutdown = subscription_shutdown,
107 .notifier = &dialog_notifier,
110 static void exten_state_subscription_destructor(void *obj)
112 struct exten_state_subscription *sub = obj;
114 ast_free(sub->user_agent);
115 ao2_cleanup(sub->sip_sub);
118 static char *get_user_agent(const struct ast_sip_subscription *sip_sub)
121 char *user_agent = NULL;
122 pjsip_user_agent_hdr *user_agent_hdr = ast_sip_subscription_get_header(
123 sip_sub, "User-Agent");
125 if (!user_agent_hdr) {
129 size = pj_strlen(&user_agent_hdr->hvalue) + 1;
130 user_agent = ast_malloc(size);
131 ast_copy_pj_str(user_agent, &user_agent_hdr->hvalue, size);
132 return ast_str_to_lower(user_agent);
137 * \brief Initialize the last extension state to something outside
140 #define INITIAL_LAST_EXTEN_STATE -3
144 * \brief Allocates an exten_state_subscription object.
146 * Creates the underlying SIP subscription for the given request. First makes
147 * sure that there are registered handler and provider objects available.
149 static struct exten_state_subscription *exten_state_subscription_alloc(
150 struct ast_sip_subscription *sip_sub, struct ast_sip_endpoint *endpoint)
152 struct exten_state_subscription * exten_state_sub;
154 exten_state_sub = ao2_alloc(sizeof(*exten_state_sub), exten_state_subscription_destructor);
155 if (!exten_state_sub) {
159 exten_state_sub->sip_sub = ao2_bump(sip_sub);
160 exten_state_sub->last_exten_state = INITIAL_LAST_EXTEN_STATE;
161 exten_state_sub->last_presence_state = AST_PRESENCE_NOT_SET;
162 exten_state_sub->user_agent = get_user_agent(sip_sub);
163 return exten_state_sub;
166 struct notify_task_data {
167 struct ast_sip_exten_state_data exten_state_data;
168 struct exten_state_subscription *exten_state_sub;
172 static void notify_task_data_destructor(void *obj)
174 struct notify_task_data *task_data = obj;
176 ao2_ref(task_data->exten_state_sub, -1);
177 ao2_cleanup(task_data->exten_state_data.device_state_info);
178 ast_free(task_data->exten_state_data.presence_subtype);
179 ast_free(task_data->exten_state_data.presence_message);
180 ast_free(task_data->exten_state_data.user_agent);
183 static struct notify_task_data *alloc_notify_task_data(char *exten, struct exten_state_subscription *exten_state_sub,
184 struct ast_state_cb_info *info)
186 struct notify_task_data *task_data =
187 ao2_alloc(sizeof(*task_data), notify_task_data_destructor);
190 ast_log(LOG_WARNING, "Unable to create notify task data\n");
194 task_data->exten_state_sub = exten_state_sub;
195 task_data->exten_state_sub->last_exten_state = info->exten_state;
196 task_data->exten_state_sub->last_presence_state = info->presence_state;
197 ao2_ref(task_data->exten_state_sub, +1);
199 task_data->exten_state_data.exten = exten_state_sub->exten;
200 task_data->exten_state_data.exten_state = info->exten_state;
201 task_data->exten_state_data.presence_state = info->presence_state;
202 task_data->exten_state_data.presence_subtype = ast_strdup(info->presence_subtype);
203 task_data->exten_state_data.presence_message = ast_strdup(info->presence_message);
204 task_data->exten_state_data.user_agent = ast_strdup(exten_state_sub->user_agent);
205 task_data->exten_state_data.device_state_info = ao2_bump(info->device_state_info);
206 task_data->exten_state_data.sub = exten_state_sub->sip_sub;
208 ast_sip_subscription_get_local_uri(exten_state_sub->sip_sub,
209 task_data->exten_state_data.local, sizeof(task_data->exten_state_data.local));
210 ast_sip_subscription_get_remote_uri(exten_state_sub->sip_sub,
211 task_data->exten_state_data.remote, sizeof(task_data->exten_state_data.remote));
213 if ((info->exten_state == AST_EXTENSION_DEACTIVATED) ||
214 (info->exten_state == AST_EXTENSION_REMOVED)) {
215 ast_verb(2, "Watcher for hint %s %s\n", exten, info->exten_state
216 == AST_EXTENSION_REMOVED ? "removed" : "deactivated");
217 task_data->terminate = 1;
223 static int notify_task(void *obj)
225 RAII_VAR(struct notify_task_data *, task_data, obj, ao2_cleanup);
226 struct ast_sip_body_data data = {
227 .body_type = AST_SIP_EXTEN_STATE_DATA,
228 .body_data = &task_data->exten_state_data,
231 /* Pool allocation has to happen here so that we allocate within a PJLIB thread */
232 task_data->exten_state_data.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
233 "exten_state", 1024, 1024);
234 if (!task_data->exten_state_data.pool) {
238 task_data->exten_state_data.sub = task_data->exten_state_sub->sip_sub;
240 ast_sip_subscription_notify(task_data->exten_state_sub->sip_sub, &data,
241 task_data->terminate);
243 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(),
244 task_data->exten_state_data.pool);
250 * \brief Callback for exten/device state changes.
252 * Upon state change, send the appropriate notification to the subscriber.
254 static int state_changed(char *context, char *exten,
255 struct ast_state_cb_info *info, void *data)
257 struct notify_task_data *task_data;
258 struct exten_state_subscription *exten_state_sub = data;
260 if (!(task_data = alloc_notify_task_data(exten, exten_state_sub, info))) {
264 /* safe to push this async since we copy the data from info and
265 add a ref for the device state info */
266 if (ast_sip_push_task(ast_sip_subscription_get_serializer(task_data->exten_state_sub->sip_sub),
267 notify_task, task_data)) {
268 ao2_cleanup(task_data);
274 static void state_changed_destroy(int id, void *data)
276 struct exten_state_subscription *exten_state_sub = data;
277 ao2_cleanup(exten_state_sub);
280 static struct ast_datastore_info ds_info = { };
281 static const char ds_name[] = "exten state datastore";
285 * \brief Add a datastore for exten exten_state_subscription.
287 * Adds the exten_state_subscription wrapper object to a datastore so it can be retrieved
288 * later based upon its association with the ast_sip_subscription.
290 static int add_datastore(struct exten_state_subscription *exten_state_sub)
292 RAII_VAR(struct ast_datastore *, datastore,
293 ast_sip_subscription_alloc_datastore(&ds_info, ds_name), ao2_cleanup);
299 datastore->data = exten_state_sub;
300 ast_sip_subscription_add_datastore(exten_state_sub->sip_sub, datastore);
301 ao2_ref(exten_state_sub, +1);
307 * \brief Get the exten_state_subscription object associated with the given
308 * ast_sip_subscription in the datastore.
310 static struct exten_state_subscription *get_exten_state_sub(
311 struct ast_sip_subscription *sub)
313 RAII_VAR(struct ast_datastore *, datastore,
314 ast_sip_subscription_get_datastore(sub, ds_name), ao2_cleanup);
316 return datastore ? datastore->data : NULL;
319 static void subscription_shutdown(struct ast_sip_subscription *sub)
321 struct exten_state_subscription *exten_state_sub = get_exten_state_sub(sub);
323 if (!exten_state_sub) {
327 ast_extension_state_del(exten_state_sub->id, state_changed);
328 ast_sip_subscription_remove_datastore(exten_state_sub->sip_sub, ds_name);
329 /* remove data store reference */
330 ao2_cleanup(exten_state_sub);
333 static int new_subscribe(struct ast_sip_endpoint *endpoint,
334 const char *resource)
336 if (!ast_exists_extension(NULL, endpoint->context, resource, PRIORITY_HINT, NULL)) {
337 ast_log(LOG_NOTICE, "Extension %s does not exist or has no associated hint\n", resource);
344 static int subscription_established(struct ast_sip_subscription *sip_sub)
346 struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sip_sub);
347 const char *resource = ast_sip_subscription_get_resource_name(sip_sub);
348 struct exten_state_subscription *exten_state_sub;
350 if (!(exten_state_sub = exten_state_subscription_alloc(sip_sub, endpoint))) {
351 ao2_cleanup(endpoint);
355 ast_copy_string(exten_state_sub->context, endpoint->context, sizeof(exten_state_sub->context));
356 ast_copy_string(exten_state_sub->exten, resource, sizeof(exten_state_sub->exten));
358 if ((exten_state_sub->id = ast_extension_state_add_destroy_extended(
359 exten_state_sub->context, exten_state_sub->exten,
360 state_changed, state_changed_destroy, exten_state_sub)) < 0) {
361 ast_log(LOG_WARNING, "Unable to subscribe endpoint '%s' to extension '%s@%s'\n",
362 ast_sorcery_object_get_id(endpoint), exten_state_sub->exten,
363 exten_state_sub->context);
364 ao2_cleanup(endpoint);
365 ao2_cleanup(exten_state_sub);
369 /* Go ahead and cleanup the endpoint since we don't need it anymore */
370 ao2_cleanup(endpoint);
372 /* bump the ref since ast_extension_state_add holds a reference */
373 ao2_ref(exten_state_sub, +1);
375 if (add_datastore(exten_state_sub)) {
376 ast_log(LOG_WARNING, "Unable to add to subscription datastore.\n");
377 ao2_cleanup(exten_state_sub);
381 ao2_cleanup(exten_state_sub);
385 static void exten_state_data_destructor(void *obj)
387 struct ast_sip_exten_state_data *exten_state_data = obj;
389 ao2_cleanup(exten_state_data->device_state_info);
390 ast_free(exten_state_data->presence_subtype);
391 ast_free(exten_state_data->presence_message);
392 if (exten_state_data->pool) {
393 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), exten_state_data->pool);
397 static struct ast_sip_exten_state_data *exten_state_data_alloc(struct ast_sip_subscription *sip_sub,
398 struct exten_state_subscription *exten_state_sub)
400 struct ast_sip_exten_state_data *exten_state_data;
401 char *subtype = NULL;
402 char *message = NULL;
404 exten_state_data = ao2_alloc(sizeof(*exten_state_data), exten_state_data_destructor);
405 if (!exten_state_data) {
409 exten_state_data->exten = exten_state_sub->exten;
410 if ((exten_state_data->presence_state = ast_hint_presence_state(NULL, exten_state_sub->context,
411 exten_state_sub->exten, &subtype, &message)) == -1) {
412 ao2_cleanup(exten_state_data);
415 exten_state_data->presence_subtype = subtype;
416 exten_state_data->presence_message = message;
417 exten_state_data->user_agent = exten_state_sub->user_agent;
418 ast_sip_subscription_get_local_uri(sip_sub, exten_state_data->local,
419 sizeof(exten_state_data->local));
420 ast_sip_subscription_get_remote_uri(sip_sub, exten_state_data->remote,
421 sizeof(exten_state_data->remote));
422 exten_state_data->sub = sip_sub;
424 exten_state_data->exten_state = ast_extension_state_extended(
425 NULL, exten_state_sub->context, exten_state_sub->exten,
426 &exten_state_data->device_state_info);
427 if (exten_state_data->exten_state < 0) {
428 ao2_cleanup(exten_state_data);
432 exten_state_data->pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
433 "exten_state", 1024, 1024);
434 if (!exten_state_data->pool) {
435 ao2_cleanup(exten_state_data);
439 return exten_state_data;
442 static void *get_notify_data(struct ast_sip_subscription *sub)
444 struct exten_state_subscription *exten_state_sub;
446 exten_state_sub = get_exten_state_sub(sub);
447 if (!exten_state_sub) {
451 return exten_state_data_alloc(sub, exten_state_sub);
454 static void to_ami(struct ast_sip_subscription *sub,
455 struct ast_str **buf)
457 struct exten_state_subscription *exten_state_sub =
458 get_exten_state_sub(sub);
460 if (!exten_state_sub) {
464 ast_str_append(buf, 0, "SubscriptionType: extension_state\r\n"
465 "Extension: %s\r\nExtensionStates: %s\r\n",
466 exten_state_sub->exten, ast_extension_state2str(
467 exten_state_sub->last_exten_state));
470 static int load_module(void)
472 CHECK_PJSIP_MODULE_LOADED();
474 if (ast_sip_register_subscription_handler(&presence_handler)) {
475 ast_log(LOG_WARNING, "Unable to register subscription handler %s\n",
476 presence_handler.event_name);
477 return AST_MODULE_LOAD_DECLINE;
480 if (ast_sip_register_subscription_handler(&dialog_handler)) {
481 ast_log(LOG_WARNING, "Unable to register subscription handler %s\n",
482 dialog_handler.event_name);
483 ast_sip_unregister_subscription_handler(&presence_handler);
484 return AST_MODULE_LOAD_DECLINE;
487 return AST_MODULE_LOAD_SUCCESS;
490 static int unload_module(void)
492 ast_sip_unregister_subscription_handler(&dialog_handler);
493 ast_sip_unregister_subscription_handler(&presence_handler);
497 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State Notifications",
498 .support_level = AST_MODULE_SUPPORT_CORE,
500 .unload = unload_module,
501 .load_pri = AST_MODPRI_CHANNEL_DEPEND,