support hci usb h4 for usb bluetooth

This commit is contained in:
sakumisu
2024-01-25 22:03:35 +08:00
parent 24511c4d4b
commit cdfb7c3fb2
8 changed files with 431 additions and 157 deletions

View File

@@ -1,5 +1,9 @@
## Note
当前支持 nimble 主线 1.6.0 版本,**禁止使用其余软件包或者第三方包**
- ble_hs_startup_set_evmask_tx 函数中 BLE_HCI_OCF_CB_SET_EVENT_MASK2 功能需要关闭
```
@@ -19,14 +23,147 @@
```
- 如果使用 rt-thread 中的软件包,请删除软件包中 SConscript 文件以下内容
- 主线中头文件和源文件参与编译例子
```
# if rtconfig.CROSS_TOOL == 'keil':
#LOCAL_CCFLAGS += ' --gnu --diag_suppress=111'
# __BYTE_ORDER__ & __ORDER_BIG_ENDIAN__ & __ORDER_LITTLE_ENDIAN__ is not defined in keil, the specific values comes from gcc.
# CPPDEFINES.append('__ORDER_LITTLE_ENDIAN__=1234')
# CPPDEFINES.append('__ORDER_BIG_ENDIAN__=4321')
# CPPDEFINES.append('__BYTE_ORDER__=1234')
```
源文件
nimble/host/src/ble_att_clt.c
nimble/host/src/ble_att_cmd.c
nimble/host/src/ble_att_svr.c
nimble/host/src/ble_att.c
nimble/host/src/ble_audio_broadcast.c
nimble/host/src/ble_dtm.c
nimble/host/src/ble_eatt.c
nimble/host/src/ble_eddystone.c
nimble/host/src/ble_gap.c
nimble/host/src/ble_gattc.c
nimble/host/src/ble_gatts.c
nimble/host/src/ble_gatts_lcl.c
nimble/host/src/ble_hs.c
nimble/host/src/ble_hs_adv.c
nimble/host/src/ble_hs_atomic.c
nimble/host/src/ble_hs_cfg.c
nimble/host/src/ble_hs_conn.c
nimble/host/src/ble_hs_flow.c
nimble/host/src/ble_hs_hci.c
nimble/host/src/ble_hs_hci_cmd.c
nimble/host/src/ble_hs_hci_evt.c
nimble/host/src/ble_hs_hci_util.c
nimble/host/src/ble_hs_id.c
nimble/host/src/ble_hs_log.c
nimble/host/src/ble_hs_mbuf.c
nimble/host/src/ble_hs_misc.c
nimble/host/src/ble_hs_mqueue.c
nimble/host/src/ble_hs_periodic_sync.c
nimble/host/src/ble_hs_pvcy.c
nimble/host/src/ble_hs_shutdown.c
nimble/host/src/ble_hs_startup.c
nimble/host/src/ble_hs_stop.c
nimble/host/src/ble_ibeacon.c
nimble/host/src/ble_iso.c
nimble/host/src/ble_l2cap.c
nimble/host/src/ble_l2cap_coc.c
nimble/host/src/ble_l2cap_sig.c
nimble/host/src/ble_l2cap_sig_cmd.c
nimble/host/src/ble_sm.c
nimble/host/src/ble_sm_alg.c
nimble/host/src/ble_sm_cmd.c
nimble/host/src/ble_sm_lgcy.c
nimble/host/src/ble_sm_sc.c
nimble/host/src/ble_store.c
nimble/host/src/ble_store_util.c
nimble/host/src/ble_uuid.c
nimble/host/services/gap/src/ble_svc_gap.c
nimble/host/services/gatt/src/ble_svc_gatt.c
nimble/host/services/bas/src/ble_svc_bas.c
nimble/host/services/dis/src/ble_svc_dis.c
# nimble/host/store/config/src/ble_store_config.c
# nimble/host/store/config/src/ble_store_config_conf.c
nimble/host/store/ram/src/ble_store_ram.c
nimble/host/util/src/addr.c
nimble/transport/common/hci_h4/src/hci_h4.c
nimble/transport/src/transport.c
ext/tinycrypt/src/aes_decrypt.c
ext/tinycrypt/src/aes_encrypt.c
ext/tinycrypt/src/cbc_mode.c
ext/tinycrypt/src/ccm_mode.c
ext/tinycrypt/src/cmac_mode.c
ext/tinycrypt/src/ctr_mode.c
ext/tinycrypt/src/ctr_prng.c
ext/tinycrypt/src/ecc.c
ext/tinycrypt/src/ecc_dh.c
ext/tinycrypt/src/ecc_dsa.c
ext/tinycrypt/src/ecc_platform_specific.c
ext/tinycrypt/src/hmac.c
ext/tinycrypt/src/hmac_prng.c
ext/tinycrypt/src/sha256.c
ext/tinycrypt/src/utils.c
===========头文件=================
nimble/include
nimble/host/include
nimble/host/services/ans/include
nimble/host/services/bas/include
nimble/host/services/dis/include
nimble/host/services/gap/include
nimble/host/services/gatt/include
nimble/host/services/ias/include
nimble/host/services/lls/include
nimble/host/services/tps/include
nimble/host/store/ram/include
nimble/host/util/include
nimble/transport/include
ext/tinycrypt/include
nimble/transport/common/hci_h4/include
```
- porting 请禁止使用 nimble 官方源码,请使用 porting 目录下的文件
```
头文件
porting/nimble/include
porting/npl/freertos/include
源文件
porting/nimble/src/endian.c
porting/nimble/src/mem.c
porting/nimble/src/nimble_port.c
porting/nimble/src/os_mbuf.c
porting/nimble/src/os_mempool.c
porting/nimble/src/os_msys_init.c
porting/npl/freertos/src/nimble_port_freertos.c
porting/npl/freertos/src/npl_os_freertos.c
```
- 初始化 nimble请注意必须在 usb bluetooth 识别以后才能初始化
```
#include "nimble/nimble_port.h"
#include "usbh_core.h"
void nimble_thread_entry(void *parameter)
{
nimble_port_run();
}
void nimble_init(void)
{
nimble_port_init();
usb_osal_thread_create("nimble", 2048, CONFIG_USBHOST_PSC_PRIO + 1, nimble_thread_entry, NULL);
}
```

View File

@@ -18,23 +18,6 @@ void ble_transport_ll_init(void)
/* nothing here */
}
static void hci_dump(uint8_t hci_type, uint8_t *data, uint32_t len)
{
uint32_t i = 0;
USB_LOG_DBG("\r\nhci type:%u", hci_type);
for (i = 0; i < len; i++) {
if (i % 16 == 0) {
USB_LOG_DBG("\r\n");
}
USB_LOG_DBG("%02x ", data[i]);
}
USB_LOG_DBG("\r\n");
}
static int hci_usb_frame_cb(uint8_t pkt_type, void *data)
{
switch (pkt_type) {
@@ -49,37 +32,16 @@ static int hci_usb_frame_cb(uint8_t pkt_type, void *data)
return -1;
}
static int ble_usb_transport_init(void)
void usbh_bluetooth_hci_read_callback(uint8_t *data, uint32_t len)
{
hci_h4_sm_init(&g_hci_h4sm, &hci_h4_allocs_from_ll, hci_usb_frame_cb);
return 0;
}
size_t remaining = len;
uint8_t pkt_indicator;
void usbh_bluetooth_hci_rx_callback(uint8_t hci_type, uint8_t *data, uint32_t len)
{
uint8_t pkt_type = 0;
pkt_indicator = *data++;
remaining -= sizeof(pkt_indicator);
switch (hci_type) {
case USB_BLUETOOTH_HCI_EVT:
pkt_type = HCI_H4_EVT;
break;
case USB_BLUETOOTH_HCI_ACL_IN:
pkt_type = HCI_H4_ACL;
break;
// case USB_BLUETOOTH_HCI_ISO_IN:
// break;
default:
USB_LOG_ERR("Unknown HCI type %u\r\n", hci_type);
return;
}
hci_dump(pkt_type, data, len);
hci_h4_sm_rx(&g_hci_h4sm, &pkt_type, 1);
hci_h4_sm_rx(&g_hci_h4sm, data, len);
hci_h4_sm_rx(&g_hci_h4sm, &pkt_indicator, 1);
hci_h4_sm_rx(&g_hci_h4sm, data, remaining);
}
int ble_transport_to_ll_cmd_impl(void *buf)
@@ -88,9 +50,7 @@ int ble_transport_to_ll_cmd_impl(void *buf)
uint8_t *cmd_pkt_data = (uint8_t *)buf;
size_t pkt_len = cmd_pkt_data[2] + 3;
hci_dump(HCI_H4_CMD, buf, pkt_len);
ret = usbh_bluetooth_hci_cmd(buf, pkt_len);
ret = usbh_bluetooth_hci_write(USB_BLUETOOTH_HCI_CMD, buf, pkt_len);
if (ret < 0) {
ret = BLE_ERR_MEM_CAPACITY;
} else {
@@ -107,9 +67,7 @@ int ble_transport_to_ll_acl_impl(struct os_mbuf *om)
int ret = 0;
while (x != NULL) {
hci_dump(HCI_H4_ACL, x->om_data, x->om_len);
ret = usbh_bluetooth_hci_acl_out(x->om_data, x->om_len);
ret = usbh_bluetooth_hci_write(USB_BLUETOOTH_HCI_ACL, x->om_data, x->om_len);
if (ret < 0) {
ret = BLE_ERR_MEM_CAPACITY;
break;
@@ -124,7 +82,6 @@ int ble_transport_to_ll_acl_impl(struct os_mbuf *om)
return ret;
}
__WEAK void usbh_bluetooth_run_callback(void)
{
/* bt_enable() */
@@ -137,11 +94,14 @@ __WEAK void usbh_bluetooth_stop_callback(void)
void usbh_bluetooth_run(struct usbh_bluetooth *bluetooth_class)
{
ble_usb_transport_init();
hci_h4_sm_init(&g_hci_h4sm, &hci_h4_allocs_from_ll, hci_usb_frame_cb);
usb_osal_thread_create("ble_event", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_event_rx_thread, NULL);
#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();
}

View File

@@ -1,31 +1,51 @@
/*
* Copyright (c) 2024, sakumisu
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "usbh_core.h"
#include "usbh_bluetooth.h"
#include "byteorder.h"
#include "hci_host.h"
#include "hci_driver.h"
#include <sys/byteorder.h>
#include <drivers/bluetooth/hci_driver.h>
static void hci_dump(uint8_t hci_type, uint8_t *data, uint32_t len)
#ifndef CONFIG_BT_RECV_IS_RX_THREAD
#error usb bluetooth must enable CONFIG_BT_RECV_IS_RX_THREAD
#endif
/* compatible with low version that less than v2.7.5 */
/* @brief The HCI event shall be given to bt_recv_prio */
#define __BT_HCI_EVT_FLAG_RECV_PRIO BIT(0)
/* @brief The HCI event shall be given to bt_recv. */
#define __BT_HCI_EVT_FLAG_RECV BIT(1)
/** @brief Get HCI event flags.
*
* Helper for the HCI driver to get HCI event flags that describes rules that.
* must be followed.
*
* When CONFIG_BT_RECV_IS_RX_THREAD is enabled the flags
* __BT_HCI_EVT_FLAG_RECV and __BT_HCI_EVT_FLAG_RECV_PRIO indicates if the event
* should be given to bt_recv or bt_recv_prio.
*
* @param evt HCI event code.
*
* @return HCI event flags for the specified event.
*/
static inline uint8_t __bt_hci_evt_get_flags(uint8_t evt)
{
uint32_t i = 0;
USB_LOG_DBG("hci type:%u\r\n", hci_type);
for (i = 0; i < len; i++) {
if (i % 16 == 0) {
USB_LOG_DBG("\r\n");
}
USB_LOG_DBG("%02x ", data[i]);
switch (evt) {
case BT_HCI_EVT_DISCONN_COMPLETE:
return __BT_HCI_EVT_FLAG_RECV | __BT_HCI_EVT_FLAG_RECV_PRIO;
/* fallthrough */
#if defined(CONFIG_BT_CONN) || defined(CONFIG_BT_ISO)
case BT_HCI_EVT_NUM_COMPLETED_PACKETS:
#if defined(CONFIG_BT_CONN)
case BT_HCI_EVT_DATA_BUF_OVERFLOW:
#endif /* defined(CONFIG_BT_CONN) */
#endif /* CONFIG_BT_CONN || CONFIG_BT_ISO */
case BT_HCI_EVT_CMD_COMPLETE:
case BT_HCI_EVT_CMD_STATUS:
return __BT_HCI_EVT_FLAG_RECV_PRIO;
default:
return __BT_HCI_EVT_FLAG_RECV;
}
USB_LOG_DBG("\r\n");
}
static bool is_hci_event_discardable(const uint8_t *evt_data)
@@ -184,23 +204,25 @@ static struct net_buf *usbh_bt_iso_recv(uint8_t *data, size_t remaining)
return buf;
}
static int usbh_hci_host_rcv_pkt(uint8_t pkt_indicator, uint8_t *data, uint16_t len)
static int usbh_hci_host_rcv_pkt(uint8_t *data, uint32_t len)
{
struct net_buf *buf = NULL;
size_t remaining = len;
bool prio = true;
uint8_t pkt_indicator;
pkt_indicator = *data++;
remaining -= sizeof(pkt_indicator);
switch (pkt_indicator) {
case USB_BLUETOOTH_HCI_EVT:
buf = usbh_bt_evt_recv(data, remaining);
break;
case USB_BLUETOOTH_HCI_ACL_IN:
case USB_BLUETOOTH_HCI_ACL:
buf = usbh_bt_acl_recv(data, remaining);
prio = false;
break;
case USB_BLUETOOTH_HCI_ISO_IN:
case USB_BLUETOOTH_HCI_SCO:
buf = usbh_bt_iso_recv(data, remaining);
break;
@@ -209,10 +231,19 @@ static int usbh_hci_host_rcv_pkt(uint8_t pkt_indicator, uint8_t *data, uint16_t
return -1;
}
hci_dump(pkt_indicator, buf->data, buf->len);
if (buf) {
bt_recv(buf);
if (pkt_indicator == USB_BLUETOOTH_HCI_EVT) {
struct bt_hci_evt_hdr *hdr = (void *)buf->data;
uint8_t evt_flags = __bt_hci_evt_get_flags(hdr->evt);
if (evt_flags & __BT_HCI_EVT_FLAG_RECV_PRIO) {
bt_recv_prio(buf);
} else {
bt_recv(buf);
}
} else {
bt_recv(buf);
}
}
return 0;
@@ -226,23 +257,29 @@ static int bt_usbh_open(void)
static int bt_usbh_send(struct net_buf *buf)
{
int err = 0;
uint8_t pkt_indicator = bt_buf_get_type(buf);
uint8_t pkt_indicator;
hci_dump(pkt_indicator, buf->data, buf->len);
switch (pkt_indicator) {
switch (bt_buf_get_type(buf)) {
case BT_BUF_ACL_OUT:
usbh_bluetooth_hci_acl_out(buf->data, buf->len);
pkt_indicator = USB_BLUETOOTH_HCI_ACL;
break;
case BT_BUF_CMD:
usbh_bluetooth_hci_cmd(buf->data, buf->len);
pkt_indicator = USB_BLUETOOTH_HCI_CMD;
break;
case BT_BUF_ISO_OUT:
pkt_indicator = USB_BLUETOOTH_HCI_ISO;
break;
default:
USB_LOG_ERR("Unknown type %u\r\n", pkt_indicator);
USB_LOG_ERR("Unknown type %u", bt_buf_get_type(buf));
goto done;
}
err = usbh_bluetooth_hci_write(pkt_indicator, buf->data, buf->len);
if (err < 0) {
err = 255;
} else {
err = 0;
}
done:
net_buf_unref(buf);
@@ -250,7 +287,7 @@ done:
}
static const struct bt_hci_driver usbh_drv = {
.name = "usbhost btble",
.name = "hci_usb",
.open = bt_usbh_open,
.send = bt_usbh_send,
.bus = BT_HCI_DRIVER_BUS_USB,
@@ -273,9 +310,12 @@ void usbh_bluetooth_run(struct usbh_bluetooth *bluetooth_class)
{
bt_hci_driver_register(&usbh_drv);
usb_osal_thread_create("ble_event", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bluetooth_hci_event_rx_thread, NULL);
#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();
}
@@ -284,7 +324,7 @@ void usbh_bluetooth_stop(struct usbh_bluetooth *bluetooth_class)
usbh_bluetooth_stop_callback();
}
void usbh_bluetooth_hci_rx_callback(uint8_t hci_type, uint8_t *data, uint32_t len)
void usbh_bluetooth_hci_read_callback(uint8_t *data, uint32_t len)
{
usbh_hci_host_rcv_pkt(hci_type, data, len);
usbh_hci_host_rcv_pkt(data, len);
}