Components/Storage/src/include/Storage/Device.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Device.h - external storage device API
8  *
9  ****/
10 #pragma once
11 
12 #include <WString.h>
13 #include <Printable.h>
14 #include <Data/LinkedObjectList.h>
15 #include "PartitionTable.h"
16 
17 #define STORAGE_TYPE_MAP(XX) \
18  XX(unknown, 0x00, "Other storage device") \
19  XX(flash, 0x01, "SPI flash") \
20  XX(spiram, 0x02, "SPI RAM") \
21  XX(sdcard, 0x03, "SD Card") \
22  XX(disk, 0x04, "Physical disk") \
23  XX(file, 0x05, "Backing file on separate filesystem") \
24  XX(sysmem, 0x06, "System Memory")
25 
26 namespace Storage
27 {
28 class SpiFlash;
29 
33 class Device : public LinkedObjectTemplate<Device>
34 {
35 public:
38 
42  enum class Type : uint8_t {
44 #define XX(type, value, desc) type = value,
46 #undef XX
47  };
48 
49  Device() : mPartitions(*this)
50  {
51  }
52 
54 
55  bool operator==(const String& name) const
56  {
57  return getName() == name;
58  }
59 
63  const PartitionTable& partitions() const
64  {
65  return mPartitions;
66  }
67 
72  {
73  return mPartitions;
74  }
75 
81  bool loadPartitions(uint32_t tableOffset)
82  {
83  return loadPartitions(*this, tableOffset);
84  }
85 
92  bool loadPartitions(Device& source, uint32_t tableOffset);
93 
97  virtual String getName() const = 0;
98 
103  virtual uint32_t getId() const
104  {
105  return 0;
106  }
107 
111  virtual size_t getBlockSize() const = 0;
112 
117  virtual storage_size_t getSize() const = 0;
118 
122  virtual Type getType() const = 0;
123 
131  virtual bool read(storage_size_t address, void* dst, size_t size) = 0;
132 
140  virtual bool write(storage_size_t address, const void* src, size_t size) = 0;
141 
148  virtual bool erase_range(storage_size_t address, storage_size_t size) = 0;
149 
156  virtual uint16_t getSectorSize() const
157  {
158  return defaultSectorSize;
159  }
160 
165  {
166  return getSize() / getSectorSize();
167  }
168 
175  virtual bool sync()
176  {
177  return true;
178  }
179 
183  static constexpr uint16_t defaultSectorSize{512};
184 
185  size_t printTo(Print& p) const;
186 
187 protected:
189 };
190 
191 } // namespace Storage
192 
#define STORAGE_TYPE_MAP(XX)
Definition: Components/Storage/src/include/Storage/Device.h:17
String toLongString(Storage::Device::Type type)
String toString(Storage::Device::Type type)
uint32_t storage_size_t
Definition: Components/Storage/src/include/Storage/Types.h:19
Definition: LinkedObjectList.h:90
Base class template for linked items with type casting.
Definition: LinkedObject.h:62
Provides formatted output to stream.
Definition: Print.h:37
Represents a storage device (e.g. flash memory)
Definition: Components/Storage/src/include/Storage/Device.h:34
virtual uint32_t getId() const
Obtain device ID.
Definition: Components/Storage/src/include/Storage/Device.h:103
virtual size_t getBlockSize() const =0
Obtain smallest allocation unit for erase operations.
const PartitionTable & partitions() const
Provide read-only access to partition table.
Definition: Components/Storage/src/include/Storage/Device.h:63
virtual storage_size_t getSectorCount() const
Obtain total number of sectors on this device.
Definition: Components/Storage/src/include/Storage/Device.h:164
virtual Type getType() const =0
Obtain device type.
static constexpr uint16_t defaultSectorSize
Definition: Components/Storage/src/include/Storage/Device.h:183
size_t printTo(Print &p) const
virtual bool read(storage_size_t address, void *dst, size_t size)=0
Read data from the storage device.
virtual bool write(storage_size_t address, const void *src, size_t size)=0
Write data to the storage device.
virtual storage_size_t getSize() const =0
Obtain addressable size of this device.
Device()
Definition: Components/Storage/src/include/Storage/Device.h:49
virtual bool sync()
Flush any pending writes to the physical media.
Definition: Components/Storage/src/include/Storage/Device.h:175
bool loadPartitions(Device &source, uint32_t tableOffset)
Load partition table entries from another table.
bool loadPartitions(uint32_t tableOffset)
Load partition table entries @tableOffset Location of partition table to read.
Definition: Components/Storage/src/include/Storage/Device.h:81
bool operator==(const String &name) const
Definition: Components/Storage/src/include/Storage/Device.h:55
PartitionTable mPartitions
Definition: Components/Storage/src/include/Storage/Device.h:188
PartitionTable & editablePartitions()
Provide full access to partition table.
Definition: Components/Storage/src/include/Storage/Device.h:71
virtual bool erase_range(storage_size_t address, storage_size_t size)=0
Erase a region of storage in preparation for writing.
virtual uint16_t getSectorSize() const
Get sector size, the unit of allocation for block-access devices.
Definition: Components/Storage/src/include/Storage/Device.h:156
Type
Storage type.
Definition: Components/Storage/src/include/Storage/Device.h:42
XX(type, value, desc)
virtual String getName() const =0
Obtain unique device name.
Definition: PartitionTable.h:19
The String class.
Definition: WString.h:137
Definition: FileDevice.h:26