Mesh: Add support for Mesh network Destroy 05/240805/1
authorAnupam Roy <anupam.r@samsung.com>
Tue, 11 Aug 2020 14:27:09 +0000 (19:57 +0530)
committerAnupam Roy <anupam.r@samsung.com>
Tue, 11 Aug 2020 14:27:09 +0000 (19:57 +0530)
Change-Id: Ie2e8dd2f038567ea2f2c235b96885b6ac44a786d
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
include/bluetooth_internal.h
src/bluetooth-mesh.c

index f255de5..e3ffdc7 100644 (file)
@@ -4488,6 +4488,24 @@ int bt_mesh_network_create(bt_mesh_node_h config_client,
                        bt_mesh_network_h *network,
                                char **token);
 
+
+/**
+ * @internal
+ * @ingroup CAPI_NETWORK_BLUETOOTH_MESH_MODULE
+ * @brief Call to destroy a Mesh Network.
+ *
+ * @param: in: network, Network handle
+ *
+ * @remarks: If it is a local Mesh provisioner network, it is
+ * recommended to reset the provisioned nodes before destroying the
+ * network, otherwise, provisioned nodes will be orphaned and need to be
+ * possibly hard-resetted, to make them advertise again as device.
+ *
+ * @since_tizen 6.0
+ * @privlevel platform
+ */
+int bt_mesh_network_destroy(bt_mesh_network_h network);
+
 /**
  * @internal
  * @ingroup CAPI_NETWORK_BLUETOOTH_MESH_MODULE
index 8742cc9..a8d09ae 100644 (file)
@@ -344,6 +344,13 @@ static void __bt_mesh_free_elements(void *data)
        g_free(elem);
 }
 
+static void __bt_mesh_free_appkeys(void *data)
+{
+       bt_mesh_appkey_s *appkey = (bt_mesh_appkey_s*)data;
+       appkey_list = g_slist_remove(appkey_list, appkey);
+       g_free(appkey);
+}
+
 static gint __bt_mesh_compare_net_uuid(gpointer *a, gpointer *b)
 {
        bt_mesh_network_s *net = (bt_mesh_network_s *)a;
@@ -737,6 +744,58 @@ int bt_mesh_node_destroy(bt_mesh_node_h node_handle)
        return BT_ERROR_NONE;
 }
 
+static void __bt_mesh_destroy_network_handles(bt_mesh_network_s *net)
+{
+       GSList *l;
+
+       if (!net)
+               return;
+
+       BT_INFO("Mesh: Destroy handles of Network [%s] UUID [%s] Token [%s]",
+                       net->name, net->uuid, net->token);
+
+       /* Remove all Nodes of Network */
+       BT_INFO("Mesh: Total nodes present in Network [%d]",
+               g_slist_length(net->nodes));
+       for (l = net->nodes; l != NULL; l = l->next) {
+               bt_mesh_node_s *node_s = (bt_mesh_node_s*)l->data;
+
+               net->nodes = g_slist_remove(net->nodes, node_s);
+               node_list = g_slist_remove(node_list, node_s);
+               BT_INFO("Mesh: Total elements present in Node [%d]",
+                       g_slist_length(node_s->elements));
+
+               g_slist_free_full(node_s->elements, __bt_mesh_free_elements);
+               g_free(node_s);
+       }
+
+       /* Remove all Netkeys & Appkeys of Network */
+       BT_INFO("Mesh: Total netkeys present in Network [%d]",
+               g_slist_length(net->netkeys));
+       for (l = net->netkeys; l != NULL; l = l->next) {
+               bt_mesh_netkey_s *netkey_s = (bt_mesh_netkey_s*)l->data;
+
+               net->netkeys = g_slist_remove(net->netkeys, netkey_s);
+               netkey_list = g_slist_remove(netkey_list, netkey_s);
+               BT_INFO("Mesh: Total appkeys present in Netkey [%d]",
+                       g_slist_length(netkey_s->appkeys));
+               g_slist_free_full(netkey_s->appkeys, __bt_mesh_free_appkeys);
+               g_free(netkey_s);
+       }
+
+       /* Remove all groups of Network */
+       BT_INFO("Mesh: Total groups present in Network [%d]",
+               g_slist_length(net->groups));
+       for (l = net->groups; l != NULL; l = l->next) {
+               bt_mesh_group_s *group_s = (bt_mesh_group_s*)l->data;
+
+               net->groups = g_slist_remove(net->groups, group_s);
+               group_list = g_slist_remove(group_list, group_s);
+               g_free(group_s);
+       }
+       BT_INFO("Mesh: Handle Removal Done!");
+}
+
 static void __bt_mesh_generate_element(bt_mesh_node_s *node_s,
                bt_mesh_element_s *elem_s)
 {
@@ -1016,6 +1075,50 @@ int bt_mesh_appkey_get_netkey(bt_mesh_appkey_h appkey_handle,
        return BT_ERROR_NONE;
 }
 
+int bt_mesh_network_destroy(bt_mesh_network_h network)
+{
+       FUNC_ENTRY;
+
+       int error_code = BT_ERROR_NONE;
+       bt_mesh_network_s *network_s;
+       bluetooth_mesh_network_t net;
+
+       BT_CHECK_MESH_SUPPORT();
+       BT_CHECK_MESH_INIT_STATUS();
+       BT_CHECK_INPUT_PARAMETER(network);
+       BT_MESH_VALIDATE_HANDLE(network, networks);
+
+       network_s = (bt_mesh_network_s*) network;
+       memset(&net, 0x00, sizeof(bluetooth_mesh_network_t));
+
+       g_strlcpy(net.uuid, network_s->uuid, 33);
+       g_strlcpy(net.token.token, network_s->token, 17);
+       g_strlcpy(net.name.name, network_s->name, BT_MESH_NETWORK_NAME_STRING_MAX_LEN + 1);
+
+       BT_INFO("Mesh: Destroy Network: Name [%s]", network_s->name);
+
+       BT_INFO("Mesh: Send Network Destroy Request to FRWK");
+       error_code = _bt_get_error_code(bluetooth_mesh_network_destroy(&net));
+       if (error_code != BT_ERROR_NONE) {
+               BT_INFO("Mesh: Network Destroy Failed!!");
+               BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);
+
+               FUNC_EXIT;
+               return error_code;
+       }
+
+       BT_INFO("Mesh: Network Destroyed successfully");
+
+       __bt_mesh_destroy_network_handles(network_s);
+       networks = g_slist_remove(networks, network_s);
+       g_free(network_s);
+
+       BT_INFO("Mesh: Current number of networks after removing [%d]",
+                       g_slist_length(networks));
+       FUNC_EXIT;
+       return error_code;
+}
+
 /* Async API's to bt-service & stack */
 int bt_mesh_network_create(bt_mesh_node_h config_client,
                const char *network_name,
@@ -1484,7 +1587,8 @@ int bt_mesh_netkey_delete(bt_mesh_netkey_h netkey)
 
        network_s->netkeys = g_slist_remove(network_s->netkeys, netkey_s);
        netkey_list = g_slist_remove(netkey_list, netkey_s);
-       g_slist_free_full(netkey_s->appkeys, g_free);
+       g_slist_free_full(netkey_s->appkeys, __bt_mesh_free_appkeys);
+       g_free(netkey_s);
 
        FUNC_EXIT;
        return BT_ERROR_NONE;