diff --git a/mbed-client/m2mresourceinstance.h b/mbed-client/m2mresourceinstance.h index e927ecad..ef159650 100644 --- a/mbed-client/m2mresourceinstance.h +++ b/mbed-client/m2mresourceinstance.h @@ -199,7 +199,7 @@ friend class M2MResource; * \brief Converts a value to integer and returns it. Note: Conversion * errors are not detected. */ - int get_value_int(); + int64_t get_value_int() const; /** * Get the value as a string object. No encoding/charset conversions diff --git a/module.json b/module.json index 39447fa7..f899ef22 100644 --- a/module.json +++ b/module.json @@ -8,7 +8,8 @@ "license": "Apache-2.0", "dependencies": { "mbed-client-c": "^5.0.0", - "mbed-trace": ">=0.2.0,<2.0.0" + "mbed-trace": ">=0.2.0,<2.0.0", + "nanostack-libservice": "^3.0.0" }, "targetDependencies": { "arm": { diff --git a/run_unit_tests.sh b/run_unit_tests.sh index 04939785..f6c734fa 100755 --- a/run_unit_tests.sh +++ b/run_unit_tests.sh @@ -32,7 +32,8 @@ find ./build -name '*.xml' | xargs cp -t ./results/ find ./build/x86-linux-native-coverage/test -name '*.gcno' | xargs cp -t ./coverage/ find ./build/x86-linux-native-coverage/test -name '*.gcda' | xargs cp -t ./coverage/ touch coverage/*.gcda -exclude_files="${PWD}/test/" +exclude_files="${PWD}/test/|${PWD}/yotta_modules*" +#exclude_files2="${PWD}/yotta_modules*" gcovr -r ./ --gcov-filter='.*source*.' --exclude-unreachable-branches --exclude $exclude_files --object-directory ./coverage -x -o ./results/gcovr.xml echo echo "Create coverage document" diff --git a/source/include/m2mtlvserializer.h b/source/include/m2mtlvserializer.h index a31a0486..7bd945cf 100644 --- a/source/include/m2mtlvserializer.h +++ b/source/include/m2mtlvserializer.h @@ -95,4 +95,6 @@ private : static void serialize_id(uint16_t id, uint32_t &size, uint8_t *id_ptr); static void serialize_length(uint32_t length, uint32_t &size, uint8_t *length_ptr); + + static bool serialize_TLV_binary_int(const M2MResourceInstance *resource, uint8_t type, uint16_t id, uint8_t *&data, uint32_t &size); }; diff --git a/source/m2mresourceinstance.cpp b/source/m2mresourceinstance.cpp index 3bd7b8ac..58c9499b 100644 --- a/source/m2mresourceinstance.cpp +++ b/source/m2mresourceinstance.cpp @@ -337,17 +337,12 @@ void M2MResourceInstance::get_value(uint8_t *&value, uint32_t &value_length) } } -int M2MResourceInstance::get_value_int() -{ - int value_int = 0; - // 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; - get_value(buffer,length); - if(buffer) { - value_int = atoi((const char*)buffer); - free(buffer); +int64_t M2MResourceInstance::get_value_int() const +{ + int64_t value_int = 0; + + if(_value) { + value_int = atoll((const char*)_value); } return value_int; } diff --git a/source/m2mtlvserializer.cpp b/source/m2mtlvserializer.cpp index 07a381b2..73c5e506 100644 --- a/source/m2mtlvserializer.cpp +++ b/source/m2mtlvserializer.cpp @@ -17,6 +17,7 @@ #include "include/nsdllinker.h" #include "mbed-client/m2mconstants.h" #include +#include "common_functions.h" #define TRACE_GROUP "mClt" @@ -126,8 +127,15 @@ bool M2MTLVSerializer::serialize_resource(const M2MResource *resource, uint8_t * { bool success = false; if(resource->name_id() != -1) { - success = serialize_TILV(TYPE_RESOURCE, resource->name_id(), + if ( (resource->resource_instance_type() == M2MResourceInstance::INTEGER) || + (resource->resource_instance_type() == M2MResourceInstance::BOOLEAN) || + (resource->resource_instance_type() == M2MResourceInstance::TIME) ) { + success = serialize_TLV_binary_int(resource, TYPE_RESOURCE, resource->name_id(), data, size); + } + else { + success = serialize_TILV(TYPE_RESOURCE, resource->name_id(), resource->value(), resource->value_length(), data, size); + } } return success; } @@ -166,9 +174,43 @@ bool M2MTLVSerializer::serialize_multiple_resource(const M2MResource *resource, bool M2MTLVSerializer::serialize_resource_instance(uint16_t id, const M2MResourceInstance *resource, uint8_t *&data, uint32_t &size) { - return serialize_TILV(TYPE_RESOURCE_INSTANCE, id, resource->value(), resource->value_length(), data, size); + bool success; + + if ( (resource->resource_instance_type() == M2MResourceInstance::INTEGER) || + (resource->resource_instance_type() == M2MResourceInstance::BOOLEAN) || + (resource->resource_instance_type() == M2MResourceInstance::TIME) ) { + success=serialize_TLV_binary_int(resource, TYPE_RESOURCE_INSTANCE, id, data, size); + } + else { + success=serialize_TILV(TYPE_RESOURCE_INSTANCE, id, resource->value(), resource->value_length(), data, size); + } + + return success; +} + +/* See, OMA-TS-LightweightM2M-V1_0-20170208-A, Appendix C, + * Data Types, Integer, Boolean and TY + * Yime, TLV Format */ +bool M2MTLVSerializer::serialize_TLV_binary_int(const M2MResourceInstance *resource, uint8_t type, uint16_t id, uint8_t *&data, uint32_t &size) +{ + int64_t valueInt = resource->get_value_int(); + uint32_t buffer_size; + /* max len 8 bytes */ + uint8_t buffer[8]; + + if (resource->resource_instance_type() == M2MResourceInstance::BOOLEAN) { + buffer_size = 1; + buffer[0] = valueInt; + } + else { + buffer_size = 8; + common_write_64_bit(valueInt, buffer); + } + + return serialize_TILV(type, id, buffer, buffer_size, data, size); } + bool M2MTLVSerializer::serialize_TILV(uint8_t type, uint16_t id, uint8_t *value, uint32_t value_length, uint8_t *&data, uint32_t &size) { uint8_t *tlv = 0; diff --git a/test/mbedclient/utest/m2mtlvdeserializer/CMakeLists.txt b/test/mbedclient/utest/m2mtlvdeserializer/CMakeLists.txt index 498b1e65..d554cbd6 100644 --- a/test/mbedclient/utest/m2mtlvdeserializer/CMakeLists.txt +++ b/test/mbedclient/utest/m2mtlvdeserializer/CMakeLists.txt @@ -21,6 +21,7 @@ add_executable(m2mtlv target_link_libraries(m2mtlv CppUTest CppUTestExt + nanostack-libservice ) set_target_properties(m2mtlv PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS}" diff --git a/test/mbedclient/utest/stub/m2mresourceinstance_stub.cpp b/test/mbedclient/utest/stub/m2mresourceinstance_stub.cpp index aeb611e2..dbe743d8 100755 --- a/test/mbedclient/utest/stub/m2mresourceinstance_stub.cpp +++ b/test/mbedclient/utest/stub/m2mresourceinstance_stub.cpp @@ -170,20 +170,16 @@ void M2MResourceInstance::get_value(uint8_t *&value, uint32_t &value_length) } } -int M2MResourceInstance::get_value_int() +int64_t M2MResourceInstance::get_value_int() const { // Note: this is a copy-paste from the original version, as the tests // set only m2mresourceinstance_stub::value. int value_int = 0; - // 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; - get_value(buffer,length); + uint8_t* buffer = m2mresourceinstance_stub::value; + if(buffer) { value_int = atoi((const char*)buffer); - free(buffer); } return value_int; }