From c4d3c57e802425864bb96d1178f0f4e5679f8f3e Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sat, 5 Nov 2022 02:59:45 +0300 Subject: [PATCH] light: ws channel data is text --- code/espurna/light.cpp | 33 ++++++++++++++++++++++----------- code/espurna/utils.cpp | 5 +++-- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/code/espurna/light.cpp b/code/espurna/light.cpp index 7dfb5915..8bebcf1f 100644 --- a/code/espurna/light.cpp +++ b/code/espurna/light.cpp @@ -1462,19 +1462,30 @@ String _lightGroupPayload() { long _lightAdjustValue(long value, espurna::StringView operation) { if (operation.length()) { - char* endp { nullptr }; - auto updated = std::strtol(operation.begin(), &endp, 10); - if ((endp == operation.begin()) || (*endp != '\0')) { - return value; - } - switch (operation[0]) { case '+': case '-': - return updated + value; + { + const long multiplier = (operation[0] == '-') ? -1 : 1; + operation = espurna::StringView( + operation.begin() + 1, operation.end()); + + const auto result = parseUnsigned(operation, 10); + if (result.ok && result.value < std::numeric_limits::max()) { + return value + (static_cast(result.value) * multiplier); + } + break; } - return updated; + default: + { + const auto result = parseUnsigned(operation, 10); + if (result.ok && result.value < std::numeric_limits::max()) { + return result.value; + } + } + + } } return value; @@ -2576,12 +2587,12 @@ void _lightWebSocketOnAction(uint32_t client_id, const char* action, JsonObject& if (channel.success()) { for (auto& kv : channel) { - const auto id = parseUnsigned(kv.key, 10); - if (!id.ok) { + size_t id; + if (!_lightTryParseChannel(kv.key, id)) { break; } - lightChannel(id.value, kv.value.as()); + _lightAdjustChannel(id, kv.value.as()); update = true; } } diff --git a/code/espurna/utils.cpp b/code/espurna/utils.cpp index 0c6f7cda..993c1e5c 100644 --- a/code/espurna/utils.cpp +++ b/code/espurna/utils.cpp @@ -355,9 +355,10 @@ const uint8_t* hexEncodeImpl(const uint8_t* in_begin, const uint8_t* in_end, T&& auto* in_ptr = in_begin; for (; in_ptr != in_end; ++in_ptr) { - char buf[2] { + const char buf[2] { base16[((*in_ptr) & Left) >> Shift], - base16[(*in_ptr) & Right]}; + base16[(*in_ptr) & Right] + }; if (!callback(buf)) { break; }