A metronome using the FRDM K64F board

Committer:
ram54288
Date:
Sun May 14 18:40:18 2017 +0000
Revision:
0:a7a43371b306
Initial commit

Who changed what in which revision?

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