Fix crashing caused by calling time() in a critical section (#159)

* Fix for random notify crash in ISR context

Tested for stability over 30 minutes with a daisy chain of 3 esp32

Co-authored-by: h2zero <powellperalta@gmail.com>
This commit is contained in:
Anthony Doud 2020-12-19 21:53:44 -07:00 committed by h2zero
parent a85ac6ad5a
commit 7ed962d963
3 changed files with 6 additions and 3 deletions

View file

@ -473,9 +473,10 @@ void NimBLECharacteristic::setValue(const uint8_t* data, size_t length) {
return; return;
} }
time_t t = time(nullptr);
portENTER_CRITICAL(&m_valMux); portENTER_CRITICAL(&m_valMux);
m_value = std::string((char*)data, length); m_value = std::string((char*)data, length);
m_timestamp = time(nullptr); m_timestamp = t;
portEXIT_CRITICAL(&m_valMux); portEXIT_CRITICAL(&m_valMux);
NIMBLE_LOGD(LOG_TAG, "<< setValue"); NIMBLE_LOGD(LOG_TAG, "<< setValue");

View file

@ -789,9 +789,10 @@ 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());
time_t t = time(nullptr);
portENTER_CRITICAL(&(*characteristic)->m_valMux); portENTER_CRITICAL(&(*characteristic)->m_valMux);
(*characteristic)->m_value = std::string((char *)event->notify_rx.om->om_data, event->notify_rx.om->om_len); (*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_timestamp = t;
portEXIT_CRITICAL(&(*characteristic)->m_valMux); portEXIT_CRITICAL(&(*characteristic)->m_valMux);
if ((*characteristic)->m_notifyCallback != nullptr) { if ((*characteristic)->m_notifyCallback != nullptr) {

View file

@ -447,9 +447,10 @@ std::string NimBLERemoteCharacteristic::readValue(time_t *timestamp) {
} }
} while(rc != 0 && retryCount--); } while(rc != 0 && retryCount--);
time_t t = time(nullptr);
portENTER_CRITICAL(&m_valMux); portENTER_CRITICAL(&m_valMux);
m_value = value; m_value = value;
m_timestamp = time(nullptr); m_timestamp = t;
if(timestamp != nullptr) { if(timestamp != nullptr) {
*timestamp = m_timestamp; *timestamp = m_timestamp;
} }