diff --git a/lib/openstrap_protocol.dart b/lib/openstrap_protocol.dart index 226428c..964a56b 100644 --- a/lib/openstrap_protocol.dart +++ b/lib/openstrap_protocol.dart @@ -29,6 +29,9 @@ export 'src/pebble.dart'; export 'src/garmin_fit.dart'; export 'src/garmin_transfer.dart'; +// WHOOP 4 v25 optical samples. +export 'src/v25_samples.dart'; + // Source 1 — record decoders. export 'src/records.dart' show diff --git a/lib/src/records.dart b/lib/src/records.dart index e92342b..0a5bc0a 100644 --- a/lib/src/records.dart +++ b/lib/src/records.dart @@ -12,7 +12,7 @@ import 'constants.dart'; class R24 { final int histVersion; // historical layout version byte @ inner[1] (frame[5]) final int tsEpoch; // unix seconds @ inner[7:11] (frame[11:15]) - final int tsSubsec; // sub-seconds @ inner[11:13] (frame[15:17], v24/v12) + final int tsSubsec; // sub-seconds @ inner[11:13] (frame[15:17], v24/v12/v25) final int counter; // record counter @ inner[3:7] (frame[7:11]) final int hr; // heart rate bpm @ inner[17] (frame[21], v24/v12) @@ -251,9 +251,13 @@ ByteData _view(Uint8List b) => b.buffer.asByteData(b.offsetInBytes, b.lengthInBytes); /// Decode a v25 historical record. v25 is NOT a 1 Hz biometric record: it is a -/// raw ~24 Hz PPG waveform, sent in 13–27 s bursts roughly every 20 minutes. -/// The only field solved in it is the unix second at inner[7:11]; the waveform -/// itself is left whole in [R24.rawTail]. +/// compressed optical burst, sent in bursts (up to ~40 records) roughly every 20 +/// minutes. +/// Header: counter @ inner[3:7], unix second @ [7:11], sub-seconds (1/32768 s) +/// @ [11:13] — the same header as v24 — so the timestamp is full resolution. +/// The waveform is left whole in [R24.rawTail]; `reconstructV25Samples` decodes it: +/// 25 samples per record, an i32 first sample @ [15:19] followed by 24 i16 +/// saturating deltas @ [19:67] (see v25_samples.dart). /// /// So everything else comes back absent, and that is deliberate: /// @@ -282,7 +286,7 @@ R24? _parseV25(Uint8List inner) { return R24( histVersion: 25, tsEpoch: view.getUint32(7, Endian.little), - tsSubsec: 0, + tsSubsec: view.getUint16(11, Endian.little), counter: view.getUint32(3, Endian.little), hr: 0, rrCount: 0, @@ -333,8 +337,9 @@ bool _physiologicallyPlausible(List accelG, int hr) { /// packet-type byte. /// /// Routing: -/// - v25 → the 24 Hz PPG-waveform layout ([_parseV25]): timestamp only, with -/// HR 0 and accelG empty. +/// - v25 → the optical-burst layout ([_parseV25]): timestamp only, with +/// HR 0 and accelG empty; the samples come from +/// `reconstructV25Samples` (v25_samples.dart). /// - v24 / v12 → our hardware-validated field map, returned as-is. /// - v18 / v9 / v7 → the SAME field map but with the HR byte read at that /// version's offset (only the HR offset is independently confirmed for diff --git a/lib/src/v25_samples.dart b/lib/src/v25_samples.dart new file mode 100644 index 0000000..bd8611f --- /dev/null +++ b/lib/src/v25_samples.dart @@ -0,0 +1,38 @@ +// v25_samples.dart — the optical samples of a WHOOP 4 v25 historical record. +// +// Same encoding as gen5's v26 window: an i32 first sample @ inner[15:19], then +// 24 saturating i16 deltas @ inner[19:67], 25 samples in all. So this reuses +// [reconstructSaturatedDeltaWindow] and its evidence about the tail. +// +// How it was checked (one WHOOP 4.0, firmware harvard 41.17.6.0, realtime raw +// stream on): 739 v25 records have an R11 frame with the same timestamp. In +// 734 the 25 samples equal R11 slot 1 lane A samples 0, 2, ..., 48 exactly. The +// other five hold 24 differing deltas, every one the live difference clamped +// to the i16 range, and every one a rail value. So all records without a rail +// delta matched, and all five records with one did not. A rail delta is +// ambiguous (the true difference may be larger), so the sample it reaches and +// every later absolute value in that record are not trustworthy. The next +// record starts from its own absolute value. +// Which LED/photodiode slot 1 lane A is, is not established. + +import 'dart:typed_data'; + +import 'constants.dart'; +import 'gen5_records.dart' + show Gen5PpgReconstruction, reconstructSaturatedDeltaWindow; + +/// Samples of one v25 record (~26 Hz), or null when [inner] is not a v25 +/// historical record of at least 67 bytes. Decode each record on its own: the +/// first sample is absolute, never carry a sum across records. Use +/// [Gen5PpgReconstruction.trustedSamples]; do not trust the tail after a rail delta. +Gen5PpgReconstruction? reconstructV25Samples(Uint8List inner) { + if (inner.length < 67 || + inner[0] != PacketType.historicalData || + inner[1] != 25) { + return null; + } + final view = ByteData.sublistView(inner); + return reconstructSaturatedDeltaWindow(view.getInt32(15, Endian.little), [ + for (var k = 0; k < 24; k++) view.getInt16(19 + 2 * k, Endian.little), + ]); +} diff --git a/test/v25_test.dart b/test/v25_test.dart index 1bfe588..c6e34f2 100644 --- a/test/v25_test.dart +++ b/test/v25_test.dart @@ -1,3 +1,5 @@ +import 'dart:typed_data'; + import 'package:openstrap_protocol/openstrap_protocol.dart'; import 'package:test/test.dart'; @@ -45,6 +47,62 @@ void main() { expect(r.rawTail, _innerHex(records[0]).substring(13 * 2)); }); + test('v25 carries the v24 sub-second field at inner[11:13]', () { + final rows = [ + for (final hex in records) parseR24(hexToBytes(_innerHex(hex)))! + ]; + expect(rows.map((r) => r.tsSubsec).toList(), [17184, 15912, 14648]); + // One record every ~0.961 s: each +1 s step of the unix second comes + // with the sub-second count falling by ~1270/32768 s. + double t(R24 r) => r.tsEpoch + r.tsSubsec / 32768; + expect(t(rows[1]) - t(rows[0]), closeTo(0.9612, 0.001)); + expect(t(rows[2]) - t(rows[1]), closeTo(0.9614, 0.001)); + }); + + test('reconstructV25Samples: i32 first sample + 24 i16 deltas -> 25 samples', () { + // Expected values computed independently (Python, struct.unpack) from + // the same hex. + final s0 = reconstructV25Samples(hexToBytes(_innerHex(records[0])))!.samples; + expect(s0.length, 25); + expect(s0.take(4).toList(), [209219, 209857, 210470, 211168]); + expect(s0.last, 235637); + final s1 = reconstructV25Samples(hexToBytes(_innerHex(records[1])))!.samples; + expect([s1.first, s1[1], s1[2], s1[3], s1.last], + [241056, 244762, 246434, 246842, 231647]); + final s2 = reconstructV25Samples(hexToBytes(_innerHex(records[2])))!.samples; + expect([s2.first, s2.last], [233842, 285571]); + }); + + test('no rail delta: re-encoding gives the original bytes', () { + for (final hex in records) { + final inner = hexToBytes(_innerHex(hex)); + final r = reconstructV25Samples(inner)!; + expect(r.hasSaturatedDelta, isFalse); + final s = r.samples; + final out = ByteData(48 + 4); + out.setInt32(0, s[0], Endian.little); + for (var k = 0; k < 24; k++) { + out.setInt16(4 + 2 * k, s[k + 1] - s[k], Endian.little); + } + expect(out.buffer.asUint8List(), inner.sublist(15, 67)); + } + }); + + test('reconstructV25Samples rejects non-v25 and truncated records', () { + final inner = hexToBytes(_innerHex(records[0])); + expect(reconstructV25Samples(inner.sublist(0, 66)), isNull); + final v24 = Uint8List.fromList(inner)..[1] = 24; + expect(reconstructV25Samples(v24), isNull); + }); + + test('a rail delta marks that sample and the rest as ambiguous', () { + final inner = hexToBytes(_innerHex(records[0])); + ByteData.sublistView(inner).setInt16(19 + 2 * 5, 32767, Endian.little); + final r = reconstructV25Samples(inner)!; + expect(r.firstAmbiguousSampleIndex, 6); + expect(r.trustedSamples.length, 6); + }); + test('decodeRecord surfaces v25 timestamps', () { final decoded = [for (final hex in records) decodeRecord(_innerHex(hex))]; expect(decoded.every((r) => r != null), isTrue);