From 2c5beb31926a467dee08773a6ff68930180e2545 Mon Sep 17 00:00:00 2001 From: mikee47 Date: Mon, 11 Mar 2024 12:00:17 +0000 Subject: [PATCH] Add `mode` parameter to PartititionStream constructor --- .../Storage/src/PartitionStream.cpp | 6 ++++- .../src/include/Storage/PartitionStream.h | 22 +++++++++++-------- samples/Basic_Ota/app/application.cpp | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Sming/Components/Storage/src/PartitionStream.cpp b/Sming/Components/Storage/src/PartitionStream.cpp index beec49dd7d..07980de01b 100644 --- a/Sming/Components/Storage/src/PartitionStream.cpp +++ b/Sming/Components/Storage/src/PartitionStream.cpp @@ -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(); diff --git a/Sming/Components/Storage/src/include/Storage/PartitionStream.h b/Sming/Components/Storage/src/include/Storage/PartitionStream.h index af0b02ddd5..129249de68 100644 --- a/Sming/Components/Storage/src/include/Storage/PartitionStream.h +++ b/Sming/Components/Storage/src/include/Storage/PartitionStream.h @@ -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 * @@ -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) { } @@ -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 diff --git a/samples/Basic_Ota/app/application.cpp b/samples/Basic_Ota/app/application.cpp index 12cabc2c5c..29cb63097b 100644 --- a/samples/Basic_Ota/app/application.cpp +++ b/samples/Basic_Ota/app/application.cpp @@ -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