mesh: Change API to deliver tokens via JoinComplete 13/234213/1
authorPrzemysław Fierek <przemyslaw.fierek@silvair.com>
Tue, 14 Apr 2020 08:55:55 +0000 (10:55 +0200)
committerAbhay Agarwal <ay.agarwal@samsung.com>
Fri, 22 May 2020 04:23:43 +0000 (09:53 +0530)
This patch changes Import and CreateNetwork API to deliver tokens via
the JoinComplete method call.  When application doesn't raise any error
during handling JoinComplete then it is assumed that the token has been
saved, otherwise when application replies with an error message then the
node is removed.

Change-Id: I76b7e61ede93aac732bb24ff6948ff75108d939e
Signed-off-by: Abhay Agarwal <ay.agarwal@samsung.com>
mesh/mesh.c

index ecb49b7..807b152 100644 (file)
@@ -428,6 +428,20 @@ 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,
+                                                               void *user_data)
+{
+       bool failed = false;
+
+       if (!message || l_dbus_message_is_error(message))
+               failed = true;
+
+       if (!failed)
+               node_attach_io(join_pending->node, mesh.io);
+
+       free_pending_join_call(failed);
+}
+
 static bool prov_complete_cb(void *user_data, uint8_t status,
                                        struct mesh_prov_node_info *info)
 {
@@ -454,18 +468,16 @@ static bool prov_complete_cb(void *user_data, uint8_t status,
                return false;
        }
 
-       node_attach_io(join_pending->node, mesh.io);
        token = node_get_token(join_pending->node);
 
+       l_debug("Calling JoinComplete (prov)");
        msg = l_dbus_message_new_method_call(dbus, owner, path,
                                                MESH_APPLICATION_INTERFACE,
                                                "JoinComplete");
 
        l_dbus_message_set_arguments(msg, "t", l_get_be64(token));
-
-       l_dbus_send(dbus, msg);
-
-       free_pending_join_call(false);
+       l_dbus_send_with_reply(dbus, msg,
+                               prov_join_complete_reply_cb, NULL, NULL);
 
        return true;
 }
@@ -659,11 +671,28 @@ static struct l_dbus_message *leave_call(struct l_dbus *dbus,
        return l_dbus_message_new_method_return(msg);
 }
 
+static void create_join_complete_reply_cb(struct l_dbus_message *message,
+                                                               void *user_data)
+{
+       struct mesh_node *node = user_data;
+
+       if (!message || l_dbus_message_is_error(message)) {
+               node_remove(node);
+               return;
+       }
+
+       node_attach_io(node, mesh.io);
+}
+
 static void create_node_ready_cb(void *user_data, int status,
                                                        struct mesh_node *node)
 {
+       struct l_dbus *dbus = dbus_get_bus();
        struct l_dbus_message *reply;
        struct l_dbus_message *pending_msg;
+       struct l_dbus_message *msg;
+       const char *owner;
+       const char *path;
        const uint8_t *token;
 
        pending_msg = l_queue_find(pending_queue, simple_match, user_data);
@@ -672,20 +701,29 @@ static void create_node_ready_cb(void *user_data, int status,
 
        if (status != MESH_ERROR_NONE) {
                reply = dbus_error(pending_msg, status, NULL);
-               goto done;
-       }
 
-       node_attach_io(node, mesh.io);
+               l_dbus_send(dbus_get_bus(), reply);
+               l_queue_remove(pending_queue, pending_msg);
+               return;
+       }
 
        reply = l_dbus_message_new_method_return(pending_msg);
+
+       l_dbus_send(dbus, reply);
+       l_queue_remove(pending_queue, pending_msg);
+
+       owner = l_dbus_message_get_sender(pending_msg);
+       path = node_get_app_path(node);
        token = node_get_token(node);
 
-       l_debug();
-       l_dbus_message_set_arguments(reply, "t", l_get_be64(token));
+       l_debug("Calling JoinComplete (create)");
+       msg = l_dbus_message_new_method_call(dbus, owner, path,
+                                               MESH_APPLICATION_INTERFACE,
+                                               "JoinComplete");
 
-done:
-       l_dbus_send(dbus_get_bus(), reply);
-       l_queue_remove(pending_queue, pending_msg);
+       l_dbus_message_set_arguments(msg, "t", l_get_be64(token));
+       l_dbus_send_with_reply(dbus, msg,
+                               create_join_complete_reply_cb, node, NULL);
 }
 
 static struct l_dbus_message *create_network_call(struct l_dbus *dbus,
@@ -839,11 +877,11 @@ static void setup_network_interface(struct l_dbus_interface *iface)
                                                                "token");
 
        l_dbus_interface_method(iface, "CreateNetwork", 0, create_network_call,
-                                       "t", "oay", "token", "app", "uuid");
+                                       "", "oay", "app", "uuid");
 
        l_dbus_interface_method(iface, "Import", 0,
                                        import_call,
-                                       "t", "oayayayqa{sv}uq", "token",
+                                       "", "oayayayqa{sv}uq",
                                        "app", "uuid", "dev_key", "net_key",
                                        "net_index", "flags", "iv_index",
                                        "unicast");