mirror of
https://github.com/deavmi/doap
synced 2024-07-04 16:10:15 +02:00
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:
parent
025758aa66
commit
36254da902
|
@ -142,14 +142,26 @@ public class CoapPacket
|
||||||
return this.tokenLen;
|
return this.tokenLen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Code getCode()
|
||||||
|
{
|
||||||
|
return this.code;
|
||||||
|
}
|
||||||
|
|
||||||
public static CoapPacket fromBytes(ubyte[] data)
|
public static CoapPacket fromBytes(ubyte[] data)
|
||||||
{
|
{
|
||||||
CoapPacket packet = new CoapPacket();
|
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.ver = data[0]>>6;
|
||||||
packet.type = cast(MessageType)( (data[0]>>4) & 3);
|
packet.type = cast(MessageType)( (data[0]>>4) & 3);
|
||||||
packet.tokenLen = data[0]&15;
|
packet.tokenLen = data[0]&15;
|
||||||
|
|
||||||
|
packet.code = cast(Code)(data[1]);
|
||||||
|
|
||||||
|
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
@ -253,11 +265,13 @@ unittest
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
// Version: 1 | Type: RESET (3) : TLK: 0
|
// Version: 1 | Type: RESET (3) : TLK: 0
|
||||||
ubyte[] packetData = [112];
|
// Code: 2 (POST) | ...
|
||||||
|
ubyte[] packetData = [112, 2, 0, 0];
|
||||||
|
|
||||||
CoapPacket packet = CoapPacket.fromBytes(packetData);
|
CoapPacket packet = CoapPacket.fromBytes(packetData);
|
||||||
|
|
||||||
assert(packet.getVersion() == 1);
|
assert(packet.getVersion() == 1);
|
||||||
assert(packet.getType() == MessageType.RESET);
|
assert(packet.getType() == MessageType.RESET);
|
||||||
assert(packet.getTokenLength() == 0);
|
assert(packet.getTokenLength() == 0);
|
||||||
|
assert(packet.getCode() == Code.POST);
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user