diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c index 2875f7fa93..bd049d1a28 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c @@ -287,22 +287,86 @@ uint16_t esp_ble_mesh_provisioner_get_node_index(const char *name) return bt_mesh_provisioner_get_node_index(name); } -esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_info(const uint8_t uuid[16]) +esp_err_t esp_ble_mesh_provisioner_store_node_comp_data(uint16_t unicast_addr, uint8_t *data, uint16_t length) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(unicast_addr) || !data || length <= 14) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA; + + arg.store_node_comp_data.unicast_addr = unicast_addr; + arg.store_node_comp_data.length = length; + arg.store_node_comp_data.data = data; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), btc_ble_mesh_prov_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16]) { if (!uuid) { return NULL; } - return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_prov_node_info(uuid); + return btc_ble_mesh_provisioner_get_node_with_uuid(uuid); } -esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_info_with_addr(uint16_t unicast_addr) +esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr) { if (!ESP_BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) { return NULL; } - return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_info(unicast_addr); + return btc_ble_mesh_provisioner_get_node_with_addr(unicast_addr); +} + +esp_err_t esp_ble_mesh_provisioner_delete_node_with_uuid(const uint8_t uuid[16]) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!uuid) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_UUID; + + memcpy(arg.delete_node_with_uuid.uuid, uuid, 16); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_delete_node_with_addr(uint16_t unicast_addr) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_ADDR; + + arg.delete_node_with_addr.unicast_addr = unicast_addr; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } esp_err_t esp_ble_mesh_provisioner_add_local_app_key(const uint8_t app_key[16], @@ -387,28 +451,6 @@ const uint8_t *esp_ble_mesh_provisioner_get_local_net_key(uint16_t net_idx) return bt_mesh_provisioner_local_net_key_get(net_idx); } -esp_err_t esp_ble_mesh_provisioner_store_node_comp_data(uint16_t addr, uint8_t *data, uint16_t length) -{ - btc_ble_mesh_prov_args_t arg = {0}; - btc_msg_t msg = {0}; - - if (!ESP_BLE_MESH_ADDR_IS_UNICAST(addr) || !data || length <= 14) { - return ESP_ERR_INVALID_ARG; - } - - ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); - - msg.sig = BTC_SIG_API_CALL; - msg.pid = BTC_PID_PROV; - msg.act = BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA; - - arg.store_node_comp_data.addr = addr; - arg.store_node_comp_data.length = length; - arg.store_node_comp_data.data = data; - return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), btc_ble_mesh_prov_arg_deep_copy) - == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); -} - #endif /* CONFIG_BLE_MESH_PROVISIONER */ #if (CONFIG_BLE_MESH_FAST_PROV) diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h index a8521e0c5a..b1eecf9b9c 100644 --- a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h @@ -212,6 +212,18 @@ const char *esp_ble_mesh_provisioner_get_node_name(uint16_t index); */ uint16_t esp_ble_mesh_provisioner_get_node_index(const char *name); +/** + * @brief This function is called to store the Composition Data of the node. + * + * @param[in] addr: Element address of the node + * @param[in] data: Pointer of Composition Data + * @param[in] length: Length of Composition Data + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_store_node_comp_data(uint16_t unicast_addr, uint8_t *data, uint16_t length); + /** * @brief This function is called to get the provisioned node information * with the node device uuid. @@ -221,7 +233,7 @@ uint16_t esp_ble_mesh_provisioner_get_node_index(const char *name); * @return Pointer of the node info struct or NULL on failure. * */ -esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_info(const uint8_t uuid[16]); +esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16]); /** * @brief This function is called to get the provisioned node information @@ -232,19 +244,29 @@ esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_info(const uint8_t uuid[1 * @return Pointer of the node info struct or NULL on failure. * */ -esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_info_with_addr(uint16_t unicast_addr); +esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr); /** - * @brief This function is called to store the Composition Data of the node. + * @brief This function is called to delete the provisioned node information + * with the node device uuid. * - * @param[in] addr: Element address of the node - * @param[in] data: Pointer of Composition Data - * @param[in] length: Length of Composition Data + * @param[in] uuid: Device UUID of the node * * @return ESP_OK on success or error code otherwise. * */ -esp_err_t esp_ble_mesh_provisioner_store_node_comp_data(uint16_t addr, uint8_t *data, uint16_t length); +esp_err_t esp_ble_mesh_provisioner_delete_node_with_uuid(const uint8_t uuid[16]); + +/** + * @brief This function is called to delete the provisioned node information + * with the node unicast address. + * + * @param[in] unicast_addr: Unicast address of the node + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_delete_node_with_addr(uint16_t unicast_addr); /** * @brief This function is called to set the app key for the local BLE Mesh stack. diff --git a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h index 635f189e53..5e25203e1c 100644 --- a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h +++ b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h @@ -764,6 +764,8 @@ typedef enum { ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT, /*!< Provisioner bind local model with local app key completion event */ ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_NET_KEY_COMP_EVT, /*!< Provisioner add local network key completion event */ ESP_BLE_MESH_PROVISIONER_STORE_NODE_COMP_DATA_COMP_EVT, /*!< Provisioner store node composition data completion event */ + ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT, /*!< Provisioner delete node with uuid completion event */ + ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT, /*!< Provisioner delete node with unicast address completion event */ ESP_BLE_MESH_SET_FAST_PROV_INFO_COMP_EVT, /*!< Set fast provisioning information (e.g. unicast address range, net_idx, etc.) completion event */ ESP_BLE_MESH_SET_FAST_PROV_ACTION_COMP_EVT, /*!< Set fast provisioning action completion event */ ESP_BLE_MESH_HEARTBEAT_MESSAGE_RECV_EVT, /*!< Receive Heartbeat message event */ @@ -1071,6 +1073,20 @@ typedef union { int err_code; /*!< Indicate the result of storing node composition data by the Provisioner */ uint16_t addr; /*!< Node element address */ } provisioner_store_node_comp_data_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_STORE_NODE_COMP_DATA_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT + */ + struct ble_mesh_provisioner_delete_node_with_uuid_comp_data_comp_param { + int err_code; /*!< Indicate the result of deleting node with uuid by the Provisioner */ + uint8_t uuid[16]; /*!< Node device uuid */ + } provisioner_delete_node_with_uuid_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT + */ + struct ble_mesh_provisioner_delete_node_with_addr_comp_data_comp_param { + int err_code; /*!< Indicate the result of deleting node with unicast address by the Provisioner */ + uint16_t unicast_addr; /*!< Node unicast address */ + } provisioner_delete_node_with_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT */ /** * @brief ESP_BLE_MESH_SET_FAST_PROV_INFO_COMP_EVT */ diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c index c1b343b6c1..1b96927f0c 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c @@ -771,6 +771,17 @@ static void btc_ble_mesh_provisioner_prov_complete_cb( btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT); return; } + +esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16]) +{ + return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_with_uuid(uuid); +} + +esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr) +{ + return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_with_addr(unicast_addr); +} + #endif /* CONFIG_BLE_MESH_PROVISIONER */ static void btc_ble_mesh_heartbeat_msg_recv_cb(u8_t hops, u16_t feature) @@ -1741,11 +1752,23 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) } case BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA: act = ESP_BLE_MESH_PROVISIONER_STORE_NODE_COMP_DATA_COMP_EVT; - param.provisioner_store_node_comp_data_comp.addr = arg->store_node_comp_data.addr; + param.provisioner_store_node_comp_data_comp.addr = arg->store_node_comp_data.unicast_addr; param.provisioner_store_node_comp_data_comp.err_code = - bt_mesh_provisioner_store_node_comp_data(arg->store_node_comp_data.addr, + bt_mesh_provisioner_store_node_comp_data(arg->store_node_comp_data.unicast_addr, arg->store_node_comp_data.data, arg->store_node_comp_data.length); break; + case BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_UUID: + act = ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT; + memcpy(param.provisioner_delete_node_with_uuid_comp.uuid, arg->delete_node_with_uuid.uuid, 16); + param.provisioner_delete_node_with_uuid_comp.err_code = + bt_mesh_provisioner_delete_node_with_uuid(arg->delete_node_with_uuid.uuid); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_ADDR: + act = ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT; + param.provisioner_delete_node_with_addr_comp.unicast_addr = arg->delete_node_with_addr.unicast_addr; + param.provisioner_delete_node_with_addr_comp.err_code = + bt_mesh_provisioner_delete_node_with_addr(arg->delete_node_with_addr.unicast_addr); + break; #endif /* CONFIG_BLE_MESH_PROVISIONER */ #if CONFIG_BLE_MESH_FAST_PROV case BTC_BLE_MESH_ACT_SET_FAST_PROV_INFO: diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h index 721aa8804e..97735c7e3b 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h @@ -49,6 +49,8 @@ typedef enum { BTC_BLE_MESH_ACT_PROVISIONER_BIND_LOCAL_MOD_APP, BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_NET_KEY, BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA, + BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_UUID, + BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_ADDR, BTC_BLE_MESH_ACT_SET_FAST_PROV_INFO, BTC_BLE_MESH_ACT_SET_FAST_PROV_ACTION, BTC_BLE_MESH_ACT_LPN_ENABLE, @@ -164,10 +166,16 @@ typedef union { uint16_t net_idx; } add_local_net_key; struct ble_mesh_provisioner_store_node_comp_data_args { - uint16_t addr; + uint16_t unicast_addr; uint16_t length; uint8_t *data; } store_node_comp_data; + struct ble_mesh_provisioner_delete_node_with_uuid_args { + uint8_t uuid[16]; + } delete_node_with_uuid; + struct ble_mesh_provisioner_delete_node_with_addr_args { + uint16_t unicast_addr; + } delete_node_with_addr; struct ble_mesh_set_fast_prov_info_args { uint16_t unicast_min; uint16_t unicast_max; @@ -243,6 +251,10 @@ void btc_ble_mesh_prov_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); void btc_ble_mesh_model_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); +esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16]); + +esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr); + int btc_ble_mesh_deinit(void); int btc_ble_mesh_client_model_init(esp_ble_mesh_model_t *model); 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 fc0a6940d0..1f8bc30076 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c @@ -33,6 +33,8 @@ static osi_mutex_t provisioner_lock; static u16_t all_node_count; static u16_t prov_node_count; +static int provisioner_remove_node(u16_t index); + static void bt_mesh_provisioner_mutex_new(void) { if (!provisioner_lock) { @@ -63,327 +65,6 @@ static void bt_mesh_provisioner_unlock(void) } } -static int provisioner_check_node_index(u16_t index) -{ - BT_DBG("%s", __func__); - - if (index >= ARRAY_SIZE(mesh_nodes)) { - BT_ERR("%s, Too big node index %d", __func__, index); - return -EINVAL; - } - - if (mesh_nodes[index] == NULL) { - BT_ERR("%s, Node %d is not found", __func__, index); - return -ENODEV; - } - - return 0; -} - -static void provisioner_node_count_inc(bool prov) -{ - all_node_count++; - if (prov) { - prov_node_count++; - } -} - -static void provisioner_node_count_dec(bool prov) -{ - if (all_node_count) { - all_node_count--; - } - if (prov) { - if (prov_node_count) { - prov_node_count--; - } - } -} - -u16_t bt_mesh_provisioner_get_prov_node_count(void) -{ - return prov_node_count; -} - -u16_t bt_mesh_provisioner_get_all_node_count(void) -{ - return all_node_count; -} - -static int provisioner_store_node(struct bt_mesh_node *node, bool prov, bool store, u16_t *index) -{ - u16_t min, max; - size_t i; - - bt_mesh_provisioner_lock(); - - /* Check if the node already exists */ - for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { - if (mesh_nodes[i] && !memcmp(mesh_nodes[i]->dev_uuid, node->dev_uuid, 16)) { - BT_WARN("Node already exists, uuid %s", bt_hex(node->dev_uuid, 16)); - bt_mesh_provisioner_unlock(); - return -EEXIST; - } - } - - /** - * 0 ~ (CONFIG_BLE_MESH_MAX_PROV_NODES - 1) are used to store - * the information of self-provisioned nodes. - */ - if (prov) { - min = 0U; - max = CONFIG_BLE_MESH_MAX_PROV_NODES; - } else { - min = CONFIG_BLE_MESH_MAX_PROV_NODES; - max = ARRAY_SIZE(mesh_nodes); - } - - for (i = min; i < max; i++) { - if (mesh_nodes[i] == NULL) { - mesh_nodes[i] = osi_calloc(sizeof(struct bt_mesh_node)); - if (!mesh_nodes[i]) { - BT_ERR("%s, Failed to allocate memory", __func__); - bt_mesh_provisioner_unlock(); - return -ENOMEM; - } - - memcpy(mesh_nodes[i], node, sizeof(struct bt_mesh_node)); - provisioner_node_count_inc(prov); - if (index) { - *index = i; - } - - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) { - bt_mesh_store_node_info(mesh_nodes[i], prov); - } - - bt_mesh_provisioner_unlock(); - return 0; - } - } - - BT_ERR("%s, Node queue is full", __func__); - bt_mesh_provisioner_unlock(); - return -ENOMEM; -} - -int bt_mesh_provisioner_provision(const bt_mesh_addr_t *addr, const u8_t uuid[16], u16_t oob_info, - u16_t unicast_addr, u8_t element_num, u16_t net_idx, u8_t flags, - u32_t iv_index, const u8_t dev_key[16], u16_t *index) -{ - struct bt_mesh_node node = {0}; - - BT_DBG("%s", __func__); - - if (!addr || !uuid || !dev_key || !index) { - BT_ERR("%s, Invalid parameter", __func__); - return -EINVAL; - } - - BT_INFO("unicast_addr 0x%04x, elem_num %d, net_idx 0x%04x", - unicast_addr, element_num, net_idx); - BT_INFO("dev_uuid %s", bt_hex(uuid, 16)); - BT_INFO("dev_key %s", bt_hex(dev_key, 16)); - - memcpy(node.addr, addr->val, BLE_MESH_ADDR_LEN); - node.addr_type = addr->type; - memcpy(node.dev_uuid, uuid, 16); - node.oob_info = oob_info; - node.unicast_addr = unicast_addr; - node.element_num = element_num; - node.net_idx = net_idx; - node.flags = flags; - node.iv_index = iv_index; - memcpy(node.dev_key, dev_key, 16); - - return provisioner_store_node(&node, true, true, index); -} - -static int provisioner_remove_node(u16_t index) -{ - struct bt_mesh_node *node = NULL; - struct bt_mesh_rpl *rpl = NULL; - bool is_prov; - size_t i; - - BT_DBG("%s, reset node %d", __func__, index); - - bt_mesh_provisioner_lock(); - - if (mesh_nodes[index] == NULL) { - bt_mesh_provisioner_unlock(); - return 0; - } - - node = mesh_nodes[index]; - - /* Reset corresponding network cache when reset the node */ - bt_mesh_msg_cache_clear(node->unicast_addr, node->element_num); - - /* Reset corresponding rpl when removing the node */ - for (i = 0U; i < ARRAY_SIZE(bt_mesh.rpl); i++) { - rpl = &bt_mesh.rpl[i]; - if (rpl->src >= node->unicast_addr && - rpl->src < node->unicast_addr + node->element_num) { - memset(rpl, 0, sizeof(struct bt_mesh_rpl)); - - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_clear_rpl_single(node->unicast_addr); - } - } - } - - if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { - bt_mesh_friend_remove_lpn(node->unicast_addr); - } - - is_prov = index < CONFIG_BLE_MESH_MAX_PROV_NODES ? true : false; - - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_clear_node_info(node->unicast_addr, is_prov); - } - - if (mesh_nodes[index]->comp_data) { - osi_free(mesh_nodes[index]->comp_data); - } - osi_free(mesh_nodes[index]); - mesh_nodes[index] = NULL; - - provisioner_node_count_dec(is_prov); - - bt_mesh_provisioner_unlock(); - return 0; -} - -bool bt_mesh_provisioner_find_node_with_uuid(const u8_t uuid[16], bool reset) -{ - size_t i; - - for (i = 0U; i < CONFIG_BLE_MESH_MAX_PROV_NODES; i++) { - if (mesh_nodes[i]) { - if (!memcmp(mesh_nodes[i]->dev_uuid, uuid, 16)) { - if (reset) { - provisioner_remove_node(i); - } - return true; - } - } - } - - return false; -} - -bool bt_mesh_provisioner_find_node_with_addr(const bt_mesh_addr_t *addr, bool reset) -{ - size_t i; - - for (i = 0U; i < CONFIG_BLE_MESH_MAX_PROV_NODES; i++) { - if (mesh_nodes[i]) { - if (!memcmp(mesh_nodes[i]->addr, addr->val, BLE_MESH_ADDR_LEN) && - mesh_nodes[i]->addr_type == addr->type) { - if (reset) { - provisioner_remove_node(i); - } - return true; - } - } - } - - return false; -} - -int bt_mesh_provisioner_remove_node(const u8_t uuid[16]) -{ - size_t i; - - if (uuid == NULL) { - for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { - if (mesh_nodes[i]) { - provisioner_remove_node(i); - } - } - return 0; - } - - for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { - if (mesh_nodes[i]) { - if (!memcmp(mesh_nodes[i]->dev_uuid, uuid, 16)) { - provisioner_remove_node(i); - return 0; - } - } - } - - BT_WARN("Node %s not exist", bt_hex(uuid, 16)); - return -ENODEV; -} - -struct bt_mesh_node *bt_mesh_provisioner_get_prov_node_info(const u8_t uuid[16]) -{ - size_t i; - - if (uuid == NULL) { - BT_ERR("%s, Invalid parameter", __func__); - return NULL; - } - - bt_mesh_provisioner_lock(); - - for (i = 0U; i < CONFIG_BLE_MESH_MAX_PROV_NODES; i++) { - if (mesh_nodes[i]) { - if (!memcmp(mesh_nodes[i]->dev_uuid, uuid, 16)) { - bt_mesh_provisioner_unlock(); - return mesh_nodes[i]; - } - } - } - - bt_mesh_provisioner_unlock(); - return NULL; -} - -bool bt_mesh_provisioner_check_is_addr_dup(u16_t addr, u8_t elem_num, bool comp_with_own) -{ - const struct bt_mesh_comp *comp = NULL; - struct bt_mesh_node *node = NULL; - u16_t primary_addr = BLE_MESH_ADDR_UNASSIGNED; - u16_t comp_addr; - size_t i; - - if (comp_with_own) { - comp = bt_mesh_comp_get(); - if (!comp) { - BT_ERR("NULL composition data"); - return true; - } - - primary_addr = bt_mesh_provisioner_get_primary_elem_addr(); - if (!BLE_MESH_ADDR_IS_UNICAST(primary_addr)) { - BT_ERR("%s, Not a unicast address 0x%04x", __func__, primary_addr); - return true; - } - } - - for (comp_addr = addr; comp_addr < addr + elem_num; comp_addr++) { - for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { - node = mesh_nodes[i]; - if (node && comp_addr >= node->unicast_addr && - comp_addr < node->unicast_addr + node->element_num) { - BT_ERR("Duplicate with node address 0x%04x", comp_addr); - return true; - } - - if (comp_with_own && comp_addr >= primary_addr && - comp_addr < primary_addr + comp->elem_count) { - BT_ERR("Duplicate with Provisioner address 0x%04x", comp_addr); - return true; - } - } - } - - return false; -} - int bt_mesh_provisioner_init(void) { bt_mesh_provisioner_mutex_new(); @@ -533,7 +214,572 @@ int bt_mesh_provisioner_deinit(void) return 0; } -/* The following APIs are for Provisioner internal usage */ +bool bt_mesh_provisioner_check_is_addr_dup(u16_t addr, u8_t elem_num, bool comp_with_own) +{ + const struct bt_mesh_comp *comp = NULL; + struct bt_mesh_node *node = NULL; + u16_t primary_addr = BLE_MESH_ADDR_UNASSIGNED; + u16_t comp_addr; + size_t i; + + if (comp_with_own) { + comp = bt_mesh_comp_get(); + if (!comp) { + BT_ERR("NULL composition data"); + return true; + } + + primary_addr = bt_mesh_provisioner_get_primary_elem_addr(); + if (!BLE_MESH_ADDR_IS_UNICAST(primary_addr)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, primary_addr); + return true; + } + } + + for (comp_addr = addr; comp_addr < addr + elem_num; comp_addr++) { + for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { + node = mesh_nodes[i]; + if (node && comp_addr >= node->unicast_addr && + comp_addr < node->unicast_addr + node->element_num) { + BT_ERR("Duplicate with node address 0x%04x", comp_addr); + return true; + } + + if (comp_with_own && comp_addr >= primary_addr && + comp_addr < primary_addr + comp->elem_count) { + BT_ERR("Duplicate with Provisioner address 0x%04x", comp_addr); + return true; + } + } + } + + return false; +} + +static void provisioner_node_count_inc(bool prov) +{ + all_node_count++; + if (prov) { + prov_node_count++; + } +} + +static void provisioner_node_count_dec(bool prov) +{ + if (all_node_count) { + all_node_count--; + } + if (prov) { + if (prov_node_count) { + prov_node_count--; + } + } +} + +u16_t bt_mesh_provisioner_get_prov_node_count(void) +{ + return prov_node_count; +} + +u16_t bt_mesh_provisioner_get_all_node_count(void) +{ + return all_node_count; +} + +static int provisioner_store_node(struct bt_mesh_node *node, bool prov, bool store, u16_t *index) +{ + u16_t min, max; + size_t i; + + bt_mesh_provisioner_lock(); + + /* Check if the node already exists */ + for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i] && !memcmp(mesh_nodes[i]->dev_uuid, node->dev_uuid, 16)) { + BT_WARN("Node already exists, uuid %s", bt_hex(node->dev_uuid, 16)); + bt_mesh_provisioner_unlock(); + return -EEXIST; + } + } + + /** + * 0 ~ (CONFIG_BLE_MESH_MAX_PROV_NODES - 1) are used to store + * the information of self-provisioned nodes. + */ + if (prov) { + min = 0U; + max = CONFIG_BLE_MESH_MAX_PROV_NODES; + } else { + min = CONFIG_BLE_MESH_MAX_PROV_NODES; + max = ARRAY_SIZE(mesh_nodes); + } + + for (i = min; i < max; i++) { + if (mesh_nodes[i] == NULL) { + mesh_nodes[i] = osi_calloc(sizeof(struct bt_mesh_node)); + if (!mesh_nodes[i]) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_provisioner_unlock(); + return -ENOMEM; + } + + memcpy(mesh_nodes[i], node, sizeof(struct bt_mesh_node)); + provisioner_node_count_inc(prov); + if (index) { + *index = i; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) { + bt_mesh_store_node_info(mesh_nodes[i], prov); + } + + bt_mesh_provisioner_unlock(); + return 0; + } + } + + BT_ERR("%s, Node queue is full", __func__); + bt_mesh_provisioner_unlock(); + return -ENOMEM; +} + +int bt_mesh_provisioner_restore_node_info(struct bt_mesh_node *node, bool prov) +{ + if (!node) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + return provisioner_store_node(node, prov, false, NULL); +} + +int bt_mesh_provisioner_provision(const bt_mesh_addr_t *addr, const u8_t uuid[16], u16_t oob_info, + u16_t unicast_addr, u8_t element_num, u16_t net_idx, u8_t flags, + u32_t iv_index, const u8_t dev_key[16], u16_t *index) +{ + struct bt_mesh_node node = {0}; + + BT_DBG("%s", __func__); + + if (!addr || !uuid || !dev_key || !index) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + BT_INFO("unicast_addr 0x%04x, elem_num %d, net_idx 0x%04x", + unicast_addr, element_num, net_idx); + BT_INFO("dev_uuid %s", bt_hex(uuid, 16)); + BT_INFO("dev_key %s", bt_hex(dev_key, 16)); + + memcpy(node.addr, addr->val, BLE_MESH_ADDR_LEN); + node.addr_type = addr->type; + memcpy(node.dev_uuid, uuid, 16); + node.oob_info = oob_info; + node.unicast_addr = unicast_addr; + node.element_num = element_num; + node.net_idx = net_idx; + node.flags = flags; + node.iv_index = iv_index; + memcpy(node.dev_key, dev_key, 16); + + return provisioner_store_node(&node, true, true, index); +} + +static int provisioner_remove_node(u16_t index) +{ + struct bt_mesh_node *node = NULL; + struct bt_mesh_rpl *rpl = NULL; + bool is_prov; + size_t i; + + BT_DBG("%s, reset node %d", __func__, index); + + bt_mesh_provisioner_lock(); + + if (mesh_nodes[index] == NULL) { + bt_mesh_provisioner_unlock(); + return 0; + } + + node = mesh_nodes[index]; + + /* Reset corresponding network cache when reset the node */ + bt_mesh_msg_cache_clear(node->unicast_addr, node->element_num); + + /* Reset corresponding rpl when removing the node */ + for (i = 0U; i < ARRAY_SIZE(bt_mesh.rpl); i++) { + rpl = &bt_mesh.rpl[i]; + if (rpl->src >= node->unicast_addr && + rpl->src < node->unicast_addr + node->element_num) { + memset(rpl, 0, sizeof(struct bt_mesh_rpl)); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_rpl_single(node->unicast_addr); + } + } + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + bt_mesh_friend_remove_lpn(node->unicast_addr); + } + + is_prov = index < CONFIG_BLE_MESH_MAX_PROV_NODES ? true : false; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_node_info(node->unicast_addr, is_prov); + } + + if (mesh_nodes[index]->comp_data) { + osi_free(mesh_nodes[index]->comp_data); + } + osi_free(mesh_nodes[index]); + mesh_nodes[index] = NULL; + + provisioner_node_count_dec(is_prov); + + bt_mesh_provisioner_unlock(); + return 0; +} + +static struct bt_mesh_node *provisioner_find_node_with_uuid(const u8_t uuid[16], u16_t *index) +{ + size_t i; + + BT_DBG("%s", __func__); + + if (uuid == NULL) { + BT_ERR("%s, Invalid device uuid", __func__); + return NULL; + } + + bt_mesh_provisioner_lock(); + + for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i] && !memcmp(mesh_nodes[i]->dev_uuid, uuid, 16)) { + if (index) { + *index = i; + } + bt_mesh_provisioner_unlock(); + return mesh_nodes[i]; + } + } + + bt_mesh_provisioner_unlock(); + return NULL; +} + +bool bt_mesh_provisioner_find_node_with_uuid(const u8_t uuid[16], bool reset) +{ + struct bt_mesh_node *node = NULL; + u16_t index; + + node = provisioner_find_node_with_uuid(uuid, &index); + if (!node) { + return false; + } + + if (reset) { + provisioner_remove_node(index); + } + return true; +} + +bool bt_mesh_provisioner_find_node_with_addr(const bt_mesh_addr_t *addr, bool reset) +{ + size_t i; + + for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i]) { + if (!memcmp(mesh_nodes[i]->addr, addr->val, BLE_MESH_ADDR_LEN) && + mesh_nodes[i]->addr_type == addr->type) { + if (reset) { + provisioner_remove_node(i); + } + return true; + } + } + } + + return false; +} + +int bt_mesh_provisioner_remove_node(const u8_t uuid[16]) +{ + struct bt_mesh_node *node = NULL; + u16_t index; + size_t i; + + if (uuid == NULL) { + for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i]) { + provisioner_remove_node(i); + } + } + return 0; + } + + node = provisioner_find_node_with_uuid(uuid, &index); + if (!node) { + BT_WARN("Node %s not exist", bt_hex(uuid, 16)); + return -ENODEV; + } + + provisioner_remove_node(index); + return 0; +} + +static struct bt_mesh_node *provisioner_find_node_with_addr(u16_t addr, u16_t *index) +{ + struct bt_mesh_node *node = NULL; + size_t i; + + BT_DBG("%s", __func__); + + if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, addr); + return NULL; + } + + bt_mesh_provisioner_lock(); + + for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { + node = mesh_nodes[i]; + if (node && addr >= node->unicast_addr && + addr < (node->unicast_addr + node->element_num)) { + if (index) { + *index = i; + } + bt_mesh_provisioner_unlock(); + return node; + } + } + + bt_mesh_provisioner_unlock(); + return NULL; +} + +int bt_mesh_provisioner_restore_node_name(u16_t addr, const char *name) +{ + struct bt_mesh_node *node = NULL; + + node = provisioner_find_node_with_addr(addr, NULL); + if (node == NULL) { + BT_ERR("%s, Node 0x%04x not exist", __func__, addr); + return -ENODEV; + } + + strncpy(node->name, name, BLE_MESH_NODE_NAME_SIZE); + return 0; +} + +int bt_mesh_provisioner_restore_node_comp_data(u16_t addr, const u8_t *data, u16_t length, bool prov) +{ + struct bt_mesh_node *node = NULL; + + node = provisioner_find_node_with_addr(addr, NULL); + if (node == NULL) { + BT_ERR("%s, Node 0x%04x not exist", __func__, addr); + return -ENODEV; + } + + node->comp_data = osi_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; +} + +int bt_mesh_provisioner_store_node_info(struct bt_mesh_node *node) +{ + if (!node) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + return provisioner_store_node(node, false, true, NULL); +} + +struct bt_mesh_node *bt_mesh_provisioner_get_node_with_uuid(const u8_t uuid[16]) +{ + return provisioner_find_node_with_uuid(uuid, NULL); +} + +struct bt_mesh_node *bt_mesh_provisioner_get_node_with_addr(u16_t unicast_addr) +{ + return provisioner_find_node_with_addr(unicast_addr, NULL); +} + +int bt_mesh_provisioner_delete_node_with_uuid(const u8_t uuid[16]) +{ + struct bt_mesh_node *node = NULL; + u16_t index; + + node = provisioner_find_node_with_uuid(uuid, &index); + if (!node) { + BT_WARN("Node %s not exist", bt_hex(uuid, 16)); + return -ENODEV; + } + + provisioner_remove_node(index); + return 0; +} + +int bt_mesh_provisioner_delete_node_with_addr(u16_t unicast_addr) +{ + struct bt_mesh_node *node = NULL; + u16_t index; + + node = provisioner_find_node_with_addr(unicast_addr, &index); + if (!node) { + BT_WARN("Node 0x%04x not exist", unicast_addr); + return -ENODEV; + } + + provisioner_remove_node(index); + return 0; +} + +static int provisioner_check_node_index(u16_t index) +{ + BT_DBG("%s", __func__); + + if (index >= ARRAY_SIZE(mesh_nodes)) { + BT_ERR("%s, Too big node index %d", __func__, index); + return -EINVAL; + } + + if (mesh_nodes[index] == NULL) { + BT_ERR("%s, Node %d is not found", __func__, index); + return -ENODEV; + } + + return 0; +} + +int bt_mesh_provisioner_set_node_name(u16_t index, const char *name) +{ + size_t length, name_len; + size_t i; + + BT_DBG("%s", __func__); + + if (!name) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (provisioner_check_node_index(index)) { + BT_ERR("%s, Failed to check node index", __func__); + return -EINVAL; + } + + BT_DBG("name len is %d, name is %s", strlen(name), name); + + length = (strlen(name) <= BLE_MESH_NODE_NAME_SIZE) ? strlen(name) : BLE_MESH_NODE_NAME_SIZE; + for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i]) { + name_len = strlen(mesh_nodes[i]->name); + if (length != name_len) { + continue; + } + if (!strncmp(mesh_nodes[i]->name, name, length)) { + BT_WARN("Node name %s already exists", name); + return -EEXIST; + } + } + } + + memset(mesh_nodes[index]->name, 0, BLE_MESH_NODE_NAME_SIZE); + + strncpy(mesh_nodes[index]->name, name, length); + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_node_name(mesh_nodes[index], + index < CONFIG_BLE_MESH_MAX_PROV_NODES ? true : false); + } + + return 0; +} + +const char *bt_mesh_provisioner_get_node_name(u16_t index) +{ + BT_DBG("%s", __func__); + + if (provisioner_check_node_index(index)) { + BT_ERR("%s, Failed to check node index", __func__); + return NULL; + } + + return mesh_nodes[index]->name; +} + +u16_t bt_mesh_provisioner_get_node_index(const char *name) +{ + size_t length, name_len; + size_t i; + + BT_DBG("%s", __func__); + + if (!name) { + BT_ERR("%s, Invalid parameter", __func__); + return BLE_MESH_INVALID_NODE_INDEX; + } + + length = (strlen(name) <= BLE_MESH_NODE_NAME_SIZE) ? strlen(name) : BLE_MESH_NODE_NAME_SIZE; + for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i]) { + name_len = strlen(mesh_nodes[i]->name); + if (length != name_len) { + continue; + } + if (!strncmp(mesh_nodes[i]->name, name, length)) { + return (u16_t)i; + } + } + } + + BT_ERR("%s, Node name %s not exist", __func__, 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) +{ + struct bt_mesh_node *node = NULL; + + if (!BLE_MESH_ADDR_IS_UNICAST(addr) || !data || + (length % 2) || length <= 14) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + node = provisioner_find_node_with_addr(addr, NULL); + if (node == NULL) { + BT_ERR("%s, Node 0x%04x not exist", __func__, addr); + return -ENODEV; + } + + node->comp_data = osi_calloc(length); + if (node->comp_data == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + memcpy(node->comp_data, data, length); + node->comp_length = length; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_node_comp_data(node, + (node - mesh_nodes[0]) < CONFIG_BLE_MESH_MAX_PROV_NODES ? true : false); + } + + return 0; +} + +/* Provisioner DevKey, NetKey and AppKey related functions */ const u8_t *bt_mesh_provisioner_net_key_get(u16_t net_idx) { @@ -643,218 +889,6 @@ struct bt_mesh_app_key *bt_mesh_provisioner_app_key_find(u16_t app_idx) return NULL; } -int bt_mesh_provisioner_restore_node_info(struct bt_mesh_node *node, bool prov) -{ - if (!node) { - BT_ERR("%s, Invalid parameter", __func__); - return -EINVAL; - } - - return provisioner_store_node(node, prov, false, NULL); -} - -static struct bt_mesh_node *provisioner_find_node_with_elem_addr(u16_t addr) -{ - struct bt_mesh_node *node = NULL; - size_t i; - - BT_DBG("%s", __func__); - - if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { - BT_ERR("%s, Not a unicast address 0x%04x", __func__, addr); - return NULL; - } - - bt_mesh_provisioner_lock(); - - for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { - node = mesh_nodes[i]; - if (node && addr >= node->unicast_addr && - addr < (node->unicast_addr + node->element_num)) { - bt_mesh_provisioner_unlock(); - return node; - } - } - - bt_mesh_provisioner_unlock(); - return NULL; -} - -int bt_mesh_provisioner_restore_node_name(u16_t addr, const char *name) -{ - struct bt_mesh_node *node = NULL; - - node = provisioner_find_node_with_elem_addr(addr); - if (node == NULL) { - BT_ERR("%s, Node 0x%04x not exist", __func__, addr); - return -ENODEV; - } - - strncpy(node->name, name, BLE_MESH_NODE_NAME_SIZE); - return 0; -} - -int bt_mesh_provisioner_restore_node_comp_data(u16_t addr, const u8_t *data, u16_t length, bool prov) -{ - struct bt_mesh_node *node = NULL; - - node = provisioner_find_node_with_elem_addr(addr); - if (node == NULL) { - BT_ERR("%s, Node 0x%04x not exist", __func__, addr); - return -ENODEV; - } - - node->comp_data = osi_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; -} - -/* Provisioner related internal test functions */ - -int bt_mesh_provisioner_store_node_info(struct bt_mesh_node *node) -{ - if (!node) { - BT_ERR("%s, Invalid parameter", __func__); - return -EINVAL; - } - - return provisioner_store_node(node, false, true, NULL); -} - -struct bt_mesh_node *bt_mesh_provisioner_get_node_info(u16_t unicast_addr) -{ - return provisioner_find_node_with_elem_addr(unicast_addr); -} - -/* Provisioner related node name management functions */ - -int bt_mesh_provisioner_set_node_name(u16_t index, const char *name) -{ - size_t length, name_len; - size_t i; - - BT_DBG("%s", __func__); - - if (!name) { - BT_ERR("%s, Invalid parameter", __func__); - return -EINVAL; - } - - if (provisioner_check_node_index(index)) { - BT_ERR("%s, Failed to check node index", __func__); - return -EINVAL; - } - - BT_DBG("name len is %d, name is %s", strlen(name), name); - - length = (strlen(name) <= BLE_MESH_NODE_NAME_SIZE) ? strlen(name) : BLE_MESH_NODE_NAME_SIZE; - for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { - if (mesh_nodes[i]) { - name_len = strlen(mesh_nodes[i]->name); - if (length != name_len) { - continue; - } - if (!strncmp(mesh_nodes[i]->name, name, length)) { - BT_WARN("Node name %s already exists", name); - return -EEXIST; - } - } - } - - memset(mesh_nodes[index]->name, 0, BLE_MESH_NODE_NAME_SIZE); - - strncpy(mesh_nodes[index]->name, name, length); - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_store_node_name(mesh_nodes[index], - index < CONFIG_BLE_MESH_MAX_PROV_NODES ? true : false); - } - - return 0; -} - -const char *bt_mesh_provisioner_get_node_name(u16_t index) -{ - BT_DBG("%s", __func__); - - if (provisioner_check_node_index(index)) { - BT_ERR("%s, Failed to check node index", __func__); - return NULL; - } - - return mesh_nodes[index]->name; -} - -u16_t bt_mesh_provisioner_get_node_index(const char *name) -{ - size_t length, name_len; - size_t i; - - BT_DBG("%s", __func__); - - if (!name) { - BT_ERR("%s, Invalid parameter", __func__); - return BLE_MESH_INVALID_NODE_INDEX; - } - - length = (strlen(name) <= BLE_MESH_NODE_NAME_SIZE) ? strlen(name) : BLE_MESH_NODE_NAME_SIZE; - for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { - if (mesh_nodes[i]) { - name_len = strlen(mesh_nodes[i]->name); - if (length != name_len) { - continue; - } - if (!strncmp(mesh_nodes[i]->name, name, length)) { - return (u16_t)i; - } - } - } - - BT_ERR("%s, Node name %s not exist", __func__, 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) -{ - struct bt_mesh_node *node = NULL; - - if (!BLE_MESH_ADDR_IS_UNICAST(addr) || !data || - (length % 2) || length <= 14) { - BT_ERR("%s, Invalid parameter", __func__); - return -EINVAL; - } - - node = provisioner_find_node_with_elem_addr(addr); - if (node == NULL) { - BT_ERR("%s, Node 0x%04x not exist", __func__, addr); - return -ENODEV; - } - - node->comp_data = osi_calloc(length); - if (node->comp_data == NULL) { - BT_ERR("%s, Failed to allocate memory", __func__); - return -ENOMEM; - } - - memcpy(node->comp_data, data, length); - node->comp_length = length; - - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_store_node_comp_data(node, - (node - mesh_nodes[0]) < CONFIG_BLE_MESH_MAX_PROV_NODES ? true : false); - } - - return 0; -} - -/* Provisioner related NetKey and AppKey functions */ - static int provisioner_check_app_key(const u8_t app_key[16], u16_t *app_idx) { struct bt_mesh_app_key *key = NULL; diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h index ad689a56ae..87c3afe724 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h @@ -43,12 +43,20 @@ struct bt_mesh_node { u8_t *comp_data; /* Value of Composition Data */ } __packed; -/* The following APIs are for key init, node provision & node reset. */ +int bt_mesh_provisioner_init(void); + +int bt_mesh_provisioner_net_create(void); + +int bt_mesh_provisioner_deinit(void); + +bool bt_mesh_provisioner_check_is_addr_dup(u16_t addr, u8_t elem_num, bool comp_with_own); u16_t bt_mesh_provisioner_get_prov_node_count(void); u16_t bt_mesh_provisioner_get_all_node_count(void); +int bt_mesh_provisioner_restore_node_info(struct bt_mesh_node *node, bool prov); + int bt_mesh_provisioner_provision(const bt_mesh_addr_t *addr, const u8_t uuid[16], u16_t oob_info, u16_t unicast_addr, u8_t element_num, u16_t net_idx, u8_t flags, u32_t iv_index, const u8_t dev_key[16], u16_t *index); @@ -59,16 +67,27 @@ bool bt_mesh_provisioner_find_node_with_addr(const bt_mesh_addr_t *addr, bool re int bt_mesh_provisioner_remove_node(const u8_t uuid[16]); -struct bt_mesh_node *bt_mesh_provisioner_get_prov_node_info(const u8_t uuid[16]); +int bt_mesh_provisioner_restore_node_name(u16_t addr, const char *name); -bool bt_mesh_provisioner_check_is_addr_dup(u16_t addr, u8_t elem_num, bool comp_with_own); +int bt_mesh_provisioner_restore_node_comp_data(u16_t addr, const u8_t *data, u16_t length, bool prov); -int bt_mesh_provisioner_init(void); -int bt_mesh_provisioner_deinit(void); +int bt_mesh_provisioner_store_node_info(struct bt_mesh_node *node); -int bt_mesh_provisioner_net_create(void); +struct bt_mesh_node *bt_mesh_provisioner_get_node_with_uuid(const u8_t uuid[16]); -/* The following APIs are for provisioner upper layers internal usage. */ +struct bt_mesh_node *bt_mesh_provisioner_get_node_with_addr(u16_t unicast_addr); + +int bt_mesh_provisioner_delete_node_with_uuid(const u8_t uuid[16]); + +int bt_mesh_provisioner_delete_node_with_addr(u16_t unicast_addr); + +int bt_mesh_provisioner_set_node_name(u16_t index, const char *name); + +const char *bt_mesh_provisioner_get_node_name(u16_t index); + +u16_t bt_mesh_provisioner_get_node_index(const char *name); + +int bt_mesh_provisioner_store_node_comp_data(u16_t addr, const u8_t *data, u16_t length); const u8_t *bt_mesh_provisioner_net_key_get(u16_t net_idx); @@ -80,26 +99,6 @@ const u8_t *bt_mesh_provisioner_dev_key_get(u16_t dst); struct bt_mesh_app_key *bt_mesh_provisioner_app_key_find(u16_t app_idx); -int bt_mesh_provisioner_restore_node_info(struct bt_mesh_node *node, bool prov); - -int bt_mesh_provisioner_restore_node_name(u16_t addr, const char *name); - -int bt_mesh_provisioner_restore_node_comp_data(u16_t addr, const u8_t *data, u16_t length, bool prov); - -/* The following APIs are for provisioner application use. */ - -int bt_mesh_provisioner_store_node_info(struct bt_mesh_node *node); - -struct bt_mesh_node *bt_mesh_provisioner_get_node_info(u16_t unicast_addr); - -int bt_mesh_provisioner_set_node_name(u16_t index, const char *name); - -const char *bt_mesh_provisioner_get_node_name(u16_t index); - -u16_t bt_mesh_provisioner_get_node_index(const char *name); - -int bt_mesh_provisioner_store_node_comp_data(u16_t addr, const u8_t *data, u16_t length); - int bt_mesh_provisioner_local_app_key_add(const u8_t app_key[16], u16_t net_idx, u16_t *app_idx); const u8_t *bt_mesh_provisioner_local_app_key_get(u16_t net_idx, u16_t app_idx); diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c index 2720f4cfe6..d04b9c6f74 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c @@ -2498,7 +2498,7 @@ static void send_prov_data(const u8_t idx) */ /* Check if this device is a re-provisioned device */ - node = bt_mesh_provisioner_get_prov_node_info(link[idx].uuid); + node = bt_mesh_provisioner_get_node_with_uuid(link[idx].uuid); if (node) { if (link[idx].element_num <= node->element_num) { /** diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/main/ble_mesh_register_provisioner_cmd.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/main/ble_mesh_register_provisioner_cmd.c index 8b845381e0..8b107fcc29 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/main/ble_mesh_register_provisioner_cmd.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/main/ble_mesh_register_provisioner_cmd.c @@ -201,7 +201,7 @@ int ble_mesh_provisioner_get_node(int argc, char **argv) } arg_int_to_value(provisioner_get_node.unicast_addr, unicast_addr, "unicast address"); - node_info = bt_mesh_provisioner_get_node_info(unicast_addr); + node_info = esp_ble_mesh_provisioner_get_node_with_addr(unicast_addr); if (node_info == NULL) { return ESP_FAIL;