Disk: Implement NeoMemoryDiskReadDriver

v0.3.0-dev
Paul Hollinsky 2021-11-16 00:35:32 -05:00
parent fe4d5e0c15
commit f8bfb243fa
10 changed files with 127 additions and 10 deletions

View File

@ -150,6 +150,7 @@ set(SRC_FILES
device/device.cpp
disk/diskreaddriver.cpp
disk/nulldiskreaddriver.cpp
disk/neomemorydiskreaddriver.cpp
${PLATFORM_SRC}
)

View File

@ -4,6 +4,7 @@
#include "icsneo/communication/message/resetstatusmessage.h"
#include "icsneo/communication/message/readsettingsmessage.h"
#include "icsneo/communication/message/canerrorcountmessage.h"
#include "icsneo/communication/message/neoreadmemorysdmessage.h"
#include "icsneo/communication/message/flexray/control/flexraycontrolmessage.h"
#include "icsneo/communication/command.h"
#include "icsneo/device/device.h"
@ -179,6 +180,18 @@ bool Decoder::decode(std::shared_ptr<Message>& result, const std::shared_ptr<Pac
result = std::make_shared<RawMessage>(packet->network, packet->data);
return true;
}
case Network::NetID::NeoMemorySDRead: {
if(packet->data.size() != 512 + sizeof(uint32_t)) {
report(APIEvent::Type::PacketDecodingError, APIEvent::Severity::Error);
return false; // Should get enough data for a start address and sector
}
const auto msg = std::make_shared<NeoReadMemorySDMessage>();
result = msg;
msg->startAddress = *reinterpret_cast<uint32_t*>(packet->data.data());
msg->data.insert(msg->data.end(), packet->data.begin() + 4, packet->data.end());
return true;
}
case Network::NetID::FlexRayControl: {
auto frResult = std::make_shared<FlexRayControlMessage>(*packet);
if(!frResult->decoded) {

View File

@ -197,6 +197,7 @@ bool Encoder::encode(const Packetizer& packetizer, std::vector<uint8_t>& result,
case Command::EnableNetworkCommunicationEx:
case Command::GetMainVersion:
case Command::GetSecondaryVersions:
case Command::NeoReadMemory:
// There is a firmware handling idiosyncrasy with these commands
// They must be encoded in the short format
m51msg->forceShortFormat = true;

View File

@ -0,0 +1,40 @@
#include "icsneo/disk/neomemorydiskreaddriver.h"
#include "icsneo/communication/message/neoreadmemorysdmessage.h"
#include <cstring>
using namespace icsneo;
optional<uint64_t> NeoMemoryDiskReadDriver::readLogicalDiskAligned(Communication& com, device_eventhandler_t report,
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout) {
static std::shared_ptr<MessageFilter> NeoMemorySDRead = std::make_shared<MessageFilter>(Network::NetID::NeoMemorySDRead);
if(amount != SectorSize)
return 0;
const uint64_t currentSector = pos / SectorSize;
auto msg = com.waitForMessageSync([&currentSector, &com] {
return com.sendCommand(Command::NeoReadMemory, {
MemoryTypeSD,
uint8_t(currentSector & 0xFF),
uint8_t((currentSector >> 8) & 0xFF),
uint8_t((currentSector >> 16) & 0xFF),
uint8_t((currentSector >> 24) & 0xFF),
uint8_t(SectorSize & 0xFF),
uint8_t((SectorSize >> 8) & 0xFF),
uint8_t((SectorSize >> 16) & 0xFF),
uint8_t((SectorSize >> 24) & 0xFF)
});
}, NeoMemorySDRead, timeout);
if(!msg)
return 0;
const auto sdmsg = std::dynamic_pointer_cast<NeoReadMemorySDMessage>(msg);
if(!sdmsg || sdmsg->data.size() != SectorSize) {
report(APIEvent::Type::PacketDecodingError, APIEvent::Severity::Error);
return nullopt;
}
memcpy(into, sdmsg->data.data(), SectorSize);
return SectorSize;
}

View File

@ -2,14 +2,14 @@
using namespace icsneo;
optional<uint64_t> NullDiskReadDriver::readLogicalDisk(Communication& com, device_eventhandler_t report,
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout) {
optional<uint64_t> NullDiskReadDriver::readLogicalDisk(Communication&, device_eventhandler_t report,
uint64_t, uint8_t*, uint64_t, std::chrono::milliseconds) {
report(APIEvent::Type::DiskNotSupported, APIEvent::Severity::Error);
return std::nullopt;
return nullopt;
}
optional<uint64_t> NullDiskReadDriver::readLogicalDiskAligned(Communication& com, device_eventhandler_t report,
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout) {
optional<uint64_t> NullDiskReadDriver::readLogicalDiskAligned(Communication&, device_eventhandler_t report,
uint64_t, uint8_t*, uint64_t, std::chrono::milliseconds) {
report(APIEvent::Type::DiskNotSupported, APIEvent::Severity::Error);
return std::nullopt;
return nullopt;
}

View File

@ -8,6 +8,7 @@ namespace icsneo {
enum class Command : uint8_t {
EnableNetworkCommunication = 0x07,
EnableNetworkCommunicationEx = 0x08,
NeoReadMemory = 0x40,
RequestSerialNumber = 0xA1,
GetMainVersion = 0xA3, // Previously known as RED_CMD_APP_VERSION_REQ
SetSettings = 0xA4, // Previously known as RED_CMD_SET_BAUD_REQ, follow up with SaveSettings to write to EEPROM

View File

@ -0,0 +1,20 @@
#ifndef __READMEMORYMESSAGE_H_
#define __READMEMORYMESSAGE_H_
#ifdef __cplusplus
#include "icsneo/communication/message/message.h"
namespace icsneo {
class NeoReadMemorySDMessage : public RawMessage {
public:
NeoReadMemorySDMessage() : RawMessage(Message::Type::RawMessage, Network::NetID::NeoMemorySDRead) {}
uint32_t startAddress = 0;
};
}
#endif // __cplusplus
#endif

View File

@ -41,7 +41,7 @@ public:
RED_EXT_MEMORYREAD = 20,
RED_INT_MEMORYREAD = 21,
RED_DFLASH_READ = 22,
RED_SDCARD_READ = 23,
NeoMemorySDRead = 23, // Response from NeoMemory (MemoryTypeSD)
CAN_ERRBITS = 24,
RED_DFLASH_WRITE_DONE = 25,
RED_WAVE_CAN1_LOGICAL = 26,
@ -272,6 +272,7 @@ public:
case NetID::Main51:
case NetID::ReadSettings:
case NetID::EthPHYControl:
case NetID::NeoMemorySDRead:
return Type::Internal;
case NetID::Invalid:
case NetID::Any:
@ -360,8 +361,8 @@ public:
return "RED_INT_MEMORYREAD";
case NetID::RED_DFLASH_READ:
return "RED_DFLASH_READ";
case NetID::RED_SDCARD_READ:
return "RED_SDCARD_READ";
case NetID::NeoMemorySDRead:
return "NeoMemorySDRead";
case NetID::CAN_ERRBITS:
return "CAN_ERRBITS";
case NetID::RED_DFLASH_WRITE_DONE:

View File

@ -0,0 +1,35 @@
#ifndef __NEOMEMORYDISKREADDRIVER_H__
#define __NEOMEMORYDISKREADDRIVER_H__
#ifdef __cplusplus
#include "icsneo/disk/diskreaddriver.h"
#include <limits>
namespace icsneo {
/**
* A disk read driver which uses the neoMemory command to read from the disk
*
* This can only request reads by sector, so it will be very slow, but is likely supported by any device with a disk
*/
class NeoMemoryDiskReadDriver : public DiskReadDriver {
public:
Access getAccess() const override { return Access::VSA; }
std::pair<uint32_t, uint32_t> getBlockSizeBounds() const override {
static_assert(SectorSize <= std::numeric_limits<uint32_t>::max(), "Incorrect sector size");
static_assert(SectorSize >= std::numeric_limits<uint32_t>::min(), "Incorrect sector size");
return { static_cast<uint32_t>(SectorSize), static_cast<uint32_t>(SectorSize) };
}
private:
static constexpr const uint8_t MemoryTypeSD = 0x01; // Logical Disk
optional<uint64_t> readLogicalDiskAligned(Communication& com, device_eventhandler_t report,
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout) override;
};
}
#endif // __cplusplus
#endif // __NEOMEMORYDISKREADDRIVER_H__

View File

@ -4,6 +4,7 @@
#ifdef __cplusplus
#include "icsneo/disk/diskreaddriver.h"
#include <limits>
namespace icsneo {
@ -17,7 +18,11 @@ public:
optional<uint64_t> readLogicalDisk(Communication& com, device_eventhandler_t report,
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout = DefaultTimeout) override;
Access getAccess() const override { return Access::None; }
std::pair<uint32_t, uint32_t> getBlockSizeBounds() const override { return {SectorSize, SectorSize}; }
std::pair<uint32_t, uint32_t> getBlockSizeBounds() const override {
static_assert(SectorSize <= std::numeric_limits<uint32_t>::max(), "Incorrect sector size");
static_assert(SectorSize >= std::numeric_limits<uint32_t>::min(), "Incorrect sector size");
return { static_cast<uint32_t>(SectorSize), static_cast<uint32_t>(SectorSize) };
}
private:
optional<uint64_t> readLogicalDiskAligned(Communication& com, device_eventhandler_t report,