|
|
@@ -17,6 +17,7 @@
|
|
|
#include "../../include/defines.h"
|
|
|
|
|
|
#include "server_GPIO.h"
|
|
|
+#include "board_gpio_profile.h"
|
|
|
|
|
|
#include "ClassLogFile.h"
|
|
|
#include "configFile.h"
|
|
|
@@ -32,6 +33,35 @@
|
|
|
static const char *TAG = "GPIO";
|
|
|
QueueHandle_t gpio_queue_handle = NULL;
|
|
|
|
|
|
+static uint8_t requiredCapabilitiesForPinMode(gpio_pin_mode_t mode)
|
|
|
+{
|
|
|
+ switch (mode) {
|
|
|
+ case GPIO_PIN_MODE_INPUT: return GPIO_PIN_CAP_INPUT;
|
|
|
+ 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;
|
|
|
+ default: return 0U;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static bool isAllowedUserPinConfiguration(gpio_num_t gpio, gpio_pin_mode_t mode,
|
|
|
+ gpio_int_type_t interruptType,
|
|
|
+ std::string *reason)
|
|
|
+{
|
|
|
+ uint8_t required = requiredCapabilitiesForPinMode(mode);
|
|
|
+ if (required == 0U) {
|
|
|
+ if (reason != NULL) *reason = "board-owned or unsupported GPIO mode";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (interruptType != GPIO_INTR_DISABLE) required |= GPIO_PIN_CAP_INTERRUPT;
|
|
|
+ if (!gpioBoardPinSupports(gpio, required)) {
|
|
|
+ if (reason != NULL) *reason = "GPIO is not whitelisted for this board or requested mode";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
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;
|
|
|
@@ -100,19 +130,18 @@ void GpioPin::gpioInterrupt(int value) {
|
|
|
|
|
|
void GpioPin::init()
|
|
|
{
|
|
|
- gpio_config_t io_conf;
|
|
|
- //set interrupt
|
|
|
+ gpio_config_t io_conf = {};
|
|
|
io_conf.intr_type = _interruptType;
|
|
|
- //set as output mode
|
|
|
- io_conf.mode = (_mode == GPIO_PIN_MODE_OUTPUT) || (_mode == GPIO_PIN_MODE_BUILT_IN_FLASH_LED) ? gpio_mode_t::GPIO_MODE_OUTPUT : gpio_mode_t::GPIO_MODE_INPUT;
|
|
|
- //bit mask of the pins that you want to set,e.g.GPIO18/19
|
|
|
- io_conf.pin_bit_mask = (1ULL << _gpio);
|
|
|
- //set pull-down mode
|
|
|
- io_conf.pull_down_en = _mode == GPIO_PIN_MODE_INPUT_PULLDOWN ? gpio_pulldown_t::GPIO_PULLDOWN_ENABLE : gpio_pulldown_t::GPIO_PULLDOWN_DISABLE;
|
|
|
- //set pull-up mode
|
|
|
- io_conf.pull_up_en = _mode == GPIO_PIN_MODE_INPUT_PULLDOWN ? gpio_pullup_t::GPIO_PULLUP_ENABLE : gpio_pullup_t::GPIO_PULLUP_DISABLE;
|
|
|
- //configure GPIO with the given settings
|
|
|
- gpio_config(&io_conf);
|
|
|
+ io_conf.mode = (_mode == GPIO_PIN_MODE_OUTPUT) ? GPIO_MODE_OUTPUT : GPIO_MODE_INPUT;
|
|
|
+ io_conf.pin_bit_mask = (1ULL << static_cast<unsigned int>(_gpio));
|
|
|
+ io_conf.pull_down_en = (_mode == GPIO_PIN_MODE_INPUT_PULLDOWN) ? GPIO_PULLDOWN_ENABLE : GPIO_PULLDOWN_DISABLE;
|
|
|
+ io_conf.pull_up_en = (_mode == GPIO_PIN_MODE_INPUT_PULLUP) ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE;
|
|
|
+
|
|
|
+ const esp_err_t configResult = gpio_config(&io_conf);
|
|
|
+ if (configResult != ESP_OK) {
|
|
|
+ ESP_LOGE(TAG, "gpio_config failed for GPIO%d: %s", static_cast<int>(_gpio), esp_err_to_name(configResult));
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
// if (_interruptType != GPIO_INTR_DISABLE) { // ohne GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X, wenn das genutzt wird, dann soll auch der Handler hier nicht initialisiert werden, da das dann über SmartLED erfolgt.
|
|
|
if ((_interruptType != GPIO_INTR_DISABLE) && (_interruptType != GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X)) {
|
|
|
@@ -368,6 +397,11 @@ bool GpioHandler::readConfig()
|
|
|
if (!_isEnabled)
|
|
|
return false;
|
|
|
|
|
|
+ if (!gpioBoardProfileIsValid()) {
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "GPIO disabled: invalid board GPIO profile");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
// ESP_LOGD(TAG, "readConfig - Start 3");
|
|
|
|
|
|
#ifdef ENABLE_MQTT
|
|
|
@@ -380,6 +414,7 @@ bool GpioHandler::readConfig()
|
|
|
}
|
|
|
#endif // ENABLE_MQTT
|
|
|
bool registerISR = false;
|
|
|
+ size_t acceptedUserChannels = 0U;
|
|
|
while (configFile.getNextLine(&line, disabledLine, eof) && !configFile.isNewParagraph(line))
|
|
|
{
|
|
|
splitted = ZerlegeZeile(line);
|
|
|
@@ -400,6 +435,25 @@ bool GpioHandler::readConfig()
|
|
|
gpio_pin_mode_t pinMode = resolvePinMode(toLower(splitted[1]));
|
|
|
gpio_int_type_t intType = resolveIntType(toLower(splitted[2]));
|
|
|
uint16_t dutyResolution = (uint8_t)atoi(splitted[3].c_str());
|
|
|
+
|
|
|
+ if (pinMode == GPIO_PIN_MODE_DISABLED) {
|
|
|
+ ESP_LOGW(TAG, "Ignoring disabled/unknown GPIO mode in %s", splitted[0].c_str());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ std::string validationReason;
|
|
|
+ if (!isAllowedUserPinConfiguration(gpioNr, pinMode, intType, &validationReason)) {
|
|
|
+ ESP_LOGW(TAG, "Ignoring unsafe GPIO configuration %s: %s", splitted[0].c_str(), validationReason.c_str());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ const auto existingPin = gpioMap->find(gpioNr);
|
|
|
+ if (existingPin == gpioMap->end() &&
|
|
|
+ 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;
|
|
|
+ }
|
|
|
#ifdef ENABLE_MQTT
|
|
|
bool mqttEnabled = (toLower(splitted[4]) == "true");
|
|
|
#endif // ENABLE_MQTT
|
|
|
@@ -423,11 +477,12 @@ bool GpioHandler::readConfig()
|
|
|
}
|
|
|
const auto existingPin = gpioMap->find(gpioNr);
|
|
|
if (existingPin != gpioMap->end()) {
|
|
|
- // Duplicate GPIO entries replace the previous uninitialized definition.
|
|
|
- delete existingPin->second;
|
|
|
- existingPin->second = gpioPin;
|
|
|
+ delete gpioPin;
|
|
|
+ LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Ignoring duplicate GPIO entry " + splitted[0]);
|
|
|
+ continue;
|
|
|
} else {
|
|
|
gpioMap->emplace(gpioNr, gpioPin);
|
|
|
+ ++acceptedUserChannels;
|
|
|
}
|
|
|
|
|
|
if (pinMode == GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X)
|
|
|
@@ -571,11 +626,10 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
|
|
|
|
|
|
int gpionum = stoi(gpio);
|
|
|
|
|
|
- // frei: 16; 12-15; 2; 4 // nur 12 und 13 funktionieren 2: reboot, 4: BlitzLED, 15: PSRAM, 14/15: DMA für SDKarte ???
|
|
|
- gpio_num_t gpio_num = resolvePinNr(gpionum);
|
|
|
- if (gpio_num == GPIO_NUM_NC)
|
|
|
+ gpio_num_t gpio_num = static_cast<gpio_num_t>(gpionum);
|
|
|
+ if (gpioBoardFindUserPin(gpio_num) == NULL)
|
|
|
{
|
|
|
- std::string zw = "GPIO" + std::to_string(gpionum) + " unsupported - only 12 & 13 free";
|
|
|
+ std::string zw = "GPIO" + std::to_string(gpionum) + " is not available in this board profile";
|
|
|
httpd_resp_sendstr_chunk(req, zw.c_str());
|
|
|
httpd_resp_sendstr_chunk(req, NULL);
|
|
|
return ESP_OK;
|