Просмотр исходного кода

Enforce per-pin HTTP policy and reject duplicate GPIO entries

SybexXx 3 дней назад
Родитель
Сommit
726ea2898b
1 измененных файлов с 16 добавлено и 12 удалено
  1. 16 12
      code/components/jomjol_controlGPIO/server_GPIO.cpp

+ 16 - 12
code/components/jomjol_controlGPIO/server_GPIO.cpp

@@ -40,7 +40,9 @@ static uint8_t requiredCapabilitiesForPinMode(gpio_pin_mode_t mode)
         case GPIO_PIN_MODE_INPUT_PULLUP: return GPIO_PIN_CAP_INPUT | GPIO_PIN_CAP_PULL_UP;
         case GPIO_PIN_MODE_INPUT_PULLDOWN: return GPIO_PIN_CAP_INPUT | GPIO_PIN_CAP_PULL_DOWN;
         case GPIO_PIN_MODE_OUTPUT: return GPIO_PIN_CAP_OUTPUT;
-        case GPIO_PIN_MODE_OUTPUT_PWM: return GPIO_PIN_CAP_OUTPUT | GPIO_PIN_CAP_PWM;
+        case GPIO_PIN_MODE_OUTPUT_PWM:
+            /* PWM remains unavailable until a tested LEDC backend is implemented. */
+            return 0U;
         default: return 0U;
     }
 }
@@ -69,6 +71,7 @@ GpioPin::GpioPin(gpio_num_t gpio, const char* name, gpio_pin_mode_t mode, gpio_i
     _mode = mode;
     _interruptType = interruptType;    
     _mqttTopic = mqttTopic;
+    _httpEnable = httpEnable;
 }
 
 GpioPin::~GpioPin()
@@ -448,8 +451,11 @@ bool GpioHandler::readConfig()
             }
 
             const auto existingPin = gpioMap->find(gpioNr);
-            if (existingPin == gpioMap->end() &&
-                acceptedUserChannels >= gpioGetBoardProfile()->maxUserChannels) {
+            if (existingPin != gpioMap->end()) {
+                ESP_LOGW(TAG, "Ignoring duplicate GPIO entry %s", splitted[0].c_str());
+                continue;
+            }
+            if (acceptedUserChannels >= gpioGetBoardProfile()->maxUserChannels) {
                 ESP_LOGW(TAG, "Ignoring %s: maximum of %u user GPIO channels reached",
                          splitted[0].c_str(), static_cast<unsigned int>(gpioGetBoardProfile()->maxUserChannels));
                 continue;
@@ -475,15 +481,8 @@ bool GpioHandler::readConfig()
                 LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate GPIO pin configuration");
                 return false;
             }
-            const auto existingPin = gpioMap->find(gpioNr);
-            if (existingPin != gpioMap->end()) {
-                delete gpioPin;
-                LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Ignoring duplicate GPIO entry " + splitted[0]);
-                continue;
-            } else {
-                gpioMap->emplace(gpioNr, gpioPin);
-                ++acceptedUserChannels;
-            }
+            gpioMap->emplace(gpioNr, gpioPin);
+            ++acceptedUserChannels;
 
             if (pinMode == GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X)
             {
@@ -641,6 +640,11 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
         httpd_resp_send(req, resp_str, strlen(resp_str));  
         return ESP_OK;     
     }
+
+    if (!(*gpioMap)[gpio_num]->isHttpEnabled()) {
+        httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "HTTP access is disabled for this GPIO");
+        return ESP_OK;
+    }
     
     if (status == "") 
     {