diff --git a/lib/src/control.dart b/lib/src/control.dart index 0a860cd..539e014 100644 --- a/lib/src/control.dart +++ b/lib/src/control.dart @@ -1014,7 +1014,21 @@ 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, 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) { + 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..8017a6d 100644 --- a/test/control_plane_offsets_test.dart +++ b/test/control_plane_offsets_test.dart @@ -93,6 +93,38 @@ 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('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;