This is a simple mbed client example demonstrating, registration of a device with mbed Device Connector and reading and writing values as well as deregistering on different Network Interfaces including Ethernet, WiFi, 6LoWPAN ND and Thread respectively.
Fork of mbed-os-example-client by
simpleclient.h@48:bef96b79dec0, 2016-12-20 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Dec 20 14:45:09 2016 +0000
- Revision:
- 48:bef96b79dec0
- Parent:
- 43:90fccf21be20
- Child:
- 57:bb3eca53b307
Use port=0 for randomization
Network stack will randomize the address once it's being set to port 0.
NOTE! Dependency on mbed OS 5.3, nanostack does the port randomization starting with mbedOS 5.3.
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-client
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yogesh Pande |
0:7d5ec759888b | 1 | /* |
Yogesh Pande |
0:7d5ec759888b | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
Yogesh Pande |
0:7d5ec759888b | 3 | * SPDX-License-Identifier: Apache-2.0 |
Yogesh Pande |
0:7d5ec759888b | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
Yogesh Pande |
0:7d5ec759888b | 5 | * not use this file except in compliance with the License. |
Yogesh Pande |
0:7d5ec759888b | 6 | * You may obtain a copy of the License at |
Yogesh Pande |
0:7d5ec759888b | 7 | * |
Yogesh Pande |
0:7d5ec759888b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Yogesh Pande |
0:7d5ec759888b | 9 | * |
Yogesh Pande |
0:7d5ec759888b | 10 | * Unless required by applicable law or agreed to in writing, software |
Yogesh Pande |
0:7d5ec759888b | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
Yogesh Pande |
0:7d5ec759888b | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Yogesh Pande |
0:7d5ec759888b | 13 | * See the License for the specific language governing permissions and |
Yogesh Pande |
0:7d5ec759888b | 14 | * limitations under the License. |
Yogesh Pande |
0:7d5ec759888b | 15 | */ |
Yogesh Pande |
0:7d5ec759888b | 16 | |
Yogesh Pande |
0:7d5ec759888b | 17 | #ifndef __SIMPLECLIENT_H__ |
Yogesh Pande |
0:7d5ec759888b | 18 | #define __SIMPLECLIENT_H__ |
Yogesh Pande |
0:7d5ec759888b | 19 | |
Yogesh Pande |
0:7d5ec759888b | 20 | #include "mbed-client/m2minterfacefactory.h" |
Yogesh Pande |
0:7d5ec759888b | 21 | #include "mbed-client/m2mdevice.h" |
Yogesh Pande |
0:7d5ec759888b | 22 | #include "mbed-client/m2minterfaceobserver.h" |
Yogesh Pande |
0:7d5ec759888b | 23 | #include "mbed-client/m2minterface.h" |
Yogesh Pande |
0:7d5ec759888b | 24 | #include "mbed-client/m2mobject.h" |
Yogesh Pande |
0:7d5ec759888b | 25 | #include "mbed-client/m2mobjectinstance.h" |
Yogesh Pande |
0:7d5ec759888b | 26 | #include "mbed-client/m2mresource.h" |
Yogesh Pande |
0:7d5ec759888b | 27 | #include "mbed-client/m2mconfig.h" |
mbed_official | 21:b88cdeb5b302 | 28 | #include "mbed-client/m2mblockmessage.h" |
Yogesh Pande |
0:7d5ec759888b | 29 | #include "security.h" |
Yogesh Pande |
0:7d5ec759888b | 30 | #include "mbed.h" |
Yogesh Pande |
0:7d5ec759888b | 31 | |
mbed_official | 21:b88cdeb5b302 | 32 | #define ETHERNET 1 |
mbed_official | 21:b88cdeb5b302 | 33 | #define WIFI 2 |
mbed_official | 21:b88cdeb5b302 | 34 | #define MESH_LOWPAN_ND 3 |
mbed_official | 21:b88cdeb5b302 | 35 | #define MESH_THREAD 4 |
mbed_official | 21:b88cdeb5b302 | 36 | #define ATMEL 5 |
mbed_official | 21:b88cdeb5b302 | 37 | #define MCR20 6 |
mbed_official | 21:b88cdeb5b302 | 38 | |
mbed_official | 21:b88cdeb5b302 | 39 | #define STRINGIFY(s) #s |
mbed_official | 21:b88cdeb5b302 | 40 | |
mbed_official | 43:90fccf21be20 | 41 | // Check if using mesh networking, define helper |
mbed_official | 43:90fccf21be20 | 42 | #if MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND |
mbed_official | 43:90fccf21be20 | 43 | #define MESH |
mbed_official | 43:90fccf21be20 | 44 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD |
mbed_official | 43:90fccf21be20 | 45 | #define MESH |
mbed_official | 43:90fccf21be20 | 46 | #endif |
mbed_official | 43:90fccf21be20 | 47 | |
mbed_official | 42:d9a3043214a6 | 48 | #if defined (MESH) || (MBED_CONF_LWIP_IPV6_ENABLED==true) |
mbed_official | 42:d9a3043214a6 | 49 | // Mesh is always IPV6 - also WiFi and ETH can be IPV6 if IPV6 is enabled |
mbed_official | 42:d9a3043214a6 | 50 | M2MInterface::NetworkStack NETWORK_STACK = M2MInterface::LwIP_IPv6; |
mbed_official | 42:d9a3043214a6 | 51 | #else |
mbed_official | 42:d9a3043214a6 | 52 | // Everything else - we assume it's IPv4 |
mbed_official | 42:d9a3043214a6 | 53 | M2MInterface::NetworkStack NETWORK_STACK = M2MInterface::LwIP_IPv4; |
mbed_official | 42:d9a3043214a6 | 54 | #endif |
mbed_official | 21:b88cdeb5b302 | 55 | |
mbed_official | 42:d9a3043214a6 | 56 | //Select binding mode: UDP or TCP -- note - Mesh networking is IPv6 UDP ONLY |
mbed_official | 42:d9a3043214a6 | 57 | #ifdef MESH |
mbed_official | 42:d9a3043214a6 | 58 | M2MInterface::BindingMode SOCKET_MODE = M2MInterface::UDP; |
mbed_official | 42:d9a3043214a6 | 59 | #else |
mbed_official | 42:d9a3043214a6 | 60 | // WiFi or Ethernet supports both - TCP by default to avoid |
mbed_official | 42:d9a3043214a6 | 61 | // NAT problems, but UDP will also work - IF you configure |
mbed_official | 42:d9a3043214a6 | 62 | // your network right. |
mbed_official | 42:d9a3043214a6 | 63 | M2MInterface::BindingMode SOCKET_MODE = M2MInterface::TCP; |
mbed_official | 42:d9a3043214a6 | 64 | #endif |
Yogesh Pande |
0:7d5ec759888b | 65 | |
mbed_official | 21:b88cdeb5b302 | 66 | |
Yogesh Pande |
0:7d5ec759888b | 67 | // MBED_DOMAIN and MBED_ENDPOINT_NAME come |
Yogesh Pande |
0:7d5ec759888b | 68 | // from the security.h file copied from connector.mbed.com |
Yogesh Pande |
0:7d5ec759888b | 69 | |
Yogesh Pande |
0:7d5ec759888b | 70 | struct MbedClientDevice { |
Yogesh Pande |
0:7d5ec759888b | 71 | const char* Manufacturer; |
Yogesh Pande |
0:7d5ec759888b | 72 | const char* Type; |
Yogesh Pande |
0:7d5ec759888b | 73 | const char* ModelNumber; |
Yogesh Pande |
0:7d5ec759888b | 74 | const char* SerialNumber; |
Yogesh Pande |
0:7d5ec759888b | 75 | }; |
Yogesh Pande |
0:7d5ec759888b | 76 | |
Yogesh Pande |
0:7d5ec759888b | 77 | /* |
Yogesh Pande |
0:7d5ec759888b | 78 | * Wrapper for mbed client stack that handles all callbacks, error handling, and |
Yogesh Pande |
0:7d5ec759888b | 79 | * other shenanigans to make the mbed client stack easier to use. |
Yogesh Pande |
0:7d5ec759888b | 80 | * |
Yogesh Pande |
0:7d5ec759888b | 81 | * The end user should only have to care about configuring the parameters at the |
Yogesh Pande |
0:7d5ec759888b | 82 | * top of this file and making sure they add the security.h file correctly. |
Yogesh Pande |
0:7d5ec759888b | 83 | * To add resources you can copy the _TODO__ function and add as many instances as |
Yogesh Pande |
0:7d5ec759888b | 84 | * you want. |
Yogesh Pande |
0:7d5ec759888b | 85 | * |
Yogesh Pande |
0:7d5ec759888b | 86 | */ |
Yogesh Pande |
0:7d5ec759888b | 87 | class MbedClient: public M2MInterfaceObserver { |
Yogesh Pande |
0:7d5ec759888b | 88 | public: |
Yogesh Pande |
0:7d5ec759888b | 89 | |
Yogesh Pande |
0:7d5ec759888b | 90 | // constructor for MbedClient object, initialize private variables |
Yogesh Pande |
0:7d5ec759888b | 91 | MbedClient(struct MbedClientDevice device) { |
Yogesh Pande |
0:7d5ec759888b | 92 | _interface = NULL; |
Yogesh Pande |
0:7d5ec759888b | 93 | _bootstrapped = false; |
Yogesh Pande |
0:7d5ec759888b | 94 | _error = false; |
Yogesh Pande |
0:7d5ec759888b | 95 | _registered = false; |
Yogesh Pande |
0:7d5ec759888b | 96 | _unregistered = false; |
Yogesh Pande |
0:7d5ec759888b | 97 | _register_security = NULL; |
Yogesh Pande |
0:7d5ec759888b | 98 | _value = 0; |
Yogesh Pande |
0:7d5ec759888b | 99 | _object = NULL; |
Yogesh Pande |
0:7d5ec759888b | 100 | _device = device; |
Yogesh Pande |
0:7d5ec759888b | 101 | } |
Yogesh Pande |
0:7d5ec759888b | 102 | |
Yogesh Pande |
0:7d5ec759888b | 103 | // de-constructor for MbedClient object, you can ignore this |
Yogesh Pande |
0:7d5ec759888b | 104 | ~MbedClient() { |
Yogesh Pande |
0:7d5ec759888b | 105 | if(_interface) { |
Yogesh Pande |
0:7d5ec759888b | 106 | delete _interface; |
Yogesh Pande |
0:7d5ec759888b | 107 | } |
Yogesh Pande |
0:7d5ec759888b | 108 | if(_register_security){ |
Yogesh Pande |
0:7d5ec759888b | 109 | delete _register_security; |
Yogesh Pande |
0:7d5ec759888b | 110 | } |
Yogesh Pande |
0:7d5ec759888b | 111 | } |
Yogesh Pande |
0:7d5ec759888b | 112 | |
Yogesh Pande |
0:7d5ec759888b | 113 | // debug printf function |
Yogesh Pande |
0:7d5ec759888b | 114 | void trace_printer(const char* str) { |
Yogesh Pande |
0:7d5ec759888b | 115 | printf("\r\n%s\r\n", str); |
Yogesh Pande |
0:7d5ec759888b | 116 | } |
Yogesh Pande |
0:7d5ec759888b | 117 | |
Yogesh Pande |
0:7d5ec759888b | 118 | /* |
Yogesh Pande |
0:7d5ec759888b | 119 | * Creates M2MInterface using which endpoint can |
Yogesh Pande |
0:7d5ec759888b | 120 | * setup its name, resource type, life time, connection mode, |
Yogesh Pande |
0:7d5ec759888b | 121 | * Currently only LwIPv4 is supported. |
Yogesh Pande |
0:7d5ec759888b | 122 | */ |
Yogesh Pande |
0:7d5ec759888b | 123 | void create_interface(const char *server_address, |
Yogesh Pande |
0:7d5ec759888b | 124 | void *handler=NULL) { |
mbed_official | 32:7b3841243d70 | 125 | // Randomizing listening port for Certificate mode connectivity |
Yogesh Pande |
0:7d5ec759888b | 126 | _server_address = server_address; |
mbed_official | 48:bef96b79dec0 | 127 | uint16_t port = 0; // Network interface will randomize with port 0 |
mbed_official | 21:b88cdeb5b302 | 128 | |
Yogesh Pande |
0:7d5ec759888b | 129 | // create mDS interface object, this is the base object everything else attaches to |
Yogesh Pande |
0:7d5ec759888b | 130 | _interface = M2MInterfaceFactory::create_interface(*this, |
Yogesh Pande |
0:7d5ec759888b | 131 | MBED_ENDPOINT_NAME, // endpoint name string |
Yogesh Pande |
0:7d5ec759888b | 132 | "test", // endpoint type string |
Yogesh Pande |
0:7d5ec759888b | 133 | 100, // lifetime |
Yogesh Pande |
0:7d5ec759888b | 134 | port, // listen port |
Yogesh Pande |
0:7d5ec759888b | 135 | MBED_DOMAIN, // domain string |
Yogesh Pande |
0:7d5ec759888b | 136 | SOCKET_MODE, // binding mode |
mbed_official | 21:b88cdeb5b302 | 137 | NETWORK_STACK, // network stack |
Yogesh Pande |
0:7d5ec759888b | 138 | ""); // context address string |
Yogesh Pande |
0:7d5ec759888b | 139 | const char *binding_mode = (SOCKET_MODE == M2MInterface::UDP) ? "UDP" : "TCP"; |
Yogesh Pande |
0:7d5ec759888b | 140 | printf("\r\nSOCKET_MODE : %s\r\n", binding_mode); |
Yogesh Pande |
0:7d5ec759888b | 141 | printf("Connecting to %s\r\n", server_address); |
Yogesh Pande |
0:7d5ec759888b | 142 | |
Yogesh Pande |
0:7d5ec759888b | 143 | if(_interface) { |
Yogesh Pande |
0:7d5ec759888b | 144 | _interface->set_platform_network_handler(handler); |
Yogesh Pande |
0:7d5ec759888b | 145 | } |
Yogesh Pande |
0:7d5ec759888b | 146 | |
Yogesh Pande |
0:7d5ec759888b | 147 | } |
Yogesh Pande |
0:7d5ec759888b | 148 | |
Yogesh Pande |
0:7d5ec759888b | 149 | /* |
Yogesh Pande |
0:7d5ec759888b | 150 | * check private variable to see if the registration was sucessful or not |
Yogesh Pande |
0:7d5ec759888b | 151 | */ |
Yogesh Pande |
0:7d5ec759888b | 152 | bool register_successful() { |
Yogesh Pande |
0:7d5ec759888b | 153 | return _registered; |
Yogesh Pande |
0:7d5ec759888b | 154 | } |
Yogesh Pande |
0:7d5ec759888b | 155 | |
Yogesh Pande |
0:7d5ec759888b | 156 | /* |
Yogesh Pande |
0:7d5ec759888b | 157 | * check private variable to see if un-registration was sucessful or not |
Yogesh Pande |
0:7d5ec759888b | 158 | */ |
Yogesh Pande |
0:7d5ec759888b | 159 | bool unregister_successful() { |
Yogesh Pande |
0:7d5ec759888b | 160 | return _unregistered; |
Yogesh Pande |
0:7d5ec759888b | 161 | } |
Yogesh Pande |
0:7d5ec759888b | 162 | |
Yogesh Pande |
0:7d5ec759888b | 163 | /* |
Yogesh Pande |
0:7d5ec759888b | 164 | * Creates register server object with mbed device server address and other parameters |
Yogesh Pande |
0:7d5ec759888b | 165 | * required for client to connect to mbed device server. |
Yogesh Pande |
0:7d5ec759888b | 166 | */ |
Yogesh Pande |
0:7d5ec759888b | 167 | M2MSecurity* create_register_object() { |
Yogesh Pande |
0:7d5ec759888b | 168 | // create security object using the interface factory. |
Yogesh Pande |
0:7d5ec759888b | 169 | // this will generate a security ObjectID and ObjectInstance |
Yogesh Pande |
0:7d5ec759888b | 170 | M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer); |
Yogesh Pande |
0:7d5ec759888b | 171 | |
Yogesh Pande |
0:7d5ec759888b | 172 | // make sure security ObjectID/ObjectInstance was created successfully |
Yogesh Pande |
0:7d5ec759888b | 173 | if(security) { |
Yogesh Pande |
0:7d5ec759888b | 174 | // Add ResourceID's and values to the security ObjectID/ObjectInstance |
Yogesh Pande |
0:7d5ec759888b | 175 | security->set_resource_value(M2MSecurity::M2MServerUri, _server_address); |
Yogesh Pande |
0:7d5ec759888b | 176 | security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate); |
Yogesh Pande |
0:7d5ec759888b | 177 | security->set_resource_value(M2MSecurity::ServerPublicKey, SERVER_CERT, sizeof(SERVER_CERT)); |
Yogesh Pande |
0:7d5ec759888b | 178 | security->set_resource_value(M2MSecurity::PublicKey, CERT, sizeof(CERT)); |
Yogesh Pande |
0:7d5ec759888b | 179 | security->set_resource_value(M2MSecurity::Secretkey, KEY, sizeof(KEY)); |
Yogesh Pande |
0:7d5ec759888b | 180 | } |
Yogesh Pande |
0:7d5ec759888b | 181 | return security; |
Yogesh Pande |
0:7d5ec759888b | 182 | } |
Yogesh Pande |
0:7d5ec759888b | 183 | |
Yogesh Pande |
0:7d5ec759888b | 184 | /* |
Yogesh Pande |
0:7d5ec759888b | 185 | * Creates device object which contains mandatory resources linked with |
Yogesh Pande |
0:7d5ec759888b | 186 | * device endpoint. |
Yogesh Pande |
0:7d5ec759888b | 187 | */ |
Yogesh Pande |
0:7d5ec759888b | 188 | M2MDevice* create_device_object() { |
Yogesh Pande |
0:7d5ec759888b | 189 | // create device objectID/ObjectInstance |
Yogesh Pande |
0:7d5ec759888b | 190 | M2MDevice *device = M2MInterfaceFactory::create_device(); |
Yogesh Pande |
0:7d5ec759888b | 191 | // make sure device object was created successfully |
Yogesh Pande |
0:7d5ec759888b | 192 | if(device) { |
Yogesh Pande |
0:7d5ec759888b | 193 | // add resourceID's to device objectID/ObjectInstance |
Yogesh Pande |
0:7d5ec759888b | 194 | device->create_resource(M2MDevice::Manufacturer, _device.Manufacturer); |
Yogesh Pande |
0:7d5ec759888b | 195 | device->create_resource(M2MDevice::DeviceType, _device.Type); |
Yogesh Pande |
0:7d5ec759888b | 196 | device->create_resource(M2MDevice::ModelNumber, _device.ModelNumber); |
Yogesh Pande |
0:7d5ec759888b | 197 | device->create_resource(M2MDevice::SerialNumber, _device.SerialNumber); |
Yogesh Pande |
0:7d5ec759888b | 198 | } |
Yogesh Pande |
0:7d5ec759888b | 199 | return device; |
Yogesh Pande |
0:7d5ec759888b | 200 | } |
Yogesh Pande |
0:7d5ec759888b | 201 | |
Yogesh Pande |
0:7d5ec759888b | 202 | /* |
Yogesh Pande |
0:7d5ec759888b | 203 | * register an object |
Yogesh Pande |
0:7d5ec759888b | 204 | */ |
Yogesh Pande |
0:7d5ec759888b | 205 | void test_register(M2MSecurity *register_object, M2MObjectList object_list){ |
Yogesh Pande |
0:7d5ec759888b | 206 | if(_interface) { |
Yogesh Pande |
0:7d5ec759888b | 207 | // Register function |
Yogesh Pande |
0:7d5ec759888b | 208 | _interface->register_object(register_object, object_list); |
Yogesh Pande |
0:7d5ec759888b | 209 | } |
Yogesh Pande |
0:7d5ec759888b | 210 | } |
Yogesh Pande |
0:7d5ec759888b | 211 | |
Yogesh Pande |
0:7d5ec759888b | 212 | /* |
Yogesh Pande |
0:7d5ec759888b | 213 | * unregister all objects |
Yogesh Pande |
0:7d5ec759888b | 214 | */ |
Yogesh Pande |
0:7d5ec759888b | 215 | void test_unregister() { |
Yogesh Pande |
0:7d5ec759888b | 216 | if(_interface) { |
Yogesh Pande |
0:7d5ec759888b | 217 | // Unregister function |
Yogesh Pande |
0:7d5ec759888b | 218 | _interface->unregister_object(NULL); // NULL will unregister all objects |
Yogesh Pande |
0:7d5ec759888b | 219 | } |
Yogesh Pande |
0:7d5ec759888b | 220 | } |
Yogesh Pande |
0:7d5ec759888b | 221 | |
Yogesh Pande |
0:7d5ec759888b | 222 | //Callback from mbed client stack when the bootstrap |
Yogesh Pande |
0:7d5ec759888b | 223 | // is successful, it returns the mbed Device Server object |
Yogesh Pande |
0:7d5ec759888b | 224 | // which will be used for registering the resources to |
Yogesh Pande |
0:7d5ec759888b | 225 | // mbed Device server. |
Yogesh Pande |
0:7d5ec759888b | 226 | void bootstrap_done(M2MSecurity *server_object){ |
Yogesh Pande |
0:7d5ec759888b | 227 | if(server_object) { |
Yogesh Pande |
0:7d5ec759888b | 228 | _bootstrapped = true; |
Yogesh Pande |
0:7d5ec759888b | 229 | _error = false; |
Yogesh Pande |
0:7d5ec759888b | 230 | trace_printer("Bootstrapped"); |
Yogesh Pande |
0:7d5ec759888b | 231 | } |
Yogesh Pande |
0:7d5ec759888b | 232 | } |
Yogesh Pande |
0:7d5ec759888b | 233 | |
Yogesh Pande |
0:7d5ec759888b | 234 | //Callback from mbed client stack when the registration |
Yogesh Pande |
0:7d5ec759888b | 235 | // is successful, it returns the mbed Device Server object |
Yogesh Pande |
0:7d5ec759888b | 236 | // to which the resources are registered and registered objects. |
Yogesh Pande |
0:7d5ec759888b | 237 | void object_registered(M2MSecurity */*security_object*/, const M2MServer &/*server_object*/){ |
Yogesh Pande |
0:7d5ec759888b | 238 | _registered = true; |
Yogesh Pande |
0:7d5ec759888b | 239 | _unregistered = false; |
Yogesh Pande |
0:7d5ec759888b | 240 | trace_printer("Registered object successfully!"); |
Yogesh Pande |
0:7d5ec759888b | 241 | } |
Yogesh Pande |
0:7d5ec759888b | 242 | |
Yogesh Pande |
0:7d5ec759888b | 243 | //Callback from mbed client stack when the unregistration |
Yogesh Pande |
0:7d5ec759888b | 244 | // is successful, it returns the mbed Device Server object |
Yogesh Pande |
0:7d5ec759888b | 245 | // to which the resources were unregistered. |
Yogesh Pande |
0:7d5ec759888b | 246 | void object_unregistered(M2MSecurity */*server_object*/){ |
Yogesh Pande |
0:7d5ec759888b | 247 | trace_printer("Unregistered Object Successfully"); |
Yogesh Pande |
0:7d5ec759888b | 248 | _unregistered = true; |
mbed_official | 21:b88cdeb5b302 | 249 | _registered = false; |
Yogesh Pande |
0:7d5ec759888b | 250 | } |
Yogesh Pande |
0:7d5ec759888b | 251 | |
Yogesh Pande |
0:7d5ec759888b | 252 | /* |
Yogesh Pande |
0:7d5ec759888b | 253 | * Callback from mbed client stack when registration is updated |
Yogesh Pande |
0:7d5ec759888b | 254 | */ |
Yogesh Pande |
0:7d5ec759888b | 255 | void registration_updated(M2MSecurity */*security_object*/, const M2MServer & /*server_object*/){ |
Yogesh Pande |
0:7d5ec759888b | 256 | /* The registration is updated automatically and frequently by the |
Yogesh Pande |
0:7d5ec759888b | 257 | * mbed client stack. This print statement is turned off because it |
Yogesh Pande |
0:7d5ec759888b | 258 | * tends to happen alot. |
Yogesh Pande |
0:7d5ec759888b | 259 | */ |
Yogesh Pande |
0:7d5ec759888b | 260 | //trace_printer("\r\nRegistration Updated\r\n"); |
Yogesh Pande |
0:7d5ec759888b | 261 | } |
Yogesh Pande |
0:7d5ec759888b | 262 | |
Yogesh Pande |
0:7d5ec759888b | 263 | // Callback from mbed client stack if any error is encountered |
Yogesh Pande |
0:7d5ec759888b | 264 | // during any of the LWM2M operations. Error type is passed in |
Yogesh Pande |
0:7d5ec759888b | 265 | // the callback. |
Yogesh Pande |
0:7d5ec759888b | 266 | void error(M2MInterface::Error error){ |
Yogesh Pande |
0:7d5ec759888b | 267 | _error = true; |
Yogesh Pande |
0:7d5ec759888b | 268 | switch(error){ |
Yogesh Pande |
0:7d5ec759888b | 269 | case M2MInterface::AlreadyExists: |
Yogesh Pande |
0:7d5ec759888b | 270 | trace_printer("[ERROR:] M2MInterface::AlreadyExist"); |
Yogesh Pande |
0:7d5ec759888b | 271 | break; |
Yogesh Pande |
0:7d5ec759888b | 272 | case M2MInterface::BootstrapFailed: |
Yogesh Pande |
0:7d5ec759888b | 273 | trace_printer("[ERROR:] M2MInterface::BootstrapFailed"); |
Yogesh Pande |
0:7d5ec759888b | 274 | break; |
Yogesh Pande |
0:7d5ec759888b | 275 | case M2MInterface::InvalidParameters: |
Yogesh Pande |
0:7d5ec759888b | 276 | trace_printer("[ERROR:] M2MInterface::InvalidParameters"); |
Yogesh Pande |
0:7d5ec759888b | 277 | break; |
Yogesh Pande |
0:7d5ec759888b | 278 | case M2MInterface::NotRegistered: |
Yogesh Pande |
0:7d5ec759888b | 279 | trace_printer("[ERROR:] M2MInterface::NotRegistered"); |
Yogesh Pande |
0:7d5ec759888b | 280 | break; |
Yogesh Pande |
0:7d5ec759888b | 281 | case M2MInterface::Timeout: |
Yogesh Pande |
0:7d5ec759888b | 282 | trace_printer("[ERROR:] M2MInterface::Timeout"); |
Yogesh Pande |
0:7d5ec759888b | 283 | break; |
Yogesh Pande |
0:7d5ec759888b | 284 | case M2MInterface::NetworkError: |
Yogesh Pande |
0:7d5ec759888b | 285 | trace_printer("[ERROR:] M2MInterface::NetworkError"); |
Yogesh Pande |
0:7d5ec759888b | 286 | break; |
Yogesh Pande |
0:7d5ec759888b | 287 | case M2MInterface::ResponseParseFailed: |
Yogesh Pande |
0:7d5ec759888b | 288 | trace_printer("[ERROR:] M2MInterface::ResponseParseFailed"); |
Yogesh Pande |
0:7d5ec759888b | 289 | break; |
Yogesh Pande |
0:7d5ec759888b | 290 | case M2MInterface::UnknownError: |
Yogesh Pande |
0:7d5ec759888b | 291 | trace_printer("[ERROR:] M2MInterface::UnknownError"); |
Yogesh Pande |
0:7d5ec759888b | 292 | break; |
Yogesh Pande |
0:7d5ec759888b | 293 | case M2MInterface::MemoryFail: |
Yogesh Pande |
0:7d5ec759888b | 294 | trace_printer("[ERROR:] M2MInterface::MemoryFail"); |
Yogesh Pande |
0:7d5ec759888b | 295 | break; |
Yogesh Pande |
0:7d5ec759888b | 296 | case M2MInterface::NotAllowed: |
Yogesh Pande |
0:7d5ec759888b | 297 | trace_printer("[ERROR:] M2MInterface::NotAllowed"); |
Yogesh Pande |
0:7d5ec759888b | 298 | break; |
Yogesh Pande |
0:7d5ec759888b | 299 | case M2MInterface::SecureConnectionFailed: |
Yogesh Pande |
0:7d5ec759888b | 300 | trace_printer("[ERROR:] M2MInterface::SecureConnectionFailed"); |
Yogesh Pande |
0:7d5ec759888b | 301 | break; |
Yogesh Pande |
0:7d5ec759888b | 302 | case M2MInterface::DnsResolvingFailed: |
Yogesh Pande |
0:7d5ec759888b | 303 | trace_printer("[ERROR:] M2MInterface::DnsResolvingFailed"); |
Yogesh Pande |
0:7d5ec759888b | 304 | break; |
Yogesh Pande |
0:7d5ec759888b | 305 | |
Yogesh Pande |
0:7d5ec759888b | 306 | default: |
Yogesh Pande |
0:7d5ec759888b | 307 | break; |
Yogesh Pande |
0:7d5ec759888b | 308 | } |
Yogesh Pande |
0:7d5ec759888b | 309 | } |
Yogesh Pande |
0:7d5ec759888b | 310 | |
Yogesh Pande |
0:7d5ec759888b | 311 | /* Callback from mbed client stack if any value has changed |
Yogesh Pande |
0:7d5ec759888b | 312 | * during PUT operation. Object and its type is passed in |
Yogesh Pande |
0:7d5ec759888b | 313 | * the callback. |
Yogesh Pande |
0:7d5ec759888b | 314 | * BaseType enum from m2mbase.h |
Yogesh Pande |
0:7d5ec759888b | 315 | * Object = 0x0, Resource = 0x1, ObjectInstance = 0x2, ResourceInstance = 0x3 |
Yogesh Pande |
0:7d5ec759888b | 316 | */ |
Yogesh Pande |
0:7d5ec759888b | 317 | void value_updated(M2MBase *base, M2MBase::BaseType type) { |
Yogesh Pande |
0:7d5ec759888b | 318 | printf("\r\nPUT Request Received!"); |
Yogesh Pande |
0:7d5ec759888b | 319 | printf("\r\nName :'%s', \r\nType : '%d' (0 for Object, 1 for Resource), \r\nType : '%s'\r\n", |
Yogesh Pande |
0:7d5ec759888b | 320 | base->name().c_str(), |
Yogesh Pande |
0:7d5ec759888b | 321 | type, |
Yogesh Pande |
0:7d5ec759888b | 322 | base->resource_type().c_str() |
Yogesh Pande |
0:7d5ec759888b | 323 | ); |
Yogesh Pande |
0:7d5ec759888b | 324 | } |
Yogesh Pande |
0:7d5ec759888b | 325 | |
Yogesh Pande |
0:7d5ec759888b | 326 | /* |
Yogesh Pande |
0:7d5ec759888b | 327 | * update the registration period |
Yogesh Pande |
0:7d5ec759888b | 328 | */ |
Yogesh Pande |
0:7d5ec759888b | 329 | void test_update_register() { |
Yogesh Pande |
0:7d5ec759888b | 330 | if (_registered) { |
Yogesh Pande |
0:7d5ec759888b | 331 | _interface->update_registration(_register_security, 100); |
Yogesh Pande |
0:7d5ec759888b | 332 | } |
Yogesh Pande |
0:7d5ec759888b | 333 | } |
Yogesh Pande |
0:7d5ec759888b | 334 | |
Yogesh Pande |
0:7d5ec759888b | 335 | /* |
Yogesh Pande |
0:7d5ec759888b | 336 | * manually configure the security object private variable |
Yogesh Pande |
0:7d5ec759888b | 337 | */ |
Yogesh Pande |
0:7d5ec759888b | 338 | void set_register_object(M2MSecurity *register_object) { |
Yogesh Pande |
0:7d5ec759888b | 339 | if (_register_security == NULL) { |
Yogesh Pande |
0:7d5ec759888b | 340 | _register_security = register_object; |
Yogesh Pande |
0:7d5ec759888b | 341 | } |
Yogesh Pande |
0:7d5ec759888b | 342 | } |
Yogesh Pande |
0:7d5ec759888b | 343 | |
Yogesh Pande |
0:7d5ec759888b | 344 | private: |
Yogesh Pande |
0:7d5ec759888b | 345 | |
Yogesh Pande |
0:7d5ec759888b | 346 | /* |
Yogesh Pande |
0:7d5ec759888b | 347 | * Private variables used in class |
Yogesh Pande |
0:7d5ec759888b | 348 | */ |
Yogesh Pande |
0:7d5ec759888b | 349 | M2MInterface *_interface; |
Yogesh Pande |
0:7d5ec759888b | 350 | M2MSecurity *_register_security; |
Yogesh Pande |
0:7d5ec759888b | 351 | M2MObject *_object; |
Yogesh Pande |
0:7d5ec759888b | 352 | volatile bool _bootstrapped; |
Yogesh Pande |
0:7d5ec759888b | 353 | volatile bool _error; |
Yogesh Pande |
0:7d5ec759888b | 354 | volatile bool _registered; |
Yogesh Pande |
0:7d5ec759888b | 355 | volatile bool _unregistered; |
Yogesh Pande |
0:7d5ec759888b | 356 | int _value; |
Yogesh Pande |
0:7d5ec759888b | 357 | struct MbedClientDevice _device; |
Yogesh Pande |
0:7d5ec759888b | 358 | String _server_address; |
Yogesh Pande |
0:7d5ec759888b | 359 | }; |
Yogesh Pande |
0:7d5ec759888b | 360 | |
Yogesh Pande |
0:7d5ec759888b | 361 | #endif // __SIMPLECLIENT_H__ |