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