mbed client on ethernet with LWIP
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
main.cpp@4:dcd0494556be, 2016-02-11 (annotated)
- Committer:
- geky
- Date:
- Thu Feb 11 23:53:32 2016 +0000
- Revision:
- 4:dcd0494556be
- Parent:
- 0:9ad9d701b1c3
- Child:
- 11:cada08fc8a70
Switched to semaphore over conditions for signalling multiple updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
geky | 4:dcd0494556be | 1 | /* |
geky | 4:dcd0494556be | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
geky | 4:dcd0494556be | 3 | * SPDX-License-Identifier: Apache-2.0 |
geky | 4:dcd0494556be | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
geky | 4:dcd0494556be | 5 | * not use this file except in compliance with the License. |
geky | 4:dcd0494556be | 6 | * You may obtain a copy of the License at |
geky | 4:dcd0494556be | 7 | * |
geky | 4:dcd0494556be | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
geky | 4:dcd0494556be | 9 | * |
geky | 4:dcd0494556be | 10 | * Unless required by applicable law or agreed to in writing, software |
geky | 4:dcd0494556be | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
geky | 4:dcd0494556be | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
geky | 4:dcd0494556be | 13 | * See the License for the specific language governing permissions and |
geky | 4:dcd0494556be | 14 | * limitations under the License. |
geky | 4:dcd0494556be | 15 | */ |
geky | 4:dcd0494556be | 16 | #include "EthernetInterface.h" |
geky | 4:dcd0494556be | 17 | #include "mbed-client/m2minterfacefactory.h" |
geky | 4:dcd0494556be | 18 | #include "mbed-client/m2mdevice.h" |
geky | 4:dcd0494556be | 19 | #include "mbed-client/m2minterfaceobserver.h" |
geky | 4:dcd0494556be | 20 | #include "mbed-client/m2minterface.h" |
geky | 4:dcd0494556be | 21 | #include "mbed-client/m2mobjectinstance.h" |
geky | 4:dcd0494556be | 22 | #include "mbed-client/m2mresource.h" |
geky | 4:dcd0494556be | 23 | #include "security.h" |
geky | 4:dcd0494556be | 24 | #include "ns_trace.h" |
geky | 4:dcd0494556be | 25 | |
geky | 4:dcd0494556be | 26 | #include "mbed.h" |
geky | 4:dcd0494556be | 27 | |
geky | 4:dcd0494556be | 28 | |
geky | 4:dcd0494556be | 29 | Serial output(USBTX, USBRX); |
geky | 4:dcd0494556be | 30 | |
geky | 4:dcd0494556be | 31 | //Select binding mode: UDP or TCP |
geky | 4:dcd0494556be | 32 | M2MInterface::BindingMode SOCKET_MODE = M2MInterface::UDP; |
geky | 4:dcd0494556be | 33 | |
geky | 4:dcd0494556be | 34 | // This is address to mbed Device Connector |
geky | 4:dcd0494556be | 35 | const String &MBED_SERVER_ADDRESS = "coap://api.connector.mbed.com:5684"; |
geky | 4:dcd0494556be | 36 | |
geky | 4:dcd0494556be | 37 | const String &MBED_USER_NAME_DOMAIN = MBED_DOMAIN; |
geky | 4:dcd0494556be | 38 | const String &ENDPOINT_NAME = MBED_ENDPOINT_NAME; |
geky | 4:dcd0494556be | 39 | |
geky | 4:dcd0494556be | 40 | const String &MANUFACTURER = "manufacturer"; |
geky | 4:dcd0494556be | 41 | const String &TYPE = "type"; |
geky | 4:dcd0494556be | 42 | const String &MODEL_NUMBER = "2015"; |
geky | 4:dcd0494556be | 43 | const String &SERIAL_NUMBER = "12345"; |
geky | 4:dcd0494556be | 44 | |
geky | 4:dcd0494556be | 45 | const uint8_t STATIC_VALUE[] = "Static value"; |
geky | 4:dcd0494556be | 46 | |
geky | 4:dcd0494556be | 47 | #if defined(TARGET_K64F) |
geky | 4:dcd0494556be | 48 | #define OBS_BUTTON SW2 |
geky | 4:dcd0494556be | 49 | #define UNREG_BUTTON SW3 |
geky | 4:dcd0494556be | 50 | #endif |
geky | 4:dcd0494556be | 51 | |
geky | 4:dcd0494556be | 52 | |
geky | 4:dcd0494556be | 53 | class MbedClient: public M2MInterfaceObserver { |
geky | 4:dcd0494556be | 54 | public: |
geky | 4:dcd0494556be | 55 | MbedClient(){ |
geky | 4:dcd0494556be | 56 | _interface = NULL; |
geky | 4:dcd0494556be | 57 | _bootstrapped = false; |
geky | 4:dcd0494556be | 58 | _error = false; |
geky | 4:dcd0494556be | 59 | _registered = false; |
geky | 4:dcd0494556be | 60 | _unregistered = false; |
geky | 4:dcd0494556be | 61 | _register_security = NULL; |
geky | 4:dcd0494556be | 62 | _value = 0; |
geky | 4:dcd0494556be | 63 | _object = NULL; |
geky | 4:dcd0494556be | 64 | } |
geky | 4:dcd0494556be | 65 | |
geky | 4:dcd0494556be | 66 | ~MbedClient() { |
geky | 4:dcd0494556be | 67 | if(_interface) { |
geky | 4:dcd0494556be | 68 | delete _interface; |
geky | 4:dcd0494556be | 69 | } |
geky | 4:dcd0494556be | 70 | if(_register_security){ |
geky | 4:dcd0494556be | 71 | delete _register_security; |
geky | 4:dcd0494556be | 72 | } |
geky | 4:dcd0494556be | 73 | } |
geky | 4:dcd0494556be | 74 | |
geky | 4:dcd0494556be | 75 | void trace_printer(const char* str) { |
geky | 4:dcd0494556be | 76 | output.printf("\r\n%s\r\n", str); |
geky | 4:dcd0494556be | 77 | } |
geky | 4:dcd0494556be | 78 | |
geky | 4:dcd0494556be | 79 | void create_interface() { |
geky | 4:dcd0494556be | 80 | // Creates M2MInterface using which endpoint can |
geky | 4:dcd0494556be | 81 | // setup its name, resource type, life time, connection mode, |
geky | 4:dcd0494556be | 82 | // Currently only LwIPv4 is supported. |
geky | 4:dcd0494556be | 83 | |
geky | 4:dcd0494556be | 84 | // Randomizing listening port for Certificate mode connectivity |
geky | 4:dcd0494556be | 85 | srand(time(NULL)); |
geky | 4:dcd0494556be | 86 | uint16_t port = rand() % 65535 + 12345; |
geky | 4:dcd0494556be | 87 | |
geky | 4:dcd0494556be | 88 | _interface = M2MInterfaceFactory::create_interface(*this, |
geky | 4:dcd0494556be | 89 | ENDPOINT_NAME, |
geky | 4:dcd0494556be | 90 | "test", |
geky | 4:dcd0494556be | 91 | 3600, |
geky | 4:dcd0494556be | 92 | port, |
geky | 4:dcd0494556be | 93 | MBED_USER_NAME_DOMAIN, |
geky | 4:dcd0494556be | 94 | SOCKET_MODE, |
geky | 4:dcd0494556be | 95 | M2MInterface::LwIP_IPv4, |
geky | 4:dcd0494556be | 96 | ""); |
geky | 4:dcd0494556be | 97 | } |
geky | 4:dcd0494556be | 98 | |
geky | 4:dcd0494556be | 99 | bool register_successful() { |
geky | 4:dcd0494556be | 100 | return _registered; |
geky | 4:dcd0494556be | 101 | } |
geky | 4:dcd0494556be | 102 | |
geky | 4:dcd0494556be | 103 | bool unregister_successful() { |
geky | 4:dcd0494556be | 104 | return _unregistered; |
geky | 4:dcd0494556be | 105 | } |
geky | 4:dcd0494556be | 106 | |
geky | 4:dcd0494556be | 107 | M2MSecurity* create_register_object() { |
geky | 4:dcd0494556be | 108 | // Creates register server object with mbed device server address and other parameters |
geky | 4:dcd0494556be | 109 | // required for client to connect to mbed device server. |
geky | 4:dcd0494556be | 110 | M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer); |
geky | 4:dcd0494556be | 111 | if(security) { |
geky | 4:dcd0494556be | 112 | security->set_resource_value(M2MSecurity::M2MServerUri, MBED_SERVER_ADDRESS); |
geky | 4:dcd0494556be | 113 | security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate); |
geky | 4:dcd0494556be | 114 | security->set_resource_value(M2MSecurity::ServerPublicKey,SERVER_CERT,sizeof(SERVER_CERT)); |
geky | 4:dcd0494556be | 115 | security->set_resource_value(M2MSecurity::PublicKey,CERT,sizeof(CERT)); |
geky | 4:dcd0494556be | 116 | security->set_resource_value(M2MSecurity::Secretkey,KEY,sizeof(KEY)); |
geky | 4:dcd0494556be | 117 | } |
geky | 4:dcd0494556be | 118 | return security; |
geky | 4:dcd0494556be | 119 | } |
geky | 4:dcd0494556be | 120 | |
geky | 4:dcd0494556be | 121 | M2MDevice* create_device_object() { |
geky | 4:dcd0494556be | 122 | // Creates device object which contains mandatory resources linked with |
geky | 4:dcd0494556be | 123 | // device endpoint. |
geky | 4:dcd0494556be | 124 | M2MDevice *device = M2MInterfaceFactory::create_device(); |
geky | 4:dcd0494556be | 125 | if(device) { |
geky | 4:dcd0494556be | 126 | device->create_resource(M2MDevice::Manufacturer,MANUFACTURER); |
geky | 4:dcd0494556be | 127 | device->create_resource(M2MDevice::DeviceType,TYPE); |
geky | 4:dcd0494556be | 128 | device->create_resource(M2MDevice::ModelNumber,MODEL_NUMBER); |
geky | 4:dcd0494556be | 129 | device->create_resource(M2MDevice::SerialNumber,SERIAL_NUMBER); |
geky | 4:dcd0494556be | 130 | } |
geky | 4:dcd0494556be | 131 | return device; |
geky | 4:dcd0494556be | 132 | } |
geky | 4:dcd0494556be | 133 | |
geky | 4:dcd0494556be | 134 | M2MObject* create_generic_object() { |
geky | 4:dcd0494556be | 135 | _object = M2MInterfaceFactory::create_object("Test"); |
geky | 4:dcd0494556be | 136 | if(_object) { |
geky | 4:dcd0494556be | 137 | M2MObjectInstance* inst = _object->create_object_instance(); |
geky | 4:dcd0494556be | 138 | if(inst) { |
geky | 4:dcd0494556be | 139 | M2MResource* res = inst->create_dynamic_resource("D", |
geky | 4:dcd0494556be | 140 | "ResourceTest", |
geky | 4:dcd0494556be | 141 | M2MResourceInstance::INTEGER, |
geky | 4:dcd0494556be | 142 | true); |
geky | 4:dcd0494556be | 143 | char buffer[20]; |
geky | 4:dcd0494556be | 144 | int size = sprintf(buffer,"%d",_value); |
geky | 4:dcd0494556be | 145 | res->set_operation(M2MBase::GET_PUT_ALLOWED); |
geky | 4:dcd0494556be | 146 | res->set_value((const uint8_t*)buffer, |
geky | 4:dcd0494556be | 147 | (const uint32_t)size); |
geky | 4:dcd0494556be | 148 | _value++; |
geky | 4:dcd0494556be | 149 | |
geky | 4:dcd0494556be | 150 | inst->create_static_resource("S", |
geky | 4:dcd0494556be | 151 | "ResourceTest", |
geky | 4:dcd0494556be | 152 | M2MResourceInstance::STRING, |
geky | 4:dcd0494556be | 153 | STATIC_VALUE, |
geky | 4:dcd0494556be | 154 | sizeof(STATIC_VALUE)-1); |
geky | 4:dcd0494556be | 155 | } |
geky | 4:dcd0494556be | 156 | } |
geky | 4:dcd0494556be | 157 | return _object; |
geky | 4:dcd0494556be | 158 | } |
geky | 4:dcd0494556be | 159 | |
geky | 4:dcd0494556be | 160 | void update_resource() { |
geky | 4:dcd0494556be | 161 | if(_object) { |
geky | 4:dcd0494556be | 162 | M2MObjectInstance* inst = _object->object_instance(); |
geky | 4:dcd0494556be | 163 | if(inst) { |
geky | 4:dcd0494556be | 164 | M2MResource* res = inst->resource("D"); |
geky | 4:dcd0494556be | 165 | |
geky | 4:dcd0494556be | 166 | char buffer[20]; |
geky | 4:dcd0494556be | 167 | int size = sprintf(buffer,"%d",_value); |
geky | 4:dcd0494556be | 168 | res->set_value((const uint8_t*)buffer, |
geky | 4:dcd0494556be | 169 | (const uint32_t)size); |
geky | 4:dcd0494556be | 170 | _value++; |
geky | 4:dcd0494556be | 171 | } |
geky | 4:dcd0494556be | 172 | } |
geky | 4:dcd0494556be | 173 | } |
geky | 4:dcd0494556be | 174 | |
geky | 4:dcd0494556be | 175 | void test_register(M2MSecurity *register_object, M2MObjectList object_list){ |
geky | 4:dcd0494556be | 176 | if(_interface) { |
geky | 4:dcd0494556be | 177 | // Register function |
geky | 4:dcd0494556be | 178 | _interface->register_object(register_object, object_list); |
geky | 4:dcd0494556be | 179 | } |
geky | 4:dcd0494556be | 180 | } |
geky | 4:dcd0494556be | 181 | |
geky | 4:dcd0494556be | 182 | void test_unregister() { |
geky | 4:dcd0494556be | 183 | if(_interface) { |
geky | 4:dcd0494556be | 184 | // Unregister function |
geky | 4:dcd0494556be | 185 | _interface->unregister_object(NULL); |
geky | 4:dcd0494556be | 186 | } |
geky | 4:dcd0494556be | 187 | } |
geky | 0:9ad9d701b1c3 | 188 | |
geky | 4:dcd0494556be | 189 | //Callback from mbed client stack when the bootstrap |
geky | 4:dcd0494556be | 190 | // is successful, it returns the mbed Device Server object |
geky | 4:dcd0494556be | 191 | // which will be used for registering the resources to |
geky | 4:dcd0494556be | 192 | // mbed Device server. |
geky | 4:dcd0494556be | 193 | void bootstrap_done(M2MSecurity *server_object){ |
geky | 4:dcd0494556be | 194 | if(server_object) { |
geky | 4:dcd0494556be | 195 | _bootstrapped = true; |
geky | 4:dcd0494556be | 196 | _error = false; |
geky | 4:dcd0494556be | 197 | trace_printer("\nBootstrapped\n"); |
geky | 4:dcd0494556be | 198 | } |
geky | 4:dcd0494556be | 199 | } |
geky | 4:dcd0494556be | 200 | |
geky | 4:dcd0494556be | 201 | //Callback from mbed client stack when the registration |
geky | 4:dcd0494556be | 202 | // is successful, it returns the mbed Device Server object |
geky | 4:dcd0494556be | 203 | // to which the resources are registered and registered objects. |
geky | 4:dcd0494556be | 204 | void object_registered(M2MSecurity */*security_object*/, const M2MServer &/*server_object*/){ |
geky | 4:dcd0494556be | 205 | _registered = true; |
geky | 4:dcd0494556be | 206 | _unregistered = false; |
geky | 4:dcd0494556be | 207 | trace_printer("\nRegistered\n"); |
geky | 4:dcd0494556be | 208 | } |
geky | 4:dcd0494556be | 209 | |
geky | 4:dcd0494556be | 210 | //Callback from mbed client stack when the unregistration |
geky | 4:dcd0494556be | 211 | // is successful, it returns the mbed Device Server object |
geky | 4:dcd0494556be | 212 | // to which the resources were unregistered. |
geky | 4:dcd0494556be | 213 | void object_unregistered(M2MSecurity */*server_object*/){ |
geky | 4:dcd0494556be | 214 | _unregistered = true; |
geky | 4:dcd0494556be | 215 | _registered = false; |
geky | 4:dcd0494556be | 216 | trace_printer("\nUnregistered\n"); |
geky | 4:dcd0494556be | 217 | } |
geky | 4:dcd0494556be | 218 | |
geky | 4:dcd0494556be | 219 | void registration_updated(M2MSecurity */*security_object*/, const M2MServer & /*server_object*/){ |
geky | 4:dcd0494556be | 220 | } |
geky | 4:dcd0494556be | 221 | |
geky | 4:dcd0494556be | 222 | //Callback from mbed client stack if any error is encountered |
geky | 4:dcd0494556be | 223 | // during any of the LWM2M operations. Error type is passed in |
geky | 4:dcd0494556be | 224 | // the callback. |
geky | 4:dcd0494556be | 225 | void error(M2MInterface::Error error){ |
geky | 4:dcd0494556be | 226 | _error = true; |
geky | 4:dcd0494556be | 227 | switch(error){ |
geky | 4:dcd0494556be | 228 | case M2MInterface::AlreadyExists: |
geky | 4:dcd0494556be | 229 | trace_printer("[ERROR:] M2MInterface::AlreadyExists\n"); |
geky | 4:dcd0494556be | 230 | break; |
geky | 4:dcd0494556be | 231 | case M2MInterface::BootstrapFailed: |
geky | 4:dcd0494556be | 232 | trace_printer("[ERROR:] M2MInterface::BootstrapFailed\n"); |
geky | 4:dcd0494556be | 233 | break; |
geky | 4:dcd0494556be | 234 | case M2MInterface::InvalidParameters: |
geky | 4:dcd0494556be | 235 | trace_printer("[ERROR:] M2MInterface::InvalidParameters\n"); |
geky | 4:dcd0494556be | 236 | break; |
geky | 4:dcd0494556be | 237 | case M2MInterface::NotRegistered: |
geky | 4:dcd0494556be | 238 | trace_printer("[ERROR:] M2MInterface::NotRegistered\n"); |
geky | 4:dcd0494556be | 239 | break; |
geky | 4:dcd0494556be | 240 | case M2MInterface::Timeout: |
geky | 4:dcd0494556be | 241 | trace_printer("[ERROR:] M2MInterface::Timeout\n"); |
geky | 4:dcd0494556be | 242 | break; |
geky | 4:dcd0494556be | 243 | case M2MInterface::NetworkError: |
geky | 4:dcd0494556be | 244 | trace_printer("[ERROR:] M2MInterface::NetworkError\n"); |
geky | 4:dcd0494556be | 245 | break; |
geky | 4:dcd0494556be | 246 | case M2MInterface::ResponseParseFailed: |
geky | 4:dcd0494556be | 247 | trace_printer("[ERROR:] M2MInterface::ResponseParseFailed\n"); |
geky | 4:dcd0494556be | 248 | break; |
geky | 4:dcd0494556be | 249 | case M2MInterface::UnknownError: |
geky | 4:dcd0494556be | 250 | trace_printer("[ERROR:] M2MInterface::UnknownError\n"); |
geky | 4:dcd0494556be | 251 | break; |
geky | 4:dcd0494556be | 252 | case M2MInterface::MemoryFail: |
geky | 4:dcd0494556be | 253 | trace_printer("[ERROR:] M2MInterface::MemoryFail\n"); |
geky | 4:dcd0494556be | 254 | break; |
geky | 4:dcd0494556be | 255 | case M2MInterface::NotAllowed: |
geky | 4:dcd0494556be | 256 | trace_printer("[ERROR:] M2MInterface::NotAllowed\n"); |
geky | 4:dcd0494556be | 257 | break; |
geky | 4:dcd0494556be | 258 | default: |
geky | 4:dcd0494556be | 259 | break; |
geky | 4:dcd0494556be | 260 | } |
geky | 4:dcd0494556be | 261 | } |
geky | 4:dcd0494556be | 262 | |
geky | 4:dcd0494556be | 263 | //Callback from mbed client stack if any value has changed |
geky | 4:dcd0494556be | 264 | // during PUT operation. Object and its type is passed in |
geky | 4:dcd0494556be | 265 | // the callback. |
geky | 4:dcd0494556be | 266 | void value_updated(M2MBase *base, M2MBase::BaseType type) { |
geky | 4:dcd0494556be | 267 | output.printf("\nValue updated of Object name %s and Type %d\n", |
geky | 4:dcd0494556be | 268 | base->name().c_str(), type); |
geky | 4:dcd0494556be | 269 | } |
geky | 4:dcd0494556be | 270 | |
geky | 4:dcd0494556be | 271 | void test_update_register() { |
geky | 4:dcd0494556be | 272 | if (_registered) { |
geky | 4:dcd0494556be | 273 | _interface->update_registration(_register_security, 3600); |
geky | 4:dcd0494556be | 274 | } |
geky | 4:dcd0494556be | 275 | } |
geky | 4:dcd0494556be | 276 | |
geky | 4:dcd0494556be | 277 | void set_register_object(M2MSecurity *register_object) { |
geky | 4:dcd0494556be | 278 | if (_register_security == NULL) { |
geky | 4:dcd0494556be | 279 | _register_security = register_object; |
geky | 4:dcd0494556be | 280 | } |
geky | 4:dcd0494556be | 281 | } |
geky | 4:dcd0494556be | 282 | |
geky | 4:dcd0494556be | 283 | private: |
geky | 4:dcd0494556be | 284 | |
geky | 4:dcd0494556be | 285 | M2MInterface *_interface; |
geky | 4:dcd0494556be | 286 | M2MSecurity *_register_security; |
geky | 4:dcd0494556be | 287 | M2MObject *_object; |
geky | 4:dcd0494556be | 288 | volatile bool _bootstrapped; |
geky | 4:dcd0494556be | 289 | volatile bool _error; |
geky | 4:dcd0494556be | 290 | volatile bool _registered; |
geky | 4:dcd0494556be | 291 | volatile bool _unregistered; |
geky | 4:dcd0494556be | 292 | int _value; |
geky | 4:dcd0494556be | 293 | }; |
geky | 4:dcd0494556be | 294 | |
geky | 4:dcd0494556be | 295 | EthernetInterface eth; |
geky | 4:dcd0494556be | 296 | // Instantiate the class which implements |
geky | 4:dcd0494556be | 297 | // LWM2M Client API |
geky | 4:dcd0494556be | 298 | MbedClient mbed_client; |
geky | 4:dcd0494556be | 299 | |
geky | 4:dcd0494556be | 300 | // Set up Hardware interrupt button. |
geky | 4:dcd0494556be | 301 | InterruptIn obs_button(OBS_BUTTON); |
geky | 4:dcd0494556be | 302 | InterruptIn unreg_button(UNREG_BUTTON); |
geky | 4:dcd0494556be | 303 | |
geky | 4:dcd0494556be | 304 | // Network interaction must be performed outside of interrupt context |
geky | 4:dcd0494556be | 305 | Semaphore updates(0); |
geky | 4:dcd0494556be | 306 | volatile bool registered = false; |
geky | 4:dcd0494556be | 307 | osThreadId mainThread; |
geky | 4:dcd0494556be | 308 | |
geky | 4:dcd0494556be | 309 | void unregister() { |
geky | 4:dcd0494556be | 310 | registered = false; |
geky | 4:dcd0494556be | 311 | updates.release(); |
geky | 4:dcd0494556be | 312 | } |
geky | 4:dcd0494556be | 313 | |
geky | 4:dcd0494556be | 314 | void update() { |
geky | 4:dcd0494556be | 315 | updates.release(); |
geky | 4:dcd0494556be | 316 | } |
geky | 4:dcd0494556be | 317 | |
geky | 4:dcd0494556be | 318 | // Status indication |
geky | 4:dcd0494556be | 319 | Ticker status_ticker; |
geky | 4:dcd0494556be | 320 | DigitalOut status_led(LED_GREEN); |
geky | 4:dcd0494556be | 321 | void blinky() { status_led = !status_led; } |
geky | 4:dcd0494556be | 322 | |
geky | 4:dcd0494556be | 323 | |
geky | 4:dcd0494556be | 324 | // Entry point to the program |
geky | 4:dcd0494556be | 325 | int main() { |
geky | 4:dcd0494556be | 326 | status_ticker.attach_us(blinky, 250000); |
geky | 4:dcd0494556be | 327 | |
geky | 4:dcd0494556be | 328 | // Keep track of the main thread |
geky | 4:dcd0494556be | 329 | mainThread = osThreadGetId(); |
geky | 4:dcd0494556be | 330 | |
geky | 4:dcd0494556be | 331 | // Sets the console baud-rate |
geky | 4:dcd0494556be | 332 | output.baud(115200); |
geky | 4:dcd0494556be | 333 | |
geky | 4:dcd0494556be | 334 | // This sets up the network interface configuration which will be used |
geky | 4:dcd0494556be | 335 | // by LWM2M Client API to communicate with mbed Device server. |
geky | 4:dcd0494556be | 336 | eth.init(); //Use DHCP |
geky | 4:dcd0494556be | 337 | eth.connect(); |
geky | 4:dcd0494556be | 338 | |
geky | 4:dcd0494556be | 339 | //lwipv4_socket_init(); |
geky | 4:dcd0494556be | 340 | output.printf("IP address %s\r\n", eth.getIPAddress()); |
geky | 4:dcd0494556be | 341 | |
geky | 4:dcd0494556be | 342 | // On press of SW3 button on K64F board, example application |
geky | 4:dcd0494556be | 343 | // will call unregister API towards mbed Device Server |
geky | 4:dcd0494556be | 344 | //unreg_button.fall(&mbed_client,&MbedClient::test_unregister); |
geky | 4:dcd0494556be | 345 | unreg_button.fall(&unregister); |
geky | 4:dcd0494556be | 346 | |
geky | 4:dcd0494556be | 347 | // On press of SW2 button on K64F board, example application |
geky | 4:dcd0494556be | 348 | // will send observation towards mbed Device Server |
geky | 4:dcd0494556be | 349 | obs_button.fall(&update); |
geky | 4:dcd0494556be | 350 | |
geky | 4:dcd0494556be | 351 | // Create LWM2M Client API interface to manage register and unregister |
geky | 4:dcd0494556be | 352 | mbed_client.create_interface(); |
geky | 4:dcd0494556be | 353 | |
geky | 4:dcd0494556be | 354 | // Create LWM2M server object specifying mbed device server |
geky | 4:dcd0494556be | 355 | // information. |
geky | 4:dcd0494556be | 356 | M2MSecurity* register_object = mbed_client.create_register_object(); |
geky | 4:dcd0494556be | 357 | |
geky | 4:dcd0494556be | 358 | // Create LWM2M device object specifying device resources |
geky | 4:dcd0494556be | 359 | // as per OMA LWM2M specification. |
geky | 4:dcd0494556be | 360 | M2MDevice* device_object = mbed_client.create_device_object(); |
geky | 4:dcd0494556be | 361 | |
geky | 4:dcd0494556be | 362 | // Create Generic object specifying custom resources |
geky | 4:dcd0494556be | 363 | M2MObject* generic_object = mbed_client.create_generic_object(); |
geky | 4:dcd0494556be | 364 | |
geky | 4:dcd0494556be | 365 | // Add all the objects that you would like to register |
geky | 4:dcd0494556be | 366 | // into the list and pass the list for register API. |
geky | 4:dcd0494556be | 367 | M2MObjectList object_list; |
geky | 4:dcd0494556be | 368 | object_list.push_back(device_object); |
geky | 4:dcd0494556be | 369 | object_list.push_back(generic_object); |
geky | 4:dcd0494556be | 370 | |
geky | 4:dcd0494556be | 371 | mbed_client.set_register_object(register_object); |
geky | 4:dcd0494556be | 372 | |
geky | 4:dcd0494556be | 373 | // Register with mbed Device Connector |
geky | 4:dcd0494556be | 374 | mbed_client.test_register(register_object, object_list); |
geky | 4:dcd0494556be | 375 | registered = true; |
geky | 4:dcd0494556be | 376 | |
geky | 4:dcd0494556be | 377 | while (true) { |
geky | 4:dcd0494556be | 378 | int update = updates.wait(25000); |
geky | 4:dcd0494556be | 379 | |
geky | 4:dcd0494556be | 380 | if (!registered) { |
geky | 4:dcd0494556be | 381 | break; |
geky | 4:dcd0494556be | 382 | } else if (update) { |
geky | 4:dcd0494556be | 383 | mbed_client.update_resource(); |
geky | 4:dcd0494556be | 384 | } else { |
geky | 4:dcd0494556be | 385 | mbed_client.test_update_register(); |
geky | 4:dcd0494556be | 386 | } |
geky | 4:dcd0494556be | 387 | } |
geky | 4:dcd0494556be | 388 | |
geky | 4:dcd0494556be | 389 | mbed_client.test_unregister(); |
geky | 4:dcd0494556be | 390 | status_ticker.detach(); |
geky | 4:dcd0494556be | 391 | } |
geky | 4:dcd0494556be | 392 | |
geky | 4:dcd0494556be | 393 |