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 <Data/LinkedObjectList.h>
14 #include "PartitionTable.h"
15 
16 #define STORAGE_TYPE_MAP(XX) \
17  XX(unknown, 0x00, "Other storage device") \
18  XX(flash, 0x01, "SPI flash") \
19  XX(spiram, 0x02, "SPI RAM") \
20  XX(sdcard, 0x03, "SD Card") \
21  XX(disk, 0x04, "Physical disk") \
22  XX(file, 0x05, "Backing file on separate filesystem") \
23  XX(sysmem, 0x06, "System Memory")
24 
25 namespace Storage
26 {
27 class SpiFlash;
28 
32 class Device : public LinkedObjectTemplate<Device>
33 {
34 public:
37 
41  enum class Type : uint8_t {
42 #define XX(type, value, desc) type = value,
44 #undef XX
45  };
46 
47  Device() : mPartitions(*this)
48  {
49  }
50 
51  ~Device();
52 
53  bool operator==(const String& name) const
54  {
55  return getName() == name;
56  }
57 
59  {
60  return mPartitions;
61  }
62 
63  const PartitionTable& partitions() const
64  {
65  return mPartitions;
66  }
67 
73  bool loadPartitions(uint32_t tableOffset)
74  {
75  return loadPartitions(*this, tableOffset);
76  }
77 
84  bool loadPartitions(Device& source, uint32_t tableOffset);
85 
89  virtual String getName() const = 0;
90 
95  virtual uint32_t getId() const
96  {
97  return 0;
98  }
99 
103  virtual size_t getBlockSize() const = 0;
104 
109  virtual size_t getSize() const = 0;
110 
114  virtual Type getType() const = 0;
115 
123  virtual bool read(uint32_t address, void* dst, size_t size) = 0;
124 
132  virtual bool write(uint32_t address, const void* src, size_t size) = 0;
133 
140  virtual bool erase_range(uint32_t address, size_t size) = 0;
141 
142 protected:
144 };
145 
146 } // namespace Storage
147 
const PartitionTable & partitions() const
Definition: Components/Storage/src/include/Storage/Device.h:63
Definition: LinkedObjectList.h:62
virtual bool write(uint32_t address, const void *src, size_t size)=0
Write data to the storage device.
virtual uint32_t getId() const
Obtain device ID.
Definition: Components/Storage/src/include/Storage/Device.h:95
String toString(Storage::Device::Type type)
The String class.
Definition: WString.h:136
virtual bool erase_range(uint32_t address, size_t size)=0
Erase a region of storage in preparation for writing.
Type
Storage type.
Definition: Components/Storage/src/include/Storage/Device.h:41
virtual bool read(uint32_t address, void *dst, size_t size)=0
Read data from the storage device.
virtual String getName() const =0
Obtain unique device name.
virtual size_t getBlockSize() const =0
Obtain smallest allocation unit for erase operations.
#define STORAGE_TYPE_MAP(XX)
Definition: Components/Storage/src/include/Storage/Device.h:16
Base class template for linked items with type casting.
Definition: LinkedObject.h:56
Class template for singly-linked list of objects.
Definition: LinkedObjectList.h:119
XX(type, value, desc)
Device()
Definition: Components/Storage/src/include/Storage/Device.h:47
Represents a storage device (e.g. flash memory)
Definition: Components/Storage/src/include/Storage/Device.h:32
PartitionTable & partitions()
Definition: Components/Storage/src/include/Storage/Device.h:58
virtual Type getType() const =0
Obtain device type.
bool operator==(const String &name) const
Definition: Components/Storage/src/include/Storage/Device.h:53
Definition: FileDevice.h:23
Definition: PartitionTable.h:18
virtual size_t getSize() const =0
Obtain addressable size of this device.
bool loadPartitions(uint32_t tableOffset)
Load partition table entries Location of partition table to read.
Definition: Components/Storage/src/include/Storage/Device.h:73
String toLongString(Storage::Device::Type type)
PartitionTable mPartitions
Definition: Components/Storage/src/include/Storage/Device.h:143