From b4b425bc512f8b0ab8179ba9327eec7e285cbfbc Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Mon, 21 Sep 2026 22:37:20 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20zbyl=C3=A9=20t=C5=99i=20n=C3=A1lezy?= =?UTF-8?q?=20z=20review=20=E2=80=94=20rozpo=C4=8Det,=20rename,=20duplicit?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5 --- src/helpers/AdvertDataHelpers.h | 19 +++++++ src/helpers/BatteryIcon.h | 15 +++--- src/helpers/CommonCLI.cpp | 46 ++++++++++++----- src/helpers/CommonCLI.h | 3 +- test/test_battery_icon/test_battery_icon.cpp | 52 ++++++++++++++++---- 5 files changed, 104 insertions(+), 31 deletions(-) diff --git a/src/helpers/AdvertDataHelpers.h b/src/helpers/AdvertDataHelpers.h index f4e109e9..4c393e10 100644 --- a/src/helpers/AdvertDataHelpers.h +++ b/src/helpers/AdvertDataHelpers.h @@ -31,6 +31,25 @@ public: void setFeat1(uint16_t extra) { _extra1 = extra; } void setFeat2(uint16_t extra) { _extra2 = extra; } + void setName(const char* name) { _name = name; } + + /** + * \brief bytes left for the name with the fields set so far. + * + * CUSTOM (TeTeHacko): the name is encoded last and clamped to whatever is + * left, so anyone who wants to append to it (battery icon, see + * helpers/BatteryIcon.h) has to know the remainder. Deriving it a second + * time elsewhere goes stale the moment a field is added -- a copy in + * BatteryIcon.h already missed FEAT1/FEAT2 and would have let the UTF-8 + * clamp cut a glyph in half. + */ + size_t nameBudget() const { + size_t used = 1; // flags + if (_has_loc) used += 8; // lat + lon + if (_extra1) used += 2; + if (_extra2) used += 2; + return MAX_ADVERT_DATA_SIZE - used; + } /** * \brief encode the given advertisement data. diff --git a/src/helpers/BatteryIcon.h b/src/helpers/BatteryIcon.h index b1fdbba3..97a231f7 100644 --- a/src/helpers/BatteryIcon.h +++ b/src/helpers/BatteryIcon.h @@ -89,15 +89,14 @@ inline size_t battIconWidth() { return (sizeof(BATT_ICON_MARKER) - 1) + (lvl > ext ? lvl : ext); } -// Bytes of app_data left for the name. Mirrors AdvertDataBuilder::encodeTo(): -// one byte of flags, plus 4+4 for lat/lon when a position is advertised. -inline size_t advertNameBudget(bool with_loc) { - return MAX_ADVERT_DATA_SIZE - 1 - (with_loc ? 8 : 0); -} - // Does the name still fit with the indicator appended? -inline bool battIconFits(size_t name_len, bool with_loc) { - return name_len + battIconWidth() <= advertNameBudget(with_loc); +// +// `budget` musí přijít z AdvertDataBuilder::nameBudget(), ne z druhého výpočtu +// tady: rozpočet závisí i na FEAT1/FEAT2, a kopie, která o nich nevěděla, by +// tvrdila o 2-4 bajty víc, než je pravda. Pak by UTF-8 clamp uřízl glyf +// vpůli -- přesně to tiché selhání, které tenhle soubor jinde odmítá. +inline bool battIconFits(size_t name_len, size_t budget) { + return name_len + battIconWidth() <= budget; } // Copy the glyph for `level` into dest, which must hold diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 21c97ff0..ff520d82 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -175,15 +175,25 @@ void CommonCLI::savePrefs() { _prefs->clearDirty(); } +// Rozpočet na jméno pro současné nastavení advertu. Staví prázdný builder +// a ptá se JEHO, ať `set`, `get` i samotný advert počítají z jednoho místa -- +// druhá kopie toho výpočtu už jednou zapomněla na FEAT1/FEAT2. +size_t CommonCLI::nameBudget() const { + if (_prefs->advert_loc_policy == ADVERT_LOC_NONE) { + return AdvertDataBuilder(ADV_TYPE_NONE).nameBudget(); + } + return AdvertDataBuilder(ADV_TYPE_NONE, NULL, 0.0, 0.0).nameBudget(); +} + // Name as it goes on the air: the configured name, plus the solar marker and // either a charge level or the external-power glyph. Returns the bare name in // every other case -- an indicator that does not fit is dropped whole rather // than letting the UTF-8 clamp eat half of it silently. -const char* CommonCLI::advertName(bool with_loc) { +const char* CommonCLI::advertName(size_t budget) { if (!_prefs->batt_icon) return _prefs->node_name; size_t nlen = strlen(_prefs->node_name); - if (!mesh::battIconFits(nlen, with_loc)) return _prefs->node_name; + if (!mesh::battIconFits(nlen, budget)) return _prefs->node_name; char glyph[8]; if (_board->isExternalPowered()) { @@ -207,14 +217,20 @@ const char* CommonCLI::advertName(bool with_loc) { } uint8_t CommonCLI::buildAdvertData(uint8_t node_type, uint8_t* app_data) { + // Builder se postaví bez jména, protože rozpočet na jméno závisí na tom, co + // v něm už je (poloha, případné FEAT). Teprve pak se jméno doplní -- jinak + // by se remainder musel počítat podruhé jinde a rozejít se s encodeTo(). if (_prefs->advert_loc_policy == ADVERT_LOC_NONE) { - AdvertDataBuilder builder(node_type, advertName(false)); + AdvertDataBuilder builder(node_type); + builder.setName(advertName(builder.nameBudget())); return builder.encodeTo(app_data); } else if (_prefs->advert_loc_policy == ADVERT_LOC_SHARE) { - AdvertDataBuilder builder(node_type, advertName(true), _sensors->node_lat, _sensors->node_lon); + AdvertDataBuilder builder(node_type, NULL, _sensors->node_lat, _sensors->node_lon); + builder.setName(advertName(builder.nameBudget())); return builder.encodeTo(app_data); } else { - AdvertDataBuilder builder(node_type, advertName(true), _prefs->node_lat, _prefs->node_lon); + AdvertDataBuilder builder(node_type, NULL, _prefs->node_lat, _prefs->node_lon); + builder.setName(advertName(builder.nameBudget())); return builder.encodeTo(app_data); } } @@ -686,7 +702,16 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep if (isValidName(&config[5])) { StrHelper::strncpy(_prefs->node_name, &config[5], sizeof(_prefs->node_name)); savePrefs(); - strcpy(reply, "OK"); + // Přejmenování může vytlačit zapnutou ikonku z advertu, a to je změna, + // kterou by nikdo neviděl: `set batt.icon` i `get batt.icon` na to + // upozorňují, ale rename je ta cesta, kterou operátor jde později -- + // a bez tohohle by mu indikátor zmizel ze vzduchu bez jediné hlášky. + if (_prefs->batt_icon && !mesh::battIconFits(strlen(_prefs->node_name), nameBudget())) { + sprintf(reply, "OK - ALE jméno má %d B, s ikonkou se vejde jen %d B - batt.icon se zahodí", + (int)strlen(_prefs->node_name), (int)(nameBudget() - mesh::battIconWidth())); + } else { + strcpy(reply, "OK"); + } } else { strcpy(reply, "Error, bad chars"); } @@ -835,14 +860,12 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep savePrefs(); if (!_prefs->batt_icon) { strcpy(reply, "off"); - } else if (!mesh::battIconFits(strlen(_prefs->node_name), - _prefs->advert_loc_policy != ADVERT_LOC_NONE)) { + } else if (!mesh::battIconFits(strlen(_prefs->node_name), nameBudget())) { // Say so now rather than let the operator wonder why nothing shows up: // the icon is dropped whole when it doesn't fit. sprintf(reply, "on - BUT name is %d B, max %d B with icon - icon omitted", (int)strlen(_prefs->node_name), - (int)(mesh::advertNameBudget(_prefs->advert_loc_policy != ADVERT_LOC_NONE) - - mesh::battIconWidth())); + (int)(nameBudget() - mesh::battIconWidth())); } else { strcpy(reply, "on - shows on next advert"); } @@ -910,10 +933,9 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep // Report what actually goes on the air, not just the flag: the indicator // is dropped when the name leaves no room, and the voltage is the only way // to tell "not there yet" from "board reads nothing". - bool with_loc = _prefs->advert_loc_policy != ADVERT_LOC_NONE; if (!_prefs->batt_icon) { strcpy(reply, "> off"); - } else if (!mesh::battIconFits(strlen(_prefs->node_name), with_loc)) { + } else if (!mesh::battIconFits(strlen(_prefs->node_name), nameBudget())) { strcpy(reply, "> on (omitted, name too long)"); } else if (_board->isExternalPowered()) { strcpy(reply, "> on (external power)"); diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index 23edd856..f1f55f25 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -335,7 +335,8 @@ class CommonCLI { uint8_t _batt_level = 0; // hysteresis state, see BatteryIcon.h mesh::RTCClock* getRTCClock() { return _rtc; } - const char* advertName(bool with_loc); + const char* advertName(size_t budget); + size_t nameBudget() const; void savePrefs(); void loadPrefsInt(FILESYSTEM* _fs, const char* filename); diff --git a/test/test_battery_icon/test_battery_icon.cpp b/test/test_battery_icon/test_battery_icon.cpp index a2ad6224..99f1aade 100644 --- a/test/test_battery_icon/test_battery_icon.cpp +++ b/test/test_battery_icon/test_battery_icon.cpp @@ -3,6 +3,7 @@ #include #include #include +#include // The whole budget rests on these being 3 bytes, not 4. Block elements and // U+2600/U+26A1 are BMP; battery emoji are not, which is why they lost. @@ -14,22 +15,53 @@ TEST(BatteryIcon, GlyphsAreThreeBytesAndThereAreEightLevels) { EXPECT_EQ(6u, mesh::battIconWidth()); // marker + one glyph } -TEST(BatteryIcon, BudgetMatchesAdvertLayout) { - EXPECT_EQ(23u, mesh::advertNameBudget(true)); // 32 - 1 flags - 8 lat/lon - EXPECT_EQ(31u, mesh::advertNameBudget(false)); +// Rozpočet teď hlásí BUILDER, ne kopie v BatteryIcon.h -- a to je celý smysl +// té změny: kopie nevěděla o FEAT1/FEAT2 a tvrdila by o 2-4 bajty víc. +TEST(BatteryIcon, BuilderReportsTheNameBudget) { + EXPECT_EQ(31u, AdvertDataBuilder(ADV_TYPE_REPEATER).nameBudget()); // jen flagy + EXPECT_EQ(23u, AdvertDataBuilder(ADV_TYPE_REPEATER, NULL, 50.4, 12.8).nameBudget()); // + lat/lon +} + +TEST(BatteryIcon, BudgetShrinksWithFeatureWords) { + AdvertDataBuilder b(ADV_TYPE_REPEATER, NULL, 50.4, 12.8); + ASSERT_EQ(23u, b.nameBudget()); + b.setFeat1(0x1234); + EXPECT_EQ(21u, b.nameBudget()); + b.setFeat2(0x5678); + EXPECT_EQ(19u, b.nameBudget()); + // A tohle je ta chyba, kterou by stará kopie udělala: s FEAT1+FEAT2 by + // 17bajtové jméno pořád hlásila jako vejde-se, a UTF-8 clamp by uřízl + // kus glyfu. + EXPECT_FALSE(mesh::battIconFits(strlen("mast-repeater-one"), b.nameBudget())); +} + +// Rozpočet z builderu musí souhlasit s tím, co encodeTo() opravdu nechá. +TEST(BatteryIcon, BudgetAgreesWithEncoder) { + for (int with_loc = 0; with_loc < 2; with_loc++) { + AdvertDataBuilder b = with_loc ? AdvertDataBuilder(ADV_TYPE_REPEATER, NULL, 50.4, 12.8) + : AdvertDataBuilder(ADV_TYPE_REPEATER); + size_t budget = b.nameBudget(); + std::string name(budget, 'a'); // přesně na doraz + b.setName(name.c_str()); + uint8_t app_data[MAX_ADVERT_DATA_SIZE]; + uint8_t len = b.encodeTo(app_data); + AdvertDataParser p(app_data, len); + ASSERT_TRUE(p.isValid()); + EXPECT_STREQ(name.c_str(), p.getName()) << "with_loc=" << with_loc; + } } // The three deployed names, at 6 bytes of indicator. TEST(BatteryIcon, FitsTheDeployedNames) { - EXPECT_TRUE(mesh::battIconFits(strlen("mast-rpt-two"), true)); // 12 + 6 = 18 - EXPECT_TRUE(mesh::battIconFits(strlen("mast-repeater-one"), true)); // 17 + 6 = 23, exactly - EXPECT_TRUE(mesh::battIconFits(strlen("mast-rpt-three.cz"), true)); // 17 + 6 = 23, exactly - EXPECT_FALSE(mesh::battIconFits(18, true)); // one byte over + EXPECT_TRUE(mesh::battIconFits(strlen("mast-rpt-two"), 23)); // 12 + 6 = 18 + EXPECT_TRUE(mesh::battIconFits(strlen("mast-repeater-one"), 23)); // 17 + 6 = 23, exactly + EXPECT_TRUE(mesh::battIconFits(strlen("mast-rpt-three.cz"), 23)); // 17 + 6 = 23, exactly + EXPECT_FALSE(mesh::battIconFits(18, 23)); // one byte over } TEST(BatteryIcon, DroppingPositionBuysEightBytes) { - EXPECT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), true)); // 21 + 6 = 27 - EXPECT_TRUE(mesh::battIconFits(strlen("tth-mast-repeater-one"), false)); // 31 B budget + EXPECT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), 23)); // 21 + 6 = 27 + EXPECT_TRUE(mesh::battIconFits(strlen("tth-mast-repeater-one"), 31)); // 31 B budget } // Every level must be the same width, or the name length would change with @@ -137,7 +169,7 @@ TEST(BatteryIcon, EncoderSilentlyDropsAnIndicatorThatDoesNotFit) { char name[48]; snprintf(name, sizeof(name), "tth-mast-repeater-one%s%s", BATT_ICON_MARKER, glyph); ASSERT_EQ(27u, strlen(name)); - ASSERT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), true)); + ASSERT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), 23)); uint8_t app_data[MAX_ADVERT_DATA_SIZE]; AdvertDataBuilder builder(ADV_TYPE_REPEATER, name, 50.38, 12.82);