Merge pull request #1510 from jonny5532/feature/battery-can-aliveness-test

Tweak CAN still-alive tests to more reliably construct batteries
This commit is contained in:
Daniel Öster 2025-09-08 18:18:39 +03:00 committed by GitHub
commit bf7d10c825
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 35 additions and 10 deletions

View file

@ -55,6 +55,7 @@ void setup_can_shunt();
#include "VOLVO-SPA-HYBRID-BATTERY.h"
void setup_battery(void);
Battery* create_battery(BatteryType type);
extern uint16_t user_selected_max_pack_voltage_dV;
extern uint16_t user_selected_min_pack_voltage_dV;

View file

@ -37,10 +37,10 @@ class BatteryTestFixture : public testing::Test {
BatteryType type;
};
// Check that the parsed logs correctly trigger an overvoltage event.
class StillAliveTimeoutTest : public BatteryTestFixture {
// Check that the CAN aliveness timeout isn't being renewed by bogus CAN frames
class TestNotStillAlive : public BatteryTestFixture {
public:
explicit StillAliveTimeoutTest(BatteryType type) : BatteryTestFixture(type) {}
explicit TestNotStillAlive(BatteryType type) : BatteryTestFixture(type) {}
void TestBody() override {
// check if battery is a CanBattery subclass
auto* battery = dynamic_cast<CanBattery*>(::battery);
@ -65,17 +65,41 @@ class StillAliveTimeoutTest : public BatteryTestFixture {
}
};
void RegisterStillAliveTests() {
for (int i = 2; i < 42; i++) {
if ((BatteryType)i == BatteryType::TestFake) {
continue;
bool IsValidCanBattery(BatteryType type) {
if (type == BatteryType::TestFake) {
return false;
}
if ((BatteryType)i == BatteryType::DalyBms) {
// We need some minimal setup or the battery constructors may segfault
datalayer = DataLayer();
// This leaks memory (but not much...)
init_hal();
auto* tmp_battery = create_battery(type);
if (tmp_battery == nullptr) {
// Not a valid battery type
return false;
}
auto* as_can_battery = dynamic_cast<CanBattery*>(tmp_battery);
if (as_can_battery == nullptr) {
// Failed to cast to CanBattery, so it's not a CAN battery
delete tmp_battery;
return false;
}
delete tmp_battery;
return true;
}
void RegisterStillAliveTests() {
for (int i = 0; i < (int)BatteryType::Highest; i++) {
if (!IsValidCanBattery((BatteryType)i)) {
continue;
}
std::string test_name = ("TestStillAliveTimeout" + snake_case_to_camel_case(name_for_battery_type((BatteryType)i)));
std::string test_name = ("TestNotStillAlive" + snake_case_to_camel_case(name_for_battery_type((BatteryType)i)));
testing::RegisterTest("StillAliveTests", test_name.c_str(), nullptr, nullptr, __FILE__, __LINE__,
[=]() -> BatteryTestFixture* { return new StillAliveTimeoutTest((BatteryType)i); });
[=]() -> BatteryTestFixture* { return new TestNotStillAlive((BatteryType)i); });
}
}