Skip to content

Commit 3e11177

Browse files
Johan Hedbergjhedberg
authored andcommitted
Bluetooth: Mesh: Clean up net validity & provisioning state handling
Tracking of the BT_MESH_VALID flag and the PB-GATT state was rather fragile. Add proper error returns to the various GATT service enable & disable handlers, and toggle the BT_MESH_VALID flag in a single file (main.c). Use the newly added error returns to ensure that we don't re-enable PB-GATT if it wasn't already enabled from before. Signed-off-by: Johan Hedberg <[email protected]>
1 parent fbe661f commit 3e11177

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

subsys/bluetooth/host/mesh/main.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,32 @@ int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx,
3838
u8_t flags, u32_t iv_index, u16_t addr,
3939
const u8_t dev_key[16])
4040
{
41+
bool pb_gatt_enabled;
4142
int err;
4243

4344
BT_INFO("Primary Element: 0x%04x", addr);
4445
BT_DBG("net_idx 0x%04x flags 0x%02x iv_index 0x%04x",
4546
net_idx, flags, iv_index);
4647

48+
if (atomic_test_and_set_bit(bt_mesh.flags, BT_MESH_VALID)) {
49+
return -EALREADY;
50+
}
51+
4752
if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT)) {
48-
bt_mesh_proxy_prov_disable();
53+
if (bt_mesh_proxy_prov_disable() == 0) {
54+
pb_gatt_enabled = true;
55+
} else {
56+
pb_gatt_enabled = false;
57+
}
58+
} else {
59+
pb_gatt_enabled = false;
4960
}
5061

5162
err = bt_mesh_net_create(net_idx, flags, net_key, iv_index);
5263
if (err) {
53-
if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT)) {
64+
atomic_clear_bit(bt_mesh.flags, BT_MESH_VALID);
65+
66+
if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT) && pb_gatt_enabled) {
5467
bt_mesh_proxy_prov_enable();
5568
}
5669

subsys/bluetooth/host/mesh/net.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,6 @@ int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16],
448448

449449
BT_DBG("NetKey %s", bt_hex(key, 16));
450450

451-
if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
452-
return -EALREADY;
453-
}
454-
455451
(void)memset(msg_cache, 0, sizeof(msg_cache));
456452
msg_cache_next = 0U;
457453

@@ -472,7 +468,6 @@ int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16],
472468
}
473469
}
474470

475-
atomic_set_bit(bt_mesh.flags, BT_MESH_VALID);
476471
sub->net_idx = idx;
477472

478473
if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) {

subsys/bluetooth/host/mesh/proxy.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,14 @@ int bt_mesh_proxy_prov_enable(void)
653653

654654
BT_DBG("");
655655

656+
if (gatt_svc == MESH_GATT_PROV) {
657+
return -EALREADY;
658+
}
659+
660+
if (gatt_svc != MESH_GATT_NONE) {
661+
return -EBUSY;
662+
}
663+
656664
bt_gatt_service_register(&prov_svc);
657665
gatt_svc = MESH_GATT_PROV;
658666
prov_fast_adv = true;
@@ -673,6 +681,14 @@ int bt_mesh_proxy_prov_disable(void)
673681

674682
BT_DBG("");
675683

684+
if (gatt_svc == MESH_GATT_NONE) {
685+
return -EALREADY;
686+
}
687+
688+
if (gatt_svc != MESH_GATT_PROV) {
689+
return -EBUSY;
690+
}
691+
676692
bt_gatt_service_unregister(&prov_svc);
677693
gatt_svc = MESH_GATT_NONE;
678694

@@ -760,6 +776,14 @@ int bt_mesh_proxy_gatt_enable(void)
760776

761777
BT_DBG("");
762778

779+
if (gatt_svc == MESH_GATT_PROXY) {
780+
return -EALREADY;
781+
}
782+
783+
if (gatt_svc != MESH_GATT_NONE) {
784+
return -EBUSY;
785+
}
786+
763787
bt_gatt_service_register(&proxy_svc);
764788
gatt_svc = MESH_GATT_PROXY;
765789

@@ -794,6 +818,14 @@ int bt_mesh_proxy_gatt_disable(void)
794818
{
795819
BT_DBG("");
796820

821+
if (gatt_svc == MESH_GATT_NONE) {
822+
return -EALREADY;
823+
}
824+
825+
if (gatt_svc != MESH_GATT_PROXY) {
826+
return -EBUSY;
827+
}
828+
797829
bt_mesh_proxy_gatt_disconnect();
798830

799831
bt_gatt_service_unregister(&proxy_svc);

0 commit comments

Comments
 (0)