Respect binary coded payload data

This commit is contained in:
Björn Freise 2019-04-23 11:42:01 +02:00
parent 3c7b11c149
commit 36fb9f243a
2 changed files with 14 additions and 10 deletions

View file

@ -42,7 +42,7 @@ class Option(object):
:return: the option value in the correct format depending on the option
"""
if type(self._value) is None:
self._value = bytearray()
self._value = bytes()
opt_type = defines.OptionRegistry.LIST[self._number].value_type
if opt_type == defines.INTEGER:
if byte_len(self._value) > 0:

View file

@ -113,13 +113,17 @@ class Serializer(object):
raise AttributeError("Packet length %s, pos %s" % (length_packet, pos))
message.payload = ""
payload = values[pos:]
try:
if message.payload_type == defines.Content_types["application/octet-stream"]:
if hasattr(message, 'payload_type') and message.payload_type in [
defines.Content_types["application/octet-stream"],
defines.Content_types["application/exi"],
defines.Content_types["application/cbor"]
]:
message.payload = payload
else:
try:
message.payload = payload.decode("utf-8")
except AttributeError:
message.payload = payload.decode("utf-8")
message.payload = payload
pos += len(payload)
return message
@ -323,7 +327,7 @@ class Serializer(object):
@staticmethod
def convert_to_raw(number, value, length):
"""
Get the value of an option as a ByteArray.
Get the value of an option as bytes.
:param number: the option number
:param value: the option value
@ -350,11 +354,11 @@ class Serializer(object):
if isinstance(value, str):
value = str(value)
if isinstance(value, str):
return bytearray(value, "utf-8")
return bytes(value, "utf-8")
elif isinstance(value, int):
return value
else:
return bytearray(value)
return bytes(value)
@staticmethod
def as_sorted_list(options):