From 8bf1402a99157d1fcbcbe52e39e88b16ac2f1a61 Mon Sep 17 00:00:00 2001 From: tracedgod Date: Fri, 9 Jun 2023 23:10:21 -0400 Subject: [PATCH] fixup preprocessor directives --- esp32_marauder/CommandLine.cpp | 40 +-- esp32_marauder/SDInterface.cpp | 388 +++++++++++++++--------------- esp32_marauder/WiFiScan.cpp | 48 +++- esp32_marauder/WiFiScan.h | 6 +- esp32_marauder/esp32_marauder.ino | 9 +- 5 files changed, 263 insertions(+), 228 deletions(-) diff --git a/esp32_marauder/CommandLine.cpp b/esp32_marauder/CommandLine.cpp index 767c333..cdab132 100644 --- a/esp32_marauder/CommandLine.cpp +++ b/esp32_marauder/CommandLine.cpp @@ -270,13 +270,16 @@ void CommandLine::runCommand(String input) { #endif } // ls command - #ifdef HAS_SD else if (cmd_args.get(0) == LS_CMD) { - if (cmd_args.size() > 1) - sd_obj.listDir(cmd_args.get(1)); - else - Serial.println("You did not provide a dir to list"); - } + #ifdef HAS_SD + if (cmd_args.size() > 1) + sd_obj.listDir(cmd_args.get(1)); + else + Serial.println("You did not provide a dir to list"); + #else + Serial.println("SD support disabled, cannot use command"); + return; + } #endif // Channel command @@ -666,16 +669,21 @@ void CommandLine::runCommand(String input) { } // Update via SD else if (sd_sw != -1) { - #ifndef WRITE_PACKETS_SERIAL - if (!sd_obj.supported) { - Serial.println("SD card is not connected. Cannot perform SD Update"); - return; - } - wifi_scan_obj.currentScanMode = OTA_UPDATE; - sd_obj.runUpdate(); - #else - Serial.println("SD card not initialized. Cannot perform SD Update"); - #endif + #ifdef HAS_SD + #ifndef WRITE_PACKETS_SERIAL + if (!sd_obj.supported) { + Serial.println("SD card is not connected. Cannot perform SD Update"); + return; + } + wifi_scan_obj.currentScanMode = OTA_UPDATE; + sd_obj.runUpdate(); + #else + Serial.println("SD card not initialized. Cannot perform SD Update"); + #endif + #else + Serial.println("SD card support disabled. Cannot perform SD Update"); + return; + #endif } } } diff --git a/esp32_marauder/SDInterface.cpp b/esp32_marauder/SDInterface.cpp index 2d3d1f0..135dedb 100644 --- a/esp32_marauder/SDInterface.cpp +++ b/esp32_marauder/SDInterface.cpp @@ -1,11 +1,10 @@ -#ifdef HAS_SD +#include "SDInterface.h" +#include "lang_var.h" - #include "SDInterface.h" - #include "lang_var.h" - - bool SDInterface::initSD() { +bool SDInterface::initSD() { + #ifdef HAS_SD String display_string = ""; - + #ifdef KIT pinMode(SD_DET, INPUT); if (digitalRead(SD_DET) == LOW) { @@ -17,11 +16,11 @@ return false; } #endif - + pinMode(SD_CS, OUTPUT); - + delay(10); - + if (!SD.begin(SD_CS)) { Serial.println(F("Failed to mount SD Card")); this->supported = false; @@ -38,221 +37,224 @@ // Serial.println(F("SD: SDHC Mounted")); //else // Serial.println(F("SD: UNKNOWN Card Mounted")); - + this->cardSizeMB = SD.cardSize() / (1024 * 1024); - + //Serial.printf("SD Card Size: %lluMB\n", this->cardSizeMB); - + if (this->supported) { const int NUM_DIGITS = log10(this->cardSizeMB) + 1; - + char sz[NUM_DIGITS + 1]; - + sz[NUM_DIGITS] = 0; for ( size_t i = NUM_DIGITS; i--; this->cardSizeMB /= 10) { sz[i] = '0' + (this->cardSizeMB % 10); display_string.concat((String)sz[i]); } - + this->card_sz = sz; } - + buffer_obj = Buffer(); - + if (!SD.exists("/SCRIPTS")) { Serial.println("/SCRIPTS does not exist. Creating..."); - + SD.mkdir("/SCRIPTS"); Serial.println("/SCRIPTS created"); } - - return true; - } - } - - void SDInterface::listDir(String str_dir){ - if (this->supported) { - File dir = SD.open(str_dir); - while (true) - { - File entry = dir.openNextFile(); - if (! entry) - { - break; - } - //for (uint8_t i = 0; i < numTabs; i++) - //{ - // Serial.print('\t'); - //} - Serial.print(entry.name()); - Serial.print("\t"); - Serial.println(entry.size()); - entry.close(); - } - } - } - - void SDInterface::addPacket(uint8_t* buf, uint32_t len) { - if ((this->supported) && (this->do_save)) { - buffer_obj.addPacket(buf, len); - } - } - - void SDInterface::openCapture(String file_name) { - bool save_pcap = settings_obj.loadSetting("SavePCAP"); - if ((this->supported) && (save_pcap)) { - buffer_obj.createPcapFile(&SD, file_name); - buffer_obj.open(); - } - } - - void SDInterface::runUpdate() { - #ifdef HAS_SCREEN - display_obj.tft.setTextWrap(false); - display_obj.tft.setFreeFont(NULL); - display_obj.tft.setCursor(0, 100); - display_obj.tft.setTextSize(1); - display_obj.tft.setTextColor(TFT_WHITE); - display_obj.tft.println(F(text15)); - #endif - File updateBin = SD.open("/update.bin"); - if (updateBin) { - if(updateBin.isDirectory()){ - #ifdef HAS_SCREEN - display_obj.tft.setTextColor(TFT_RED); - display_obj.tft.println(F(text_table2[0])); - #endif - Serial.println(F("Error, could not find \"update.bin\"")); - #ifdef HAS_SCREEN - display_obj.tft.setTextColor(TFT_WHITE); - #endif - updateBin.close(); - return; + return true; + } + + #else + Serial.println("SD support disabled, skipping init"); + return false; + #endif +} + +void SDInterface::listDir(String str_dir){ + if (this->supported) { + File dir = SD.open(str_dir); + while (true) + { + File entry = dir.openNextFile(); + if (! entry) + { + break; } + //for (uint8_t i = 0; i < numTabs; i++) + //{ + // Serial.print('\t'); + //} + Serial.print(entry.name()); + Serial.print("\t"); + Serial.println(entry.size()); + entry.close(); + } + } +} + +void SDInterface::addPacket(uint8_t* buf, uint32_t len) { + if ((this->supported) && (this->do_save)) { + buffer_obj.addPacket(buf, len); + } +} + +void SDInterface::openCapture(String file_name) { + bool save_pcap = settings_obj.loadSetting("SavePCAP"); + if ((this->supported) && (save_pcap)) { + buffer_obj.createPcapFile(&SD, file_name); + buffer_obj.open(); + } +} + +void SDInterface::runUpdate() { + #ifdef HAS_SCREEN + display_obj.tft.setTextWrap(false); + display_obj.tft.setFreeFont(NULL); + display_obj.tft.setCursor(0, 100); + display_obj.tft.setTextSize(1); + display_obj.tft.setTextColor(TFT_WHITE); - size_t updateSize = updateBin.size(); - - if (updateSize > 0) { - #ifdef HAS_SCREEN - display_obj.tft.println(F(text_table2[1])); - #endif - Serial.println(F("Starting update over SD. Please wait...")); - this->performUpdate(updateBin, updateSize); - } - else { - #ifdef HAS_SCREEN - display_obj.tft.setTextColor(TFT_RED); - display_obj.tft.println(F(text_table2[2])); - #endif - Serial.println(F("Error, file is empty")); - #ifdef HAS_SCREEN - display_obj.tft.setTextColor(TFT_WHITE); - #endif - return; - } - - updateBin.close(); - - // whe finished remove the binary from sd card to indicate end of the process + display_obj.tft.println(F(text15)); + #endif + File updateBin = SD.open("/update.bin"); + if (updateBin) { + if(updateBin.isDirectory()){ #ifdef HAS_SCREEN - display_obj.tft.println(F(text_table2[3])); + display_obj.tft.setTextColor(TFT_RED); + display_obj.tft.println(F(text_table2[0])); #endif - Serial.println(F("rebooting...")); - //SD.remove("/update.bin"); - delay(1000); - ESP.restart(); + Serial.println(F("Error, could not find \"update.bin\"")); + #ifdef HAS_SCREEN + display_obj.tft.setTextColor(TFT_WHITE); + #endif + updateBin.close(); + return; + } + + size_t updateSize = updateBin.size(); + + if (updateSize > 0) { + #ifdef HAS_SCREEN + display_obj.tft.println(F(text_table2[1])); + #endif + Serial.println(F("Starting update over SD. Please wait...")); + this->performUpdate(updateBin, updateSize); } else { #ifdef HAS_SCREEN display_obj.tft.setTextColor(TFT_RED); - display_obj.tft.println(F(text_table2[4])); + display_obj.tft.println(F(text_table2[2])); #endif - Serial.println(F("Could not load update.bin from sd root")); + Serial.println(F("Error, file is empty")); #ifdef HAS_SCREEN display_obj.tft.setTextColor(TFT_WHITE); #endif + return; } - } - - void SDInterface::performUpdate(Stream &updateSource, size_t updateSize) { - if (Update.begin(updateSize)) { - #ifdef HAS_SCREEN - display_obj.tft.println(text_table2[5] + String(updateSize)); - display_obj.tft.println(F(text_table2[6])); - #endif - size_t written = Update.writeStream(updateSource); - if (written == updateSize) { - #ifdef HAS_SCREEN - display_obj.tft.println(text_table2[7] + String(written) + text_table2[10]); - #endif - Serial.println("Written : " + String(written) + " successfully"); - } - else { - #ifdef HAS_SCREEN - display_obj.tft.println(text_table2[8] + String(written) + "/" + String(updateSize) + text_table2[9]); - #endif - Serial.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?"); - } - if (Update.end()) { - Serial.println("OTA done!"); - if (Update.isFinished()) { - #ifdef HAS_SCREEN - display_obj.tft.println(F(text_table2[11])); - #endif - Serial.println(F("Update successfully completed. Rebooting.")); - } - else { - #ifdef HAS_SCREEN - display_obj.tft.setTextColor(TFT_RED); - display_obj.tft.println(text_table2[12]); - #endif - Serial.println("Update not finished? Something went wrong!"); - #ifdef HAS_SCREEN - display_obj.tft.setTextColor(TFT_WHITE); - #endif - } - } - else { - #ifdef HAS_SCREEN - display_obj.tft.println(text_table2[13] + String(Update.getError())); - #endif - Serial.println("Error Occurred. Error #: " + String(Update.getError())); - } - - } - else - { - #ifdef HAS_SCREEN - display_obj.tft.println(text_table2[14]); - #endif - Serial.println("Not enough space to begin OTA"); - } - } - - bool SDInterface::checkDetectPin() { - #ifdef KIT - if (digitalRead(SD_DET) == LOW) - return true; - else - return false; - #endif - - return false; - } - - void SDInterface::main() { - if ((this->supported) && (this->do_save)) { - //Serial.println("Saving packet..."); - buffer_obj.forceSave(&SD); - } - else if (!this->supported) { - if (checkDetectPin()) { - delay(100); - this->initSD(); - } - } - } -#endif \ No newline at end of file + updateBin.close(); + + // whe finished remove the binary from sd card to indicate end of the process + #ifdef HAS_SCREEN + display_obj.tft.println(F(text_table2[3])); + #endif + Serial.println(F("rebooting...")); + //SD.remove("/update.bin"); + delay(1000); + ESP.restart(); + } + else { + #ifdef HAS_SCREEN + display_obj.tft.setTextColor(TFT_RED); + display_obj.tft.println(F(text_table2[4])); + #endif + Serial.println(F("Could not load update.bin from sd root")); + #ifdef HAS_SCREEN + display_obj.tft.setTextColor(TFT_WHITE); + #endif + } +} + +void SDInterface::performUpdate(Stream &updateSource, size_t updateSize) { + if (Update.begin(updateSize)) { + #ifdef HAS_SCREEN + display_obj.tft.println(text_table2[5] + String(updateSize)); + display_obj.tft.println(F(text_table2[6])); + #endif + size_t written = Update.writeStream(updateSource); + if (written == updateSize) { + #ifdef HAS_SCREEN + display_obj.tft.println(text_table2[7] + String(written) + text_table2[10]); + #endif + Serial.println("Written : " + String(written) + " successfully"); + } + else { + #ifdef HAS_SCREEN + display_obj.tft.println(text_table2[8] + String(written) + "/" + String(updateSize) + text_table2[9]); + #endif + Serial.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?"); + } + if (Update.end()) { + Serial.println("OTA done!"); + if (Update.isFinished()) { + #ifdef HAS_SCREEN + display_obj.tft.println(F(text_table2[11])); + #endif + Serial.println(F("Update successfully completed. Rebooting.")); + } + else { + #ifdef HAS_SCREEN + display_obj.tft.setTextColor(TFT_RED); + display_obj.tft.println(text_table2[12]); + #endif + Serial.println("Update not finished? Something went wrong!"); + #ifdef HAS_SCREEN + display_obj.tft.setTextColor(TFT_WHITE); + #endif + } + } + else { + #ifdef HAS_SCREEN + display_obj.tft.println(text_table2[13] + String(Update.getError())); + #endif + Serial.println("Error Occurred. Error #: " + String(Update.getError())); + } + + } + else + { + #ifdef HAS_SCREEN + display_obj.tft.println(text_table2[14]); + #endif + Serial.println("Not enough space to begin OTA"); + } +} + +bool SDInterface::checkDetectPin() { + #ifdef KIT + if (digitalRead(SD_DET) == LOW) + return true; + else + return false; + #endif + + return false; +} + +void SDInterface::main() { + if ((this->supported) && (this->do_save)) { + //Serial.println("Saving packet..."); + buffer_obj.forceSave(&SD); + } + else if (!this->supported) { + if (checkDetectPin()) { + delay(100); + this->initSD(); + } + } +} \ No newline at end of file diff --git a/esp32_marauder/WiFiScan.cpp b/esp32_marauder/WiFiScan.cpp index 2ca7228..9974f0d 100644 --- a/esp32_marauder/WiFiScan.cpp +++ b/esp32_marauder/WiFiScan.cpp @@ -580,8 +580,10 @@ void WiFiScan::RunAPScan(uint8_t scan_mode, uint16_t color) { #ifdef WRITE_PACKETS_SERIAL buffer_obj.open(); - #else + #elif defined(HAS_SD) sd_obj.openCapture("ap"); + #else + return; #endif #ifdef MARAUDER_FLIPPER @@ -809,7 +811,7 @@ void WiFiScan::RunInfo() #ifdef HAS_SCREEN display_obj.tft.println(text_table4[48]); #endif - #else + #elif defined(HAS_SD) if (sd_obj.supported) { #ifdef HAS_SCREEN display_obj.tft.println(text_table4[28]); @@ -823,6 +825,8 @@ void WiFiScan::RunInfo() display_obj.tft.println(text_table4[31]); #endif } + #else + return; #endif #ifdef HAS_BATTERY @@ -848,8 +852,10 @@ void WiFiScan::RunInfo() void WiFiScan::RunEspressifScan(uint8_t scan_mode, uint16_t color) { #ifdef WRITE_PACKETS_SERIAL buffer_obj.open(); - #else + #elif defined(HAS_SD) sd_obj.openCapture("espressif"); + #else + return; #endif #ifdef MARAUDER_FLIPPER @@ -901,8 +907,10 @@ void WiFiScan::RunPacketMonitor(uint8_t scan_mode, uint16_t color) #ifdef WRITE_PACKETS_SERIAL buffer_obj.open(); - #else + #elif defined(HAS_SD) sd_obj.openCapture("packet_monitor"); + #else + return; #endif #ifdef HAS_ILI9341 @@ -1020,8 +1028,10 @@ void WiFiScan::RunEapolScan(uint8_t scan_mode, uint16_t color) #else #ifdef WRITE_PACKETS_SERIAL buffer_obj.open(); - #else + #elif defined(HAS_SD) sd_obj.openCapture("eapol"); + #else + return; #endif #ifdef HAS_SCREEN @@ -1119,8 +1129,10 @@ void WiFiScan::RunPwnScan(uint8_t scan_mode, uint16_t color) { #ifdef WRITE_PACKETS_SERIAL buffer_obj.open(); - #else + #elif defined(HAS_SD) sd_obj.openCapture("pwnagotchi"); + #else + return; #endif #ifdef MARAUDER_FLIPPER @@ -1165,8 +1177,10 @@ void WiFiScan::RunBeaconScan(uint8_t scan_mode, uint16_t color) { #ifdef WRITE_PACKETS_SERIAL buffer_obj.open(); - #else + #elif defined(HAS_SD) sd_obj.openCapture("beacon"); + #else + return; #endif #ifdef MARAUDER_FLIPPER @@ -1210,8 +1224,10 @@ void WiFiScan::RunStationScan(uint8_t scan_mode, uint16_t color) { #ifdef WRITE_PACKETS_SERIAL buffer_obj.open(); - #else + #elif defined(HAS_SD) sd_obj.openCapture("station"); + #else + return; #endif #ifdef MARAUDER_FLIPPER @@ -1255,9 +1271,11 @@ void WiFiScan::RunRawScan(uint8_t scan_mode, uint16_t color) { #ifdef WRITE_PACKETS_SERIAL buffer_obj.open(); - #else + #elif defined(HAS_SD) if (scan_mode != WIFI_SCAN_SIG_STREN) sd_obj.openCapture("raw"); + #else + return; #endif #ifdef MARAUDER_FLIPPER @@ -1304,8 +1322,10 @@ void WiFiScan::RunDeauthScan(uint8_t scan_mode, uint16_t color) { #ifdef WRITE_PACKETS_SERIAL buffer_obj.open(); - #else + #elif defined(HAS_SD) sd_obj.openCapture("deauth"); + #else + return; #endif #ifdef MARAUDER_FLIPPER @@ -1351,8 +1371,10 @@ void WiFiScan::RunProbeScan(uint8_t scan_mode, uint16_t color) { #ifdef WRITE_PACKETS_SERIAL buffer_obj.open(); - #else + #elif defined(HAS_SD) sd_obj.openCapture("probe"); + #else + return; #endif #ifdef MARAUDER_FLIPPER @@ -3209,8 +3231,10 @@ void WiFiScan::addPacket(wifi_promiscuous_pkt_t *snifferPacket, int len) { if (save_packet) { #ifdef WRITE_PACKETS_SERIAL buffer_obj.addPacket(snifferPacket->payload, len); - #else + #elif defined(HAS_SD) sd_obj.addPacket(snifferPacket->payload, len); + #else + return; #endif } } diff --git a/esp32_marauder/WiFiScan.h b/esp32_marauder/WiFiScan.h index c4076b2..654489d 100644 --- a/esp32_marauder/WiFiScan.h +++ b/esp32_marauder/WiFiScan.h @@ -38,11 +38,9 @@ #include "Assets.h" #ifdef MARAUDER_FLIPPER #include "flipperLED.h" -#endif -#ifdef XIAO_ESP32_S3 +#elif defined(XIAO_ESP32_S3) #include "xiaoLED.h" -#endif -#ifndef MARAUDER_FLIPPER || XIAO_ESP32_S3 +#else #include "LedInterface.h" #endif //#include "MenuFunctions.h" diff --git a/esp32_marauder/esp32_marauder.ino b/esp32_marauder/esp32_marauder.ino index f541400..232889e 100644 --- a/esp32_marauder/esp32_marauder.ino +++ b/esp32_marauder/esp32_marauder.ino @@ -116,7 +116,6 @@ CommandLine cli_obj; #endif const String PROGMEM version_number = MARAUDER_VERSION; -const String PROGMEM board_target = MARAUDER_TARGET; #ifdef HAS_NEOPIXEL_LED Adafruit_NeoPixel strip = Adafruit_NeoPixel(Pixels, PIN, NEO_GRB + NEO_KHZ800); @@ -267,7 +266,7 @@ void setup() #ifdef WRITE_PACKETS_SERIAL buffer_obj = Buffer(); - #else + #elif defined(HAS_SD) // Do some SD stuff if(sd_obj.initSD()) { #ifdef HAS_SCREEN @@ -281,6 +280,8 @@ void setup() display_obj.tft.setTextColor(TFT_CYAN, TFT_BLACK); #endif } + #else + return; #endif #ifdef HAS_BATTERY @@ -374,8 +375,10 @@ void loop() #ifdef WRITE_PACKETS_SERIAL buffer_obj.forceSaveSerial(); - #else + #elif defined(HAS_SD) sd_obj.main(); + #else + return; #endif #ifdef HAS_BATTERY