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
|
|
|
*
|
|
|
|
* Copyright (c) 2016, Intel Corporation
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2020-12-26 20:13:27 +01:00
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
2018-03-30 02:36:03 +02:00
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
2020-12-26 20:13:27 +01:00
|
|
|
* 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.
|
2018-03-30 02:36:03 +02:00
|
|
|
*
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its
|
2020-12-26 20:13:27 +01:00
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
2018-03-30 02:36:03 +02:00
|
|
|
*
|
2020-12-26 20:13:27 +01:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
|
2018-03-30 02:36:03 +02:00
|
|
|
* 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
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
#include <nuttx/net/bluetooth.h>
|
|
|
|
|
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>
|
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 ntotal = 0;
|
2020-12-27 08:40:30 +01:00
|
|
|
ssize_t nread;
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2022-10-01 21:23:04 +02:00
|
|
|
wlinfo("buflen %zu minread %zu\n", buflen, minread);
|
2018-04-17 21:07:28 +02:00
|
|
|
|
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);
|
2020-12-27 12:57:16 +01:00
|
|
|
if (nread == 0 || nread == -EINTR)
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
2019-09-21 15:16:37 +02:00
|
|
|
wlwarn("Got zero bytes from UART\n");
|
2018-03-30 02:36:03 +02:00
|
|
|
if (ntotal < minread)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (nread < 0)
|
|
|
|
{
|
2022-10-01 21:23:04 +02:00
|
|
|
wlwarn("Returned error %zd\n", nread);
|
2018-03-30 02:36:03 +02:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2022-10-01 21:23:04 +02:00
|
|
|
wlinfo("read %zd remaining %zu\n", nread, buflen - nread);
|
2018-03-30 02:36:03 +02:00
|
|
|
|
|
|
|
buflen -= nread;
|
|
|
|
ntotal += nread;
|
|
|
|
buffer += nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ntotal;
|
|
|
|
}
|
|
|
|
|
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;
|
2021-05-18 15:45:01 +02:00
|
|
|
uint8_t data[BLUETOOTH_MAX_FRAMELEN];
|
|
|
|
enum bt_buf_type_e type;
|
2020-12-27 08:34:36 +01:00
|
|
|
unsigned int hdrlen;
|
2021-05-18 15:45:01 +02:00
|
|
|
unsigned int pktlen;
|
2018-03-30 02:36:03 +02:00
|
|
|
ssize_t nread;
|
2021-05-18 15:45:01 +02:00
|
|
|
union
|
|
|
|
{
|
|
|
|
struct bt_hci_evt_hdr_s evt;
|
|
|
|
struct bt_hci_acl_hdr_s acl;
|
|
|
|
}
|
|
|
|
|
|
|
|
*hdr;
|
2018-03-30 02:36:03 +02:00
|
|
|
|
|
|
|
upper = (FAR struct btuart_upperhalf_s *)arg;
|
|
|
|
|
2020-12-26 20:13:27 +01:00
|
|
|
/* Beginning of a new packet.
|
|
|
|
* Read the first byte to get the packet type.
|
|
|
|
*/
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
nread = btuart_read(upper, data, H4_HEADER_SIZE, 0);
|
2022-10-01 21:23:04 +02:00
|
|
|
if (nread != H4_HEADER_SIZE)
|
2018-04-17 21:07:28 +02:00
|
|
|
{
|
2022-10-01 21:23:04 +02:00
|
|
|
wlwarn("WARNING: Unable to read H4 packet type: %zd\n", nread);
|
2018-04-17 21:07:28 +02:00
|
|
|
goto errout_with_busy;
|
|
|
|
}
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
if (data[0] == H4_EVT)
|
2018-04-17 21:07:28 +02:00
|
|
|
{
|
2021-05-18 15:45:01 +02:00
|
|
|
hdrlen = sizeof(struct bt_hci_evt_hdr_s);
|
2018-03-30 02:36:03 +02:00
|
|
|
}
|
2021-05-18 15:45:01 +02:00
|
|
|
else if (data[0] == H4_ACL)
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
2021-05-18 15:45:01 +02:00
|
|
|
hdrlen = sizeof(struct bt_hci_acl_hdr_s);
|
2018-04-17 21:07:28 +02:00
|
|
|
}
|
2021-05-18 15:45:01 +02:00
|
|
|
else
|
2018-04-17 21:07:28 +02:00
|
|
|
{
|
2021-05-18 15:45:01 +02:00
|
|
|
wlerr("ERROR: Unknown H4 type %u\n", data[0]);
|
|
|
|
goto errout_with_busy;
|
2018-03-30 02:36:03 +02:00
|
|
|
}
|
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
nread = btuart_read(upper, data + H4_HEADER_SIZE,
|
|
|
|
hdrlen, hdrlen);
|
|
|
|
if (nread != hdrlen)
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
2022-10-01 21:23:04 +02:00
|
|
|
wlwarn("WARNING: Unable to read H4 packet header: %zd\n", nread);
|
2021-05-18 15:45:01 +02:00
|
|
|
goto errout_with_busy;
|
|
|
|
}
|
2019-09-21 15:16:37 +02:00
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
hdr = (void *)(data + H4_HEADER_SIZE);
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
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;
|
2018-03-30 02:36:03 +02:00
|
|
|
}
|
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
nread = btuart_read(upper, data + H4_HEADER_SIZE + hdrlen,
|
|
|
|
pktlen, pktlen);
|
|
|
|
if (nread != pktlen)
|
|
|
|
{
|
2022-10-01 21:23:04 +02:00
|
|
|
wlwarn("WARNING: Unable to read H4 packet: %zd\n", nread);
|
2021-05-18 15:45:01 +02:00
|
|
|
goto errout_with_busy;
|
|
|
|
}
|
2018-04-18 16:17:15 +02:00
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
/* Pass buffer to the stack */
|
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
BT_DUMP("Received", data, H4_HEADER_SIZE + hdrlen + pktlen);
|
2019-09-21 15:16:37 +02:00
|
|
|
upper->busy = false;
|
2021-05-18 15:45:01 +02:00
|
|
|
bt_netdev_receive(&upper->dev, type, data + H4_HEADER_SIZE,
|
|
|
|
hdrlen + pktlen);
|
2018-03-30 02:36:03 +02:00
|
|
|
return;
|
|
|
|
|
2018-04-17 21:07:28 +02:00
|
|
|
errout_with_busy:
|
|
|
|
upper->busy = false;
|
2018-03-30 02:36:03 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2019-09-21 15:16:37 +02:00
|
|
|
upper->busy = true;
|
2018-04-17 21:07:28 +02:00
|
|
|
int ret = work_queue(HPWORK, &upper->work, btuart_rxwork, arg, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-09-21 15:16:37 +02:00
|
|
|
upper->busy = false;
|
2018-04-17 21:07:28 +02:00
|
|
|
wlerr("ERROR: work_queue failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 01:48:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
int btuart_send(FAR struct bt_driver_s *dev,
|
|
|
|
enum bt_buf_type_e type,
|
|
|
|
FAR void *data, size_t len)
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
2021-05-18 15:45:01 +02:00
|
|
|
FAR uint8_t *hdr = (FAR uint8_t *)data - dev->head_reserve;
|
2018-03-30 02:36:03 +02:00
|
|
|
FAR struct btuart_upperhalf_s *upper;
|
|
|
|
FAR const struct btuart_lowerhalf_s *lower;
|
2020-12-27 12:57:16 +01:00
|
|
|
ssize_t ntotal = 0;
|
2018-03-30 02:36:03 +02:00
|
|
|
|
|
|
|
upper = (FAR struct btuart_upperhalf_s *)dev;
|
|
|
|
DEBUGASSERT(upper != NULL && upper->lower != NULL);
|
|
|
|
lower = upper->lower;
|
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
if (type == BT_CMD)
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
2021-05-18 15:45:01 +02:00
|
|
|
*hdr = H4_CMD;
|
2018-03-30 02:36:03 +02:00
|
|
|
}
|
2021-05-18 15:45:01 +02:00
|
|
|
else if (type == BT_ACL_OUT)
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
2021-05-18 15:45:01 +02:00
|
|
|
*hdr = H4_ACL;
|
2018-03-30 02:36:03 +02:00
|
|
|
}
|
2021-05-18 15:45:01 +02:00
|
|
|
else if (type == BT_ISO_OUT)
|
|
|
|
{
|
|
|
|
*hdr = H4_ISO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
len += H4_HEADER_SIZE;
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
BT_DUMP("Sending", hdr, len);
|
2018-04-18 20:46:47 +02:00
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
while (ntotal < len)
|
2018-03-30 02:36:03 +02:00
|
|
|
{
|
2020-12-27 12:57:16 +01:00
|
|
|
ssize_t nwritten;
|
2018-03-30 02:36:03 +02:00
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
nwritten = lower->write(lower, hdr + ntotal, len - ntotal);
|
2020-12-27 12:57:16 +01:00
|
|
|
if (nwritten >= 0)
|
|
|
|
{
|
|
|
|
ntotal += nwritten;
|
|
|
|
}
|
|
|
|
else if (nwritten != -EINTR)
|
|
|
|
{
|
|
|
|
return nwritten;
|
|
|
|
}
|
2018-03-30 02:36:03 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 12:57:16 +01:00
|
|
|
return OK;
|
2018-03-30 02:36:03 +02:00
|
|
|
}
|
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
int btuart_open(FAR 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;
|
|
|
|
|
|
|
|
/* Drain any cached Rx data */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
lower->rxdrain(lower);
|
2018-03-30 02:36:03 +02:00
|
|
|
|
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
|
|
|
}
|
2022-10-01 21:20:15 +02:00
|
|
|
|
|
|
|
void btuart_close(FAR struct bt_driver_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct btuart_upperhalf_s *upper;
|
|
|
|
FAR const struct btuart_lowerhalf_s *lower;
|
|
|
|
|
|
|
|
upper = (FAR struct btuart_upperhalf_s *)dev;
|
|
|
|
DEBUGASSERT(upper != NULL && upper->lower != NULL);
|
|
|
|
lower = upper->lower;
|
|
|
|
|
|
|
|
/* Disable Rx callbacks */
|
|
|
|
|
|
|
|
lower->rxenable(lower, false);
|
|
|
|
|
|
|
|
/* Detach the Rx event handler */
|
|
|
|
|
|
|
|
lower->rxattach(lower, NULL, NULL);
|
|
|
|
}
|
2022-10-01 21:34:17 +02:00
|
|
|
|
|
|
|
int btuart_ioctl(FAR struct bt_driver_s *dev,
|
|
|
|
int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct btuart_upperhalf_s *upper;
|
|
|
|
FAR const struct btuart_lowerhalf_s *lower;
|
|
|
|
|
|
|
|
upper = (FAR struct btuart_upperhalf_s *)dev;
|
|
|
|
DEBUGASSERT(upper != NULL && upper->lower != NULL);
|
|
|
|
lower = upper->lower;
|
|
|
|
|
|
|
|
if (lower->ioctl)
|
|
|
|
{
|
|
|
|
return lower->ioctl(lower, cmd, arg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
}
|