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 <lknop@users.noreply.github.com>
This commit is contained in:
h2zero 2020-07-14 15:14:29 -06:00
parent 2a5df0c905
commit 2b4c1cd4f1
3 changed files with 30 additions and 3 deletions

View file

@ -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 `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. 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 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 it's characteristics / descriptors will remain valid and the service can be re-added in the future
using `NimBLEServer::addService`. using `NimBLEServer::addService`.
<br/> <br/>
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.
<br/>
# Client # Client
NimBLERemoteCharacteristic::readValue(time_t\*, bool) NimBLERemoteCharacteristic::readValue(time_t\*, bool)

View file

@ -207,6 +207,7 @@ int NimBLECharacteristic::handleGapEvent(uint16_t conn_handle, uint16_t attr_han
{ {
const ble_uuid_t *uuid; const ble_uuid_t *uuid;
int rc; int rc;
struct ble_gap_conn_desc desc;
NimBLECharacteristic* pCharacteristic = (NimBLECharacteristic*)arg; NimBLECharacteristic* pCharacteristic = (NimBLECharacteristic*)arg;
NIMBLE_LOGD(LOG_TAG, "Characteristic %s %s event", pCharacteristic->getUUID().toString().c_str(), 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 // 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. // so we don't want to call the onRead() callback again.
if(ctxt->om->om_pkthdr_len > 8) { 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);
pCharacteristic->m_pCallbacks->onRead(pCharacteristic, &desc);
} }
portENTER_CRITICAL(&pCharacteristic->m_valMux); portENTER_CRITICAL(&pCharacteristic->m_valMux);
@ -249,10 +253,11 @@ int NimBLECharacteristic::handleGapEvent(uint16_t conn_handle, uint16_t attr_han
len += next->om_len; len += next->om_len;
next = SLIST_NEXT(next, om_next); next = SLIST_NEXT(next, om_next);
} }
rc = ble_gap_conn_find(conn_handle, &desc);
assert(rc == 0);
pCharacteristic->setValue(buf, len); pCharacteristic->setValue(buf, len);
pCharacteristic->m_pCallbacks->onWrite(pCharacteristic); pCharacteristic->m_pCallbacks->onWrite(pCharacteristic);
pCharacteristic->m_pCallbacks->onWrite(pCharacteristic, &desc);
return 0; return 0;
} }
default: default:
@ -524,6 +529,14 @@ void NimBLECharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic
NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onRead: default"); NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onRead: default");
} // onRead } // 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. * @brief Callback function to support a write request.
@ -533,6 +546,14 @@ void NimBLECharacteristicCallbacks::onWrite(NimBLECharacteristic* pCharacteristi
NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onWrite: default"); NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onWrite: default");
} // onWrite } // 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. * @brief Callback function to support a Notify request.

View file

@ -176,7 +176,9 @@ public:
virtual ~NimBLECharacteristicCallbacks(); virtual ~NimBLECharacteristicCallbacks();
virtual void onRead(NimBLECharacteristic* pCharacteristic); 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);
virtual void onWrite(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc);
virtual void onNotify(NimBLECharacteristic* pCharacteristic); virtual void onNotify(NimBLECharacteristic* pCharacteristic);
virtual void onStatus(NimBLECharacteristic* pCharacteristic, Status s, int code); virtual void onStatus(NimBLECharacteristic* pCharacteristic, Status s, int code);
}; };