Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove CustomDevice, use editablePartitions() method #2572

Merged
merged 2 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sming/Components/IFS
10 changes: 5 additions & 5 deletions Sming/Components/Storage/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -332,19 +332,19 @@ This is a C++ interface. Some examples::

Storage::Partition part = Storage::findPartition("spiffs0"); // Find by name
if(part) {
debugf("Partition '%s' found", part.name().c_str());
Serial << part << endl;
} else {
debugf("Partition NOT found");
Serial << "spiffs0 partition NOT Found" << endl;
}

// Enumerate all partitions
for(auto part: Storage::findPartition()) {
debugf("Found '%s' at 0x%08x, size 0x%08x", part.name().c_str(), part.address(), part.size());
Serial << part << endl;
}

// Enumerate all SPIFFS partitions
for(auto part: Storage::findPartition(Storage::Partition::SubType::Data::spiffs)) {
debugf("Found '%s' at 0x%08x, size 0x%08x", part.name().c_str(), part.address(), part.size());
Serial << part << endl;
}


Expand All @@ -361,7 +361,7 @@ You can query partition entries from a Storage object directly, for example::
#include <Storage/SpiFlash.h>

for(auto part: Storage::spiFlash->partitions()) {
debugf("Found '%s' at 0x%08x, size 0x%08x", part.name().c_str(), part.address(), part.size());
Serial << part << endl;
}


Expand Down
2 changes: 1 addition & 1 deletion Sming/Components/Storage/src/ProgMem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Partition ProgMem::ProgMemPartitionTable::add(const String& name, const void* fl
return Partition{};
}

return CustomPartitionTable::add(name, type, addr, size, Partition::Flag::readOnly);
return PartitionTable::add(name, type, addr, size, Partition::Flag::readOnly);
}

} // namespace Storage
33 changes: 0 additions & 33 deletions Sming/Components/Storage/src/include/Storage/CustomDevice.h

This file was deleted.

8 changes: 8 additions & 0 deletions Sming/Components/Storage/src/include/Storage/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ class Device : public LinkedObjectTemplate<Device>
return mPartitions;
}

/**
* @brief Provide full access to partition table
*/
PartitionTable& editablePartitions()
{
return mPartitions;
}

/**
* @brief Load partition table entries
* @tableOffset Location of partition table to read
Expand Down
12 changes: 6 additions & 6 deletions Sming/Components/Storage/src/include/Storage/PartitionTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,6 @@ class PartitionTable
return mDevice;
}

protected:
friend Device;
friend Iterator;

void load(const esp_partition_info_t* entry, unsigned count);

/**
* @brief Add new partition using given Info
* @param info Must be allocated using `new`: Device will take ownership
Expand All @@ -124,6 +118,12 @@ class PartitionTable
mEntries.clear();
}

protected:
friend Device;
friend Iterator;

void load(const esp_partition_info_t* entry, unsigned count);

Device& mDevice;
Partition::Info::OwnedList mEntries;
};
Expand Down
8 changes: 4 additions & 4 deletions Sming/Components/Storage/src/include/Storage/ProgMem.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
****/
#pragma once

#include "CustomDevice.h"
#include "Device.h"

namespace Storage
{
/**
* @brief Storage device to access PROGMEM using flash API
*/
class ProgMem : public CustomDevice
class ProgMem : public Device
{
public:
String getName() const override
Expand Down Expand Up @@ -51,7 +51,7 @@ class ProgMem : public CustomDevice
return false;
}

class ProgMemPartitionTable : public CustomPartitionTable
class ProgMemPartitionTable : public PartitionTable
{
public:
/**
Expand All @@ -73,7 +73,7 @@ class ProgMem : public CustomDevice
}
};

ProgMemPartitionTable& partitions()
ProgMemPartitionTable& editablePartitions()
{
return static_cast<ProgMemPartitionTable&>(mPartitions);
}
Expand Down
6 changes: 3 additions & 3 deletions Sming/Components/Storage/src/include/Storage/StreamDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* StreamDevice.h
*/

#include "CustomDevice.h"
#include "Device.h"
#include <Data/Stream/DataSourceStream.h>

namespace Storage
Expand All @@ -11,10 +11,10 @@ namespace Storage
* @brief Read-only partition on a stream object
* @note Writes not possible as streams always append data, cannot do random writes
*/
class StreamDevice : public CustomDevice
class StreamDevice : public Device
{
public:
StreamDevice(IDataSourceStream* stream, size_t size) : CustomDevice(nameOf(stream), size), mStream(stream)
StreamDevice(IDataSourceStream* stream, size_t size) : Device(nameOf(stream), size), mStream(stream)
{
}

Expand Down
12 changes: 6 additions & 6 deletions Sming/Components/Storage/src/include/Storage/SysMem.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

#pragma once

#include "CustomDevice.h"
#include "Device.h"

namespace Storage
{
/**
* @brief Storage device to access system memory, e.g. RAM
*/
class SysMem : public CustomDevice
class SysMem : public Device
{
public:
String getName() const override
Expand Down Expand Up @@ -70,20 +70,20 @@ class SysMem : public CustomDevice
return true;
}

class SysMemPartitionTable : public CustomPartitionTable
class SysMemPartitionTable : public PartitionTable
{
public:
/**
* @brief Add partition entry for FlashString data access
*/
Partition add(const String& name, const FSTR::ObjectBase& fstr, Partition::FullType type)
{
return CustomPartitionTable::add(name, type, reinterpret_cast<uint32_t>(fstr.data()), fstr.size(),
Partition::Flag::readOnly);
return PartitionTable::add(name, type, reinterpret_cast<uint32_t>(fstr.data()), fstr.size(),
Partition::Flag::readOnly);
}
};

SysMemPartitionTable& partitions()
SysMemPartitionTable& editablePartitions()
{
return static_cast<SysMemPartitionTable&>(mPartitions);
}
Expand Down
2 changes: 1 addition & 1 deletion Sming/Libraries/LittleFS
38 changes: 22 additions & 16 deletions docs/source/upgrading/4.6-4.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,42 @@ From v4.6 to v4.7
Storage Partition methods
-------------------------

The ``Storage::Partition::getDevice()`` method has been removed.
This could be used to bypass protections offered by the partitioning API.
The ``Storage::Partition::getDevice()`` method has been removed because
it could be used to bypass protections offered by the partitioning API.


Storage Device Partitions
-------------------------

The ``Storage::CustomDevice::createPartition()`` methods have been removed.
Instead, use partition table methods.
The ``CustomDevice`` class has been removed as it is simpler and more flexible to instead use PartitionTable methods.

For example::
The :cpp:func:`Storage::Device::partitions` method returns a read-only (const) :cpp:class:`Storage::PartitionTable` object
for general use to avoid inadvertent modification.

part = device->createPartition("archive", Storage::Partition::SubType::Data::fwfs, startOffset, size);
Use the :cpp:func:`Storage::Device::editablePartitions` method to make partition table changes.

becomes::
For example::

part = device->partitions().add("archive", Storage::Partition::SubType::Data::fwfs, startOffset, size);
part = device->createPartition("archive", Storage::Partition::SubType::Data::fwfs, startOffset, size);

This also applies to derivatives :cpp:class:`Storage::SysMem` and :cpp:class:`Storage::ProgMem`.
becomes::

part = device->editablePartitions().add("archive", Storage::Partition::SubType::Data::fwfs, startOffset, size);

Creating custom partition types require use of :cpp:struct:`Storage::Partition::FullType`.

For example::

part = device->createPartitions("fs_app", Storage::Partition::Type::data, 100, startOffset, size);
Custom Partition Types
----------------------

becomes::
Creating custom partition types now require use of :cpp:struct:`Storage::Partition::FullType`.

part = device->partitions().add("fs_app", {Storage::Partition::Type::data, 100}, startOffset, size);
For example::

Note how the ``type`` and ``subtype`` values are enclosed in braces (instantiating a ``FullType`` struct).
This avoids confusing the subtype value ``100`` with the start offset.
part = device->createPartition("fs_app", Storage::Partition::Type::data, 100, startOffset, size);

becomes::

part = device->editablePartitions().add("fs_app", {Storage::Partition::Type::data, 100}, startOffset, size);

Note how the ``type`` and ``subtype`` values are enclosed in braces (instantiating a ``FullType`` struct).
This avoids confusing the subtype value ``100`` with the start offset.
3 changes: 2 additions & 1 deletion samples/Basic_IFS/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ bool initFileSystem()

#ifdef ENABLE_FLASHSTRING_IMAGE
// Create a partition wrapping some flashstring data
auto part = Storage::progMem.partitions().add(F("fwfsMem"), fwfsImage, Storage::Partition::SubType::Data::fwfs);
auto part =
Storage::progMem.editablePartitions().add(F("fwfsMem"), fwfsImage, Storage::Partition::SubType::Data::fwfs);
#else
auto part = Storage::findDefaultPartition(Storage::Partition::SubType::Data::fwfs);
#endif
Expand Down
7 changes: 4 additions & 3 deletions samples/Basic_Storage/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,20 @@ void init()
Serial.println(_F("** Reading tests, repeat 3 times to show effect of caching (if any)"));

Serial.println(_F("** Reading SysMem device (flash)"));
part = Storage::sysMem.partitions().add(F("fs_app"), FS_app, {Storage::Partition::Type::data, 100});
part = Storage::sysMem.editablePartitions().add(F("fs_app FLASH"), FS_app, {Storage::Partition::Type::data, 100});
printPart(part);
printPart(part);
printPart(part);

Serial.println(_F("** Reading SysMem device (RAM)"));
part = Storage::sysMem.partitions().add(F("fs_app"), FS_app, {Storage::Partition::Type::data, 100});
part = Storage::sysMem.editablePartitions().add(F("fs_app RAM"), FS_app, {Storage::Partition::Type::data, 100});
printPart(part);
printPart(part);
printPart(part);

Serial.println(_F("** Reading ProgMem device"));
part = Storage::progMem.partitions().add(F("fs_app"), FS_app, {Storage::Partition::Type::data, 100});
part =
Storage::progMem.editablePartitions().add(F("fs_app PROGMEM"), FS_app, {Storage::Partition::Type::data, 100});
printPart(part);
printPart(part);
printPart(part);
Expand Down
4 changes: 2 additions & 2 deletions tests/HostTests/modules/Spiffs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ class SpiffsTest : public TestGroup
}
auto dev = new Storage::FileDevice(tag, hfs, f);
Storage::registerDevice(dev);
auto part = dev->partitions().add(tag, Storage::Partition::SubType::Data::spiffs, 0, dev->getSize(),
Storage::Partition::Flag::readOnly);
auto part = dev->editablePartitions().add(tag, Storage::Partition::SubType::Data::spiffs, 0, dev->getSize(),
Storage::Partition::Flag::readOnly);

auto fs = IFS::createSpiffsFilesystem(part);
int err = fs->mount();
Expand Down