mesh: Add destroy callback to dbus_send_with_timeout()
authorInga Stotland <inga.stotland@intel.com>
Wed, 10 Jun 2020 17:11:19 +0000 (10:11 -0700)
committerAbhay Agarwal <ay.agarwal@samsung.com>
Fri, 18 Dec 2020 05:40:30 +0000 (11:10 +0530)
This adds a destroy callback as a function parameter to
dbus_send_with_timeout() to allow automatic release of user data
on either reply or timeout.

Change-Id: Id97ac8108066c58130a2c1ee629d197a8ad8cb67
Signed-off-by: anuj.bhumiya <anuj.bhumiya@samsung.com>
mesh/dbus.c
mesh/dbus.h
mesh/mesh.c

index 83ae22c..63ea420 100644 (file)
@@ -41,6 +41,7 @@ struct send_info {
        struct l_dbus *dbus;
        struct l_timeout *timeout;
        l_dbus_message_func_t cb;
+       l_dbus_destroy_func_t destroy;
        void *user_data;
        uint32_t serial;
 };
@@ -159,6 +160,10 @@ static void send_reply(struct l_dbus_message *message, void *user_data)
 
        l_timeout_remove(info->timeout);
        info->cb(message, info->user_data);
+
+       if (info->destroy)
+               info->destroy(info->user_data);
+
        l_free(info);
 }
 
@@ -173,6 +178,7 @@ static void send_timeout(struct l_timeout *timeout, void *user_data)
 void dbus_send_with_timeout(struct l_dbus *dbus, struct l_dbus_message *msg,
                                                l_dbus_message_func_t cb,
                                                void *user_data,
+                                               l_dbus_destroy_func_t destroy,
                                                unsigned int seconds)
 {
        struct send_info *info = l_new(struct send_info, 1);
@@ -180,6 +186,7 @@ void dbus_send_with_timeout(struct l_dbus *dbus, struct l_dbus_message *msg,
        info->dbus = dbus;
        info->cb = cb;
        info->user_data = user_data;
+       info->destroy = destroy;
        info->serial = l_dbus_send_with_reply(dbus, msg, send_reply,
                                                                info, NULL);
        info->timeout = l_timeout_create(seconds, send_timeout, info, NULL);
index aafb85f..89d6b1d 100644 (file)
@@ -36,4 +36,5 @@ struct l_dbus_message *dbus_error(struct l_dbus_message *msg, int err,
 void dbus_send_with_timeout(struct l_dbus *dbus, struct l_dbus_message *msg,
                                                l_dbus_message_func_t cb,
                                                void *user_data,
+                                               l_dbus_destroy_func_t destroy,
                                                unsigned int seconds);
index c744ddf..7a13f40 100644 (file)
@@ -438,12 +438,12 @@ static void send_join_failed(const char *owner, const char *path,
        free_pending_join_call(true);
 }
 
-static void prov_join_complete_reply_cb(struct l_dbus_message *message,
+static void prov_join_complete_reply_cb(struct l_dbus_message *msg,
                                                                void *user_data)
 {
        bool failed = false;
 
-       if (!message || l_dbus_message_is_error(message))
+       if (!msg || l_dbus_message_is_error(msg))
                failed = true;
 
        if (!failed)
@@ -487,7 +487,7 @@ static bool prov_complete_cb(void *user_data, uint8_t status,
 
        l_dbus_message_set_arguments(msg, "t", l_get_be64(token));
        dbus_send_with_timeout(dbus, msg, prov_join_complete_reply_cb,
-                                               NULL, DEFAULT_DBUS_TIMEOUT);
+                                               NULL, NULL, DEFAULT_DBUS_TIMEOUT);
 
        return true;
 }
@@ -688,12 +688,12 @@ static struct l_dbus_message *release_call(struct l_dbus *dbus,
 }
 #endif
 
-static void create_join_complete_reply_cb(struct l_dbus_message *message,
+static void create_join_complete_reply_cb(struct l_dbus_message *msg,
                                                                void *user_data)
 {
        struct mesh_node *node = user_data;
 
-       if (!message || l_dbus_message_is_error(message)) {
+        if (!msg || l_dbus_message_is_error(msg)) {
                node_remove(node);
                return;
        }
@@ -738,7 +738,7 @@ static void create_node_ready_cb(void *user_data, int status,
 
        l_dbus_message_set_arguments(msg, "t", l_get_be64(token));
        dbus_send_with_timeout(dbus, msg, create_join_complete_reply_cb,
-                                               node, DEFAULT_DBUS_TIMEOUT);
+                                               node, NULL, DEFAULT_DBUS_TIMEOUT);
        l_dbus_message_unref(pending_msg);
 }