Device: Add disk read driver framework

Allow access to the device's logical disk.

At the moment, no drivers are implemented, so all devices have Access::None.
This commit is contained in:
Paul Hollinsky
2022-02-25 01:14:57 -05:00
parent 9e6970fd39
commit fe4d5e0c15
9 changed files with 200 additions and 1 deletions
+3
View File
@@ -47,6 +47,7 @@ public:
UnsupportedTXNetwork = 0x1011,
MessageMaxLengthExceeded = 0x1012,
ValueNotYetPresent = 0x1013,
Timeout = 0x1014,
// Device Events
PollingMessageOverflow = 0x2000,
@@ -80,6 +81,8 @@ public:
NoSerialNumber12V = 0x2028, // The device must be powered with 12V for communication to be established
NoSerialNumberFW12V = 0x2029, // The device must be powered with 12V for communication to be established, a firmware update was already attempted
EthPhyRegisterControlNotAvailable = 0x2030, //The device doesn't support Ethernet PHY MDIO access
DiskNotSupported = 0x2031,
EOFReached = 0x2032,
SettingsDefaultsUsed = 0x2033,
// Transport Events
+22 -1
View File
@@ -16,6 +16,8 @@
#include "icsneo/device/nullsettings.h"
#include "icsneo/device/devicetype.h"
#include "icsneo/device/deviceversion.h"
#include "icsneo/disk/diskreaddriver.h"
#include "icsneo/disk/nulldiskreaddriver.h"
#include "icsneo/communication/communication.h"
#include "icsneo/communication/packetizer.h"
#include "icsneo/communication/encoder.h"
@@ -139,6 +141,23 @@ public:
virtual size_t getNetworkCountByType(Network::Type) const;
virtual Network getNetworkByNumber(Network::Type, size_t) const;
/**
* Read from the logical disk in this device, starting from byte `pos`
* and reading up to `amount` bytes.
*
* The number of bytes read will be returned in case of success.
*
* If the number of bytes read is less than the amount requested,
* an error will be set in icsneo::GetLastError() explaining why.
* Likely, either the end of the logical disk has been reached, or
* the timeout was reached while the read had only partially completed.
*
* Upon failure, icsneo::nullopt will be returned and an error will be
* set in icsneo::GetLastError().
*/
optional<uint64_t> readLogicalDisk(uint64_t pos, uint8_t* into, uint64_t amount,
std::chrono::milliseconds timeout = DiskReadDriver::DefaultTimeout);
/**
* Retrieve the number of Ethernet (DoIP) Activation lines present
* on this device.
@@ -257,7 +276,7 @@ protected:
data.device = this;
}
template<typename Driver, typename Settings = NullSettings>
template<typename Driver, typename Settings = NullSettings, typename DiskRead = NullDiskReadDriver>
void initialize() {
report = makeEventHandler();
auto driver = makeDriver<Driver>();
@@ -270,6 +289,7 @@ protected:
setupCommunication(*com);
settings = makeSettings<Settings>(com);
setupSettings(*settings);
diskReadDriver = std::make_unique<DiskRead>();
setupSupportedRXNetworks(supportedRXNetworks);
setupSupportedTXNetworks(supportedTXNetworks);
setupExtensions();
@@ -345,6 +365,7 @@ private:
neodevice_t data;
std::shared_ptr<ResetStatusMessage> latestResetStatus;
std::vector<optional<DeviceAppVersion>> versions;
std::unique_ptr<DiskReadDriver> diskReadDriver;
mutable std::mutex extensionsLock;
std::vector<std::shared_ptr<DeviceExtension>> extensions;
+47
View File
@@ -0,0 +1,47 @@
#ifndef __DISKREADDRIVER_H__
#define __DISKREADDRIVER_H__
#ifdef __cplusplus
#include "icsneo/platform/optional.h"
#include "icsneo/communication/communication.h"
#include "icsneo/api/eventmanager.h"
#include <cstdint>
#include <chrono>
namespace icsneo {
/**
* Interface for drivers which read block data from devices
*/
class DiskReadDriver {
public:
static constexpr const std::chrono::milliseconds DefaultTimeout{2000};
static constexpr const size_t SectorSize = 512;
enum class Access {
None,
EntireCard,
VSA
};
virtual ~DiskReadDriver() = default;
virtual optional<uint64_t> readLogicalDisk(Communication& com, device_eventhandler_t report,
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout = DefaultTimeout);
virtual Access getAccess() const = 0;
virtual std::pair<uint32_t, uint32_t> getBlockSizeBounds() const = 0;
protected:
/**
* Perform a read which the driver can do in one shot.
*
* The `pos` requested must be sector-aligned, and the `amount` must be
* within the block size bounds provided by the driver.
*/
virtual optional<uint64_t> readLogicalDiskAligned(Communication& com, device_eventhandler_t report,
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout) = 0;
};
}
#endif // __cplusplus
#endif // __DISKREADDRIVER_H__
+30
View File
@@ -0,0 +1,30 @@
#ifndef __NULLDISKREADDRIVER_H__
#define __NULLDISKREADDRIVER_H__
#ifdef __cplusplus
#include "icsneo/disk/diskreaddriver.h"
namespace icsneo {
/**
* A disk driver which always returns the requested disk as unsupported
*
* Used for devices which do not have a disk, or do not provide any means for accessing it
*/
class NullDiskReadDriver : public DiskReadDriver {
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}; }
private:
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 // __NULLDISKREADDRIVER_H__