Skip to content

Commit 3083778

Browse files
committed
Merge branch 'emulator'
2 parents 9a8ce24 + 97efc0c commit 3083778

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
kotlin.code.style=official
22

33
group=io.rebble.libpebblecommon
4-
version=0.0.16
4+
version=0.0.17
55
org.gradle.jvmargs=-Xms1G
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.rebble.libpebblecommon.packets
2+
3+
import io.rebble.libpebblecommon.exceptions.PacketDecodeException
4+
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket
5+
import io.rebble.libpebblecommon.structmapper.SBytes
6+
import io.rebble.libpebblecommon.structmapper.SUShort
7+
import io.rebble.libpebblecommon.structmapper.StructMapper
8+
import io.rebble.libpebblecommon.util.DataBuffer
9+
10+
const val HEADER_SIGNATURE = 0xFEEDU
11+
const val FOOTER_SIGNATURE = 0xBEEFU
12+
13+
open class QemuInboundPacket {
14+
val m = StructMapper()
15+
val signature = SUShort(m, HEADER_SIGNATURE.toUShort())
16+
val protocol = SUShort(m)
17+
val length = SUShort(m)
18+
19+
class QemuSPP: QemuInboundPacket() {
20+
val payload = SBytes(m)
21+
val footer = SUShort(m, FOOTER_SIGNATURE.toUShort())
22+
23+
init {
24+
payload.linkWithSize(length)
25+
}
26+
}
27+
28+
companion object {
29+
fun deserialize(packet: UByteArray): QemuInboundPacket {
30+
val buf = DataBuffer(packet)
31+
val meta = StructMapper()
32+
val header = SUShort(meta)
33+
val protocol = SUShort(meta)
34+
meta.fromBytes(buf)
35+
return when (protocol.get()) {
36+
1u.toUShort() -> QemuSPP().also { it.m.fromBytes(buf) }
37+
else -> {
38+
println("Warning: QEMU packet left generic")
39+
QemuInboundPacket().also { it.m.fromBytes(buf) }
40+
}
41+
}
42+
}
43+
}
44+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,21 +267,21 @@ class SFixedString(mapper: StructMapper, size: Int, default: String = "") :
267267

268268
/**
269269
* Represents arbitrary bytes in a struct
270-
* @param length the number of bytes, when serializing this is used to pad/truncate the provided value to ensure it's 'length' bytes long
270+
* @param length the number of bytes, when serializing this is used to pad/truncate the provided value to ensure it's 'length' bytes long (-1 to disable this)
271271
*/
272272
class SBytes(
273273
mapper: StructMapper,
274-
length: Int,
274+
length: Int = -1,
275275
default: UByteArray = ubyteArrayOf(),
276276
endianness: Char = '|'
277277
) :
278278
StructElement<UByteArray>(
279279
{ buf, el ->
280280
if (el.size != 0) {
281281
var mValue = el.get()
282-
if (mValue.size > el.size) {
282+
if (el.size != -1 && mValue.size > el.size) {
283283
mValue = el.get().sliceArray(0 until length - 1) // Truncate if too long
284-
} else if (mValue.size < length) {
284+
} else if (el.size != -1 && mValue.size < length) {
285285
mValue += UByteArray(length - el.size)// Pad if too short
286286
}
287287
buf.putBytes(if (el.isLittleEndian) mValue.reversedArray() else mValue)

0 commit comments

Comments
 (0)