diff --git a/nimble/host/mesh/src/mesh.c b/nimble/host/mesh/src/mesh.c index 5c0e91e7e..82755b98c 100644 --- a/nimble/host/mesh/src/mesh.c +++ b/nimble/host/mesh/src/mesh.c @@ -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(); } diff --git a/nimble/host/mesh/src/net.c b/nimble/host/mesh/src/net.c index 7dc8a3079..81a683194 100644 --- a/nimble/host/mesh/src/net.c +++ b/nimble/host/mesh/src/net.c @@ -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))) { diff --git a/nimble/host/mesh/src/proxy.c b/nimble/host/mesh/src/proxy.c index ab28f8c30..7c0da02ca 100644 --- a/nimble/host/mesh/src/proxy.c +++ b/nimble/host/mesh/src/proxy.c @@ -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);