diff --git a/source/include/m2mtlvdeserializer.h b/source/include/m2mtlvdeserializer.h index c6ee6be8..8e11fc3a 100644 --- a/source/include/m2mtlvdeserializer.h +++ b/source/include/m2mtlvdeserializer.h @@ -20,8 +20,8 @@ /** * @brief M2MTLVDeserializer - * TLV Deserialiser get the object instances and resources as binary data and - * builds the lwm2m representation from it. See OMA-LWM2M + * TLV Deserialiser get the object instances and resources as binary data and + * builds the lwm2m representation from it. See OMA-LWM2M * specification, chapter 6.1 for the resource model and chapter 6.3.3 for * the OMA-TLV specification. */ @@ -51,9 +51,9 @@ public : */ static bool is_object_instance(uint8_t *tlv); - + /** - * This method checks whether the given binary encodes a resource or + * This method checks whether the given binary encodes a resource or * something else. It returns true if bits 7-6 of the first * byte is "11". * @param tlv Binary to be checked as LWM2M resource. @@ -69,7 +69,7 @@ public : * @return true or false. */ static bool is_multiple_resource(uint8_t *tlv); - + /** * This method checks whether the given binary encodes a resource instance * or something else. It returns true if bits 7-6 of the first @@ -81,7 +81,7 @@ public : /** * Deserialises the given binary that must encode object instances. Binary - * array can be checked before invoking this method with + * array can be checked before invoking this method with */ static M2MTLVDeserializer::Error deserialise_object_instances(uint8_t* tlv, uint32_t tlv_size, @@ -89,7 +89,7 @@ public : M2MTLVDeserializer::Operation operation); /** - * Deserialises the given binary that must encode resources. Binary array + * Deserialises the given binary that must encode resources. Binary array * can be checked before invoking this method. */ static M2MTLVDeserializer::Error deserialize_resources(uint8_t *tlv, @@ -111,7 +111,7 @@ public : * @return Object instance id or resource id. */ static uint16_t instance_id(uint8_t *tlv); - + private: static M2MTLVDeserializer::Error deserialize_object_instances(uint8_t *tlv, @@ -120,7 +120,7 @@ public : M2MObject &object, M2MTLVDeserializer::Operation operation, bool update_value); - + static M2MTLVDeserializer::Error deserialize_resources(uint8_t *tlv, uint32_t tlv_size, uint32_t offset, @@ -144,12 +144,14 @@ public : bool update_value); static bool is_object_instance(uint8_t *tlv, uint32_t offset); - + static bool is_resource(uint8_t *tlv, uint32_t offset); - + static bool is_multiple_resource(uint8_t *tlv, uint32_t offset); - + static bool is_resource_instance(uint8_t *tlv, uint32_t offset); + + static void set_resource_instance_value(M2MResourceInstance *res, uint8_t *tlv, uint32_t size); }; class TypeIdLength { diff --git a/source/m2mnsdlinterface.cpp b/source/m2mnsdlinterface.cpp index a7235b7e..0262bb40 100644 --- a/source/m2mnsdlinterface.cpp +++ b/source/m2mnsdlinterface.cpp @@ -1668,6 +1668,11 @@ bool M2MNsdlInterface::validate_security_object() uint32_t public_key_size = _security->get_resource(M2MSecurity::PublicKey)->value_length(); uint32_t server_key_size = _security->get_resource(M2MSecurity::ServerPublicKey)->value_length(); uint32_t pkey_size = _security->get_resource(M2MSecurity::Secretkey)->value_length(); + M2MDevice* dev = M2MInterfaceFactory::create_device(); + if (dev) { + tr_debug("M2MNsdlInterface::validate_security_object - Current time: %" PRIu32, + dev->resource_value_int(M2MDevice::CurrentTime)); + } tr_debug("M2MNsdlInterface::validate_security_object - Server URI /0/0: %s", address.c_str()); tr_debug("M2MNsdlInterface::validate_security_object - is bs server /0/1: %d", is_bs_server); tr_debug("M2MNsdlInterface::validate_security_object - Security Mode /0/2: %" PRIu32, sec_mode); diff --git a/source/m2msecurity.cpp b/source/m2msecurity.cpp index abb5c840..b5439811 100644 --- a/source/m2msecurity.cpp +++ b/source/m2msecurity.cpp @@ -190,12 +190,8 @@ bool M2MSecurity::set_resource_value(SecurityResource resource, M2MSecurity::M2MServerSMSNumber == resource || M2MSecurity::ShortServerID == resource || M2MSecurity::ClientHoldOffTime == resource) { - // If it is any of the above resource - // set the value of the resource. - uint8_t size = 0; - uint8_t *buffer = String::convert_integer_to_array(value, size); - success = res->set_value(buffer,size); - free(buffer); + success = res->set_value(value); + } } return success; @@ -272,15 +268,8 @@ uint32_t M2MSecurity::resource_value_int(SecurityResource resource) const M2MSecurity::M2MServerSMSNumber == resource || M2MSecurity::ShortServerID == resource || M2MSecurity::ClientHoldOffTime == resource) { - // Get the value and convert it into integer. This is not the most - // efficient way, as it takes pointless heap copy to get the zero termination. - uint8_t* buffer = NULL; - uint32_t length = 0; - res->get_value(buffer,length); - if(buffer) { - value = String::convert_array_to_integer(buffer,length); - free(buffer); - } + // note: the value may be 32bit int on 32b archs. + value = res->get_value_int(); } } return value; diff --git a/source/m2mtlvdeserializer.cpp b/source/m2mtlvdeserializer.cpp index d2ff3929..0378cd87 100644 --- a/source/m2mtlvdeserializer.cpp +++ b/source/m2mtlvdeserializer.cpp @@ -167,7 +167,24 @@ M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_resources(uint8_t *tlv if(update_value) { if(til._length > 0) { tr_debug("M2MTLVDeserializer::deserialize_resources() - Update value"); - (*it)->set_value(tlv+offset, til._length); + int64_t value = 0; + switch ((*it)->resource_instance_type()) { + case M2MResourceInstance::INTEGER: + case M2MResourceInstance::BOOLEAN: + value = String::convert_array_to_integer(tlv+offset, til._length); + (*it)->set_value(value); + break; + // Todo! implement support for other types as well + case M2MResourceInstance::STRING: + case M2MResourceInstance::FLOAT: + case M2MResourceInstance::OPAQUE: + case M2MResourceInstance::TIME: + case M2MResourceInstance::OBJLINK: + (*it)->set_value(tlv+offset, til._length); + break; + default: + break; + } } else { tr_debug("M2MTLVDeserializer::deserialize_resources() - Clear Value"); (*it)->clear_value(); @@ -235,7 +252,7 @@ M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_resource_instances(uin found = true; if(update_value) { if(til._length > 0) { - (*it)->set_value(tlv+offset, til._length); + set_resource_instance_value((*it), tlv+offset, til._length); } else { (*it)->clear_value(); } @@ -295,7 +312,7 @@ M2MTLVDeserializer::Error M2MTLVDeserializer::deserialize_resource_instances(uin found = true; if(update_value) { if(til._length > 0) { - (*it)->set_value(tlv+offset, til._length); + set_resource_instance_value((*it),tlv+offset, til._length); } else { (*it)->clear_value(); } @@ -371,6 +388,28 @@ bool M2MTLVDeserializer::is_resource_instance(uint8_t *tlv, uint32_t offset) return ret; } +void M2MTLVDeserializer::set_resource_instance_value(M2MResourceInstance *res, uint8_t *tlv, uint32_t size) +{ + int64_t value = 0; + switch (res->resource_instance_type()) { + case M2MResourceInstance::INTEGER: + case M2MResourceInstance::BOOLEAN: + value = String::convert_array_to_integer(tlv, size); + res->set_value(value); + break; + // Todo! implement conversion for other types as well + case M2MResourceInstance::STRING: + case M2MResourceInstance::FLOAT: + case M2MResourceInstance::OPAQUE: + case M2MResourceInstance::TIME: + case M2MResourceInstance::OBJLINK: + res->set_value(tlv, size); + break; + default: + break; + } +} + TypeIdLength::TypeIdLength(uint8_t *tlv, uint32_t offset) { _tlv = tlv;