Skip to content

Commit

Permalink
Merge branch 'bugfix/add_return_type_nimble_port_init' into 'master'
Browse files Browse the repository at this point in the history
Nimble: Added return value (success / failure ) to nimble_port_init  / nimble_port_deinit

See merge request espressif/esp-idf!21718
  • Loading branch information
rahult-github committed Feb 1, 2023
2 parents 57c4508 + 826495a commit 9916eb6
Show file tree
Hide file tree
Showing 23 changed files with 144 additions and 49 deletions.
17 changes: 15 additions & 2 deletions components/bt/porting/nimble/include/nimble/nimble_port.h
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion components/protocomm/src/transports/protocomm_nimble.c
Expand Up @@ -494,7 +494,11 @@ static int simple_ble_start(const simple_ble_cfg_t *cfg)
int rc;
ESP_LOGD(TAG, "Free memory at start of simple_ble_init %" PRIu32, esp_get_free_heap_size());

nimble_port_init();
rc = nimble_port_init();
if (rc != ESP_OK) {
ESP_LOGE(TAG, "Failed to init nimble %d ", rc);
return rc;
}

/* Initialize the NimBLE host configuration. */
ble_hs_cfg.reset_cb = simple_ble_on_reset;
Expand Down
Expand Up @@ -132,13 +132,20 @@ void mesh_host_task(void *param)

esp_err_t bluetooth_init(void)
{
esp_err_t ret;

mesh_sem = xSemaphoreCreateBinary();
if (mesh_sem == NULL) {
ESP_LOGE(TAG, "Failed to create mesh semaphore");
return ESP_FAIL;
}

nimble_port_init();
ret = nimble_port_init();
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to init nimble %d ", ret);
return ret;
}

/* Initialize the NimBLE host configuration. */
ble_hs_cfg.reset_cb = mesh_on_reset;
ble_hs_cfg.sync_cb = mesh_on_sync;
Expand Down
Expand Up @@ -506,7 +506,9 @@ app_main(void)
}
ESP_ERROR_CHECK(ret);

nimble_port_init();
ret = nimble_port_init();
assert(ret == 0);

/* Configure the host. */
ble_hs_cfg.reset_cb = blecent_on_reset;
ble_hs_cfg.sync_cb = blecent_on_sync;
Expand Down
Expand Up @@ -435,7 +435,12 @@ app_main(void)
}
ESP_ERROR_CHECK(ret);

nimble_port_init();
ret = nimble_port_init();
if (ret != ESP_OK) {
ESP_LOGE(tag, "Failed to init nimble %d ", ret);
return;
}

/* Initialize the NimBLE host configuration. */
ble_hs_cfg.reset_cb = bleprph_on_reset;
ble_hs_cfg.sync_cb = bleprph_on_sync;
Expand Down
8 changes: 7 additions & 1 deletion examples/bluetooth/nimble/ble_periodic_adv/main/main.c
Expand Up @@ -188,7 +188,13 @@ app_main(void)
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
nimble_port_init();

ret = nimble_port_init();
if (ret != ESP_OK) {
ESP_LOGE(tag, "Failed to init nimble %d ", ret);
return;
}

/* Initialize the NimBLE host configuration. */
ble_hs_cfg.reset_cb = periodic_adv_on_reset;
ble_hs_cfg.sync_cb = periodic_adv_on_sync;
Expand Down
7 changes: 6 additions & 1 deletion examples/bluetooth/nimble/ble_periodic_sync/main/main.c
Expand Up @@ -184,7 +184,12 @@ app_main(void)
}
ESP_ERROR_CHECK(ret);

nimble_port_init();
ret = nimble_port_init();
if (ret != ESP_OK) {
MODLOG_DFLT(ERROR, "Failed to init nimble %d \n", ret);
return;
}

/* Configure the host. */
ble_hs_cfg.reset_cb = periodic_sync_on_reset;
ble_hs_cfg.sync_cb = periodic_sync_on_sync;
Expand Down
7 changes: 6 additions & 1 deletion examples/bluetooth/nimble/ble_phy/phy_cent/main/main.c
Expand Up @@ -477,7 +477,12 @@ app_main(void)
}
ESP_ERROR_CHECK(ret);

nimble_port_init();
ret = nimble_port_init();
if (ret != ESP_OK) {
MODLOG_DFLT(ERROR, "Failed to init nimble %d \n", ret);
return;
}

/* Configure the host. */
ble_hs_cfg.reset_cb = blecent_on_reset;
ble_hs_cfg.sync_cb = blecent_on_sync;
Expand Down
7 changes: 6 additions & 1 deletion examples/bluetooth/nimble/ble_phy/phy_prph/main/main.c
Expand Up @@ -396,7 +396,12 @@ app_main(void)

//ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());

nimble_port_init();
ret = nimble_port_init();
if (ret != ESP_OK) {
MODLOG_DFLT(ERROR, "Failed to init nimble %d \n", ret);
return;
}

/* Initialize the NimBLE host configuration. */
ble_hs_cfg.reset_cb = bleprph_on_reset;
ble_hs_cfg.sync_cb = bleprph_on_sync;
Expand Down
6 changes: 5 additions & 1 deletion examples/bluetooth/nimble/ble_spp/spp_client/main/main.c
Expand Up @@ -414,7 +414,11 @@ app_main(void)
}
ESP_ERROR_CHECK(ret);

nimble_port_init();
ret = nimble_port_init();
if (ret != ESP_OK) {
MODLOG_DFLT(ERROR, "Failed to init nimble %d \n", ret);
return;
}

/* Initialize UART driver and start uart task */
ble_spp_uart_init();
Expand Down
6 changes: 5 additions & 1 deletion examples/bluetooth/nimble/ble_spp/spp_server/main/main.c
Expand Up @@ -406,7 +406,11 @@ app_main(void)
}
ESP_ERROR_CHECK(ret);

nimble_port_init();
ret = nimble_port_init();
if (ret != ESP_OK) {
MODLOG_DFLT(ERROR, "Failed to init nimble %d \n", ret);
return;
}

/* Initialize connection_handle array */
for (int i = 0; i <= CONFIG_BT_NIMBLE_MAX_CONNECTIONS; i++) {
Expand Down
14 changes: 12 additions & 2 deletions examples/bluetooth/nimble/blecent/main/main.c
Expand Up @@ -615,7 +615,12 @@ static void stack_init_deinit(void)

ESP_LOGI(tag, "Init host");

nimble_port_init();
rc = nimble_port_init();
if (rc != ESP_OK) {
ESP_LOGI(tag, "Failed to init nimble %d ", rc);
break;
}

nimble_port_freertos_init(blecent_host_task);

ESP_LOGI(tag, "Waiting for 1 second");
Expand All @@ -635,7 +640,12 @@ app_main(void)
}
ESP_ERROR_CHECK(ret);

nimble_port_init();
ret = nimble_port_init();
if (ret != ESP_OK) {
ESP_LOGE(tag, "Failed to init nimble %d ", ret);
return;
}

/* Configure the host. */
ble_hs_cfg.reset_cb = blecent_on_reset;
ble_hs_cfg.sync_cb = blecent_on_sync;
Expand Down
7 changes: 6 additions & 1 deletion examples/bluetooth/nimble/blehr/main/main.c
Expand Up @@ -280,7 +280,12 @@ void app_main(void)
}
ESP_ERROR_CHECK(ret);

nimble_port_init();
ret = nimble_port_init();
if (ret != ESP_OK) {
MODLOG_DFLT(ERROR, "Failed to init nimble %d \n", ret);
return;
}

/* Initialize the NimBLE host configuration */
ble_hs_cfg.sync_cb = blehr_on_sync;
ble_hs_cfg.reset_cb = blehr_on_reset;
Expand Down
6 changes: 5 additions & 1 deletion examples/bluetooth/nimble/blemesh/main/app_mesh.c
Expand Up @@ -451,7 +451,11 @@ void app_main(void)
}
ESP_ERROR_CHECK(ret);

nimble_port_init();
ret = nimble_port_init();
if (ret != ESP_OK) {
ESP_LOGE(tag, "Failed to init nimble %d ", ret);
return;
}

ble_svc_gap_init();
ble_svc_gatt_init();
Expand Down
6 changes: 5 additions & 1 deletion examples/bluetooth/nimble/bleprph/main/main.c
Expand Up @@ -496,7 +496,11 @@ app_main(void)
}
ESP_ERROR_CHECK(ret);

nimble_port_init();
ret = nimble_port_init();
if (ret != ESP_OK) {
ESP_LOGE(tag, "Failed to init nimble %d ", ret);
return;
}
/* Initialize the NimBLE host configuration. */
ble_hs_cfg.reset_cb = bleprph_on_reset;
ble_hs_cfg.sync_cb = bleprph_on_sync;
Expand Down
6 changes: 5 additions & 1 deletion examples/bluetooth/nimble/bleprph_wifi_coex/main/main.c
Expand Up @@ -539,7 +539,11 @@ app_main(void)
wifi_init_sta();
do_ping_cmd();

nimble_port_init();
ret = nimble_port_init();
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to init nimble %d ", ret);
return;
}
/* Initialize the NimBLE host configuration. */
ble_hs_cfg.reset_cb = bleprph_on_reset;
ble_hs_cfg.sync_cb = bleprph_on_sync;
Expand Down
Expand Up @@ -723,7 +723,11 @@ app_main(void)
}
ESP_ERROR_CHECK(ret);

nimble_port_init();
ret = nimble_port_init();
if (ret != ESP_OK) {
ESP_LOGE(tag, "Failed to init nimble %d ", ret);
return;
}

/* Configure the host. */
ble_hs_cfg.reset_cb = blecent_on_reset;
Expand Down
Expand Up @@ -371,7 +371,12 @@ void app_main(void)
}
ESP_ERROR_CHECK(ret);

nimble_port_init();
ret = nimble_port_init();
if (ret != ESP_OK) {
ESP_LOGE(tag, "Failed to init nimble %d ", ret);
return;
}

/* Initialize the NimBLE host configuration */
ble_hs_cfg.sync_cb = gatts_on_sync;
ble_hs_cfg.reset_cb = gatts_on_reset;
Expand Down
Expand Up @@ -273,7 +273,7 @@ void app_main(void)
#endif // CONFIG_EXAMPLE_CONNECT_WIFI

#if CONFIG_BT_BLE_ENABLED || CONFIG_BT_NIMBLE_ENABLED
esp_ble_helper_init();
ESP_ERROR_CHECK(esp_ble_helper_init());
#endif

xTaskCreate(&advanced_ota_example_task, "advanced_ota_example_task", 1024 * 8, NULL, 5, NULL);
Expand Down
30 changes: 18 additions & 12 deletions examples/system/ota/advanced_https_ota/main/ble_helper/ble_api.c
Expand Up @@ -16,62 +16,66 @@
#include "bluedroid_gatts.h"
#endif

#if CONFIG_BT_BLE_ENABLED
static const char *TAG = "BLE_API";
#endif

void esp_ble_helper_init(void)
esp_err_t esp_ble_helper_init(void)
{
esp_err_t err;
esp_err_t err = ESP_OK;
#if CONFIG_BT_BLE_ENABLED
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));

esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
err = esp_bt_controller_init(&bt_cfg);
if (err) {
ESP_LOGE(TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(err));
return;
return err;
}

err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (err) {
ESP_LOGE(TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(err));
return;
return err;
}
err = esp_bluedroid_init();
if (err) {
ESP_LOGE(TAG, "%s init bluetooth failed: %s\n", __func__, esp_err_to_name(err));
return;
return err;
}
err = esp_bluedroid_enable();
if (err) {
ESP_LOGE(TAG, "%s enable bluetooth failed: %s\n", __func__, esp_err_to_name(err));
return;
return err;
}

err = esp_ble_gatts_register_callback(gatts_event_handler);
if (err) {
ESP_LOGE(TAG, "gatts register error, error code = %x", err);
return;
return err;
}
err = esp_ble_gap_register_callback(gap_event_handler);
if (err) {
ESP_LOGE(TAG, "gap register error, error code = %x", err);
return;
return err;
}
err = esp_ble_gatts_app_register(PROFILE_A_APP_ID);
if (err) {
ESP_LOGE(TAG, "gatts app register error, error code = %x", err);
return;
return err;
}
esp_err_t local_mtu_err = esp_ble_gatt_set_local_mtu(500);
if (local_mtu_err) {
ESP_LOGE(TAG, "set local MTU failed, error code = %x", local_mtu_err);
return local_mtu_err;
}

#elif CONFIG_BT_NIMBLE_ENABLED

nimble_port_init();
err = nimble_port_init();
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to init nimble %d ", err);
return err;
}

/* Initialize the NimBLE host configuration. */
ble_hs_cfg.reset_cb = bleprph_on_reset;
ble_hs_cfg.sync_cb = bleprph_on_sync;
Expand All @@ -90,6 +94,8 @@ void esp_ble_helper_init(void)
nimble_port_freertos_init(bleprph_host_task);

#endif

return ESP_OK;
}

#endif

0 comments on commit 9916eb6

Please sign in to comment.