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/hci_driver.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 HCI_CMD                 0x01
21 #define HCI_ACL                 0x02
22 #define HCI_SCO                 0x03
23 #define HCI_EVT                 0x04
24 #define HCI_ISO                 0x05
25 
26 #define HCI_BT_ESP32_TIMEOUT K_MSEC(2000)
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_BREDR)
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 	uint8_t pkt_indicator;
190 	struct net_buf *buf = NULL;
191 	size_t remaining = len;
192 
193 	LOG_HEXDUMP_DBG(data, len, "host packet data:");
194 
195 	pkt_indicator = *data++;
196 	remaining -= sizeof(pkt_indicator);
197 
198 	switch (pkt_indicator) {
199 	case HCI_EVT:
200 		buf = bt_esp_evt_recv(data, remaining);
201 		break;
202 
203 	case HCI_ACL:
204 		buf = bt_esp_acl_recv(data, remaining);
205 		break;
206 
207 	case HCI_SCO:
208 		buf = bt_esp_iso_recv(data, remaining);
209 		break;
210 
211 	default:
212 		LOG_ERR("Unknown HCI type %u", pkt_indicator);
213 		return -1;
214 	}
215 
216 	if (buf) {
217 		LOG_DBG("Calling bt_recv(%p)", buf);
218 
219 		bt_recv(buf);
220 	}
221 
222 	return 0;
223 }
224 
hci_esp_controller_rcv_pkt_ready(void)225 static void hci_esp_controller_rcv_pkt_ready(void)
226 {
227 	k_sem_give(&hci_send_sem);
228 }
229 
230 static esp_vhci_host_callback_t vhci_host_cb = {
231 	hci_esp_controller_rcv_pkt_ready,
232 	hci_esp_host_rcv_pkt
233 };
234 
bt_esp32_send(struct net_buf * buf)235 static int bt_esp32_send(struct net_buf *buf)
236 {
237 	int err = 0;
238 	uint8_t pkt_indicator;
239 
240 	LOG_DBG("buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len);
241 
242 	switch (bt_buf_get_type(buf)) {
243 	case BT_BUF_ACL_OUT:
244 		pkt_indicator = HCI_ACL;
245 		break;
246 	case BT_BUF_CMD:
247 		pkt_indicator = HCI_CMD;
248 		break;
249 	case BT_BUF_ISO_OUT:
250 		pkt_indicator = HCI_ISO;
251 		break;
252 	default:
253 		LOG_ERR("Unknown type %u", bt_buf_get_type(buf));
254 		goto done;
255 	}
256 	net_buf_push_u8(buf, pkt_indicator);
257 
258 	LOG_HEXDUMP_DBG(buf->data, buf->len, "Final HCI buffer:");
259 
260 	if (!esp_vhci_host_check_send_available()) {
261 		LOG_WRN("Controller not ready to receive packets");
262 	}
263 
264 	if (k_sem_take(&hci_send_sem, HCI_BT_ESP32_TIMEOUT) == 0) {
265 		esp_vhci_host_send_packet(buf->data, buf->len);
266 	} else {
267 		LOG_ERR("Send packet timeout error");
268 		err = -ETIMEDOUT;
269 	}
270 
271 done:
272 	net_buf_unref(buf);
273 	k_sem_give(&hci_send_sem);
274 
275 	return err;
276 }
277 
bt_esp32_ble_init(void)278 static int bt_esp32_ble_init(void)
279 {
280 	int ret;
281 	esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
282 
283 #if defined(CONFIG_BT_BREDR) && defined(CONFIG_SOC_SERIES_ESP32)
284 	esp_bt_mode_t mode = ESP_BT_MODE_BTDM;
285 #else
286 	esp_bt_mode_t mode = ESP_BT_MODE_BLE;
287 #endif
288 
289 	ret = esp_bt_controller_init(&bt_cfg);
290 	if (ret) {
291 		LOG_ERR("Bluetooth controller init failed %d", ret);
292 		return ret;
293 	}
294 
295 	ret = esp_bt_controller_enable(mode);
296 	if (ret) {
297 		LOG_ERR("Bluetooth controller enable failed: %d", ret);
298 		return ret;
299 	}
300 
301 	esp_vhci_host_register_callback(&vhci_host_cb);
302 
303 	return 0;
304 }
305 
bt_esp32_ble_deinit(void)306 static int bt_esp32_ble_deinit(void)
307 {
308 	int ret;
309 
310 	ret = esp_bt_controller_disable();
311 	if (ret) {
312 		LOG_ERR("Bluetooth controller disable failed %d", ret);
313 		return ret;
314 	}
315 
316 	ret = esp_bt_controller_deinit();
317 	if (ret) {
318 		LOG_ERR("Bluetooth controller deinit failed %d", ret);
319 		return ret;
320 	}
321 
322 	return 0;
323 }
324 
bt_esp32_open(void)325 static int bt_esp32_open(void)
326 {
327 	int err;
328 
329 	err = bt_esp32_ble_init();
330 	if (err) {
331 		return err;
332 	}
333 
334 	LOG_DBG("ESP32 BT started");
335 
336 	return 0;
337 }
338 
bt_esp32_close(void)339 static int bt_esp32_close(void)
340 {
341 	int err;
342 
343 	err = bt_esp32_ble_deinit();
344 	if (err) {
345 		return err;
346 	}
347 
348 	LOG_DBG("ESP32 BT stopped");
349 
350 	return 0;
351 }
352 
353 static const struct bt_hci_driver drv = {
354 	.name           = "BT ESP32",
355 	.open           = bt_esp32_open,
356 	.send           = bt_esp32_send,
357 	.close          = bt_esp32_close,
358 	.bus            = BT_HCI_DRIVER_BUS_IPM,
359 #if defined(CONFIG_BT_DRIVER_QUIRK_NO_AUTO_DLE)
360 	.quirks         = BT_QUIRK_NO_AUTO_DLE,
361 #endif
362 };
363 
bt_esp32_init(void)364 static int bt_esp32_init(void)
365 {
366 	bt_hci_driver_register(&drv);
367 
368 	return 0;
369 }
370 
371 SYS_INIT(bt_esp32_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
372