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 
53  Device(const Device&) = delete;
54  Device(Device&&) = delete;
55  Device& operator=(const Device&) = delete;
56  Device& operator=(Device&&) = delete;
57 
59 
60  bool operator==(const String& name) const
61  {
62  return getName() == name;
63  }
64 
68  const PartitionTable& partitions() const
69  {
70  return mPartitions;
71  }
72 
77  {
78  return mPartitions;
79  }
80 
86  bool loadPartitions(uint32_t tableOffset)
87  {
88  return loadPartitions(*this, tableOffset);
89  }
90 
97  bool loadPartitions(Device& source, uint32_t tableOffset);
98 
102  virtual String getName() const = 0;
103 
108  virtual uint32_t getId() const
109  {
110  return 0;
111  }
112 
116  virtual size_t getBlockSize() const = 0;
117 
122  virtual storage_size_t getSize() const = 0;
123 
127  virtual Type getType() const = 0;
128 
136  virtual bool read(storage_size_t address, void* dst, size_t size) = 0;
137 
145  virtual bool write(storage_size_t address, const void* src, size_t size) = 0;
146 
153  virtual bool erase_range(storage_size_t address, storage_size_t size) = 0;
154 
161  virtual uint16_t getSectorSize() const
162  {
163  return defaultSectorSize;
164  }
165 
170  {
171  return getSize() / getSectorSize();
172  }
173 
180  virtual bool sync()
181  {
182  return true;
183  }
184 
188  static constexpr uint16_t defaultSectorSize{512};
189 
190  size_t printTo(Print& p) const;
191 
192 protected:
194 };
195 
196 } // namespace Storage
197 
#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
Device(const Device &)=delete
virtual uint32_t getId() const
Obtain device ID.
Definition: Components/Storage/src/include/Storage/Device.h:108
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:68
virtual storage_size_t getSectorCount() const
Obtain total number of sectors on this device.
Definition: Components/Storage/src/include/Storage/Device.h:169
virtual Type getType() const =0
Obtain device type.
static constexpr uint16_t defaultSectorSize
Definition: Components/Storage/src/include/Storage/Device.h:188
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.
Device & operator=(Device &&)=delete
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:180
bool loadPartitions(Device &source, uint32_t tableOffset)
Load partition table entries from another table.
Device(Device &&)=delete
bool loadPartitions(uint32_t tableOffset)
Load partition table entries @tableOffset Location of partition table to read.
Definition: Components/Storage/src/include/Storage/Device.h:86
bool operator==(const String &name) const
Definition: Components/Storage/src/include/Storage/Device.h:60
Device & operator=(const Device &)=delete
PartitionTable mPartitions
Definition: Components/Storage/src/include/Storage/Device.h:193
PartitionTable & editablePartitions()
Provide full access to partition table.
Definition: Components/Storage/src/include/Storage/Device.h:76
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:161
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