From c938a144148f718d048d420a2ce49432f868fe76 Mon Sep 17 00:00:00 2001 From: magnetarQ <340235260+magnetarQ@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:43:05 +0300 Subject: [PATCH 1/3] gen4 GET_CLOCK: emit the reply's sub-seconds (1/32768 s) --- lib/src/control.dart | 14 +++++++++++++- test/control_plane_offsets_test.dart | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/src/control.dart b/lib/src/control.dart index 0a860cd..f48e028 100644 --- a/lib/src/control.dart +++ b/lib/src/control.dart @@ -1014,7 +1014,19 @@ CmdResponse? parseCommandResponse(Uint8List inner, final v = u32(payload, at); // Emit nothing rather than a guess: a value outside the plausible // window means we are not looking at the clock field. - if (_plausibleUnix(v)) dec['clock_epoch'] = v; + if (_plausibleUnix(v)) { + dec['clock_epoch'] = v; + // gen4 only: the second u32 is the sub-second count in 1/32768 s, the + // same unit as the record/event sub-second fields. On a WHOOP 4.0 + // (harvard 41.17.6.0) all 13 replies we logged had it < 32768 (upper + // half 0), and only with it do the clock-offset bounds the replies give + // ([sec + subsec/32768 - t_reply, ... - t_write]) agree within a day. + // Anything >= 32768 is not that count, so it is not emitted. + if (!profile.isGen5 && payload.length >= at + 8) { + final sub = u32(payload, at + 4); + if (sub < 32768) dec['clock_subsec'] = sub; + } + } } } else if (op == Cmd.getDataRange) { // 65-byte reply body, starting at payload[2] (payload[0] = echoed request diff --git a/test/control_plane_offsets_test.dart b/test/control_plane_offsets_test.dart index 4c4af65..555d2b2 100644 --- a/test/control_plane_offsets_test.dart +++ b/test/control_plane_offsets_test.dart @@ -93,6 +93,28 @@ void main() { expect(r.decoded['clock_epoch'], isNot(straddle)); }); + 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]))!; + expect(real.decoded['clock_epoch'], 0x6af4a5dc); + expect(real.decoded['clock_subsec'], 584); + final r = parseCommandResponse( + cmdResponse(Cmd.getClock, [...le32(1786000000), ...le32(32767)]))!; + expect(r.decoded['clock_subsec'], 32767); + final big = parseCommandResponse( + cmdResponse(Cmd.getClock, [...le32(1786000000), ...le32(32768)]))!; + expect(big.decoded['clock_epoch'], 1786000000); + expect(big.decoded.containsKey('clock_subsec'), isFalse); + final noSub = parseCommandResponse( + cmdResponse(Cmd.getClock, [...le32(1786000000), 0x48, 0x02]))!; + expect(noSub.decoded.containsKey('clock_subsec'), isFalse); + final bad = parseCommandResponse( + cmdResponse(Cmd.getClock, [...le32(0), ...le32(584)]))!; + expect(bad.decoded.containsKey('clock_subsec'), isFalse); + }); + test('gen5 (0x93) epoch is the u32 at payload[3], after the revision byte', () { const sec = 1786000123; From bd92b6489a062eb41f242faa364f59a9e1d7ee48 Mon Sep 17 00:00:00 2001 From: magnetarQ <340235260+magnetarQ@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:53:04 +0300 Subject: [PATCH 2/3] GET_CLOCK: read clock_subsec only from the gen4 opcode --- lib/src/control.dart | 4 +++- test/control_plane_offsets_test.dart | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/src/control.dart b/lib/src/control.dart index f48e028..5a45f9a 100644 --- a/lib/src/control.dart +++ b/lib/src/control.dart @@ -1022,7 +1022,9 @@ CmdResponse? parseCommandResponse(Uint8List inner, // half 0), and only with it do the clock-offset bounds the replies give // ([sec + subsec/32768 - t_reply, ... - t_write]) agree within a day. // Anything >= 32768 is not that count, so it is not emitted. - if (!profile.isGen5 && payload.length >= at + 8) { + if (op == Cmd.getClock && + !profile.isGen5 && + payload.length >= at + 8) { final sub = u32(payload, at + 4); if (sub < 32768) dec['clock_subsec'] = sub; } diff --git a/test/control_plane_offsets_test.dart b/test/control_plane_offsets_test.dart index 555d2b2..8017a6d 100644 --- a/test/control_plane_offsets_test.dart +++ b/test/control_plane_offsets_test.dart @@ -115,6 +115,16 @@ void main() { expect(bad.decoded.containsKey('clock_subsec'), isFalse); }); + test('a gen5 (0x93) reply on the gen4 profile never yields clock_subsec', + () { + // [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]))!; + expect(r.decoded['clock_epoch'], 1786000000); + expect(r.decoded.containsKey('clock_subsec'), isFalse); + }); + test('gen5 (0x93) epoch is the u32 at payload[3], after the revision byte', () { const sec = 1786000123; From 290f28ebf1d75b5fb0f3747b39125c3e28e46a0e Mon Sep 17 00:00:00 2001 From: magnetarQ <340235260+magnetarQ@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:06:46 +0300 Subject: [PATCH 3/3] GET_CLOCK: correct the sub-second evidence comment --- lib/src/control.dart | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/src/control.dart b/lib/src/control.dart index 5a45f9a..539e014 100644 --- a/lib/src/control.dart +++ b/lib/src/control.dart @@ -1016,12 +1016,12 @@ CmdResponse? parseCommandResponse(Uint8List inner, // window means we are not looking at the clock field. if (_plausibleUnix(v)) { dec['clock_epoch'] = v; - // gen4 only: the second u32 is the sub-second count in 1/32768 s, the - // same unit as the record/event sub-second fields. On a WHOOP 4.0 - // (harvard 41.17.6.0) all 13 replies we logged had it < 32768 (upper - // half 0), and only with it do the clock-offset bounds the replies give - // ([sec + subsec/32768 - t_reply, ... - t_write]) agree within a day. - // Anything >= 32768 is not that count, so it is not emitted. + // gen4 only: the second u32 is the sub-second count, read as 1/32768 s + // like the record/event sub-second fields. On a WHOOP 4.0 (harvard + // 41.17.6.0) all 17 replies had it < 32768, and the clock-offset bounds + // the replies give agree within a day with it but not always without + // it. Those bounds alone would also allow 1/65536 s. Anything >= 32768 + // is not that count, so it is not emitted. if (op == Cmd.getClock && !profile.isGen5 && payload.length >= at + 8) {