From 6fdc79a61f6023478bd04703d55b929a1ed80b02 Mon Sep 17 00:00:00 2001 From: wangzhi7 Date: Fri, 29 Dec 2023 17:02:45 +0800 Subject: [PATCH] [bt_uart.c] fix bug:cant receive data rootcause: in btuart_rxwork, read data in blocking mode and btuart_read do three times, maybe remote send some packets one time,so it wont read the sencond packets. Signed-off-by: wangzhi7 --- drivers/wireless/bluetooth/Kconfig | 2 +- drivers/wireless/bluetooth/bt_uart.c | 123 ++++++++++------------ drivers/wireless/bluetooth/bt_uart.h | 1 - drivers/wireless/bluetooth/bt_uart_shim.c | 2 +- 4 files changed, 60 insertions(+), 68 deletions(-) diff --git a/drivers/wireless/bluetooth/Kconfig b/drivers/wireless/bluetooth/Kconfig index e1af5d6cf3005..2e88523d3e3e4 100644 --- a/drivers/wireless/bluetooth/Kconfig +++ b/drivers/wireless/bluetooth/Kconfig @@ -18,7 +18,7 @@ config BLUETOOTH_UART_GENERIC default n config BLUETOOTH_UART_SHIM - bool + bool "Bluetooth UART SHIM driver" default n if BLUETOOTH_UART diff --git a/drivers/wireless/bluetooth/bt_uart.c b/drivers/wireless/bluetooth/bt_uart.c index 4b0a87e9bdb54..c96cd6f0a121e 100644 --- a/drivers/wireless/bluetooth/bt_uart.c +++ b/drivers/wireless/bluetooth/bt_uart.c @@ -121,71 +121,69 @@ static void btuart_rxwork(FAR void *arg) * Read the first byte to get the packet type. */ - nread = btuart_read(upper, data, H4_HEADER_SIZE, 0); - if (nread != H4_HEADER_SIZE) + while (true) { - wlwarn("WARNING: Unable to read H4 packet type: %zd\n", nread); - goto errout_with_busy; - } - - if (data[0] == H4_EVT) - { - hdrlen = sizeof(struct bt_hci_evt_hdr_s); - } - else if (data[0] == H4_ACL) - { - hdrlen = sizeof(struct bt_hci_acl_hdr_s); - } - else - { - wlerr("ERROR: Unknown H4 type %u\n", data[0]); - goto errout_with_busy; - } + nread = btuart_read(upper, data, H4_HEADER_SIZE, 0); + if (nread != H4_HEADER_SIZE) + { + wlwarn("WARNING: Unable to read H4 packet type: %zd\n", nread); + break; + } - nread = btuart_read(upper, data + H4_HEADER_SIZE, - hdrlen, hdrlen); - if (nread != hdrlen) - { - wlwarn("WARNING: Unable to read H4 packet header: %zd\n", nread); - goto errout_with_busy; - } + if (data[0] == H4_EVT) + { + hdrlen = sizeof(struct bt_hci_evt_hdr_s); + } + else if (data[0] == H4_ACL) + { + hdrlen = sizeof(struct bt_hci_acl_hdr_s); + } + else + { + wlerr("ERROR: Unknown H4 type %u\n", data[0]); + break; + } - hdr = (FAR void *)(data + H4_HEADER_SIZE); + nread = btuart_read(upper, data + H4_HEADER_SIZE, + hdrlen, hdrlen); + if (nread != hdrlen) + { + wlwarn("WARNING: Unable to read H4 packet header: %zd\n", nread); + break; + } - if (data[0] == H4_EVT) - { - pktlen = hdr->evt.len; - type = BT_EVT; - } - else if (data[0] == H4_ACL) - { - pktlen = hdr->acl.len; - type = BT_ACL_IN; - } - else - { - wlerr("ERROR: Unknown H4 type %u\n", data[0]); - goto errout_with_busy; - } + hdr = (FAR void *)(data + H4_HEADER_SIZE); - nread = btuart_read(upper, data + H4_HEADER_SIZE + hdrlen, - pktlen, pktlen); - if (nread != pktlen) - { - wlwarn("WARNING: Unable to read H4 packet: %zd\n", nread); - goto errout_with_busy; - } + if (data[0] == H4_EVT) + { + pktlen = hdr->evt.len; + type = BT_EVT; + } + else if (data[0] == H4_ACL) + { + pktlen = hdr->acl.len; + type = BT_ACL_IN; + } + else + { + wlerr("ERROR: Unknown H4 type %u\n", data[0]); + break; + } - /* Pass buffer to the stack */ + nread = btuart_read(upper, data + H4_HEADER_SIZE + hdrlen, + pktlen, pktlen); + if (nread != pktlen) + { + wlwarn("WARNING: Unable to read H4 packet: %zd\n", nread); + break; + } - BT_DUMP("Received", data, H4_HEADER_SIZE + hdrlen + pktlen); - upper->busy = false; - bt_netdev_receive(&upper->dev, type, data + H4_HEADER_SIZE, - hdrlen + pktlen); - return; + /* Pass buffer to the stack */ -errout_with_busy: - upper->busy = false; + BT_DUMP("Received", data, H4_HEADER_SIZE + hdrlen + pktlen); + bt_netdev_receive(&upper->dev, type, data + H4_HEADER_SIZE, + hdrlen + pktlen); + } } static void btuart_rxcallback(FAR const struct btuart_lowerhalf_s *lower, @@ -196,15 +194,10 @@ static void btuart_rxcallback(FAR const struct btuart_lowerhalf_s *lower, DEBUGASSERT(lower != NULL && arg != NULL); upper = (FAR struct btuart_upperhalf_s *)arg; - if (!upper->busy) + int ret = work_queue(HPWORK, &upper->work, btuart_rxwork, arg, 0); + if (ret < 0) { - upper->busy = true; - int ret = work_queue(HPWORK, &upper->work, btuart_rxwork, arg, 0); - if (ret < 0) - { - upper->busy = false; - wlerr("ERROR: work_queue failed: %d\n", ret); - } + wlerr("ERROR: work_queue failed: %d\n", ret); } } diff --git a/drivers/wireless/bluetooth/bt_uart.h b/drivers/wireless/bluetooth/bt_uart.h index 9aeb32c29796b..ff3cef1244246 100644 --- a/drivers/wireless/bluetooth/bt_uart.h +++ b/drivers/wireless/bluetooth/bt_uart.h @@ -78,7 +78,6 @@ struct btuart_upperhalf_s /* Work queue support */ struct work_s work; - volatile bool busy; }; /**************************************************************************** diff --git a/drivers/wireless/bluetooth/bt_uart_shim.c b/drivers/wireless/bluetooth/bt_uart_shim.c index 7fc69472ebdb6..fdd42bd66c724 100644 --- a/drivers/wireless/bluetooth/bt_uart_shim.c +++ b/drivers/wireless/bluetooth/bt_uart_shim.c @@ -359,7 +359,7 @@ FAR struct btuart_lowerhalf_s *btuart_shim_getdevice(FAR const char *path) s = &n->state; - ret = file_open(&s->f, path, O_RDWR | O_CLOEXEC); + ret = file_open(&s->f, path, O_RDWR | O_CLOEXEC | O_NONBLOCK); if (ret < 0) { kmm_free(n);