From 6718f053084704c1f4b816800bb842b1112d169f Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sat, 19 Sep 2026 12:04:26 +0530 Subject: [PATCH] cmdGetBattery: take a profile like its siblings control.dart already branches on profile.isGen5 to decode this exact opcode's reply (gen4 u16 deci-percent vs gen5 direct u8 percent), but the request builder had no way to frame gen5 at all. same fix as cmdAbortHistorical got in #62. --- lib/src/commands.dart | 4 ++-- test/gen5_command_surface_test.dart | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/src/commands.dart b/lib/src/commands.dart index fb339db..256ceb5 100644 --- a/lib/src/commands.dart +++ b/lib/src/commands.dart @@ -95,8 +95,8 @@ final List initPackets = [ // ── Convenience builders for live ops ────────────────────────────────────── Uint8List cmdLinkValid(int seq) => buildCommand(seq, Cmd.linkValid, const [0x00]); -Uint8List cmdGetBattery(int seq) => - buildCommand(seq, Cmd.getBatteryLevel, const []); +Uint8List cmdGetBattery(int seq, {BandProfile profile = BandProfile.gen4}) => + buildCommand(seq, Cmd.getBatteryLevel, const [], profile); Uint8List cmdGetHello(int seq) => buildCommand(seq, Cmd.getHelloHarvard, const [0x00]); Uint8List cmdGetHelloModern(int seq) => diff --git a/test/gen5_command_surface_test.dart b/test/gen5_command_surface_test.dart index 9e2b444..87a4d45 100644 --- a/test/gen5_command_surface_test.dart +++ b/test/gen5_command_surface_test.dart @@ -461,6 +461,25 @@ void main() { expect(f4.opcode, Cmd.abortHistoricalTransmits); }); + test('GET_BATTERY_LEVEL (26) is profile-aware', () { + // control.dart's decode branches on profile.isGen5 for this exact + // opcode's reply (u8 percent vs u16 LE deci-percent), so the request + // builder must be able to frame gen5 too. + final gen5Frame = cmdGetBattery(1, profile: gen5); + final f = parseFrame(gen5Frame, profile: gen5)!; + expect(f.valid, isTrue, reason: 'must parse as a gen5 frame'); + expect(f.opcode, Cmd.getBatteryLevel); + final asGen4 = parseFrame(gen5Frame, profile: BandProfile.gen4); + expect(asGen4 == null || !asGen4.valid, isTrue, + reason: 'a gen5 frame must not also parse as valid gen4'); + + // Default (no profile arg) stays gen4 — backward compatible. + final gen4Frame = cmdGetBattery(1); + final f4 = parseFrame(gen4Frame, profile: BandProfile.gen4)!; + expect(f4.valid, isTrue); + expect(f4.opcode, Cmd.getBatteryLevel); + }); + // Filtered reading ("Labrador", R17) — the three lifecycle toggles. // Every body is [revision 01][operation]; 124's operation is // NOT a boolean.