- 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
This commit is contained in:
Tristan B. Velloza Kildaire 2023-09-12 10:48:01 +02:00
parent 8e8d1d2ba1
commit d3ffdd46db
2 changed files with 33 additions and 1 deletions

View File

@ -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);
}

12
source/doap/types.d Normal file
View File

@ -0,0 +1,12 @@
module doap.types;
public enum MessageType : ubyte
{
// Request
CONFIRMABLE = 0,
NON_CONFIRMABLE = 1,
// Response
ACKNOWLEDGEMENT = 2,
RESET = 3
}