- Defined `CoapPacket` to represent a CoAP packet
- Added the required fields
- Constructor sets the version to `1` (the only version available)
- `getBytes()` no encodes the first byte of the CoAP header (versio, type and token length)
This commit is contained in:
Tristan B. Velloza Kildaire 2023-09-11 22:18:19 +02:00
parent ec4f57af5d
commit d6a9f7d329

36
source/doap/packet.d Normal file
View File

@ -0,0 +1,36 @@
module doap.packet;
import doap.codes : Code;
/**
* Represents a CoAP packet
*/
public class CoapPacket
{
private ubyte ver, type;
private ubyte tokenLen;
private Code code;
private ushort mid;
private ubyte[] token;
private uint options;
private ubyte[] payload;
this()
{
// Set the version (Default is 1)
ver = 1;
}
public ubyte[] getBytes()
{
ubyte[] encoded;
// Calculate the first byte (ver | type | tkl)
ubyte firstByte = cast(ubyte)(ver << 6);
firstByte = firstByte | cast(ubyte)(type << 4);
firstByte = firstByte | tokenLen;
return encoded;
}
}