diff --git a/components/bt/common/btc/profile/esp/blufi/include/esp_blufi.h b/components/bt/common/btc/profile/esp/blufi/include/esp_blufi.h index db2203b1e6..862f7bf02f 100644 --- a/components/bt/common/btc/profile/esp/blufi/include/esp_blufi.h +++ b/components/bt/common/btc/profile/esp/blufi/include/esp_blufi.h @@ -44,6 +44,7 @@ void esp_blufi_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *a /* Initialise gatt server */ int esp_blufi_gatt_svr_init(void); +int esp_blufi_gatt_svr_deinit(void); void esp_blufi_btc_init(void); void esp_blufi_btc_deinit(void); #endif diff --git a/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c b/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c index bd24eb8ba9..eac4242e4b 100644 --- a/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c +++ b/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c @@ -240,6 +240,32 @@ static void init_gatt_values(void) } +static void deinit_gatt_values(void) +{ + int i = 0; + const struct ble_gatt_svc_def *svc; + const struct ble_gatt_chr_def *chr; + const struct ble_gatt_dsc_def *dsc; + + for (svc = gatt_svr_svcs; svc && svc->uuid; svc++) { + for (chr = svc->characteristics; chr && chr->uuid; chr++) { + if (i < SERVER_MAX_VALUES && gatt_values[i].buf != NULL) { + os_mbuf_free(gatt_values[i].buf); /* Free the buffer */ + gatt_values[i].buf = NULL; /* Nullify the pointer to avoid dangling references */ + } + ++i; + + for (dsc = chr->descriptors; dsc && dsc->uuid; dsc++) { + if (i < SERVER_MAX_VALUES && gatt_values[i].buf != NULL) { + os_mbuf_free(gatt_values[i].buf); /* Free the buffer */ + gatt_values[i].buf = NULL; /* Nullify the pointer to avoid dangling references */ + } + ++i; + } + } + } +} + int esp_blufi_gatt_svr_init(void) { int rc; @@ -260,6 +286,18 @@ int esp_blufi_gatt_svr_init(void) return 0; } +int esp_blufi_gatt_svr_deinit(void) +{ + deinit_gatt_values(); + + ble_gatts_free_svcs(); + /* Deinitialize BLE GATT and GAP services */ + ble_svc_gatt_deinit(); + ble_svc_gap_deinit(); + + return 0; +} + static int esp_blufi_gap_event(struct ble_gap_event *event, void *arg) { diff --git a/components/bt/host/nimble/nimble b/components/bt/host/nimble/nimble index 24df26650d..e959fe7aa2 160000 --- a/components/bt/host/nimble/nimble +++ b/components/bt/host/nimble/nimble @@ -1 +1 @@ -Subproject commit 24df26650dcc42d5b1dd692c6ef586631feee77c +Subproject commit e959fe7aa220c922dc7bc84c2524f6d3bb978fe4 diff --git a/examples/bluetooth/blufi/main/blufi_init.c b/examples/bluetooth/blufi/main/blufi_init.c index 8361a4d168..cc7e30acfc 100644 --- a/examples/bluetooth/blufi/main/blufi_init.c +++ b/examples/bluetooth/blufi/main/blufi_init.c @@ -233,14 +233,17 @@ esp_err_t esp_blufi_host_deinit(void) { esp_err_t ret = ESP_OK; + esp_blufi_gatt_svr_deinit(); ret = nimble_port_stop(); - + if (ret != ESP_OK) { + return ret; + } if (ret == 0) { esp_nimble_deinit(); } ret = esp_blufi_profile_deinit(); - if(ret != ESP_OK) { + if (ret != ESP_OK) { return ret; }