StreamDevice.h
Go to the documentation of this file.
1 /*
2  * StreamDevice.h
3  */
4 
5 #include "CustomDevice.h"
7 
8 namespace Storage
9 {
14 class StreamDevice : public CustomDevice
15 {
16 public:
17  StreamDevice(IDataSourceStream* stream, size_t size) : CustomDevice(nameOf(stream), size), mStream(stream)
18  {
19  }
20 
21  StreamDevice(IDataSourceStream* stream) : StreamDevice(stream, size_t(stream->available()))
22  {
23  }
24 
26  {
27  String s;
28  if(stream != nullptr) {
29  s = stream->getName();
30  }
31  if(!s) {
32  s = F("stream_") + String(uint32_t(stream), HEX);
33  }
34  return s;
35  }
36 
37  Type getType() const override
38  {
39  return Type::stream;
40  }
41 
42  bool read(uint32_t address, void* buffer, size_t len) override
43  {
44  if(mStream == nullptr) {
45  return false;
46  }
47  if(mStream->seekFrom(address, SeekOrigin::Start) != int(address)) {
48  return false;
49  }
50  return mStream->readBytes(static_cast<char*>(buffer), len) == len;
51  }
52 
53  bool write(uint32_t address, const void* data, size_t len) override
54  {
55  return false;
56  }
57 
58  bool erase_range(uint32_t address, size_t len) override
59  {
60  return false;
61  }
62 
63 private:
64  std::unique_ptr<IDataSourceStream> mStream;
65 };
66 
67 } // namespace Storage
static String nameOf(IDataSourceStream *stream)
Definition: StreamDevice.h:25
Base class for read-only stream.
Definition: DataSourceStream.h:40
#define HEX
Definition: WConstants.h:67
SEEK_SET: Start of file.
The String class.
Definition: WString.h:136
Class to support dynamic partitions.
Definition: CustomDevice.h:16
Type
Storage type.
Definition: Components/Storage/src/include/Storage/Device.h:41
#define F(string_literal)
Wrap a string literal stored in flash and access it using a String object.
Definition: WString.h:113
Read-only partition on a stream object.
Definition: StreamDevice.h:14
StreamDevice(IDataSourceStream *stream, size_t size)
Definition: StreamDevice.h:17
virtual String getName() const
Returns name of the resource.
Definition: DataSourceStream.h:160
StreamDevice(IDataSourceStream *stream)
Definition: StreamDevice.h:21
bool erase_range(uint32_t address, size_t len) override
Erase a region of storage in preparation for writing.
Definition: StreamDevice.h:58
bool write(uint32_t address, const void *data, size_t len) override
Write data to the storage device.
Definition: StreamDevice.h:53
Type getType() const override
Obtain device type.
Definition: StreamDevice.h:37
Definition: FileDevice.h:23
bool read(uint32_t address, void *buffer, size_t len) override
Read data from the storage device.
Definition: StreamDevice.h:42