diff --git a/components/ocs_system/CMakeLists.txt b/components/ocs_system/CMakeLists.txt index 6491ab34..21acb7b8 100644 --- a/components/ocs_system/CMakeLists.txt +++ b/components/ocs_system/CMakeLists.txt @@ -23,6 +23,7 @@ idf_component_register( "target_esp32/native_updater.cpp" "target_esp32/partition_updater.cpp" "target_esp32/cache_updater.cpp" + "target_esp32/spiffs_updater.cpp" REQUIRES "esp_timer" diff --git a/components/ocs_system/target_esp32/spiffs_updater.cpp b/components/ocs_system/target_esp32/spiffs_updater.cpp new file mode 100644 index 00000000..54a64f7f --- /dev/null +++ b/components/ocs_system/target_esp32/spiffs_updater.cpp @@ -0,0 +1,51 @@ +/* + * SPDX-FileCopyrightText: 2025 Tendry Lab + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "freertos/FreeRTOSConfig.h" + +#include "esp_spiffs.h" + +#include "ocs_core/log.h" +#include "ocs_system/target_esp32/spiffs_updater.h" + +namespace ocs { +namespace system { + +namespace { + +const char* log_tag = "spiffs_updater"; + +} // namespace + +SpiffsUpdater::SpiffsUpdater(IUpdater& updater, const char* partition_label) + : partition_label_(partition_label) + , updater_(updater) { +} + +status::StatusCode SpiffsUpdater::begin(size_t total_size, uint32_t crc32) { + const auto err = esp_vfs_spiffs_unregister(partition_label_.c_str()); + if (err != ESP_OK) { + ocs_loge(log_tag, "esp_vfs_spiffs_unregister(): %s", esp_err_to_name(err)); + + return status::StatusCode::Error; + } + + return updater_.begin(total_size, crc32); +} + +status::StatusCode SpiffsUpdater::write(const uint8_t* buf, size_t len) { + return updater_.write(buf, len); +} + +status::StatusCode SpiffsUpdater::commit() { + return updater_.commit(); +} + +status::StatusCode SpiffsUpdater::end() { + return updater_.end(); +} + +} // namespace system +} // namespace ocs diff --git a/components/ocs_system/target_esp32/spiffs_updater.h b/components/ocs_system/target_esp32/spiffs_updater.h new file mode 100644 index 00000000..41019aa9 --- /dev/null +++ b/components/ocs_system/target_esp32/spiffs_updater.h @@ -0,0 +1,44 @@ +/* + * SPDX-FileCopyrightText: 2025 Tendry Lab + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +#include "ocs_core/noncopyable.h" +#include "ocs_system/iupdater.h" + +namespace ocs { +namespace system { + +class SpiffsUpdater : public IUpdater, private core::NonCopyable<> { +public: + //! Initialize. + //! + //! @params + //! - @p updater - to perform the actual update process. + //! - @p partition_label - SPIFFS partition label. + SpiffsUpdater(IUpdater& updater, const char* partition_label); + + //! Unmount SPIFFS partition and begin the firmware update process. + status::StatusCode begin(size_t total_size, uint32_t crc32) override; + + //! Write firmware data. + status::StatusCode write(const uint8_t* buf, size_t len) override; + + //! Commit firmware update. + status::StatusCode commit() override; + + //! End firmware update. + status::StatusCode end() override; + +private: + const std::string partition_label_; + + IUpdater& updater_; +}; + +} // namespace system +} // namespace ocs