FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

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