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
ram54288 0:dbad57390bd1 17 #include "mbedclient.h"
ram54288 0:dbad57390bd1 18
ram54288 0:dbad57390bd1 19 #include "mbed-client/m2minterfacefactory.h"
ram54288 0:dbad57390bd1 20 #include "mbed-client/m2mdevice.h"
ram54288 0:dbad57390bd1 21 #include "mbed-client/m2mobjectinstance.h"
ram54288 0:dbad57390bd1 22 #include "mbed-client/m2mresource.h"
ram54288 0:dbad57390bd1 23
ram54288 0:dbad57390bd1 24 #ifdef TARGET_LIKE_MBED
ram54288 0:dbad57390bd1 25 #include "minar/minar.h"
ram54288 0:dbad57390bd1 26 #include "test_env.h"
ram54288 0:dbad57390bd1 27 #endif
ram54288 0:dbad57390bd1 28
ram54288 0:dbad57390bd1 29 #include <time.h>
ram54288 0:dbad57390bd1 30 #include <stdio.h>
ram54288 0:dbad57390bd1 31
ram54288 0:dbad57390bd1 32 // Enter your mbed Device Server's IPv4 address and Port number in
ram54288 0:dbad57390bd1 33 // mentioned format like 192.168.0.1:5693
ram54288 0:dbad57390bd1 34 const String &BOOTSTRAP_SERVER_ADDRESS = "coap://10.45.3.10:5693";
ram54288 0:dbad57390bd1 35 const String &BOOTSTRAP_SERVER_DTLS_ADDRESS = "coap://10.45.3.10:5694";
ram54288 0:dbad57390bd1 36
ram54288 0:dbad57390bd1 37 #ifdef SIXLOWPAN_INTERFACE
ram54288 0:dbad57390bd1 38 const String &MBED_SERVER_ADDRESS = "coap://FD00:FF1:CE0B:A5E1:1068:AF13:9B61:D557:5683";
ram54288 0:dbad57390bd1 39 const String &MBED_SERVER_DTLS_ADDRESS = "coap://FD00:FF1:CE0B:A5E1:1068:AF13:9B61:D557:5684";
ram54288 0:dbad57390bd1 40 #else
ram54288 0:dbad57390bd1 41 const String &MBED_SERVER_ADDRESS = "coap://10.45.3.10:5683";
ram54288 0:dbad57390bd1 42 //const String &MBED_SERVER_ADDRESS = "coap://10.45.0.152:5683";
ram54288 0:dbad57390bd1 43 const String &MBED_SERVER_DTLS_ADDRESS = "coap://10.45.3.10:5684";
ram54288 0:dbad57390bd1 44 #endif
ram54288 0:dbad57390bd1 45 const String CLIENT_NAME = "secure-client";
ram54288 0:dbad57390bd1 46
ram54288 0:dbad57390bd1 47 const String &MANUFACTURER = "ARM";
ram54288 0:dbad57390bd1 48 const String &TYPE = "type";
ram54288 0:dbad57390bd1 49 const String &MODEL_NUMBER = "2015";
ram54288 0:dbad57390bd1 50 const String &SERIAL_NUMBER = "12345";
ram54288 0:dbad57390bd1 51 const uint8_t STATIC_VALUE[] = "Static value";
ram54288 0:dbad57390bd1 52
ram54288 0:dbad57390bd1 53 #ifdef TARGET_LIKE_LINUX
ram54288 0:dbad57390bd1 54 #include <unistd.h>
ram54288 0:dbad57390bd1 55 #else
ram54288 0:dbad57390bd1 56
ram54288 0:dbad57390bd1 57 #endif
ram54288 0:dbad57390bd1 58
ram54288 0:dbad57390bd1 59 MbedClient::MbedClient()
ram54288 0:dbad57390bd1 60 #ifndef TARGET_LIKE_LINUX
ram54288 0:dbad57390bd1 61 :_led(LED3)
ram54288 0:dbad57390bd1 62 #endif
ram54288 0:dbad57390bd1 63 {
ram54288 0:dbad57390bd1 64 _security = NULL;
ram54288 0:dbad57390bd1 65 _interface = NULL;
ram54288 0:dbad57390bd1 66 _register_security = NULL;
ram54288 0:dbad57390bd1 67 _device = NULL;
ram54288 0:dbad57390bd1 68 _object = NULL;
ram54288 0:dbad57390bd1 69 _bootstrapped = false;
ram54288 0:dbad57390bd1 70 _error = false;
ram54288 0:dbad57390bd1 71 _registered = false;
ram54288 0:dbad57390bd1 72 _unregistered = false;
ram54288 0:dbad57390bd1 73 _registration_updated = false;
ram54288 0:dbad57390bd1 74 _value = 0;
ram54288 0:dbad57390bd1 75 }
ram54288 0:dbad57390bd1 76
ram54288 0:dbad57390bd1 77 MbedClient::~MbedClient() {
ram54288 0:dbad57390bd1 78 if(_security) {
ram54288 0:dbad57390bd1 79 delete _security;
ram54288 0:dbad57390bd1 80 }
ram54288 0:dbad57390bd1 81 if(_register_security){
ram54288 0:dbad57390bd1 82 delete _register_security;
ram54288 0:dbad57390bd1 83 }
ram54288 0:dbad57390bd1 84 if(_device) {
ram54288 0:dbad57390bd1 85 M2MDevice::delete_instance();
ram54288 0:dbad57390bd1 86 }
ram54288 0:dbad57390bd1 87 if(_object) {
ram54288 0:dbad57390bd1 88 delete _object;
ram54288 0:dbad57390bd1 89 }
ram54288 0:dbad57390bd1 90 if(_interface) {
ram54288 0:dbad57390bd1 91 delete _interface;
ram54288 0:dbad57390bd1 92 }
ram54288 0:dbad57390bd1 93 }
ram54288 0:dbad57390bd1 94
ram54288 0:dbad57390bd1 95 bool MbedClient::create_interface() {
ram54288 0:dbad57390bd1 96
ram54288 0:dbad57390bd1 97 M2MInterface::NetworkStack stack = M2MInterface::LwIP_IPv4;
ram54288 0:dbad57390bd1 98 #ifdef SIXLOWPAN_INTERFACE
ram54288 0:dbad57390bd1 99 stack = M2MInterface::Nanostack_IPv6;
ram54288 0:dbad57390bd1 100 #endif
ram54288 0:dbad57390bd1 101
ram54288 0:dbad57390bd1 102 /* From http://www.iana.org/assignments/port-numbers:
ram54288 0:dbad57390bd1 103 "The Dynamic and/or Private Ports are those from 49152 through 65535" */
ram54288 0:dbad57390bd1 104 srand(time(NULL));
ram54288 0:dbad57390bd1 105 uint16_t port = (rand() % (65535-49152)) + 49152;
ram54288 0:dbad57390bd1 106
ram54288 0:dbad57390bd1 107 _interface = M2MInterfaceFactory::create_interface(*this,
ram54288 0:dbad57390bd1 108 CLIENT_NAME,
ram54288 0:dbad57390bd1 109 "test",
ram54288 0:dbad57390bd1 110 60,
ram54288 0:dbad57390bd1 111 port,
ram54288 0:dbad57390bd1 112 "",
ram54288 0:dbad57390bd1 113 M2MInterface::UDP,
ram54288 0:dbad57390bd1 114 stack,
ram54288 0:dbad57390bd1 115 "");
ram54288 0:dbad57390bd1 116
ram54288 0:dbad57390bd1 117 return (_interface == NULL) ? false : true;
ram54288 0:dbad57390bd1 118 }
ram54288 0:dbad57390bd1 119
ram54288 0:dbad57390bd1 120 bool MbedClient::bootstrap_successful() {
ram54288 0:dbad57390bd1 121 #ifdef TARGET_LIKE_LINUX
ram54288 0:dbad57390bd1 122 while(!_bootstrapped && !_error) {
ram54288 0:dbad57390bd1 123 sleep(1);
ram54288 0:dbad57390bd1 124 }
ram54288 0:dbad57390bd1 125 #endif
ram54288 0:dbad57390bd1 126 return _bootstrapped;
ram54288 0:dbad57390bd1 127 }
ram54288 0:dbad57390bd1 128
ram54288 0:dbad57390bd1 129 bool MbedClient::register_successful() {
ram54288 0:dbad57390bd1 130 #ifdef TARGET_LIKE_LINUX
ram54288 0:dbad57390bd1 131 while(!_registered && !_error) {
ram54288 0:dbad57390bd1 132 sleep(1);
ram54288 0:dbad57390bd1 133 }
ram54288 0:dbad57390bd1 134 #endif
ram54288 0:dbad57390bd1 135 return _registered;
ram54288 0:dbad57390bd1 136 }
ram54288 0:dbad57390bd1 137
ram54288 0:dbad57390bd1 138 bool MbedClient::unregister_successful() {
ram54288 0:dbad57390bd1 139 #ifdef TARGET_LIKE_LINUX
ram54288 0:dbad57390bd1 140 while(!_unregistered && !_error) {
ram54288 0:dbad57390bd1 141 sleep(1);
ram54288 0:dbad57390bd1 142 }
ram54288 0:dbad57390bd1 143 #endif
ram54288 0:dbad57390bd1 144 return _unregistered;
ram54288 0:dbad57390bd1 145 }
ram54288 0:dbad57390bd1 146
ram54288 0:dbad57390bd1 147 bool MbedClient::registration_update_successful() {
ram54288 0:dbad57390bd1 148 #ifdef TARGET_LIKE_LINUX
ram54288 0:dbad57390bd1 149 while(!_registration_updated && !_error) {
ram54288 0:dbad57390bd1 150 sleep(1);
ram54288 0:dbad57390bd1 151 }
ram54288 0:dbad57390bd1 152 #endif
ram54288 0:dbad57390bd1 153 return _registration_updated;
ram54288 0:dbad57390bd1 154 }
ram54288 0:dbad57390bd1 155
ram54288 0:dbad57390bd1 156 M2MSecurity* MbedClient::create_bootstrap_object(bool useSecureConnection) {
ram54288 0:dbad57390bd1 157 // Creates bootstrap server object with Bootstrap server address and other parameters
ram54288 0:dbad57390bd1 158 // required for client to connect to bootstrap server.
ram54288 0:dbad57390bd1 159 M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::Bootstrap);
ram54288 0:dbad57390bd1 160 if(security) {
ram54288 0:dbad57390bd1 161 if(useSecureConnection){
ram54288 0:dbad57390bd1 162 security->set_resource_value(M2MSecurity::M2MServerUri, BOOTSTRAP_SERVER_DTLS_ADDRESS);
ram54288 0:dbad57390bd1 163 //TODO: remove these, when bootstrapping server supports DTLS
ram54288 0:dbad57390bd1 164 delete security;
ram54288 0:dbad57390bd1 165 return NULL;
ram54288 0:dbad57390bd1 166 }else{
ram54288 0:dbad57390bd1 167 security->set_resource_value(M2MSecurity::M2MServerUri, BOOTSTRAP_SERVER_ADDRESS);
ram54288 0:dbad57390bd1 168 //TODO: refactor this out
ram54288 0:dbad57390bd1 169 security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity);
ram54288 0:dbad57390bd1 170 }
ram54288 0:dbad57390bd1 171 }
ram54288 0:dbad57390bd1 172 return security;
ram54288 0:dbad57390bd1 173 }
ram54288 0:dbad57390bd1 174
ram54288 0:dbad57390bd1 175 M2MSecurity* MbedClient::create_register_object(bool useSecureConnection) {
ram54288 0:dbad57390bd1 176 // Creates bootstrap server object with Bootstrap server address and other parameters
ram54288 0:dbad57390bd1 177 // required for client to connect to bootstrap server.
ram54288 0:dbad57390bd1 178 M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer);
ram54288 0:dbad57390bd1 179 if(security) {
ram54288 0:dbad57390bd1 180 if(useSecureConnection){
ram54288 0:dbad57390bd1 181 security->set_resource_value(M2MSecurity::M2MServerUri, MBED_SERVER_DTLS_ADDRESS);
ram54288 0:dbad57390bd1 182 }else{
ram54288 0:dbad57390bd1 183 security->set_resource_value(M2MSecurity::M2MServerUri, MBED_SERVER_ADDRESS);
ram54288 0:dbad57390bd1 184 }
ram54288 0:dbad57390bd1 185 }
ram54288 0:dbad57390bd1 186 return security;
ram54288 0:dbad57390bd1 187 }
ram54288 0:dbad57390bd1 188
ram54288 0:dbad57390bd1 189 void MbedClient::test_bootstrap(M2MSecurity *security) {
ram54288 0:dbad57390bd1 190 if(_interface) {
ram54288 0:dbad57390bd1 191 // Bootstrap function.
ram54288 0:dbad57390bd1 192 _interface->bootstrap(security);
ram54288 0:dbad57390bd1 193 }
ram54288 0:dbad57390bd1 194 }
ram54288 0:dbad57390bd1 195
ram54288 0:dbad57390bd1 196 M2MDevice* MbedClient::create_device_object() {
ram54288 0:dbad57390bd1 197 // Creates device object which contains mandatory resources linked with
ram54288 0:dbad57390bd1 198 // device endpoint.
ram54288 0:dbad57390bd1 199 M2MDevice *device = M2MInterfaceFactory::create_device();
ram54288 0:dbad57390bd1 200 if(device) {
ram54288 0:dbad57390bd1 201 device->create_resource(M2MDevice::Manufacturer,MANUFACTURER);
ram54288 0:dbad57390bd1 202 device->create_resource(M2MDevice::DeviceType,TYPE);
ram54288 0:dbad57390bd1 203 device->create_resource(M2MDevice::ModelNumber,MODEL_NUMBER);
ram54288 0:dbad57390bd1 204 device->create_resource(M2MDevice::SerialNumber,SERIAL_NUMBER);
ram54288 0:dbad57390bd1 205 }
ram54288 0:dbad57390bd1 206 return device;
ram54288 0:dbad57390bd1 207 }
ram54288 0:dbad57390bd1 208
ram54288 0:dbad57390bd1 209 void MbedClient::execute_function(void *argument) {
ram54288 0:dbad57390bd1 210 #ifdef TARGET_LIKE_LINUX
ram54288 0:dbad57390bd1 211 if(argument) {
ram54288 0:dbad57390bd1 212 char* arguments = (char*)argument;
ram54288 0:dbad57390bd1 213 printf("Received %s!!\n", arguments);
ram54288 0:dbad57390bd1 214 }
ram54288 0:dbad57390bd1 215 printf("I am executed !!\n");
ram54288 0:dbad57390bd1 216 #else
ram54288 0:dbad57390bd1 217 _led == 0 ? _led = 1 : _led = 0;
ram54288 0:dbad57390bd1 218 #endif
ram54288 0:dbad57390bd1 219 }
ram54288 0:dbad57390bd1 220
ram54288 0:dbad57390bd1 221 M2MObject* MbedClient::create_generic_object() {
ram54288 0:dbad57390bd1 222 _object = M2MInterfaceFactory::create_object("Test");
ram54288 0:dbad57390bd1 223 if(_object) {
ram54288 0:dbad57390bd1 224 M2MObjectInstance* inst = _object->create_object_instance();
ram54288 0:dbad57390bd1 225 if(inst) {
ram54288 0:dbad57390bd1 226 inst->set_operation(M2MBase::GET_ALLOWED);
ram54288 0:dbad57390bd1 227
ram54288 0:dbad57390bd1 228 M2MResource* res = inst->create_dynamic_resource("Dynamic",
ram54288 0:dbad57390bd1 229 "ResourceTest",
ram54288 0:dbad57390bd1 230 M2MResourceInstance::INTEGER,
ram54288 0:dbad57390bd1 231 true);
ram54288 0:dbad57390bd1 232
ram54288 0:dbad57390bd1 233 char buffer[20];
ram54288 0:dbad57390bd1 234 int size = sprintf(buffer,"%d",_value);
ram54288 0:dbad57390bd1 235 res->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
ram54288 0:dbad57390bd1 236 res->set_value((const uint8_t*)buffer,
ram54288 0:dbad57390bd1 237 (const uint32_t)size);
ram54288 0:dbad57390bd1 238 res->set_execute_function(execute_callback(this,&MbedClient::execute_function));
ram54288 0:dbad57390bd1 239 _value++;
ram54288 0:dbad57390bd1 240
ram54288 0:dbad57390bd1 241 inst->create_static_resource("Static",
ram54288 0:dbad57390bd1 242 "ResourceTest",
ram54288 0:dbad57390bd1 243 M2MResourceInstance::STRING,
ram54288 0:dbad57390bd1 244 STATIC_VALUE,
ram54288 0:dbad57390bd1 245 sizeof(STATIC_VALUE)-1);
ram54288 0:dbad57390bd1 246 }
ram54288 0:dbad57390bd1 247 }
ram54288 0:dbad57390bd1 248 return _object;
ram54288 0:dbad57390bd1 249 }
ram54288 0:dbad57390bd1 250
ram54288 0:dbad57390bd1 251 void MbedClient::update_resource() {
ram54288 0:dbad57390bd1 252 if(_object) {
ram54288 0:dbad57390bd1 253 M2MObjectInstance* inst = _object->object_instance();
ram54288 0:dbad57390bd1 254 if(inst) {
ram54288 0:dbad57390bd1 255 M2MResource* res = inst->resource("Dynamic");
ram54288 0:dbad57390bd1 256 if( res ){
ram54288 0:dbad57390bd1 257 char buffer[20];
ram54288 0:dbad57390bd1 258 int size = sprintf(buffer,"%d",_value);
ram54288 0:dbad57390bd1 259 res->set_value((const uint8_t*)buffer,
ram54288 0:dbad57390bd1 260 (const uint32_t)size);
ram54288 0:dbad57390bd1 261 _value++;
ram54288 0:dbad57390bd1 262 }
ram54288 0:dbad57390bd1 263 }
ram54288 0:dbad57390bd1 264 }
ram54288 0:dbad57390bd1 265 }
ram54288 0:dbad57390bd1 266
ram54288 0:dbad57390bd1 267 void MbedClient::test_register(M2MObjectList object_list){
ram54288 0:dbad57390bd1 268 if(_interface) {
ram54288 0:dbad57390bd1 269 _interface->register_object(_register_security, object_list);
ram54288 0:dbad57390bd1 270 }
ram54288 0:dbad57390bd1 271 }
ram54288 0:dbad57390bd1 272
ram54288 0:dbad57390bd1 273 void MbedClient::set_register_object(M2MSecurity *&register_object){
ram54288 0:dbad57390bd1 274 if(_register_security) {
ram54288 0:dbad57390bd1 275 delete _register_security;
ram54288 0:dbad57390bd1 276 _register_security = NULL;
ram54288 0:dbad57390bd1 277 }
ram54288 0:dbad57390bd1 278 _register_security = register_object;
ram54288 0:dbad57390bd1 279
ram54288 0:dbad57390bd1 280 }
ram54288 0:dbad57390bd1 281
ram54288 0:dbad57390bd1 282 void MbedClient::test_update_register() {
ram54288 0:dbad57390bd1 283 uint32_t updated_lifetime = 20;
ram54288 0:dbad57390bd1 284 _registered = false;
ram54288 0:dbad57390bd1 285 _unregistered = false;
ram54288 0:dbad57390bd1 286 if(_interface){
ram54288 0:dbad57390bd1 287 _interface->update_registration(_register_security,updated_lifetime);
ram54288 0:dbad57390bd1 288 }
ram54288 0:dbad57390bd1 289 }
ram54288 0:dbad57390bd1 290
ram54288 0:dbad57390bd1 291 void MbedClient::test_unregister() {
ram54288 0:dbad57390bd1 292 if(_interface) {
ram54288 0:dbad57390bd1 293 _interface->unregister_object(NULL);
ram54288 0:dbad57390bd1 294 }
ram54288 0:dbad57390bd1 295 }
ram54288 0:dbad57390bd1 296
ram54288 0:dbad57390bd1 297 void MbedClient::bootstrap_done(M2MSecurity *server_object){
ram54288 0:dbad57390bd1 298 if(server_object) {
ram54288 0:dbad57390bd1 299 set_register_object(server_object);
ram54288 0:dbad57390bd1 300 _bootstrapped = true;
ram54288 0:dbad57390bd1 301 _error = false;
ram54288 0:dbad57390bd1 302 printf("\nBootstrapped\n");
ram54288 0:dbad57390bd1 303 }
ram54288 0:dbad57390bd1 304 }
ram54288 0:dbad57390bd1 305
ram54288 0:dbad57390bd1 306 void MbedClient::object_registered(M2MSecurity */*security_object*/, const M2MServer &/*server_object*/){
ram54288 0:dbad57390bd1 307 _registered = true;
ram54288 0:dbad57390bd1 308 _unregistered = false;
ram54288 0:dbad57390bd1 309 printf("\nRegistered\n");
ram54288 0:dbad57390bd1 310 }
ram54288 0:dbad57390bd1 311
ram54288 0:dbad57390bd1 312 void MbedClient::object_unregistered(M2MSecurity */*server_object*/){
ram54288 0:dbad57390bd1 313 _unregistered = true;
ram54288 0:dbad57390bd1 314 _registered = false;
ram54288 0:dbad57390bd1 315
ram54288 0:dbad57390bd1 316 #ifdef TARGET_LIKE_MBED
ram54288 0:dbad57390bd1 317 notify_completion(_unregistered);
ram54288 0:dbad57390bd1 318 minar::Scheduler::stop();
ram54288 0:dbad57390bd1 319 #endif
ram54288 0:dbad57390bd1 320 printf("\nUnregistered\n");
ram54288 0:dbad57390bd1 321 }
ram54288 0:dbad57390bd1 322
ram54288 0:dbad57390bd1 323 void MbedClient::registration_updated(M2MSecurity */*security_object*/, const M2MServer & /*server_object*/){
ram54288 0:dbad57390bd1 324 _registration_updated = true;
ram54288 0:dbad57390bd1 325 printf("\nregistration updated\n");
ram54288 0:dbad57390bd1 326 }
ram54288 0:dbad57390bd1 327
ram54288 0:dbad57390bd1 328 void MbedClient::error(M2MInterface::Error error){
ram54288 0:dbad57390bd1 329 _error = true;
ram54288 0:dbad57390bd1 330 printf("\nError occured Error code %d\n", (int)error);
ram54288 0:dbad57390bd1 331 }
ram54288 0:dbad57390bd1 332
ram54288 0:dbad57390bd1 333 void MbedClient::value_updated(M2MBase *base, M2MBase::BaseType type) {
ram54288 0:dbad57390bd1 334 printf("\nValue updated of Object name %s and Type %d\n",
ram54288 0:dbad57390bd1 335 base->name(), type);
ram54288 0:dbad57390bd1 336 }