2021-10-02 17:39:27 +01:00
|
|
|
#include "AvrResponseFrame.hpp"
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
#include "src/Exceptions/Exception.hpp"
|
|
|
|
|
|
2023-11-17 22:20:39 +00:00
|
|
|
namespace DebugToolDrivers::Microchip::Protocols::Edbg::Avr
|
2022-02-05 15:32:08 +00:00
|
|
|
{
|
2023-08-13 15:47:51 +01:00
|
|
|
using namespace Exceptions;
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00: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()
|
2022-10-01 20:42:37 +01:00
|
|
|
auto rawFrame = std::vector<unsigned char>();
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
for (const auto& avrResponse : avrResponses) {
|
2022-10-01 20:42:37 +01:00
|
|
|
rawFrame.insert(rawFrame.end(), avrResponse.responsePacket.begin(), avrResponse.responsePacket.end());
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
return this->initFromRawFrame(rawFrame);
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void AvrResponseFrame::initFromRawFrame(const std::vector<unsigned char>& rawFrame) {
|
|
|
|
|
if (rawFrame.size() < 4) {
|
2022-10-01 20:42:37 +01:00
|
|
|
/*
|
|
|
|
|
* All AVR response frames must consist of at least four bytes (SOF, sequence ID (two bytes) and
|
|
|
|
|
* a protocol handler ID).
|
|
|
|
|
*/
|
2022-02-05 15:32:08 +00:00
|
|
|
throw Exception("Failed to construct AvrResponseFrame - unexpected end to raw frame");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rawFrame[0] != 0x0E) {
|
2022-10-01 20:42:37 +01:00
|
|
|
// Invalid SOF byte value
|
|
|
|
|
throw Exception("Failed to construct AvrResponseFrame - unexpected SOF byte value in raw frame");
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-10-01 20:42:37 +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
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
this->payload.insert(payload.begin(), rawFrame.begin() + 4, rawFrame.end());
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-06-22 23:52:31 +01:00
|
|
|
}
|