dhgdh
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
main.cpp
00001 /* 00002 * Copyright (c) 2015 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include <unistd.h> 00017 #include <malloc.h> 00018 #include <stdio.h> 00019 #include <stdarg.h> 00020 #include <pthread.h> 00021 #include <signal.h> /* For SIGIGN and SIGINT */ 00022 #include "mbed-client/m2minterfacefactory.h" 00023 #include "mbed-client/m2mdevice.h" 00024 #include "mbed-client/m2minterfaceobserver.h" 00025 #include "mbed-client/m2minterface.h" 00026 #include "mbed-client/m2mobjectinstance.h" 00027 #include "mbed-client/m2mresource.h" 00028 00029 #include "mbed-trace/mbed_trace.h" 00030 00031 const String &BOOTSTRAP_SERVER_ADDRESS = "coap://10.45.3.10:5693"; 00032 const String &M2M_SERVER_ADDRESS = "coap://10.45.3.10:5683"; 00033 const String &MANUFACTURER = "manufacturer"; 00034 const String &TYPE = "type"; 00035 const String &MODEL_NUMBER = "2015"; 00036 const String &SERIAL_NUMBER = "12345"; 00037 00038 const uint8_t STATIC_VALUE[] = "Open Mobile Alliance"; 00039 00040 static void ctrl_c_handle_function(void); 00041 void close_function(); 00042 typedef void (*signalhandler_t)(int); /* Function pointer type for ctrl-c */ 00043 00044 class MbedClient: public M2MInterfaceObserver { 00045 public: 00046 MbedClient(){ 00047 _security = NULL; 00048 _interface = NULL; 00049 _register_security = NULL; 00050 _device = NULL; 00051 _object = NULL; 00052 _bootstrapped = false; 00053 _error = false; 00054 _registered = false; 00055 _unregistered = false; 00056 _registration_updated = false; 00057 _value = 0; 00058 } 00059 00060 ~MbedClient() { 00061 if(_security) { 00062 delete _security; 00063 } 00064 if(_register_security){ 00065 delete _register_security; 00066 } 00067 if(_device) { 00068 M2MDevice::delete_instance(); 00069 _device = NULL; 00070 } 00071 if(_object) { 00072 delete _object; 00073 } 00074 if(_interface) { 00075 delete _interface; 00076 } 00077 } 00078 00079 bool create_interface() { 00080 _interface = M2MInterfaceFactory::create_interface(*this, 00081 "linux-endpoint", 00082 "test", 00083 60, 00084 5683, 00085 "", 00086 M2MInterface::UDP, 00087 M2MInterface::LwIP_IPv4, 00088 ""); 00089 printf("Endpoint Name : linux-endpoint\n"); 00090 return (_interface == NULL) ? false : true; 00091 } 00092 00093 bool bootstrap_successful() { 00094 while(!_bootstrapped && !_error) { 00095 sleep(1); 00096 } 00097 return _bootstrapped; 00098 } 00099 00100 bool register_successful() { 00101 while(!_registered && !_error) { 00102 sleep(1); 00103 } 00104 return _registered; 00105 } 00106 00107 bool unregister_successful() { 00108 while(!_unregistered && !_error) { 00109 sleep(1); 00110 } 00111 return _unregistered; 00112 } 00113 00114 bool registration_update_successful() { 00115 while(!_registration_updated && !_error) { 00116 } 00117 return _registration_updated; 00118 } 00119 00120 bool create_bootstrap_object() { 00121 bool success = false; 00122 if(_security) { 00123 delete _security; 00124 } 00125 _security = M2MInterfaceFactory::create_security(M2MSecurity::Bootstrap); 00126 if(_security) { 00127 if(_security->set_resource_value(M2MSecurity::M2MServerUri, BOOTSTRAP_SERVER_ADDRESS) && 00128 _security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity)) { 00129 success = true; 00130 /* Not used now because there is no TLS or DTLS implementation available for stack. 00131 security->set_resource_value(M2MSecurity::ServerPublicKey,certificates->certificate_ptr[0],certificates->certificate_len[0]); 00132 security->set_resource_value(M2MSecurity::PublicKey,certificates->certificate_ptr[1],certificates->certificate_len[1]); 00133 security->set_resource_value(M2MSecurity::Secretkey,certificates->own_private_key_ptr,certificates->own_private_key_len); 00134 */ 00135 } 00136 } 00137 printf("Bootstrap Server Address %s\n", BOOTSTRAP_SERVER_ADDRESS.c_str()); 00138 return success; 00139 } 00140 00141 bool create_register_object() { 00142 bool success = false; 00143 _register_security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer); 00144 if(_register_security) { 00145 if(_register_security->set_resource_value(M2MSecurity::M2MServerUri, M2M_SERVER_ADDRESS) && 00146 _register_security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity)) { 00147 success = true; 00148 /* Not used now because there is no TLS or DTLS implementation available for stack. 00149 security->set_resource_value(M2MSecurity::ServerPublicKey,certificates->certificate_ptr[0],certificates->certificate_len[0]); 00150 security->set_resource_value(M2MSecurity::PublicKey,certificates->certificate_ptr[1],certificates->certificate_len[1]); 00151 security->set_resource_value(M2MSecurity::Secretkey,certificates->own_private_key_ptr,certificates->own_private_key_len); 00152 */ 00153 } 00154 } 00155 return success; 00156 } 00157 00158 void test_bootstrap() { 00159 _interface->bootstrap(_security); 00160 } 00161 00162 bool create_device_object() { 00163 bool success = false; 00164 _device = M2MInterfaceFactory::create_device(); 00165 if(_device) { 00166 _device->object_instance()->set_operation(M2MBase::GET_PUT_POST_ALLOWED); 00167 if(_device->create_resource(M2MDevice::Manufacturer,MANUFACTURER) && 00168 _device->create_resource(M2MDevice::DeviceType,TYPE) && 00169 _device->create_resource(M2MDevice::ModelNumber,MODEL_NUMBER) && 00170 _device->create_resource(M2MDevice::SerialNumber,SERIAL_NUMBER)) { 00171 success = true; 00172 } 00173 } 00174 return success; 00175 } 00176 00177 void execute_function(void *argument) { 00178 if(argument) { 00179 char* arguments = (char*)argument; 00180 printf("Received %s!!\n", arguments); 00181 } 00182 printf("I am executed !!\n"); 00183 } 00184 00185 bool create_generic_object() { 00186 bool success = false; 00187 _object = M2MInterfaceFactory::create_object("10"); 00188 if(_object) { 00189 _object->set_operation(M2MBase::GET_PUT_POST_ALLOWED); 00190 M2MObjectInstance* inst = _object->create_object_instance(); 00191 if(inst) { 00192 inst->set_operation(M2MBase::GET_PUT_POST_ALLOWED); 00193 inst->set_observable(false); 00194 char buffer[20]; 00195 int size = sprintf(buffer,"%d",_value); 00196 00197 inst->create_static_resource("0", 00198 "ResourceTest", 00199 M2MResourceInstance::INTEGER, 00200 STATIC_VALUE, 00201 sizeof(STATIC_VALUE)-1); 00202 00203 M2MResourceInstance* instance = inst->create_dynamic_resource_instance("1", 00204 "ResourceTest", 00205 M2MResourceInstance::INTEGER, 00206 true,0); 00207 00208 if(instance) { 00209 instance->set_operation(M2MBase::GET_PUT_POST_ALLOWED); 00210 instance->set_value((const uint8_t*)buffer, 00211 (const uint32_t)size); 00212 instance->set_execute_function(execute_callback (this,&MbedClient::execute_function)); 00213 _value++; 00214 } 00215 } 00216 } 00217 return success; 00218 } 00219 00220 void update_resource() { 00221 if(_object) { 00222 M2MObjectInstance* inst = _object->object_instance(); 00223 if(inst) { 00224 M2MResource* res = inst->resource("1"); 00225 res = inst->resource("1"); 00226 if(res) { 00227 M2MResourceInstance *res_inst = res->resource_instance(0); 00228 if(res_inst) { 00229 char buffer1[20]; 00230 int size1 = sprintf(buffer1,"%d",_value); 00231 res_inst->set_value((const uint8_t*)buffer1, 00232 (const uint32_t)size1); 00233 _value++; 00234 } 00235 } 00236 } 00237 } 00238 } 00239 00240 void test_register(){ 00241 M2MObjectList object_list; 00242 object_list.push_back(_device); 00243 object_list.push_back(_object); 00244 00245 _interface->register_object(_register_security,object_list); 00246 } 00247 00248 void test_update_register() { 00249 uint32_t updated_lifetime = 20; 00250 _registered = false; 00251 _unregistered = false; 00252 _interface->update_registration(_register_security,updated_lifetime); 00253 } 00254 00255 void test_unregister() { 00256 _interface->unregister_object(NULL); 00257 } 00258 00259 void bootstrap_done(M2MSecurity *server_object){ 00260 if(server_object) { 00261 _register_security = server_object; 00262 _bootstrapped = true; 00263 printf("\nBootstrapped\n"); 00264 printf("mDS Address %s\n", 00265 _register_security->resource_value_string(M2MSecurity::M2MServerUri).c_str()); 00266 } 00267 } 00268 00269 void object_registered(M2MSecurity */*security_object*/, const M2MServer &/*server_object*/){ 00270 _registered = true; 00271 printf("\nRegistered\n"); 00272 } 00273 00274 void object_unregistered(M2MSecurity */*server_object*/){ 00275 _unregistered = true; 00276 printf("\nUnregistered\n"); 00277 } 00278 00279 void registration_updated(M2MSecurity */*security_object*/, const M2MServer & /*server_object*/){ 00280 _registration_updated = true; 00281 printf("\nregistration updated\n"); 00282 00283 } 00284 00285 void error(M2MInterface::Error error){ 00286 _error = true; 00287 close_function(); 00288 printf("\nError occured Error Code : %d\n", (int8_t)error); 00289 00290 } 00291 00292 void value_updated(M2MBase *base, M2MBase::BaseType type) { 00293 printf("\nValue updated of Object name %s and Type %d\n", 00294 base->name().c_str(), type); 00295 } 00296 00297 private: 00298 00299 M2MInterface *_interface; 00300 M2MSecurity *_security; 00301 M2MSecurity *_register_security; 00302 M2MDevice *_device; 00303 M2MObject *_object; 00304 bool _bootstrapped; 00305 bool _error; 00306 bool _registered; 00307 bool _unregistered; 00308 bool _registration_updated; 00309 int _value; 00310 }; 00311 00312 void* wait_for_bootstrap(void* arg) { 00313 MbedClient *client; 00314 client = (MbedClient*) arg; 00315 if(client->bootstrap_successful()) { 00316 printf("Registering endpoint\n"); 00317 client->test_register(); 00318 } 00319 return NULL; 00320 } 00321 00322 void* wait_for_unregister(void* arg) { 00323 MbedClient *client; 00324 client = (MbedClient*) arg; 00325 if(client->unregister_successful()) { 00326 printf("Unregistered done --> exiting\n"); 00327 close_function(); 00328 } 00329 return NULL; 00330 } 00331 00332 void* send_observation(void* arg) { 00333 MbedClient *client; 00334 client = (MbedClient*) arg; 00335 static uint8_t counter = 0; 00336 while(1) { 00337 sleep(1); 00338 if(counter >= 5 && 00339 client->register_successful()) { 00340 printf("Sending observation\n"); 00341 client->update_resource(); 00342 counter = 0; 00343 } 00344 else 00345 counter++; 00346 } 00347 return NULL; 00348 } 00349 00350 static MbedClient *m2mclient = NULL; 00351 00352 static void ctrl_c_handle_function(void) 00353 { 00354 if(m2mclient && m2mclient->register_successful()) { 00355 printf("Unregistering endpoint\n"); 00356 m2mclient->test_unregister(); 00357 } 00358 } 00359 00360 void trace_printer(const char* str) 00361 { 00362 printf("%s\r\n", str); 00363 } 00364 00365 static pthread_t bootstrap_thread; 00366 static pthread_t unregister_thread; 00367 static pthread_t observation_thread; 00368 00369 void close_function() { 00370 pthread_cancel(bootstrap_thread); 00371 pthread_cancel(unregister_thread); 00372 pthread_cancel(observation_thread); 00373 } 00374 00375 int main() { 00376 00377 MbedClient mbed_client; 00378 00379 m2mclient = &mbed_client; 00380 00381 mbed_trace_init(); 00382 mbed_trace_print_function_set( trace_printer ); 00383 mbed_trace_config_set(TRACE_MODE_COLOR|TRACE_ACTIVE_LEVEL_DEBUG|TRACE_CARRIAGE_RETURN); 00384 00385 signal(SIGINT, (signalhandler_t)ctrl_c_handle_function); 00386 00387 bool result = mbed_client.create_interface(); 00388 if(true == result) { 00389 printf("\nInterface created\n"); 00390 } 00391 result = mbed_client.create_bootstrap_object(); 00392 if(true == result) { 00393 printf("Bootstrap object created"); 00394 } 00395 00396 result = mbed_client.create_register_object(); 00397 if(true == result) { 00398 printf("Register object created"); 00399 } 00400 00401 result = mbed_client.create_device_object(); 00402 if(true == result){ 00403 printf("\nDevice object created !!\n"); 00404 } 00405 00406 result = mbed_client.create_generic_object(); 00407 00408 if(true == result) { 00409 printf("\nGeneric object created\n"); 00410 } 00411 00412 // printf("Bootstrapping endpoint\n"); 00413 // mbed_client.test_bootstrap(); 00414 00415 printf("Registering endpoint\n"); 00416 mbed_client.test_register(); 00417 00418 00419 pthread_create(&bootstrap_thread, NULL, &wait_for_bootstrap, (void*) &mbed_client); 00420 pthread_create(&observation_thread, NULL, &send_observation, (void*) &mbed_client); 00421 pthread_create(&unregister_thread, NULL, &wait_for_unregister, (void*) &mbed_client); 00422 00423 pthread_join(bootstrap_thread, NULL); 00424 pthread_join(unregister_thread, NULL); 00425 pthread_join(observation_thread, NULL); 00426 00427 exit(EXIT_SUCCESS); 00428 } 00429
Generated on Tue Jul 12 2022 11:01:53 by
1.7.2
