Skip to content

Commit

Permalink
Initial emulator packet support
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Feb 14, 2021
1 parent fff6b3a commit 97efc0c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin.code.style=official

group=io.rebble.libpebblecommon
version=0.0.16
version=0.0.17
org.gradle.jvmargs=-Xms1G
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.rebble.libpebblecommon.packets

import io.rebble.libpebblecommon.exceptions.PacketDecodeException
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket
import io.rebble.libpebblecommon.structmapper.SBytes
import io.rebble.libpebblecommon.structmapper.SUShort
import io.rebble.libpebblecommon.structmapper.StructMapper
import io.rebble.libpebblecommon.util.DataBuffer

const val HEADER_SIGNATURE = 0xFEEDU
const val FOOTER_SIGNATURE = 0xBEEFU

open class QemuInboundPacket {
val m = StructMapper()
val signature = SUShort(m, HEADER_SIGNATURE.toUShort())
val protocol = SUShort(m)
val length = SUShort(m)

class QemuSPP: QemuInboundPacket() {
val payload = SBytes(m)
val footer = SUShort(m, FOOTER_SIGNATURE.toUShort())

init {
payload.linkWithSize(length)
}
}

companion object {
fun deserialize(packet: UByteArray): QemuInboundPacket {
val buf = DataBuffer(packet)
val meta = StructMapper()
val header = SUShort(meta)
val protocol = SUShort(meta)
meta.fromBytes(buf)
return when (protocol.get()) {
1u.toUShort() -> QemuSPP().also { it.m.fromBytes(buf) }
else -> {
println("Warning: QEMU packet left generic")
QemuInboundPacket().also { it.m.fromBytes(buf) }
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,21 +267,21 @@ class SFixedString(mapper: StructMapper, size: Int, default: String = "") :

/**
* Represents arbitrary bytes in a struct
* @param length the number of bytes, when serializing this is used to pad/truncate the provided value to ensure it's 'length' bytes long
* @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)
*/
class SBytes(
mapper: StructMapper,
length: Int,
length: Int = -1,
default: UByteArray = ubyteArrayOf(),
endianness: Char = '|'
) :
StructElement<UByteArray>(
{ buf, el ->
if (el.size != 0) {
var mValue = el.get()
if (mValue.size > el.size) {
if (el.size != -1 && mValue.size > el.size) {
mValue = el.get().sliceArray(0 until length - 1) // Truncate if too long
} else if (mValue.size < length) {
} else if (el.size != -1 && mValue.size < length) {
mValue += UByteArray(length - el.size)// Pad if too short
}
buf.putBytes(if (el.isLittleEndian) mValue.reversedArray() else mValue)
Expand Down

0 comments on commit 97efc0c

Please sign in to comment.