Ram Gandikota
/
ABCD
A metronome using the FRDM K64F board
mbed-client/test/mbedclient-smokeTest/main.cpp@0:a7a43371b306, 2017-05-14 (annotated)
- Committer:
- ram54288
- Date:
- Sun May 14 18:40:18 2017 +0000
- Revision:
- 0:a7a43371b306
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ram54288 | 0:a7a43371b306 | 1 | /* |
ram54288 | 0:a7a43371b306 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
ram54288 | 0:a7a43371b306 | 3 | * SPDX-License-Identifier: Apache-2.0 |
ram54288 | 0:a7a43371b306 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
ram54288 | 0:a7a43371b306 | 5 | * not use this file except in compliance with the License. |
ram54288 | 0:a7a43371b306 | 6 | * You may obtain a copy of the License at |
ram54288 | 0:a7a43371b306 | 7 | * |
ram54288 | 0:a7a43371b306 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ram54288 | 0:a7a43371b306 | 9 | * |
ram54288 | 0:a7a43371b306 | 10 | * Unless required by applicable law or agreed to in writing, software |
ram54288 | 0:a7a43371b306 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
ram54288 | 0:a7a43371b306 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ram54288 | 0:a7a43371b306 | 13 | * See the License for the specific language governing permissions and |
ram54288 | 0:a7a43371b306 | 14 | * limitations under the License. |
ram54288 | 0:a7a43371b306 | 15 | */ |
ram54288 | 0:a7a43371b306 | 16 | #include "sockets/UDPSocket.h" |
ram54288 | 0:a7a43371b306 | 17 | #include "EthernetInterface.h" |
ram54288 | 0:a7a43371b306 | 18 | #include "test_env.h" |
ram54288 | 0:a7a43371b306 | 19 | #include "mbed-client/m2minterfacefactory.h" |
ram54288 | 0:a7a43371b306 | 20 | #include "mbed-client/m2minterfaceobserver.h" |
ram54288 | 0:a7a43371b306 | 21 | #include "mbed-client/m2mdevice.h" |
ram54288 | 0:a7a43371b306 | 22 | #include "mbed-client/m2mobjectinstance.h" |
ram54288 | 0:a7a43371b306 | 23 | #include "mbed-client/m2minterface.h" |
ram54288 | 0:a7a43371b306 | 24 | #include "testconfig.h" |
ram54288 | 0:a7a43371b306 | 25 | |
ram54288 | 0:a7a43371b306 | 26 | // TODO: Remove when yotta supports init. |
ram54288 | 0:a7a43371b306 | 27 | #include "lwipv4_init.h" |
ram54288 | 0:a7a43371b306 | 28 | |
ram54288 | 0:a7a43371b306 | 29 | // Minar for event scheduling |
ram54288 | 0:a7a43371b306 | 30 | #include "minar/minar.h" |
ram54288 | 0:a7a43371b306 | 31 | |
ram54288 | 0:a7a43371b306 | 32 | using namespace mbed::util; |
ram54288 | 0:a7a43371b306 | 33 | const String &MANUFACTURER = "ARM"; |
ram54288 | 0:a7a43371b306 | 34 | const String &TYPE = "type"; |
ram54288 | 0:a7a43371b306 | 35 | |
ram54288 | 0:a7a43371b306 | 36 | static bool test_result = false; |
ram54288 | 0:a7a43371b306 | 37 | |
ram54288 | 0:a7a43371b306 | 38 | // Dynamic resource variables |
ram54288 | 0:a7a43371b306 | 39 | const String &DYNAMIC_RESOURCE_NAME = "Dynamic"; |
ram54288 | 0:a7a43371b306 | 40 | const String &DYNAMIC_RESOURCE_TYPE = "DynamicType"; |
ram54288 | 0:a7a43371b306 | 41 | const String &STATIC_RESOURCE_NAME = "Static"; |
ram54288 | 0:a7a43371b306 | 42 | const String &STATIC_RESOURCE_TYPE = "StaticType"; |
ram54288 | 0:a7a43371b306 | 43 | const uint8_t STATIC_VALUE[] = "Static value"; |
ram54288 | 0:a7a43371b306 | 44 | |
ram54288 | 0:a7a43371b306 | 45 | //TODO: should this be configured in .json conf file, and communicated via host test to here? |
ram54288 | 0:a7a43371b306 | 46 | int CALLBACK_TIMEOUT = 5; |
ram54288 | 0:a7a43371b306 | 47 | int MINAR_DELAY = 10000; |
ram54288 | 0:a7a43371b306 | 48 | |
ram54288 | 0:a7a43371b306 | 49 | #define SUITE_TEST_INFO(test_name, info) printf("Suite-%s: %s\n", test_name, info) |
ram54288 | 0:a7a43371b306 | 50 | #define SUITE_TEST_RESULT(test_name, result) printf("Suite-%s: result %s\n", test_name, result ? "PASSED" : "FAILED") |
ram54288 | 0:a7a43371b306 | 51 | #define SUITE_RESULT(result) printf("Suite: result %s\n", result ? "success" : "failure") |
ram54288 | 0:a7a43371b306 | 52 | |
ram54288 | 0:a7a43371b306 | 53 | |
ram54288 | 0:a7a43371b306 | 54 | class MbedClient: public M2MInterfaceObserver { |
ram54288 | 0:a7a43371b306 | 55 | public: |
ram54288 | 0:a7a43371b306 | 56 | MbedClient(TestConfig *test_config){ |
ram54288 | 0:a7a43371b306 | 57 | _interface = NULL; |
ram54288 | 0:a7a43371b306 | 58 | _register_security = NULL; |
ram54288 | 0:a7a43371b306 | 59 | _resource_object = NULL; |
ram54288 | 0:a7a43371b306 | 60 | _bootstrapped = false; |
ram54288 | 0:a7a43371b306 | 61 | _error = false; |
ram54288 | 0:a7a43371b306 | 62 | _registered = false; |
ram54288 | 0:a7a43371b306 | 63 | _unregistered = false; |
ram54288 | 0:a7a43371b306 | 64 | _registration_updated = false; |
ram54288 | 0:a7a43371b306 | 65 | _resource_value = 0; |
ram54288 | 0:a7a43371b306 | 66 | _object = NULL; |
ram54288 | 0:a7a43371b306 | 67 | _test_config = test_config; |
ram54288 | 0:a7a43371b306 | 68 | } |
ram54288 | 0:a7a43371b306 | 69 | |
ram54288 | 0:a7a43371b306 | 70 | virtual ~MbedClient() { |
ram54288 | 0:a7a43371b306 | 71 | if(_interface) { |
ram54288 | 0:a7a43371b306 | 72 | delete _interface; |
ram54288 | 0:a7a43371b306 | 73 | } |
ram54288 | 0:a7a43371b306 | 74 | if( _register_security){ |
ram54288 | 0:a7a43371b306 | 75 | delete _register_security; |
ram54288 | 0:a7a43371b306 | 76 | } |
ram54288 | 0:a7a43371b306 | 77 | } |
ram54288 | 0:a7a43371b306 | 78 | |
ram54288 | 0:a7a43371b306 | 79 | bool create_interface() { |
ram54288 | 0:a7a43371b306 | 80 | bool success = false; |
ram54288 | 0:a7a43371b306 | 81 | // Creates M2MInterface using which endpoint can |
ram54288 | 0:a7a43371b306 | 82 | // setup its name, resource type, life time, connection mode, |
ram54288 | 0:a7a43371b306 | 83 | // Currently only LwIPv4 is supported. |
ram54288 | 0:a7a43371b306 | 84 | _interface = M2MInterfaceFactory::create_interface( *this, |
ram54288 | 0:a7a43371b306 | 85 | _test_config->get_endpoint_name(), |
ram54288 | 0:a7a43371b306 | 86 | _test_config->get_endpoint_type(), |
ram54288 | 0:a7a43371b306 | 87 | _test_config->get_lifetime(), |
ram54288 | 0:a7a43371b306 | 88 | _test_config->get_port(), |
ram54288 | 0:a7a43371b306 | 89 | _test_config->get_domain(), |
ram54288 | 0:a7a43371b306 | 90 | M2MInterface::UDP, |
ram54288 | 0:a7a43371b306 | 91 | M2MInterface::LwIP_IPv4, |
ram54288 | 0:a7a43371b306 | 92 | ""); |
ram54288 | 0:a7a43371b306 | 93 | if (_interface) { |
ram54288 | 0:a7a43371b306 | 94 | success = true; |
ram54288 | 0:a7a43371b306 | 95 | } |
ram54288 | 0:a7a43371b306 | 96 | |
ram54288 | 0:a7a43371b306 | 97 | return success; |
ram54288 | 0:a7a43371b306 | 98 | } |
ram54288 | 0:a7a43371b306 | 99 | |
ram54288 | 0:a7a43371b306 | 100 | bool bootstrap_successful() { |
ram54288 | 0:a7a43371b306 | 101 | return _bootstrapped; |
ram54288 | 0:a7a43371b306 | 102 | } |
ram54288 | 0:a7a43371b306 | 103 | |
ram54288 | 0:a7a43371b306 | 104 | M2MSecurity* create_bootstrap_object() { |
ram54288 | 0:a7a43371b306 | 105 | // Creates bootstrap server object with Bootstrap server address and other parameters |
ram54288 | 0:a7a43371b306 | 106 | // required for client to connect to bootstrap server. |
ram54288 | 0:a7a43371b306 | 107 | M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::Bootstrap); |
ram54288 | 0:a7a43371b306 | 108 | if(security) { |
ram54288 | 0:a7a43371b306 | 109 | security->set_resource_value(M2MSecurity::M2MServerUri, _test_config->get_bootstrap_server()); |
ram54288 | 0:a7a43371b306 | 110 | security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity); |
ram54288 | 0:a7a43371b306 | 111 | } |
ram54288 | 0:a7a43371b306 | 112 | return security; |
ram54288 | 0:a7a43371b306 | 113 | } |
ram54288 | 0:a7a43371b306 | 114 | |
ram54288 | 0:a7a43371b306 | 115 | void test_bootstrap(M2MSecurity *security) { |
ram54288 | 0:a7a43371b306 | 116 | if(_interface) { |
ram54288 | 0:a7a43371b306 | 117 | // Bootstrap function. |
ram54288 | 0:a7a43371b306 | 118 | _interface->bootstrap(security); |
ram54288 | 0:a7a43371b306 | 119 | } |
ram54288 | 0:a7a43371b306 | 120 | } |
ram54288 | 0:a7a43371b306 | 121 | |
ram54288 | 0:a7a43371b306 | 122 | bool register_successful() { |
ram54288 | 0:a7a43371b306 | 123 | return _registered; |
ram54288 | 0:a7a43371b306 | 124 | } |
ram54288 | 0:a7a43371b306 | 125 | |
ram54288 | 0:a7a43371b306 | 126 | bool unregister_successful() { |
ram54288 | 0:a7a43371b306 | 127 | return _unregistered; |
ram54288 | 0:a7a43371b306 | 128 | } |
ram54288 | 0:a7a43371b306 | 129 | |
ram54288 | 0:a7a43371b306 | 130 | bool update_register_successful() { |
ram54288 | 0:a7a43371b306 | 131 | return _registration_updated; |
ram54288 | 0:a7a43371b306 | 132 | } |
ram54288 | 0:a7a43371b306 | 133 | |
ram54288 | 0:a7a43371b306 | 134 | void check_result(const char* result) { |
ram54288 | 0:a7a43371b306 | 135 | if(_registered && _registration_updated && _unregistered) { |
ram54288 | 0:a7a43371b306 | 136 | SUITE_TEST_RESULT(result, true); |
ram54288 | 0:a7a43371b306 | 137 | test_result = true; |
ram54288 | 0:a7a43371b306 | 138 | } else { |
ram54288 | 0:a7a43371b306 | 139 | SUITE_TEST_RESULT(result, false); |
ram54288 | 0:a7a43371b306 | 140 | test_result = false; |
ram54288 | 0:a7a43371b306 | 141 | } |
ram54288 | 0:a7a43371b306 | 142 | minar::Scheduler::stop(); |
ram54288 | 0:a7a43371b306 | 143 | } |
ram54288 | 0:a7a43371b306 | 144 | |
ram54288 | 0:a7a43371b306 | 145 | M2MSecurity* create_register_object() { |
ram54288 | 0:a7a43371b306 | 146 | // Creates server object with LWM2M server address and other parameters |
ram54288 | 0:a7a43371b306 | 147 | // required for client to connect to LWM2M server. |
ram54288 | 0:a7a43371b306 | 148 | M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer); |
ram54288 | 0:a7a43371b306 | 149 | if(security) { |
ram54288 | 0:a7a43371b306 | 150 | security->set_resource_value(M2MSecurity::M2MServerUri, _test_config->get_mds_server()); |
ram54288 | 0:a7a43371b306 | 151 | //security->set_resource_value(M2MSecurity::M2MServerUri, "ds-test.dev.mbed.com"); |
ram54288 | 0:a7a43371b306 | 152 | security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity); |
ram54288 | 0:a7a43371b306 | 153 | } |
ram54288 | 0:a7a43371b306 | 154 | return security; |
ram54288 | 0:a7a43371b306 | 155 | } |
ram54288 | 0:a7a43371b306 | 156 | |
ram54288 | 0:a7a43371b306 | 157 | void test_register(M2MObjectList object_list){ |
ram54288 | 0:a7a43371b306 | 158 | if(_interface) { |
ram54288 | 0:a7a43371b306 | 159 | // Register function |
ram54288 | 0:a7a43371b306 | 160 | _interface->register_object(_register_security, object_list); |
ram54288 | 0:a7a43371b306 | 161 | } |
ram54288 | 0:a7a43371b306 | 162 | } |
ram54288 | 0:a7a43371b306 | 163 | |
ram54288 | 0:a7a43371b306 | 164 | void test_update_register(const uint32_t lifetime) |
ram54288 | 0:a7a43371b306 | 165 | { |
ram54288 | 0:a7a43371b306 | 166 | if(_interface && _register_security) { |
ram54288 | 0:a7a43371b306 | 167 | _interface->update_registration(_register_security,lifetime); |
ram54288 | 0:a7a43371b306 | 168 | } |
ram54288 | 0:a7a43371b306 | 169 | } |
ram54288 | 0:a7a43371b306 | 170 | |
ram54288 | 0:a7a43371b306 | 171 | void test_unregister(){ |
ram54288 | 0:a7a43371b306 | 172 | if(_interface) { |
ram54288 | 0:a7a43371b306 | 173 | // Unregister function |
ram54288 | 0:a7a43371b306 | 174 | _interface->unregister_object(NULL); |
ram54288 | 0:a7a43371b306 | 175 | } |
ram54288 | 0:a7a43371b306 | 176 | } |
ram54288 | 0:a7a43371b306 | 177 | |
ram54288 | 0:a7a43371b306 | 178 | void set_register_object(M2MSecurity *®ister_object){ |
ram54288 | 0:a7a43371b306 | 179 | if(_register_security) { |
ram54288 | 0:a7a43371b306 | 180 | delete _register_security; |
ram54288 | 0:a7a43371b306 | 181 | _register_security = NULL; |
ram54288 | 0:a7a43371b306 | 182 | } |
ram54288 | 0:a7a43371b306 | 183 | _register_security = register_object; |
ram54288 | 0:a7a43371b306 | 184 | } |
ram54288 | 0:a7a43371b306 | 185 | |
ram54288 | 0:a7a43371b306 | 186 | M2MDevice* create_device_object() { |
ram54288 | 0:a7a43371b306 | 187 | M2MDevice *device = M2MInterfaceFactory::create_device(); |
ram54288 | 0:a7a43371b306 | 188 | if (device) { |
ram54288 | 0:a7a43371b306 | 189 | device->create_resource(M2MDevice::Manufacturer, MANUFACTURER); |
ram54288 | 0:a7a43371b306 | 190 | device->create_resource(M2MDevice::DeviceType, TYPE); |
ram54288 | 0:a7a43371b306 | 191 | } |
ram54288 | 0:a7a43371b306 | 192 | return device; |
ram54288 | 0:a7a43371b306 | 193 | } |
ram54288 | 0:a7a43371b306 | 194 | |
ram54288 | 0:a7a43371b306 | 195 | M2MObject* create_generic_object() { |
ram54288 | 0:a7a43371b306 | 196 | _object = M2MInterfaceFactory::create_object("Test"); |
ram54288 | 0:a7a43371b306 | 197 | if(_object) { |
ram54288 | 0:a7a43371b306 | 198 | M2MObjectInstance* inst = _object->create_object_instance(); |
ram54288 | 0:a7a43371b306 | 199 | if(inst) { |
ram54288 | 0:a7a43371b306 | 200 | M2MResource* res = inst->create_dynamic_resource("D","ResourceTest", |
ram54288 | 0:a7a43371b306 | 201 | M2MResourceInstance::INTEGER, |
ram54288 | 0:a7a43371b306 | 202 | true); |
ram54288 | 0:a7a43371b306 | 203 | char buffer[20]; |
ram54288 | 0:a7a43371b306 | 204 | int size = sprintf(buffer,"%d",_resource_value); |
ram54288 | 0:a7a43371b306 | 205 | res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); |
ram54288 | 0:a7a43371b306 | 206 | res->set_value((const uint8_t*)buffer, |
ram54288 | 0:a7a43371b306 | 207 | (const uint32_t)size); |
ram54288 | 0:a7a43371b306 | 208 | _resource_value++; |
ram54288 | 0:a7a43371b306 | 209 | |
ram54288 | 0:a7a43371b306 | 210 | inst->create_static_resource("S", |
ram54288 | 0:a7a43371b306 | 211 | "ResourceTest", |
ram54288 | 0:a7a43371b306 | 212 | M2MResourceInstance::STRING, |
ram54288 | 0:a7a43371b306 | 213 | STATIC_VALUE, |
ram54288 | 0:a7a43371b306 | 214 | sizeof(STATIC_VALUE)-1); |
ram54288 | 0:a7a43371b306 | 215 | } |
ram54288 | 0:a7a43371b306 | 216 | } |
ram54288 | 0:a7a43371b306 | 217 | return _object; |
ram54288 | 0:a7a43371b306 | 218 | } |
ram54288 | 0:a7a43371b306 | 219 | |
ram54288 | 0:a7a43371b306 | 220 | //Callback from mbed client stack when the bootstrap |
ram54288 | 0:a7a43371b306 | 221 | // is successful, it returns the mbed Device Server object |
ram54288 | 0:a7a43371b306 | 222 | // which will be used for registering the resources to |
ram54288 | 0:a7a43371b306 | 223 | // mbed Device server. |
ram54288 | 0:a7a43371b306 | 224 | void bootstrap_done(M2MSecurity *server_object){ |
ram54288 | 0:a7a43371b306 | 225 | if(server_object) { |
ram54288 | 0:a7a43371b306 | 226 | _bootstrapped = true; |
ram54288 | 0:a7a43371b306 | 227 | _error = false; |
ram54288 | 0:a7a43371b306 | 228 | } |
ram54288 | 0:a7a43371b306 | 229 | } |
ram54288 | 0:a7a43371b306 | 230 | |
ram54288 | 0:a7a43371b306 | 231 | //Callback from mbed client stack when the registration |
ram54288 | 0:a7a43371b306 | 232 | // is successful, it returns the mbed Device Server object |
ram54288 | 0:a7a43371b306 | 233 | // to which the resources are registered and registered objects. |
ram54288 | 0:a7a43371b306 | 234 | void object_registered(M2MSecurity */*security_object*/, const M2MServer &/*server_object*/){ |
ram54288 | 0:a7a43371b306 | 235 | _registered = true; |
ram54288 | 0:a7a43371b306 | 236 | } |
ram54288 | 0:a7a43371b306 | 237 | |
ram54288 | 0:a7a43371b306 | 238 | //Callback from mbed client stack when the registration update |
ram54288 | 0:a7a43371b306 | 239 | // is successful, it returns the mbed Device Server object |
ram54288 | 0:a7a43371b306 | 240 | // to which the resources are registered and registered objects. |
ram54288 | 0:a7a43371b306 | 241 | void registration_updated(M2MSecurity */*security_object*/, const M2MServer &/*server_object*/){ |
ram54288 | 0:a7a43371b306 | 242 | _registration_updated = true; |
ram54288 | 0:a7a43371b306 | 243 | } |
ram54288 | 0:a7a43371b306 | 244 | |
ram54288 | 0:a7a43371b306 | 245 | //Callback from mbed client stack when the unregistration |
ram54288 | 0:a7a43371b306 | 246 | // is successful, it returns the mbed Device Server object |
ram54288 | 0:a7a43371b306 | 247 | // to which the resources were unregistered. |
ram54288 | 0:a7a43371b306 | 248 | void object_unregistered(M2MSecurity */*server_object*/){ |
ram54288 | 0:a7a43371b306 | 249 | _unregistered = true; |
ram54288 | 0:a7a43371b306 | 250 | } |
ram54288 | 0:a7a43371b306 | 251 | |
ram54288 | 0:a7a43371b306 | 252 | //Callback from mbed client stack if any value has changed |
ram54288 | 0:a7a43371b306 | 253 | // during PUT operation. Object and its type is passed in |
ram54288 | 0:a7a43371b306 | 254 | // the callback. |
ram54288 | 0:a7a43371b306 | 255 | void value_updated(M2MBase *base, M2MBase::BaseType type) { |
ram54288 | 0:a7a43371b306 | 256 | printf("\nValue updated of Object name %s and Type %d\n", |
ram54288 | 0:a7a43371b306 | 257 | base->name().c_str(), type); |
ram54288 | 0:a7a43371b306 | 258 | } |
ram54288 | 0:a7a43371b306 | 259 | |
ram54288 | 0:a7a43371b306 | 260 | //Callback from mbed client stack if any error is encountered |
ram54288 | 0:a7a43371b306 | 261 | // during any of the LWM2M operations. Error type is passed in |
ram54288 | 0:a7a43371b306 | 262 | // the callback. |
ram54288 | 0:a7a43371b306 | 263 | void error(M2MInterface::Error error){ |
ram54288 | 0:a7a43371b306 | 264 | _error = true; |
ram54288 | 0:a7a43371b306 | 265 | printf("\nError occured %d\n", error); |
ram54288 | 0:a7a43371b306 | 266 | } |
ram54288 | 0:a7a43371b306 | 267 | |
ram54288 | 0:a7a43371b306 | 268 | private: |
ram54288 | 0:a7a43371b306 | 269 | |
ram54288 | 0:a7a43371b306 | 270 | M2MInterface *_interface; |
ram54288 | 0:a7a43371b306 | 271 | M2MSecurity *_register_security; |
ram54288 | 0:a7a43371b306 | 272 | M2MObject *_resource_object; |
ram54288 | 0:a7a43371b306 | 273 | M2MObject *_object; |
ram54288 | 0:a7a43371b306 | 274 | volatile bool _bootstrapped; |
ram54288 | 0:a7a43371b306 | 275 | volatile bool _error; |
ram54288 | 0:a7a43371b306 | 276 | volatile bool _registered; |
ram54288 | 0:a7a43371b306 | 277 | volatile bool _unregistered; |
ram54288 | 0:a7a43371b306 | 278 | volatile bool _registration_updated; |
ram54288 | 0:a7a43371b306 | 279 | int _resource_value; |
ram54288 | 0:a7a43371b306 | 280 | TestConfig *_test_config; |
ram54288 | 0:a7a43371b306 | 281 | }; |
ram54288 | 0:a7a43371b306 | 282 | |
ram54288 | 0:a7a43371b306 | 283 | #define WAIT_CALLBACK(X, TIMEOUT) \ |
ram54288 | 0:a7a43371b306 | 284 | {int _timer = 0;\ |
ram54288 | 0:a7a43371b306 | 285 | while ( 1 ) \ |
ram54288 | 0:a7a43371b306 | 286 | { \ |
ram54288 | 0:a7a43371b306 | 287 | _result &= (X); \ |
ram54288 | 0:a7a43371b306 | 288 | if (_result) { \ |
ram54288 | 0:a7a43371b306 | 289 | SUITE_TEST_INFO(_tn, "callback done"); \ |
ram54288 | 0:a7a43371b306 | 290 | break; \ |
ram54288 | 0:a7a43371b306 | 291 | } \ |
ram54288 | 0:a7a43371b306 | 292 | wait_ms(1000); _timer+=1; \ |
ram54288 | 0:a7a43371b306 | 293 | if (_timer >= TIMEOUT) { \ |
ram54288 | 0:a7a43371b306 | 294 | SUITE_TEST_INFO(_tn, "ERROR: callback timeout");\ |
ram54288 | 0:a7a43371b306 | 295 | break; \ |
ram54288 | 0:a7a43371b306 | 296 | } \ |
ram54288 | 0:a7a43371b306 | 297 | }} |
ram54288 | 0:a7a43371b306 | 298 | |
ram54288 | 0:a7a43371b306 | 299 | bool test_bootStrap(TestConfig *test_config) { |
ram54288 | 0:a7a43371b306 | 300 | bool _result = true; |
ram54288 | 0:a7a43371b306 | 301 | const char* _tn = "TC1_bootStrap"; |
ram54288 | 0:a7a43371b306 | 302 | |
ram54288 | 0:a7a43371b306 | 303 | SUITE_TEST_INFO(_tn, "STARTED"); |
ram54288 | 0:a7a43371b306 | 304 | |
ram54288 | 0:a7a43371b306 | 305 | // Instantiate the class which implements |
ram54288 | 0:a7a43371b306 | 306 | // mbed Client API |
ram54288 | 0:a7a43371b306 | 307 | MbedClient *mbed_client = new MbedClient(test_config); |
ram54288 | 0:a7a43371b306 | 308 | |
ram54288 | 0:a7a43371b306 | 309 | SUITE_TEST_INFO(_tn, "client done"); |
ram54288 | 0:a7a43371b306 | 310 | |
ram54288 | 0:a7a43371b306 | 311 | // Create LWM2M Client API interface to manage bootstrap, |
ram54288 | 0:a7a43371b306 | 312 | // register and unregister |
ram54288 | 0:a7a43371b306 | 313 | _result &= mbed_client->create_interface(); |
ram54288 | 0:a7a43371b306 | 314 | |
ram54288 | 0:a7a43371b306 | 315 | // Create LWM2M bootstrap object specifying bootstrap server |
ram54288 | 0:a7a43371b306 | 316 | // information. |
ram54288 | 0:a7a43371b306 | 317 | M2MSecurity* security_object = mbed_client->create_bootstrap_object(); |
ram54288 | 0:a7a43371b306 | 318 | |
ram54288 | 0:a7a43371b306 | 319 | // Issue bootstrap command. |
ram54288 | 0:a7a43371b306 | 320 | mbed_client->test_bootstrap(security_object); |
ram54288 | 0:a7a43371b306 | 321 | SUITE_TEST_INFO(_tn, "bootstrap done"); |
ram54288 | 0:a7a43371b306 | 322 | |
ram54288 | 0:a7a43371b306 | 323 | SUITE_TEST_INFO(_tn, "waiting bootstrap callback..."); |
ram54288 | 0:a7a43371b306 | 324 | // Wait till the bootstrap callback is called successfully. |
ram54288 | 0:a7a43371b306 | 325 | // Callback comes in bootstrap_done() |
ram54288 | 0:a7a43371b306 | 326 | WAIT_CALLBACK(mbed_client->bootstrap_successful(), CALLBACK_TIMEOUT); |
ram54288 | 0:a7a43371b306 | 327 | |
ram54288 | 0:a7a43371b306 | 328 | // Delete security object created for bootstrapping |
ram54288 | 0:a7a43371b306 | 329 | if(security_object) { |
ram54288 | 0:a7a43371b306 | 330 | delete security_object; |
ram54288 | 0:a7a43371b306 | 331 | } |
ram54288 | 0:a7a43371b306 | 332 | |
ram54288 | 0:a7a43371b306 | 333 | if (mbed_client) { |
ram54288 | 0:a7a43371b306 | 334 | delete mbed_client; |
ram54288 | 0:a7a43371b306 | 335 | } |
ram54288 | 0:a7a43371b306 | 336 | |
ram54288 | 0:a7a43371b306 | 337 | SUITE_TEST_RESULT(_tn, _result); |
ram54288 | 0:a7a43371b306 | 338 | return _result; |
ram54288 | 0:a7a43371b306 | 339 | } |
ram54288 | 0:a7a43371b306 | 340 | |
ram54288 | 0:a7a43371b306 | 341 | void test_deviceObject(TestConfig *test_config) { |
ram54288 | 0:a7a43371b306 | 342 | const char* _tn = "TC2_deviceObject"; |
ram54288 | 0:a7a43371b306 | 343 | |
ram54288 | 0:a7a43371b306 | 344 | SUITE_TEST_INFO(_tn, "STARTED"); |
ram54288 | 0:a7a43371b306 | 345 | |
ram54288 | 0:a7a43371b306 | 346 | // Instantiate the class which implements |
ram54288 | 0:a7a43371b306 | 347 | // LWM2M Client API |
ram54288 | 0:a7a43371b306 | 348 | MbedClient mbed_client(test_config);// = new MbedClient(test_config); |
ram54288 | 0:a7a43371b306 | 349 | |
ram54288 | 0:a7a43371b306 | 350 | SUITE_TEST_INFO(_tn, "client done"); |
ram54288 | 0:a7a43371b306 | 351 | |
ram54288 | 0:a7a43371b306 | 352 | // Create LWM2M Client API interface for M2M server |
ram54288 | 0:a7a43371b306 | 353 | mbed_client.create_interface(); |
ram54288 | 0:a7a43371b306 | 354 | |
ram54288 | 0:a7a43371b306 | 355 | M2MSecurity *register_object = mbed_client.create_register_object(); |
ram54288 | 0:a7a43371b306 | 356 | |
ram54288 | 0:a7a43371b306 | 357 | mbed_client.set_register_object(register_object); |
ram54288 | 0:a7a43371b306 | 358 | |
ram54288 | 0:a7a43371b306 | 359 | // Create LWM2M device object specifying device resources |
ram54288 | 0:a7a43371b306 | 360 | // as per OMA LWM2M specification. |
ram54288 | 0:a7a43371b306 | 361 | M2MDevice* device_object = mbed_client.create_device_object(); |
ram54288 | 0:a7a43371b306 | 362 | |
ram54288 | 0:a7a43371b306 | 363 | // Add the device object that we want to register |
ram54288 | 0:a7a43371b306 | 364 | // into the list and pass the list for register API. |
ram54288 | 0:a7a43371b306 | 365 | M2MObjectList object_list; |
ram54288 | 0:a7a43371b306 | 366 | object_list.push_back(device_object); |
ram54288 | 0:a7a43371b306 | 367 | |
ram54288 | 0:a7a43371b306 | 368 | // Issue register command. |
ram54288 | 0:a7a43371b306 | 369 | |
ram54288 | 0:a7a43371b306 | 370 | FunctionPointer1<void, M2MObjectList> tr(&mbed_client, &MbedClient::test_register); |
ram54288 | 0:a7a43371b306 | 371 | minar::Scheduler::postCallback(tr.bind(object_list)); |
ram54288 | 0:a7a43371b306 | 372 | |
ram54288 | 0:a7a43371b306 | 373 | // Issue update register command. |
ram54288 | 0:a7a43371b306 | 374 | |
ram54288 | 0:a7a43371b306 | 375 | uint32_t lifetime = 2222; |
ram54288 | 0:a7a43371b306 | 376 | |
ram54288 | 0:a7a43371b306 | 377 | FunctionPointer1<void, uint32_t> ur(&mbed_client, &MbedClient::test_update_register); |
ram54288 | 0:a7a43371b306 | 378 | minar::Scheduler::postCallback(ur.bind(lifetime)).delay(MINAR_DELAY); |
ram54288 | 0:a7a43371b306 | 379 | |
ram54288 | 0:a7a43371b306 | 380 | // Issue unregister command. |
ram54288 | 0:a7a43371b306 | 381 | |
ram54288 | 0:a7a43371b306 | 382 | FunctionPointer0<void> tur(&mbed_client, &MbedClient::test_unregister); |
ram54288 | 0:a7a43371b306 | 383 | minar::Scheduler::postCallback(tur.bind()).delay(MINAR_DELAY*2); |
ram54288 | 0:a7a43371b306 | 384 | |
ram54288 | 0:a7a43371b306 | 385 | FunctionPointer1<void, const char*> cus(&mbed_client, &MbedClient::check_result); |
ram54288 | 0:a7a43371b306 | 386 | minar::Scheduler::postCallback(cus.bind(_tn)).delay(MINAR_DELAY*3); |
ram54288 | 0:a7a43371b306 | 387 | |
ram54288 | 0:a7a43371b306 | 388 | minar::Scheduler::start(); |
ram54288 | 0:a7a43371b306 | 389 | |
ram54288 | 0:a7a43371b306 | 390 | // Delete device object created for registering device |
ram54288 | 0:a7a43371b306 | 391 | // resources. |
ram54288 | 0:a7a43371b306 | 392 | |
ram54288 | 0:a7a43371b306 | 393 | if(device_object) { |
ram54288 | 0:a7a43371b306 | 394 | M2MDevice::delete_instance(); |
ram54288 | 0:a7a43371b306 | 395 | } |
ram54288 | 0:a7a43371b306 | 396 | } |
ram54288 | 0:a7a43371b306 | 397 | |
ram54288 | 0:a7a43371b306 | 398 | bool test_resource(TestConfig *test_config) { |
ram54288 | 0:a7a43371b306 | 399 | bool _result = true; |
ram54288 | 0:a7a43371b306 | 400 | const char* _tn = "TC3_resource"; |
ram54288 | 0:a7a43371b306 | 401 | SUITE_TEST_INFO(_tn, "STARTED"); |
ram54288 | 0:a7a43371b306 | 402 | |
ram54288 | 0:a7a43371b306 | 403 | // Instantiate the class which implements LWM2M Client API |
ram54288 | 0:a7a43371b306 | 404 | MbedClient mbed_client(test_config); |
ram54288 | 0:a7a43371b306 | 405 | SUITE_TEST_INFO(_tn, "client done"); |
ram54288 | 0:a7a43371b306 | 406 | |
ram54288 | 0:a7a43371b306 | 407 | // Create LWM2M Client API interface for M2M server |
ram54288 | 0:a7a43371b306 | 408 | _result &= mbed_client.create_interface(); |
ram54288 | 0:a7a43371b306 | 409 | |
ram54288 | 0:a7a43371b306 | 410 | M2MSecurity *register_object = mbed_client.create_register_object(); |
ram54288 | 0:a7a43371b306 | 411 | |
ram54288 | 0:a7a43371b306 | 412 | mbed_client.set_register_object(register_object); |
ram54288 | 0:a7a43371b306 | 413 | |
ram54288 | 0:a7a43371b306 | 414 | // Create LWM2M device object specifying device resources |
ram54288 | 0:a7a43371b306 | 415 | // as per OMA LWM2M specification. |
ram54288 | 0:a7a43371b306 | 416 | M2MDevice* device_object = mbed_client.create_device_object(); |
ram54288 | 0:a7a43371b306 | 417 | |
ram54288 | 0:a7a43371b306 | 418 | // Create LWM2M generic object for resource |
ram54288 | 0:a7a43371b306 | 419 | M2MObject* resource_object = mbed_client.create_generic_object(); |
ram54288 | 0:a7a43371b306 | 420 | |
ram54288 | 0:a7a43371b306 | 421 | // Add the device object that we want to register |
ram54288 | 0:a7a43371b306 | 422 | // into the list and pass the list for register API. |
ram54288 | 0:a7a43371b306 | 423 | M2MObjectList object_list; |
ram54288 | 0:a7a43371b306 | 424 | object_list.push_back(device_object); |
ram54288 | 0:a7a43371b306 | 425 | object_list.push_back(resource_object); |
ram54288 | 0:a7a43371b306 | 426 | |
ram54288 | 0:a7a43371b306 | 427 | // Issue register command. |
ram54288 | 0:a7a43371b306 | 428 | |
ram54288 | 0:a7a43371b306 | 429 | FunctionPointer1<void, M2MObjectList> tr(&mbed_client, &MbedClient::test_register); |
ram54288 | 0:a7a43371b306 | 430 | minar::Scheduler::postCallback(tr.bind(object_list)); |
ram54288 | 0:a7a43371b306 | 431 | |
ram54288 | 0:a7a43371b306 | 432 | // Issue update register command. |
ram54288 | 0:a7a43371b306 | 433 | |
ram54288 | 0:a7a43371b306 | 434 | uint32_t lifetime = 2222; |
ram54288 | 0:a7a43371b306 | 435 | |
ram54288 | 0:a7a43371b306 | 436 | FunctionPointer1<void, uint32_t> ur(&mbed_client, &MbedClient::test_update_register); |
ram54288 | 0:a7a43371b306 | 437 | minar::Scheduler::postCallback(ur.bind(lifetime)).delay(MINAR_DELAY); |
ram54288 | 0:a7a43371b306 | 438 | |
ram54288 | 0:a7a43371b306 | 439 | // Issue unregister command. |
ram54288 | 0:a7a43371b306 | 440 | |
ram54288 | 0:a7a43371b306 | 441 | FunctionPointer0<void> tur(&mbed_client, &MbedClient::test_unregister); |
ram54288 | 0:a7a43371b306 | 442 | minar::Scheduler::postCallback(tur.bind()).delay(MINAR_DELAY*2); |
ram54288 | 0:a7a43371b306 | 443 | |
ram54288 | 0:a7a43371b306 | 444 | FunctionPointer1<void, const char*> cus(&mbed_client, &MbedClient::check_result); |
ram54288 | 0:a7a43371b306 | 445 | minar::Scheduler::postCallback(cus.bind(_tn)).delay(MINAR_DELAY*3); |
ram54288 | 0:a7a43371b306 | 446 | |
ram54288 | 0:a7a43371b306 | 447 | minar::Scheduler::start(); |
ram54288 | 0:a7a43371b306 | 448 | |
ram54288 | 0:a7a43371b306 | 449 | // Delete device object created for registering device resources. |
ram54288 | 0:a7a43371b306 | 450 | if(device_object) { |
ram54288 | 0:a7a43371b306 | 451 | M2MDevice::delete_instance(); |
ram54288 | 0:a7a43371b306 | 452 | } |
ram54288 | 0:a7a43371b306 | 453 | |
ram54288 | 0:a7a43371b306 | 454 | // Delete resource object for registering resources. |
ram54288 | 0:a7a43371b306 | 455 | if(resource_object) { |
ram54288 | 0:a7a43371b306 | 456 | delete resource_object; |
ram54288 | 0:a7a43371b306 | 457 | } |
ram54288 | 0:a7a43371b306 | 458 | } |
ram54288 | 0:a7a43371b306 | 459 | |
ram54288 | 0:a7a43371b306 | 460 | |
ram54288 | 0:a7a43371b306 | 461 | |
ram54288 | 0:a7a43371b306 | 462 | void app_start(int /*argc*/, char* /*argv*/[]) { |
ram54288 | 0:a7a43371b306 | 463 | DigitalOut _led = DigitalOut(LED3); |
ram54288 | 0:a7a43371b306 | 464 | _led = 1; |
ram54288 | 0:a7a43371b306 | 465 | |
ram54288 | 0:a7a43371b306 | 466 | MBED_HOSTTEST_TIMEOUT(40); |
ram54288 | 0:a7a43371b306 | 467 | MBED_HOSTTEST_SELECT(mbed_client_auto); |
ram54288 | 0:a7a43371b306 | 468 | MBED_HOSTTEST_DESCRIPTION(LWM2MClient Smoke Test); |
ram54288 | 0:a7a43371b306 | 469 | MBED_HOSTTEST_START("LWM2MClientSmokeTest"); |
ram54288 | 0:a7a43371b306 | 470 | |
ram54288 | 0:a7a43371b306 | 471 | // This sets up the network interface configuration which will be used |
ram54288 | 0:a7a43371b306 | 472 | // by LWM2M Client API to communicate with mbed Device server. |
ram54288 | 0:a7a43371b306 | 473 | EthernetInterface eth; |
ram54288 | 0:a7a43371b306 | 474 | eth.init(); //Use DHCP |
ram54288 | 0:a7a43371b306 | 475 | eth.connect(); |
ram54288 | 0:a7a43371b306 | 476 | |
ram54288 | 0:a7a43371b306 | 477 | lwipv4_socket_init(); |
ram54288 | 0:a7a43371b306 | 478 | |
ram54288 | 0:a7a43371b306 | 479 | // Create test config object, and setup with unique MAC address |
ram54288 | 0:a7a43371b306 | 480 | TestConfig test_config; |
ram54288 | 0:a7a43371b306 | 481 | test_config.setup(); |
ram54288 | 0:a7a43371b306 | 482 | |
ram54288 | 0:a7a43371b306 | 483 | _led = 0; |
ram54288 | 0:a7a43371b306 | 484 | |
ram54288 | 0:a7a43371b306 | 485 | // Bootstrap test is uncommented, until it will be supported. |
ram54288 | 0:a7a43371b306 | 486 | //result &= test_bootStrap(&test_config); |
ram54288 | 0:a7a43371b306 | 487 | test_deviceObject(&test_config); |
ram54288 | 0:a7a43371b306 | 488 | test_resource(&test_config); |
ram54288 | 0:a7a43371b306 | 489 | |
ram54288 | 0:a7a43371b306 | 490 | _led = 1; |
ram54288 | 0:a7a43371b306 | 491 | |
ram54288 | 0:a7a43371b306 | 492 | // Disconnect and teardown the network interface |
ram54288 | 0:a7a43371b306 | 493 | eth.disconnect(); |
ram54288 | 0:a7a43371b306 | 494 | |
ram54288 | 0:a7a43371b306 | 495 | SUITE_RESULT(test_result); |
ram54288 | 0:a7a43371b306 | 496 | } |
ram54288 | 0:a7a43371b306 | 497 |