This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 772807c50ffa7d596016289e8abd9aa273bd09b5
Author: chengkai <[email protected]>
AuthorDate: Tue Jan 2 16:37:00 2024 +0800

    bluetooth: add bt_driver_register interface
    
    add bt_driver_register interface, which could handle
    these cases:bth4 bth5 btbridge btslip and btuart_lowerhalf_s etc.
    
    Signed-off-by: chengkai <[email protected]>
---
 drivers/wireless/bluetooth/Kconfig           |   6 ++
 drivers/wireless/bluetooth/Make.defs         |   2 +
 drivers/wireless/bluetooth/bt_driver.c       | 133 +++++++++++++++++++++++++++
 drivers/wireless/bluetooth/bt_uart.c         |   4 +-
 include/nuttx/wireless/bluetooth/bt_driver.h |  21 +++++
 5 files changed, 164 insertions(+), 2 deletions(-)

diff --git a/drivers/wireless/bluetooth/Kconfig 
b/drivers/wireless/bluetooth/Kconfig
index bdcad8f9ff..6a2126955a 100644
--- a/drivers/wireless/bluetooth/Kconfig
+++ b/drivers/wireless/bluetooth/Kconfig
@@ -5,6 +5,12 @@
 
 if DRIVERS_BLUETOOTH
 
+config BLUETOOTH_DEVICE_ID
+       int "Bluetooth Device ID, eg: /dev/ttyBT0"
+       default 0
+       ---help---
+               Bluetooth Device ID.
+
 config BLUETOOTH_UART
        bool "Bluetooth UART driver"
        default n
diff --git a/drivers/wireless/bluetooth/Make.defs 
b/drivers/wireless/bluetooth/Make.defs
index a5bc196bb8..0b10e46f06 100644
--- a/drivers/wireless/bluetooth/Make.defs
+++ b/drivers/wireless/bluetooth/Make.defs
@@ -24,6 +24,8 @@ ifeq ($(CONFIG_DRIVERS_BLUETOOTH),y)
 
 # Include Bluetooth drivers into the build
 
+CSRCS += bt_driver.c
+
 ifeq ($(CONFIG_BLUETOOTH_UART),y)
 CSRCS += bt_uart.c
 ifeq ($(CONFIG_BLUETOOTH_UART_GENERIC),y)
diff --git a/drivers/wireless/bluetooth/bt_driver.c 
b/drivers/wireless/bluetooth/bt_driver.c
new file mode 100644
index 0000000000..bb2a80efdd
--- /dev/null
+++ b/drivers/wireless/bluetooth/bt_driver.c
@@ -0,0 +1,133 @@
+/****************************************************************************
+ * drivers/wireless/bluetooth/bt_driver.c
+ *
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/wireless/bluetooth/bt_driver.h>
+
+#ifdef CONFIG_UART_BTH4
+#  include <nuttx/serial/uart_bth4.h>
+#endif
+
+#ifdef CONFIG_BLUETOOTH_BRIDGE
+#  include <nuttx/wireless/bluetooth/bt_bridge.h>
+#endif
+
+#ifdef CONFIG_BLUETOOTH_SLIP
+#  include <nuttx/wireless/bluetooth/bt_slip.h>
+#endif
+
+#include <stdio.h>
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int bt_driver_register_internal(FAR struct bt_driver_s *driver,
+                                       int id, bool bt)
+{
+#ifdef CONFIG_UART_BTH4
+  char name[32];
+
+  if (bt)
+    {
+      snprintf(name, sizeof(name), "/dev/ttyBT%d", id);
+    }
+  else
+    {
+      snprintf(name, sizeof(name), "/dev/ttyBLE%d", id);
+    }
+
+  return uart_bth4_register(name, driver);
+#elif defined(CONFIG_NET_BLUETOOTH)
+  return bt_netdev_register(driver);
+#else
+# error "Please select CONFIG_UART_BTH4 or CONFIG_NET_BLUETOOTH"
+#endif
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bt_driver_register_with_id
+ *
+ * Description:
+ *   Register bluetooth driver.
+ *
+ * Input Parameters:
+ *   driver - an instance of the bt_driver_s interface
+ *   id     - bluetooth device id
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+int bt_driver_register_with_id(FAR struct bt_driver_s *driver, int id)
+{
+#ifdef CONFIG_BLUETOOTH_BRIDGE
+  FAR struct bt_driver_s *btdrv;
+  FAR struct bt_driver_s *bledrv;
+#endif
+  int ret;
+
+#ifdef CONFIG_BLUETOOTH_SLIP
+  driver = bt_slip_register(driver);
+  if (driver == NULL)
+    {
+      return -ENOMEM;
+    }
+#endif
+
+#ifdef CONFIG_BLUETOOTH_BRIDGE
+  ret = bt_bridge_register(driver, &btdrv, &bledrv);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = bt_driver_register_internal(btdrv, id, true);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = bt_driver_register_internal(bledrv, id, false);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+#else
+  ret = bt_driver_register_internal(driver, id, true);
+  if (ret < 0)
+    {
+      return ret;
+    }
+#endif
+
+  return ret;
+}
diff --git a/drivers/wireless/bluetooth/bt_uart.c 
b/drivers/wireless/bluetooth/bt_uart.c
index 22373a48a5..b6b30268ca 100644
--- a/drivers/wireless/bluetooth/bt_uart.c
+++ b/drivers/wireless/bluetooth/bt_uart.c
@@ -353,10 +353,10 @@ int btuart_register(FAR const struct btuart_lowerhalf_s 
*lower)
       return ret;
     }
 
-  ret = bt_netdev_register(driver);
+  ret = bt_driver_register(driver);
   if (ret < 0)
     {
-      wlerr("ERROR: bt_netdev_register failed: %d\n", ret);
+      wlerr("ERROR: bt_driver_register failed: %d\n", ret);
       kmm_free(driver);
     }
 
diff --git a/include/nuttx/wireless/bluetooth/bt_driver.h 
b/include/nuttx/wireless/bluetooth/bt_driver.h
index 1f268b00f6..4d61a67f5a 100644
--- a/include/nuttx/wireless/bluetooth/bt_driver.h
+++ b/include/nuttx/wireless/bluetooth/bt_driver.h
@@ -49,6 +49,9 @@
 #define bt_netdev_receive(btdev, type, data, len) \
         (btdev)->receive(btdev, type, data, len)
 
+#define bt_driver_register(btdev) \
+        bt_driver_register_with_id(btdev, CONFIG_BLUETOOTH_DEVICE_ID)
+
 /****************************************************************************
  * Public Types
  ****************************************************************************/
@@ -136,4 +139,22 @@ int bt_netdev_register(FAR struct bt_driver_s *btdev);
 
 int bt_netdev_unregister(FAR struct bt_driver_s *btdev);
 
+/****************************************************************************
+ * Name: bt_driver_register_with_id
+ *
+ * Description:
+ *   Register bluetooth driver.
+ *
+ * Input Parameters:
+ *   driver - an instance of the bt_driver_s interface
+ *   id     - bluetooth device id
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value is returned on any
+ *   failure.
+ *
+ ****************************************************************************/
+
+int bt_driver_register_with_id(FAR struct bt_driver_s *driver, int id);
+
 #endif /* __INCLUDE_NUTTX_WIRELESS_BLUETOOTH_BT_DRIVER_H */

Reply via email to