Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,9 @@ class HapticsTermination {
/// must check both. A response can carry SUCCESS as its outer result and still
/// report here that the alarm was refused, which is the whole reason the byte
/// exists: it is the only place the strap says *why*.
///
/// Exception: a gen4 SET reply that echoes the stored epoch (rev 1, plausible
/// u32 at payload[3]) has no status byte; `alarm_status` is then absent.
class AlarmStatus {
static const int unknown = 0;
static const int validInputPattern = 1;
Expand Down
11 changes: 10 additions & 1 deletion lib/src/control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,16 @@ CmdResponse? parseCommandResponse(Uint8List inner,
// The revision byte at body 0 is read but NOT gated on: it reads 3 for
// SET and 2 for RUN, and a strap answering with a revision we do not
// recognise still put a status byte where the status byte goes.
if (payload.length >= 4) {
//
// gen4's real SET reply is `[rev=1][u32 epoch LE][u16]` from body 0: the
// byte at payload[3] is the epoch's low byte, not a status, so no status
// is emitted for that layout.
final echoLayout = op == Cmd.setAlarmTime &&
!profile.isGen5 &&
payload.length >= 7 &&
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
payload[2] == 1 &&
u32(payload, 3) >= 1500000000; // plausible unix time (2017+)
if (payload.length >= 4 && !echoLayout) {
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
final code = payload[3];
dec['alarm_status'] = code;
dec['alarm_status_name'] = AlarmStatus.name(code);
Expand Down
64 changes: 46 additions & 18 deletions test/control_plane_offsets_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ void main() {
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]))!;
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(
Expand All @@ -119,8 +119,8 @@ void main() {
() {
// [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]))!;
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);
});
Expand Down Expand Up @@ -295,6 +295,33 @@ void main() {
expect(r['alarm_status_name'], 'invalid_alarm_id');
});

test('gen4 SET reply echoing the epoch has no status byte', () {
// epoch low byte 0x0a would otherwise read as invalid_alarm_time
final r = parseCommandResponse(cmdResponse(
Cmd.setAlarmTime, [1, 0x0a, 0x00, 0x00, 0x66, 0x00, 0x00]))!;
expect(r.decoded.containsKey('alarm_status'), isFalse);
// status-shaped body with trailing bytes but no epoch keeps its status
final s = parseCommandResponse(
cmdResponse(Cmd.setAlarmTime, [1, 10, 0, 0, 0, 0, 0]))!;
expect(s.decoded['alarm_status'], 10);
});

test('echo guard: gen5 keeps status, threshold boundary is inclusive', () {
// same echo-shaped bytes on gen5 are a real status layout
final g5 = parseCommandResponse(
cmdResponse(
Cmd.setAlarmTime, [1, 0x0a, 0x00, 0x00, 0x66, 0x00, 0x00]),
profile: BandProfile.gen5)!;
expect(g5.decoded['alarm_status'], 10);
// 1500000000 = 0x59682F00 -> echo; one below -> status kept
final at = parseCommandResponse(
cmdResponse(Cmd.setAlarmTime, [1, 0x00, 0x2f, 0x68, 0x59, 0, 0]))!;
expect(at.decoded.containsKey('alarm_status'), isFalse);
final below = parseCommandResponse(
cmdResponse(Cmd.setAlarmTime, [1, 0xff, 0x2e, 0x68, 0x59, 0, 0]))!;
expect(below.decoded['alarm_status'], 0xff);
});

test('an unrecognised response revision does not suppress the status', () {
// The SET reply's revision byte reads 3, but it is not a gate: a strap
// answering with another revision still put the status where it goes.
Expand Down Expand Up @@ -334,7 +361,8 @@ void main() {
Map<String, dynamic> get4(int activeFlag) => parseCommandResponse(
cmdResponse(Cmd.getAlarmTime,
[0x04, activeFlag, ...le32(1786000000), ...le16(0)]),
)!.decoded;
)!
.decoded;
expect(get4(1)['alarm_active'], isTrue);
expect(get4(1)['alarm_epoch'], 1786000000);
expect(get4(0)['alarm_active'], isFalse);
Expand Down Expand Up @@ -373,8 +401,8 @@ void main() {
// semantics are unconfirmed on gen4, so gating it would silently drop
// valid alarm reads rather than risk a stale one.
final body = [0x04, 1, ...le32(1786000000), ...le16(0)];
final r = parseCommandResponse(
cmdResponse(Cmd.getAlarmTime, body, status: 0))!;
final r =
parseCommandResponse(cmdResponse(Cmd.getAlarmTime, body, status: 0))!;
expect(r.decoded['alarm_epoch'], 1786000000);
expect(r.decoded['alarm_active'], isTrue);
});
Expand All @@ -394,7 +422,8 @@ void main() {
}
});

test('GET_BODY_LOCATION_AND_STATUS: gen5 a success outer status still '
test(
'GET_BODY_LOCATION_AND_STATUS: gen5 a success outer status still '
'decodes normally', () {
final body = [0x01, 0x00, 0xFF, 0x07];
final r = parseCommandResponse(
Expand Down Expand Up @@ -461,8 +490,8 @@ void main() {
});

test('a declared length longer than the frame is clamped, not thrown', () {
final e = parseEvent(envelopePacket(0x30, EventId.setRtc, [0xAA],
declaredLen: 64))!;
final e = parseEvent(
envelopePacket(0x30, EventId.setRtc, [0xAA], declaredLen: 64))!;
expect(e.body, [0xAA]);
});

Expand Down Expand Up @@ -583,10 +612,10 @@ void main() {
});

test('HAPTICS_TERMINATED(100) separates expiry, error and dismissal', () {
Map<String, dynamic> terminated(int code) => parseEvent(
envelopePacket(0x30, EventId.hapticsTerminated, [1, code]),
profile: BandProfile.gen5)!
.decoded;
Map<String, dynamic> terminated(int code) =>
parseEvent(envelopePacket(0x30, EventId.hapticsTerminated, [1, code]),
profile: BandProfile.gen5)!
.decoded;

expect(terminated(0)['haptics_termination'], 'expired');
expect(terminated(1)['haptics_termination'], 'error');
Expand Down Expand Up @@ -664,8 +693,8 @@ void main() {
});

test('the other gen5-scoped ids stay numeric and un-decoded on gen4', () {
final h = parseEvent(
envelopePacket(0x30, EventId.hapticsTerminated, [1, 2]))!;
final h =
parseEvent(envelopePacket(0x30, EventId.hapticsTerminated, [1, 2]))!;
expect(h.name, 'EVENT_${EventId.hapticsTerminated}');
expect(h.decoded, isEmpty);
});
Expand Down Expand Up @@ -739,8 +768,7 @@ void main() {

test(
'a caller that only flushes on hasCompletedRun keeps a 4-chunk line '
'whole, even though every add() returns false on a fresh instance',
() {
'whole, even though every add() returns false on a fresh instance', () {
// add() returning false on the very first chunk into a fresh instance
// used to be indistinguishable from a real gap. A caller that flushed
// on every `false` (the old documented contract) reset _lastIndex each
Expand Down
Loading