Add callback function calls to parser
This commit is contained in:
parent
e8ec695273
commit
fac5ec5e83
2 changed files with 68 additions and 3 deletions
|
@ -5,11 +5,13 @@ message::parser::StateVisitor::StateVisitor(etl::byte_stream_reader &sreader)
|
||||||
|
|
||||||
auto message::parser::StateVisitor::operator()(state::ModeSelection) -> State {
|
auto message::parser::StateVisitor::operator()(state::ModeSelection) -> State {
|
||||||
if (!stream.available<uint8_t>()) {
|
if (!stream.available<uint8_t>()) {
|
||||||
|
invalidCallback("ModeSelection: Stream not available");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto mode = stream.read<uint8_t>();
|
auto mode = stream.read<uint8_t>();
|
||||||
if (!mode.has_value()) {
|
if (!mode.has_value()) {
|
||||||
|
invalidCallback("ModeSelection: Mode has no value");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,17 +25,20 @@ auto message::parser::StateVisitor::operator()(state::ModeSelection) -> State {
|
||||||
case static_cast<uint8_t>(protocol::Mode::Command):
|
case static_cast<uint8_t>(protocol::Mode::Command):
|
||||||
return state::Command{};
|
return state::Command{};
|
||||||
default:
|
default:
|
||||||
|
invalidCallback("ModeSelection: Invalid index");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto message::parser::StateVisitor::operator()(state::Message) -> State {
|
auto message::parser::StateVisitor::operator()(state::Message) -> State {
|
||||||
if (!stream.available<uint8_t>()) {
|
if (!stream.available<uint8_t>()) {
|
||||||
|
invalidCallback("Message: Stream not available");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto mode = stream.read<uint8_t>();
|
auto mode = stream.read<uint8_t>();
|
||||||
if (!mode.has_value()) {
|
if (!mode.has_value()) {
|
||||||
|
invalidCallback("Message: Mode has no value");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,33 +54,41 @@ auto message::parser::StateVisitor::operator()(state::Message) -> State {
|
||||||
case static_cast<uint8_t>(protocol::Message::Success):
|
case static_cast<uint8_t>(protocol::Message::Success):
|
||||||
return state::MessageSuccess{};
|
return state::MessageSuccess{};
|
||||||
default:
|
default:
|
||||||
|
invalidCallback("Message: Invalid index");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::MessageLightData)
|
auto message::parser::StateVisitor::operator()(state::MessageLightData)
|
||||||
-> State {
|
-> State {
|
||||||
|
messageLightDataCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::MessageInfo) -> State {
|
auto message::parser::StateVisitor::operator()(state::MessageInfo) -> State {
|
||||||
|
messageInfoCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::MessageWarning) -> State {
|
auto message::parser::StateVisitor::operator()(state::MessageWarning) -> State {
|
||||||
|
messageWarningCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::MessageError) -> State {
|
auto message::parser::StateVisitor::operator()(state::MessageError) -> State {
|
||||||
|
messageErrorCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::MessageSuccess) -> State {
|
auto message::parser::StateVisitor::operator()(state::MessageSuccess) -> State {
|
||||||
|
messageSuccessCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto message::parser::StateVisitor::operator()(state::Settings) -> State {
|
auto message::parser::StateVisitor::operator()(state::Settings) -> State {
|
||||||
if (!stream.available<uint8_t>()) {
|
if (!stream.available<uint8_t>()) {
|
||||||
|
invalidCallback("Settings: Stream not available");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto mode = stream.read<uint8_t>();
|
auto mode = stream.read<uint8_t>();
|
||||||
if (!mode.has_value()) {
|
if (!mode.has_value()) {
|
||||||
|
invalidCallback("Settings: Mode has no value");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,29 +100,35 @@ auto message::parser::StateVisitor::operator()(state::Settings) -> State {
|
||||||
case static_cast<uint8_t>(protocol::Settings::SetWifiSSID):
|
case static_cast<uint8_t>(protocol::Settings::SetWifiSSID):
|
||||||
return state::SettingsWifiSSID{};
|
return state::SettingsWifiSSID{};
|
||||||
default:
|
default:
|
||||||
|
invalidCallback("Settings: Invalid index");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::SettingsSetBaud)
|
auto message::parser::StateVisitor::operator()(state::SettingsSetBaud)
|
||||||
-> State {
|
-> State {
|
||||||
|
settingsSetBaudCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::SettingsWifiPassword)
|
auto message::parser::StateVisitor::operator()(state::SettingsWifiPassword)
|
||||||
-> State {
|
-> State {
|
||||||
|
settingsWifiPasswordCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::SettingsWifiSSID)
|
auto message::parser::StateVisitor::operator()(state::SettingsWifiSSID)
|
||||||
-> State {
|
-> State {
|
||||||
|
settingsWifiSSIDCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto message::parser::StateVisitor::operator()(state::LightControl) -> State {
|
auto message::parser::StateVisitor::operator()(state::LightControl) -> State {
|
||||||
if (!stream.available<uint8_t>()) {
|
if (!stream.available<uint8_t>()) {
|
||||||
|
invalidCallback("LightControl: Stream not available");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto mode = stream.read<uint8_t>();
|
auto mode = stream.read<uint8_t>();
|
||||||
if (!mode.has_value()) {
|
if (!mode.has_value()) {
|
||||||
|
invalidCallback("LightControl: Mode has no value");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,51 +144,65 @@ auto message::parser::StateVisitor::operator()(state::LightControl) -> State {
|
||||||
case static_cast<uint8_t>(protocol::LightControl::Decrease):
|
case static_cast<uint8_t>(protocol::LightControl::Decrease):
|
||||||
return state::LightControlDecrease{};
|
return state::LightControlDecrease{};
|
||||||
default:
|
default:
|
||||||
|
invalidCallback("LightControl: Invalid index");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto message::parser::StateVisitor::operator()(state::LightControlOn) -> State {
|
auto message::parser::StateVisitor::operator()(state::LightControlOn) -> State {
|
||||||
if (stream.available_bytes() > 0) {
|
if (stream.available_bytes() > 0) {
|
||||||
|
invalidCallback("LightControlOn: Too many bytes left");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
lightControlOnCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::LightControlOff)
|
auto message::parser::StateVisitor::operator()(state::LightControlOff)
|
||||||
-> State {
|
-> State {
|
||||||
if (stream.available_bytes() > 0) {
|
if (stream.available_bytes() > 0) {
|
||||||
|
invalidCallback("LightControlOff: Too many bytes left");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
lightControlOffCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::LightControlToggle)
|
auto message::parser::StateVisitor::operator()(state::LightControlToggle)
|
||||||
-> State {
|
-> State {
|
||||||
if (stream.available_bytes() > 1) {
|
if (stream.available_bytes() > 1) {
|
||||||
|
invalidCallback("LightControlToggle: Too many bytes left");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
lightControlToggleCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::LightControlIncrease)
|
auto message::parser::StateVisitor::operator()(state::LightControlIncrease)
|
||||||
-> State {
|
-> State {
|
||||||
if (stream.available_bytes() > 1) {
|
if (stream.available_bytes() > 1) {
|
||||||
|
invalidCallback("LightControlIncrease: Too many bytes left");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
lightControlIncreaseCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::LightControlDecrease)
|
auto message::parser::StateVisitor::operator()(state::LightControlDecrease)
|
||||||
-> State {
|
-> State {
|
||||||
if (stream.available_bytes() > 1) {
|
if (stream.available_bytes() > 1) {
|
||||||
|
invalidCallback("LightControlDecrease: Too many bytes left");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
lightControlDecreaseCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto message::parser::StateVisitor::operator()(state::Command) -> State {
|
auto message::parser::StateVisitor::operator()(state::Command) -> State {
|
||||||
if (!stream.available<uint8_t>()) {
|
if (!stream.available<uint8_t>()) {
|
||||||
|
invalidCallback("Command: Stream not available");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto mode = stream.read<uint8_t>();
|
auto mode = stream.read<uint8_t>();
|
||||||
if (!mode.has_value()) {
|
if (!mode.has_value()) {
|
||||||
|
invalidCallback("Command: Mode has no value");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,38 +220,48 @@ auto message::parser::StateVisitor::operator()(state::Command) -> State {
|
||||||
case static_cast<uint8_t>(protocol::Command::Version):
|
case static_cast<uint8_t>(protocol::Command::Version):
|
||||||
return state::CommandVersion{};
|
return state::CommandVersion{};
|
||||||
default:
|
default:
|
||||||
|
invalidCallback("Command: Invalid index");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::CommandRequestLightData)
|
auto message::parser::StateVisitor::operator()(state::CommandRequestLightData)
|
||||||
-> State {
|
-> State {
|
||||||
|
commandRequestLightDataCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(
|
auto message::parser::StateVisitor::operator()(
|
||||||
state::CommandEnterConsoleFlashing) -> State {
|
state::CommandEnterConsoleFlashing) -> State {
|
||||||
if (stream.available_bytes() > 0) {
|
if (stream.available_bytes() > 0) {
|
||||||
|
invalidCallback("CommandEnterConsoleFlashing: Too many bytes left");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
commandEnterConsoleFlashingCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(
|
auto message::parser::StateVisitor::operator()(
|
||||||
state::CommandExitConsoleFlashing) -> State {
|
state::CommandExitConsoleFlashing) -> State {
|
||||||
if (stream.available_bytes() > 0) {
|
if (stream.available_bytes() > 0) {
|
||||||
|
invalidCallback("CommandExitConsoleFlashing: Too many bytes left");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
commandExitConsoleFlashingCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::CommandPairBluetooth)
|
auto message::parser::StateVisitor::operator()(state::CommandPairBluetooth)
|
||||||
-> State {
|
-> State {
|
||||||
if (stream.available_bytes() > 0) {
|
if (stream.available_bytes() > 0) {
|
||||||
|
invalidCallback("CommandPairBluetooth: Too many bytes left");
|
||||||
return state::Invalid{};
|
return state::Invalid{};
|
||||||
}
|
}
|
||||||
|
commandPairBluetoothCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::CommandHelp) -> State {
|
auto message::parser::StateVisitor::operator()(state::CommandHelp) -> State {
|
||||||
|
commandHelpCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
auto message::parser::StateVisitor::operator()(state::CommandVersion) -> State {
|
auto message::parser::StateVisitor::operator()(state::CommandVersion) -> State {
|
||||||
|
commandVersionCallback();
|
||||||
return state::Complete{};
|
return state::Complete{};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "Protocol.h"
|
#include "Protocol.h"
|
||||||
#include <etl/byte_stream.h>
|
#include <etl/byte_stream.h>
|
||||||
|
#include <etl/function.h>
|
||||||
#include <etl/variant.h>
|
#include <etl/variant.h>
|
||||||
|
|
||||||
namespace message {
|
namespace message {
|
||||||
|
@ -49,9 +50,6 @@ using State = etl::variant<
|
||||||
void parse(etl::byte_stream_reader &reader);
|
void parse(etl::byte_stream_reader &reader);
|
||||||
|
|
||||||
class StateVisitor {
|
class StateVisitor {
|
||||||
private:
|
|
||||||
etl::byte_stream_reader stream;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
StateVisitor(etl::byte_stream_reader &);
|
StateVisitor(etl::byte_stream_reader &);
|
||||||
State operator()(state::ModeSelection);
|
State operator()(state::ModeSelection);
|
||||||
|
@ -83,6 +81,30 @@ public:
|
||||||
|
|
||||||
State operator()(state::Complete);
|
State operator()(state::Complete);
|
||||||
State operator()(state::Invalid);
|
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 parser
|
||||||
} // namespace message
|
} // namespace message
|
||||||
|
|
Loading…
Reference in a new issue