From d3921346a9a7f62229e4659f8aea714d5e6b5770 Mon Sep 17 00:00:00 2001 From: lly Date: Fri, 22 May 2020 20:25:44 +0800 Subject: [PATCH] ble_mesh: Same func for storing/restoring comp data --- .../esp_ble_mesh/mesh_core/provisioner_main.c | 58 +++++++------------ 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c index feaf93a95..58feece1f 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c @@ -495,33 +495,6 @@ int bt_mesh_provisioner_restore_node_name(u16_t addr, const char *name) return 0; } -int bt_mesh_provisioner_restore_node_comp_data(u16_t addr, const u8_t *data, u16_t length) -{ - struct bt_mesh_node *node = NULL; - - if (!data || length == 0U) { - BT_ERR("%s, Invalid comp data info", __func__); - return -EINVAL; - } - - node = provisioner_find_node_with_addr(addr, NULL); - if (node == NULL) { - BT_ERR("%s, Node not exists, addr 0x%04x", __func__, addr); - return -ENODEV; - } - - node->comp_data = bt_mesh_calloc(length); - if (!node->comp_data) { - BT_ERR("%s, Failed to allocate memory", __func__); - return -ENOMEM; - } - - node->comp_length = length; - memcpy(node->comp_data, data, length); - - return 0; -} - struct bt_mesh_node *bt_mesh_provisioner_get_node_with_uuid(const u8_t uuid[16]) { return provisioner_find_node_with_uuid(uuid, NULL); @@ -676,18 +649,23 @@ u16_t bt_mesh_provisioner_get_node_index(const char *name) return BLE_MESH_INVALID_NODE_INDEX; } -int bt_mesh_provisioner_store_node_comp_data(u16_t addr, const u8_t *data, u16_t length) +#define COMP_DATA_PAGE_0_MIN_LEN 16 + +static int store_node_comp_data(u16_t addr, const u8_t *data, u16_t length, bool store) { struct bt_mesh_node *node = NULL; - u16_t index = 0U; - if (!BLE_MESH_ADDR_IS_UNICAST(addr) || !data || - (length % 2) || length <= 14) { - BT_ERR("%s, Invalid parameter", __func__); + if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, addr); return -EINVAL; } - node = provisioner_find_node_with_addr(addr, &index); + if (data == NULL || (length % 2) || length < COMP_DATA_PAGE_0_MIN_LEN) { + BT_ERR("%s, Invalid composition data", __func__); + return -EINVAL; + } + + node = provisioner_find_node_with_addr(addr, NULL); if (node == NULL) { BT_ERR("%s, Node not exists, addr 0x%04x", __func__, addr); return -ENODEV; @@ -699,18 +677,26 @@ int bt_mesh_provisioner_store_node_comp_data(u16_t addr, const u8_t *data, u16_t return -ENOMEM; } - BT_DBG("node index %d", index); - memcpy(node->comp_data, data, length); node->comp_length = length; - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) { bt_mesh_store_node_comp_data(node); } return 0; } +int bt_mesh_provisioner_store_node_comp_data(u16_t addr, const u8_t *data, u16_t length) +{ + return store_node_comp_data(addr, data, length, true); +} + +int bt_mesh_provisioner_restore_node_comp_data(u16_t addr, const u8_t *data, u16_t length) +{ + return store_node_comp_data(addr, data, length, false); +} + /* Provisioner DevKey, NetKey and AppKey related functions */ const u8_t *bt_mesh_provisioner_net_key_get(u16_t net_idx)