From 750fc0987fb553cac77a2650c64c76d591e0f098 Mon Sep 17 00:00:00 2001 From: i-am-shodan <6901273+i-am-shodan@users.noreply.github.com> Date: Fri, 27 Sep 2024 14:03:05 +0100 Subject: [PATCH] Make the codebase easier to reuse elsewhere --- esp32_marauder/CommandLine.h | 5 ++++ esp32_marauder/GpsInterface.h | 2 ++ esp32_marauder/LedInterface.cpp | 10 ++++++- esp32_marauder/LedInterface.h | 6 +++++ esp32_marauder/SDInterface.cpp | 3 +-- esp32_marauder/SDInterface.h | 8 +++++- esp32_marauder/WiFiScan.cpp | 1 + esp32_marauder/WiFiScan.h | 2 -- esp32_marauder/settings.cpp | 46 ++++++++++++++++++++++++++++++--- esp32_marauder/settings.h | 2 ++ 10 files changed, 75 insertions(+), 10 deletions(-) diff --git a/esp32_marauder/CommandLine.h b/esp32_marauder/CommandLine.h index c6be7e0fe..6cdcf72dc 100644 --- a/esp32_marauder/CommandLine.h +++ b/esp32_marauder/CommandLine.h @@ -151,7 +151,9 @@ class CommandLine { LinkedList parseCommand(String input, char* delim); String toLowerCase(String str); void filterAccessPoints(String filter); +#ifndef ENABLE_NONSERIAL_COMMAND_EXECUTION void runCommand(String input); +#endif bool checkValueExists(LinkedList* cmd_args_list, int index); bool inRange(int max, int index); bool apSelected(); @@ -191,6 +193,9 @@ class CommandLine { void RunSetup(); void main(uint32_t currentTime); +#ifdef ENABLE_NONSERIAL_COMMAND_EXECUTION + void runCommand(String input); +#endif }; #endif diff --git a/esp32_marauder/GpsInterface.h b/esp32_marauder/GpsInterface.h index 27640ce8f..fa7557268 100644 --- a/esp32_marauder/GpsInterface.h +++ b/esp32_marauder/GpsInterface.h @@ -3,6 +3,7 @@ #ifndef GpsInterface_h #define GpsInterface_h +#ifdef HAS_GPS #include #include #include @@ -120,3 +121,4 @@ class GpsInterface { }; #endif +#endif diff --git a/esp32_marauder/LedInterface.cpp b/esp32_marauder/LedInterface.cpp index be97b664f..1d36adfef 100644 --- a/esp32_marauder/LedInterface.cpp +++ b/esp32_marauder/LedInterface.cpp @@ -6,6 +6,7 @@ LedInterface::LedInterface() { void LedInterface::RunSetup() { //Serial.println("Setting neopixel to black..."); +#ifndef DISABLE_STATUS_LED strip.setBrightness(0); strip.begin(); strip.setPixelColor(0, strip.Color(0, 0, 0)); @@ -15,6 +16,7 @@ void LedInterface::RunSetup() { strip.setPixelColor(0, strip.Color(0, 0, 0)); strip.show(); this->initTime = millis(); +#endif } void LedInterface::main(uint32_t currentTime) { @@ -50,8 +52,10 @@ uint8_t LedInterface::getMode() { } void LedInterface::setColor(int r, int g, int b) { +#ifndef DISABLE_STATUS_LED strip.setPixelColor(0, strip.Color(r, g, b)); - strip.show(); + strip.show(); +#endif } void LedInterface::sniffLed() { @@ -67,6 +71,7 @@ void LedInterface::ledOff() { } void LedInterface::rainbow() { +#ifndef DISABLE_STATUS_LED strip.setPixelColor(0, this->Wheel((0 * 256 / 100 + this->wheel_pos) % 256)); strip.show(); @@ -75,9 +80,11 @@ void LedInterface::rainbow() { this->wheel_pos = this->wheel_pos - this->wheel_speed; if (this->wheel_pos < 0) this->wheel_pos = 255; +#endif } uint32_t LedInterface::Wheel(byte WheelPos) { +#ifndef DISABLE_STATUS_LED WheelPos = 255 - WheelPos; if(WheelPos < 85) { return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); @@ -88,4 +95,5 @@ uint32_t LedInterface::Wheel(byte WheelPos) { } WheelPos -= 170; return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); +#endif } \ No newline at end of file diff --git a/esp32_marauder/LedInterface.h b/esp32_marauder/LedInterface.h index 7ce087884..07be76b07 100644 --- a/esp32_marauder/LedInterface.h +++ b/esp32_marauder/LedInterface.h @@ -6,7 +6,10 @@ #include "configs.h" #include "settings.h" #include + +#ifndef DISABLE_STATUS_LED #include +#endif #define Pixels 1 @@ -17,7 +20,10 @@ #define MODE_CUSTOM 4 extern Settings settings_obj; + +#ifndef DISABLE_STATUS_LED extern Adafruit_NeoPixel strip; +#endif class LedInterface { diff --git a/esp32_marauder/SDInterface.cpp b/esp32_marauder/SDInterface.cpp index c5a769eb0..7b0c89d47 100644 --- a/esp32_marauder/SDInterface.cpp +++ b/esp32_marauder/SDInterface.cpp @@ -3,7 +3,7 @@ bool SDInterface::initSD() { - #ifdef HAS_SD + #if defined HAS_SD && !defined USE_SD_MMC_INTERFACE String display_string = ""; #ifdef KIT @@ -86,7 +86,6 @@ bool SDInterface::initSD() { return true; } - #else Serial.println("SD support disabled, skipping init"); return false; diff --git a/esp32_marauder/SDInterface.h b/esp32_marauder/SDInterface.h index eda62bafc..4c9044b67 100644 --- a/esp32_marauder/SDInterface.h +++ b/esp32_marauder/SDInterface.h @@ -6,7 +6,13 @@ #include "configs.h" #include "settings.h" -#include "SD.h" +#ifdef USE_SD_MMC_INTERFACE + #include "SD_MMC.h" + #define SD SD_MMC +#else + #include "SD.h" +#endif + #include "Buffer.h" #ifdef HAS_SCREEN #include "Display.h" diff --git a/esp32_marauder/WiFiScan.cpp b/esp32_marauder/WiFiScan.cpp index 148ccf73e..4187d41c8 100644 --- a/esp32_marauder/WiFiScan.cpp +++ b/esp32_marauder/WiFiScan.cpp @@ -20,6 +20,7 @@ extern "C" int ieee80211_raw_frame_sanity_check(int32_t arg, int32_t arg2, int32 extern "C" { uint8_t esp_base_mac_addr[6]; esp_err_t esp_ble_gap_set_rand_addr(const uint8_t *rand_addr); + esp_err_t esp_base_mac_addr_set(const uint8_t *addr); } #ifdef HAS_BT diff --git a/esp32_marauder/WiFiScan.h b/esp32_marauder/WiFiScan.h index 5b5ec6074..30f31d8e0 100644 --- a/esp32_marauder/WiFiScan.h +++ b/esp32_marauder/WiFiScan.h @@ -208,8 +208,6 @@ class WiFiScan "08 and hurt you" }; - char* prefix = "G"; - typedef struct { int16_t fctl; diff --git a/esp32_marauder/settings.cpp b/esp32_marauder/settings.cpp index 83bc12550..b2ffb44b6 100644 --- a/esp32_marauder/settings.cpp +++ b/esp32_marauder/settings.cpp @@ -1,9 +1,43 @@ #include "settings.h" +#define DEFAULT_SETTING_FILE "/settings.json" + String Settings::getSettingsString() { return this->json_settings_string; } +bool Settings::begin(fs::FS fs, String filename) { + Serial.println("in maurader setting"); + if (!fs.exists(filename)) + { + Serial.println("not exist creating default"); + if (!this->createDefaultSettings(fs, filename)) + { + return false; + } + } + + Serial.println("opening file setting"); + File settingsFile = fs.open(filename, FILE_READ); + if (!settingsFile) + { + Serial.println("file bad"); + return false; + } + + Serial.println("deserialising setting"); + + String json_string; + DynamicJsonDocument jsonBuffer(1024); + DeserializationError error = deserializeJson(jsonBuffer, settingsFile); + serializeJson(jsonBuffer, json_string); + this->json_settings_string = json_string; + + Serial.println("finish maurader setting"); + + return true; +} + bool Settings::begin() { if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){ Serial.println("Settings SPIFFS Mount Failed"); @@ -12,10 +46,10 @@ bool Settings::begin() { File settingsFile; - //SPIFFS.remove("/settings.json"); // NEED TO REMOVE THIS LINE + //SPIFFS.remove(DEFAULT_SETTING_FILE); // NEED TO REMOVE THIS LINE - if (SPIFFS.exists("/settings.json")) { - settingsFile = SPIFFS.open("/settings.json", FILE_READ); + if (SPIFFS.exists(DEFAULT_SETTING_FILE)) { + settingsFile = SPIFFS.open(DEFAULT_SETTING_FILE, FILE_READ); if (!settingsFile) { settingsFile.close(); @@ -240,9 +274,13 @@ void Settings::printJsonSettings(String json_string) { } bool Settings::createDefaultSettings(fs::FS &fs) { + createDefaultSettings(fs, DEFAULT_SETTING_FILE); +} + +bool Settings::createDefaultSettings(fs::FS &fs, String filename) { Serial.println(F("Creating default settings file: settings.json")); - File settingsFile = fs.open("/settings.json", FILE_WRITE); + File settingsFile = fs.open(filename, FILE_WRITE); if (!settingsFile) { Serial.println(F("Failed to create settings file")); diff --git a/esp32_marauder/settings.h b/esp32_marauder/settings.h index c85b67516..c01c8e997 100644 --- a/esp32_marauder/settings.h +++ b/esp32_marauder/settings.h @@ -24,6 +24,7 @@ class Settings { public: bool begin(); + bool begin(fs::FS fs, String filename); template T loadSetting(String name); @@ -50,6 +51,7 @@ class Settings { String getSettingsString(); bool createDefaultSettings(fs::FS &fs); + bool createDefaultSettings(fs::FS &fs, String filename); void printJsonSettings(String json_string); void main(uint32_t currentTime); };