diff --git a/lib/src/oura.dart b/lib/src/oura.dart index 0b7a7a8..249dd14 100644 --- a/lib/src/oura.dart +++ b/lib/src/oura.dart @@ -99,6 +99,10 @@ const int kOuraEvtTemp = 0x46; /// A single skin-temperature reading. const int kOuraEvtTempPeriod = 0x69; +/// The RTC beacon, a wall-clock anchor the ring emits on its own. See +/// [decodeRtcBeacon]. +const int kOuraEvtRtcBeacon = 0x85; + /// Firmware diagnostics. Subtype-multiplexed; see [decodeDebugData]. const int kOuraEvtDebugData = 0x61; @@ -127,6 +131,21 @@ int? decodeTimeSync(OuraEvent e) { return (v >= 1700000000 && v <= 4100000000) ? v : null; } +/// Unix seconds from an `rtc_beacon` (`0x85`) body, or null when [e] is not +/// one or the value is not a date. +/// +/// Layout: `u32` LE Unix seconds at offset 0, then reserved bytes and a `u16` +/// at offset 8 whose meaning is unknown. Bodies under 10 bytes are refused. +/// Same date window as [decodeTimeSync], so an unset RTC never becomes an +/// anchor. +int? decodeRtcBeacon(OuraEvent e) { + if (e.tag != kOuraEvtRtcBeacon || e.body.length < 10) return null; + final v = e.body.buffer + .asByteData(e.body.offsetInBytes) + .getUint32(0, Endian.little); + return (v >= 1700000000 && v <= 4100000000) ? v : null; +} + /// Skin temperature in degrees Celsius, one entry per probe. /// /// The wire carries signed 16-bit little-endian CENTI-degrees. Anything outside diff --git a/test/oura_test.dart b/test/oura_test.dart index 010b6cb..aee42b0 100644 --- a/test/oura_test.dart +++ b/test/oura_test.dart @@ -243,6 +243,23 @@ void main() { parseOuraFrame(_hex('4208') + _hex('01000000') + _hex('00000000'))!)!; expect(decodeTimeSync(unset), isNull); }); + test('an rtc beacon is a little-endian unix second', () { + final ok = parseOuraEvent(parseOuraFrame( + _hex('850e') + _hex('01000000') + _hex('4fd2376a00000000e803'))!)!; + expect(decodeRtcBeacon(ok), 1782043215); + // A body too short for the full layout is not a beacon. + final short = parseOuraEvent(parseOuraFrame( + _hex('8509') + _hex('01000000') + _hex('4fd2376a00000000'))!)!; + expect(decodeRtcBeacon(short), isNull); + // A different tag is not a beacon either. + final other = parseOuraEvent(parseOuraFrame( + _hex('420e') + _hex('01000000') + _hex('4fd2376a00000000e803'))!)!; + expect(decodeRtcBeacon(other), isNull); + // An unset RTC is not an anchor. + final unset = parseOuraEvent(parseOuraFrame( + _hex('850e') + _hex('01000000') + _hex('0100000000000000e803'))!)!; + expect(decodeRtcBeacon(unset), isNull); + }); }); group('outbound frames', () {