From 610ebc8896ee8aaab18cfa4689748083591ef51c Mon Sep 17 00:00:00 2001 From: Anupam Roy Date: Thu, 13 Aug 2020 15:53:08 +0530 Subject: [PATCH] Mesh: Add Node Reset API in FRWK Change-Id: I70a34fc3601861d383fb9c7a82028ce2ac45811b Signed-off-by: Anupam Roy --- bt-api/bt-mesh.c | 19 ++++++++ bt-service/services/bt-request-handler.c | 14 ++++++ .../include/bt-service-mesh-config-client.h | 3 ++ .../services/include/bt-service-mesh-network.h | 2 + .../services/mesh/bt-service-mesh-config-client.c | 57 ++++++++++++++++++++++ bt-service/services/mesh/bt-service-mesh-network.c | 35 +++++++++++++ bt-service/services/mesh/bt-service-mesh-nodes.c | 39 +++++++++++++++ include/bluetooth-mesh-api.h | 7 +++ include/bt-internal-types.h | 1 + 9 files changed, 177 insertions(+) diff --git a/bt-api/bt-mesh.c b/bt-api/bt-mesh.c index 02525a1..07af058 100644 --- a/bt-api/bt-mesh.c +++ b/bt-api/bt-mesh.c @@ -116,6 +116,25 @@ BT_EXPORT_API int bluetooth_mesh_network_destroy(bluetooth_mesh_network_t *netwo return result; } +BT_EXPORT_API int bluetooth_mesh_node_reset(bluetooth_mesh_node_info_t *node) +{ + int result; + + BT_CHECK_PARAMETER(node, return); + + BT_INIT_PARAMS(); + BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param); + + g_array_append_vals(in_param1, node, sizeof(bluetooth_mesh_node_info_t)); + + BT_INFO("Mesh: Reset the Node"); + result = _bt_send_request(BT_BLUEZ_SERVICE, BT_MESH_NODE_RESET, + in_param1, in_param2, in_param3, in_param4, &out_param); + BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param); + + return result; +} + BT_EXPORT_API int bluetooth_mesh_network_create( const char *net_name, bluetooth_mesh_node_t *node, uint16_t total_models, bluetooth_mesh_model_t **models, diff --git a/bt-service/services/bt-request-handler.c b/bt-service/services/bt-request-handler.c index 8d0fdbc..8ac2903 100644 --- a/bt-service/services/bt-request-handler.c +++ b/bt-service/services/bt-request-handler.c @@ -3955,6 +3955,19 @@ normal: requester_unique_creds, sender, &node, out_param1); break; } + case BT_MESH_NODE_RESET: { + bluetooth_mesh_node_info_t node; + memset(&node, 0x00, sizeof(bluetooth_mesh_node_info_t)); + + __bt_service_get_parameters(in_param1, + &node, sizeof(bluetooth_mesh_node_info_t)); + + BT_INFO("Mesh: Reset the Node"); + result = _bt_mesh_network_reset_node(requester_unique_creds, + sender, &node); + + break; + } case BT_MESH_MODEL_GET_APPKEY_LIST: { bluetooth_mesh_model_configure_t req; @@ -5130,6 +5143,7 @@ gboolean __bt_service_check_privilege(int function_name, case BT_MESH_NODE_TTL_EXECUTE: case BT_MESH_NODE_GET_NETKEYS: case BT_MESH_NODE_GET_APPKEYS: + case BT_MESH_NODE_RESET: case BT_MESH_MODEL_CONFIGURE_APPKEY: case BT_MESH_MODEL_GET_APPKEY_LIST: case BT_MESH_MODEL_EXECUTE_MSG: diff --git a/bt-service/services/include/bt-service-mesh-config-client.h b/bt-service/services/include/bt-service-mesh-config-client.h index 0e8f21f..e534ba2 100644 --- a/bt-service/services/include/bt-service-mesh-config-client.h +++ b/bt-service/services/include/bt-service-mesh-config-client.h @@ -41,6 +41,9 @@ int _bt_mesh_browse_remote_node(const char *app_cred, const char *sender, bluetooth_mesh_node_discover_t *req); +int _bt_mesh_network_reset_node(const char *app_cred, const char *sender, + bluetooth_mesh_node_info_t *node); + bool _bt_mesh_check_pending_request(uint32_t opcode, uint16_t dest, uint8_t net_uuid[]); diff --git a/bt-service/services/include/bt-service-mesh-network.h b/bt-service/services/include/bt-service-mesh-network.h index 341964f..4300455 100644 --- a/bt-service/services/include/bt-service-mesh-network.h +++ b/bt-service/services/include/bt-service-mesh-network.h @@ -40,6 +40,8 @@ int _bt_mesh_network_destroy(const char *app_cred, int _bt_mesh_network_remove_net_configuration(bluetooth_mesh_network_t *net); +int _bt_mesh_network_remove_node_configuration(bluetooth_mesh_node_info_t *node); + int _bt_mesh_network_create_cdb(int result, const char *sender, const char *app_creds, uint8_t uuid[16], uint8_t token[8], diff --git a/bt-service/services/mesh/bt-service-mesh-config-client.c b/bt-service/services/mesh/bt-service-mesh-config-client.c index f450f26..9999f10 100644 --- a/bt-service/services/mesh/bt-service-mesh-config-client.c +++ b/bt-service/services/mesh/bt-service-mesh-config-client.c @@ -2311,6 +2311,63 @@ int _bt_mesh_node_model_get_appkey_list(const char *app_cred, const char *sender return BLUETOOTH_ERROR_NONE; } +int _bt_mesh_network_reset_node(const char *app_cred, const char *sender, + bluetooth_mesh_node_info_t *node) +{ + int ret = OAL_STATUS_SUCCESS; + uint16_t netkey_idx; + oal_uuid_t net_uuid; + uint16_t data_len; + uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN]; + + /* If Scanning is going on */ + if (_bt_mesh_is_provisioning() || + _bt_mesh_is_scanning()) { + BT_ERR("Device is buzy.."); + return BLUETOOTH_ERROR_DEVICE_BUSY; + } + + _bt_mesh_util_convert_string_to_hex(node->net_uuid, + strlen(node->net_uuid), net_uuid.uuid, 16); + + /* Check pending request */ + if (_bt_mesh_check_pending_request(MESH_OPCODE_NODE_RESET, + node->primary_unicast, net_uuid.uuid)) { + BT_ERR("Device is buzy.."); + return BLUETOOTH_ERROR_DEVICE_BUSY; + } + BT_INFO("Mesh: Reset a remote Node [0x%2.2x] Elem Cnt [0x%2.2x]", + node->primary_unicast, node->num_elements); + /* Get Subnet index of the remote node for TX encryption */ + netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, node->primary_unicast); + if (netkey_idx == MESH_NET_IDX_INVALID) + return BLUETOOTH_ERROR_INTERNAL; + + data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_NODE_RESET, buffer); + + /* Send Node Reset command 5 times to ensure reliability */ + for (int i = 0; i < 5; i++) + ret = mesh_conf_send_message(&net_uuid, node->primary_unicast, + true, netkey_idx, buffer, data_len); + + if (ret != OAL_STATUS_SUCCESS) { + BT_ERR("Mesh: Node Reset Failed!: %d", ret); + return BLUETOOTH_ERROR_INTERNAL; + } + + BT_INFO("Mesh: Node reset Done, Send Delete req: Unicast[0x%2.2x]", + node->primary_unicast); + + ret = mesh_delete_remote_node(&net_uuid, node->primary_unicast, + node->num_elements); + if (ret != OAL_STATUS_SUCCESS) { + BT_ERR("Mesh: Remote Node Deletion Failed!: %d", ret); + return BLUETOOTH_ERROR_INTERNAL; + } + + return _bt_mesh_network_remove_node_configuration(node); +} + int _bt_mesh_node_model_appkey_execute(const char *app_cred, const char *sender, bluetooth_mesh_model_configure_t *req) { diff --git a/bt-service/services/mesh/bt-service-mesh-network.c b/bt-service/services/mesh/bt-service-mesh-network.c index 9b32131..976e5a8 100644 --- a/bt-service/services/mesh/bt-service-mesh-network.c +++ b/bt-service/services/mesh/bt-service-mesh-network.c @@ -188,6 +188,41 @@ int _bt_mesh_network_request_provisioning_data_request(uint8_t net_uuid[], return BLUETOOTH_ERROR_NONE; } +int _bt_mesh_network_remove_node_configuration( + bluetooth_mesh_node_info_t *node) +{ + GSList *l; + _bt_mesh_cdb_t *cdb_cfg = NULL; + uint8_t net_uuid[16]; + BT_INFO("Mesh: Remove Node Configuration: Unicast [0x%2.2x]", + node->primary_unicast); + + _bt_mesh_util_convert_string_to_hex(node->net_uuid, + strlen(node->net_uuid), net_uuid, 16); + + /* Find CDB */ + l = g_slist_find_custom(cdb_list, net_uuid, + __mesh_compare_app_network_uuid); + if (!l) { + BT_ERR("Mesh: Could not find Network Entry: unexpected!!"); + return BLUETOOTH_ERROR_INVALID_PARAM; + } + + cdb_cfg = (_bt_mesh_cdb_t*)l->data; + + if (_bt_mesh_conf_delete_node(cdb_cfg, node->primary_unicast)) { + BT_INFO("Mesh: Node Entry deleted from Config DB"); + if (!_bt_mesh_node_del_node(cdb_cfg, node->primary_unicast)) { + BT_ERR("Mesh: Node Entry could not be unloaded from memory"); + return BLUETOOTH_ERROR_INTERNAL; + } + } else { + BT_ERR("Mesh: Node Entry could not be deleted from Config DB"); + return BLUETOOTH_ERROR_INTERNAL; + } + return BLUETOOTH_ERROR_NONE; +} + int _bt_mesh_network_remove_net_configuration( bluetooth_mesh_network_t *net) { diff --git a/bt-service/services/mesh/bt-service-mesh-nodes.c b/bt-service/services/mesh/bt-service-mesh-nodes.c index 5bae17f..ca76db9 100644 --- a/bt-service/services/mesh/bt-service-mesh-nodes.c +++ b/bt-service/services/mesh/bt-service-mesh-nodes.c @@ -258,6 +258,45 @@ bool _bt_mesh_node_get_element_count(uint8_t net_uuid[], return true; } +uint8_t _bt_mesh_node_del_node(_bt_mesh_cdb_t *cfg, + uint16_t unicast) +{ + struct _bt_mesh_node_t *rmt; + uint8_t num_ele, i; + struct mesh_network_t *network; + + if (!cfg) + return 0; + + BT_INFO("Mesh: Uload Node Entry [0x%2.2x]", unicast); + network = l_queue_find(networks, __mesh_net_uuid_match, cfg->uuid); + if (!network) + return 0; + + rmt = l_queue_find(network->nodes, + __mesh_match_node_addr, L_UINT_TO_PTR(unicast)); + if (!rmt) + return 0; + + rmt = l_queue_remove_if(network->nodes, __mesh_match_node_addr, + L_UINT_TO_PTR(unicast)); + if (!rmt) + return 0; + + num_ele = rmt->num_ele; + + for (i = 0; i < num_ele; ++i) + l_queue_destroy(rmt->els[i], NULL); + + l_free(rmt->els); + + l_queue_destroy(rmt->net_keys, NULL); + l_queue_destroy(rmt->app_keys, NULL); + l_free(rmt); + + return num_ele; +} + bool _bt_mesh_node_add_node(uint8_t net_uuid[], const uint8_t node_uuid[16], uint16_t unicast, uint8_t ele_cnt, uint16_t net_idx) diff --git a/include/bluetooth-mesh-api.h b/include/bluetooth-mesh-api.h index 081bd0b..eaf5d58 100644 --- a/include/bluetooth-mesh-api.h +++ b/include/bluetooth-mesh-api.h @@ -229,7 +229,9 @@ typedef struct { } bluetooth_mesh_authentication_request_t; typedef struct { + char net_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1]; uint16_t primary_unicast; + uint16_t num_elements; char dev_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1]; } bluetooth_mesh_node_info_t; @@ -259,6 +261,11 @@ int bluetooth_mesh_network_create(const char *net_name, int bluetooth_mesh_network_destroy(bluetooth_mesh_network_t *network); /** + * Reset a remote Node & remove it from network + */ +int bluetooth_mesh_node_reset(bluetooth_mesh_node_info_t *node); + +/** * Load already created Network */ int bluetooth_mesh_network_load(const char *token, diff --git a/include/bt-internal-types.h b/include/bt-internal-types.h index 536dd4c..9a38ef9 100644 --- a/include/bt-internal-types.h +++ b/include/bt-internal-types.h @@ -493,6 +493,7 @@ typedef enum { BT_MESH_NODE_TTL_EXECUTE, BT_MESH_NODE_GET_NETKEYS, BT_MESH_NODE_GET_APPKEYS, + BT_MESH_NODE_RESET, BT_MESH_MODEL_CONFIGURE_APPKEY, BT_MESH_MODEL_GET_APPKEY_LIST, BT_MESH_MODEL_EXECUTE_MSG, -- 2.7.4