diff --git a/app/src/main/java/com/pulseloop/health/HealthConnectExporter.kt b/app/src/main/java/com/pulseloop/health/HealthConnectExporter.kt index fbe57bcd..062cf887 100644 --- a/app/src/main/java/com/pulseloop/health/HealthConnectExporter.kt +++ b/app/src/main/java/com/pulseloop/health/HealthConnectExporter.kt @@ -8,7 +8,9 @@ import androidx.health.connect.client.records.ExerciseRoute import androidx.health.connect.client.records.ExerciseRouteResult import androidx.health.connect.client.records.ExerciseSessionRecord import androidx.health.connect.client.records.Record +import androidx.health.connect.client.records.SleepSessionRecord import androidx.health.connect.client.records.metadata.Device +import androidx.health.connect.client.time.TimeRangeFilter import com.pulseloop.data.PulseLoopDatabase import com.pulseloop.health.exporters.ActivityExporter import com.pulseloop.health.exporters.NutritionExporter @@ -16,7 +18,9 @@ import com.pulseloop.health.exporters.RestingHeartRateExporter import com.pulseloop.health.exporters.SleepExporter import com.pulseloop.health.exporters.VitalsExporter import com.pulseloop.health.exporters.WorkoutExporter +import kotlinx.coroutines.CancellationException import kotlinx.coroutines.delay +import java.time.Instant /** * Chunk + retry progress for one kind's insert pass (see [healthConnectInsertChunked]). @@ -167,6 +171,38 @@ internal fun watermarkAdvance( internal fun effectiveWatermark(storedWatermark: Long?, newOnlyConsentAt: Long?): Long = maxOf(storedWatermark ?: 0L, newOnlyConsentAt ?: 0L) +internal fun sleepIdentityV2MigrationRequired( + prefs: HealthConnectPrefs, + granted: Set, +): Boolean = prefs.enabled && + prefs.backfillChoice != HealthConnectPrefs.BackfillChoice.NOT_ASKED && + prefs.sleep && + HealthConnectPermissions.sleep.first() in granted && + !prefs.sleepIdentityV2Done + +/** + * Functional seam for the ordered one-time sleep identity migration. A false gate is a successful + * no-op; a failed delete returns false without resetting the watermark or setting the marker. + */ +internal suspend fun migrateSleepIdentityV2( + required: Boolean, + deleteLegacyRecords: suspend () -> Unit, + resetSleepWatermark: () -> Unit, + markDone: () -> Unit, +): Boolean { + if (!required) return true + return try { + deleteLegacyRecords() + resetSleepWatermark() + markDone() + true + } catch (e: CancellationException) { + throw e + } catch (_: Exception) { + false + } +} + /** * If [e] carries the platform's single-record size-limit message and [records] contains an * [ExerciseSessionRecord] whose route is long enough to decimate, returns a copy of the list @@ -371,14 +407,43 @@ class HealthConnectExporter( if (groupHigh > (vitalsWm ?: 0L)) store.setWatermark(HealthConnectWatermarks.Key.VITALS, groupHigh) } + val sleepMigrationRequired = sleepIdentityV2MigrationRequired(prefs, granted) + val sleepIdentityReady = migrateSleepIdentityV2( + required = sleepMigrationRequired, + deleteLegacyRecords = { + // Health Connect scopes this range deletion to records written by the calling app; + // records from other apps and PulseLoop's Room data are not touched. + client.deleteRecords( + SleepSessionRecord::class, + TimeRangeFilter.after(Instant.EPOCH), + ) + }, + resetSleepWatermark = { + store.resetWatermarks(setOf(HealthConnectWatermarks.Key.SLEEP)) + }, + markDone = { + store.update { it.copy(sleepIdentityV2Done = true) } + }, + ) + if (sleepMigrationRequired && sleepIdentityReady) { + // The reset invalidated the snapshot; the normal sleep exporter rebuilds v2 records + // from the allowed Room window in this same pass. + wm0 = store.currentWatermarks + } else if (!sleepIdentityReady) { + errors += "sleep migration: could not replace legacy Health Connect sleep records; " + + "sleep export was skipped and will retry on the next pass" + } + // ── Sleep group (Phase 2; watermarked on SleepSessionEntity.updatedAt — a re-synced - // night re-upserts the same pl-sleep- record in place) ── + // session re-upserts the same stable v2 record in place) ── if (!prefs.sleep) { skipped += "sleep (toggle off)" } else { val permission = HealthConnectPermissions.sleep.first() if (permission !in granted) { skipped += "sleep (permission not granted)" + } else if (!sleepIdentityReady) { + skipped += "sleep (stable identity migration pending)" } else { val sleepPending = SleepExporter(db).build(effectiveWatermark(wm0.sleep, prefs.newOnlyConsentAt), device) val sleepProgress = insertChunked(sleepPending.records, sleepPending.highWaters) { chunk -> diff --git a/app/src/main/java/com/pulseloop/health/HealthConnectPrefsStore.kt b/app/src/main/java/com/pulseloop/health/HealthConnectPrefsStore.kt index 3473d15f..9013c224 100644 --- a/app/src/main/java/com/pulseloop/health/HealthConnectPrefsStore.kt +++ b/app/src/main/java/com/pulseloop/health/HealthConnectPrefsStore.kt @@ -54,6 +54,12 @@ data class HealthConnectPrefs( * decode), which is exactly the "still needs the flip" state for upgrading users. */ val nettingFlipDone: Boolean = false, + /** + * One-time marker for replacing role-based sleep record identities with stable v2 identities. + * Old blobs decode to false; removal deliberately leaves this true because it deletes the + * calling app's Health Connect records rather than restoring legacy identities. + */ + val sleepIdentityV2Done: Boolean = false, /** * Phase 6: one-shot flag for the full-revocation reset offer (Gadgetbridge pattern). Set when * the user declines ("Not now") or confirms the reset; cleared on a later re-grant (a grow) so diff --git a/app/src/main/java/com/pulseloop/health/HealthConnectTypeMappings.kt b/app/src/main/java/com/pulseloop/health/HealthConnectTypeMappings.kt index eb034129..a1932d63 100644 --- a/app/src/main/java/com/pulseloop/health/HealthConnectTypeMappings.kt +++ b/app/src/main/java/com/pulseloop/health/HealthConnectTypeMappings.kt @@ -4,9 +4,11 @@ import androidx.health.connect.client.records.ExerciseSessionRecord import androidx.health.connect.client.records.MealType import androidx.health.connect.client.records.SleepSessionRecord import com.pulseloop.ring.SleepStage +import java.nio.charset.StandardCharsets import java.time.Instant import java.time.ZoneId import java.time.ZoneOffset +import java.util.UUID /** * Pure identity + mapping helpers for the Health Connect export — the Android port of the iOS @@ -76,6 +78,17 @@ object HealthConnectTypeMappings { fun hrRecordId(hourStartEpochMs: Long, segmentIndex: Int? = null): String = if (segmentIndex == null) "pl-hr-$hourStartEpochMs" else "pl-hr-$hourStartEpochMs-$segmentIndex" + /** + * Bounded v2 sleep identity derived only from the stable Room session id. Corrections can + * change a session's duration, bounds, waking day, or main/nap role without changing this id. + */ + fun sleepSessionRecordIdV2(sessionId: String): String { + val uuid = UUID.nameUUIDFromBytes( + "pulseloop:sleep:v2:$sessionId".toByteArray(StandardCharsets.UTF_8), + ) + return "pl-sleep-v2-$uuid" + } + // ── time helpers ── /** Local-midnight start of the hour containing [epochMs] (epoch millis). */ diff --git a/app/src/main/java/com/pulseloop/health/exporters/SleepExporter.kt b/app/src/main/java/com/pulseloop/health/exporters/SleepExporter.kt index 277345f5..9e250931 100644 --- a/app/src/main/java/com/pulseloop/health/exporters/SleepExporter.kt +++ b/app/src/main/java/com/pulseloop/health/exporters/SleepExporter.kt @@ -6,9 +6,9 @@ import androidx.health.connect.client.records.metadata.Device import androidx.health.connect.client.records.metadata.Metadata import com.pulseloop.data.PulseLoopDatabase import com.pulseloop.data.entity.SleepSessionEntity +import com.pulseloop.data.entity.SleepStageBlockEntity import com.pulseloop.health.HealthConnectTypeMappings import com.pulseloop.health.HealthConnectTypeMappings.EXCLUDED_SOURCES -import com.pulseloop.health.HealthConnectTypeMappings.SleepDaySession import com.pulseloop.health.HealthConnectTypeMappings.SleepStageSpan import java.time.Instant import java.time.ZoneId @@ -21,9 +21,8 @@ import java.time.ZoneId * ([HealthConnectTypeMappings.normalizeSleepStages]) — with the client's stage-type constants * (never raw ints). * - * Identity (plan §3, identity trap #1): `clientRecordId = pl-sleep-` from the - * session's `date` — NEVER the block id (a fresh random UUID on every re-sync) nor the session - * UUID — so a re-synced night upserts the SAME record in place. `clientRecordVersion` = the + * Identity: the bounded v2 `clientRecordId` is derived only from the stable + * [SleepSessionEntity.id], never from waking-day main/nap rank. `clientRecordVersion` = the * session's `updatedAt`, so the later, fuller re-sync always wins the upsert. * * The selection is watermark-driven on `updatedAt` (sleep is a mutable group: re-synced nights @@ -76,11 +75,6 @@ class SleepExporter(private val db: PulseLoopDatabase) { .forSessions(sessions.map { it.id }) .groupBy { it.sessionId } - // Main-session selection needs each day's FULL non-demo session set — the pending list - // alone cannot tell a night from the nap that shares its waking day (plan §3: the plain - // id belongs to the day's main sleep). - val dayCache = HashMap>() - val records = mutableListOf() val highWaters = mutableListOf() var skipped = 0 @@ -91,56 +85,63 @@ class SleepExporter(private val db: PulseLoopDatabase) { drop(session.updatedAt) continue // the record constructor would reject it } - val day = dayCache.getOrPut(session.date) { dao.ringAllByDay(session.date) } - val index = day.indexOfFirst { it.id == session.id } - if (index < 0) { - skipped++ - drop(session.updatedAt) - continue // cannot happen: the query filters to the same sourceRaw set - } - val recordId = HealthConnectTypeMappings.sleepSessionRecordId( - session.date, - HealthConnectTypeMappings.sleepSessionSuffix( - day.map { SleepDaySession(it.startAt, it.totalMinutes.toLong()) }, - index, - ), - ) + val recordId = HealthConnectTypeMappings.sleepSessionRecordIdV2(session.id) - val spans = blocksBySession[session.id].orEmpty().map { - SleepStageSpan( - it.startAt, - it.startAt + it.durationMinutes * 60_000L, - HealthConnectTypeMappings.sleepStageType(it.stageRaw), - ) - } - val stages = HealthConnectTypeMappings.normalizeSleepStages(session.startAt, session.endAt, spans) - if (stages.isEmpty()) { + val record = buildSleepSessionRecord( + session, + blocksBySession[session.id].orEmpty(), + recordId, + device, + zone, + ) + if (record == null) { skipped++ drop(session.updatedAt) continue // Gadgetbridge parity: a session with no valid stages is not written; // a later re-sync that adds stages bumps updatedAt and re-selects it } - val start = Instant.ofEpochMilli(session.startAt) - val end = Instant.ofEpochMilli(session.endAt) - records += SleepSessionRecord( - startTime = start, - startZoneOffset = HealthConnectTypeMappings.zoneOffsetAt(start, zone), - endTime = end, - endZoneOffset = HealthConnectTypeMappings.zoneOffsetAt(end, zone), - metadata = Metadata.autoRecorded(device, recordId, session.updatedAt), - title = null, - notes = null, - stages = stages.map { - SleepSessionRecord.Stage( - Instant.ofEpochMilli(it.startMs), - Instant.ofEpochMilli(it.endMs), - it.stageType, - ) - }, - ) + records += record highWaters += session.updatedAt } return PendingSleep(records, highWaters, skipped, droppedHigh) } } + +internal fun buildSleepSessionRecord( + session: SleepSessionEntity, + blocks: List, + recordId: String, + device: Device, + zone: ZoneId, +): SleepSessionRecord? { + if (session.endAt <= session.startAt) return null + val spans = blocks.map { + SleepStageSpan( + it.startAt, + it.startAt + it.durationMinutes * 60_000L, + HealthConnectTypeMappings.sleepStageType(it.stageRaw), + ) + } + val stages = HealthConnectTypeMappings.normalizeSleepStages(session.startAt, session.endAt, spans) + if (stages.isEmpty()) return null + + val start = Instant.ofEpochMilli(session.startAt) + val end = Instant.ofEpochMilli(session.endAt) + return SleepSessionRecord( + startTime = start, + startZoneOffset = HealthConnectTypeMappings.zoneOffsetAt(start, zone), + endTime = end, + endZoneOffset = HealthConnectTypeMappings.zoneOffsetAt(end, zone), + metadata = Metadata.autoRecorded(device, recordId, session.updatedAt), + title = null, + notes = null, + stages = stages.map { + SleepSessionRecord.Stage( + Instant.ofEpochMilli(it.startMs), + Instant.ofEpochMilli(it.endMs), + it.stageType, + ) + }, + ) +} diff --git a/app/src/main/java/com/pulseloop/ring/RingEventBridge.kt b/app/src/main/java/com/pulseloop/ring/RingEventBridge.kt index 0811a193..b2535f55 100644 --- a/app/src/main/java/com/pulseloop/ring/RingEventBridge.kt +++ b/app/src/main/java/com/pulseloop/ring/RingEventBridge.kt @@ -85,7 +85,10 @@ object RingEventBridge { listOf(PulseEvent.SyncProgress("done")) is RingDecodedEvent.SleepTimeline -> { - if (!isWithinHistoryWindow(decoded._timestamp, now) || decoded.stages.isEmpty()) emptyList() + val end = decoded._timestamp.plusSeconds(decoded.stages.size * 60L) + if (!isWithinHistoryWindow(decoded._timestamp, now) || + !isWithinHistoryWindow(end, now) || + decoded.stages.isEmpty()) emptyList() else listOf(PulseEvent.SleepTimeline(decoded._timestamp, decoded.stages, decoded.completeSession)) } diff --git a/app/src/main/java/com/pulseloop/ring/YCBTHealthRecords.kt b/app/src/main/java/com/pulseloop/ring/YCBTHealthRecords.kt index 2d68c24d..cb456284 100644 --- a/app/src/main/java/com/pulseloop/ring/YCBTHealthRecords.kt +++ b/app/src/main/java/com/pulseloop/ring/YCBTHealthRecords.kt @@ -185,6 +185,7 @@ object YCBTHealthRecords { continue } val recordLength = YCBTBytes.u16(buffer, cursor + 2) + val remaining = buffer.size - cursor // The vendor reads the session's own bounds out of the header (DataUnpack's // `startTime` at +4, `endTime` at +8) rather than inferring them from the segments. val headerStart = YCBTBytes.u32(buffer, cursor + 4) @@ -193,11 +194,19 @@ object YCBTHealthRecords { val declared = maxOf(0, recordLength - headerLength) / segmentLength val available = (buffer.size - segmentsStart) / segmentLength val segmentCount = minOf(declared, available) + val validDeclaredWidth = recordLength >= headerLength && + recordLength <= remaining && + (recordLength - headerLength) % segmentLength == 0 val segments = mutableListOf() val seenStarts = mutableSetOf() + var nextRecordBoundary: Int? = null for (index in 0 until segmentCount) { val offset = segmentsStart + index * segmentLength + if (isSleepSessionHeader(buffer, offset)) { + nextRecordBoundary = offset + break + } val stage = sleepStage(buffer[offset].toInt() and 0xFF) ?: continue val segmentStart = YCBTBytes.u32(buffer, offset + 1) val segmentSeconds = YCBTBytes.u24(buffer, offset + 5) @@ -215,26 +224,34 @@ object YCBTHealthRecords { if (!seenStarts.add(segmentStart)) continue segments.add(SleepSegment(stage, segmentStart, segmentSeconds)) } - val stages = placeStages(segments, headerStart, headerEnd) + val structurallyComplete = validDeclaredWidth && + segmentCount == declared && + nextRecordBoundary == null + val useHeaderBounds = structurallyComplete && usableHeaderBounds(headerStart, headerEnd) + val stages = placeStages(segments, headerStart, headerEnd, useHeaderBounds) if (segments.isNotEmpty() && stages.isNotEmpty()) { - val start = if (usableHeaderBounds(headerStart, headerEnd)) headerStart - else segments.first().startSeconds + val start = if (useHeaderBounds) headerStart else segments.first().startSeconds events.add( RingDecodedEvent.SleepTimeline( _timestamp = YCBTBytes.date(start), stages = stages, - completeSession = true, + completeSession = useHeaderBounds, ) ) } - cursor = segmentsStart + segmentCount * segmentLength + cursor = when { + nextRecordBoundary != null -> nextRecordBoundary + recordLength in headerLength..remaining -> cursor + recordLength + recordLength > remaining -> maxOf(cursor + 1, segmentsStart + segmentCount * segmentLength) + else -> cursor + 1 + } } return events } private fun usableHeaderBounds(startSeconds: Int, endSeconds: Int): Boolean = startSeconds > 0 && endSeconds > startSeconds && - (endSeconds - startSeconds) / 60 <= MAX_SLEEP_SESSION_MINUTES + (endSeconds.toLong() - startSeconds.toLong()) <= MAX_SLEEP_SESSION_MINUTES * 60L /** * A record's minute-by-minute stage timeline (issue #63). @@ -261,9 +278,10 @@ object YCBTHealthRecords { segments: List, headerStart: Int, headerEnd: Int, + useHeaderBounds: Boolean = usableHeaderBounds(headerStart, headerEnd), ): List { if (segments.isEmpty()) return emptyList() - if (!usableHeaderBounds(headerStart, headerEnd)) { + if (!useHeaderBounds) { val stages = mutableListOf() for (segment in segments) { val remaining = MAX_SLEEP_SESSION_MINUTES - stages.size diff --git a/app/src/main/java/com/pulseloop/service/EventPersistenceSubscriber.kt b/app/src/main/java/com/pulseloop/service/EventPersistenceSubscriber.kt index 15e18e8b..10799c38 100644 --- a/app/src/main/java/com/pulseloop/service/EventPersistenceSubscriber.kt +++ b/app/src/main/java/com/pulseloop/service/EventPersistenceSubscriber.kt @@ -887,7 +887,7 @@ class EventPersistenceSubscriber( totalMinutes = totalMin, score = score, syncedAt = now, - updatedAt = now, + updatedAt = row?.let { nextSleepUpdatedAt(now, it.updatedAt) } ?: now, ) ) seg.blocks.forEach { @@ -1261,6 +1261,11 @@ internal fun replaceOverlappingSleepBlocks( return byStart.values.sortedBy { it.startAt } } +internal fun nextSleepUpdatedAt(wallClockNow: Long, existingUpdatedAt: Long): Long { + val successor = if (existingUpdatedAt == Long.MAX_VALUE) Long.MAX_VALUE else existingUpdatedAt + 1L + return maxOf(wallClockNow, successor) +} + // Extension for Set CSV from WearableCapability.kt private fun Set.toCsv(): String = WearableCapability.entries.filter { it in this }.joinToString(",") { it.key } diff --git a/app/src/test/java/com/pulseloop/health/HealthConnectExporterTest.kt b/app/src/test/java/com/pulseloop/health/HealthConnectExporterTest.kt index 3989a347..ae0ba87f 100644 --- a/app/src/test/java/com/pulseloop/health/HealthConnectExporterTest.kt +++ b/app/src/test/java/com/pulseloop/health/HealthConnectExporterTest.kt @@ -6,6 +6,7 @@ import androidx.health.connect.client.records.ExerciseSessionRecord import androidx.health.connect.client.records.Record import androidx.health.connect.client.records.metadata.Device import androidx.health.connect.client.records.metadata.Metadata +import kotlinx.coroutines.CancellationException import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest import org.junit.Assert.assertEquals @@ -173,6 +174,82 @@ class HealthConnectExporterTest { assertEquals(499L, p.lastCompletedHighWater) } + @Test + fun sleepIdentityMigrationDeletesThenResetsThenMarks() = runTest { + val calls = mutableListOf() + val migrated = migrateSleepIdentityV2( + required = true, + deleteLegacyRecords = { calls += "delete" }, + resetSleepWatermark = { calls += "reset" }, + markDone = { calls += "mark" }, + ) + + assertTrue(migrated) + assertEquals(listOf("delete", "reset", "mark"), calls) + } + + @Test + fun sleepIdentityMigrationDeleteFailureDoesNotResetOrMark() = runTest { + val calls = mutableListOf() + val migrated = migrateSleepIdentityV2( + required = true, + deleteLegacyRecords = { + calls += "delete" + throw IllegalStateException("provider unavailable") + }, + resetSleepWatermark = { calls += "reset" }, + markDone = { calls += "mark" }, + ) + + assertFalse(migrated) + assertEquals(listOf("delete"), calls) + } + + @Test + fun sleepIdentityMigrationDoesNotSwallowCancellation() = runTest { + try { + migrateSleepIdentityV2( + required = true, + deleteLegacyRecords = { throw CancellationException("cancelled") }, + resetSleepWatermark = { fail("must not reset after cancellation") }, + markDone = { fail("must not mark after cancellation") }, + ) + fail("expected cancellation") + } catch (_: CancellationException) { + // Structured cancellation must propagate to WorkManager/coroutine ownership. + } + } + + @Test + fun sleepIdentityMigrationGatesSkipWithoutSideEffects() = runTest { + val permission = HealthConnectPermissions.sleep.first() + val ready = HealthConnectPrefs( + enabled = true, + sleep = true, + backfillChoice = HealthConnectPrefs.BackfillChoice.EXPORT_ALL, + ) + val gated = listOf( + ready.copy(enabled = false) to setOf(permission), + ready.copy(backfillChoice = HealthConnectPrefs.BackfillChoice.NOT_ASKED) to setOf(permission), + ready.copy(sleep = false) to setOf(permission), + ready to emptySet(), + ready.copy(sleepIdentityV2Done = true) to setOf(permission), + ) + + assertTrue(sleepIdentityV2MigrationRequired(ready, setOf(permission))) + gated.forEach { (prefs, granted) -> + var called = false + val migrated = migrateSleepIdentityV2( + required = sleepIdentityV2MigrationRequired(prefs, granted), + deleteLegacyRecords = { called = true }, + resetSleepWatermark = { called = true }, + markDone = { called = true }, + ) + assertTrue(migrated) + assertFalse(called) + } + } + // ── Phase 3: one source row can emit several records sharing one high water ── @Test diff --git a/app/src/test/java/com/pulseloop/health/HealthConnectPrefsStoreTest.kt b/app/src/test/java/com/pulseloop/health/HealthConnectPrefsStoreTest.kt index 9125f05b..e3bd11b3 100644 --- a/app/src/test/java/com/pulseloop/health/HealthConnectPrefsStoreTest.kt +++ b/app/src/test/java/com/pulseloop/health/HealthConnectPrefsStoreTest.kt @@ -116,10 +116,23 @@ class HealthConnectPrefsStoreTest { val store = storeWith("{\"enabled\":true}") assertTrue(store.current.enabled) assertTrue(store.current.workouts) // field did not exist when the blob was written + assertFalse(store.current.sleepIdentityV2Done) assertNull(store.current.lastSyncAt) assertFalse(store.current.isConnected) } + @Test + fun sleepIdentityV2MarkerDefaultsFalseAndRoundTripsTrue() { + assertFalse(storeWith(null).current.sleepIdentityV2Done) + assertFalse(storeWith("{\"enabled\":true}").current.sleepIdentityV2Done) + + val fake = FakeSharedPreferences() + val store = HealthConnectPrefsStore(fake) + store.update { it.copy(sleepIdentityV2Done = true) } + + assertTrue(HealthConnectPrefsStore(fake).current.sleepIdentityV2Done) + } + @Test fun corruptBlobFallsBackToDefaults() { val store = storeWith("not-json{") diff --git a/app/src/test/java/com/pulseloop/health/SleepExporterMappingTest.kt b/app/src/test/java/com/pulseloop/health/SleepExporterMappingTest.kt new file mode 100644 index 00000000..3b5009d8 --- /dev/null +++ b/app/src/test/java/com/pulseloop/health/SleepExporterMappingTest.kt @@ -0,0 +1,141 @@ +package com.pulseloop.health + +import androidx.health.connect.client.records.SleepSessionRecord +import androidx.health.connect.client.records.metadata.Device +import com.pulseloop.data.entity.SleepSessionEntity +import com.pulseloop.data.entity.SleepStageBlockEntity +import com.pulseloop.health.exporters.buildSleepSessionRecord +import com.pulseloop.ring.SleepStage +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import java.time.Instant +import java.time.ZoneId + +class SleepExporterMappingTest { + private val device = Device(type = Device.TYPE_RING, manufacturer = "test", model = "YCBT") + + @Test + fun `corrected Room bounds and unknown gaps build a valid Health Connect sleep record`() { + val start = Instant.parse("2026-07-06T22:30:00Z") + val end = start.plusSeconds(6 * 60 * 60L) + val day = Instant.parse("2026-07-07T00:00:00Z").toEpochMilli() + val updatedAt = 2_000L + val session = SleepSessionEntity( + id = "room-session", + date = day, + startAt = start.toEpochMilli(), + endAt = end.toEpochMilli(), + totalMinutes = 360, + updatedAt = updatedAt, + ) + val blocks = listOf( + block(session.id, start, 0, 30, SleepStage.UNKNOWN), + block(session.id, start.plusSeconds(30 * 60L), 30, 60, SleepStage.LIGHT), + block(session.id, start.plusSeconds(90 * 60L), 90, 150, SleepStage.UNKNOWN), + block(session.id, start.plusSeconds(4 * 60 * 60L), 240, 56, SleepStage.DEEP), + block(session.id, start.plusSeconds(4 * 60 * 60L + 56 * 60L), 296, 64, SleepStage.UNKNOWN), + ) + val recordId = HealthConnectTypeMappings.sleepSessionRecordIdV2(session.id) + + val record = buildSleepSessionRecord(session, blocks, recordId, device, ZoneId.of("UTC"))!! + + assertEquals(start, record.startTime) + assertEquals(end, record.endTime) + assertEquals(recordId, record.metadata.clientRecordId) + assertEquals(updatedAt, record.metadata.clientRecordVersion) + assertEquals( + listOf( + SleepSessionRecord.STAGE_TYPE_UNKNOWN, + SleepSessionRecord.STAGE_TYPE_LIGHT, + SleepSessionRecord.STAGE_TYPE_UNKNOWN, + SleepSessionRecord.STAGE_TYPE_DEEP, + SleepSessionRecord.STAGE_TYPE_UNKNOWN, + ), + record.stages.map { it.stage }, + ) + assertEquals(start, record.stages.first().startTime) + assertEquals(end, record.stages.last().endTime) + assertTrue(record.stages.zipWithNext().all { (a, b) -> a.endTime <= b.startTime }) + } + + @Test + fun `sleep correction keeps client identity and advances updatedAt version`() { + val day = Instant.parse("2026-07-07T00:00:00Z").toEpochMilli() + val start = Instant.parse("2026-07-06T22:30:00Z") + fun record(updatedAt: Long, minutes: Int): SleepSessionRecord { + val end = start.plusSeconds(minutes * 60L) + val session = SleepSessionEntity( + id = "room-session", + date = day, + startAt = start.toEpochMilli(), + endAt = end.toEpochMilli(), + totalMinutes = minutes, + updatedAt = updatedAt, + ) + val blocks = listOf(block(session.id, start, 0, minutes, SleepStage.UNKNOWN)) + return buildSleepSessionRecord( + session, + blocks, + HealthConnectTypeMappings.sleepSessionRecordIdV2(session.id), + device, + ZoneId.of("UTC"), + )!! + } + + val collapsed = record(updatedAt = 1_000L, minutes = 116) + val corrected = record(updatedAt = 2_000L, minutes = 360) + + assertEquals(collapsed.metadata.clientRecordId, corrected.metadata.clientRecordId) + assertEquals(1_000L, collapsed.metadata.clientRecordVersion) + assertEquals(2_000L, corrected.metadata.clientRecordVersion) + assertTrue(corrected.endTime > collapsed.endTime) + } + + @Test + fun `role change keeps every stable v2 identity unchanged`() { + val day = Instant.parse("2026-07-07T00:00:00Z").toEpochMilli() + val nightStart = Instant.parse("2026-07-06T22:30:00Z") + val napStart = Instant.parse("2026-07-07T14:00:00Z") + + fun session(id: String, start: Instant, minutes: Int, updatedAt: Long) = SleepSessionEntity( + id = id, + date = day, + startAt = start.toEpochMilli(), + endAt = start.plusSeconds(minutes * 60L).toEpochMilli(), + totalMinutes = minutes, + updatedAt = updatedAt, + ) + + val collapsedNight = session("night-session", nightStart, 116, 1_000L) + val nap = session("nap-session", napStart, 180, 1_000L) + val correctedNight = session("night-session", nightStart, 360, 2_000L) + + val idsBefore = listOf(collapsedNight, nap).associate { session -> + session.id to HealthConnectTypeMappings.sleepSessionRecordIdV2(session.id) + } + val idsAfter = listOf(correctedNight, nap).associate { session -> + session.id to HealthConnectTypeMappings.sleepSessionRecordIdV2(session.id) + } + + assertTrue(collapsedNight.totalMinutes < nap.totalMinutes) + assertTrue(correctedNight.totalMinutes > nap.totalMinutes) + assertEquals(idsBefore, idsAfter) + assertEquals(2, idsAfter.values.toSet().size) + assertTrue(idsAfter.values.all { it.length <= 100 }) + } + + private fun block( + sessionId: String, + start: Instant, + startMinute: Int, + durationMinutes: Int, + stage: SleepStage, + ) = SleepStageBlockEntity( + sessionId = sessionId, + startAt = start.toEpochMilli(), + startMinute = startMinute, + durationMinutes = durationMinutes, + stageRaw = stage.name, + ) +} diff --git a/app/src/test/java/com/pulseloop/ring/RingEventBridgeTest.kt b/app/src/test/java/com/pulseloop/ring/RingEventBridgeTest.kt index 3f50996c..122fa1f6 100644 --- a/app/src/test/java/com/pulseloop/ring/RingEventBridgeTest.kt +++ b/app/src/test/java/com/pulseloop/ring/RingEventBridgeTest.kt @@ -233,6 +233,16 @@ class RingEventBridgeTest { assertTrue(RingEventBridge.eventsFor(sleep, now).isEmpty()) } + @Test + fun `sleep timeline whose end is over one hour in the future is dropped`() { + val start = now.minus(30, ChronoUnit.MINUTES) + val sleep = RingDecodedEvent.SleepTimeline( + _timestamp = start, + stages = List(150) { SleepStage.LIGHT }, + ) + assertTrue(RingEventBridge.eventsFor(sleep, now).isEmpty()) + } + // ── Battery Gating ────────────────────────────────────────────────── @Test diff --git a/app/src/test/java/com/pulseloop/ring/YCBTHealthRecordsTest.kt b/app/src/test/java/com/pulseloop/ring/YCBTHealthRecordsTest.kt index 4a075d24..d4c7a5dc 100644 --- a/app/src/test/java/com/pulseloop/ring/YCBTHealthRecordsTest.kt +++ b/app/src/test/java/com/pulseloop/ring/YCBTHealthRecordsTest.kt @@ -200,6 +200,116 @@ class YCBTHealthRecordsTest { session = session.copyOfRange(0, session.size - 8) val event = YCBTHealthRecords.sleep(session).first() as RingDecodedEvent.SleepTimeline assertEquals(10, event.stages.size) + assertFalse(event.completeSession) + } + + @Test + fun `header longer than one day falls back instead of expanding the session`() { + val start = 0x31def01c + val record = sleepRecordAt( + start, + start + 24 * 60 * 60 + 59, + listOf(Triple(0xf2, start, 30 * 60)), + ) + val event = YCBTHealthRecords.sleep(record).single() as RingDecodedEvent.SleepTimeline + + assertFalse(event.completeSession) + assertEquals(30, event.stages.size) + } + + @Test + fun `valid full night header on a truncated record does not invent or replace its missing tail`() { + val start = 0x31def01c + val complete = sleepRecordAt( + start, + start + 8 * 60 * 60, + listOf( + Triple(0xf2, start + 30 * 60, 60 * 60), + Triple(0xf1, start + 90 * 60, 6 * 60 * 60), + ), + ) + val event = YCBTHealthRecords.sleep(complete.copyOf(complete.size - 8)) + .single() as RingDecodedEvent.SleepTimeline + + assertFalse(event.completeSession) + assertEquals(YCBTBytes.date(start + 30 * 60), event._timestamp) + assertEquals(60, event.stages.size) + assertTrue(event.stages.all { it == SleepStage.LIGHT }) + } + + @Test + fun `record with a partial segment width is not authoritative`() { + val start = 0x31def01c + val session = sleepRecordAt( + start, + start + 60 * 60, + listOf(Triple(0xf2, start, 60 * 60)), + ) + byteArrayOf(0) + session[2] = (session.size and 0xFF).toByte() + session[3] = ((session.size ushr 8) and 0xFF).toByte() + + val event = YCBTHealthRecords.sleep(session).single() as RingDecodedEvent.SleepTimeline + + assertFalse(event.completeSession) + assertEquals(60, event.stages.size) + } + + @Test + fun `partial-width record advances by its declared length before the next record`() { + val firstStart = 0x31def01c + val malformed = (sleepRecordAt( + firstStart, + firstStart + 60 * 60, + listOf(Triple(0xf2, firstStart, 60 * 60)), + ) + byteArrayOf(0)).also { + it[2] = (it.size and 0xFF).toByte() + it[3] = ((it.size ushr 8) and 0xFF).toByte() + } + val secondStart = firstStart + 150 * 60 + val valid = sleepRecordAt( + secondStart, + secondStart + 120 * 60, + listOf(Triple(0xf1, secondStart, 120 * 60)), + ) + + val events = YCBTHealthRecords.sleep(malformed + valid) + .filterIsInstance() + + assertEquals(2, events.size) + assertFalse(events[0].completeSession) + assertEquals(YCBTBytes.date(secondStart), events[1]._timestamp) + assertTrue(events[1].completeSession) + assertEquals(120, events[1].stages.size) + } + + @Test + fun `truncated record stops at the following record preamble instead of borrowing it`() { + val firstStart = 0x31def01c + val truncated = sleepRecordAt( + firstStart, + firstStart + 120 * 60, + listOf(Triple(0xf2, firstStart, 60 * 60)), + ).also { + val claimed = it.size + 8 + it[2] = (claimed and 0xFF).toByte() + it[3] = ((claimed ushr 8) and 0xFF).toByte() + } + val secondStart = firstStart + 180 * 60 + val valid = sleepRecordAt( + secondStart, + secondStart + 90 * 60, + listOf(Triple(0xf1, secondStart, 90 * 60)), + ) + + val events = YCBTHealthRecords.sleep(truncated + valid) + .filterIsInstance() + + assertEquals(2, events.size) + assertFalse(events[0].completeSession) + assertEquals(60, events[0].stages.size) + assertEquals(YCBTBytes.date(secondStart), events[1]._timestamp) + assertTrue(events[1].completeSession) + assertEquals(90, events[1].stages.size) } @Test diff --git a/app/src/test/java/com/pulseloop/service/EventPersistenceIdentityTest.kt b/app/src/test/java/com/pulseloop/service/EventPersistenceIdentityTest.kt index f7e016b5..fa87c80a 100644 --- a/app/src/test/java/com/pulseloop/service/EventPersistenceIdentityTest.kt +++ b/app/src/test/java/com/pulseloop/service/EventPersistenceIdentityTest.kt @@ -258,4 +258,15 @@ class EventPersistenceIdentityTest { // Nothing of ours: nothing to adopt, whatever the ring sends. assertTrue(spotReadingsMatching(emptyList(), 1_000_000L).isEmpty()) } + + @Test + fun `corrected sleep updatedAt advances within the same millisecond`() { + assertEquals(1_001L, nextSleepUpdatedAt(wallClockNow = 1_000L, existingUpdatedAt = 1_000L)) + } + + @Test + fun `corrected sleep updatedAt advances across clock rollback and saturates safely`() { + assertEquals(2_001L, nextSleepUpdatedAt(wallClockNow = 1_000L, existingUpdatedAt = 2_000L)) + assertEquals(Long.MAX_VALUE, nextSleepUpdatedAt(0L, Long.MAX_VALUE)) + } } diff --git a/tools/qc_snoop.py b/tools/qc_snoop.py new file mode 100644 index 00000000..d717d8e6 --- /dev/null +++ b/tools/qc_snoop.py @@ -0,0 +1,215 @@ +#!/usr/bin/env python3 +"""Decode QC/Oudmon ring traffic (QRing-family Colmi/Yawell rings) out of an Android HCI log. + +Pull the log from a phone with "Bluetooth HCI snoop log" set to *Enabled* (not Filtered — the +filtered log truncates every ATT payload to a few bytes): + + adb bugreport bug.zip + unzip -o bug.zip 'FS/data/misc/bluetooth/logs/*' + python3 tools/qc_snoop.py FS/data/misc/bluetooth/logs/btsnoop_hci.log* + +Several files (the live log and its rotated `.last`) are merged in time order. The ring's two +channels are recognised by content rather than by GATT handle, so traffic from earbuds, cars and +other peripherals in the same log is skipped: + + * UART — 16-byte frames whose last byte is the low byte of the sum of the other 15 + (write 6e400002 / notify 6e400003). + * BIG — `bc ` frames, reassembled across + notifications (write de5bf72a / notify de5bf729). + +Opcode names come from the vendor SDK (`com.oudmon.ble.base.communication.Constants` and +`LargeDataHandler.respMap`, decompiled-qring-official/) and from PulseLoop's ColmiProtocol.kt. +Python 3 standard library only. +""" +import argparse +import collections +import datetime +import struct +import sys + +# btsnoop timestamps count microseconds from 0000-01-01; this is 1970-01-01 on that scale. +BTSNOOP_EPOCH_DELTA_US = 0x00DCDDB30F2F8000 + +UART_NAMES = { + 0x01: "SET_TIME", 0x02: "TAKE_PICTURE", 0x03: "BATTERY", 0x04: "SET_PHONE_OS", + 0x05: "DISPLAY_PREF", 0x06: "MUTE", 0x07: "STEP_TOTAL_DAY", 0x08: "REBOOT/POWER_OFF", + 0x0A: "PREFERENCES", 0x0C: "BP_TIMING_SWITCH", 0x0D: "HR_TIMING_DATA", 0x0E: "HR_TIMING_CONFIRM", + 0x10: "BIND_SUCCESS", 0x11: "PHONE_NOTIFY", 0x13: "GET_SPORT", 0x14: "BP_READ", + 0x15: "HR_HISTORY", 0x16: "AUTO_HR_PREF", 0x19: "DEGREE_SWITCH", 0x1E: "REALTIME_HR", + 0x21: "GOALS", 0x22: "FIND_PHONE", 0x25: "SET_SIT_LONG", 0x26: "GET_SIT_LONG", + 0x27: "SET_DRINK", 0x28: "GET_DRINK", 0x2B: "MENSTRUATION", 0x2C: "AUTO_SPO2_PREF", + 0x2F: "PACKET_SIZE", 0x36: "AUTO_STRESS_PREF", 0x37: "STRESS_HISTORY", 0x38: "AUTO_HRV_PREF", + 0x39: "HRV_HISTORY", 0x3A: "AUTO_TEMP_PREF", 0x3B: "TOUCH/GESTURE", 0x3C: "DEVICE_SUPPORT", + 0x43: "ACTIVITY_DAY", 0x44: "GET_SLEEP", 0x48: "STEP_TODAY", 0x50: "FIND_DEVICE", + 0x51: "NOTIFICATION_EVENT", 0x52: "MUSLIM_REMIND", 0x60: "ANCS_SET", 0x61: "ANCS_GET", + 0x69: "MEASURE_START", 0x6A: "MEASURE_STOP", 0x72: "PUSH_MSG", 0x73: "DEVICE_NOTIFY", + 0x77: "PHONE_SPORT", 0x78: "SPORT_NOTIFY", 0x93: "HW_FW_VERSION", 0xA1: "DEVICE_REVISION", + 0xFF: "FACTORY_RESET", +} +# Second byte of 0x69/0x6A selects what is measured. +MEASURE_KINDS = {0x01: "hr", 0x02: "bp", 0x03: "spo2", 0x04: "fatigue", 0x05: "health_check", + 0x06: "realtime_hr", 0x07: "ecg", 0x08: "pressure", 0x09: "blood_sugar", + 0x0A: "hrv", 0x0B: "temperature"} +BIG_NAMES = { + 0x20: "DEVICE_LOCATION", 0x25: "TEMPERATURE", 0x27: "SLEEP", 0x28: "MANUAL_HR_LIST", + 0x29: "CONTACTS", 0x2A: "SPO2", 0x2C: "ALARM", 0x2E: "CLASSIC_BT", 0x2F: "QR_CODE", + 0x3A: "CUSTOM_WATCH", 0x3E: "SLEEP_LUNCH", 0x47: "BLOOD_SUGAR", 0x49: "MANUAL_SPO2_LIST", + # From the SDK's addHeader() callers: FileHandle.start, SportPlusHandle.cmdSummary/cmdRequest, + # LargeDataHandler.setDeviceNickName. + 0x30: "FILE_TRANSFER_START", 0x41: "SPORT_SUMMARY", 0x42: "SPORT_SUMMARY_REPLY", + 0x43: "SPORT_DETAIL", 0x4A: "DEVICE_NICKNAME", + 0x76: "EMERGENCY_CONTACT", 0x77: "INTERVAL_TEMPERATURE", 0x78: "SINGLE_ECG", +} + + +def read_btsnoop(path): + """Yield (unix_us, sent_by_host, h4_packet) for every record in one btsnoop file.""" + data = open(path, "rb").read() + if data[:8] != b"btsnoop\x00": + raise SystemExit(f"{path}: not a btsnoop file") + datalink = struct.unpack_from(">I", data, 12)[0] + if datalink != 1002: + raise SystemExit(f"{path}: datalink {datalink}, expected 1002 (H4)") + pos = 16 + while pos + 24 <= len(data): + _orig, incl, flags, _drops, ts = struct.unpack_from(">IIIIq", data, pos) + pos += 24 + pkt = data[pos:pos + incl] + pos += incl + # flags bit 0: 0 = sent (host -> controller), 1 = received + yield ts - BTSNOOP_EPOCH_DELTA_US, (flags & 1) == 0, pkt + + +def is_uart_frame(b): + return len(b) == 16 and sum(b[:15]) & 0xFF == b[15] + + +def describe_uart(b): + op = b[0] + name = UART_NAMES.get(op & 0x7F, f"0x{op & 0x7F:02X}") + if op & 0x80 and op != 0xFF and (op & 0x7F) in UART_NAMES: + name += " ERROR" + if op in (0x69, 0x6A): + name += f"[{MEASURE_KINDS.get(b[1], hex(b[1]))}]" + return name + + +class Decoder: + def __init__(self, addr_filter=None): + self.addr_filter = addr_filter.lower() if addr_filter else None + self.peer = {} # ACL handle -> peer address string + self.acl_buf = {} # (handle, sent) -> [expected_len, bytes] + self.att_role = {} # (handle, att_handle) -> "UART" | "BIG" + self.big = {} # (handle, att_handle) -> [expected_len, bytes, t0] + self.rows = [] + + def feed(self, t_us, sent, pkt): + if not pkt: + return + kind = pkt[0] + if kind == 0x04 and len(pkt) > 3: + self._event(pkt[1], pkt[3:]) + elif kind == 0x02 and len(pkt) >= 5: + self._acl(t_us, sent, pkt[1:]) + + def _event(self, code, p): + if code == 0x3E and p and p[0] in (0x01, 0x0A) and len(p) >= 12 and p[1] == 0: + handle = struct.unpack_from("= 3: + self.peer.pop(struct.unpack_from("> 12) & 0x3 + frag = a[4:4 + length] + key = (handle, sent) + if pb == 0x1: # continuation + if key not in self.acl_buf: + return + self.acl_buf[key][1] += frag + else: # start of an L2CAP PDU + if len(frag) < 4: + return + self.acl_buf[key] = [struct.unpack_from("= 6: + role = "BIG" + else: + return + self.att_role[key] = role + if role == "UART": + if is_uart_frame(value): + self.rows.append((t_us, sent, "UART", describe_uart(value), value)) + return + # BIG: start a frame on a `bc` header, otherwise append to the one in flight. + state = self.big.get(key) + if state is None or value[:1] == b"\xbc" and len(state[1]) >= state[0]: + if value[:1] != b"\xbc" or len(value) < 6: + return + state = [struct.unpack_from("= state[0]: + frame = bytes(state[1][:state[0]]) + del self.big[key] + name = BIG_NAMES.get(frame[1], f"0x{frame[1]:02X}") + self.rows.append((state[2], sent, "BIG", f"{name} len={state[0] - 6}", frame)) + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) + ap.add_argument("logs", nargs="+", help="btsnoop_hci.log files (rotated .last too)") + ap.add_argument("--addr", help="only this peer (substring of the BD address)") + ap.add_argument("--after", help="local time HH:MM[:SS]; drop frames before it") + ap.add_argument("--summary", action="store_true", help="count frames per opcode instead") + ap.add_argument("--width", type=int, default=64, help="max hex chars per line (0 = all)") + args = ap.parse_args() + + records = [r for path in args.logs for r in read_btsnoop(path)] + records.sort(key=lambda r: r[0]) + dec = Decoder(args.addr) + for t_us, sent, pkt in records: + dec.feed(t_us, sent, pkt) + + rows = dec.rows + if args.after: + hh, mm, *ss = (int(x) for x in args.after.split(":")) + rows = [r for r in rows + if datetime.datetime.fromtimestamp(r[0] / 1e6).time() >= datetime.time(hh, mm, *(ss or [0]))] + if args.summary: + counts = collections.Counter((r[2], "->" if r[1] else "<-", r[3].split(" len=")[0]) for r in rows) + for (chan, d, name), n in sorted(counts.items()): + print(f"{chan:4} {d} {name:28} {n}") + return + for t_us, sent, chan, name, frame in rows: + ts = datetime.datetime.fromtimestamp(t_us / 1e6).strftime("%H:%M:%S.%f")[:-3] + hexs = frame.hex() + if args.width and len(hexs) > args.width: + hexs = hexs[:args.width] + f"…(+{len(frame) - args.width // 2}B)" + print(f"{ts} {'->' if sent else '<-'} {chan:4} {name:28} {hexs}") + + +if __name__ == "__main__": + sys.exit(main())