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 <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2019-01-25 18:24:44 +02:00 committed by Johan Hedberg
commit 3e11177a06
3 changed files with 47 additions and 7 deletions

View file

@ -38,19 +38,32 @@ int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx,
u8_t flags, u32_t iv_index, u16_t addr,
const u8_t dev_key[16])
{
bool pb_gatt_enabled;
int err;
BT_INFO("Primary Element: 0x%04x", addr);
BT_DBG("net_idx 0x%04x flags 0x%02x iv_index 0x%04x",
net_idx, flags, iv_index);
if (atomic_test_and_set_bit(bt_mesh.flags, BT_MESH_VALID)) {
return -EALREADY;
}
if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT)) {
bt_mesh_proxy_prov_disable();
if (bt_mesh_proxy_prov_disable() == 0) {
pb_gatt_enabled = true;
} else {
pb_gatt_enabled = false;
}
} else {
pb_gatt_enabled = false;
}
err = bt_mesh_net_create(net_idx, flags, net_key, iv_index);
if (err) {
if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT)) {
atomic_clear_bit(bt_mesh.flags, BT_MESH_VALID);
if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT) && pb_gatt_enabled) {
bt_mesh_proxy_prov_enable();
}

View file

@ -448,10 +448,6 @@ int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16],
BT_DBG("NetKey %s", bt_hex(key, 16));
if (atomic_test_bit(bt_mesh.flags, BT_MESH_VALID)) {
return -EALREADY;
}
(void)memset(msg_cache, 0, sizeof(msg_cache));
msg_cache_next = 0U;
@ -472,7 +468,6 @@ int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16],
}
}
atomic_set_bit(bt_mesh.flags, BT_MESH_VALID);
sub->net_idx = idx;
if (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) {

View file

@ -653,6 +653,14 @@ int bt_mesh_proxy_prov_enable(void)
BT_DBG("");
if (gatt_svc == MESH_GATT_PROV) {
return -EALREADY;
}
if (gatt_svc != MESH_GATT_NONE) {
return -EBUSY;
}
bt_gatt_service_register(&prov_svc);
gatt_svc = MESH_GATT_PROV;
prov_fast_adv = true;
@ -673,6 +681,14 @@ int bt_mesh_proxy_prov_disable(void)
BT_DBG("");
if (gatt_svc == MESH_GATT_NONE) {
return -EALREADY;
}
if (gatt_svc != MESH_GATT_PROV) {
return -EBUSY;
}
bt_gatt_service_unregister(&prov_svc);
gatt_svc = MESH_GATT_NONE;
@ -760,6 +776,14 @@ int bt_mesh_proxy_gatt_enable(void)
BT_DBG("");
if (gatt_svc == MESH_GATT_PROXY) {
return -EALREADY;
}
if (gatt_svc != MESH_GATT_NONE) {
return -EBUSY;
}
bt_gatt_service_register(&proxy_svc);
gatt_svc = MESH_GATT_PROXY;
@ -794,6 +818,14 @@ int bt_mesh_proxy_gatt_disable(void)
{
BT_DBG("");
if (gatt_svc == MESH_GATT_NONE) {
return -EALREADY;
}
if (gatt_svc != MESH_GATT_PROXY) {
return -EBUSY;
}
bt_mesh_proxy_gatt_disconnect();
bt_gatt_service_unregister(&proxy_svc);