2021-04-04 21:04:12 +01:00
|
|
|
#include "AvrEvent.hpp"
|
2021-10-02 17:39:27 +01:00
|
|
|
|
2023-08-19 17:12:40 +01:00
|
|
|
#include <cstdint>
|
|
|
|
|
|
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;
|
2022-02-05 15:32:08 +00:00
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
AvrEvent::AvrEvent(const std::vector<unsigned char>& rawResponse)
|
|
|
|
|
: Response(rawResponse)
|
|
|
|
|
{
|
|
|
|
|
if (this->id != 0x82) {
|
2024-07-23 21:14:22 +01:00
|
|
|
throw Exception{"Failed to construct AvrEvent object - invalid response ID."};
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
if (this->data.size() < 7) {
|
2024-07-23 21:14:22 +01:00
|
|
|
throw Exception{"Failed to construct AvrEvent object - unexpected size of AVR_EVT response."};
|
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::size_t>((this->data[0] << 8) | this->data[1]);
|
|
|
|
|
|
|
|
|
|
if (responsePacketSize == 0) {
|
|
|
|
|
// No event available
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-02-05 15:32:08 +00:00
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
if (this->data.size() < responsePacketSize + 7) {
|
2024-07-23 21:14:22 +01:00
|
|
|
throw Exception{"Failed to construct AvrEvent object - invalid size of AVR_EVT response packet."};
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2022-10-01 20:42:37 +01:00
|
|
|
* Ignore the SOF, protocol version, handler ID, sequence ID and size bytes (which all make up 7 bytes
|
|
|
|
|
* in total).
|
2022-02-05 15:32:08 +00:00
|
|
|
*/
|
2024-07-23 21:14:22 +01:00
|
|
|
this->eventData = std::vector<unsigned char>{
|
2022-10-01 20:42:37 +01:00
|
|
|
this->data.begin() + 7,
|
|
|
|
|
this->data.begin() + 7 + static_cast<std::int64_t>(responsePacketSize)
|
2024-07-23 21:14:22 +01:00
|
|
|
};
|
2022-02-05 15:32:08 +00:00
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
this->eventId = static_cast<AvrEventId>(this->eventData[0]);
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
}
|