Add callback function calls to parser

This commit is contained in:
GHOSCHT 2023-03-11 17:38:23 +01:00
parent e8ec695273
commit fac5ec5e83
2 changed files with 68 additions and 3 deletions

View file

@ -5,11 +5,13 @@ message::parser::StateVisitor::StateVisitor(etl::byte_stream_reader &sreader)
auto message::parser::StateVisitor::operator()(state::ModeSelection) -> State {
if (!stream.available<uint8_t>()) {
invalidCallback("ModeSelection: Stream not available");
return state::Invalid{};
}
auto mode = stream.read<uint8_t>();
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<uint8_t>(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<uint8_t>()) {
invalidCallback("Message: Stream not available");
return state::Invalid{};
}
auto mode = stream.read<uint8_t>();
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<uint8_t>(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<uint8_t>()) {
invalidCallback("Settings: Stream not available");
return state::Invalid{};
}
auto mode = stream.read<uint8_t>();
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<uint8_t>(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<uint8_t>()) {
invalidCallback("LightControl: Stream not available");
return state::Invalid{};
}
auto mode = stream.read<uint8_t>();
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<uint8_t>(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<uint8_t>()) {
invalidCallback("Command: Stream not available");
return state::Invalid{};
}
auto mode = stream.read<uint8_t>();
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<uint8_t>(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{};
}

View file

@ -1,6 +1,7 @@
#pragma once
#include "Protocol.h"
#include <etl/byte_stream.h>
#include <etl/function.h>
#include <etl/variant.h>
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<void, etl::string_view> invalidCallback;
etl::function<void, void> messageLightDataCallback;
etl::function<void, void> messageInfoCallback;
etl::function<void, void> messageWarningCallback;
etl::function<void, void> messageErrorCallback;
etl::function<void, void> messageSuccessCallback;
etl::function<void, void> settingsSetBaudCallback;
etl::function<void, void> settingsWifiPasswordCallback;
etl::function<void, void> settingsWifiSSIDCallback;
etl::function<void, void> lightControlOnCallback;
etl::function<void, void> lightControlOffCallback;
etl::function<void, void> lightControlToggleCallback;
etl::function<void, void> lightControlIncreaseCallback;
etl::function<void, void> lightControlDecreaseCallback;
etl::function<void, void> commandRequestLightDataCallback;
etl::function<void, void> commandEnterConsoleFlashingCallback;
etl::function<void, void> commandExitConsoleFlashingCallback;
etl::function<void, void> commandPairBluetoothCallback;
etl::function<void, void> commandHelpCallback;
etl::function<void, void> commandVersionCallback;
};
} // namespace parser
} // namespace message