From 8b06f6d23d9e3e2f47d0bb225375c8c43dfa4ef5 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Sun, 10 Mar 2024 14:59:28 +0000 Subject: [PATCH] Rename to DEV_RELAY_SLIP and move SPoverSLIP to devrelay --- AppleWin-VS2022.vcxproj | 182 +++++++----------- AppleWin-VS2022.vcxproj.filters | 102 ++++------ source/Configuration/PageAdvanced.cpp | 4 +- source/Configuration/PropertySheetHelper.cpp | 4 +- source/Core.h | 4 +- source/SPoverSLIP/CloseRequest.cpp | 26 --- source/SPoverSLIP/CloseRequest.h | 15 -- source/SPoverSLIP/CloseResponse.cpp | 11 -- source/SPoverSLIP/CloseResponse.h | 9 - source/SPoverSLIP/ControlRequest.cpp | 31 --- source/SPoverSLIP/ControlRequest.h | 22 --- source/SPoverSLIP/ControlResponse.cpp | 11 -- source/SPoverSLIP/ControlResponse.h | 9 - source/SPoverSLIP/FormatRequest.cpp | 26 --- source/SPoverSLIP/FormatRequest.h | 15 -- source/SPoverSLIP/FormatResponse.cpp | 11 -- source/SPoverSLIP/FormatResponse.h | 9 - source/SPoverSLIP/InitRequest.cpp | 26 --- source/SPoverSLIP/InitRequest.h | 15 -- source/SPoverSLIP/InitResponse.cpp | 11 -- source/SPoverSLIP/InitResponse.h | 9 - source/SPoverSLIP/OpenRequest.cpp | 26 --- source/SPoverSLIP/OpenRequest.h | 15 -- source/SPoverSLIP/OpenResponse.cpp | 11 -- source/SPoverSLIP/OpenResponse.h | 9 - source/SPoverSLIP/Packet.h | 12 -- source/SPoverSLIP/ReadBlockRequest.cpp | 42 ---- source/SPoverSLIP/ReadBlockRequest.h | 23 --- source/SPoverSLIP/ReadBlockResponse.cpp | 22 --- source/SPoverSLIP/ReadBlockResponse.h | 16 -- source/SPoverSLIP/ReadRequest.cpp | 40 ---- source/SPoverSLIP/ReadRequest.h | 27 --- source/SPoverSLIP/ReadResponse.cpp | 21 -- source/SPoverSLIP/ReadResponse.h | 15 -- source/SPoverSLIP/Request.cpp | 8 - source/SPoverSLIP/Request.h | 25 --- source/SPoverSLIP/ResetRequest.cpp | 26 --- source/SPoverSLIP/ResetRequest.h | 15 -- source/SPoverSLIP/ResetResponse.cpp | 11 -- source/SPoverSLIP/ResetResponse.h | 9 - source/SPoverSLIP/SPoSLIP.h | 15 -- source/SPoverSLIP/SmartPortCodes.h | 16 -- source/SPoverSLIP/StatusRequest.cpp | 41 ---- source/SPoverSLIP/StatusRequest.h | 19 -- source/SPoverSLIP/StatusResponse.cpp | 24 --- source/SPoverSLIP/StatusResponse.h | 19 -- source/SPoverSLIP/WriteBlockRequest.cpp | 44 ----- source/SPoverSLIP/WriteBlockRequest.h | 27 --- source/SPoverSLIP/WriteBlockResponse.cpp | 11 -- source/SPoverSLIP/WriteBlockResponse.h | 9 - source/SPoverSLIP/WriteResponse.cpp | 11 -- source/SPoverSLIP/WriteResponse.h | 9 - source/SmartPortOverSlip.cpp | 86 ++++----- source/SmartPortOverSlip.h | 21 +- source/Utilities.cpp | 2 +- source/Windows/WinFrame.cpp | 2 +- source/devrelay/commands/Close.cpp | 50 +++++ source/devrelay/commands/Close.h | 26 +++ source/devrelay/commands/Control.cpp | 64 ++++++ source/devrelay/commands/Control.h | 33 ++++ source/devrelay/commands/Format.cpp | 48 +++++ source/devrelay/commands/Format.h | 26 +++ source/devrelay/commands/Init.cpp | 49 +++++ source/devrelay/commands/Init.h | 26 +++ source/devrelay/commands/Open.cpp | 50 +++++ source/devrelay/commands/Open.h | 26 +++ source/devrelay/commands/Read.cpp | 79 ++++++++ source/devrelay/commands/Read.h | 45 +++++ source/devrelay/commands/ReadBlock.cpp | 88 +++++++++ source/devrelay/commands/ReadBlock.h | 46 +++++ source/devrelay/commands/Reset.cpp | 49 +++++ source/devrelay/commands/Reset.h | 26 +++ source/devrelay/commands/Status.cpp | 88 +++++++++ source/devrelay/commands/Status.h | 40 ++++ .../commands/Write.cpp} | 48 ++++- .../commands/Write.h} | 18 +- source/devrelay/commands/WriteBlock.cpp | 81 ++++++++ source/devrelay/commands/WriteBlock.h | 41 ++++ source/devrelay/service/COMConnection.cpp | 67 +++++++ source/devrelay/service/COMConnection.h | 41 ++++ .../service}/Connection.cpp | 7 +- .../service}/Connection.h | 3 + .../service}/Listener.cpp | 35 ++-- .../service}/Listener.h | 11 +- .../service}/Requestor.cpp | 8 +- .../service}/Requestor.h | 7 +- .../service}/TCPConnection.cpp | 14 +- .../service}/TCPConnection.h | 6 +- source/{SPoverSLIP => devrelay/slip}/SLIP.cpp | 7 +- source/{SPoverSLIP => devrelay/slip}/SLIP.h | 0 source/devrelay/types/Command.h | 32 +++ source/devrelay/types/Request.cpp | 120 ++++++++++++ source/devrelay/types/Request.h | 40 ++++ .../types}/Response.cpp | 6 +- .../{SPoverSLIP => devrelay/types}/Response.h | 5 +- source/devrelay/util.cpp | 18 ++ source/devrelay/util.h | 9 + 97 files changed, 1601 insertions(+), 1163 deletions(-) delete mode 100644 source/SPoverSLIP/CloseRequest.cpp delete mode 100644 source/SPoverSLIP/CloseRequest.h delete mode 100644 source/SPoverSLIP/CloseResponse.cpp delete mode 100644 source/SPoverSLIP/CloseResponse.h delete mode 100644 source/SPoverSLIP/ControlRequest.cpp delete mode 100644 source/SPoverSLIP/ControlRequest.h delete mode 100644 source/SPoverSLIP/ControlResponse.cpp delete mode 100644 source/SPoverSLIP/ControlResponse.h delete mode 100644 source/SPoverSLIP/FormatRequest.cpp delete mode 100644 source/SPoverSLIP/FormatRequest.h delete mode 100644 source/SPoverSLIP/FormatResponse.cpp delete mode 100644 source/SPoverSLIP/FormatResponse.h delete mode 100644 source/SPoverSLIP/InitRequest.cpp delete mode 100644 source/SPoverSLIP/InitRequest.h delete mode 100644 source/SPoverSLIP/InitResponse.cpp delete mode 100644 source/SPoverSLIP/InitResponse.h delete mode 100644 source/SPoverSLIP/OpenRequest.cpp delete mode 100644 source/SPoverSLIP/OpenRequest.h delete mode 100644 source/SPoverSLIP/OpenResponse.cpp delete mode 100644 source/SPoverSLIP/OpenResponse.h delete mode 100644 source/SPoverSLIP/Packet.h delete mode 100644 source/SPoverSLIP/ReadBlockRequest.cpp delete mode 100644 source/SPoverSLIP/ReadBlockRequest.h delete mode 100644 source/SPoverSLIP/ReadBlockResponse.cpp delete mode 100644 source/SPoverSLIP/ReadBlockResponse.h delete mode 100644 source/SPoverSLIP/ReadRequest.cpp delete mode 100644 source/SPoverSLIP/ReadRequest.h delete mode 100644 source/SPoverSLIP/ReadResponse.cpp delete mode 100644 source/SPoverSLIP/ReadResponse.h delete mode 100644 source/SPoverSLIP/Request.cpp delete mode 100644 source/SPoverSLIP/Request.h delete mode 100644 source/SPoverSLIP/ResetRequest.cpp delete mode 100644 source/SPoverSLIP/ResetRequest.h delete mode 100644 source/SPoverSLIP/ResetResponse.cpp delete mode 100644 source/SPoverSLIP/ResetResponse.h delete mode 100644 source/SPoverSLIP/SPoSLIP.h delete mode 100644 source/SPoverSLIP/SmartPortCodes.h delete mode 100644 source/SPoverSLIP/StatusRequest.cpp delete mode 100644 source/SPoverSLIP/StatusRequest.h delete mode 100644 source/SPoverSLIP/StatusResponse.cpp delete mode 100644 source/SPoverSLIP/StatusResponse.h delete mode 100644 source/SPoverSLIP/WriteBlockRequest.cpp delete mode 100644 source/SPoverSLIP/WriteBlockRequest.h delete mode 100644 source/SPoverSLIP/WriteBlockResponse.cpp delete mode 100644 source/SPoverSLIP/WriteBlockResponse.h delete mode 100644 source/SPoverSLIP/WriteResponse.cpp delete mode 100644 source/SPoverSLIP/WriteResponse.h create mode 100644 source/devrelay/commands/Close.cpp create mode 100644 source/devrelay/commands/Close.h create mode 100644 source/devrelay/commands/Control.cpp create mode 100644 source/devrelay/commands/Control.h create mode 100644 source/devrelay/commands/Format.cpp create mode 100644 source/devrelay/commands/Format.h create mode 100644 source/devrelay/commands/Init.cpp create mode 100644 source/devrelay/commands/Init.h create mode 100644 source/devrelay/commands/Open.cpp create mode 100644 source/devrelay/commands/Open.h create mode 100644 source/devrelay/commands/Read.cpp create mode 100644 source/devrelay/commands/Read.h create mode 100644 source/devrelay/commands/ReadBlock.cpp create mode 100644 source/devrelay/commands/ReadBlock.h create mode 100644 source/devrelay/commands/Reset.cpp create mode 100644 source/devrelay/commands/Reset.h create mode 100644 source/devrelay/commands/Status.cpp create mode 100644 source/devrelay/commands/Status.h rename source/{SPoverSLIP/WriteRequest.cpp => devrelay/commands/Write.cpp} (55%) rename source/{SPoverSLIP/WriteRequest.h => devrelay/commands/Write.h} (61%) create mode 100644 source/devrelay/commands/WriteBlock.cpp create mode 100644 source/devrelay/commands/WriteBlock.h create mode 100644 source/devrelay/service/COMConnection.cpp create mode 100644 source/devrelay/service/COMConnection.h rename source/{SPoverSLIP => devrelay/service}/Connection.cpp (97%) rename source/{SPoverSLIP => devrelay/service}/Connection.h (96%) rename source/{SPoverSLIP => devrelay/service}/Listener.cpp (92%) rename source/{SPoverSLIP => devrelay/service}/Listener.h (91%) rename source/{SPoverSLIP => devrelay/service}/Requestor.cpp (96%) rename source/{SPoverSLIP => devrelay/service}/Requestor.h (86%) rename source/{SPoverSLIP => devrelay/service}/TCPConnection.cpp (94%) rename source/{SPoverSLIP => devrelay/service}/TCPConnection.h (88%) rename source/{SPoverSLIP => devrelay/slip}/SLIP.cpp (98%) rename source/{SPoverSLIP => devrelay/slip}/SLIP.h (100%) create mode 100644 source/devrelay/types/Command.h create mode 100644 source/devrelay/types/Request.cpp create mode 100644 source/devrelay/types/Request.h rename source/{SPoverSLIP => devrelay/types}/Response.cpp (58%) rename source/{SPoverSLIP => devrelay/types}/Response.h (81%) create mode 100644 source/devrelay/util.cpp create mode 100644 source/devrelay/util.h diff --git a/AppleWin-VS2022.vcxproj b/AppleWin-VS2022.vcxproj index 35cb7bfb4..83f399a6e 100644 --- a/AppleWin-VS2022.vcxproj +++ b/AppleWin-VS2022.vcxproj @@ -27,9 +27,27 @@ - - - + + + + + + + + + + + + + + + + + + + + + @@ -87,31 +105,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - @@ -138,10 +131,6 @@ - - - - @@ -185,106 +174,91 @@ - + NotUsing - + NotUsing - + NotUsing - - - - - - - - - - - - - - - - - - - - - - - - - - - NotUsing - - - NotUsing - - - NotUsing - - - NotUsing - - - NotUsing - - + NotUsing - + NotUsing - + NotUsing - + NotUsing - + NotUsing - + NotUsing - + NotUsing - + NotUsing - + NotUsing - + NotUsing - + NotUsing - + NotUsing - + NotUsing - + NotUsing - + NotUsing - + NotUsing - + NotUsing + + + + + + + + + + + + + + + + + + + + + + + + + @@ -325,18 +299,6 @@ - - NotUsing - - - NotUsing - - - NotUsing - - - NotUsing - Create @@ -585,7 +547,7 @@ Use Level3 Disabled - WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;NO_DSHOW_STRSAFE;YAML_DECLARE_STATIC;%(PreprocessorDefinitions) + WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;NO_DSHOW_STRSAFE;YAML_DECLARE_STATIC;%(PreprocessorDefinitions);DEV_RELAY_SLIP;SLIP_PROTOCOL_NET true source;source\cpu;source\debugger;zlib;zip_lib;libyaml\include;%(AdditionalIncludeDirectories) MultiThreadedDebug @@ -612,7 +574,7 @@ Use Level3 Disabled - WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;NO_DSHOW_STRSAFE;YAML_DECLARE_STATIC;%(PreprocessorDefinitions) + WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;NO_DSHOW_STRSAFE;YAML_DECLARE_STATIC;%(PreprocessorDefinitions);DEV_RELAY_SLIP;SLIP_PROTOCOL_NET true source;source\cpu;source\debugger;zlib;zip_lib;libyaml\include;%(AdditionalIncludeDirectories) MultiThreadedDebug @@ -641,7 +603,7 @@ Use Level3 Disabled - WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;NO_DSHOW_STRSAFE;NO_DIRECT_X;YAML_DECLARE_STATIC;%(PreprocessorDefinitions) + WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;NO_DSHOW_STRSAFE;NO_DIRECT_X;YAML_DECLARE_STATIC;%(PreprocessorDefinitions);DEV_RELAY_SLIP;SLIP_PROTOCOL_NET true source;source\cpu;source\debugger;zlib;zip_lib;libyaml\include;%(AdditionalIncludeDirectories) MultiThreadedDebug @@ -669,7 +631,7 @@ MaxSpeed true true - WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;NO_DSHOW_STRSAFE;YAML_DECLARE_STATIC;%(PreprocessorDefinitions) + WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;NO_DSHOW_STRSAFE;YAML_DECLARE_STATIC;%(PreprocessorDefinitions);DEV_RELAY_SLIP;SLIP_PROTOCOL_NET true source;source\cpu;source\debugger;zlib;zip_lib;libyaml\include;%(AdditionalIncludeDirectories) MultiThreaded @@ -701,7 +663,7 @@ MaxSpeed true true - WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;NO_DSHOW_STRSAFE;YAML_DECLARE_STATIC;%(PreprocessorDefinitions) + WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;NO_DSHOW_STRSAFE;YAML_DECLARE_STATIC;%(PreprocessorDefinitions);DEV_RELAY_SLIP;SLIP_PROTOCOL_NET true source;source\cpu;source\debugger;zlib;zip_lib;libyaml\include;%(AdditionalIncludeDirectories) MultiThreaded @@ -735,7 +697,7 @@ MaxSpeed true true - WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;NO_DSHOW_STRSAFE;NO_DIRECT_X;YAML_DECLARE_STATIC;%(PreprocessorDefinitions) + WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;NO_DSHOW_STRSAFE;NO_DIRECT_X;YAML_DECLARE_STATIC;%(PreprocessorDefinitions);DEV_RELAY_SLIP;SLIP_PROTOCOL_NET true source;source\cpu;source\debugger;zlib;zip_lib;libyaml\include;%(AdditionalIncludeDirectories) MultiThreaded diff --git a/AppleWin-VS2022.vcxproj.filters b/AppleWin-VS2022.vcxproj.filters index 99f88c399..30b5de531 100644 --- a/AppleWin-VS2022.vcxproj.filters +++ b/AppleWin-VS2022.vcxproj.filters @@ -275,35 +275,26 @@ Source Files\Emulator - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + @@ -649,38 +640,27 @@ Source Files\Emulator - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + diff --git a/source/Configuration/PageAdvanced.cpp b/source/Configuration/PageAdvanced.cpp index 23414f31f..24b6e7d55 100644 --- a/source/Configuration/PageAdvanced.cpp +++ b/source/Configuration/PageAdvanced.cpp @@ -152,7 +152,7 @@ INT_PTR CPageAdvanced::DlgProcInternal(HWND hWnd, UINT message, WPARAM wparam, L CheckDlgButton(hWnd, IDC_SAVESTATE_ON_EXIT, g_bSaveStateOnExit ? BST_CHECKED : BST_UNCHECKED); // SP over SLIP configuration values - auto& listener = GetSPoverSLIPListener(); + auto& listener = GetCommandListener(); CheckDlgButton(hWnd, IDC_SPOSLIP_ENABLE_LISTENER, listener.get_start_on_init() ? BST_CHECKED : BST_UNCHECKED); SendDlgItemMessage(hWnd, IDC_SPOSLIP_ADDRESS, WM_SETTEXT, 0, (LPARAM) listener.get_ip_address().c_str()); std::string port_string = std::to_string(listener.get_port()); @@ -205,7 +205,7 @@ void CPageAdvanced::DlgOK(HWND hWnd) REGSAVE(TEXT(REGVALUE_SAVE_STATE_ON_EXIT), g_bSaveStateOnExit ? 1 : 0); // SP over SLIP - auto& listener = GetSPoverSLIPListener(); + auto& listener = GetCommandListener(); auto current_ip = listener.get_ip_address(); auto current_port = listener.get_port(); diff --git a/source/Configuration/PropertySheetHelper.cpp b/source/Configuration/PropertySheetHelper.cpp index 278181954..3ef806b1b 100644 --- a/source/Configuration/PropertySheetHelper.cpp +++ b/source/Configuration/PropertySheetHelper.cpp @@ -343,11 +343,11 @@ void CPropertySheetHelper::ApplyNewConfig(const CConfigNeedingRestart& ConfigNew SaveSPoverSLIPListenerStartup(ConfigNew.m_startSPOverSlipListener); // if we're changing to on, start it, else stop it if (ConfigNew.m_startSPOverSlipListener) { - GetSPoverSLIPListener().start(); + GetCommandListener().start(); } else { - GetSPoverSLIPListener().stop(); + GetCommandListener().stop(); } } diff --git a/source/Core.h b/source/Core.h index de0c64f91..1feda5d61 100644 --- a/source/Core.h +++ b/source/Core.h @@ -4,7 +4,7 @@ #include "Common.h" #include "StrFormat.h" #include "Log.h" -#include "SPoverSLIP/Listener.h" +#include "devrelay/service/Listener.h" void LogFileTimeUntilFirstKeyReadReset(void); void LogFileTimeUntilFirstKeyRead(void); @@ -73,7 +73,7 @@ extern bool g_bDisableDirectSoundMockingboard; // Cmd line switch: don't i class Pravets& GetPravets(void); -extern class Listener &GetSPoverSLIPListener(void); +extern class Listener &GetCommandListener(void); //#define LOG_PERF_TIMINGS #ifdef LOG_PERF_TIMINGS diff --git a/source/SPoverSLIP/CloseRequest.cpp b/source/SPoverSLIP/CloseRequest.cpp deleted file mode 100644 index 7638eeb8b..000000000 --- a/source/SPoverSLIP/CloseRequest.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "CloseRequest.h" - -#include "CloseResponse.h" -#include "SmartPortCodes.h" - -CloseRequest::CloseRequest(const uint8_t request_sequence_number, const uint8_t sp_unit) : Request(request_sequence_number, SP_CLOSE, sp_unit) {} - -std::vector CloseRequest::serialize() const -{ - std::vector request_data; - request_data.push_back(this->get_request_sequence_number()); - request_data.push_back(this->get_command_number()); - request_data.push_back(this->get_sp_unit()); - return request_data; -} - -std::unique_ptr CloseRequest::deserialize(const std::vector &data) const -{ - if (data.size() < 2) - { - throw std::runtime_error("Not enough data to deserialize CloseResponse"); - } - - auto response = std::make_unique(data[0], data[1]); - return response; -} diff --git a/source/SPoverSLIP/CloseRequest.h b/source/SPoverSLIP/CloseRequest.h deleted file mode 100644 index ad0aef2c8..000000000 --- a/source/SPoverSLIP/CloseRequest.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include -#include - -#include "Request.h" -#include "Response.h" - -class CloseRequest : public Request -{ -public: - CloseRequest(uint8_t request_sequence_number, uint8_t sp_unit); - std::vector serialize() const override; - std::unique_ptr deserialize(const std::vector &data) const override; -}; diff --git a/source/SPoverSLIP/CloseResponse.cpp b/source/SPoverSLIP/CloseResponse.cpp deleted file mode 100644 index f1bef058e..000000000 --- a/source/SPoverSLIP/CloseResponse.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "CloseResponse.h" - -CloseResponse::CloseResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} - -std::vector CloseResponse::serialize() const -{ - std::vector data; - data.push_back(this->get_request_sequence_number()); - data.push_back(this->get_status()); - return data; -} \ No newline at end of file diff --git a/source/SPoverSLIP/CloseResponse.h b/source/SPoverSLIP/CloseResponse.h deleted file mode 100644 index 5b921fe64..000000000 --- a/source/SPoverSLIP/CloseResponse.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include "Response.h" - -class CloseResponse : public Response -{ -public: - explicit CloseResponse(uint8_t request_sequence_number, uint8_t status); - std::vector serialize() const override; -}; diff --git a/source/SPoverSLIP/ControlRequest.cpp b/source/SPoverSLIP/ControlRequest.cpp deleted file mode 100644 index a923b598b..000000000 --- a/source/SPoverSLIP/ControlRequest.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "ControlRequest.h" - -#include "ControlResponse.h" -#include "SmartPortCodes.h" - -ControlRequest::ControlRequest(const uint8_t request_sequence_number, const uint8_t sp_unit, const uint8_t control_code, std::vector &data) - : Request(request_sequence_number, SP_CONTROL, sp_unit), control_code_(control_code), data_(std::move(data)) -{ -} - -std::vector ControlRequest::serialize() const -{ - std::vector request_data; - request_data.push_back(this->get_request_sequence_number()); - request_data.push_back(this->get_command_number()); - request_data.push_back(this->get_sp_unit()); - request_data.push_back(this->get_control_code()); - request_data.insert(request_data.end(), get_data().begin(), get_data().end()); - return request_data; -} - -std::unique_ptr ControlRequest::deserialize(const std::vector &data) const -{ - if (data.size() < 2) - { - throw std::runtime_error("Not enough data to deserialize ControlResponse"); - } - - auto response = std::make_unique(data[0], data[1]); - return response; -} diff --git a/source/SPoverSLIP/ControlRequest.h b/source/SPoverSLIP/ControlRequest.h deleted file mode 100644 index 0716df2a2..000000000 --- a/source/SPoverSLIP/ControlRequest.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include -#include - -#include "Request.h" -#include "Response.h" - -class ControlRequest : public Request -{ -public: - ControlRequest(const uint8_t request_sequence_number, const uint8_t sp_unit, const uint8_t control_code, std::vector &data); - std::vector serialize() const override; - std::unique_ptr deserialize(const std::vector &data) const override; - - const std::vector &get_data() const { return data_; } - uint8_t get_control_code() const { return control_code_; } - -private: - uint8_t control_code_; - std::vector data_; -}; diff --git a/source/SPoverSLIP/ControlResponse.cpp b/source/SPoverSLIP/ControlResponse.cpp deleted file mode 100644 index 1b0bb5a21..000000000 --- a/source/SPoverSLIP/ControlResponse.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "ControlResponse.h" - -ControlResponse::ControlResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} - -std::vector ControlResponse::serialize() const -{ - std::vector data; - data.push_back(this->get_request_sequence_number()); - data.push_back(this->get_status()); - return data; -} \ No newline at end of file diff --git a/source/SPoverSLIP/ControlResponse.h b/source/SPoverSLIP/ControlResponse.h deleted file mode 100644 index bcff3af9b..000000000 --- a/source/SPoverSLIP/ControlResponse.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include "Response.h" - -class ControlResponse : public Response -{ -public: - explicit ControlResponse(uint8_t request_sequence_number, uint8_t status); - std::vector serialize() const override; -}; diff --git a/source/SPoverSLIP/FormatRequest.cpp b/source/SPoverSLIP/FormatRequest.cpp deleted file mode 100644 index e56befebf..000000000 --- a/source/SPoverSLIP/FormatRequest.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "FormatRequest.h" - -#include "FormatResponse.h" -#include "SmartPortCodes.h" - -FormatRequest::FormatRequest(const uint8_t request_sequence_number, const uint8_t sp_unit) : Request(request_sequence_number, SP_FORMAT, sp_unit) {} - -std::vector FormatRequest::serialize() const -{ - std::vector request_data; - request_data.push_back(this->get_request_sequence_number()); - request_data.push_back(this->get_command_number()); - request_data.push_back(this->get_sp_unit()); - return request_data; -} - -std::unique_ptr FormatRequest::deserialize(const std::vector &data) const -{ - if (data.size() < 2) - { - throw std::runtime_error("Not enough data to deserialize FormatResponse"); - } - - auto response = std::make_unique(data[0], data[1]); - return response; -} diff --git a/source/SPoverSLIP/FormatRequest.h b/source/SPoverSLIP/FormatRequest.h deleted file mode 100644 index d45afd10a..000000000 --- a/source/SPoverSLIP/FormatRequest.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include -#include - -#include "Request.h" -#include "Response.h" - -class FormatRequest : public Request -{ -public: - FormatRequest(const uint8_t request_sequence_number, const uint8_t sp_unit); - std::vector serialize() const override; - std::unique_ptr deserialize(const std::vector &data) const override; -}; diff --git a/source/SPoverSLIP/FormatResponse.cpp b/source/SPoverSLIP/FormatResponse.cpp deleted file mode 100644 index 58e266d92..000000000 --- a/source/SPoverSLIP/FormatResponse.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "FormatResponse.h" - -FormatResponse::FormatResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} - -std::vector FormatResponse::serialize() const -{ - std::vector data; - data.push_back(this->get_request_sequence_number()); - data.push_back(this->get_status()); - return data; -} \ No newline at end of file diff --git a/source/SPoverSLIP/FormatResponse.h b/source/SPoverSLIP/FormatResponse.h deleted file mode 100644 index 04b785f7a..000000000 --- a/source/SPoverSLIP/FormatResponse.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include "Response.h" - -class FormatResponse : public Response -{ -public: - explicit FormatResponse(uint8_t request_sequence_number, uint8_t status); - std::vector serialize() const override; -}; diff --git a/source/SPoverSLIP/InitRequest.cpp b/source/SPoverSLIP/InitRequest.cpp deleted file mode 100644 index 24e69a001..000000000 --- a/source/SPoverSLIP/InitRequest.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "InitRequest.h" - -#include "InitResponse.h" -#include "SmartPortCodes.h" - -InitRequest::InitRequest(const uint8_t request_sequence_number, const uint8_t sp_unit) : Request(request_sequence_number, SP_INIT, sp_unit) {} - -std::vector InitRequest::serialize() const -{ - std::vector request_data; - request_data.push_back(this->get_request_sequence_number()); - request_data.push_back(this->get_command_number()); - request_data.push_back(this->get_sp_unit()); - return request_data; -} - -std::unique_ptr InitRequest::deserialize(const std::vector &data) const -{ - if (data.size() < 2) - { - throw std::runtime_error("Not enough data to deserialize InitResponse"); - } - - auto response = std::make_unique(data[0], data[1]); - return response; -} diff --git a/source/SPoverSLIP/InitRequest.h b/source/SPoverSLIP/InitRequest.h deleted file mode 100644 index 3e34b1dc5..000000000 --- a/source/SPoverSLIP/InitRequest.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include -#include - -#include "Request.h" -#include "Response.h" - -class InitRequest : public Request -{ -public: - InitRequest(uint8_t request_sequence_number, uint8_t sp_unit); - std::vector serialize() const override; - std::unique_ptr deserialize(const std::vector &data) const override; -}; diff --git a/source/SPoverSLIP/InitResponse.cpp b/source/SPoverSLIP/InitResponse.cpp deleted file mode 100644 index 7c8628420..000000000 --- a/source/SPoverSLIP/InitResponse.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "InitResponse.h" - -InitResponse::InitResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} - -std::vector InitResponse::serialize() const -{ - std::vector data; - data.push_back(this->get_request_sequence_number()); - data.push_back(this->get_status()); - return data; -} \ No newline at end of file diff --git a/source/SPoverSLIP/InitResponse.h b/source/SPoverSLIP/InitResponse.h deleted file mode 100644 index f539564c3..000000000 --- a/source/SPoverSLIP/InitResponse.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include "Response.h" - -class InitResponse : public Response -{ -public: - explicit InitResponse(uint8_t request_sequence_number, uint8_t status); - std::vector serialize() const override; -}; diff --git a/source/SPoverSLIP/OpenRequest.cpp b/source/SPoverSLIP/OpenRequest.cpp deleted file mode 100644 index c2c93bbe7..000000000 --- a/source/SPoverSLIP/OpenRequest.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "OpenRequest.h" - -#include "OpenResponse.h" -#include "SmartPortCodes.h" - -OpenRequest::OpenRequest(const uint8_t request_sequence_number, const uint8_t sp_unit) : Request(request_sequence_number, SP_OPEN, sp_unit) {} - -std::vector OpenRequest::serialize() const -{ - std::vector request_data; - request_data.push_back(this->get_request_sequence_number()); - request_data.push_back(this->get_command_number()); - request_data.push_back(this->get_sp_unit()); - return request_data; -} - -std::unique_ptr OpenRequest::deserialize(const std::vector &data) const -{ - if (data.size() < 2) - { - throw std::runtime_error("Not enough data to deserialize OpenResponse"); - } - - auto response = std::make_unique(data[0], data[1]); - return response; -} diff --git a/source/SPoverSLIP/OpenRequest.h b/source/SPoverSLIP/OpenRequest.h deleted file mode 100644 index 745b5f29f..000000000 --- a/source/SPoverSLIP/OpenRequest.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include -#include - -#include "Request.h" -#include "Response.h" - -class OpenRequest : public Request -{ -public: - OpenRequest(uint8_t request_sequence_number, uint8_t sp_unit); - std::vector serialize() const override; - std::unique_ptr deserialize(const std::vector &data) const override; -}; diff --git a/source/SPoverSLIP/OpenResponse.cpp b/source/SPoverSLIP/OpenResponse.cpp deleted file mode 100644 index 76a631812..000000000 --- a/source/SPoverSLIP/OpenResponse.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "OpenResponse.h" - -OpenResponse::OpenResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} - -std::vector OpenResponse::serialize() const -{ - std::vector data; - data.push_back(this->get_request_sequence_number()); - data.push_back(this->get_status()); - return data; -} \ No newline at end of file diff --git a/source/SPoverSLIP/OpenResponse.h b/source/SPoverSLIP/OpenResponse.h deleted file mode 100644 index d544e1692..000000000 --- a/source/SPoverSLIP/OpenResponse.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include "Response.h" - -class OpenResponse : public Response -{ -public: - explicit OpenResponse(uint8_t request_sequence_number, uint8_t status); - std::vector serialize() const override; -}; diff --git a/source/SPoverSLIP/Packet.h b/source/SPoverSLIP/Packet.h deleted file mode 100644 index aa650f6dc..000000000 --- a/source/SPoverSLIP/Packet.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include "SPoSLIP.h" -#include -#include - -class Packet : public SPoSLIP -{ -public: - explicit Packet(const uint8_t request_sequence_number) : SPoSLIP(request_sequence_number) {} - virtual std::vector serialize() const = 0; -}; diff --git a/source/SPoverSLIP/ReadBlockRequest.cpp b/source/SPoverSLIP/ReadBlockRequest.cpp deleted file mode 100644 index a782f02b4..000000000 --- a/source/SPoverSLIP/ReadBlockRequest.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "ReadBlockRequest.h" - -#include "ReadBlockResponse.h" -#include "SmartPortCodes.h" - -ReadBlockRequest::ReadBlockRequest(const uint8_t request_sequence_number, const uint8_t sp_unit) : Request(request_sequence_number, SP_READ_BLOCK, sp_unit), block_number_{} {} - -std::vector ReadBlockRequest::serialize() const -{ - std::vector request_data; - request_data.push_back(this->get_request_sequence_number()); - request_data.push_back(this->get_command_number()); - request_data.push_back(this->get_sp_unit()); - request_data.insert(request_data.end(), block_number_.begin(), block_number_.end()); - return request_data; -} - -std::unique_ptr ReadBlockRequest::deserialize(const std::vector &data) const -{ - if (data.size() != 514) - { - throw std::runtime_error("Not enough data to deserialize ReadBlockResponse"); - } - - auto response = std::make_unique(data[0], data[1]); - if (response->get_status() == 0) - { - response->set_block_data(data.begin() + 2, data.end()); - } - return response; -} - -const std::array &ReadBlockRequest::get_block_number() const { return block_number_; } - -void ReadBlockRequest::set_block_number_from_ptr(const uint8_t *ptr, const size_t offset) { std::copy_n(ptr + offset, block_number_.size(), block_number_.begin()); } - -void ReadBlockRequest::set_block_number_from_bytes(uint8_t l, uint8_t m, uint8_t h) -{ - block_number_[0] = l; - block_number_[1] = m; - block_number_[2] = h; -} \ No newline at end of file diff --git a/source/SPoverSLIP/ReadBlockRequest.h b/source/SPoverSLIP/ReadBlockRequest.h deleted file mode 100644 index 55e130ab3..000000000 --- a/source/SPoverSLIP/ReadBlockRequest.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "Request.h" -#include "Response.h" - -class ReadBlockRequest : public Request -{ -public: - ReadBlockRequest(uint8_t request_sequence_number, uint8_t sp_unit); - std::vector serialize() const override; - std::unique_ptr deserialize(const std::vector &data) const override; - const std::array &get_block_number() const; - void set_block_number_from_ptr(const uint8_t *ptr, size_t offset); - void set_block_number_from_bytes(uint8_t l, uint8_t m, uint8_t h); - -private: - std::array block_number_; -}; diff --git a/source/SPoverSLIP/ReadBlockResponse.cpp b/source/SPoverSLIP/ReadBlockResponse.cpp deleted file mode 100644 index a487b6a83..000000000 --- a/source/SPoverSLIP/ReadBlockResponse.cpp +++ /dev/null @@ -1,22 +0,0 @@ -// ReSharper disable CppPassValueParameterByConstReference -#include - -#include "ReadBlockResponse.h" - -ReadBlockResponse::ReadBlockResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status), block_data_{} {} - -std::vector ReadBlockResponse::serialize() const -{ - std::vector data; - data.push_back(this->get_request_sequence_number()); - data.push_back(this->get_status()); - data.insert(data.end(), block_data_.begin(), block_data_.end()); - return data; -} - -void ReadBlockResponse::set_block_data(std::vector::const_iterator begin, std::vector::const_iterator end) -{ - std::copy(begin, end, block_data_.begin()); // NOLINT(performance-unnecessary-value-param) -} - -const std::array &ReadBlockResponse::get_block_data() const { return block_data_; } \ No newline at end of file diff --git a/source/SPoverSLIP/ReadBlockResponse.h b/source/SPoverSLIP/ReadBlockResponse.h deleted file mode 100644 index 1fe689018..000000000 --- a/source/SPoverSLIP/ReadBlockResponse.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include "Response.h" -#include - -class ReadBlockResponse : public Response -{ -public: - explicit ReadBlockResponse(uint8_t request_sequence_number, uint8_t status); - std::vector serialize() const override; - - void set_block_data(std::vector::const_iterator begin, std::vector::const_iterator end); - const std::array &get_block_data() const; - -private: - std::array block_data_; -}; diff --git a/source/SPoverSLIP/ReadRequest.cpp b/source/SPoverSLIP/ReadRequest.cpp deleted file mode 100644 index a29474c30..000000000 --- a/source/SPoverSLIP/ReadRequest.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "ReadRequest.h" - -#include "ReadResponse.h" -#include "SmartPortCodes.h" - -ReadRequest::ReadRequest(const uint8_t request_sequence_number, const uint8_t sp_unit) : Request(request_sequence_number, SP_READ, sp_unit), byte_count_(), address_() {} - -std::vector ReadRequest::serialize() const -{ - std::vector request_data; - request_data.push_back(this->get_request_sequence_number()); - request_data.push_back(this->get_command_number()); - request_data.push_back(this->get_sp_unit()); - request_data.insert(request_data.end(), get_byte_count().begin(), get_byte_count().end()); - request_data.insert(request_data.end(), get_address().begin(), get_address().end()); - return request_data; -} - -std::unique_ptr ReadRequest::deserialize(const std::vector &data) const -{ - if (data.size() < 3) - { - throw std::runtime_error("Not enough data to deserialize ReadResponse"); - } - - auto response = std::make_unique(data[0], data[1]); - if (response->get_status() == 0) - { - response->set_data(data.begin() + 2, data.end()); - } - return response; -} - -const std::array &ReadRequest::get_byte_count() const { return byte_count_; } - -const std::array &ReadRequest::get_address() const { return address_; } - -void ReadRequest::set_byte_count_from_ptr(const uint8_t *ptr, const size_t offset) { std::copy_n(ptr + offset, byte_count_.size(), byte_count_.begin()); } - -void ReadRequest::set_address_from_ptr(const uint8_t *ptr, const size_t offset) { std::copy_n(ptr + offset, address_.size(), address_.begin()); } diff --git a/source/SPoverSLIP/ReadRequest.h b/source/SPoverSLIP/ReadRequest.h deleted file mode 100644 index 8396c1393..000000000 --- a/source/SPoverSLIP/ReadRequest.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "Request.h" -#include "Response.h" - -class ReadRequest : public Request -{ -public: - ReadRequest(uint8_t request_sequence_number, uint8_t sp_unit); - std::vector serialize() const override; - std::unique_ptr deserialize(const std::vector &data) const override; - - const std::array &get_byte_count() const; - void set_byte_count_from_ptr(const uint8_t *ptr, size_t offset); - - const std::array &get_address() const; - void set_address_from_ptr(const uint8_t *ptr, size_t offset); - -private: - std::array byte_count_; - std::array address_; -}; diff --git a/source/SPoverSLIP/ReadResponse.cpp b/source/SPoverSLIP/ReadResponse.cpp deleted file mode 100644 index 635f92102..000000000 --- a/source/SPoverSLIP/ReadResponse.cpp +++ /dev/null @@ -1,21 +0,0 @@ -// ReSharper disable CppPassValueParameterByConstReference -#include "ReadResponse.h" -#include - -ReadResponse::ReadResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} - -std::vector ReadResponse::serialize() const -{ - std::vector data; - data.push_back(this->get_request_sequence_number()); - data.push_back(this->get_status()); - data.insert(data.end(), get_data().begin(), get_data().end()); - return data; -} - -void ReadResponse::set_data(const std::vector::const_iterator &begin, const std::vector::const_iterator &end) -{ - const size_t new_size = std::distance(begin, end); - data_.resize(new_size); - std::copy(begin, end, data_.begin()); // NOLINT(performance-unnecessary-value-param) -} diff --git a/source/SPoverSLIP/ReadResponse.h b/source/SPoverSLIP/ReadResponse.h deleted file mode 100644 index 4fb00429c..000000000 --- a/source/SPoverSLIP/ReadResponse.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include "Response.h" - -class ReadResponse : public Response -{ -public: - explicit ReadResponse(uint8_t request_sequence_number, uint8_t status); - std::vector serialize() const override; - - const std::vector &get_data() const { return data_; } - void set_data(const std::vector::const_iterator &begin, const std::vector::const_iterator &end); - -private: - std::vector data_; -}; diff --git a/source/SPoverSLIP/Request.cpp b/source/SPoverSLIP/Request.cpp deleted file mode 100644 index de0f38eb9..000000000 --- a/source/SPoverSLIP/Request.cpp +++ /dev/null @@ -1,8 +0,0 @@ - -#include "Request.h" - -Request::Request(const uint8_t request_sequence_number, const uint8_t command_number, const uint8_t sp_unit) : Packet(request_sequence_number), command_number_(command_number), sp_unit_(sp_unit) {} - -uint8_t Request::get_command_number() const { return command_number_; } - -uint8_t Request::get_sp_unit() const { return sp_unit_; } diff --git a/source/SPoverSLIP/Request.h b/source/SPoverSLIP/Request.h deleted file mode 100644 index 688ed7d54..000000000 --- a/source/SPoverSLIP/Request.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include "Packet.h" -#include -#include -#include -#include - -// Forward reference to break circular dependency -class Response; - -class Request : public Packet -{ -public: - Request(uint8_t request_sequence_number, uint8_t command_number, uint8_t sp_unit); - std::vector serialize() const override = 0; - virtual std::unique_ptr deserialize(const std::vector &data) const = 0; - - uint8_t get_command_number() const; - uint8_t get_sp_unit() const; - -private: - uint8_t command_number_ = 0; - uint8_t sp_unit_ = 0; -}; diff --git a/source/SPoverSLIP/ResetRequest.cpp b/source/SPoverSLIP/ResetRequest.cpp deleted file mode 100644 index 11cbf6e67..000000000 --- a/source/SPoverSLIP/ResetRequest.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "ResetRequest.h" - -#include "ResetResponse.h" -#include "SmartPortCodes.h" - -ResetRequest::ResetRequest(const uint8_t request_sequence_number, const uint8_t sp_unit) : Request(request_sequence_number, SP_RESET, sp_unit) {} - -std::vector ResetRequest::serialize() const -{ - std::vector request_data; - request_data.push_back(this->get_request_sequence_number()); - request_data.push_back(this->get_command_number()); - request_data.push_back(this->get_sp_unit()); - return request_data; -} - -std::unique_ptr ResetRequest::deserialize(const std::vector &data) const -{ - if (data.size() < 2) - { - throw std::runtime_error("Not enough data to deserialize ResetResponse"); - } - - auto response = std::make_unique(data[0], data[1]); - return response; -} diff --git a/source/SPoverSLIP/ResetRequest.h b/source/SPoverSLIP/ResetRequest.h deleted file mode 100644 index 6bb2151d8..000000000 --- a/source/SPoverSLIP/ResetRequest.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include -#include - -#include "Request.h" -#include "Response.h" - -class ResetRequest : public Request -{ -public: - ResetRequest(uint8_t request_sequence_number, uint8_t sp_unit); - std::vector serialize() const override; - std::unique_ptr deserialize(const std::vector &data) const override; -}; diff --git a/source/SPoverSLIP/ResetResponse.cpp b/source/SPoverSLIP/ResetResponse.cpp deleted file mode 100644 index ccc7e39d4..000000000 --- a/source/SPoverSLIP/ResetResponse.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "ResetResponse.h" - -ResetResponse::ResetResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} - -std::vector ResetResponse::serialize() const -{ - std::vector data; - data.push_back(this->get_request_sequence_number()); - data.push_back(this->get_status()); - return data; -} \ No newline at end of file diff --git a/source/SPoverSLIP/ResetResponse.h b/source/SPoverSLIP/ResetResponse.h deleted file mode 100644 index a11dae72f..000000000 --- a/source/SPoverSLIP/ResetResponse.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include "Response.h" - -class ResetResponse : public Response -{ -public: - explicit ResetResponse(uint8_t request_sequence_number, uint8_t status); - std::vector serialize() const override; -}; diff --git a/source/SPoverSLIP/SPoSLIP.h b/source/SPoverSLIP/SPoSLIP.h deleted file mode 100644 index db8cf8dd8..000000000 --- a/source/SPoverSLIP/SPoSLIP.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include - -class SPoSLIP -{ -private: - uint8_t request_sequence_number_ = 0; - -public: - explicit SPoSLIP(const uint8_t request_sequence_number) : request_sequence_number_(request_sequence_number) {} - virtual ~SPoSLIP() = default; - - uint8_t get_request_sequence_number() const { return request_sequence_number_; } -}; diff --git a/source/SPoverSLIP/SmartPortCodes.h b/source/SPoverSLIP/SmartPortCodes.h deleted file mode 100644 index bb328b689..000000000 --- a/source/SPoverSLIP/SmartPortCodes.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -enum -{ - SP_STATUS = 0, - SP_READ_BLOCK = 1, - SP_WRITE_BLOCK = 2, - SP_FORMAT = 3, - SP_CONTROL = 4, - SP_INIT = 5, - SP_OPEN = 6, - SP_CLOSE = 7, - SP_READ = 8, - SP_WRITE = 9, - SP_RESET = 10 -}; diff --git a/source/SPoverSLIP/StatusRequest.cpp b/source/SPoverSLIP/StatusRequest.cpp deleted file mode 100644 index 022109ced..000000000 --- a/source/SPoverSLIP/StatusRequest.cpp +++ /dev/null @@ -1,41 +0,0 @@ - -#include - -#include "SmartPortCodes.h" -#include "StatusRequest.h" -#include "StatusResponse.h" - -StatusRequest::StatusRequest(const uint8_t request_sequence_number, const uint8_t sp_unit, const uint8_t status_code) : Request(request_sequence_number, SP_STATUS, sp_unit), status_code_(status_code) -{ -} - -std::vector StatusRequest::serialize() const -{ - std::vector request_data; - request_data.push_back(this->get_request_sequence_number()); - request_data.push_back(this->get_command_number()); - request_data.push_back(this->get_sp_unit()); - request_data.push_back(this->get_status_code()); - - return request_data; -} - -std::unique_ptr StatusRequest::deserialize(const std::vector &data) const -{ - if (data.size() < 2) - { - throw std::runtime_error("Not enough data to deserialize StatusResponse"); - } - - auto response = std::make_unique(data[0], data[1]); - - if (response->get_status() == 0 && data.size() > 2) - { - for (size_t i = 2; i < data.size(); ++i) - { - response->add_data(data[i]); - } - } - - return response; -} diff --git a/source/SPoverSLIP/StatusRequest.h b/source/SPoverSLIP/StatusRequest.h deleted file mode 100644 index 18a86098e..000000000 --- a/source/SPoverSLIP/StatusRequest.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "Request.h" -#include "Response.h" -#include -#include - -class StatusRequest : public Request -{ -public: - StatusRequest(uint8_t request_sequence_number, uint8_t sp_unit, uint8_t status_code); - virtual std::vector serialize() const override; - std::unique_ptr deserialize(const std::vector &data) const override; - - uint8_t get_status_code() const { return status_code_; } - -private: - uint8_t status_code_; -}; diff --git a/source/SPoverSLIP/StatusResponse.cpp b/source/SPoverSLIP/StatusResponse.cpp deleted file mode 100644 index d4a003bd9..000000000 --- a/source/SPoverSLIP/StatusResponse.cpp +++ /dev/null @@ -1,24 +0,0 @@ - -#include "StatusResponse.h" -#include "StatusRequest.h" - -StatusResponse::StatusResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} - -const std::vector &StatusResponse::get_data() const { return data_; } - -void StatusResponse::add_data(const uint8_t d) { data_.push_back(d); } - -void StatusResponse::set_data(const std::vector &data) { data_ = data; } - -std::vector StatusResponse::serialize() const -{ - std::vector data; - data.push_back(this->get_request_sequence_number()); - data.push_back(this->get_status()); - - for (uint8_t b : get_data()) - { - data.push_back(b); - } - return data; -} diff --git a/source/SPoverSLIP/StatusResponse.h b/source/SPoverSLIP/StatusResponse.h deleted file mode 100644 index 2e88bb559..000000000 --- a/source/SPoverSLIP/StatusResponse.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "Response.h" -#include -#include - -class StatusResponse : public Response -{ -public: - explicit StatusResponse(uint8_t request_sequence_number, uint8_t status); - std::vector serialize() const override; - - const std::vector &get_data() const; - void add_data(uint8_t d); - void set_data(const std::vector &data); - -private: - std::vector data_; -}; diff --git a/source/SPoverSLIP/WriteBlockRequest.cpp b/source/SPoverSLIP/WriteBlockRequest.cpp deleted file mode 100644 index 2f854fb2f..000000000 --- a/source/SPoverSLIP/WriteBlockRequest.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "WriteBlockRequest.h" - -#include "SmartPortCodes.h" -#include "WriteBlockResponse.h" - -WriteBlockRequest::WriteBlockRequest(const uint8_t request_sequence_number, const uint8_t sp_unit) : Request(request_sequence_number, SP_WRITE_BLOCK, sp_unit), block_number_{}, block_data_{} {} - -std::vector WriteBlockRequest::serialize() const -{ - std::vector request_data; - request_data.push_back(this->get_request_sequence_number()); - request_data.push_back(this->get_command_number()); - request_data.push_back(this->get_sp_unit()); - request_data.insert(request_data.end(), block_number_.begin(), block_number_.end()); - request_data.insert(request_data.end(), block_data_.begin(), block_data_.end()); - - return request_data; -} - -std::unique_ptr WriteBlockRequest::deserialize(const std::vector &data) const -{ - if (data.size() < 2) - { - throw std::runtime_error("Not enough data to deserialize WriteBlockResponse"); - } - - auto response = std::make_unique(data[0], data[1]); - return response; -} - -const std::array &WriteBlockRequest::get_block_number() const { return block_number_; } - -const std::array &WriteBlockRequest::get_block_data() const { return block_data_; } - -void WriteBlockRequest::set_block_number_from_ptr(const uint8_t *ptr, const size_t offset) { std::copy_n(ptr + offset, block_number_.size(), block_number_.begin()); } - -void WriteBlockRequest::set_block_data_from_ptr(const uint8_t *ptr, const size_t offset) { std::copy_n(ptr + offset, block_data_.size(), block_data_.begin()); } - -void WriteBlockRequest::set_block_number_from_bytes(uint8_t l, uint8_t m, uint8_t h) -{ - block_number_[0] = l; - block_number_[1] = m; - block_number_[2] = h; -} \ No newline at end of file diff --git a/source/SPoverSLIP/WriteBlockRequest.h b/source/SPoverSLIP/WriteBlockRequest.h deleted file mode 100644 index f88c969cb..000000000 --- a/source/SPoverSLIP/WriteBlockRequest.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "Request.h" -#include "Response.h" - -class WriteBlockRequest : public Request -{ -public: - WriteBlockRequest(uint8_t request_sequence_number, uint8_t sp_unit); - std::vector serialize() const override; - std::unique_ptr deserialize(const std::vector &data) const override; - const std::array &get_block_number() const; - void set_block_number_from_ptr(const uint8_t *ptr, size_t offset); - - const std::array &get_block_data() const; - void set_block_data_from_ptr(const uint8_t *ptr, size_t offset); - void set_block_number_from_bytes(uint8_t l, uint8_t m, uint8_t h); - -private: - std::array block_number_; - std::array block_data_; -}; diff --git a/source/SPoverSLIP/WriteBlockResponse.cpp b/source/SPoverSLIP/WriteBlockResponse.cpp deleted file mode 100644 index 458a16806..000000000 --- a/source/SPoverSLIP/WriteBlockResponse.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "WriteBlockResponse.h" - -WriteBlockResponse::WriteBlockResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} - -std::vector WriteBlockResponse::serialize() const -{ - std::vector data; - data.push_back(this->get_request_sequence_number()); - data.push_back(this->get_status()); - return data; -} \ No newline at end of file diff --git a/source/SPoverSLIP/WriteBlockResponse.h b/source/SPoverSLIP/WriteBlockResponse.h deleted file mode 100644 index c8a942da3..000000000 --- a/source/SPoverSLIP/WriteBlockResponse.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include "Response.h" - -class WriteBlockResponse : public Response -{ -public: - explicit WriteBlockResponse(uint8_t request_sequence_number, uint8_t status); - std::vector serialize() const override; -}; diff --git a/source/SPoverSLIP/WriteResponse.cpp b/source/SPoverSLIP/WriteResponse.cpp deleted file mode 100644 index 4a448de6c..000000000 --- a/source/SPoverSLIP/WriteResponse.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "WriteResponse.h" - -WriteResponse::WriteResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} - -std::vector WriteResponse::serialize() const -{ - std::vector data; - data.push_back(this->get_request_sequence_number()); - data.push_back(this->get_status()); - return data; -} \ No newline at end of file diff --git a/source/SPoverSLIP/WriteResponse.h b/source/SPoverSLIP/WriteResponse.h deleted file mode 100644 index 8fb9eb000..000000000 --- a/source/SPoverSLIP/WriteResponse.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include "Response.h" - -class WriteResponse : public Response -{ -public: - explicit WriteResponse(uint8_t request_sequence_number, uint8_t status); - std::vector serialize() const override; -}; diff --git a/source/SmartPortOverSlip.cpp b/source/SmartPortOverSlip.cpp index 05454c66d..8397815d2 100644 --- a/source/SmartPortOverSlip.cpp +++ b/source/SmartPortOverSlip.cpp @@ -37,29 +37,20 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include "CPU.h" #include "Log.h" #include "../resource/resource.h" -#include "SPoverSLIP/CloseRequest.h" -#include "SPoverSLIP/CloseResponse.h" -#include "SPoverSLIP/ControlRequest.h" -#include "SPoverSLIP/ControlResponse.h" -#include "SPoverSLIP/FormatRequest.h" -#include "SPoverSLIP/FormatResponse.h" -#include "SPoverSLIP/InitRequest.h" -#include "SPoverSLIP/InitResponse.h" -#include "SPoverSLIP/OpenRequest.h" -#include "SPoverSLIP/OpenResponse.h" -#include "SPoverSLIP/ReadBlockRequest.h" -#include "SPoverSLIP/ReadBlockResponse.h" -#include "SPoverSLIP/ReadRequest.h" -#include "SPoverSLIP/ReadResponse.h" -#include "SPoverSLIP/ResetRequest.h" -#include "SPoverSLIP/ResetResponse.h" -#include "SPoverSLIP/Requestor.h" -#include "SPoverSLIP/StatusRequest.h" -#include "SPoverSLIP/StatusResponse.h" -#include "SPoverSLIP/WriteBlockRequest.h" -#include "SPoverSLIP/WriteBlockResponse.h" -#include "SPoverSLIP/WriteRequest.h" -#include "SPoverSLIP/WriteResponse.h" + +#include "devrelay/commands/Close.h" +#include "devrelay/commands/Control.h" +#include "devrelay/commands/Format.h" +#include "devrelay/commands/Init.h" +#include "devrelay/commands/Open.h" +#include "devrelay/commands/ReadBlock.h" +#include "devrelay/commands/Read.h" +#include "devrelay/commands/Reset.h" +#include "devrelay/commands/Status.h" +#include "devrelay/commands/WriteBlock.h" +#include "devrelay/commands/Write.h" + +#include "devrelay/service/Requestor.h" int SmartPortOverSlip::active_instances = 0; @@ -94,7 +85,7 @@ void SmartPortOverSlip::Reset(const bool powerCycle) if (powerCycle) { // send RESET to all devices - const auto connections = GetSPoverSLIPListener().get_all_connections(); + const auto connections = GetCommandListener().get_all_connections(); for (const auto &id_and_connection : connections) { reset(id_and_connection.first, id_and_connection.second); @@ -135,10 +126,10 @@ void SmartPortOverSlip::handle_smartport_call() return; } - const auto id_and_connection = GetSPoverSLIPListener().find_connection_with_device(unit_number); + const auto id_and_connection = GetCommandListener().find_connection_with_device(unit_number); if (id_and_connection.second == nullptr) { - regs.a = 1; // TODO: what value should we error with? + regs.a = 1; regs.x = 0; regs.y = 0; unset_processor_status(AF_ZERO); @@ -150,37 +141,37 @@ void SmartPortOverSlip::handle_smartport_call() switch (command) { - case SP_CMD_STATUS: + case CMD_STATUS: status_sp(device_id, connection, sp_payload_loc, mem[params_loc]); break; - case SP_CMD_READBLOCK: + case CMD_READ_BLOCK: read_block(device_id, connection, sp_payload_loc, params_loc); break; - case SP_CMD_WRITEBLOCK: + case CMD_WRITE_BLOCK: write_block(device_id, connection, sp_payload_loc, params_loc); break; - case SP_CMD_FORMAT: + case CMD_FORMAT: format(device_id, connection); break; - case SP_CMD_CONTROL: + case CMD_CONTROL: control(device_id, connection, sp_payload_loc, mem[params_loc]); break; - case SP_CMD_INIT: + case CMD_INIT: init(device_id, connection); break; - case SP_CMD_OPEN: + case CMD_OPEN: open(device_id, connection); break; - case SP_CMD_CLOSE: + case CMD_CLOSE: close(device_id, connection); break; - case SP_CMD_READ: + case CMD_READ: read(device_id, connection, sp_payload_loc, params_loc); break; - case SP_CMD_WRITE: + case CMD_WRITE: write(device_id, connection, sp_payload_loc, params_loc); break; - case SP_CMD_RESET: + case CMD_RESET: reset(device_id, connection); break; default: @@ -225,7 +216,10 @@ void SmartPortOverSlip::handle_prodos_call() // We need to look for the first registered device of all connections to us that are disks, and use first 2 as our drives. // we can call the listener to do this for us, and it can cache the results so we can keep calling it - std::pair disk_devices = GetSPoverSLIPListener().first_two_disk_devices(); + std::pair disk_devices = GetCommandListener().first_two_disk_devices([](const std::vector& data) -> bool { + return data.size() >= 22 && (data[21] == 0x01 || data[21] == 0x02 || data[21] == 0x0A) && ((data[0] & 0x10) == 0x10); + }); + if ((drive_num == 1 && disk_devices.first == -1) || (drive_num == 2 && disk_devices.second == -1)) { // No devices found, pretend it's just an error so ProDOS keeps trying later when we do have disks. @@ -280,7 +274,7 @@ void SmartPortOverSlip::handle_prodos_status(uint8_t drive_num, std::pair response = status_pd(id_connection.first, id_connection.second.get(), 0); // the first byte of the data in the status response: /* @@ -332,10 +326,10 @@ void SmartPortOverSlip::handle_prodos_read(uint8_t drive_num, std::pair(mem[0x44]) + static_cast(mem[0x45] << 8); auto device_id = drive_num == 1 ? disk_devices.first : disk_devices.second; - auto id_connection = GetSPoverSLIPListener().find_connection_with_device(device_id); + auto id_connection = GetCommandListener().find_connection_with_device(device_id); // Do a ReadRequest, and shove the 512 byte block into the required memory - ReadBlockRequest request(Requestor::next_request_number(), id_connection.first); + ReadBlockRequest request(Requestor::next_request_number(), id_connection.first, 512); // $46-47 = Block Number request.set_block_number_from_bytes(mem[0x46], mem[0x47], 0); auto response = Requestor::send_request(request, id_connection.second.get()); @@ -359,9 +353,9 @@ void SmartPortOverSlip::handle_prodos_write(uint8_t drive_num, std::pair(mem[0x44]) + static_cast(mem[0x45] << 8); auto device_id = drive_num == 1 ? disk_devices.first : disk_devices.second; - auto id_connection = GetSPoverSLIPListener().find_connection_with_device(device_id); + auto id_connection = GetCommandListener().find_connection_with_device(device_id); - WriteBlockRequest request(Requestor::next_request_number(), id_connection.first); + WriteBlockRequest request(Requestor::next_request_number(), id_connection.first, 512); // $46-47 = Block Number request.set_block_number_from_bytes(mem[0x46], mem[0x47], 0); // put data into the request we're sending @@ -401,7 +395,7 @@ void SmartPortOverSlip::device_count(const WORD sp_payload_loc) { // Fill the status information directly into SP payload memory. // The count is from sum of all devices across all Connections. - const BYTE deviceCount = GetSPoverSLIPListener().get_total_device_count(); + const BYTE deviceCount = GetCommandListener().get_total_device_count(); mem[sp_payload_loc] = deviceCount; mem[sp_payload_loc + 1] = 1 << 6; // no interrupt mem[sp_payload_loc + 2] = 0x4D; // 0x4D46 == MF for vendor ID @@ -421,7 +415,7 @@ void SmartPortOverSlip::device_count(const WORD sp_payload_loc) void SmartPortOverSlip::read_block(const BYTE unit_number, Connection *connection, const WORD buffer_location, const WORD block_count_address) { - ReadBlockRequest request(Requestor::next_request_number(), unit_number); + ReadBlockRequest request(Requestor::next_request_number(), unit_number, 512); // Assume that (cmd_list_loc + 4 == block_count_address) holds 3 bytes for the block number. If it's in the payload, this is wrong and will have to be fixed. request.set_block_number_from_ptr(mem, block_count_address); auto response = Requestor::send_request(request, connection); @@ -441,7 +435,7 @@ void SmartPortOverSlip::read_block(const BYTE unit_number, Connection *connectio void SmartPortOverSlip::write_block(const BYTE unit_number, Connection *connection, const WORD sp_payload_loc, const WORD params_loc) { - WriteBlockRequest request(Requestor::next_request_number(), unit_number); + WriteBlockRequest request(Requestor::next_request_number(), unit_number, 512); // Assume that (cmd_list_loc + 4 == params_loc) holds 3 bytes for the block number. The payload contains the data to write request.set_block_number_from_ptr(mem, params_loc); request.set_block_data_from_ptr(mem, sp_payload_loc); diff --git a/source/SmartPortOverSlip.h b/source/SmartPortOverSlip.h index 65f08ed80..5d4949a04 100644 --- a/source/SmartPortOverSlip.h +++ b/source/SmartPortOverSlip.h @@ -6,28 +6,13 @@ #include #include "CPU.h" -#include "SPoverSLIP/Listener.h" -#include "SPoverSLIP/Response.h" -#include "SPoverSLIP/StatusResponse.h" +#include "devrelay/service/Listener.h" +#include "devrelay/types/Response.h" +#include "devrelay/commands/Status.h" class ControlResponse; class InitResponse; -enum -{ - SP_CMD_STATUS = 0, - SP_CMD_READBLOCK = 1, - SP_CMD_WRITEBLOCK = 2, - SP_CMD_FORMAT = 3, - SP_CMD_CONTROL = 4, - SP_CMD_INIT = 5, - SP_CMD_OPEN = 6, - SP_CMD_CLOSE = 7, - SP_CMD_READ = 8, - SP_CMD_WRITE = 9, - SP_CMD_RESET = 10 -}; - class SmartPortOverSlip : public Card { public: diff --git a/source/Utilities.cpp b/source/Utilities.cpp index 1157ab0dd..de7c11476 100644 --- a/source/Utilities.cpp +++ b/source/Utilities.cpp @@ -102,7 +102,7 @@ void LoadConfiguration(bool loadImages) /////////////////////////////////////////////////////////////// // SmartPort over SLIP - auto& listener = GetSPoverSLIPListener(); + auto& listener = GetCommandListener(); DWORD dwRegStartListener = 0; bool bStartListener = listener.default_start_listener; TCHAR tcAddress[16]; diff --git a/source/Windows/WinFrame.cpp b/source/Windows/WinFrame.cpp index 97de0b555..da594733f 100644 --- a/source/Windows/WinFrame.cpp +++ b/source/Windows/WinFrame.cpp @@ -1054,7 +1054,7 @@ LRESULT Win32Frame::WndProc( case WM_DESTROY: LogFileOutput("WM_DESTROY\n"); - GetSPoverSLIPListener().stop(); + GetCommandListener().stop(); DragAcceptFiles(window,0); if (!g_bRestart) // GH#564: Only save-state on shutdown (not on a restart) Snapshot_Shutdown(); diff --git a/source/devrelay/commands/Close.cpp b/source/devrelay/commands/Close.cpp new file mode 100644 index 000000000..6c52509b7 --- /dev/null +++ b/source/devrelay/commands/Close.cpp @@ -0,0 +1,50 @@ +#ifdef DEV_RELAY_SLIP + +#include "Close.h" + +CloseRequest::CloseRequest(const uint8_t request_sequence_number, const uint8_t device_id) : Request(request_sequence_number, CMD_CLOSE, device_id) {} + +std::vector CloseRequest::serialize() const +{ + std::vector request_data; + request_data.push_back(this->get_request_sequence_number()); + request_data.push_back(this->get_command_number()); + request_data.push_back(this->get_device_id()); + return request_data; +} + +std::unique_ptr CloseRequest::deserialize(const std::vector &data) const +{ + if (data.size() < 2) + { + throw std::runtime_error("Not enough data to deserialize CloseResponse"); + } + + auto response = std::make_unique(data[0], data[1]); + return response; +} + +void CloseRequest::create_command(uint8_t* cmd_data) const +{ + init_command(cmd_data); +} + +std::unique_ptr CloseRequest::create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const +{ + std::unique_ptr response = std::make_unique(get_request_sequence_number(), status); + return response; +} + + +CloseResponse::CloseResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} + +std::vector CloseResponse::serialize() const +{ + std::vector data; + data.push_back(this->get_request_sequence_number()); + data.push_back(this->get_status()); + return data; +} + + +#endif diff --git a/source/devrelay/commands/Close.h b/source/devrelay/commands/Close.h new file mode 100644 index 000000000..4e179b87a --- /dev/null +++ b/source/devrelay/commands/Close.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include + +#include "../types/Request.h" +#include "../types/Response.h" + +class CloseRequest : public Request +{ +public: + CloseRequest(uint8_t request_sequence_number, uint8_t device_id); + std::vector serialize() const override; + std::unique_ptr deserialize(const std::vector &data) const override; + void create_command(uint8_t* output_data) const override; + void copy_payload(uint8_t* data) const override {} + size_t payload_size() const override { return 0; }; + std::unique_ptr create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const override; +}; + +class CloseResponse : public Response +{ +public: + explicit CloseResponse(uint8_t request_sequence_number, uint8_t status); + std::vector serialize() const override; +}; \ No newline at end of file diff --git a/source/devrelay/commands/Control.cpp b/source/devrelay/commands/Control.cpp new file mode 100644 index 000000000..f8a19f6ed --- /dev/null +++ b/source/devrelay/commands/Control.cpp @@ -0,0 +1,64 @@ +#ifdef DEV_RELAY_SLIP + +#include "Control.h" + +ControlRequest::ControlRequest(const uint8_t request_sequence_number, const uint8_t device_id, const uint8_t control_code, std::vector &data) + : Request(request_sequence_number, CMD_CONTROL, device_id), control_code_(control_code), data_(std::move(data)) +{ +} + +std::vector ControlRequest::serialize() const +{ + std::vector request_data; + request_data.push_back(this->get_request_sequence_number()); + request_data.push_back(this->get_command_number()); + request_data.push_back(this->get_device_id()); + request_data.push_back(this->get_control_code()); + request_data.insert(request_data.end(), get_data().begin(), get_data().end()); + return request_data; +} + +std::unique_ptr ControlRequest::deserialize(const std::vector &data) const +{ + if (data.size() < 2) + { + throw std::runtime_error("Not enough data to deserialize ControlResponse"); + } + + auto response = std::make_unique(data[0], data[1]); + return response; +} + +void ControlRequest::create_command(uint8_t* cmd_data) const +{ + init_command(cmd_data); + // The control byte is at params[2], or cmd_data[4], it is then followed by the payload, see serialize above. + cmd_data[4] = control_code_; +} + +void ControlRequest::copy_payload(uint8_t* data) const { + std::copy(data_.begin(), data_.end(), data); +} + +size_t ControlRequest::payload_size() const { + return data_.size(); +} + +std::unique_ptr ControlRequest::create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const +{ + std::unique_ptr response = std::make_unique(get_request_sequence_number(), status); + return response; +} + +ControlResponse::ControlResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} + +std::vector ControlResponse::serialize() const +{ + std::vector data; + data.push_back(this->get_request_sequence_number()); + data.push_back(this->get_status()); + return data; +} + + +#endif diff --git a/source/devrelay/commands/Control.h b/source/devrelay/commands/Control.h new file mode 100644 index 000000000..ce92f3748 --- /dev/null +++ b/source/devrelay/commands/Control.h @@ -0,0 +1,33 @@ +#pragma once + +#include +#include + +#include "../types/Request.h" +#include "../types/Response.h" + +class ControlRequest : public Request +{ +public: + ControlRequest(const uint8_t request_sequence_number, const uint8_t device_id, const uint8_t control_code, std::vector &data); + std::vector serialize() const override; + std::unique_ptr deserialize(const std::vector &data) const override; + + const std::vector &get_data() const { return data_; } + uint8_t get_control_code() const { return control_code_; } + void create_command(uint8_t* output_data) const override; + void copy_payload(uint8_t* data) const override; + size_t payload_size() const override; + std::unique_ptr create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const override; + +private: + uint8_t control_code_; + std::vector data_; +}; + +class ControlResponse : public Response +{ +public: + explicit ControlResponse(uint8_t request_sequence_number, uint8_t status); + std::vector serialize() const override; +}; \ No newline at end of file diff --git a/source/devrelay/commands/Format.cpp b/source/devrelay/commands/Format.cpp new file mode 100644 index 000000000..578ea719e --- /dev/null +++ b/source/devrelay/commands/Format.cpp @@ -0,0 +1,48 @@ +#ifdef DEV_RELAY_SLIP + +#include "Format.h" + +FormatRequest::FormatRequest(const uint8_t request_sequence_number, const uint8_t device_id) : Request(request_sequence_number, CMD_FORMAT, device_id) {} + +std::vector FormatRequest::serialize() const +{ + std::vector request_data; + request_data.push_back(this->get_request_sequence_number()); + request_data.push_back(this->get_command_number()); + request_data.push_back(this->get_device_id()); + return request_data; +} + +std::unique_ptr FormatRequest::deserialize(const std::vector &data) const +{ + if (data.size() < 2) + { + throw std::runtime_error("Not enough data to deserialize FormatResponse"); + } + + auto response = std::make_unique(data[0], data[1]); + return response; +} + +FormatResponse::FormatResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} + +std::vector FormatResponse::serialize() const +{ + std::vector data; + data.push_back(this->get_request_sequence_number()); + data.push_back(this->get_status()); + return data; +} + +void FormatRequest::create_command(uint8_t* cmd_data) const +{ + init_command(cmd_data); +} + +std::unique_ptr FormatRequest::create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const +{ + std::unique_ptr response = std::make_unique(get_request_sequence_number(), status); + return response; +} + +#endif diff --git a/source/devrelay/commands/Format.h b/source/devrelay/commands/Format.h new file mode 100644 index 000000000..ca3f24ed8 --- /dev/null +++ b/source/devrelay/commands/Format.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include + +#include "../types/Request.h" +#include "../types/Response.h" + +class FormatRequest : public Request +{ +public: + FormatRequest(const uint8_t request_sequence_number, const uint8_t device_id); + std::vector serialize() const override; + std::unique_ptr deserialize(const std::vector &data) const override; + void create_command(uint8_t* output_data) const override; + void copy_payload(uint8_t* data) const override {} + size_t payload_size() const override { return 0; }; + std::unique_ptr create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const override; +}; + +class FormatResponse : public Response +{ +public: + explicit FormatResponse(uint8_t request_sequence_number, uint8_t status); + std::vector serialize() const override; +}; diff --git a/source/devrelay/commands/Init.cpp b/source/devrelay/commands/Init.cpp new file mode 100644 index 000000000..33583edf5 --- /dev/null +++ b/source/devrelay/commands/Init.cpp @@ -0,0 +1,49 @@ +#ifdef DEV_RELAY_SLIP + +#include "Init.h" + +InitRequest::InitRequest(const uint8_t request_sequence_number, const uint8_t device_id) : Request(request_sequence_number, CMD_INIT, device_id) {} + +std::vector InitRequest::serialize() const +{ + std::vector request_data; + request_data.push_back(this->get_request_sequence_number()); + request_data.push_back(this->get_command_number()); + request_data.push_back(this->get_device_id()); + return request_data; +} + +std::unique_ptr InitRequest::deserialize(const std::vector &data) const +{ + if (data.size() < 2) + { + throw std::runtime_error("Not enough data to deserialize InitResponse"); + } + + auto response = std::make_unique(data[0], data[1]); + return response; +} + +void InitRequest::create_command(uint8_t* cmd_data) const +{ + init_command(cmd_data); +} + +std::unique_ptr InitRequest::create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const +{ + std::unique_ptr response = std::make_unique(get_request_sequence_number(), status); + return response; +} + +InitResponse::InitResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} + +std::vector InitResponse::serialize() const +{ + std::vector data; + data.push_back(this->get_request_sequence_number()); + data.push_back(this->get_status()); + return data; +} + + +#endif diff --git a/source/devrelay/commands/Init.h b/source/devrelay/commands/Init.h new file mode 100644 index 000000000..b8b17e818 --- /dev/null +++ b/source/devrelay/commands/Init.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include + +#include "../types/Request.h" +#include "../types/Response.h" + +class InitRequest : public Request +{ +public: + InitRequest(uint8_t request_sequence_number, uint8_t device_id); + std::vector serialize() const override; + std::unique_ptr deserialize(const std::vector &data) const override; + void create_command(uint8_t* output_data) const override; + void copy_payload(uint8_t* data) const override {} + size_t payload_size() const override { return 0; }; + std::unique_ptr create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const override; +}; + +class InitResponse : public Response +{ +public: + explicit InitResponse(uint8_t request_sequence_number, uint8_t status); + std::vector serialize() const override; +}; \ No newline at end of file diff --git a/source/devrelay/commands/Open.cpp b/source/devrelay/commands/Open.cpp new file mode 100644 index 000000000..699ef89af --- /dev/null +++ b/source/devrelay/commands/Open.cpp @@ -0,0 +1,50 @@ +#ifdef DEV_RELAY_SLIP + +#include "Open.h" + +OpenRequest::OpenRequest(const uint8_t request_sequence_number, const uint8_t device_id) : Request(request_sequence_number, CMD_OPEN, device_id) {} + +std::vector OpenRequest::serialize() const +{ + std::vector request_data; + request_data.push_back(this->get_request_sequence_number()); + request_data.push_back(this->get_command_number()); + request_data.push_back(this->get_device_id()); + return request_data; +} + +std::unique_ptr OpenRequest::deserialize(const std::vector &data) const +{ + if (data.size() < 2) + { + throw std::runtime_error("Not enough data to deserialize OpenResponse"); + } + + auto response = std::make_unique(data[0], data[1]); + return response; +} + +void OpenRequest::create_command(uint8_t* cmd_data) const +{ + init_command(cmd_data); +} + +std::unique_ptr OpenRequest::create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const +{ + std::unique_ptr response = std::make_unique(get_request_sequence_number(), status); + return response; +} + + +OpenResponse::OpenResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} + +std::vector OpenResponse::serialize() const +{ + std::vector data; + data.push_back(this->get_request_sequence_number()); + data.push_back(this->get_status()); + return data; +} + + +#endif diff --git a/source/devrelay/commands/Open.h b/source/devrelay/commands/Open.h new file mode 100644 index 000000000..25685464a --- /dev/null +++ b/source/devrelay/commands/Open.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include + +#include "../types/Request.h" +#include "../types/Response.h" + +class OpenRequest : public Request +{ +public: + OpenRequest(uint8_t request_sequence_number, uint8_t device_id); + std::vector serialize() const override; + std::unique_ptr deserialize(const std::vector &data) const override; + void create_command(uint8_t* output_data) const override; + void copy_payload(uint8_t* data) const override {} + size_t payload_size() const override { return 0; }; + std::unique_ptr create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const override; +}; + +class OpenResponse : public Response +{ +public: + explicit OpenResponse(uint8_t request_sequence_number, uint8_t status); + std::vector serialize() const override; +}; diff --git a/source/devrelay/commands/Read.cpp b/source/devrelay/commands/Read.cpp new file mode 100644 index 000000000..ae07686cc --- /dev/null +++ b/source/devrelay/commands/Read.cpp @@ -0,0 +1,79 @@ +#ifdef DEV_RELAY_SLIP + +#include "Read.h" + +ReadRequest::ReadRequest(const uint8_t request_sequence_number, const uint8_t device_id) : Request(request_sequence_number, CMD_READ, device_id), byte_count_(), address_() {} + +std::vector ReadRequest::serialize() const +{ + std::vector request_data; + request_data.push_back(this->get_request_sequence_number()); + request_data.push_back(this->get_command_number()); + request_data.push_back(this->get_device_id()); + request_data.insert(request_data.end(), get_byte_count().begin(), get_byte_count().end()); + request_data.insert(request_data.end(), get_address().begin(), get_address().end()); + return request_data; +} + +std::unique_ptr ReadRequest::deserialize(const std::vector &data) const +{ + if (data.size() < 3) + { + throw std::runtime_error("Not enough data to deserialize ReadResponse"); + } + + auto response = std::make_unique(data[0], data[1]); + if (response->get_status() == 0) + { + response->set_data(data.begin() + 2, data.end()); + } + return response; +} + +const std::array &ReadRequest::get_byte_count() const { return byte_count_; } + +const std::array &ReadRequest::get_address() const { return address_; } + +void ReadRequest::set_byte_count_from_ptr(const uint8_t *ptr, const size_t offset) { std::copy_n(ptr + offset, byte_count_.size(), byte_count_.begin()); } + +void ReadRequest::set_address_from_ptr(const uint8_t *ptr, const size_t offset) { std::copy_n(ptr + offset, address_.size(), address_.begin()); } + +void ReadRequest::create_command(uint8_t* cmd_data) const +{ + init_command(cmd_data); + std::copy(byte_count_.begin(), byte_count_.end(), cmd_data + 4); + std::copy(address_.begin(), address_.end(), cmd_data + 6); +} + +std::unique_ptr ReadRequest::create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const +{ + std::unique_ptr response = std::make_unique(get_request_sequence_number(), status); + // Copy the return data if the status is OK + if (status == 0) { + std::vector data_vector(data, data + num); + response->set_data(data_vector.begin(), data_vector.end()); + } + return response; +} + + +ReadResponse::ReadResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} + +std::vector ReadResponse::serialize() const +{ + std::vector data; + data.push_back(this->get_request_sequence_number()); + data.push_back(this->get_status()); + data.insert(data.end(), get_data().begin(), get_data().end()); + return data; +} + +void ReadResponse::set_data(const std::vector::const_iterator &begin, const std::vector::const_iterator &end) +{ + const size_t new_size = std::distance(begin, end); + data_.resize(new_size); + std::copy(begin, end, data_.begin()); // NOLINT(performance-unnecessary-value-param) +} + + +#endif diff --git a/source/devrelay/commands/Read.h b/source/devrelay/commands/Read.h new file mode 100644 index 000000000..8a0b64ff7 --- /dev/null +++ b/source/devrelay/commands/Read.h @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "../types/Request.h" +#include "../types/Response.h" + +class ReadRequest : public Request +{ +public: + ReadRequest(uint8_t request_sequence_number, uint8_t device_id); + std::vector serialize() const override; + std::unique_ptr deserialize(const std::vector &data) const override; + + const std::array &get_byte_count() const; + void set_byte_count_from_ptr(const uint8_t *ptr, size_t offset); + + const std::array &get_address() const; + void set_address_from_ptr(const uint8_t *ptr, size_t offset); + void create_command(uint8_t *output_data) const override; + void copy_payload(uint8_t *data) const override {} + size_t payload_size() const override { return 0; }; + std::unique_ptr create_response(uint8_t source, uint8_t status, const uint8_t *data, uint16_t num) const override; + +private: + std::array byte_count_; + std::array address_; +}; + +class ReadResponse : public Response +{ +public: + explicit ReadResponse(uint8_t request_sequence_number, uint8_t status); + std::vector serialize() const override; + + const std::vector &get_data() const { return data_; } + void set_data(const std::vector::const_iterator &begin, const std::vector::const_iterator &end); + +private: + std::vector data_; +}; \ No newline at end of file diff --git a/source/devrelay/commands/ReadBlock.cpp b/source/devrelay/commands/ReadBlock.cpp new file mode 100644 index 000000000..4d952470e --- /dev/null +++ b/source/devrelay/commands/ReadBlock.cpp @@ -0,0 +1,88 @@ +#ifdef DEV_RELAY_SLIP + +#include "ReadBlock.h" + +ReadBlockRequest::ReadBlockRequest(const uint8_t request_sequence_number, const uint8_t device_id, uint16_t block_size) : Request(request_sequence_number, CMD_READ_BLOCK, device_id), block_number_{}, block_size_(block_size) {} + +std::vector ReadBlockRequest::serialize() const +{ + std::vector request_data; + request_data.push_back(this->get_request_sequence_number()); + request_data.push_back(this->get_command_number()); + request_data.push_back(this->get_device_id()); + request_data.push_back(this->get_block_size() & 0xFF); + request_data.push_back((this->get_block_size() >> 8) & 0xFF); + request_data.insert(request_data.end(), block_number_.begin(), block_number_.end()); + return request_data; +} + +std::unique_ptr ReadBlockRequest::deserialize(const std::vector &data) const +{ + if (data.size() != (block_size_ + 2)) + { + throw std::runtime_error("Not enough data to deserialize ReadBlockResponse"); + } + + auto response = std::make_unique(data[0], data[1], block_size_); + if (response->get_status() == 0) + { + response->set_block_data(data.begin() + 2, data.end()); + } + return response; +} + +const std::array &ReadBlockRequest::get_block_number() const { return block_number_; } + +const uint16_t ReadBlockRequest::get_block_size() const { return block_size_; } + +void ReadBlockRequest::set_block_number_from_ptr(const uint8_t *ptr, const size_t offset) { std::copy_n(ptr + offset, block_number_.size(), block_number_.begin()); } + +void ReadBlockRequest::set_block_number_from_bytes(uint8_t l, uint8_t m, uint8_t h) +{ + block_number_[0] = l; + block_number_[1] = m; + block_number_[2] = h; +} + +void ReadBlockRequest::create_command(uint8_t *cmd_data) const { + init_command(cmd_data); + std::copy(block_number_.begin(), block_number_.end(), cmd_data + 4); +} + +std::unique_ptr ReadBlockRequest::create_response(uint8_t source, uint8_t status, const uint8_t *data, uint16_t num) const { + std::unique_ptr response = std::make_unique(get_request_sequence_number(), status, num); + // Copy the return data if the status is OK + if (status == 0) { + std::vector data_vector(data, data + num); + response->set_block_data(data_vector.begin(), data_vector.end()); + } + return response; +} + + +ReadBlockResponse::ReadBlockResponse(const uint8_t request_sequence_number, const uint8_t status, uint16_t block_size) : Response(request_sequence_number, status), block_size_(block_size) { + block_data_.resize(block_size_); +} + +std::vector ReadBlockResponse::serialize() const +{ + std::vector data; + data.push_back(this->get_request_sequence_number()); + data.push_back(this->get_status()); + data.insert(data.end(), block_data_.begin(), block_data_.end()); + return data; +} + +void ReadBlockResponse::set_block_data(std::vector::const_iterator begin, std::vector::const_iterator end) +{ + std::copy(begin, end, block_data_.begin()); // NOLINT(performance-unnecessary-value-param) +} + +const std::vector& ReadBlockResponse::get_block_data() const { + return block_data_; +} + +const uint16_t ReadBlockResponse::get_block_size() const { return block_size_; } + + +#endif diff --git a/source/devrelay/commands/ReadBlock.h b/source/devrelay/commands/ReadBlock.h new file mode 100644 index 000000000..54481a833 --- /dev/null +++ b/source/devrelay/commands/ReadBlock.h @@ -0,0 +1,46 @@ +#pragma once + +#include +#include +#include +#include + +#include "../types/Request.h" +#include "../types/Response.h" + +class ReadBlockRequest : public Request +{ +public: + ReadBlockRequest(uint8_t request_sequence_number, uint8_t device_id, uint16_t block_size); + std::vector serialize() const override; + std::unique_ptr deserialize(const std::vector &data) const override; + const std::array &get_block_number() const; + void set_block_number_from_ptr(const uint8_t *ptr, size_t offset); + void set_block_number_from_bytes(uint8_t l, uint8_t m, uint8_t h); + const uint16_t get_block_size() const; + + void create_command(uint8_t *output_data) const override; + void copy_payload(uint8_t *data) const override {} + size_t payload_size() const override { return 0; }; + std::unique_ptr create_response(uint8_t source, uint8_t status, const uint8_t *data, uint16_t num) const override; + +private: + std::array block_number_; + uint16_t block_size_; +}; + + +class ReadBlockResponse : public Response +{ +public: + explicit ReadBlockResponse(uint8_t request_sequence_number, uint8_t status, uint16_t block_size); + std::vector serialize() const override; + + void set_block_data(std::vector::const_iterator begin, std::vector::const_iterator end); + const std::vector& get_block_data() const; + const uint16_t get_block_size() const; + +private: + std::vector block_data_; + uint16_t block_size_; +}; \ No newline at end of file diff --git a/source/devrelay/commands/Reset.cpp b/source/devrelay/commands/Reset.cpp new file mode 100644 index 000000000..627d30d43 --- /dev/null +++ b/source/devrelay/commands/Reset.cpp @@ -0,0 +1,49 @@ +#ifdef DEV_RELAY_SLIP + +#include "Reset.h" + +ResetRequest::ResetRequest(const uint8_t request_sequence_number, const uint8_t device_id) : Request(request_sequence_number, CMD_RESET, device_id) {} + +std::vector ResetRequest::serialize() const +{ + std::vector request_data; + request_data.push_back(this->get_request_sequence_number()); + request_data.push_back(this->get_command_number()); + request_data.push_back(this->get_device_id()); + return request_data; +} + +std::unique_ptr ResetRequest::deserialize(const std::vector &data) const +{ + if (data.size() < 2) + { + throw std::runtime_error("Not enough data to deserialize ResetResponse"); + } + + auto response = std::make_unique(data[0], data[1]); + return response; +} + +void ResetRequest::create_command(uint8_t* cmd_data) const +{ + init_command(cmd_data); +} + +std::unique_ptr ResetRequest::create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const +{ + std::unique_ptr response = std::make_unique(get_request_sequence_number(), status); + return response; +} + +ResetResponse::ResetResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} + +std::vector ResetResponse::serialize() const +{ + std::vector data; + data.push_back(this->get_request_sequence_number()); + data.push_back(this->get_status()); + return data; +} + + +#endif diff --git a/source/devrelay/commands/Reset.h b/source/devrelay/commands/Reset.h new file mode 100644 index 000000000..51e532a0f --- /dev/null +++ b/source/devrelay/commands/Reset.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include + +#include "../types/Request.h" +#include "../types/Response.h" + +class ResetRequest : public Request +{ +public: + ResetRequest(uint8_t request_sequence_number, uint8_t device_id); + std::vector serialize() const override; + std::unique_ptr deserialize(const std::vector &data) const override; + void create_command(uint8_t* output_data) const override; + void copy_payload(uint8_t* data) const override {} + size_t payload_size() const override { return 0; }; + std::unique_ptr create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const override; +}; + +class ResetResponse : public Response +{ +public: + explicit ResetResponse(uint8_t request_sequence_number, uint8_t status); + std::vector serialize() const override; +}; diff --git a/source/devrelay/commands/Status.cpp b/source/devrelay/commands/Status.cpp new file mode 100644 index 000000000..3ddcb6e01 --- /dev/null +++ b/source/devrelay/commands/Status.cpp @@ -0,0 +1,88 @@ +#ifdef DEV_RELAY_SLIP + +#include + +#include "Status.h" + +StatusRequest::StatusRequest(const uint8_t request_sequence_number, const uint8_t device_id, const uint8_t status_code) : Request(request_sequence_number, CMD_STATUS, device_id), status_code_(status_code) +{ +} + +std::vector StatusRequest::serialize() const +{ + std::vector request_data; + request_data.push_back(this->get_request_sequence_number()); + request_data.push_back(this->get_command_number()); + request_data.push_back(this->get_device_id()); + request_data.push_back(this->get_status_code()); + + return request_data; +} + +std::unique_ptr StatusRequest::deserialize(const std::vector &data) const +{ + if (data.size() < 2) + { + throw std::runtime_error("Not enough data to deserialize StatusResponse"); + } + + auto response = std::make_unique(data[0], data[1]); + + if (response->get_status() == 0 && data.size() > 2) + { + for (size_t i = 2; i < data.size(); ++i) + { + response->add_data(data[i]); + } + } + + return response; +} + +void StatusRequest::create_command(uint8_t* cmd_data) const +{ + init_command(cmd_data); + cmd_data[4] = status_code_; +} + +std::unique_ptr StatusRequest::create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const +{ + std::unique_ptr response = std::make_unique(get_request_sequence_number(), status); + // Copy the return data if the status is OK + if (status == 0) { + std::vector data_vector(data, data + num); + response->set_data(data_vector.begin(), data_vector.end()); + } + + return response; +} + + +StatusResponse::StatusResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} + +const std::vector &StatusResponse::get_data() const { return data_; } + +void StatusResponse::add_data(const uint8_t d) { data_.push_back(d); } + +void StatusResponse::set_data(const std::vector::const_iterator& begin, const std::vector::const_iterator& end) +{ + const size_t new_size = std::distance(begin, end); + data_.resize(new_size); + std::copy(begin, end, data_.begin()); // NOLINT(performance-unnecessary-value-param) +} + +std::vector StatusResponse::serialize() const +{ + std::vector data; + data.push_back(this->get_request_sequence_number()); + data.push_back(this->get_status()); + + for (uint8_t b : get_data()) + { + data.push_back(b); + } + return data; +} + + +#endif diff --git a/source/devrelay/commands/Status.h b/source/devrelay/commands/Status.h new file mode 100644 index 000000000..ce905b2f2 --- /dev/null +++ b/source/devrelay/commands/Status.h @@ -0,0 +1,40 @@ +#pragma once + +#include +#include + +#include "../types/Request.h" +#include "../types/Response.h" + +class StatusRequest : public Request +{ +public: + StatusRequest(uint8_t request_sequence_number, uint8_t device_id, uint8_t status_code); + virtual std::vector serialize() const override; + std::unique_ptr deserialize(const std::vector &data) const override; + + uint8_t get_status_code() const { return status_code_; } + + void create_command(uint8_t* output_data) const override; + void copy_payload(uint8_t* data) const override {} + size_t payload_size() const override { return 0; }; + std::unique_ptr create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const override; + +private: + uint8_t status_code_; +}; + + +class StatusResponse : public Response +{ +public: + explicit StatusResponse(uint8_t request_sequence_number, uint8_t status); + std::vector serialize() const override; + + const std::vector &get_data() const; + void add_data(uint8_t d); + void set_data(const std::vector::const_iterator& begin, const std::vector::const_iterator& end); + +private: + std::vector data_; +}; diff --git a/source/SPoverSLIP/WriteRequest.cpp b/source/devrelay/commands/Write.cpp similarity index 55% rename from source/SPoverSLIP/WriteRequest.cpp rename to source/devrelay/commands/Write.cpp index 4086dfe5c..273c2c18c 100644 --- a/source/SPoverSLIP/WriteRequest.cpp +++ b/source/devrelay/commands/Write.cpp @@ -1,18 +1,15 @@ -// ReSharper disable CppPassValueParameterByConstReference +#ifdef DEV_RELAY_SLIP -#include "WriteRequest.h" +#include "Write.h" -#include "SmartPortCodes.h" -#include "WriteResponse.h" - -WriteRequest::WriteRequest(const uint8_t request_sequence_number, const uint8_t sp_unit) : Request(request_sequence_number, SP_WRITE, sp_unit), byte_count_(), address_() {} +WriteRequest::WriteRequest(const uint8_t request_sequence_number, const uint8_t device_id) : Request(request_sequence_number, CMD_WRITE, device_id), byte_count_(), address_() {} std::vector WriteRequest::serialize() const { std::vector request_data; request_data.push_back(this->get_request_sequence_number()); request_data.push_back(this->get_command_number()); - request_data.push_back(this->get_sp_unit()); + request_data.push_back(this->get_device_id()); request_data.insert(request_data.end(), get_byte_count().begin(), get_byte_count().end()); request_data.insert(request_data.end(), get_address().begin(), get_address().end()); request_data.insert(request_data.end(), get_data().begin(), get_data().end()); @@ -42,4 +39,39 @@ void WriteRequest::set_data_from_ptr(const uint8_t *ptr, const size_t offset, co { data_.clear(); data_.insert(data_.end(), ptr + offset, ptr + offset + length); -} \ No newline at end of file +} + +void WriteRequest::create_command(uint8_t* cmd_data) const +{ + init_command(cmd_data); + std::copy(byte_count_.begin(), byte_count_.end(), cmd_data + 4); + std::copy(address_.begin(), address_.end(), cmd_data + 6); +} + +void WriteRequest::copy_payload(uint8_t* data) const { + std::copy(data_.begin(), data_.end(), data); +} + +size_t WriteRequest::payload_size() const { + return data_.size(); +} + +std::unique_ptr WriteRequest::create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const +{ + std::unique_ptr response = std::make_unique(get_request_sequence_number(), status); + return response; +} + + +WriteResponse::WriteResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} + +std::vector WriteResponse::serialize() const +{ + std::vector data; + data.push_back(this->get_request_sequence_number()); + data.push_back(this->get_status()); + return data; +} + + +#endif diff --git a/source/SPoverSLIP/WriteRequest.h b/source/devrelay/commands/Write.h similarity index 61% rename from source/SPoverSLIP/WriteRequest.h rename to source/devrelay/commands/Write.h index 0e16abed8..a114959c4 100644 --- a/source/SPoverSLIP/WriteRequest.h +++ b/source/devrelay/commands/Write.h @@ -5,13 +5,13 @@ #include #include -#include "Request.h" -#include "Response.h" +#include "../types/Request.h" +#include "../types/Response.h" class WriteRequest : public Request { public: - WriteRequest(const uint8_t request_sequence_number, const uint8_t sp_unit); + WriteRequest(const uint8_t request_sequence_number, const uint8_t device_id); std::vector serialize() const override; std::unique_ptr deserialize(const std::vector &data) const override; @@ -24,8 +24,20 @@ class WriteRequest : public Request const std::vector &get_data() const { return data_; } void set_data_from_ptr(const uint8_t *ptr, size_t offset, size_t length); + void create_command(uint8_t *output_data) const override; + void copy_payload(uint8_t *data) const override; + size_t payload_size() const override; + std::unique_ptr create_response(uint8_t source, uint8_t status, const uint8_t *data, uint16_t num) const override; + private: std::array byte_count_; std::array address_; std::vector data_; }; + +class WriteResponse : public Response +{ +public: + explicit WriteResponse(uint8_t request_sequence_number, uint8_t status); + std::vector serialize() const override; +}; diff --git a/source/devrelay/commands/WriteBlock.cpp b/source/devrelay/commands/WriteBlock.cpp new file mode 100644 index 000000000..3f686d245 --- /dev/null +++ b/source/devrelay/commands/WriteBlock.cpp @@ -0,0 +1,81 @@ +#ifdef DEV_RELAY_SLIP + +#include "WriteBlock.h" +#include + +WriteBlockRequest::WriteBlockRequest(const uint8_t request_sequence_number, const uint8_t device_id, const uint16_t block_size) : Request(request_sequence_number, CMD_WRITE_BLOCK, device_id), block_number_{}, block_data_{}, block_size_(block_size) {} + +std::vector WriteBlockRequest::serialize() const +{ + std::vector request_data; + request_data.push_back(this->get_request_sequence_number()); + request_data.push_back(this->get_command_number()); + request_data.push_back(this->get_device_id()); + request_data.push_back(this->get_block_size() & 0xFF); + request_data.push_back((this->get_block_size() >> 8) & 0xFF); + request_data.insert(request_data.end(), block_number_.begin(), block_number_.end()); + request_data.insert(request_data.end(), block_data_.begin(), block_data_.end()); + + return request_data; +} + +std::unique_ptr WriteBlockRequest::deserialize(const std::vector &data) const +{ + if (data.size() < 2) + { + throw std::runtime_error("Not enough data to deserialize WriteBlockResponse"); + } + + auto response = std::make_unique(data[0], data[1]); + return response; +} + +const std::array &WriteBlockRequest::get_block_number() const { return block_number_; } + +const std::vector &WriteBlockRequest::get_block_data() const { return block_data_; } + +const uint16_t WriteBlockRequest::get_block_size() const { return block_size_; } + +void WriteBlockRequest::set_block_number_from_ptr(const uint8_t *ptr, const size_t offset) { std::copy_n(ptr + offset, block_number_.size(), block_number_.begin()); } + +void WriteBlockRequest::set_block_data_from_ptr(const uint8_t *ptr, const size_t offset) { std::copy_n(ptr + offset, block_data_.size(), block_data_.begin()); } + +void WriteBlockRequest::set_block_number_from_bytes(uint8_t l, uint8_t m, uint8_t h) +{ + block_number_[0] = l; + block_number_[1] = m; + block_number_[2] = h; +} + +void WriteBlockRequest::create_command(uint8_t* cmd_data) const +{ + init_command(cmd_data); + std::copy(block_number_.begin(), block_number_.end(), cmd_data + 4); +} + +void WriteBlockRequest::copy_payload(uint8_t* data) const { + std::copy(block_data_.begin(), block_data_.end(), data); +} + +size_t WriteBlockRequest::payload_size() const { + return block_data_.size(); +} + +std::unique_ptr WriteBlockRequest::create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const +{ + std::unique_ptr response = std::make_unique(get_request_sequence_number(), status); + return response; +} + +WriteBlockResponse::WriteBlockResponse(const uint8_t request_sequence_number, const uint8_t status) : Response(request_sequence_number, status) {} + +std::vector WriteBlockResponse::serialize() const +{ + std::vector data; + data.push_back(this->get_request_sequence_number()); + data.push_back(this->get_status()); + return data; +} + + +#endif diff --git a/source/devrelay/commands/WriteBlock.h b/source/devrelay/commands/WriteBlock.h new file mode 100644 index 000000000..04cb1ceb1 --- /dev/null +++ b/source/devrelay/commands/WriteBlock.h @@ -0,0 +1,41 @@ +#pragma once + +#include +#include +#include +#include + +#include "../types/Request.h" +#include "../types/Response.h" + +class WriteBlockRequest : public Request +{ +public: + WriteBlockRequest(uint8_t request_sequence_number, uint8_t device_id, uint16_t block_size); + std::vector serialize() const override; + std::unique_ptr deserialize(const std::vector &data) const override; + const std::array &get_block_number() const; + void set_block_number_from_ptr(const uint8_t *ptr, size_t offset); + + const std::vector &get_block_data() const; + void set_block_data_from_ptr(const uint8_t *ptr, size_t offset); + void set_block_number_from_bytes(uint8_t l, uint8_t m, uint8_t h); + const uint16_t get_block_size() const; + + void create_command(uint8_t *output_data) const override; + void copy_payload(uint8_t *data) const override; + size_t payload_size() const override; + std::unique_ptr create_response(uint8_t source, uint8_t status, const uint8_t *data, uint16_t num) const override; + +private: + std::array block_number_; + std::vector block_data_; + uint16_t block_size_; +}; + +class WriteBlockResponse : public Response +{ +public: + explicit WriteBlockResponse(uint8_t request_sequence_number, uint8_t status); + std::vector serialize() const override; +}; diff --git a/source/devrelay/service/COMConnection.cpp b/source/devrelay/service/COMConnection.cpp new file mode 100644 index 000000000..019a47f32 --- /dev/null +++ b/source/devrelay/service/COMConnection.cpp @@ -0,0 +1,67 @@ +#if defined(DEV_RELAY_SLIP) && defined(SLIP_PROTOCOL_COM) + +#include "COMConnection.h" +#include +#include "../slip/SLIP.h" + +COMConnection::COMConnection(const std::string &port_name, struct sp_port *port, bool is_connected) : port_name_(port_name), port_(port) { set_is_connected(is_connected); } + +COMConnection::~COMConnection() { close_connection(); } + +void COMConnection::send_data(const std::vector &data) +{ + if (!is_connected()) + { + std::cerr << "COMConnection: Not connected\n"; + return; + } + + const auto slip_data = SLIP::encode(data); + sp_nonblocking_write(port_, slip_data.data(), slip_data.size()); +} + +void COMConnection::create_read_channel() +{ + reading_thread_ = std::thread([self = shared_from_this()]() { + std::vector buffer(1024); + while (self->is_connected()) + { + int bytes_read = sp_nonblocking_read(self->port_, buffer.data(), buffer.size()); + if (bytes_read > 0) + { + std::vector> decoded_packets = SLIP::split_into_packets(buffer.data(), buffer.size()); + if (!decoded_packets.empty()) + { + for (const auto &packet : decoded_packets) + { + if (!packet.empty()) + { + { + std::lock_guard lock(self->data_mutex_); + self->data_map_[packet[0]] = packet; + } + self->data_cv_.notify_all(); + } + } + } + } + } + }); +} + +void COMConnection::close_connection() +{ + set_is_connected(false); + if (reading_thread_.joinable()) + { + reading_thread_.join(); + } + if (port_) + { + sp_close(port_); + sp_free_port(port_); + port_ = nullptr; + } +} + +#endif diff --git a/source/devrelay/service/COMConnection.h b/source/devrelay/service/COMConnection.h new file mode 100644 index 000000000..7aeceeb5b --- /dev/null +++ b/source/devrelay/service/COMConnection.h @@ -0,0 +1,41 @@ +#pragma once + +#if defined(DEV_RELAY_SLIP) && defined(SLIP_PROTOCOL_COM) + +#include "Connection.h" +#include +#include +#include +#include +#include +#include +#include +#include + +class COMConnection : public Connection, public std::enable_shared_from_this +{ +public: + explicit COMConnection(const std::string &port_name, struct sp_port *port, bool is_connected); + virtual ~COMConnection(); + + void send_data(const std::vector &data) override; + void create_read_channel() override; + void close_connection() override; + + sp_port *get_port() const { return port_; } + void set_port(sp_port *port) + { + if (port_ != nullptr) + { + sp_close(port_); + sp_free_port(port_); + } + port_ = port; + } + +private: + std::string port_name_; + struct sp_port *port_; +}; + +#endif diff --git a/source/SPoverSLIP/Connection.cpp b/source/devrelay/service/Connection.cpp similarity index 97% rename from source/SPoverSLIP/Connection.cpp rename to source/devrelay/service/Connection.cpp index 4b2aa51f8..16e7ce898 100644 --- a/source/SPoverSLIP/Connection.cpp +++ b/source/devrelay/service/Connection.cpp @@ -1,10 +1,13 @@ -#include "Connection.h" +#ifdef DEV_RELAY_SLIP + #include #include #include #include #include +#include "Connection.h" + // This is called after AppleWin sends a request to a device, and is waiting for the response std::vector Connection::wait_for_response(uint8_t request_id, std::chrono::seconds timeout) { @@ -47,3 +50,5 @@ void Connection::join() reading_thread_.join(); } } + +#endif diff --git a/source/SPoverSLIP/Connection.h b/source/devrelay/service/Connection.h similarity index 96% rename from source/SPoverSLIP/Connection.h rename to source/devrelay/service/Connection.h index 8f703e95e..1aa3bbe72 100644 --- a/source/SPoverSLIP/Connection.h +++ b/source/devrelay/service/Connection.h @@ -1,4 +1,5 @@ #pragma once +#ifdef DEV_RELAY_SLIP #include #include @@ -37,3 +38,5 @@ class Connection std::mutex data_mutex_; std::condition_variable data_cv_; }; + +#endif \ No newline at end of file diff --git a/source/SPoverSLIP/Listener.cpp b/source/devrelay/service/Listener.cpp similarity index 92% rename from source/SPoverSLIP/Listener.cpp rename to source/devrelay/service/Listener.cpp index c4bba3cb2..bbdfc642f 100644 --- a/source/SPoverSLIP/Listener.cpp +++ b/source/devrelay/service/Listener.cpp @@ -1,19 +1,20 @@ +#if defined(DEV_RELAY_SLIP) && defined(SLIP_PROTOCOL_NET) + #include #include +#include #include #include #include "Listener.h" -#include "InitRequest.h" -#include "InitResponse.h" -#include "SLIP.h" -#include "TCPConnection.h" -#include "StatusRequest.h" -#include "StatusResponse.h" +#include "../commands/Init.h" +#include "../commands/Status.h" +#include "../slip/SLIP.h" #include "Log.h" #include "Requestor.h" +#include "TCPConnection.h" #ifdef WIN32 #include @@ -28,8 +29,6 @@ #include #include #include - #include "StatusRequest.h" - #include "StatusRequest.h" #define CLOSE_SOCKET close #define SOCKET_ERROR_CODE errno #define INVALID_SOCKET -1 @@ -38,7 +37,7 @@ const std::regex Listener::ipPattern("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"); -Listener &GetSPoverSLIPListener(void) +Listener &GetCommandListener(void) { static Listener listener; return listener; @@ -260,13 +259,13 @@ void Listener::stop() } // Returns the target's device id, and connection to it from the host_id supplied by caller. -// We store (for example) device_ids in applewin with values 1-5 for connection 1, 6-8 for connection 2, but each device things they are 1-5, and 1-3 (not 6-8). +// We store (for example) device_ids in applewin with values 1-5 for connection 1, 6-8 for connection 2, but each device thinks they are 1-5, and 1-3 (not 6-8). // However the apple side sees 1-8, and so we have to convert 6, 7, 8 into the target's 1, 2, 3 -std::pair> Listener::find_connection_with_device(const uint8_t host_id) const +std::pair> Listener::find_connection_with_device(const uint8_t host_device_id) const { std::pair> result; - auto it = connection_info_map_.find(host_id); + auto it = connection_info_map_.find(host_device_id); if (it != connection_info_map_.end()) { auto connection_info = it->second; @@ -285,10 +284,10 @@ std::vector> Listener::get_all_connections() co return connections; } -std::pair Listener::first_two_disk_devices() const +std::pair Listener::first_two_disk_devices(std::function&)> is_disk_device) const { std::pair disk_ids = {-1, -1}; - const auto connections = GetSPoverSLIPListener().get_all_connections(); + const auto connections = GetCommandListener().get_all_connections(); for (const auto &id_and_connection : connections) { const uint8_t unit_number = id_and_connection.first; @@ -305,8 +304,7 @@ std::pair Listener::first_two_disk_devices() const { const std::vector &data = statusResponse->get_data(); - // Check if data size is at least 22 and the device type is a disk, and that the disk status is ONLINE (status[0] bit 4) - if (data.size() >= 22 && (data[21] == 0x01 || data[21] == 0x02 || data[21] == 0x0A) && ((data[0] & 0x10) == 0x10)) + if (is_disk_device(data)) { // We use the unique unit_number below, as eventually we'll look these up again in the Listener's map to find a connection. @@ -342,4 +340,7 @@ void Listener::connection_closed(Connection *connection) ++it; } } -} \ No newline at end of file +} + + +#endif diff --git a/source/SPoverSLIP/Listener.h b/source/devrelay/service/Listener.h similarity index 91% rename from source/SPoverSLIP/Listener.h rename to source/devrelay/service/Listener.h index 1377ec905..b9ccee1e8 100644 --- a/source/SPoverSLIP/Listener.h +++ b/source/devrelay/service/Listener.h @@ -1,11 +1,15 @@ #pragma once +#if defined(DEV_RELAY_SLIP) && defined(SLIP_PROTOCOL_NET) +#include +#include #include #include #include -#include #include #include +#include +#include #include "Connection.h" @@ -37,7 +41,6 @@ class Listener std::map connection_info_map_; public: - // Listener(std::string ip_address, const uint16_t port); Listener(); ~Listener(); @@ -57,7 +60,7 @@ class Listener uint8_t get_total_device_count(); void set_start_on_init(bool should_start) { should_start_ = should_start; } bool get_start_on_init() { return should_start_; } - std::pair first_two_disk_devices() const; + std::pair first_two_disk_devices(std::function&)> is_disk_device) const; // default values for listener bool default_start_listener = true; @@ -84,3 +87,5 @@ class Listener void connection_closed(Connection *connection); void add_connection_info(uint8_t key, const ConnectionInfo &info) { connection_info_map_[key] = info; } }; + +#endif diff --git a/source/SPoverSLIP/Requestor.cpp b/source/devrelay/service/Requestor.cpp similarity index 96% rename from source/SPoverSLIP/Requestor.cpp rename to source/devrelay/service/Requestor.cpp index 826469bc0..95c89b8fd 100644 --- a/source/SPoverSLIP/Requestor.cpp +++ b/source/devrelay/service/Requestor.cpp @@ -1,7 +1,9 @@ +#ifdef DEV_RELAY_SLIP + +#include #include "Requestor.h" #include "Listener.h" -#include uint8_t Requestor::request_number_ = 0; @@ -32,4 +34,6 @@ uint8_t Requestor::next_request_number() const uint8_t current_number = request_number_; request_number_ = (request_number_ + 1) % 256; return current_number; -} \ No newline at end of file +} + +#endif diff --git a/source/SPoverSLIP/Requestor.h b/source/devrelay/service/Requestor.h similarity index 86% rename from source/SPoverSLIP/Requestor.h rename to source/devrelay/service/Requestor.h index 9a6453864..5b2d5c277 100644 --- a/source/SPoverSLIP/Requestor.h +++ b/source/devrelay/service/Requestor.h @@ -1,10 +1,11 @@ #pragma once -#include "Connection.h" -#include "Request.h" -#include "Response.h" #include +#include "Connection.h" +#include "../types/Request.h" +#include "../types/Response.h" + class Requestor { public: diff --git a/source/SPoverSLIP/TCPConnection.cpp b/source/devrelay/service/TCPConnection.cpp similarity index 94% rename from source/SPoverSLIP/TCPConnection.cpp rename to source/devrelay/service/TCPConnection.cpp index 1f831894a..006166405 100644 --- a/source/SPoverSLIP/TCPConnection.cpp +++ b/source/devrelay/service/TCPConnection.cpp @@ -1,9 +1,11 @@ -#include "TCPConnection.h" +#if defined(DEV_RELAY_SLIP) && defined(SLIP_PROTOCOL_NET) #include #include #include +#include "TCPConnection.h" + #ifdef WIN32 #include #pragma comment(lib, "ws2_32.lib") @@ -22,8 +24,10 @@ #endif #include "Log.h" -#include "SLIP.h" -#include +#include "../slip/SLIP.h" +#include "Listener.h" + +extern class Listener &GetCommandListener(void); void TCPConnection::close_connection() { @@ -129,9 +133,11 @@ void TCPConnection::create_read_channel() complete_data.clear(); } } - GetSPoverSLIPListener().connection_closed(self.get()); + GetCommandListener().connection_closed(self.get()); LogFileOutput("TCPConnection::create_read_channel - thread is EXITING\n"); }); reading_thread_.detach(); } + +#endif \ No newline at end of file diff --git a/source/SPoverSLIP/TCPConnection.h b/source/devrelay/service/TCPConnection.h similarity index 88% rename from source/SPoverSLIP/TCPConnection.h rename to source/devrelay/service/TCPConnection.h index 982a99c5e..223910193 100644 --- a/source/SPoverSLIP/TCPConnection.h +++ b/source/devrelay/service/TCPConnection.h @@ -1,9 +1,12 @@ #pragma once -#include "Connection.h" +#if defined(DEV_RELAY_SLIP) && defined(SLIP_PROTOCOL_NET) + #include #include +#include "Connection.h" + class TCPConnection : public Connection, public std::enable_shared_from_this { public: @@ -19,3 +22,4 @@ class TCPConnection : public Connection, public std::enable_shared_from_this +#include "SLIP.h" + std::vector SLIP::encode(const std::vector &data) { std::vector encoded_data; @@ -142,3 +144,6 @@ std::vector> SLIP::split_into_packets(const uint8_t *data, return decoded_packets; } + + +#endif diff --git a/source/SPoverSLIP/SLIP.h b/source/devrelay/slip/SLIP.h similarity index 100% rename from source/SPoverSLIP/SLIP.h rename to source/devrelay/slip/SLIP.h diff --git a/source/devrelay/types/Command.h b/source/devrelay/types/Command.h new file mode 100644 index 000000000..9167f66ba --- /dev/null +++ b/source/devrelay/types/Command.h @@ -0,0 +1,32 @@ +#pragma once + +#include +#include + +enum +{ + CMD_STATUS = 0, + CMD_READ_BLOCK = 1, + CMD_WRITE_BLOCK = 2, + CMD_FORMAT = 3, + CMD_CONTROL = 4, + CMD_INIT = 5, + CMD_OPEN = 6, + CMD_CLOSE = 7, + CMD_READ = 8, + CMD_WRITE = 9, + CMD_RESET = 10 +}; + +class Command +{ +private: + uint8_t request_sequence_number_ = 0; + +public: + explicit Command(const uint8_t request_sequence_number) : request_sequence_number_(request_sequence_number) {} + virtual ~Command() = default; + + uint8_t get_request_sequence_number() const { return request_sequence_number_; } + virtual std::vector serialize() const = 0; +}; diff --git a/source/devrelay/types/Request.cpp b/source/devrelay/types/Request.cpp new file mode 100644 index 000000000..c8c41e3c2 --- /dev/null +++ b/source/devrelay/types/Request.cpp @@ -0,0 +1,120 @@ +#ifdef DEV_RELAY_SLIP + +#include + +#include "Request.h" + +#include "../commands/Close.h" +#include "../commands/Control.h" +#include "../commands/Format.h" +#include "../commands/Init.h" +#include "../commands/Open.h" +#include "../commands/Read.h" +#include "../commands/ReadBlock.h" +#include "../commands/Reset.h" +#include "../commands/Status.h" +#include "../commands/Write.h" +#include "../commands/WriteBlock.h" + +Request::Request(const uint8_t request_sequence_number, const uint8_t command_number, const uint8_t device_id) : Command(request_sequence_number), command_number_(command_number), device_id_(device_id) {} + +uint8_t Request::get_command_number() const { return command_number_; } + +uint8_t Request::get_device_id() const { return device_id_; } + +// All Request subclasses when writing to the command data will first initialise it and set command value +// cmd_data is really a pointer to a iwm_decoded_cmd_t object. This all needs rewriting to be cleaner. +void Request::init_command(uint8_t* cmd_data) const { + std::fill(cmd_data, cmd_data + 9, 0); + cmd_data[0] = get_command_number(); +} + +std::unique_ptr Request::from_packet(const std::vector& packet) { + std::unique_ptr request; + uint8_t command = packet[1]; + switch(command) { + + case CMD_STATUS: { + request = std::make_unique(packet[0], packet[2], packet[3]); + break; + } + + case CMD_CONTROL: { + // +6 = 3 for "header", 1 for control code, 2 for length bytes we need to skip + std::vector payload(packet.begin() + 6, packet.end()); + request = std::make_unique(packet[0], packet[2], packet[3], payload); + break; + } + + case CMD_READ_BLOCK: { + auto bs = (packet[4] << 8) | packet[3]; + auto readBlockRequest = std::make_unique(packet[0], packet[2], bs); + readBlockRequest->set_block_number_from_ptr(packet.data(), 5); + request = std::move(readBlockRequest); + break; + } + + case CMD_WRITE_BLOCK: { + auto bs = (packet[4] << 8) | packet[3]; + auto writeBlockRequest = std::make_unique(packet[0], packet[2], bs); + writeBlockRequest->set_block_number_from_ptr(packet.data(), 5); + writeBlockRequest->set_block_data_from_ptr(packet.data(), 8); + request = std::move(writeBlockRequest); + break; + } + + case CMD_FORMAT: { + request = std::make_unique(packet[0], packet[2]); + break; + } + + case CMD_INIT: { + request = std::make_unique(packet[0], packet[2]); + break; + } + + case CMD_OPEN: { + request = std::make_unique(packet[0], packet[2]); + break; + } + + case CMD_CLOSE: { + request = std::make_unique(packet[0], packet[2]); + break; + } + + case CMD_READ: { + auto readRequest = std::make_unique(packet[0], packet[2]); + readRequest->set_byte_count_from_ptr(packet.data(), 3); + readRequest->set_address_from_ptr(packet.data(), 5); + request = std::move(readRequest); + break; + } + + case CMD_WRITE: { + auto writeRequest = std::make_unique(packet[0], packet[2]); + writeRequest->set_byte_count_from_ptr(packet.data(), 3); + writeRequest->set_address_from_ptr(packet.data(), 5); + writeRequest->set_data_from_ptr(packet.data(), 8, packet.size() - 8); + request = std::move(writeRequest); + break; + } + + case CMD_RESET: { + request = std::make_unique(packet[0], packet[2]); + break; + } + + default: { + std::ostringstream oss; + oss << "Unknown command: %d" << command; + throw std::runtime_error(oss.str()); + break; + } + + } + return request; +} + + +#endif diff --git a/source/devrelay/types/Request.h b/source/devrelay/types/Request.h new file mode 100644 index 000000000..1e75bbde2 --- /dev/null +++ b/source/devrelay/types/Request.h @@ -0,0 +1,40 @@ +#pragma once + +#include +#include +#include +#include + +#include "Command.h" + +// Forward reference +class Response; + +class Request : public Command +{ +public: + Request(uint8_t request_sequence_number, uint8_t command_number, uint8_t device_id); + std::vector serialize() const override = 0; + virtual std::unique_ptr deserialize(const std::vector &data) const = 0; + + uint8_t get_command_number() const; + uint8_t get_device_id() const; + + // Create the subclass specific Request type from the packet data + static std::unique_ptr from_packet(const std::vector& packet); + + // These are implemented per subclass if they are required. + virtual void copy_payload(uint8_t* data) const = 0; + virtual size_t payload_size() const = 0; + + // Creates a Response subclass version specific to the Request subclass, using the data given to us to process + virtual std::unique_ptr create_response(uint8_t source, uint8_t status, const uint8_t* data, uint16_t num) const = 0; + + // this is part of the iwm_decoded_cmd_t handling (even though we're given a pointer to its data), clearing the output data and setting the command value in it + void init_command(uint8_t* cmd_data) const; + virtual void create_command(uint8_t* cmd_data) const = 0; + +private: + uint8_t command_number_ = 0; + uint8_t device_id_ = 0; +}; diff --git a/source/SPoverSLIP/Response.cpp b/source/devrelay/types/Response.cpp similarity index 58% rename from source/SPoverSLIP/Response.cpp rename to source/devrelay/types/Response.cpp index d212ae0f6..f085217b5 100644 --- a/source/SPoverSLIP/Response.cpp +++ b/source/devrelay/types/Response.cpp @@ -1,6 +1,10 @@ +#ifdef DEV_RELAY_SLIP #include "Response.h" -Response::Response(const uint8_t request_sequence_number, const uint8_t status) : Packet(request_sequence_number), status_(status) {} +Response::Response(const uint8_t request_sequence_number, const uint8_t status) : Command(request_sequence_number), status_(status) {} uint8_t Response::get_status() const { return status_; } + + +#endif diff --git a/source/SPoverSLIP/Response.h b/source/devrelay/types/Response.h similarity index 81% rename from source/SPoverSLIP/Response.h rename to source/devrelay/types/Response.h index a87ad1ecf..86fffacfe 100644 --- a/source/SPoverSLIP/Response.h +++ b/source/devrelay/types/Response.h @@ -1,10 +1,11 @@ #pragma once -#include "Packet.h" #include #include -class Response : public Packet +#include "Command.h" + +class Response : public Command { public: Response(uint8_t request_sequence_number, uint8_t status); diff --git a/source/devrelay/util.cpp b/source/devrelay/util.cpp new file mode 100644 index 000000000..e0febfb59 --- /dev/null +++ b/source/devrelay/util.cpp @@ -0,0 +1,18 @@ +#ifdef DEV_RELAY_SLIP +#include "util.h" + +void hexDump(const void* data, size_t size) { + const unsigned char* byte = reinterpret_cast(data); + for (size_t i = 0; i < size; ++i) { + if (i % 16 == 0) { + std::cout << std::setfill('0') << std::setw(4) << std::hex << i << ": "; + } + + std::cout << std::setfill('0') << std::setw(2) << std::hex << (unsigned int)byte[i] << " "; + + if ((i + 1) % 16 == 0 || i == size - 1) { + std::cout << std::endl; + } + } +} +#endif diff --git a/source/devrelay/util.h b/source/devrelay/util.h new file mode 100644 index 000000000..8e5abc3fa --- /dev/null +++ b/source/devrelay/util.h @@ -0,0 +1,9 @@ +#pragma once +#ifdef DEV_RELAY_SLIP + +#include +#include + +void hexDump(const void* data, size_t size); + +#endif