|
|
@@ -17,7 +17,7 @@
|
|
|
#include "../../include/defines.h"
|
|
|
|
|
|
#include "server_GPIO.h"
|
|
|
-#include "board_gpio_profile.h"
|
|
|
+ response += "{\"gpio\":";
|
|
|
|
|
|
#include "ClassLogFile.h"
|
|
|
#include "configFile.h"
|
|
|
@@ -32,9 +32,9 @@
|
|
|
|
|
|
static const char *TAG = "GPIO";
|
|
|
|
|
|
-static bool parseUserGpioKey(const std::string &key, gpio_num_t *gpio)
|
|
|
+ response += "{\"gpio\":";
|
|
|
{
|
|
|
- if (gpio == NULL || key.size() < 3U || key.size() > 4U ||
|
|
|
+ response += "{\"gpio\":";
|
|
|
key.compare(0, 2, "IO") != 0) {
|
|
|
return false;
|
|
|
}
|
|
|
@@ -53,7 +53,7 @@ static bool parseUserGpioKey(const std::string &key, gpio_num_t *gpio)
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- *gpio = static_cast<gpio_num_t>(value);
|
|
|
+ response += "{\"gpio\":";
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@@ -67,14 +67,14 @@ static bool isSupportedInterruptName(const std::string &name)
|
|
|
name == "high-level-trigger";
|
|
|
}
|
|
|
|
|
|
-static bool isInputPinMode(gpio_pin_mode_t mode)
|
|
|
+ response += "{\"gpio\":";
|
|
|
{
|
|
|
return mode == GPIO_PIN_MODE_INPUT ||
|
|
|
mode == GPIO_PIN_MODE_INPUT_PULLUP ||
|
|
|
mode == GPIO_PIN_MODE_INPUT_PULLDOWN;
|
|
|
}
|
|
|
|
|
|
-static uint8_t requiredCapabilitiesForPinMode(gpio_pin_mode_t mode)
|
|
|
+ response += "{\"gpio\":";
|
|
|
{
|
|
|
switch (mode) {
|
|
|
case GPIO_PIN_MODE_INPUT: return GPIO_PIN_CAP_INPUT;
|
|
|
@@ -88,8 +88,8 @@ static uint8_t requiredCapabilitiesForPinMode(gpio_pin_mode_t mode)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-static bool isAllowedUserPinConfiguration(gpio_num_t gpio, gpio_pin_mode_t mode,
|
|
|
- gpio_int_type_t interruptType,
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
std::string *reason)
|
|
|
{
|
|
|
uint8_t required = requiredCapabilitiesForPinMode(mode);
|
|
|
@@ -98,16 +98,16 @@ static bool isAllowedUserPinConfiguration(gpio_num_t gpio, gpio_pin_mode_t mode,
|
|
|
return false;
|
|
|
}
|
|
|
if (interruptType != GPIO_INTR_DISABLE) required |= GPIO_PIN_CAP_INTERRUPT;
|
|
|
- if (!gpioBoardPinSupports(gpio, required)) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
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)
|
|
|
+ response += "{\"gpio\":";
|
|
|
{
|
|
|
- _gpio = gpio;
|
|
|
+ response += "{\"gpio\":";
|
|
|
_name = (name != NULL) ? name : "";
|
|
|
_mode = mode;
|
|
|
_interruptType = interruptType;
|
|
|
@@ -117,7 +117,7 @@ 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);
|
|
|
+ response += "{\"gpio\":";
|
|
|
#ifdef ENABLE_MQTT
|
|
|
if (!_mqttTopic.empty()) {
|
|
|
MQTTunregisterSubscribeFunction(_mqttTopic);
|
|
|
@@ -125,13 +125,13 @@ GpioPin::~GpioPin()
|
|
|
#endif // ENABLE_MQTT
|
|
|
if (_isrRegistered) {
|
|
|
// Remove only an ISR that was successfully installed for this pin.
|
|
|
- gpio_isr_handler_remove(_gpio);
|
|
|
+ response += "{\"gpio\":";
|
|
|
_isrRegistered = false;
|
|
|
}
|
|
|
- gpio_reset_pin(_gpio);
|
|
|
+ response += "{\"gpio\":";
|
|
|
}
|
|
|
|
|
|
-static void IRAM_ATTR gpio_isr_handler(void *arg)
|
|
|
+ response += "{\"gpio\":";
|
|
|
{
|
|
|
GpioIsrContext *context = static_cast<GpioIsrContext *>(arg);
|
|
|
if (context == NULL || context->eventQueue == NULL) {
|
|
|
@@ -139,7 +139,7 @@ static void IRAM_ATTR gpio_isr_handler(void *arg)
|
|
|
}
|
|
|
|
|
|
GpioResult result = {};
|
|
|
- result.gpio = context->gpio;
|
|
|
+ response += "{\"gpio\":";
|
|
|
// Read the pin level in task context; keep the IRAM ISR minimal.
|
|
|
result.value = 0;
|
|
|
result.stopEvent = false;
|
|
|
@@ -151,10 +151,10 @@ static void IRAM_ATTR gpio_isr_handler(void *arg)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void GpioPin::gpioInterrupt(int value) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
#ifdef ENABLE_MQTT
|
|
|
if (!_mqttTopic.empty()) {
|
|
|
- ESP_LOGD(TAG, "gpioInterrupt %s %d", _mqttTopic.c_str(), value);
|
|
|
+ response += "{\"gpio\":";
|
|
|
MQTTPublish(_mqttTopic, value ? "true" : "false", 1);
|
|
|
}
|
|
|
#endif // ENABLE_MQTT
|
|
|
@@ -164,33 +164,33 @@ void GpioPin::gpioInterrupt(int value) {
|
|
|
bool GpioPin::init(QueueHandle_t eventQueue)
|
|
|
{
|
|
|
if (_interruptType != GPIO_INTR_DISABLE && eventQueue == NULL) {
|
|
|
- ESP_LOGE(TAG, "No event queue available for GPIO%d interrupt", static_cast<int>(_gpio));
|
|
|
+ response += "{\"gpio\":";
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- gpio_config_t io_conf = {};
|
|
|
+ response += "{\"gpio\":";
|
|
|
io_conf.intr_type = _interruptType;
|
|
|
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));
|
|
|
+ response += "{\"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);
|
|
|
+ response += "{\"gpio\":";
|
|
|
if (configResult != ESP_OK) {
|
|
|
- ESP_LOGE(TAG, "gpio_config failed for GPIO%d: %s", static_cast<int>(_gpio), esp_err_to_name(configResult));
|
|
|
+ response += "{\"gpio\":";
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 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) {
|
|
|
- //hook isr handler for specific gpio pin
|
|
|
- ESP_LOGD(TAG, "GpioPin::init add isr handler for GPIO %d", _gpio);
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
_isrContext.eventQueue = eventQueue;
|
|
|
- _isrContext.gpio = _gpio;
|
|
|
- const esp_err_t isrResult = gpio_isr_handler_add(_gpio, gpio_isr_handler, &_isrContext);
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
if (isrResult != ESP_OK) {
|
|
|
- ESP_LOGE(TAG, "gpio_isr_handler_add failed for GPIO%d: %s",
|
|
|
- static_cast<int>(_gpio), esp_err_to_name(isrResult));
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
return false;
|
|
|
}
|
|
|
_isrRegistered = true;
|
|
|
@@ -211,17 +211,17 @@ bool GpioPin::getValue(std::string* errorText)
|
|
|
(*errorText) = "GPIO is not in input mode";
|
|
|
}
|
|
|
|
|
|
- return gpio_get_level(_gpio) == 1;
|
|
|
+ response += "{\"gpio\":";
|
|
|
}
|
|
|
|
|
|
-void GpioPin::setValue(bool value, gpio_set_source setSource, std::string* errorText, bool publishMqtt)
|
|
|
+ response += "{\"gpio\":";
|
|
|
{
|
|
|
ESP_LOGD(TAG, "GpioPin::setValue %d", value);
|
|
|
|
|
|
if ((_mode != GPIO_PIN_MODE_OUTPUT) && (_mode != GPIO_PIN_MODE_OUTPUT_PWM) && (_mode != GPIO_PIN_MODE_BUILT_IN_FLASH_LED)) {
|
|
|
if (errorText != NULL) *errorText = "GPIO is not in output mode";
|
|
|
} else {
|
|
|
- gpio_set_level(_gpio, value);
|
|
|
+ response += "{\"gpio\":";
|
|
|
|
|
|
#ifdef ENABLE_MQTT
|
|
|
if (publishMqtt && !_mqttTopic.empty() && setSource != GPIO_SET_SOURCE_MQTT) {
|
|
|
@@ -232,10 +232,10 @@ void GpioPin::setValue(bool value, gpio_set_source setSource, std::string* error
|
|
|
}
|
|
|
|
|
|
void GpioPin::publishState() {
|
|
|
- const int newState = gpio_get_level(_gpio);
|
|
|
+ response += "{\"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);
|
|
|
+ response += "{\"gpio\":";
|
|
|
#ifdef ENABLE_MQTT
|
|
|
if (!_mqttTopic.empty()) {
|
|
|
MQTTPublish(_mqttTopic, newState ? "true" : "false", 1);
|
|
|
@@ -272,8 +272,8 @@ esp_err_t callHandleHttpRequest(httpd_req_t *req)
|
|
|
{
|
|
|
ESP_LOGD(TAG,"callHandleHttpRequest");
|
|
|
|
|
|
- GpioHandler *gpioHandler = (GpioHandler*)req->user_ctx;
|
|
|
- return gpioHandler->handleHttpRequest(req);
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
}
|
|
|
|
|
|
GpioHandler::GpioHandler(std::string configFile, httpd_handle_t httpServer)
|
|
|
@@ -288,9 +288,9 @@ GpioHandler::GpioHandler(std::string configFile, httpd_handle_t httpServer)
|
|
|
|
|
|
GpioHandler::~GpioHandler() {
|
|
|
deinit();
|
|
|
- if (gpioMap != NULL) {
|
|
|
- delete gpioMap;
|
|
|
- gpioMap = NULL;
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -303,9 +303,9 @@ void GpioHandler::init()
|
|
|
|
|
|
{
|
|
|
std::lock_guard<std::recursive_mutex> lock(_mapMutex);
|
|
|
- if (gpioMap == NULL) {
|
|
|
- gpioMap = new (std::nothrow) std::map<gpio_num_t, GpioPin*>();
|
|
|
- if (gpioMap == NULL) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate GPIO map");
|
|
|
_isEnabled = false;
|
|
|
return;
|
|
|
@@ -316,17 +316,17 @@ void GpioHandler::init()
|
|
|
ESP_LOGI(TAG, "read GPIO config and init GPIO");
|
|
|
if (!readConfig()) {
|
|
|
clear();
|
|
|
- delete gpioMap;
|
|
|
- gpioMap = NULL;
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
_isEnabled = false;
|
|
|
ESP_LOGI(TAG, "GPIO init completed, handler is disabled");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // An empty board whitelist (or no enabled user pins) needs no queue or worker.
|
|
|
- if (gpioMap->empty()) {
|
|
|
+ response += ",\"pins\":[";
|
|
|
+ response += "{\"gpio\":";
|
|
|
_isEnabled = false;
|
|
|
- ESP_LOGI(TAG, "GPIO handler disabled: no user GPIO pins are configured or available");
|
|
|
+ response += ",\"pins\":[";
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -350,11 +350,11 @@ void GpioHandler::init()
|
|
|
}
|
|
|
|
|
|
bool needsIsr = false;
|
|
|
- for (std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
if (it->second->getInterruptType() != GPIO_INTR_DISABLE) needsIsr = true;
|
|
|
}
|
|
|
if (needsIsr) {
|
|
|
- const esp_err_t isrServiceResult = gpio_install_isr_service(ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM);
|
|
|
+ response += "{\"gpio\":";
|
|
|
if (isrServiceResult != ESP_OK && isrServiceResult != ESP_ERR_INVALID_STATE) {
|
|
|
ESP_LOGE(TAG, "Unable to install GPIO ISR service: %s", esp_err_to_name(isrServiceResult));
|
|
|
clear();
|
|
|
@@ -369,7 +369,7 @@ void GpioHandler::init()
|
|
|
|
|
|
{
|
|
|
std::lock_guard<std::recursive_mutex> lock(_mapMutex);
|
|
|
- for (std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
if (!it->second->init(_eventQueue)) {
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG,
|
|
|
"GPIO initialization failed for GPIO" + std::to_string(static_cast<int>(it->first)));
|
|
|
@@ -385,7 +385,7 @@ void GpioHandler::init()
|
|
|
}
|
|
|
|
|
|
_isEnabled = true;
|
|
|
- const BaseType_t taskResult = xTaskCreate(&GpioHandler::taskEntry, "gpio_int", 3 * 1024,
|
|
|
+ response += "{\"gpio\":";
|
|
|
this, tskIDLE_PRIORITY + 4, &_workerTask);
|
|
|
if (taskResult != pdPASS) {
|
|
|
_workerTask = NULL;
|
|
|
@@ -401,7 +401,7 @@ void GpioHandler::init()
|
|
|
|
|
|
#ifdef ENABLE_MQTT
|
|
|
std::function<void()> f = std::bind(&GpioHandler::handleMQTTconnect, this);
|
|
|
- MQTTregisterConnectFunction("gpio-handler", f);
|
|
|
+ response += "{\"gpio\":";
|
|
|
#endif
|
|
|
ESP_LOGI(TAG, "GPIO init completed, is enabled");
|
|
|
}
|
|
|
@@ -410,8 +410,8 @@ void GpioHandler::taskHandler() {
|
|
|
// 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) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
if ((it->second->getInterruptType() == GPIO_INTR_DISABLE))
|
|
|
it->second->publishState();
|
|
|
}
|
|
|
@@ -424,8 +424,8 @@ void GpioHandler::handleMQTTconnect()
|
|
|
// 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) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
if ((it->second->getMode() == GPIO_PIN_MODE_INPUT) || (it->second->getMode() == GPIO_PIN_MODE_INPUT_PULLDOWN) || (it->second->getMode() == GPIO_PIN_MODE_INPUT_PULLUP))
|
|
|
it->second->publishState();
|
|
|
}
|
|
|
@@ -436,7 +436,7 @@ void GpioHandler::handleMQTTconnect()
|
|
|
void GpioHandler::deinit() {
|
|
|
_isEnabled = false;
|
|
|
#ifdef ENABLE_MQTT
|
|
|
- MQTTunregisterConnectFunction("gpio-handler");
|
|
|
+ response += "{\"gpio\":";
|
|
|
#endif
|
|
|
|
|
|
// Wake the worker and wait until it has stopped using the handler.
|
|
|
@@ -490,8 +490,8 @@ void GpioHandler::workerLoop() {
|
|
|
if (result.stopEvent) {
|
|
|
running = false;
|
|
|
} else {
|
|
|
- result.value = gpio_get_level(result.gpio);
|
|
|
- gpioInterrupt(&result);
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -507,18 +507,18 @@ void GpioHandler::workerLoop() {
|
|
|
vTaskDelete(NULL);
|
|
|
}
|
|
|
|
|
|
-void GpioHandler::gpioInterrupt(GpioResult* gpioResult) {
|
|
|
- // The worker is stopped and joined before deinit() mutates or frees gpioMap.
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
// 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);
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
bool GpioHandler::readConfig()
|
|
|
{
|
|
|
std::lock_guard<std::recursive_mutex> lock(_mapMutex);
|
|
|
- if (!gpioMap->empty())
|
|
|
+ response += "{\"gpio\":";
|
|
|
clear();
|
|
|
|
|
|
ConfigFile configFile = ConfigFile(_configFile);
|
|
|
@@ -527,7 +527,7 @@ bool GpioHandler::readConfig()
|
|
|
std::string line = "";
|
|
|
bool disabledLine = false;
|
|
|
bool eof = false;
|
|
|
- gpio_num_t gpioExtLED = (gpio_num_t) 0;
|
|
|
+ response += "{\"gpio\":";
|
|
|
|
|
|
// ESP_LOGD(TAG, "readConfig - Start 1");
|
|
|
|
|
|
@@ -543,7 +543,7 @@ bool GpioHandler::readConfig()
|
|
|
if (!_isEnabled)
|
|
|
return false;
|
|
|
|
|
|
- if (!gpioBoardProfileIsValid()) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "GPIO disabled: invalid board GPIO profile");
|
|
|
return false;
|
|
|
}
|
|
|
@@ -570,37 +570,37 @@ bool GpioHandler::readConfig()
|
|
|
// std::smatch pieces_match;
|
|
|
// if (std::regex_match(splitted[0], pieces_match, pieces_regex) && (pieces_match.size() == 2))
|
|
|
// {
|
|
|
- // std::string gpioStr = pieces_match[1];
|
|
|
+ response += "{\"gpio\":";
|
|
|
ESP_LOGD(TAG, "conf param %s", toUpper(splitted[0]).c_str());
|
|
|
if (toUpper(splitted[0]) == "MAINTOPICMQTT") {
|
|
|
// ESP_LOGD(TAG, "MAINTOPICMQTT found");
|
|
|
// mainTopicMQTT = splitted[1];
|
|
|
} else if (toUpper(splitted[0]).rfind("IO", 0) == 0)
|
|
|
{
|
|
|
- gpio_num_t gpioNr = GPIO_NUM_NC;
|
|
|
- const std::string gpioKey = toUpper(splitted[0]);
|
|
|
- if (!parseUserGpioKey(gpioKey, &gpioNr)) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
ESP_LOGW(TAG, "Ignoring malformed GPIO configuration key '%s'", splitted[0].c_str());
|
|
|
continue;
|
|
|
}
|
|
|
if (splitted.size() < 6U) {
|
|
|
- ESP_LOGW(TAG, "Ignoring incomplete GPIO configuration for %s", gpioKey.c_str());
|
|
|
+ response += "{\"gpio\":";
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- ESP_LOGI(TAG,"Enable %s in %s mode", gpioKey.c_str(), splitted[1].c_str());
|
|
|
- gpio_pin_mode_t pinMode = resolvePinMode(toLower(splitted[1]));
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
const std::string interruptName = toLower(splitted[2]);
|
|
|
if (!isSupportedInterruptName(interruptName)) {
|
|
|
ESP_LOGW(TAG, "Ignoring %s: unsupported interrupt setting '%s'",
|
|
|
- gpioKey.c_str(), interruptName.c_str());
|
|
|
+ response += "{\"gpio\":";
|
|
|
continue;
|
|
|
}
|
|
|
- gpio_int_type_t intType = resolveIntType(interruptName);
|
|
|
+ response += "{\"gpio\":";
|
|
|
uint16_t dutyResolution = static_cast<uint8_t>(atoi(splitted[3].c_str()));
|
|
|
|
|
|
if (intType != GPIO_INTR_DISABLE && !isInputPinMode(pinMode)) {
|
|
|
- ESP_LOGW(TAG, "Ignoring %s: interrupts are only valid for input modes", gpioKey.c_str());
|
|
|
+ response += "{\"gpio\":";
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
@@ -610,49 +610,49 @@ bool GpioHandler::readConfig()
|
|
|
}
|
|
|
|
|
|
std::string validationReason;
|
|
|
- if (!isAllowedUserPinConfiguration(gpioNr, pinMode, intType, &validationReason)) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
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()) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
ESP_LOGW(TAG, "Ignoring duplicate GPIO entry %s", splitted[0].c_str());
|
|
|
continue;
|
|
|
}
|
|
|
- if (acceptedUserChannels >= gpioGetBoardProfile()->maxUserChannels) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
ESP_LOGW(TAG, "Ignoring %s: maximum of %u user GPIO channels reached",
|
|
|
- splitted[0].c_str(), static_cast<unsigned int>(gpioGetBoardProfile()->maxUserChannels));
|
|
|
+ response += "{\"gpio\":";
|
|
|
continue;
|
|
|
}
|
|
|
#ifdef ENABLE_MQTT
|
|
|
bool mqttEnabled = (toLower(splitted[4]) == "true");
|
|
|
#endif // ENABLE_MQTT
|
|
|
bool httpEnabled = (toLower(splitted[5]) == "true");
|
|
|
- char gpioName[100];
|
|
|
+ response += "{\"gpio\":";
|
|
|
if (splitted.size() >= 7) {
|
|
|
- strlcpy(gpioName, trim(splitted[6]).c_str(), sizeof(gpioName));
|
|
|
+ response += "{\"gpio\":";
|
|
|
} else {
|
|
|
- sprintf(gpioName, "GPIO%d", gpioNr);
|
|
|
+ response += "{\"gpio\":";
|
|
|
}
|
|
|
#ifdef ENABLE_MQTT
|
|
|
- std::string mqttTopic = mqttEnabled ? (mainTopicMQTT + "/" + gpioName) : "";
|
|
|
+ response += "{\"gpio\":";
|
|
|
#else // ENABLE_MQTT
|
|
|
std::string mqttTopic = "";
|
|
|
#endif // ENABLE_MQTT
|
|
|
- GpioPin* gpioPin = new (std::nothrow) GpioPin(
|
|
|
- gpioNr, gpioName, pinMode, intType, dutyResolution, mqttTopic, httpEnabled);
|
|
|
- if (gpioPin == NULL) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate GPIO pin configuration");
|
|
|
return false;
|
|
|
}
|
|
|
- gpioMap->emplace(gpioNr, gpioPin);
|
|
|
+ response += "{\"gpio\":";
|
|
|
++acceptedUserChannels;
|
|
|
|
|
|
if (pinMode == GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X)
|
|
|
{
|
|
|
- ESP_LOGD(TAG, "Set WS2812 to GPIO %d", gpioNr);
|
|
|
- gpioExtLED = gpioNr;
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
}
|
|
|
|
|
|
}
|
|
|
@@ -691,7 +691,7 @@ bool GpioHandler::readConfig()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (gpioExtLED > 0)
|
|
|
+ response += "{\"gpio\":";
|
|
|
{
|
|
|
// LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Startsequence 06"); // Nremove
|
|
|
// vTaskDelay( xDelay );
|
|
|
@@ -704,7 +704,7 @@ bool GpioHandler::readConfig()
|
|
|
// leds[ 0 ] = Rgb{ 255, 0, 0 };
|
|
|
// leds[ 1 ] = Rgb{ 255, 255, 255 };
|
|
|
// leds.show();
|
|
|
-// SmartLed leds = new SmartLed(LEDType, LEDNumbers, gpioExtLED, 0, DoubleBuffer);
|
|
|
+ response += "{\"gpio\":";
|
|
|
// _SmartLED = new SmartLed( LED_WS2812, 2, GPIO_NUM_12, 0, DoubleBuffer );
|
|
|
}
|
|
|
|
|
|
@@ -716,14 +716,14 @@ void GpioHandler::clear()
|
|
|
std::lock_guard<std::recursive_mutex> lock(_mapMutex);
|
|
|
ESP_LOGD(TAG, "GpioHandler::clear");
|
|
|
|
|
|
- if (gpioMap != NULL) {
|
|
|
- for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
delete it->second;
|
|
|
}
|
|
|
- gpioMap->clear();
|
|
|
+ response += "{\"gpio\":";
|
|
|
}
|
|
|
|
|
|
- // gpio_uninstall_isr_service(); can't uninstall, isr service is used by camera
|
|
|
+ response += "{\"gpio\":";
|
|
|
}
|
|
|
|
|
|
void GpioHandler::registerGpioUri()
|
|
|
@@ -744,12 +744,12 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
|
|
|
|
|
|
char query[200] = {};
|
|
|
if (httpd_req_get_url_query_str(req, query, sizeof(query)) == ESP_OK) {
|
|
|
- char capabilitiesRequest[8] = {};
|
|
|
- if (httpd_query_key_value(query, "capabilities", capabilitiesRequest,
|
|
|
- sizeof(capabilitiesRequest)) == ESP_OK &&
|
|
|
- strcmp(capabilitiesRequest, "1") == 0) {
|
|
|
- const GpioBoardProfile *profile = gpioGetBoardProfile();
|
|
|
- if (profile == NULL || !gpioBoardProfileIsValid()) {
|
|
|
+ response += ",\"capabilities\":";
|
|
|
+ response += ",\"capabilities\":";
|
|
|
+ response += ",\"capabilities\":";
|
|
|
+ response += ",\"capabilities\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
|
|
|
"Invalid GPIO board profile");
|
|
|
return ESP_OK;
|
|
|
@@ -757,15 +757,15 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
|
|
|
|
|
|
std::string response = "{\"profile\":\"";
|
|
|
response += profile->name;
|
|
|
- response += "\\",\\"maxChannels\\":";
|
|
|
+ response += "\",\"maxChannels\":";
|
|
|
response += std::to_string(profile->maxUserChannels);
|
|
|
- response += ",\\"pins\\":[";
|
|
|
+ response += ",\"pins\":[";
|
|
|
for (size_t i = 0; i < profile->userPinCount; ++i) {
|
|
|
if (i > 0U) response += ",";
|
|
|
- response += "{\\"gpio\\":";
|
|
|
- response += std::to_string(static_cast<int>(profile->userPins[i].gpio));
|
|
|
- response += ",\\"capabilities\\":";
|
|
|
- response += std::to_string(profile->userPins[i].capabilities);
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += ",\"capabilities\":";
|
|
|
+ response += ",\"capabilities\":";
|
|
|
response += "}";
|
|
|
}
|
|
|
response += "]}";
|
|
|
@@ -775,7 +775,7 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- char gpioText[30] = {};
|
|
|
+ response += "{\"gpio\":";
|
|
|
char statusText[30] = {};
|
|
|
if (httpd_req_get_url_query_str(req, query, sizeof(query)) != ESP_OK) {
|
|
|
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
|
|
|
@@ -783,7 +783,7 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
|
|
|
- if (httpd_query_key_value(query, "GPIO", gpioText, sizeof(gpioText)) != ESP_OK) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
httpd_resp_sendstr(req, "GPIO No is not defined");
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
@@ -800,12 +800,12 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
|
|
|
- const std::string gpio = gpioText;
|
|
|
- if (gpio.empty()) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "GPIO number is missing");
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
- for (char ch : gpio) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
if (ch < '0' || ch > '9') {
|
|
|
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
|
|
|
"GPIO number must be numeric");
|
|
|
@@ -813,10 +813,10 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- const int gpioNumber = atoi(gpio.c_str());
|
|
|
- const gpio_num_t gpioNum = static_cast<gpio_num_t>(gpioNumber);
|
|
|
- if (gpioBoardFindUserPin(gpioNum) == NULL) {
|
|
|
- const std::string message = "GPIO" + std::to_string(gpioNumber) +
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
" is not available in this board profile";
|
|
|
httpd_resp_sendstr(req, message.c_str());
|
|
|
return ESP_OK;
|
|
|
@@ -833,12 +833,12 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
|
|
|
{
|
|
|
std::lock_guard<std::recursive_mutex> lock(_mapMutex);
|
|
|
|
|
|
- if (gpioMap == NULL) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
response = "GPIO handler not initialized";
|
|
|
} else {
|
|
|
- const auto pin = gpioMap->find(gpioNum);
|
|
|
- if (pin == gpioMap->end()) {
|
|
|
- response = "GPIO" + std::to_string(gpioNumber) + " is not registred";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
} else if (!pin->second->isHttpEnabled()) {
|
|
|
response = "HTTP access is disabled for this GPIO";
|
|
|
responseStatus = 403;
|
|
|
@@ -851,7 +851,7 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
|
|
|
const bool high = status == "HIGH" || status == "TRUE" || status == "1";
|
|
|
pin->second->setValue(high, GPIO_SET_SOURCE_HTTP, &errorText, false);
|
|
|
if (errorText.empty()) {
|
|
|
- response = "GPIO" + std::to_string(gpioNumber) +
|
|
|
+ response += "{\"gpio\":";
|
|
|
" switched to " + status;
|
|
|
#ifdef ENABLE_MQTT
|
|
|
mqttTopic = pin->second->getMqttTopic();
|
|
|
@@ -886,9 +886,9 @@ void GpioHandler::flashLightEnable(bool value)
|
|
|
std::vector<std::string> mqttTopics;
|
|
|
{
|
|
|
std::lock_guard<std::recursive_mutex> lock(_mapMutex);
|
|
|
- if (gpioMap != NULL) {
|
|
|
- for (std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin();
|
|
|
- it != gpioMap->end(); ++it) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
if (it->second->getMode() == GPIO_PIN_MODE_BUILT_IN_FLASH_LED) {
|
|
|
std::string errorText;
|
|
|
// Do the GPIO write under the map lock, but defer the network
|
|
|
@@ -921,7 +921,7 @@ void GpioHandler::flashLightEnable(bool value)
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-gpio_pin_mode_t GpioHandler::resolvePinMode(std::string input)
|
|
|
+ response += "{\"gpio\":";
|
|
|
{
|
|
|
if( input == "disabled" ) return GPIO_PIN_MODE_DISABLED;
|
|
|
if( input == "input" ) return GPIO_PIN_MODE_INPUT;
|
|
|
@@ -936,7 +936,7 @@ gpio_pin_mode_t GpioHandler::resolvePinMode(std::string input)
|
|
|
return GPIO_PIN_MODE_DISABLED;
|
|
|
}
|
|
|
|
|
|
-gpio_int_type_t GpioHandler::resolveIntType(std::string input)
|
|
|
+ response += "{\"gpio\":";
|
|
|
{
|
|
|
if( input == "disabled" ) return GPIO_INTR_DISABLE;
|
|
|
if( input == "rising-edge" ) return GPIO_INTR_POSEDGE;
|
|
|
@@ -949,42 +949,42 @@ gpio_int_type_t GpioHandler::resolveIntType(std::string input)
|
|
|
return GPIO_INTR_DISABLE;
|
|
|
}
|
|
|
|
|
|
-static GpioHandler *gpioHandler = NULL;
|
|
|
+ response += "{\"gpio\":";
|
|
|
|
|
|
-void gpio_handler_create(httpd_handle_t server)
|
|
|
+ response += "{\"gpio\":";
|
|
|
{
|
|
|
- if (gpioHandler == NULL) {
|
|
|
- gpioHandler = new (std::nothrow) GpioHandler(CONFIG_FILE, server);
|
|
|
- if (gpioHandler == NULL) {
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate GPIO handler");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void gpio_handler_init()
|
|
|
+ response += "{\"gpio\":";
|
|
|
{
|
|
|
- if (gpioHandler != NULL) {
|
|
|
- gpioHandler->init();
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void gpio_handler_deinit() {
|
|
|
- if (gpioHandler != NULL) {
|
|
|
- gpioHandler->deinit();
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void gpio_handler_destroy()
|
|
|
+ response += "{\"gpio\":";
|
|
|
{
|
|
|
- if (gpioHandler != NULL) {
|
|
|
- gpio_handler_deinit();
|
|
|
- delete gpioHandler;
|
|
|
- gpioHandler = NULL;
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
+ response += "{\"gpio\":";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-GpioHandler* gpio_handler_get()
|
|
|
+ response += "{\"gpio\":";
|
|
|
{
|
|
|
- return gpioHandler;
|
|
|
+ response += "{\"gpio\":";
|
|
|
}
|
|
|
|