2018-03-30 18:50:44 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* wireless/bluetooth/bt_ioctl.c
|
2024-09-11 09:04:25 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-03-30 18:50:44 +02:00
|
|
|
*
|
2021-02-19 09:38:00 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2018-03-30 18:50:44 +02:00
|
|
|
*
|
2021-02-19 09:38:00 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-30 18:50:44 +02:00
|
|
|
*
|
2021-02-19 09:38:00 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2018-03-30 18:50:44 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/wqueue.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2018-03-30 18:50:44 +02:00
|
|
|
#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! */
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-01-14 00:37:54 +01:00
|
|
|
* Private Types
|
2018-03-30 18:50:44 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2018-04-19 19:26:36 +02:00
|
|
|
/* These structures encapsulate all globals used by the IOCTL logic. */
|
2020-03-29 19:57:53 +02:00
|
|
|
|
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
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t bs_lock; /* 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) */
|
2024-01-12 07:18:02 +01:00
|
|
|
uint32_t msgcount; /* Number of warnings printed */
|
2018-03-30 18:50:44 +02:00
|
|
|
|
|
|
|
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 */
|
2022-09-06 08:18:45 +02:00
|
|
|
sem_t bd_donesem; /* Done notification */
|
2018-04-19 19:26:36 +02:00
|
|
|
};
|
|
|
|
|
2018-04-21 16:38:08 +02:00
|
|
|
/* GATT read state variables. */
|
|
|
|
|
|
|
|
struct btnet_rdstate_s
|
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
struct btreq_s *rd_btreq;
|
2018-04-21 16:38:08 +02:00
|
|
|
uint8_t rd_result; /* The result of the read */
|
2022-09-06 08:18:45 +02:00
|
|
|
sem_t rd_donesem; /* Done notification */
|
2018-12-08 19:58:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* GATT write state variables. */
|
|
|
|
|
|
|
|
struct btnet_wrstate_s
|
|
|
|
{
|
|
|
|
struct btreq_s *wr_btreq;
|
|
|
|
uint8_t wr_result; /* The result of the read */
|
2022-09-06 08:18:45 +02:00
|
|
|
sem_t wr_donesem; /* Done notification */
|
2018-04-21 16:38:08 +02:00
|
|
|
};
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static struct btnet_scanstate_s g_scanstate =
|
|
|
|
{
|
|
|
|
NXMUTEX_INITIALIZER,
|
|
|
|
};
|
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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-29 19:57:53 +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;
|
2020-03-29 19:57:53 +02:00
|
|
|
int ret;
|
2018-03-30 18:50:44 +02:00
|
|
|
|
|
|
|
if (!g_scanstate.bs_scanning)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Results received while not scanning\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
if (len > CONFIG_BLUETOOTH_MAXSCANDATA)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Scan result is too big: %u\n", len);
|
|
|
|
return;
|
|
|
|
}
|
2018-03-30 18:50:44 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the scan data */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_scanstate.bs_lock);
|
2020-03-29 19:57:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
wlerr("nxsem_wait_uninterruptible() failed: %d\n", ret);
|
|
|
|
return;
|
|
|
|
}
|
2018-03-30 18:50:44 +02:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2024-01-12 07:18:02 +01:00
|
|
|
/* Print only one error message for each SIOCBTSCANSTART call */
|
|
|
|
|
|
|
|
if (g_scanstate.msgcount++ == 0)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Too many scan results\n");
|
|
|
|
}
|
2018-03-30 18:50:44 +02:00
|
|
|
|
|
|
|
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;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_scanstate.bs_lock);
|
2018-03-30 18:50:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
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)
|
2020-03-29 19:57:53 +02:00
|
|
|
{
|
2018-04-19 21:43:21 +02:00
|
|
|
/* Get exclusive access to the scan data */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_scanstate.bs_lock);
|
2018-04-19 21:43:21 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
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-12-08 19:58:27 +01:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_scanstate.bs_lock);
|
2018-12-08 19:58:27 +01:00
|
|
|
}
|
2018-04-19 21:43:21 +02:00
|
|
|
|
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
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
FAR struct btnet_wrstate_s *pstate = conn->p_iostate;
|
|
|
|
FAR struct btreq_s *btreq = pstate->wr_btreq;
|
|
|
|
|
|
|
|
wlinfo("%s\n", result == 0 ? "succeeded" : "failed");
|
|
|
|
|
|
|
|
btreq->btr_exresult = result;
|
|
|
|
nxsem_post(&pstate->wr_donesem);
|
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
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
FAR struct bt_gatt_discover_params_s *params = arg;
|
|
|
|
FAR struct btreq_s *btreq = (FAR struct btreq_s *)(params->p_data);
|
2018-04-19 19:26:36 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
wlinfo("Discovered handle %x\n", attr->handle);
|
2018-04-19 19:26:36 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
if (btreq->btr_indx >= btreq->btr_gnrsp)
|
2018-04-19 19:26:36 +02:00
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
wlerr("ERROR: No space for results\n");
|
2018-04-19 19:26:36 +02:00
|
|
|
return BT_GATT_ITER_STOP;
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
btreq->btr_grsp[btreq->btr_indx].dr_handle = attr->handle;
|
|
|
|
btreq->btr_grsp[btreq->btr_indx].dr_perm = attr->perm;
|
|
|
|
btreq->btr_indx++;
|
2018-04-19 19:26:36 +02:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
struct btnet_discoverstate_s *pstate = arg;
|
2018-04-19 19:26:36 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
wlinfo("Discover destroy\n");
|
2018-04-19 19:26:36 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
nxsem_post(&pstate->bd_donesem);
|
2018-04-19 19:26:36 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
FAR struct btnet_rdstate_s *pstate = conn->p_iostate;
|
|
|
|
FAR struct btreq_s *btreq = pstate->rd_btreq;
|
2018-04-21 16:38:08 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
wlinfo("Read complete: result %d length %u\n", result, length);
|
2018-04-21 16:38:08 +02:00
|
|
|
|
|
|
|
if (length > HCI_GATTRD_DATA)
|
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
wlerr("ERROR: Unexpected length %u, result = %d\n", length, result);
|
|
|
|
btreq->btr_rdresult = ENFILE;
|
2018-04-21 16:38:08 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGASSERT((unsigned int)result < UINT8_MAX);
|
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
btreq->btr_rdresult = result;
|
|
|
|
btreq->btr_rdsize = length;
|
|
|
|
memcpy(btreq->btr_rddata, data, length);
|
2018-04-21 16:38:08 +02:00
|
|
|
}
|
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
nxsem_post(&pstate->rd_donesem);
|
2018-04-21 16:38:08 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
FAR struct btnet_wrstate_s *pstate = conn->p_iostate;
|
|
|
|
FAR struct btreq_s *btreq = pstate->wr_btreq;
|
|
|
|
|
|
|
|
wlinfo("%s\n", result == 0 ? "succeeded" : "failed");
|
|
|
|
|
|
|
|
btreq->btr_wrresult = result;
|
|
|
|
nxsem_post(&pstate->wr_donesem);
|
2018-04-21 16:55:42 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2020-11-23 09:32:34 +01:00
|
|
|
wlinfo("cmd=%04x arg=%lu\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-12-02 17:57:51 +01:00
|
|
|
case SIOCBTCONNECT:
|
|
|
|
{
|
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
conn = bt_conn_create_le(&btreq->btr_rmtpeer);
|
|
|
|
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
wlerr("Connection failed\n");
|
|
|
|
ret = -ENOTCONN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wlinfo("Connection pending\n");
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIOCBTDISCONNECT:
|
|
|
|
{
|
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
|
|
|
|
conn = bt_conn_lookup_addr_le(&btreq->btr_rmtpeer);
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
wlerr("Peer not connected\n");
|
|
|
|
ret = -ENOTCONN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-29 19:57:53 +02:00
|
|
|
ret = bt_conn_disconnect(conn,
|
|
|
|
BT_HCI_ERR_REMOTE_USER_TERM_CONN);
|
2018-12-02 17:57:51 +01:00
|
|
|
if (ret == -ENOTCONN)
|
|
|
|
{
|
|
|
|
wlerr("Already disconnected\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Release reference taken in bt_conn_create_le */
|
|
|
|
|
|
|
|
bt_conn_release(conn);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release reference taken in bt_conn_lookup_addr_le */
|
|
|
|
|
|
|
|
bt_conn_release(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
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,
|
2020-07-26 02:52:06 +02:00
|
|
|
btreq->btr_advsd);
|
2018-04-06 18:39:12 +02:00
|
|
|
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 */
|
|
|
|
|
|
|
|
g_scanstate.bs_scanning = true;
|
|
|
|
g_scanstate.bs_head = 0;
|
|
|
|
g_scanstate.bs_tail = 0;
|
2024-01-12 07:18:02 +01:00
|
|
|
g_scanstate.msgcount = 0;
|
2018-03-30 18:50:44 +02:00
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2020-04-23 08:31:46 +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);
|
|
|
|
|
|
|
|
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:
|
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
|
|
|
|
/* Get the connection associated with the provided LE address */
|
2018-04-19 23:51:40 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
conn = bt_conn_lookup_addr_le(&btreq->btr_expeer);
|
|
|
|
if (conn == NULL)
|
2018-04-19 23:51:40 +02:00
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
wlwarn("WARNING: Peer not connected\n");
|
|
|
|
ret = -ENOTCONN;
|
2018-04-19 23:51:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
struct btnet_wrstate_s wrstate;
|
|
|
|
|
|
|
|
memset(&wrstate, 0, sizeof(wrstate));
|
|
|
|
wrstate.wr_btreq = btreq;
|
|
|
|
conn->p_iostate = &wrstate;
|
|
|
|
nxsem_init(&wrstate.wr_donesem, 0, 0);
|
2018-04-19 23:51:40 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
btreq->btr_wrresult = EBUSY;
|
2018-04-19 23:51:40 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
ret = bt_gatt_exchange_mtu(conn, btnet_exchange_rsp);
|
|
|
|
if (ret < 0)
|
2018-04-19 23:51:40 +02:00
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
wlerr("ERROR: Exchange operation failed: %d\n", ret);
|
2018-04-19 23:51:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
/* Wait for callback to complete the exchange */
|
2018-04-19 23:51:40 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
ret = nxsem_wait(&wrstate.wr_donesem);
|
2018-04-19 23:51:40 +02:00
|
|
|
}
|
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
nxsem_destroy(&wrstate.wr_donesem);
|
2018-04-19 23:51:40 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
bt_conn_release(conn);
|
|
|
|
}
|
2018-04-19 23:51:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-04-19 19:26:36 +02:00
|
|
|
/* SIOCBTDISCOVER: Starts GATT discovery */
|
|
|
|
|
|
|
|
case SIOCBTDISCOVER:
|
|
|
|
{
|
|
|
|
FAR struct bt_conn_s *conn;
|
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
/* Get the connection associated with the provided LE address */
|
2018-04-19 19:26:36 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
conn = bt_conn_lookup_addr_le(&btreq->btr_dpeer);
|
|
|
|
if (conn == NULL)
|
2018-04-19 19:26:36 +02:00
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
wlwarn("WARNING: Peer not connected\n");
|
|
|
|
ret = -ENOTCONN;
|
2018-04-19 19:26:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
struct btnet_discoverstate_s dstate;
|
|
|
|
FAR struct bt_gatt_discover_params_s *params;
|
|
|
|
memset(&dstate, 0, sizeof(dstate));
|
2018-04-19 19:26:36 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
/* Set up the query */
|
|
|
|
|
|
|
|
dstate.bd_uuid.type = BT_UUID_16;
|
|
|
|
dstate.bd_uuid.u.u16 = btreq->btr_duuid16;
|
|
|
|
|
|
|
|
params = &dstate.bd_params;
|
|
|
|
params->func = btnet_discover_func;
|
|
|
|
params->destroy = btnet_discover_destroy;
|
|
|
|
params->start_handle = btreq->btr_dstart;
|
|
|
|
params->end_handle = btreq->btr_dend;
|
2024-08-25 01:21:12 +02:00
|
|
|
params->p_data = (FAR void *)arg;
|
2018-12-08 19:58:27 +01:00
|
|
|
btreq->btr_indx = 0;
|
|
|
|
|
|
|
|
if (btreq->btr_duuid16 == 0)
|
2018-04-19 19:26:36 +02:00
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
params->uuid = NULL;
|
2018-04-19 19:26:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
params->uuid = &dstate.bd_uuid;
|
|
|
|
}
|
2018-04-19 19:57:15 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
nxsem_init(&dstate.bd_donesem, 0, 0);
|
2018-04-19 19:26:36 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
/* Start the query */
|
2018-04-19 19:26:36 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
switch (btreq->btr_dtype)
|
|
|
|
{
|
|
|
|
case GATT_DISCOVER:
|
|
|
|
ret = bt_gatt_discover(conn, params);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GATT_DISCOVER_DESC:
|
|
|
|
ret = bt_gatt_discover_descriptor(conn, params);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GATT_DISCOVER_CHAR:
|
|
|
|
ret = bt_gatt_discover_characteristic(conn, params);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
wlerr("ERROR: Unrecognized GATT discover type: %u\n",
|
|
|
|
btreq->btr_dtype);
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
2018-04-19 19:26:36 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Failed to start discovery: %d\n", ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = nxsem_wait(&dstate.bd_donesem);
|
|
|
|
if (ret == 0)
|
2018-04-19 19:26:36 +02:00
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
btreq->btr_gnrsp = btreq->btr_indx;
|
2018-04-19 19:26:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
nxsem_destroy(&dstate.bd_donesem);
|
2018-04-19 19:26:36 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
bt_conn_release(conn);
|
2018-04-19 19:26:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
/* SIOCBTGATTRD: Read GATT data */
|
2018-04-21 16:38:08 +02:00
|
|
|
|
|
|
|
case SIOCBTGATTRD:
|
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
FAR struct bt_conn_s *conn;
|
2018-04-21 16:38:08 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
/* Get the connection associated with the provided LE address */
|
|
|
|
|
|
|
|
conn = bt_conn_lookup_addr_le(&btreq->btr_rdpeer);
|
|
|
|
if (conn == NULL)
|
2018-04-21 16:38:08 +02:00
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
wlwarn("WARNING: Peer not connected\n");
|
|
|
|
ret = -ENOTCONN;
|
2018-04-21 16:38:08 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
/* Set up for the read */
|
2018-04-21 16:38:08 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
struct btnet_rdstate_s rdstate;
|
2018-04-21 16:38:08 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
memset(&rdstate, 0, sizeof(rdstate));
|
|
|
|
rdstate.rd_btreq = btreq;
|
|
|
|
conn->p_iostate = &rdstate;
|
|
|
|
nxsem_init(&rdstate.rd_donesem, 0, 0);
|
2018-04-21 16:38:08 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
/* Initiate read.. single or multiple? */
|
2018-04-21 16:38:08 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
if (btreq->btr_rdnhandles == 1)
|
|
|
|
{
|
|
|
|
/* Read single */
|
2018-04-21 16:38:08 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
DEBUGASSERT(conn->p_iostate != NULL);
|
|
|
|
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 */
|
2018-04-21 16:38:08 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
DEBUGASSERT(conn->p_iostate != NULL);
|
|
|
|
DEBUGASSERT(btreq->btr_rdnhandles > 0);
|
|
|
|
ret = bt_gatt_read_multiple(conn, btreq->btr_rdhandles,
|
|
|
|
btreq->btr_rdnhandles,
|
|
|
|
btnet_read_callback);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENFILE;
|
|
|
|
}
|
2018-04-21 16:38:08 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Read operation failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Wait for callback to complete the transfer */
|
2018-04-21 16:38:08 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
ret = nxsem_wait(&rdstate.rd_donesem);
|
2018-04-21 16:38:08 +02:00
|
|
|
}
|
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
nxsem_destroy(&rdstate.rd_donesem);
|
2018-04-21 16:38:08 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
bt_conn_release(conn);
|
|
|
|
}
|
2018-04-21 16:38:08 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* SIOCBTGATTWR: Write GATT data */
|
|
|
|
|
|
|
|
case SIOCBTGATTWR:
|
|
|
|
{
|
2018-04-21 16:55:42 +02:00
|
|
|
DEBUGASSERT(btreq->btr_wrdata != NULL);
|
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
FAR struct bt_conn_s *conn;
|
2018-04-21 16:55:42 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
/* Get the connection associated with the provided LE address */
|
|
|
|
|
|
|
|
conn = bt_conn_lookup_addr_le(&btreq->btr_wrpeer);
|
|
|
|
if (conn == NULL)
|
2018-04-21 16:55:42 +02:00
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
wlwarn("WARNING: Peer not connected\n");
|
|
|
|
ret = -ENOTCONN;
|
2018-04-21 16:55:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
/* Set up for the write */
|
|
|
|
|
|
|
|
struct btnet_wrstate_s wrstate;
|
2018-04-21 16:55:42 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
memset(&wrstate, 0, sizeof(wrstate));
|
|
|
|
wrstate.wr_btreq = btreq;
|
|
|
|
conn->p_iostate = &wrstate;
|
|
|
|
nxsem_init(&wrstate.wr_donesem, 0, 0);
|
2018-04-21 16:55:42 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
btreq->btr_wrresult = EBUSY;
|
|
|
|
|
|
|
|
/* Initiate write */
|
|
|
|
|
|
|
|
ret = bt_gatt_write(conn, btreq->btr_wrhandle,
|
|
|
|
btreq->btr_wrdata,
|
|
|
|
btreq->btr_wrnbytes,
|
|
|
|
bnet_write_callback);
|
|
|
|
if (ret < 0)
|
2018-04-21 16:55:42 +02:00
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
wlerr("ERROR: Write operation failed: %d\n", ret);
|
2018-04-21 16:55:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-08 19:58:27 +01:00
|
|
|
/* Wait for callback to complete the transfer */
|
2018-04-21 16:55:42 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
ret = nxsem_wait(&wrstate.wr_donesem);
|
2018-04-21 16:55:42 +02:00
|
|
|
}
|
2018-04-21 16:38:08 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
nxsem_destroy(&wrstate.wr_donesem);
|
2018-04-21 16:38:08 +02:00
|
|
|
|
2018-12-08 19:58:27 +01:00
|
|
|
bt_conn_release(conn);
|
|
|
|
}
|
2018-04-21 16:38:08 +02:00
|
|
|
}
|
|
|
|
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 */
|