Skip to content

Commit

Permalink
Merge pull request #18 from matejdro/app_reorder
Browse files Browse the repository at this point in the history
App reorder
  • Loading branch information
crc-32 authored Apr 22, 2021
2 parents 920ee37 + 5660366 commit 5442634
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.rebble.libpebblecommon.packets

import com.benasher44.uuid.Uuid
import io.rebble.libpebblecommon.protocolhelpers.PacketRegistry
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
import io.rebble.libpebblecommon.structmapper.*

class AppReorderResult() :
PebblePacket(ProtocolEndpoint.APP_REORDER) {
/**
* Result code. See [AppOrderResultCode].
*/
val status = SUByte(m)

}

sealed class AppReorderOutgoingPacket(type: AppReorderType) :
PebblePacket(ProtocolEndpoint.APP_REORDER) {

/**
* Packet type. See [AppReorderType].
*/
val command = SUByte(m, type.value)
}

enum class AppOrderResultCode(val value: UByte) {
SUCCESS(0x01u),
FAILED(0x02u),
INVALID(0x03u),
RETRY(0x04u);

companion object {
fun fromByte(value: UByte): AppOrderResultCode {
return values().firstOrNull { it.value == value } ?: error("Unknown result: $value")
}
}
}


/**
* Packet sent from the watch when user opens an app that is not in the watch storage.
*/
class AppReorderRequest(
appList: List<Uuid>
) : AppReorderOutgoingPacket(AppReorderType.REORDER_APPS) {
val appCount = SByte(m, appList.size.toByte())

val appList = SFixedList<SUUID>(
mapper = m,
count = appList.size,
default = appList.map { SUUID(StructMapper(), it) },
itemFactory = {
SUUID(
StructMapper()
)
})
}

enum class AppReorderType(val value: UByte) {
REORDER_APPS(0x01u)
}


fun appReorderIncomingRegister() {
PacketRegistry.register(
ProtocolEndpoint.APP_REORDER,
) { AppReorderResult() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import io.rebble.libpebblecommon.packets.blobdb.timelinePacketsRegister
*/
object PacketRegistry {
private var typeOffsets: MutableMap<ProtocolEndpoint, Int> = mutableMapOf()
private var decoders: MutableMap<ProtocolEndpoint, MutableMap<UByte, (UByteArray) -> PebblePacket>> = mutableMapOf()
private var typedDecoders: MutableMap<ProtocolEndpoint, MutableMap<UByte, (UByteArray) -> PebblePacket>> =
mutableMapOf()
private var universalDecoders: MutableMap<ProtocolEndpoint, (UByteArray) -> PebblePacket> =
mutableMapOf()

init {
systemPacketsRegister()
Expand All @@ -22,6 +25,7 @@ object PacketRegistry {
musicPacketsRegister()
appFetchIncomingPacketsRegister()
putBytesIncomingPacketsRegister()
appReorderIncomingRegister()
}

/**
Expand All @@ -33,15 +37,21 @@ object PacketRegistry {
typeOffsets[endpoint] = offset
}

fun register(endpoint: ProtocolEndpoint, decoder: (UByteArray) -> PebblePacket) {
universalDecoders[endpoint] = decoder
}

fun register(endpoint: ProtocolEndpoint, type: UByte, decoder: (UByteArray) -> PebblePacket) {
if (decoders[endpoint] == null) {
decoders[endpoint] = mutableMapOf()
if (typedDecoders[endpoint] == null) {
typedDecoders[endpoint] = mutableMapOf()
}
decoders[endpoint]!![type] = decoder
typedDecoders[endpoint]!![type] = decoder
}

fun get(endpoint: ProtocolEndpoint, packet: UByteArray): PebblePacket {
val epdecoders = decoders[endpoint]
universalDecoders[endpoint]?.let { return it(packet) }

val epdecoders = typedDecoders[endpoint]
?: throw PacketDecodeException("No packet class registered for endpoint $endpoint")

val typeOffset = if (typeOffsets[endpoint] != null) typeOffsets[endpoint]!! else 4
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.rebble.libpebblecommon.services

import io.rebble.libpebblecommon.ProtocolHandler
import io.rebble.libpebblecommon.packets.AppOrderResultCode
import io.rebble.libpebblecommon.packets.AppReorderOutgoingPacket
import io.rebble.libpebblecommon.packets.AppReorderResult
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
import kotlinx.coroutines.channels.Channel

class AppReorderService(private val protocolHandler: ProtocolHandler) : ProtocolService {
val receivedMessages = Channel<AppReorderResult>(Channel.BUFFERED)
private var lastOrderPacket: AppReorderOutgoingPacket? = null

init {
protocolHandler.registerReceiveCallback(ProtocolEndpoint.APP_REORDER, this::receive)
}

suspend fun send(packet: AppReorderOutgoingPacket) {
protocolHandler.send(packet)
}

suspend fun receive(packet: PebblePacket) {
if (packet !is AppReorderResult) {
throw IllegalStateException("Received invalid packet type: $packet")
}

if (packet.status.get() == AppOrderResultCode.RETRY.value) {
lastOrderPacket?.let { send(it) }
return
}

lastOrderPacket = null
receivedMessages.offer(packet)
}
}

0 comments on commit 5442634

Please sign in to comment.