From fbfd693f5d69cf8f14cbfacb8326b8a2a85d0134 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Tue, 10 Sep 2019 18:05:18 +0900 Subject: [PATCH] Added CreditMigrate.ino --- .travis.yml | 1 + README.md | 3 + examples/CreditMigrate/CreditMigrate.ino | 115 +++++++++++++++++++++++ library.json | 2 +- library.properties | 2 +- mkdocs/changelog.md | 3 + 6 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 examples/CreditMigrate/CreditMigrate.ino diff --git a/.travis.yml b/.travis.yml index c994c17..58d23d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -34,6 +34,7 @@ install: script: - buildExampleSketch ConfigIP - buildExampleSketch Credential + - if [[ "$BOARD" =~ "esp32:esp32:" ]]; then buildExampleSketch CreditMigrate; fi - buildExampleSketch Elements - buildExampleSketch FileUpload - buildExampleSketch FSBrowser diff --git a/README.md b/README.md index 7a0c4d9..047c849 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,9 @@ Full documentation is available on https://Hieromon.github.io/AutoConnect, some ## Change log +### [1.0.1] Sept. 10, 2019 +- Added a sketch for ESP32 boards that migrates credentials stored in EEPROM partition to the Preferences. + ### [1.0.0] Sept. 7, 2019 - Supports Arduino core for ESP32 1.0.3. - Supports AutoConnectUpdate for the OTA update. diff --git a/examples/CreditMigrate/CreditMigrate.ino b/examples/CreditMigrate/CreditMigrate.ino new file mode 100644 index 0000000..c700bf6 --- /dev/null +++ b/examples/CreditMigrate/CreditMigrate.ino @@ -0,0 +1,115 @@ +/* + CreditMigrate.ino + Copyright (c) 2019, Hieromon Ikasamo + https://github.com/Hieromon/AutoConnect + This software is released under the MIT License. + https://opensource.org/licenses/MIT + This sketch migrates the credentials past saved to EEPROM in ESP32 to + Preferences. + */ + +#ifndef ARDUINO_ARCH_ESP32 +#error This sketch should be compiled with the board of ESP32. +#endif + +#include +#include +#include +#include +#include + +/** + * Retrieve saved credentials from eeprom partition. + * @param size Returns a size of the eeprom partition + * @return Retrieved data buffered pointer + */ +uint8_t* retrievePartition(size_t *size) { + const esp_partition_t* eeprom = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "eeprom"); + + if (!eeprom) { + Serial.println("EEPROM partition not found"); + return nullptr; + } + uint8_t* pBuf = (uint8_t*)malloc(eeprom->size); + if (!pBuf) { + Serial.println("Insufficient memory to retrieve EEPROM partition"); + return nullptr; + } + if (esp_partition_read(eeprom, 0, (void*)pBuf, eeprom->size) != ESP_OK) { + Serial.println("Unable to read EEPROM partition"); + free(pBuf); + return nullptr; + } + *size = eeprom->size; + return pBuf; +} + +/** + * Write credentials in retrieved buffer to Preferences. + * @param eeprom Retrieved data buffered pointer + * @param size Retrieved data size + */ +void convert(uint8_t* eeprom, size_t size) { + uint8_t* ac_credt = (uint8_t*)strstr((const char*)eeprom, "AC_CREDT"); + if (!ac_credt) + Serial.println("AC_CREDT identifier not found in EEPROM partition."); + else { + AutoConnectCredential credential; + uint8_t* bp = ac_credt + sizeof("AC_CREDT") - 1; + uint8_t* dp = bp; + uint8_t entries = *dp++; + size_t dpSize = *dp++; + dpSize += *dp++ << 8; + Serial.printf("%d stored credential(s), size:%d\n", (int)entries, dpSize); + + // Start EEPROM to Preferences migration + uint8_t* ep = dp + dpSize - 1; + for (int ec = 1; dp <= ep; ec++) { + + // Skip erased entry + while (*dp == 0xff) { + if (++dp > ep) + break; + } + if (dp > ep) // It reached at the end of the credential region. + break; + + // Obtain each entry and store to Preferences + struct station_config config; + Serial.printf("[%d] ", ec); + uint8_t ei = 0; + do { + config.ssid[ei++] = *dp; + } while (*dp++); + Serial.print((char*)config.ssid); + ei = 0; + do { + config.password[ei++] = *dp; + } while (*dp++); + Serial.printf("(%s)", config.password); + for (ei = 0; ei < sizeof(config.bssid); ei++) { + config.bssid[ei] = *dp++; + Serial.printf(":%02x", config.bssid[ei]); + } + bool rc = credential.save(&config); + Serial.println(rc ? " saved" : " failed to save"); + } + } +} + +void setup() { + delay(1000); + Serial.begin(115200); + Serial.println(); + + size_t eepromSize; + uint8_t* eepromData = retrievePartition(&eepromSize); + if (eepromData) { + Serial.println("Start EEPROM migration to Preferences"); + convert(eepromData, eepromSize); + Serial.println("Conversion ended"); + free(eepromData); + } +} + +void loop() {} diff --git a/library.json b/library.json index d9d48b2..b2f9f6b 100644 --- a/library.json +++ b/library.json @@ -25,6 +25,6 @@ "espressif8266", "espressif32" ], - "version": "1.0.0", + "version": "1.0.1", "license": "MIT" } diff --git a/library.properties b/library.properties index d54e9d6..58bb112 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=AutoConnect -version=1.0.0 +version=1.0.1 author=Hieromon Ikasamo maintainer=Hieromon Ikasamo sentence=ESP8266/ESP32 WLAN configuration at runtime with web interface. diff --git a/mkdocs/changelog.md b/mkdocs/changelog.md index e30c56b..e66fb60 100644 --- a/mkdocs/changelog.md +++ b/mkdocs/changelog.md @@ -1,3 +1,6 @@ +#### [1.0.1] Sept. 10, 2019 +- Added a sketch for ESP32 boards that migrates credentials stored in EEPROM partition to the Preferences. + #### [1.0.0] Sept. 7, 2019 - Supports Arduino core for ESP32 1.0.3. - Supports AutoConnectUpdate for the [OTA update](otaupdate.md).