mirror of
https://github.com/intrepidcs/libicsneo.git
synced 2026-08-05 01:18:36 +02:00
Device: Add disk write driver framework
At the moment, no drivers are implemented, so all devices have Access::None.
This commit is contained in:
@@ -84,6 +84,8 @@ public:
|
||||
DiskNotSupported = 0x2031,
|
||||
EOFReached = 0x2032,
|
||||
SettingsDefaultsUsed = 0x2033,
|
||||
AtomicOperationRetried = 0x2034,
|
||||
AtomicOperationCompletedNonatomically = 0x2035,
|
||||
|
||||
// Transport Events
|
||||
FailedToRead = 0x3000,
|
||||
|
||||
@@ -17,7 +17,8 @@
|
||||
#include "icsneo/device/devicetype.h"
|
||||
#include "icsneo/device/deviceversion.h"
|
||||
#include "icsneo/disk/diskreaddriver.h"
|
||||
#include "icsneo/disk/nulldiskreaddriver.h"
|
||||
#include "icsneo/disk/diskwritedriver.h"
|
||||
#include "icsneo/disk/nulldiskdriver.h"
|
||||
#include "icsneo/communication/communication.h"
|
||||
#include "icsneo/communication/packetizer.h"
|
||||
#include "icsneo/communication/encoder.h"
|
||||
@@ -156,7 +157,24 @@ public:
|
||||
* set in icsneo::GetLastError().
|
||||
*/
|
||||
optional<uint64_t> readLogicalDisk(uint64_t pos, uint8_t* into, uint64_t amount,
|
||||
std::chrono::milliseconds timeout = DiskReadDriver::DefaultTimeout);
|
||||
std::chrono::milliseconds timeout = Disk::DefaultTimeout);
|
||||
|
||||
/**
|
||||
* Write to the logical disk in this device, starting from byte `pos`
|
||||
* and writing up to `amount` bytes.
|
||||
*
|
||||
* The number of bytes written will be returned in case of success.
|
||||
*
|
||||
* If the number of bytes written 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 write had only partially completed.
|
||||
*
|
||||
* Upon failure, icsneo::nullopt will be returned and an error will be
|
||||
* set in icsneo::GetLastError().
|
||||
*/
|
||||
optional<uint64_t> writeLogicalDisk(uint64_t pos, const uint8_t* from, uint64_t amount,
|
||||
std::chrono::milliseconds timeout = Disk::DefaultTimeout);
|
||||
|
||||
/**
|
||||
* Retrieve the number of Ethernet (DoIP) Activation lines present
|
||||
@@ -276,7 +294,7 @@ protected:
|
||||
data.device = this;
|
||||
}
|
||||
|
||||
template<typename Driver, typename Settings = NullSettings, typename DiskRead = NullDiskReadDriver>
|
||||
template<typename Driver, typename Settings = NullSettings, typename DiskRead = Disk::NullDriver, typename DiskWrite = Disk::NullDriver>
|
||||
void initialize() {
|
||||
report = makeEventHandler();
|
||||
auto driver = makeDriver<Driver>();
|
||||
@@ -289,7 +307,8 @@ protected:
|
||||
setupCommunication(*com);
|
||||
settings = makeSettings<Settings>(com);
|
||||
setupSettings(*settings);
|
||||
diskReadDriver = std::make_unique<DiskRead>();
|
||||
diskReadDriver = std::unique_ptr<DiskRead>(new DiskRead());
|
||||
diskWriteDriver = std::unique_ptr<DiskWrite>(new DiskWrite());
|
||||
setupSupportedRXNetworks(supportedRXNetworks);
|
||||
setupSupportedTXNetworks(supportedTXNetworks);
|
||||
setupExtensions();
|
||||
@@ -365,7 +384,8 @@ private:
|
||||
neodevice_t data;
|
||||
std::shared_ptr<ResetStatusMessage> latestResetStatus;
|
||||
std::vector<optional<DeviceAppVersion>> versions;
|
||||
std::unique_ptr<DiskReadDriver> diskReadDriver;
|
||||
std::unique_ptr<Disk::ReadDriver> diskReadDriver;
|
||||
std::unique_ptr<Disk::WriteDriver> diskWriteDriver;
|
||||
|
||||
mutable std::mutex extensionsLock;
|
||||
std::vector<std::shared_ptr<DeviceExtension>> extensions;
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#ifndef __DISKDRIVER_H__
|
||||
#define __DISKDRIVER_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
#include <cstdint>
|
||||
#include <chrono>
|
||||
#include <utility>
|
||||
|
||||
namespace icsneo {
|
||||
|
||||
namespace Disk {
|
||||
|
||||
constexpr const std::chrono::milliseconds DefaultTimeout{2000};
|
||||
constexpr const size_t SectorSize = 512;
|
||||
enum class Access {
|
||||
None,
|
||||
EntireCard,
|
||||
VSA
|
||||
};
|
||||
|
||||
/**
|
||||
* Interface for drivers which work with block data on devices
|
||||
*/
|
||||
class Driver {
|
||||
public:
|
||||
virtual ~Driver() = default;
|
||||
virtual Access getAccess() const = 0;
|
||||
virtual std::pair<uint32_t, uint32_t> getBlockSizeBounds() const = 0;
|
||||
};
|
||||
|
||||
} // namespace Disk
|
||||
|
||||
} // namespace icsneo
|
||||
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // __DISKDRIVER_H__
|
||||
@@ -6,29 +6,21 @@
|
||||
#include "icsneo/platform/optional.h"
|
||||
#include "icsneo/communication/communication.h"
|
||||
#include "icsneo/api/eventmanager.h"
|
||||
#include "icsneo/disk/diskdriver.h"
|
||||
#include <cstdint>
|
||||
#include <chrono>
|
||||
|
||||
namespace icsneo {
|
||||
|
||||
namespace Disk {
|
||||
|
||||
/**
|
||||
* Interface for drivers which read block data from devices
|
||||
*/
|
||||
class DiskReadDriver {
|
||||
class ReadDriver : public virtual Driver {
|
||||
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:
|
||||
/**
|
||||
@@ -41,7 +33,9 @@ protected:
|
||||
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace Disk
|
||||
|
||||
} // namespace icsneo
|
||||
|
||||
#endif // __cplusplus
|
||||
#endif // __DISKREADDRIVER_H__
|
||||
@@ -0,0 +1,65 @@
|
||||
#ifndef __DISKWRITEDRIVER_H__
|
||||
#define __DISKWRITEDRIVER_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
#include "icsneo/platform/optional.h"
|
||||
#include "icsneo/communication/communication.h"
|
||||
#include "icsneo/api/eventmanager.h"
|
||||
#include "icsneo/disk/diskreaddriver.h"
|
||||
#include <cstdint>
|
||||
#include <chrono>
|
||||
|
||||
namespace icsneo {
|
||||
|
||||
namespace Disk {
|
||||
|
||||
/**
|
||||
* Interface for drivers which write block data from devices
|
||||
*/
|
||||
class WriteDriver : public virtual Driver {
|
||||
public:
|
||||
virtual optional<uint64_t> writeLogicalDisk(Communication& com, device_eventhandler_t report, ReadDriver& readDriver,
|
||||
uint64_t pos, const uint8_t* from, uint64_t amount, std::chrono::milliseconds timeout = DefaultTimeout);
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Flag returned from writeLogicalDiskAligned when the
|
||||
* operation failed to be performed atomically and can
|
||||
* be retried after rereading.
|
||||
*/
|
||||
static const uint64_t RetryAtomic;
|
||||
|
||||
/**
|
||||
* The severity to report with when an atomic operation
|
||||
* is requested that the driver is unable to attempt.
|
||||
*/
|
||||
static const APIEvent::Severity NonatomicSeverity;
|
||||
|
||||
/**
|
||||
* Perform a write 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.
|
||||
*
|
||||
* If `atomicBuf` is provided, it will be used to ensure that the disk
|
||||
* data changes from `atomicBuf` to `from` without trampling any reads
|
||||
* that may have happened while modifying the data.
|
||||
*
|
||||
* The flag `RetryAtomic` is returned if the operation was attempted
|
||||
* atomically but failed.
|
||||
*
|
||||
* If the driver does not support atomic operations, but `atomicBuf`
|
||||
* is non-null, an APIEvent::AtomicOperationCompletedNonatomically
|
||||
* should be reported with `NonatomicSeverity`.
|
||||
*/
|
||||
virtual optional<uint64_t> writeLogicalDiskAligned(Communication& com, device_eventhandler_t report,
|
||||
uint64_t pos, const uint8_t* atomicBuf, const uint8_t* from, uint64_t amount, std::chrono::milliseconds timeout) = 0;
|
||||
};
|
||||
|
||||
} // namespace Disk
|
||||
|
||||
} // namespace icsneo
|
||||
|
||||
#endif // __cplusplus
|
||||
#endif // __DISKWRITEDRIVER_H__
|
||||
@@ -8,12 +8,14 @@
|
||||
|
||||
namespace icsneo {
|
||||
|
||||
namespace Disk {
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
class NeoMemoryDiskReadDriver : public ReadDriver {
|
||||
public:
|
||||
Access getAccess() const override { return Access::VSA; }
|
||||
std::pair<uint32_t, uint32_t> getBlockSizeBounds() const override {
|
||||
@@ -29,7 +31,9 @@ private:
|
||||
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout) override;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace Disk
|
||||
|
||||
} // namespace icsneo
|
||||
|
||||
#endif // __cplusplus
|
||||
#endif // __NEOMEMORYDISKREADDRIVER_H__
|
||||
@@ -4,19 +4,24 @@
|
||||
#ifdef __cplusplus
|
||||
|
||||
#include "icsneo/disk/diskreaddriver.h"
|
||||
#include "icsneo/disk/diskwritedriver.h"
|
||||
#include <limits>
|
||||
|
||||
namespace icsneo {
|
||||
|
||||
namespace Disk {
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
class NullDriver : public ReadDriver, public WriteDriver {
|
||||
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;
|
||||
optional<uint64_t> writeLogicalDisk(Communication& com, device_eventhandler_t report, ReadDriver& readDriver,
|
||||
uint64_t pos, const uint8_t* from, 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 {
|
||||
static_assert(SectorSize <= std::numeric_limits<uint32_t>::max(), "Incorrect sector size");
|
||||
@@ -27,9 +32,13 @@ public:
|
||||
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;
|
||||
optional<uint64_t> writeLogicalDiskAligned(Communication& com, device_eventhandler_t report,
|
||||
uint64_t pos, const uint8_t* atomicBuf, const uint8_t* from, uint64_t amount, std::chrono::milliseconds timeout) override;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace Disk
|
||||
|
||||
} // namespace icsneo
|
||||
|
||||
#endif // __cplusplus
|
||||
#endif // __NULLDISKREADDRIVER_H__
|
||||
Reference in New Issue
Block a user