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 db6b6a144b..cbfa0ad9a2 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 @@ -334,6 +334,25 @@ esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_addr(uint16_t unicas return btc_ble_mesh_provisioner_get_node_with_addr(unicast_addr); } +esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_name(const char *name) +{ + if (!name || (strlen(name) > ESP_BLE_MESH_NODE_NAME_MAX_LEN)) { + return NULL; + } + + return btc_ble_mesh_provisioner_get_node_with_name(name); +} + +uint16_t esp_ble_mesh_provisioner_get_prov_node_count(void) +{ + return btc_ble_mesh_provisioner_get_prov_node_count(); +} + +const esp_ble_mesh_node_t **esp_ble_mesh_provisioner_get_node_table_entry(void) +{ + return btc_ble_mesh_provisioner_get_node_table_entry(); +} + esp_err_t esp_ble_mesh_provisioner_delete_node_with_uuid(const uint8_t uuid[16]) { btc_ble_mesh_prov_args_t arg = {0}; @@ -501,12 +520,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); } - -uint16_t esp_ble_mesh_provisioner_get_prov_node_count(void) -{ - return btc_ble_mesh_provisioner_get_prov_node_count(); -} - #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 c2def986f2..3a74a25757 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 @@ -250,6 +250,48 @@ esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_uuid(const uint8_t u */ esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr); +/** + * @brief This function is called to get the provisioned node information + * with the node name. + * + * @param[in] name: Name of the node (end by '\0'). + * + * @return Pointer of the node info struct or NULL on failure. + * + */ +esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_name(const char *name); + +/** + * @brief This function is called by Provisioner to get provisioned node count. + * + * @return Number of the provisioned nodes. + * + */ +uint16_t esp_ble_mesh_provisioner_get_prov_node_count(void); + +/** + * @brief This function is called by Provisioner to get the entry of the node table. + * + * @note After invoking the function to get the entry of nodes, users can use the "for" + * loop combined with the macro CONFIG_BLE_MESH_MAX_PROV_NODES to get each node's + * information. Before trying to read the node's information, users need to check + * if the node exists, i.e. if the *(esp_ble_mesh_node_t **node) is NULL. + * For example: + * ``` + * const esp_ble_mesh_node_t **entry = esp_ble_mesh_provisioner_get_node_table_entry(); + * for (int i = 0; i < CONFIG_BLE_MESH_MAX_PROV_NODES; i++) { + * const esp_ble_mesh_node_t *node = entry[i]; + * if (node) { + * ...... + * } + * } + * ``` + * + * @return Pointer to the start of the node table. + * + */ +const esp_ble_mesh_node_t **esp_ble_mesh_provisioner_get_node_table_entry(void); + /** * @brief This function is called to delete the provisioned node information * with the node device uuid. @@ -366,14 +408,6 @@ esp_err_t esp_ble_mesh_provisioner_update_local_net_key(const uint8_t net_key[16 */ const uint8_t *esp_ble_mesh_provisioner_get_local_net_key(uint16_t net_idx); -/** - * @brief This function is called by Provisioner to get provisioned node count. - * - * @return Number of the provisioned nodes. - * - */ -uint16_t esp_ble_mesh_provisioner_get_prov_node_count(void); - /** * @brief This function is called to get fast provisioning application key. * 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 f0d4ea066b..1535b6f445 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 @@ -782,6 +782,20 @@ esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_addr(uint16_t unicas return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_with_addr(unicast_addr); } +esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_name(const char *name) +{ + return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_with_name(name); +} + +u16_t btc_ble_mesh_provisioner_get_prov_node_count(void) +{ + return bt_mesh_provisioner_get_node_count(); +} + +const esp_ble_mesh_node_t **btc_ble_mesh_provisioner_get_node_table_entry(void) +{ + return (const esp_ble_mesh_node_t **)bt_mesh_provisioner_get_node_table_entry(); +} #endif /* CONFIG_BLE_MESH_PROVISIONER */ static void btc_ble_mesh_heartbeat_msg_recv_cb(u8_t hops, u16_t feature) @@ -991,11 +1005,6 @@ const esp_ble_mesh_comp_t *btc_ble_mesh_comp_get(void) return (const esp_ble_mesh_comp_t *)bt_mesh_comp_get(); } -u16_t btc_ble_mesh_provisioner_get_prov_node_count(void) -{ - return bt_mesh_provisioner_get_node_count(); -} - /* Configuration Models */ extern const struct bt_mesh_model_op bt_mesh_cfg_srv_op[]; extern const struct bt_mesh_model_op bt_mesh_cfg_cli_op[]; 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 e56425f151..4566135c89 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 @@ -298,6 +298,12 @@ esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_uuid(const uint8_t u esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr); +esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_name(const char *name); + +u16_t btc_ble_mesh_provisioner_get_prov_node_count(void); + +const esp_ble_mesh_node_t **btc_ble_mesh_provisioner_get_node_table_entry(void); + int btc_ble_mesh_client_model_init(esp_ble_mesh_model_t *model); int btc_ble_mesh_client_model_deinit(esp_ble_mesh_model_t *model); @@ -320,8 +326,6 @@ esp_ble_mesh_model_t *btc_ble_mesh_model_find(const esp_ble_mesh_elem_t *elem, const esp_ble_mesh_comp_t *btc_ble_mesh_comp_get(void); -u16_t btc_ble_mesh_provisioner_get_prov_node_count(void); - void btc_ble_mesh_model_call_handler(btc_msg_t *msg); void btc_ble_mesh_model_cb_handler(btc_msg_t *msg); 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 58feece1ff..f27e2f01cd 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c @@ -550,57 +550,57 @@ int bt_mesh_provisioner_delete_node_with_dev_addr(const bt_mesh_addr_t *addr) return -ENODEV; } -static int provisioner_check_node_index(u16_t index) +static struct bt_mesh_node **provisioner_find_node_with_name(const char *name) { - 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 not exists, index %d", __func__, index); - return -ENODEV; - } - - return 0; -} - -int bt_mesh_provisioner_set_node_name(u16_t index, const char *name) -{ - size_t length = 0U, name_len = 0U; + size_t length = 0U; int i; - BT_DBG("%s", __func__); + BT_DBG("node name %s", name); - if (!name) { - BT_ERR("%s, Invalid parameter", __func__); - return -EINVAL; - } + length = MIN(strlen(name), BLE_MESH_NODE_NAME_SIZE); - if (provisioner_check_node_index(index)) { - return -EINVAL; - } + bt_mesh_provisioner_lock(); - BT_DBG("len %d, name %s", strlen(name), name); - - length = (strlen(name) <= BLE_MESH_NODE_NAME_SIZE) ? strlen(name) : BLE_MESH_NODE_NAME_SIZE; for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { if (mesh_nodes[i]) { - name_len = strlen(mesh_nodes[i]->name); - if (length != name_len) { + if (strlen(mesh_nodes[i]->name) != length) { continue; } if (!strncmp(mesh_nodes[i]->name, name, length)) { - BT_WARN("Node name %s exists", name); - return -EEXIST; + bt_mesh_provisioner_unlock(); + return &mesh_nodes[i]; } } } + bt_mesh_provisioner_unlock(); + return NULL; +} + +int bt_mesh_provisioner_set_node_name(u16_t index, const char *name) +{ + if (index >= ARRAY_SIZE(mesh_nodes)) { + BT_ERR("Invalid node index %d", index); + return -EINVAL; + } + + if (mesh_nodes[index] == NULL) { + BT_ERR("Node not exists, index %d", index); + return -EINVAL; + } + + if (name == NULL) { + BT_ERR("Invalid node name"); + return -EINVAL; + } + + if (provisioner_find_node_with_name(name)) { + BT_WARN("Node name \"%s\" already exists", name); + return -EEXIST; + } + memset(mesh_nodes[index]->name, 0, sizeof(mesh_nodes[index]->name)); - strncpy(mesh_nodes[index]->name, name, length); + strncpy(mesh_nodes[index]->name, name, BLE_MESH_NODE_NAME_SIZE); if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { bt_mesh_store_node_name(mesh_nodes[index]); @@ -611,9 +611,13 @@ int bt_mesh_provisioner_set_node_name(u16_t index, const char *name) const char *bt_mesh_provisioner_get_node_name(u16_t index) { - BT_DBG("%s", __func__); + if (index >= ARRAY_SIZE(mesh_nodes)) { + BT_ERR("Invalid node index %d", index); + return NULL; + } - if (provisioner_check_node_index(index)) { + if (mesh_nodes[index] == NULL) { + BT_ERR("Node not exists, index %d", index); return NULL; } @@ -622,31 +626,43 @@ const char *bt_mesh_provisioner_get_node_name(u16_t index) u16_t bt_mesh_provisioner_get_node_index(const char *name) { - size_t length = 0U, name_len = 0U; - int i; + struct bt_mesh_node **node = NULL; - BT_DBG("%s", __func__); - - if (!name) { - BT_ERR("%s, Invalid parameter", __func__); + if (name == NULL) { + BT_ERR("Invalid node name"); return BLE_MESH_INVALID_NODE_INDEX; } - length = (strlen(name) <= BLE_MESH_NODE_NAME_SIZE) ? strlen(name) : BLE_MESH_NODE_NAME_SIZE; - for (i = 0; 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; - } - } + node = provisioner_find_node_with_name(name); + if (node == NULL) { + BT_ERR("Node name \"%s\" not exists", name); + return BLE_MESH_INVALID_NODE_INDEX; } - BT_ERR("Node name %s not exists", name); - return BLE_MESH_INVALID_NODE_INDEX; + return (node - mesh_nodes); +} + +struct bt_mesh_node *bt_mesh_provisioner_get_node_with_name(const char *name) +{ + struct bt_mesh_node **node = NULL; + + if (name == NULL) { + BT_ERR("Invalid node name"); + return NULL; + } + + node = provisioner_find_node_with_name(name); + if (node == NULL) { + BT_ERR("Node name \"%s\" not exists", name); + return NULL; + } + + return *node; +} + +const struct bt_mesh_node **bt_mesh_provisioner_get_node_table_entry(void) +{ + return (const struct bt_mesh_node **)mesh_nodes; } #define COMP_DATA_PAGE_0_MIN_LEN 16 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 f5552cd242..38ee43465e 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h @@ -85,6 +85,10 @@ const char *bt_mesh_provisioner_get_node_name(u16_t index); u16_t bt_mesh_provisioner_get_node_index(const char *name); +struct bt_mesh_node *bt_mesh_provisioner_get_node_with_name(const char *name); + +const struct bt_mesh_node **bt_mesh_provisioner_get_node_table_entry(void); + 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);