From 36254da902713b252f5498ea20de2149527c0d96 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Wed, 13 Sep 2023 21:34:02 +0200 Subject: [PATCH] CoapPacket - `fromBytes(ubyte[])` now extracts the request/response code - `fromBytes(ubyte[])` also throws an exception on message size underflow - Added `getCode()` Packet (unit tests) - Added request/response code tests - Padded message (for now) to make tests pass --- source/doap/protocol/packet.d | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/source/doap/protocol/packet.d b/source/doap/protocol/packet.d index bb543d4..1917841 100644 --- a/source/doap/protocol/packet.d +++ b/source/doap/protocol/packet.d @@ -142,14 +142,26 @@ public class CoapPacket return this.tokenLen; } + public Code getCode() + { + return this.code; + } + public static CoapPacket fromBytes(ubyte[] data) { CoapPacket packet = new CoapPacket(); + if(data.length < 4) + { + throw new CoapException("CoAP message must be at least 4 bytes in size"); + } + packet.ver = data[0]>>6; packet.type = cast(MessageType)( (data[0]>>4) & 3); packet.tokenLen = data[0]&15; + packet.code = cast(Code)(data[1]); + return packet; } @@ -253,11 +265,13 @@ unittest unittest { // Version: 1 | Type: RESET (3) : TLK: 0 - ubyte[] packetData = [112]; + // Code: 2 (POST) | ... + ubyte[] packetData = [112, 2, 0, 0]; CoapPacket packet = CoapPacket.fromBytes(packetData); assert(packet.getVersion() == 1); assert(packet.getType() == MessageType.RESET); assert(packet.getTokenLength() == 0); + assert(packet.getCode() == Code.POST); } \ No newline at end of file