18#ifdef CONFIG_BT_BLUEDROID_ENABLED
20#include "esp_bt_main.h"
21#include "esp_bt_device.h"
24#ifdef CONFIG_BT_NIMBLE_ENABLED
25#include "nimble/nimble_port.h"
26#include "nimble/nimble_port_freertos.h"
27#include "host/ble_hs.h"
28#include "host/util/util.h"
29#include "console/console.h"
32#include "esp_ble_mesh_defs.h"
34#define TAG "EXAMPLE_INIT"
36#ifdef CONFIG_BT_BLUEDROID_ENABLED
39 if (dev_uuid == NULL) {
40 ESP_LOGE(
TAG,
"%s, Invalid device uuid", __func__);
49 memcpy(dev_uuid + 2, esp_bt_dev_get_address(), BD_ADDR_LEN);
66 ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
68 esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
69 ret = esp_bt_controller_init(&bt_cfg);
71 ESP_LOGE(
TAG,
"%s initialize controller failed", __func__);
75 ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
77 ESP_LOGE(
TAG,
"%s enable controller failed", __func__);
81 ret = esp_bluedroid_init();
83 ESP_LOGE(
TAG,
"%s init bluetooth failed", __func__);
86 ret = esp_bluedroid_enable();
88 ESP_LOGE(
TAG,
"%s enable bluetooth failed", __func__);
96#ifdef CONFIG_BT_NIMBLE_ENABLED
97static SemaphoreHandle_t mesh_sem;
98static uint8_t own_addr_type;
99void ble_store_config_init(
void);
100static uint8_t addr_val[6] = {0};
112 if (dev_uuid == NULL) {
113 ESP_LOGE(
TAG,
"%s, Invalid device uuid", __func__);
122 memcpy(dev_uuid + 2, addr_val, BD_ADDR_LEN);
133static void mesh_on_reset(
int reason)
135 ESP_LOGI(
TAG,
"Resetting state; reason=%d", reason);
144static void mesh_on_sync(
void)
148 rc = ble_hs_util_ensure_addr(0);
152 rc = ble_hs_id_infer_auto(0, &own_addr_type);
154 ESP_LOGI(
TAG,
"error determining address type; rc=%d", rc);
158 rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
160 xSemaphoreGive(mesh_sem);
171void mesh_host_task(
void *param)
173 ESP_LOGI(
TAG,
"BLE Host Task Started");
177 nimble_port_freertos_deinit();
194 mesh_sem = xSemaphoreCreateBinary();
195 if (mesh_sem == NULL) {
196 ESP_LOGE(
TAG,
"Failed to create mesh semaphore");
200 ret = nimble_port_init();
202 ESP_LOGE(
TAG,
"Failed to init nimble %d ", ret);
207 ble_hs_cfg.reset_cb = mesh_on_reset;
208 ble_hs_cfg.sync_cb = mesh_on_sync;
209 ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
212 ble_store_config_init();
214 nimble_port_freertos_init(mesh_host_task);
216 xSemaphoreTake(mesh_sem, portMAX_DELAY);
esp_err_t bluetooth_init(void)
Initialize the Bluetooth stack for BLE Mesh.
void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
Retrieve the device UUID for BLE Mesh.