Skip to content

Commit

Permalink
use enum for endianness, app blobdb data little endian
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Sep 20, 2024
1 parent fc1285e commit 55cb810
Show file tree
Hide file tree
Showing 20 changed files with 114 additions and 81 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
kotlin.code.style=official

group=io.rebble.libpebblecommon
version=0.1.23
version=0.1.24
org.gradle.jvmargs=-Xms128M -Xmx1G -XX:ReservedCodeCacheSize=200M
kotlin.native.binary.memoryModel=experimental
kotlin.mpp.androidSourceSetLayoutVersion=2
Expand Down
7 changes: 4 additions & 3 deletions src/androidMain/kotlin/util/DataBuffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ actual class DataBuffer {

actual fun array(): UByteArray = actualBuf.array().toUByteArray()

actual fun setEndian(endian: Char) {
actual fun setEndian(endian: Endian) {
when (endian) {
'>' -> actualBuf.order(ByteOrder.BIG_ENDIAN)
'<' -> actualBuf.order(ByteOrder.LITTLE_ENDIAN)
Endian.Big -> actualBuf.order(ByteOrder.BIG_ENDIAN)
Endian.Little -> actualBuf.order(ByteOrder.LITTLE_ENDIAN)
else -> {}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.rebble.libpebblecommon.structmapper.SUByte
import io.rebble.libpebblecommon.structmapper.SUShort
import io.rebble.libpebblecommon.util.Bitmap
import io.rebble.libpebblecommon.util.DataBuffer
import io.rebble.libpebblecommon.util.Endian

class AppCustomizationSetStockAppTitleMessage(
appType: AppType,
Expand All @@ -23,21 +24,21 @@ class AppCustomizationSetStockAppIconMessage(
) : PebblePacket(ProtocolEndpoint.APP_CUSTOMIZE) {
// First bit being set signifies that this is icon packet instead of name packet
val appType = SUByte(m, appType.value or 0b10000000u)
val bytesPerLine = SUShort(m, endianness = '<')
val bytesPerLine = SUShort(m, endianness = Endian.Little)

/**
* No idea what flags are possible. Stock app always sends 4096 here.
*/
val flags = SUShort(m, 4096u, endianness = '<')
val flags = SUShort(m, 4096u, endianness = Endian.Little)

/**
* Offset is not supported by app. Always 0.
*/
val originY = SUShort(m, 0u, endianness = '<')
val originX = SUShort(m, 0u, endianness = '<')
val originY = SUShort(m, 0u, endianness = Endian.Little)
val originX = SUShort(m, 0u, endianness = Endian.Little)

val width = SUShort(m, endianness = '<')
val height = SUShort(m, endianness = '<')
val width = SUShort(m, endianness = Endian.Little)
val height = SUShort(m, endianness = Endian.Little)

val imageData = SBytes(m)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
import io.rebble.libpebblecommon.structmapper.SUByte
import io.rebble.libpebblecommon.structmapper.SUInt
import io.rebble.libpebblecommon.structmapper.SUUID
import io.rebble.libpebblecommon.util.Endian

sealed class AppFetchIncomingPacket() : PebblePacket(ProtocolEndpoint.APP_FETCH) {
/**
Expand Down Expand Up @@ -38,7 +39,7 @@ class AppFetchRequest : AppFetchIncomingPacket() {
/**
* ID of the app bank. Use in the [PutBytesAppInit] packet to identify this app install.
*/
val appId = SUInt(m, endianness = '<')
val appId = SUInt(m, endianness = Endian.Little)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.rebble.libpebblecommon.protocolhelpers.PebblePacket
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
import io.rebble.libpebblecommon.structmapper.*
import io.rebble.libpebblecommon.util.DataBuffer
import io.rebble.libpebblecommon.util.Endian


class AppMessageTuple() : StructMappable() {
Expand All @@ -22,9 +23,9 @@ class AppMessageTuple() : StructMappable() {
}
}

val key = SUInt(m, endianness = '<')
val key = SUInt(m, endianness = Endian.Little)
val type = SUByte(m)
val dataLength = SUShort(m, endianness = '<')
val dataLength = SUShort(m, endianness = Endian.Little)
val data = SBytes(m, 0)

init {
Expand All @@ -46,8 +47,8 @@ class AppMessageTuple() : StructMappable() {
get() {
val obj = when (val size = dataLength.get().toInt()) {
1 -> SByte(StructMapper())
2 -> SShort(StructMapper(), endianness = '<')
4 -> SInt(StructMapper(), endianness = '<')
2 -> SShort(StructMapper(), endianness = Endian.Little)
4 -> SInt(StructMapper(), endianness = Endian.Little)
else -> error("Size not supported: $size")
}
return obj.apply {
Expand All @@ -59,8 +60,8 @@ class AppMessageTuple() : StructMappable() {
get() {
val obj = when (val size = dataLength.get().toInt()) {
1 -> SUByte(StructMapper())
2 -> SUShort(StructMapper(), endianness = '<')
4 -> SUInt(StructMapper(), endianness = '<')
2 -> SUShort(StructMapper(), endianness = Endian.Little)
4 -> SUInt(StructMapper(), endianness = Endian.Little)
else -> error("Size not supported: $size")
}
return obj.apply {
Expand Down Expand Up @@ -146,7 +147,7 @@ class AppMessageTuple() : StructMappable() {
this.key.set(key)
this.type.set(Type.Int.value)

val bytes = SShort(StructMapper(), data, endianness = '<').toBytes()
val bytes = SShort(StructMapper(), data, endianness = Endian.Little).toBytes()
this.dataLength.set(bytes.size.toUShort())
this.data.set(bytes)
}
Expand All @@ -158,7 +159,7 @@ class AppMessageTuple() : StructMappable() {
this.key.set(key)
this.type.set(Type.UInt.value)

val bytes = SUShort(StructMapper(), data, endianness = '<').toBytes()
val bytes = SUShort(StructMapper(), data, endianness = Endian.Little).toBytes()
this.dataLength.set(bytes.size.toUShort())
this.data.set(bytes)
}
Expand All @@ -170,7 +171,7 @@ class AppMessageTuple() : StructMappable() {
this.key.set(key)
this.type.set(Type.Int.value)

val bytes = SInt(StructMapper(), data, endianness = '<').toBytes()
val bytes = SInt(StructMapper(), data, endianness = Endian.Little).toBytes()
this.dataLength.set(bytes.size.toUShort())
this.data.set(bytes)
}
Expand All @@ -182,7 +183,7 @@ class AppMessageTuple() : StructMappable() {
this.key.set(key)
this.type.set(Type.UInt.value)

val bytes = SUInt(StructMapper(), data, endianness = '<').toBytes()
val bytes = SUInt(StructMapper(), data, endianness = Endian.Little).toBytes()
this.dataLength.set(bytes.size.toUShort())
this.data.set(bytes)
}
Expand Down
11 changes: 6 additions & 5 deletions src/commonMain/kotlin/io/rebble/libpebblecommon/packets/Music.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.rebble.libpebblecommon.protocolhelpers.PacketRegistry
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
import io.rebble.libpebblecommon.structmapper.*
import io.rebble.libpebblecommon.util.Endian

open class MusicControl(val message: Message) : PebblePacket(ProtocolEndpoint.MUSIC_CONTROL) {
val command = SUByte(m, message.value)
Expand Down Expand Up @@ -43,17 +44,17 @@ open class MusicControl(val message: Message) : PebblePacket(ProtocolEndpoint.MU
val title = SString(m, title)
val trackLength = SOptional(
m,
SUInt(StructMapper(), trackLength?.toUInt() ?: 0u, '<'),
SUInt(StructMapper(), trackLength?.toUInt() ?: 0u, Endian.Little),
trackLength != null
)
val trackCount = SOptional(
m,
SUInt(StructMapper(), trackCount?.toUInt() ?: 0u, '<'),
SUInt(StructMapper(), trackCount?.toUInt() ?: 0u, Endian.Little),
trackCount != null
)
val currentTrack = SOptional(
m,
SUInt(StructMapper(), currentTrack?.toUInt() ?: 0u, '<'),
SUInt(StructMapper(), currentTrack?.toUInt() ?: 0u, Endian.Little),
currentTrack != null
)
}
Expand All @@ -72,8 +73,8 @@ open class MusicControl(val message: Message) : PebblePacket(ProtocolEndpoint.MU
repeat: RepeatState = RepeatState.Unknown
) : MusicControl(Message.UpdatePlayStateInfo) {
val state = SUByte(m, playbackState.value)
val trackPosition = SUInt(m, trackPosition, '<')
val playRate = SUInt(m, playRate, '<')
val trackPosition = SUInt(m, trackPosition, Endian.Little)
val playRate = SUInt(m, playRate, Endian.Little)
val shuffle = SUByte(m, shuffle.value)
val repeat = SUByte(m, repeat.value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.rebble.libpebblecommon.protocolhelpers.PacketRegistry
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
import io.rebble.libpebblecommon.structmapper.*
import io.rebble.libpebblecommon.util.Endian

sealed class SystemPacket(endpoint: ProtocolEndpoint) : PebblePacket(endpoint)

Expand Down Expand Up @@ -410,8 +411,8 @@ open class SystemMessage(message: Message) : SystemPacket(endpoint) {

class NewFirmwareAvailable: SystemMessage(Message.NewFirmwareAvailable)
class FirmwareUpdateStart(bytesAlreadyTransferred: UInt, bytesToSend: UInt): SystemMessage(Message.FirmwareUpdateStart) {
val bytesAlreadyTransferred = SUInt(m, bytesAlreadyTransferred, endianness = '<')
val bytesToSend = SUInt(m, bytesToSend, endianness = '<')
val bytesAlreadyTransferred = SUInt(m, bytesAlreadyTransferred, endianness = Endian.Little)
val bytesToSend = SUInt(m, bytesToSend, endianness = Endian.Little)
}
class FirmwareUpdateComplete: SystemMessage(Message.FirmwareUpdateComplete)
class FirmwareUpdateFailed: SystemMessage(Message.FirmwareUpdateFailed)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.rebble.libpebblecommon.packets.blobdb

import io.rebble.libpebblecommon.structmapper.*
import io.rebble.libpebblecommon.util.Endian

/**
* Data of the APP BlobDB Entry
Expand All @@ -14,12 +15,12 @@ class AppMetadata() : StructMappable() {
/**
* App install flags.
*/
val flags: SUInt = SUInt(m)
val flags: SUInt = SUInt(m, endianness = Endian.Little)

/**
* Resource ID of the primary icon.
*/
val icon: SUInt = SUInt(m)
val icon: SUInt = SUInt(m, endianness = Endian.Little)

/**
* Major app version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
import io.rebble.libpebblecommon.structmapper.SBytes
import io.rebble.libpebblecommon.structmapper.SUByte
import io.rebble.libpebblecommon.structmapper.SUShort
import io.rebble.libpebblecommon.util.Endian

open class BlobCommand constructor(message: Message, token: UShort, database: BlobDatabase) :
PebblePacket(
Expand Down Expand Up @@ -46,7 +47,7 @@ open class BlobCommand constructor(message: Message, token: UShort, database: Bl
) {
val keySize = SUByte(m, key.size.toUByte())
val targetKey = SBytes(m, key.size, key)
val valSize = SUShort(m, value.size.toUShort(), endianness = '<')
val valSize = SUShort(m, value.size.toUShort(), endianness = Endian.Little)
val targetValue = SBytes(m, value.size, value)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.rebble.libpebblecommon.protocolhelpers.PebblePacket
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
import io.rebble.libpebblecommon.structmapper.*
import io.rebble.libpebblecommon.util.DataBuffer
import io.rebble.libpebblecommon.util.Endian

class TimelineItem(
itemId: Uuid,
Expand Down Expand Up @@ -52,12 +53,12 @@ class TimelineItem(
/**
* Timeline pin timestamp in unix time
*/
val timestamp = SUInt(m, timestamp, endianness = '<')
val timestamp = SUInt(m, timestamp, endianness = Endian.Little)

/**
* Duration of the pin in minutes
*/
val duration = SUShort(m, duration, endianness = '<')
val duration = SUShort(m, duration, endianness = Endian.Little)

/**
* Serialization of [Type]. Use [Type.value].
Expand All @@ -67,10 +68,10 @@ class TimelineItem(
/**
* Serialization of [Flag] entries. Use [Flag.makeFlags].
*/
val flags = SUShort(m, flags, endianness = '<')
val flags = SUShort(m, flags, endianness = Endian.Little)

val layout = SUByte(m, layout.value)
val dataLength = SUShort(m, endianness = '<')
val dataLength = SUShort(m, endianness = Endian.Little)
val attrCount = SUByte(m, attributes.size.toUByte())
val actionCount = SUByte(m, actions.size.toUByte())
val attributes =
Expand All @@ -87,7 +88,7 @@ class TimelineItem(
dataLength.set((this.attributes.toBytes().size + this.actions.toBytes().size).toUShort())
}

class Action(actionID: UByte, type: Type, attributes: List<Attribute>) : Mappable {
class Action(actionID: UByte, type: Type, attributes: List<Attribute>) : Mappable() {
val m = StructMapper()

enum class Type(val value: UByte) {
Expand Down Expand Up @@ -121,22 +122,21 @@ class TimelineItem(
get() = m.size
}

class Attribute() : StructMappable() {
class Attribute(contentEndianness: Endian = Endian.Unspecified) : StructMappable() {
val attributeId = SUByte(m)
val length = SUShort(m, endianness = '<')
val content = SBytes(m, 0)
val length = SUShort(m, endianness = Endian.Little)
val content = SBytes(m, 0, endianness = contentEndianness)

constructor(
attributeId: UByte,
content: UByteArray,
contentEndianness: Char = '|'
) : this() {
contentEndianness: Endian = Endian.Unspecified
) : this(contentEndianness) {
this.attributeId.set(attributeId)

this.length.set(content.size.toUShort())

this.content.set(content)
this.content.setEndiannes(contentEndianness)
}

init {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.rebble.libpebblecommon.structmapper

import io.rebble.libpebblecommon.util.DataBuffer
import io.rebble.libpebblecommon.util.Endian

abstract class StructMappable : Mappable {
val m = StructMapper()
abstract class StructMappable(endianness: Endian = Endian.Unspecified) : Mappable(endianness) {
val m = StructMapper(endianness = endianness)

override fun toBytes(): UByteArray {
return m.toBytes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package io.rebble.libpebblecommon.structmapper

import io.rebble.libpebblecommon.exceptions.PacketDecodeException
import io.rebble.libpebblecommon.util.DataBuffer
import io.rebble.libpebblecommon.util.Endian

/**
* Maps class properties to a struct equivalent
*/
class StructMapper: Mappable {
class StructMapper(endianness: Endian = Endian.Unspecified): Mappable(endianness) {
private var struct: MutableList<Mappable> = mutableListOf()

/**
Expand Down
Loading

0 comments on commit 55cb810

Please sign in to comment.