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
This commit is contained in:
Tristan B. Velloza Kildaire 2023-09-13 21:34:02 +02:00
parent 025758aa66
commit 36254da902

View File

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