Use NimBLE calls for critical sections

This commit is contained in:
h2zero 2021-12-29 08:10:57 -07:00
parent 8620092c90
commit 9debfcd226
7 changed files with 21 additions and 27 deletions

View file

@ -49,7 +49,6 @@ NimBLECharacteristic::NimBLECharacteristic(const NimBLEUUID &uuid, uint16_t prop
m_pCallbacks = &defaultCallback; m_pCallbacks = &defaultCallback;
m_pService = pService; m_pService = pService;
m_value = ""; m_value = "";
m_valMux = portMUX_INITIALIZER_UNLOCKED;
m_timestamp = 0; m_timestamp = 0;
m_removed = 0; m_removed = 0;
} // NimBLECharacteristic } // NimBLECharacteristic
@ -235,12 +234,12 @@ NimBLEUUID NimBLECharacteristic::getUUID() {
* @return A std::string containing the current characteristic value. * @return A std::string containing the current characteristic value.
*/ */
std::string NimBLECharacteristic::getValue(time_t *timestamp) { std::string NimBLECharacteristic::getValue(time_t *timestamp) {
portENTER_CRITICAL(&m_valMux); ble_npl_hw_enter_critical();
std::string retVal = m_value; std::string retVal = m_value;
if(timestamp != nullptr) { if(timestamp != nullptr) {
*timestamp = m_timestamp; *timestamp = m_timestamp;
} }
portEXIT_CRITICAL(&m_valMux); ble_npl_hw_exit_critical(0);
return retVal; return retVal;
} // getValue } // getValue
@ -251,10 +250,9 @@ std::string NimBLECharacteristic::getValue(time_t *timestamp) {
* @return The length of the current characteristic data. * @return The length of the current characteristic data.
*/ */
size_t NimBLECharacteristic::getDataLength() { size_t NimBLECharacteristic::getDataLength() {
portENTER_CRITICAL(&m_valMux); ble_npl_hw_enter_critical();
size_t len = m_value.length(); size_t len = m_value.length();
portEXIT_CRITICAL(&m_valMux); ble_npl_hw_exit_critical(0);
return len; return len;
} }
@ -287,11 +285,10 @@ int NimBLECharacteristic::handleGapEvent(uint16_t conn_handle, uint16_t attr_han
pCharacteristic->m_pCallbacks->onRead(pCharacteristic, &desc); pCharacteristic->m_pCallbacks->onRead(pCharacteristic, &desc);
} }
portENTER_CRITICAL(&pCharacteristic->m_valMux); ble_npl_hw_enter_critical();
rc = os_mbuf_append(ctxt->om, (uint8_t*)pCharacteristic->m_value.data(), rc = os_mbuf_append(ctxt->om, (uint8_t*)pCharacteristic->m_value.data(),
pCharacteristic->m_value.length()); pCharacteristic->m_value.length());
portEXIT_CRITICAL(&pCharacteristic->m_valMux); ble_npl_hw_exit_critical(0);
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
} }
@ -535,10 +532,10 @@ void NimBLECharacteristic::setValue(const uint8_t* data, size_t length) {
} }
time_t t = time(nullptr); time_t t = time(nullptr);
portENTER_CRITICAL(&m_valMux); ble_npl_hw_enter_critical();
m_value = std::string((char*)data, length); m_value = std::string((char*)data, length);
m_timestamp = t; m_timestamp = t;
portEXIT_CRITICAL(&m_valMux); ble_npl_hw_exit_critical(0);
NIMBLE_LOGD(LOG_TAG, "<< setValue"); NIMBLE_LOGD(LOG_TAG, "<< setValue");
} // setValue } // setValue

View file

@ -155,7 +155,6 @@ private:
NimBLEService* m_pService; NimBLEService* m_pService;
std::string m_value; std::string m_value;
std::vector<NimBLEDescriptor*> m_dscVec; std::vector<NimBLEDescriptor*> m_dscVec;
portMUX_TYPE m_valMux;
time_t m_timestamp; time_t m_timestamp;
uint8_t m_removed; uint8_t m_removed;

View file

@ -948,11 +948,11 @@ uint16_t NimBLEClient::getMTU() {
(*characteristic)->toString().c_str()); (*characteristic)->toString().c_str());
time_t t = time(nullptr); time_t t = time(nullptr);
portENTER_CRITICAL(&(*characteristic)->m_valMux); ble_npl_hw_enter_critical();
(*characteristic)->m_value = std::string((char *)event->notify_rx.om->om_data, (*characteristic)->m_value = std::string((char *)event->notify_rx.om->om_data,
event->notify_rx.om->om_len); event->notify_rx.om->om_len);
(*characteristic)->m_timestamp = t; (*characteristic)->m_timestamp = t;
portEXIT_CRITICAL(&(*characteristic)->m_valMux); ble_npl_hw_exit_critical(0);
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",

View file

@ -49,7 +49,6 @@ NimBLEDescriptor::NimBLEDescriptor(NimBLEUUID uuid, uint16_t properties, uint16_
m_pCharacteristic = pCharacteristic; m_pCharacteristic = pCharacteristic;
m_pCallbacks = &defaultCallbacks; // No initial callback. m_pCallbacks = &defaultCallbacks; // No initial callback.
m_value.attr_value = (uint8_t*) calloc(max_len,1); // Allocate storage for the value. m_value.attr_value = (uint8_t*) calloc(max_len,1); // Allocate storage for the value.
m_valMux = portMUX_INITIALIZER_UNLOCKED;
m_properties = 0; m_properties = 0;
m_removed = 0; m_removed = 0;
@ -162,9 +161,10 @@ int NimBLEDescriptor::handleGapEvent(uint16_t conn_handle, uint16_t attr_handle,
if(ctxt->om->om_pkthdr_len > 8) { if(ctxt->om->om_pkthdr_len > 8) {
pDescriptor->m_pCallbacks->onRead(pDescriptor); pDescriptor->m_pCallbacks->onRead(pDescriptor);
} }
portENTER_CRITICAL(&pDescriptor->m_valMux);
ble_npl_hw_enter_critical();
rc = os_mbuf_append(ctxt->om, pDescriptor->getValue(), pDescriptor->getLength()); rc = os_mbuf_append(ctxt->om, pDescriptor->getValue(), pDescriptor->getLength());
portEXIT_CRITICAL(&pDescriptor->m_valMux); ble_npl_hw_exit_critical(0);
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
} }
@ -235,10 +235,11 @@ void NimBLEDescriptor::setValue(const uint8_t* data, size_t length) {
NIMBLE_LOGE(LOG_TAG, "Size %d too large, must be no bigger than %d", length, m_value.attr_max_len); NIMBLE_LOGE(LOG_TAG, "Size %d too large, must be no bigger than %d", length, m_value.attr_max_len);
return; return;
} }
portENTER_CRITICAL(&m_valMux);
ble_npl_hw_enter_critical();
m_value.attr_len = length; m_value.attr_len = length;
memcpy(m_value.attr_value, data, length); memcpy(m_value.attr_value, data, length);
portEXIT_CRITICAL(&m_valMux); ble_npl_hw_exit_critical(0);
} // setValue } // setValue

View file

@ -90,7 +90,6 @@ private:
NimBLECharacteristic* m_pCharacteristic; NimBLECharacteristic* m_pCharacteristic;
uint8_t m_properties; uint8_t m_properties;
attr_value_t m_value; attr_value_t m_value;
portMUX_TYPE m_valMux;
uint8_t m_removed; uint8_t m_removed;
}; // NimBLEDescriptor }; // NimBLEDescriptor

View file

@ -57,7 +57,6 @@ static const char* LOG_TAG = "NimBLERemoteCharacteristic";
m_pRemoteService = pRemoteService; m_pRemoteService = pRemoteService;
m_notifyCallback = nullptr; m_notifyCallback = nullptr;
m_timestamp = 0; m_timestamp = 0;
m_valMux = portMUX_INITIALIZER_UNLOCKED;
NIMBLE_LOGD(LOG_TAG, "<< NimBLERemoteCharacteristic(): %s", m_uuid.toString().c_str()); NIMBLE_LOGD(LOG_TAG, "<< NimBLERemoteCharacteristic(): %s", m_uuid.toString().c_str());
} // NimBLERemoteCharacteristic } // NimBLERemoteCharacteristic
@ -405,12 +404,12 @@ NimBLEUUID NimBLERemoteCharacteristic::getUUID() {
* @return The value of the remote characteristic. * @return The value of the remote characteristic.
*/ */
std::string NimBLERemoteCharacteristic::getValue(time_t *timestamp) { std::string NimBLERemoteCharacteristic::getValue(time_t *timestamp) {
portENTER_CRITICAL(&m_valMux); ble_npl_hw_enter_critical();
std::string value = m_value; std::string value = m_value;
if(timestamp != nullptr) { if(timestamp != nullptr) {
*timestamp = m_timestamp; *timestamp = m_timestamp;
} }
portEXIT_CRITICAL(&m_valMux); ble_npl_hw_exit_critical(0);
return value; return value;
} }
@ -512,13 +511,13 @@ std::string NimBLERemoteCharacteristic::readValue(time_t *timestamp) {
} while(rc != 0 && retryCount--); } while(rc != 0 && retryCount--);
time_t t = time(nullptr); time_t t = time(nullptr);
portENTER_CRITICAL(&m_valMux); ble_npl_hw_enter_critical();
m_value = value; m_value = value;
m_timestamp = t; m_timestamp = t;
if(timestamp != nullptr) { if(timestamp != nullptr) {
*timestamp = m_timestamp; *timestamp = m_timestamp;
} }
portEXIT_CRITICAL(&m_valMux); ble_npl_hw_exit_critical(0);
NIMBLE_LOGD(LOG_TAG, "<< readValue length: %d rc=%d", value.length(), rc); NIMBLE_LOGD(LOG_TAG, "<< readValue length: %d rc=%d", value.length(), rc);
return value; return value;

View file

@ -159,7 +159,6 @@ private:
std::string m_value; std::string m_value;
notify_callback m_notifyCallback; notify_callback m_notifyCallback;
time_t m_timestamp; time_t m_timestamp;
portMUX_TYPE m_valMux;
// 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;