Files
BloomPatched/src/DebugServer/Gdb/AvrGdb/CommandPackets/EepromFill.cpp

108 lines
4.2 KiB
C++
Raw Normal View History

2022-12-10 19:22:53 +00:00
#include "EepromFill.hpp"
#include <QByteArray>
#include <algorithm>
#include "src/DebugServer/Gdb/ResponsePackets/ResponsePacket.hpp"
#include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp"
2023-01-21 14:27:45 +00:00
#include "src/Services/StringService.hpp"
#include "src/Logger/Logger.hpp"
2022-12-10 19:22:53 +00:00
#include "src/DebugServer/Gdb/Exceptions/InvalidCommandOption.hpp"
#include "src/Exceptions/Exception.hpp"
namespace DebugServer::Gdb::AvrGdb::CommandPackets
2022-12-10 19:22:53 +00:00
{
using Services::TargetControllerService;
2022-12-10 19:22:53 +00:00
using ResponsePackets::ResponsePacket;
using ResponsePackets::ErrorResponsePacket;
using ::Exceptions::Exception;
2022-12-10 19:22:53 +00:00
using Exceptions::InvalidCommandOption;
EepromFill::EepromFill(Monitor&& monitorPacket, const TargetDescriptor& gdbTargetDescriptor)
2022-12-10 19:22:53 +00:00
: Monitor(std::move(monitorPacket))
, eepromAddressSpaceDescriptor(gdbTargetDescriptor.eepromAddressSpaceDescriptor)
, eepromMemorySegmentDescriptor(gdbTargetDescriptor.eepromMemorySegmentDescriptor)
2022-12-10 19:22:53 +00:00
{
const auto fillValueOptionIt = this->commandOptions.find("value");
if (fillValueOptionIt != this->commandOptions.end() && fillValueOptionIt->second.has_value()) {
this->fillValue = Services::StringService::dataFromHex(*(fillValueOptionIt->second));
2022-12-10 19:22:53 +00:00
}
}
void EepromFill::handle(
Gdb::DebugSession& debugSession,
const Gdb::TargetDescriptor& gdbTargetDescriptor,
const Targets::TargetDescriptor& targetDescriptor,
TargetControllerService& targetControllerService
) {
Logger::info("Handling EepromFill packet");
2022-12-10 19:22:53 +00:00
try {
const auto eepromSize = this->eepromMemorySegmentDescriptor.size();
2022-12-10 19:22:53 +00:00
const auto fillValueSize = this->fillValue.size();
if (fillValueSize == 0) {
throw InvalidCommandOption{"Fill value required"};
2022-12-10 19:22:53 +00:00
}
if (fillValueSize > eepromSize) {
throw InvalidCommandOption{
"Fill value size (" + std::to_string(fillValueSize) + " bytes) exceeds EEPROM size ("
2022-12-10 19:22:53 +00:00
+ std::to_string(eepromSize) + " bytes)"
};
2022-12-10 19:22:53 +00:00
}
if ((eepromSize % fillValueSize) != 0) {
Logger::warning(
"The fill value size (" + std::to_string(fillValueSize) + " bytes) is not a multiple of the EEPROM "
"size (" + std::to_string(eepromSize) + " bytes) - the fill value will be truncated"
2022-12-10 19:22:53 +00:00
);
}
Logger::warning("Filling " + std::to_string(eepromSize) + " bytes of EEPROM");
auto data = Targets::TargetMemoryBuffer{};
2022-12-10 19:22:53 +00:00
data.reserve(eepromSize);
// Repeat this->fillValue until we've filled `data`
while (data.size() < eepromSize) {
data.insert(
data.end(),
this->fillValue.begin(),
this->fillValue.begin() + static_cast<std::int32_t>(
std::min(fillValueSize, (eepromSize - data.size()))
)
);
}
2023-01-21 14:27:45 +00:00
const auto hexValues = Services::StringService::toHex(data);
2022-12-10 19:22:53 +00:00
Logger::debug("Filling EEPROM with values: " + hexValues);
targetControllerService.writeMemory(
this->eepromAddressSpaceDescriptor,
this->eepromMemorySegmentDescriptor,
this->eepromMemorySegmentDescriptor.addressRange.startAddress,
2022-12-10 19:22:53 +00:00
std::move(data)
);
debugSession.connection.writePacket(ResponsePacket{Services::StringService::toHex(
2022-12-10 19:22:53 +00:00
"Filled " + std::to_string(eepromSize) + " bytes of EEPROM, with values: " + hexValues + "\n"
)});
2022-12-10 19:22:53 +00:00
} catch (const InvalidCommandOption& exception) {
Logger::error(exception.getMessage());
2023-01-21 14:27:45 +00:00
debugSession.connection.writePacket(
ResponsePacket{Services::StringService::toHex(exception.getMessage() + "\n")}
2023-01-21 14:27:45 +00:00
);
2022-12-10 19:22:53 +00:00
} catch (const Exception& exception) {
Logger::error("Failed to fill EEPROM - " + exception.getMessage());
debugSession.connection.writePacket(ErrorResponsePacket{});
2022-12-10 19:22:53 +00:00
}
}
}