Skip to content

Commit

Permalink
[bt_uart.c] fix bug:cant receive data
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
wangzhi7 authored and W-M-R committed Oct 16, 2024
1 parent 48dd8bc commit b4aced4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 68 deletions.
2 changes: 1 addition & 1 deletion drivers/wireless/bluetooth/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
123 changes: 58 additions & 65 deletions drivers/wireless/bluetooth/bt_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
}

Expand Down
1 change: 0 additions & 1 deletion drivers/wireless/bluetooth/bt_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ struct btuart_upperhalf_s
/* Work queue support */

struct work_s work;
volatile bool busy;
};

/****************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion drivers/wireless/bluetooth/bt_uart_shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b4aced4

Please sign in to comment.