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/openstrap_protocol.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export 'src/labrador.dart'
show LabradorR17, LabradorFlags, LabradorUnreadableMask, LabradorR16Raw;
export 'src/live.dart'
show
isR10Record,
DecodedSample,
ImuFrame,
RealtimeRrResult,
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions lib/src/colmi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,17 @@ List<ColmiTemperature> colmiTemperatures(List<int> 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<int> 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)),
);
}
29 changes: 28 additions & 1 deletion lib/src/control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 ──────────────────────────────────
Expand Down Expand Up @@ -1606,3 +1606,30 @@ Decoded _decodeDataRecord(Uint8List inner,
}
return Decoded('data_record', {'rec_type': recType});
}

/// 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) {
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
/// 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<int> 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;
}
13 changes: 13 additions & 0 deletions lib/src/live.dart
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,16 @@ 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. Reads the bytes the
/// way [hexToBytes] does, so whitespace and signs cannot slip through.
bool isR10Record(String hex) {
final Uint8List b;
try {
b = hexToBytes(hex);
} catch (_) {
return false;
}
return b.length >= 2 && b[1] == 10;
}
6 changes: 6 additions & 0 deletions lib/src/miscale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,9 @@ List<int> 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<int> value) =>
value.isNotEmpty && value[0] >= 0x01 && value[0] <= 0x03;
18 changes: 18 additions & 0 deletions lib/src/pebble.dart
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,21 @@ PebbleOverlay? parsePebbleOverlayItem(List<int> 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<int> 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<int> pebblePpogattResetReply({required bool negotiated}) =>
negotiated ? const [0x03, 0x19, 0x19] : const [0x03];
74 changes: 74 additions & 0 deletions test/concern_moves_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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(' 2b0a00 '), isTrue);
expect(isR10Record('2b1800'), isFalse);
expect(isR10Record('2b'), isFalse);
expect(isR10Record('2bzz'), isFalse);
expect(isR10Record('2b+a00'), isFalse);
});

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;
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);
});
}
Loading