2018-03-30 02:36:03 +02:00
|
|
|
/****************************************************************************
|
2018-04-03 17:00:18 +02:00
|
|
|
* drivers/wireless/bluetooth/bt_uart.c
|
2018-03-30 02:36:03 +02:00
|
|
|
* UART based Bluetooth driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Ported from the Intel/Zephyr arduino101_firmware_source-v1.tar package
|
|
|
|
* where the code was released with a compatible 3-clause BSD license:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016, Intel Corporation
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from this
|
|
|
|
* software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2018-04-20 01:48:04 +02:00
|
|
|
#include <string.h>
|
2018-03-30 02:36:03 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2018-04-20 01:48:04 +02:00
|
|
|
#include <debug.h>
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2018-04-29 20:43:09 +02:00
|
|
|
#include <nuttx/wireless/bluetooth/bt_core.h>
|
|
|
|
#include <nuttx/wireless/bluetooth/bt_hci.h>
|
|
|
|
#include <nuttx/wireless/bluetooth/bt_driver.h>
|
|
|
|
#include <nuttx/wireless/bluetooth/bt_uart.h>
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2018-04-20 01:48:04 +02:00
|
|
|
#include "bt_uart.h"
|
2018-03-30 02:36:03 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t btuart_read(FAR struct btuart_upperhalf_s *upper,
|
|
|
|
FAR uint8_t *buffer, size_t buflen,
|
|
|
|
size_t minread)
|
|
|
|
{
|
|
|
|
FAR const struct btuart_lowerhalf_s *lower;
|
|
|
|
ssize_t nread;
|
|
|
|
ssize_t ntotal = 0;
|
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
wlinfo("buflen %lu minread %lu\n",
|
|
|
|
(unsigned long)buflen, (unsigned long)minread);
|
|
|
|
|
2018-03-30 02:36:03 +02:00
|
|
|
DEBUGASSERT(upper != NULL && upper->lower != NULL);
|
|
|
|
lower = upper->lower;
|
|
|
|
DEBUGASSERT(lower->read != NULL);
|
|
|
|
|
|
|
|
while (buflen > 0)
|
|
|
|
{
|
|
|
|
nread = lower->read(lower, buffer, buflen);
|
|
|
|
if (nread == 0)
|
|
|
|
{
|
|
|
|
wlinfo("Got zero bytes from UART\n");
|
|
|
|
if (ntotal < minread)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (nread < 0)
|
|
|
|
{
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlinfo("read %ld remaining %lu\n",
|
|
|
|
(long)nread, (unsigned long)(buflen - nread));
|
|
|
|
|
|
|
|
buflen -= nread;
|
|
|
|
ntotal += nread;
|
|
|
|
buffer += nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ntotal;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FAR struct bt_buf_s *btuart_evt_recv(FAR struct btuart_upperhalf_s *upper,
|
2018-04-17 21:07:28 +02:00
|
|
|
FAR unsigned int *hdrlen)
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
|
|
|
FAR struct bt_buf_s *buf;
|
|
|
|
struct bt_hci_evt_hdr_s hdr;
|
|
|
|
ssize_t nread;
|
|
|
|
|
|
|
|
/* We can ignore the return value since we pass buflen == minread */
|
|
|
|
|
|
|
|
nread = btuart_read(upper, (FAR uint8_t *)&hdr,
|
|
|
|
sizeof(struct bt_hci_evt_hdr_s),
|
|
|
|
sizeof(struct bt_hci_evt_hdr_s));
|
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
if (nread != sizeof(struct bt_hci_evt_hdr_s))
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
2018-04-17 21:07:28 +02:00
|
|
|
wlerr("ERROR: btuart_read returned %ld\n", (long)nread);
|
2018-03-30 02:36:03 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-01 22:00:40 +02:00
|
|
|
buf = bt_buf_alloc(BT_EVT, NULL, 0);
|
2018-03-30 02:36:03 +02:00
|
|
|
if (buf != NULL)
|
|
|
|
{
|
2018-04-01 20:25:30 +02:00
|
|
|
memcpy(bt_buf_extend(buf, sizeof(struct bt_hci_evt_hdr_s)), &hdr,
|
2018-03-30 02:36:03 +02:00
|
|
|
sizeof(struct bt_hci_evt_hdr_s));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wlerr("ERROR: No available event buffers!\n");
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
*hdrlen = (int)hdr.len;
|
|
|
|
|
|
|
|
wlinfo("hdrlen %u\n", hdr.len);
|
2018-03-30 02:36:03 +02:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FAR struct bt_buf_s *btuart_acl_recv(FAR struct btuart_upperhalf_s *upper,
|
2018-04-17 21:07:28 +02:00
|
|
|
FAR unsigned int *hdrlen)
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
|
|
|
FAR struct bt_buf_s *buf;
|
|
|
|
struct bt_hci_acl_hdr_s hdr;
|
|
|
|
ssize_t nread;
|
|
|
|
|
|
|
|
/* We can ignore the return value since we pass buflen == minread */
|
|
|
|
|
|
|
|
nread = btuart_read(upper, (FAR uint8_t *)&hdr,
|
|
|
|
sizeof(struct bt_hci_acl_hdr_s),
|
|
|
|
sizeof(struct bt_hci_acl_hdr_s));
|
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
if (nread != sizeof(struct bt_hci_acl_hdr_s))
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
2018-04-17 21:07:28 +02:00
|
|
|
wlerr("ERROR: btuart_read returned %ld\n", (long)nread);
|
2018-03-30 02:36:03 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-01 22:00:40 +02:00
|
|
|
buf = bt_buf_alloc(BT_ACL_IN, NULL, 0);
|
2018-04-17 21:07:28 +02:00
|
|
|
if (buf != NULL)
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
2018-04-01 20:25:30 +02:00
|
|
|
memcpy(bt_buf_extend(buf, sizeof(struct bt_hci_acl_hdr_s)), &hdr,
|
2018-03-30 02:36:03 +02:00
|
|
|
sizeof(struct bt_hci_acl_hdr_s));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wlerr("ERROR: No available ACL buffers!\n");
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
*hdrlen = BT_LE162HOST(hdr.len);
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
wlinfo("hdrlen %u\n", *hdrlen);
|
2018-03-30 02:36:03 +02:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
static void btuart_rxwork(FAR void *arg)
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
|
|
|
FAR struct btuart_upperhalf_s *upper;
|
2018-04-18 16:17:15 +02:00
|
|
|
FAR const struct btuart_lowerhalf_s *lower;
|
2018-03-30 02:36:03 +02:00
|
|
|
static FAR struct bt_buf_s *buf;
|
2018-04-17 21:07:28 +02:00
|
|
|
static unsigned int hdrlen;
|
2018-03-30 02:36:03 +02:00
|
|
|
static int remaining;
|
|
|
|
ssize_t nread;
|
2018-04-17 21:07:28 +02:00
|
|
|
uint8_t type;
|
2018-03-30 02:36:03 +02:00
|
|
|
|
|
|
|
upper = (FAR struct btuart_upperhalf_s *)arg;
|
2018-04-18 16:17:15 +02:00
|
|
|
DEBUGASSERT(upper != NULL && upper->lower != NULL);
|
|
|
|
lower = upper->lower;
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
/* Beginning of a new packet. Read the first byte to get the packet type. */
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
buf = NULL;
|
|
|
|
hdrlen = 0;
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
nread = btuart_read(upper, &type, 1, 0);
|
|
|
|
if (nread != 1)
|
|
|
|
{
|
|
|
|
wlwarn("WARNING: Unable to read H4 packet type: %ld\n",
|
|
|
|
(long)nread);
|
|
|
|
goto errout_with_busy;
|
|
|
|
}
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case H4_EVT:
|
|
|
|
buf = btuart_evt_recv(upper, &hdrlen);
|
|
|
|
break;
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
case H4_ACL:
|
|
|
|
buf = btuart_acl_recv(upper, &hdrlen);
|
|
|
|
break;
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
default:
|
|
|
|
wlerr("ERROR: Unknown H4 type %u\n", type);
|
|
|
|
goto errout_with_busy;
|
2018-03-30 02:36:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buf == NULL)
|
|
|
|
{
|
2018-04-18 16:17:15 +02:00
|
|
|
/* Failed to allocate a buffer. Drain the Rx data and fail the read. */
|
2018-04-17 21:07:28 +02:00
|
|
|
|
2018-03-30 02:36:03 +02:00
|
|
|
nread = lower->rxdrain(lower);
|
|
|
|
wlwarn("WARNING: Discarded %ld bytes\n", (long)nread);
|
2018-04-17 21:07:28 +02:00
|
|
|
goto errout_with_busy;
|
|
|
|
}
|
|
|
|
else if ((hdrlen - 1) > bt_buf_tailroom(buf))
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Not enough space in buffer\n");
|
|
|
|
goto errout_with_buf;
|
2018-03-30 02:36:03 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 20:46:47 +02:00
|
|
|
remaining = hdrlen;
|
|
|
|
wlinfo("Need to get %u bytes\n", remaining);
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
while (remaining > 0)
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
2018-04-17 21:07:28 +02:00
|
|
|
nread = btuart_read(upper, bt_buf_tail(buf), remaining, 0);
|
|
|
|
wlinfo("Received %ld bytes\n", (long)nread);
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
buf->len += nread;
|
|
|
|
remaining -= nread;
|
2018-03-30 02:36:03 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 16:17:15 +02:00
|
|
|
wlinfo("Full packet received\n");
|
|
|
|
|
|
|
|
/* Drain any un-read bytes from the Rx buffer */
|
|
|
|
|
|
|
|
nread = lower->rxdrain(lower);
|
2018-04-18 20:46:47 +02:00
|
|
|
if (nread > 0)
|
|
|
|
{
|
|
|
|
wlwarn("WARNING: Discarded %ld bytes\n", (long)nread);
|
|
|
|
}
|
2018-04-18 16:17:15 +02:00
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
/* Pass buffer to the stack */
|
|
|
|
|
|
|
|
upper->busy = false;
|
2018-04-18 20:46:47 +02:00
|
|
|
|
|
|
|
BT_DUMP("Received", buf->data, buf->len);
|
2018-04-17 21:07:28 +02:00
|
|
|
bt_hci_receive(buf);
|
2018-03-30 02:36:03 +02:00
|
|
|
return;
|
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
errout_with_buf:
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_release(buf);
|
2018-04-17 21:07:28 +02:00
|
|
|
|
|
|
|
errout_with_busy:
|
|
|
|
upper->busy = false;
|
2018-03-30 02:36:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
static void btuart_rxcallback(FAR const struct btuart_lowerhalf_s *lower,
|
|
|
|
FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct btuart_upperhalf_s *upper;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: work_queue failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
upper->busy = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 01:48:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int btuart_send(FAR const struct bt_driver_s *dev, FAR struct bt_buf_s *buf)
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
|
|
|
FAR struct btuart_upperhalf_s *upper;
|
|
|
|
FAR const struct btuart_lowerhalf_s *lower;
|
|
|
|
FAR uint8_t *type;
|
|
|
|
ssize_t nwritten;
|
|
|
|
|
|
|
|
upper = (FAR struct btuart_upperhalf_s *)dev;
|
|
|
|
DEBUGASSERT(upper != NULL && upper->lower != NULL);
|
|
|
|
lower = upper->lower;
|
|
|
|
|
|
|
|
if (bt_buf_headroom(buf) < H4_HEADER_SIZE)
|
|
|
|
{
|
|
|
|
wlerr("Not enough headroom in buffer\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
type = bt_buf_provide(buf, 1);
|
2018-03-30 02:36:03 +02:00
|
|
|
|
|
|
|
switch (buf->type)
|
|
|
|
{
|
|
|
|
case BT_CMD:
|
|
|
|
*type = H4_CMD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_ACL_OUT:
|
|
|
|
*type = H4_ACL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_EVT:
|
|
|
|
*type = H4_EVT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
wlerr("Unknown buf type %u\n", buf->type);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-04-18 20:46:47 +02:00
|
|
|
BT_DUMP("Sending", buf->data, buf->len);
|
|
|
|
|
2018-03-30 02:36:03 +02:00
|
|
|
nwritten = lower->write(lower, buf->data, buf->len);
|
|
|
|
if (nwritten == buf->len)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
|
|
|
return (int)nwritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2018-04-20 01:48:04 +02:00
|
|
|
int btuart_open(FAR const struct bt_driver_s *dev)
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
|
|
|
FAR struct btuart_upperhalf_s *upper;
|
|
|
|
FAR const struct btuart_lowerhalf_s *lower;
|
|
|
|
|
2018-04-17 15:13:03 +02:00
|
|
|
wlinfo("dev %p\n", dev);
|
|
|
|
|
2018-03-30 02:36:03 +02:00
|
|
|
upper = (FAR struct btuart_upperhalf_s *)dev;
|
|
|
|
DEBUGASSERT(upper != NULL && upper->lower != NULL);
|
|
|
|
lower = upper->lower;
|
|
|
|
|
2018-04-13 18:36:23 +02:00
|
|
|
/* Disable Rx callbacks */
|
2018-03-30 02:36:03 +02:00
|
|
|
|
|
|
|
lower->rxenable(lower, false);
|
|
|
|
|
|
|
|
/* Drain any cached Rx data */
|
|
|
|
|
|
|
|
(void)lower->rxdrain(lower);
|
|
|
|
|
2018-04-20 01:48:04 +02:00
|
|
|
/* Attach the Rx event handler */
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2018-04-13 18:36:23 +02:00
|
|
|
lower->rxattach(lower, btuart_rxcallback, upper);
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2018-04-20 01:48:04 +02:00
|
|
|
/* Re-enable Rx callbacks */
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2018-04-20 01:48:04 +02:00
|
|
|
lower->rxenable(lower, true);
|
|
|
|
return OK;
|
2018-03-30 02:36:03 +02:00
|
|
|
}
|