From 35d11bcd7c02ee011b00b5a8ea09228912d3cf09 Mon Sep 17 00:00:00 2001 From: Rob Dobson Date: Sun, 25 Oct 2026 15:57:37 +0100 Subject: [PATCH 1/2] BLE: name the reason for a subscribe event correctly The subscribe event's reason was formatted with getHSErrorMsg(), as if it were a host error code, so a CCCD write was logged as "TryAgain" and the end of the connection as "AlreadyInProgress". getSubscribeReasonName() gives "write", "term" or "restore". The DEBUG_RESP_SUBSCRIPTION log line now includes the CCCD details too (previous and current notify/indicate). Co-Authored-By: Claude Opus 5.5 --- components/BLEManager/BLEGattServer.cpp | 21 ++++++++++++++++++--- components/BLEManager/BLEGattServer.h | 2 ++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/components/BLEManager/BLEGattServer.cpp b/components/BLEManager/BLEGattServer.cpp index 46f7653..d71073b 100644 --- a/components/BLEManager/BLEGattServer.cpp +++ b/components/BLEManager/BLEGattServer.cpp @@ -558,18 +558,33 @@ bool BLEGattServer::handleSubscription(struct ble_gap_event * pEvent, String& st if (isResponseChar) subscribed = (pEvent->subscribe.cur_notify != 0) || (pEvent->subscribe.cur_indicate != 0); statusStr = "subscribe attr_handle=" + String(pEvent->subscribe.attr_handle) + - " reason=" + getHSErrorMsg(pEvent->subscribe.reason) + + " reason=" + getSubscribeReasonName(pEvent->subscribe.reason) + " prevNotify=" + String(pEvent->subscribe.prev_notify) + " curNotify=" + String(pEvent->subscribe.cur_notify) + " prevInd=" + String(pEvent->subscribe.prev_indicate) + " curInd=" + String(pEvent->subscribe.cur_indicate); #ifdef DEBUG_RESP_SUBSCRIPTION - LOG_W(MODULE_PREFIX, "handleSubscription connHandle %d responseChar %s subscribed %s", pEvent->subscribe.conn_handle, - isResponseChar ? "YES" : "NO", subscribed ? "YES" : "NO"); + LOG_W(MODULE_PREFIX, "handleSubscription connHandle %d responseChar %s subscribed %s %s", pEvent->subscribe.conn_handle, + isResponseChar ? "YES" : "NO", subscribed ? "YES" : "NO", statusStr.c_str()); #endif return isResponseChar; } +///////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Name of the reason for a subscribe event (it isn't an error code) +///////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +const char* BLEGattServer::getSubscribeReasonName(uint8_t reason) +{ + switch (reason) + { + case BLE_GAP_SUBSCRIBE_REASON_WRITE: return "write"; + case BLE_GAP_SUBSCRIBE_REASON_TERM: return "term"; + case BLE_GAP_SUBSCRIBE_REASON_RESTORE: return "restore"; + default: return "unknown"; + } +} + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Get HS error message ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/components/BLEManager/BLEGattServer.h b/components/BLEManager/BLEGattServer.h index a2bf216..55251b1 100644 --- a/components/BLEManager/BLEGattServer.h +++ b/components/BLEManager/BLEGattServer.h @@ -106,6 +106,8 @@ class BLEGattServer // Get HS error message static String getHSErrorMsg(int errorCode); + // Name of the reason for a subscribe event (CCCD written, connection ended, or restored when bonded) + static const char* getSubscribeReasonName(uint8_t reason); // Outbound BLEGattOutbound& getOutbound() From f30cce75120fb76bf13aa9bf9dcd024ae480dd80 Mon Sep 17 00:00:00 2001 From: Rob Dobson Date: Sun, 25 Oct 2026 15:57:37 +0100 Subject: [PATCH 2/2] BLE: manufacturer data - no wrap when the fixed data fills the limit The space left for the serial number was advManufValueBytes minus the bytes already in the manufacturer data, an unsigned subtraction that wrapped when the ID and advManufData already filled the limit, so the whole serial number was added and then truncated away. The result was the same, but it now clamps to zero instead. Co-Authored-By: Claude Opus 5.5 --- components/BLEManager/BLEGapServer.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/components/BLEManager/BLEGapServer.cpp b/components/BLEManager/BLEGapServer.cpp index 6657f8f..ac0e971 100644 --- a/components/BLEManager/BLEGapServer.cpp +++ b/components/BLEManager/BLEGapServer.cpp @@ -1761,8 +1761,9 @@ std::vector BLEGapServer::generateManufacturerData(uint16_t manufacture // Convert serial number from ascii hex to binary (will be like BCD) auto serialNoBytes = Raft::getBytesFromHexStr(serialNo.c_str(), MAX_SERIAL_NO_BYTES); - // Use the last N digits of the serial number - uint32_t maxSerialNoBytes = _bleConfig.advManufTotalByteLimit - manufacturerData.size(); + // Use the last N digits of the serial number (none if the ID and fixed data already fill the limit) + uint32_t maxSerialNoBytes = _bleConfig.advManufTotalByteLimit > manufacturerData.size() ? + _bleConfig.advManufTotalByteLimit - manufacturerData.size() : 0; uint32_t startPos = serialNoBytes.size() > maxSerialNoBytes ? serialNoBytes.size() - maxSerialNoBytes : 0; manufacturerData.insert(manufacturerData.end(), serialNoBytes.begin() + startPos, serialNoBytes.begin() + serialNoBytes.size()); }