From 2f68cc45ebacf6d228b76e1de78e429de443a308 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Fri, 29 Sep 2023 10:02:50 +0200 Subject: [PATCH] CoapPacket - Implemented `determineLenType(size_t)` --- source/doap/protocol/packet.d | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/source/doap/protocol/packet.d b/source/doap/protocol/packet.d index 05a653e..e168a6b 100644 --- a/source/doap/protocol/packet.d +++ b/source/doap/protocol/packet.d @@ -116,6 +116,39 @@ public class CoapPacket return this.options; } + /** + * Given a payload size this determins + * the required type of option length + * encoding to be used. + * + * If the size is unsupported then + * `OptionLenType.UPPER_PAYLOAD_MARKER` + * is returned. + * + * Params: + * dataSize = the payload's size + * Returns: the `OptionLenType` + */ + private static OptionLenType determineLenType(size_t dataSize) + { + if(dataSize >= 0 && dataSize <= 12) + { + return OptionLenType.ZERO_TO_TWELVE; + } + else if(dataSize >= 13 && dataSize <= 268) + { + return OptionLenType._8BIT_EXTENDED; + } + else if(dataSize >= 269 && dataSize <= 65804) + { + return OptionLenType._12_BIT_EXTENDED; + } + else + { + return OptionLenType.UPPER_PAYLOAD_MARKER; + } + } + public void setType(MessageType type) {