Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbedclient.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 00017 #include "mbedclient.h" 00018 00019 #include "mbed-client/m2minterfacefactory.h" 00020 #include "mbed-client/m2mdevice.h" 00021 #include "mbed-client/m2mobjectinstance.h" 00022 #include "mbed-client/m2mresource.h" 00023 00024 #ifdef TARGET_LIKE_MBED 00025 #include "minar/minar.h" 00026 #include "test_env.h" 00027 #endif 00028 00029 #include <time.h> 00030 #include <stdio.h> 00031 00032 // Enter your mbed Device Server's IPv4 address and Port number in 00033 // mentioned format like 192.168.0.1:5693 00034 const String &BOOTSTRAP_SERVER_ADDRESS = "coap://10.45.3.10:5693"; 00035 const String &BOOTSTRAP_SERVER_DTLS_ADDRESS = "coap://10.45.3.10:5694"; 00036 00037 #ifdef SIXLOWPAN_INTERFACE 00038 const String &MBED_SERVER_ADDRESS = "coap://FD00:FF1:CE0B:A5E1:1068:AF13:9B61:D557:5683"; 00039 const String &MBED_SERVER_DTLS_ADDRESS = "coap://FD00:FF1:CE0B:A5E1:1068:AF13:9B61:D557:5684"; 00040 #else 00041 const String &MBED_SERVER_ADDRESS = "coap://10.45.3.10:5683"; 00042 //const String &MBED_SERVER_ADDRESS = "coap://10.45.0.152:5683"; 00043 const String &MBED_SERVER_DTLS_ADDRESS = "coap://10.45.3.10:5684"; 00044 #endif 00045 const String CLIENT_NAME = "secure-client"; 00046 00047 const String &MANUFACTURER = "ARM"; 00048 const String &TYPE = "type"; 00049 const String &MODEL_NUMBER = "2015"; 00050 const String &SERIAL_NUMBER = "12345"; 00051 const uint8_t STATIC_VALUE[] = "Static value"; 00052 00053 #ifdef TARGET_LIKE_LINUX 00054 #include <unistd.h> 00055 #else 00056 00057 #endif 00058 00059 MbedClient::MbedClient() 00060 #ifndef TARGET_LIKE_LINUX 00061 :_led(LED3) 00062 #endif 00063 { 00064 _security = NULL; 00065 _interface = NULL; 00066 _register_security = NULL; 00067 _device = NULL; 00068 _object = NULL; 00069 _bootstrapped = false; 00070 _error = false; 00071 _registered = false; 00072 _unregistered = false; 00073 _registration_updated = false; 00074 _value = 0; 00075 } 00076 00077 MbedClient::~MbedClient() { 00078 if(_security) { 00079 delete _security; 00080 } 00081 if(_register_security){ 00082 delete _register_security; 00083 } 00084 if(_device) { 00085 M2MDevice::delete_instance(); 00086 } 00087 if(_object) { 00088 delete _object; 00089 } 00090 if(_interface) { 00091 delete _interface; 00092 } 00093 } 00094 00095 bool MbedClient::create_interface() { 00096 00097 M2MInterface::NetworkStack stack = M2MInterface::LwIP_IPv4; 00098 #ifdef SIXLOWPAN_INTERFACE 00099 stack = M2MInterface::Nanostack_IPv6; 00100 #endif 00101 00102 /* From http://www.iana.org/assignments/port-numbers: 00103 "The Dynamic and/or Private Ports are those from 49152 through 65535" */ 00104 srand(time(NULL)); 00105 uint16_t port = (rand() % (65535-49152)) + 49152; 00106 00107 _interface = M2MInterfaceFactory::create_interface(*this, 00108 CLIENT_NAME, 00109 "test", 00110 60, 00111 port, 00112 "", 00113 M2MInterface::UDP, 00114 stack, 00115 ""); 00116 00117 return (_interface == NULL) ? false : true; 00118 } 00119 00120 bool MbedClient::bootstrap_successful() { 00121 #ifdef TARGET_LIKE_LINUX 00122 while(!_bootstrapped && !_error) { 00123 sleep(1); 00124 } 00125 #endif 00126 return _bootstrapped; 00127 } 00128 00129 bool MbedClient::register_successful() { 00130 #ifdef TARGET_LIKE_LINUX 00131 while(!_registered && !_error) { 00132 sleep(1); 00133 } 00134 #endif 00135 return _registered; 00136 } 00137 00138 bool MbedClient::unregister_successful() { 00139 #ifdef TARGET_LIKE_LINUX 00140 while(!_unregistered && !_error) { 00141 sleep(1); 00142 } 00143 #endif 00144 return _unregistered; 00145 } 00146 00147 bool MbedClient::registration_update_successful() { 00148 #ifdef TARGET_LIKE_LINUX 00149 while(!_registration_updated && !_error) { 00150 sleep(1); 00151 } 00152 #endif 00153 return _registration_updated; 00154 } 00155 00156 M2MSecurity* MbedClient::create_bootstrap_object(bool useSecureConnection) { 00157 // Creates bootstrap server object with Bootstrap server address and other parameters 00158 // required for client to connect to bootstrap server. 00159 M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::Bootstrap); 00160 if(security) { 00161 if(useSecureConnection){ 00162 security->set_resource_value(M2MSecurity::M2MServerUri, BOOTSTRAP_SERVER_DTLS_ADDRESS); 00163 //TODO: remove these, when bootstrapping server supports DTLS 00164 delete security; 00165 return NULL; 00166 }else{ 00167 security->set_resource_value(M2MSecurity::M2MServerUri, BOOTSTRAP_SERVER_ADDRESS); 00168 //TODO: refactor this out 00169 security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity); 00170 } 00171 } 00172 return security; 00173 } 00174 00175 M2MSecurity* MbedClient::create_register_object(bool useSecureConnection) { 00176 // Creates bootstrap server object with Bootstrap server address and other parameters 00177 // required for client to connect to bootstrap server. 00178 M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer); 00179 if(security) { 00180 if(useSecureConnection){ 00181 security->set_resource_value(M2MSecurity::M2MServerUri, MBED_SERVER_DTLS_ADDRESS); 00182 }else{ 00183 security->set_resource_value(M2MSecurity::M2MServerUri, MBED_SERVER_ADDRESS); 00184 } 00185 } 00186 return security; 00187 } 00188 00189 void MbedClient::test_bootstrap(M2MSecurity *security) { 00190 if(_interface) { 00191 // Bootstrap function. 00192 _interface->bootstrap(security); 00193 } 00194 } 00195 00196 M2MDevice* MbedClient::create_device_object() { 00197 // Creates device object which contains mandatory resources linked with 00198 // device endpoint. 00199 M2MDevice *device = M2MInterfaceFactory::create_device(); 00200 if(device) { 00201 device->create_resource(M2MDevice::Manufacturer,MANUFACTURER); 00202 device->create_resource(M2MDevice::DeviceType,TYPE); 00203 device->create_resource(M2MDevice::ModelNumber,MODEL_NUMBER); 00204 device->create_resource(M2MDevice::SerialNumber,SERIAL_NUMBER); 00205 } 00206 return device; 00207 } 00208 00209 void MbedClient::execute_function(void *argument) { 00210 #ifdef TARGET_LIKE_LINUX 00211 if(argument) { 00212 char* arguments = (char*)argument; 00213 printf("Received %s!!\n", arguments); 00214 } 00215 printf("I am executed !!\n"); 00216 #else 00217 _led == 0 ? _led = 1 : _led = 0; 00218 #endif 00219 } 00220 00221 M2MObject* MbedClient::create_generic_object() { 00222 _object = M2MInterfaceFactory::create_object("Test"); 00223 if(_object) { 00224 M2MObjectInstance* inst = _object->create_object_instance(); 00225 if(inst) { 00226 inst->set_operation(M2MBase::GET_ALLOWED); 00227 00228 M2MResource* res = inst->create_dynamic_resource("Dynamic", 00229 "ResourceTest", 00230 M2MResourceInstance::INTEGER, 00231 true); 00232 00233 char buffer[20]; 00234 int size = sprintf(buffer,"%d",_value); 00235 res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); 00236 res->set_value((const uint8_t*)buffer, 00237 (const uint32_t)size); 00238 res->set_execute_function(execute_callback (this,&MbedClient::execute_function)); 00239 _value++; 00240 00241 inst->create_static_resource("Static", 00242 "ResourceTest", 00243 M2MResourceInstance::STRING, 00244 STATIC_VALUE, 00245 sizeof(STATIC_VALUE)-1); 00246 } 00247 } 00248 return _object; 00249 } 00250 00251 void MbedClient::update_resource() { 00252 if(_object) { 00253 M2MObjectInstance* inst = _object->object_instance(); 00254 if(inst) { 00255 M2MResource* res = inst->resource("Dynamic"); 00256 if( res ){ 00257 char buffer[20]; 00258 int size = sprintf(buffer,"%d",_value); 00259 res->set_value((const uint8_t*)buffer, 00260 (const uint32_t)size); 00261 _value++; 00262 } 00263 } 00264 } 00265 } 00266 00267 void MbedClient::test_register(M2MObjectList object_list){ 00268 if(_interface) { 00269 _interface->register_object(_register_security, object_list); 00270 } 00271 } 00272 00273 void MbedClient::set_register_object(M2MSecurity *®ister_object){ 00274 if(_register_security) { 00275 delete _register_security; 00276 _register_security = NULL; 00277 } 00278 _register_security = register_object; 00279 00280 } 00281 00282 void MbedClient::test_update_register() { 00283 uint32_t updated_lifetime = 20; 00284 _registered = false; 00285 _unregistered = false; 00286 if(_interface){ 00287 _interface->update_registration(_register_security,updated_lifetime); 00288 } 00289 } 00290 00291 void MbedClient::test_unregister() { 00292 if(_interface) { 00293 _interface->unregister_object(NULL); 00294 } 00295 } 00296 00297 void MbedClient::bootstrap_done(M2MSecurity *server_object){ 00298 if(server_object) { 00299 set_register_object(server_object); 00300 _bootstrapped = true; 00301 _error = false; 00302 printf("\nBootstrapped\n"); 00303 } 00304 } 00305 00306 void MbedClient::object_registered(M2MSecurity */*security_object*/, const M2MServer &/*server_object*/){ 00307 _registered = true; 00308 _unregistered = false; 00309 printf("\nRegistered\n"); 00310 } 00311 00312 void MbedClient::object_unregistered(M2MSecurity */*server_object*/){ 00313 _unregistered = true; 00314 _registered = false; 00315 00316 #ifdef TARGET_LIKE_MBED 00317 notify_completion(_unregistered); 00318 minar::Scheduler::stop(); 00319 #endif 00320 printf("\nUnregistered\n"); 00321 } 00322 00323 void MbedClient::registration_updated(M2MSecurity */*security_object*/, const M2MServer & /*server_object*/){ 00324 _registration_updated = true; 00325 printf("\nregistration updated\n"); 00326 } 00327 00328 void MbedClient::error(M2MInterface::Error error){ 00329 _error = true; 00330 printf("\nError occured Error code %d\n", (int)error); 00331 } 00332 00333 void MbedClient::value_updated(M2MBase *base, M2MBase::BaseType type) { 00334 printf("\nValue updated of Object name %s and Type %d\n", 00335 base->name(), type); 00336 }
Generated on Tue Jul 12 2022 21:20:28 by
1.7.2