From 6b4d3ecb26c40f5c48493e2c6d534ac128a6ceef Mon Sep 17 00:00:00 2001 From: Nav Date: Sat, 21 Jan 2023 13:37:56 +0000 Subject: [PATCH] Moved `toHex` functions to String helper class --- .../Gdb/AvrGdb/CommandPackets/ReadMemory.cpp | 4 ++- .../Gdb/CommandPackets/BloomVersion.cpp | 5 ++- .../CommandPackets/BloomVersionMachine.cpp | 5 ++- .../Gdb/CommandPackets/EepromFill.cpp | 10 +++--- .../Gdb/CommandPackets/GenerateSvd.cpp | 10 +++--- .../Gdb/CommandPackets/HelpMonitorInfo.cpp | 4 ++- .../Gdb/CommandPackets/ReadRegisters.cpp | 4 ++- .../Gdb/CommandPackets/ResetTarget.cpp | 4 ++- src/DebugServer/Gdb/Packet.hpp | 36 ------------------- .../Gdb/ResponsePackets/TargetStopped.hpp | 4 ++- src/Helpers/String.cpp | 30 ++++++++++++++++ src/Helpers/String.hpp | 5 +++ 12 files changed, 70 insertions(+), 51 deletions(-) diff --git a/src/DebugServer/Gdb/AvrGdb/CommandPackets/ReadMemory.cpp b/src/DebugServer/Gdb/AvrGdb/CommandPackets/ReadMemory.cpp index 2fa47881..7e69ca1c 100644 --- a/src/DebugServer/Gdb/AvrGdb/CommandPackets/ReadMemory.cpp +++ b/src/DebugServer/Gdb/AvrGdb/CommandPackets/ReadMemory.cpp @@ -3,7 +3,9 @@ #include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp" #include "src/DebugServer/Gdb/ResponsePackets/ResponsePacket.hpp" +#include "src/Helpers/String.hpp" #include "src/Logger/Logger.hpp" + #include "src/Exceptions/Exception.hpp" namespace Bloom::DebugServer::Gdb::AvrGdb::CommandPackets @@ -146,7 +148,7 @@ namespace Bloom::DebugServer::Gdb::AvrGdb::CommandPackets memoryBuffer.insert(memoryBuffer.end(), (this->bytes - bytesToRead), 0x00); } - debugSession.connection.writePacket(ResponsePacket(Packet::toHex(memoryBuffer))); + debugSession.connection.writePacket(ResponsePacket(String::toHex(memoryBuffer))); } catch (const Exception& exception) { Logger::error("Failed to read memory from target - " + exception.getMessage()); diff --git a/src/DebugServer/Gdb/CommandPackets/BloomVersion.cpp b/src/DebugServer/Gdb/CommandPackets/BloomVersion.cpp index 5f1ca2b6..9a2fd7a7 100644 --- a/src/DebugServer/Gdb/CommandPackets/BloomVersion.cpp +++ b/src/DebugServer/Gdb/CommandPackets/BloomVersion.cpp @@ -6,8 +6,11 @@ #include "src/DebugServer/Gdb/ResponsePackets/ResponsePacket.hpp" #include "src/Application.hpp" + #include "src/Helpers/Paths.hpp" +#include "src/Helpers/String.hpp" #include "src/Logger/Logger.hpp" + #include "src/Exceptions/Exception.hpp" namespace Bloom::DebugServer::Gdb::CommandPackets @@ -26,7 +29,7 @@ namespace Bloom::DebugServer::Gdb::CommandPackets void BloomVersion::handle(DebugSession& debugSession, TargetControllerConsole&) { Logger::debug("Handling BloomVersion packet"); - debugSession.connection.writePacket(ResponsePacket(Packet::toHex( + debugSession.connection.writePacket(ResponsePacket(String::toHex( std::string( "Bloom v" + Application::VERSION.toString() + "\n" + Paths::homeDomainName() + "\n" diff --git a/src/DebugServer/Gdb/CommandPackets/BloomVersionMachine.cpp b/src/DebugServer/Gdb/CommandPackets/BloomVersionMachine.cpp index 163d32fb..8d9c35f4 100644 --- a/src/DebugServer/Gdb/CommandPackets/BloomVersionMachine.cpp +++ b/src/DebugServer/Gdb/CommandPackets/BloomVersionMachine.cpp @@ -7,7 +7,10 @@ #include "src/DebugServer/Gdb/ResponsePackets/ResponsePacket.hpp" #include "src/Application.hpp" + +#include "src/Helpers/String.hpp" #include "src/Logger/Logger.hpp" + #include "src/Exceptions/Exception.hpp" namespace Bloom::DebugServer::Gdb::CommandPackets @@ -25,7 +28,7 @@ namespace Bloom::DebugServer::Gdb::CommandPackets void BloomVersionMachine::handle(DebugSession& debugSession, TargetControllerConsole&) { Logger::debug("Handling BloomVersionMachine packet"); - debugSession.connection.writePacket(ResponsePacket(Packet::toHex( + debugSession.connection.writePacket(ResponsePacket(String::toHex( QJsonDocument(QJsonObject({ {"version", QString::fromStdString(Application::VERSION.toString())}, {"components", QJsonObject({ diff --git a/src/DebugServer/Gdb/CommandPackets/EepromFill.cpp b/src/DebugServer/Gdb/CommandPackets/EepromFill.cpp index 45882732..4a677b74 100644 --- a/src/DebugServer/Gdb/CommandPackets/EepromFill.cpp +++ b/src/DebugServer/Gdb/CommandPackets/EepromFill.cpp @@ -6,9 +6,11 @@ #include "src/DebugServer/Gdb/ResponsePackets/ResponsePacket.hpp" #include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp" +#include "src/Helpers/String.hpp" +#include "src/Logger/Logger.hpp" + #include "src/DebugServer/Gdb/Exceptions/InvalidCommandOption.hpp" #include "src/Exceptions/Exception.hpp" -#include "src/Logger/Logger.hpp" namespace Bloom::DebugServer::Gdb::CommandPackets { @@ -85,7 +87,7 @@ namespace Bloom::DebugServer::Gdb::CommandPackets ); } - const auto hexValues = Packet::toHex(data); + const auto hexValues = String::toHex(data); Logger::debug("Filling EEPROM with values: " + hexValues); targetControllerConsole.writeMemory( @@ -94,13 +96,13 @@ namespace Bloom::DebugServer::Gdb::CommandPackets std::move(data) ); - debugSession.connection.writePacket(ResponsePacket(Packet::toHex( + debugSession.connection.writePacket(ResponsePacket(String::toHex( "Filled " + std::to_string(eepromSize) + " bytes of EEPROM, with values: " + hexValues + "\n" ))); } catch (const InvalidCommandOption& exception) { Logger::error(exception.getMessage()); - debugSession.connection.writePacket(ResponsePacket(Packet::toHex(exception.getMessage() + "\n"))); + debugSession.connection.writePacket(ResponsePacket(String::toHex(exception.getMessage() + "\n"))); } catch (const Exception& exception) { Logger::error("Failed to fill EEPROM - " + exception.getMessage()); diff --git a/src/DebugServer/Gdb/CommandPackets/GenerateSvd.cpp b/src/DebugServer/Gdb/CommandPackets/GenerateSvd.cpp index 51f3b311..fb061dc2 100644 --- a/src/DebugServer/Gdb/CommandPackets/GenerateSvd.cpp +++ b/src/DebugServer/Gdb/CommandPackets/GenerateSvd.cpp @@ -7,12 +7,14 @@ #include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp" #include "src/Targets/TargetMemory.hpp" - #include "src/Application.hpp" + #include "src/Helpers/Paths.hpp" -#include "src/Exceptions/Exception.hpp" +#include "src/Helpers/String.hpp" #include "src/Logger/Logger.hpp" +#include "src/Exceptions/Exception.hpp" + namespace Bloom::DebugServer::Gdb::CommandPackets { using TargetController::TargetControllerConsole; @@ -40,7 +42,7 @@ namespace Bloom::DebugServer::Gdb::CommandPackets ); if (this->sendOutput) { - debugSession.connection.writePacket(ResponsePacket(Packet::toHex(svdXml.toString().toStdString()))); + debugSession.connection.writePacket(ResponsePacket(String::toHex(svdXml.toString().toStdString()))); return; } @@ -56,7 +58,7 @@ namespace Bloom::DebugServer::Gdb::CommandPackets outputFile.write(svdXml.toByteArray()); outputFile.close(); - debugSession.connection.writePacket(ResponsePacket(Packet::toHex( + debugSession.connection.writePacket(ResponsePacket(String::toHex( "SVD output saved to " + svdOutputFilePath + "\n" ))); diff --git a/src/DebugServer/Gdb/CommandPackets/HelpMonitorInfo.cpp b/src/DebugServer/Gdb/CommandPackets/HelpMonitorInfo.cpp index 4217ed8f..ade98dde 100644 --- a/src/DebugServer/Gdb/CommandPackets/HelpMonitorInfo.cpp +++ b/src/DebugServer/Gdb/CommandPackets/HelpMonitorInfo.cpp @@ -7,7 +7,9 @@ #include "src/DebugServer/Gdb/ResponsePackets/ResponsePacket.hpp" #include "src/Helpers/Paths.hpp" +#include "src/Helpers/String.hpp" #include "src/Logger/Logger.hpp" + #include "src/Exceptions/Exception.hpp" namespace Bloom::DebugServer::Gdb::CommandPackets @@ -45,7 +47,7 @@ namespace Bloom::DebugServer::Gdb::CommandPackets } debugSession.connection.writePacket( - ResponsePacket(Packet::toHex("\n" + QTextStream(&helpFile).readAll().toUtf8().toStdString() + "\n")) + ResponsePacket(String::toHex("\n" + QTextStream(&helpFile).readAll().toUtf8().toStdString() + "\n")) ); } catch (const Exception& exception) { diff --git a/src/DebugServer/Gdb/CommandPackets/ReadRegisters.cpp b/src/DebugServer/Gdb/CommandPackets/ReadRegisters.cpp index 7007f09d..196f7059 100644 --- a/src/DebugServer/Gdb/CommandPackets/ReadRegisters.cpp +++ b/src/DebugServer/Gdb/CommandPackets/ReadRegisters.cpp @@ -4,7 +4,9 @@ #include "src/Targets/TargetRegister.hpp" +#include "src/Helpers/String.hpp" #include "src/Logger/Logger.hpp" + #include "src/Exceptions/Exception.hpp" namespace Bloom::DebugServer::Gdb::CommandPackets @@ -87,7 +89,7 @@ namespace Bloom::DebugServer::Gdb::CommandPackets } debugSession.connection.writePacket( - ResponsePacket(Packet::toHex(registers)) + ResponsePacket(String::toHex(registers)) ); } catch (const Exception& exception) { diff --git a/src/DebugServer/Gdb/CommandPackets/ResetTarget.cpp b/src/DebugServer/Gdb/CommandPackets/ResetTarget.cpp index eb4ce692..86715e35 100644 --- a/src/DebugServer/Gdb/CommandPackets/ResetTarget.cpp +++ b/src/DebugServer/Gdb/CommandPackets/ResetTarget.cpp @@ -3,7 +3,9 @@ #include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp" #include "src/DebugServer/Gdb/ResponsePackets/ResponsePacket.hpp" +#include "src/Helpers/String.hpp" #include "src/Logger/Logger.hpp" + #include "src/Exceptions/Exception.hpp" namespace Bloom::DebugServer::Gdb::CommandPackets @@ -27,7 +29,7 @@ namespace Bloom::DebugServer::Gdb::CommandPackets targetControllerConsole.resetTarget(); Logger::info("Target reset complete"); - debugSession.connection.writePacket(ResponsePacket(Packet::toHex( + debugSession.connection.writePacket(ResponsePacket(String::toHex( "Target reset complete - use the 'continue' command to begin execution.\n" ))); diff --git a/src/DebugServer/Gdb/Packet.hpp b/src/DebugServer/Gdb/Packet.hpp index e06bf722..e29e24d3 100644 --- a/src/DebugServer/Gdb/Packet.hpp +++ b/src/DebugServer/Gdb/Packet.hpp @@ -83,42 +83,6 @@ namespace Bloom::DebugServer::Gdb return packet; } - /** - * Converts raw data to hexadecimal form, the form in which responses are expected to be delivered from the - * server. - * - * @param data - * @return - */ - static std::string toHex(const std::vector& data) { - std::stringstream stream; - stream << std::hex << std::setfill('0'); - - for (const auto& byte : data) { - stream << std::setw(2) << static_cast(byte); - } - - return stream.str(); - } - - /** - * Converts a string to hexadecimal form, the form in which responses are expected to be delivered from the - * server. - * - * @param data - * @return - */ - static std::string toHex(const std::string& data) { - std::stringstream stream; - stream << std::hex << std::setfill('0'); - - for (const auto& byte : data) { - stream << std::setw(2) << static_cast(byte); - } - - return stream.str(); - } - /** * Converts data in hexadecimal form to raw data. * diff --git a/src/DebugServer/Gdb/ResponsePackets/TargetStopped.hpp b/src/DebugServer/Gdb/ResponsePackets/TargetStopped.hpp index ec636330..d665c82e 100644 --- a/src/DebugServer/Gdb/ResponsePackets/TargetStopped.hpp +++ b/src/DebugServer/Gdb/ResponsePackets/TargetStopped.hpp @@ -7,6 +7,8 @@ #include "src/DebugServer/Gdb/Signal.hpp" #include "src/DebugServer/Gdb/StopReason.hpp" +#include "src/Helpers/String.hpp" + namespace Bloom::DebugServer::Gdb::ResponsePackets { /** @@ -23,7 +25,7 @@ namespace Bloom::DebugServer::Gdb::ResponsePackets : signal(signal) , stopReason(stopReason) { - std::string packetData = "T" + Packet::toHex(std::vector({static_cast(this->signal)})); + std::string packetData = "T" + String::toHex(std::vector({static_cast(this->signal)})); if (this->stopReason.has_value()) { static const auto stopReasonMapping = getStopReasonToNameMapping(); diff --git a/src/Helpers/String.cpp b/src/Helpers/String.cpp index be5d7257..3b3fd496 100644 --- a/src/Helpers/String.cpp +++ b/src/Helpers/String.cpp @@ -2,6 +2,8 @@ #include #include +#include +#include namespace Bloom { @@ -26,4 +28,32 @@ namespace Bloom return character > 127; }); } + + std::string String::toHex(unsigned char value) { + auto stream = std::stringstream(); + stream << std::hex << std::setfill('0') << std::setw(2) << static_cast(value); + return stream.str(); + } + + std::string String::toHex(const std::vector& data) { + auto stream = std::stringstream(); + stream << std::hex << std::setfill('0'); + + for (const auto& byte : data) { + stream << std::setw(2) << static_cast(byte); + } + + return stream.str(); + } + + std::string String::toHex(const std::string& data) { + std::stringstream stream; + stream << std::hex << std::setfill('0'); + + for (const auto& byte : data) { + stream << std::setw(2) << static_cast(byte); + } + + return stream.str(); + } } diff --git a/src/Helpers/String.hpp b/src/Helpers/String.hpp index 03689089..44b330d6 100644 --- a/src/Helpers/String.hpp +++ b/src/Helpers/String.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include namespace Bloom { @@ -12,5 +13,9 @@ namespace Bloom static std::string asciiToUpper(std::string str); static bool isAscii(const std::string& str); + + static std::string toHex(unsigned char value); + static std::string toHex(const std::vector& data); + static std::string toHex(const std::string& data); }; }