Skip to content

Commit 55cb810

Browse files
committed
use enum for endianness, app blobdb data little endian
1 parent fc1285e commit 55cb810

File tree

20 files changed

+114
-81
lines changed

20 files changed

+114
-81
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
kotlin.code.style=official
22

33
group=io.rebble.libpebblecommon
4-
version=0.1.23
4+
version=0.1.24
55
org.gradle.jvmargs=-Xms128M -Xmx1G -XX:ReservedCodeCacheSize=200M
66
kotlin.native.binary.memoryModel=experimental
77
kotlin.mpp.androidSourceSetLayoutVersion=2

src/androidMain/kotlin/util/DataBuffer.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ actual class DataBuffer {
6262

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

65-
actual fun setEndian(endian: Char) {
65+
actual fun setEndian(endian: Endian) {
6666
when (endian) {
67-
'>' -> actualBuf.order(ByteOrder.BIG_ENDIAN)
68-
'<' -> actualBuf.order(ByteOrder.LITTLE_ENDIAN)
67+
Endian.Big -> actualBuf.order(ByteOrder.BIG_ENDIAN)
68+
Endian.Little -> actualBuf.order(ByteOrder.LITTLE_ENDIAN)
69+
else -> {}
6970
}
7071
}
7172

src/commonMain/kotlin/io/rebble/libpebblecommon/packets/AppCustomization.kt

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import io.rebble.libpebblecommon.structmapper.SUByte
88
import io.rebble.libpebblecommon.structmapper.SUShort
99
import io.rebble.libpebblecommon.util.Bitmap
1010
import io.rebble.libpebblecommon.util.DataBuffer
11+
import io.rebble.libpebblecommon.util.Endian
1112

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

2829
/**
2930
* No idea what flags are possible. Stock app always sends 4096 here.
3031
*/
31-
val flags = SUShort(m, 4096u, endianness = '<')
32+
val flags = SUShort(m, 4096u, endianness = Endian.Little)
3233

3334
/**
3435
* Offset is not supported by app. Always 0.
3536
*/
36-
val originY = SUShort(m, 0u, endianness = '<')
37-
val originX = SUShort(m, 0u, endianness = '<')
37+
val originY = SUShort(m, 0u, endianness = Endian.Little)
38+
val originX = SUShort(m, 0u, endianness = Endian.Little)
3839

39-
val width = SUShort(m, endianness = '<')
40-
val height = SUShort(m, endianness = '<')
40+
val width = SUShort(m, endianness = Endian.Little)
41+
val height = SUShort(m, endianness = Endian.Little)
4142

4243
val imageData = SBytes(m)
4344

src/commonMain/kotlin/io/rebble/libpebblecommon/packets/AppFetch.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
66
import io.rebble.libpebblecommon.structmapper.SUByte
77
import io.rebble.libpebblecommon.structmapper.SUInt
88
import io.rebble.libpebblecommon.structmapper.SUUID
9+
import io.rebble.libpebblecommon.util.Endian
910

1011
sealed class AppFetchIncomingPacket() : PebblePacket(ProtocolEndpoint.APP_FETCH) {
1112
/**
@@ -38,7 +39,7 @@ class AppFetchRequest : AppFetchIncomingPacket() {
3839
/**
3940
* ID of the app bank. Use in the [PutBytesAppInit] packet to identify this app install.
4041
*/
41-
val appId = SUInt(m, endianness = '<')
42+
val appId = SUInt(m, endianness = Endian.Little)
4243
}
4344

4445
/**

src/commonMain/kotlin/io/rebble/libpebblecommon/packets/AppMessage.kt

+11-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.rebble.libpebblecommon.protocolhelpers.PebblePacket
66
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
77
import io.rebble.libpebblecommon.structmapper.*
88
import io.rebble.libpebblecommon.util.DataBuffer
9+
import io.rebble.libpebblecommon.util.Endian
910

1011

1112
class AppMessageTuple() : StructMappable() {
@@ -22,9 +23,9 @@ class AppMessageTuple() : StructMappable() {
2223
}
2324
}
2425

25-
val key = SUInt(m, endianness = '<')
26+
val key = SUInt(m, endianness = Endian.Little)
2627
val type = SUByte(m)
27-
val dataLength = SUShort(m, endianness = '<')
28+
val dataLength = SUShort(m, endianness = Endian.Little)
2829
val data = SBytes(m, 0)
2930

3031
init {
@@ -46,8 +47,8 @@ class AppMessageTuple() : StructMappable() {
4647
get() {
4748
val obj = when (val size = dataLength.get().toInt()) {
4849
1 -> SByte(StructMapper())
49-
2 -> SShort(StructMapper(), endianness = '<')
50-
4 -> SInt(StructMapper(), endianness = '<')
50+
2 -> SShort(StructMapper(), endianness = Endian.Little)
51+
4 -> SInt(StructMapper(), endianness = Endian.Little)
5152
else -> error("Size not supported: $size")
5253
}
5354
return obj.apply {
@@ -59,8 +60,8 @@ class AppMessageTuple() : StructMappable() {
5960
get() {
6061
val obj = when (val size = dataLength.get().toInt()) {
6162
1 -> SUByte(StructMapper())
62-
2 -> SUShort(StructMapper(), endianness = '<')
63-
4 -> SUInt(StructMapper(), endianness = '<')
63+
2 -> SUShort(StructMapper(), endianness = Endian.Little)
64+
4 -> SUInt(StructMapper(), endianness = Endian.Little)
6465
else -> error("Size not supported: $size")
6566
}
6667
return obj.apply {
@@ -146,7 +147,7 @@ class AppMessageTuple() : StructMappable() {
146147
this.key.set(key)
147148
this.type.set(Type.Int.value)
148149

149-
val bytes = SShort(StructMapper(), data, endianness = '<').toBytes()
150+
val bytes = SShort(StructMapper(), data, endianness = Endian.Little).toBytes()
150151
this.dataLength.set(bytes.size.toUShort())
151152
this.data.set(bytes)
152153
}
@@ -158,7 +159,7 @@ class AppMessageTuple() : StructMappable() {
158159
this.key.set(key)
159160
this.type.set(Type.UInt.value)
160161

161-
val bytes = SUShort(StructMapper(), data, endianness = '<').toBytes()
162+
val bytes = SUShort(StructMapper(), data, endianness = Endian.Little).toBytes()
162163
this.dataLength.set(bytes.size.toUShort())
163164
this.data.set(bytes)
164165
}
@@ -170,7 +171,7 @@ class AppMessageTuple() : StructMappable() {
170171
this.key.set(key)
171172
this.type.set(Type.Int.value)
172173

173-
val bytes = SInt(StructMapper(), data, endianness = '<').toBytes()
174+
val bytes = SInt(StructMapper(), data, endianness = Endian.Little).toBytes()
174175
this.dataLength.set(bytes.size.toUShort())
175176
this.data.set(bytes)
176177
}
@@ -182,7 +183,7 @@ class AppMessageTuple() : StructMappable() {
182183
this.key.set(key)
183184
this.type.set(Type.UInt.value)
184185

185-
val bytes = SUInt(StructMapper(), data, endianness = '<').toBytes()
186+
val bytes = SUInt(StructMapper(), data, endianness = Endian.Little).toBytes()
186187
this.dataLength.set(bytes.size.toUShort())
187188
this.data.set(bytes)
188189
}

src/commonMain/kotlin/io/rebble/libpebblecommon/packets/Music.kt

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import io.rebble.libpebblecommon.protocolhelpers.PacketRegistry
44
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket
55
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
66
import io.rebble.libpebblecommon.structmapper.*
7+
import io.rebble.libpebblecommon.util.Endian
78

89
open class MusicControl(val message: Message) : PebblePacket(ProtocolEndpoint.MUSIC_CONTROL) {
910
val command = SUByte(m, message.value)
@@ -43,17 +44,17 @@ open class MusicControl(val message: Message) : PebblePacket(ProtocolEndpoint.MU
4344
val title = SString(m, title)
4445
val trackLength = SOptional(
4546
m,
46-
SUInt(StructMapper(), trackLength?.toUInt() ?: 0u, '<'),
47+
SUInt(StructMapper(), trackLength?.toUInt() ?: 0u, Endian.Little),
4748
trackLength != null
4849
)
4950
val trackCount = SOptional(
5051
m,
51-
SUInt(StructMapper(), trackCount?.toUInt() ?: 0u, '<'),
52+
SUInt(StructMapper(), trackCount?.toUInt() ?: 0u, Endian.Little),
5253
trackCount != null
5354
)
5455
val currentTrack = SOptional(
5556
m,
56-
SUInt(StructMapper(), currentTrack?.toUInt() ?: 0u, '<'),
57+
SUInt(StructMapper(), currentTrack?.toUInt() ?: 0u, Endian.Little),
5758
currentTrack != null
5859
)
5960
}
@@ -72,8 +73,8 @@ open class MusicControl(val message: Message) : PebblePacket(ProtocolEndpoint.MU
7273
repeat: RepeatState = RepeatState.Unknown
7374
) : MusicControl(Message.UpdatePlayStateInfo) {
7475
val state = SUByte(m, playbackState.value)
75-
val trackPosition = SUInt(m, trackPosition, '<')
76-
val playRate = SUInt(m, playRate, '<')
76+
val trackPosition = SUInt(m, trackPosition, Endian.Little)
77+
val playRate = SUInt(m, playRate, Endian.Little)
7778
val shuffle = SUByte(m, shuffle.value)
7879
val repeat = SUByte(m, repeat.value)
7980
}

src/commonMain/kotlin/io/rebble/libpebblecommon/packets/System.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.rebble.libpebblecommon.protocolhelpers.PacketRegistry
99
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket
1010
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
1111
import io.rebble.libpebblecommon.structmapper.*
12+
import io.rebble.libpebblecommon.util.Endian
1213

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

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

411412
class NewFirmwareAvailable: SystemMessage(Message.NewFirmwareAvailable)
412413
class FirmwareUpdateStart(bytesAlreadyTransferred: UInt, bytesToSend: UInt): SystemMessage(Message.FirmwareUpdateStart) {
413-
val bytesAlreadyTransferred = SUInt(m, bytesAlreadyTransferred, endianness = '<')
414-
val bytesToSend = SUInt(m, bytesToSend, endianness = '<')
414+
val bytesAlreadyTransferred = SUInt(m, bytesAlreadyTransferred, endianness = Endian.Little)
415+
val bytesToSend = SUInt(m, bytesToSend, endianness = Endian.Little)
415416
}
416417
class FirmwareUpdateComplete: SystemMessage(Message.FirmwareUpdateComplete)
417418
class FirmwareUpdateFailed: SystemMessage(Message.FirmwareUpdateFailed)

src/commonMain/kotlin/io/rebble/libpebblecommon/packets/blobdb/App.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.rebble.libpebblecommon.packets.blobdb
22

33
import io.rebble.libpebblecommon.structmapper.*
4+
import io.rebble.libpebblecommon.util.Endian
45

56
/**
67
* Data of the APP BlobDB Entry
@@ -14,12 +15,12 @@ class AppMetadata() : StructMappable() {
1415
/**
1516
* App install flags.
1617
*/
17-
val flags: SUInt = SUInt(m)
18+
val flags: SUInt = SUInt(m, endianness = Endian.Little)
1819

1920
/**
2021
* Resource ID of the primary icon.
2122
*/
22-
val icon: SUInt = SUInt(m)
23+
val icon: SUInt = SUInt(m, endianness = Endian.Little)
2324

2425
/**
2526
* Major app version.

src/commonMain/kotlin/io/rebble/libpebblecommon/packets/blobdb/BlobDB.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
66
import io.rebble.libpebblecommon.structmapper.SBytes
77
import io.rebble.libpebblecommon.structmapper.SUByte
88
import io.rebble.libpebblecommon.structmapper.SUShort
9+
import io.rebble.libpebblecommon.util.Endian
910

1011
open class BlobCommand constructor(message: Message, token: UShort, database: BlobDatabase) :
1112
PebblePacket(
@@ -46,7 +47,7 @@ open class BlobCommand constructor(message: Message, token: UShort, database: Bl
4647
) {
4748
val keySize = SUByte(m, key.size.toUByte())
4849
val targetKey = SBytes(m, key.size, key)
49-
val valSize = SUShort(m, value.size.toUShort(), endianness = '<')
50+
val valSize = SUShort(m, value.size.toUShort(), endianness = Endian.Little)
5051
val targetValue = SBytes(m, value.size, value)
5152
}
5253

src/commonMain/kotlin/io/rebble/libpebblecommon/packets/blobdb/Timeline.kt

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.rebble.libpebblecommon.protocolhelpers.PebblePacket
77
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
88
import io.rebble.libpebblecommon.structmapper.*
99
import io.rebble.libpebblecommon.util.DataBuffer
10+
import io.rebble.libpebblecommon.util.Endian
1011

1112
class TimelineItem(
1213
itemId: Uuid,
@@ -52,12 +53,12 @@ class TimelineItem(
5253
/**
5354
* Timeline pin timestamp in unix time
5455
*/
55-
val timestamp = SUInt(m, timestamp, endianness = '<')
56+
val timestamp = SUInt(m, timestamp, endianness = Endian.Little)
5657

5758
/**
5859
* Duration of the pin in minutes
5960
*/
60-
val duration = SUShort(m, duration, endianness = '<')
61+
val duration = SUShort(m, duration, endianness = Endian.Little)
6162

6263
/**
6364
* Serialization of [Type]. Use [Type.value].
@@ -67,10 +68,10 @@ class TimelineItem(
6768
/**
6869
* Serialization of [Flag] entries. Use [Flag.makeFlags].
6970
*/
70-
val flags = SUShort(m, flags, endianness = '<')
71+
val flags = SUShort(m, flags, endianness = Endian.Little)
7172

7273
val layout = SUByte(m, layout.value)
73-
val dataLength = SUShort(m, endianness = '<')
74+
val dataLength = SUShort(m, endianness = Endian.Little)
7475
val attrCount = SUByte(m, attributes.size.toUByte())
7576
val actionCount = SUByte(m, actions.size.toUByte())
7677
val attributes =
@@ -87,7 +88,7 @@ class TimelineItem(
8788
dataLength.set((this.attributes.toBytes().size + this.actions.toBytes().size).toUShort())
8889
}
8990

90-
class Action(actionID: UByte, type: Type, attributes: List<Attribute>) : Mappable {
91+
class Action(actionID: UByte, type: Type, attributes: List<Attribute>) : Mappable() {
9192
val m = StructMapper()
9293

9394
enum class Type(val value: UByte) {
@@ -121,22 +122,21 @@ class TimelineItem(
121122
get() = m.size
122123
}
123124

124-
class Attribute() : StructMappable() {
125+
class Attribute(contentEndianness: Endian = Endian.Unspecified) : StructMappable() {
125126
val attributeId = SUByte(m)
126-
val length = SUShort(m, endianness = '<')
127-
val content = SBytes(m, 0)
127+
val length = SUShort(m, endianness = Endian.Little)
128+
val content = SBytes(m, 0, endianness = contentEndianness)
128129

129130
constructor(
130131
attributeId: UByte,
131132
content: UByteArray,
132-
contentEndianness: Char = '|'
133-
) : this() {
133+
contentEndianness: Endian = Endian.Unspecified
134+
) : this(contentEndianness) {
134135
this.attributeId.set(attributeId)
135136

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

138139
this.content.set(content)
139-
this.content.setEndiannes(contentEndianness)
140140
}
141141

142142
init {

src/commonMain/kotlin/io/rebble/libpebblecommon/structmapper/StructMappable.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package io.rebble.libpebblecommon.structmapper
22

33
import io.rebble.libpebblecommon.util.DataBuffer
4+
import io.rebble.libpebblecommon.util.Endian
45

5-
abstract class StructMappable : Mappable {
6-
val m = StructMapper()
6+
abstract class StructMappable(endianness: Endian = Endian.Unspecified) : Mappable(endianness) {
7+
val m = StructMapper(endianness = endianness)
78

89
override fun toBytes(): UByteArray {
910
return m.toBytes()

src/commonMain/kotlin/io/rebble/libpebblecommon/structmapper/StructMapper.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package io.rebble.libpebblecommon.structmapper
22

33
import io.rebble.libpebblecommon.exceptions.PacketDecodeException
44
import io.rebble.libpebblecommon.util.DataBuffer
5+
import io.rebble.libpebblecommon.util.Endian
56

67
/**
78
* Maps class properties to a struct equivalent
89
*/
9-
class StructMapper: Mappable {
10+
class StructMapper(endianness: Endian = Endian.Unspecified): Mappable(endianness) {
1011
private var struct: MutableList<Mappable> = mutableListOf()
1112

1213
/**

0 commit comments

Comments
 (0)