From fac5ec5e8319f7343e3fa19d32c7e11f55f577f6 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Sat, 11 Mar 2023 17:38:23 +0100 Subject: [PATCH] Add callback function calls to parser --- Firmware/src/Messages/Parser.cpp | 43 ++++++++++++++++++++++++++++++++ Firmware/src/Messages/Parser.h | 28 ++++++++++++++++++--- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/Firmware/src/Messages/Parser.cpp b/Firmware/src/Messages/Parser.cpp index d619148..0f1a898 100644 --- a/Firmware/src/Messages/Parser.cpp +++ b/Firmware/src/Messages/Parser.cpp @@ -5,11 +5,13 @@ message::parser::StateVisitor::StateVisitor(etl::byte_stream_reader &sreader) auto message::parser::StateVisitor::operator()(state::ModeSelection) -> State { if (!stream.available()) { + invalidCallback("ModeSelection: Stream not available"); return state::Invalid{}; } auto mode = stream.read(); if (!mode.has_value()) { + invalidCallback("ModeSelection: Mode has no value"); return state::Invalid{}; } @@ -23,17 +25,20 @@ auto message::parser::StateVisitor::operator()(state::ModeSelection) -> State { case static_cast(protocol::Mode::Command): return state::Command{}; default: + invalidCallback("ModeSelection: Invalid index"); return state::Invalid{}; } } auto message::parser::StateVisitor::operator()(state::Message) -> State { if (!stream.available()) { + invalidCallback("Message: Stream not available"); return state::Invalid{}; } auto mode = stream.read(); if (!mode.has_value()) { + invalidCallback("Message: Mode has no value"); return state::Invalid{}; } @@ -49,33 +54,41 @@ auto message::parser::StateVisitor::operator()(state::Message) -> State { case static_cast(protocol::Message::Success): return state::MessageSuccess{}; default: + invalidCallback("Message: Invalid index"); return state::Invalid{}; } } auto message::parser::StateVisitor::operator()(state::MessageLightData) -> State { + messageLightDataCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::MessageInfo) -> State { + messageInfoCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::MessageWarning) -> State { + messageWarningCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::MessageError) -> State { + messageErrorCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::MessageSuccess) -> State { + messageSuccessCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::Settings) -> State { if (!stream.available()) { + invalidCallback("Settings: Stream not available"); return state::Invalid{}; } auto mode = stream.read(); if (!mode.has_value()) { + invalidCallback("Settings: Mode has no value"); return state::Invalid{}; } @@ -87,29 +100,35 @@ auto message::parser::StateVisitor::operator()(state::Settings) -> State { case static_cast(protocol::Settings::SetWifiSSID): return state::SettingsWifiSSID{}; default: + invalidCallback("Settings: Invalid index"); return state::Invalid{}; } } auto message::parser::StateVisitor::operator()(state::SettingsSetBaud) -> State { + settingsSetBaudCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::SettingsWifiPassword) -> State { + settingsWifiPasswordCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::SettingsWifiSSID) -> State { + settingsWifiSSIDCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::LightControl) -> State { if (!stream.available()) { + invalidCallback("LightControl: Stream not available"); return state::Invalid{}; } auto mode = stream.read(); if (!mode.has_value()) { + invalidCallback("LightControl: Mode has no value"); return state::Invalid{}; } @@ -125,51 +144,65 @@ auto message::parser::StateVisitor::operator()(state::LightControl) -> State { case static_cast(protocol::LightControl::Decrease): return state::LightControlDecrease{}; default: + invalidCallback("LightControl: Invalid index"); return state::Invalid{}; } } + auto message::parser::StateVisitor::operator()(state::LightControlOn) -> State { if (stream.available_bytes() > 0) { + invalidCallback("LightControlOn: Too many bytes left"); return state::Invalid{}; } + lightControlOnCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::LightControlOff) -> State { if (stream.available_bytes() > 0) { + invalidCallback("LightControlOff: Too many bytes left"); return state::Invalid{}; } + lightControlOffCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::LightControlToggle) -> State { if (stream.available_bytes() > 1) { + invalidCallback("LightControlToggle: Too many bytes left"); return state::Invalid{}; } + lightControlToggleCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::LightControlIncrease) -> State { if (stream.available_bytes() > 1) { + invalidCallback("LightControlIncrease: Too many bytes left"); return state::Invalid{}; } + lightControlIncreaseCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::LightControlDecrease) -> State { if (stream.available_bytes() > 1) { + invalidCallback("LightControlDecrease: Too many bytes left"); return state::Invalid{}; } + lightControlDecreaseCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::Command) -> State { if (!stream.available()) { + invalidCallback("Command: Stream not available"); return state::Invalid{}; } auto mode = stream.read(); if (!mode.has_value()) { + invalidCallback("Command: Mode has no value"); return state::Invalid{}; } @@ -187,38 +220,48 @@ auto message::parser::StateVisitor::operator()(state::Command) -> State { case static_cast(protocol::Command::Version): return state::CommandVersion{}; default: + invalidCallback("Command: Invalid index"); return state::Invalid{}; } } auto message::parser::StateVisitor::operator()(state::CommandRequestLightData) -> State { + commandRequestLightDataCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()( state::CommandEnterConsoleFlashing) -> State { if (stream.available_bytes() > 0) { + invalidCallback("CommandEnterConsoleFlashing: Too many bytes left"); return state::Invalid{}; } + commandEnterConsoleFlashingCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()( state::CommandExitConsoleFlashing) -> State { if (stream.available_bytes() > 0) { + invalidCallback("CommandExitConsoleFlashing: Too many bytes left"); return state::Invalid{}; } + commandExitConsoleFlashingCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::CommandPairBluetooth) -> State { if (stream.available_bytes() > 0) { + invalidCallback("CommandPairBluetooth: Too many bytes left"); return state::Invalid{}; } + commandPairBluetoothCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::CommandHelp) -> State { + commandHelpCallback(); return state::Complete{}; } auto message::parser::StateVisitor::operator()(state::CommandVersion) -> State { + commandVersionCallback(); return state::Complete{}; } diff --git a/Firmware/src/Messages/Parser.h b/Firmware/src/Messages/Parser.h index 9ecaf66..dbd93aa 100644 --- a/Firmware/src/Messages/Parser.h +++ b/Firmware/src/Messages/Parser.h @@ -1,6 +1,7 @@ #pragma once #include "Protocol.h" #include +#include #include namespace message { @@ -49,9 +50,6 @@ using State = etl::variant< void parse(etl::byte_stream_reader &reader); class StateVisitor { -private: - etl::byte_stream_reader stream; - public: StateVisitor(etl::byte_stream_reader &); State operator()(state::ModeSelection); @@ -83,6 +81,30 @@ public: State operator()(state::Complete); State operator()(state::Invalid); + +private: + etl::byte_stream_reader stream; + // TODO: callback function parameter types + etl::function invalidCallback; + etl::function messageLightDataCallback; + etl::function messageInfoCallback; + etl::function messageWarningCallback; + etl::function messageErrorCallback; + etl::function messageSuccessCallback; + etl::function settingsSetBaudCallback; + etl::function settingsWifiPasswordCallback; + etl::function settingsWifiSSIDCallback; + etl::function lightControlOnCallback; + etl::function lightControlOffCallback; + etl::function lightControlToggleCallback; + etl::function lightControlIncreaseCallback; + etl::function lightControlDecreaseCallback; + etl::function commandRequestLightDataCallback; + etl::function commandEnterConsoleFlashingCallback; + etl::function commandExitConsoleFlashingCallback; + etl::function commandPairBluetoothCallback; + etl::function commandHelpCallback; + etl::function commandVersionCallback; }; } // namespace parser } // namespace message