From 2386504e2db6444133c941c8a47123ab64560751 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Tue, 6 Oct 2026 23:46:40 +0200 Subject: [PATCH] blepwr/fairness: opravy z review-three MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `blepwr` parsuje striktně (strtol + kontrola celého argumentu a platné hodnoty) — `atoi` udělal z „216" −40 a z „abc" 0, a teď se to ukládá. Stará syntaxe `blepwr -20 hold` skončí ERR místo tichého uložení −20. - „saved" jen když zápis do InternalFS opravdu prošel, jinak „NOT saved". - mast-three env: RPT_BLE_CONN_TX_POWER=8, aby po flashi nenaběhl na 0 dBm, na kterých most z bridge-host přes zavřené okno padá. - FairnessLimiter: opravené odůvodnění 0x00/0xFF — náš companion identitu přegeneruje, ale na CZ meshi za 30 dní prošlo 50 floodů od takových odesílatelů a u ANON_REQ je payload[1] bajt náhodného klíče (2/256 všech). Test přejmenován (cap 100 je teď pod defaultem). - tools/fairness_replay.py: přehrání floodů z CoreScope přes model limiteru, odkud jsou výchozí hodnoty; pro přepočet, až se provoz změní. Ověřeno na x1: 216/264/abc/„-20 hold"/1 → ERR; 6 → saved → reboot → 6. Co-Authored-By: Claude Opus 5.5 --- examples/simple_repeater/main.cpp | 28 +++++++++++++------ src/helpers/FairnessLimiter.h | 15 ++++++---- .../test_fairness_limiter.cpp | 4 +-- variants/sensecap_solar/platformio.ini | 5 ++++ 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 869ef20e..caa70b59 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -483,11 +483,15 @@ bool identHandleCommand(const char* command, char* reply) { || (p >= 2 && p <= 8); } - static void bleConnTxPowerPersist(int8_t p) { + // true when the value will come back after a reboot + static bool bleConnTxPowerPersist(int8_t p) { InternalFS.remove(BLE_PWR_FILE); // FILE_O_WRITE appends; start clean - if (p == RPT_BLE_CONN_TX_POWER) return; // default needs no file + if (p == RPT_BLE_CONN_TX_POWER) return true; // default needs no file auto f = InternalFS.open(BLE_PWR_FILE, Adafruit_LittleFS_Namespace::FILE_O_WRITE); - if (f) { f.write((uint8_t)p); f.close(); } + if (!f) return false; + bool ok = f.write((uint8_t)p) == 1; + f.close(); + return ok; } static int8_t bleConnTxPowerLoad() { @@ -543,20 +547,28 @@ bool identHandleCommand(const char* command, char* reply) { // rejects anything else, hence the applied/failed count in the reply. if (memcmp(command, "blepwr", 6) == 0 && (command[6] == 0 || command[6] == ' ')) { if (command[6] == ' ') { - int8_t p = (int8_t) atoi(&command[7]); + // strict parse: now that the value is saved, a typo must not become + // permanent (atoi turned "216" into -40 and "abc" into 0) + char* end; + long v = strtol(&command[7], &end, 10); + if (end == &command[7] || *end != 0 || !bleConnTxPowerValid((int) v)) { + strcpy(reply, "ERR - blepwr : -40,-20,-16,-12,-8,-4,0,2..8"); + return true; + } + int8_t p = (int8_t) v; int live = 0; int ok = bleApplyConnTxPower(p, &live); // only latch a value the radio actually accepted, otherwise the next // reconnect would silently re-apply a bogus one bool applied = (live == 0 || ok > 0); - // with no live connection nothing has vetted the value yet - if (live == 0 && !bleConnTxPowerValid(p)) applied = false; + bool saved = false; if (applied) { ble_conn_tx_power = p; - bleConnTxPowerPersist(p); + saved = bleConnTxPowerPersist(p); } sprintf(reply, "%s - BLE conn tx %d dBm (%d/%d conn)%s", - applied ? "OK" : "ERR", (int) p, ok, live, applied ? ", saved" : ""); + applied ? "OK" : "ERR", (int) p, ok, live, + applied ? (saved ? ", saved" : ", NOT saved") : ""); } else { sprintf(reply, "BLE conn tx: %d dBm, adv tx: %d dBm", (int) ble_conn_tx_power, (int) Bluefruit.getTxPower()); diff --git a/src/helpers/FairnessLimiter.h b/src/helpers/FairnessLimiter.h index 5dafa608..860f12d7 100644 --- a/src/helpers/FairnessLimiter.h +++ b/src/helpers/FairnessLimiter.h @@ -19,10 +19,14 @@ #include #include -// Sender hashes 0x00 and 0xFF are NOT special here: repeaters, room servers -// and sensors re-roll such identities, but companions do not, so real users -// send with them (~1 in 128). Denying them always (as the original branch did) -// dropped every flood DM/PATH/REQ of those users. +// Sender hashes 0x00 and 0xFF are NOT special here. Our firmware never creates +// such an identity (re-roll on create, validatePrivateKey() on import), but the +// mesh still carries them: over 30 days (Sep-Oct 2026, CZ mesh) 50 floods of +// REQ/PATH/TXT_MSG/RESPONSE came from 0x00/0xFF senders -- older identities and +// other implementations. And for ANON_REQ payload[1] is the first byte of a +// random ephemeral key, so "always deny" also dropped 2 in 256 anonymous +// requests from anyone. Denying them always (as the original branch did) +// dropped every such flood; now they are limited like any other bucket. // Note: mask, cap and refill interval are highly connected parameters. #define GROUP_MASK 0b00011111 // 5-bit address #define SENDER_NORMAL_MASK 0b00111111 // 6-bit address @@ -32,7 +36,8 @@ // 1/interval, cap bounds the burst. Overridable per env from platformio ini. // // Sized from real traffic (6 Sep - 6 Oct 2026: 207k floods heard on the CZ -// mesh at one-site, replayed through this limiter). The zero-loss frontier +// mesh at one-site, replayed through this limiter with +// tools/fairness_replay.py -- rerun it when the traffic changes). The zero-loss frontier // over those 30 days was group 60 s/cap 60, sender 10 s/cap 60, advert 600 s/ // cap 12; the defaults below keep about 2x headroom on top. The busiest // legitimate channel runs ~26 msg/h around the clock, so the limiter is a net diff --git a/test/test_fairness_limiter/test_fairness_limiter.cpp b/test/test_fairness_limiter/test_fairness_limiter.cpp index 77474e1f..c8769d97 100644 --- a/test/test_fairness_limiter/test_fairness_limiter.cpp +++ b/test/test_fairness_limiter/test_fairness_limiter.cpp @@ -99,7 +99,7 @@ TEST(FairnessLimiter, SenderBucketKeysOnSrcHash) { } TEST(FairnessLimiter, SenderHashes00AndFFAreOrdinaryBuckets) { - // Companions don't re-roll 0x00/0xFF identities, so these are real users. + // Real senders on the mesh, and every ANON_REQ with such an ephemeral key. FairnessLimiter fl; fl.refillSenderNormal(); fl.refillSenderLow(); @@ -157,7 +157,7 @@ TEST(FairnessLimiter, SetCapZeroRestoresDefault) { EXPECT_EQ(fl.groupCap(), FAIRNESS_GROUP_CAP); } -TEST(FairnessLimiter, RaisedSenderCapAllowsBiggerBurst) { +TEST(FairnessLimiter, SetSenderCapOverridesDefault) { FairnessLimiter fl; fl.setSenderNormalCap(100); for (int i = 0; i < 200; i++) fl.refillSenderNormal(); // fill to the raised cap diff --git a/variants/sensecap_solar/platformio.ini b/variants/sensecap_solar/platformio.ini index adb498c8..bba34037 100644 --- a/variants/sensecap_solar/platformio.ini +++ b/variants/sensecap_solar/platformio.ini @@ -67,6 +67,11 @@ build_flags = ; Real BLE PIN lives ONLY in the git-ignored platformio.local.ini ([secrets] ble_pin). ; The committed default (123456) is a non-secret placeholder. -D BLE_PIN_CODE=${secrets.ble_pin} + ; BLE CONNECTION tx power from the first boot (SoftDevice default 0 dBm). + ; The bridge on bridge-host reaches mast-three through a closed window and drops the + ; link at 0 dBm. Advertising is already +8 (simple_repeater/main.cpp); + ; runtime: `blepwr `, saved in /ble_pwr. + -D RPT_BLE_CONN_TX_POWER=8 ; INA226 defaults to 0x44 in EnvironmentSensorManager = collision with SHT41 ; (false detection, garbage in telemetry). 0x40 is the real INA226 HW default. -D TELEM_INA226_ADDRESS=0x40