From 2cde8bd789da94e37d76b1f084e5ae0f6b10e73a Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Thu, 4 Jul 2024 13:58:33 -0700 Subject: [PATCH] Avoid deadlock BT/LE HID send when disconnected (#2252) Fixes #2251 The 2-phase send could get out of whack if transmission was attempted when no device was connected. Clear things up so if things aren't connected, then no data gets set as pending. --- libraries/HID_Bluetooth/src/PicoBluetoothBLEHID.h | 10 ++++++---- libraries/HID_Bluetooth/src/PicoBluetoothHID.h | 12 +++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/libraries/HID_Bluetooth/src/PicoBluetoothBLEHID.h b/libraries/HID_Bluetooth/src/PicoBluetoothBLEHID.h index 996f292..f251bfc 100644 --- a/libraries/HID_Bluetooth/src/PicoBluetoothBLEHID.h +++ b/libraries/HID_Bluetooth/src/PicoBluetoothBLEHID.h @@ -178,11 +178,13 @@ public: while (connected() && _needToSend) { /* noop busy wait */ } - _needToSend = true; - _sendReport = rpt; - _sendReportLen = len; __lockBluetooth(); - hids_device_request_can_send_now_event(_con_handle); + if (connected()) { + _needToSend = true; + _sendReport = rpt; + _sendReportLen = len; + hids_device_request_can_send_now_event(_con_handle); + } __unlockBluetooth(); while (connected() && _needToSend) { /* noop busy wait */ diff --git a/libraries/HID_Bluetooth/src/PicoBluetoothHID.h b/libraries/HID_Bluetooth/src/PicoBluetoothHID.h index 2285095..ae0fa05 100644 --- a/libraries/HID_Bluetooth/src/PicoBluetoothHID.h +++ b/libraries/HID_Bluetooth/src/PicoBluetoothHID.h @@ -142,12 +142,14 @@ public: } bool send(int id, void *rpt, int len) { - _needToSend = true; - _sendReportID = id; - _sendReport = rpt; - _sendReportLen = len; __lockBluetooth(); - hid_device_request_can_send_now_event(getCID()); + if (connected()) { + _needToSend = true; + _sendReportID = id; + _sendReport = rpt; + _sendReportLen = len; + hid_device_request_can_send_now_event(getCID()); + } __unlockBluetooth(); while (connected() && _needToSend) { /* noop busy wait */