From 661e1f25005c51cdd6f1bb60582edeb1c2065cfc Mon Sep 17 00:00:00 2001 From: lly Date: Tue, 30 Jun 2020 11:09:58 +0800 Subject: [PATCH 1/2] ble_mesh: stack: Move fast prov functions to a single file --- components/bt/CMakeLists.txt | 1 + .../btc/include/btc_ble_mesh_prov.h | 1 + components/bt/esp_ble_mesh/mesh_core/access.c | 1 + .../bt/esp_ble_mesh/mesh_core/fast_prov.c | 197 ++++++++++++++++++ .../bt/esp_ble_mesh/mesh_core/fast_prov.h | 44 ++++ .../mesh_core/include/mesh_main.h | 11 - components/bt/esp_ble_mesh/mesh_core/main.c | 66 ------ .../esp_ble_mesh/mesh_core/provisioner_main.c | 146 ------------- .../esp_ble_mesh/mesh_core/provisioner_main.h | 16 -- 9 files changed, 244 insertions(+), 239 deletions(-) create mode 100644 components/bt/esp_ble_mesh/mesh_core/fast_prov.c create mode 100644 components/bt/esp_ble_mesh/mesh_core/fast_prov.h diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index 5d2e9c548..27826e8d4 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -376,6 +376,7 @@ if(CONFIG_BT_ENABLED) "esp_ble_mesh/mesh_core/cfg_cli.c" "esp_ble_mesh/mesh_core/cfg_srv.c" "esp_ble_mesh/mesh_core/crypto.c" + "esp_ble_mesh/mesh_core/fast_prov.c" "esp_ble_mesh/mesh_core/friend.c" "esp_ble_mesh/mesh_core/health_cli.c" "esp_ble_mesh/mesh_core/health_srv.c" 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 4566135c8..0266317da 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 @@ -18,6 +18,7 @@ #include "btc/btc_manage.h" #include "mesh_byteorder.h" #include "mesh_main.h" +#include "fast_prov.h" #include "provisioner_prov.h" #include "esp_ble_mesh_defs.h" diff --git a/components/bt/esp_ble_mesh/mesh_core/access.c b/components/bt/esp_ble_mesh/mesh_core/access.c index 2711e724b..6ffeca8e5 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.c +++ b/components/bt/esp_ble_mesh/mesh_core/access.c @@ -19,6 +19,7 @@ #include "foundation.h" #include "mesh_main.h" #include "mesh_common.h" +#include "fast_prov.h" #include "provisioner_main.h" #include "generic_client.h" diff --git a/components/bt/esp_ble_mesh/mesh_core/fast_prov.c b/components/bt/esp_ble_mesh/mesh_core/fast_prov.c new file mode 100644 index 000000000..305b37550 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/fast_prov.c @@ -0,0 +1,197 @@ +// Copyright 2017-2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "mesh.h" +#include "mesh_common.h" +#include "access.h" +#include "beacon.h" +#include "foundation.h" +#include "proxy_client.h" +#include "provisioner_prov.h" +#include "provisioner_main.h" + +#if CONFIG_BLE_MESH_FAST_PROV + +#define ACTION_ENTER 0x01 +#define ACTION_SUSPEND 0x02 +#define ACTION_EXIT 0x03 + +const u8_t *bt_mesh_fast_prov_dev_key_get(u16_t dst) +{ + if (!BLE_MESH_ADDR_IS_UNICAST(dst)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, dst); + return NULL; + } + + if (dst == bt_mesh_primary_addr()) { + return bt_mesh.dev_key; + } + + return bt_mesh_provisioner_dev_key_get(dst); +} + +struct bt_mesh_subnet *bt_mesh_fast_prov_subnet_get(u16_t net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + sub = &bt_mesh.sub[i]; + if (sub->net_idx == net_idx) { + return sub; + } + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + sub = bt_mesh.p_sub[i]; + if (sub && sub->net_idx == net_idx) { + return sub; + } + } + + return NULL; +} + +struct bt_mesh_app_key *bt_mesh_fast_prov_app_key_find(u16_t app_idx) +{ + struct bt_mesh_app_key *key = NULL; + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { + key = &bt_mesh.app_keys[i]; + if (key->net_idx != BLE_MESH_KEY_UNUSED && + key->app_idx == app_idx) { + return key; + } + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { + key = bt_mesh.p_app_keys[i]; + if (key && key->net_idx != BLE_MESH_KEY_UNUSED && + key->app_idx == app_idx) { + return key; + } + } + + return NULL; +} + +u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx) +{ + struct bt_mesh_subnet_keys *key = NULL; + struct bt_mesh_subnet *sub = NULL; + + sub = bt_mesh_fast_prov_subnet_get(net_idx); + if (sub) { + key = BLE_MESH_KEY_REFRESH(sub->kr_flag) ? &sub->keys[1] : &sub->keys[0]; + return bt_mesh_provisioner_set_fast_prov_net_idx(key->net, net_idx); + } + + /* If NetKey is not found, set net_idx for fast provisioning, + * and wait for Primary Provisioner to add NetKey. + */ + return bt_mesh_provisioner_set_fast_prov_net_idx(NULL, net_idx); +} + +u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16]) +{ + const u8_t *keys = NULL; + u16_t net_idx = 0U; + int err = 0; + + net_idx = bt_mesh_provisioner_get_fast_prov_net_idx(); + bt_mesh.p_net_idx_next = net_idx; + + err = bt_mesh_provisioner_local_net_key_add(net_key, &net_idx); + if (err) { + return 0x01; /* status: add net_key fail */ + }; + + keys = bt_mesh_provisioner_local_net_key_get(net_idx); + if (!keys) { + return 0x01; /* status: add net_key fail */ + } + + return bt_mesh_provisioner_set_fast_prov_net_idx(keys, net_idx); +} + +const u8_t *bt_mesh_get_fast_prov_net_key(u16_t net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + + sub = bt_mesh_fast_prov_subnet_get(net_idx); + if (!sub) { + BT_ERR("%s, NetKey Index 0x%03x not exists", __func__, net_idx); + return NULL; + } + + return (sub->kr_flag ? sub->keys[1].net : sub->keys[0].net); +} + +const u8_t *bt_mesh_get_fast_prov_app_key(u16_t net_idx, u16_t app_idx) +{ + struct bt_mesh_app_key *key = NULL; + + key = bt_mesh_fast_prov_app_key_find(app_idx); + if (!key) { + BT_ERR("%s, AppKey Index 0x%03x not exists", __func__, app_idx); + return NULL; + } + + return (key->updated ? key->keys[1].val : key->keys[0].val); +} + +u8_t bt_mesh_set_fast_prov_action(u8_t action) +{ + if (!action || action > ACTION_EXIT) { + return 0x01; + } + + if ((!bt_mesh_is_provisioner_en() && (action == ACTION_SUSPEND || action == ACTION_EXIT)) || + (bt_mesh_is_provisioner_en() && (action == ACTION_ENTER))) { + BT_WARN("%s, Already", __func__); + return 0x0; + } + + if (action == ACTION_ENTER) { + if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { + bt_mesh_beacon_disable(); + } + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { + bt_mesh_provisioner_pb_gatt_enable(); + } + bt_mesh_provisioner_set_primary_elem_addr(bt_mesh_primary_addr()); + bt_mesh_provisioner_set_prov_bearer(BLE_MESH_PROV_ADV, false); + bt_mesh_provisioner_fast_prov_enable(true); + bt_mesh_atomic_or(bt_mesh.flags, BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV)); + } else { + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { + bt_mesh_provisioner_pb_gatt_disable(); + } + if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { + bt_mesh_beacon_enable(); + } + bt_mesh_atomic_and(bt_mesh.flags, ~(BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV))); + bt_mesh_provisioner_fast_prov_enable(false); + if (action == ACTION_EXIT) { + bt_mesh_provisioner_remove_node(NULL); + } + } + + return 0x0; +} +#endif /* CONFIG_BLE_MESH_FAST_PROV */ diff --git a/components/bt/esp_ble_mesh/mesh_core/fast_prov.h b/components/bt/esp_ble_mesh/mesh_core/fast_prov.h new file mode 100644 index 000000000..b06f3638f --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/fast_prov.h @@ -0,0 +1,44 @@ +// Copyright 2017-2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _FAST_PROV_H_ +#define _FAST_PROV_H_ + +#include "net.h" + +#ifdef __cplusplus +extern "C" { +#endif + +const u8_t *bt_mesh_fast_prov_dev_key_get(u16_t dst); + +struct bt_mesh_subnet *bt_mesh_fast_prov_subnet_get(u16_t net_idx); + +struct bt_mesh_app_key *bt_mesh_fast_prov_app_key_find(u16_t app_idx); + +u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx); + +u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16]); + +const u8_t *bt_mesh_get_fast_prov_net_key(u16_t net_idx); + +const u8_t *bt_mesh_get_fast_prov_app_key(u16_t net_idx, u16_t app_idx); + +u8_t bt_mesh_set_fast_prov_action(u8_t action); + +#ifdef __cplusplus +} +#endif + +#endif /* _FAST_PROV_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h index 362c0bec6..35d38a9c7 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h @@ -359,17 +359,6 @@ int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers); */ int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers); -/* The following API is for BLE Mesh Fast Provisioning */ - -/** @brief Change the device action - * - * @param[IN] action: role of device to be set - * 0x01 - enter, 0x02 - suspend, 0x03 - exit - * - * @return status - */ -u8_t bt_mesh_set_fast_prov_action(u8_t action); - /* The following APIs are for BLE Mesh Provisioner */ /** @brief Provide provisioning input OOB string. diff --git a/components/bt/esp_ble_mesh/mesh_core/main.c b/components/bt/esp_ble_mesh/mesh_core/main.c index 978a0aa24..32a9c0939 100644 --- a/components/bt/esp_ble_mesh/mesh_core/main.c +++ b/components/bt/esp_ble_mesh/mesh_core/main.c @@ -29,10 +29,6 @@ #include "provisioner_prov.h" #include "provisioner_main.h" -#define ACTION_ENTER 0x01 -#define ACTION_SUSPEND 0x02 -#define ACTION_EXIT 0x03 - static bool mesh_init = false; int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx, @@ -627,65 +623,3 @@ int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers) return 0; } #endif /* CONFIG_BLE_MESH_PROVISIONER */ - -/* The following API is for fast provisioning */ - -#if CONFIG_BLE_MESH_FAST_PROV -u8_t bt_mesh_set_fast_prov_action(u8_t action) -{ - if (!action || action > ACTION_EXIT) { - return 0x01; - } - - if ((!bt_mesh_is_provisioner_en() && (action == ACTION_SUSPEND || action == ACTION_EXIT)) || - (bt_mesh_is_provisioner_en() && (action == ACTION_ENTER))) { - BT_WARN("%s, Already", __func__); - return 0x0; - } - - if (action == ACTION_ENTER) { -#if 0 - /* If the device is provisioned using PB-GATT and connected to - * the phone with proxy service, proxy_gatt shall not be disabled - * here. The node needs to send some status messages to the phone - * while it is connected. - */ - if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { - bt_mesh_proxy_gatt_disable(); - } -#endif - if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { - bt_mesh_beacon_disable(); - } - if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { - bt_mesh_provisioner_pb_gatt_enable(); - } - bt_mesh_provisioner_set_primary_elem_addr(bt_mesh_primary_addr()); - bt_mesh_provisioner_set_prov_bearer(BLE_MESH_PROV_ADV, false); - bt_mesh_provisioner_fast_prov_enable(true); - bt_mesh_atomic_or(bt_mesh.flags, BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV)); - } else { - if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { - bt_mesh_provisioner_pb_gatt_disable(); - } - if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { - bt_mesh_beacon_enable(); - } -#if 0 - /* Mesh Proxy GATT will be re-enabled on application layer */ - if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && - bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_NOT_SUPPORTED) { - bt_mesh_proxy_gatt_enable(); - bt_mesh_adv_update(); - } -#endif - bt_mesh_atomic_and(bt_mesh.flags, ~(BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV))); - bt_mesh_provisioner_fast_prov_enable(false); - if (action == ACTION_EXIT) { - bt_mesh_provisioner_remove_node(NULL); - } - } - - return 0x0; -} -#endif /* CONFIG_BLE_MESH_FAST_PROV */ 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 f27e2f01c..ceb534ce7 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c @@ -1538,149 +1538,3 @@ int bt_mesh_provisioner_store_node_info(struct bt_mesh_node *node) #endif /* CONFIG_BLE_MESH_TEST_AUTO_ENTER_NETWORK */ #endif /* CONFIG_BLE_MESH_PROVISIONER */ - -/* The following APIs are for fast provisioning */ - -#if CONFIG_BLE_MESH_FAST_PROV - -const u8_t *bt_mesh_fast_prov_dev_key_get(u16_t dst) -{ - struct bt_mesh_node *node = NULL; - int i; - - BT_DBG("%s", __func__); - - if (!BLE_MESH_ADDR_IS_UNICAST(dst)) { - BT_ERR("%s, Not a unicast address 0x%04x", __func__, dst); - return NULL; - } - - if (dst == bt_mesh_primary_addr()) { - return bt_mesh.dev_key; - } - - for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { - node = mesh_nodes[i]; - if (node && node->unicast_addr == dst) { - return node->dev_key; - } - } - - return NULL; -} - -struct bt_mesh_subnet *bt_mesh_fast_prov_subnet_get(u16_t net_idx) -{ - struct bt_mesh_subnet *sub = NULL; - int i; - - BT_DBG("%s", __func__); - - for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { - sub = &bt_mesh.sub[i]; - if (sub->net_idx == net_idx) { - return sub; - } - } - - for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { - sub = bt_mesh.p_sub[i]; - if (sub && sub->net_idx == net_idx) { - return sub; - } - } - - return NULL; -} - -struct bt_mesh_app_key *bt_mesh_fast_prov_app_key_find(u16_t app_idx) -{ - struct bt_mesh_app_key *key = NULL; - int i; - - BT_DBG("%s", __func__); - - for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { - key = &bt_mesh.app_keys[i]; - if (key->net_idx != BLE_MESH_KEY_UNUSED && - key->app_idx == app_idx) { - return key; - } - } - - for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { - key = bt_mesh.p_app_keys[i]; - if (key && key->net_idx != BLE_MESH_KEY_UNUSED && - key->app_idx == app_idx) { - return key; - } - } - - return NULL; -} - -u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx) -{ - struct bt_mesh_subnet_keys *key = NULL; - struct bt_mesh_subnet *sub = NULL; - - sub = bt_mesh_fast_prov_subnet_get(net_idx); - if (sub) { - key = BLE_MESH_KEY_REFRESH(sub->kr_flag) ? &sub->keys[1] : &sub->keys[0]; - return bt_mesh_provisioner_set_fast_prov_net_idx(key->net, net_idx); - } - - /* If net_idx is not found, set net_idx to fast_prov first, - * and wait for primary provisioner to add net_key */ - return bt_mesh_provisioner_set_fast_prov_net_idx(NULL, net_idx); -} - -u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16]) -{ - const u8_t *keys = NULL; - u16_t net_idx = 0U; - int err = 0; - - net_idx = bt_mesh_provisioner_get_fast_prov_net_idx(); - bt_mesh.p_net_idx_next = net_idx; - - err = bt_mesh_provisioner_local_net_key_add(net_key, &net_idx); - if (err) { - return 0x01; /* status: add net_key fail */ - }; - - keys = bt_mesh_provisioner_local_net_key_get(net_idx); - if (!keys) { - return 0x01; /* status: add net_key fail */ - } - - return bt_mesh_provisioner_set_fast_prov_net_idx(keys, net_idx); -} - -const u8_t *bt_mesh_get_fast_prov_net_key(u16_t net_idx) -{ - struct bt_mesh_subnet *sub = NULL; - - sub = bt_mesh_fast_prov_subnet_get(net_idx); - if (!sub) { - BT_ERR("%s, NetKey Index 0x%03x not exists", __func__, net_idx); - return NULL; - } - - return (sub->kr_flag ? sub->keys[1].net : sub->keys[0].net); -} - -const u8_t *bt_mesh_get_fast_prov_app_key(u16_t net_idx, u16_t app_idx) -{ - struct bt_mesh_app_key *key = NULL; - - key = bt_mesh_fast_prov_app_key_find(app_idx); - if (!key) { - BT_ERR("%s, AppKey Index 0x%03x not exists", __func__, app_idx); - return NULL; - } - - return (key->updated ? key->keys[1].val : key->keys[0].val); -} - -#endif /* CONFIG_BLE_MESH_FAST_PROV */ 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 38ee43465..f742bfcd9 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h @@ -126,22 +126,6 @@ int bt_mesh_print_local_composition_data(void); int bt_mesh_provisioner_store_node_info(struct bt_mesh_node *node); -/* The following APIs are for fast provisioning */ - -const u8_t *bt_mesh_fast_prov_dev_key_get(u16_t dst); - -struct bt_mesh_subnet *bt_mesh_fast_prov_subnet_get(u16_t net_idx); - -struct bt_mesh_app_key *bt_mesh_fast_prov_app_key_find(u16_t app_idx); - -u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx); - -u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16]); - -const u8_t *bt_mesh_get_fast_prov_net_key(u16_t net_idx); - -const u8_t *bt_mesh_get_fast_prov_app_key(u16_t net_idx, u16_t app_idx); - #ifdef __cplusplus } #endif From dd6de08beaf14b2ab2e518a51899e9d309bb2f0f Mon Sep 17 00:00:00 2001 From: lly Date: Tue, 30 Jun 2020 14:45:51 +0800 Subject: [PATCH 2/2] ble_mesh: stack: Update some fast prov functions --- .../bt/esp_ble_mesh/mesh_core/fast_prov.c | 33 ++++++++----------- .../bt/esp_ble_mesh/mesh_core/fast_prov.h | 4 +-- .../esp_ble_mesh/mesh_core/provisioner_prov.c | 24 +++++--------- .../esp_ble_mesh/mesh_core/provisioner_prov.h | 5 ++- 4 files changed, 25 insertions(+), 41 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/fast_prov.c b/components/bt/esp_ble_mesh/mesh_core/fast_prov.c index 305b37550..eb6437f92 100644 --- a/components/bt/esp_ble_mesh/mesh_core/fast_prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/fast_prov.c @@ -92,24 +92,20 @@ struct bt_mesh_app_key *bt_mesh_fast_prov_app_key_find(u16_t app_idx) u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx) { - struct bt_mesh_subnet_keys *key = NULL; - struct bt_mesh_subnet *sub = NULL; + /* Set net_idx for fast provisioning */ + bt_mesh_provisioner_set_fast_prov_net_idx(net_idx); - sub = bt_mesh_fast_prov_subnet_get(net_idx); - if (sub) { - key = BLE_MESH_KEY_REFRESH(sub->kr_flag) ? &sub->keys[1] : &sub->keys[0]; - return bt_mesh_provisioner_set_fast_prov_net_idx(key->net, net_idx); + if (bt_mesh_fast_prov_subnet_get(net_idx) == NULL) { + /* If NetKey is not found, wait for NetKey to be added. */ + BT_WARN("Wait for NetKey for fast provisioning"); + return 0x01; /*status: Wait for NetKey */ } - /* If NetKey is not found, set net_idx for fast provisioning, - * and wait for Primary Provisioner to add NetKey. - */ - return bt_mesh_provisioner_set_fast_prov_net_idx(NULL, net_idx); + return 0x0; /* status: Succeed */ } -u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16]) +u8_t bt_mesh_fast_prov_net_key_add(const u8_t net_key[16]) { - const u8_t *keys = NULL; u16_t net_idx = 0U; int err = 0; @@ -118,18 +114,15 @@ u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16]) err = bt_mesh_provisioner_local_net_key_add(net_key, &net_idx); if (err) { - return 0x01; /* status: add net_key fail */ + BT_ERR("%s, Failed to add NetKey 0x%04x", __func__, net_idx); + return 0x01; /* status: Add NetKey failed */ }; - keys = bt_mesh_provisioner_local_net_key_get(net_idx); - if (!keys) { - return 0x01; /* status: add net_key fail */ - } - - return bt_mesh_provisioner_set_fast_prov_net_idx(keys, net_idx); + bt_mesh_provisioner_set_fast_prov_net_idx(net_idx); + return 0x0; /* status: Succeed */ } -const u8_t *bt_mesh_get_fast_prov_net_key(u16_t net_idx) +const u8_t *bt_mesh_fast_prov_net_key_get(u16_t net_idx) { struct bt_mesh_subnet *sub = NULL; diff --git a/components/bt/esp_ble_mesh/mesh_core/fast_prov.h b/components/bt/esp_ble_mesh/mesh_core/fast_prov.h index b06f3638f..9ab6fc4be 100644 --- a/components/bt/esp_ble_mesh/mesh_core/fast_prov.h +++ b/components/bt/esp_ble_mesh/mesh_core/fast_prov.h @@ -29,9 +29,9 @@ struct bt_mesh_app_key *bt_mesh_fast_prov_app_key_find(u16_t app_idx); u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx); -u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16]); +u8_t bt_mesh_fast_prov_net_key_add(const u8_t net_key[16]); -const u8_t *bt_mesh_get_fast_prov_net_key(u16_t net_idx); +const u8_t *bt_mesh_fast_prov_net_key_get(u16_t net_idx); const u8_t *bt_mesh_get_fast_prov_app_key(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 d385eb701..ec45b7afa 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c @@ -20,6 +20,7 @@ #include "mesh.h" #include "access.h" #include "settings.h" +#include "fast_prov.h" #include "mesh_common.h" #include "proxy_client.h" #include "provisioner_prov.h" @@ -276,7 +277,6 @@ struct bt_mesh_prov_ctx { struct { bool enable; u16_t net_idx; - const u8_t *net_key; u8_t flags; u32_t iv_index; u16_t unicast_addr_min; @@ -1280,17 +1280,9 @@ void bt_mesh_provisioner_fast_prov_enable(bool enable) prov_ctx.fast_prov.enable = enable; } -u8_t bt_mesh_provisioner_set_fast_prov_net_idx(const u8_t *net_key, u16_t net_idx) +void bt_mesh_provisioner_set_fast_prov_net_idx(u16_t net_idx) { prov_ctx.fast_prov.net_idx = net_idx; - prov_ctx.fast_prov.net_key = net_key; - - if (!net_key) { - BT_WARN("Wait for NetKey for fast provisioning"); - return 0x01; /*status: wait for net_key */ - } - - return 0x0; /* status: success */ } u16_t bt_mesh_provisioner_get_fast_prov_net_idx(void) @@ -2456,10 +2448,10 @@ static void send_prov_data(const u8_t idx) * will be added to the primary subnet, and may add an API to choose to which * subnet will the device be provisioned later. */ - if (FAST_PROV_ENABLE()) { - netkey = prov_ctx.fast_prov.net_key; + if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && FAST_PROV_ENABLE()) { + netkey = bt_mesh_fast_prov_net_key_get(prov_ctx.fast_prov.net_idx); if (!netkey) { - BT_ERR("%s, Failed to get NetKey for fast provisioning", __func__); + BT_ERR("No NetKey for fast provisioning"); goto fail; } memcpy(pdu, netkey, 16); @@ -2469,7 +2461,7 @@ static void send_prov_data(const u8_t idx) } else { netkey = bt_mesh_provisioner_net_key_get(prov_ctx.curr_net_idx); if (!netkey) { - BT_ERR("%s, Failed to get NetKey for provisioning data", __func__); + BT_ERR("No NetKey for provisioning data"); goto fail; } memcpy(pdu, netkey, 16); @@ -2580,7 +2572,7 @@ static void send_prov_data(const u8_t idx) } } - if (FAST_PROV_ENABLE()) { + if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && FAST_PROV_ENABLE()) { link[idx].ki_flags = prov_ctx.fast_prov.flags; link[idx].iv_index = prov_ctx.fast_prov.iv_index; } else { @@ -2668,7 +2660,7 @@ static void prov_complete(const u8_t idx, const u8_t *data) return; } - if (FAST_PROV_ENABLE()) { + if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && FAST_PROV_ENABLE()) { net_idx = prov_ctx.fast_prov.net_idx; } else { net_idx = prov_ctx.curr_net_idx; diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h index 4b74b425e..95eb46013 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h @@ -386,12 +386,11 @@ void bt_mesh_provisioner_fast_prov_enable(bool enable); /** * @brief This function is called to set netkey index used for fast provisioning. * - * @param[in] net_key: Netkey value * @param[in] net_idx: Netkey index * - * @return status for set netkey index msg + * @return None */ -u8_t bt_mesh_provisioner_set_fast_prov_net_idx(const u8_t *net_key, u16_t net_idx); +void bt_mesh_provisioner_set_fast_prov_net_idx(u16_t net_idx); /** * @brief This function is called to get netkey index used for fast provisioning.