From 5edb79f372ba205fb68a3020b762fefae941493f Mon Sep 17 00:00:00 2001 From: acidburns Date: Tue, 17 Mar 2026 12:31:16 +0100 Subject: [PATCH] refactor(build): consolidate envs to production/debug/test; add compiler hardening flags - Replace 11 per-frequency build environments with 3 role-based targets (production, debug, test) using shared [env] base section - Move LoRa frequency and sender-ID config from build flags into config.h so all variants build from the same source - Add -fstack-protector-strong, -D_FORTIFY_SOURCE=2, -Wformat-security - Add Unity test framework to lib_deps for pio test support - Add __pycache__/ to .gitignore --- .gitignore | 2 + include/config.h | 31 +++++--- platformio.ini | 195 ++++++++--------------------------------------- 3 files changed, 57 insertions(+), 171 deletions(-) diff --git a/.gitignore b/.gitignore index 89cc49c..ba10d54 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ .vscode/c_cpp_properties.json .vscode/launch.json .vscode/ipch + +__pycache__/ diff --git a/include/config.h b/include/config.h index 29223c0..180526f 100644 --- a/include/config.h +++ b/include/config.h @@ -12,6 +12,25 @@ enum class BatchRetryPolicy : uint8_t { Drop = 1 }; +// ============================================================================= +// ██ DEPLOYMENT SETTINGS — adjust these for your hardware / frequency band +// ============================================================================= + +// LoRa frequency — uncomment ONE line: +#define LORA_FREQUENCY_HZ 433E6 // 433 MHz (EU ISM, default) +// #define LORA_FREQUENCY_HZ 868E6 // 868 MHz (EU SRD) +// #define LORA_FREQUENCY_HZ 915E6 // 915 MHz (US ISM) + +// Expected sender device IDs (short-IDs). The receiver will only accept +// batches from these senders. Add one entry per physical sender board. +constexpr uint8_t NUM_SENDERS = 1; +inline constexpr uint16_t EXPECTED_SENDER_IDS[NUM_SENDERS] = { + 0xF19C // TTGO #1 – 433 MHz sender + // 0x7EB4 // TTGO #2 – 868 MHz sender (uncomment & adjust NUM_SENDERS) +}; + +// ============================================================================= + // Pin definitions constexpr uint8_t PIN_LORA_SCK = 5; constexpr uint8_t PIN_LORA_MISO = 19; @@ -34,10 +53,7 @@ constexpr uint8_t PIN_OLED_CTRL = 13; constexpr uint8_t PIN_METER_RX = 34; -// LoRa settings -#ifndef LORA_FREQUENCY_HZ -#define LORA_FREQUENCY_HZ 433E6 -#endif +// LoRa radio parameters constexpr long LORA_FREQUENCY = LORA_FREQUENCY_HZ; constexpr uint8_t LORA_SPREADING_FACTOR = 12; constexpr long LORA_BANDWIDTH = 125E3; @@ -110,6 +126,8 @@ constexpr const char *AP_SSID_PREFIX = "DD3-Bridge-"; constexpr const char *AP_PASSWORD = "changeme123"; constexpr bool WEB_AUTH_REQUIRE_STA = true; constexpr bool WEB_AUTH_REQUIRE_AP = true; +// SECURITY: these defaults are only used until the user sets credentials via +// the web config page (/wifi). The first-boot AP forces password change. constexpr const char *WEB_AUTH_DEFAULT_USER = "admin"; constexpr const char *WEB_AUTH_DEFAULT_PASS = "admin"; inline constexpr char HA_MANUFACTURER[] = "AcidBurns"; @@ -126,11 +144,6 @@ static_assert( HA_MANUFACTURER[9] == '\0', "HA_MANUFACTURER must remain exactly \"AcidBurns\""); -constexpr uint8_t NUM_SENDERS = 1; constexpr uint32_t MIN_ACCEPTED_EPOCH_UTC = 1769904000UL; // 2026-02-01 00:00:00 UTC -inline constexpr uint16_t EXPECTED_SENDER_IDS[NUM_SENDERS] = { - 0xF19C //433mhz sender - //0x7EB4 //868mhz sender -}; DeviceRole detect_role(); diff --git a/platformio.ini b/platformio.ini index 7da0e1e..e4f69c2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -1,14 +1,14 @@ ; PlatformIO Project Configuration File ; -; Build options: build flags, source filter -; Upload options: custom upload port, speed and extra flags -; Library options: dependencies, extra library storages -; Advanced options: extra scripting +; Build targets: +; production – serial off, light-sleep on (normal deployment) +; debug – serial + meter diag + state tracing (real meter, real data) +; test – synthetic meter data + payload codec self-test (no real meter needed) ; -; Please visit documentation for the other options and examples -; https://docs.platformio.org/page/projectconf.html +; LoRa frequency and sender IDs are configured in include/config.h, +; NOT via build flags. Change them there before building. -[env:lilygo-t3-v1-6-1] +[env] platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip board = ttgo-lora32-v1 framework = arduino @@ -18,169 +18,40 @@ lib_deps = adafruit/Adafruit SSD1306@^2.5.9 adafruit/Adafruit GFX Library@^1.11.9 knolleary/PubSubClient@^2.8 -build_flags = - -DSERIAL_DEBUG_MODE_FLAG=1 + throwtheswitch/Unity@^2.6.1 -[env:lilygo-t3-v1-6-1-test] -platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip -board = ttgo-lora32-v1 -framework = arduino -lib_deps = - sandeepmistry/LoRa@^0.8.0 - bblanchon/ArduinoJson@^6.21.5 - adafruit/Adafruit SSD1306@^2.5.9 - adafruit/Adafruit GFX Library@^1.11.9 - knolleary/PubSubClient@^2.8 +; --- Hardening flags for all builds --- build_flags = - -DSERIAL_DEBUG_MODE_FLAG=1 - -DENABLE_TEST_MODE + -fstack-protector-strong + -D_FORTIFY_SOURCE=2 + -Wformat -Wformat-security + -Wno-format-truncation -[env:lilygo-t3-v1-6-1-868] -platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip -board = ttgo-lora32-v1 -framework = arduino -lib_deps = - sandeepmistry/LoRa@^0.8.0 - bblanchon/ArduinoJson@^6.21.5 - adafruit/Adafruit SSD1306@^2.5.9 - adafruit/Adafruit GFX Library@^1.11.9 - knolleary/PubSubClient@^2.8 -build_flags = - -DSERIAL_DEBUG_MODE_FLAG=1 - -DLORA_FREQUENCY_HZ=868E6 - -[env:lilygo-t3-v1-6-1-868-test] -platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip -board = ttgo-lora32-v1 -framework = arduino -lib_deps = - sandeepmistry/LoRa@^0.8.0 - bblanchon/ArduinoJson@^6.21.5 - adafruit/Adafruit SSD1306@^2.5.9 - adafruit/Adafruit GFX Library@^1.11.9 - knolleary/PubSubClient@^2.8 -build_flags = - -DSERIAL_DEBUG_MODE_FLAG=1 - -DENABLE_TEST_MODE - -DLORA_FREQUENCY_HZ=868E6 - -[env:lilygo-t3-v1-6-1-payload-test] -platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip -board = ttgo-lora32-v1 -framework = arduino -lib_deps = - sandeepmistry/LoRa@^0.8.0 - bblanchon/ArduinoJson@^6.21.5 - adafruit/Adafruit SSD1306@^2.5.9 - adafruit/Adafruit GFX Library@^1.11.9 - knolleary/PubSubClient@^2.8 -build_flags = - -DSERIAL_DEBUG_MODE_FLAG=1 - -DPAYLOAD_CODEC_TEST - -[env:lilygo-t3-v1-6-1-868-payload-test] -platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip -board = ttgo-lora32-v1 -framework = arduino -lib_deps = - sandeepmistry/LoRa@^0.8.0 - bblanchon/ArduinoJson@^6.21.5 - adafruit/Adafruit SSD1306@^2.5.9 - adafruit/Adafruit GFX Library@^1.11.9 - knolleary/PubSubClient@^2.8 -build_flags = - -DSERIAL_DEBUG_MODE_FLAG=1 - -DPAYLOAD_CODEC_TEST - -DLORA_FREQUENCY_HZ=868E6 - -[env:lilygo-t3-v1-6-1-prod] -platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip -board = ttgo-lora32-v1 -framework = arduino -lib_deps = - sandeepmistry/LoRa@^0.8.0 - bblanchon/ArduinoJson@^6.21.5 - adafruit/Adafruit SSD1306@^2.5.9 - adafruit/Adafruit GFX Library@^1.11.9 - knolleary/PubSubClient@^2.8 -build_flags = - -DSERIAL_DEBUG_MODE_FLAG=0 - -[env:lilygo-t3-v1-6-1-868-prod] -platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip -board = ttgo-lora32-v1 -framework = arduino -lib_deps = - sandeepmistry/LoRa@^0.8.0 - bblanchon/ArduinoJson@^6.21.5 - adafruit/Adafruit SSD1306@^2.5.9 - adafruit/Adafruit GFX Library@^1.11.9 - knolleary/PubSubClient@^2.8 -build_flags = - -DSERIAL_DEBUG_MODE_FLAG=0 - -DLORA_FREQUENCY_HZ=868E6 - -; Diagnostic build: enables extended meter fault telemetry via DEBUG_METER_DIAG. -; Use for investigating meter error rates; disable in production. -[env:lilygo-t3-v1-6-1-diag] -platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip -board = ttgo-lora32-v1 -framework = arduino -lib_deps = - sandeepmistry/LoRa@^0.8.0 - bblanchon/ArduinoJson@^6.21.5 - adafruit/Adafruit SSD1306@^2.5.9 - adafruit/Adafruit GFX Library@^1.11.9 - knolleary/PubSubClient@^2.8 -build_flags = - -DSERIAL_DEBUG_MODE_FLAG=1 - -DDEBUG_METER_DIAG - -; Power-optimised sender build: light-sleep between 1 Hz samples, serial off. -; Use for long-duration battery-life measurements and production deployments. -[env:lilygo-t3-v1-6-1-lowpower] -platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip -board = ttgo-lora32-v1 -framework = arduino -lib_deps = - sandeepmistry/LoRa@^0.8.0 - bblanchon/ArduinoJson@^6.21.5 - adafruit/Adafruit SSD1306@^2.5.9 - adafruit/Adafruit GFX Library@^1.11.9 - knolleary/PubSubClient@^2.8 +; --- Production: serial off, light-sleep on --- +[env:production] build_flags = + ${env.build_flags} -DSERIAL_DEBUG_MODE_FLAG=0 -DENABLE_LIGHT_SLEEP_IDLE=1 -; Power-optimised + 868 MHz variant. -[env:lilygo-t3-v1-6-1-868-lowpower] -platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip -board = ttgo-lora32-v1 -framework = arduino -lib_deps = - sandeepmistry/LoRa@^0.8.0 - bblanchon/ArduinoJson@^6.21.5 - adafruit/Adafruit SSD1306@^2.5.9 - adafruit/Adafruit GFX Library@^1.11.9 - knolleary/PubSubClient@^2.8 -build_flags = - -DSERIAL_DEBUG_MODE_FLAG=0 - -DENABLE_LIGHT_SLEEP_IDLE=1 - -DLORA_FREQUENCY_HZ=868E6 - -; Power-optimised sender build with debug output enabled for validation. -; Use during the measurement / verification phase. -[env:lilygo-t3-v1-6-1-lowpower-debug] -platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip -board = ttgo-lora32-v1 -framework = arduino -lib_deps = - sandeepmistry/LoRa@^0.8.0 - bblanchon/ArduinoJson@^6.21.5 - adafruit/Adafruit SSD1306@^2.5.9 - adafruit/Adafruit GFX Library@^1.11.9 - knolleary/PubSubClient@^2.8 +; --- Debug: serial + all diagnostics, real meter data --- +; Does NOT enable test mode — uses real meter + real LoRa. +[env:debug] build_flags = + ${env.build_flags} -DSERIAL_DEBUG_MODE_FLAG=1 -DENABLE_LIGHT_SLEEP_IDLE=1 -DDEBUG_METER_DIAG + -DDD3_DEBUG + +; --- Test: synthetic meter samples, payload codec self-test at boot --- +; Replaces real meter reading with fake 1 Hz data and publishes to test MQTT topic. +; Use for bench testing without a physical meter attached. +[env:test] +build_flags = + ${env.build_flags} + -DSERIAL_DEBUG_MODE_FLAG=1 + -DENABLE_TEST_MODE + -DPAYLOAD_CODEC_TEST + -DDEBUG_METER_DIAG + -DDD3_DEBUG