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

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

commit 698bc07240220c061428e6f1f4b2918da4e56b29
Author: Kai Cheng <[email protected]>
AuthorDate: Thu Nov 5 14:15:50 2026 +0800

    wireless/bluetooth/zblue: Add peripheral example
    
    Add zblue_peripheral (CONFIG_ZBLUE_PERIPHERAL_EXAMPLE), written against
    the unmodified Zephyr Bluetooth API: it advertises with the name
    CONFIG_BT_DEVICE_NAME, accepts connections with the GAP and GATT
    services and advertises again after a disconnection.
    
    Tested on sim:zblue with a USB Bluetooth controller and a second
    controller with BlueZ as central: scan, connection, primary service
    discovery, read of the device name, disconnection and reconnection.
    
    Signed-off-by: Kai Cheng <[email protected]>
---
 wireless/bluetooth/zblue/CMakeLists.txt        |  18 ++++
 wireless/bluetooth/zblue/Kconfig               |   7 ++
 wireless/bluetooth/zblue/Makefile              |  10 ++
 wireless/bluetooth/zblue/examples/peripheral.c | 133 +++++++++++++++++++++++++
 4 files changed, 168 insertions(+)

diff --git a/wireless/bluetooth/zblue/CMakeLists.txt 
b/wireless/bluetooth/zblue/CMakeLists.txt
index 67d296372..ee54638ce 100644
--- a/wireless/bluetooth/zblue/CMakeLists.txt
+++ b/wireless/bluetooth/zblue/CMakeLists.txt
@@ -248,4 +248,22 @@ if(CONFIG_ZBLUE)
   nuttx_add_library(zblue STATIC)
   target_sources(zblue PRIVATE ${ZBLUE_GEN}/zblue.o)
   set_target_properties(zblue PROPERTIES LINKER_LANGUAGE C)
+
+  if(CONFIG_ZBLUE_PERIPHERAL_EXAMPLE)
+    nuttx_add_application(
+      NAME
+      zblue_peripheral
+      SRCS
+      examples/peripheral.c
+      STACKSIZE
+      ${CONFIG_DEFAULT_TASK_STACKSIZE}
+      MODULE
+      ${CONFIG_ZBLUE_PERIPHERAL_EXAMPLE}
+      COMPILE_FLAGS
+      ${ZBLUE_FLAGS}
+      INCLUDE_DIRECTORIES
+      ${ZBLUE_INCDIRS}
+      DEPENDS
+      zblue)
+  endif()
 endif()
diff --git a/wireless/bluetooth/zblue/Kconfig b/wireless/bluetooth/zblue/Kconfig
index 197a80317..f4e72de8b 100644
--- a/wireless/bluetooth/zblue/Kconfig
+++ b/wireless/bluetooth/zblue/Kconfig
@@ -83,4 +83,11 @@ config ZBLUE_THREAD_PRIORITY
                Zephyr cooperative priorities are mapped below this value,
                preemptive priorities above it.
 
+config ZBLUE_PERIPHERAL_EXAMPLE
+       tristate "Peripheral example"
+       default n
+       ---help---
+               "zblue_peripheral": advertise and accept connections, with
+               the GAP and GATT services only.
+
 endif # ZBLUE
diff --git a/wireless/bluetooth/zblue/Makefile 
b/wireless/bluetooth/zblue/Makefile
index 7f08dcd88..c2d3004ac 100644
--- a/wireless/bluetooth/zblue/Makefile
+++ b/wireless/bluetooth/zblue/Makefile
@@ -198,4 +198,14 @@ clean::
        $(call DELDIR,$(ZOBJDIR))
        $(call DELFILE,$(ZBLUE_GEN)/zblue.o)
 
+# Example
+
+ifneq ($(CONFIG_ZBLUE_PERIPHERAL_EXAMPLE),)
+PROGNAME  = zblue_peripheral
+PRIORITY  = SCHED_PRIORITY_DEFAULT
+STACKSIZE = $(CONFIG_DEFAULT_TASK_STACKSIZE)
+MODULE    = $(CONFIG_ZBLUE_PERIPHERAL_EXAMPLE)
+MAINSRC   = examples/peripheral.c
+endif
+
 include $(APPDIR)/Application.mk
diff --git a/wireless/bluetooth/zblue/examples/peripheral.c 
b/wireless/bluetooth/zblue/examples/peripheral.c
new file mode 100644
index 000000000..a1308cc14
--- /dev/null
+++ b/wireless/bluetooth/zblue/examples/peripheral.c
@@ -0,0 +1,133 @@
+/****************************************************************************
+ * apps/wireless/bluetooth/zblue/examples/peripheral.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/* Connectable advertising with the GAP and GATT services, written against
+ * the Zephyr Bluetooth API exactly as a Zephyr application would be.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdio.h>
+
+#include <zephyr/kernel.h>
+#include <zephyr/bluetooth/bluetooth.h>
+#include <zephyr/bluetooth/conn.h>
+#include <zephyr/bluetooth/gatt.h>
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct bt_data g_ad[] =
+{
+  BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR),
+  BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME,
+          sizeof(CONFIG_BT_DEVICE_NAME) - 1),
+};
+
+static K_SEM_DEFINE(g_disconnected, 0, 1);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void connected(struct bt_conn *conn, uint8_t err)
+{
+  char addr[BT_ADDR_LE_STR_LEN];
+
+  bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
+  if (err != 0)
+    {
+      printf("zblue: connection to %s failed (0x%02x)\n", addr, err);
+      return;
+    }
+
+  printf("zblue: connected %s\n", addr);
+}
+
+static void disconnected(struct bt_conn *conn, uint8_t reason)
+{
+  char addr[BT_ADDR_LE_STR_LEN];
+
+  bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
+  printf("zblue: disconnected %s (0x%02x)\n", addr, reason);
+  k_sem_give(&g_disconnected);
+}
+
+BT_CONN_CB_DEFINE(g_conn_cb) =
+{
+  .connected    = connected,
+  .disconnected = disconnected,
+};
+
+static int advertise(void)
+{
+  int err;
+
+  err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, g_ad, ARRAY_SIZE(g_ad),
+                        NULL, 0);
+  if (err != 0)
+    {
+      printf("zblue: advertising failed: %d\n", err);
+      return err;
+    }
+
+  printf("zblue: advertising as \"%s\"\n", CONFIG_BT_DEVICE_NAME);
+  return 0;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(int argc, char *argv[])
+{
+  bt_addr_le_t addr;
+  size_t count = 1;
+  char str[BT_ADDR_LE_STR_LEN];
+  int err;
+
+  err = bt_enable(NULL);
+  if (err != 0)
+    {
+      printf("zblue: bt_enable failed: %d\n", err);
+      return 1;
+    }
+
+  bt_id_get(&addr, &count);
+  bt_addr_le_to_str(&addr, str, sizeof(str));
+  printf("zblue: Bluetooth ready, identity %s\n", str);
+
+  for (; ; )
+    {
+      if (advertise() != 0)
+        {
+          return 1;
+        }
+
+      k_sem_take(&g_disconnected, K_FOREVER);
+    }
+
+  return 0;
+}

Reply via email to