From b0920d2c3329a78e2323f43f68e10af6755b2272 Mon Sep 17 00:00:00 2001 From: magnetarQ <340235260+magnetarQ@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:21:23 +0300 Subject: [PATCH] WHOOP 4 v24: decode the record tail --- lib/openstrap_protocol.dart | 4 + lib/src/v24_status.dart | 230 ++++++++++++++++++++++++++++++++++++ test/v24_status_test.dart | 195 ++++++++++++++++++++++++++++++ 3 files changed, 429 insertions(+) create mode 100644 lib/src/v24_status.dart create mode 100644 test/v24_status_test.dart diff --git a/lib/openstrap_protocol.dart b/lib/openstrap_protocol.dart index 80922ec..9ed8866 100644 --- a/lib/openstrap_protocol.dart +++ b/lib/openstrap_protocol.dart @@ -35,6 +35,10 @@ export 'src/pebble.dart'; export 'src/garmin_fit.dart'; export 'src/garmin_transfer.dart'; +// WHOOP 4 v24 record tail: step-like counter, temperature candidate, activity +// state and the three optical control words — see v24_status.dart. +export 'src/v24_status.dart'; + // Source 1 — record decoders. export 'src/records.dart' show diff --git a/lib/src/v24_status.dart b/lib/src/v24_status.dart new file mode 100644 index 0000000..a7b745c --- /dev/null +++ b/lib/src/v24_status.dart @@ -0,0 +1,230 @@ +// v24_status.dart — the tail of a WHOOP 4 v24 historical record (inner[72:92]). +// +// PURE Dart. Separate from records.dart on purpose: R24.toMap()'s key set is +// frozen (parity oracle + shipped SQLite columns), so nothing here adds a key +// to it. This is an additional, opt-in view of bytes R24 already carries in +// rawTail. +// +// Evidence: 85,331 v24 records + simultaneous R10/R11 live captures from one +// WHOOP 4.0 (one wearer, harvard 41.17.6.0), and the v24 entries already in +// decode_parity_cases.json (recorded by the OpenStrap project: 1,887 entries, +// 629 distinct records). The structural claims that corpus can show are +// asserted in test/v24_status_test.dart. +// Offsets are INNER (packet type 0x2f at inner[0]); add 4 for frame-absolute. + +import 'dart:typed_data'; + +/// Coarse activity state, v24 inner[80] bits 4-5 (duplicated at inner[30] +/// bits 5-6). The names are the firmware's own `SLEEPFLAG` console names. +/// +/// This is the strap's coarse flag, not a sleep-stage decode: `sleep` persists +/// while the strap lies off the body. +enum V24ActivityState { wake, still, sleep, up } + +/// One of the three 2-byte optical control words at v24 inner[74:80]. +/// +/// Each word describes one slot of the realtime R11 optical packet, and is +/// enough to rebuild that slot's 25-byte descriptor: +/// +/// byte 0 bits 0-3 slot index (0, 1, 2 — constant per word) +/// byte 0 bits 4-5 lane A code → descriptor lane parameter = code × 800 +/// byte 0 bits 6-7 lane B code → descriptor lane parameter = code × 800 +/// byte 1 bits 0-3 drive code → a per-slot lookup; 12 = configured off +/// byte 1 bits 4-7 flags +/// +/// The word for slot 1 describes the R11 packet ONE RECORD LATER (22,419 / +/// 22,419 history/live pairs); same-record pairing mismatches at every +/// setting change. Physical units (LED current, offset DAC, ...) are unknown: +/// the numbers are the descriptor's, nothing more. The drive lookups were +/// built from the same captures, so they are not independent evidence. +class V24SlotControl { + /// 0, 1 or 2 — R11 slot 1, 2 or 3. + final int slotIndex; + + /// 0-3. The R11 descriptor's lane-A parameter is [laneAParam]. + final int laneACode; + + /// 0-3. The R11 descriptor's lane-B parameter is [laneBParam]. + final int laneBCode; + + /// 0-15. 12 means configured off; see [configuredOff] and [driveParam]. + final int driveCode; + + /// The word's high nibble of byte 1, unshifted (0x10, 0x20, 0x40, 0x80). + final int flags; + + const V24SlotControl({ + required this.slotIndex, + required this.laneACode, + required this.laneBCode, + required this.driveCode, + required this.flags, + }); + + /// Drive code 12: the record says this slot is off. This is configuration, + /// not a sample count: in 16 records (32 slot observations, at the end of a + /// window) history said off while R11 still carried 50 samples. For live + /// data read R11's own sample count. + bool get configuredOff => driveCode == 12; + + int get laneAParam => laneACode * 800; + int get laneBParam => laneBCode * 800; + + /// The R11 descriptor's drive parameter for this code, or null when the code + /// has never been seen next to a live descriptor. NOT interpolated: an + /// unseen code stays unknown. + /// + /// Each slot has its own table; a code seen only in another slot is unknown. + /// Slot 1 (index 0): 3→600, 5→900, 6→1150, 7→1400, 8→1750, 9→2200, + /// 10→2750, 11→3350. Slot 2 (index 1): 11→12750. Slot 3 (index 2): 6→4250, + /// 10→10300. + int? get driveParam => switch (slotIndex) { + 0 => _kSlot1Drive[driveCode], + 1 => _kSlot2Drive[driveCode], + 2 => _kSlot3Drive[driveCode], + _ => null, + }; + + /// Slot 1 only: more than 40 of the 50 lane-A samples of the SAME record + /// sit at the ADC ceiling 524287 (22 positives, 0 exceptions in 22,420 + /// pairs, both directions). It says nothing about lane B. + bool get laneAClipped => slotIndex == 0 && (flags & 0x10) != 0; + + /// Slot 1 only: as [laneAClipped], for lane B (34 positives, 0 exceptions). + /// It says nothing about lane A. + bool get laneBClipped => slotIndex == 0 && (flags & 0x40) != 0; + + static const Map _kSlot1Drive = { + 3: 600, + 5: 900, + 6: 1150, + 7: 1400, + 8: 1750, + 9: 2200, + 10: 2750, + 11: 3350, + }; + static const Map _kSlot2Drive = {11: 12750}; + static const Map _kSlot3Drive = {6: 4250, 10: 10300}; +} + +/// Decoded tail of a v24 record. See [parseV24Status]. +class V24Status { + /// u16 LE @ inner[72]. Probably a temperature in tenths of a degree Celsius: + /// on that scale it reads 34.8-35.4 in every parity-corpus record, plateaus at + /// 33.3-33.4 on our upper arm, and read 23.0 after 36 min on a table in a room + /// of roughly 21.5 (the AC setting). It is slow. + /// The whole range seen is 20.6-43.8; the highest values are from around + /// charging. There is no reference thermometer, so the scale and accuracy are + /// not proven. It is not a core body temperature. + final int temperatureCandidateRaw; + + /// The three optical control words, R11 slots 1, 2, 3 in that order. + final List slots; + + final V24ActivityState activityState; + + /// u8 @ inner[82]. Non-zero around slot 2/3 measurement windows. Values of + /// 77-100 look like a percentage (see [percentLikeValue]); the others look + /// like status codes. This is NOT shown to be SpO2: no reference oximeter + /// comparison exists. + final int measurementWindowByte; + + /// u8 @ inner[85]. Looks motion-related. It is 0 in all 237 parity-corpus + /// records whose state is `still`. Scale unknown. + final int motionByte; + + /// u8 @ inner[87]. Meaning unknown. + final int sessionClassByte; + + /// u32 LE @ inner[88]. A cumulative step-like counter. The high half was 0 + /// in every record seen (max 9,045), so a u16 fits equally; the rollover and + /// any midnight or reboot reset are unknown. + /// + /// Against hand-counted steps of one person it reads HIGH: +22 % on a long + /// walk on the wrist (252 for 207), +21 % on the upper arm (429 for 356), + /// +5 % to +41 % by gait; and 62 for 30 s of arm shaking with no steps. The + /// first increment comes 8-20 s after the start of a walk and is 11-13, so + /// one increment is not one step. Use differences between records; the + /// absolute value has no known zero. + final int stepLikeCounter; + + const V24Status({ + required this.temperatureCandidateRaw, + required this.slots, + required this.activityState, + required this.measurementWindowByte, + required this.motionByte, + required this.sessionClassByte, + required this.stepLikeCounter, + }); + + /// Slots 2 and 3 are both configured on. Configuration only: see + /// [V24SlotControl.configuredOff]. + bool get slotsConfiguredOn => + !slots[1].configuredOff && !slots[2].configuredOff; + + /// [measurementWindowByte] when it is 77-100 AND slots 2/3 carry drive codes + /// 11/6, else null. In 85,331 records a value in that range occurred only + /// with that pair (482 records); every other pair, seen or unseen, gives + /// null. + int? get percentLikeValue => + slots[1].driveCode == 11 && + slots[2].driveCode == 6 && + measurementWindowByte >= 77 && + measurementWindowByte <= 100 + ? measurementWindowByte + : null; +} + +/// Decode inner[72:92] of a WHOOP 4 **v24** historical record. +/// +/// Returns null for anything that is not a 96-byte v24 record, and for a +/// record whose three control words do not carry the slot indices 0, 1, 2 — +/// that structure held in every real record seen, so a record without it is +/// not this layout and reading its fields would be fabrication. All 85,331 of our +/// records and every corpus record are exactly 96 bytes. +V24Status? parseV24Status(Uint8List inner) { + if (inner.length != 96 || inner[0] != 0x2f || inner[1] != 24) return null; + final slots = []; + for (var i = 0; i < 3; i++) { + final b0 = inner[74 + 2 * i]; + final b1 = inner[75 + 2 * i]; + if ((b0 & 0x0f) != i) return null; + slots.add(V24SlotControl( + slotIndex: i, + laneACode: (b0 >> 4) & 3, + laneBCode: (b0 >> 6) & 3, + driveCode: b1 & 0x0f, + flags: b1 & 0xf0, + )); + } + final view = + inner.buffer.asByteData(inner.offsetInBytes, inner.lengthInBytes); + return V24Status( + temperatureCandidateRaw: view.getUint16(72, Endian.little), + slots: List.unmodifiable(slots), + activityState: V24ActivityState.values[(inner[80] >> 4) & 3], + measurementWindowByte: inner[82], + motionByte: inner[85], + sessionClassByte: inner[87], + stepLikeCounter: view.getUint32(88, Endian.little), + ); +} + +/// Rebuild the 25-byte R11 slot-1 descriptor (R11 inner[13:38]) that the +/// NEXT record's realtime packet carries, from a v24 record's first control +/// word. Null when the drive code has no known descriptor value. +/// +/// 00 32 04 05 01 04 20 00 00 00 +/// 05 20 00 00 00 (u16 little-endian) +Uint8List? v24NextSlot1Descriptor(V24SlotControl slot1) { + final d = slot1.slotIndex == 0 ? slot1.driveParam : null; + if (d == null) return null; + final a = slot1.laneAParam, b = slot1.laneBParam; + return Uint8List.fromList([ + 0x00, 0x32, 0x04, d & 0xff, d >> 8, 0x05, d & 0xff, d >> 8, 0x01, // + d & 0xff, d >> 8, 0x04, 0x20, 0x00, 0x00, 0x00, a & 0xff, a >> 8, // + 0x05, 0x20, 0x00, 0x00, 0x00, b & 0xff, b >> 8, + ]); +} diff --git a/test/v24_status_test.dart b/test/v24_status_test.dart new file mode 100644 index 0000000..0a51c16 --- /dev/null +++ b/test/v24_status_test.dart @@ -0,0 +1,195 @@ +// The v24 record tail (inner[72:92]) — every structural claim in +// v24_status.dart, asserted against the real v24 records already in +// decode_parity_cases.json, plus hand-built records for the states that +// corpus does not contain (a measurement window, a clipped lane). + +import 'dart:convert'; +import 'dart:io'; +import 'dart:typed_data'; + +import 'package:openstrap_protocol/openstrap_protocol.dart'; +import 'package:test/test.dart'; + +List _realV24Records() { + final cases = + json.decode(File('decode_parity_cases.json').readAsStringSync()) as List; + final out = []; + for (final c in cases) { + final hex = (c as Map)['hex'] as String; + if (hex.length != 192) continue; + final b = hexToBytes(hex); + if (b[1] == 24) out.add(b); + } + return out; +} + +int _counter(Uint8List b) => b.buffer + .asByteData(b.offsetInBytes, b.lengthInBytes) + .getUint32(3, Endian.little); + +Uint8List _with(Uint8List base, Map bytes) { + final b = Uint8List.fromList(base); + bytes.forEach((k, v) => b[k] = v); + return b; +} + +void main() { + late final List records; + setUpAll(() { + records = _realV24Records()..sort((a, b) => _counter(a) - _counter(b)); + // 1,887 entries; several decoder kinds reuse one frame, so fewer distinct. + expect(records.length, 1887, reason: 'v24 entries in the corpus'); + expect({for (final b in records) b.join(',')}.length, 629); + }); + + group('v24 tail on the real corpus', () { + test('every record decodes; the control words carry slot index 0/1/2', () { + for (final b in records) { + final s = parseV24Status(b); + expect(s, isNotNull); + expect([for (final w in s!.slots) w.slotIndex], [0, 1, 2]); + } + }); + + test('slots 2 and 3 are off, slot 1 has a known drive value', () { + for (final b in records) { + final s = parseV24Status(b)!; + expect(s.slots[1].configuredOff, isTrue); + expect(s.slots[2].configuredOff, isTrue); + expect(s.slotsConfiguredOn, isFalse); + expect(s.measurementWindowByte, 0); + expect(s.percentLikeValue, isNull); + expect(s.slots[0].configuredOff, isFalse); + expect(s.slots[0].driveParam, isIn([2200, 3350])); + expect(v24NextSlot1Descriptor(s.slots[0])!.length, 25); + } + }); + + test('the step-like counter never decreases across 8 h of records', () { + var prev = -1; + for (final b in records) { + final v = parseV24Status(b)!.stepLikeCounter; + expect(v, greaterThanOrEqualTo(prev)); + prev = v; + } + expect(parseV24Status(records.first)!.stepLikeCounter, 3153); + expect(parseV24Status(records.last)!.stepLikeCounter, 6248); + }); + + test('the temperature candidate stays in 348-354', () { + for (final b in records) { + final t = parseV24Status(b)!.temperatureCandidateRaw; + expect(t, inInclusiveRange(348, 354)); + } + }); + + test('the activity state is the same two bits as inner[30] bits 5-6', () { + for (final b in records) { + final s = parseV24Status(b)!; + expect(s.activityState.index, (b[30] >> 5) & 3); + } + }); + + test('the motion byte is zero in every STILL record', () { + var still = 0; + for (final b in records) { + final s = parseV24Status(b)!; + if (s.activityState == V24ActivityState.still) { + still++; + expect(s.motionByte, 0); + } + } + expect(still, 237); + }); + }); + + group('v24 tail — states the corpus does not contain', () { + test('slots 2 and 3 configured on', () { + // Drive codes 11/6: live R11 showed slot 2 drive 12750 and slot 3 drive + // 4250 for them. The lane codes here are edited in. + final b = _with(records.first, {76: 0x01, 77: 0x0b, 78: 0x92, 79: 0x06}); + final s = parseV24Status(_with(b, {82: 97}))!; + expect(s.slotsConfiguredOn, isTrue); + expect(s.slots[1].driveParam, 12750); + expect(s.slots[2].driveParam, 4250); + expect(s.slots[2].laneAParam, 800); + expect(s.slots[2].laneBParam, 1600); + expect(s.percentLikeValue, 97); + // Only 77-100 counts; the raw byte is always kept. + for (final v in [0, 76, 101, 160]) { + final o = parseV24Status(_with(b, {82: v}))!; + expect(o.percentLikeValue, isNull); + expect(o.measurementWindowByte, v); + } + for (final v in [77, 100]) { + expect(parseV24Status(_with(b, {82: v}))!.percentLikeValue, v); + } + }); + + test('percentLikeValue fails closed for every drive pair but 11/6', () { + for (var c2 = 0; c2 < 16; c2++) { + for (var c3 = 0; c3 < 16; c3++) { + final s = parseV24Status( + _with(records.first, {77: c2, 79: c3, 82: 97}))!; + expect(s.percentLikeValue, c2 == 11 && c3 == 6 ? 97 : isNull, + reason: '$c2/$c3'); + expect(s.measurementWindowByte, 97); + expect(s.slotsConfiguredOn, c2 != 12 && c3 != 12); + } + } + }); + + test('single-byte fields come from their own offsets', () { + final s = parseV24Status( + _with(records.first, {82: 11, 84: 22, 85: 33, 86: 44, 87: 55}))!; + expect( + [s.measurementWindowByte, s.motionByte, s.sessionClassByte], + [11, 33, 55]); + }); + + test('drive tables are per slot', () { + for (var c = 0; c < 16; c++) { + final s = parseV24Status(_with(records.first, {77: c, 79: c}))!; + expect(s.slots[1].driveParam, const {11: 12750}[c], reason: 'slot 2'); + expect(s.slots[2].driveParam, const {6: 4250, 10: 10300}[c], + reason: 'slot 3'); + } + }); + + test('clip flags are per lane', () { + final a = parseV24Status(_with(records.first, {75: 0x16}))!.slots[0]; + expect([a.laneAClipped, a.laneBClipped], [true, false]); + final b = parseV24Status(_with(records.first, {75: 0x46}))!.slots[0]; + expect([b.laneAClipped, b.laneBClipped], [false, true]); + }); + + test('slot-1 descriptor and clip flags', () { + // 0x90 = lane A code 1, lane B code 2; 0x56 = drive code 6 + flags + // 0x10 (lane A at the ADC ceiling) and 0x40 (lane B). + final s = parseV24Status(_with(records.first, {74: 0x90, 75: 0x56}))!; + expect(s.slots[0].laneAClipped, isTrue); + expect(s.slots[0].laneBClipped, isTrue); + expect( + v24NextSlot1Descriptor(s.slots[0]), + hexToBytes('0032047e04057e04017e04042000000020030520000000' + '4006'), + ); + }); + + test('an unseen drive code is unknown, not interpolated', () { + final s = parseV24Status(_with(records.first, {75: 0x04}))!; + expect(s.slots[0].driveParam, isNull); + expect(v24NextSlot1Descriptor(s.slots[0]), isNull); + }); + + test('not a v24 tail: wrong version, short, or broken slot indices', () { + expect(parseV24Status(_with(records.first, {1: 25})), isNull); + expect( + parseV24Status(Uint8List.sublistView(records.first, 0, 95)), isNull); + expect(parseV24Status(records.first), isNotNull); + expect( + parseV24Status(Uint8List.fromList([...records.first, 0])), isNull); + expect(parseV24Status(_with(records.first, {76: 0x00})), isNull); + }); + }); +}