This is an example of BLE GATT Client, which receives broadcast data from BLE_Server_BME280 ( a GATT server) , then transfers values up to mbed Device Connector (cloud).

Please refer details about BLEClient_mbedDevConn below. https://github.com/soramame21/BLEClient_mbedDevConn

The location of required BLE GATT server, BLE_Server_BME280, is at here. https://developer.mbed.org/users/edamame22/code/BLE_Server_BME280/

Committer:
Ren Boting
Date:
Tue Sep 05 11:56:13 2017 +0900
Revision:
2:b894b3508057
Parent:
0:29983394c6b6
Update all libraries and reform main.cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
edamame22 0:29983394c6b6 1 # How to use the mbed Client API
edamame22 0:29983394c6b6 2
edamame22 0:29983394c6b6 3 This section explains how to use our C++ API to create and configure different types of Objects, Object Instances and Resources for mbed Client to comply with the OMA LWM2M specifications. These resources can then be communicated to mbed Device Server.
edamame22 0:29983394c6b6 4
edamame22 0:29983394c6b6 5 <span class="notes">**Note:** The API complies with the OMA LWM2M specifications.</span>
edamame22 0:29983394c6b6 6
edamame22 0:29983394c6b6 7 As per the OMA LWM2M specification:
edamame22 0:29983394c6b6 8
edamame22 0:29983394c6b6 9 - The client must have defined Objects, under which it can create Object Instances.
edamame22 0:29983394c6b6 10 - The client must have created Object Instances under Objects, which will eventually contain Resources.
edamame22 0:29983394c6b6 11 - The client must have Resources under Object Instances which belong to Objects.
edamame22 0:29983394c6b6 12 - The client must have created Resources under Object Instance. You can create M2MResource from the M2MObjectInstance class.
edamame22 0:29983394c6b6 13 - The client must have created Resource Instances under Resources. You can create M2MResourceInstance from the M2MObjectInstance class.
edamame22 0:29983394c6b6 14
edamame22 0:29983394c6b6 15 Read the API doxygen documentation [here](https://docs.mbed.com/docs/mbed-client-guide/en/latest/api/index.html).
edamame22 0:29983394c6b6 16
edamame22 0:29983394c6b6 17 ## How to create and configure Objects
edamame22 0:29983394c6b6 18
edamame22 0:29983394c6b6 19 With this API, you can create and configure the following Objects:
edamame22 0:29983394c6b6 20
edamame22 0:29983394c6b6 21 - [Device Objects](#device-object)
edamame22 0:29983394c6b6 22 - [Security Objects](#security-object)
edamame22 0:29983394c6b6 23 - [Custom Objects](#custom-object)
edamame22 0:29983394c6b6 24
edamame22 0:29983394c6b6 25 The M2MObject class is derived from the M2MBase class, so all the public methods can be used from the M2MObject and its derived classes.
edamame22 0:29983394c6b6 26
edamame22 0:29983394c6b6 27 ### Creating OMA-defined Objects
edamame22 0:29983394c6b6 28
edamame22 0:29983394c6b6 29 #### Device Object
edamame22 0:29983394c6b6 30
edamame22 0:29983394c6b6 31 To create a Device Object:
edamame22 0:29983394c6b6 32
edamame22 0:29983394c6b6 33 `static M2MDevice *create_device();`
edamame22 0:29983394c6b6 34
edamame22 0:29983394c6b6 35 Because there can be only one instance of M2MDevice, it is a static class and you can delete it as follows:
edamame22 0:29983394c6b6 36
edamame22 0:29983394c6b6 37 `M2MDevice::delete_instance();`
edamame22 0:29983394c6b6 38
edamame22 0:29983394c6b6 39 Check the [M2MDevice class documentation](https://docs.mbed.com/docs/mbed-client-guide/en/latest/api/m2mdevice_8h.html) to see how to configure the Device Object.
edamame22 0:29983394c6b6 40
edamame22 0:29983394c6b6 41 #### Security Object
edamame22 0:29983394c6b6 42
edamame22 0:29983394c6b6 43 To create a Security Object:
edamame22 0:29983394c6b6 44
edamame22 0:29983394c6b6 45 `static M2MSecurity *create_security(M2MSecurity::ServerType server_type);`
edamame22 0:29983394c6b6 46
edamame22 0:29983394c6b6 47 You can create a Bootstrap or normal mbed Device Server by passing the appropriate `enum` value.
edamame22 0:29983394c6b6 48
edamame22 0:29983394c6b6 49 Check the [M2MSecurity class documentation](https://docs.mbed.com/docs/mbed-client-guide/en/latest/api/m2msecurity_8h.html) to see how to configure the Security Object, as well as how to create appropriate Resources and assign values to them.
edamame22 0:29983394c6b6 50
edamame22 0:29983394c6b6 51 #### Custom Object
edamame22 0:29983394c6b6 52
edamame22 0:29983394c6b6 53 For a Custom Object, you need to pass the name of the Object that you would like to create (for example `Test`); this will create an Object with that name in mbed Device Server.
edamame22 0:29983394c6b6 54
edamame22 0:29983394c6b6 55 To create an M2MObject:
edamame22 0:29983394c6b6 56
edamame22 0:29983394c6b6 57 `static M2MObject *create_object(const String &name);`
edamame22 0:29983394c6b6 58
edamame22 0:29983394c6b6 59 #### Configuring the Object
edamame22 0:29983394c6b6 60
edamame22 0:29983394c6b6 61 Once you have created an Object (whether OMA-specific or custom), you can configure various parameters in it to control or modify communication with mbed Device Server.
edamame22 0:29983394c6b6 62
edamame22 0:29983394c6b6 63 ```
edamame22 0:29983394c6b6 64 M2MObject * _object = M2MInterfaceFactory::create_object("Test");
edamame22 0:29983394c6b6 65 if(_object) {
edamame22 0:29983394c6b6 66 _object->set_register_uri(true); // The registration message explicitly sends this object path as registered resource such as /Test.
edamame22 0:29983394c6b6 67 }
edamame22 0:29983394c6b6 68 ```
edamame22 0:29983394c6b6 69
edamame22 0:29983394c6b6 70 Here, we discuss a few of the most important parameters, which you must configure properly to work with the Objects.
edamame22 0:29983394c6b6 71
edamame22 0:29983394c6b6 72 ##### Setting Operation Mode
edamame22 0:29983394c6b6 73
edamame22 0:29983394c6b6 74 To set the operation mode of the Objects so that they can handle `GET`, `PUT`, `POST`, `DELETE` or a combination of these requests coming from mbed Device Server:
edamame22 0:29983394c6b6 75
edamame22 0:29983394c6b6 76 ```
edamame22 0:29983394c6b6 77 virtual void set_operation(M2MBase::Operation operation);
edamame22 0:29983394c6b6 78 _object->set_operation(M2MBase::GET_PUT_POST_ALLOWED); // This defines the REST operations that can be performed on this object.
edamame22 0:29983394c6b6 79 ```
edamame22 0:29983394c6b6 80
edamame22 0:29983394c6b6 81 ##### Setting Observable Mode
edamame22 0:29983394c6b6 82
edamame22 0:29983394c6b6 83 To set the Object to be an observable resource:
edamame22 0:29983394c6b6 84
edamame22 0:29983394c6b6 85 `virtual void set_observable(bool observable);`
edamame22 0:29983394c6b6 86
edamame22 0:29983394c6b6 87 By default, all the created objects are non-observable. You can set them to be observable or not observable using this API.
edamame22 0:29983394c6b6 88
edamame22 0:29983394c6b6 89 ```
edamame22 0:29983394c6b6 90 _object->set_observable(true); // The object can be observed from server.
edamame22 0:29983394c6b6 91 _object->set_observable(false); // The object cannot be observed from server.
edamame22 0:29983394c6b6 92 ```
edamame22 0:29983394c6b6 93
edamame22 0:29983394c6b6 94 ##### Setting CoAP content type
edamame22 0:29983394c6b6 95
edamame22 0:29983394c6b6 96 Currently, the only available content type is the OMA TLV type. The OMA TLV type works only for Objects with a numeric value. For example, if you are creating a custom Object it must be of a numeric type such as `100`.
edamame22 0:29983394c6b6 97
edamame22 0:29983394c6b6 98 `M2MObject * _object = M2MInterfaceFactory::create_object("100");`
edamame22 0:29983394c6b6 99
edamame22 0:29983394c6b6 100 By default, all the numeric objects are assigned the CoAP content type of `99` by mbed Client but if you want your Object to assign any other CoAP content type, for example 120, you can do that by setting the object's CoAP content type as shown below:
edamame22 0:29983394c6b6 101
edamame22 0:29983394c6b6 102 ```
edamame22 0:29983394c6b6 103 virtual void set_coap_content_type(const uint8_t content_type);
edamame22 0:29983394c6b6 104 _object->set_coap_content_type(120);
edamame22 0:29983394c6b6 105 ```
edamame22 0:29983394c6b6 106
edamame22 0:29983394c6b6 107 <span class="tips">**Tip:** In future releases, we will introduce support for the JSON content types.</span>
edamame22 0:29983394c6b6 108
edamame22 0:29983394c6b6 109 ## How to create and configure Object Instances
edamame22 0:29983394c6b6 110
edamame22 0:29983394c6b6 111 With this API, you can create and configure the following Object Instances:
edamame22 0:29983394c6b6 112
edamame22 0:29983394c6b6 113 - [Device Object Instance](#device-object-instance)
edamame22 0:29983394c6b6 114 - [Security Object Instance](#security-object-instance)
edamame22 0:29983394c6b6 115 - [Custom Object Instance](#custom-object-instance)
edamame22 0:29983394c6b6 116
edamame22 0:29983394c6b6 117 The `M2MObjectInstance` class is derived from the `M2MBase` class, so all the public methods from `M2MObjectInstance` and its derived classes can be used.
edamame22 0:29983394c6b6 118
edamame22 0:29983394c6b6 119 ### Creating an OMA-defined Object Instance
edamame22 0:29983394c6b6 120
edamame22 0:29983394c6b6 121 #### Device Object Instance
edamame22 0:29983394c6b6 122
edamame22 0:29983394c6b6 123 Because there can only be one instance for the Device Object, the Object Instance is automatically created when creating an M2MDevice Object.
edamame22 0:29983394c6b6 124
edamame22 0:29983394c6b6 125 #### Security Object Instance
edamame22 0:29983394c6b6 126
edamame22 0:29983394c6b6 127 Because there can only be one instance for the Security Object, the Object Instance is automatically created when creating an M2MDevice Object based on a selected server type.
edamame22 0:29983394c6b6 128
edamame22 0:29983394c6b6 129 #### Custom Object Instances
edamame22 0:29983394c6b6 130
edamame22 0:29983394c6b6 131 Object Instances need IDs. Normally, the IDs start from '0' and increment,
edamame22 0:29983394c6b6 132 so the Object Instance structure on the mbed Device Server side would look like this:
edamame22 0:29983394c6b6 133
edamame22 0:29983394c6b6 134 `Object/Object Instance ID`
edamame22 0:29983394c6b6 135
edamame22 0:29983394c6b6 136 When you create an Object Instance, you therefore need to pass the ID that you would like to create. For example, if you pass `0`, you get an object `/Test/0` on mbed Device Server.
edamame22 0:29983394c6b6 137
edamame22 0:29983394c6b6 138 To create an M2M Object Instance:
edamame22 0:29983394c6b6 139
edamame22 0:29983394c6b6 140 `M2MObject::create_object_instance(uint16_t instance_id);`
edamame22 0:29983394c6b6 141
edamame22 0:29983394c6b6 142 You can pass an Object Instance ID to create appropriate Object Instances. Normally, Object Instances will start from `0` and increment. So the Object Instance structure on the mbed Device Server side would look like this:
edamame22 0:29983394c6b6 143
edamame22 0:29983394c6b6 144 `Object/Object Instance ID`
edamame22 0:29983394c6b6 145
edamame22 0:29983394c6b6 146 ```
edamame22 0:29983394c6b6 147 M2MObjectInstance * object_instance = _object->create_object_instance(0);
edamame22 0:29983394c6b6 148 if(object_instance) {
edamame22 0:29983394c6b6 149 object_instance->set_register_uri(true); // The registration message explicitly sends this object instance path as registered resource such as /Test/0.
edamame22 0:29983394c6b6 150 }
edamame22 0:29983394c6b6 151 ```
edamame22 0:29983394c6b6 152
edamame22 0:29983394c6b6 153 ### Configuring the Object Instance
edamame22 0:29983394c6b6 154
edamame22 0:29983394c6b6 155 When you have created an Object Instance (whether OMA-specific or custom), you can configure various parameters in it to control or modify communication with mbed Device Server.
edamame22 0:29983394c6b6 156
edamame22 0:29983394c6b6 157 Here, we present a few of the most important parameters that you must configure properly to work with the object instances.
edamame22 0:29983394c6b6 158
edamame22 0:29983394c6b6 159 ##### Setting Operation Mode
edamame22 0:29983394c6b6 160
edamame22 0:29983394c6b6 161 To set the operation mode of the Objects so that they can handle `GET`, `PUT`, `POST`, `DELETE` or a combination of these requests coming from mbed Device Server:
edamame22 0:29983394c6b6 162
edamame22 0:29983394c6b6 163 ```
edamame22 0:29983394c6b6 164 virtual void set_operation(M2MBase::Operation operation);
edamame22 0:29983394c6b6 165 object_instance->set_operation(M2MBase::GET_PUT_POST_ALLOWED); // This defines the REST operations that can be performed on this object instance.
edamame22 0:29983394c6b6 166 ```
edamame22 0:29983394c6b6 167
edamame22 0:29983394c6b6 168 ##### Setting Observable Mode
edamame22 0:29983394c6b6 169
edamame22 0:29983394c6b6 170 To set the Object Instance to be an observing resource:
edamame22 0:29983394c6b6 171
edamame22 0:29983394c6b6 172 `virtual void set_observable(bool observable);`
edamame22 0:29983394c6b6 173
edamame22 0:29983394c6b6 174 By default, all the created Object Instances are non-observable. You can set them to be observable or not observable using this API.
edamame22 0:29983394c6b6 175
edamame22 0:29983394c6b6 176 ```
edamame22 0:29983394c6b6 177 _object_instance->set_observable(true); // This defines that the Object Instance can be observed from server.
edamame22 0:29983394c6b6 178 _object_instance->set_observable(false); // This defines that the Object Instance cannot be observed from server.
edamame22 0:29983394c6b6 179 ```
edamame22 0:29983394c6b6 180
edamame22 0:29983394c6b6 181 ##### Setting CoAP content type
edamame22 0:29983394c6b6 182
edamame22 0:29983394c6b6 183 Currently, the only available content type is the OMA TLV type. The OMA TLV type works only for Objects and Object Instances with a numeric value. For example, if you are creating a custom Object and Object Instances it must be of a numeric type such as `100`.
edamame22 0:29983394c6b6 184
edamame22 0:29983394c6b6 185 ```
edamame22 0:29983394c6b6 186 M2MObject * _object = M2MInterfaceFactory::create_object("100");
edamame22 0:29983394c6b6 187 M2MObjectInstance * object_instance = _object->create_object_instance(0);
edamame22 0:29983394c6b6 188 ```
edamame22 0:29983394c6b6 189
edamame22 0:29983394c6b6 190 By default, all numeric objects are assigned the CoAP content type of `99` by mbed Client but if you want your Object Instance to assign any other CoAP content type, for example 120, you can do that by setting the object's CoAP content type as follows:
edamame22 0:29983394c6b6 191
edamame22 0:29983394c6b6 192 ```
edamame22 0:29983394c6b6 193 virtual void set_coap_content_type(const uint8_t content_type);
edamame22 0:29983394c6b6 194 object_instance->set_coap_content_type(120);
edamame22 0:29983394c6b6 195 ```
edamame22 0:29983394c6b6 196
edamame22 0:29983394c6b6 197 <span class="tips">**Tip:** In future version, we will introduce support for the JSON content types.</span>
edamame22 0:29983394c6b6 198
edamame22 0:29983394c6b6 199 There are additional APIs that provide getter and remove functions for Object Instances in the `M2MObjectInstance` class; [check the API documentation](https://docs.mbed.com/docs/mbed-client-guide/en/latest/api/classM2MObjectInstance.html) for their usage.
edamame22 0:29983394c6b6 200
edamame22 0:29983394c6b6 201 ## How to create and configure Resources and Resource Instances
edamame22 0:29983394c6b6 202
edamame22 0:29983394c6b6 203 With this API, you can create and configure the following Resources and Resource Instances:
edamame22 0:29983394c6b6 204
edamame22 0:29983394c6b6 205 - [Device Object Resources](#device-object-resources)
edamame22 0:29983394c6b6 206 - [Security Object Resources](#security-object-resources)
edamame22 0:29983394c6b6 207 - [Custom Resources](#custom-resources)
edamame22 0:29983394c6b6 208
edamame22 0:29983394c6b6 209
edamame22 0:29983394c6b6 210 The `M2MResource` class is derived from the `M2MResourceInstance`, which in turn is derived from the `M2MBase` class, so all the public methods can be used from `M2MResource` or `M2MResourceInstance` and their derived classes.
edamame22 0:29983394c6b6 211
edamame22 0:29983394c6b6 212 ### Creating OMA-defined Resources
edamame22 0:29983394c6b6 213
edamame22 0:29983394c6b6 214 #### Device Object Resources
edamame22 0:29983394c6b6 215
edamame22 0:29983394c6b6 216 There are direct APIs to create and set values for the Device Resources. You can create the required Resource and set values based on their data types.
edamame22 0:29983394c6b6 217
edamame22 0:29983394c6b6 218 - For Resources that take string values:
edamame22 0:29983394c6b6 219
edamame22 0:29983394c6b6 220 `M2MResource* create_resource(DeviceResource resource, const String &value);`
edamame22 0:29983394c6b6 221
edamame22 0:29983394c6b6 222 - For Resources that take integer values:
edamame22 0:29983394c6b6 223
edamame22 0:29983394c6b6 224 `M2MResource* create_resource(DeviceResource resource, uint32_t value);`
edamame22 0:29983394c6b6 225
edamame22 0:29983394c6b6 226 - There are a few Resources that can have multiple instances. To create these Resources:
edamame22 0:29983394c6b6 227
edamame22 0:29983394c6b6 228 `M2MResourceInstance* create_resource_instance(DeviceResource resource, uint32_t value,uint16_t instance_id);`
edamame22 0:29983394c6b6 229
edamame22 0:29983394c6b6 230 Where `instance_id` is the Resource Instance ID, for example `/3/0/11/0`.
edamame22 0:29983394c6b6 231
edamame22 0:29983394c6b6 232 Check the [M2MDevice API documentation](https://docs.mbed.com/docs/mbed-client-guide/en/latest/api/classM2MDevice.html) to find which enums are supported for `integer` or `string` value types.
edamame22 0:29983394c6b6 233
edamame22 0:29983394c6b6 234 There are other APIs in the `M2MDevice` class that you can use to set, remove and modify new values for the resources.
edamame22 0:29983394c6b6 235
edamame22 0:29983394c6b6 236 #### Security Object Resources
edamame22 0:29983394c6b6 237
edamame22 0:29983394c6b6 238 _Mandatory Resources_
edamame22 0:29983394c6b6 239
edamame22 0:29983394c6b6 240 Most of the mandatory Resources are created automatically when you create an M2MSecurity Object. You can set their values based on their data types.
edamame22 0:29983394c6b6 241
edamame22 0:29983394c6b6 242 Resources that are automatically created and their accepted data types:
edamame22 0:29983394c6b6 243
edamame22 0:29983394c6b6 244 - `SecurityMode`
edamame22 0:29983394c6b6 245 - `ShortServerID`
edamame22 0:29983394c6b6 246 - `M2MServerUri`
edamame22 0:29983394c6b6 247 - `BootstrapServer`
edamame22 0:29983394c6b6 248 - `PublicKey`
edamame22 0:29983394c6b6 249 - `ServerPublicKey`
edamame22 0:29983394c6b6 250 - `Secretkey`
edamame22 0:29983394c6b6 251
edamame22 0:29983394c6b6 252 For Resources (`SecurityMode`, `ShortServerID`) that take integer values, you can set the values as follows:
edamame22 0:29983394c6b6 253
edamame22 0:29983394c6b6 254 `bool set_resource_value(SecurityResource resource,uint32_t value);`
edamame22 0:29983394c6b6 255
edamame22 0:29983394c6b6 256 ```
edamame22 0:29983394c6b6 257 security_object->set_resource_value(M2MSecurity::SecurityMode, 1);
edamame22 0:29983394c6b6 258 security_object->set_resource_value(M2MSecurity::ShortServerID, 1);
edamame22 0:29983394c6b6 259 ```
edamame22 0:29983394c6b6 260
edamame22 0:29983394c6b6 261 For Resources (`M2MServerUri`) that take string values, you can set the values as follows:
edamame22 0:29983394c6b6 262
edamame22 0:29983394c6b6 263 `bool set_resource_value(SecurityResource resource,const String &value);`
edamame22 0:29983394c6b6 264
edamame22 0:29983394c6b6 265 ```
edamame22 0:29983394c6b6 266 security_object->set_resource_value(M2MSecurity::M2MServerUri, "coap://api.connector.mbed.com:5684");
edamame22 0:29983394c6b6 267 ```
edamame22 0:29983394c6b6 268
edamame22 0:29983394c6b6 269 For Resources (`PublicKey`, `ServerPublicKey`, `Secretkey`) that take binary values, you can set the values as follows:
edamame22 0:29983394c6b6 270
edamame22 0:29983394c6b6 271 `bool set_resource_value(SecurityResource resource,onst uint8_t *value,const uint16_t length);`
edamame22 0:29983394c6b6 272
edamame22 0:29983394c6b6 273 ```
edamame22 0:29983394c6b6 274 uint8_t key[] = {"key"};
edamame22 0:29983394c6b6 275 security_object->set_resource_value(M2MSecurity::PublicKey, key, sizeof(key));
edamame22 0:29983394c6b6 276 ```
edamame22 0:29983394c6b6 277
edamame22 0:29983394c6b6 278 _Optional Resources_
edamame22 0:29983394c6b6 279
edamame22 0:29983394c6b6 280 Optional Resources as defined in the Security Object:
edamame22 0:29983394c6b6 281
edamame22 0:29983394c6b6 282 - `SMSSecurityMode`
edamame22 0:29983394c6b6 283 - `M2MServerSMSNumber`
edamame22 0:29983394c6b6 284 - `ClientHoldOffTime`
edamame22 0:29983394c6b6 285
edamame22 0:29983394c6b6 286 To create and set values for the optional Resources that take an integer value:
edamame22 0:29983394c6b6 287
edamame22 0:29983394c6b6 288 `M2MResource* create_resource(SecurityResource resource, uint32_t value);`
edamame22 0:29983394c6b6 289
edamame22 0:29983394c6b6 290 `security_object->create_resource(M2MSecurity::M2MServerSMSNumber, 123542323);`
edamame22 0:29983394c6b6 291
edamame22 0:29983394c6b6 292
edamame22 0:29983394c6b6 293 Check the [M2MSecurity API documentation](https://docs.mbed.com/docs/mbed-client-guide/en/latest/api/classM2MSecurity.html) to find which enums are supported for `integer`, `string` or `uint8_t*` value types.
edamame22 0:29983394c6b6 294
edamame22 0:29983394c6b6 295 There are more APIs in the `M2MSecurity` class that you can use to set, remove and modify Resource values.
edamame22 0:29983394c6b6 296
edamame22 0:29983394c6b6 297 #### Custom Resources
edamame22 0:29983394c6b6 298
edamame22 0:29983394c6b6 299 For Custom Objects, you can create Resources of two types:
edamame22 0:29983394c6b6 300
edamame22 0:29983394c6b6 301 - **M2MResource**: a resource with a single instance, for example `/Test/0/Resource`.
edamame22 0:29983394c6b6 302
edamame22 0:29983394c6b6 303 - **M2MResourceInstance**: a resource with multiple instances, for example `/Test/0/Resource/0`, `/Test/0/Resource/1`.
edamame22 0:29983394c6b6 304
edamame22 0:29983394c6b6 305 For each of these types, the Resource and Resource Instances can be either static or dynamic:
edamame22 0:29983394c6b6 306
edamame22 0:29983394c6b6 307 - **Static**: Resource and Resource Instances whose value does not change over time, these are not observable.
edamame22 0:29983394c6b6 308 - **Dynamic**: Resource and Resource Instances whose value can change. These can be made observable.
edamame22 0:29983394c6b6 309
edamame22 0:29983394c6b6 310 **Creating dynamic and static single-instance Resources**
edamame22 0:29983394c6b6 311
edamame22 0:29983394c6b6 312 - To create a single-instance Resource with a static value (`/Test/0/Resource`):
edamame22 0:29983394c6b6 313
edamame22 0:29983394c6b6 314 ```
edamame22 0:29983394c6b6 315 M2MObject * object = M2MInterfaceFactory::create_object("Test");
edamame22 0:29983394c6b6 316 M2MObjectInstance * object_instance = object->create_object_instance(0);
edamame22 0:29983394c6b6 317
edamame22 0:29983394c6b6 318 uint8_t value[] ={"value"};
edamame22 0:29983394c6b6 319 M2MResource* resource = object_instance->create_static_resource("Resource", "sensor",M2MResourceInstance::INTEGER,value,sizeof(value),false);
edamame22 0:29983394c6b6 320 ```
edamame22 0:29983394c6b6 321
edamame22 0:29983394c6b6 322 - To create an observable single-instance Resource (`/Test/0/Resource`) with a dynamic value that can be set later on:
edamame22 0:29983394c6b6 323
edamame22 0:29983394c6b6 324 ```
edamame22 0:29983394c6b6 325 M2MObject * object = M2MInterfaceFactory::create_object("Test");
edamame22 0:29983394c6b6 326 M2MObjectInstance * object_instance = object->create_object_instance(0);
edamame22 0:29983394c6b6 327
edamame22 0:29983394c6b6 328 M2MResource* resource = object_instance->create_dynamic_resource("Resource", "sensor",M2MResourceInstance::INTEGER, true, false);
edamame22 0:29983394c6b6 329 int64_t value = 1000;
edamame22 0:29983394c6b6 330 resource->set_value(value);
edamame22 0:29983394c6b6 331 ```
edamame22 0:29983394c6b6 332
edamame22 0:29983394c6b6 333 **Creating dynamic and static Resource Instances**
edamame22 0:29983394c6b6 334
edamame22 0:29983394c6b6 335 - To create a Resource Instance (`/Test/0/Resource/0`) with a static value:
edamame22 0:29983394c6b6 336
edamame22 0:29983394c6b6 337 ```
edamame22 0:29983394c6b6 338 M2MObject * object = M2MInterfaceFactory::create_object("Test");
edamame22 0:29983394c6b6 339 M2MObjectInstance * object_instance = object->create_object_instance(0);
edamame22 0:29983394c6b6 340
edamame22 0:29983394c6b6 341 uint8_t value[] ={"value"};
edamame22 0:29983394c6b6 342 M2MResourceInstance* resource_instance = object_instance->create_static_resource_instance("Resource", "sensor",M2MResourceInstance::INTEGER,value,sizeof(value),0);
edamame22 0:29983394c6b6 343 ```
edamame22 0:29983394c6b6 344
edamame22 0:29983394c6b6 345
edamame22 0:29983394c6b6 346 - To create an observable Resource Instance (`/Test/0/Resource/0`) with a dynamic value that can be set later on:
edamame22 0:29983394c6b6 347
edamame22 0:29983394c6b6 348 ```
edamame22 0:29983394c6b6 349 M2MObject * object = M2MInterfaceFactory::create_object("Test");
edamame22 0:29983394c6b6 350 M2MObjectInstance * object_instance = object->create_object_instance(0);
edamame22 0:29983394c6b6 351
edamame22 0:29983394c6b6 352 uint8_t value[] ={"value"};
edamame22 0:29983394c6b6 353 M2MResourceInstance* resource_instance = object_instance->create_dynamic_resource_instance("Resource", "sensor",M2MResourceInstance::INTEGER, true, 0);
edamame22 0:29983394c6b6 354 int64_t value = 1000;
edamame22 0:29983394c6b6 355 resource_instance->set_value(value);
edamame22 0:29983394c6b6 356
edamame22 0:29983394c6b6 357 ```
edamame22 0:29983394c6b6 358
edamame22 0:29983394c6b6 359 #### Configuring the Resource and Resource Instance
edamame22 0:29983394c6b6 360
edamame22 0:29983394c6b6 361 When you have created a Resource or Resource Instance (whether OMA-specific or custom), you can configure various parameters to control or modify communication with mbed Device Server.
edamame22 0:29983394c6b6 362
edamame22 0:29983394c6b6 363 Here, we present a few of the most important parameters that you must configure properly to work with the Resource and Resource Instance.
edamame22 0:29983394c6b6 364
edamame22 0:29983394c6b6 365 ##### Setting Operation Mode
edamame22 0:29983394c6b6 366
edamame22 0:29983394c6b6 367 You can set the Resource or Resource Instance operation mode so that they can handle `GET`, `PUT`, `POST`, `DELETE` or a combination of these requests coming from mbed Device Server.
edamame22 0:29983394c6b6 368
edamame22 0:29983394c6b6 369 To set the operation mode:
edamame22 0:29983394c6b6 370
edamame22 0:29983394c6b6 371 ```
edamame22 0:29983394c6b6 372 virtual void set_operation(M2MBase::Operation operation);
edamame22 0:29983394c6b6 373 resource->set_operation(M2MBase::GET_PUT_POST_ALLOWED); // This defines the REST operations that can be performed on this Resource.
edamame22 0:29983394c6b6 374 resource_instance->set_operation(M2MBase::GET_PUT_POST_ALLOWED); // This defines the REST operations that can be performed on this Resource Instance.
edamame22 0:29983394c6b6 375 ```
edamame22 0:29983394c6b6 376
edamame22 0:29983394c6b6 377 ##### Setting Observable Mode
edamame22 0:29983394c6b6 378
edamame22 0:29983394c6b6 379 To set the Resource or Resource Instance to be an observable resource:
edamame22 0:29983394c6b6 380
edamame22 0:29983394c6b6 381 `virtual void set_observable(bool observable);`
edamame22 0:29983394c6b6 382
edamame22 0:29983394c6b6 383 By default, all created static Resources or Resource Instances are non-observable. While creating a dynamic Resource or Resource Instance you can specify through API whether they are observable or not. You can change that later as well as follows:
edamame22 0:29983394c6b6 384
edamame22 0:29983394c6b6 385 ```
edamame22 0:29983394c6b6 386 resource->set_observable(true); // This defines that the Resource or Resource Instance can be observed from server.
edamame22 0:29983394c6b6 387 resource->set_observable(false); // This defines that the Resource or Resource Instance cannot be observed from server.
edamame22 0:29983394c6b6 388 ```
edamame22 0:29983394c6b6 389
edamame22 0:29983394c6b6 390 ##### Setting the value of a dynamic Resource or Resource Instance
edamame22 0:29983394c6b6 391
edamame22 0:29983394c6b6 392 You can set the value of a dynamic Resource or Resource Instance so that they can be sent to mbed Device Server using `GET` requests.
edamame22 0:29983394c6b6 393
edamame22 0:29983394c6b6 394 To set the values:
edamame22 0:29983394c6b6 395
edamame22 0:29983394c6b6 396 ```
edamame22 0:29983394c6b6 397 virtual bool set_value(const uint8_t *value, const uint32_t value_length);
edamame22 0:29983394c6b6 398 uint8_t value[] = {"value"};
edamame22 0:29983394c6b6 399 resource->set_value(value,sizeof(value));
edamame22 0:29983394c6b6 400 ```
edamame22 0:29983394c6b6 401
edamame22 0:29983394c6b6 402 ##### Setting an executable function
edamame22 0:29983394c6b6 403
edamame22 0:29983394c6b6 404 For dynamic Resources, you can pass a function pointer to the Resource or Resource Instance. It will be executed when mbed Device Server calls a `POST` method on that resource. The Resource or Resource Instance must support the `POST` operation mode for this feature to work.
edamame22 0:29983394c6b6 405
edamame22 0:29983394c6b6 406 To pass the function pointer:
edamame22 0:29983394c6b6 407
edamame22 0:29983394c6b6 408 ```
edamame22 0:29983394c6b6 409 virtual void set_execute_function(execute_callback callback);
edamame22 0:29983394c6b6 410 void execute_function_example(void *) {
edamame22 0:29983394c6b6 411 // Code
edamame22 0:29983394c6b6 412 };
edamame22 0:29983394c6b6 413 resource->set_execute_function(execute_callback(this,&execute_function_example));
edamame22 0:29983394c6b6 414 ```
edamame22 0:29983394c6b6 415
edamame22 0:29983394c6b6 416 In case execute callback function is defined as a global function and it's outside of your class scope you can use overloaded set_execute_function:
edamame22 0:29983394c6b6 417 ```
edamame22 0:29983394c6b6 418 virtual void set_execute_function(execute_callback_2 callback);
edamame22 0:29983394c6b6 419 static void c_style_function(void *) {
edamame22 0:29983394c6b6 420 // Code
edamame22 0:29983394c6b6 421 }
edamame22 0:29983394c6b6 422 resource->set_execute_function(&c_style_function);
edamame22 0:29983394c6b6 423 ```
edamame22 0:29983394c6b6 424 There are additional APIs that provide getter and remove functions for Resource and Resource Instances in the `M2MResource` and `M2MResourceInstance` classes. Check [the API documentation](https://docs.mbed.com/docs/mbed-client-guide/en/latest/api/annotated.html) for their usage.
edamame22 0:29983394c6b6 425
edamame22 0:29983394c6b6 426 ##### Setting an external handler for block-wise messages
edamame22 0:29983394c6b6 427
edamame22 0:29983394c6b6 428 For dynamic Resources, you can pass a function pointer to the Resource Instance. It will be executed when mbed Device Server calls a `PUT` method on that resource with large payload using block-wise operation. The Resource must support the `PUT` operation mode for this feature to work. If the callback is set, the application will be notified for every incoming block-wise message and the message is not stored in mbed Client side anymore. In such case, it is application's responsibility to store each block-wise message and combine them when the last block has arrived.
edamame22 0:29983394c6b6 429
edamame22 0:29983394c6b6 430 <span class="notes">**Note:** Due to a limitation in the mbed-client-c library, GET request can only contain data up to 65KB.</span>
edamame22 0:29983394c6b6 431
edamame22 0:29983394c6b6 432 To pass the function pointer for an incoming block-wise message:
edamame22 0:29983394c6b6 433
edamame22 0:29983394c6b6 434 ```
edamame22 0:29983394c6b6 435 virtual void set_incoming_block_message_callback(incoming_block_message_callback callback);
edamame22 0:29983394c6b6 436 void block_message_received(M2MBlockMessage *argument) {
edamame22 0:29983394c6b6 437 // Code
edamame22 0:29983394c6b6 438 }
edamame22 0:29983394c6b6 439 resource->set_incoming_block_message_callback(incoming_block_message_callback(this, &block_message_received));
edamame22 0:29983394c6b6 440 ```
edamame22 0:29983394c6b6 441
edamame22 0:29983394c6b6 442 To pass the function pointer for an outgoing block-wise message:
edamame22 0:29983394c6b6 443
edamame22 0:29983394c6b6 444 ```
edamame22 0:29983394c6b6 445 virtual void set_outgoing_block_message_callback(outgoing_block_message_callback callback);
edamame22 0:29983394c6b6 446 void block_message_requested(const String& resource, uint8_t *&data, uint32_t &len) {
edamame22 0:29983394c6b6 447 // Code
edamame22 0:29983394c6b6 448 }
edamame22 0:29983394c6b6 449 resource->set_outgoing_block_message_callback(outgoing_block_message_callback(this, &block_message_requested));
edamame22 0:29983394c6b6 450 ```
edamame22 0:29983394c6b6 451
edamame22 0:29983394c6b6 452 Applications can define their own maximum incoming message size in bytes at build time. For mbed OS, create a `mbed_app.json` file in the application level and overwrite the value as described below:
edamame22 0:29983394c6b6 453
edamame22 0:29983394c6b6 454 ```
edamame22 0:29983394c6b6 455 "target_overrides": {
edamame22 0:29983394c6b6 456 "*": {
edamame22 0:29983394c6b6 457 "mbed-client.sn-coap-max-incoming-message-size": 100000
edamame22 0:29983394c6b6 458 }
edamame22 0:29983394c6b6 459
edamame22 0:29983394c6b6 460 ```
edamame22 0:29983394c6b6 461
edamame22 0:29983394c6b6 462 For yotta based builds, you need to create a `config.json` file in the application level:
edamame22 0:29983394c6b6 463
edamame22 0:29983394c6b6 464 ```
edamame22 0:29983394c6b6 465 {
edamame22 0:29983394c6b6 466 "coap_max_incoming_block_message_size": 100000
edamame22 0:29983394c6b6 467 }
edamame22 0:29983394c6b6 468 ```