2018-03-30 18:50:44 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* wireless/bluetooth/bt_ioctl.c
|
|
|
|
* Bluetooth network IOCTL handler
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT 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
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/wqueue.h>
|
|
|
|
#include <nuttx/semaphore.h>
|
|
|
|
#include <nuttx/net/netdev.h>
|
2018-04-06 18:39:12 +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_ioctl.h>
|
2018-03-30 18:50:44 +02:00
|
|
|
|
|
|
|
#include "bt_hcicore.h"
|
2018-04-03 01:34:41 +02:00
|
|
|
#include "bt_conn.h"
|
2018-03-30 18:50:44 +02:00
|
|
|
#include "bt_ioctl.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDEV_IOCTL /* Not optional! */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-04-19 19:26:36 +02:00
|
|
|
/* These structures encapsulate all globals used by the IOCTL logic. */
|
2018-04-19 19:57:15 +02:00
|
|
|
/* Scan state variables */
|
2018-03-30 18:50:44 +02:00
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
struct btnet_scanstate_s
|
2018-03-30 18:50:44 +02:00
|
|
|
{
|
|
|
|
sem_t bs_exclsem; /* Manages exclusive access */
|
2018-04-19 23:51:40 +02:00
|
|
|
volatile bool bs_scanning; /* True: Scanning in progress */
|
|
|
|
volatile uint8_t bs_head; /* Head of circular list (for removal) */
|
2018-03-30 18:50:44 +02:00
|
|
|
uint8_t bs_tail; /* Tail of circular list (for addition) */
|
|
|
|
|
|
|
|
struct bt_scanresponse_s bs_rsp[CONFIG_BLUETOOTH_MAXSCANRESULT];
|
|
|
|
};
|
|
|
|
|
2018-04-19 19:57:15 +02:00
|
|
|
/* Discovery state variables. NOTE: This function must be cast-compatible
|
|
|
|
* with struct bt_gatt_discover_params_s.
|
|
|
|
*/
|
|
|
|
|
2018-04-19 19:26:36 +02:00
|
|
|
struct btnet_discoverstate_s
|
|
|
|
{
|
2018-04-19 19:57:15 +02:00
|
|
|
struct bt_gatt_discover_params_s bd_params;
|
|
|
|
struct bt_uuid_s bd_uuid; /* Discovery UUID */
|
2018-04-19 19:26:36 +02:00
|
|
|
sem_t bd_exclsem; /* Manages exclusive access */
|
2018-04-19 23:51:40 +02:00
|
|
|
volatile bool bd_discovering; /* True: Discovery in progress */
|
|
|
|
volatile uint8_t bd_head; /* Head of circular list (for removal) */
|
|
|
|
volatile uint8_t bd_tail; /* Tail of circular list (for addition) */
|
2018-04-19 19:26:36 +02:00
|
|
|
|
|
|
|
struct bt_discresonse_s bd_rsp[CONFIG_BLUETOOTH_MAXDISCOVER];
|
|
|
|
};
|
|
|
|
|
2018-04-21 16:38:08 +02:00
|
|
|
/* GATT read state variables. */
|
|
|
|
|
|
|
|
struct btnet_rdstate_s
|
|
|
|
{
|
|
|
|
bool rd_pending; /* True: result not yet received */
|
|
|
|
uint8_t rd_result; /* The result of the read */
|
|
|
|
uint8_t rd_head; /* Start of data to read */
|
|
|
|
uint8_t rd_tail; /* End data to read */
|
|
|
|
uint8_t rd_data[HCI_GATTRD_DATA]; /* Data read */
|
|
|
|
};
|
|
|
|
|
2018-03-30 18:50:44 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* At present only a single Bluetooth device is supported. So we can simply
|
2018-04-22 15:36:39 +02:00
|
|
|
* maintain the pending scan, discovery, MTU exchange, read and write states
|
|
|
|
* as globals.
|
2018-04-19 19:57:15 +02:00
|
|
|
*
|
|
|
|
* NOTE: This limits to a single Bluetooth device with one concurrent scan
|
2018-04-22 15:36:39 +02:00
|
|
|
* action, one concurrent MTU exchange, one concurrent discovery action,
|
|
|
|
* etc.
|
2018-04-21 16:38:08 +02:00
|
|
|
*
|
|
|
|
* REVISIT: A fix might be to (1) allocate instances on each IOCTL command
|
|
|
|
* the starts an operation, keeping the allocated structures in a list. (2)
|
2018-04-22 15:36:39 +02:00
|
|
|
* Return a reference number with each such command that starts an
|
2018-04-22 16:39:18 +02:00
|
|
|
* operation. (3) That reference number would then be used in each IOCTL
|
|
|
|
* command that gets the result of the requested operation. (4) The
|
|
|
|
* allocated instance would be freed when either: (a) the result has been
|
|
|
|
* returned or (b) it has expired without being harvested. This implies
|
2018-04-22 15:36:39 +02:00
|
|
|
* a timer that runs while there are pending operations in order to expire
|
|
|
|
* the unharvested results.
|
2018-03-30 18:50:44 +02:00
|
|
|
*/
|
|
|
|
|
2018-04-19 19:26:36 +02:00
|
|
|
static struct btnet_scanstate_s g_scanstate;
|
|
|
|
static struct btnet_discoverstate_s g_discoverstate;
|
2018-04-20 17:14:59 +02:00
|
|
|
static struct bt_result_s g_exchangeresult;
|
2018-04-21 16:38:08 +02:00
|
|
|
static struct btnet_rdstate_s g_rdstate;
|
|
|
|
static struct bt_result_s g_gattwrresult;
|
2018-03-30 18:50:44 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-03-31 22:55:03 +02:00
|
|
|
* Name: btnet_scan_callback
|
2018-03-30 18:50:44 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is an HCI callback function. HCI provides scan result data via
|
|
|
|
* this callback function. The scan result data will be added to the
|
|
|
|
* cached scan results.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* Scan result data
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
static void btnet_scan_callback(FAR const bt_addr_le_t *addr, int8_t rssi,
|
|
|
|
uint8_t adv_type, FAR const uint8_t *adv_data,
|
|
|
|
uint8_t len)
|
2018-03-30 18:50:44 +02:00
|
|
|
{
|
|
|
|
FAR struct bt_scanresponse_s *rsp;
|
|
|
|
uint8_t nexttail;
|
|
|
|
uint8_t head;
|
|
|
|
uint8_t tail;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!g_scanstate.bs_scanning)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Results received while not scanning\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > CONFIG_BLUETOOTH_MAXSCANDATA)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Scan result is too big: %u\n", len);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get exclusive access to the scan data */
|
|
|
|
|
|
|
|
while ((ret = nxsem_wait(&g_scanstate.bs_exclsem)) < 0)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
|
|
|
|
if (ret != -EINTR)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the scan data to the cache */
|
|
|
|
|
|
|
|
tail = g_scanstate.bs_tail;
|
|
|
|
nexttail = tail + 1;
|
|
|
|
|
|
|
|
if (nexttail >= CONFIG_BLUETOOTH_MAXSCANRESULT)
|
|
|
|
{
|
|
|
|
nexttail = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the circular buffer full? */
|
|
|
|
|
|
|
|
head = g_scanstate.bs_head;
|
|
|
|
if (nexttail == head)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Too many scan results\n");
|
|
|
|
|
|
|
|
if (++head >= CONFIG_BLUETOOTH_MAXSCANRESULT)
|
|
|
|
{
|
|
|
|
head = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_scanstate.bs_head = head;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the new scan result */
|
|
|
|
|
|
|
|
rsp = &g_scanstate.bs_rsp[tail];
|
|
|
|
memcpy(&rsp->sr_addr, addr, sizeof(bt_addr_le_t));
|
|
|
|
rsp->sr_rssi = rssi;
|
|
|
|
rsp->sr_type = adv_type;
|
|
|
|
rsp->sr_len = len;
|
|
|
|
memcpy(&rsp->sr_data, adv_data, len);
|
|
|
|
|
|
|
|
g_scanstate.bs_tail = nexttail;
|
|
|
|
nxsem_post(&g_scanstate.bs_exclsem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-03-31 22:55:03 +02:00
|
|
|
* Name: btnet_scan_result
|
2018-03-30 18:50:44 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2018-04-19 19:26:36 +02:00
|
|
|
* This function implements the SIOCBTSCANGET IOCTL command. It returns
|
|
|
|
* the current, buffered discovered handles.
|
2018-03-30 18:50:44 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-04-06 17:42:41 +02:00
|
|
|
* result - Location to return the scan result data
|
|
|
|
* maxrsp - The maximum number of responses that can be returned.
|
2018-03-30 18:50:44 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-04-06 17:42:41 +02:00
|
|
|
* On success, the actual number of scan results obtain is returned. A
|
|
|
|
* negated errno value is returned on any failure.
|
2018-03-30 18:50:44 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-04-06 17:42:41 +02:00
|
|
|
static int btnet_scan_result(FAR struct bt_scanresponse_s *result,
|
|
|
|
uint8_t maxrsp)
|
2018-03-30 18:50:44 +02:00
|
|
|
{
|
|
|
|
uint8_t head;
|
|
|
|
uint8_t tail;
|
|
|
|
uint8_t nrsp;
|
2018-04-19 23:51:40 +02:00
|
|
|
bool scanning;
|
2018-03-30 18:50:44 +02:00
|
|
|
int ret;
|
|
|
|
|
2018-04-19 23:51:40 +02:00
|
|
|
scanning = g_scanstate.bs_scanning;
|
|
|
|
wlinfo("Scanning? %s\n", scanning ? "YES" : "NO");
|
2018-03-30 18:50:44 +02:00
|
|
|
|
2018-04-19 21:43:21 +02:00
|
|
|
/* Get exclusive access to the scan data while we are actively scanning.
|
|
|
|
* The semaphore is uninitialized in other cases.
|
|
|
|
*/
|
2018-03-30 18:50:44 +02:00
|
|
|
|
2018-04-19 23:51:40 +02:00
|
|
|
if (scanning)
|
2018-04-19 21:43:21 +02:00
|
|
|
{
|
|
|
|
/* Get exclusive access to the scan data */
|
|
|
|
|
|
|
|
ret = nxsem_wait(&g_scanstate.bs_exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-03-30 18:50:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy all available results */
|
|
|
|
|
2018-04-19 19:26:36 +02:00
|
|
|
head = g_scanstate.bs_head;
|
|
|
|
tail = g_scanstate.bs_tail;
|
2018-03-30 18:50:44 +02:00
|
|
|
|
|
|
|
for (nrsp = 0; nrsp < maxrsp && head != tail; nrsp++)
|
|
|
|
{
|
|
|
|
FAR const uint8_t *src;
|
|
|
|
FAR uint8_t *dest;
|
|
|
|
|
|
|
|
/* Copy data from the head index into the user buffer */
|
|
|
|
|
|
|
|
src = (FAR const uint8_t *)&g_scanstate.bs_rsp[head];
|
2018-04-06 17:42:41 +02:00
|
|
|
dest = (FAR uint8_t *)&result[nrsp];
|
2018-03-30 18:50:44 +02:00
|
|
|
memcpy(dest, src, sizeof(struct bt_scanresponse_s));
|
|
|
|
|
|
|
|
/* Increment the head index */
|
|
|
|
|
|
|
|
if (++head >= CONFIG_BLUETOOTH_MAXSCANRESULT)
|
|
|
|
{
|
|
|
|
head = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_scanstate.bs_head = head;
|
2018-04-19 21:43:21 +02:00
|
|
|
|
2018-04-19 23:51:40 +02:00
|
|
|
if (scanning)
|
2018-04-19 21:43:21 +02:00
|
|
|
{
|
|
|
|
nxsem_post(&g_scanstate.bs_exclsem);
|
|
|
|
}
|
|
|
|
|
2018-04-06 17:42:41 +02:00
|
|
|
return nrsp;
|
2018-03-30 18:50:44 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 23:51:40 +02:00
|
|
|
/****************************************************************************
|
2018-04-21 16:38:08 +02:00
|
|
|
* Name: btnet_exchange_rsp
|
2018-04-19 23:51:40 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Result of MTU exchange.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The address of the peer in the MTU exchange
|
|
|
|
* result - The result of the MTU exchange
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-04-21 16:38:08 +02:00
|
|
|
static void btnet_exchange_rsp(FAR struct bt_conn_s *conn, uint8_t result)
|
2018-04-19 23:51:40 +02:00
|
|
|
{
|
|
|
|
wlinfo("Exchange %s\n", result == 0 ? "succeeded" : "failed");
|
2018-04-21 16:38:08 +02:00
|
|
|
g_exchangeresult.br_pending = false;
|
2018-04-20 17:14:59 +02:00
|
|
|
g_exchangeresult.br_result = result;
|
2018-04-19 23:51:40 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 19:26:36 +02:00
|
|
|
/****************************************************************************
|
2018-04-21 16:38:08 +02:00
|
|
|
* Name: btnet_discover_func
|
2018-04-19 19:26:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* GATT discovery callback. This function is called when a new handle is
|
|
|
|
* discovered
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* attr - The discovered attributes
|
|
|
|
* arg - The original discovery parameters
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* BT_GATT_ITER_CONTINUE meaning to continue the iteration.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-04-21 16:38:08 +02:00
|
|
|
static uint8_t btnet_discover_func(FAR const struct bt_gatt_attr_s *attr,
|
|
|
|
FAR void *arg)
|
2018-04-19 19:26:36 +02:00
|
|
|
{
|
|
|
|
uint8_t nexttail;
|
|
|
|
uint8_t head;
|
|
|
|
uint8_t tail;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
wlinfo("Discovered handle %u\n", attr->handle);
|
|
|
|
|
|
|
|
if (!g_discoverstate.bd_discovering)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Results received while not discovering\n");
|
|
|
|
return BT_GATT_ITER_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get exclusive access to the discovered data */
|
|
|
|
|
|
|
|
while ((ret = nxsem_wait(&g_discoverstate.bd_exclsem)) < 0)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
|
|
|
|
if (ret != -EINTR)
|
|
|
|
{
|
|
|
|
return BT_GATT_ITER_STOP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the discovered data to the cache */
|
|
|
|
|
|
|
|
tail = g_discoverstate.bd_tail;
|
|
|
|
nexttail = tail + 1;
|
|
|
|
|
|
|
|
if (nexttail >= CONFIG_BLUETOOTH_MAXSCANRESULT)
|
|
|
|
{
|
|
|
|
nexttail = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the circular buffer full? */
|
|
|
|
|
|
|
|
head = g_discoverstate.bd_head;
|
|
|
|
if (nexttail == head)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Too many handles discovered. Data lost.\n");
|
|
|
|
|
|
|
|
if (++head >= CONFIG_BLUETOOTH_MAXSCANRESULT)
|
|
|
|
{
|
|
|
|
head = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_discoverstate.bd_head = head;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the newly discovered handle */
|
|
|
|
|
|
|
|
g_discoverstate.bd_rsp[tail].dr_handle = attr->handle;
|
|
|
|
g_discoverstate.bd_rsp[tail].dr_perm = attr->perm;
|
|
|
|
g_discoverstate.bd_tail = nexttail;
|
|
|
|
|
|
|
|
nxsem_post(&g_discoverstate.bd_exclsem);
|
|
|
|
return BT_GATT_ITER_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-04-21 16:38:08 +02:00
|
|
|
* Name: btnet_discover_destroy
|
2018-04-19 19:26:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* GATT destroy callback. This function is called when a discovery
|
|
|
|
* completes.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - The original discovery parameters
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-04-21 16:38:08 +02:00
|
|
|
static void btnet_discover_destroy(FAR void *arg)
|
2018-04-19 19:26:36 +02:00
|
|
|
{
|
|
|
|
FAR struct bt_gatt_discover_params *params = arg;
|
|
|
|
|
|
|
|
/* There is nothing that needs to be down here. The parameters were
|
|
|
|
* allocated on the stack and are long gone.
|
|
|
|
*/
|
|
|
|
|
|
|
|
wlinfo("Discover destroy. params %p\n", params);
|
2018-04-19 19:57:15 +02:00
|
|
|
DEBUGASSERT(params != NULL && g_discoverstate.bd_discovering);
|
2018-04-20 00:54:17 +02:00
|
|
|
UNUSED(params);
|
2018-04-19 19:26:36 +02:00
|
|
|
|
2018-04-19 19:57:15 +02:00
|
|
|
memset(&g_discoverstate.bd_params, 0, sizeof(struct btnet_discoverstate_s));
|
2018-04-19 19:26:36 +02:00
|
|
|
nxsem_destroy(&g_discoverstate.bd_exclsem);
|
|
|
|
g_discoverstate.bd_discovering = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btnet_discover_result
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements the SIOCBTDISCGET IOCTL command. It returns
|
|
|
|
* the current, buffered discovered handles.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* result - Location to return the discovery result data
|
|
|
|
* maxrsp - The maximum number of responses that can be returned.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, the actual number of discovery results obtain is returned. A
|
|
|
|
* negated errno value is returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int btnet_discover_result(FAR struct bt_discresonse_s *result,
|
|
|
|
uint8_t maxrsp)
|
|
|
|
{
|
|
|
|
uint8_t head;
|
|
|
|
uint8_t tail;
|
|
|
|
uint8_t nrsp;
|
2018-04-19 23:51:40 +02:00
|
|
|
bool discovering;
|
2018-04-19 19:26:36 +02:00
|
|
|
int ret;
|
|
|
|
|
2018-04-19 23:51:40 +02:00
|
|
|
discovering = g_discoverstate.bd_discovering;
|
|
|
|
wlinfo("Discovering? %s\n", discovering ? "YES" : "NO");
|
2018-04-19 19:26:36 +02:00
|
|
|
|
2018-04-19 21:43:21 +02:00
|
|
|
/* Get exclusive access to the discovery data while we are actively
|
|
|
|
* discovering. The semaphore is uninitialized in other cases.
|
|
|
|
*/
|
2018-04-19 19:26:36 +02:00
|
|
|
|
2018-04-19 23:51:40 +02:00
|
|
|
if (discovering)
|
2018-04-19 21:43:21 +02:00
|
|
|
{
|
|
|
|
ret = nxsem_wait(&g_discoverstate.bd_exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-04-19 19:26:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy all available results */
|
|
|
|
|
|
|
|
head = g_discoverstate.bd_head;
|
|
|
|
tail = g_discoverstate.bd_tail;
|
|
|
|
|
|
|
|
for (nrsp = 0; nrsp < maxrsp && head != tail; nrsp++)
|
|
|
|
{
|
|
|
|
/* Copy data from the head index into the user buffer */
|
|
|
|
|
|
|
|
result[nrsp].dr_handle = g_discoverstate.bd_rsp[head].dr_handle;
|
|
|
|
result[nrsp].dr_perm = g_discoverstate.bd_rsp[head].dr_perm;
|
|
|
|
|
|
|
|
/* Increment the head index */
|
|
|
|
|
|
|
|
if (++head >= CONFIG_BLUETOOTH_MAXDISCOVER)
|
|
|
|
{
|
|
|
|
head = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_discoverstate.bd_head = head;
|
2018-04-19 21:43:21 +02:00
|
|
|
|
2018-04-19 23:51:40 +02:00
|
|
|
if (discovering)
|
2018-04-19 21:43:21 +02:00
|
|
|
{
|
|
|
|
nxsem_post(&g_discoverstate.bd_exclsem);
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:26:36 +02:00
|
|
|
return nrsp;
|
|
|
|
}
|
|
|
|
|
2018-04-21 16:38:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: btnet_read_callback
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle network IOCTL commands directed to this device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - Connection of read
|
|
|
|
* result - The result status of the read.
|
|
|
|
* data - The data read
|
|
|
|
* length - The Number of bytes read
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void btnet_read_callback(FAR struct bt_conn_s *conn, int result,
|
|
|
|
FAR const void *data, uint16_t length)
|
|
|
|
{
|
|
|
|
wlinfo("Read complete: result %d length %u\n", result, length);
|
|
|
|
|
|
|
|
DEBUGASSERT(conn != NULL && g_rdstate.rd_pending);
|
|
|
|
|
|
|
|
if (length > HCI_GATTRD_DATA)
|
|
|
|
{
|
|
|
|
g_rdstate.rd_result = ENFILE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGASSERT((unsigned int)result < UINT8_MAX);
|
|
|
|
|
|
|
|
g_rdstate.rd_result = result;
|
|
|
|
g_rdstate.rd_head = 0;
|
|
|
|
g_rdstate.rd_tail = length;
|
|
|
|
memcpy(g_rdstate.rd_data, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_rdstate.rd_pending = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btnet_read_result
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle network IOCTL commands directed to this device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* btreq - IOCTL command data
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int btnet_read_result(FAR struct btreq_s *btreq)
|
|
|
|
{
|
|
|
|
int head;
|
|
|
|
int rdlen;
|
|
|
|
|
|
|
|
DEBUGASSERT(btreq != NULL && btreq->btr_rddata != NULL);
|
|
|
|
|
|
|
|
/* Is the read complete? */
|
|
|
|
|
|
|
|
btreq->btr_rdpending = g_rdstate.rd_pending;
|
|
|
|
if (!g_rdstate.rd_pending)
|
|
|
|
{
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes... return the read data */
|
|
|
|
|
|
|
|
head = g_rdstate.rd_head;
|
|
|
|
if (head >= g_rdstate.rd_tail)
|
|
|
|
{
|
|
|
|
return -ENODATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
rdlen = btreq->btr_rdsize;
|
|
|
|
if (rdlen + head >= g_rdstate.rd_tail)
|
|
|
|
{
|
|
|
|
rdlen = g_rdstate.rd_tail - head;
|
|
|
|
}
|
|
|
|
|
|
|
|
btreq->btr_rdresult = g_rdstate.rd_result;
|
|
|
|
btreq->btr_rdsize = rdlen;
|
|
|
|
memcpy(btreq->btr_rddata, &g_rdstate.rd_data[head], rdlen);
|
|
|
|
|
|
|
|
g_rdstate.rd_head = head + rdlen;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2018-04-21 16:55:42 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bnet_write_callback
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Result of write operation.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The address of the peer in the MTU exchange
|
|
|
|
* result - The result of the MTU exchange
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void bnet_write_callback(FAR struct bt_conn_s *conn, uint8_t result)
|
|
|
|
{
|
|
|
|
wlinfo("Exchange %s\n", result == 0 ? "succeeded" : "failed");
|
|
|
|
g_gattwrresult.br_pending = false;
|
|
|
|
g_gattwrresult.br_result = result;
|
|
|
|
}
|
|
|
|
|
2018-03-30 18:50:44 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-03-31 22:55:03 +02:00
|
|
|
* Name: btnet_ioctl
|
2018-03-30 18:50:44 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle network IOCTL commands directed to this device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-04-14 19:33:29 +02:00
|
|
|
* netdev - Reference to the NuttX driver state structure
|
|
|
|
* cmd - The IOCTL command
|
|
|
|
* arg - The argument for the IOCTL command
|
2018-03-30 18:50:44 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-04-14 19:33:29 +02:00
|
|
|
int btnet_ioctl(FAR struct net_driver_s *netdev, int cmd, unsigned long arg)
|
2018-03-30 18:50:44 +02:00
|
|
|
{
|
2018-04-06 18:39:12 +02:00
|
|
|
FAR struct btreq_s *btreq = (FAR struct btreq_s *)((uintptr_t)arg);
|
2018-03-30 18:50:44 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
wlinfo("cmd=%04x arg=%ul\n", cmd, arg);
|
2018-04-14 19:33:29 +02:00
|
|
|
DEBUGASSERT(netdev != NULL && netdev->d_private != NULL);
|
2018-03-30 18:50:44 +02:00
|
|
|
|
2018-04-06 18:39:12 +02:00
|
|
|
if (btreq == NULL)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlinfo("ifname: %s\n", btreq->btr_name);
|
|
|
|
|
|
|
|
/* Process the command */
|
|
|
|
|
2018-03-30 18:50:44 +02:00
|
|
|
switch (cmd)
|
|
|
|
{
|
2018-04-06 18:39:12 +02:00
|
|
|
/* SIOCGBTINFO: Get Bluetooth device Info. Given the device name,
|
|
|
|
* fill in the btreq_s structure.
|
|
|
|
*
|
|
|
|
* REVISIT: Little more than a stub at present. It does return the
|
|
|
|
* device address associated with the device name which in itself is
|
|
|
|
* important.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case SIOCGBTINFO:
|
|
|
|
{
|
|
|
|
memset(&btreq->btru.btri, 0, sizeof(btreq->btru.btri));
|
|
|
|
BLUETOOTH_ADDRCOPY(btreq->btr_bdaddr.val, g_btdev.bdaddr.val);
|
|
|
|
btreq->btr_num_cmd = CONFIG_BLUETOOTH_BUFFER_PREALLOC;
|
|
|
|
btreq->btr_num_acl = CONFIG_BLUETOOTH_BUFFER_PREALLOC;
|
|
|
|
btreq->btr_acl_mtu = BLUETOOTH_MAX_MTU;
|
|
|
|
btreq->btr_sco_mtu = BLUETOOTH_MAX_MTU;
|
|
|
|
btreq->btr_max_acl = CONFIG_IOB_NBUFFERS;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-04-07 00:22:49 +02:00
|
|
|
/* SIOCGBTFEAT
|
|
|
|
* Get Bluetooth BR/BDR device Features. This returns the cached
|
|
|
|
* basic (page 0) and extended (page 1 & 2) features. Only page 0
|
|
|
|
* is valid.
|
|
|
|
* SIOCGBTLEFEAT
|
|
|
|
* Get Bluetooth LE device Features. This returns the cached page
|
|
|
|
* 0-2 features. Only page 0 is value.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case SIOCGBTFEAT:
|
|
|
|
case SIOCGBTLEFEAT:
|
|
|
|
{
|
|
|
|
FAR const uint8_t *src;
|
|
|
|
|
|
|
|
memset(&btreq->btru.btrf, 0, sizeof(btreq->btru.btrf));
|
|
|
|
if (cmd == SIOCGBTFEAT)
|
|
|
|
{
|
|
|
|
src = g_btdev.features;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
src = g_btdev.le_features;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(btreq->btr_features0, src, 8);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-04-06 18:39:12 +02:00
|
|
|
/* SIOCBTADVSTART: Set advertisement data, scan response data,
|
|
|
|
* advertisement parameters and start advertising.
|
2018-03-30 18:50:44 +02:00
|
|
|
*/
|
|
|
|
|
2018-04-06 17:42:41 +02:00
|
|
|
case SIOCBTADVSTART:
|
2018-03-30 18:50:44 +02:00
|
|
|
{
|
2018-04-06 18:39:12 +02:00
|
|
|
ret = bt_start_advertising(btreq->btr_advtype,
|
|
|
|
btreq->btr_advad,
|
|
|
|
btreq->btr_advad);
|
|
|
|
wlinfo("Start advertising: %d\n", ret);
|
2018-03-30 18:50:44 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-04-06 18:39:12 +02:00
|
|
|
/* SIOCBTADVSTOP: Stop advertising. */
|
2018-03-30 18:50:44 +02:00
|
|
|
|
2018-04-06 17:42:41 +02:00
|
|
|
case SIOCBTADVSTOP:
|
2018-03-30 18:50:44 +02:00
|
|
|
{
|
|
|
|
wlinfo("Stop advertising\n");
|
|
|
|
bt_stop_advertising();
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-04-06 18:39:12 +02:00
|
|
|
/* SIOCBTSCANSTART: Start LE scanning. Buffered scan results may be
|
|
|
|
* obtained via SIOCBTSCANGET
|
2018-03-30 18:50:44 +02:00
|
|
|
*/
|
|
|
|
|
2018-04-06 17:42:41 +02:00
|
|
|
case SIOCBTSCANSTART:
|
2018-03-30 18:50:44 +02:00
|
|
|
{
|
|
|
|
/* Are we already scanning? */
|
|
|
|
|
2018-04-06 18:39:12 +02:00
|
|
|
if (g_scanstate.bs_scanning)
|
2018-03-30 18:50:44 +02:00
|
|
|
{
|
2018-04-19 19:26:36 +02:00
|
|
|
wlwarn("WARNING: Already scanning\n");
|
2018-03-30 18:50:44 +02:00
|
|
|
ret = -EBUSY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Initialize scan state */
|
|
|
|
|
|
|
|
nxsem_init(&g_scanstate.bs_exclsem, 0, 1);
|
|
|
|
g_scanstate.bs_scanning = true;
|
|
|
|
g_scanstate.bs_head = 0;
|
|
|
|
g_scanstate.bs_tail = 0;
|
|
|
|
|
2018-04-06 17:42:41 +02:00
|
|
|
ret = bt_start_scanning(btreq->btr_dupenable,
|
2018-04-02 21:03:46 +02:00
|
|
|
btnet_scan_callback);
|
2018-03-30 18:50:44 +02:00
|
|
|
wlinfo("Start scan: %d\n", ret);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nxsem_destroy(&g_scanstate.bs_exclsem);
|
|
|
|
g_scanstate.bs_scanning = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-04-06 18:39:12 +02:00
|
|
|
/* SIOCBTSCANGET: Return scan results buffered since the call time
|
|
|
|
* that the SIOCBTSCANGET command was invoked.
|
2018-03-30 18:50:44 +02:00
|
|
|
*/
|
|
|
|
|
2018-04-06 17:42:41 +02:00
|
|
|
case SIOCBTSCANGET:
|
2018-03-30 18:50:44 +02:00
|
|
|
{
|
2018-04-06 18:39:12 +02:00
|
|
|
ret = btnet_scan_result(btreq->btr_rsp, btreq->btr_nrsp);
|
|
|
|
wlinfo("Get scan results: %d\n", ret);
|
2018-03-30 18:50:44 +02:00
|
|
|
|
2018-04-06 18:39:12 +02:00
|
|
|
if (ret >= 0)
|
2018-03-30 18:50:44 +02:00
|
|
|
{
|
2018-04-06 18:39:12 +02:00
|
|
|
btreq->btr_nrsp = ret;
|
|
|
|
ret = OK;
|
2018-03-30 18:50:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-04-06 18:39:12 +02:00
|
|
|
/* SIOCBTSCANSTOP: Stop LE scanning and discard any buffered results. */
|
2018-03-30 18:50:44 +02:00
|
|
|
|
2018-04-06 17:42:41 +02:00
|
|
|
case SIOCBTSCANSTOP:
|
2018-03-30 18:50:44 +02:00
|
|
|
{
|
|
|
|
/* Stop scanning */
|
|
|
|
|
|
|
|
ret = bt_stop_scanning();
|
|
|
|
wlinfo("Stop scanning: %d\n", ret);
|
|
|
|
|
|
|
|
nxsem_destroy(&g_scanstate.bs_exclsem);
|
|
|
|
g_scanstate.bs_scanning = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-04-06 18:39:12 +02:00
|
|
|
/* SIOCBTSECURITY: Enable security for a connection. */
|
2018-04-03 01:34:41 +02:00
|
|
|
|
2018-04-06 17:42:41 +02:00
|
|
|
case SIOCBTSECURITY:
|
2018-04-03 01:34:41 +02:00
|
|
|
{
|
2018-04-06 18:39:12 +02:00
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
|
|
|
|
/* Get the connection associated with the provided LE address */
|
2018-04-03 01:34:41 +02:00
|
|
|
|
2018-04-06 18:39:12 +02:00
|
|
|
conn = bt_conn_lookup_addr_le(&btreq->btr_secaddr);
|
|
|
|
if (conn == NULL)
|
2018-04-03 01:34:41 +02:00
|
|
|
{
|
2018-04-06 18:39:12 +02:00
|
|
|
wlwarn("WARNING: Peer not connected\n");
|
|
|
|
ret = -ENOTCONN;
|
2018-04-03 01:34:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-06 18:39:12 +02:00
|
|
|
ret = bt_conn_security(conn, btreq->btr_seclevel);
|
|
|
|
if (ret < 0)
|
2018-04-03 01:34:41 +02:00
|
|
|
{
|
2018-04-06 18:39:12 +02:00
|
|
|
wlerr("ERROR: Security setting failed: %d\n", ret);
|
2018-04-03 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2018-04-06 18:39:12 +02:00
|
|
|
bt_conn_release(conn);
|
2018-04-03 01:34:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-04-19 23:51:40 +02:00
|
|
|
/* SIOCBTEXCHANGE: Exchange MTUs */
|
|
|
|
|
|
|
|
case SIOCBTEXCHANGE:
|
|
|
|
{
|
|
|
|
/* Check if we are still waiting for the result of the last exchange */
|
|
|
|
|
2018-04-20 17:14:59 +02:00
|
|
|
if (g_exchangeresult.br_pending)
|
2018-04-19 23:51:40 +02:00
|
|
|
{
|
|
|
|
wlwarn("WARNING: Last exchange not yet complete\n");
|
|
|
|
ret = -EBUSY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
|
|
|
|
/* Get the connection associated with the provided LE address */
|
|
|
|
|
|
|
|
conn = bt_conn_lookup_addr_le(&btreq->btr_expeer);
|
|
|
|
if (conn == NULL)
|
|
|
|
{
|
|
|
|
wlwarn("WARNING: Peer not connected\n");
|
|
|
|
ret = -ENOTCONN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-21 16:38:08 +02:00
|
|
|
ret = bt_gatt_exchange_mtu(conn, btnet_exchange_rsp);
|
2018-04-19 23:51:40 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2018-04-20 17:14:59 +02:00
|
|
|
g_exchangeresult.br_pending = true;
|
|
|
|
g_exchangeresult.br_result = EBUSY;
|
2018-04-19 23:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bt_conn_release(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* SIOCBTEXRESULT: Get the result of the MTU exchange */
|
|
|
|
|
|
|
|
case SIOCBTEXRESULT:
|
|
|
|
{
|
2018-04-20 17:14:59 +02:00
|
|
|
btreq->btr_expending = g_exchangeresult.br_pending;
|
|
|
|
btreq->btr_exresult = g_exchangeresult.br_result;
|
2018-04-19 23:51:40 +02:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-04-19 19:26:36 +02:00
|
|
|
/* SIOCBTDISCOVER: Starts GATT discovery */
|
|
|
|
|
|
|
|
case SIOCBTDISCOVER:
|
|
|
|
{
|
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
|
|
|
|
/* Check if discovery is already in progress */
|
|
|
|
|
|
|
|
if (g_discoverstate.bd_discovering)
|
|
|
|
{
|
|
|
|
wlwarn("WARNING: Discovery is already in progress\n");
|
|
|
|
ret = -EBUSY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Get the connection associated with the provided LE address */
|
|
|
|
|
|
|
|
conn = bt_conn_lookup_addr_le(&btreq->btr_dpeer);
|
|
|
|
if (conn == NULL)
|
|
|
|
{
|
|
|
|
wlwarn("WARNING: Peer not connected\n");
|
|
|
|
ret = -ENOTCONN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-19 19:57:15 +02:00
|
|
|
FAR struct bt_gatt_discover_params_s *params;
|
2018-04-19 19:26:36 +02:00
|
|
|
|
|
|
|
/* Set up the query */
|
|
|
|
|
2018-04-19 19:57:15 +02:00
|
|
|
g_discoverstate.bd_uuid.type = BT_UUID_16;
|
|
|
|
g_discoverstate.bd_uuid.u.u16 = btreq->btr_duuid16;
|
|
|
|
|
|
|
|
params = &g_discoverstate.bd_params;
|
|
|
|
params->uuid = &g_discoverstate.bd_uuid;
|
2018-04-21 16:38:08 +02:00
|
|
|
params->func = btnet_discover_func;
|
|
|
|
params->destroy = btnet_discover_destroy;
|
2018-04-19 19:57:15 +02:00
|
|
|
params->start_handle = btreq->btr_dstart;
|
|
|
|
params->end_handle = btreq->btr_dend;
|
|
|
|
|
2018-04-19 19:26:36 +02:00
|
|
|
nxsem_init(&g_discoverstate.bd_exclsem, 0, 1);
|
|
|
|
g_discoverstate.bd_discovering = true;
|
|
|
|
g_discoverstate.bd_head = 0;
|
|
|
|
g_discoverstate.bd_tail = 0;
|
|
|
|
|
|
|
|
/* Start the query */
|
|
|
|
|
|
|
|
switch (btreq->btr_dtype)
|
|
|
|
{
|
|
|
|
case GATT_DISCOVER:
|
2018-04-19 19:57:15 +02:00
|
|
|
ret = bt_gatt_discover(conn, params);
|
2018-04-19 19:26:36 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GATT_DISCOVER_DESC:
|
2018-04-19 19:57:15 +02:00
|
|
|
ret = bt_gatt_discover_descriptor(conn, params);
|
2018-04-19 19:26:36 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GATT_DISCOVER_CHAR:
|
2018-04-19 19:57:15 +02:00
|
|
|
ret = bt_gatt_discover_characteristic(conn, params);
|
2018-04-19 19:26:36 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
wlerr("ERROR: Unrecognized GATT discover type: %u\n",
|
|
|
|
btreq->btr_dtype);
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Failed to start discovery: %d\n", ret);
|
2018-04-21 16:38:08 +02:00
|
|
|
btnet_discover_destroy(params);
|
2018-04-19 19:26:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bt_conn_release(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* SIOCBTDISCGET: Return discovered results buffered since the call time
|
|
|
|
* that the SIOCBTDISCGET command was invoked.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case SIOCBTDISCGET:
|
|
|
|
{
|
|
|
|
ret = btnet_discover_result(btreq->btr_grsp, btreq->btr_gnrsp);
|
|
|
|
wlinfo("Get discovery results: %d\n", ret);
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
btreq->btr_nrsp = ret;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-04-21 16:38:08 +02:00
|
|
|
/* SIOCBTGATTRD: Initiate read of GATT data */
|
|
|
|
|
|
|
|
case SIOCBTGATTRD:
|
|
|
|
{
|
|
|
|
/* Is there already a read in progress? The current
|
|
|
|
* implementation can support only one read at a time.
|
|
|
|
* REVISIT.. see suggested design improvement above.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (g_rdstate.rd_pending)
|
|
|
|
{
|
|
|
|
wlwarn("WARNING: Read pending\n");
|
|
|
|
ret = -EBUSY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
|
|
|
|
/* Get the connection associated with the provided LE address */
|
|
|
|
|
|
|
|
conn = bt_conn_lookup_addr_le(&btreq->btr_rdpeer);
|
|
|
|
if (conn == NULL)
|
|
|
|
{
|
|
|
|
wlwarn("WARNING: Peer not connected\n");
|
|
|
|
ret = -ENOTCONN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Set up for the read */
|
|
|
|
|
|
|
|
g_rdstate.rd_pending = true;
|
|
|
|
g_rdstate.rd_head = 0;
|
|
|
|
g_rdstate.rd_tail = 0;
|
|
|
|
|
|
|
|
/* Initiate read.. single or multiple? */
|
|
|
|
|
|
|
|
if (btreq->btr_rdnhandles == 1)
|
|
|
|
{
|
|
|
|
/* Read single */
|
|
|
|
|
|
|
|
ret = bt_gatt_read(conn, btreq->btr_rdhandles[0],
|
|
|
|
btreq->btr_rdoffset,
|
|
|
|
btnet_read_callback);
|
|
|
|
}
|
|
|
|
else if (btreq->btr_rdnhandles < HCI_GATT_MAXHANDLES)
|
|
|
|
{
|
|
|
|
/* Read multiple */
|
|
|
|
|
|
|
|
DEBUGASSERT(btreq->btr_rdnhandles > 0);
|
|
|
|
ret = bt_gatt_read_multiple(conn, btreq->btr_rdhandles,
|
|
|
|
btreq->btr_rdnhandles,
|
|
|
|
btnet_read_callback);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENFILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Read operation failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_conn_release(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* SIOCBTGATTRDGET: Get the result of the GATT data read */
|
|
|
|
|
|
|
|
case SIOCBTGATTRDGET:
|
|
|
|
{
|
|
|
|
ret = btnet_read_result(btreq);
|
|
|
|
wlinfo("Get read results: %d\n", ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* SIOCBTGATTWR: Write GATT data */
|
|
|
|
|
|
|
|
case SIOCBTGATTWR:
|
|
|
|
{
|
2018-04-21 16:55:42 +02:00
|
|
|
DEBUGASSERT(btreq->btr_wrdata != NULL);
|
|
|
|
|
|
|
|
/* Is there already a write in progress? The current
|
|
|
|
* implementation can support only one write at a time.
|
|
|
|
* REVISIT.. see suggested design improvement above.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (g_gattwrresult.br_pending)
|
|
|
|
{
|
|
|
|
wlwarn("WARNING: Read pending\n");
|
|
|
|
ret = -EBUSY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
|
|
|
|
/* Get the connection associated with the provided LE address */
|
|
|
|
|
|
|
|
conn = bt_conn_lookup_addr_le(&btreq->btr_wrpeer);
|
|
|
|
if (conn == NULL)
|
|
|
|
{
|
|
|
|
wlwarn("WARNING: Peer not connected\n");
|
|
|
|
ret = -ENOTCONN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Set up for the write */
|
|
|
|
|
|
|
|
g_gattwrresult.br_pending = true;
|
|
|
|
g_gattwrresult.br_result = EBUSY;
|
|
|
|
|
|
|
|
/* Initiate write */
|
|
|
|
|
|
|
|
ret = bt_gatt_write(conn, btreq->btr_wrhandle,
|
|
|
|
btreq->btr_wrdata,
|
|
|
|
btreq->btr_wrnbytes,
|
|
|
|
bnet_write_callback);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Write operation failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_conn_release(conn);
|
|
|
|
}
|
|
|
|
}
|
2018-04-21 16:38:08 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* SIOCBTGATTWRGET: Get the result of the GATT data write */
|
|
|
|
|
|
|
|
case SIOCBTGATTWRGET:
|
|
|
|
{
|
|
|
|
btreq->btr_wrpending = g_gattwrresult.br_pending;
|
|
|
|
btreq->btr_wrresult = g_gattwrresult.br_result;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-03-30 18:50:44 +02:00
|
|
|
default:
|
|
|
|
wlwarn("WARNING: Unrecognized IOCTL command: %02x\n", cmd);
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NETDEV_IOCTL */
|