]> granicus.if.org Git - esp-idf/commitdiff
nvs_flash: emulator: fix issues in load function, add save function
authorIvan Grokhotkov <ivan@espressif.com>
Mon, 18 Sep 2017 14:43:03 +0000 (22:43 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Tue, 19 Sep 2017 02:39:44 +0000 (10:39 +0800)
components/nvs_flash/test_nvs_host/spi_flash_emulation.h

index 14e56bab6e46bfce1a1fc9e8b957e341b7877eac..d8099322d500958890a4905fd6433bcca20a3ea7 100644 (file)
@@ -148,10 +148,20 @@ public:
         fseek(f, 0, SEEK_END);
         off_t size = ftell(f);
         assert(size % SPI_FLASH_SEC_SIZE == 0);
-        mData.resize(size);
+        mData.resize(size / sizeof(uint32_t));
         fseek(f, 0, SEEK_SET);
         auto s = fread(mData.data(), SPI_FLASH_SEC_SIZE, size / SPI_FLASH_SEC_SIZE, f);
         assert(s == static_cast<size_t>(size / SPI_FLASH_SEC_SIZE));
+        fclose(f);
+    }
+    
+    void save(const char* filename)
+    {
+        FILE* f = fopen(filename, "wb");
+        auto n_sectors = mData.size() * sizeof(uint32_t) / SPI_FLASH_SEC_SIZE;
+        auto s = fwrite(mData.data(), SPI_FLASH_SEC_SIZE, n_sectors, f);
+        assert(s == n_sectors);
+        fclose(f);
     }
 
     void clearStats()