Mistake on this page? Email us

Handling resources from a device application

This chapter describes how Device Management Client and the REST API process the different data types. It covers:

  • How to pass the different data types to the API.
  • How the API receives the data, and how it converts the data into the right type.

Passing data types to Device Management Client

You pass data from Device Management Client to Device Management Connect. The Device Management Client is based on the CoAP client, so the resource values are always plain text. You must therefore convert different data type values into a plain text value. There can be used available setters, which take care of conversion. It is also possible to use setter for string value for every data type when value must be in plain text format. For example, an integer value 100 is passed in a text buffer format of 100, and it appears on the web service side as 100 in text format. The service must then interpret this data back to its appropriate format.

Note: The Device Management Client message resending queue sn-coap-resending-queue-size-msgs is by default only two messages long. The queue can fill up if your application sends more than two messages before the server has acknowledged the first message, which can happen in slow networks or if the messages are sent in quick succession (less than one second). You will need to define a longer queue to overcome this. More information is available in the Mbed Client API documentation.

Generic format

The generic format for passing a value is:

M2MResource* instance = object_instance->create_dynamic_resource(“5700", “IntegerType“, M2MResourceInstance::<DATA_TYPE>, true);

Passing an integer value

Integer (from the device application):

int value = 1;
M2MResource* instance = object_instance->create_dynamic_resource(“5700", “IntegerType“, M2MResourceInstance::INTEGER, true);
instance->set_value(value);

Response on the REST API:

{"async-response-id":...{ "id" : "1131895768#f4b93d6e-4652-4874-82e4-41a3ced0cd56@b02ac40a-a501-44d9-be29-e6e0e5303f85/3303/0/5700", "status" : 200, "payload" : "MQ==", "ct" : "text/plain", "max-age" : 300 }
  • Base64 encoded value of the payload -> MQ==
  • Base64 decoded value of the payload -> 1

Passing a float value

Float (from the device application):

float value = 1.0232f;
M2MResource* instance = object_instance->create_dynamic_resource(“5700", “IntegerType“, M2MResourceInstance::FLOAT, true);
instance->set_value_float(value);

Response on the REST API:

{"async-response-id":...{ "id" : "1132945328#f4b93d6e-4652-4874-82e4-41a3ced0cd56@b02ac40a-a501-44d9-be29-e6e0e5303f85/3303/0/5700", "status" : 200, "payload" : "MS4wMjMyMDA=",
"ct" : "text/plain", "max-age" : 300 }
  • Base64 encoded value of the payload -> MS4wMjMyMDA=
  • Base64 decoded value of the payload -> 1.023200

Passing a boolean value

Boolean (from the device application):

bool value = true;
M2MResource* instance = object_instance->create_dynamic_resource(“5700", “IntegerType“, M2MResourceInstance::BOOLEAN, true);
instance->set_value(value);

Response on the REST API:

{"async-response-id":….{ "id" : "1131895768#f4b93d6e-4652-4874-82e4-41a3ced0cd56@b02ac40a-a501-44d9-be29-e6e0e5303f85/3303/0/5700", "status" : 200, "payload" : "MQ==", "ct" : "text/plain", "max-age" : 300 }
  • Base64 encoded value of the payload -> MQ==
  • Base64 decoded value of the payload -> 1

Passing a string value

String (from the device application):

M2MResource* instance = object_instance->create_dynamic_resource(“5700", “IntegerType“, M2MResourceInstance::STRING, true);
char buffer[10] = “Test Data”;
instance->set_value((const uint8_t*)buffer, 10);

Response on the REST API:

{"async-response-id":... { "id" : "1133335779#f4b93d6e-4652-4874-82e4-41a3ced0cd56@b02ac40a-a501-44d9-be29-e6e0e5303f85/3303/0/5700", "status" : 200, "payload" : "VGVzdCBEYXRh",
"ct" : "text/plain", "max-age" : 300 }
  • Base64 encoded value of the payload -> VGVzdCBEYXRh
  • Base64 decoded value of the payload -> Test Data

Passing an opaque value

Opaque (from the device application):

M2MResource* instance = object_instance->create_dynamic_resource(“5700", “IntegerType“, M2MResourceInstance::OPAQUE, true);
uint8_t buffer[12] = “Opaque Data”;
instance->set_value(buffer, 12);

Response on the REST API:

{"async-response-id":... { "id" : "1134045351#f4b93d6e-4652-4874-82e4-41a3ced0cd56@b02ac40a-a501-44d9-be29-e6e0e5303f85/3303/0/5700", "status" : 200, "payload" : "T3BhcXVlIERhdGEA",
"ct" : "application/octet-stream", "max-age" : 300 }
  • Base64 encoded value of the payload -> T3BhcXVlIERhdGEA
  • Base64 decoded value of the payload -> Opaque Data

Working with the server cache

To store the value of a resource in the Device Management server cache, set the resource's lifetime using the max_age parameter:

instance->set_max_age(300) ; // This is the resource lifetime in the server cache (in seconds).

The cache stores the resource value for the period you define, and subsequent requests to the same resource receive the response directly from the cache.

Note: For more information about the Device Management server cache, see the optimization section.