mirror of
https://github.com/espressif/esp-nimble.git
synced 2026-08-22 01:40:00 +00:00
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.
This commit is contained in:
@@ -37,19 +37,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, (unsigned) iv_index);
|
||||
|
||||
if (atomic_test_and_set_bit(bt_mesh.flags, BT_MESH_VALID)) {
|
||||
return -EALREADY;
|
||||
}
|
||||
|
||||
if ((MYNEWT_VAL(BLE_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 ((MYNEWT_VAL(BLE_MESH_PB_GATT))) {
|
||||
atomic_clear_bit(bt_mesh.flags, BT_MESH_VALID);
|
||||
|
||||
if (MYNEWT_VAL(BLE_MESH_PB_GATT) && pb_gatt_enabled) {
|
||||
bt_mesh_proxy_prov_enable();
|
||||
}
|
||||
|
||||
|
||||
@@ -443,12 +443,8 @@ 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;
|
||||
}
|
||||
|
||||
memset(msg_cache, 0, sizeof(msg_cache));
|
||||
msg_cache_next = 0;
|
||||
(void)memset(msg_cache, 0, sizeof(msg_cache));
|
||||
msg_cache_next = 0U;
|
||||
|
||||
sub = &bt_mesh.sub[0];
|
||||
|
||||
@@ -467,7 +463,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 ((MYNEWT_VAL(BLE_MESH_GATT_PROXY))) {
|
||||
|
||||
@@ -683,6 +683,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;
|
||||
}
|
||||
|
||||
rc = ble_gatts_find_svc(BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_VAL), &handle);
|
||||
assert(rc == 0);
|
||||
ble_gatts_svc_set_visibility(handle, 1);
|
||||
@@ -710,6 +718,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;
|
||||
}
|
||||
|
||||
rc = ble_gatts_find_svc(BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_VAL), &handle);
|
||||
assert(rc == 0);
|
||||
ble_gatts_svc_set_visibility(handle, 0);
|
||||
@@ -757,6 +773,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;
|
||||
}
|
||||
|
||||
rc = ble_gatts_find_svc(BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_VAL), &handle);
|
||||
assert(rc == 0);
|
||||
ble_gatts_svc_set_visibility(handle, 1);
|
||||
@@ -801,6 +825,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();
|
||||
|
||||
rc = ble_gatts_find_svc(BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_VAL), &handle);
|
||||
|
||||
Reference in New Issue
Block a user