CreditMigrate.ino 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. CreditMigrate.ino
  3. Copyright (c) 2019, Hieromon Ikasamo
  4. https://github.com/Hieromon/AutoConnect
  5. This software is released under the MIT License.
  6. https://opensource.org/licenses/MIT
  7. This sketch migrates the credentials past saved to EEPROM in ESP32 to
  8. Preferences.
  9. */
  10. #ifndef ARDUINO_ARCH_ESP32
  11. #error This sketch should be compiled with the board of ESP32.
  12. #endif
  13. #include <Arduino.h>
  14. #include <EEPROM.h>
  15. #include <esp_partition.h>
  16. #include <AutoConnectCredential.h>
  17. #include <string.h>
  18. /**
  19. * Retrieve saved credentials from eeprom partition.
  20. * @param size Returns a size of the eeprom partition
  21. * @return Retrieved data buffered pointer
  22. */
  23. uint8_t* retrievePartition(const char* name, size_t *size) {
  24. const esp_partition_t* eeprom = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, name);
  25. if (!eeprom) {
  26. Serial.printf("%s partition not found\n", name);
  27. return nullptr;
  28. }
  29. uint8_t* pBuf = (uint8_t*)malloc(eeprom->size);
  30. if (!pBuf) {
  31. Serial.printf("Insufficient memory to retrieve %s partition\n", name);
  32. return nullptr;
  33. }
  34. if (esp_partition_read(eeprom, 0, (void*)pBuf, eeprom->size) != ESP_OK) {
  35. Serial.printf("Unable to read %s partition\n", name);
  36. free(pBuf);
  37. return nullptr;
  38. }
  39. *size = eeprom->size;
  40. return pBuf;
  41. }
  42. /**
  43. * Write credentials in retrieved buffer to Preferences.
  44. * @param eeprom Retrieved data buffered pointer
  45. * @param size Retrieved data size
  46. */
  47. void convert(const uint8_t* eeprom, const size_t size) {
  48. uint8_t* ac_credt = (uint8_t*)strstr((const char*)eeprom, "AC_CREDT");
  49. if (!ac_credt)
  50. Serial.println("AC_CREDT identifier not found in the partition.");
  51. else {
  52. AutoConnectCredential credential;
  53. uint8_t* bp = ac_credt + sizeof("AC_CREDT") - sizeof('\0');
  54. uint8_t* dp = bp;
  55. uint8_t entries = *dp++;
  56. size_t dpSize = *dp++;
  57. dpSize += *dp++ << 8;
  58. Serial.printf("%d stored credential(s),size:%d\n", (int)entries, dpSize);
  59. // Start EEPROM to Preferences migration
  60. uint8_t* ep = dp + dpSize - 1;
  61. for (int ec = 1; dp <= ep; ec++) {
  62. // Skip erased entry
  63. while (*dp == 0xff) {
  64. if (++dp > ep)
  65. break;
  66. }
  67. if (dp > ep) // It reached at the end of the credential region.
  68. break;
  69. // Obtain each entry and store to Preferences
  70. station_config_t config;
  71. Serial.printf("[%d] ", ec);
  72. uint8_t ei = 0;
  73. do {
  74. config.ssid[ei++] = *dp;
  75. } while (*dp++);
  76. Serial.print((char*)config.ssid);
  77. ei = 0;
  78. do {
  79. config.password[ei++] = *dp;
  80. } while (*dp++);
  81. Serial.printf("(%s)", config.password);
  82. for (ei = 0; ei < sizeof(config.bssid); ei++) {
  83. config.bssid[ei] = *dp++;
  84. Serial.printf(":%02x", config.bssid[ei]);
  85. }
  86. config.dhcp = *dp++;
  87. if (config.dhcp == STA_STATIC) {
  88. for (uint8_t e = 0; e < sizeof(station_config_t::_config::addr) / sizeof(uint32_t); e++) {
  89. uint32_t* ip = &config.config.addr[e];
  90. *ip = 0;
  91. for (uint8_t b = 0; b < sizeof(uint32_t); b++) {
  92. *ip <<= 8;
  93. *ip += *dp++;
  94. }
  95. }
  96. }
  97. bool rc = credential.save(&config);
  98. Serial.println(rc ? " transferred" : " failed to save Preferences");
  99. }
  100. }
  101. }
  102. void setup() {
  103. delay(1000);
  104. Serial.begin(115200);
  105. Serial.println();
  106. size_t eepromSize;
  107. uint8_t* eepromData = retrievePartition("eeprom", &eepromSize);
  108. if (eepromData) {
  109. Serial.println("Start migration to Preferences");
  110. convert(eepromData, eepromSize);
  111. Serial.println("Transfer ended");
  112. free(eepromData);
  113. }
  114. }
  115. void loop() {}