feat(advert): stav baterie jako ikonka na konci advertovaného jména

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5 committed 2026-09-21 19:13:38 +02:00
1 parent 78bcabaa2f
commit 5f56a80a78
7 files changed
+331 -3

No files matched your search

+3
View File
@@ -2,6 +2,9 @@
#include <cstdint>
#include <cmath>
// Real Arduino.h pulls stdio in; code under test (AdvertTimeHelper) relies on
// sprintf being available in the global namespace from this header alone.
#include <stdio.h>
#include "Stream.h"
inline uint32_t g_mock_millis = 0;
@@ -0,0 +1,110 @@
#include <gtest/gtest.h>
#include <helpers/BatteryIcon.h>
#include <helpers/AdvertDataHelpers.h>
#include <string.h>
// The icons are not one byte each, and the sun carries a variation selector
// that is invisible in an editor but costs three bytes. Pin both down: the
// whole budget math rests on these numbers.
TEST(BatteryIcon, IconsHaveTheExpectedByteLength) {
EXPECT_EQ(6u, strlen(BATT_ICON_OK)); // U+2600 + U+FE0F
EXPECT_EQ(4u, strlen(BATT_ICON_LOW)); // U+1FAAB
}
TEST(BatteryIcon, BudgetMatchesAdvertLayout) {
EXPECT_EQ(23u, mesh::advertNameBudget(true)); // 32 - 1 flags - 8 lat/lon
EXPECT_EQ(31u, mesh::advertNameBudget(false));
}
// The real names of the three nodes this was written for.
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(strlen("tth-mast-repeater-one"), true)); // 21 + 6 = 27
}
// A name is only accepted when the LONGER icon fits. Budget for the current
// state instead and the name would change length as the battery crosses the
// threshold, rewriting the contact in every client that hears it.
TEST(BatteryIcon, BudgetsForTheLongerIconNotTheCurrentOne) {
// 18 bytes: the 4-byte low icon would fit (22), the 6-byte one would not (24).
EXPECT_FALSE(mesh::battIconFits(18, true));
EXPECT_TRUE(mesh::battIconFits(17, true));
}
TEST(BatteryIcon, DroppingPositionBuysEightBytes) {
EXPECT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), true));
EXPECT_TRUE(mesh::battIconFits(strlen("tth-mast-repeater-one"), false));
}
TEST(BatteryIcon, HysteresisNeedsARealRecoveryNotJustTheThreshold) {
const uint16_t low = 3700, hyst = 100;
EXPECT_FALSE(mesh::battIconIsLow(false, 3701, low, hyst)); // above, stays ok
EXPECT_TRUE(mesh::battIconIsLow(false, 3699, low, hyst)); // crosses down
EXPECT_TRUE(mesh::battIconIsLow(true, 3750, low, hyst)); // back above, but inside hysteresis
EXPECT_TRUE(mesh::battIconIsLow(true, 3800, low, hyst)); // still inside, boundary
EXPECT_FALSE(mesh::battIconIsLow(true, 3801, low, hyst)); // genuinely recovered
}
// The measured daily swing of a solar node under weak sun was 38-46 mV. The
// icon must not follow sunrise and sunset.
TEST(BatteryIcon, SurvivesTheMeasuredDailySwing) {
bool low = mesh::battIconIsLow(false, 3690, 3700, BATT_ICON_HYST_MV);
ASSERT_TRUE(low);
// night down, day up, +-23 mV around 3690 -- must stay low the whole time
for (int i = 0; i < 10; i++) {
low = mesh::battIconIsLow(low, 3667, 3700, BATT_ICON_HYST_MV);
EXPECT_TRUE(low);
low = mesh::battIconIsLow(low, 3713, 3700, BATT_ICON_HYST_MV);
EXPECT_TRUE(low);
}
}
// End to end against the real encoder: a name sized to the budget keeps the
// icon intact, and the parser on the far side reads it back byte for byte.
TEST(BatteryIcon, EncoderKeepsTheIconOnAFittingName) {
char name[32];
snprintf(name, sizeof(name), "mast-repeater-one%s", BATT_ICON_OK);
ASSERT_EQ(23u, strlen(name));
uint8_t app_data[MAX_ADVERT_DATA_SIZE];
AdvertDataBuilder builder(ADV_TYPE_REPEATER, name, 50.38, 12.82);
uint8_t len = builder.encodeTo(app_data);
AdvertDataParser parser(app_data, len);
ASSERT_TRUE(parser.isValid());
ASSERT_TRUE(parser.hasName());
EXPECT_STREQ(name, parser.getName());
EXPECT_TRUE(parser.hasLatLon());
}
// And the failure mode we refuse to ship: one byte over and the clamp eats
// the icon whole, leaving a name that looks fine and silently lost its state.
TEST(BatteryIcon, EncoderSilentlyDropsAnIconThatDoesNotFit) {
char name[40];
snprintf(name, sizeof(name), "tth-mast-repeater-one%s", BATT_ICON_OK);
ASSERT_EQ(27u, strlen(name));
ASSERT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), true));
uint8_t app_data[MAX_ADVERT_DATA_SIZE];
AdvertDataBuilder builder(ADV_TYPE_REPEATER, name, 50.38, 12.82);
uint8_t len = builder.encodeTo(app_data);
AdvertDataParser parser(app_data, len);
ASSERT_TRUE(parser.isValid());
EXPECT_STREQ("tth-mast-repeater-one", parser.getName()); // icon gone, no error
}
// Whatever the icons are changed to, they must survive `set name`'s filter.
TEST(BatteryIcon, IconsPassTheNameCharacterFilter) {
EXPECT_TRUE(AdvertDataParser::isValidName(BATT_ICON_OK));
EXPECT_TRUE(AdvertDataParser::isValidName(BATT_ICON_LOW));
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}