From e3577d5ebdb88a240d090daeb1825d5f5c01a51f Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sun, 29 Nov 2026 01:37:06 +0530 Subject: [PATCH 1/2] wire reads that lived in edge: alarm set epoch, scale write guard, colmi crc parts, ppogatt, r10 check --- lib/openstrap_protocol.dart | 3 ++ lib/src/colmi.dart | 14 +++++++++ lib/src/control.dart | 23 +++++++++++++++ lib/src/live.dart | 7 +++++ lib/src/miscale.dart | 6 ++++ lib/src/pebble.dart | 18 ++++++++++++ test/concern_moves_test.dart | 57 ++++++++++++++++++++++++++++++++++++ 7 files changed, 128 insertions(+) create mode 100644 test/concern_moves_test.dart diff --git a/lib/openstrap_protocol.dart b/lib/openstrap_protocol.dart index 80922ec..acecb48 100644 --- a/lib/openstrap_protocol.dart +++ b/lib/openstrap_protocol.dart @@ -85,6 +85,7 @@ export 'src/labrador.dart' show LabradorR17, LabradorFlags, LabradorUnreadableMask, LabradorR16Raw; export 'src/live.dart' show + isR10Record, DecodedSample, ImuFrame, RealtimeRrResult, @@ -189,6 +190,8 @@ export 'src/commands.dart' // Control-plane parsers (HELLO / EVENT / METADATA / COMMAND_RESPONSE / dispatch). export 'src/control.dart' show + alarmSetEventEpoch, + setAlarmReplyEchoEpoch, R10Lite, parseR10Lite, RealtimeHr, diff --git a/lib/src/colmi.dart b/lib/src/colmi.dart index 0bd937a..600e8fe 100644 --- a/lib/src/colmi.dart +++ b/lib/src/colmi.dart @@ -671,3 +671,17 @@ List colmiTemperatures(List r, } return out; } + +/// The CRC a Service B reply carries and the CRC its payload really has, for a +/// reply [colmiBigDataCrcOk] rejected. Null when the reply is too short to +/// hold its declared payload. +({int got, int want})? colmiBigDataCrcParts(List reply) { + if (reply.length < kColmiBigHeaderLength) return null; + final len = reply[2] | (reply[3] << 8); + if (reply.length < kColmiBigHeaderLength + len) return null; + return ( + got: reply[4] | (reply[5] << 8), + want: crc16Modbus( + reply.sublist(kColmiBigHeaderLength, kColmiBigHeaderLength + len)), + ); +} diff --git a/lib/src/control.dart b/lib/src/control.dart index 0a860cd..78855c4 100644 --- a/lib/src/control.dart +++ b/lib/src/control.dart @@ -1606,3 +1606,26 @@ Decoded _decodeDataRecord(Uint8List inner, } return Decoded('data_record', {'rec_type': recType}); } + +/// The wake time (unix sec, strap frame) an ALARM_SET (56) event body +/// carries: 4 bytes LE at offset 13 of the inner hex. Null when the body is +/// too short, not an event 56, or not a plausible epoch. +int? alarmSetEventEpoch(String hex) { + if (hex.length < 34 || hex.substring(4, 6) != '38') return null; + final e = int.tryParse( + hex.substring(32, 34) + + hex.substring(30, 32) + + hex.substring(28, 30) + + hex.substring(26, 28), + radix: 16); + return e != null && e >= _minPlausibleUnix ? e : null; +} + +/// The wake time (unix sec, strap frame) a WHOOP 4 SET_ALARM_TIME reply +/// echoes: `[type][seq][0x42][req seq][result][rev=1][u32 epoch LE][u16]`. +/// Null when the reply is too short, not revision 1, or not plausible. +int? setAlarmReplyEchoEpoch(List inner) { + if (inner.length < 10 || inner[5] != 1) return null; + final e = inner[6] | inner[7] << 8 | inner[8] << 16 | inner[9] << 24; + return e >= _minPlausibleUnix ? e : null; +} diff --git a/lib/src/live.dart b/lib/src/live.dart index 0c1b03f..f2b8552 100644 --- a/lib/src/live.dart +++ b/lib/src/live.dart @@ -633,3 +633,10 @@ R11Raw? decodeR11Raw(String hex) { channelB: channel(236), ); } + +/// True iff a live inner packet's record-type byte ([1], as hex) is 10 (R10), +/// the only R-R-bearing record a 0x2B envelope carries. +bool isR10Record(String hex) { + if (hex.length < 4) return false; + return int.tryParse(hex.substring(2, 4), radix: 16) == 10; +} diff --git a/lib/src/miscale.dart b/lib/src/miscale.dart index 696633d..31f8874 100644 --- a/lib/src/miscale.dart +++ b/lib/src/miscale.dart @@ -183,3 +183,9 @@ List miScaleClockValue(DateTime utc) => [ 0, 0, ]; + +/// Whether a write to [kMiScaleHistoryChar] is one the history flow needs: +/// request (01), send (02) or stop (03). Anything else is refused, because +/// the 04 acknowledgement deletes the scale's stored records. +bool miScaleHistoryWriteAllowed(List value) => + value.isNotEmpty && value[0] >= 0x01 && value[0] <= 0x03; diff --git a/lib/src/pebble.dart b/lib/src/pebble.dart index 2e9035a..53537f2 100644 --- a/lib/src/pebble.dart +++ b/lib/src/pebble.dart @@ -285,3 +285,21 @@ PebbleOverlay? parsePebbleOverlayItem(List b) { if (version != 1 && version != 3) return null; return PebbleOverlay(_u16(b, 4), _u32(b, 10), _u32(b, 14)); } + +/// The lower 3 bits of a PPoGATT header byte, the packet's command. +const int kPebblePpogattCmdData = 0; +const int kPebblePpogattCmdAck = 1; +const int kPebblePpogattCmdReset = 2; + +/// A PPoGATT header's command (low 3 bits) and serial (the rest). +({int cmd, int serial}) pebblePpogattHeader(int header) => + (cmd: header & 0x7, serial: header >> 3); + +/// The transport ACK for the data packet numbered [serial]. The watch +/// re-sends a packet that is not ACKed. +List pebblePpogattAck(int serial) => [(serial << 3) | kPebblePpogattCmdAck]; + +/// The fixed reply to a reset request: the long form when the request +/// carried a payload (a version negotiation), the short one otherwise. +List pebblePpogattResetReply({required bool negotiated}) => + negotiated ? const [0x03, 0x19, 0x19] : const [0x03]; diff --git a/test/concern_moves_test.dart b/test/concern_moves_test.dart new file mode 100644 index 0000000..15135c4 --- /dev/null +++ b/test/concern_moves_test.dart @@ -0,0 +1,57 @@ +import 'package:openstrap_protocol/openstrap_protocol.dart'; +import 'package:test/test.dart'; + +void main() { + test('mi scale history writes: request, send, stop only', () { + expect(miScaleHistoryWriteAllowed([0x01, 0, 0, 0, 0]), isTrue); + expect(miScaleHistoryWriteAllowed([0x03]), isTrue); + expect(miScaleHistoryWriteAllowed([0x04]), isFalse); + expect(miScaleHistoryWriteAllowed([0x00]), isFalse); + expect(miScaleHistoryWriteAllowed([]), isFalse); + }); + + test('colmi crc parts name both values of a bad reply', () { + final payload = [1, 2, 3]; + final want = crc16Modbus(payload); + final bad = [0xbc, 0x27, 3, 0, 0x34, 0x12, ...payload]; + expect(colmiBigDataCrcOk(bad), isFalse); + final parts = colmiBigDataCrcParts(bad)!; + expect(parts.got, 0x1234); + expect(parts.want, want); + expect(colmiBigDataCrcParts([0xbc, 0x27, 9, 0, 0, 0, 1]), isNull); + }); + + test('pebble ppogatt header, ack and reset reply', () { + final h = pebblePpogattHeader((5 << 3) | kPebblePpogattCmdData); + expect((h.cmd, h.serial), (kPebblePpogattCmdData, 5)); + expect(pebblePpogattAck(5), [(5 << 3) | 1]); + expect(pebblePpogattResetReply(negotiated: true), [0x03, 0x19, 0x19]); + expect(pebblePpogattResetReply(negotiated: false), [0x03]); + }); + + test('r10 record test reads the record-type byte', () { + expect(isR10Record('2b0a00'), isTrue); + expect(isR10Record('2b1800'), isFalse); + expect(isR10Record('2b'), isFalse); + expect(isR10Record('2bzz'), isFalse); + }); + + test('alarm set event epoch (56) and SET_ALARM_TIME reply echo', () { + // body: u32 LE at hex offset 26..34 of an event-56 inner + String hex(int e) { + final b = [e & 255, (e >> 8) & 255, (e >> 16) & 255, (e >> 24) & 255] + .map((x) => x.toRadixString(16).padLeft(2, '0')) + .join(); + return '30${'00'}38${'00' * 10}$b'; + } + const epoch = 1800000000; + expect(alarmSetEventEpoch(hex(epoch)), epoch); + expect(alarmSetEventEpoch(hex(100)), isNull); + expect(alarmSetEventEpoch('30003900'), isNull); + final inner = [0x24, 1, 0x42, 1, 0, 1, epoch & 255, (epoch >> 8) & 255, + (epoch >> 16) & 255, (epoch >> 24) & 255, 0, 0]; + expect(setAlarmReplyEchoEpoch(inner), epoch); + inner[5] = 2; + expect(setAlarmReplyEchoEpoch(inner), isNull); + }); +} From 0150530cd20d703b96d69d46e382efea1f1c8fc1 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sun, 29 Nov 2026 02:23:15 +0530 Subject: [PATCH 2/2] alarm epoch reads the parsed event, r10 check reads bytes like hexToBytes --- lib/src/control.dart | 28 +++++++++++++++----------- lib/src/live.dart | 12 ++++++++--- test/concern_moves_test.dart | 39 ++++++++++++++++++++++++++---------- 3 files changed, 53 insertions(+), 26 deletions(-) diff --git a/lib/src/control.dart b/lib/src/control.dart index 78855c4..7d68fd4 100644 --- a/lib/src/control.dart +++ b/lib/src/control.dart @@ -10,7 +10,7 @@ import 'band.dart'; import 'constants.dart'; import 'framing.dart'; import 'gen5_records.dart'; -import 'live.dart' show kR10MinLength; +import 'live.dart' show kR10MinLength, hexToBytes; import 'records.dart'; // ── little-endian helpers over a byte list ────────────────────────────────── @@ -1607,18 +1607,22 @@ Decoded _decodeDataRecord(Uint8List inner, return Decoded('data_record', {'rec_type': recType}); } -/// The wake time (unix sec, strap frame) an ALARM_SET (56) event body -/// carries: 4 bytes LE at offset 13 of the inner hex. Null when the body is -/// too short, not an event 56, or not a plausible epoch. +/// The wake time (unix sec, strap frame) an ALARM_SET (56) event carries: the +/// body is `[rev][u32 epoch LE]...`, bounded by the frame's declared body +/// length. Null when the hex is not an event 56, the declared body is too +/// short, or the epoch is not plausible. int? alarmSetEventEpoch(String hex) { - if (hex.length < 34 || hex.substring(4, 6) != '38') return null; - final e = int.tryParse( - hex.substring(32, 34) + - hex.substring(30, 32) + - hex.substring(28, 30) + - hex.substring(26, 28), - radix: 16); - return e != null && e >= _minPlausibleUnix ? e : null; + final Uint8List inner; + try { + inner = hexToBytes(hex); + } catch (_) { + return null; + } + final e = parseEvent(inner); + if (e == null || e.eventId != EventId.strapDrivenAlarmSet) return null; + if (e.body.length < 5) return null; + final epoch = u32(e.body, 1); + return epoch >= _minPlausibleUnix ? epoch : null; } /// The wake time (unix sec, strap frame) a WHOOP 4 SET_ALARM_TIME reply diff --git a/lib/src/live.dart b/lib/src/live.dart index f2b8552..9a4fadb 100644 --- a/lib/src/live.dart +++ b/lib/src/live.dart @@ -635,8 +635,14 @@ R11Raw? decodeR11Raw(String hex) { } /// True iff a live inner packet's record-type byte ([1], as hex) is 10 (R10), -/// the only R-R-bearing record a 0x2B envelope carries. +/// the only R-R-bearing record a 0x2B envelope carries. Reads the bytes the +/// way [hexToBytes] does, so whitespace and signs cannot slip through. bool isR10Record(String hex) { - if (hex.length < 4) return false; - return int.tryParse(hex.substring(2, 4), radix: 16) == 10; + final Uint8List b; + try { + b = hexToBytes(hex); + } catch (_) { + return false; + } + return b.length >= 2 && b[1] == 10; } diff --git a/test/concern_moves_test.dart b/test/concern_moves_test.dart index 15135c4..9b496db 100644 --- a/test/concern_moves_test.dart +++ b/test/concern_moves_test.dart @@ -31,23 +31,40 @@ void main() { test('r10 record test reads the record-type byte', () { expect(isR10Record('2b0a00'), isTrue); + expect(isR10Record(' 2b0a00 '), isTrue); expect(isR10Record('2b1800'), isFalse); expect(isR10Record('2b'), isFalse); expect(isR10Record('2bzz'), isFalse); + expect(isR10Record('2b+a00'), isFalse); }); - test('alarm set event epoch (56) and SET_ALARM_TIME reply echo', () { - // body: u32 LE at hex offset 26..34 of an event-56 inner - String hex(int e) { - final b = [e & 255, (e >> 8) & 255, (e >> 16) & 255, (e >> 24) & 255] - .map((x) => x.toRadixString(16).padLeft(2, '0')) - .join(); - return '30${'00'}38${'00' * 10}$b'; - } + String eventHex(int epoch, + {String head = '30003800', int bodyLen = 8, int rev = 1}) { + String le(int v, int n) => List.generate( + n, (i) => ((v >> (8 * i)) & 255).toRadixString(16).padLeft(2, '0')) + .join(); + // [type][seq][id u16][ts u32][subsec u16][body len u16][rev][epoch u32]... + return '$head${le(1800000000, 4)}${le(0, 2)}${le(bodyLen, 2)}' + '${le(rev, 1)}${le(epoch, 4)}${le(0, 3)}'; + } + + test('alarm set event epoch (56) needs the event, the id and a real body', () { + const epoch = 1800000000; + expect(alarmSetEventEpoch(eventHex(epoch)), epoch); + expect(alarmSetEventEpoch(' ${eventHex(epoch)} '), epoch); + expect(alarmSetEventEpoch(eventHex(100)), isNull, reason: 'not an epoch'); + // id 0x0138 only matches in the low byte + expect(alarmSetEventEpoch(eventHex(epoch, head: '30003801')), isNull); + // a command reply that shares the id bytes + expect(alarmSetEventEpoch(eventHex(epoch, head: '24003800')), isNull); + // a declared body too short to hold the revision and the epoch + expect(alarmSetEventEpoch(eventHex(epoch, bodyLen: 0)), isNull); + expect(alarmSetEventEpoch(eventHex(epoch, bodyLen: 4)), isNull); + expect(alarmSetEventEpoch('zz'), isNull); + }); + + test('SET_ALARM_TIME reply echo', () { const epoch = 1800000000; - expect(alarmSetEventEpoch(hex(epoch)), epoch); - expect(alarmSetEventEpoch(hex(100)), isNull); - expect(alarmSetEventEpoch('30003900'), isNull); final inner = [0x24, 1, 0x42, 1, 0, 1, epoch & 255, (epoch >> 8) & 255, (epoch >> 16) & 255, (epoch >> 24) & 255, 0, 0]; expect(setAlarmReplyEchoEpoch(inner), epoch);