From d3ffdd46db5c09e885febc478706ee2515ad986c Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Tue, 12 Sep 2023 10:48:01 +0200 Subject: [PATCH] Packet - Added `setType(MessageType)` which sets the messagr type - The `type` field is no longer a `ubyte` but rather a `MessageType` Packet (unit tests) - Test the encoding of the type field Types - Added new module - Added `MessageType` enum --- source/doap/packet.d | 22 +++++++++++++++++++++- source/doap/types.d | 12 ++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 source/doap/types.d diff --git a/source/doap/packet.d b/source/doap/packet.d index 71a2d17..1c08b50 100644 --- a/source/doap/packet.d +++ b/source/doap/packet.d @@ -1,5 +1,6 @@ module doap.packet; +import doap.types : MessageType; import doap.codes : Code; /** @@ -7,7 +8,8 @@ import doap.codes : Code; */ public class CoapPacket { - private ubyte ver, type; + private ubyte ver; + private MessageType type; private ubyte tokenLen; private Code code; private ushort mid; @@ -40,6 +42,11 @@ public class CoapPacket return encoded; } + public void setType(MessageType type) + { + this.type = type; + } + // public ubyte getVersion() // { @@ -47,6 +54,11 @@ public class CoapPacket } +version(unittest) +{ + import std.stdio; +} + /** * Encoding tests * @@ -58,6 +70,8 @@ public class CoapPacket unittest { CoapPacket packet = new CoapPacket(); + packet.setType(MessageType.RESET); + ubyte[] encoded = packet.getBytes(); ubyte firstByte = encoded[0]; @@ -66,5 +80,11 @@ unittest ubyte versionField = cast(ubyte)(firstByte & 192) >> 6; assert(versionField == 1); + // Ensure the type is 3/RESET + writeln(firstByte); + ubyte typeField = cast(ubyte)(firstByte & 48) >> 4; + writeln(typeField); + writeln(cast(ubyte)MessageType.RESET); + assert(typeField == MessageType.RESET); } \ No newline at end of file diff --git a/source/doap/types.d b/source/doap/types.d new file mode 100644 index 0000000..2882a30 --- /dev/null +++ b/source/doap/types.d @@ -0,0 +1,12 @@ +module doap.types; + +public enum MessageType : ubyte +{ + // Request + CONFIRMABLE = 0, + NON_CONFIRMABLE = 1, + + // Response + ACKNOWLEDGEMENT = 2, + RESET = 3 +} \ No newline at end of file