1 /*
2 * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/bluetooth/hci.h>
8
9 #include <zephyr/init.h>
10 #include <zephyr/sys/byteorder.h>
11
12 #include <zephyr/drivers/bluetooth.h>
13
14 #include <esp_bt.h>
15
16 #define LOG_LEVEL CONFIG_BT_HCI_DRIVER_LOG_LEVEL
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_REGISTER(bt_hci_driver_esp32);
19
20 #define DT_DRV_COMPAT espressif_esp32_bt_hci
21
22 #define HCI_BT_ESP32_TIMEOUT K_MSEC(2000)
23
24 struct bt_esp32_data {
25 bt_hci_recv_t recv;
26 };
27
28 static K_SEM_DEFINE(hci_send_sem, 1, 1);
29
is_hci_event_discardable(const uint8_t * evt_data)30 static bool is_hci_event_discardable(const uint8_t *evt_data)
31 {
32 uint8_t evt_type = evt_data[0];
33
34 switch (evt_type) {
35 #if defined(CONFIG_BT_CLASSIC)
36 case BT_HCI_EVT_INQUIRY_RESULT_WITH_RSSI:
37 case BT_HCI_EVT_EXTENDED_INQUIRY_RESULT:
38 return true;
39 #endif
40 case BT_HCI_EVT_LE_META_EVENT: {
41 uint8_t subevt_type = evt_data[sizeof(struct bt_hci_evt_hdr)];
42
43 switch (subevt_type) {
44 case BT_HCI_EVT_LE_ADVERTISING_REPORT:
45 return true;
46 default:
47 return false;
48 }
49 }
50 default:
51 return false;
52 }
53 }
54
bt_esp_evt_recv(uint8_t * data,size_t remaining)55 static struct net_buf *bt_esp_evt_recv(uint8_t *data, size_t remaining)
56 {
57 bool discardable = false;
58 struct bt_hci_evt_hdr hdr;
59 struct net_buf *buf;
60 size_t buf_tailroom;
61
62 if (remaining < sizeof(hdr)) {
63 LOG_ERR("Not enough data for event header");
64 return NULL;
65 }
66
67 discardable = is_hci_event_discardable(data);
68
69 memcpy((void *)&hdr, data, sizeof(hdr));
70 data += sizeof(hdr);
71 remaining -= sizeof(hdr);
72
73 if (remaining != hdr.len) {
74 LOG_ERR("Event payload length is not correct");
75 return NULL;
76 }
77 LOG_DBG("len %u", hdr.len);
78
79 buf = bt_buf_get_evt(hdr.evt, discardable, K_NO_WAIT);
80 if (!buf) {
81 if (discardable) {
82 LOG_DBG("Discardable buffer pool full, ignoring event");
83 } else {
84 LOG_ERR("No available event buffers!");
85 }
86 return buf;
87 }
88
89 net_buf_add_mem(buf, &hdr, sizeof(hdr));
90
91 buf_tailroom = net_buf_tailroom(buf);
92 if (buf_tailroom < remaining) {
93 LOG_ERR("Not enough space in buffer %zu/%zu", remaining, buf_tailroom);
94 net_buf_unref(buf);
95 return NULL;
96 }
97
98 net_buf_add_mem(buf, data, remaining);
99
100 return buf;
101 }
102
bt_esp_acl_recv(uint8_t * data,size_t remaining)103 static struct net_buf *bt_esp_acl_recv(uint8_t *data, size_t remaining)
104 {
105 struct bt_hci_acl_hdr hdr;
106 struct net_buf *buf;
107 size_t buf_tailroom;
108
109 if (remaining < sizeof(hdr)) {
110 LOG_ERR("Not enough data for ACL header");
111 return NULL;
112 }
113
114 buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_NO_WAIT);
115 if (buf) {
116 memcpy((void *)&hdr, data, sizeof(hdr));
117 data += sizeof(hdr);
118 remaining -= sizeof(hdr);
119
120 net_buf_add_mem(buf, &hdr, sizeof(hdr));
121 } else {
122 LOG_ERR("No available ACL buffers!");
123 return NULL;
124 }
125
126 if (remaining != sys_le16_to_cpu(hdr.len)) {
127 LOG_ERR("ACL payload length is not correct");
128 net_buf_unref(buf);
129 return NULL;
130 }
131
132 buf_tailroom = net_buf_tailroom(buf);
133 if (buf_tailroom < remaining) {
134 LOG_ERR("Not enough space in buffer %zu/%zu", remaining, buf_tailroom);
135 net_buf_unref(buf);
136 return NULL;
137 }
138
139 LOG_DBG("len %u", remaining);
140 net_buf_add_mem(buf, data, remaining);
141
142 return buf;
143 }
144
bt_esp_iso_recv(uint8_t * data,size_t remaining)145 static struct net_buf *bt_esp_iso_recv(uint8_t *data, size_t remaining)
146 {
147 struct bt_hci_iso_hdr hdr;
148 struct net_buf *buf;
149 size_t buf_tailroom;
150
151 if (remaining < sizeof(hdr)) {
152 LOG_ERR("Not enough data for ISO header");
153 return NULL;
154 }
155
156 buf = bt_buf_get_rx(BT_BUF_ISO_IN, K_NO_WAIT);
157 if (buf) {
158 memcpy((void *)&hdr, data, sizeof(hdr));
159 data += sizeof(hdr);
160 remaining -= sizeof(hdr);
161
162 net_buf_add_mem(buf, &hdr, sizeof(hdr));
163 } else {
164 LOG_ERR("No available ISO buffers!");
165 return NULL;
166 }
167
168 if (remaining != bt_iso_hdr_len(sys_le16_to_cpu(hdr.len))) {
169 LOG_ERR("ISO payload length is not correct");
170 net_buf_unref(buf);
171 return NULL;
172 }
173
174 buf_tailroom = net_buf_tailroom(buf);
175 if (buf_tailroom < remaining) {
176 LOG_ERR("Not enough space in buffer %zu/%zu", remaining, buf_tailroom);
177 net_buf_unref(buf);
178 return NULL;
179 }
180
181 LOG_DBG("len %zu", remaining);
182 net_buf_add_mem(buf, data, remaining);
183
184 return buf;
185 }
186
hci_esp_host_rcv_pkt(uint8_t * data,uint16_t len)187 static int hci_esp_host_rcv_pkt(uint8_t *data, uint16_t len)
188 {
189 const struct device *dev = DEVICE_DT_GET(DT_DRV_INST(0));
190 struct bt_esp32_data *hci = dev->data;
191 uint8_t pkt_indicator;
192 struct net_buf *buf = NULL;
193 size_t remaining = len;
194
195 LOG_HEXDUMP_DBG(data, len, "host packet data:");
196
197 pkt_indicator = *data++;
198 remaining -= sizeof(pkt_indicator);
199
200 switch (pkt_indicator) {
201 case BT_HCI_H4_EVT:
202 buf = bt_esp_evt_recv(data, remaining);
203 break;
204
205 case BT_HCI_H4_ACL:
206 buf = bt_esp_acl_recv(data, remaining);
207 break;
208
209 case BT_HCI_H4_SCO:
210 buf = bt_esp_iso_recv(data, remaining);
211 break;
212
213 default:
214 LOG_ERR("Unknown HCI type %u", pkt_indicator);
215 return -1;
216 }
217
218 if (buf) {
219 LOG_DBG("Calling bt_recv(%p)", buf);
220
221 hci->recv(dev, buf);
222 }
223
224 return 0;
225 }
226
hci_esp_controller_rcv_pkt_ready(void)227 static void hci_esp_controller_rcv_pkt_ready(void)
228 {
229 k_sem_give(&hci_send_sem);
230 }
231
232 static esp_vhci_host_callback_t vhci_host_cb = {
233 hci_esp_controller_rcv_pkt_ready,
234 hci_esp_host_rcv_pkt
235 };
236
bt_esp32_send(const struct device * dev,struct net_buf * buf)237 static int bt_esp32_send(const struct device *dev, struct net_buf *buf)
238 {
239 int err = 0;
240 uint8_t pkt_indicator;
241
242 LOG_DBG("buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len);
243
244 switch (bt_buf_get_type(buf)) {
245 case BT_BUF_ACL_OUT:
246 pkt_indicator = BT_HCI_H4_ACL;
247 break;
248 case BT_BUF_CMD:
249 pkt_indicator = BT_HCI_H4_CMD;
250 break;
251 case BT_BUF_ISO_OUT:
252 pkt_indicator = BT_HCI_H4_ISO;
253 break;
254 default:
255 LOG_ERR("Unknown type %u", bt_buf_get_type(buf));
256 goto done;
257 }
258 net_buf_push_u8(buf, pkt_indicator);
259
260 LOG_HEXDUMP_DBG(buf->data, buf->len, "Final HCI buffer:");
261
262 if (!esp_vhci_host_check_send_available()) {
263 LOG_WRN("Controller not ready to receive packets");
264 }
265
266 if (k_sem_take(&hci_send_sem, HCI_BT_ESP32_TIMEOUT) == 0) {
267 esp_vhci_host_send_packet(buf->data, buf->len);
268 } else {
269 LOG_ERR("Send packet timeout error");
270 err = -ETIMEDOUT;
271 }
272
273 done:
274 net_buf_unref(buf);
275 k_sem_give(&hci_send_sem);
276
277 return err;
278 }
279
bt_esp32_ble_init(void)280 static int bt_esp32_ble_init(void)
281 {
282 int ret;
283 esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
284
285 #if defined(CONFIG_BT_CLASSIC) && defined(CONFIG_SOC_SERIES_ESP32)
286 esp_bt_mode_t mode = ESP_BT_MODE_BTDM;
287 #else
288 esp_bt_mode_t mode = ESP_BT_MODE_BLE;
289 #endif
290
291 ret = esp_bt_controller_init(&bt_cfg);
292 if (ret) {
293 LOG_ERR("Bluetooth controller init failed %d", ret);
294 return ret;
295 }
296
297 ret = esp_bt_controller_enable(mode);
298 if (ret) {
299 LOG_ERR("Bluetooth controller enable failed: %d", ret);
300 return ret;
301 }
302
303 esp_vhci_host_register_callback(&vhci_host_cb);
304
305 return 0;
306 }
307
bt_esp32_ble_deinit(void)308 static int bt_esp32_ble_deinit(void)
309 {
310 int ret;
311
312 ret = esp_bt_controller_disable();
313 if (ret) {
314 LOG_ERR("Bluetooth controller disable failed %d", ret);
315 return ret;
316 }
317
318 ret = esp_bt_controller_deinit();
319 if (ret) {
320 LOG_ERR("Bluetooth controller deinit failed %d", ret);
321 return ret;
322 }
323
324 return 0;
325 }
326
bt_esp32_open(const struct device * dev,bt_hci_recv_t recv)327 static int bt_esp32_open(const struct device *dev, bt_hci_recv_t recv)
328 {
329 struct bt_esp32_data *hci = dev->data;
330 int err;
331
332 err = bt_esp32_ble_init();
333 if (err) {
334 return err;
335 }
336
337 hci->recv = recv;
338
339 LOG_DBG("ESP32 BT started");
340
341 return 0;
342 }
343
bt_esp32_close(const struct device * dev)344 static int bt_esp32_close(const struct device *dev)
345 {
346 struct bt_esp32_data *hci = dev->data;
347 int err;
348
349 err = bt_esp32_ble_deinit();
350 if (err) {
351 return err;
352 }
353
354 hci->recv = NULL;
355
356 LOG_DBG("ESP32 BT stopped");
357
358 return 0;
359 }
360
361 static const struct bt_hci_driver_api drv = {
362 .open = bt_esp32_open,
363 .send = bt_esp32_send,
364 .close = bt_esp32_close,
365 };
366
367 #define BT_ESP32_DEVICE_INIT(inst) \
368 static struct bt_esp32_data bt_esp32_data_##inst = { \
369 }; \
370 DEVICE_DT_INST_DEFINE(inst, NULL, NULL, &bt_esp32_data_##inst, NULL, \
371 POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &drv)
372
373 /* Only one instance supported */
374 BT_ESP32_DEVICE_INIT(0)
375