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 <jorge.gzm@gmail.com>
This commit is contained in:
Jorge Guzman 2026-07-16 15:47:43 -03:00 • committed by Xiang Xiao
parent 11c1b68e21
commit d938fdbb00
2 changed files with 36 additions and 3 deletions

View file

@ -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

View file

@ -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) */