mirror of
https://github.com/deavmi/doap
synced 2024-07-02 13:47:36 +02:00
CoapPacket
- Added `getType()` - Added `getTokenLength()` - Added `getVersion()` - Added `fromBytes(ubyte[])` which decodes the provided bytes and produces a `CoapPacket` at the end Packet (unit tests) - Added initial decode tests
This commit is contained in:
parent
09ac0f0f20
commit
69a0fa12c5
|
@ -127,10 +127,35 @@ public class CoapPacket
|
|||
this.payload = payload;
|
||||
}
|
||||
|
||||
// public ubyte getVersion()
|
||||
// {
|
||||
public ubyte getVersion()
|
||||
{
|
||||
return this.ver;
|
||||
}
|
||||
|
||||
// }
|
||||
public MessageType getType()
|
||||
{
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public ubyte getTokenLength()
|
||||
{
|
||||
return this.tokenLen;
|
||||
}
|
||||
|
||||
import std.stdio;
|
||||
|
||||
public static CoapPacket fromBytes(ubyte[] data)
|
||||
{
|
||||
CoapPacket packet = new CoapPacket();
|
||||
|
||||
packet.ver = data[0]>>6;
|
||||
packet.type = cast(MessageType)( (data[0]>>4) & 3);
|
||||
writeln(packet.type);
|
||||
packet.tokenLen = data[0]&15;
|
||||
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -219,4 +244,23 @@ unittest
|
|||
assert(eighthByte == 255);
|
||||
assert(ninthByte == 254);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Decoding tests
|
||||
*
|
||||
* These tests take a byte array of an encoded
|
||||
* CoAP packet and then decodes it into a new
|
||||
* `CoapPacket` object
|
||||
*/
|
||||
unittest
|
||||
{
|
||||
// Version: 1 | Type: RESET (3) : TLK: 0
|
||||
ubyte[] packetData = [112];
|
||||
|
||||
CoapPacket packet = CoapPacket.fromBytes(packetData);
|
||||
|
||||
assert(packet.getVersion() == 1);
|
||||
assert(packet.getType() == MessageType.RESET);
|
||||
assert(packet.getTokenLength() == 0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user