From cab7ba8b8ffa582c84ff5fca1fa356324169ac2e Mon Sep 17 00:00:00 2001
From: Gregory Nutt <gnutt@nuttx.org>
Date: Fri, 6 Apr 2018 09:42:41 -0600
Subject: [PATCH] Bluetooth:  Better integrate NuttX Bluetooth IOCTL commands
 and NetBSD IOCTL commands.  Stanardize naming.

---
 include/nuttx/wireless/bt_ioctl.h | 213 +++++++++++++++++-------------
 net/netdev/netdev_ioctl.c         |  88 ++++++++----
 wireless/bluetooth/bt_hcicore.c   |   8 +-
 wireless/bluetooth/bt_ioctl.c     | 102 +++++++-------
 4 files changed, 235 insertions(+), 176 deletions(-)

diff --git a/include/nuttx/wireless/bt_ioctl.h b/include/nuttx/wireless/bt_ioctl.h
index 21f30e93c7..4d7c37f46b 100644
--- a/include/nuttx/wireless/bt_ioctl.h
+++ b/include/nuttx/wireless/bt_ioctl.h
@@ -64,6 +64,8 @@
 
 /* NetBSD IOCTL commands ****************************************************/
 /* All of the following use an argument of type struct btreg_s:
+ *
+ * Bluetooth Information Queries
  *
  * SIOCGBTINFO
  *   Get Bluetooth device Info.  Given the device name, fill in the btreq_s
@@ -77,92 +79,96 @@
  *   will be returned.  Otherwise, the next device will be returned until
  *   no more devices are found when the call will fail, with error ENXIO.
  *   Thus, you can cycle through all devices in the system.
- * SIOCSBTFLAGS
- *   Set Bluetooth device Flags.  Not all flags are settable.
  * SIOCGBTFEAT
  *   Get Bluetooth device Features.  This returns the cached basic (page 0)
  *   and extended (page 1 & 2) features.
+ */
+
+#define SIOCGBTINFO            _WLIOC(WL_BLUETOOTHFIRST + 0)
+#define SIOCGBTINFOA           _WLIOC(WL_BLUETOOTHFIRST + 1)
+#define SIOCNBTINFO            _WLIOC(WL_BLUETOOTHFIRST + 2)
+#define SIOCGBTFEAT            _WLIOC(WL_BLUETOOTHFIRST + 3)
+
+/* Set Flags, Link Policy, and Packet Types
+ *
+ * SIOCSBTFLAGS
+ *   Set Bluetooth device Flags.  Not all flags are settable.
  * SIOCSBTPOLICY
  *   Set Bluetooth device Link Policy bits.
  * SIOCSBTPTYPE
  *   Set Bluetooth device Packet Types.  You can only set packet types that
  *   the device supports.
+ */
+
+#define SIOCSBTFLAGS           _WLIOC(WL_BLUETOOTHFIRST + 4)
+#define SIOCSBTPOLICY          _WLIOC(WL_BLUETOOTHFIRST + 5)
+#define SIOCSBTPTYPE           _WLIOC(WL_BLUETOOTHFIRST + 6)
+
+/* Get Statistics:
+ *
  * SIOCGBTSTATS
  *   Read device statistics.
  * SIOCZBTSTATS
  *   Read device statistics, and zero them.
- *
- * NOTE: These are here for reference.  None of the NetBSD IOCTL commands
- * have been implemented in NuttX.
  */
 
-#define SIOCGBTINFO            _WLIOC(WL_BLUETOOTHFIRST + 0)
-#define SIOCGBTINFOA           _WLIOC(WL_BLUETOOTHFIRST + 1)
-#define SIOCNBTINFO            _WLIOC(WL_BLUETOOTHFIRST + 2)
-#define SIOCSBTFLAGS           _WLIOC(WL_BLUETOOTHFIRST + 3)
-#define SIOCGBTFEAT            _WLIOC(WL_BLUETOOTHFIRST + 4)
-#define SIOCSBTPOLICY          _WLIOC(WL_BLUETOOTHFIRST + 5)
-#define SIOCSBTPTYPE           _WLIOC(WL_BLUETOOTHFIRST + 6)
 #define SIOCGBTSTATS           _WLIOC(WL_BLUETOOTHFIRST + 7)
 #define SIOCZBTSTATS           _WLIOC(WL_BLUETOOTHFIRST + 8)
 
 /* NuttX-specific IOCTL commands. *******************************************/
-/* SIOCBT_ADVERTISESTART
+/* All of the following use an argument of type struct btreg_s: */
+
+/* SIOCBTADVSTART
  *   Description:   Set advertisement data, scan response data,
  *                  advertisement parameters and start advertising.
- *   Input:         Pointer to read-write instance of struct
- *                  bt_advertisestart_s.
+ *   Input:         Pointer to read-btreq_s instance of struct btreq_s.
  *   Output:        None
  */
 
-#define SIOCBT_ADVERTISESTART  _WLIOC(WL_BLUETOOTHFIRST + 9)
+#define SIOCBTADVSTART         _WLIOC(WL_BLUETOOTHFIRST + 9)
 
-/* SIOCBT_ADVERTISESTOP
+/* SIOCBTADVSTOP
  *   Description:   Stop advertising.
- *   Input:         A reference to a write-able instance of struct
- *                  bt_scanstop_s.
+ *   Input:         A reference to a write-able instance of struct btreq_s.
  *   Output:        None
  */
 
-#define SIOCBT_ADVERTISESTOP   _WLIOC(WL_BLUETOOTHFIRST + 10)
+#define SIOCBTADVSTOP          _WLIOC(WL_BLUETOOTHFIRST + 10)
 
-/* SIOCBT_SCANSTART
+/* SIOCBTSCANSTART
  *   Description:   Start LE scanning.  Buffered scan results may be
- *                  obtained via SIOCBT_SCANGET
- *   Input:         A read-only referent to struct bt_scanstart_s.
+ *                  obtained via SIOCBTSCANGET
+ *   Input:         A read-only referent to struct btreq_s.
  *   Output:        None
  */
 
-#define SIOCBT_SCANSTART       _WLIOC(WL_BLUETOOTHFIRST + 11)
+#define SIOCBTSCANSTART        _WLIOC(WL_BLUETOOTHFIRST + 11)
 
-/* SIOCBT_SCANGET
+/* SIOCBTSCANGET
  *   Description:   Return scan results buffered since the call time that
- *                  the SIOCBT_SCANGET command was invoked.
- *   Input:         A reference to a write-able instance of struct
- *                  bt_scanresult_s.
+ *                  the SIOCBTSCANGET command was invoked.
+ *   Input:         A reference to a write-able instance of struct btreq_s.
  *   Output:        Buffered scan result results are returned in the user-
  *                  provided buffer space.
  */
 
-#define SIOCBT_SCANGET         _WLIOC(WL_BLUETOOTHFIRST + 12)
+#define SIOCBTSCANGET          _WLIOC(WL_BLUETOOTHFIRST + 12)
 
-/* SIOCBT_SCANSTOP
+/* SIOCBTSCANSTOP
  *   Description:   Stop LE scanning and discard any buffered results.
- *   Input:         A reference to a write-able instance of struct
- *                  bt_scanstop_s.
+ *   Input:         A reference to a read-only instance of struct btreq_s.
  *   Output:        None
  */
 
-#define SIOCBT_SCANSTOP        _WLIOC(WL_BLUETOOTHFIRST + 13)
+#define SIOCBTSCANSTOP         _WLIOC(WL_BLUETOOTHFIRST + 13)
 
-/* SIOCBT_SECURITY
+/* SIOCBTSECURITY
  *   Description:   Enable security for a connection.
- *   Input:         A reference to a write-able instance of struct
- *                  bt_security_s.
+ *   Input:         A reference to a write-able instance of struct btreq_s.
  *   Output:        None
  */
 
-#define SIOCBT_SECURITY        _WLIOC(WL_BLUETOOTHFIRST + 14)
+#define SIOCBTSECURITY         _WLIOC(WL_BLUETOOTHFIRST + 14)
 
 /* Definitions associated with struct btreg_s *******************************/
 /* struct btreq_s union field accessors */
@@ -178,9 +184,23 @@
 #define btr_packet_type        btru.btri.btri_packet_type
 #define btr_max_acl            btru.btri.btri_max_acl
 #define btr_max_sco            btru.btri.btri_max_sco
+
 #define btr_features0          btru.btrf.btrf_page0
 #define btr_features1          btru.btrf.btrf_page1
 #define btr_features2          btru.btrf.btrf_page2
+
+#define btr_advtype            btru.btras.btras_advtype
+#define btr_advad              btru.btras.btras_advad
+#define btr_advas              btru.btras.btras_advsd
+
+#define btr_dupenable          btru.btrss.btrss_dupenable
+
+#define btr_nrsp               btru.btrsr.brtsr_nrsp
+#define btr_rsp                btru.btrsr.btrsr_rsp
+
+#define btr_secaddr            btru.btrse.btrse_secaddr
+#define btr_seclevel           btru.btrse.btrse_seclevel
+
 #define btr_stats              btru.btrs
 
 /* btr_flags */
@@ -201,9 +221,21 @@
  * Public Types
  ****************************************************************************/
 
-/* Common structure for Bluetooth IOCTL commands */
+/* Write-able data that accompanies the SIOCBTSCANGET IOCTL command */
+
+struct bt_scanresponse_s
+{
+  char sr_name[HCI_DEVNAME_SIZE];   /* Device name */
+  bt_addr_le_t sr_addr;             /* Advertiser LE address and type */
+  int8_t sr_rssi;                   /* Strength of advertiser signal */
+  uint8_t sr_type;                  /* Type of advertising response */
+  uint8_t sr_len;                   /* Length of advertiser data */
+  uint8_t sr_data[CONFIG_BLUETOOTH_MAXSCANDATA];
+};
+
+/* Bluetooth statistics */
 
-struct bt_stats
+struct bt_stats_s
 {
   uint32_t err_tx;
   uint32_t err_rx;
@@ -217,11 +249,15 @@ struct bt_stats
   uint32_t byte_rx;
 };
 
+/* Common structure for Bluetooth IOCTL commands */
+
 struct btreq_s
   {
     char btr_name[HCI_DEVNAME_SIZE]; /* Device name */
     union
     {
+      /* Bluetooth information used by most NetBSD IOCTL commands */
+
       struct
       {
          bt_addr_t btri_bdaddr;      /* Device bdaddr */
@@ -236,81 +272,68 @@ struct btreq_s
          uint16_t btri_max_acl;      /* max ACL buffers */
          uint16_t btri_max_sco;      /* max SCO buffers */
        } btri;
+
+       /* Bluetooth Features */
+
        struct
        {
          uint8_t btrf_page0[HCI_FEATURES_SIZE]; /* basic */
          uint8_t btrf_page1[HCI_FEATURES_SIZE]; /* extended page 1 */
          uint8_t btrf_page2[HCI_FEATURES_SIZE]; /* extended page 2 */
        } btrf;
-       struct bt_stats btrs;   /* unit stats */
-   } btru;
-};
 
-/* Read-only data that accompanies the SIOCBT_ADVERTISESTART IOCTL command.
- * Advertising types are defined in bt_hci.h.
- */
+      /* Read-only data that accompanies the SIOCBTADVSTART IOCTL command.
+       * Advertising types are defined in bt_hci.h.  NOTE that btras_ad and
+       * btras_sd pointers to the beginning of a list of "Extended Inquire
+       * Responses".   Each list is terminated with a dummy, NULL entry
+       * identified with a length of zero.
+       */
 
-struct bt_advertisestart_s
-{
-  char as_name[HCI_DEVNAME_SIZE];  /* Device name */
-  uint8_t as_type;                 /* Advertising type */
-  FAR struct bt_eir_s as_ad;       /* Data for advertisement packets */
-  FAR struct bt_eir_s as_sd;       /* Data for scan response packets */
-};
-
-/* The read-only data that accompanies the SIOCBT_SCANSTOP IOCTL command */
+      struct
+      {
+        uint8_t btras_advtype;             /* Advertising type */
+        FAR struct bt_eir_s *btras_advad;  /* Data for advertisement packets */
+        FAR struct bt_eir_s *btras_advsd;  /* Data for scan response packets */
+      } btras;
 
-struct bt_advertisestop_s
-{
-  char at_name[HCI_DEVNAME_SIZE];   /* Device name */
-};
+      /* NOTE: No additional data accompanies the SIOCBTADVSTOP */
 
-/* The read-only data that accompanies the SIOCBT_SCANSTART IOCTL command */
+      /* The read-only data that accompanies the SIOCBTSCANSTART IOCTL
+       * command.
+       */
 
-struct bt_scanstart_s
-{
-  char ss_name[HCI_DEVNAME_SIZE];   /* Device name */
-  bool ss_dupenable;                /* True: enable duplicate filtering */
-};
+      struct
+      {
+        bool btrss_dupenable;           /* True: enable duplicate filtering */
+      } btrss;
 
-/* The read-only data that accompanies the SIOCBT_SCANSTOP IOCTL command */
+      /* Write-able data that accompanies the SIOCBTSCANGET IOCTL command */
 
-struct bt_scanstop_s
-{
-  char st_name[HCI_DEVNAME_SIZE];   /* Device name */
-};
+      struct
+      {
+        uint8_t brtsr_nrsp;             /* Input:  Max number of responses
+                                         * Return: Actual number of responses */
 
-/* Write-able data that accompanies the SIOCBT_SCANGET IOCTL command */
+        /* Reference to a beginning of an array in user memory in which to
+         * return the scan response data.  The size of the array is
+         * btrsr_nrsp.
+         */
 
-struct bt_scanresponse_s
-{
-  char sr_name[HCI_DEVNAME_SIZE];   /* Device name */
-  bt_addr_le_t sr_addr;             /* Advertiser LE address and type */
-  int8_t sr_rssi;                   /* Strength of advertiser signal */
-  uint8_t sr_type;                  /* Type of advertising response */
-  uint8_t sr_len;                   /* Length of advertiser data */
-  uint8_t sr_data[CONFIG_BLUETOOTH_MAXSCANDATA];
-};
+        FAR struct bt_scanresponse_s *btrsr_rsp;
+      } btrsr;
 
-struct bt_scanresult_s
-{
-  char sr_name[HCI_DEVNAME_SIZE];   /* Device name */
-  uint8_t sr_nrsp;                  /* Input:  Max number of responses
-                                     * Return: Actual number of responses */
-  struct bt_scanresponse_s sr_rsp[1];
-};
+      /* NOTE: No additional data accompanies the SIOCBTSCANSTOP */
 
-#define SIZEOF_BT_SCANRESULT_S(n) \
-  (sizeof(struct bt_scanresult_s) + \
-   ((n) - 1) * sizeof(struct bt_scanresponse_s))
+      /* Read-only data that accompanies the SIOCBTSECURITY IOCTL command */
 
-/* Read-only data that accompanies the SIOCBT_SECURITY IOCTL command */
+      struct
+      {
+        bt_addr_le_t btrse_secaddr;        /* BLE address */
+        enum bt_security_e btrse_seclevel; /* Security level */
+      } btrse;
 
-struct bt_security_s
-{
-  char se_name[HCI_DEVNAME_SIZE];   /* Device name */
-  bt_addr_le_t se_addr;             /* BLE address */
-  enum bt_security_e se_level;      /* Security level */
+      struct bt_stats_s btrs;           /* Unit statistics */
+   } btru;
 };
 
 /****************************************************************************
diff --git a/net/netdev/netdev_ioctl.c b/net/netdev/netdev_ioctl.c
index bfe6fb2e22..19fa1c411c 100644
--- a/net/netdev/netdev_ioctl.c
+++ b/net/netdev/netdev_ioctl.c
@@ -72,18 +72,16 @@
 #  include <nuttx/wireless/wireless.h>
 #endif
 
-#if defined(CONFIG_NETDEV_IOCTL) && defined(CONFIG_NET_6LOWPAN)
-#  ifdef CONFIG_WIRELESS_BLUETOOTH
-#    include <nuttx/wireless/bt_ioctl.h>
-#  endif
+#ifdef CONFIG_WIRELESS_BLUETOOTH
+#  include <nuttx/wireless/bt_ioctl.h>
+#endif
 
-#  ifdef CONFIG_WIRELESS_IEEE802154
-#    include <nuttx/wireless/ieee802154/ieee802154_mac.h>
-#  endif
+#ifdef CONFIG_WIRELESS_IEEE802154
+#  include <nuttx/wireless/ieee802154/ieee802154_mac.h>
+#endif
 
-#  ifdef CONFIG_WIRELESS_PKTRADIO
-#    include <nuttx/wireless/pktradio.h>
-#  endif
+#ifdef CONFIG_WIRELESS_PKTRADIO
+#  include <nuttx/wireless/pktradio.h>
 #endif
 
 #include "arp/arp.h"
@@ -114,6 +112,31 @@
 #  endif
 #endif
 
+#undef HAVE_IEEE802154_IOCTL
+#undef HAVE_PKTRADIO_IOCTL
+#undef HAVE_BLUETOOTH_IOCTL
+
+#ifdef CONFIG_NETDEV_IOCTL
+/* IEEE 802.15.4 6LoWPAN or raw packet support */
+
+#if defined(CONFIG_NET_IEEE802154) || (defined(CONFIG_NET_6LOWPAN) && \
+    defined(CONFIG_WIRELESS_IEEE802154))
+#  define HAVE_IEEE802154_IOCTL 1
+#endif
+
+/* pktradio raw packet support not implemented */
+
+#if defined(CONFIG_NET_6LOWPAN) && defined(CONFIG_WIRELESS_PKTRADIO)
+#  define HAVE_PKTRADIO_IOCTL 1
+#endif
+
+/* Bluetooth 6LoWPAN support not implemented */
+
+#if defined(CONFIG_NET_BLUETOOTH)
+#  define HAVE_BLUETOOTH_IOCTL 1
+#endif
+#endif /* CONFIG_NETDEV_IOCTL */
+
 /* This is really kind of bogus.. When asked for an IP address, this is
  * family that is returned in the ifr structure.  Probably could just skip
  * this since the address family has nothing to do with the Ethernet address.
@@ -375,7 +398,7 @@ static void ioctl_set_ipv6addr(FAR net_ipv6addr_t outaddr,
 #endif
 
 /****************************************************************************
- * Name: netdev_iee802154_ioctl
+ * Name: netdev_bluetooth_ioctl
  *
  * Description:
  *   Perform Bluetooth network device specific operations.
@@ -392,14 +415,13 @@ static void ioctl_set_ipv6addr(FAR net_ipv6addr_t outaddr,
  *
  ****************************************************************************/
 
-#if defined(CONFIG_NETDEV_IOCTL) && defined(CONFIG_NET_6LOWPAN) && \
-    defined(CONFIG_WIRELESS_BLUETOOTH)
-static int netdev_iee802154_ioctl(FAR struct socket *psock, int cmd,
+#ifdef HAVE_BLUETOOTH_IOCTL
+static int netdev_bluetooth_ioctl(FAR struct socket *psock, int cmd,
                                   unsigned long arg)
 {
   FAR struct net_driver_s *dev;
   FAR char *ifname;
-  int ret = -ENOTTY;
+  int ret = -EINVAL;
 
   if (arg != 0ul)
     {
@@ -409,10 +431,10 @@ static int netdev_iee802154_ioctl(FAR struct socket *psock, int cmd,
            * command
            */
 
-          FAR struct bluetooth_ifreq_s *cmddata =
-            (FAR struct bluetooth_ifreq_s *)((uintptr_t)arg);
+          FAR struct btreq_s *btreq =
+            (FAR struct btreq_s *)((uintptr_t)arg);
 
-          ifname = cmddata->ifr_name;
+          ifname = btreq->btr_name;
         }
       else
         {
@@ -424,6 +446,8 @@ static int netdev_iee802154_ioctl(FAR struct socket *psock, int cmd,
       /* Find the device with this name */
 
       dev = netdev_findbyname(ifname);
+      ret = -ENODEV;
+
       if (dev != NULL && dev->d_lltype == NET_LL_BLUETOOTH)
         {
           /* Perform the device IOCTL */
@@ -454,8 +478,7 @@ static int netdev_iee802154_ioctl(FAR struct socket *psock, int cmd,
  *
  ****************************************************************************/
 
-#if defined(CONFIG_NETDEV_IOCTL) && defined(CONFIG_NET_6LOWPAN) && \
-    defined(CONFIG_WIRELESS_IEEE802154)
+#ifdef HAVE_IEEE802154_IOCTL
 static int netdev_iee802154_ioctl(FAR struct socket *psock, int cmd,
                                   unsigned long arg)
 {
@@ -496,7 +519,7 @@ static int netdev_iee802154_ioctl(FAR struct socket *psock, int cmd,
 
   return ret;
 }
-#endif
+#endif /* HAVE_IEEE802154_IOCTL */
 
 /****************************************************************************
  * Name: netdev_pktradio_ioctl
@@ -516,8 +539,7 @@ static int netdev_iee802154_ioctl(FAR struct socket *psock, int cmd,
  *
  ****************************************************************************/
 
-#if defined(CONFIG_NETDEV_IOCTL) && defined(CONFIG_NET_6LOWPAN) && \
-    defined(CONFIG_WIRELESS_PKTRADIO)
+#ifdef HAVE_PKTRADIO_IOCTL
 static int netdev_pktradio_ioctl(FAR struct socket *psock, int cmd,
                                  unsigned long arg)
 {
@@ -559,7 +581,7 @@ static int netdev_pktradio_ioctl(FAR struct socket *psock, int cmd,
 
   return ret;
 }
-#endif
+#endif /* HAVE_PKTRADIO_IOCTL */
 
 /****************************************************************************
  * Name: netdev_wifr_ioctl
@@ -1472,23 +1494,31 @@ int psock_ioctl(FAR struct socket *psock, int cmd, unsigned long arg)
     }
 #endif
 
-#if defined(CONFIG_NETDEV_IOCTL) && defined(CONFIG_NET_6LOWPAN)
-#ifdef CONFIG_WIRELESS_IEEE802154
-  /* Check for a IEEE802.15.4 network device command */
+#ifdef HAVE_IEEE802154_IOCTL
+  /* Check for a IEEE802.15.4 network device IOCTL command */
 
   if (ret == -ENOTTY)
     {
       ret = netdev_iee802154_ioctl(psock, cmd, arg);
     }
 #endif
-#ifdef CONFIG_WIRELESS_PKTRADIO
-  /* Check for a non-IEEE802.15.4 packet radio network device command */
+
+#ifdef HAVE_PKTRADIO_IOCTL
+  /* Check for a non-IEEE802.15.4 packet radio network device IOCTL command */
 
   if (ret == -ENOTTY)
     {
       ret = netdev_pktradio_ioctl(psock, cmd, arg);
     }
 #endif
+
+#ifdef HAVE_BLUETOOTH_IOCTL
+  /* Check for Bluetooth network device IOCTL command */
+
+  if (ret == -ENOTTY)
+    {
+      ret = netdev_bluetooth_ioctl(psock, cmd, arg);
+    }
 #endif
 
 #ifdef CONFIG_NET_IGMP
diff --git a/wireless/bluetooth/bt_hcicore.c b/wireless/bluetooth/bt_hcicore.c
index 7b79d69ef6..c2d16115dd 100644
--- a/wireless/bluetooth/bt_hcicore.c
+++ b/wireless/bluetooth/bt_hcicore.c
@@ -1536,7 +1536,7 @@ int bt_start_advertising(uint8_t type, FAR const struct bt_eir_s *ad,
   FAR struct bt_hci_cp_le_set_adv_parameters_s *set_param;
   int i;
 
-  if (!ad)
+  if (ad == NULL)
     {
       goto send_scan_rsp;
     }
@@ -1551,7 +1551,7 @@ int bt_start_advertising(uint8_t type, FAR const struct bt_eir_s *ad,
 
   memset(set_data, 0, sizeof(*set_data));
 
-  for (i = 0; ad[i].len; i++)
+  for (i = 0; ad[i].len > 0; i++)
     {
       /* Check if ad fit in the remaining buffer */
 
@@ -1567,7 +1567,7 @@ int bt_start_advertising(uint8_t type, FAR const struct bt_eir_s *ad,
   bt_hci_cmd_send(BT_HCI_OP_LE_SET_ADV_DATA, buf);
 
 send_scan_rsp:
-  if (!sd)
+  if (sd == NULL)
     {
       goto send_set_param;
     }
@@ -1583,7 +1583,7 @@ send_scan_rsp:
 
   memset(scan_rsp, 0, sizeof(*scan_rsp));
 
-  for (i = 0; sd[i].len; i++)
+  for (i = 0; sd[i].len > 0; i++)
     {
       /* Check if ad fit in the remaining buffer */
 
diff --git a/wireless/bluetooth/bt_ioctl.c b/wireless/bluetooth/bt_ioctl.c
index cab370d502..0f06d51dfc 100644
--- a/wireless/bluetooth/bt_ioctl.c
+++ b/wireless/bluetooth/bt_ioctl.c
@@ -183,18 +183,20 @@ static void btnet_scan_callback(FAR const bt_addr_le_t *addr, int8_t rssi,
  *   cached scan results.
  *
  * Input Parameters:
- *   Scan result data
+ *   result - Location to return the scan result data
+ *   maxrsp - The maximum number of responses that can be returned.
  *
  * Returned Value:
- *   None
+ *   On success, the actual number of scan results obtain is returned.  A
+ *   negated errno value is returned on any failure.
  *
  ****************************************************************************/
 
-static int btnet_scan_result(FAR struct bt_scanresult_s *result)
+static int btnet_scan_result(FAR struct bt_scanresponse_s *result,
+                             uint8_t maxrsp)
 {
   uint8_t head;
   uint8_t tail;
-  uint8_t maxrsp;
   uint8_t nrsp;
   int ret;
 
@@ -217,7 +219,6 @@ static int btnet_scan_result(FAR struct bt_scanresult_s *result)
 
   head   = g_scanstate.bs_head;
   tail   = g_scanstate.bs_tail;
-  maxrsp = result->sr_nrsp;
 
   for (nrsp = 0; nrsp < maxrsp && head != tail; nrsp++)
     {
@@ -227,7 +228,7 @@ static int btnet_scan_result(FAR struct bt_scanresult_s *result)
       /* Copy data from the head index into the user buffer */
 
       src  = (FAR const uint8_t *)&g_scanstate.bs_rsp[head];
-      dest = (FAR uint8_t *)&result->sr_rsp[nrsp];
+      dest = (FAR uint8_t *)&result[nrsp];
       memcpy(dest, src, sizeof(struct bt_scanresponse_s));
 
       /* Increment the head index */
@@ -239,9 +240,8 @@ static int btnet_scan_result(FAR struct bt_scanresult_s *result)
     }
 
   g_scanstate.bs_head = head;
-  result->sr_nrsp     = nrsp;
   nxsem_post(&g_scanstate.bs_exclsem);
-  return OK;
+  return nrsp;
 }
 
 /****************************************************************************
@@ -273,40 +273,40 @@ int btnet_ioctl(FAR struct net_driver_s *dev, int cmd, unsigned long arg)
 
   switch (cmd)
     {
-      /* SIOCBT_ADVERTISESTART
+      /* SIOCBTADVSTART
        *   Description:   Set advertisement data, scan response data,
        *                  advertisement parameters and start advertising.
-       *   Input:         Pointer to read-write instance of struct
-       *                  bt_advertisestart_s.
+       *   Input:         Pointer to read-only instance of struct btreq_s.
        *   Output:        None
        */
 
-      case SIOCBT_ADVERTISESTART:
+      case SIOCBTADVSTART:
         {
-          FAR struct bt_advertisestart_s *adv =
-            (FAR struct bt_advertisestart_s *)((uintptr_t)arg);
+          FAR struct btreq_s *btreq =
+            (FAR struct btreq_s *)((uintptr_t)arg);
 
-          if (adv == NULL)
+          if (btreq == NULL)
             {
               ret = -EINVAL;
             }
           else
             {
-              ret = bt_start_advertising(adv->as_type, &adv->as_ad,
-                                         &adv->as_sd);
+              ret = bt_start_advertising(btreq->btr_advtype,
+                                         btreq->btr_advad,
+                                         btreq->btr_advad);
               wlinfo("Start advertising: %d\n", ret);
             }
         }
         break;
 
-      /* SIOCBT_ADVERTISESTOP
+      /* SIOCBTADVSTOP
        *   Description:   Stop advertising.
        *   Input:         A reference to a write-able instance of struct
-       *                  bt_scanstop_s.
+       *                  btreq_s.
        *   Output:        None
        */
 
-      case SIOCBT_ADVERTISESTOP:
+      case SIOCBTADVSTOP:
         {
           wlinfo("Stop advertising\n");
           bt_stop_advertising();
@@ -314,19 +314,19 @@ int btnet_ioctl(FAR struct net_driver_s *dev, int cmd, unsigned long arg)
         }
         break;
 
-      /* SIOCBT_SCANSTART
+      /* SIOCBTSCANSTART
        *   Description:   Start LE scanning.  Buffered scan results may be
-       *                  obtained via SIOCBT_SCANGET
-       *   Input:         A read-only referent to struct bt_scanstart_s.
+       *                  obtained via SIOCBTSCANGET
+       *   Input:         A read-only referent to struct btreq_s.
        *   Output:        None
        */
 
-      case SIOCBT_SCANSTART:
+      case SIOCBTSCANSTART:
         {
-          FAR struct bt_scanstart_s *start =
-            (FAR struct bt_scanstart_s *)((uintptr_t)arg);
+          FAR struct btreq_s *btreq =
+            (FAR struct btreq_s *)((uintptr_t)arg);
 
-          if (start == NULL)
+          if (btreq == NULL)
             {
               ret = -EINVAL;
             }
@@ -346,7 +346,7 @@ int btnet_ioctl(FAR struct net_driver_s *dev, int cmd, unsigned long arg)
               g_scanstate.bs_head     = 0;
               g_scanstate.bs_tail     = 0;
 
-              ret = bt_start_scanning(start->ss_dupenable,
+              ret = bt_start_scanning(btreq->btr_dupenable,
                                       btnet_scan_callback);
               wlinfo("Start scan: %d\n", ret);
 
@@ -359,40 +359,46 @@ int btnet_ioctl(FAR struct net_driver_s *dev, int cmd, unsigned long arg)
         }
         break;
 
-      /* SIOCBT_SCANGET
+      /* SIOCBTSCANGET
        *   Description:   Return scan results buffered since the call time
-       *                  that the SIOCBT_SCANGET command was invoked.
+       *                  that the SIOCBTSCANGET command was invoked.
        *   Input:         A reference to a write-able instance of struct
-       *                  bt_scanresult_s.
+       *                  btreq_s.
        *   Output:        Buffered scan result results are returned in the
        *                  user-provided buffer space.
        */
 
-      case SIOCBT_SCANGET:
+      case SIOCBTSCANGET:
         {
-          FAR struct bt_scanresult_s *result =
-            (FAR struct bt_scanresult_s *)((uintptr_t)arg);
+          FAR struct btreq_s *btreq =
+            (FAR struct btreq_s *)((uintptr_t)arg);
 
-          if (result == NULL)
+          if (btreq == NULL)
             {
               ret = -EINVAL;
             }
           else
             {
-              ret = btnet_scan_result(result);
+              ret = btnet_scan_result(btreq->btr_rsp, btreq->btr_nrsp);
               wlinfo("Get scan results: %d\n", ret);
+
+              if (ret >= 0)
+                {
+                  btreq->btr_nrsp = ret;
+                  ret = OK;
+              }
             }
         }
         break;
 
-      /* SIOCBT_SCANSTOP
+      /* SIOCBTSCANSTOP
        *   Description:   Stop LE scanning and discard any buffered results.
-       *   Input:         A reference to a write-able instance of struct
-       *                  bt_scanstop_s.
+       *   Input:         A reference to a read-only instance of struct
+       *                  btreq_s.
        *   Output:        None
        */
 
-      case SIOCBT_SCANSTOP:
+      case SIOCBTSCANSTOP:
         {
           /* Stop scanning */
 
@@ -404,19 +410,19 @@ int btnet_ioctl(FAR struct net_driver_s *dev, int cmd, unsigned long arg)
         }
         break;
 
-      /* SIOCBT_SECURITY
+      /* SIOCBTSECURITY
        *   Description:   Enable security for a connection.
        *   Input:         A reference to a write-able instance of struct
-       *                  bt_security_s.
+       *                  btreq_s.
        *   Output:        None
        */
 
-      case SIOCBT_SECURITY:
+      case SIOCBTSECURITY:
         {
-          FAR struct bt_security_s *sec =
-            (FAR struct bt_security_s *)((uintptr_t)arg);
+          FAR struct btreq_s *btreq =
+            (FAR struct btreq_s *)((uintptr_t)arg);
 
-          if (sec == NULL)
+          if (btreq == NULL)
             {
               ret = -EINVAL;
             }
@@ -426,7 +432,7 @@ int btnet_ioctl(FAR struct net_driver_s *dev, int cmd, unsigned long arg)
 
               /* Get the connection associated with the provided LE address */
 
-              conn = bt_conn_lookup_addr_le(&sec->se_addr);
+              conn = bt_conn_lookup_addr_le(&btreq->btr_secaddr);
               if (conn == NULL)
                 {
                   wlwarn("WARNING:  Peer not connected\n");
@@ -434,7 +440,7 @@ int btnet_ioctl(FAR struct net_driver_s *dev, int cmd, unsigned long arg)
                 }
               else
                 {
-                  ret = bt_conn_security(conn, sec->se_level);
+                  ret = bt_conn_security(conn, btreq->btr_seclevel);
                   if (ret < 0)
                     {
                       wlerr("ERROR:  Security setting failed: %d\n", ret);
-- 
GitLab