Device: Implement version handling

This commit is contained in:
Paul Hollinsky
2021-05-05 02:17:38 -04:00
parent 8be3bbaee5
commit 595cc36545
12 changed files with 183 additions and 1 deletions
+32 -1
View File
@@ -12,6 +12,7 @@
#include "icsneo/communication/message/serialnumbermessage.h"
#include "icsneo/communication/message/filter/main51messagefilter.h"
#include "icsneo/communication/message/readsettingsmessage.h"
#include "icsneo/communication/message/versionmessage.h"
using namespace icsneo;
@@ -118,7 +119,6 @@ bool Communication::getSettingsSync(std::vector<uint8_t>& data, std::chrono::mil
}
std::shared_ptr<SerialNumberMessage> Communication::getSerialNumberSync(std::chrono::milliseconds timeout) {
sendCommand(Command::RequestSerialNumber);
std::shared_ptr<Message> msg = waitForMessageSync([this]() {
return sendCommand(Command::RequestSerialNumber);
}, Main51MessageFilter(Command::RequestSerialNumber), timeout);
@@ -132,6 +132,37 @@ std::shared_ptr<SerialNumberMessage> Communication::getSerialNumberSync(std::chr
return std::dynamic_pointer_cast<SerialNumberMessage>(m51);
}
optional< std::vector< optional<DeviceAppVersion> > > Communication::getVersionsSync(std::chrono::milliseconds timeout) {
std::vector< optional<DeviceAppVersion> > ret;
std::shared_ptr<Message> msg = waitForMessageSync([this]() {
return sendCommand(Command::GetMainVersion);
}, Main51MessageFilter(Command::GetMainVersion), timeout);
if(!msg) // Did not receive a message
return nullopt;
auto ver = std::dynamic_pointer_cast<VersionMessage>(msg);
if(!ver) // Could not upcast for some reason
return nullopt;
if(!ver->MainChip || ver->Versions.size() != 1)
return nullopt;
ret.push_back(ver->Versions.front());
msg = waitForMessageSync([this]() {
return sendCommand(Command::GetSecondaryVersions);
}, Main51MessageFilter(Command::GetSecondaryVersions), timeout);
if(msg) { // This one is allowed to fail
ver = std::dynamic_pointer_cast<VersionMessage>(msg);
if(ver && !ver->MainChip) {
ret.insert(ret.end(), ver->Versions.begin(), ver->Versions.end());
}
}
return ret;
}
int Communication::addMessageCallback(const MessageCallback& cb) {
std::lock_guard<std::mutex> lk(messageCallbacksLock);
messageCallbacks.insert(std::make_pair(messageCallbackIDCounter, cb));
+19
View File
@@ -10,6 +10,7 @@
#include "icsneo/communication/packet/ethernetpacket.h"
#include "icsneo/communication/packet/flexraypacket.h"
#include "icsneo/communication/packet/iso9141packet.h"
#include "icsneo/communication/packet/versionpacket.h"
#include <iostream>
using namespace icsneo;
@@ -179,6 +180,24 @@ bool Decoder::decode(std::shared_ptr<Message>& result, const std::shared_ptr<Pac
result = msg;
return true;
}
case Command::GetMainVersion: {
result = HardwareVersionPacket::DecodeMainToMessage(packet->data);
if(!result) {
report(APIEvent::Type::PacketDecodingError, APIEvent::Severity::Error);
return false;
}
return true;
}
case Command::GetSecondaryVersions: {
result = HardwareVersionPacket::DecodeSecondaryToMessage(packet->data);
if(!result) {
report(APIEvent::Type::PacketDecodingError, APIEvent::Severity::Error);
return false;
}
return true;
}
default:
auto msg = std::make_shared<Main51Message>();
msg->network = packet->network;
+2
View File
@@ -153,6 +153,8 @@ bool Encoder::encode(const Packetizer& packetizer, std::vector<uint8_t>& result,
case Command::RequestSerialNumber:
case Command::EnableNetworkCommunication:
case Command::EnableNetworkCommunicationEx:
case Command::GetMainVersion:
case Command::GetSecondaryVersions:
// There is a firmware handling idiosyncrasy with these commands
// They must be encoded in the short format
m51msg->forceShortFormat = true;
+37
View File
@@ -0,0 +1,37 @@
#include "icsneo/communication/packet/versionpacket.h"
using namespace icsneo;
std::shared_ptr<VersionMessage> HardwareVersionPacket::DecodeMainToMessage(const std::vector<uint8_t>& bytestream) {
if(bytestream.size() < 3) // Not enough bytes to decode
return std::shared_ptr<VersionMessage>();
auto msg = std::make_shared<VersionMessage>(true);
optional<DeviceAppVersion>& version = msg->Versions.emplace_back();
version.emplace();
version->major = bytestream[1];
version->minor = bytestream[2];
return msg;
}
std::shared_ptr<VersionMessage> HardwareVersionPacket::DecodeSecondaryToMessage(const std::vector<uint8_t>& bytestream) {
auto msg = std::make_shared<VersionMessage>(false);
size_t bytesLeft = bytestream.size();
if(bytesLeft)
bytesLeft--; // Disregard command byte
while(bytesLeft >= 3) {
const bool versionValid = bytestream[bytestream.size() - bytesLeft + 0];
optional<DeviceAppVersion>& version = msg->Versions.emplace_back();
if(versionValid) {
version.emplace();
version->major = bytestream[bytestream.size() - bytesLeft + 1];
version->minor = bytestream[bytestream.size() - bytesLeft + 2];
}
bytesLeft -= std::min<size_t>(3, bytesLeft);
}
return msg;
}