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 <unistd.h>
ram54288 0:a2cb7295a1f7 17 #include <malloc.h>
ram54288 0:a2cb7295a1f7 18 #include <stdio.h>
ram54288 0:a2cb7295a1f7 19 #include <stdarg.h>
ram54288 0:a2cb7295a1f7 20 #include <pthread.h>
ram54288 0:a2cb7295a1f7 21 #include <signal.h> /* For SIGIGN and SIGINT */
ram54288 0:a2cb7295a1f7 22 #include "mbed-client/m2minterfacefactory.h"
ram54288 0:a2cb7295a1f7 23 #include "mbed-client/m2mdevice.h"
ram54288 0:a2cb7295a1f7 24 #include "mbed-client/m2minterfaceobserver.h"
ram54288 0:a2cb7295a1f7 25 #include "mbed-client/m2minterface.h"
ram54288 0:a2cb7295a1f7 26 #include "mbed-client/m2mobjectinstance.h"
ram54288 0:a2cb7295a1f7 27 #include "mbed-client/m2mresource.h"
ram54288 0:a2cb7295a1f7 28
ram54288 0:a2cb7295a1f7 29 #include "mbed-trace/mbed_trace.h"
ram54288 0:a2cb7295a1f7 30
ram54288 0:a2cb7295a1f7 31 const String &BOOTSTRAP_SERVER_ADDRESS = "coap://10.45.3.10:5693";
ram54288 0:a2cb7295a1f7 32 const String &M2M_SERVER_ADDRESS = "coap://10.45.3.10:5683";
ram54288 0:a2cb7295a1f7 33 const String &MANUFACTURER = "manufacturer";
ram54288 0:a2cb7295a1f7 34 const String &TYPE = "type";
ram54288 0:a2cb7295a1f7 35 const String &MODEL_NUMBER = "2015";
ram54288 0:a2cb7295a1f7 36 const String &SERIAL_NUMBER = "12345";
ram54288 0:a2cb7295a1f7 37
ram54288 0:a2cb7295a1f7 38 const uint8_t STATIC_VALUE[] = "Open Mobile Alliance";
ram54288 0:a2cb7295a1f7 39
ram54288 0:a2cb7295a1f7 40 static void ctrl_c_handle_function(void);
ram54288 0:a2cb7295a1f7 41 void close_function();
ram54288 0:a2cb7295a1f7 42 typedef void (*signalhandler_t)(int); /* Function pointer type for ctrl-c */
ram54288 0:a2cb7295a1f7 43
ram54288 0:a2cb7295a1f7 44 class MbedClient: public M2MInterfaceObserver {
ram54288 0:a2cb7295a1f7 45 public:
ram54288 0:a2cb7295a1f7 46 MbedClient(){
ram54288 0:a2cb7295a1f7 47 _security = NULL;
ram54288 0:a2cb7295a1f7 48 _interface = NULL;
ram54288 0:a2cb7295a1f7 49 _register_security = NULL;
ram54288 0:a2cb7295a1f7 50 _device = NULL;
ram54288 0:a2cb7295a1f7 51 _object = NULL;
ram54288 0:a2cb7295a1f7 52 _bootstrapped = false;
ram54288 0:a2cb7295a1f7 53 _error = false;
ram54288 0:a2cb7295a1f7 54 _registered = false;
ram54288 0:a2cb7295a1f7 55 _unregistered = false;
ram54288 0:a2cb7295a1f7 56 _registration_updated = false;
ram54288 0:a2cb7295a1f7 57 _value = 0;
ram54288 0:a2cb7295a1f7 58 }
ram54288 0:a2cb7295a1f7 59
ram54288 0:a2cb7295a1f7 60 ~MbedClient() {
ram54288 0:a2cb7295a1f7 61 if(_security) {
ram54288 0:a2cb7295a1f7 62 delete _security;
ram54288 0:a2cb7295a1f7 63 }
ram54288 0:a2cb7295a1f7 64 if(_register_security){
ram54288 0:a2cb7295a1f7 65 delete _register_security;
ram54288 0:a2cb7295a1f7 66 }
ram54288 0:a2cb7295a1f7 67 if(_device) {
ram54288 0:a2cb7295a1f7 68 M2MDevice::delete_instance();
ram54288 0:a2cb7295a1f7 69 _device = NULL;
ram54288 0:a2cb7295a1f7 70 }
ram54288 0:a2cb7295a1f7 71 if(_object) {
ram54288 0:a2cb7295a1f7 72 delete _object;
ram54288 0:a2cb7295a1f7 73 }
ram54288 0:a2cb7295a1f7 74 if(_interface) {
ram54288 0:a2cb7295a1f7 75 delete _interface;
ram54288 0:a2cb7295a1f7 76 }
ram54288 0:a2cb7295a1f7 77 }
ram54288 0:a2cb7295a1f7 78
ram54288 0:a2cb7295a1f7 79 bool create_interface() {
ram54288 0:a2cb7295a1f7 80 _interface = M2MInterfaceFactory::create_interface(*this,
ram54288 0:a2cb7295a1f7 81 "linux-endpoint",
ram54288 0:a2cb7295a1f7 82 "test",
ram54288 0:a2cb7295a1f7 83 60,
ram54288 0:a2cb7295a1f7 84 5683,
ram54288 0:a2cb7295a1f7 85 "",
ram54288 0:a2cb7295a1f7 86 M2MInterface::UDP,
ram54288 0:a2cb7295a1f7 87 M2MInterface::LwIP_IPv4,
ram54288 0:a2cb7295a1f7 88 "");
ram54288 0:a2cb7295a1f7 89 printf("Endpoint Name : linux-endpoint\n");
ram54288 0:a2cb7295a1f7 90 return (_interface == NULL) ? false : true;
ram54288 0:a2cb7295a1f7 91 }
ram54288 0:a2cb7295a1f7 92
ram54288 0:a2cb7295a1f7 93 bool bootstrap_successful() {
ram54288 0:a2cb7295a1f7 94 while(!_bootstrapped && !_error) {
ram54288 0:a2cb7295a1f7 95 sleep(1);
ram54288 0:a2cb7295a1f7 96 }
ram54288 0:a2cb7295a1f7 97 return _bootstrapped;
ram54288 0:a2cb7295a1f7 98 }
ram54288 0:a2cb7295a1f7 99
ram54288 0:a2cb7295a1f7 100 bool register_successful() {
ram54288 0:a2cb7295a1f7 101 while(!_registered && !_error) {
ram54288 0:a2cb7295a1f7 102 sleep(1);
ram54288 0:a2cb7295a1f7 103 }
ram54288 0:a2cb7295a1f7 104 return _registered;
ram54288 0:a2cb7295a1f7 105 }
ram54288 0:a2cb7295a1f7 106
ram54288 0:a2cb7295a1f7 107 bool unregister_successful() {
ram54288 0:a2cb7295a1f7 108 while(!_unregistered && !_error) {
ram54288 0:a2cb7295a1f7 109 sleep(1);
ram54288 0:a2cb7295a1f7 110 }
ram54288 0:a2cb7295a1f7 111 return _unregistered;
ram54288 0:a2cb7295a1f7 112 }
ram54288 0:a2cb7295a1f7 113
ram54288 0:a2cb7295a1f7 114 bool registration_update_successful() {
ram54288 0:a2cb7295a1f7 115 while(!_registration_updated && !_error) {
ram54288 0:a2cb7295a1f7 116 }
ram54288 0:a2cb7295a1f7 117 return _registration_updated;
ram54288 0:a2cb7295a1f7 118 }
ram54288 0:a2cb7295a1f7 119
ram54288 0:a2cb7295a1f7 120 bool create_bootstrap_object() {
ram54288 0:a2cb7295a1f7 121 bool success = false;
ram54288 0:a2cb7295a1f7 122 if(_security) {
ram54288 0:a2cb7295a1f7 123 delete _security;
ram54288 0:a2cb7295a1f7 124 }
ram54288 0:a2cb7295a1f7 125 _security = M2MInterfaceFactory::create_security(M2MSecurity::Bootstrap);
ram54288 0:a2cb7295a1f7 126 if(_security) {
ram54288 0:a2cb7295a1f7 127 if(_security->set_resource_value(M2MSecurity::M2MServerUri, BOOTSTRAP_SERVER_ADDRESS) &&
ram54288 0:a2cb7295a1f7 128 _security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity)) {
ram54288 0:a2cb7295a1f7 129 success = true;
ram54288 0:a2cb7295a1f7 130 /* Not used now because there is no TLS or DTLS implementation available for stack.
ram54288 0:a2cb7295a1f7 131 security->set_resource_value(M2MSecurity::ServerPublicKey,certificates->certificate_ptr[0],certificates->certificate_len[0]);
ram54288 0:a2cb7295a1f7 132 security->set_resource_value(M2MSecurity::PublicKey,certificates->certificate_ptr[1],certificates->certificate_len[1]);
ram54288 0:a2cb7295a1f7 133 security->set_resource_value(M2MSecurity::Secretkey,certificates->own_private_key_ptr,certificates->own_private_key_len);
ram54288 0:a2cb7295a1f7 134 */
ram54288 0:a2cb7295a1f7 135 }
ram54288 0:a2cb7295a1f7 136 }
ram54288 0:a2cb7295a1f7 137 printf("Bootstrap Server Address %s\n", BOOTSTRAP_SERVER_ADDRESS.c_str());
ram54288 0:a2cb7295a1f7 138 return success;
ram54288 0:a2cb7295a1f7 139 }
ram54288 0:a2cb7295a1f7 140
ram54288 0:a2cb7295a1f7 141 bool create_register_object() {
ram54288 0:a2cb7295a1f7 142 bool success = false;
ram54288 0:a2cb7295a1f7 143 _register_security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer);
ram54288 0:a2cb7295a1f7 144 if(_register_security) {
ram54288 0:a2cb7295a1f7 145 if(_register_security->set_resource_value(M2MSecurity::M2MServerUri, M2M_SERVER_ADDRESS) &&
ram54288 0:a2cb7295a1f7 146 _register_security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity)) {
ram54288 0:a2cb7295a1f7 147 success = true;
ram54288 0:a2cb7295a1f7 148 /* Not used now because there is no TLS or DTLS implementation available for stack.
ram54288 0:a2cb7295a1f7 149 security->set_resource_value(M2MSecurity::ServerPublicKey,certificates->certificate_ptr[0],certificates->certificate_len[0]);
ram54288 0:a2cb7295a1f7 150 security->set_resource_value(M2MSecurity::PublicKey,certificates->certificate_ptr[1],certificates->certificate_len[1]);
ram54288 0:a2cb7295a1f7 151 security->set_resource_value(M2MSecurity::Secretkey,certificates->own_private_key_ptr,certificates->own_private_key_len);
ram54288 0:a2cb7295a1f7 152 */
ram54288 0:a2cb7295a1f7 153 }
ram54288 0:a2cb7295a1f7 154 }
ram54288 0:a2cb7295a1f7 155 return success;
ram54288 0:a2cb7295a1f7 156 }
ram54288 0:a2cb7295a1f7 157
ram54288 0:a2cb7295a1f7 158 void test_bootstrap() {
ram54288 0:a2cb7295a1f7 159 _interface->bootstrap(_security);
ram54288 0:a2cb7295a1f7 160 }
ram54288 0:a2cb7295a1f7 161
ram54288 0:a2cb7295a1f7 162 bool create_device_object() {
ram54288 0:a2cb7295a1f7 163 bool success = false;
ram54288 0:a2cb7295a1f7 164 _device = M2MInterfaceFactory::create_device();
ram54288 0:a2cb7295a1f7 165 if(_device) {
ram54288 0:a2cb7295a1f7 166 _device->object_instance()->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
ram54288 0:a2cb7295a1f7 167 if(_device->create_resource(M2MDevice::Manufacturer,MANUFACTURER) &&
ram54288 0:a2cb7295a1f7 168 _device->create_resource(M2MDevice::DeviceType,TYPE) &&
ram54288 0:a2cb7295a1f7 169 _device->create_resource(M2MDevice::ModelNumber,MODEL_NUMBER) &&
ram54288 0:a2cb7295a1f7 170 _device->create_resource(M2MDevice::SerialNumber,SERIAL_NUMBER)) {
ram54288 0:a2cb7295a1f7 171 success = true;
ram54288 0:a2cb7295a1f7 172 }
ram54288 0:a2cb7295a1f7 173 }
ram54288 0:a2cb7295a1f7 174 return success;
ram54288 0:a2cb7295a1f7 175 }
ram54288 0:a2cb7295a1f7 176
ram54288 0:a2cb7295a1f7 177 void execute_function(void *argument) {
ram54288 0:a2cb7295a1f7 178 if(argument) {
ram54288 0:a2cb7295a1f7 179 char* arguments = (char*)argument;
ram54288 0:a2cb7295a1f7 180 printf("Received %s!!\n", arguments);
ram54288 0:a2cb7295a1f7 181 }
ram54288 0:a2cb7295a1f7 182 printf("I am executed !!\n");
ram54288 0:a2cb7295a1f7 183 }
ram54288 0:a2cb7295a1f7 184
ram54288 0:a2cb7295a1f7 185 bool create_generic_object() {
ram54288 0:a2cb7295a1f7 186 bool success = false;
ram54288 0:a2cb7295a1f7 187 _object = M2MInterfaceFactory::create_object("10");
ram54288 0:a2cb7295a1f7 188 if(_object) {
ram54288 0:a2cb7295a1f7 189 _object->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
ram54288 0:a2cb7295a1f7 190 M2MObjectInstance* inst = _object->create_object_instance();
ram54288 0:a2cb7295a1f7 191 if(inst) {
ram54288 0:a2cb7295a1f7 192 inst->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
ram54288 0:a2cb7295a1f7 193 inst->set_observable(false);
ram54288 0:a2cb7295a1f7 194 char buffer[20];
ram54288 0:a2cb7295a1f7 195 int size = sprintf(buffer,"%d",_value);
ram54288 0:a2cb7295a1f7 196
ram54288 0:a2cb7295a1f7 197 inst->create_static_resource("0",
ram54288 0:a2cb7295a1f7 198 "ResourceTest",
ram54288 0:a2cb7295a1f7 199 M2MResourceInstance::INTEGER,
ram54288 0:a2cb7295a1f7 200 STATIC_VALUE,
ram54288 0:a2cb7295a1f7 201 sizeof(STATIC_VALUE)-1);
ram54288 0:a2cb7295a1f7 202
ram54288 0:a2cb7295a1f7 203 M2MResourceInstance* instance = inst->create_dynamic_resource_instance("1",
ram54288 0:a2cb7295a1f7 204 "ResourceTest",
ram54288 0:a2cb7295a1f7 205 M2MResourceInstance::INTEGER,
ram54288 0:a2cb7295a1f7 206 true,0);
ram54288 0:a2cb7295a1f7 207
ram54288 0:a2cb7295a1f7 208 if(instance) {
ram54288 0:a2cb7295a1f7 209 instance->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
ram54288 0:a2cb7295a1f7 210 instance->set_value((const uint8_t*)buffer,
ram54288 0:a2cb7295a1f7 211 (const uint32_t)size);
ram54288 0:a2cb7295a1f7 212 instance->set_execute_function(execute_callback(this,&MbedClient::execute_function));
ram54288 0:a2cb7295a1f7 213 _value++;
ram54288 0:a2cb7295a1f7 214 }
ram54288 0:a2cb7295a1f7 215 }
ram54288 0:a2cb7295a1f7 216 }
ram54288 0:a2cb7295a1f7 217 return success;
ram54288 0:a2cb7295a1f7 218 }
ram54288 0:a2cb7295a1f7 219
ram54288 0:a2cb7295a1f7 220 void update_resource() {
ram54288 0:a2cb7295a1f7 221 if(_object) {
ram54288 0:a2cb7295a1f7 222 M2MObjectInstance* inst = _object->object_instance();
ram54288 0:a2cb7295a1f7 223 if(inst) {
ram54288 0:a2cb7295a1f7 224 M2MResource* res = inst->resource("1");
ram54288 0:a2cb7295a1f7 225 res = inst->resource("1");
ram54288 0:a2cb7295a1f7 226 if(res) {
ram54288 0:a2cb7295a1f7 227 M2MResourceInstance *res_inst = res->resource_instance(0);
ram54288 0:a2cb7295a1f7 228 if(res_inst) {
ram54288 0:a2cb7295a1f7 229 char buffer1[20];
ram54288 0:a2cb7295a1f7 230 int size1 = sprintf(buffer1,"%d",_value);
ram54288 0:a2cb7295a1f7 231 res_inst->set_value((const uint8_t*)buffer1,
ram54288 0:a2cb7295a1f7 232 (const uint32_t)size1);
ram54288 0:a2cb7295a1f7 233 _value++;
ram54288 0:a2cb7295a1f7 234 }
ram54288 0:a2cb7295a1f7 235 }
ram54288 0:a2cb7295a1f7 236 }
ram54288 0:a2cb7295a1f7 237 }
ram54288 0:a2cb7295a1f7 238 }
ram54288 0:a2cb7295a1f7 239
ram54288 0:a2cb7295a1f7 240 void test_register(){
ram54288 0:a2cb7295a1f7 241 M2MObjectList object_list;
ram54288 0:a2cb7295a1f7 242 object_list.push_back(_device);
ram54288 0:a2cb7295a1f7 243 object_list.push_back(_object);
ram54288 0:a2cb7295a1f7 244
ram54288 0:a2cb7295a1f7 245 _interface->register_object(_register_security,object_list);
ram54288 0:a2cb7295a1f7 246 }
ram54288 0:a2cb7295a1f7 247
ram54288 0:a2cb7295a1f7 248 void test_update_register() {
ram54288 0:a2cb7295a1f7 249 uint32_t updated_lifetime = 20;
ram54288 0:a2cb7295a1f7 250 _registered = false;
ram54288 0:a2cb7295a1f7 251 _unregistered = false;
ram54288 0:a2cb7295a1f7 252 _interface->update_registration(_register_security,updated_lifetime);
ram54288 0:a2cb7295a1f7 253 }
ram54288 0:a2cb7295a1f7 254
ram54288 0:a2cb7295a1f7 255 void test_unregister() {
ram54288 0:a2cb7295a1f7 256 _interface->unregister_object(NULL);
ram54288 0:a2cb7295a1f7 257 }
ram54288 0:a2cb7295a1f7 258
ram54288 0:a2cb7295a1f7 259 void bootstrap_done(M2MSecurity *server_object){
ram54288 0:a2cb7295a1f7 260 if(server_object) {
ram54288 0:a2cb7295a1f7 261 _register_security = server_object;
ram54288 0:a2cb7295a1f7 262 _bootstrapped = true;
ram54288 0:a2cb7295a1f7 263 printf("\nBootstrapped\n");
ram54288 0:a2cb7295a1f7 264 printf("mDS Address %s\n",
ram54288 0:a2cb7295a1f7 265 _register_security->resource_value_string(M2MSecurity::M2MServerUri).c_str());
ram54288 0:a2cb7295a1f7 266 }
ram54288 0:a2cb7295a1f7 267 }
ram54288 0:a2cb7295a1f7 268
ram54288 0:a2cb7295a1f7 269 void object_registered(M2MSecurity */*security_object*/, const M2MServer &/*server_object*/){
ram54288 0:a2cb7295a1f7 270 _registered = true;
ram54288 0:a2cb7295a1f7 271 printf("\nRegistered\n");
ram54288 0:a2cb7295a1f7 272 }
ram54288 0:a2cb7295a1f7 273
ram54288 0:a2cb7295a1f7 274 void object_unregistered(M2MSecurity */*server_object*/){
ram54288 0:a2cb7295a1f7 275 _unregistered = true;
ram54288 0:a2cb7295a1f7 276 printf("\nUnregistered\n");
ram54288 0:a2cb7295a1f7 277 }
ram54288 0:a2cb7295a1f7 278
ram54288 0:a2cb7295a1f7 279 void registration_updated(M2MSecurity */*security_object*/, const M2MServer & /*server_object*/){
ram54288 0:a2cb7295a1f7 280 _registration_updated = true;
ram54288 0:a2cb7295a1f7 281 printf("\nregistration updated\n");
ram54288 0:a2cb7295a1f7 282
ram54288 0:a2cb7295a1f7 283 }
ram54288 0:a2cb7295a1f7 284
ram54288 0:a2cb7295a1f7 285 void error(M2MInterface::Error error){
ram54288 0:a2cb7295a1f7 286 _error = true;
ram54288 0:a2cb7295a1f7 287 close_function();
ram54288 0:a2cb7295a1f7 288 printf("\nError occured Error Code : %d\n", (int8_t)error);
ram54288 0:a2cb7295a1f7 289
ram54288 0:a2cb7295a1f7 290 }
ram54288 0:a2cb7295a1f7 291
ram54288 0:a2cb7295a1f7 292 void value_updated(M2MBase *base, M2MBase::BaseType type) {
ram54288 0:a2cb7295a1f7 293 printf("\nValue updated of Object name %s and Type %d\n",
ram54288 0:a2cb7295a1f7 294 base->name(), type);
ram54288 0:a2cb7295a1f7 295 }
ram54288 0:a2cb7295a1f7 296
ram54288 0:a2cb7295a1f7 297 private:
ram54288 0:a2cb7295a1f7 298
ram54288 0:a2cb7295a1f7 299 M2MInterface *_interface;
ram54288 0:a2cb7295a1f7 300 M2MSecurity *_security;
ram54288 0:a2cb7295a1f7 301 M2MSecurity *_register_security;
ram54288 0:a2cb7295a1f7 302 M2MDevice *_device;
ram54288 0:a2cb7295a1f7 303 M2MObject *_object;
ram54288 0:a2cb7295a1f7 304 bool _bootstrapped;
ram54288 0:a2cb7295a1f7 305 bool _error;
ram54288 0:a2cb7295a1f7 306 bool _registered;
ram54288 0:a2cb7295a1f7 307 bool _unregistered;
ram54288 0:a2cb7295a1f7 308 bool _registration_updated;
ram54288 0:a2cb7295a1f7 309 int _value;
ram54288 0:a2cb7295a1f7 310 };
ram54288 0:a2cb7295a1f7 311
ram54288 0:a2cb7295a1f7 312 void* wait_for_bootstrap(void* arg) {
ram54288 0:a2cb7295a1f7 313 MbedClient *client;
ram54288 0:a2cb7295a1f7 314 client = (MbedClient*) arg;
ram54288 0:a2cb7295a1f7 315 if(client->bootstrap_successful()) {
ram54288 0:a2cb7295a1f7 316 printf("Registering endpoint\n");
ram54288 0:a2cb7295a1f7 317 client->test_register();
ram54288 0:a2cb7295a1f7 318 }
ram54288 0:a2cb7295a1f7 319 return NULL;
ram54288 0:a2cb7295a1f7 320 }
ram54288 0:a2cb7295a1f7 321
ram54288 0:a2cb7295a1f7 322 void* wait_for_unregister(void* arg) {
ram54288 0:a2cb7295a1f7 323 MbedClient *client;
ram54288 0:a2cb7295a1f7 324 client = (MbedClient*) arg;
ram54288 0:a2cb7295a1f7 325 if(client->unregister_successful()) {
ram54288 0:a2cb7295a1f7 326 printf("Unregistered done --> exiting\n");
ram54288 0:a2cb7295a1f7 327 close_function();
ram54288 0:a2cb7295a1f7 328 }
ram54288 0:a2cb7295a1f7 329 return NULL;
ram54288 0:a2cb7295a1f7 330 }
ram54288 0:a2cb7295a1f7 331
ram54288 0:a2cb7295a1f7 332 void* send_observation(void* arg) {
ram54288 0:a2cb7295a1f7 333 MbedClient *client;
ram54288 0:a2cb7295a1f7 334 client = (MbedClient*) arg;
ram54288 0:a2cb7295a1f7 335 static uint8_t counter = 0;
ram54288 0:a2cb7295a1f7 336 while(1) {
ram54288 0:a2cb7295a1f7 337 sleep(1);
ram54288 0:a2cb7295a1f7 338 if(counter >= 5 &&
ram54288 0:a2cb7295a1f7 339 client->register_successful()) {
ram54288 0:a2cb7295a1f7 340 printf("Sending observation\n");
ram54288 0:a2cb7295a1f7 341 client->update_resource();
ram54288 0:a2cb7295a1f7 342 counter = 0;
ram54288 0:a2cb7295a1f7 343 }
ram54288 0:a2cb7295a1f7 344 else
ram54288 0:a2cb7295a1f7 345 counter++;
ram54288 0:a2cb7295a1f7 346 }
ram54288 0:a2cb7295a1f7 347 return NULL;
ram54288 0:a2cb7295a1f7 348 }
ram54288 0:a2cb7295a1f7 349
ram54288 0:a2cb7295a1f7 350 static MbedClient *m2mclient = NULL;
ram54288 0:a2cb7295a1f7 351
ram54288 0:a2cb7295a1f7 352 static void ctrl_c_handle_function(void)
ram54288 0:a2cb7295a1f7 353 {
ram54288 0:a2cb7295a1f7 354 if(m2mclient && m2mclient->register_successful()) {
ram54288 0:a2cb7295a1f7 355 printf("Unregistering endpoint\n");
ram54288 0:a2cb7295a1f7 356 m2mclient->test_unregister();
ram54288 0:a2cb7295a1f7 357 }
ram54288 0:a2cb7295a1f7 358 }
ram54288 0:a2cb7295a1f7 359
ram54288 0:a2cb7295a1f7 360 void trace_printer(const char* str)
ram54288 0:a2cb7295a1f7 361 {
ram54288 0:a2cb7295a1f7 362 printf("%s\r\n", str);
ram54288 0:a2cb7295a1f7 363 }
ram54288 0:a2cb7295a1f7 364
ram54288 0:a2cb7295a1f7 365 static pthread_t bootstrap_thread;
ram54288 0:a2cb7295a1f7 366 static pthread_t unregister_thread;
ram54288 0:a2cb7295a1f7 367 static pthread_t observation_thread;
ram54288 0:a2cb7295a1f7 368
ram54288 0:a2cb7295a1f7 369 void close_function() {
ram54288 0:a2cb7295a1f7 370 pthread_cancel(bootstrap_thread);
ram54288 0:a2cb7295a1f7 371 pthread_cancel(unregister_thread);
ram54288 0:a2cb7295a1f7 372 pthread_cancel(observation_thread);
ram54288 0:a2cb7295a1f7 373 }
ram54288 0:a2cb7295a1f7 374
ram54288 0:a2cb7295a1f7 375 int main() {
ram54288 0:a2cb7295a1f7 376
ram54288 0:a2cb7295a1f7 377 MbedClient mbed_client;
ram54288 0:a2cb7295a1f7 378
ram54288 0:a2cb7295a1f7 379 m2mclient = &mbed_client;
ram54288 0:a2cb7295a1f7 380
ram54288 0:a2cb7295a1f7 381 mbed_trace_init();
ram54288 0:a2cb7295a1f7 382 mbed_trace_print_function_set( trace_printer );
ram54288 0:a2cb7295a1f7 383 mbed_trace_config_set(TRACE_MODE_COLOR|TRACE_ACTIVE_LEVEL_DEBUG|TRACE_CARRIAGE_RETURN);
ram54288 0:a2cb7295a1f7 384
ram54288 0:a2cb7295a1f7 385 signal(SIGINT, (signalhandler_t)ctrl_c_handle_function);
ram54288 0:a2cb7295a1f7 386
ram54288 0:a2cb7295a1f7 387 bool result = mbed_client.create_interface();
ram54288 0:a2cb7295a1f7 388 if(true == result) {
ram54288 0:a2cb7295a1f7 389 printf("\nInterface created\n");
ram54288 0:a2cb7295a1f7 390 }
ram54288 0:a2cb7295a1f7 391 result = mbed_client.create_bootstrap_object();
ram54288 0:a2cb7295a1f7 392 if(true == result) {
ram54288 0:a2cb7295a1f7 393 printf("Bootstrap object created");
ram54288 0:a2cb7295a1f7 394 }
ram54288 0:a2cb7295a1f7 395
ram54288 0:a2cb7295a1f7 396 result = mbed_client.create_register_object();
ram54288 0:a2cb7295a1f7 397 if(true == result) {
ram54288 0:a2cb7295a1f7 398 printf("Register object created");
ram54288 0:a2cb7295a1f7 399 }
ram54288 0:a2cb7295a1f7 400
ram54288 0:a2cb7295a1f7 401 result = mbed_client.create_device_object();
ram54288 0:a2cb7295a1f7 402 if(true == result){
ram54288 0:a2cb7295a1f7 403 printf("\nDevice object created !!\n");
ram54288 0:a2cb7295a1f7 404 }
ram54288 0:a2cb7295a1f7 405
ram54288 0:a2cb7295a1f7 406 result = mbed_client.create_generic_object();
ram54288 0:a2cb7295a1f7 407
ram54288 0:a2cb7295a1f7 408 if(true == result) {
ram54288 0:a2cb7295a1f7 409 printf("\nGeneric object created\n");
ram54288 0:a2cb7295a1f7 410 }
ram54288 0:a2cb7295a1f7 411
ram54288 0:a2cb7295a1f7 412 // printf("Bootstrapping endpoint\n");
ram54288 0:a2cb7295a1f7 413 // mbed_client.test_bootstrap();
ram54288 0:a2cb7295a1f7 414
ram54288 0:a2cb7295a1f7 415 printf("Registering endpoint\n");
ram54288 0:a2cb7295a1f7 416 mbed_client.test_register();
ram54288 0:a2cb7295a1f7 417
ram54288 0:a2cb7295a1f7 418
ram54288 0:a2cb7295a1f7 419 pthread_create(&bootstrap_thread, NULL, &wait_for_bootstrap, (void*) &mbed_client);
ram54288 0:a2cb7295a1f7 420 pthread_create(&observation_thread, NULL, &send_observation, (void*) &mbed_client);
ram54288 0:a2cb7295a1f7 421 pthread_create(&unregister_thread, NULL, &wait_for_unregister, (void*) &mbed_client);
ram54288 0:a2cb7295a1f7 422
ram54288 0:a2cb7295a1f7 423 pthread_join(bootstrap_thread, NULL);
ram54288 0:a2cb7295a1f7 424 pthread_join(unregister_thread, NULL);
ram54288 0:a2cb7295a1f7 425 pthread_join(observation_thread, NULL);
ram54288 0:a2cb7295a1f7 426
ram54288 0:a2cb7295a1f7 427 exit(EXIT_SUCCESS);
ram54288 0:a2cb7295a1f7 428 }
ram54288 0:a2cb7295a1f7 429