1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
/*
* Copyright (c) 2024, sakumisu
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "usbh_core.h"
#include "usbh_bluetooth.h"
#include <nimble/ble.h>
#include "nimble/hci_common.h"
#include "nimble/transport.h"
#include "nimble/transport/hci_h4.h"
struct hci_h4_sm g_hci_h4sm;
void ble_transport_ll_init(void)
{
/* nothing here */
}
static int hci_usb_frame_cb(uint8_t pkt_type, void *data)
{
switch (pkt_type) {
case HCI_H4_EVT:
return ble_transport_to_hs_evt(data);
case HCI_H4_ACL:
return ble_transport_to_hs_acl(data);
default:
assert(0);
break;
}
return -1;
}
void usbh_bluetooth_hci_read_callback(uint8_t *data, uint32_t len)
{
hci_h4_sm_rx(&g_hci_h4sm, data, (uint16_t)len);
}
int ble_transport_to_ll_cmd_impl(void *buf)
{
const struct ble_hci_cmd *cmd = buf;
size_t pkt_len = sizeof(*cmd) + cmd->length;
int ret;
ret = usbh_bluetooth_hci_write(USB_BLUETOOTH_HCI_CMD, buf, pkt_len);
if (ret < 0) {
ret = BLE_ERR_MEM_CAPACITY;
} else {
ret = 0;
}
ble_transport_free(buf);
return ret;
}
int ble_transport_to_ll_acl_impl(struct os_mbuf *om)
{
uint8_t buf[BLE_HCI_DATA_HDR_SZ + MYNEWT_VAL(BLE_TRANSPORT_ACL_SIZE)];
uint16_t pkt_len = OS_MBUF_PKTLEN(om);
int ret;
if (pkt_len > sizeof(buf)) {
os_mbuf_free_chain(om);
return BLE_ERR_MEM_CAPACITY;
}
os_mbuf_copydata(om, 0, pkt_len, buf);
ret = usbh_bluetooth_hci_write(USB_BLUETOOTH_HCI_ACL, buf, pkt_len);
os_mbuf_free_chain(om);
return ret < 0 ? BLE_ERR_MEM_CAPACITY : 0;
}
__WEAK void usbh_bluetooth_run_callback(void)
{
/* bt_enable() */
}
__WEAK void usbh_bluetooth_stop_callback(void)
{
/* bt_disable() */
}
void usbh_bluetooth_run(struct usbh_bluetooth *bluetooth_class)
{
(void)bluetooth_class;
hci_h4_sm_init(&g_hci_h4sm, &hci_h4_allocs_from_ll, hci_usb_frame_cb);
#ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
usb_osal_thread_create("ble_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_rx_thread, NULL);
#else
usb_osal_thread_create("ble_evt", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_evt_rx_thread, NULL);
usb_osal_thread_create("ble_acl", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_acl_rx_thread, NULL);
#endif
usbh_bluetooth_run_callback();
}
void usbh_bluetooth_stop(struct usbh_bluetooth *bluetooth_class)
{
(void)bluetooth_class;
usbh_bluetooth_stop_callback();
}
|