Skip to content

Commit 64e76ee

Browse files
llyespressif-bot
authored andcommitted
ble_mesh: stack: Move fast prov functions to a single file
1 parent ec661be commit 64e76ee

File tree

9 files changed

+244
-239
lines changed

9 files changed

+244
-239
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: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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+
struct bt_mesh_subnet_keys *key = NULL;
96+
struct bt_mesh_subnet *sub = NULL;
97+
98+
sub = bt_mesh_fast_prov_subnet_get(net_idx);
99+
if (sub) {
100+
key = BLE_MESH_KEY_REFRESH(sub->kr_flag) ? &sub->keys[1] : &sub->keys[0];
101+
return bt_mesh_provisioner_set_fast_prov_net_idx(key->net, net_idx);
102+
}
103+
104+
/* If NetKey is not found, set net_idx for fast provisioning,
105+
* and wait for Primary Provisioner to add NetKey.
106+
*/
107+
return bt_mesh_provisioner_set_fast_prov_net_idx(NULL, net_idx);
108+
}
109+
110+
u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16])
111+
{
112+
const u8_t *keys = NULL;
113+
u16_t net_idx = 0U;
114+
int err = 0;
115+
116+
net_idx = bt_mesh_provisioner_get_fast_prov_net_idx();
117+
bt_mesh.p_net_idx_next = net_idx;
118+
119+
err = bt_mesh_provisioner_local_net_key_add(net_key, &net_idx);
120+
if (err) {
121+
return 0x01; /* status: add net_key fail */
122+
};
123+
124+
keys = bt_mesh_provisioner_local_net_key_get(net_idx);
125+
if (!keys) {
126+
return 0x01; /* status: add net_key fail */
127+
}
128+
129+
return bt_mesh_provisioner_set_fast_prov_net_idx(keys, net_idx);
130+
}
131+
132+
const u8_t *bt_mesh_get_fast_prov_net_key(u16_t net_idx)
133+
{
134+
struct bt_mesh_subnet *sub = NULL;
135+
136+
sub = bt_mesh_fast_prov_subnet_get(net_idx);
137+
if (!sub) {
138+
BT_ERR("%s, NetKey Index 0x%03x not exists", __func__, net_idx);
139+
return NULL;
140+
}
141+
142+
return (sub->kr_flag ? sub->keys[1].net : sub->keys[0].net);
143+
}
144+
145+
const u8_t *bt_mesh_get_fast_prov_app_key(u16_t net_idx, u16_t app_idx)
146+
{
147+
struct bt_mesh_app_key *key = NULL;
148+
149+
key = bt_mesh_fast_prov_app_key_find(app_idx);
150+
if (!key) {
151+
BT_ERR("%s, AppKey Index 0x%03x not exists", __func__, app_idx);
152+
return NULL;
153+
}
154+
155+
return (key->updated ? key->keys[1].val : key->keys[0].val);
156+
}
157+
158+
u8_t bt_mesh_set_fast_prov_action(u8_t action)
159+
{
160+
if (!action || action > ACTION_EXIT) {
161+
return 0x01;
162+
}
163+
164+
if ((!bt_mesh_is_provisioner_en() && (action == ACTION_SUSPEND || action == ACTION_EXIT)) ||
165+
(bt_mesh_is_provisioner_en() && (action == ACTION_ENTER))) {
166+
BT_WARN("%s, Already", __func__);
167+
return 0x0;
168+
}
169+
170+
if (action == ACTION_ENTER) {
171+
if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) {
172+
bt_mesh_beacon_disable();
173+
}
174+
if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) {
175+
bt_mesh_provisioner_pb_gatt_enable();
176+
}
177+
bt_mesh_provisioner_set_primary_elem_addr(bt_mesh_primary_addr());
178+
bt_mesh_provisioner_set_prov_bearer(BLE_MESH_PROV_ADV, false);
179+
bt_mesh_provisioner_fast_prov_enable(true);
180+
bt_mesh_atomic_or(bt_mesh.flags, BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV));
181+
} else {
182+
if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) {
183+
bt_mesh_provisioner_pb_gatt_disable();
184+
}
185+
if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) {
186+
bt_mesh_beacon_enable();
187+
}
188+
bt_mesh_atomic_and(bt_mesh.flags, ~(BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV)));
189+
bt_mesh_provisioner_fast_prov_enable(false);
190+
if (action == ACTION_EXIT) {
191+
bt_mesh_provisioner_remove_node(NULL);
192+
}
193+
}
194+
195+
return 0x0;
196+
}
197+
#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_add_fast_prov_net_key(const u8_t net_key[16]);
33+
34+
const u8_t *bt_mesh_get_fast_prov_net_key(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)