Skip to content

Commit

Permalink
Add mode parameter to PartititionStream constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Mar 11, 2024
1 parent 7b87eab commit 2c5beb3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
6 changes: 5 additions & 1 deletion Sming/Components/Storage/src/PartitionStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ int PartitionStream::seekFrom(int offset, SeekOrigin origin)

size_t PartitionStream::write(const uint8_t* data, size_t length)
{
if(mode < Mode::Write) {
return 0;
}

auto len = std::min(size_t(size - writePos), length);
if(len == 0) {
return 0;
}

if(blockErase) {
if(mode == Mode::BlockErase) {
auto endPos = writePos + len;
if(endPos > erasePos) {
size_t blockSize = partition.getBlockSize();
Expand Down
22 changes: 13 additions & 9 deletions Sming/Components/Storage/src/include/Storage/PartitionStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@

namespace Storage
{
enum class Mode {
ReadOnly,
Write, ///< Write but do not erase, region should be pre-erased
BlockErase, ///< Erase blocks as required before writing
};

/**
* @brief Stream operating directory on a Storage partition
*
Expand All @@ -30,24 +36,22 @@ class PartitionStream : public ReadWriteStream
* @param partition
* @param offset Limit access to this starting offset
* @param size Limit access to this number of bytes from starting offset
* @param blockErase Set to true to erase blocks before writing
*
* If blockErase is false then region must be pre-erased before writing.
* @param mode
*/
PartitionStream(Partition partition, storage_size_t offset, size_t size, bool blockErase = false)
: partition(partition), startOffset(offset), size(size), blockErase(blockErase)
PartitionStream(Partition partition, storage_size_t offset, size_t size, Mode mode = Mode::ReadOnly)
: partition(partition), startOffset(offset), size(size), mode(mode)
{
}

/**
* @brief Access entire partition using stream
* @param partition
* @param blockErase Set to true to erase blocks before writing
* @param mode
*
* If blockErase is false then partition must be pre-erased before writing.
*/
PartitionStream(Partition partition, bool blockErase = false)
: partition(partition), startOffset{0}, size(partition.size()), blockErase(blockErase)
PartitionStream(Partition partition, Mode mode = Mode::ReadOnly)
: partition(partition), startOffset{0}, size(partition.size()), mode(mode)
{
}

Expand All @@ -74,7 +78,7 @@ class PartitionStream : public ReadWriteStream
uint32_t writePos{0};
uint32_t readPos{0};
uint32_t erasePos{0};
bool blockErase;
Mode mode;
};

} // namespace Storage
2 changes: 1 addition & 1 deletion samples/Basic_Ota/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void doUpgrade()
auto spiffsPart = findSpiffsPartition(part);
if(spiffsPart) {
// use user supplied values (defaults for 4mb flash in hardware config)
otaUpdater->addItem(SPIFFS_URL, spiffsPart, new Storage::PartitionStream(spiffsPart, true));
otaUpdater->addItem(SPIFFS_URL, spiffsPart, new Storage::PartitionStream(spiffsPart, Storage::Mode::BlockErase));
}

// request switch and reboot on success
Expand Down

0 comments on commit 2c5beb3

Please sign in to comment.