From 93de7ab8edb8c8283fd9e8187fb5468adaf1771d Mon Sep 17 00:00:00 2001 From: h2zero Date: Sun, 26 Jun 2022 17:03:21 -0600 Subject: [PATCH] Bugfix/onRead callback not called for non-long read commands. --- src/NimBLECharacteristic.cpp | 12 +++++++----- src/NimBLEDescriptor.cpp | 11 ++++++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/NimBLECharacteristic.cpp b/src/NimBLECharacteristic.cpp index 3b95030..9394ac1 100644 --- a/src/NimBLECharacteristic.cpp +++ b/src/NimBLECharacteristic.cpp @@ -273,11 +273,13 @@ int NimBLECharacteristic::handleGapEvent(uint16_t conn_handle, uint16_t attr_han if(ble_uuid_cmp(uuid, &pCharacteristic->getUUID().getNative()->u) == 0){ switch(ctxt->op) { case BLE_GATT_ACCESS_OP_READ_CHR: { - // If the packet header is only 8 bytes this is a follow up of a long read - // so we don't want to call the onRead() callback again. - if(ctxt->om->om_pkthdr_len > 8) { - rc = ble_gap_conn_find(conn_handle, &desc); - assert(rc == 0); + rc = ble_gap_conn_find(conn_handle, &desc); + assert(rc == 0); + + // If the packet header is only 8 bytes this is a follow up of a long read + // so we don't want to call the onRead() callback again. + if(ctxt->om->om_pkthdr_len > 8 || + pCharacteristic->m_value.size() <= (ble_att_mtu(desc.conn_handle) - 3)) { pCharacteristic->m_pCallbacks->onRead(pCharacteristic); pCharacteristic->m_pCallbacks->onRead(pCharacteristic, &desc); } diff --git a/src/NimBLEDescriptor.cpp b/src/NimBLEDescriptor.cpp index 3429d13..eccb677 100644 --- a/src/NimBLEDescriptor.cpp +++ b/src/NimBLEDescriptor.cpp @@ -155,6 +155,7 @@ int NimBLEDescriptor::handleGapEvent(uint16_t conn_handle, uint16_t attr_handle, const ble_uuid_t *uuid; int rc; + struct ble_gap_conn_desc desc; NimBLEDescriptor* pDescriptor = (NimBLEDescriptor*)arg; NIMBLE_LOGD(LOG_TAG, "Descriptor %s %s event", pDescriptor->getUUID().toString().c_str(), @@ -164,9 +165,13 @@ int NimBLEDescriptor::handleGapEvent(uint16_t conn_handle, uint16_t attr_handle, if(ble_uuid_cmp(uuid, &pDescriptor->getUUID().getNative()->u) == 0){ switch(ctxt->op) { case BLE_GATT_ACCESS_OP_READ_DSC: { - // If the packet header is only 8 bytes this is a follow up of a long read - // so we don't want to call the onRead() callback again. - if(ctxt->om->om_pkthdr_len > 8) { + rc = ble_gap_conn_find(conn_handle, &desc); + assert(rc == 0); + + // If the packet header is only 8 bytes this is a follow up of a long read + // so we don't want to call the onRead() callback again. + if(ctxt->om->om_pkthdr_len > 8 || + pDescriptor->m_value.size() <= (ble_att_mtu(desc.conn_handle) - 3)) { pDescriptor->m_pCallbacks->onRead(pDescriptor); }