Merge "res_pjsip/res_pjsip_callerid: NULL check on caller id name string"
[asterisk/asterisk.git] / res / res_stasis_device_state.c
index 1d135fe..344cb40 100644 (file)
@@ -23,8 +23,6 @@
 
 #include "asterisk.h"
 
-ASTERISK_REGISTER_FILE()
-
 #include "asterisk/astdb.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/module.h"
@@ -44,6 +42,9 @@ ASTERISK_REGISTER_FILE()
 /*! Number of hash buckets for device state subscriptions */
 #define DEVICE_STATE_BUCKETS 37
 
+/*! The key used for tracking a subscription to all device states */
+#define DEVICE_STATE_ALL "__AST_DEVICE_STATE_ALL_TOPIC"
+
 /*! Container for subscribed device states */
 static struct ao2_container *device_state_subscriptions;
 
@@ -105,18 +106,24 @@ static int device_state_subscriptions_cmp(void *obj, void *arg, int flags)
 static void device_state_subscription_destroy(void *obj)
 {
        struct device_state_subscription *sub = obj;
-       sub->sub = stasis_unsubscribe(sub->sub);
+       sub->sub = stasis_unsubscribe_and_join(sub->sub);
        ast_string_field_free_memory(sub);
 }
 
 static struct device_state_subscription *device_state_subscription_create(
        const struct stasis_app *app, const char *device_name)
 {
-       struct device_state_subscription *sub = ao2_alloc(
-               sizeof(*sub), device_state_subscription_destroy);
+       struct device_state_subscription *sub;
        const char *app_name = stasis_app_name(app);
-       size_t size = strlen(device_name) + strlen(app_name) + 2;
+       size_t size;
+
+       if (ast_strlen_zero(device_name)) {
+               device_name = DEVICE_STATE_ALL;
+       }
+
+       size = strlen(device_name) + strlen(app_name) + 2;
 
+       sub = ao2_alloc(sizeof(*sub), device_state_subscription_destroy);
        if (!sub) {
                return NULL;
        }
@@ -139,13 +146,13 @@ static struct device_state_subscription *find_device_state_subscription(
                .device_name = name
        };
 
-       return ao2_find(device_state_subscriptions, &dummy_sub, OBJ_SEARCH_OBJECT);
+       return ao2_find(device_state_subscriptions, &dummy_sub, OBJ_SEARCH_OBJECT | OBJ_NOLOCK);
 }
 
 static void remove_device_state_subscription(
        struct device_state_subscription *sub)
 {
-       ao2_unlink(device_state_subscriptions, sub);
+       ao2_unlink_flags(device_state_subscriptions, sub, OBJ_NOLOCK);
 }
 
 struct ast_json *stasis_app_device_state_to_json(
@@ -294,6 +301,12 @@ static void device_state_cb(void *data, struct stasis_subscription *sub,
 {
        struct ast_device_state_message *device_state;
 
+       if (stasis_subscription_final_message(sub, msg)) {
+               /* Remove stasis subscription's reference to device_state_subscription */
+               ao2_ref(data, -1);
+               return;
+       }
+
        if (ast_device_state_message_type() != stasis_message_type(msg)) {
                return;
        }
@@ -314,39 +327,91 @@ static void *find_device_state(const struct stasis_app *app, const char *name)
 
 static int is_subscribed_device_state(struct stasis_app *app, const char *name)
 {
-       RAII_VAR(struct device_state_subscription *, sub,
-                find_device_state_subscription(app, name), ao2_cleanup);
-       return sub != NULL;
+       struct device_state_subscription *sub;
+
+       sub = find_device_state_subscription(app, DEVICE_STATE_ALL);
+       if (sub) {
+               ao2_ref(sub, -1);
+               return 1;
+       }
+
+       sub = find_device_state_subscription(app, name);
+       if (sub) {
+               ao2_ref(sub, -1);
+               return 1;
+       }
+
+       return 0;
+}
+
+static int is_subscribed_device_state_lock(struct stasis_app *app, const char *name)
+{
+       int is_subscribed;
+
+       ao2_lock(device_state_subscriptions);
+       is_subscribed = is_subscribed_device_state(app, name);
+       ao2_unlock(device_state_subscriptions);
+
+       return is_subscribed;
 }
 
 static int subscribe_device_state(struct stasis_app *app, void *obj)
 {
        struct device_state_subscription *sub = obj;
+       struct stasis_topic *topic;
 
-       ast_debug(3, "Subscribing to device %s", sub->device_name);
+       if (!sub) {
+               sub = device_state_subscription_create(app, NULL);
+               if (!sub) {
+                       return -1;
+               }
+       }
+
+       if (strcmp(sub->device_name, DEVICE_STATE_ALL)) {
+               topic = ast_device_state_topic(sub->device_name);
+       } else {
+               topic = ast_device_state_topic_all();
+       }
+
+       ao2_lock(device_state_subscriptions);
 
        if (is_subscribed_device_state(app, sub->device_name)) {
+               ao2_unlock(device_state_subscriptions);
                ast_debug(3, "App %s is already subscribed to %s\n", stasis_app_name(app), sub->device_name);
                return 0;
        }
 
-       if (!(sub->sub = stasis_subscribe_pool(
-                       ast_device_state_topic(sub->device_name),
-                       device_state_cb, sub))) {
+       ast_debug(3, "Subscribing to device %s\n", sub->device_name);
+
+       sub->sub = stasis_subscribe_pool(topic, device_state_cb, ao2_bump(sub));
+       if (!sub->sub) {
+               ao2_unlock(device_state_subscriptions);
                ast_log(LOG_ERROR, "Unable to subscribe to device %s\n",
                        sub->device_name);
+               /* Reference we added when attempting to stasis_subscribe_pool */
+               ao2_ref(sub, -1);
                return -1;
        }
 
-       ao2_link(device_state_subscriptions, sub);
+       ao2_link_flags(device_state_subscriptions, sub, OBJ_NOLOCK);
+       ao2_unlock(device_state_subscriptions);
+
        return 0;
 }
 
 static int unsubscribe_device_state(struct stasis_app *app, const char *name)
 {
-       RAII_VAR(struct device_state_subscription *, sub,
-                find_device_state_subscription(app, name), ao2_cleanup);
-       remove_device_state_subscription(sub);
+       struct device_state_subscription *sub;
+
+       ao2_lock(device_state_subscriptions);
+       sub = find_device_state_subscription(app, name);
+       if (sub) {
+               remove_device_state_subscription(sub);
+       }
+       ao2_unlock(device_state_subscriptions);
+
+       ao2_cleanup(sub);
+
        return 0;
 }
 
@@ -379,7 +444,7 @@ struct stasis_app_event_source device_state_event_source = {
        .find = find_device_state,
        .subscribe = subscribe_device_state,
        .unsubscribe = unsubscribe_device_state,
-       .is_subscribed = is_subscribed_device_state,
+       .is_subscribed = is_subscribed_device_state_lock,
        .to_json = devices_to_json
 };
 
@@ -388,13 +453,14 @@ static int load_module(void)
        populate_cache();
        if (ast_devstate_prov_add(DEVICE_STATE_PROVIDER_STASIS,
                                  stasis_device_state_cb)) {
-               return AST_MODULE_LOAD_FAILURE;
+               return AST_MODULE_LOAD_DECLINE;
        }
 
        if (!(device_state_subscriptions = ao2_container_alloc(
                      DEVICE_STATE_BUCKETS, device_state_subscriptions_hash,
                      device_state_subscriptions_cmp))) {
-               return AST_MODULE_LOAD_FAILURE;
+               ast_devstate_prov_del(DEVICE_STATE_PROVIDER_STASIS);
+               return AST_MODULE_LOAD_DECLINE;
        }
 
        stasis_app_register_event_source(&device_state_event_source);