Skip to content

Commit f1103d9

Browse files
Jeroen88h2zero
andauthored
Update remote characteristic value from a notification (#49)
* Add NimBLERemoteCharacteristic::getValue(time_t *timestamp = nullptr) to get the latest remote characteristic and (optionally) it's timestamp. * Added a timestamp to NimBLEAdvertisedDevice for the moment a device was scanned Co-authored-by: h2zero <[email protected]>
1 parent 4197a1c commit f1103d9

File tree

6 files changed

+36
-5
lines changed

6 files changed

+36
-5
lines changed

src/NimBLEAdvertisedDevice.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,11 @@ uint8_t NimBLEAdvertisedDevice::getAddressType() {
546546
}
547547

548548

549+
time_t NimBLEAdvertisedDevice::getTimestamp() {
550+
return m_timestamp;
551+
}
552+
553+
549554
void NimBLEAdvertisedDevice::setAddressType(uint8_t type) {
550555
m_addressType = type;
551556
}

src/NimBLEAdvertisedDevice.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class NimBLEAdvertisedDevice {
5555
uint8_t* getPayload();
5656
size_t getPayloadLength();
5757
uint8_t getAddressType();
58-
void setAddressType(uint8_t type);
58+
time_t getTimestamp();
59+
void setAddressType(uint8_t type);
5960

6061

6162
bool isAdvertisingService(const NimBLEUUID &uuid);
@@ -111,6 +112,7 @@ class NimBLEAdvertisedDevice {
111112
uint8_t* m_payload;
112113
size_t m_payloadLength = 0;
113114
uint8_t m_addressType;
115+
time_t m_timestamp;
114116
};
115117

116118
/**

src/NimBLEClient.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,14 +720,18 @@ uint16_t NimBLEClient::getMTU() {
720720
if(characteristic != cVector->cend()) {
721721
NIMBLE_LOGD(LOG_TAG, "Got Notification for characteristic %s", (*characteristic)->toString().c_str());
722722

723+
if((*characteristic)->m_semaphoreReadCharEvt.take(0, "notifyValue")) {
724+
(*characteristic)->m_value = std::string((char *)event->notify_rx.om->om_data, event->notify_rx.om->om_len);
725+
(*characteristic)->m_timestamp = time(nullptr);
726+
(*characteristic)->m_semaphoreReadCharEvt.give();
727+
}
723728
if ((*characteristic)->m_notifyCallback != nullptr) {
724729
NIMBLE_LOGD(LOG_TAG, "Invoking callback for notification on characteristic %s",
725730
(*characteristic)->toString().c_str());
726731
(*characteristic)->m_notifyCallback(*characteristic, event->notify_rx.om->om_data,
727732
event->notify_rx.om->om_len,
728733
!event->notify_rx.indication);
729734
}
730-
731735
break;
732736
}
733737
}

src/NimBLERemoteCharacteristic.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ static const char* LOG_TAG = "NimBLERemoteCharacteristic";
6060
m_notifyCallback = nullptr;
6161
m_rawData = nullptr;
6262
m_dataLen = 0;
63+
m_timestamp = 0;
6364
} // NimBLERemoteCharacteristic
6465

6566

@@ -389,8 +390,6 @@ std::string NimBLERemoteCharacteristic::readValue() {
389390

390391
int rc = 0;
391392
int retryCount = 1;
392-
// Clear the value before reading.
393-
m_value = "";
394393

395394
NimBLEClient* pClient = getRemoteService()->getClient();
396395

@@ -402,6 +401,8 @@ std::string NimBLERemoteCharacteristic::readValue() {
402401

403402
do {
404403
m_semaphoreReadCharEvt.take("readValue");
404+
// Clear the value before reading.
405+
m_value = "";
405406

406407
rc = ble_gattc_read_long(pClient->getConnId(), m_handle, 0,
407408
NimBLERemoteCharacteristic::onReadCB,
@@ -440,6 +441,21 @@ std::string NimBLERemoteCharacteristic::readValue() {
440441
} // readValue
441442

442443

444+
/**
445+
* @brief Get the value of the remote characteristic.
446+
* @return The value of the remote characteristic.
447+
*/
448+
std::string NimBLERemoteCharacteristic::getValue(time_t *timestamp) {
449+
m_semaphoreReadCharEvt.take("getValue");
450+
std::string value = m_value;
451+
if(timestamp != nullptr) {
452+
*timestamp = m_timestamp;
453+
}
454+
m_semaphoreReadCharEvt.give();
455+
return value;
456+
}
457+
458+
443459
/**
444460
* @brief Callback for characteristic read operation.
445461
* @return 0 or error code.
@@ -462,6 +478,7 @@ int NimBLERemoteCharacteristic::onReadCB(uint16_t conn_handle,
462478
NIMBLE_LOGD(LOG_TAG, "Got %d bytes", attr->om->om_len);
463479

464480
characteristic->m_value += std::string((char*) attr->om->om_data, attr->om->om_len);
481+
characteristic->m_timestamp = time(nullptr);
465482
return 0;
466483
}
467484
}
@@ -509,7 +526,7 @@ bool NimBLERemoteCharacteristic::registerForNotify(notify_callback notifyCallbac
509526
/**
510527
* @brief Delete the descriptors in the descriptor vector.
511528
* We maintain a vector called m_descriptorVector that contains pointers to BLERemoteDescriptors
512-
* object references. Since we allocated these in this class, we are also responsible for deleteing
529+
* object references. Since we allocated these in this class, we are also responsible for deleting
513530
* them. This method does just that.
514531
* @return N/A.
515532
*/

src/NimBLERemoteCharacteristic.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class NimBLERemoteCharacteristic {
5757
uint8_t readUInt8();
5858
uint16_t readUInt16();
5959
uint32_t readUInt32();
60+
std::string getValue(time_t *timestamp = nullptr);
6061
bool registerForNotify(notify_callback _callback,
6162
bool notifications = true,
6263
bool response = true);
@@ -105,6 +106,7 @@ class NimBLERemoteCharacteristic {
105106
uint8_t* m_rawData;
106107
size_t m_dataLen;
107108
notify_callback m_notifyCallback;
109+
time_t m_timestamp;
108110

109111
// We maintain a vector of descriptors owned by this characteristic.
110112
std::vector<NimBLERemoteDescriptor*> m_descriptorVector;

src/NimBLEScan.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ NimBLEScan::NimBLEScan() {
143143
advertisedDevice->parseAdvertisement(&fields);
144144
advertisedDevice->setScan(pScan);
145145
advertisedDevice->setAdvertisementResult(event->disc.data, event->disc.length_data);
146+
advertisedDevice->m_timestamp = time(nullptr);
146147

147148
if (pScan->m_pAdvertisedDeviceCallbacks) {
148149
// If not active scanning report the result to the listener.

0 commit comments

Comments
 (0)