From 086a45706b867311510b136b90a518ae37a291fd Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sat, 5 Dec 2026 08:14:12 +0530 Subject: [PATCH 1/3] gen4 set alarm reply: don't read the echoed epoch's low byte as alarm status --- lib/src/control.dart | 10 +++++++++- test/control_plane_offsets_test.dart | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/src/control.dart b/lib/src/control.dart index 9e9a5f1..f3dfd29 100644 --- a/lib/src/control.dart +++ b/lib/src/control.dart @@ -966,7 +966,15 @@ CmdResponse? parseCommandResponse(Uint8List inner, // The revision byte at body 0 is read but NOT gated on: it reads 3 for // SET and 2 for RUN, and a strap answering with a revision we do not // recognise still put a status byte where the status byte goes. - if (payload.length >= 4) { + // + // gen4's real SET reply is `[rev=1][u32 epoch LE][u16]` from body 0: the + // byte at payload[3] is the epoch's low byte, not a status, so no status + // is emitted for that layout. + final echoLayout = op == Cmd.setAlarmTime && + !profile.isGen5 && + payload.length >= 7 && + payload[2] == 1; + if (payload.length >= 4 && !echoLayout) { final code = payload[3]; dec['alarm_status'] = code; dec['alarm_status_name'] = AlarmStatus.name(code); diff --git a/test/control_plane_offsets_test.dart b/test/control_plane_offsets_test.dart index 3bc3baf..200d0ab 100644 --- a/test/control_plane_offsets_test.dart +++ b/test/control_plane_offsets_test.dart @@ -295,6 +295,13 @@ void main() { expect(r['alarm_status_name'], 'invalid_alarm_id'); }); + test('gen4 SET reply echoing the epoch has no status byte', () { + // epoch low byte 0x0a would otherwise read as invalid_alarm_time + final r = parseCommandResponse(cmdResponse( + Cmd.setAlarmTime, [1, 0x0a, 0x00, 0x00, 0x66, 0x00, 0x00]))!; + expect(r.decoded.containsKey('alarm_status'), isFalse); + }); + test('an unrecognised response revision does not suppress the status', () { // The SET reply's revision byte reads 3, but it is not a gate: a strap // answering with another revision still put the status where it goes. From a93ae904b68043ab7fabc0217e314737170a37c0 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sat, 5 Dec 2026 08:16:24 +0530 Subject: [PATCH 2/3] require a plausible epoch before treating the set reply as an echo, document it --- lib/src/constants.dart | 3 +++ lib/src/control.dart | 3 ++- test/control_plane_offsets_test.dart | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/src/constants.dart b/lib/src/constants.dart index 90b6f5e..9e8d151 100644 --- a/lib/src/constants.dart +++ b/lib/src/constants.dart @@ -542,6 +542,9 @@ class HapticsTermination { /// must check both. A response can carry SUCCESS as its outer result and still /// report here that the alarm was refused, which is the whole reason the byte /// exists: it is the only place the strap says *why*. +/// +/// Exception: a gen4 SET reply that echoes the stored epoch (rev 1, plausible +/// u32 at payload[3]) has no status byte; `alarm_status` is then absent. class AlarmStatus { static const int unknown = 0; static const int validInputPattern = 1; diff --git a/lib/src/control.dart b/lib/src/control.dart index f3dfd29..6e19363 100644 --- a/lib/src/control.dart +++ b/lib/src/control.dart @@ -973,7 +973,8 @@ CmdResponse? parseCommandResponse(Uint8List inner, final echoLayout = op == Cmd.setAlarmTime && !profile.isGen5 && payload.length >= 7 && - payload[2] == 1; + payload[2] == 1 && + u32(payload, 3) >= 1500000000; // plausible unix time (2017+) if (payload.length >= 4 && !echoLayout) { final code = payload[3]; dec['alarm_status'] = code; diff --git a/test/control_plane_offsets_test.dart b/test/control_plane_offsets_test.dart index 200d0ab..a92a02d 100644 --- a/test/control_plane_offsets_test.dart +++ b/test/control_plane_offsets_test.dart @@ -300,6 +300,11 @@ void main() { final r = parseCommandResponse(cmdResponse( Cmd.setAlarmTime, [1, 0x0a, 0x00, 0x00, 0x66, 0x00, 0x00]))!; expect(r.decoded.containsKey('alarm_status'), isFalse); + // status-shaped body with trailing bytes but no epoch keeps its status + final s = parseCommandResponse( + cmdResponse(Cmd.setAlarmTime, [1, 10, 0, 0, 0, 0, 0]))!; + expect(s.decoded['alarm_status'], 10); + expect(r.decoded.containsKey('alarm_status'), isFalse); }); test('an unrecognised response revision does not suppress the status', () { From b5e9d5158278d9897c431deaeea50250878c64ce Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sat, 5 Dec 2026 12:25:13 +0530 Subject: [PATCH 3/3] pin echo guard: gen5 keeps status, threshold boundary, drop dup assert --- test/control_plane_offsets_test.dart | 54 ++++++++++++++++++---------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/test/control_plane_offsets_test.dart b/test/control_plane_offsets_test.dart index a92a02d..8e531f3 100644 --- a/test/control_plane_offsets_test.dart +++ b/test/control_plane_offsets_test.dart @@ -96,8 +96,8 @@ void main() { test('gen4 (0x0B) sub-seconds: second u32, 1/32768 s, emitted only < 32768', () { // Body bytes of a real WHOOP 4.0 reply: sec 0x6af4a5dc, subsec 0x0248. - final real = parseCommandResponse(cmdResponse(Cmd.getClock, - [0xdc, 0xa5, 0xf4, 0x6a, 0x48, 0x02, 0x00, 0x00]))!; + final real = parseCommandResponse(cmdResponse( + Cmd.getClock, [0xdc, 0xa5, 0xf4, 0x6a, 0x48, 0x02, 0x00, 0x00]))!; expect(real.decoded['clock_epoch'], 0x6af4a5dc); expect(real.decoded['clock_subsec'], 584); final r = parseCommandResponse( @@ -119,8 +119,8 @@ void main() { () { // [rev=1][u32 sec][u16 subsec] plus two trailing bytes: enough length for // the gen4 u32 read, which is not this reply's layout. - final r = parseCommandResponse(cmdResponse(Cmd.getClockGen5, - [1, ...le32(1786000000), 0x48, 0x02, 0x00, 0x00]))!; + final r = parseCommandResponse(cmdResponse( + Cmd.getClockGen5, [1, ...le32(1786000000), 0x48, 0x02, 0x00, 0x00]))!; expect(r.decoded['clock_epoch'], 1786000000); expect(r.decoded.containsKey('clock_subsec'), isFalse); }); @@ -304,7 +304,22 @@ void main() { final s = parseCommandResponse( cmdResponse(Cmd.setAlarmTime, [1, 10, 0, 0, 0, 0, 0]))!; expect(s.decoded['alarm_status'], 10); - expect(r.decoded.containsKey('alarm_status'), isFalse); + }); + + test('echo guard: gen5 keeps status, threshold boundary is inclusive', () { + // same echo-shaped bytes on gen5 are a real status layout + final g5 = parseCommandResponse( + cmdResponse( + Cmd.setAlarmTime, [1, 0x0a, 0x00, 0x00, 0x66, 0x00, 0x00]), + profile: BandProfile.gen5)!; + expect(g5.decoded['alarm_status'], 10); + // 1500000000 = 0x59682F00 -> echo; one below -> status kept + final at = parseCommandResponse( + cmdResponse(Cmd.setAlarmTime, [1, 0x00, 0x2f, 0x68, 0x59, 0, 0]))!; + expect(at.decoded.containsKey('alarm_status'), isFalse); + final below = parseCommandResponse( + cmdResponse(Cmd.setAlarmTime, [1, 0xff, 0x2e, 0x68, 0x59, 0, 0]))!; + expect(below.decoded['alarm_status'], 0xff); }); test('an unrecognised response revision does not suppress the status', () { @@ -346,7 +361,8 @@ void main() { Map get4(int activeFlag) => parseCommandResponse( cmdResponse(Cmd.getAlarmTime, [0x04, activeFlag, ...le32(1786000000), ...le16(0)]), - )!.decoded; + )! + .decoded; expect(get4(1)['alarm_active'], isTrue); expect(get4(1)['alarm_epoch'], 1786000000); expect(get4(0)['alarm_active'], isFalse); @@ -385,8 +401,8 @@ void main() { // semantics are unconfirmed on gen4, so gating it would silently drop // valid alarm reads rather than risk a stale one. final body = [0x04, 1, ...le32(1786000000), ...le16(0)]; - final r = parseCommandResponse( - cmdResponse(Cmd.getAlarmTime, body, status: 0))!; + final r = + parseCommandResponse(cmdResponse(Cmd.getAlarmTime, body, status: 0))!; expect(r.decoded['alarm_epoch'], 1786000000); expect(r.decoded['alarm_active'], isTrue); }); @@ -406,7 +422,8 @@ void main() { } }); - test('GET_BODY_LOCATION_AND_STATUS: gen5 a success outer status still ' + test( + 'GET_BODY_LOCATION_AND_STATUS: gen5 a success outer status still ' 'decodes normally', () { final body = [0x01, 0x00, 0xFF, 0x07]; final r = parseCommandResponse( @@ -473,8 +490,8 @@ void main() { }); test('a declared length longer than the frame is clamped, not thrown', () { - final e = parseEvent(envelopePacket(0x30, EventId.setRtc, [0xAA], - declaredLen: 64))!; + final e = parseEvent( + envelopePacket(0x30, EventId.setRtc, [0xAA], declaredLen: 64))!; expect(e.body, [0xAA]); }); @@ -595,10 +612,10 @@ void main() { }); test('HAPTICS_TERMINATED(100) separates expiry, error and dismissal', () { - Map terminated(int code) => parseEvent( - envelopePacket(0x30, EventId.hapticsTerminated, [1, code]), - profile: BandProfile.gen5)! - .decoded; + Map terminated(int code) => + parseEvent(envelopePacket(0x30, EventId.hapticsTerminated, [1, code]), + profile: BandProfile.gen5)! + .decoded; expect(terminated(0)['haptics_termination'], 'expired'); expect(terminated(1)['haptics_termination'], 'error'); @@ -676,8 +693,8 @@ void main() { }); test('the other gen5-scoped ids stay numeric and un-decoded on gen4', () { - final h = parseEvent( - envelopePacket(0x30, EventId.hapticsTerminated, [1, 2]))!; + final h = + parseEvent(envelopePacket(0x30, EventId.hapticsTerminated, [1, 2]))!; expect(h.name, 'EVENT_${EventId.hapticsTerminated}'); expect(h.decoded, isEmpty); }); @@ -751,8 +768,7 @@ void main() { test( 'a caller that only flushes on hasCompletedRun keeps a 4-chunk line ' - 'whole, even though every add() returns false on a fresh instance', - () { + 'whole, even though every add() returns false on a fresh instance', () { // add() returning false on the very first chunk into a fresh instance // used to be indistinguishable from a real gap. A caller that flushed // on every `false` (the old documented contract) reset _lastIndex each