diff --git a/src/commonMain/kotlin/Codec.kt b/src/commonMain/kotlin/Codec.kt index 22aa676..c072ef3 100644 --- a/src/commonMain/kotlin/Codec.kt +++ b/src/commonMain/kotlin/Codec.kt @@ -2,6 +2,7 @@ package cz.smarteon.loxone import com.ditchoom.buffer.ByteOrder import com.ditchoom.buffer.PlatformBuffer +import com.ditchoom.buffer.allocate import com.ditchoom.buffer.wrap import cz.smarteon.loxone.message.MessageHeader import cz.smarteon.loxone.message.MessageHeader.Companion.FIRST_BYTE @@ -56,4 +57,13 @@ object Codec { ) } } + + internal fun writeHeader(header: MessageHeader): ByteArray { + val buffer = PlatformBuffer.allocate(PAYLOAD_LENGTH, byteOrder = ByteOrder.LITTLE_ENDIAN) + buffer[0] = FIRST_BYTE + buffer[1] = header.kind.ordinal.toByte() + buffer[2] = (if (header.sizeEstimated) 1 else 0).toByte() + buffer[MSG_SIZE_POSITION] = header.messageSize.toUInt() + return buffer.readByteArray(PAYLOAD_LENGTH) + } } diff --git a/src/commonTest/kotlin/CodecTest.kt b/src/commonTest/kotlin/CodecTest.kt index cb2edbf..48dbf64 100644 --- a/src/commonTest/kotlin/CodecTest.kt +++ b/src/commonTest/kotlin/CodecTest.kt @@ -20,4 +20,9 @@ class CodecTest : StringSpec({ val header = Codec.readHeader(Codec.hexToBytes("03000000ab000000")) header shouldBe MessageHeader(MessageKind.TEXT, false, 171) } + + "should write header" { + val header = MessageHeader(MessageKind.TEXT, false, 171) + Codec.writeHeader(header) shouldBe Codec.hexToBytes("03000000ab000000") + } })