This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 6c147f8484d13e67c54e700385d86e2784bffd2a Author: Lingao Meng <[email protected]> AuthorDate: Tue Sep 8 17:18:25 2026 +0800 arch/sim: Fix HCI socket watchdog lifetime Start the simulated HCI socket receive watchdog only after the host HCI socket has been opened successfully. The previous code armed the watchdog immediately after driver registration, before the Bluetooth stack opened the driver and before the device had a valid host fd. Cancel the watchdog on close/free and close any opened host fd during allocation-failure cleanup. This keeps the polling path tied to the actual socket lifetime and prevents the watchdog from polling an invalid host fd. Testing: Host: Ubuntu 22.04 x86_64 Board/config: sim:bthcisock Style checks: git diff --check HEAD~2..HEAD PATH=/home/mi/bsim-auto-test/.venv/bin:$PATH \ ./tools/checkpatch.sh -c -u -m -g HEAD~2..HEAD Clean build: make distclean ./tools/configure.sh -l -a ../../nuttx-apps sim:bthcisock kconfig-tweak --file .config --set-val STACK_USAGE_WARNING 0 make olddefconfig make -j16 Default startup smoke test: printf 'poweroff\n' | timeout 10s ./nuttx Verified the sim still reaches NSH and powers off cleanly. When no host HCI controller is available through the default BlueZ target, the board reports sim_bthcisock_register() failure and continues booting; no invalid-fd watchdog crash occurs. Assisted-by: OpenAI Codex Signed-off-by: Lingao Meng <[email protected]> --- arch/sim/src/sim/sim_hcisocket.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/arch/sim/src/sim/sim_hcisocket.c b/arch/sim/src/sim/sim_hcisocket.c index 462bc182a33..387c2f1cca5 100644 --- a/arch/sim/src/sim/sim_hcisocket.c +++ b/arch/sim/src/sim/sim_hcisocket.c @@ -88,6 +88,7 @@ static int bthcisock_send(struct bt_driver_s *drv, static int bthcisock_open(struct bt_driver_s *drv); static void bthcisock_close(struct bt_driver_s *drv); static int bthcisock_receive(struct bt_driver_s *drv); +static void sim_bthcisock_interrupt(wdparm_t arg); /**************************************************************************** * Private Functions @@ -127,6 +128,7 @@ static void bthcisock_close(struct bt_driver_s *drv) { struct bthcisock_s *dev = (struct bthcisock_s *)drv; + wd_cancel(&dev->wdog); host_bthcisock_close(dev->fd); dev->fd = -1; } @@ -153,10 +155,9 @@ static int bthcisock_receive(struct bt_driver_s *drv) hdr = (union bt_hdr_u *)&dev->rxbuf[H4_HEADER_SIZE]; switch (dev->rxbuf[0]) { - case H4_EVT: - { + case H4_EVT: if (dev->rxlen < H4_HEADER_SIZE - + sizeof (struct bt_hci_evt_hdr_s)) + + sizeof(struct bt_hci_evt_hdr_s)) { return ret; } @@ -164,10 +165,8 @@ static int bthcisock_receive(struct bt_driver_s *drv) type = BT_EVT; pktlen = H4_HEADER_SIZE + sizeof(struct bt_hci_evt_hdr_s) + hdr->evt.len; - } - break; - case H4_ACL: - { + break; + case H4_ACL: if (dev->rxlen < H4_HEADER_SIZE + sizeof(struct bt_hci_acl_hdr_s)) { @@ -177,10 +176,8 @@ static int bthcisock_receive(struct bt_driver_s *drv) type = BT_ACL_IN; pktlen = H4_HEADER_SIZE + sizeof(struct bt_hci_acl_hdr_s) + hdr->acl.len; - } - break; - case H4_ISO: - { + break; + case H4_ISO: if (dev->rxlen < H4_HEADER_SIZE + sizeof(struct bt_hci_iso_hdr_s)) { @@ -190,10 +187,9 @@ static int bthcisock_receive(struct bt_driver_s *drv) type = BT_ISO_IN; pktlen = H4_HEADER_SIZE + sizeof(struct bt_hci_iso_hdr_s) + hdr->iso.len; - } - break; - default: - return -EINVAL; + break; + default: + return -EINVAL; } if (dev->rxlen < pktlen) @@ -223,6 +219,7 @@ static int bthcisock_open(struct bt_driver_s *drv) } dev->fd = fd; + wd_start(&dev->wdog, 0, sim_bthcisock_interrupt, (wdparm_t)dev); return OK; } @@ -253,6 +250,12 @@ static struct bthcisock_s *bthcisock_alloc(int dev_id) static void bthcisock_free(struct bthcisock_s *dev) { + wd_cancel(&dev->wdog); + if (dev->fd >= 0) + { + host_bthcisock_close(dev->fd); + } + kmm_free(dev); } @@ -315,7 +318,5 @@ int sim_bthcisock_register(int dev_id) return ret; } - wd_start(&dev->wdog, 0, sim_bthcisock_interrupt, (wdparm_t)dev); - return 0; }
