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 {
43 #define XX(type, value, desc) type = value,
45 #undef XX
46  };
47 
48  Device() : mPartitions(*this)
49  {
50  }
51 
52  ~Device();
53 
54  bool operator==(const String& name) const
55  {
56  return getName() == name;
57  }
58 
62  const PartitionTable& partitions() const
63  {
64  return mPartitions;
65  }
66 
71  {
72  return mPartitions;
73  }
74 
80  bool loadPartitions(uint32_t tableOffset)
81  {
82  return loadPartitions(*this, tableOffset);
83  }
84 
91  bool loadPartitions(Device& source, uint32_t tableOffset);
92 
96  virtual String getName() const = 0;
97 
102  virtual uint32_t getId() const
103  {
104  return 0;
105  }
106 
110  virtual size_t getBlockSize() const = 0;
111 
116  virtual storage_size_t getSize() const = 0;
117 
121  virtual Type getType() const = 0;
122 
130  virtual bool read(storage_size_t address, void* dst, size_t size) = 0;
131 
139  virtual bool write(storage_size_t address, const void* src, size_t size) = 0;
140 
147  virtual bool erase_range(storage_size_t address, storage_size_t size) = 0;
148 
155  virtual uint16_t getSectorSize() const
156  {
157  return defaultSectorSize;
158  }
159 
164  {
165  return getSize() / getSectorSize();
166  }
167 
174  virtual bool sync()
175  {
176  return true;
177  }
178 
182  static constexpr uint16_t defaultSectorSize{512};
183 
184  size_t printTo(Print& p) const;
185 
186 protected:
188 };
189 
190 } // namespace Storage
191 
PartitionTable mPartitions
Definition: Components/Storage/src/include/Storage/Device.h:187
String toLongString(Storage::Device::Type type)
Base class template for linked items with type casting.
Definition: LinkedObject.h:61
const PartitionTable & partitions() const
Provide read-only access to partition table.
Definition: Components/Storage/src/include/Storage/Device.h:62
virtual bool erase_range(storage_size_t address, storage_size_t size)=0
Erase a region of storage in preparation for writing.
XX(type, value, desc)
The String class.
Definition: WString.h:136
#define STORAGE_TYPE_MAP(XX)
Definition: Components/Storage/src/include/Storage/Device.h:17
virtual uint32_t getId() const
Obtain device ID.
Definition: Components/Storage/src/include/Storage/Device.h:102
virtual uint16_t getSectorSize() const
Get sector size, the unit of allocation for block-access devices.
Definition: Components/Storage/src/include/Storage/Device.h:155
PartitionTable & editablePartitions()
Provide full access to partition table.
Definition: Components/Storage/src/include/Storage/Device.h:70
Definition: PartitionTable.h:26
Type
Storage type.
Definition: Components/Storage/src/include/Storage/Device.h:42
virtual storage_size_t getSectorCount() const
Obtain total number of sectors on this device.
Definition: Components/Storage/src/include/Storage/Device.h:163
static constexpr uint16_t defaultSectorSize
Definition: Components/Storage/src/include/Storage/Device.h:182
Definition: FileDevice.h:25
virtual String getName() const =0
Obtain unique device name.
virtual bool sync()
Flush any pending writes to the physical media.
Definition: Components/Storage/src/include/Storage/Device.h:174
virtual bool read(storage_size_t address, void *dst, size_t size)=0
Read data from the storage device.
virtual size_t getBlockSize() const =0
Obtain smallest allocation unit for erase operations.
virtual storage_size_t getSize() const =0
Obtain addressable size of this device.
Represents a storage device (e.g. flash memory)
Definition: Components/Storage/src/include/Storage/Device.h:33
Provides formatted output to stream.
Definition: Print.h:36
bool operator==(const String &name) const
Definition: Components/Storage/src/include/Storage/Device.h:54
Device()
Definition: Components/Storage/src/include/Storage/Device.h:48
String toString(Storage::Device::Type type)
virtual bool write(storage_size_t address, const void *src, size_t size)=0
Write data to the storage device.
uint32_t storage_size_t
Definition: Components/Storage/src/include/Storage/Types.h:19
size_t printTo(Print &p) const
virtual Type getType() const =0
Obtain device type.
bool loadPartitions(uint32_t tableOffset)
Load partition table entries @tableOffset Location of partition table to read.
Definition: Components/Storage/src/include/Storage/Device.h:80