arch/arm/src/stm32 and drivers/wireless/bluetooth: Fix some buffer management errors in HCI UART upper and lower halves.

This commit is contained in:
Gregory Nutt 2018-04-18 08:17:15 -06:00
parent d93b22d042
commit 1224db454b
3 changed files with 23 additions and 19 deletions

View File

@ -1154,7 +1154,7 @@ static ssize_t hciuart_copytorxbuffer(const struct hciuart_config_s *config)
nxsem_post(&state->rxwait); nxsem_post(&state->rxwait);
} }
wlinfo("nbytes %ld\n", (long)nbytes); wlinfo("rxhead %u rxtail %u nbytes %ld\n", rxhead, rxtail, (long)nbytes);
return nbytes; return nbytes;
} }
@ -1171,7 +1171,7 @@ static ssize_t hciuart_copyfromrxbuffer(const struct hciuart_config_s *config,
uint8_t *dest, size_t destlen) uint8_t *dest, size_t destlen)
{ {
struct hciuart_state_s *state; struct hciuart_state_s *state;
ssize_t nbytes = 0; ssize_t nbytes;
uint16_t rxhead; uint16_t rxhead;
uint16_t rxtail; uint16_t rxtail;
uint8_t rxbyte; uint8_t rxbyte;
@ -1181,20 +1181,14 @@ static ssize_t hciuart_copyfromrxbuffer(const struct hciuart_config_s *config,
state = config->state; state = config->state;
rxhead = state->rxhead; rxhead = state->rxhead;
rxtail = state->rxtail; rxtail = state->rxtail;
nbytes = 0;
/* Is there data available in the Rx FIFO? */ /* Is there data available in the Rx buffer? Is there space in the user
* buffer?
*/
while ((hciuart_getreg32(config, STM32_USART_SR_OFFSET) & USART_SR_RXNE) != 0) while (rxhead != rxtail && nbytes < destlen)
{ {
/* Is the Rx buffer empty? Is there space in the user buffer? */
if (rxhead == rxtail && nbytes < destlen)
{
/* Yes, stop the copy and update the indices */
break;
}
/* Get a byte from the head of the Rx buffer */ /* Get a byte from the head of the Rx buffer */
rxbyte = config->rxbuffer[rxhead]; rxbyte = config->rxbuffer[rxhead];
@ -1202,6 +1196,9 @@ static ssize_t hciuart_copyfromrxbuffer(const struct hciuart_config_s *config,
/* And add it to the caller's buffer buffer */ /* And add it to the caller's buffer buffer */
dest[nbytes] = rxbyte; dest[nbytes] = rxbyte;
/* Update indices and counts */
nbytes++; nbytes++;
if (++rxhead >= config->rxbufsize) if (++rxhead >= config->rxbufsize)
@ -1218,7 +1215,7 @@ static ssize_t hciuart_copyfromrxbuffer(const struct hciuart_config_s *config,
state->rxhead = rxhead; state->rxhead = rxhead;
wlinfo("nbytes %ld\n", (long)nbytes); wlinfo("rxhead %u rxtail %u nbytes %ld\n", rxhead, rxtail, (long)nbytes);
return nbytes; return nbytes;
} }

View File

@ -12,6 +12,7 @@ CONFIG_ARCH_BOARD="stm32f4discovery"
CONFIG_ARCH_BUTTONS=y CONFIG_ARCH_BUTTONS=y
CONFIG_ARCH_CHIP_STM32=y CONFIG_ARCH_CHIP_STM32=y
CONFIG_ARCH_CHIP_STM32F407VG=y CONFIG_ARCH_CHIP_STM32F407VG=y
CONFIG_ARCH_INTERRUPTSTACK=2048
CONFIG_ARCH_STACKDUMP=y CONFIG_ARCH_STACKDUMP=y
CONFIG_ARCH="arm" CONFIG_ARCH="arm"
CONFIG_ARMV7M_TOOLCHAIN_CODESOURCERYW=y CONFIG_ARMV7M_TOOLCHAIN_CODESOURCERYW=y
@ -65,7 +66,6 @@ CONFIG_RAM_SIZE=114688
CONFIG_RAM_START=0x20000000 CONFIG_RAM_START=0x20000000
CONFIG_RAW_BINARY=y CONFIG_RAW_BINARY=y
CONFIG_RR_INTERVAL=200 CONFIG_RR_INTERVAL=200
CONFIG_SCHED_HPWORK=y
CONFIG_SCHED_HPWORKPRIORITY=192 CONFIG_SCHED_HPWORKPRIORITY=192
CONFIG_SCHED_WAITPID=y CONFIG_SCHED_WAITPID=y
CONFIG_SDCLONE_DISABLE=y CONFIG_SDCLONE_DISABLE=y

View File

@ -212,6 +212,7 @@ static FAR struct bt_buf_s *btuart_acl_recv(FAR struct btuart_upperhalf_s *upper
static void btuart_rxwork(FAR void *arg) static void btuart_rxwork(FAR void *arg)
{ {
FAR struct btuart_upperhalf_s *upper; FAR struct btuart_upperhalf_s *upper;
FAR const struct btuart_lowerhalf_s *lower;
static FAR struct bt_buf_s *buf; static FAR struct bt_buf_s *buf;
static unsigned int hdrlen; static unsigned int hdrlen;
static int remaining; static int remaining;
@ -219,7 +220,8 @@ static void btuart_rxwork(FAR void *arg)
uint8_t type; uint8_t type;
upper = (FAR struct btuart_upperhalf_s *)arg; upper = (FAR struct btuart_upperhalf_s *)arg;
DEBUGASSERT(upper != NULL); DEBUGASSERT(upper != NULL && upper->lower != NULL);
lower = upper->lower;
/* Beginning of a new packet. Read the first byte to get the packet type. */ /* Beginning of a new packet. Read the first byte to get the packet type. */
@ -251,8 +253,7 @@ static void btuart_rxwork(FAR void *arg)
if (buf == NULL) if (buf == NULL)
{ {
FAR const struct btuart_lowerhalf_s *lower = upper->lower; /* Failed to allocate a buffer. Drain the Rx data and fail the read. */
DEBUGASSERT(lower != NULL);
nread = lower->rxdrain(lower); nread = lower->rxdrain(lower);
wlwarn("WARNING: Discarded %ld bytes\n", (long)nread); wlwarn("WARNING: Discarded %ld bytes\n", (long)nread);
@ -276,9 +277,15 @@ static void btuart_rxwork(FAR void *arg)
remaining -= nread; remaining -= nread;
} }
wlinfo("Full packet received\n");
/* Drain any un-read bytes from the Rx buffer */
nread = lower->rxdrain(lower);
wlwarn("WARNING: Discarded %ld bytes\n", (long)nread);
/* Pass buffer to the stack */ /* Pass buffer to the stack */
wlinfo("Full packet received\n");
upper->busy = false; upper->busy = false;
bt_hci_receive(buf); bt_hci_receive(buf);
return; return;