Moved toHex functions to String helper class

This commit is contained in:
Nav
2023-01-21 13:37:56 +00:00
parent 662806769e
commit 6b4d3ecb26
12 changed files with 70 additions and 51 deletions

View File

@@ -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());

View File

@@ -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"

View File

@@ -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({

View File

@@ -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());

View File

@@ -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"
)));

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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"
)));

View File

@@ -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<unsigned char>& data) {
std::stringstream stream;
stream << std::hex << std::setfill('0');
for (const auto& byte : data) {
stream << std::setw(2) << static_cast<unsigned int>(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<unsigned int>(byte);
}
return stream.str();
}
/**
* Converts data in hexadecimal form to raw data.
*

View File

@@ -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<unsigned char>(this->signal)}));
std::string packetData = "T" + String::toHex(std::vector({static_cast<unsigned char>(this->signal)}));
if (this->stopReason.has_value()) {
static const auto stopReasonMapping = getStopReasonToNameMapping();

View File

@@ -2,6 +2,8 @@
#include <algorithm>
#include <cctype>
#include <sstream>
#include <iomanip>
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<unsigned int>(value);
return stream.str();
}
std::string String::toHex(const std::vector<unsigned char>& data) {
auto stream = std::stringstream();
stream << std::hex << std::setfill('0');
for (const auto& byte : data) {
stream << std::setw(2) << static_cast<unsigned int>(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<unsigned int>(byte);
}
return stream.str();
}
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <vector>
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<unsigned char>& data);
static std::string toHex(const std::string& data);
};
}