Skip to content

Commit e157707

Browse files
author
Jani Suonperä
authored
Add support for TLV binary integers (ARMmbed#463)
This support TLV binary formats Integer, Boolean and Time. If resource type is INTEGER then value will convert to 64-bit binary buffer. Format is two's complement big endian word. See, OMA-TS-LightweightM2M-V1_0-20170208-A, Appendix C, data Types, Integer, TLV Format. * Change atoi to atoll * Add support for TLV binary boolean * Add support for TLV binary time * Review fixes * Review fixes. * Fix unit tests coverage
1 parent f8f0fc8 commit e157707

File tree

8 files changed

+61
-23
lines changed

8 files changed

+61
-23
lines changed

mbed-client/m2mresourceinstance.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ friend class M2MResource;
199199
* \brief Converts a value to integer and returns it. Note: Conversion
200200
* errors are not detected.
201201
*/
202-
int get_value_int();
202+
int64_t get_value_int() const;
203203

204204
/**
205205
* Get the value as a string object. No encoding/charset conversions

module.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"license": "Apache-2.0",
99
"dependencies": {
1010
"mbed-client-c": "^5.0.0",
11-
"mbed-trace": ">=0.2.0,<2.0.0"
11+
"mbed-trace": ">=0.2.0,<2.0.0",
12+
"nanostack-libservice": "^3.0.0"
1213
},
1314
"targetDependencies": {
1415
"arm": {

run_unit_tests.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ find ./build -name '*.xml' | xargs cp -t ./results/
3232
find ./build/x86-linux-native-coverage/test -name '*.gcno' | xargs cp -t ./coverage/
3333
find ./build/x86-linux-native-coverage/test -name '*.gcda' | xargs cp -t ./coverage/
3434
touch coverage/*.gcda
35-
exclude_files="${PWD}/test/"
35+
exclude_files="${PWD}/test/|${PWD}/yotta_modules*"
36+
#exclude_files2="${PWD}/yotta_modules*"
3637
gcovr -r ./ --gcov-filter='.*source*.' --exclude-unreachable-branches --exclude $exclude_files --object-directory ./coverage -x -o ./results/gcovr.xml
3738
echo
3839
echo "Create coverage document"

source/include/m2mtlvserializer.h

+2
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,6 @@ private :
9595
static void serialize_id(uint16_t id, uint32_t &size, uint8_t *id_ptr);
9696

9797
static void serialize_length(uint32_t length, uint32_t &size, uint8_t *length_ptr);
98+
99+
static bool serialize_TLV_binary_int(const M2MResourceInstance *resource, uint8_t type, uint16_t id, uint8_t *&data, uint32_t &size);
98100
};

source/m2mresourceinstance.cpp

+6-11
Original file line numberDiff line numberDiff line change
@@ -337,17 +337,12 @@ void M2MResourceInstance::get_value(uint8_t *&value, uint32_t &value_length)
337337
}
338338
}
339339

340-
int M2MResourceInstance::get_value_int()
341-
{
342-
int value_int = 0;
343-
// Get the value and convert it into integer. This is not the most
344-
// efficient way, as it takes pointless heap copy to get the zero termination.
345-
uint8_t* buffer = NULL;
346-
uint32_t length;
347-
get_value(buffer,length);
348-
if(buffer) {
349-
value_int = atoi((const char*)buffer);
350-
free(buffer);
340+
int64_t M2MResourceInstance::get_value_int() const
341+
{
342+
int64_t value_int = 0;
343+
344+
if(_value) {
345+
value_int = atoll((const char*)_value);
351346
}
352347
return value_int;
353348
}

source/m2mtlvserializer.cpp

+44-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "include/nsdllinker.h"
1818
#include "mbed-client/m2mconstants.h"
1919
#include <stdlib.h>
20+
#include "common_functions.h"
2021

2122
#define TRACE_GROUP "mClt"
2223

@@ -126,8 +127,15 @@ bool M2MTLVSerializer::serialize_resource(const M2MResource *resource, uint8_t *
126127
{
127128
bool success = false;
128129
if(resource->name_id() != -1) {
129-
success = serialize_TILV(TYPE_RESOURCE, resource->name_id(),
130+
if ( (resource->resource_instance_type() == M2MResourceInstance::INTEGER) ||
131+
(resource->resource_instance_type() == M2MResourceInstance::BOOLEAN) ||
132+
(resource->resource_instance_type() == M2MResourceInstance::TIME) ) {
133+
success = serialize_TLV_binary_int(resource, TYPE_RESOURCE, resource->name_id(), data, size);
134+
}
135+
else {
136+
success = serialize_TILV(TYPE_RESOURCE, resource->name_id(),
130137
resource->value(), resource->value_length(), data, size);
138+
}
131139
}
132140
return success;
133141
}
@@ -166,9 +174,43 @@ bool M2MTLVSerializer::serialize_multiple_resource(const M2MResource *resource,
166174

167175
bool M2MTLVSerializer::serialize_resource_instance(uint16_t id, const M2MResourceInstance *resource, uint8_t *&data, uint32_t &size)
168176
{
169-
return serialize_TILV(TYPE_RESOURCE_INSTANCE, id, resource->value(), resource->value_length(), data, size);
177+
bool success;
178+
179+
if ( (resource->resource_instance_type() == M2MResourceInstance::INTEGER) ||
180+
(resource->resource_instance_type() == M2MResourceInstance::BOOLEAN) ||
181+
(resource->resource_instance_type() == M2MResourceInstance::TIME) ) {
182+
success=serialize_TLV_binary_int(resource, TYPE_RESOURCE_INSTANCE, id, data, size);
183+
}
184+
else {
185+
success=serialize_TILV(TYPE_RESOURCE_INSTANCE, id, resource->value(), resource->value_length(), data, size);
186+
}
187+
188+
return success;
189+
}
190+
191+
/* See, OMA-TS-LightweightM2M-V1_0-20170208-A, Appendix C,
192+
* Data Types, Integer, Boolean and TY
193+
* Yime, TLV Format */
194+
bool M2MTLVSerializer::serialize_TLV_binary_int(const M2MResourceInstance *resource, uint8_t type, uint16_t id, uint8_t *&data, uint32_t &size)
195+
{
196+
int64_t valueInt = resource->get_value_int();
197+
uint32_t buffer_size;
198+
/* max len 8 bytes */
199+
uint8_t buffer[8];
200+
201+
if (resource->resource_instance_type() == M2MResourceInstance::BOOLEAN) {
202+
buffer_size = 1;
203+
buffer[0] = valueInt;
204+
}
205+
else {
206+
buffer_size = 8;
207+
common_write_64_bit(valueInt, buffer);
208+
}
209+
210+
return serialize_TILV(type, id, buffer, buffer_size, data, size);
170211
}
171212

213+
172214
bool M2MTLVSerializer::serialize_TILV(uint8_t type, uint16_t id, uint8_t *value, uint32_t value_length, uint8_t *&data, uint32_t &size)
173215
{
174216
uint8_t *tlv = 0;

test/mbedclient/utest/m2mtlvdeserializer/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ add_executable(m2mtlv
2121
target_link_libraries(m2mtlv
2222
CppUTest
2323
CppUTestExt
24+
nanostack-libservice
2425
)
2526
set_target_properties(m2mtlv
2627
PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS}"

test/mbedclient/utest/stub/m2mresourceinstance_stub.cpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,16 @@ void M2MResourceInstance::get_value(uint8_t *&value, uint32_t &value_length)
170170
}
171171
}
172172

173-
int M2MResourceInstance::get_value_int()
173+
int64_t M2MResourceInstance::get_value_int() const
174174
{
175175
// Note: this is a copy-paste from the original version, as the tests
176176
// set only m2mresourceinstance_stub::value.
177177

178178
int value_int = 0;
179-
// Get the value and convert it into integer. This is not the most
180-
// efficient way, as it takes pointless heap copy to get the zero termination.
181-
uint8_t* buffer = NULL;
182-
uint32_t length;
183-
get_value(buffer,length);
179+
uint8_t* buffer = m2mresourceinstance_stub::value;
180+
184181
if(buffer) {
185182
value_int = atoi((const char*)buffer);
186-
free(buffer);
187183
}
188184
return value_int;
189185
}

0 commit comments

Comments
 (0)