Explorar el Código

Fix GPIO queue and MQTT callback lifetimes

SybexXx hace 2 días
padre
commit
1e5f3c2979
Se han modificado 1 ficheros con 77 adiciones y 23 borrados
  1. 77 23
      code/components/jomjol_controlGPIO/server_GPIO.cpp

+ 77 - 23
code/components/jomjol_controlGPIO/server_GPIO.cpp

@@ -5,6 +5,8 @@
 #include <string.h>
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
+#include "freertos/queue.h"
+#include <new>
 #include "esp_system.h"
 
 #include "esp_log.h"
@@ -33,7 +35,7 @@ QueueHandle_t gpio_queue_handle = NULL;
 GpioPin::GpioPin(gpio_num_t gpio, const char* name, gpio_pin_mode_t mode, gpio_int_type_t interruptType, uint8_t dutyResolution, std::string mqttTopic, bool httpEnable) 
 {
     _gpio = gpio;
-    _name = name; 
+    _name = (name != NULL) ? name : ""; 
     _mode = mode;
     _interruptType = interruptType;    
     _mqttTopic = mqttTopic;
@@ -42,8 +44,13 @@ GpioPin::GpioPin(gpio_num_t gpio, const char* name, gpio_pin_mode_t mode, gpio_i
 GpioPin::~GpioPin()
 {
     ESP_LOGD(TAG,"reset GPIO pin %d", _gpio);
+#ifdef ENABLE_MQTT
+    if (!_mqttTopic.empty()) {
+        MQTTunregisterSubscribeFunction(_mqttTopic);
+    }
+#endif // ENABLE_MQTT
     if (_interruptType != GPIO_INTR_DISABLE) {
-        //hook isr handler for specific gpio pin
+        // Remove the interrupt before releasing this pin's callback target.
         gpio_isr_handler_remove(_gpio);
     }
     gpio_reset_pin(_gpio);
@@ -209,36 +216,48 @@ GpioHandler::GpioHandler(std::string configFile, httpd_handle_t httpServer)
 }
 
 GpioHandler::~GpioHandler()  {
+    deinit();
     if (gpioMap != NULL) {
-        clear();
         delete gpioMap;
+        gpioMap = NULL;
     }
 }
 
 void GpioHandler::init()
 {
-    // TickType_t xDelay = 60000 / portTICK_PERIOD_MS;
-    // ESP_LOGD(TAG, "wait before start %ldms", (long) xDelay);
-    // vTaskDelay( xDelay );
-
     ESP_LOGD(TAG, "*************** Start GPIOHandler_Init *****************");
 
+    // Stop any previous task, queue and pin callbacks before rebuilding them.
+    deinit();
+
     if (gpioMap == NULL) {
-        gpioMap = new std::map<gpio_num_t, GpioPin*>();
-    } else {
-        clear();
+        gpioMap = new (std::nothrow) std::map<gpio_num_t, GpioPin*>();
+        if (gpioMap == NULL) {
+            LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate GPIO map");
+            _isEnabled = false;
+            return;
+        }
     }
-    
-    
+
     ESP_LOGI(TAG, "read GPIO config and init GPIO");
     if (!readConfig()) {
         clear();
         delete gpioMap;
         gpioMap = NULL;
+        _isEnabled = false;
         ESP_LOGI(TAG, "GPIO init completed, handler is disabled");
         return;
     }
 
+    // The ISR can enqueue as soon as a pin interrupt is registered. Create the
+    // queue before initializing any configured GPIO pins.
+    gpio_queue_handle = xQueueCreate(10, sizeof(GpioResult));
+    if (gpio_queue_handle == NULL) {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate GPIO interrupt queue");
+        clear();
+        _isEnabled = false;
+        return;
+    }
 
     for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
         it->second->init();
@@ -249,16 +268,22 @@ void GpioHandler::init()
     MQTTregisterConnectFunction("gpio-handler", f);
 #endif //ENABLE_MQTT
 
-    if (xHandleTaskGpio == NULL) {
-        gpio_queue_handle = xQueueCreate(10,sizeof(GpioResult));
-        BaseType_t  xReturned = xTaskCreate(&gpioHandlerTask, "gpio_int", 3 * 1024, (void *)this, tskIDLE_PRIORITY + 4, &xHandleTaskGpio);
-        if(xReturned == pdPASS ) {
-            ESP_LOGD(TAG, "xHandletaskGpioHandler started");
-        } else {
-            ESP_LOGD(TAG, "xHandletaskGpioHandler not started %d ", (int)xHandleTaskGpio);
-        }
+    const BaseType_t taskResult = xTaskCreate(&gpioHandlerTask, "gpio_int", 3 * 1024,
+                                               (void *)this, tskIDLE_PRIORITY + 4, &xHandleTaskGpio);
+    if (taskResult != pdPASS) {
+        xHandleTaskGpio = NULL;
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to create GPIO handler task");
+#ifdef ENABLE_MQTT
+        MQTTunregisterConnectFunction("gpio-handler");
+#endif
+        clear();
+        vQueueDelete(gpio_queue_handle);
+        gpio_queue_handle = NULL;
+        _isEnabled = false;
+        return;
     }
 
+    _isEnabled = true;
     ESP_LOGI(TAG, "GPIO init completed, is enabled");
 }
 
@@ -287,11 +312,28 @@ void GpioHandler::deinit() {
 #ifdef ENABLE_MQTT
     MQTTunregisterConnectFunction("gpio-handler");
 #endif //ENABLE_MQTT
-    clear();
+
+    // Stop the worker before deleting its GPIO callback targets or queue.
     if (xHandleTaskGpio != NULL) {
         vTaskDelete(xHandleTaskGpio);
         xHandleTaskGpio = NULL;
     }
+#ifdef __LEDGLOBAL
+    if (leds_global != NULL) {
+        if (!leds_global->wait(pdMS_TO_TICKS(1000))) {
+            ESP_LOGW(TAG, "Timed out waiting for LED transmission before deinit");
+        }
+        delete leds_global;
+        leds_global = NULL;
+    }
+#endif
+    clear();
+
+    if (gpio_queue_handle != NULL) {
+        vQueueDelete(gpio_queue_handle);
+        gpio_queue_handle = NULL;
+    }
+    _isEnabled = false;
 }
 
 void GpioHandler::gpioInterrupt(GpioResult* gpioResult) {
@@ -374,8 +416,20 @@ bool GpioHandler::readConfig()
 #else // ENABLE_MQTT
             std::string mqttTopic = "";
 #endif // ENABLE_MQTT
-            GpioPin* gpioPin = new GpioPin(gpioNr, gpioName, pinMode, intType,dutyResolution, mqttTopic, httpEnabled);
-            (*gpioMap)[gpioNr] = gpioPin;
+            GpioPin* gpioPin = new (std::nothrow) GpioPin(
+                gpioNr, gpioName, pinMode, intType, dutyResolution, mqttTopic, httpEnabled);
+            if (gpioPin == NULL) {
+                LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate GPIO pin configuration");
+                return false;
+            }
+            const auto existingPin = gpioMap->find(gpioNr);
+            if (existingPin != gpioMap->end()) {
+                // Duplicate GPIO entries replace the previous uninitialized definition.
+                delete existingPin->second;
+                existingPin->second = gpioPin;
+            } else {
+                gpioMap->emplace(gpioNr, gpioPin);
+            }
 
             if (pinMode == GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X)
             {