From d043480625114141254072d92e5485cbbf4c0785 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 10 Nov 2024 19:52:40 +0000 Subject: [PATCH 1/2] Restyled by astyle --- minigotchi-ESP32/config.cpp | 104 ++++++------ minigotchi-ESP32/minigotchi.cpp | 278 ++++++++++++++++---------------- minigotchi-ESP32/webui.cpp | 164 +++++++++---------- minigotchi-ESP32/webui.h | 14 +- 4 files changed, 282 insertions(+), 278 deletions(-) diff --git a/minigotchi-ESP32/config.cpp b/minigotchi-ESP32/config.cpp index a8b62bc..cd822a8 100644 --- a/minigotchi-ESP32/config.cpp +++ b/minigotchi-ESP32/config.cpp @@ -114,65 +114,65 @@ std::string Config::version = "3.4.2-beta"; * Loads configuration values from NVS */ void Config::loadConfig() { - nvs_handle_t cfgHandle; - esp_err_t err = nvs_open("storage", NVS_READWRITE, &cfgHandle); - if (err == ESP_OK) { - // load Config::configured - uint8_t configured = 0; - err = nvs_get_u8(cfgHandle, "configured", &configured); + nvs_handle_t cfgHandle; + esp_err_t err = nvs_open("storage", NVS_READWRITE, &cfgHandle); if (err == ESP_OK) { - Config::configured = (configured == 1); - } + // load Config::configured + uint8_t configured = 0; + err = nvs_get_u8(cfgHandle, "configured", &configured); + if (err == ESP_OK) { + Config::configured = (configured == 1); + } - // load Config::whitelist - size_t required_size = 0; - err = nvs_get_str(cfgHandle, "whitelist", NULL, &required_size); - if (err == ESP_OK && required_size > 0) { - char *whitelistStr = (char *)malloc(required_size); - err = nvs_get_str(cfgHandle, "whitelist", whitelistStr, &required_size); - if (err == ESP_OK) { - // convert back into a vector - Config::whitelist.clear(); - std::stringstream ss(whitelistStr); - std::string item; - while (std::getline(ss, item, ',')) { - Config::whitelist.push_back(item); + // load Config::whitelist + size_t required_size = 0; + err = nvs_get_str(cfgHandle, "whitelist", NULL, &required_size); + if (err == ESP_OK && required_size > 0) { + char *whitelistStr = (char *)malloc(required_size); + err = nvs_get_str(cfgHandle, "whitelist", whitelistStr, &required_size); + if (err == ESP_OK) { + // convert back into a vector + Config::whitelist.clear(); + std::stringstream ss(whitelistStr); + std::string item; + while (std::getline(ss, item, ',')) { + Config::whitelist.push_back(item); + } + } + free(whitelistStr); } - } - free(whitelistStr); - } - nvs_close(cfgHandle); - } + nvs_close(cfgHandle); + } } /** * Saves configuration to NVS */ void Config::saveConfig() { - nvs_handle_t cfgHandle; - esp_err_t err = nvs_open("storage", NVS_READWRITE, &cfgHandle); - if (err == ESP_OK) { - // save Config::configured - uint8_t configured = Config::configured ? 1 : 0; - err = nvs_set_u8(cfgHandle, "configured", configured); - ESP_ERROR_CHECK(err); - - // save Config::whitelist - std::string whitelistStr; - for (size_t i = 0; i < Config::whitelist.size(); ++i) { - whitelistStr += Config::whitelist[i]; - if (i < Config::whitelist.size() - 1) { - whitelistStr += ","; - } - } - err = nvs_set_str(cfgHandle, "whitelist", whitelistStr.c_str()); - ESP_ERROR_CHECK(err); + nvs_handle_t cfgHandle; + esp_err_t err = nvs_open("storage", NVS_READWRITE, &cfgHandle); + if (err == ESP_OK) { + // save Config::configured + uint8_t configured = Config::configured ? 1 : 0; + err = nvs_set_u8(cfgHandle, "configured", configured); + ESP_ERROR_CHECK(err); + + // save Config::whitelist + std::string whitelistStr; + for (size_t i = 0; i < Config::whitelist.size(); ++i) { + whitelistStr += Config::whitelist[i]; + if (i < Config::whitelist.size() - 1) { + whitelistStr += ","; + } + } + err = nvs_set_str(cfgHandle, "whitelist", whitelistStr.c_str()); + ESP_ERROR_CHECK(err); - err = nvs_commit(cfgHandle); - ESP_ERROR_CHECK(err); - nvs_close(cfgHandle); - } + err = nvs_commit(cfgHandle); + ESP_ERROR_CHECK(err); + nvs_close(cfgHandle); + } } /** developer note: @@ -187,9 +187,13 @@ void Config::saveConfig() { * @param min Lowest number * @param max Highest number */ -int Config::random(int min, int max) { return min + rand() % (max - min + 1); } +int Config::random(int min, int max) { + return min + rand() % (max - min + 1); +} /** * Checks current uptime */ -int Config::time() { return millis() / 1000; } +int Config::time() { + return millis() / 1000; +} diff --git a/minigotchi-ESP32/minigotchi.cpp b/minigotchi-ESP32/minigotchi.cpp index 0be710a..06dbbea 100644 --- a/minigotchi-ESP32/minigotchi.cpp +++ b/minigotchi-ESP32/minigotchi.cpp @@ -44,186 +44,186 @@ int Minigotchi::currentEpoch = 0; * WebUI task for freeRTOS */ void Minigotchi::WebUITask(void *pvParameters) { - // setup web server - WebUI *web = new WebUI(); + // setup web server + WebUI *web = new WebUI(); - // hang until something cool happens (this is not cool trust me) - while (!Config::configured) { - taskYIELD(); // wait - } + // hang until something cool happens (this is not cool trust me) + while (!Config::configured) { + taskYIELD(); // wait + } - // clean up when done - delete web; - web = nullptr; // crowdstrike forgot about this one lol + // clean up when done + delete web; + web = nullptr; // crowdstrike forgot about this one lol - // delete task - vTaskDelete(NULL); + // delete task + vTaskDelete(NULL); } /** * Wait for WebUI to get input that the configuration is done */ void Minigotchi::waitForInput() { - // on core one - if (!Config::configured) { - xTaskCreatePinnedToCore(WebUITask, "WebUI Task", 8192, NULL, 1, NULL, 1); - } - - // wait until it's done - while (!Config::configured) { - delay(1); - } + // on core one + if (!Config::configured) { + xTaskCreatePinnedToCore(WebUITask, "WebUI Task", 8192, NULL, 1, NULL, 1); + } + + // wait until it's done + while (!Config::configured) { + delay(1); + } } /** * Increment/increase current epoch by one */ int Minigotchi::addEpoch() { - Minigotchi::currentEpoch++; - return Minigotchi::currentEpoch; + Minigotchi::currentEpoch++; + return Minigotchi::currentEpoch; } /** * Show current Minigotchi epoch */ void Minigotchi::epoch() { - Minigotchi::addEpoch(); - Parasite::readData(); - Serial.print(mood.getNeutral() + " Current Epoch: "); - Serial.println(Minigotchi::currentEpoch); - Serial.println(" "); - Display::updateDisplay(mood.getNeutral(), - "Current Epoch: " + Minigotchi::currentEpoch); + Minigotchi::addEpoch(); + Parasite::readData(); + Serial.print(mood.getNeutral() + " Current Epoch: "); + Serial.println(Minigotchi::currentEpoch); + Serial.println(" "); + Display::updateDisplay(mood.getNeutral(), + "Current Epoch: " + Minigotchi::currentEpoch); } /** * Things to do on startup */ void Minigotchi::boot() { - // configure moods - Mood::init(Config::happy, Config::sad, Config::broken, Config::intense, - Config::looking1, Config::looking2, Config::neutral, - Config::sleeping); - - // StickC Plus 1.1 and 2 power management, to keep turned On after unplug USB - // cable - if (Config::screen == "M5STICKCP") { - AXP192 axp192; - axp192.begin(); // Use the instance of AXP192 - axp192.ScreenBreath(100); // Use the instance of AXP192 - } else if (Config::screen == "M5STICKCP2") { - pinMode(4, OUTPUT); - digitalWrite(4, HIGH); - } - - Display::startScreen(); - Serial.println(" "); - Serial.println(mood.getHappy() + - " Hi, I'm Minigotchi, your pwnagotchi's best friend!"); - Display::updateDisplay(mood.getHappy(), "Hi, I'm Minigotchi"); - Serial.println(" "); - Serial.println(mood.getNeutral() + - " You can edit my configuration parameters in config.cpp!"); - Serial.println(" "); - delay(Config::shortDelay); - Display::updateDisplay(mood.getNeutral(), "Edit my config.cpp!"); - delay(Config::shortDelay); - Serial.println(mood.getIntense() + " Starting now..."); - Serial.println(" "); - Display::updateDisplay(mood.getIntense(), "Starting now"); - delay(Config::shortDelay); - Serial.println("################################################"); - Serial.println("# BOOTUP PROCESS #"); - Serial.println("################################################"); - Serial.println(" "); - ESP_ERROR_CHECK(esp_wifi_init(&Config::wifiCfg)); - esp_err_t err = nvs_flash_init(); - if (err == ESP_ERR_NVS_NO_FREE_PAGES || - err == ESP_ERR_NVS_NEW_VERSION_FOUND) { - ESP_ERROR_CHECK(nvs_flash_erase()); - err = nvs_flash_init(); - } - - Config::loadConfig(); - ESP_ERROR_CHECK(err); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - ESP_ERROR_CHECK(esp_wifi_set_country(&Config::ctryCfg)); - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_start()); - - // wait for the webui configuration - if (!Config::configured) { - waitForInput(); - } - - Deauth::list(); - Channel::init(Config::channel); - Minigotchi::info(); - Parasite::sendName(); - Minigotchi::finish(); + // configure moods + Mood::init(Config::happy, Config::sad, Config::broken, Config::intense, + Config::looking1, Config::looking2, Config::neutral, + Config::sleeping); + + // StickC Plus 1.1 and 2 power management, to keep turned On after unplug USB + // cable + if (Config::screen == "M5STICKCP") { + AXP192 axp192; + axp192.begin(); // Use the instance of AXP192 + axp192.ScreenBreath(100); // Use the instance of AXP192 + } else if (Config::screen == "M5STICKCP2") { + pinMode(4, OUTPUT); + digitalWrite(4, HIGH); + } + + Display::startScreen(); + Serial.println(" "); + Serial.println(mood.getHappy() + + " Hi, I'm Minigotchi, your pwnagotchi's best friend!"); + Display::updateDisplay(mood.getHappy(), "Hi, I'm Minigotchi"); + Serial.println(" "); + Serial.println(mood.getNeutral() + + " You can edit my configuration parameters in config.cpp!"); + Serial.println(" "); + delay(Config::shortDelay); + Display::updateDisplay(mood.getNeutral(), "Edit my config.cpp!"); + delay(Config::shortDelay); + Serial.println(mood.getIntense() + " Starting now..."); + Serial.println(" "); + Display::updateDisplay(mood.getIntense(), "Starting now"); + delay(Config::shortDelay); + Serial.println("################################################"); + Serial.println("# BOOTUP PROCESS #"); + Serial.println("################################################"); + Serial.println(" "); + ESP_ERROR_CHECK(esp_wifi_init(&Config::wifiCfg)); + esp_err_t err = nvs_flash_init(); + if (err == ESP_ERR_NVS_NO_FREE_PAGES || + err == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_ERROR_CHECK(nvs_flash_erase()); + err = nvs_flash_init(); + } + + Config::loadConfig(); + ESP_ERROR_CHECK(err); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + ESP_ERROR_CHECK(esp_wifi_set_country(&Config::ctryCfg)); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_start()); + + // wait for the webui configuration + if (!Config::configured) { + waitForInput(); + } + + Deauth::list(); + Channel::init(Config::channel); + Minigotchi::info(); + Parasite::sendName(); + Minigotchi::finish(); } /** * Show current Minigotchi info/stats */ void Minigotchi::info() { - delay(Config::shortDelay); - Serial.println(" "); - Serial.println(mood.getNeutral() + " Current Minigotchi Stats: "); - Display::updateDisplay(mood.getNeutral(), "Current Minigotchi Stats:"); - version(); - mem(); - cpu(); - Serial.println(" "); - delay(Config::shortDelay); + delay(Config::shortDelay); + Serial.println(" "); + Serial.println(mood.getNeutral() + " Current Minigotchi Stats: "); + Display::updateDisplay(mood.getNeutral(), "Current Minigotchi Stats:"); + version(); + mem(); + cpu(); + Serial.println(" "); + delay(Config::shortDelay); } /** * This is printed after everything is done in the bootup process */ void Minigotchi::finish() { - Serial.println("################################################"); - Serial.println(" "); - Serial.println(mood.getNeutral() + " Started successfully!"); - Serial.println(" "); - Display::updateDisplay(mood.getNeutral(), "Started sucessfully"); - delay(Config::shortDelay); + Serial.println("################################################"); + Serial.println(" "); + Serial.println(mood.getNeutral() + " Started successfully!"); + Serial.println(" "); + Display::updateDisplay(mood.getNeutral(), "Started sucessfully"); + delay(Config::shortDelay); } /** * Shows current Minigotchi version */ void Minigotchi::version() { - Serial.print(mood.getNeutral() + " Version: "); - Serial.println(Config::version.c_str()); - Display::updateDisplay(mood.getNeutral(), - "Version: " + (String)Config::version.c_str()); - delay(Config::shortDelay); + Serial.print(mood.getNeutral() + " Version: "); + Serial.println(Config::version.c_str()); + Display::updateDisplay(mood.getNeutral(), + "Version: " + (String)Config::version.c_str()); + delay(Config::shortDelay); } /** * Shows current Minigotchi memory usage */ void Minigotchi::mem() { - Serial.print(mood.getNeutral() + " Heap: "); - Serial.print(ESP.getFreeHeap()); - Serial.println(" bytes"); - Display::updateDisplay(mood.getNeutral(), - "Heap: " + (String)ESP.getFreeHeap() + " bytes"); - delay(Config::shortDelay); + Serial.print(mood.getNeutral() + " Heap: "); + Serial.print(ESP.getFreeHeap()); + Serial.println(" bytes"); + Display::updateDisplay(mood.getNeutral(), + "Heap: " + (String)ESP.getFreeHeap() + " bytes"); + delay(Config::shortDelay); } /** * Shows current Minigotchi Frequency */ void Minigotchi::cpu() { - Serial.print(mood.getNeutral() + " CPU Frequency: "); - Serial.print(ESP.getCpuFreqMHz()); - Serial.println(" MHz"); - Display::updateDisplay(mood.getNeutral(), - "CPU Frequency: " + (String)ESP.getCpuFreqMHz() + - " MHz"); - delay(Config::shortDelay); + Serial.print(mood.getNeutral() + " CPU Frequency: "); + Serial.print(ESP.getCpuFreqMHz()); + Serial.println(" MHz"); + Display::updateDisplay(mood.getNeutral(), + "CPU Frequency: " + (String)ESP.getCpuFreqMHz() + + " MHz"); + delay(Config::shortDelay); } /** developer note: @@ -247,22 +247,22 @@ void Minigotchi::cpu() { * Puts Minigotchi in promiscuous mode */ void Minigotchi::monStart() { - // disconnect from WiFi if we were at all - WiFi.disconnect(); + // disconnect from WiFi if we were at all + WiFi.disconnect(); - // revert to station mode - WiFi.mode(WIFI_STA); - esp_wifi_set_promiscuous(true); + // revert to station mode + WiFi.mode(WIFI_STA); + esp_wifi_set_promiscuous(true); } /** * Takes Minigotchi out of promiscuous mode */ void Minigotchi::monStop() { - esp_wifi_set_promiscuous(false); + esp_wifi_set_promiscuous(false); - // revert to station mode - WiFi.mode(WIFI_STA); + // revert to station mode + WiFi.mode(WIFI_STA); } /** developer note: @@ -288,30 +288,30 @@ void Minigotchi::monStop() { * Channel cycling */ void Minigotchi::cycle() { - Parasite::readData(); - Channel::cycle(); + Parasite::readData(); + Channel::cycle(); } /** * Pwnagotchi detection */ void Minigotchi::detect() { - Parasite::readData(); - Pwnagotchi::detect(); + Parasite::readData(); + Pwnagotchi::detect(); } /** * Deauthing */ void Minigotchi::deauth() { - Parasite::readData(); - Deauth::deauth(); + Parasite::readData(); + Deauth::deauth(); } /** * Advertising */ void Minigotchi::advertise() { - Parasite::readData(); - Frame::advertise(); + Parasite::readData(); + Frame::advertise(); } diff --git a/minigotchi-ESP32/webui.cpp b/minigotchi-ESP32/webui.cpp index e595da5..334089a 100644 --- a/minigotchi-ESP32/webui.cpp +++ b/minigotchi-ESP32/webui.cpp @@ -124,92 +124,92 @@ const char WebUI::html[] = R"rawliteral( // captive portal class, this isn't the main class though class CaptivePortalHandler : public AsyncWebHandler { public: - CaptivePortalHandler() {} - virtual ~CaptivePortalHandler() {} - - bool canHandle(AsyncWebServerRequest *request) { - return request->url() == "/"; - } - - void handleRequest(AsyncWebServerRequest *request) { - if (request->method() == HTTP_GET && request->url() == "/") { - request->send(200, "text/html", WebUI::html); - } else { - request->send(200, "text/html", WebUI::html); + CaptivePortalHandler() {} + virtual ~CaptivePortalHandler() {} + + bool canHandle(AsyncWebServerRequest *request) { + return request->url() == "/"; + } + + void handleRequest(AsyncWebServerRequest *request) { + if (request->method() == HTTP_GET && request->url() == "/") { + request->send(200, "text/html", WebUI::html); + } else { + request->send(200, "text/html", WebUI::html); + } } - } }; /** * Starts and constructs Web Server */ WebUI::WebUI() { - Serial.println(mood.getIntense() + " Starting Web Server..."); - Display::updateDisplay(mood.getIntense(), "Starting Web Server..."); + Serial.println(mood.getIntense() + " Starting Web Server..."); + Display::updateDisplay(mood.getIntense(), "Starting Web Server..."); - WiFi.mode(WIFI_AP); - WiFi.softAP(Config::ssid); + WiFi.mode(WIFI_AP); + WiFi.softAP(Config::ssid); - setupServer(); + setupServer(); - dnsServer.setErrorReplyCode(DNSReplyCode::NoError); - dnsServer.setTTL(300); - dnsServer.start(53, "*", WiFi.softAPIP()); + dnsServer.setErrorReplyCode(DNSReplyCode::NoError); + dnsServer.setTTL(300); + dnsServer.start(53, "*", WiFi.softAPIP()); - server.begin(); - WebUI::running = true; + server.begin(); + WebUI::running = true; - while (running) { - dnsServer.processNextRequest(); - } + while (running) { + dnsServer.processNextRequest(); + } } WebUI::~WebUI() { - // debugging - Serial.println("WebUI Destructor called"); + // debugging + Serial.println("WebUI Destructor called"); - // nah fuck it - dnsServer.stop(); - server.end(); - WiFi.softAPdisconnect(true); + // nah fuck it + dnsServer.stop(); + server.end(); + WiFi.softAPdisconnect(true); - running = false; + running = false; } /** * Sets up Web Server */ void WebUI::setupServer() { - server.addHandler(new CaptivePortalHandler()).setFilter(ON_AP_FILTER); - - // handle whitelist - server.on("/get", HTTP_GET, [](AsyncWebServerRequest *request) { - if (request->hasParam("whitelist")) { - String newWhitelist = request->getParam("whitelist")->value(); - updateWhitelist(newWhitelist); - request->send(200, "text/html", - mood.getHappy() + " Whitelist updated successfully!
Return to Home Page"); - } else if (request->hasParam("config")) { - String configValue = request->getParam("config")->value(); - Config::configured = (configValue == "true"); - Config::saveConfig(); - // Serial.println("Config check: " + String(Config::configured ? "true" : - // "false")); - request->send(200, "text/html", - mood.getHappy() + - " Configuration updated! You may exit this tab and " - "disconnect from the Wifi AP.
"); - } else { - request->send(200, "text/html", - mood.getBroken() + " No valid input received.
Return to Home Page"); - } - }); - - server.onNotFound([&](AsyncWebServerRequest *request) { - request->send(200, "text/html", html); - }); + server.addHandler(new CaptivePortalHandler()).setFilter(ON_AP_FILTER); + + // handle whitelist + server.on("/get", HTTP_GET, [](AsyncWebServerRequest *request) { + if (request->hasParam("whitelist")) { + String newWhitelist = request->getParam("whitelist")->value(); + updateWhitelist(newWhitelist); + request->send(200, "text/html", + mood.getHappy() + " Whitelist updated successfully!
Return to Home Page"); + } else if (request->hasParam("config")) { + String configValue = request->getParam("config")->value(); + Config::configured = (configValue == "true"); + Config::saveConfig(); + // Serial.println("Config check: " + String(Config::configured ? "true" : + // "false")); + request->send(200, "text/html", + mood.getHappy() + + " Configuration updated! You may exit this tab and " + "disconnect from the Wifi AP.
"); + } else { + request->send(200, "text/html", + mood.getBroken() + " No valid input received.
Return to Home Page"); + } + }); + + server.onNotFound([&](AsyncWebServerRequest *request) { + request->send(200, "text/html", html); + }); } /** @@ -217,23 +217,23 @@ void WebUI::setupServer() { * @param newWhiteList new whitelist to use */ void WebUI::updateWhitelist(String newWhitelist) { - Config::whitelist.clear(); // clear existing values - int start = 0; - int end = newWhitelist.indexOf(','); - - while (end != -1) { - Config::whitelist.push_back(newWhitelist.substring(start, end).c_str()); - start = end + 1; - end = newWhitelist.indexOf(',', start); - } - - // add last element after last comma - Config::whitelist.push_back(newWhitelist.substring(start).c_str()); - - /* - Serial.println(mood.getNeutral() + " Updated whitelist:"); - for (const auto &entry : Config::whitelist) { - Serial.println(entry.c_str()); - } - */ + Config::whitelist.clear(); // clear existing values + int start = 0; + int end = newWhitelist.indexOf(','); + + while (end != -1) { + Config::whitelist.push_back(newWhitelist.substring(start, end).c_str()); + start = end + 1; + end = newWhitelist.indexOf(',', start); + } + + // add last element after last comma + Config::whitelist.push_back(newWhitelist.substring(start).c_str()); + + /* + Serial.println(mood.getNeutral() + " Updated whitelist:"); + for (const auto &entry : Config::whitelist) { + Serial.println(entry.c_str()); + } + */ } diff --git a/minigotchi-ESP32/webui.h b/minigotchi-ESP32/webui.h index 67c11bd..3b72021 100644 --- a/minigotchi-ESP32/webui.h +++ b/minigotchi-ESP32/webui.h @@ -37,15 +37,15 @@ class Mood; // this is the actual class we use class WebUI { public: - WebUI(); - ~WebUI(); - static void setupServer(); - static void updateWhitelist(String newWhitelist); - static const char html[] PROGMEM; - static bool running; + WebUI(); + ~WebUI(); + static void setupServer(); + static void updateWhitelist(String newWhitelist); + static const char html[] PROGMEM; + static bool running; private: - static Mood &mood; + static Mood &mood; }; #endif // WEBUI_H From 10ad1e10301b93fa0b3b576174d9dace68e191a9 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 10 Nov 2024 19:52:45 +0000 Subject: [PATCH 2/2] Restyled by clang-format --- minigotchi-ESP32/config.cpp | 104 ++++++------ minigotchi-ESP32/minigotchi.cpp | 278 ++++++++++++++++---------------- minigotchi-ESP32/webui.cpp | 162 +++++++++---------- minigotchi-ESP32/webui.h | 14 +- 4 files changed, 277 insertions(+), 281 deletions(-) diff --git a/minigotchi-ESP32/config.cpp b/minigotchi-ESP32/config.cpp index cd822a8..a8b62bc 100644 --- a/minigotchi-ESP32/config.cpp +++ b/minigotchi-ESP32/config.cpp @@ -114,65 +114,65 @@ std::string Config::version = "3.4.2-beta"; * Loads configuration values from NVS */ void Config::loadConfig() { - nvs_handle_t cfgHandle; - esp_err_t err = nvs_open("storage", NVS_READWRITE, &cfgHandle); + nvs_handle_t cfgHandle; + esp_err_t err = nvs_open("storage", NVS_READWRITE, &cfgHandle); + if (err == ESP_OK) { + // load Config::configured + uint8_t configured = 0; + err = nvs_get_u8(cfgHandle, "configured", &configured); if (err == ESP_OK) { - // load Config::configured - uint8_t configured = 0; - err = nvs_get_u8(cfgHandle, "configured", &configured); - if (err == ESP_OK) { - Config::configured = (configured == 1); - } + Config::configured = (configured == 1); + } - // load Config::whitelist - size_t required_size = 0; - err = nvs_get_str(cfgHandle, "whitelist", NULL, &required_size); - if (err == ESP_OK && required_size > 0) { - char *whitelistStr = (char *)malloc(required_size); - err = nvs_get_str(cfgHandle, "whitelist", whitelistStr, &required_size); - if (err == ESP_OK) { - // convert back into a vector - Config::whitelist.clear(); - std::stringstream ss(whitelistStr); - std::string item; - while (std::getline(ss, item, ',')) { - Config::whitelist.push_back(item); - } - } - free(whitelistStr); + // load Config::whitelist + size_t required_size = 0; + err = nvs_get_str(cfgHandle, "whitelist", NULL, &required_size); + if (err == ESP_OK && required_size > 0) { + char *whitelistStr = (char *)malloc(required_size); + err = nvs_get_str(cfgHandle, "whitelist", whitelistStr, &required_size); + if (err == ESP_OK) { + // convert back into a vector + Config::whitelist.clear(); + std::stringstream ss(whitelistStr); + std::string item; + while (std::getline(ss, item, ',')) { + Config::whitelist.push_back(item); } - - nvs_close(cfgHandle); + } + free(whitelistStr); } + + nvs_close(cfgHandle); + } } /** * Saves configuration to NVS */ void Config::saveConfig() { - nvs_handle_t cfgHandle; - esp_err_t err = nvs_open("storage", NVS_READWRITE, &cfgHandle); - if (err == ESP_OK) { - // save Config::configured - uint8_t configured = Config::configured ? 1 : 0; - err = nvs_set_u8(cfgHandle, "configured", configured); - ESP_ERROR_CHECK(err); - - // save Config::whitelist - std::string whitelistStr; - for (size_t i = 0; i < Config::whitelist.size(); ++i) { - whitelistStr += Config::whitelist[i]; - if (i < Config::whitelist.size() - 1) { - whitelistStr += ","; - } - } - err = nvs_set_str(cfgHandle, "whitelist", whitelistStr.c_str()); - ESP_ERROR_CHECK(err); - - err = nvs_commit(cfgHandle); - ESP_ERROR_CHECK(err); - nvs_close(cfgHandle); + nvs_handle_t cfgHandle; + esp_err_t err = nvs_open("storage", NVS_READWRITE, &cfgHandle); + if (err == ESP_OK) { + // save Config::configured + uint8_t configured = Config::configured ? 1 : 0; + err = nvs_set_u8(cfgHandle, "configured", configured); + ESP_ERROR_CHECK(err); + + // save Config::whitelist + std::string whitelistStr; + for (size_t i = 0; i < Config::whitelist.size(); ++i) { + whitelistStr += Config::whitelist[i]; + if (i < Config::whitelist.size() - 1) { + whitelistStr += ","; + } } + err = nvs_set_str(cfgHandle, "whitelist", whitelistStr.c_str()); + ESP_ERROR_CHECK(err); + + err = nvs_commit(cfgHandle); + ESP_ERROR_CHECK(err); + nvs_close(cfgHandle); + } } /** developer note: @@ -187,13 +187,9 @@ void Config::saveConfig() { * @param min Lowest number * @param max Highest number */ -int Config::random(int min, int max) { - return min + rand() % (max - min + 1); -} +int Config::random(int min, int max) { return min + rand() % (max - min + 1); } /** * Checks current uptime */ -int Config::time() { - return millis() / 1000; -} +int Config::time() { return millis() / 1000; } diff --git a/minigotchi-ESP32/minigotchi.cpp b/minigotchi-ESP32/minigotchi.cpp index 06dbbea..0be710a 100644 --- a/minigotchi-ESP32/minigotchi.cpp +++ b/minigotchi-ESP32/minigotchi.cpp @@ -44,186 +44,186 @@ int Minigotchi::currentEpoch = 0; * WebUI task for freeRTOS */ void Minigotchi::WebUITask(void *pvParameters) { - // setup web server - WebUI *web = new WebUI(); + // setup web server + WebUI *web = new WebUI(); - // hang until something cool happens (this is not cool trust me) - while (!Config::configured) { - taskYIELD(); // wait - } + // hang until something cool happens (this is not cool trust me) + while (!Config::configured) { + taskYIELD(); // wait + } - // clean up when done - delete web; - web = nullptr; // crowdstrike forgot about this one lol + // clean up when done + delete web; + web = nullptr; // crowdstrike forgot about this one lol - // delete task - vTaskDelete(NULL); + // delete task + vTaskDelete(NULL); } /** * Wait for WebUI to get input that the configuration is done */ void Minigotchi::waitForInput() { - // on core one - if (!Config::configured) { - xTaskCreatePinnedToCore(WebUITask, "WebUI Task", 8192, NULL, 1, NULL, 1); - } - - // wait until it's done - while (!Config::configured) { - delay(1); - } + // on core one + if (!Config::configured) { + xTaskCreatePinnedToCore(WebUITask, "WebUI Task", 8192, NULL, 1, NULL, 1); + } + + // wait until it's done + while (!Config::configured) { + delay(1); + } } /** * Increment/increase current epoch by one */ int Minigotchi::addEpoch() { - Minigotchi::currentEpoch++; - return Minigotchi::currentEpoch; + Minigotchi::currentEpoch++; + return Minigotchi::currentEpoch; } /** * Show current Minigotchi epoch */ void Minigotchi::epoch() { - Minigotchi::addEpoch(); - Parasite::readData(); - Serial.print(mood.getNeutral() + " Current Epoch: "); - Serial.println(Minigotchi::currentEpoch); - Serial.println(" "); - Display::updateDisplay(mood.getNeutral(), - "Current Epoch: " + Minigotchi::currentEpoch); + Minigotchi::addEpoch(); + Parasite::readData(); + Serial.print(mood.getNeutral() + " Current Epoch: "); + Serial.println(Minigotchi::currentEpoch); + Serial.println(" "); + Display::updateDisplay(mood.getNeutral(), + "Current Epoch: " + Minigotchi::currentEpoch); } /** * Things to do on startup */ void Minigotchi::boot() { - // configure moods - Mood::init(Config::happy, Config::sad, Config::broken, Config::intense, - Config::looking1, Config::looking2, Config::neutral, - Config::sleeping); - - // StickC Plus 1.1 and 2 power management, to keep turned On after unplug USB - // cable - if (Config::screen == "M5STICKCP") { - AXP192 axp192; - axp192.begin(); // Use the instance of AXP192 - axp192.ScreenBreath(100); // Use the instance of AXP192 - } else if (Config::screen == "M5STICKCP2") { - pinMode(4, OUTPUT); - digitalWrite(4, HIGH); - } - - Display::startScreen(); - Serial.println(" "); - Serial.println(mood.getHappy() + - " Hi, I'm Minigotchi, your pwnagotchi's best friend!"); - Display::updateDisplay(mood.getHappy(), "Hi, I'm Minigotchi"); - Serial.println(" "); - Serial.println(mood.getNeutral() + - " You can edit my configuration parameters in config.cpp!"); - Serial.println(" "); - delay(Config::shortDelay); - Display::updateDisplay(mood.getNeutral(), "Edit my config.cpp!"); - delay(Config::shortDelay); - Serial.println(mood.getIntense() + " Starting now..."); - Serial.println(" "); - Display::updateDisplay(mood.getIntense(), "Starting now"); - delay(Config::shortDelay); - Serial.println("################################################"); - Serial.println("# BOOTUP PROCESS #"); - Serial.println("################################################"); - Serial.println(" "); - ESP_ERROR_CHECK(esp_wifi_init(&Config::wifiCfg)); - esp_err_t err = nvs_flash_init(); - if (err == ESP_ERR_NVS_NO_FREE_PAGES || - err == ESP_ERR_NVS_NEW_VERSION_FOUND) { - ESP_ERROR_CHECK(nvs_flash_erase()); - err = nvs_flash_init(); - } - - Config::loadConfig(); - ESP_ERROR_CHECK(err); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - ESP_ERROR_CHECK(esp_wifi_set_country(&Config::ctryCfg)); - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_start()); - - // wait for the webui configuration - if (!Config::configured) { - waitForInput(); - } - - Deauth::list(); - Channel::init(Config::channel); - Minigotchi::info(); - Parasite::sendName(); - Minigotchi::finish(); + // configure moods + Mood::init(Config::happy, Config::sad, Config::broken, Config::intense, + Config::looking1, Config::looking2, Config::neutral, + Config::sleeping); + + // StickC Plus 1.1 and 2 power management, to keep turned On after unplug USB + // cable + if (Config::screen == "M5STICKCP") { + AXP192 axp192; + axp192.begin(); // Use the instance of AXP192 + axp192.ScreenBreath(100); // Use the instance of AXP192 + } else if (Config::screen == "M5STICKCP2") { + pinMode(4, OUTPUT); + digitalWrite(4, HIGH); + } + + Display::startScreen(); + Serial.println(" "); + Serial.println(mood.getHappy() + + " Hi, I'm Minigotchi, your pwnagotchi's best friend!"); + Display::updateDisplay(mood.getHappy(), "Hi, I'm Minigotchi"); + Serial.println(" "); + Serial.println(mood.getNeutral() + + " You can edit my configuration parameters in config.cpp!"); + Serial.println(" "); + delay(Config::shortDelay); + Display::updateDisplay(mood.getNeutral(), "Edit my config.cpp!"); + delay(Config::shortDelay); + Serial.println(mood.getIntense() + " Starting now..."); + Serial.println(" "); + Display::updateDisplay(mood.getIntense(), "Starting now"); + delay(Config::shortDelay); + Serial.println("################################################"); + Serial.println("# BOOTUP PROCESS #"); + Serial.println("################################################"); + Serial.println(" "); + ESP_ERROR_CHECK(esp_wifi_init(&Config::wifiCfg)); + esp_err_t err = nvs_flash_init(); + if (err == ESP_ERR_NVS_NO_FREE_PAGES || + err == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_ERROR_CHECK(nvs_flash_erase()); + err = nvs_flash_init(); + } + + Config::loadConfig(); + ESP_ERROR_CHECK(err); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + ESP_ERROR_CHECK(esp_wifi_set_country(&Config::ctryCfg)); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_start()); + + // wait for the webui configuration + if (!Config::configured) { + waitForInput(); + } + + Deauth::list(); + Channel::init(Config::channel); + Minigotchi::info(); + Parasite::sendName(); + Minigotchi::finish(); } /** * Show current Minigotchi info/stats */ void Minigotchi::info() { - delay(Config::shortDelay); - Serial.println(" "); - Serial.println(mood.getNeutral() + " Current Minigotchi Stats: "); - Display::updateDisplay(mood.getNeutral(), "Current Minigotchi Stats:"); - version(); - mem(); - cpu(); - Serial.println(" "); - delay(Config::shortDelay); + delay(Config::shortDelay); + Serial.println(" "); + Serial.println(mood.getNeutral() + " Current Minigotchi Stats: "); + Display::updateDisplay(mood.getNeutral(), "Current Minigotchi Stats:"); + version(); + mem(); + cpu(); + Serial.println(" "); + delay(Config::shortDelay); } /** * This is printed after everything is done in the bootup process */ void Minigotchi::finish() { - Serial.println("################################################"); - Serial.println(" "); - Serial.println(mood.getNeutral() + " Started successfully!"); - Serial.println(" "); - Display::updateDisplay(mood.getNeutral(), "Started sucessfully"); - delay(Config::shortDelay); + Serial.println("################################################"); + Serial.println(" "); + Serial.println(mood.getNeutral() + " Started successfully!"); + Serial.println(" "); + Display::updateDisplay(mood.getNeutral(), "Started sucessfully"); + delay(Config::shortDelay); } /** * Shows current Minigotchi version */ void Minigotchi::version() { - Serial.print(mood.getNeutral() + " Version: "); - Serial.println(Config::version.c_str()); - Display::updateDisplay(mood.getNeutral(), - "Version: " + (String)Config::version.c_str()); - delay(Config::shortDelay); + Serial.print(mood.getNeutral() + " Version: "); + Serial.println(Config::version.c_str()); + Display::updateDisplay(mood.getNeutral(), + "Version: " + (String)Config::version.c_str()); + delay(Config::shortDelay); } /** * Shows current Minigotchi memory usage */ void Minigotchi::mem() { - Serial.print(mood.getNeutral() + " Heap: "); - Serial.print(ESP.getFreeHeap()); - Serial.println(" bytes"); - Display::updateDisplay(mood.getNeutral(), - "Heap: " + (String)ESP.getFreeHeap() + " bytes"); - delay(Config::shortDelay); + Serial.print(mood.getNeutral() + " Heap: "); + Serial.print(ESP.getFreeHeap()); + Serial.println(" bytes"); + Display::updateDisplay(mood.getNeutral(), + "Heap: " + (String)ESP.getFreeHeap() + " bytes"); + delay(Config::shortDelay); } /** * Shows current Minigotchi Frequency */ void Minigotchi::cpu() { - Serial.print(mood.getNeutral() + " CPU Frequency: "); - Serial.print(ESP.getCpuFreqMHz()); - Serial.println(" MHz"); - Display::updateDisplay(mood.getNeutral(), - "CPU Frequency: " + (String)ESP.getCpuFreqMHz() + - " MHz"); - delay(Config::shortDelay); + Serial.print(mood.getNeutral() + " CPU Frequency: "); + Serial.print(ESP.getCpuFreqMHz()); + Serial.println(" MHz"); + Display::updateDisplay(mood.getNeutral(), + "CPU Frequency: " + (String)ESP.getCpuFreqMHz() + + " MHz"); + delay(Config::shortDelay); } /** developer note: @@ -247,22 +247,22 @@ void Minigotchi::cpu() { * Puts Minigotchi in promiscuous mode */ void Minigotchi::monStart() { - // disconnect from WiFi if we were at all - WiFi.disconnect(); + // disconnect from WiFi if we were at all + WiFi.disconnect(); - // revert to station mode - WiFi.mode(WIFI_STA); - esp_wifi_set_promiscuous(true); + // revert to station mode + WiFi.mode(WIFI_STA); + esp_wifi_set_promiscuous(true); } /** * Takes Minigotchi out of promiscuous mode */ void Minigotchi::monStop() { - esp_wifi_set_promiscuous(false); + esp_wifi_set_promiscuous(false); - // revert to station mode - WiFi.mode(WIFI_STA); + // revert to station mode + WiFi.mode(WIFI_STA); } /** developer note: @@ -288,30 +288,30 @@ void Minigotchi::monStop() { * Channel cycling */ void Minigotchi::cycle() { - Parasite::readData(); - Channel::cycle(); + Parasite::readData(); + Channel::cycle(); } /** * Pwnagotchi detection */ void Minigotchi::detect() { - Parasite::readData(); - Pwnagotchi::detect(); + Parasite::readData(); + Pwnagotchi::detect(); } /** * Deauthing */ void Minigotchi::deauth() { - Parasite::readData(); - Deauth::deauth(); + Parasite::readData(); + Deauth::deauth(); } /** * Advertising */ void Minigotchi::advertise() { - Parasite::readData(); - Frame::advertise(); + Parasite::readData(); + Frame::advertise(); } diff --git a/minigotchi-ESP32/webui.cpp b/minigotchi-ESP32/webui.cpp index 334089a..420d34d 100644 --- a/minigotchi-ESP32/webui.cpp +++ b/minigotchi-ESP32/webui.cpp @@ -124,92 +124,92 @@ const char WebUI::html[] = R"rawliteral( // captive portal class, this isn't the main class though class CaptivePortalHandler : public AsyncWebHandler { public: - CaptivePortalHandler() {} - virtual ~CaptivePortalHandler() {} - - bool canHandle(AsyncWebServerRequest *request) { - return request->url() == "/"; - } - - void handleRequest(AsyncWebServerRequest *request) { - if (request->method() == HTTP_GET && request->url() == "/") { - request->send(200, "text/html", WebUI::html); - } else { - request->send(200, "text/html", WebUI::html); - } + CaptivePortalHandler() {} + virtual ~CaptivePortalHandler() {} + + bool canHandle(AsyncWebServerRequest *request) { + return request->url() == "/"; + } + + void handleRequest(AsyncWebServerRequest *request) { + if (request->method() == HTTP_GET && request->url() == "/") { + request->send(200, "text/html", WebUI::html); + } else { + request->send(200, "text/html", WebUI::html); } + } }; /** * Starts and constructs Web Server */ WebUI::WebUI() { - Serial.println(mood.getIntense() + " Starting Web Server..."); - Display::updateDisplay(mood.getIntense(), "Starting Web Server..."); + Serial.println(mood.getIntense() + " Starting Web Server..."); + Display::updateDisplay(mood.getIntense(), "Starting Web Server..."); - WiFi.mode(WIFI_AP); - WiFi.softAP(Config::ssid); + WiFi.mode(WIFI_AP); + WiFi.softAP(Config::ssid); - setupServer(); + setupServer(); - dnsServer.setErrorReplyCode(DNSReplyCode::NoError); - dnsServer.setTTL(300); - dnsServer.start(53, "*", WiFi.softAPIP()); + dnsServer.setErrorReplyCode(DNSReplyCode::NoError); + dnsServer.setTTL(300); + dnsServer.start(53, "*", WiFi.softAPIP()); - server.begin(); - WebUI::running = true; + server.begin(); + WebUI::running = true; - while (running) { - dnsServer.processNextRequest(); - } + while (running) { + dnsServer.processNextRequest(); + } } WebUI::~WebUI() { - // debugging - Serial.println("WebUI Destructor called"); + // debugging + Serial.println("WebUI Destructor called"); - // nah fuck it - dnsServer.stop(); - server.end(); - WiFi.softAPdisconnect(true); + // nah fuck it + dnsServer.stop(); + server.end(); + WiFi.softAPdisconnect(true); - running = false; + running = false; } /** * Sets up Web Server */ void WebUI::setupServer() { - server.addHandler(new CaptivePortalHandler()).setFilter(ON_AP_FILTER); - - // handle whitelist - server.on("/get", HTTP_GET, [](AsyncWebServerRequest *request) { - if (request->hasParam("whitelist")) { - String newWhitelist = request->getParam("whitelist")->value(); - updateWhitelist(newWhitelist); - request->send(200, "text/html", - mood.getHappy() + " Whitelist updated successfully!
Return to Home Page"); - } else if (request->hasParam("config")) { - String configValue = request->getParam("config")->value(); - Config::configured = (configValue == "true"); - Config::saveConfig(); - // Serial.println("Config check: " + String(Config::configured ? "true" : - // "false")); - request->send(200, "text/html", - mood.getHappy() + - " Configuration updated! You may exit this tab and " - "disconnect from the Wifi AP.
"); - } else { - request->send(200, "text/html", - mood.getBroken() + " No valid input received.
Return to Home Page"); - } - }); + server.addHandler(new CaptivePortalHandler()).setFilter(ON_AP_FILTER); + + // handle whitelist + server.on("/get", HTTP_GET, [](AsyncWebServerRequest *request) { + if (request->hasParam("whitelist")) { + String newWhitelist = request->getParam("whitelist")->value(); + updateWhitelist(newWhitelist); + request->send(200, "text/html", + mood.getHappy() + " Whitelist updated successfully!
Return to Home Page"); + } else if (request->hasParam("config")) { + String configValue = request->getParam("config")->value(); + Config::configured = (configValue == "true"); + Config::saveConfig(); + // Serial.println("Config check: " + String(Config::configured ? "true" : + // "false")); + request->send(200, "text/html", + mood.getHappy() + + " Configuration updated! You may exit this tab and " + "disconnect from the Wifi AP.
"); + } else { + request->send(200, "text/html", + mood.getBroken() + " No valid input received.
Return to Home Page"); + } + }); - server.onNotFound([&](AsyncWebServerRequest *request) { - request->send(200, "text/html", html); - }); + server.onNotFound([&](AsyncWebServerRequest *request) { + request->send(200, "text/html", html); + }); } /** @@ -217,23 +217,23 @@ void WebUI::setupServer() { * @param newWhiteList new whitelist to use */ void WebUI::updateWhitelist(String newWhitelist) { - Config::whitelist.clear(); // clear existing values - int start = 0; - int end = newWhitelist.indexOf(','); - - while (end != -1) { - Config::whitelist.push_back(newWhitelist.substring(start, end).c_str()); - start = end + 1; - end = newWhitelist.indexOf(',', start); - } - - // add last element after last comma - Config::whitelist.push_back(newWhitelist.substring(start).c_str()); - - /* - Serial.println(mood.getNeutral() + " Updated whitelist:"); - for (const auto &entry : Config::whitelist) { - Serial.println(entry.c_str()); - } - */ + Config::whitelist.clear(); // clear existing values + int start = 0; + int end = newWhitelist.indexOf(','); + + while (end != -1) { + Config::whitelist.push_back(newWhitelist.substring(start, end).c_str()); + start = end + 1; + end = newWhitelist.indexOf(',', start); + } + + // add last element after last comma + Config::whitelist.push_back(newWhitelist.substring(start).c_str()); + + /* + Serial.println(mood.getNeutral() + " Updated whitelist:"); + for (const auto &entry : Config::whitelist) { + Serial.println(entry.c_str()); + } + */ } diff --git a/minigotchi-ESP32/webui.h b/minigotchi-ESP32/webui.h index 3b72021..67c11bd 100644 --- a/minigotchi-ESP32/webui.h +++ b/minigotchi-ESP32/webui.h @@ -37,15 +37,15 @@ class Mood; // this is the actual class we use class WebUI { public: - WebUI(); - ~WebUI(); - static void setupServer(); - static void updateWhitelist(String newWhitelist); - static const char html[] PROGMEM; - static bool running; + WebUI(); + ~WebUI(); + static void setupServer(); + static void updateWhitelist(String newWhitelist); + static const char html[] PROGMEM; + static bool running; private: - static Mood &mood; + static Mood &mood; }; #endif // WEBUI_H