Skip to content

Commit 0d5b983

Browse files
committed
Merge branch 'bugfix/ble_mesh_fast_prov_misc_update' into 'master'
Bugfix/ble mesh fast prov misc update See merge request espressif/esp-idf!9413
2 parents 370a1fa + 6ca4ea1 commit 0d5b983

File tree

11 files changed

+247
-258
lines changed

11 files changed

+247
-258
lines changed

components/bt/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ if(CONFIG_BT_ENABLED)
376376
"esp_ble_mesh/mesh_core/cfg_cli.c"
377377
"esp_ble_mesh/mesh_core/cfg_srv.c"
378378
"esp_ble_mesh/mesh_core/crypto.c"
379+
"esp_ble_mesh/mesh_core/fast_prov.c"
379380
"esp_ble_mesh/mesh_core/friend.c"
380381
"esp_ble_mesh/mesh_core/health_cli.c"
381382
"esp_ble_mesh/mesh_core/health_srv.c"

components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "btc/btc_manage.h"
1919
#include "mesh_byteorder.h"
2020
#include "mesh_main.h"
21+
#include "fast_prov.h"
2122
#include "provisioner_prov.h"
2223
#include "esp_ble_mesh_defs.h"
2324

components/bt/esp_ble_mesh/mesh_core/access.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "foundation.h"
2020
#include "mesh_main.h"
2121
#include "mesh_common.h"
22+
#include "fast_prov.h"
2223
#include "provisioner_main.h"
2324

2425
#include "generic_client.h"
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// Copyright 2017-2020 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <string.h>
16+
#include <errno.h>
17+
18+
#include "mesh.h"
19+
#include "mesh_common.h"
20+
#include "access.h"
21+
#include "beacon.h"
22+
#include "foundation.h"
23+
#include "proxy_client.h"
24+
#include "provisioner_prov.h"
25+
#include "provisioner_main.h"
26+
27+
#if CONFIG_BLE_MESH_FAST_PROV
28+
29+
#define ACTION_ENTER 0x01
30+
#define ACTION_SUSPEND 0x02
31+
#define ACTION_EXIT 0x03
32+
33+
const u8_t *bt_mesh_fast_prov_dev_key_get(u16_t dst)
34+
{
35+
if (!BLE_MESH_ADDR_IS_UNICAST(dst)) {
36+
BT_ERR("%s, Not a unicast address 0x%04x", __func__, dst);
37+
return NULL;
38+
}
39+
40+
if (dst == bt_mesh_primary_addr()) {
41+
return bt_mesh.dev_key;
42+
}
43+
44+
return bt_mesh_provisioner_dev_key_get(dst);
45+
}
46+
47+
struct bt_mesh_subnet *bt_mesh_fast_prov_subnet_get(u16_t net_idx)
48+
{
49+
struct bt_mesh_subnet *sub = NULL;
50+
int i;
51+
52+
for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
53+
sub = &bt_mesh.sub[i];
54+
if (sub->net_idx == net_idx) {
55+
return sub;
56+
}
57+
}
58+
59+
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) {
60+
sub = bt_mesh.p_sub[i];
61+
if (sub && sub->net_idx == net_idx) {
62+
return sub;
63+
}
64+
}
65+
66+
return NULL;
67+
}
68+
69+
struct bt_mesh_app_key *bt_mesh_fast_prov_app_key_find(u16_t app_idx)
70+
{
71+
struct bt_mesh_app_key *key = NULL;
72+
int i;
73+
74+
for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) {
75+
key = &bt_mesh.app_keys[i];
76+
if (key->net_idx != BLE_MESH_KEY_UNUSED &&
77+
key->app_idx == app_idx) {
78+
return key;
79+
}
80+
}
81+
82+
for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) {
83+
key = bt_mesh.p_app_keys[i];
84+
if (key && key->net_idx != BLE_MESH_KEY_UNUSED &&
85+
key->app_idx == app_idx) {
86+
return key;
87+
}
88+
}
89+
90+
return NULL;
91+
}
92+
93+
u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx)
94+
{
95+
/* Set net_idx for fast provisioning */
96+
bt_mesh_provisioner_set_fast_prov_net_idx(net_idx);
97+
98+
if (bt_mesh_fast_prov_subnet_get(net_idx) == NULL) {
99+
/* If NetKey is not found, wait for NetKey to be added. */
100+
BT_WARN("Wait for NetKey for fast provisioning");
101+
return 0x01; /*status: Wait for NetKey */
102+
}
103+
104+
return 0x0; /* status: Succeed */
105+
}
106+
107+
u8_t bt_mesh_fast_prov_net_key_add(const u8_t net_key[16])
108+
{
109+
u16_t net_idx = 0U;
110+
int err = 0;
111+
112+
net_idx = bt_mesh_provisioner_get_fast_prov_net_idx();
113+
bt_mesh.p_net_idx_next = net_idx;
114+
115+
err = bt_mesh_provisioner_local_net_key_add(net_key, &net_idx);
116+
if (err) {
117+
BT_ERR("%s, Failed to add NetKey 0x%04x", __func__, net_idx);
118+
return 0x01; /* status: Add NetKey failed */
119+
};
120+
121+
bt_mesh_provisioner_set_fast_prov_net_idx(net_idx);
122+
return 0x0; /* status: Succeed */
123+
}
124+
125+
const u8_t *bt_mesh_fast_prov_net_key_get(u16_t net_idx)
126+
{
127+
struct bt_mesh_subnet *sub = NULL;
128+
129+
sub = bt_mesh_fast_prov_subnet_get(net_idx);
130+
if (!sub) {
131+
BT_ERR("%s, NetKey Index 0x%03x not exists", __func__, net_idx);
132+
return NULL;
133+
}
134+
135+
return (sub->kr_flag ? sub->keys[1].net : sub->keys[0].net);
136+
}
137+
138+
const u8_t *bt_mesh_get_fast_prov_app_key(u16_t net_idx, u16_t app_idx)
139+
{
140+
struct bt_mesh_app_key *key = NULL;
141+
142+
key = bt_mesh_fast_prov_app_key_find(app_idx);
143+
if (!key) {
144+
BT_ERR("%s, AppKey Index 0x%03x not exists", __func__, app_idx);
145+
return NULL;
146+
}
147+
148+
return (key->updated ? key->keys[1].val : key->keys[0].val);
149+
}
150+
151+
u8_t bt_mesh_set_fast_prov_action(u8_t action)
152+
{
153+
if (!action || action > ACTION_EXIT) {
154+
return 0x01;
155+
}
156+
157+
if ((!bt_mesh_is_provisioner_en() && (action == ACTION_SUSPEND || action == ACTION_EXIT)) ||
158+
(bt_mesh_is_provisioner_en() && (action == ACTION_ENTER))) {
159+
BT_WARN("%s, Already", __func__);
160+
return 0x0;
161+
}
162+
163+
if (action == ACTION_ENTER) {
164+
if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) {
165+
bt_mesh_beacon_disable();
166+
}
167+
if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) {
168+
bt_mesh_provisioner_pb_gatt_enable();
169+
}
170+
bt_mesh_provisioner_set_primary_elem_addr(bt_mesh_primary_addr());
171+
bt_mesh_provisioner_set_prov_bearer(BLE_MESH_PROV_ADV, false);
172+
bt_mesh_provisioner_fast_prov_enable(true);
173+
bt_mesh_atomic_or(bt_mesh.flags, BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV));
174+
} else {
175+
if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) {
176+
bt_mesh_provisioner_pb_gatt_disable();
177+
}
178+
if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) {
179+
bt_mesh_beacon_enable();
180+
}
181+
bt_mesh_atomic_and(bt_mesh.flags, ~(BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV)));
182+
bt_mesh_provisioner_fast_prov_enable(false);
183+
if (action == ACTION_EXIT) {
184+
bt_mesh_provisioner_remove_node(NULL);
185+
}
186+
}
187+
188+
return 0x0;
189+
}
190+
#endif /* CONFIG_BLE_MESH_FAST_PROV */
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2017-2020 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef _FAST_PROV_H_
16+
#define _FAST_PROV_H_
17+
18+
#include "net.h"
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
const u8_t *bt_mesh_fast_prov_dev_key_get(u16_t dst);
25+
26+
struct bt_mesh_subnet *bt_mesh_fast_prov_subnet_get(u16_t net_idx);
27+
28+
struct bt_mesh_app_key *bt_mesh_fast_prov_app_key_find(u16_t app_idx);
29+
30+
u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx);
31+
32+
u8_t bt_mesh_fast_prov_net_key_add(const u8_t net_key[16]);
33+
34+
const u8_t *bt_mesh_fast_prov_net_key_get(u16_t net_idx);
35+
36+
const u8_t *bt_mesh_get_fast_prov_app_key(u16_t net_idx, u16_t app_idx);
37+
38+
u8_t bt_mesh_set_fast_prov_action(u8_t action);
39+
40+
#ifdef __cplusplus
41+
}
42+
#endif
43+
44+
#endif /* _FAST_PROV_H_ */

components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -359,17 +359,6 @@ int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers);
359359
*/
360360
int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers);
361361

362-
/* The following API is for BLE Mesh Fast Provisioning */
363-
364-
/** @brief Change the device action
365-
*
366-
* @param[IN] action: role of device to be set
367-
* 0x01 - enter, 0x02 - suspend, 0x03 - exit
368-
*
369-
* @return status
370-
*/
371-
u8_t bt_mesh_set_fast_prov_action(u8_t action);
372-
373362
/* The following APIs are for BLE Mesh Provisioner */
374363

375364
/** @brief Provide provisioning input OOB string.

components/bt/esp_ble_mesh/mesh_core/main.c

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
#include "provisioner_prov.h"
3030
#include "provisioner_main.h"
3131

32-
#define ACTION_ENTER 0x01
33-
#define ACTION_SUSPEND 0x02
34-
#define ACTION_EXIT 0x03
35-
3632
static bool mesh_init = false;
3733

3834
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)
627623
return 0;
628624
}
629625
#endif /* CONFIG_BLE_MESH_PROVISIONER */
630-
631-
/* The following API is for fast provisioning */
632-
633-
#if CONFIG_BLE_MESH_FAST_PROV
634-
u8_t bt_mesh_set_fast_prov_action(u8_t action)
635-
{
636-
if (!action || action > ACTION_EXIT) {
637-
return 0x01;
638-
}
639-
640-
if ((!bt_mesh_is_provisioner_en() && (action == ACTION_SUSPEND || action == ACTION_EXIT)) ||
641-
(bt_mesh_is_provisioner_en() && (action == ACTION_ENTER))) {
642-
BT_WARN("%s, Already", __func__);
643-
return 0x0;
644-
}
645-
646-
if (action == ACTION_ENTER) {
647-
#if 0
648-
/* If the device is provisioned using PB-GATT and connected to
649-
* the phone with proxy service, proxy_gatt shall not be disabled
650-
* here. The node needs to send some status messages to the phone
651-
* while it is connected.
652-
*/
653-
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
654-
bt_mesh_proxy_gatt_disable();
655-
}
656-
#endif
657-
if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) {
658-
bt_mesh_beacon_disable();
659-
}
660-
if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) {
661-
bt_mesh_provisioner_pb_gatt_enable();
662-
}
663-
bt_mesh_provisioner_set_primary_elem_addr(bt_mesh_primary_addr());
664-
bt_mesh_provisioner_set_prov_bearer(BLE_MESH_PROV_ADV, false);
665-
bt_mesh_provisioner_fast_prov_enable(true);
666-
bt_mesh_atomic_or(bt_mesh.flags, BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV));
667-
} else {
668-
if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) {
669-
bt_mesh_provisioner_pb_gatt_disable();
670-
}
671-
if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) {
672-
bt_mesh_beacon_enable();
673-
}
674-
#if 0
675-
/* Mesh Proxy GATT will be re-enabled on application layer */
676-
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
677-
bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_NOT_SUPPORTED) {
678-
bt_mesh_proxy_gatt_enable();
679-
bt_mesh_adv_update();
680-
}
681-
#endif
682-
bt_mesh_atomic_and(bt_mesh.flags, ~(BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV)));
683-
bt_mesh_provisioner_fast_prov_enable(false);
684-
if (action == ACTION_EXIT) {
685-
bt_mesh_provisioner_remove_node(NULL);
686-
}
687-
}
688-
689-
return 0x0;
690-
}
691-
#endif /* CONFIG_BLE_MESH_FAST_PROV */

0 commit comments

Comments
 (0)