|
|
@@ -152,14 +152,13 @@ static void IRAM_ATTR gpio_isr_handler(void *arg)
|
|
|
}
|
|
|
|
|
|
void GpioPin::gpioInterrupt(int value) {
|
|
|
-#ifdef ENABLE_MQTT
|
|
|
- if (_mqttTopic.compare("") != 0) {
|
|
|
+#ifdef ENABLE_MQTT
|
|
|
+ if (!_mqttTopic.empty()) {
|
|
|
ESP_LOGD(TAG, "gpioInterrupt %s %d", _mqttTopic.c_str(), value);
|
|
|
-
|
|
|
- MQTTPublish(_mqttTopic, value ? "true" : "false", 1);
|
|
|
+ MQTTPublish(_mqttTopic, value ? "true" : "false", 1);
|
|
|
}
|
|
|
-#endif //ENABLE_MQTT
|
|
|
- currentState = value;
|
|
|
+#endif // ENABLE_MQTT
|
|
|
+ currentState.store(value, std::memory_order_relaxed);
|
|
|
}
|
|
|
|
|
|
bool GpioPin::init(QueueHandle_t eventQueue)
|
|
|
@@ -233,14 +232,15 @@ void GpioPin::setValue(bool value, gpio_set_source setSource, std::string* error
|
|
|
}
|
|
|
|
|
|
void GpioPin::publishState() {
|
|
|
- int newState = gpio_get_level(_gpio);
|
|
|
- if (newState != currentState) {
|
|
|
- ESP_LOGD(TAG,"publish state of GPIO %d new state %d", _gpio, newState);
|
|
|
+ const int newState = gpio_get_level(_gpio);
|
|
|
+ const int previousState = currentState.exchange(newState, std::memory_order_relaxed);
|
|
|
+ if (newState != previousState) {
|
|
|
+ ESP_LOGD(TAG, "publish state of GPIO %d new state %d", _gpio, newState);
|
|
|
#ifdef ENABLE_MQTT
|
|
|
- if (_mqttTopic.compare("") != 0)
|
|
|
- MQTTPublish(_mqttTopic, newState ? "true" : "false", 1);
|
|
|
-#endif //ENABLE_MQTT
|
|
|
- currentState = newState;
|
|
|
+ if (!_mqttTopic.empty()) {
|
|
|
+ MQTTPublish(_mqttTopic, newState ? "true" : "false", 1);
|
|
|
+ }
|
|
|
+#endif // ENABLE_MQTT
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -407,7 +407,9 @@ void GpioHandler::init()
|
|
|
}
|
|
|
|
|
|
void GpioHandler::taskHandler() {
|
|
|
- std::lock_guard<std::recursive_mutex> lock(_mapMutex);
|
|
|
+ // The map is immutable while the worker is running. deinit() stops and joins
|
|
|
+ // this worker before clearing the map. Do not hold _mapMutex while publishing
|
|
|
+ // to MQTT: the MQTT client may wait on its event task, which can run callbacks.
|
|
|
if (gpioMap != NULL) {
|
|
|
for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
|
|
|
if ((it->second->getInterruptType() == GPIO_INTR_DISABLE))
|
|
|
@@ -419,7 +421,9 @@ void GpioHandler::taskHandler() {
|
|
|
#ifdef ENABLE_MQTT
|
|
|
void GpioHandler::handleMQTTconnect()
|
|
|
{
|
|
|
- std::lock_guard<std::recursive_mutex> lock(_mapMutex);
|
|
|
+ // MQTTconnected() invokes this while holding connectFunctionMutex. Keep the
|
|
|
+ // GPIO map lock out of synchronous MQTT publishing to avoid lock inversion.
|
|
|
+ // deinit() unregisters this callback before clearing the map.
|
|
|
if (gpioMap != NULL) {
|
|
|
for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
|
|
|
if ((it->second->getMode() == GPIO_PIN_MODE_INPUT) || (it->second->getMode() == GPIO_PIN_MODE_INPUT_PULLDOWN) || (it->second->getMode() == GPIO_PIN_MODE_INPUT_PULLUP))
|
|
|
@@ -504,7 +508,8 @@ void GpioHandler::workerLoop() {
|
|
|
}
|
|
|
|
|
|
void GpioHandler::gpioInterrupt(GpioResult* gpioResult) {
|
|
|
- std::lock_guard<std::recursive_mutex> lock(_mapMutex);
|
|
|
+ // The worker is stopped and joined before deinit() mutates or frees gpioMap.
|
|
|
+ // Avoid holding _mapMutex across the MQTT publish performed by GpioPin.
|
|
|
if ((gpioMap != NULL) && (gpioMap->find(gpioResult->gpio) != gpioMap->end())) {
|
|
|
(*gpioMap)[gpioResult->gpio]->gpioInterrupt(gpioResult->value);
|
|
|
}
|