diff --git a/porting/examples/dummy/main.c b/porting/examples/dummy/main.c index 4c17d9502..5cd4bb08d 100644 --- a/porting/examples/dummy/main.c +++ b/porting/examples/dummy/main.c @@ -21,7 +21,7 @@ int main(int argc, char **argv) { - nimble_port_init(); + int ret = nimble_port_init(); - return 0; + return ret; } diff --git a/porting/examples/linux/main.c b/porting/examples/linux/main.c index 25323b9a0..c39b927c5 100644 --- a/porting/examples/linux/main.c +++ b/porting/examples/linux/main.c @@ -66,8 +66,8 @@ int main(int argc, char *argv[]) ble_hci_sock_set_device(atoi(argv[1])); } - nimble_port_init(); ble_hci_sock_init(); + ret = nimble_port_init(); /* This example provides GATT Alert service */ ble_svc_gap_init(); diff --git a/porting/examples/linux_blemesh/main.c b/porting/examples/linux_blemesh/main.c index 5a9befdc1..064a12f9d 100644 --- a/porting/examples/linux_blemesh/main.c +++ b/porting/examples/linux_blemesh/main.c @@ -79,8 +79,8 @@ int main(int argc, char *argv[]) ble_hci_sock_set_device(atoi(argv[1])); } - nimble_port_init(); ble_hci_sock_init(); + ret = nimble_port_init(); ble_svc_gap_init(); ble_svc_gatt_init(); diff --git a/porting/examples/nuttx/main.c b/porting/examples/nuttx/main.c index 7e289be77..d2a2344e4 100644 --- a/porting/examples/nuttx/main.c +++ b/porting/examples/nuttx/main.c @@ -87,7 +87,12 @@ int main(int argc, char *argv[]) #endif printf("port init\n"); - nimble_port_init(); + + ret = nimble_port_init(); + if (ret != 0) { + printf(" Failed to init nimble %d \n", ret); + return ret; + } printf("hci init\n"); ble_hci_sock_init(); diff --git a/porting/nimble/include/nimble/nimble_port.h b/porting/nimble/include/nimble/nimble_port.h index 2ea3cd864..329281a03 100644 --- a/porting/nimble/include/nimble/nimble_port.h +++ b/porting/nimble/include/nimble/nimble_port.h @@ -35,8 +35,21 @@ extern "C" { #endif -void nimble_port_init(void); -void nimble_port_deinit(void); +/** +* @brief nimble_port_init - Initialize controller and NimBLE host stack +* +* @return esp_err_t - ESP_OK ( if success) +* Error code in case of failure +*/ +esp_err_t nimble_port_init(void); + +/** +* @brief nimble_port_deinit - Deinitialize controller and NimBLE host stack +* +* @return esp_err_t - ESP_OK ( if success) +* Error code in case of failure +*/ +esp_err_t nimble_port_deinit(void); void nimble_port_run(void); int nimble_port_stop(void); diff --git a/porting/nimble/src/nimble_port.c b/porting/nimble/src/nimble_port.c index e0ff201cb..402a77782 100644 --- a/porting/nimble/src/nimble_port.c +++ b/porting/nimble/src/nimble_port.c @@ -136,47 +136,76 @@ esp_err_t esp_nimble_deinit(void) return ESP_OK; } -void +/** + * @brief nimble_port_init - Initialize controller and NimBLE host stack + * + * @return esp_err_t + */ +esp_err_t nimble_port_init(void) { + esp_err_t ret; + #if CONFIG_IDF_TARGET_ESP32 esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); #endif #if CONFIG_BT_CONTROLLER_ENABLED esp_bt_controller_config_t config_opts = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); - if(esp_bt_controller_init(&config_opts) != ESP_OK) { + + ret = esp_bt_controller_init(&config_opts); + if (ret != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "controller init failed\n"); - return; + return ret; } - if(esp_bt_controller_enable(ESP_BT_MODE_BLE) != ESP_OK) { + + ret = esp_bt_controller_enable(ESP_BT_MODE_BLE); + if (ret != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "controller enable failed\n"); - return; + return ret; } #endif - if(esp_nimble_init() != 0) { + ret = esp_nimble_init(); + if (ret != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "nimble host init failed\n"); - return; + return ret; } + + return ESP_OK; } -void +/** + * @brief nimble_port_deinit - Deinitialize controller and NimBLE host stack + * + * @return esp_err_t + */ + +esp_err_t nimble_port_deinit(void) { - if(esp_nimble_deinit() != 0) { + esp_err_t ret; + + ret = esp_nimble_deinit(); + if(ret != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "nimble host deinit failed\n"); - return; + return ret; } + #if CONFIG_BT_CONTROLLER_ENABLED - if(esp_bt_controller_disable() != ESP_OK) { + ret = esp_bt_controller_disable(); + if(ret != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "controller disable failed\n"); - return; + return ret; } - if(esp_bt_controller_deinit() != ESP_OK) { + + ret = esp_bt_controller_deinit(); + if(ret != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "controller deinit failed\n"); - return; + return ret; } #endif + + return ESP_OK; }