Base class for sector-addressable (block) devices. More...

#include <BlockDevice.h>

Inheritance diagram for Storage::Disk::BlockDevice:
Collaboration diagram for Storage::Disk::BlockDevice:

Classes

struct  Stat
 

Public Member Functions

bool read (storage_size_t address, void *dst, size_t size) override
 Read data from the storage device. More...
 
bool write (storage_size_t address, const void *src, size_t size) override
 Write data to the storage device. More...
 
bool erase_range (storage_size_t address, storage_size_t size) override
 Erase a region of storage in preparation for writing. More...
 
size_t getBlockSize () const override
 Obtain smallest allocation unit for erase operations. More...
 
storage_size_t getSize () const override
 Obtain addressable size of this device. More...
 
storage_size_t getSectorCount () const override
 Obtain total number of sectors on this device. More...
 
bool sync () override
 Flush any pending writes to the physical media. More...
 
bool allocateBuffers (unsigned numBuffers)
 Set number of sector buffers to use. More...
 
- Public Member Functions inherited from Storage::Device
 Device ()
 
 ~Device ()
 
bool operator== (const String &name) const
 
const PartitionTablepartitions () const
 Provide read-only access to partition table. More...
 
PartitionTableeditablePartitions ()
 Provide full access to partition table. More...
 
bool loadPartitions (uint32_t tableOffset)
 Load partition table entries @tableOffset Location of partition table to read. More...
 
bool loadPartitions (Device &source, uint32_t tableOffset)
 Load partition table entries from another table. More...
 
virtual String getName () const =0
 Obtain unique device name. More...
 
virtual uint32_t getId () const
 Obtain device ID. More...
 
virtual Type getType () const =0
 Obtain device type. More...
 
virtual uint16_t getSectorSize () const
 Get sector size, the unit of allocation for block-access devices. More...
 
size_t printTo (Print &p) const
 
- Public Member Functions inherited from LinkedObjectTemplate< Device >
Device * getNext () const
 
bool insertAfter (Device *object)
 
Iterator begin () const
 
Iterator end () const
 
Iterator cbegin () const
 
Iterator cend () const
 
- Public Member Functions inherited from LinkedObject
virtual ~LinkedObject ()
 
virtual LinkedObjectnext () const
 
bool insertAfter (LinkedObject *object)
 
bool operator== (const LinkedObject &other) const
 
bool operator!= (const LinkedObject &other) const
 

Public Attributes

Stat stat
 

Protected Member Functions

virtual bool raw_sector_read (storage_size_t address, void *dst, size_t size)=0
 
virtual bool raw_sector_write (storage_size_t address, const void *src, size_t size)=0
 
virtual bool raw_sector_erase_range (storage_size_t address, size_t size)=0
 
virtual bool raw_sync ()=0
 
bool flushBuffer (Buffer &buf)
 
bool flushBuffers ()
 

Protected Attributes

std::unique_ptr< BufferListbuffers
 
uint64_t sectorCount {0}
 
uint16_t sectorSize {defaultSectorSize}
 
uint8_t sectorSizeShift {getSizeBits(defaultSectorSize)}
 
- Protected Attributes inherited from Storage::Device
PartitionTable mPartitions
 

Additional Inherited Members

- Public Types inherited from Storage::Device
enum class  Type : uint8_t { partitionType = uint8_t(Partition::Type::storage) , XX }
 Storage type. More...
 
using List = LinkedObjectListTemplate< Device >
 
using OwnedList = OwnedLinkedObjectListTemplate< Device >
 
- Public Types inherited from LinkedObjectTemplate< Device >
using Iterator = IteratorTemplate< Device, Device *, Device & >
 
using ConstIterator = IteratorTemplate< const Device, const Device *, const Device & >
 
- Static Public Attributes inherited from Storage::Device
static constexpr uint16_t defaultSectorSize {512}
 

Detailed Description

Base class for sector-addressable (block) devices.

Inherited classes must set the sectorCount value, and implement the four raw_xxx methods.

This class supports byte-level access using internal buffering, which if required must be enabled by the application via the allocateBuffers method. Without buffering, read/writes must always be sector-aligned. Rrase must always be sector-aligned.

For power-loss resiliency it is important to call sync() at appropriate times. Filing system implementations should do this after closing a file, for example. Applications should consider this if leaving files open for extended periods, and explicitly call 'flush' on the filing system or 'sync' on the partition or device if necessary.

Member Function Documentation

◆ allocateBuffers()

bool Storage::Disk::BlockDevice::allocateBuffers ( unsigned  numBuffers)

Set number of sector buffers to use.

Parameters
numBuffersNumber of buffers to allocate 1,2,4,8,etc. Pass 0 to deallocate/disable buffering.
Return values
boolfalse on memory allocation error, or if failed to flush existing buffers to disk

Required to support byte-level read/write operations on block devices. Buffering can improve performance, with diminishing returns above around 4 sectors.

◆ erase_range()

bool Storage::Disk::BlockDevice::erase_range ( storage_size_t  address,
storage_size_t  size 
)
overridevirtual

Erase a region of storage in preparation for writing.

Parameters
addressWhere to start erasing
sizeSize of region to erase, in bytes
Return values
booltrue on success, false on error

Implements Storage::Device.

◆ flushBuffer()

bool Storage::Disk::BlockDevice::flushBuffer ( Buffer buf)
protected

◆ flushBuffers()

bool Storage::Disk::BlockDevice::flushBuffers ( )
protected

◆ getBlockSize()

size_t Storage::Disk::BlockDevice::getBlockSize ( ) const
inlineoverridevirtual

Obtain smallest allocation unit for erase operations.

Implements Storage::Device.

Reimplemented in Storage::SD::Card.

◆ getSectorCount()

storage_size_t Storage::Disk::BlockDevice::getSectorCount ( ) const
inlineoverridevirtual

Obtain total number of sectors on this device.

Reimplemented from Storage::Device.

◆ getSize()

storage_size_t Storage::Disk::BlockDevice::getSize ( ) const
inlineoverridevirtual

Obtain addressable size of this device.

Return values
storage_size_tMust be at least as large as the value declared in the hardware configuration

Implements Storage::Device.

◆ raw_sector_erase_range()

virtual bool Storage::Disk::BlockDevice::raw_sector_erase_range ( storage_size_t  address,
size_t  size 
)
protectedpure virtual

◆ raw_sector_read()

virtual bool Storage::Disk::BlockDevice::raw_sector_read ( storage_size_t  address,
void *  dst,
size_t  size 
)
protectedpure virtual

◆ raw_sector_write()

virtual bool Storage::Disk::BlockDevice::raw_sector_write ( storage_size_t  address,
const void *  src,
size_t  size 
)
protectedpure virtual

◆ raw_sync()

virtual bool Storage::Disk::BlockDevice::raw_sync ( )
protectedpure virtual

◆ read()

bool Storage::Disk::BlockDevice::read ( storage_size_t  address,
void *  dst,
size_t  size 
)
overridevirtual

Read data from the storage device.

Parameters
addressWhere to start reading
dstBuffer to store data
sizeSize of data to be read, in bytes.
Return values
booltrue on success, false on error

Implements Storage::Device.

◆ sync()

bool Storage::Disk::BlockDevice::sync ( )
overridevirtual

Flush any pending writes to the physical media.

Return values
boolReturn false if sync operation failed.

Devices with intermediate buffering should implement this method.

Reimplemented from Storage::Device.

◆ write()

bool Storage::Disk::BlockDevice::write ( storage_size_t  address,
const void *  src,
size_t  size 
)
overridevirtual

Write data to the storage device.

Parameters
addressWhere to start writing
srcData to write
sizeSize of data to be written, in bytes.
Return values
booltrue on success, false on error

Implements Storage::Device.

Member Data Documentation

◆ buffers

std::unique_ptr<BufferList> Storage::Disk::BlockDevice::buffers
protected

◆ sectorCount

uint64_t Storage::Disk::BlockDevice::sectorCount {0}
protected

◆ sectorSize

uint16_t Storage::Disk::BlockDevice::sectorSize {defaultSectorSize}
protected

◆ sectorSizeShift

uint8_t Storage::Disk::BlockDevice::sectorSizeShift {getSizeBits(defaultSectorSize)}
protected

◆ stat

Stat Storage::Disk::BlockDevice::stat

The documentation for this class was generated from the following file: