// Probe: where does the Wi-Fi password end up in flash?
// ESP32-S3, arduino-esp32 3.3.12, Arduino IDE default settings.
//
// On the test bench a script generated these defines (as config.h) for
// every run, with a fresh random SSID and password each time. Here they
// are inline so the sketch builds on its own: pick a MODE below.
//
// After a run, read the flash and search for the password, for example:
//   esptool --port /dev/ttyACM0 read-flash 0x9000 0x5000 nvs.bin
//   strings nvs.bin
//
// The passwords below are made up. Do not put a real one in a test sketch:
// a password written in the sketch also ends up in the firmware.
#include <WiFi.h>
#include <nvs_flash.h>

#define MODE_CONST            1  // WiFi.begin() with credentials written in the sketch
#define MODE_NOPERSIST        2  // the same, after WiFi.persistent(false)
#define MODE_SERIAL           3  // credentials arrive over Serial, like from a setup page
#define MODE_DISCONNECT_ERASE 4  // WiFi.disconnect(true, true)
#define MODE_ERASEAP          5  // WiFi.eraseAP()
#define MODE_NOWIFI           6  // an unrelated sketch, no Wi-Fi at all
#define MODE_NVSERASE         7  // nvs_flash_erase() + nvs_flash_init()

#ifndef MODE
#define MODE MODE_CONST
#endif

#define PROBE_SSID "HomeNet-5G"
#define PROBE_PASS "blue-lemon-42"

#if MODE == MODE_CONST || MODE == MODE_NOPERSIST
const char* ssid = PROBE_SSID;
const char* pass = PROBE_PASS;
#endif

static String readLine() {
  String s;
  while (true) {
    while (!Serial.available()) delay(5);
    char c = Serial.read();
    if (c == '\n') return s;
    if (c != '\r') s += c;
  }
}

void setup() {
  Serial.begin(115200);
  delay(1500);
  Serial.printf("MODE=%d\n", MODE);

#if MODE == MODE_CONST
  WiFi.begin(ssid, pass);
  delay(3000);
#elif MODE == MODE_NOPERSIST
  WiFi.persistent(false);
  WiFi.begin(ssid, pass);
  delay(3000);
#elif MODE == MODE_SERIAL
  // Credentials are not in the firmware: they arrive at runtime,
  // like from a provisioning page. Send the SSID, then the password,
  // then DONE. On the bench each network was set in a separate boot
  // (the board was reset between networks), the way a device is
  // reconfigured in real life.
  while (true) {
    Serial.println("SEND SSID");
    String s = readLine();
    if (s == "DONE") break;
    Serial.println("SEND PASS");
    String p = readLine();
    bool ok = WiFi.begin(s.c_str(), p.c_str());
    Serial.printf("begin(%s)=%d\n", s.c_str(), ok);
    delay(3000);
  }
#elif MODE == MODE_DISCONNECT_ERASE
  WiFi.mode(WIFI_STA);
  delay(500);
  bool ok = WiFi.disconnect(true, true);   // wifioff, eraseap
  Serial.printf("disconnect(true,true)=%d\n", ok);
  delay(1000);
#elif MODE == MODE_ERASEAP
  WiFi.mode(WIFI_STA);
  delay(500);
  bool ok = WiFi.eraseAP();
  Serial.printf("eraseAP()=%d\n", ok);
  delay(1000);
#elif MODE == MODE_NVSERASE
  // No Wi-Fi started: erase the whole default NVS partition.
  esp_err_t e = nvs_flash_erase();
  Serial.printf("nvs_flash_erase()=%d\n", (int)e);
  e = nvs_flash_init();
  Serial.printf("nvs_flash_init()=%d\n", (int)e);
#elif MODE == MODE_NOWIFI
  // Unrelated sketch, no Wi-Fi at all.
#endif

  Serial.println("=== END ===");
}

void loop() { delay(1000); }
