Files
BloomPatched/src/DebugToolDrivers/Microchip/Protocols/EDBG/AVR/ResponseFrames/AvrResponseFrame.cpp

40 lines
1.5 KiB
C++
Raw Normal View History

2021-10-02 17:39:27 +01:00
#include "AvrResponseFrame.hpp"
2021-04-04 21:04:12 +01:00
#include "src/Exceptions/Exception.hpp"
namespace DebugToolDrivers::Microchip::Protocols::Edbg::Avr
{
using namespace Exceptions;
2021-04-04 21:04:12 +01:00
void AvrResponseFrame::initFromAvrResponses(const std::vector<AvrResponse>& avrResponses) {
2022-03-01 19:58:04 +00:00
// Build a raw frame buffer from the AvrResponse objects and just call initFromRawFrame()
auto rawFrame = std::vector<unsigned char>();
2021-04-04 21:04:12 +01:00
for (const auto& avrResponse : avrResponses) {
rawFrame.insert(rawFrame.end(), avrResponse.responsePacket.begin(), avrResponse.responsePacket.end());
}
2021-04-04 21:04:12 +01:00
return this->initFromRawFrame(rawFrame);
2021-04-04 21:04:12 +01:00
}
void AvrResponseFrame::initFromRawFrame(const std::vector<unsigned char>& rawFrame) {
if (rawFrame.size() < 4) {
/*
* All AVR response frames must consist of at least four bytes (SOF, sequence ID (two bytes) and
* a protocol handler ID).
*/
throw Exception("Failed to construct AvrResponseFrame - unexpected end to raw frame");
}
if (rawFrame[0] != 0x0E) {
// Invalid SOF byte value
throw Exception("Failed to construct AvrResponseFrame - unexpected SOF byte value in raw frame");
}
2021-04-04 21:04:12 +01:00
this->sequenceId = static_cast<std::uint16_t>((rawFrame[2] << 8) + rawFrame[1]);
this->protocolHandlerId = static_cast<ProtocolHandlerId>(rawFrame[3]);
2021-04-04 21:04:12 +01:00
this->payload.insert(payload.begin(), rawFrame.begin() + 4, rawFrame.end());
}
}