2021-10-02 17:39:27 +01:00
|
|
|
#include "AvrResponse.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-10-01 20:42:37 +01:00
|
|
|
AvrResponse::AvrResponse(const std::vector<unsigned char>& rawResponse)
|
|
|
|
|
: Response(rawResponse)
|
|
|
|
|
{
|
|
|
|
|
if (this->id != 0x81) {
|
2024-07-23 21:14:22 +01:00
|
|
|
throw Exception{"Failed to construct AvrResponse object - invalid response ID."};
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
if (this->data.empty()) {
|
2022-02-05 15:32:08 +00:00
|
|
|
// All AVR responses should contain at least one byte (the fragment info byte)
|
2024-07-23 21:14:22 +01:00
|
|
|
throw Exception{"Failed to construct AvrResponse object - malformed AVR_RSP data"};
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
if (this->data[0] == 0x00) {
|
2022-02-05 15:32:08 +00:00
|
|
|
// This AVR Response contains no data (the device had no data to send), so we can stop here.
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
this->fragmentCount = static_cast<std::uint8_t>(this->data[0] & 0x0FU);
|
|
|
|
|
this->fragmentNumber = static_cast<std::uint8_t>(this->data[0] >> 4);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
// Response size is two bytes, MSB
|
2022-10-01 20:42:37 +01:00
|
|
|
const auto responsePacketSize = static_cast<std::uint16_t>((this->data[1] << 8U) + this->data[2]);
|
|
|
|
|
|
|
|
|
|
if (responsePacketSize > 0) {
|
|
|
|
|
// Packet data
|
|
|
|
|
this->responsePacket.insert(
|
|
|
|
|
this->responsePacket.begin(),
|
|
|
|
|
this->data.begin() + 3,
|
|
|
|
|
this->data.begin() + 3 + responsePacketSize
|
|
|
|
|
);
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|