MeshX 0.3
This repository provides an implementation for Bluetooth Low Energy (BLE) Mesh network nodes. The project allows you to create BLE mesh nodes that can communicate with each other, enabling the development of smart home solutions or other IoT-based applications.
Loading...
Searching...
No Matches
esp_platform_bt.c
Go to the documentation of this file.
1
13
14#include <stdio.h>
15#include <string.h>
16#include <sdkconfig.h>
17
18#ifdef CONFIG_BT_BLUEDROID_ENABLED
19#include "esp_bt.h"
20#include "esp_bt_main.h"
21#include "esp_bt_device.h"
22#endif
23
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"
30#endif
31
32#include "esp_ble_mesh_defs.h"
33
34#define TAG "EXAMPLE_INIT"
35
36#ifdef CONFIG_BT_BLUEDROID_ENABLED
37void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
38{
39 if (dev_uuid == NULL) {
40 ESP_LOGE(TAG, "%s, Invalid device uuid", __func__);
41 return;
42 }
43
44 /* Copy device address to the device uuid with offset equals to 2 here.
45 * The first two bytes is used for matching device uuid by Provisioner.
46 * And using device address here is to avoid using the same device uuid
47 * by different unprovisioned devices.
48 */
49 memcpy(dev_uuid + 2, esp_bt_dev_get_address(), BD_ADDR_LEN);
50}
51
62esp_err_t bluetooth_init(void)
63{
64 esp_err_t ret;
65
66 ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
67
68 esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
69 ret = esp_bt_controller_init(&bt_cfg);
70 if (ret) {
71 ESP_LOGE(TAG, "%s initialize controller failed", __func__);
72 return ret;
73 }
74
75 ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
76 if (ret) {
77 ESP_LOGE(TAG, "%s enable controller failed", __func__);
78 return ret;
79 }
80
81 ret = esp_bluedroid_init();
82 if (ret) {
83 ESP_LOGE(TAG, "%s init bluetooth failed", __func__);
84 return ret;
85 }
86 ret = esp_bluedroid_enable();
87 if (ret) {
88 ESP_LOGE(TAG, "%s enable bluetooth failed", __func__);
89 return ret;
90 }
91
92 return ret;
93}
94#endif /* CONFIG_BT_BLUEDROID_ENABLED */
95
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};
101
110void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
111{
112 if (dev_uuid == NULL) {
113 ESP_LOGE(TAG, "%s, Invalid device uuid", __func__);
114 return;
115 }
116
117 /* Copy device address to the device uuid with offset equals to 2 here.
118 * The first two bytes is used for matching device uuid by Provisioner.
119 * And using device address here is to avoid using the same device uuid
120 * by different unprovisioned devices.
121 */
122 memcpy(dev_uuid + 2, addr_val, BD_ADDR_LEN);
123}
124
133static void mesh_on_reset(int reason)
134{
135 ESP_LOGI(TAG, "Resetting state; reason=%d", reason);
136}
137
144static void mesh_on_sync(void)
145{
146 int rc;
147
148 rc = ble_hs_util_ensure_addr(0);
149 assert(rc == 0);
150
151 /* Figure out address to use while advertising (no privacy for now) */
152 rc = ble_hs_id_infer_auto(0, &own_addr_type);
153 if (rc != 0) {
154 ESP_LOGI(TAG, "error determining address type; rc=%d", rc);
155 return;
156 }
157
158 rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
159
160 xSemaphoreGive(mesh_sem);
161}
162
171void mesh_host_task(void *param)
172{
173 ESP_LOGI(TAG, "BLE Host Task Started");
174 /* This function will return only when nimble_port_stop() is executed */
175 nimble_port_run();
176
177 nimble_port_freertos_deinit();
178}
179
190esp_err_t bluetooth_init(void)
191{
192 esp_err_t ret;
193
194 mesh_sem = xSemaphoreCreateBinary();
195 if (mesh_sem == NULL) {
196 ESP_LOGE(TAG, "Failed to create mesh semaphore");
197 return MESHX_FAIL;
198 }
199
200 ret = nimble_port_init();
201 if (ret != MESHX_SUCCESS) {
202 ESP_LOGE(TAG, "Failed to init nimble %d ", ret);
203 return ret;
204 }
205
206 /* Initialize the NimBLE host configuration. */
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;
210
211 /* XXX Need to have template for store */
212 ble_store_config_init();
213
214 nimble_port_freertos_init(mesh_host_task);
215
216 xSemaphoreTake(mesh_sem, portMAX_DELAY);
217
218 return MESHX_SUCCESS;
219}
220#endif /* CONFIG_BT_NIMBLE_ENABLED */
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.
#define TAG
@ MESHX_SUCCESS
Definition meshx_err.h:40
@ MESHX_FAIL
Definition meshx_err.h:41