From 027a1a5e7d3ddea2096fd785ff2c279ef62d2545 Mon Sep 17 00:00:00 2001 From: Matej Drobnic Date: Thu, 3 Dec 2020 11:19:09 +0100 Subject: [PATCH] update timeline packet definitions --- .../libpebblecommon/packets/blobdb/BlobDB.kt | 43 +++++++++++++++---- .../packets/blobdb/Timeline.kt | 36 +++++++++++++++- .../services/blobdb/BlobDBService.kt | 10 +++-- 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/src/commonMain/kotlin/io/rebble/libpebblecommon/packets/blobdb/BlobDB.kt b/src/commonMain/kotlin/io/rebble/libpebblecommon/packets/blobdb/BlobDB.kt index cba8064..66665ea 100644 --- a/src/commonMain/kotlin/io/rebble/libpebblecommon/packets/blobdb/BlobDB.kt +++ b/src/commonMain/kotlin/io/rebble/libpebblecommon/packets/blobdb/BlobDB.kt @@ -7,9 +7,10 @@ import io.rebble.libpebblecommon.structmapper.SBytes import io.rebble.libpebblecommon.structmapper.SUByte import io.rebble.libpebblecommon.structmapper.SUShort -open class BlobCommand constructor(message: Message, token: UShort, database: BlobDatabase) : PebblePacket( - endpoint -) { +open class BlobCommand constructor(message: Message, token: UShort, database: BlobDatabase) : + PebblePacket( + endpoint + ) { enum class Message(val value: UByte) { Insert(0x01u), Delete(0x04u), @@ -29,7 +30,12 @@ open class BlobCommand constructor(message: Message, token: UShort, database: Bl val token = SUShort(m, token) val database = SUByte(m, database.id) - open class InsertCommand(token: UShort, database: BlobDatabase, key: UByteArray, value: UByteArray) : BlobCommand( + open class InsertCommand( + token: UShort, + database: BlobDatabase, + key: UByteArray, + value: UByteArray + ) : BlobCommand( Message.Insert, token, database ) { val keySize = SUByte(m, key.size.toUByte()) @@ -66,7 +72,10 @@ open class BlobResponse(response: BlobStatus = BlobStatus.GeneralFailure) : Pebb DataStale(0x08u), NotSupported(0x09u), Locked(0xAu), - TryLater(0xBu) + TryLater(0xBu), + + // Added status by libpebblecommon to expose watch connection error + WatchDisconnected(0xFFu), } class Success : BlobResponse(BlobStatus.Success) @@ -84,6 +93,10 @@ open class BlobResponse(response: BlobStatus = BlobStatus.GeneralFailure) : Pebb val token = SUShort(m) val response = SUByte(m, response.value) + val responseValue + get() = BlobStatus.values().firstOrNull { it.value == response.get() } + ?: error("Unknown blobdb response ${response.get()}") + companion object { val endpoint = ProtocolEndpoint.BLOBDB_V1 } @@ -91,7 +104,10 @@ open class BlobResponse(response: BlobStatus = BlobStatus.GeneralFailure) : Pebb fun blobDBPacketsRegister() { PacketRegistry.registerCustomTypeOffset(BlobResponse.endpoint, 4 + UShort.SIZE_BYTES) - PacketRegistry.register(BlobResponse.endpoint, BlobResponse.BlobStatus.Success.value) { BlobResponse.Success() } + PacketRegistry.register( + BlobResponse.endpoint, + BlobResponse.BlobStatus.Success.value + ) { BlobResponse.Success() } PacketRegistry.register( BlobResponse.endpoint, BlobResponse.BlobStatus.GeneralFailure.value @@ -116,11 +132,20 @@ fun blobDBPacketsRegister() { BlobResponse.endpoint, BlobResponse.BlobStatus.DatabaseFull.value ) { BlobResponse.DatabaseFull() } - PacketRegistry.register(BlobResponse.endpoint, BlobResponse.BlobStatus.DataStale.value) { BlobResponse.DataStale() } + PacketRegistry.register( + BlobResponse.endpoint, + BlobResponse.BlobStatus.DataStale.value + ) { BlobResponse.DataStale() } PacketRegistry.register( BlobResponse.endpoint, BlobResponse.BlobStatus.NotSupported.value ) { BlobResponse.NotSupported() } - PacketRegistry.register(BlobResponse.endpoint, BlobResponse.BlobStatus.Locked.value) { BlobResponse.Locked() } - PacketRegistry.register(BlobResponse.endpoint, BlobResponse.BlobStatus.TryLater.value) { BlobResponse.TryLater() } + PacketRegistry.register( + BlobResponse.endpoint, + BlobResponse.BlobStatus.Locked.value + ) { BlobResponse.Locked() } + PacketRegistry.register( + BlobResponse.endpoint, + BlobResponse.BlobStatus.TryLater.value + ) { BlobResponse.TryLater() } } \ No newline at end of file diff --git a/src/commonMain/kotlin/io/rebble/libpebblecommon/packets/blobdb/Timeline.kt b/src/commonMain/kotlin/io/rebble/libpebblecommon/packets/blobdb/Timeline.kt index 6320c52..902f293 100644 --- a/src/commonMain/kotlin/io/rebble/libpebblecommon/packets/blobdb/Timeline.kt +++ b/src/commonMain/kotlin/io/rebble/libpebblecommon/packets/blobdb/Timeline.kt @@ -19,7 +19,14 @@ class TimelineItem( enum class Type(val value: UByte) { Notification(1u), Pin(2u), - Reminder(3u) + Reminder(3u); + + companion object { + fun fromValue(value: UByte): Type { + return values().firstOrNull { it.value == value } + ?: error("Unknown timeline item type: $value") + } + } } val itemId = SUUID(m, itemId) @@ -90,7 +97,11 @@ class TimelineItem( val length = SUShort(m, endianness = '<') val content = SBytes(m, 0) - constructor(attributeId: UByte, content: UByteArray, contentEndianness: Char = '|'): this() { + constructor( + attributeId: UByte, + content: UByteArray, + contentEndianness: Char = '|' + ) : this() { this.attributeId.set(attributeId) this.length.set(content.size.toUShort()) @@ -103,6 +114,27 @@ class TimelineItem( content.linkWithSize(length) } } + + enum class Flag(val value: Int) { + IS_VISIBLE(0), + IS_FLOATING(1), + IS_ALL_DAY(2), + FROM_WATCH(3), + FROM_ANCS(4), + PERSIST_QUICK_VIEW(5); + + companion object { + fun makeFlags(flags: List): UShort { + var short: UShort = 0u + + for (flag in flags) { + short = (1u shl flag.value).toUShort() or short + } + + return short + } + } + } } open class TimelineAction(message: Message) : PebblePacket(endpoint) { diff --git a/src/commonMain/kotlin/io/rebble/libpebblecommon/services/blobdb/BlobDBService.kt b/src/commonMain/kotlin/io/rebble/libpebblecommon/services/blobdb/BlobDBService.kt index 1339e03..1093f00 100644 --- a/src/commonMain/kotlin/io/rebble/libpebblecommon/services/blobdb/BlobDBService.kt +++ b/src/commonMain/kotlin/io/rebble/libpebblecommon/services/blobdb/BlobDBService.kt @@ -1,5 +1,6 @@ package io.rebble.libpebblecommon.services.blobdb +import io.rebble.libpebblecommon.PacketPriority import io.rebble.libpebblecommon.ProtocolHandler import io.rebble.libpebblecommon.packets.blobdb.BlobCommand import io.rebble.libpebblecommon.packets.blobdb.BlobResponse @@ -27,13 +28,16 @@ class BlobDBService(private val protocolHandler: ProtocolHandler) : ProtocolServ * * @return [BlobResponse] from the watch or *null* if the sending failed */ - suspend fun send(packet: BlobCommand): BlobResponse? { + suspend fun send( + packet: BlobCommand, + priority: PacketPriority = PacketPriority.NORMAL + ): BlobResponse { val result = CompletableDeferred() pending[packet.token.get()] = result - val sendingResult = protocolHandler.send(packet) + val sendingResult = protocolHandler.send(packet, priority) if (!sendingResult) { - return null + return BlobResponse(BlobResponse.BlobStatus.WatchDisconnected) } return result.await()