Update remote characteristic value from a notification

* 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
This commit is contained in:
h2zero 2020-05-29 18:26:41 -06:00
parent 5667c97efe
commit 10f544f80a
7 changed files with 37 additions and 6 deletions

View file

@ -546,6 +546,11 @@ uint8_t NimBLEAdvertisedDevice::getAddressType() {
} }
time_t NimBLEAdvertisedDevice::getTimestamp() {
return m_timestamp;
}
void NimBLEAdvertisedDevice::setAddressType(uint8_t type) { void NimBLEAdvertisedDevice::setAddressType(uint8_t type) {
m_addressType = type; m_addressType = type;
} }

View file

@ -55,7 +55,8 @@ public:
uint8_t* getPayload(); uint8_t* getPayload();
size_t getPayloadLength(); size_t getPayloadLength();
uint8_t getAddressType(); uint8_t getAddressType();
void setAddressType(uint8_t type); time_t getTimestamp();
void setAddressType(uint8_t type);
bool isAdvertisingService(const NimBLEUUID &uuid); bool isAdvertisingService(const NimBLEUUID &uuid);
@ -111,6 +112,7 @@ private:
uint8_t* m_payload; uint8_t* m_payload;
size_t m_payloadLength = 0; size_t m_payloadLength = 0;
uint8_t m_addressType; uint8_t m_addressType;
time_t m_timestamp;
}; };
/** /**

View file

@ -720,6 +720,11 @@ uint16_t NimBLEClient::getMTU() {
if(characteristic != cVector->cend()) { if(characteristic != cVector->cend()) {
NIMBLE_LOGD(LOG_TAG, "Got Notification for characteristic %s", (*characteristic)->toString().c_str()); NIMBLE_LOGD(LOG_TAG, "Got Notification for characteristic %s", (*characteristic)->toString().c_str());
if((*characteristic)->m_semaphoreReadCharEvt.take(0, "notifyValue")) {
(*characteristic)->m_value = std::string((char *)event->notify_rx.om->om_data, event->notify_rx.om->om_len);
(*characteristic)->m_timestamp = time(nullptr);
(*characteristic)->m_semaphoreReadCharEvt.give();
}
if ((*characteristic)->m_notifyCallback != nullptr) { if ((*characteristic)->m_notifyCallback != nullptr) {
NIMBLE_LOGD(LOG_TAG, "Invoking callback for notification on characteristic %s", NIMBLE_LOGD(LOG_TAG, "Invoking callback for notification on characteristic %s",
(*characteristic)->toString().c_str()); (*characteristic)->toString().c_str());
@ -727,7 +732,6 @@ uint16_t NimBLEClient::getMTU() {
event->notify_rx.om->om_len, event->notify_rx.om->om_len,
!event->notify_rx.indication); !event->notify_rx.indication);
} }
break; break;
} }
} }

View file

@ -40,7 +40,7 @@ static const char* LOG_TAG = "NimBLEDevice";
/** /**
* Singletons for the NimBLEDevice. * Singletons for the NimBLEDevice.
*/ */
bool initialized = false; static bool initialized = false;
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
NimBLEScan* NimBLEDevice::m_pScan = nullptr; NimBLEScan* NimBLEDevice::m_pScan = nullptr;
#endif #endif

View file

@ -60,6 +60,7 @@ static const char* LOG_TAG = "NimBLERemoteCharacteristic";
m_notifyCallback = nullptr; m_notifyCallback = nullptr;
m_rawData = nullptr; m_rawData = nullptr;
m_dataLen = 0; m_dataLen = 0;
m_timestamp = 0;
} // NimBLERemoteCharacteristic } // NimBLERemoteCharacteristic
@ -389,8 +390,6 @@ std::string NimBLERemoteCharacteristic::readValue() {
int rc = 0; int rc = 0;
int retryCount = 1; int retryCount = 1;
// Clear the value before reading.
m_value = "";
NimBLEClient* pClient = getRemoteService()->getClient(); NimBLEClient* pClient = getRemoteService()->getClient();
@ -402,6 +401,8 @@ std::string NimBLERemoteCharacteristic::readValue() {
do { do {
m_semaphoreReadCharEvt.take("readValue"); m_semaphoreReadCharEvt.take("readValue");
// Clear the value before reading.
m_value = "";
rc = ble_gattc_read_long(pClient->getConnId(), m_handle, 0, rc = ble_gattc_read_long(pClient->getConnId(), m_handle, 0,
NimBLERemoteCharacteristic::onReadCB, NimBLERemoteCharacteristic::onReadCB,
@ -440,6 +441,21 @@ std::string NimBLERemoteCharacteristic::readValue() {
} // readValue } // readValue
/**
* @brief Get the value of the remote characteristic.
* @return The value of the remote characteristic.
*/
std::string NimBLERemoteCharacteristic::getValue(time_t *timestamp) {
m_semaphoreReadCharEvt.take("getValue");
std::string value = m_value;
if(timestamp != nullptr) {
*timestamp = m_timestamp;
}
m_semaphoreReadCharEvt.give();
return value;
}
/** /**
* @brief Callback for characteristic read operation. * @brief Callback for characteristic read operation.
* @return 0 or error code. * @return 0 or error code.
@ -462,6 +478,7 @@ int NimBLERemoteCharacteristic::onReadCB(uint16_t conn_handle,
NIMBLE_LOGD(LOG_TAG, "Got %d bytes", attr->om->om_len); NIMBLE_LOGD(LOG_TAG, "Got %d bytes", attr->om->om_len);
characteristic->m_value += std::string((char*) attr->om->om_data, attr->om->om_len); characteristic->m_value += std::string((char*) attr->om->om_data, attr->om->om_len);
characteristic->m_timestamp = time(nullptr);
return 0; return 0;
} }
} }
@ -509,7 +526,7 @@ bool NimBLERemoteCharacteristic::registerForNotify(notify_callback notifyCallbac
/** /**
* @brief Delete the descriptors in the descriptor vector. * @brief Delete the descriptors in the descriptor vector.
* We maintain a vector called m_descriptorVector that contains pointers to BLERemoteDescriptors * We maintain a vector called m_descriptorVector that contains pointers to BLERemoteDescriptors
* object references. Since we allocated these in this class, we are also responsible for deleteing * object references. Since we allocated these in this class, we are also responsible for deleting
* them. This method does just that. * them. This method does just that.
* @return N/A. * @return N/A.
*/ */

View file

@ -57,6 +57,7 @@ public:
uint8_t readUInt8(); uint8_t readUInt8();
uint16_t readUInt16(); uint16_t readUInt16();
uint32_t readUInt32(); uint32_t readUInt32();
std::string getValue(time_t *timestamp = nullptr);
bool registerForNotify(notify_callback _callback, bool registerForNotify(notify_callback _callback,
bool notifications = true, bool notifications = true,
bool response = true); bool response = true);
@ -105,6 +106,7 @@ private:
uint8_t* m_rawData; uint8_t* m_rawData;
size_t m_dataLen; size_t m_dataLen;
notify_callback m_notifyCallback; notify_callback m_notifyCallback;
time_t m_timestamp;
// We maintain a vector of descriptors owned by this characteristic. // We maintain a vector of descriptors owned by this characteristic.
std::vector<NimBLERemoteDescriptor*> m_descriptorVector; std::vector<NimBLERemoteDescriptor*> m_descriptorVector;

View file

@ -143,6 +143,7 @@ NimBLEScan::NimBLEScan() {
advertisedDevice->parseAdvertisement(&fields); advertisedDevice->parseAdvertisement(&fields);
advertisedDevice->setScan(pScan); advertisedDevice->setScan(pScan);
advertisedDevice->setAdvertisementResult(event->disc.data, event->disc.length_data); advertisedDevice->setAdvertisementResult(event->disc.data, event->disc.length_data);
advertisedDevice->m_timestamp = time(nullptr);
if (pScan->m_pAdvertisedDeviceCallbacks) { if (pScan->m_pAdvertisedDeviceCallbacks) {
// If not active scanning report the result to the listener. // If not active scanning report the result to the listener.