From 2b4c1cd4f1263293d60a6c8dcade9a9774d7bac2 Mon Sep 17 00:00:00 2001 From: h2zero Date: Tue, 14 Jul 2020 15:14:29 -0600 Subject: [PATCH] Add alternative characteristic read/write callbacks Added alternative characteristic read/write callbacks that carry the connection description information. Fixes h2zero/NimBLE-Arduino#83 Author: lknop --- docs/Improvements_and_updates.md | 6 +++++- src/NimBLECharacteristic.cpp | 25 +++++++++++++++++++++++-- src/NimBLECharacteristic.h | 2 ++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/docs/Improvements_and_updates.md b/docs/Improvements_and_updates.md index abdfccd..e7f3e48 100644 --- a/docs/Improvements_and_updates.md +++ b/docs/Improvements_and_updates.md @@ -45,12 +45,16 @@ A new method `NimBLEServer::advertiseOnDisconnect(bool)` has been implemented to `NimBLEServer::removeService` takes an additional parameter `bool deleteSvc` that if true will delete the service and all characteristics / descriptors belonging to it and invalidating any pointers to them. - If false the service is only removed from visibility by clients. The pointers to the service and it's characteristics / descriptors will remain valid and the service can be re-added in the future using `NimBLEServer::addService`.
+New characteristic read/write callbacks added to NimBLECharacteristicCallbacks that receive a pointer to the connection +description of the client reading/writing. +This is useful when connected to multiple clients to discern which client is performing the operation. +
+ # Client NimBLERemoteCharacteristic::readValue(time_t\*, bool) diff --git a/src/NimBLECharacteristic.cpp b/src/NimBLECharacteristic.cpp index a6269d9..4025d83 100644 --- a/src/NimBLECharacteristic.cpp +++ b/src/NimBLECharacteristic.cpp @@ -207,6 +207,7 @@ int NimBLECharacteristic::handleGapEvent(uint16_t conn_handle, uint16_t attr_han { const ble_uuid_t *uuid; int rc; + struct ble_gap_conn_desc desc; NimBLECharacteristic* pCharacteristic = (NimBLECharacteristic*)arg; NIMBLE_LOGD(LOG_TAG, "Characteristic %s %s event", pCharacteristic->getUUID().toString().c_str(), @@ -219,7 +220,10 @@ int NimBLECharacteristic::handleGapEvent(uint16_t conn_handle, uint16_t attr_han // 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); pCharacteristic->m_pCallbacks->onRead(pCharacteristic); + pCharacteristic->m_pCallbacks->onRead(pCharacteristic, &desc); } portENTER_CRITICAL(&pCharacteristic->m_valMux); @@ -249,10 +253,11 @@ int NimBLECharacteristic::handleGapEvent(uint16_t conn_handle, uint16_t attr_han len += next->om_len; next = SLIST_NEXT(next, om_next); } - + rc = ble_gap_conn_find(conn_handle, &desc); + assert(rc == 0); pCharacteristic->setValue(buf, len); pCharacteristic->m_pCallbacks->onWrite(pCharacteristic); - + pCharacteristic->m_pCallbacks->onWrite(pCharacteristic, &desc); return 0; } default: @@ -524,6 +529,14 @@ void NimBLECharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onRead: default"); } // onRead +/** + * @brief Callback function to support a read request. + * @param [in] pCharacteristic The characteristic that is the source of the event. + * @param [in] desc The connection description struct that is associated with the peer that performed the read. + */ +void NimBLECharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc) { + NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onRead: default"); +} // onRead /** * @brief Callback function to support a write request. @@ -533,6 +546,14 @@ void NimBLECharacteristicCallbacks::onWrite(NimBLECharacteristic* pCharacteristi NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onWrite: default"); } // onWrite +/** + * @brief Callback function to support a write request. + * @param [in] pCharacteristic The characteristic that is the source of the event. + * @param [in] desc The connection description struct that is associated with the peer that performed the write. + */ +void NimBLECharacteristicCallbacks::onWrite(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc) { + NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onWrite: default"); +} // onWrite /** * @brief Callback function to support a Notify request. diff --git a/src/NimBLECharacteristic.h b/src/NimBLECharacteristic.h index a5ac901..58f18b0 100644 --- a/src/NimBLECharacteristic.h +++ b/src/NimBLECharacteristic.h @@ -176,7 +176,9 @@ public: virtual ~NimBLECharacteristicCallbacks(); virtual void onRead(NimBLECharacteristic* pCharacteristic); + virtual void onRead(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc); virtual void onWrite(NimBLECharacteristic* pCharacteristic); + virtual void onWrite(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc); virtual void onNotify(NimBLECharacteristic* pCharacteristic); virtual void onStatus(NimBLECharacteristic* pCharacteristic, Status s, int code); };