diff --git a/apps/mqttConnector/src/mqttconnector/mqtt_connector.py b/apps/mqttConnector/src/mqttconnector/mqtt_connector.py index a58b6d1..09c8431 100644 --- a/apps/mqttConnector/src/mqttconnector/mqtt_connector.py +++ b/apps/mqttConnector/src/mqttconnector/mqtt_connector.py @@ -115,7 +115,7 @@ class mqttConnector(XAE): # check payload try: readings = json_decode( - base64decode(json_decode(payload)['m2m:cin']['con'])) + base64decode(json_decode(payload)['m2m:cin']['con']).decode('utf-8')) except (ValueError, KeyError, TypeError): self.logger.error('Damaged payload; discarding') return diff --git a/common/openmtc-onem2m/src/openmtc_onem2m/client/http.py b/common/openmtc-onem2m/src/openmtc_onem2m/client/http.py index dcda4d1..3113ca7 100644 --- a/common/openmtc-onem2m/src/openmtc_onem2m/client/http.py +++ b/common/openmtc-onem2m/src/openmtc_onem2m/client/http.py @@ -203,7 +203,7 @@ class OneM2MHTTPClient(OneM2MClient): get_response_status(rsc), request=onem2m_request, rsc=rsc, - pc=decode_onem2m_content(response.read(), response.get("content-type")) + pc=decode_onem2m_content(response.read().decode("utf-8"), response.get("content-type")) ) def send_onem2m_request(self, onem2m_request): diff --git a/common/openmtc-onem2m/src/openmtc_onem2m/model.py b/common/openmtc-onem2m/src/openmtc_onem2m/model.py index bfe5328..aa8f943 100644 --- a/common/openmtc-onem2m/src/openmtc_onem2m/model.py +++ b/common/openmtc-onem2m/src/openmtc_onem2m/model.py @@ -2,7 +2,8 @@ from enum import IntEnum, unique from openmtc.model import (Resource as Res, UnicodeAttribute, DatetimeAttribute, Attribute, ListAttribute, Entity, EntityAttribute, - AnyURI, StringListAttribute, ContentResource) + AnyURI, StringListAttribute, ContentResource, + BytesAttribute) from openmtc.model.exc import ModelTypeError from futile import issubclass @@ -600,7 +601,7 @@ class ResourceC(LabeledResource): typename = None - resourceName = UnicodeAttribute(accesstype=Attribute.WO) + resourceName = UnicodeAttribute(accesstype=Attribute.WO, mandatory=False) resourceType = EntityAttribute(ResourceTypeE, accesstype=Attribute.RO) resourceID = IDS(accesstype=Attribute.RO) @@ -789,8 +790,7 @@ class Subscription(RegularResourceC): notificationForwardingURI = Attribute(AnyURI) batchNotify = EntityAttribute(BatchNotify) rateLimit = EntityAttribute(RateLimit) - preSubscriptionNotify = Attribute(int, accesstype=Attribute.WO, - mandatory=False) + preSubscriptionNotify = Attribute(int, accesstype=Attribute.WO, mandatory=False) pendingNotification = Attribute(PendingNotificationE) notificationStoragePriority = Attribute(int) latestNotify = Attribute(bool) @@ -991,8 +991,8 @@ class ContentInstance(AnnounceableSubordinateResourceC, # ex: application/json:1 contentInfo = UnicodeAttribute() # m2m:contentInfo contentSize = Attribute(int, accesstype=Attribute.RO) - ontologyRef = UnicodeAttribute(accesstype=Attribute.WO) - content = Attribute(bytes, accesstype=Attribute.WO, mandatory=True) + ontologyRef = UnicodeAttribute(accesstype=Attribute.WO, mandatory=False) + content = BytesAttribute(accesstype=Attribute.WO, mandatory=True) __child_types__ = ( Subscription, @@ -1004,8 +1004,8 @@ class ContentInstanceAnnc(AnnouncedSubordinateResourceC): stateTag = Attribute(int, accesstype=Attribute.RO) contentInfo = UnicodeAttribute(EncodingTypeE) # m2m:contentInfo contentSize = Attribute(int, accesstype=Attribute.WO) - ontologyRef = UnicodeAttribute(accesstype=Attribute.WO) - content = Attribute(bytes, accesstype=Attribute.WO, mandatory=True) + ontologyRef = UnicodeAttribute(accesstype=Attribute.WO, mandatory=False) + content = BytesAttribute(accesstype=Attribute.WO, mandatory=True) ################################################################################ @@ -1107,7 +1107,7 @@ class AEAnnc(AnnouncedResourceC, SubscribableResource): typename = "AEAnnc" - appName = UnicodeAttribute(accesstype=Attribute.WO) + appName = UnicodeAttribute(accesstype=Attribute.WO, mandatory=False) App_ID = UnicodeAttribute() AE_ID = UnicodeAttribute() pointOfAccess = StringListAttribute() diff --git a/common/openmtc-onem2m/src/openmtc_onem2m/serializer/json/__init__.py b/common/openmtc-onem2m/src/openmtc_onem2m/serializer/json/__init__.py index f14be0e..2d92166 100644 --- a/common/openmtc-onem2m/src/openmtc_onem2m/serializer/json/__init__.py +++ b/common/openmtc-onem2m/src/openmtc_onem2m/serializer/json/__init__.py @@ -33,6 +33,8 @@ def _default(x): return x.strftime("%Y%m%dT%H%M%S") elif isinstance(x, ContentInstance): return x.resourceID + elif isinstance(x, bytes): + return x.decode('utf-8') else: try: # handle model classes return x.values diff --git a/common/openmtc-onem2m/src/openmtc_onem2m/serializer/util.py b/common/openmtc-onem2m/src/openmtc_onem2m/serializer/util.py index a992a6b..9f276be 100644 --- a/common/openmtc-onem2m/src/openmtc_onem2m/serializer/util.py +++ b/common/openmtc-onem2m/src/openmtc_onem2m/serializer/util.py @@ -6,7 +6,7 @@ logger = get_logger(__name__) def decode_onem2m_content(content, content_type): - if content == "": + if not content: content = None if content_type and content is not None: serializer = get_onem2m_decoder(content_type) diff --git a/common/openmtc/src/openmtc/model/__init__.py b/common/openmtc/src/openmtc/model/__init__.py index 785b757..3928035 100644 --- a/common/openmtc/src/openmtc/model/__init__.py +++ b/common/openmtc/src/openmtc/model/__init__.py @@ -76,7 +76,7 @@ class Collection(Sequence, Mapping): class Member(LoggerMixin): - def __init__(self, type=unicode, version="1.0", *args, **kw): + def __init__(self, type=str, version="1.0", *args, **kw): super(Member, self).__init__(*args, **kw) self.type = type self.version = version @@ -109,7 +109,7 @@ class Attribute(Member): RO = "RO" WO = "WO" - def __init__(self, type=unicode, default=None, + def __init__(self, type=str, default=None, accesstype=None, mandatory=None, update_mandatory=None, id_attribute=None, path_attribute=None, @@ -157,24 +157,22 @@ class Attribute(Member): return self.default -try: - unicode +class BytesAttribute(Attribute): + def __init__(self, default=None, accesstype=None, + mandatory=None, *args, **kw): + super(BytesAttribute, self).__init__(type=bytes, + default=default, + accesstype=accesstype, + mandatory=mandatory, *args, + **kw) - class UnicodeAttribute(Attribute): - def __init__(self, default=None, accesstype=None, - mandatory=False, *args, **kw): - super(UnicodeAttribute, self).__init__(type=unicode, - default=default, - accesstype=accesstype, - mandatory=mandatory, *args, - **kw) + def convert(self, value, instance): + if isinstance(value, str): + return bytes(value, "utf-8") + return super(BytesAttribute, self).convert(value, instance) - def convert(self, value, instance): - if isinstance(value, str): - return value.decode("utf-8") - return super(UnicodeAttribute, self).convert(value, instance) -except NameError: - UnicodeAttribute = Attribute + +UnicodeAttribute = Attribute class DatetimeAttribute(Attribute): @@ -187,7 +185,7 @@ class DatetimeAttribute(Attribute): **kw) def convert(self, value, instance): - if isinstance(value, basestring): + if isinstance(value, str): try: return parse_date(value) except ParseError as e: @@ -196,7 +194,7 @@ class DatetimeAttribute(Attribute): class ListAttribute(Attribute): - def __init__(self, content_type=unicode, type=list, + def __init__(self, content_type=str, type=list, default=NOT_SET, *args, **kw): super(ListAttribute, self).__init__(type=type, default=default, *args, **kw) @@ -239,7 +237,7 @@ class ListAttribute(Attribute): class StringListAttribute(Attribute): - def __init__(self, content_type=unicode, type=list, + def __init__(self, content_type=str, type=list, default=NOT_SET, *args, **kw): super(StringListAttribute, self).__init__(type=type, default=default, *args, **kw) diff --git a/openmtc-app/src/openmtc_app/onem2m.py b/openmtc-app/src/openmtc_app/onem2m.py index d6ab584..99c68be 100644 --- a/openmtc-app/src/openmtc_app/onem2m.py +++ b/openmtc-app/src/openmtc_app/onem2m.py @@ -499,14 +499,14 @@ class XAE(LoggerMixin): cnf = fmt + ':' + str(EncodingTypeE.plain.value) # raise CSENotImplemented("Only json as b64 is supported!") else: - con = b64encode(json_dumps(content)) + con = b64encode(json_dumps(content).encode('utf-8')) cnf = fmt + ':' + str(EncodingTypeE.base64String.value) elif fmt == 'text/plain': if text: con = content cnf = fmt + ':' + str(EncodingTypeE.plain.value) else: - con = b64encode(content) + con = b64encode(content.encode('utf-8')) cnf = fmt + ':' + str(EncodingTypeE.base64String.value) else: # TODO(rst): add handling of other formats or raise not implemented diff --git a/server/openmtc-cse/src/openmtc_cse/methoddomain/controller/__init__.py b/server/openmtc-cse/src/openmtc_cse/methoddomain/controller/__init__.py index b5b6b07..2582c52 100644 --- a/server/openmtc-cse/src/openmtc_cse/methoddomain/controller/__init__.py +++ b/server/openmtc-cse/src/openmtc_cse/methoddomain/controller/__init__.py @@ -1358,7 +1358,7 @@ class ContentInstanceController(OneM2MDefaultController): super(ContentInstanceController, self)._set_mandatory_create_attributes(vals) - vals["contentSize"] = len(vals["content"].encode('utf-8')) + vals["contentSize"] = len(vals["content"]) if not vals.get("contentInfo"): vals["contentInfo"] = 'text/plain:0' @@ -1421,7 +1421,7 @@ class SemanticDescriptorController(OneM2MDefaultController): @staticmethod def _check_descriptor_data(descriptor_data): try: - data = base64.b64decode(descriptor_data) + data = base64.b64decode(descriptor_data).decode('utf-8') except binascii.Error: raise CSEContentsUnacceptable("The descriptor was not correctly base64 encoded.")