From 13039faf712215db3c0db73040e192a17438613f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hubert=20Mi=C5=9B?= Date: Tue, 22 Dec 2020 13:17:39 +0100 Subject: [PATCH] Add Size2 option handling according to RFC7959 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hubert Miś --- coapthon/defines.py | 2 ++ coapthon/layers/blocklayer.py | 3 +++ coapthon/messages/message.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/coapthon/defines.py b/coapthon/defines.py index 9b081ea..b299aef 100644 --- a/coapthon/defines.py +++ b/coapthon/defines.py @@ -124,6 +124,7 @@ class OptionRegistry(object): LOCATION_QUERY = OptionItem(20,"Location-Query",STRING, True, None) BLOCK2 = OptionItem(23, "Block2", INTEGER, False, None) BLOCK1 = OptionItem(27, "Block1", INTEGER, False, None) + SIZE2 = OptionItem(28, "Size2", INTEGER, False, 0) PROXY_URI = OptionItem(35, "Proxy-Uri", STRING, False, None) PROXY_SCHEME = OptionItem(39, "Proxy-Schema", STRING, False, None) SIZE1 = OptionItem(60, "Size1", INTEGER, False, None) @@ -147,6 +148,7 @@ class OptionRegistry(object): 20: LOCATION_QUERY, 23: BLOCK2, 27: BLOCK1, + 28: SIZE2, 35: PROXY_URI, 39: PROXY_SCHEME, 60: SIZE1, diff --git a/coapthon/layers/blocklayer.py b/coapthon/layers/blocklayer.py index 8f698a3..942b693 100644 --- a/coapthon/layers/blocklayer.py +++ b/coapthon/layers/blocklayer.py @@ -225,6 +225,9 @@ class BlockLayer(object): self._block2_receive[key_token] = BlockItem(byte, num, m, size) + if transaction.request.size2 is not None: + transaction.response.size2 = len(transaction.response.payload) + if len(transaction.response.payload) > (byte + size): m = 1 else: diff --git a/coapthon/messages/message.py b/coapthon/messages/message.py index c7aa32f..e1e468e 100644 --- a/coapthon/messages/message.py +++ b/coapthon/messages/message.py @@ -641,6 +641,38 @@ class Message(object): """ self.del_option_by_number(defines.OptionRegistry.BLOCK2.number) + @property + def size2(self): + """ + Get the Size2 option. + + :return: the Size2 value + """ + value = None + for option in self.options: + if option.number == defines.OptionRegistry.SIZE2.number: + value = option.value + return value + + @size2.setter + def size2(self, value): + """ + Set the Size2 option. + + :param value: the Block2 value + """ + option = Option() + option.number = defines.OptionRegistry.SIZE2.number + option.value = value + self.add_option(option) + + @size2.deleter + def size2(self): + """ + Delete the Size2 option. + """ + self.del_option_by_number(defines.OptionRegistry.SIZE2.number) + @property def line_print(self): """