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

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

commit d938fdbb001192cbdcf018dde95e947d2219b1f1
Author: Jorge Guzman <[email protected]>
AuthorDate: Thu Jul 16 15:47:43 2026 -0300

    risc-v/gd32vw55x: restart BLE advertising after disconnection
    
    A connection stops the legacy advertising set and the vendor stack
    leaves it stopped, so after the first central connected (or aborted
    the attempt) the device was never discoverable again until a reset.
    
    Register a connection callback and restart the advertising set with
    ble_adv_restart() on every disconnection event. This covers both the
    clean disconnect and the aborted-connection case: an incomplete
    connection holds the (single) link until the supervision timeout, and
    its disconnection event then restarts the advertising the same way.
    
    Validated on hardware (GD32VW553K-START): repeated
    scan/connect/write/disconnect cycles from a Linux central all find the
    device advertising again after the previous cycle, where it previously
    required a reset after the first connection.
    
    Assisted-by: Claude Opus 4.8
    Signed-off-by: Jorge Guzman <[email protected]>
---
 .../gd32vw55x/boards/gd32vw553k-start/index.rst    |  7 +++--
 arch/risc-v/src/gd32vw55x/gdble/gd32_ble.c         | 32 ++++++++++++++++++++++
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git 
a/Documentation/platforms/risc-v/gd32vw55x/boards/gd32vw553k-start/index.rst 
b/Documentation/platforms/risc-v/gd32vw55x/boards/gd32vw553k-start/index.rst
index 2e90d40f6c2..896eb68eebc 100644
--- a/Documentation/platforms/risc-v/gd32vw55x/boards/gd32vw553k-start/index.rst
+++ b/Documentation/platforms/risc-v/gd32vw55x/boards/gd32vw553k-start/index.rst
@@ -303,9 +303,10 @@ ble
 ``wapi`` plus BLE (``CONFIG_GD32VW55X_BLE``) and the demo GATT service
 (``CONFIG_GD32VW55X_BLE_GATT_DEMO``).  The board advertises a connectable set
 named ``NuttX`` and registers a minimal "transparent UART" service (16-bit
-UUIDs ``0xffe0`` / RX ``0xffe1`` / TX ``0xffe2``).  A central connects,
-discovers the service, and a write to the RX characteristic is logged on the
-board console::
+UUIDs ``0xffe0`` / RX ``0xffe1`` / TX ``0xffe2``). Advertising restarts on
+every disconnection, so the device stays discoverable across connections. A
+central connects, discovers the service, and a write to the RX characteristic
+is logged on the board console::
 
   nsh> BLE RX (11): Hello world
 
diff --git a/arch/risc-v/src/gd32vw55x/gdble/gd32_ble.c 
b/arch/risc-v/src/gd32vw55x/gdble/gd32_ble.c
index 33a7277b7f6..76d7f1d2c2e 100644
--- a/arch/risc-v/src/gd32vw55x/gdble/gd32_ble.c
+++ b/arch/risc-v/src/gd32vw55x/gdble/gd32_ble.c
@@ -60,6 +60,7 @@
 #include "ble_gap.h"
 #include "ble_adapter.h"
 #include "ble_adv.h"
+#include "ble_conn.h"
 #include "wrapper_os.h"
 #include "gd32vw55x_platform.h"
 
@@ -217,6 +218,8 @@ static void gd32_ble_adv_start(uint8_t adv_idx)
  *
  ****************************************************************************/
 
+static uint8_t g_adv_idx = 0xff;   /* 0xff = no advertising set yet */
+
 static void gd32_ble_adv_evt_handler(ble_adv_evt_t adv_evt, void *p_data,
                                      void *p_context)
 {
@@ -231,6 +234,7 @@ static void gd32_ble_adv_evt_handler(ble_adv_evt_t adv_evt, 
void *p_data,
 
   if (chg->state == BLE_ADV_STATE_CREATE)
     {
+      g_adv_idx = chg->adv_idx;
       gd32_ble_adv_start(chg->adv_idx);
     }
   else if (chg->state == BLE_ADV_STATE_START)
@@ -239,6 +243,30 @@ static void gd32_ble_adv_evt_handler(ble_adv_evt_t 
adv_evt, void *p_data,
     }
 }
 
+/****************************************************************************
+ * Name: gd32_ble_conn_evt_handler
+ *
+ * Description:
+ *   A connection stops the legacy advertising set and the stack leaves it
+ *   stopped, so after the first central connects (or aborts the attempt)
+ *   the device would never be discoverable again until a reset.  Restart
+ *   the set on every disconnection.
+ *
+ ****************************************************************************/
+
+static void gd32_ble_conn_evt_handler(ble_conn_evt_t event,
+                                      ble_conn_data_u *p_data)
+{
+  if (event == BLE_CONN_EVT_STATE_CHG &&
+      p_data->conn_state.state == BLE_CONN_STATE_DISCONNECTD &&
+      g_adv_idx != 0xff)
+    {
+      wlinfo("BLE disconnected (reason 0x%x), restarting advertising\n",
+             p_data->conn_state.info.discon_info.reason);
+      ble_adv_restart(g_adv_idx);
+    }
+}
+
 /****************************************************************************
  * Name: gd32_ble_adv_create
  *
@@ -483,6 +511,10 @@ int gd32_ble_initialize(void)
 
   ble_adp_callback_register(gd32_ble_adp_evt_handler);
 
+  /* Restart advertising when a central disconnects */
+
+  ble_conn_callback_register(gd32_ble_conn_evt_handler);
+
 #ifdef CONFIG_GD32VW55X_BLE_GATT_DEMO
   /* Register the demo GATT service (RX write / TX notify) */
 

Reply via email to