mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 23:21:54 +00:00 
			
		
		
		
	Merge branch 'integration' into memory_api
This commit is contained in:
		| @@ -12,7 +12,7 @@ extern "C" { | |||||||
| #include "preferences.h" | #include "preferences.h" | ||||||
|  |  | ||||||
| #include <cstring> | #include <cstring> | ||||||
| #include <vector> | #include <memory> | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace esp8266 { | namespace esp8266 { | ||||||
| @@ -67,6 +67,8 @@ static uint32_t get_esp8266_flash_sector() { | |||||||
| } | } | ||||||
| static uint32_t get_esp8266_flash_address() { return get_esp8266_flash_sector() * SPI_FLASH_SEC_SIZE; } | static uint32_t get_esp8266_flash_address() { return get_esp8266_flash_sector() * SPI_FLASH_SEC_SIZE; } | ||||||
|  |  | ||||||
|  | static inline size_t bytes_to_words(size_t bytes) { return (bytes + 3) / 4; } | ||||||
|  |  | ||||||
| template<class It> uint32_t calculate_crc(It first, It last, uint32_t type) { | template<class It> uint32_t calculate_crc(It first, It last, uint32_t type) { | ||||||
|   uint32_t crc = type; |   uint32_t crc = type; | ||||||
|   while (first != last) { |   while (first != last) { | ||||||
| @@ -123,41 +125,36 @@ class ESP8266PreferenceBackend : public ESPPreferenceBackend { | |||||||
|   size_t length_words = 0; |   size_t length_words = 0; | ||||||
|  |  | ||||||
|   bool save(const uint8_t *data, size_t len) override { |   bool save(const uint8_t *data, size_t len) override { | ||||||
|     if ((len + 3) / 4 != length_words) { |     if (bytes_to_words(len) != length_words) { | ||||||
|       return false; |       return false; | ||||||
|     } |     } | ||||||
|     std::vector<uint32_t> buffer; |     size_t buffer_size = length_words + 1; | ||||||
|     buffer.resize(length_words + 1); |     std::unique_ptr<uint32_t[]> buffer(new uint32_t[buffer_size]());  // Note the () for zero-initialization | ||||||
|     memcpy(buffer.data(), data, len); |     memcpy(buffer.get(), data, len); | ||||||
|     buffer[buffer.size() - 1] = calculate_crc(buffer.begin(), buffer.end() - 1, type); |     buffer[length_words] = calculate_crc(buffer.get(), buffer.get() + length_words, type); | ||||||
|  |  | ||||||
|     if (in_flash) { |     if (in_flash) { | ||||||
|       return save_to_flash(offset, buffer.data(), buffer.size()); |       return save_to_flash(offset, buffer.get(), buffer_size); | ||||||
|     } else { |  | ||||||
|       return save_to_rtc(offset, buffer.data(), buffer.size()); |  | ||||||
|     } |     } | ||||||
|  |     return save_to_rtc(offset, buffer.get(), buffer_size); | ||||||
|   } |   } | ||||||
|   bool load(uint8_t *data, size_t len) override { |   bool load(uint8_t *data, size_t len) override { | ||||||
|     if ((len + 3) / 4 != length_words) { |     if (bytes_to_words(len) != length_words) { | ||||||
|       return false; |       return false; | ||||||
|     } |     } | ||||||
|     std::vector<uint32_t> buffer; |     size_t buffer_size = length_words + 1; | ||||||
|     buffer.resize(length_words + 1); |     std::unique_ptr<uint32_t[]> buffer(new uint32_t[buffer_size]()); | ||||||
|     bool ret; |     bool ret = in_flash ? load_from_flash(offset, buffer.get(), buffer_size) | ||||||
|     if (in_flash) { |                         : load_from_rtc(offset, buffer.get(), buffer_size); | ||||||
|       ret = load_from_flash(offset, buffer.data(), buffer.size()); |  | ||||||
|     } else { |  | ||||||
|       ret = load_from_rtc(offset, buffer.data(), buffer.size()); |  | ||||||
|     } |  | ||||||
|     if (!ret) |     if (!ret) | ||||||
|       return false; |       return false; | ||||||
|  |  | ||||||
|     uint32_t crc = calculate_crc(buffer.begin(), buffer.end() - 1, type); |     uint32_t crc = calculate_crc(buffer.get(), buffer.get() + length_words, type); | ||||||
|     if (buffer[buffer.size() - 1] != crc) { |     if (buffer[length_words] != crc) { | ||||||
|       return false; |       return false; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     memcpy(data, buffer.data(), len); |     memcpy(data, buffer.get(), len); | ||||||
|     return true; |     return true; | ||||||
|   } |   } | ||||||
| }; | }; | ||||||
| @@ -178,7 +175,7 @@ class ESP8266Preferences : public ESPPreferences { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash) override { |   ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash) override { | ||||||
|     uint32_t length_words = (length + 3) / 4; |     uint32_t length_words = bytes_to_words(length); | ||||||
|     if (in_flash) { |     if (in_flash) { | ||||||
|       uint32_t start = current_flash_offset; |       uint32_t start = current_flash_offset; | ||||||
|       uint32_t end = start + length_words + 1; |       uint32_t end = start + length_words + 1; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user