Files
OTGateway/include/utils.h
T
Yurii 8fba88636e fix: accept form posts for manual sensor values
Accept form submissions alongside the existing JSON sensor value endpoint
for compatibility with the manual sensor use case discussed in PR #270.
Support named value and unnamed body parameters with strict numeric parsing
and bounded input lengths. Preserve authentication, unit conversion, MQTT
publication and noresponse handling.

Share payload trimming and numeric parsing between HTTP and MQTT instead
of maintaining separate parser implementations.

Refs #270
2026-11-29 05:43:51 +03:00

181 lines
4.7 KiB
C++

#pragma once
#include <Arduino.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <cerrno>
#include <cmath>
#include <cstdlib>
#include <optional>
#include <string>
#include <string_view>
#include "types.h"
inline std::string_view trimPayload(std::string_view payload) {
const auto first = payload.find_first_not_of(" \t\r\n");
if (first == std::string_view::npos) {
return {};
}
return payload.substr(first, payload.find_last_not_of(" \t\r\n") - first + 1);
}
// text must be NUL-terminated; length may exclude trailing whitespace.
inline std::optional<float> parseNumber(const char* text, size_t length) {
if (text == nullptr || length == 0) {
return std::nullopt;
}
const auto payload = std::string_view(text, length);
const char first = payload.front();
// strtof also accepts leading whitespace, '+' and hexadecimal numbers.
if ((first < '0' || first > '9') && first != '-' && first != '.') {
return std::nullopt;
}
if (payload.find_first_of("xX") != std::string_view::npos) {
return std::nullopt;
}
char* end;
errno = 0;
const float parsed = std::strtof(text, &end);
if (end != text + length || !std::isfinite(parsed) || (errno == ERANGE && parsed == 0.0f)) {
return std::nullopt;
}
return parsed;
}
inline std::optional<float> parseNumber(std::string_view payload, uint8_t bufferSize = 17) {
if (payload.empty() || bufferSize < 2) {
return std::nullopt;
}
// Long input is intentionally truncated to leave room for the terminating NUL.
payload = payload.substr(0, bufferSize - 1);
char buffer[bufferSize];
memcpy(buffer, payload.data(), payload.size());
buffer[payload.size()] = '\0';
return parseNumber(buffer, payload.size());
}
inline std::string getChipId(const char* prefix = nullptr, const char* suffix = nullptr) {
std::string chipId;
chipId.reserve(
6
+ (prefix != nullptr ? strlen(prefix) : 0)
+ (suffix != nullptr ? strlen(suffix) : 0)
);
if (prefix != nullptr) {
chipId += prefix;
}
uint32_t cid = 0;
// https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/ChipID/GetChipID/GetChipID.ino
for (uint8_t i = 0; i < 17; i = i + 8) {
cid |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
}
char hexId[7];
snprintf(hexId, sizeof(hexId), "%lx", static_cast<unsigned long>(cid));
chipId += hexId;
if (suffix != nullptr) {
chipId += suffix;
}
chipId.erase(chipId.find_last_not_of(" \t\n\r\f\v") + 1);
chipId.erase(0, chipId.find_first_not_of(" \t\n\r\f\v"));
return chipId;
}
inline float liter2gallon(float value) {
return value / 3.785411784f;
}
inline float gallon2liter(float value) {
return value * 3.785411784f;
}
inline float convertVolume(float value, const UnitSystem unitFrom, const UnitSystem unitTo) {
if (unitFrom == UnitSystem::METRIC && unitTo == UnitSystem::IMPERIAL) {
value = liter2gallon(value);
} else if (unitFrom == UnitSystem::IMPERIAL && unitTo == UnitSystem::METRIC) {
value = gallon2liter(value);
}
return value;
}
inline float bar2psi(float value) {
return value * 14.5038f;
}
inline float psi2bar(float value) {
return value / 14.5038f;
}
inline float convertPressure(float value, const UnitSystem unitFrom, const UnitSystem unitTo) {
if (unitFrom == UnitSystem::METRIC && unitTo == UnitSystem::IMPERIAL) {
value = bar2psi(value);
} else if (unitFrom == UnitSystem::IMPERIAL && unitTo == UnitSystem::METRIC) {
value = psi2bar(value);
}
return value;
}
inline float c2f(float value) {
return (9.0f / 5.0f) * value + 32.0f;
}
inline float f2c(float value) {
return (value - 32.0f) * 5.0f / 9.0f;
}
inline float convertTemp(float value, const UnitSystem unitFrom, const UnitSystem unitTo) {
if (unitFrom == UnitSystem::METRIC && unitTo == UnitSystem::IMPERIAL) {
value = c2f(value);
} else if (unitFrom == UnitSystem::IMPERIAL && unitTo == UnitSystem::METRIC) {
value = f2c(value);
}
return value;
}
inline bool isValidTemp(const float value, UnitSystem unit, const float min = 0.1f, const float max = 99.9f, const UnitSystem minMaxUnit = UnitSystem::METRIC) {
return value >= convertTemp(min, minMaxUnit, unit) && value <= convertTemp(max, minMaxUnit, unit);
}
inline bool isFloatDifferent(float first, float second, float tolerance) {
return fabsf(first - second) > tolerance;
}
inline float roundf(float value, uint8_t decimals = 2) {
if (decimals == 0) {
return std::round(value);
} else if (abs(value) < 0.00000001f) {
return 0.0f;
}
const float multiplier = pow10(decimals);
const float scaled = value * multiplier;
return std::isfinite(scaled) ? std::round(scaled) / multiplier : value;
}
inline uint8_t rssiToSignalQuality(int16_t rssi) {
return constrain(map(rssi, -100, -50, 0, 100), 0, 100);
}