![](/media/cache/profiles/debfdc81854eac26ec993b55a659c6e1.jpg.50x50_q85.jpg)
mbed client lightswitch demo
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
mbed-client/docs/Howto.md@11:cada08fc8a70, 2016-06-09 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu Jun 09 17:08:36 2016 +0000
- Revision:
- 11:cada08fc8a70
Commit for public Consumption
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 11:cada08fc8a70 | 1 | # How to use the mbed Client API |
mbedAustin | 11:cada08fc8a70 | 2 | |
mbedAustin | 11:cada08fc8a70 | 3 | This section explains how to create different types of Objects, Object Instances and Resources for the client to comply with the OMA LWM2M specifications. |
mbedAustin | 11:cada08fc8a70 | 4 | |
mbedAustin | 11:cada08fc8a70 | 5 | In order for the client to communicate its resources to the mbed Device Server (mbed DS), it needs to create Objects, Object Instances and Resources. You can do this easily with the Client C++ APIs, where the client can define its resources in a similar structure to the one defined in the LWM2M specification. |
mbedAustin | 11:cada08fc8a70 | 6 | |
mbedAustin | 11:cada08fc8a70 | 7 | This quick guide will explain how you can create and configure these resources using the C++ API. |
mbedAustin | 11:cada08fc8a70 | 8 | |
mbedAustin | 11:cada08fc8a70 | 9 | ## How to create and configure Objects |
mbedAustin | 11:cada08fc8a70 | 10 | |
mbedAustin | 11:cada08fc8a70 | 11 | The M2MObject class is derived from the M2MBase class, so all the public methods can be used from the M2MObject and its derived classes. |
mbedAustin | 11:cada08fc8a70 | 12 | |
mbedAustin | 11:cada08fc8a70 | 13 | ### Creating OMA-defined Objects |
mbedAustin | 11:cada08fc8a70 | 14 | |
mbedAustin | 11:cada08fc8a70 | 15 | #### Device Object |
mbedAustin | 11:cada08fc8a70 | 16 | |
mbedAustin | 11:cada08fc8a70 | 17 | There is a direct API in the M2MInterfaceFactory class to create a Device Object: |
mbedAustin | 11:cada08fc8a70 | 18 | |
mbedAustin | 11:cada08fc8a70 | 19 | `static M2MDevice *create_device();` |
mbedAustin | 11:cada08fc8a70 | 20 | |
mbedAustin | 11:cada08fc8a70 | 21 | Because there can be only one instance of M2MDevice, it is a static class and you can delete it as follows: |
mbedAustin | 11:cada08fc8a70 | 22 | |
mbedAustin | 11:cada08fc8a70 | 23 | `M2MDevice::delete_instance();` |
mbedAustin | 11:cada08fc8a70 | 24 | |
mbedAustin | 11:cada08fc8a70 | 25 | Check the M2MDevice class documentation to see how to configure the device object, as well as how to create appropriate Resources and assign values to them. |
mbedAustin | 11:cada08fc8a70 | 26 | |
mbedAustin | 11:cada08fc8a70 | 27 | #### Security Object |
mbedAustin | 11:cada08fc8a70 | 28 | |
mbedAustin | 11:cada08fc8a70 | 29 | There is a direct API in the M2MInterfaceFactory class to create a Security Object: |
mbedAustin | 11:cada08fc8a70 | 30 | |
mbedAustin | 11:cada08fc8a70 | 31 | `static M2MSecurity *create_security(M2MSecurity::ServerType server_type);` |
mbedAustin | 11:cada08fc8a70 | 32 | |
mbedAustin | 11:cada08fc8a70 | 33 | You can create a Bootstrap or normal mbed Device Server by passing the appropriate `enum` value. |
mbedAustin | 11:cada08fc8a70 | 34 | |
mbedAustin | 11:cada08fc8a70 | 35 | Check the M2MSecurity class documentation to see how to configure the security object, as well as how to create appropriate Resources and assign values to them. |
mbedAustin | 11:cada08fc8a70 | 36 | |
mbedAustin | 11:cada08fc8a70 | 37 | #### Creating a custom Object |
mbedAustin | 11:cada08fc8a70 | 38 | |
mbedAustin | 11:cada08fc8a70 | 39 | As per the OMA LWM2M specification, the client must have defined Objects, under which it can create Object Instances. You can create an M2MObject using this API from the M2MInterfaceFactory class: |
mbedAustin | 11:cada08fc8a70 | 40 | |
mbedAustin | 11:cada08fc8a70 | 41 | `static M2MObject *create_object(const String &name);` |
mbedAustin | 11:cada08fc8a70 | 42 | |
mbedAustin | 11:cada08fc8a70 | 43 | 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 DS. |
mbedAustin | 11:cada08fc8a70 | 44 | |
mbedAustin | 11:cada08fc8a70 | 45 | #### Configuring the Object |
mbedAustin | 11:cada08fc8a70 | 46 | |
mbedAustin | 11:cada08fc8a70 | 47 | Once you have created an Object (whether OMA-specific or custom), you can configure various parameters in that object so that they can be controlled or modified to affect communication with mbed DS. |
mbedAustin | 11:cada08fc8a70 | 48 | |
mbedAustin | 11:cada08fc8a70 | 49 | Here, we discuss a few of the most important parameters, which you must configure properly to work with the objects. |
mbedAustin | 11:cada08fc8a70 | 50 | |
mbedAustin | 11:cada08fc8a70 | 51 | ##### Setting Operation Mode |
mbedAustin | 11:cada08fc8a70 | 52 | |
mbedAustin | 11:cada08fc8a70 | 53 | You can 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 DS. |
mbedAustin | 11:cada08fc8a70 | 54 | |
mbedAustin | 11:cada08fc8a70 | 55 | The API that sets the operation mode (present in the M2MBase class) is: |
mbedAustin | 11:cada08fc8a70 | 56 | |
mbedAustin | 11:cada08fc8a70 | 57 | `virtual void set_operation(M2MBase::Operation operation);` |
mbedAustin | 11:cada08fc8a70 | 58 | |
mbedAustin | 11:cada08fc8a70 | 59 | ##### Setting Observable Mode |
mbedAustin | 11:cada08fc8a70 | 60 | |
mbedAustin | 11:cada08fc8a70 | 61 | You can set the object to be an observing resource. You can set the observable mode using the following API: |
mbedAustin | 11:cada08fc8a70 | 62 | |
mbedAustin | 11:cada08fc8a70 | 63 | `virtual void set_observable(bool observable);` |
mbedAustin | 11:cada08fc8a70 | 64 | |
mbedAustin | 11:cada08fc8a70 | 65 | ##### Setting CoAP content type |
mbedAustin | 11:cada08fc8a70 | 66 | |
mbedAustin | 11:cada08fc8a70 | 67 | 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 like `100`; only then the CoAP TLV type will work for the custom object. |
mbedAustin | 11:cada08fc8a70 | 68 | |
mbedAustin | 11:cada08fc8a70 | 69 | If you want your object to support the TLV type, set the object's CoAP content type as `99`: |
mbedAustin | 11:cada08fc8a70 | 70 | |
mbedAustin | 11:cada08fc8a70 | 71 | `virtual void set_coap_content_type(const uint8_t content_type);` |
mbedAustin | 11:cada08fc8a70 | 72 | |
mbedAustin | 11:cada08fc8a70 | 73 | Later we will introduce support for the JSON content types. |
mbedAustin | 11:cada08fc8a70 | 74 | |
mbedAustin | 11:cada08fc8a70 | 75 | ## How to create and configure Object Instances |
mbedAustin | 11:cada08fc8a70 | 76 | |
mbedAustin | 11:cada08fc8a70 | 77 | The M2MObjectInstance class is derived from the M2MBase class, so all the public methods from M2MObjectInstance and its derived classes can be used. |
mbedAustin | 11:cada08fc8a70 | 78 | |
mbedAustin | 11:cada08fc8a70 | 79 | ### Creating OMA defined ObjectInstance |
mbedAustin | 11:cada08fc8a70 | 80 | |
mbedAustin | 11:cada08fc8a70 | 81 | #### Device ObjectInstance |
mbedAustin | 11:cada08fc8a70 | 82 | |
mbedAustin | 11:cada08fc8a70 | 83 | Because there can only be one instance for the Device Object, the ObjectInstance is automatically created when creating an M2MDevice Object. |
mbedAustin | 11:cada08fc8a70 | 84 | |
mbedAustin | 11:cada08fc8a70 | 85 | #### Security ObjectInstance |
mbedAustin | 11:cada08fc8a70 | 86 | |
mbedAustin | 11:cada08fc8a70 | 87 | Because there can only be one instance for the Device Object, the Object Instance is automatically created when creating an M2MDevice Object based on a selected server type. |
mbedAustin | 11:cada08fc8a70 | 88 | |
mbedAustin | 11:cada08fc8a70 | 89 | #### Creating custom object instances |
mbedAustin | 11:cada08fc8a70 | 90 | |
mbedAustin | 11:cada08fc8a70 | 91 | As per the OMA LWM2M specification, the client must have created Object Instances under Objects, which will eventually contain Resources. |
mbedAustin | 11:cada08fc8a70 | 92 | |
mbedAustin | 11:cada08fc8a70 | 93 | You can create an M2M Object Instance using this API from the M2MObject class: |
mbedAustin | 11:cada08fc8a70 | 94 | |
mbedAustin | 11:cada08fc8a70 | 95 | `M2MObject::create_object_instance(uint16_t instance_id);` |
mbedAustin | 11:cada08fc8a70 | 96 | |
mbedAustin | 11:cada08fc8a70 | 97 | 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 DS side would look like this: |
mbedAustin | 11:cada08fc8a70 | 98 | |
mbedAustin | 11:cada08fc8a70 | 99 | `Object/Object Instance ID` |
mbedAustin | 11:cada08fc8a70 | 100 | |
mbedAustin | 11:cada08fc8a70 | 101 | You need to pass the name of the Object Instance ID that you would like to create (for example `0`); this will create an object `/Test/0` in mbed DS. |
mbedAustin | 11:cada08fc8a70 | 102 | |
mbedAustin | 11:cada08fc8a70 | 103 | ### Configuring the Object Instance |
mbedAustin | 11:cada08fc8a70 | 104 | |
mbedAustin | 11:cada08fc8a70 | 105 | When you have created an Object Instance (whether OMA-specific or custom), you can configure various parameters in that object so that they can be controlled or modified to affect communication with mbed DS. |
mbedAustin | 11:cada08fc8a70 | 106 | |
mbedAustin | 11:cada08fc8a70 | 107 | Here, we present a few of the most important parameters that you must configure properly to work with the object instances. |
mbedAustin | 11:cada08fc8a70 | 108 | |
mbedAustin | 11:cada08fc8a70 | 109 | #### Setting operation mode |
mbedAustin | 11:cada08fc8a70 | 110 | |
mbedAustin | 11:cada08fc8a70 | 111 | You can set the operation mode of the Object Instances so that they can handle `GET`, `PUT`, `POST`, `DELETE` or a combination of these requests coming from mbed DS. |
mbedAustin | 11:cada08fc8a70 | 112 | |
mbedAustin | 11:cada08fc8a70 | 113 | The API that you can use to set the operation mode (present in the M2MBase class) is: |
mbedAustin | 11:cada08fc8a70 | 114 | |
mbedAustin | 11:cada08fc8a70 | 115 | `virtual void set_operation(M2MBase::Operation operation);` |
mbedAustin | 11:cada08fc8a70 | 116 | |
mbedAustin | 11:cada08fc8a70 | 117 | #### Setting observable mode |
mbedAustin | 11:cada08fc8a70 | 118 | |
mbedAustin | 11:cada08fc8a70 | 119 | You can set the object to be an observing resource. |
mbedAustin | 11:cada08fc8a70 | 120 | |
mbedAustin | 11:cada08fc8a70 | 121 | You can set the observable mode using the following API: |
mbedAustin | 11:cada08fc8a70 | 122 | |
mbedAustin | 11:cada08fc8a70 | 123 | `virtual void set_observable(bool observable);` |
mbedAustin | 11:cada08fc8a70 | 124 | |
mbedAustin | 11:cada08fc8a70 | 125 | #### Setting CoAP content type |
mbedAustin | 11:cada08fc8a70 | 126 | |
mbedAustin | 11:cada08fc8a70 | 127 | 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 are creating a custom object it must be of a numeric type like `100`; only then the CoAP TLV type will work for the custom object. |
mbedAustin | 11:cada08fc8a70 | 128 | |
mbedAustin | 11:cada08fc8a70 | 129 | If you want your object to support the TLV type, set the CoAP content type of the object to `99`: |
mbedAustin | 11:cada08fc8a70 | 130 | |
mbedAustin | 11:cada08fc8a70 | 131 | `virtual void set_coap_content_type(const uint8_t content_type);` |
mbedAustin | 11:cada08fc8a70 | 132 | |
mbedAustin | 11:cada08fc8a70 | 133 | Later we will introduce support for the JSON content types. |
mbedAustin | 11:cada08fc8a70 | 134 | |
mbedAustin | 11:cada08fc8a70 | 135 | Apart from this, there are multiple APIs that provide getter and remove functions for Object Instances in the M2MObjectInstance class; check the API documentation for their usage. |
mbedAustin | 11:cada08fc8a70 | 136 | |
mbedAustin | 11:cada08fc8a70 | 137 | ## How to create and configure Resources and Resource Instances |
mbedAustin | 11:cada08fc8a70 | 138 | |
mbedAustin | 11:cada08fc8a70 | 139 | 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. |
mbedAustin | 11:cada08fc8a70 | 140 | |
mbedAustin | 11:cada08fc8a70 | 141 | ### Creating OMA-defined Resources |
mbedAustin | 11:cada08fc8a70 | 142 | |
mbedAustin | 11:cada08fc8a70 | 143 | #### Device Object Resources |
mbedAustin | 11:cada08fc8a70 | 144 | |
mbedAustin | 11:cada08fc8a70 | 145 | 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. |
mbedAustin | 11:cada08fc8a70 | 146 | |
mbedAustin | 11:cada08fc8a70 | 147 | - For Resources that take `string` values: |
mbedAustin | 11:cada08fc8a70 | 148 | |
mbedAustin | 11:cada08fc8a70 | 149 | `M2MResource* create_resource(DeviceResource resource, const String &value);` |
mbedAustin | 11:cada08fc8a70 | 150 | |
mbedAustin | 11:cada08fc8a70 | 151 | - For Resources that take `integer` values: |
mbedAustin | 11:cada08fc8a70 | 152 | |
mbedAustin | 11:cada08fc8a70 | 153 | `M2MResource* create_resource(DeviceResource resource, uint32_t value);` |
mbedAustin | 11:cada08fc8a70 | 154 | |
mbedAustin | 11:cada08fc8a70 | 155 | - There are a few resources that can have multiple instances. To create these resources, use these APIs: |
mbedAustin | 11:cada08fc8a70 | 156 | |
mbedAustin | 11:cada08fc8a70 | 157 | `M2MResourceInstance* create_resource_instance(DeviceResource resource, uint32_t value,uint16_t instance_id);` |
mbedAustin | 11:cada08fc8a70 | 158 | |
mbedAustin | 11:cada08fc8a70 | 159 | Where `instance_id` is the Resource Instance ID, for example `/3/0/11/0`. |
mbedAustin | 11:cada08fc8a70 | 160 | |
mbedAustin | 11:cada08fc8a70 | 161 | Check the M2MDevice API documentation to find which enums are supported for `integer` or `string` value types. |
mbedAustin | 11:cada08fc8a70 | 162 | |
mbedAustin | 11:cada08fc8a70 | 163 | There are other APIs in the M2MDevice class that you can use to set, remove and modify new values for the resources. |
mbedAustin | 11:cada08fc8a70 | 164 | |
mbedAustin | 11:cada08fc8a70 | 165 | #### Security Object Resources |
mbedAustin | 11:cada08fc8a70 | 166 | |
mbedAustin | 11:cada08fc8a70 | 167 | There are direct APIs to create and set values for the Security Resources, based on their data types. |
mbedAustin | 11:cada08fc8a70 | 168 | |
mbedAustin | 11:cada08fc8a70 | 169 | Most of the mandatory resources are created automatically when you create an M2MSecurity object. You can set their values using the following APIs: |
mbedAustin | 11:cada08fc8a70 | 170 | |
mbedAustin | 11:cada08fc8a70 | 171 | - For resources that take `integer` values: |
mbedAustin | 11:cada08fc8a70 | 172 | |
mbedAustin | 11:cada08fc8a70 | 173 | `bool set_resource_value(SecurityResource resource,uint32_t value);` |
mbedAustin | 11:cada08fc8a70 | 174 | |
mbedAustin | 11:cada08fc8a70 | 175 | - For resources that take `string` values: |
mbedAustin | 11:cada08fc8a70 | 176 | |
mbedAustin | 11:cada08fc8a70 | 177 | `bool set_resource_value(SecurityResource resource,const String &value);` |
mbedAustin | 11:cada08fc8a70 | 178 | |
mbedAustin | 11:cada08fc8a70 | 179 | - For resources that take binary values, like setting public keys and certificates: |
mbedAustin | 11:cada08fc8a70 | 180 | |
mbedAustin | 11:cada08fc8a70 | 181 | `bool set_resource_value(SecurityResource resource,onst uint8_t *value,const uint16_t length);` |
mbedAustin | 11:cada08fc8a70 | 182 | |
mbedAustin | 11:cada08fc8a70 | 183 | - You can create and set values for Resources that are not mandatory, and which take an `integer` value, using this API: |
mbedAustin | 11:cada08fc8a70 | 184 | |
mbedAustin | 11:cada08fc8a70 | 185 | `M2MResource* create_resource(SecurityResource resource, uint32_t value);` |
mbedAustin | 11:cada08fc8a70 | 186 | |
mbedAustin | 11:cada08fc8a70 | 187 | Check the M2MSecurity API documentation to find which enums are supported for `integer`, `string` or `uint8_t*` value types. |
mbedAustin | 11:cada08fc8a70 | 188 | |
mbedAustin | 11:cada08fc8a70 | 189 | There are more APIs in the M2MSecurity class that you can use to set, remove and modify Resource values. |
mbedAustin | 11:cada08fc8a70 | 190 | |
mbedAustin | 11:cada08fc8a70 | 191 | #### Creating custom Resources |
mbedAustin | 11:cada08fc8a70 | 192 | |
mbedAustin | 11:cada08fc8a70 | 193 | As per the OMA LWM2M specification, the client must have Resources under Object Instances which belong to Objects. |
mbedAustin | 11:cada08fc8a70 | 194 | |
mbedAustin | 11:cada08fc8a70 | 195 | You can create different types of Resources or Resource Instances. |
mbedAustin | 11:cada08fc8a70 | 196 | |
mbedAustin | 11:cada08fc8a70 | 197 | There are two types of Resources: |
mbedAustin | 11:cada08fc8a70 | 198 | |
mbedAustin | 11:cada08fc8a70 | 199 | - **M2MResource**: a resource with a unique instance, for example `/Test/0/Resource`. |
mbedAustin | 11:cada08fc8a70 | 200 | |
mbedAustin | 11:cada08fc8a70 | 201 | - **M2MResourceInstance**: a resource with multiple instances, for example `/Test/0/Resource/0`, `/Test/0/Resource/1`. |
mbedAustin | 11:cada08fc8a70 | 202 | |
mbedAustin | 11:cada08fc8a70 | 203 | Apart from these two types, you can create two types of Resource and Resource Instances: |
mbedAustin | 11:cada08fc8a70 | 204 | |
mbedAustin | 11:cada08fc8a70 | 205 | - Static: Resource and Resource Instances whose value does not change over time. |
mbedAustin | 11:cada08fc8a70 | 206 | - Dynamic: Resource and Resource Instances whose value can change. These resources can be made observable. |
mbedAustin | 11:cada08fc8a70 | 207 | |
mbedAustin | 11:cada08fc8a70 | 208 | **Creating dynamic and static single-instance Resources** |
mbedAustin | 11:cada08fc8a70 | 209 | |
mbedAustin | 11:cada08fc8a70 | 210 | As per the OMA LWM2M specification, the client must have created Resources under Object Instance. You can create M2MResource from the M2MObjectInstance class. |
mbedAustin | 11:cada08fc8a70 | 211 | |
mbedAustin | 11:cada08fc8a70 | 212 | - You can create a single-instance Resource with a static value using this API: |
mbedAustin | 11:cada08fc8a70 | 213 | |
mbedAustin | 11:cada08fc8a70 | 214 | ``` |
mbedAustin | 11:cada08fc8a70 | 215 | M2MResource* create_static_resource(const String &resource_name, |
mbedAustin | 11:cada08fc8a70 | 216 | const String &resource_type, |
mbedAustin | 11:cada08fc8a70 | 217 | M2MResourceInstance::ResourceType type, |
mbedAustin | 11:cada08fc8a70 | 218 | const uint8_t *value, |
mbedAustin | 11:cada08fc8a70 | 219 | const uint8_t value_length, |
mbedAustin | 11:cada08fc8a70 | 220 | bool multiple_instance = false); |
mbedAustin | 11:cada08fc8a70 | 221 | ``` |
mbedAustin | 11:cada08fc8a70 | 222 | |
mbedAustin | 11:cada08fc8a70 | 223 | - You can create a single-instance Resource with a dynamic value that can be set later on, using this API: |
mbedAustin | 11:cada08fc8a70 | 224 | |
mbedAustin | 11:cada08fc8a70 | 225 | ``` |
mbedAustin | 11:cada08fc8a70 | 226 | M2MResource* create_dynamic_resource(const String &resource_name, |
mbedAustin | 11:cada08fc8a70 | 227 | const String &resource_type, |
mbedAustin | 11:cada08fc8a70 | 228 | M2MResourceInstance::ResourceType type, |
mbedAustin | 11:cada08fc8a70 | 229 | bool observable, |
mbedAustin | 11:cada08fc8a70 | 230 | bool multiple_instance = false); |
mbedAustin | 11:cada08fc8a70 | 231 | ``` |
mbedAustin | 11:cada08fc8a70 | 232 | |
mbedAustin | 11:cada08fc8a70 | 233 | These APIs take different parameters that you can see in the documentation. |
mbedAustin | 11:cada08fc8a70 | 234 | |
mbedAustin | 11:cada08fc8a70 | 235 | **Creating dynamic and static Resource Instances** |
mbedAustin | 11:cada08fc8a70 | 236 | |
mbedAustin | 11:cada08fc8a70 | 237 | As per the OMA LWM2M specification, the client must have created Resource Instances under Resources. You can create M2MResourceInstance from the M2MObjectInstance class. |
mbedAustin | 11:cada08fc8a70 | 238 | |
mbedAustin | 11:cada08fc8a70 | 239 | - You can create a Resource Instance with a static value using this API: |
mbedAustin | 11:cada08fc8a70 | 240 | |
mbedAustin | 11:cada08fc8a70 | 241 | ``` |
mbedAustin | 11:cada08fc8a70 | 242 | M2MResourceInstance* create_static_resource_instance(const String &resource_name, |
mbedAustin | 11:cada08fc8a70 | 243 | const String &resource_type, |
mbedAustin | 11:cada08fc8a70 | 244 | M2MResourceInstance::ResourceType type, |
mbedAustin | 11:cada08fc8a70 | 245 | const uint8_t *value, |
mbedAustin | 11:cada08fc8a70 | 246 | const uint8_t value_length, |
mbedAustin | 11:cada08fc8a70 | 247 | uint16_t instance_id); |
mbedAustin | 11:cada08fc8a70 | 248 | ``` |
mbedAustin | 11:cada08fc8a70 | 249 | |
mbedAustin | 11:cada08fc8a70 | 250 | - You can create a Resource Instance with a dynamic value that can be set later on, using this API: |
mbedAustin | 11:cada08fc8a70 | 251 | |
mbedAustin | 11:cada08fc8a70 | 252 | ``` |
mbedAustin | 11:cada08fc8a70 | 253 | M2MResourceInstance* create_dynamic_resource_instance(const String &resource_name, |
mbedAustin | 11:cada08fc8a70 | 254 | const String &resource_type, |
mbedAustin | 11:cada08fc8a70 | 255 | M2MResourceInstance::ResourceType type, |
mbedAustin | 11:cada08fc8a70 | 256 | bool observable, |
mbedAustin | 11:cada08fc8a70 | 257 | uint16_t instance_id); |
mbedAustin | 11:cada08fc8a70 | 258 | ``` |
mbedAustin | 11:cada08fc8a70 | 259 | |
mbedAustin | 11:cada08fc8a70 | 260 | These APIs take different parameters that you can see in the documentation. |
mbedAustin | 11:cada08fc8a70 | 261 | |
mbedAustin | 11:cada08fc8a70 | 262 | #### Configuring the Resource and Resource Instance |
mbedAustin | 11:cada08fc8a70 | 263 | |
mbedAustin | 11:cada08fc8a70 | 264 | When you have created a Resource or Resource Instance (whether OMA-specific or custom), you can configure various parameters so they can be controlled or modified to affect communication with mbed DS. |
mbedAustin | 11:cada08fc8a70 | 265 | |
mbedAustin | 11:cada08fc8a70 | 266 | Here, we present a few of the most important parameters that you must configure properly to work with the Resource and Resource Instance. |
mbedAustin | 11:cada08fc8a70 | 267 | |
mbedAustin | 11:cada08fc8a70 | 268 | ##### Setting Operation Mode |
mbedAustin | 11:cada08fc8a70 | 269 | |
mbedAustin | 11:cada08fc8a70 | 270 | 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 DS. |
mbedAustin | 11:cada08fc8a70 | 271 | |
mbedAustin | 11:cada08fc8a70 | 272 | The API that you can use to set the operation mode (present in the M2MBase class) is: |
mbedAustin | 11:cada08fc8a70 | 273 | |
mbedAustin | 11:cada08fc8a70 | 274 | `virtual void set_operation(M2MBase::Operation operation);` |
mbedAustin | 11:cada08fc8a70 | 275 | |
mbedAustin | 11:cada08fc8a70 | 276 | ##### Setting the value of a dynamic Resource or Resource Instance |
mbedAustin | 11:cada08fc8a70 | 277 | |
mbedAustin | 11:cada08fc8a70 | 278 | You can set the value of a dynamic Resource or Resource Instance so that they can be sent to mbed DS using `GET` requests. |
mbedAustin | 11:cada08fc8a70 | 279 | |
mbedAustin | 11:cada08fc8a70 | 280 | The API used to set the values (present in the M2MResourceInstance class) is: |
mbedAustin | 11:cada08fc8a70 | 281 | |
mbedAustin | 11:cada08fc8a70 | 282 | `virtual bool set_value(const uint8_t *value, const uint32_t value_length);` |
mbedAustin | 11:cada08fc8a70 | 283 | |
mbedAustin | 11:cada08fc8a70 | 284 | ##### Setting an executable function |
mbedAustin | 11:cada08fc8a70 | 285 | |
mbedAustin | 11:cada08fc8a70 | 286 | For Dynamic Resources, you can pass a function pointer to the Resource or Resource Instance. It will be executed when mbed DS calls a `POST` method on that resource. The Resource or Resource Instance must support the `POST` operation mode for this feature to work. |
mbedAustin | 11:cada08fc8a70 | 287 | |
mbedAustin | 11:cada08fc8a70 | 288 | You can pass the function pointer using this API: |
mbedAustin | 11:cada08fc8a70 | 289 | |
mbedAustin | 11:cada08fc8a70 | 290 | `virtual void set_execute_function(execute_callback callback);` |
mbedAustin | 11:cada08fc8a70 | 291 | |
mbedAustin | 11:cada08fc8a70 | 292 | Apart from this, there are multiple APIs that provide getter and remove functions for Resource and Resource Instances in the M2MResource and M2MResourceInstance classes. Check the API documentation for their usage. |
mbedAustin | 11:cada08fc8a70 | 293 | |
mbedAustin | 11:cada08fc8a70 | 294 | ## API documentation |
mbedAustin | 11:cada08fc8a70 | 295 | |
mbedAustin | 11:cada08fc8a70 | 296 | You can generate Doxygen API documentation for these APIs from a doxy file in the `doxygen` folder. You need to run the `doxygen` command from the `doxygen/` folder; it will generate a `docs` folder at the API source directory root level, where you can find the detailed documentation for each API. |