FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:35:07 2017 +0000
Revision:
0:a2cb7295a1f7
Initial commit

Who changed what in which revision?

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