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