2018-03-29 20:35:41 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* wireless/bluetooth/bt_hcicore.c
|
2018-03-29 20:35:41 +02:00
|
|
|
*
|
2024-09-11 09:04:25 +02:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2018-03-29 20:35:41 +02:00
|
|
|
* Copyright (c) 2016, Intel Corporation
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2020-03-29 19:57:53 +02:00
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
2018-03-29 20:35:41 +02:00
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
2020-03-29 19:57:53 +02: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-29 20:35:41 +02:00
|
|
|
*
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its
|
2020-03-29 19:57:53 +02:00
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
2018-03-29 20:35:41 +02:00
|
|
|
*
|
2020-03-29 19:57:53 +02:00
|
|
|
* 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 OWNER 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
|
2018-03-29 20:35:41 +02:00
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
2018-04-14 23:09:12 +02:00
|
|
|
#include <time.h>
|
2018-04-15 01:55:30 +02:00
|
|
|
#include <sched.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2018-03-29 20:35:41 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2023-02-02 13:31:22 +01:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
2018-04-14 23:09:12 +02:00
|
|
|
#include <nuttx/clock.h>
|
2018-03-29 20:35:41 +02:00
|
|
|
#include <nuttx/kthread.h>
|
2021-05-14 04:03:23 +02:00
|
|
|
#include <nuttx/spinlock.h>
|
2018-04-19 16:52:09 +02:00
|
|
|
#include <nuttx/wqueue.h>
|
2018-04-01 23:21:58 +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>
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
#include "bt_queue.h"
|
2018-04-01 20:25:30 +02:00
|
|
|
#include "bt_buf.h"
|
2018-03-29 20:35:41 +02:00
|
|
|
#include "bt_keys.h"
|
|
|
|
#include "bt_conn.h"
|
|
|
|
#include "bt_l2cap.h"
|
2018-04-01 20:25:30 +02:00
|
|
|
#include "bt_hcicore.h"
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-04-14 23:09:12 +02:00
|
|
|
/* Wait up to 2.5 seconds for a response. This delay is arbitrary and
|
|
|
|
* intended only to avoid hangs while waiting for a response. It may need
|
|
|
|
* to be adjusted.
|
|
|
|
*/
|
|
|
|
|
2022-05-10 03:32:31 +02:00
|
|
|
#define TIMEOUT_MSEC 2500
|
2018-04-14 23:09:12 +02:00
|
|
|
|
2018-03-29 20:35:41 +02:00
|
|
|
/****************************************************************************
|
2018-04-19 16:52:09 +02:00
|
|
|
* Public Data
|
2018-03-29 20:35:41 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2018-04-19 16:52:09 +02:00
|
|
|
/* State of connected HCI device.
|
|
|
|
*
|
|
|
|
* NOTE: Because this is a global singleton, multiple HCI devices may not
|
|
|
|
* be supported.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct bt_dev_s g_btdev;
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-09-20 21:45:09 +02:00
|
|
|
#ifdef CONFIG_WIRELESS_BLUETOOTH_HOST
|
2018-03-29 20:35:41 +02:00
|
|
|
static FAR struct bt_conn_cb_s *g_callback_list;
|
|
|
|
static bt_le_scan_cb_t *g_scan_dev_found_cb;
|
2020-09-20 21:45:09 +02:00
|
|
|
#else
|
|
|
|
static struct bt_hci_cb_s *g_hci_cb;
|
|
|
|
#endif
|
2018-03-29 20:35:41 +02:00
|
|
|
|
2018-04-19 16:52:09 +02:00
|
|
|
/* Lists of pending received messages. One for low priority input that is
|
|
|
|
* processed on the low priority work queue and one for high priority
|
|
|
|
* input that is processed on high priority work queue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static FAR struct bt_bufferlist_s g_lp_rxlist;
|
|
|
|
static FAR struct bt_bufferlist_s g_hp_rxlist;
|
|
|
|
|
|
|
|
/* Work structures: One for high priority and one for low priority work */
|
|
|
|
|
|
|
|
static struct work_s g_lp_work;
|
|
|
|
static struct work_s g_hp_work;
|
|
|
|
|
2018-03-29 20:35:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-04-19 16:52:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_enqueue_bufwork
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add the provided buffer 'buf' to the head selected buffer list 'list'
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* list - The buffer list to use
|
|
|
|
* buf - The buffer to be added to the head of the buffer list
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void bt_enqueue_bufwork(FAR struct bt_bufferlist_s *list,
|
|
|
|
FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
flags = spin_lock_irqsave(NULL);
|
2018-04-19 16:52:09 +02:00
|
|
|
buf->flink = list->head;
|
|
|
|
if (list->head == NULL)
|
|
|
|
{
|
|
|
|
list->tail = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->head = buf;
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
spin_unlock_irqrestore(NULL, flags);
|
2018-04-19 16:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_dequeue_bufwork
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Remove and return the buffer at the tail of the buffer list specified
|
|
|
|
* by 'list'.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* list - The buffer list to use
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* A pointer to the buffer that was at the tail of the buffer list. NULL
|
|
|
|
* is returned if the list was empty.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
static FAR struct bt_buf_s *
|
|
|
|
bt_dequeue_bufwork(FAR struct bt_bufferlist_s *list)
|
2018-04-19 16:52:09 +02:00
|
|
|
{
|
|
|
|
FAR struct bt_buf_s *buf;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
flags = spin_lock_irqsave(NULL);
|
2018-04-19 16:52:09 +02:00
|
|
|
buf = list->tail;
|
|
|
|
if (buf != NULL)
|
|
|
|
{
|
|
|
|
if (list->head == list->tail)
|
|
|
|
{
|
|
|
|
list->head = NULL;
|
|
|
|
list->tail = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct bt_buf_s *prev;
|
|
|
|
|
|
|
|
for (prev = list->head;
|
|
|
|
prev && prev->flink != buf;
|
|
|
|
prev = prev->flink)
|
2019-09-21 15:16:37 +02:00
|
|
|
{
|
|
|
|
}
|
2018-04-19 16:52:09 +02:00
|
|
|
|
|
|
|
if (prev != NULL)
|
|
|
|
{
|
|
|
|
prev->flink = NULL;
|
|
|
|
list->tail = prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->flink = NULL;
|
|
|
|
}
|
|
|
|
|
arch, boards, drivers, include, sched, wireless: Change spinlock APIs.
Summary:
- This commit changes spinlock APIs (spin_lock_irqsave/spin_unlock_irqrestore)
- In the previous implementation, the global spinlock (i.e. g_irq_spin) was used.
- This commit allows to use caller specific spinlock but also supports to use
g_irq_spin for backword compatibility (In this case, NULL must be specified)
Impact:
- None
Testing:
- Tested with the following configurations
- spresnse:wifi, spresense:wifi_smp
- esp32-devkitc:smp (QEMU), sabre6-quad:smp (QEMU)
- maxi-bit:smp (QEMU), sim:smp
- stm32f4discovery:wifi
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2021-02-08 01:21:26 +01:00
|
|
|
spin_unlock_irqrestore(NULL, flags);
|
2018-04-19 16:52:09 +02:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2020-09-20 21:45:09 +02:00
|
|
|
#ifdef CONFIG_WIRELESS_BLUETOOTH_HOST
|
2018-03-29 20:35:41 +02:00
|
|
|
static void bt_connected(FAR struct bt_conn_s *conn)
|
|
|
|
{
|
|
|
|
FAR struct bt_conn_cb_s *cb;
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
for (cb = g_callback_list; cb; cb = cb->flink)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
if (cb->connected)
|
|
|
|
{
|
2018-04-01 20:25:30 +02:00
|
|
|
cb->connected(conn, cb->context);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bt_disconnected(FAR struct bt_conn_s *conn)
|
|
|
|
{
|
|
|
|
FAR struct bt_conn_cb_s *cb;
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
for (cb = g_callback_list; cb; cb = cb->flink)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
if (cb->disconnected)
|
|
|
|
{
|
2018-04-01 20:25:30 +02:00
|
|
|
cb->disconnected(conn, cb->context);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_acl(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_acl_hdr_s *hdr = (FAR void *)buf->data;
|
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
uint16_t handle;
|
|
|
|
uint16_t len = BT_LE162HOST(hdr->len);
|
|
|
|
uint8_t flags;
|
|
|
|
|
|
|
|
wlinfo("buf %p\n", buf);
|
|
|
|
|
|
|
|
handle = BT_LE162HOST(hdr->handle);
|
|
|
|
flags = (handle >> 12);
|
|
|
|
buf->u.acl.handle = bt_acl_handle(handle);
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_consume(buf, sizeof(*hdr));
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
wlinfo("handle %u len %u flags %u\n", buf->u.acl.handle, len, flags);
|
|
|
|
|
|
|
|
if (buf->len != len)
|
|
|
|
{
|
2020-03-29 19:57:53 +02:00
|
|
|
wlerr("ERROR: ACL data length mismatch (%u != %u)\n",
|
|
|
|
buf->len, len);
|
2018-03-29 20:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn = bt_conn_lookup_handle(buf->u.acl.handle);
|
|
|
|
if (!conn)
|
|
|
|
{
|
2020-03-29 19:57:53 +02:00
|
|
|
wlerr("ERROR: Unable to find conn for handle %u\n",
|
|
|
|
buf->u.acl.handle);
|
2018-03-29 20:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_conn_receive(conn, buf, flags);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* HCI event processing */
|
|
|
|
|
|
|
|
static void hci_encrypt_change(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_evt_encrypt_change_s *evt = (FAR void *)buf->data;
|
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
uint16_t handle = BT_LE162HOST(evt->handle);
|
|
|
|
|
|
|
|
wlinfo("status %u handle %u encrypt 0x%02x\n",
|
|
|
|
evt->status, handle, evt->encrypt);
|
|
|
|
|
|
|
|
if (evt->status)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn = bt_conn_lookup_handle(handle);
|
|
|
|
if (!conn)
|
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Unable to look up conn with handle %u\n", handle);
|
2018-03-29 20:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->encrypt = evt->encrypt;
|
|
|
|
|
|
|
|
bt_l2cap_encrypt_change(conn);
|
2018-03-31 22:55:03 +02:00
|
|
|
bt_conn_release(conn);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_reset_complete(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
uint8_t status = buf->data[0];
|
|
|
|
|
|
|
|
wlinfo("status %u\n", status);
|
|
|
|
|
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_scan_dev_found_cb = NULL;
|
|
|
|
g_btdev.scan_enable = BT_LE_SCAN_DISABLE;
|
|
|
|
g_btdev.scan_filter = BT_LE_SCAN_FILTER_DUP_ENABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_cmd_done(uint16_t opcode, uint8_t status,
|
|
|
|
FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_buf_s *sent = g_btdev.sent_cmd;
|
|
|
|
|
2018-04-04 01:57:06 +02:00
|
|
|
if (sent == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-21 01:57:06 +01:00
|
|
|
if (g_btdev.sent_cmd == NULL)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Request cmd missing!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-29 20:35:41 +02:00
|
|
|
if (g_btdev.sent_cmd->u.hci.opcode != opcode)
|
|
|
|
{
|
2024-02-19 10:22:27 +01:00
|
|
|
wlerr("ERROR: Unexpected completion of opcode 0x%04x "
|
2020-12-21 01:57:06 +01:00
|
|
|
"expected 0x%04x\n",
|
|
|
|
opcode, g_btdev.sent_cmd->u.hci.opcode);
|
2018-03-29 20:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_btdev.sent_cmd = NULL;
|
|
|
|
|
|
|
|
/* If the command was synchronous wake up bt_hci_cmd_send_sync() */
|
|
|
|
|
2018-04-04 00:28:22 +02:00
|
|
|
if (sent->u.hci.sync != NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
FAR sem_t *sem = sent->u.hci.sync;
|
|
|
|
|
2018-04-06 22:07:03 +02:00
|
|
|
if (status != 0)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-18 20:46:47 +02:00
|
|
|
wlwarn("WARNING: status %u\n", status);
|
2018-03-29 20:35:41 +02:00
|
|
|
sent->u.hci.sync = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-01 20:25:30 +02:00
|
|
|
sent->u.hci.sync = bt_buf_addref(buf);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nxsem_post(sem);
|
|
|
|
}
|
2020-12-21 01:57:06 +01:00
|
|
|
|
|
|
|
bt_buf_release(sent);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_cmd_complete(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct hci_evt_cmd_complete_s *evt = (FAR void *)buf->data;
|
|
|
|
uint16_t opcode = BT_LE162HOST(evt->opcode);
|
|
|
|
FAR uint8_t *status;
|
|
|
|
|
2018-04-04 01:23:54 +02:00
|
|
|
wlinfo("opcode %04x\n", opcode);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_consume(buf, sizeof(*evt));
|
2018-03-29 20:35:41 +02:00
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
/* All command return parameters have a 1-byte status in the beginning, so
|
|
|
|
* we can safely make this generalization.
|
2018-03-29 20:35:41 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
status = buf->data;
|
|
|
|
|
|
|
|
switch (opcode)
|
|
|
|
{
|
|
|
|
case BT_HCI_OP_RESET:
|
|
|
|
hci_reset_complete(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-04-18 20:46:47 +02:00
|
|
|
wlinfo("Unhandled opcode %04x\n", opcode);
|
2018-03-29 20:35:41 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hci_cmd_done(opcode, *status, buf);
|
|
|
|
|
2018-04-04 01:23:54 +02:00
|
|
|
if (evt->ncmd > 0 && g_btdev.ncmd == 0)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
/* Allow next command to be sent */
|
|
|
|
|
|
|
|
g_btdev.ncmd = 1;
|
|
|
|
nxsem_post(&g_btdev.ncmd_sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_cmd_status(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_evt_cmd_status_s *evt = (FAR void *)buf->data;
|
|
|
|
uint16_t opcode = BT_LE162HOST(evt->opcode);
|
|
|
|
|
2018-04-18 20:46:47 +02:00
|
|
|
wlinfo("opcode %04x\n", opcode);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_consume(buf, sizeof(*evt));
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
switch (opcode)
|
|
|
|
{
|
|
|
|
default:
|
2018-04-18 20:46:47 +02:00
|
|
|
wlinfo("Unhandled opcode %04x\n", opcode);
|
2018-03-29 20:35:41 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hci_cmd_done(opcode, evt->status, buf);
|
|
|
|
|
|
|
|
if (evt->ncmd && !g_btdev.ncmd)
|
|
|
|
{
|
|
|
|
/* Allow next command to be sent */
|
|
|
|
|
|
|
|
g_btdev.ncmd = 1;
|
|
|
|
nxsem_post(&g_btdev.ncmd_sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_num_completed_packets(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_evt_num_completed_packets_s *evt = (FAR void *)buf->data;
|
2019-09-21 15:16:37 +02:00
|
|
|
uint16_t num_handles = BT_LE162HOST(evt->num_handles);
|
|
|
|
uint16_t i;
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
wlinfo("num_handles %u\n", num_handles);
|
|
|
|
|
|
|
|
for (i = 0; i < num_handles; i++)
|
|
|
|
{
|
2018-04-13 18:36:23 +02:00
|
|
|
uint16_t handle;
|
|
|
|
uint16_t count;
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
handle = BT_LE162HOST(evt->h[i].handle);
|
|
|
|
count = BT_LE162HOST(evt->h[i].count);
|
|
|
|
|
|
|
|
wlinfo("handle %u count %u\n", handle, count);
|
2018-04-13 18:36:23 +02:00
|
|
|
UNUSED(handle);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
while (count--)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_post(&g_btdev.le_pkts_sem);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_encrypt_key_refresh_complete(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
2020-03-29 19:57:53 +02:00
|
|
|
FAR struct bt_hci_evt_encrypt_key_refresh_complete_s *evt =
|
|
|
|
(FAR void *)buf->data;
|
2018-03-29 20:35:41 +02:00
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
uint16_t handle;
|
|
|
|
|
|
|
|
handle = BT_LE162HOST(evt->handle);
|
|
|
|
|
|
|
|
wlinfo("status %u handle %u\n", evt->status, handle);
|
|
|
|
|
|
|
|
if (evt->status)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn = bt_conn_lookup_handle(handle);
|
|
|
|
if (!conn)
|
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Unable to look up conn with handle %u\n", handle);
|
2018-03-29 20:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_l2cap_encrypt_change(conn);
|
2018-03-31 22:55:03 +02:00
|
|
|
bt_conn_release(conn);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void copy_id_addr(FAR struct bt_conn_s *conn,
|
|
|
|
FAR const bt_addr_le_t *addr)
|
|
|
|
{
|
|
|
|
FAR struct bt_keys_s *keys;
|
|
|
|
|
|
|
|
/* If we have a keys struct we already know the identity */
|
|
|
|
|
|
|
|
if (conn->keys)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
keys = bt_keys_find_irk(addr);
|
|
|
|
if (keys)
|
|
|
|
{
|
|
|
|
bt_addr_le_copy(&conn->dst, &keys->addr);
|
|
|
|
conn->keys = keys;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bt_addr_le_copy(&conn->dst, addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bt_hci_start_scanning(uint8_t scan_type, uint8_t scan_filter)
|
|
|
|
{
|
|
|
|
FAR struct bt_buf_s *buf;
|
|
|
|
FAR struct bt_buf_s *rsp;
|
|
|
|
FAR struct bt_hci_cp_le_set_scan_params_s *set_param;
|
|
|
|
FAR struct bt_hci_cp_le_set_scan_enable_s *scan_enable;
|
2018-04-14 17:23:08 +02:00
|
|
|
int ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_PARAMS, sizeof(*set_param));
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
set_param = bt_buf_extend(buf, sizeof(*set_param));
|
2018-03-29 20:35:41 +02:00
|
|
|
memset(set_param, 0, sizeof(*set_param));
|
|
|
|
set_param->scan_type = scan_type;
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
/* for the rest parameters apply default values according to spec 4.2,
|
|
|
|
* vol2, part E, 7.8.10
|
2018-03-29 20:35:41 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
set_param->interval = BT_HOST2LE16(0x0010);
|
|
|
|
set_param->window = BT_HOST2LE16(0x0010);
|
|
|
|
set_param->filter_policy = 0x00;
|
|
|
|
set_param->addr_type = 0x00;
|
|
|
|
|
|
|
|
bt_hci_cmd_send(BT_HCI_OP_LE_SET_SCAN_PARAMS, buf);
|
2020-03-29 19:57:53 +02:00
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_ENABLE,
|
|
|
|
sizeof(*scan_enable));
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
scan_enable = bt_buf_extend(buf, sizeof(*scan_enable));
|
2018-03-29 20:35:41 +02:00
|
|
|
memset(scan_enable, 0, sizeof(*scan_enable));
|
|
|
|
scan_enable->filter_dup = scan_filter;
|
|
|
|
scan_enable->enable = BT_LE_SCAN_ENABLE;
|
|
|
|
|
2018-04-14 17:23:08 +02:00
|
|
|
ret = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_SCAN_ENABLE, buf, &rsp);
|
|
|
|
if (ret < 0)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: bt_hci_cmd_send_sync failed: %d\n", ret);
|
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update scan state in case of success (0) status */
|
|
|
|
|
2018-04-14 17:23:08 +02:00
|
|
|
ret = rsp->data[0];
|
|
|
|
if (!ret)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
g_btdev.scan_enable = BT_LE_SCAN_ENABLE;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_release(rsp);
|
2018-04-14 17:23:08 +02:00
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int bt_hci_stop_scanning(void)
|
|
|
|
{
|
2019-09-21 15:16:37 +02:00
|
|
|
FAR struct bt_buf_s *buf;
|
|
|
|
FAR struct bt_buf_s *rsp;
|
2018-03-29 20:35:41 +02:00
|
|
|
FAR struct bt_hci_cp_le_set_scan_enable_s *scan_enable;
|
2018-04-14 17:23:08 +02:00
|
|
|
int ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
if (g_btdev.scan_enable == BT_LE_SCAN_DISABLE)
|
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlwarn("WARNING: Scan already disabled\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_ENABLE,
|
|
|
|
sizeof(*scan_enable));
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
scan_enable = bt_buf_extend(buf, sizeof(*scan_enable));
|
2018-03-29 20:35:41 +02:00
|
|
|
memset(scan_enable, 0x0, sizeof(*scan_enable));
|
|
|
|
scan_enable->filter_dup = 0x00;
|
|
|
|
scan_enable->enable = BT_LE_SCAN_DISABLE;
|
|
|
|
|
2018-04-14 17:23:08 +02:00
|
|
|
ret = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_SCAN_ENABLE, buf, &rsp);
|
|
|
|
if (ret < 0)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: bt_hci_cmd_send_sync failed: %d\n", ret);
|
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update scan state in case of success (0) status */
|
|
|
|
|
2018-04-14 17:23:08 +02:00
|
|
|
ret = rsp->data[0];
|
|
|
|
if (!ret)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
g_btdev.scan_enable = BT_LE_SCAN_DISABLE;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_release(rsp);
|
2018-04-14 17:23:08 +02:00
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int hci_le_create_conn(FAR const bt_addr_le_t *addr)
|
|
|
|
{
|
|
|
|
FAR struct bt_buf_s *buf;
|
|
|
|
FAR struct bt_hci_cp_le_create_conn_s *cp;
|
|
|
|
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CREATE_CONN, sizeof(*cp));
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
cp = bt_buf_extend(buf, sizeof(*cp));
|
2018-03-29 20:35:41 +02:00
|
|
|
memset(cp, 0x0, sizeof(*cp));
|
|
|
|
bt_addr_le_copy(&cp->peer_addr, addr);
|
|
|
|
cp->conn_interval_max = BT_HOST2LE16(0x0028);
|
|
|
|
cp->conn_interval_min = BT_HOST2LE16(0x0018);
|
|
|
|
cp->scan_interval = BT_HOST2LE16(0x0060);
|
|
|
|
cp->scan_window = BT_HOST2LE16(0x0030);
|
2019-09-21 15:16:37 +02:00
|
|
|
cp->supervision_timeout = BT_HOST2LE16(0x07d0);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CREATE_CONN, buf, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_disconn_complete(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_evt_disconn_complete_s *evt = (FAR void *)buf->data;
|
|
|
|
uint16_t handle = BT_LE162HOST(evt->handle);
|
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
wlinfo("status %u handle %u reason %u\n",
|
|
|
|
evt->status, handle, evt->reason);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
if (evt->status)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn = bt_conn_lookup_handle(handle);
|
|
|
|
if (!conn)
|
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Unable to look up conn with handle %u\n", handle);
|
2018-03-29 20:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_l2cap_disconnected(conn);
|
|
|
|
bt_disconnected(conn);
|
|
|
|
|
|
|
|
bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
|
|
|
|
conn->handle = 0;
|
|
|
|
|
|
|
|
if (bt_atomic_testbit(conn->flags, BT_CONN_AUTO_CONNECT))
|
|
|
|
{
|
|
|
|
bt_conn_set_state(conn, BT_CONN_CONNECT_SCAN);
|
|
|
|
bt_le_scan_update();
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
bt_conn_release(conn);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
if (g_btdev.adv_enable)
|
|
|
|
{
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_ENABLE, 1);
|
|
|
|
if (buf)
|
|
|
|
{
|
2018-04-01 20:25:30 +02:00
|
|
|
memcpy(bt_buf_extend(buf, 1), &g_btdev.adv_enable, 1);
|
2018-03-29 20:35:41 +02:00
|
|
|
bt_hci_cmd_send(BT_HCI_OP_LE_SET_ADV_ENABLE, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void le_conn_complete(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_evt_le_conn_complete_s *evt = (FAR void *)buf->data;
|
|
|
|
uint16_t handle = BT_LE162HOST(evt->handle);
|
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
FAR struct bt_keys_s *keys;
|
|
|
|
|
|
|
|
wlinfo("status %u handle %u role %u %s\n", evt->status, handle,
|
|
|
|
evt->role, bt_addr_le_str(&evt->peer_addr));
|
|
|
|
|
|
|
|
/* Make lookup to check if there's a connection object in CONNECT state
|
|
|
|
* associated with passed peer LE address.
|
|
|
|
*/
|
|
|
|
|
|
|
|
keys = bt_keys_find_irk(&evt->peer_addr);
|
|
|
|
if (keys)
|
|
|
|
{
|
|
|
|
conn = bt_conn_lookup_state(&keys->addr, BT_CONN_CONNECT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
conn = bt_conn_lookup_state(&evt->peer_addr, BT_CONN_CONNECT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (evt->status)
|
|
|
|
{
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
/* Drop the reference got by lookup call in CONNECT state. We are now
|
|
|
|
* in the DISCONNECTED state since no successful LE link been made.
|
2018-03-29 20:35:41 +02:00
|
|
|
*/
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
bt_conn_release(conn);
|
2018-03-29 20:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
conn = bt_conn_add(&evt->peer_addr, evt->role);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!conn)
|
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Unable to add new conn for handle %u\n", handle);
|
2018-03-29 20:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->handle = handle;
|
|
|
|
conn->src.type = BT_ADDR_LE_PUBLIC;
|
|
|
|
memcpy(conn->src.val, g_btdev.bdaddr.val, sizeof(g_btdev.bdaddr.val));
|
|
|
|
copy_id_addr(conn, &evt->peer_addr);
|
|
|
|
conn->le_conn_interval = BT_LE162HOST(evt->interval);
|
|
|
|
|
|
|
|
bt_conn_set_state(conn, BT_CONN_CONNECTED);
|
|
|
|
|
|
|
|
bt_l2cap_connected(conn);
|
|
|
|
|
|
|
|
if (evt->role == BT_HCI_ROLE_SLAVE)
|
|
|
|
{
|
|
|
|
bt_l2cap_update_conn_param(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_connected(conn);
|
2018-03-31 22:55:03 +02:00
|
|
|
bt_conn_release(conn);
|
2018-03-29 20:35:41 +02:00
|
|
|
bt_le_scan_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_pending_conn(FAR const bt_addr_le_t *addr, uint8_t evtype,
|
|
|
|
FAR struct bt_keys_s *keys)
|
|
|
|
{
|
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
|
|
|
|
/* Return if event is not connectible */
|
|
|
|
|
|
|
|
if (evtype != BT_LE_ADV_IND && evtype != BT_LE_ADV_DIRECT_IND)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keys)
|
|
|
|
{
|
|
|
|
conn = bt_conn_lookup_state(&keys->addr, BT_CONN_CONNECT_SCAN);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
conn = bt_conn_lookup_state(addr, BT_CONN_CONNECT_SCAN);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bt_hci_stop_scanning())
|
|
|
|
{
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hci_le_create_conn(addr))
|
|
|
|
{
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_conn_set_state(conn, BT_CONN_CONNECT);
|
|
|
|
|
|
|
|
done:
|
2018-03-31 22:55:03 +02:00
|
|
|
bt_conn_release(conn);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void le_adv_report(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
2020-10-29 03:47:58 +01:00
|
|
|
FAR struct bt_hci_ev_le_advertising_report_s *info;
|
2018-03-29 20:35:41 +02:00
|
|
|
uint8_t num_reports = buf->data[0];
|
|
|
|
|
|
|
|
wlinfo("Adv number of reports %u\n", num_reports);
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
info = bt_buf_consume(buf, sizeof(num_reports));
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
while (num_reports--)
|
|
|
|
{
|
|
|
|
int8_t rssi = info->data[info->length];
|
|
|
|
FAR struct bt_keys_s *keys;
|
|
|
|
bt_addr_le_t addr;
|
|
|
|
|
|
|
|
wlinfo("%s event %u, len %u, rssi %d dBm\n",
|
|
|
|
bt_addr_le_str(&info->addr), info->evt_type, info->length,
|
|
|
|
rssi);
|
|
|
|
|
|
|
|
keys = bt_keys_find_irk(&info->addr);
|
|
|
|
if (keys)
|
|
|
|
{
|
|
|
|
bt_addr_le_copy(&addr, &keys->addr);
|
|
|
|
wlinfo("Identity %s matched RPA %s\n",
|
|
|
|
bt_addr_le_str(&keys->addr), bt_addr_le_str(&info->addr));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bt_addr_le_copy(&addr, &info->addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_scan_dev_found_cb)
|
|
|
|
{
|
|
|
|
g_scan_dev_found_cb(&addr, rssi, info->evt_type,
|
|
|
|
info->data, info->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
check_pending_conn(&info->addr, info->evt_type, keys);
|
|
|
|
|
|
|
|
/* Get next report iteration by moving pointer to right offset in buf
|
|
|
|
* according to spec 4.2, Vol 2, Part E, 7.7.65.2.
|
2020-10-29 03:47:58 +01:00
|
|
|
*
|
|
|
|
* TODO: multiple reports are stored as multiple arrays not one array
|
|
|
|
* of structs. If num_reports > 0 this will not WORK!
|
2018-03-29 20:35:41 +02:00
|
|
|
*/
|
|
|
|
|
2020-10-29 03:47:58 +01:00
|
|
|
/* Note that info already contains one byte which accounts for RSSI */
|
|
|
|
|
|
|
|
info = bt_buf_consume(buf, sizeof(*info) + info->length);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void le_ltk_request(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_evt_le_ltk_request_s *evt = (FAR void *)buf->data;
|
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
uint16_t handle;
|
|
|
|
|
|
|
|
handle = BT_LE162HOST(evt->handle);
|
|
|
|
|
|
|
|
wlinfo("handle %u\n", handle);
|
|
|
|
|
|
|
|
conn = bt_conn_lookup_handle(handle);
|
|
|
|
if (!conn)
|
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Unable to lookup conn for handle %u\n", handle);
|
2018-03-29 20:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!conn->keys)
|
|
|
|
{
|
|
|
|
conn->keys = bt_keys_find(BT_KEYS_SLAVE_LTK, &conn->dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conn->keys && (conn->keys->keys & BT_KEYS_SLAVE_LTK) &&
|
|
|
|
conn->keys->slave_ltk.rand == evt->rand &&
|
|
|
|
conn->keys->slave_ltk.ediv == evt->ediv)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_cp_le_ltk_req_reply_s *cp;
|
|
|
|
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_LE_LTK_REQ_REPLY, sizeof(*cp));
|
|
|
|
if (!buf)
|
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Out of command buffers\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
cp = bt_buf_extend(buf, sizeof(*cp));
|
2018-03-29 20:35:41 +02:00
|
|
|
cp->handle = evt->handle;
|
|
|
|
memcpy(cp->ltk, conn->keys->slave_ltk.val, 16);
|
|
|
|
|
|
|
|
bt_hci_cmd_send(BT_HCI_OP_LE_LTK_REQ_REPLY, buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_cp_le_ltk_req_neg_reply_s *cp;
|
|
|
|
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_LE_LTK_REQ_NEG_REPLY, sizeof(*cp));
|
|
|
|
if (!buf)
|
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Out of command buffers\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
cp = bt_buf_extend(buf, sizeof(*cp));
|
2018-03-29 20:35:41 +02:00
|
|
|
cp->handle = evt->handle;
|
|
|
|
|
|
|
|
bt_hci_cmd_send(BT_HCI_OP_LE_LTK_REQ_NEG_REPLY, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2018-03-31 22:55:03 +02:00
|
|
|
bt_conn_release(conn);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_le_meta_event(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_evt_le_meta_event_s *evt = (FAR void *)buf->data;
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_consume(buf, sizeof(*evt));
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
switch (evt->subevent)
|
|
|
|
{
|
|
|
|
case BT_HCI_EVT_LE_CONN_COMPLETE:
|
|
|
|
le_conn_complete(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_HCI_EVT_LE_ADVERTISING_REPORT:
|
|
|
|
le_adv_report(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_HCI_EVT_LE_LTK_REQUEST:
|
|
|
|
le_ltk_request(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-04-18 20:46:47 +02:00
|
|
|
wlinfo("Unhandled LE event %04x\n", evt->subevent);
|
2018-03-29 20:35:41 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_event(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_evt_hdr_s *hdr = (FAR void *)buf->data;
|
|
|
|
|
|
|
|
wlinfo("event %u\n", hdr->evt);
|
|
|
|
|
2018-04-04 01:23:54 +02:00
|
|
|
bt_buf_consume(buf, sizeof(struct bt_hci_evt_hdr_s));
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
switch (hdr->evt)
|
|
|
|
{
|
|
|
|
case BT_HCI_EVT_DISCONN_COMPLETE:
|
|
|
|
hci_disconn_complete(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_HCI_EVT_ENCRYPT_CHANGE:
|
|
|
|
hci_encrypt_change(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_HCI_EVT_ENCRYPT_KEY_REFRESH_COMPLETE:
|
|
|
|
hci_encrypt_key_refresh_complete(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_HCI_EVT_LE_META_EVENT:
|
|
|
|
hci_le_meta_event(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-04-14 17:23:08 +02:00
|
|
|
wlwarn("WARNING: Unhandled event 0x%02x\n", hdr->evt);
|
2018-03-29 20:35:41 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-09-20 21:45:09 +02:00
|
|
|
#endif
|
2018-03-29 20:35:41 +02:00
|
|
|
|
2018-04-19 16:52:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: hci_tx_kthread
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is a kernel thread that handles sending of commands.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* Standard kernel thread arguments
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Doesn't normally return.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-29 20:35:41 +02:00
|
|
|
static int hci_tx_kthread(int argc, FAR char *argv[])
|
|
|
|
{
|
2021-05-18 15:45:01 +02:00
|
|
|
FAR struct bt_driver_s *btdev = g_btdev.btdev;
|
2018-03-29 20:35:41 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
wlinfo("started\n");
|
|
|
|
|
2023-05-03 19:05:45 +02:00
|
|
|
for (; g_btdev.tx_status == OK; )
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
FAR struct bt_buf_s *buf;
|
|
|
|
|
|
|
|
/* Wait until ncmd > 0 */
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
ret = nxsem_wait_uninterruptible(&g_btdev.ncmd_sem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
wlerr("nxsem_wait_uninterruptible() failed: %d\n", ret);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
/* Get next command - wait if necessary */
|
|
|
|
|
|
|
|
buf = NULL;
|
2020-12-24 13:53:17 +01:00
|
|
|
ret = bt_queue_receive(&g_btdev.tx_queue, &buf);
|
2018-03-29 20:35:41 +02:00
|
|
|
DEBUGASSERT(ret >= 0 && buf != NULL);
|
|
|
|
UNUSED(ret);
|
|
|
|
|
|
|
|
g_btdev.ncmd = 0;
|
|
|
|
|
|
|
|
/* Clear out any existing sent command */
|
|
|
|
|
|
|
|
if (g_btdev.sent_cmd)
|
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Uncleared pending sent_cmd\n");
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_release(g_btdev.sent_cmd);
|
2018-03-29 20:35:41 +02:00
|
|
|
g_btdev.sent_cmd = NULL;
|
|
|
|
}
|
|
|
|
|
2023-05-03 19:05:45 +02:00
|
|
|
/* Allow transmission if module is connected. */
|
2020-10-25 16:02:55 +01:00
|
|
|
|
2023-05-03 19:05:45 +02:00
|
|
|
if (g_btdev.tx_status == OK)
|
|
|
|
{
|
|
|
|
g_btdev.sent_cmd = bt_buf_addref(buf);
|
|
|
|
|
|
|
|
wlinfo("Sending command %04x buf %p to driver\n",
|
|
|
|
buf->u.hci.opcode, buf);
|
|
|
|
|
|
|
|
bt_send(btdev, buf);
|
|
|
|
}
|
2020-10-25 16:02:55 +01:00
|
|
|
|
2020-12-21 01:57:06 +01:00
|
|
|
bt_buf_release(buf);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
2023-05-03 19:05:45 +02:00
|
|
|
/* Acknowledge the termination request. */
|
|
|
|
|
|
|
|
g_btdev.tx_status = ESHUTDOWN;
|
|
|
|
return EXIT_SUCCESS;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 16:52:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: hci_rx_work
|
|
|
|
*
|
|
|
|
* Description:
|
2018-11-30 13:31:55 +01:00
|
|
|
* This work function operates on the low priority work queue using the
|
|
|
|
* low priority buffer queue.
|
2018-04-19 16:52:09 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - Indicates which buffer queue should be used
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void hci_rx_work(FAR void *arg)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-19 16:52:09 +02:00
|
|
|
FAR struct bt_bufferlist_s *list = (FAR struct bt_bufferlist_s *)arg;
|
2018-03-29 20:35:41 +02:00
|
|
|
FAR struct bt_buf_s *buf;
|
|
|
|
|
2018-04-19 16:52:09 +02:00
|
|
|
wlinfo("list %p\n", list);
|
|
|
|
DEBUGASSERT(list != NULL);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
2018-04-19 16:52:09 +02:00
|
|
|
while ((buf = bt_dequeue_bufwork(list)) != NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
wlinfo("buf %p type %u len %u\n", buf, buf->type, buf->len);
|
|
|
|
|
2020-08-28 23:51:26 +02:00
|
|
|
/* TODO: Hook monitor callback */
|
|
|
|
|
2020-09-20 21:45:09 +02:00
|
|
|
#ifdef CONFIG_WIRELESS_BLUETOOTH_HOST
|
2018-03-29 20:35:41 +02:00
|
|
|
switch (buf->type)
|
|
|
|
{
|
|
|
|
case BT_ACL_IN:
|
|
|
|
hci_acl(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_EVT:
|
|
|
|
hci_event(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Unknown buf type %u\n", buf->type);
|
2018-03-29 20:35:41 +02:00
|
|
|
break;
|
|
|
|
}
|
2020-09-20 21:45:09 +02:00
|
|
|
#else
|
|
|
|
g_hci_cb->received(buf, g_hci_cb->context);
|
|
|
|
#endif
|
2020-12-21 01:57:06 +01:00
|
|
|
bt_buf_release(buf);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 13:31:55 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: priority_rx_work
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This work function operates on the high priority work thread using the
|
|
|
|
* high priority buffer queue.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - Indicates which buffer queue should be used
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void priority_rx_work(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct bt_bufferlist_s *list = (FAR struct bt_bufferlist_s *)arg;
|
|
|
|
FAR struct bt_buf_s *buf;
|
|
|
|
|
|
|
|
wlinfo("list %p\n", list);
|
|
|
|
DEBUGASSERT(list != NULL);
|
|
|
|
|
|
|
|
while ((buf = bt_dequeue_bufwork(list)) != NULL)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_evt_hdr_s *hdr = (FAR void *)buf->data;
|
|
|
|
|
|
|
|
wlinfo("buf %p type %u len %u\n", buf, buf->type, buf->len);
|
|
|
|
|
2020-08-28 23:51:26 +02:00
|
|
|
/* TODO: Hook monitor callback */
|
|
|
|
|
2018-11-30 13:31:55 +01:00
|
|
|
if (buf->type != BT_EVT)
|
|
|
|
{
|
|
|
|
wlerr("Unknown buf type %u\n", buf->type);
|
|
|
|
bt_buf_release(buf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-20 21:45:09 +02:00
|
|
|
#ifdef CONFIG_WIRELESS_BLUETOOTH_HOST
|
2018-11-30 13:31:55 +01:00
|
|
|
bt_buf_consume(buf, sizeof(struct bt_hci_evt_hdr_s));
|
|
|
|
|
|
|
|
switch (hdr->evt)
|
|
|
|
{
|
|
|
|
case BT_HCI_EVT_CMD_COMPLETE:
|
|
|
|
hci_cmd_complete(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_HCI_EVT_CMD_STATUS:
|
|
|
|
hci_cmd_status(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_HCI_EVT_NUM_COMPLETED_PACKETS:
|
|
|
|
hci_num_completed_packets(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
wlerr("Unknown event 0x%02x\n", hdr->evt);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-20 21:45:09 +02:00
|
|
|
#else
|
|
|
|
UNUSED(hdr);
|
2018-11-30 13:31:55 +01:00
|
|
|
|
2020-09-20 21:45:09 +02:00
|
|
|
g_hci_cb->received(buf, g_hci_cb->context);
|
|
|
|
#endif
|
2020-12-21 01:57:06 +01:00
|
|
|
bt_buf_release(buf);
|
2018-11-30 13:31:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 21:45:09 +02:00
|
|
|
#ifdef CONFIG_WIRELESS_BLUETOOTH_HOST
|
2018-03-29 20:35:41 +02:00
|
|
|
static void read_local_features_complete(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_rp_read_local_features_s *rp = (FAR void *)buf->data;
|
|
|
|
|
|
|
|
wlinfo("status %u\n", rp->status);
|
|
|
|
|
|
|
|
memcpy(g_btdev.features, rp->features, sizeof(g_btdev.features));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_local_ver_complete(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_rp_read_local_version_info_s *rp = (FAR void *)buf->data;
|
|
|
|
|
|
|
|
wlinfo("status %u\n", rp->status);
|
|
|
|
|
|
|
|
g_btdev.hci_version = rp->hci_version;
|
|
|
|
g_btdev.hci_revision = BT_LE162HOST(rp->hci_revision);
|
|
|
|
g_btdev.manufacturer = BT_LE162HOST(rp->manufacturer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_bdaddr_complete(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_rp_read_bd_addr_s *rp = (FAR void *)buf->data;
|
|
|
|
|
|
|
|
wlinfo("status %u\n", rp->status);
|
|
|
|
|
|
|
|
bt_addr_copy(&g_btdev.bdaddr, &rp->bdaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_le_features_complete(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_rp_le_read_local_features_s *rp = (FAR void *)buf->data;
|
|
|
|
|
|
|
|
wlinfo("status %u\n", rp->status);
|
|
|
|
|
|
|
|
memcpy(g_btdev.le_features, rp->features, sizeof(g_btdev.le_features));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_buffer_size_complete(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_rp_read_buffer_size_s *rp = (FAR void *)buf->data;
|
|
|
|
|
|
|
|
wlinfo("status %u\n", rp->status);
|
|
|
|
|
|
|
|
/* If LE-side has buffers we can ignore the BR/EDR values */
|
|
|
|
|
|
|
|
if (g_btdev.le_mtu)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_btdev.le_mtu = BT_LE162HOST(rp->acl_max_len);
|
|
|
|
g_btdev.le_pkts = BT_LE162HOST(rp->acl_max_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void le_read_buffer_size_complete(FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
FAR struct bt_hci_rp_le_read_buffer_size_s *rp = (FAR void *)buf->data;
|
|
|
|
|
|
|
|
wlinfo("status %u\n", rp->status);
|
|
|
|
|
|
|
|
g_btdev.le_mtu = BT_LE162HOST(rp->le_max_len);
|
|
|
|
g_btdev.le_pkts = rp->le_max_num;
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:16:37 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: hci_initialize()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* none
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-04-04 00:28:22 +02:00
|
|
|
static int hci_initialize(void)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
FAR struct bt_hci_cp_host_buffer_size_s *hbs;
|
|
|
|
FAR struct bt_hci_cp_set_event_mask_s *ev;
|
|
|
|
FAR struct bt_buf_s *buf;
|
|
|
|
FAR struct bt_buf_s *rsp;
|
|
|
|
FAR uint8_t *enable;
|
2018-04-14 17:23:08 +02:00
|
|
|
int ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
/* Send HCI_RESET */
|
|
|
|
|
2019-09-21 15:16:37 +02:00
|
|
|
ret = bt_hci_cmd_send_sync(BT_HCI_OP_RESET, NULL, &rsp);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: BT_HCI_OP_RESET failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_buf_release(rsp);
|
|
|
|
|
2018-03-29 20:35:41 +02:00
|
|
|
/* Read Local Supported Features */
|
|
|
|
|
2018-04-14 17:23:08 +02:00
|
|
|
ret = bt_hci_cmd_send_sync(BT_HCI_OP_READ_LOCAL_FEATURES, NULL, &rsp);
|
|
|
|
if (ret < 0)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2019-09-21 15:16:37 +02:00
|
|
|
wlerr("ERROR: BT_HCI_OP_READ_LOCAL_FEATURES failed: %d\n", ret);
|
2018-04-14 17:23:08 +02:00
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
read_local_features_complete(rsp);
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_release(rsp);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
/* Read Local Version Information */
|
|
|
|
|
2018-04-14 17:23:08 +02:00
|
|
|
ret = bt_hci_cmd_send_sync(BT_HCI_OP_READ_LOCAL_VERSION_INFO, NULL, &rsp);
|
|
|
|
if (ret < 0)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: bt_hci_cmd_send_sync failed: %d\n", ret);
|
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
read_local_ver_complete(rsp);
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_release(rsp);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
/* Read Bluetooth Address */
|
|
|
|
|
2018-04-14 17:23:08 +02:00
|
|
|
ret = bt_hci_cmd_send_sync(BT_HCI_OP_READ_BD_ADDR, NULL, &rsp);
|
|
|
|
if (ret < 0)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: bt_hci_cmd_send_sync failed: %d\n", ret);
|
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
read_bdaddr_complete(rsp);
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_release(rsp);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
/* For now we only support LE capable controllers */
|
|
|
|
|
|
|
|
if (!lmp_le_capable(g_btdev))
|
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Non-LE capable controller detected!\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read Low Energy Supported Features */
|
|
|
|
|
2018-04-14 17:23:08 +02:00
|
|
|
ret = bt_hci_cmd_send_sync(BT_HCI_OP_LE_READ_LOCAL_FEATURES, NULL, &rsp);
|
|
|
|
if (ret < 0)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2019-09-21 15:16:37 +02:00
|
|
|
wlerr("ERROR: BT_HCI_OP_LE_READ_LOCAL_FEATURES failed: %d\n", ret);
|
2018-04-14 17:23:08 +02:00
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
read_le_features_complete(rsp);
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_release(rsp);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
/* Read LE Buffer Size */
|
|
|
|
|
2018-04-14 17:23:08 +02:00
|
|
|
ret = bt_hci_cmd_send_sync(BT_HCI_OP_LE_READ_BUFFER_SIZE, NULL, &rsp);
|
|
|
|
if (ret < 0)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2019-09-21 15:16:37 +02:00
|
|
|
wlerr("ERROR: BT_HCI_OP_LE_READ_BUFFER_SIZE failed: %d\n", ret);
|
2018-04-14 17:23:08 +02:00
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
le_read_buffer_size_complete(rsp);
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_release(rsp);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_SET_EVENT_MASK, sizeof(*ev));
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
ev = bt_buf_extend(buf, sizeof(*ev));
|
2018-03-29 20:35:41 +02:00
|
|
|
memset(ev, 0, sizeof(*ev));
|
2019-09-21 15:16:37 +02:00
|
|
|
|
2018-03-29 20:35:41 +02:00
|
|
|
ev->events[0] |= 0x10; /* Disconnection Complete */
|
|
|
|
ev->events[1] |= 0x08; /* Read Remote Version Information Complete */
|
|
|
|
ev->events[1] |= 0x20; /* Command Complete */
|
|
|
|
ev->events[1] |= 0x40; /* Command Status */
|
|
|
|
ev->events[1] |= 0x80; /* Hardware Error */
|
|
|
|
ev->events[2] |= 0x04; /* Number of Completed Packets */
|
|
|
|
ev->events[3] |= 0x02; /* Data Buffer Overflow */
|
|
|
|
ev->events[7] |= 0x20; /* LE Meta-Event */
|
|
|
|
|
|
|
|
if (g_btdev.le_features[0] & BT_HCI_LE_ENCRYPTION)
|
|
|
|
{
|
|
|
|
ev->events[0] |= 0x80; /* Encryption Change */
|
|
|
|
ev->events[5] |= 0x80; /* Encryption Key Refresh Complete */
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_hci_cmd_send_sync(BT_HCI_OP_SET_EVENT_MASK, buf, NULL);
|
|
|
|
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_HOST_BUFFER_SIZE, sizeof(*hbs));
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
hbs = bt_buf_extend(buf, sizeof(*hbs));
|
2018-03-29 20:35:41 +02:00
|
|
|
memset(hbs, 0, sizeof(*hbs));
|
2018-04-01 23:21:58 +02:00
|
|
|
hbs->acl_mtu = BT_HOST2LE16(BLUETOOTH_MAX_FRAMELEN -
|
2018-04-18 20:46:47 +02:00
|
|
|
sizeof(struct bt_hci_acl_hdr_s) -
|
|
|
|
g_btdev.btdev->head_reserve);
|
2018-04-19 16:52:09 +02:00
|
|
|
hbs->acl_pkts = BT_HOST2LE16(CONFIG_BLUETOOTH_BUFFER_PREALLOC);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
2018-04-14 17:23:08 +02:00
|
|
|
ret = bt_hci_cmd_send(BT_HCI_OP_HOST_BUFFER_SIZE, buf);
|
|
|
|
if (ret < 0)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: bt_hci_cmd_send failed: %d\n", ret);
|
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_SET_CTL_TO_HOST_FLOW, 1);
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
enable = bt_buf_extend(buf, sizeof(*enable));
|
2018-03-29 20:35:41 +02:00
|
|
|
*enable = 0x01;
|
|
|
|
|
2018-04-14 17:23:08 +02:00
|
|
|
ret = bt_hci_cmd_send_sync(BT_HCI_OP_SET_CTL_TO_HOST_FLOW, buf, NULL);
|
|
|
|
if (ret < 0)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: bt_hci_cmd_send_sync failed: %d\n", ret);
|
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lmp_bredr_capable(g_btdev))
|
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
FAR struct bt_hci_cp_write_le_host_supp_s *cp;
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
/* Use BR/EDR buffer size if LE reports zero buffers */
|
|
|
|
|
|
|
|
if (!g_btdev.le_mtu)
|
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
ret = bt_hci_cmd_send_sync(BT_HCI_OP_READ_BUFFER_SIZE, NULL, &rsp);
|
|
|
|
if (ret < 0)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: bt_hci_cmd_send_sync failed: %d\n", ret);
|
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
read_buffer_size_complete(rsp);
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_release(rsp);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_LE_WRITE_LE_HOST_SUPP, sizeof(*cp));
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Explicitly enable LE for dual-mode controllers */
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
cp = bt_buf_extend(buf, sizeof *cp);
|
2018-03-29 20:35:41 +02:00
|
|
|
cp->le = 0x01;
|
|
|
|
cp->simul = 0x00;
|
|
|
|
|
|
|
|
bt_hci_cmd_send_sync(BT_HCI_OP_LE_WRITE_LE_HOST_SUPP, buf, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
wlinfo("HCI ver %u rev %u, manufacturer %u\n", g_btdev.hci_version,
|
|
|
|
g_btdev.hci_revision, g_btdev.manufacturer);
|
|
|
|
wlinfo("ACL buffers: pkts %u mtu %u\n", g_btdev.le_pkts, g_btdev.le_mtu);
|
|
|
|
|
|
|
|
/* Initialize & prime the semaphore for counting controller-side available
|
|
|
|
* ACL packet buffers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxsem_init(&g_btdev.le_pkts_sem, 0, g_btdev.le_pkts);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-05-03 19:05:45 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_queue_deinit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Threads, fifos and semaphores deinitialization
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* none
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void cmd_queue_deinit(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
FAR struct bt_buf_s *buf;
|
|
|
|
|
|
|
|
/* Tell the tx thread that the module is disconnected.
|
|
|
|
*/
|
|
|
|
|
|
|
|
g_btdev.tx_status = ENOTCONN;
|
|
|
|
|
|
|
|
/* Make sure the thread is not blocked by the semaphore.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxsem_post(&g_btdev.ncmd_sem);
|
|
|
|
|
|
|
|
/* We create and push a packet into the tx queue to unblock the thread.
|
|
|
|
* Any packet will do.
|
|
|
|
*/
|
|
|
|
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_RESET, 0);
|
|
|
|
if (buf)
|
|
|
|
{
|
|
|
|
ret = bt_queue_send(&g_btdev.tx_queue, buf, BT_NORMAL_PRIO);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: bt_queue_send() failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for the tx thread to exit gracefully. */
|
|
|
|
|
|
|
|
while (g_btdev.tx_status == ENOTCONN)
|
|
|
|
{
|
|
|
|
nxsig_usleep(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deinitialization */
|
|
|
|
|
|
|
|
nxsem_destroy(&g_btdev.ncmd_sem);
|
|
|
|
file_mq_close(&g_btdev.tx_queue);
|
|
|
|
work_cancel(HPWORK, &g_hp_work);
|
|
|
|
work_cancel(LPWORK, &g_lp_work);
|
|
|
|
}
|
2020-09-20 21:45:09 +02:00
|
|
|
#endif
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
/* threads, fifos and semaphores initialization */
|
|
|
|
|
|
|
|
static void cmd_queue_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
2023-10-09 22:15:18 +02:00
|
|
|
#ifdef CONFIG_BLUETOOTH_TXCMD_PINNED_TO_CORE
|
|
|
|
cpu_set_t cpuset;
|
|
|
|
#endif
|
|
|
|
int pid;
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
/* When there is a command to be sent to the Bluetooth driver, it queued on
|
|
|
|
* the Tx queue and received by logic on the Tx kernel thread.
|
|
|
|
*/
|
|
|
|
|
2023-09-13 14:37:19 +02:00
|
|
|
ret = bt_queue_open(BT_HCI_TX, O_RDWR | O_CREAT | O_CLOEXEC,
|
2018-04-04 01:23:54 +02:00
|
|
|
CONFIG_BLUETOOTH_TXCMD_NMSGS, &g_btdev.tx_queue);
|
2020-12-24 13:53:17 +01:00
|
|
|
DEBUGASSERT(ret >= 0);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
nxsem_init(&g_btdev.ncmd_sem, 0, 1);
|
|
|
|
|
|
|
|
g_btdev.ncmd = 1;
|
2023-05-03 19:05:45 +02:00
|
|
|
g_btdev.tx_status = OK;
|
2023-10-09 22:15:18 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_BLUETOOTH_TXCMD_PINNED_TO_CORE
|
|
|
|
sched_lock();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pid = kthread_create("BT HCI Tx", CONFIG_BLUETOOTH_TXCMD_PRIORITY,
|
2018-03-29 20:35:41 +02:00
|
|
|
CONFIG_BLUETOOTH_TXCMD_STACKSIZE,
|
|
|
|
hci_tx_kthread, NULL);
|
2023-10-09 22:15:18 +02:00
|
|
|
DEBUGASSERT(pid > 0);
|
|
|
|
|
|
|
|
#ifdef CONFIG_BLUETOOTH_TXCMD_PINNED_TO_CORE
|
|
|
|
CPU_ZERO(&cpuset);
|
|
|
|
CPU_SET((CONFIG_BLUETOOTH_TXCMD_CORE - 1), &cpuset);
|
|
|
|
ret = nxsched_set_affinity(pid, sizeof(cpuset), &cpuset);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
wlerr("Failed to set affinity error=%d\n", ret);
|
|
|
|
DEBUGPANIC();
|
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
#endif
|
|
|
|
|
2022-03-21 23:47:09 +01:00
|
|
|
UNUSED(ret);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-12-21 01:57:06 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_send
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send the provided buffer to the bluetooth driver
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* btdev - An instance of the low-level drivers interface structure.
|
|
|
|
* buf - The buffer to be sent by the driver
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success; a negated errno value is returned on any
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
int bt_send(FAR struct bt_driver_s *btdev,
|
2020-12-21 01:57:06 +01:00
|
|
|
FAR struct bt_buf_s *buf)
|
|
|
|
{
|
|
|
|
/* Send to driver */
|
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
return btdev->send(btdev, buf->type, buf->data, buf->len);
|
2020-12-21 01:57:06 +01:00
|
|
|
}
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize Bluetooth. Must be the called before anything else.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on success or (negative) error code otherwise.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int bt_initialize(void)
|
|
|
|
{
|
2021-05-18 15:45:01 +02:00
|
|
|
FAR struct bt_driver_s *btdev = g_btdev.btdev;
|
2018-04-14 17:23:08 +02:00
|
|
|
int ret;
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2018-04-17 16:30:16 +02:00
|
|
|
wlinfo("btdev %p\n", btdev);
|
|
|
|
|
2023-05-03 19:05:45 +02:00
|
|
|
#ifdef CONFIG_WIRELESS_BLUETOOTH_HOST
|
|
|
|
g_callback_list = NULL;
|
|
|
|
g_scan_dev_found_cb = NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memset(&g_lp_rxlist, 0, sizeof(g_lp_rxlist));
|
|
|
|
memset(&g_hp_rxlist, 0, sizeof(g_hp_rxlist));
|
|
|
|
|
2018-04-14 19:33:29 +02:00
|
|
|
DEBUGASSERT(btdev != NULL);
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_initialize();
|
2018-03-31 22:55:03 +02:00
|
|
|
|
|
|
|
cmd_queue_init();
|
|
|
|
|
2018-04-14 19:33:29 +02:00
|
|
|
ret = btdev->open(btdev);
|
2018-04-14 17:23:08 +02:00
|
|
|
if (ret < 0)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
2023-05-03 19:05:45 +02:00
|
|
|
#ifdef CONFIG_WIRELESS_BLUETOOTH_HOST
|
|
|
|
cmd_queue_deinit();
|
|
|
|
#endif
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: HCI driver open failed (%d)\n", ret);
|
|
|
|
return ret;
|
2018-03-31 22:55:03 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 21:45:09 +02:00
|
|
|
#ifdef CONFIG_WIRELESS_BLUETOOTH_HOST
|
2018-04-14 17:23:08 +02:00
|
|
|
ret = hci_initialize();
|
|
|
|
if (ret < 0)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
2023-05-03 19:05:45 +02:00
|
|
|
cmd_queue_deinit();
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: hci_initialize failed: %d\n", ret);
|
|
|
|
return ret;
|
2018-03-31 22:55:03 +02:00
|
|
|
}
|
2018-03-29 20:35:41 +02:00
|
|
|
|
2020-09-20 21:45:09 +02:00
|
|
|
ret = bt_l2cap_init();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
2018-03-31 22:55:03 +02:00
|
|
|
}
|
|
|
|
|
2023-05-03 19:05:45 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_deinitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Deinitialize Bluetooth.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on success or (negative) error code otherwise.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int bt_deinitialize(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_WIRELESS_BLUETOOTH_HOST
|
|
|
|
FAR struct bt_driver_s *btdev = g_btdev.btdev;
|
|
|
|
|
|
|
|
cmd_queue_deinit();
|
|
|
|
|
|
|
|
/* Call the bluetooth HCI driver's close function if available. */
|
|
|
|
|
|
|
|
if (btdev)
|
|
|
|
{
|
|
|
|
if (btdev->close)
|
|
|
|
{
|
|
|
|
btdev->close(btdev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_driver_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the Bluetooth low-level driver with the Bluetooth stack.
|
|
|
|
* This is called from the low-level driver and is part of the driver
|
2018-04-29 20:43:09 +02:00
|
|
|
* interface prototyped in include/nuttx/wireless/bluetooth/bt_driver.h
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
2018-04-14 17:23:08 +02:00
|
|
|
* This function associates the Bluetooth driver with the Bluetooth stack.
|
|
|
|
*
|
2018-03-31 22:55:03 +02:00
|
|
|
* Input Parameters:
|
2018-04-14 19:33:29 +02:00
|
|
|
* btdev - An instance of the low-level drivers interface structure.
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success; a negated errno value is returned on any
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
int bt_driver_register(FAR struct bt_driver_s *btdev)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
2018-04-14 19:33:29 +02:00
|
|
|
DEBUGASSERT(btdev != NULL && btdev->open != NULL && btdev->send != NULL);
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2018-04-14 19:33:29 +02:00
|
|
|
if (g_btdev.btdev != NULL)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlwarn("WARNING: Already registered\n");
|
2018-03-31 22:55:03 +02:00
|
|
|
return -EALREADY;
|
|
|
|
}
|
|
|
|
|
2023-05-03 19:05:45 +02:00
|
|
|
memset(&g_btdev, 0, sizeof(g_btdev));
|
|
|
|
|
2018-04-14 19:33:29 +02:00
|
|
|
g_btdev.btdev = btdev;
|
2018-03-31 22:55:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_driver_unregister
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unregister a Bluetooth low-level driver previously registered with
|
|
|
|
* bt_driver_register. This may be called from the low-level driver and
|
|
|
|
* is part of the driver interface prototyped in
|
2018-04-29 20:43:09 +02:00
|
|
|
* include/nuttx/wireless/bluetooth/bt_driver.h
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-04-14 19:33:29 +02:00
|
|
|
* btdev - An instance of the low-level drivers interface structure.
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
void bt_driver_unregister(FAR struct bt_driver_s *btdev)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
2018-04-14 19:33:29 +02:00
|
|
|
g_btdev.btdev = NULL;
|
2018-03-31 22:55:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2021-05-18 15:45:01 +02:00
|
|
|
* Name: bt_receive
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called by the Bluetooth low-level driver when new data is received from
|
|
|
|
* the radio. This may be called from the low-level driver and is part of
|
2020-03-29 19:57:53 +02:00
|
|
|
* the driver interface prototyped in
|
|
|
|
* include/nuttx/wireless/bluetooth/bt_driver.h
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
2018-04-19 16:52:09 +02:00
|
|
|
* NOTE: This function will defer all real work to the low or to the high
|
|
|
|
* priority work queues. Therefore, this function may safely be called
|
|
|
|
* from interrupt handling logic.
|
|
|
|
*
|
2018-03-31 22:55:03 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* buf - An instance of the buffer structure providing the received frame.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
int bt_receive(FAR struct bt_driver_s *btdev, enum bt_buf_type_e type,
|
|
|
|
FAR void *data, size_t len)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
FAR struct bt_hci_evt_hdr_s *hdr;
|
2021-05-18 15:45:01 +02:00
|
|
|
struct bt_buf_s *buf;
|
2018-03-29 20:35:41 +02:00
|
|
|
int ret;
|
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
wlinfo("data %p len %zu\n", data, len);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
2018-04-19 16:52:09 +02:00
|
|
|
/* Critical command complete/status events use the high priority work
|
|
|
|
* queue.
|
|
|
|
*/
|
|
|
|
|
2021-12-18 13:49:37 +01:00
|
|
|
buf = bt_buf_alloc(type, NULL, BLUETOOTH_H4_HDRLEN);
|
2021-05-18 15:45:01 +02:00
|
|
|
if (buf == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2021-05-18 15:45:01 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(bt_buf_extend(buf, len), data, len);
|
|
|
|
|
|
|
|
if (type != BT_ACL_IN)
|
|
|
|
{
|
|
|
|
if (type != BT_EVT)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
wlerr("ERROR: Invalid buf type %u\n", buf->type);
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_release(buf);
|
2021-05-18 15:45:01 +02:00
|
|
|
return -EINVAL;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Command Complete/Status events use high priority messages. */
|
|
|
|
|
|
|
|
hdr = (FAR void *)buf->data;
|
|
|
|
if (hdr->evt == BT_HCI_EVT_CMD_COMPLETE ||
|
|
|
|
hdr->evt == BT_HCI_EVT_CMD_STATUS ||
|
|
|
|
hdr->evt == BT_HCI_EVT_NUM_COMPLETED_PACKETS)
|
|
|
|
{
|
2018-04-19 16:52:09 +02:00
|
|
|
/* Add the buffer to the high priority Rx buffer list */
|
|
|
|
|
|
|
|
bt_enqueue_bufwork(&g_hp_rxlist, buf);
|
|
|
|
|
|
|
|
/* If there is already pending work, then do nothing. Otherwise,
|
|
|
|
* schedule processing of the Rx buffer list on the high priority
|
|
|
|
* work queue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (work_available(&g_hp_work))
|
|
|
|
{
|
2018-11-30 13:31:55 +01:00
|
|
|
ret = work_queue(HPWORK, &g_hp_work, priority_rx_work,
|
2018-04-19 16:52:09 +02:00
|
|
|
&g_hp_rxlist, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Failed to schedule HPWORK: %d\n", ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 15:45:01 +02:00
|
|
|
return OK;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 16:52:09 +02:00
|
|
|
/* All others use the low priority work queue */
|
2019-09-21 15:16:37 +02:00
|
|
|
|
2018-04-19 16:52:09 +02:00
|
|
|
/* Add the buffer to the low priority Rx buffer list */
|
|
|
|
|
|
|
|
bt_enqueue_bufwork(&g_lp_rxlist, buf);
|
|
|
|
|
|
|
|
/* If there is already pending work, then do nothing. Otherwise, schedule
|
|
|
|
* processing of the Rx buffer list on the low priority work queue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (work_available(&g_lp_work))
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-19 16:52:09 +02:00
|
|
|
ret = work_queue(LPWORK, &g_lp_work, hci_rx_work, &g_lp_rxlist, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Failed to schedule LPWORK: %d\n", ret);
|
|
|
|
}
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
2021-05-18 15:45:01 +02:00
|
|
|
|
|
|
|
return OK;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 21:45:09 +02:00
|
|
|
#ifdef CONFIG_WIRELESS_BLUETOOTH_HOST
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_hci_cmd_create
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate and initialize a buffer for a command
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* A reference to the allocated buffer. NULL could possibly be returned
|
|
|
|
* on any failure to allocate.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct bt_buf_s *bt_hci_cmd_create(uint16_t opcode, uint8_t param_len)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-03-31 22:55:03 +02:00
|
|
|
FAR struct bt_hci_cmd_hdr_s *hdr;
|
|
|
|
FAR struct bt_buf_s *buf;
|
2018-03-29 20:35:41 +02:00
|
|
|
|
2018-04-18 20:46:47 +02:00
|
|
|
wlinfo("opcode %04x param_len %u\n", opcode, param_len);
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2018-04-14 19:33:29 +02:00
|
|
|
buf = bt_buf_alloc(BT_CMD, NULL, g_btdev.btdev->head_reserve);
|
2018-03-31 22:55:03 +02:00
|
|
|
if (!buf)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-03-31 22:55:03 +02:00
|
|
|
wlerr("ERROR: Cannot get free buffer\n");
|
|
|
|
return NULL;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
wlinfo("buf %p\n", buf);
|
|
|
|
|
|
|
|
buf->u.hci.opcode = opcode;
|
|
|
|
buf->u.hci.sync = NULL;
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
hdr = bt_buf_extend(buf, sizeof(*hdr));
|
2018-03-31 22:55:03 +02:00
|
|
|
hdr->opcode = BT_HOST2LE16(opcode);
|
|
|
|
hdr->param_len = param_len;
|
|
|
|
|
|
|
|
return buf;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
int bt_hci_cmd_send(uint16_t opcode, FAR struct bt_buf_s *buf)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-03-31 22:55:03 +02:00
|
|
|
int ret;
|
2018-04-04 01:23:54 +02:00
|
|
|
|
|
|
|
if (buf == NULL)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
|
|
|
buf = bt_hci_cmd_create(opcode, 0);
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-31 22:55:03 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 20:46:47 +02:00
|
|
|
wlinfo("opcode %04x len %u\n", opcode, buf->len);
|
2018-03-31 22:55:03 +02:00
|
|
|
|
|
|
|
/* Host Number of Completed Packets can ignore the ncmd value and does not
|
|
|
|
* generate any cmd complete/status events.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (opcode == BT_HCI_OP_HOST_NUM_COMPLETED_PACKETS)
|
|
|
|
{
|
2020-08-28 23:51:26 +02:00
|
|
|
bt_send(g_btdev.btdev, buf);
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_release(buf);
|
2018-03-31 22:55:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-24 13:53:17 +01:00
|
|
|
ret = bt_queue_send(&g_btdev.tx_queue, buf, BT_NORMAL_PRIO);
|
2018-03-31 22:55:03 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: bt_queue_send() failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
int bt_hci_cmd_send_sync(uint16_t opcode, FAR struct bt_buf_s *buf,
|
|
|
|
FAR struct bt_buf_s **rsp)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-03-31 22:55:03 +02:00
|
|
|
sem_t sync_sem;
|
|
|
|
int ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
/* NOTE: This function cannot be called from the rx thread since it relies
|
|
|
|
* on the very same thread in processing the cmd_complete event and giving
|
|
|
|
* back the blocking semaphore.
|
|
|
|
*/
|
2018-03-29 20:35:41 +02:00
|
|
|
|
2018-04-04 00:28:22 +02:00
|
|
|
if (buf == NULL)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
|
|
|
buf = bt_hci_cmd_create(opcode, 0);
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-31 22:55:03 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
}
|
2018-03-29 20:35:41 +02:00
|
|
|
|
2018-04-18 20:46:47 +02:00
|
|
|
wlinfo("opcode %04x len %u\n", opcode, buf->len);
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2018-04-14 23:09:12 +02:00
|
|
|
/* Set up for the wait */
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
nxsem_init(&sync_sem, 0, 0);
|
|
|
|
buf->u.hci.sync = &sync_sem;
|
|
|
|
|
2018-04-14 23:09:12 +02:00
|
|
|
/* Send the frame */
|
|
|
|
|
2020-12-24 13:53:17 +01:00
|
|
|
ret = bt_queue_send(&g_btdev.tx_queue, buf, BT_NORMAL_PRIO);
|
2018-03-31 22:55:03 +02:00
|
|
|
if (ret < 0)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-03-31 22:55:03 +02:00
|
|
|
wlerr("ERROR: bt_queue_send() failed: %d\n", ret);
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
2018-04-14 23:09:12 +02:00
|
|
|
else
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 23:09:12 +02:00
|
|
|
/* Wait for the response to the command. An I/O error will be
|
|
|
|
* declared if the response does not occur within the timeout
|
|
|
|
* interval.
|
|
|
|
*
|
2018-04-15 01:55:30 +02:00
|
|
|
* REVISIT: The cause of the timeout could be a failure to receive a
|
|
|
|
* response to a sent frame or, perhaps, a failure to send the frame.
|
|
|
|
* Should there also be logic to flush any unsent Tx packets?
|
2018-04-14 23:09:12 +02:00
|
|
|
*/
|
|
|
|
|
2022-05-10 03:32:31 +02:00
|
|
|
ret = nxsem_tickwait_uninterruptible(&sync_sem,
|
|
|
|
MSEC2TICK(TIMEOUT_MSEC));
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
2018-04-14 23:09:12 +02:00
|
|
|
/* Indicate failure if we failed to get the response */
|
2018-03-31 22:55:03 +02:00
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2018-04-04 00:28:22 +02:00
|
|
|
if (buf->u.hci.sync == NULL)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed get return parameters\n");
|
2018-03-31 22:55:03 +02:00
|
|
|
ret = -EIO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 01:57:06 +01:00
|
|
|
/* Note: if ret < 0 the packet might just be delayed and could still
|
|
|
|
* be sent. We cannot decrease the ref count since it if it was sent
|
|
|
|
* it buf could be pointed a completely different request.
|
|
|
|
*/
|
|
|
|
|
2018-04-14 23:09:12 +02:00
|
|
|
if (rsp != NULL)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
2020-12-21 01:57:06 +01:00
|
|
|
/* If the response is expected provide the sync response */
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
*rsp = buf->u.hci.sync;
|
|
|
|
}
|
2018-04-18 20:46:47 +02:00
|
|
|
else if (buf->u.hci.sync != NULL)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
2020-12-21 01:57:06 +01:00
|
|
|
/* If a sync response was given but not requested drop it */
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
bt_buf_release(buf->u.hci.sync);
|
2018-03-31 22:55:03 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 16:16:37 +01:00
|
|
|
nxsem_destroy(&sync_sem);
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
|
2018-03-30 18:50:44 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_start_advertising
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set advertisement data, scan response data, advertisement parameters
|
|
|
|
* and start advertising.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* type - Advertising type.
|
|
|
|
* ad - Data to be used in advertisement packets.
|
|
|
|
* sd - Data to be used in scan response packets.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on success or (negative) error code otherwise.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-29 20:35:41 +02:00
|
|
|
int bt_start_advertising(uint8_t type, FAR const struct bt_eir_s *ad,
|
|
|
|
FAR const struct bt_eir_s *sd)
|
|
|
|
{
|
|
|
|
FAR struct bt_buf_s *buf;
|
|
|
|
FAR struct bt_hci_cp_le_set_adv_data_s *set_data;
|
|
|
|
FAR struct bt_hci_cp_le_set_adv_data_s *scan_rsp;
|
|
|
|
FAR struct bt_hci_cp_le_set_adv_parameters_s *set_param;
|
|
|
|
int i;
|
|
|
|
|
2018-04-06 17:42:41 +02:00
|
|
|
if (ad == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
goto send_scan_rsp;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_DATA, sizeof(*set_data));
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
set_data = bt_buf_extend(buf, sizeof(*set_data));
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
memset(set_data, 0, sizeof(*set_data));
|
|
|
|
|
2018-04-06 17:42:41 +02:00
|
|
|
for (i = 0; ad[i].len > 0; i++)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
/* Check if ad fit in the remaining buffer */
|
|
|
|
|
|
|
|
if (set_data->len + ad[i].len + 1 > 29)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&set_data->data[set_data->len], &ad[i], ad[i].len + 1);
|
|
|
|
set_data->len += ad[i].len + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_hci_cmd_send(BT_HCI_OP_LE_SET_ADV_DATA, buf);
|
|
|
|
|
|
|
|
send_scan_rsp:
|
2018-04-06 17:42:41 +02:00
|
|
|
if (sd == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
goto send_set_param;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_RSP_DATA,
|
|
|
|
sizeof(*scan_rsp));
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
scan_rsp = bt_buf_extend(buf, sizeof(*scan_rsp));
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
memset(scan_rsp, 0, sizeof(*scan_rsp));
|
|
|
|
|
2018-04-06 17:42:41 +02:00
|
|
|
for (i = 0; sd[i].len > 0; i++)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
|
|
|
/* Check if ad fit in the remaining buffer */
|
|
|
|
|
|
|
|
if (scan_rsp->len + sd[i].len + 1 > 29)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&scan_rsp->data[scan_rsp->len], &sd[i], sd[i].len + 1);
|
|
|
|
scan_rsp->len += sd[i].len + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_hci_cmd_send(BT_HCI_OP_LE_SET_SCAN_RSP_DATA, buf);
|
|
|
|
|
|
|
|
send_set_param:
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_PARAMETERS,
|
|
|
|
sizeof(*set_param));
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:25:30 +02:00
|
|
|
set_param = bt_buf_extend(buf, sizeof(*set_param));
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
memset(set_param, 0, sizeof(*set_param));
|
2020-10-29 03:47:58 +01:00
|
|
|
set_param->min_interval = BT_HOST2LE16(300);
|
|
|
|
set_param->max_interval = BT_HOST2LE16(300);
|
2018-03-29 20:35:41 +02:00
|
|
|
set_param->type = type;
|
|
|
|
set_param->channel_map = 0x07;
|
|
|
|
|
|
|
|
bt_hci_cmd_send(BT_HCI_OP_LE_SET_ADV_PARAMETERS, buf);
|
|
|
|
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_ENABLE, 1);
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_btdev.adv_enable = 0x01;
|
2018-04-01 20:25:30 +02:00
|
|
|
memcpy(bt_buf_extend(buf, 1), &g_btdev.adv_enable, 1);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_ADV_ENABLE, buf, NULL);
|
|
|
|
}
|
|
|
|
|
2018-03-30 18:50:44 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_stop_advertising
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Stops ongoing advertising.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on success or (negative) error code otherwise.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-29 20:35:41 +02:00
|
|
|
int bt_stop_advertising(void)
|
|
|
|
{
|
|
|
|
FAR struct bt_buf_s *buf;
|
|
|
|
|
|
|
|
if (!g_btdev.adv_enable)
|
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlwarn("WARNING: Already advertising\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_ENABLE, 1);
|
2018-04-14 17:23:08 +02:00
|
|
|
if (buf == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlerr("ERROR: Failed to create buffer\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_btdev.adv_enable = 0x00;
|
2018-04-01 20:25:30 +02:00
|
|
|
memcpy(bt_buf_extend(buf, 1), &g_btdev.adv_enable, 1);
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_ADV_ENABLE, buf, NULL);
|
|
|
|
}
|
|
|
|
|
2018-03-30 18:50:44 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_start_scanning
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Start LE scanning with and provide results through the specified
|
|
|
|
* callback.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* filter_dups - Enable duplicate filtering (or not).
|
|
|
|
* cb - Callback to notify scan results.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on success or error code otherwise, positive in case
|
|
|
|
* of protocol error or negative (POSIX) in case of stack internal error
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-29 20:35:41 +02:00
|
|
|
int bt_start_scanning(uint8_t scan_filter, bt_le_scan_cb_t cb)
|
|
|
|
{
|
|
|
|
/* Return if active scan is already enabled */
|
|
|
|
|
|
|
|
if (g_scan_dev_found_cb)
|
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlwarn("WARNING: Already scanning\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_scan_dev_found_cb = cb;
|
|
|
|
g_btdev.scan_filter = scan_filter;
|
|
|
|
|
|
|
|
return bt_le_scan_update();
|
|
|
|
}
|
|
|
|
|
2018-03-30 18:50:44 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_stop_scanning
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Stops ongoing LE scanning.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on success or error code otherwise, positive in case
|
|
|
|
* of protocol error or negative (POSIX) in case of stack internal error
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-29 20:35:41 +02:00
|
|
|
int bt_stop_scanning(void)
|
|
|
|
{
|
|
|
|
/* Return if active scanning is already disabled */
|
|
|
|
|
2018-04-14 17:23:08 +02:00
|
|
|
if (g_scan_dev_found_cb == NULL)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-04-14 17:23:08 +02:00
|
|
|
wlwarn("WARNING: Not scanning\n");
|
2018-03-29 20:35:41 +02:00
|
|
|
return -EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_scan_dev_found_cb = NULL;
|
|
|
|
g_btdev.scan_filter = BT_LE_SCAN_FILTER_DUP_ENABLE;
|
|
|
|
|
|
|
|
return bt_le_scan_update();
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_le_scan_update
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Used to determine whether to start scan and which scan type should be
|
|
|
|
* used.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on success or error code otherwise, positive in case
|
|
|
|
* of protocol error or negative (POSIX) in case of stack internal error
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
int bt_le_scan_update(void)
|
|
|
|
{
|
|
|
|
FAR struct bt_conn_s *conn;
|
2018-03-31 22:55:03 +02:00
|
|
|
int ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
|
|
|
|
if (g_btdev.scan_enable)
|
|
|
|
{
|
|
|
|
if (g_scan_dev_found_cb)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
ret = bt_hci_stop_scanning();
|
|
|
|
if (ret)
|
2018-03-29 20:35:41 +02:00
|
|
|
{
|
2018-03-31 22:55:03 +02:00
|
|
|
return ret;
|
2018-03-29 20:35:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_scan_dev_found_cb)
|
|
|
|
{
|
|
|
|
return bt_hci_start_scanning(BT_LE_SCAN_ACTIVE, g_btdev.scan_filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
conn = bt_conn_lookup_state(BT_ADDR_LE_ANY, BT_CONN_CONNECT_SCAN);
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
bt_conn_release(conn);
|
2018-03-29 20:35:41 +02:00
|
|
|
return bt_hci_start_scanning(BT_LE_SCAN_PASSIVE, g_btdev.scan_filter);
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_conn_cb_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register callbacks to monitor the state of connections.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* cb - Instance of the callback structure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void bt_conn_cb_register(FAR struct bt_conn_cb_s *cb)
|
|
|
|
{
|
2018-04-01 20:25:30 +02:00
|
|
|
cb->flink = g_callback_list;
|
2018-03-31 22:55:03 +02:00
|
|
|
g_callback_list = cb;
|
|
|
|
}
|
|
|
|
|
2018-03-29 20:35:41 +02:00
|
|
|
FAR const char *bt_addr_str(FAR const bt_addr_t *addr)
|
|
|
|
{
|
|
|
|
static char bufs[2][18];
|
|
|
|
static uint8_t cur;
|
|
|
|
FAR char *str;
|
|
|
|
|
|
|
|
str = bufs[cur++];
|
2023-02-02 13:31:22 +01:00
|
|
|
cur %= nitems(bufs);
|
2018-03-29 20:35:41 +02:00
|
|
|
bt_addr_to_str(addr, str, sizeof(bufs[cur]));
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
FAR const char *bt_addr_le_str(FAR const bt_addr_le_t *addr)
|
|
|
|
{
|
|
|
|
static char bufs[2][27];
|
|
|
|
static uint8_t cur;
|
|
|
|
FAR char *str;
|
|
|
|
|
|
|
|
str = bufs[cur++];
|
2023-02-02 13:31:22 +01:00
|
|
|
cur %= nitems(bufs);
|
2018-03-29 20:35:41 +02:00
|
|
|
bt_addr_le_to_str(addr, str, sizeof(bufs[cur]));
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
2020-09-20 21:45:09 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bt_hci_cb_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register callbacks to handle RAW HCI packets
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* cb - Instance of the callback structure.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void bt_hci_cb_register(FAR struct bt_hci_cb_s *cb)
|
|
|
|
{
|
|
|
|
g_hci_cb = cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|