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.
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 00017 #include "mbedclient.h" 00018 00019 #ifdef TARGET_LIKE_LINUX 00020 #include <unistd.h> 00021 #include <stdio.h> 00022 #include <stdarg.h> 00023 #include <pthread.h> 00024 #include <signal.h> /* For SIGIGN and SIGINT */ 00025 #include <time.h> 00026 #else 00027 #include "sockets/UDPSocket.h" 00028 #ifdef SIXLOWPAN_INTERFACE 00029 #include "atmel-rf-driver/driverRFPhy.h" // rf_device_register 00030 #include "mbed-mesh-api/Mesh6LoWPAN_ND.h" 00031 #endif 00032 #include "EthernetInterface.h" 00033 #include "test_env.h" 00034 // TODO: Remove when yotta supports init. 00035 #include "lwipv4_init.h" 00036 using namespace mbed::util; 00037 #endif 00038 00039 #include "mbed-client/m2minterfacefactory.h" 00040 #include "mbed-client/m2mdevice.h" 00041 #include "mbed-client/m2minterfaceobserver.h" 00042 #include "mbed-client/m2minterface.h" 00043 #include "mbed-client/m2mobjectinstance.h" 00044 #include "mbed-client/m2mresource.h" 00045 #include "mbed-trace/mbed_trace.h" 00046 00047 #ifdef TARGET_LIKE_LINUX 00048 static void ctrl_c_handle_function(void); 00049 typedef void (*signalhandler_t)(int); /* Function pointer type for ctrl-c */ 00050 #else 00051 #if defined(TARGET_K64F) 00052 #define OBS_BUTTON SW2 00053 #define UNREG_BUTTON SW3 00054 #endif 00055 #endif 00056 00057 bool _have_secure_conn = false; 00058 00059 const unsigned char psk[] = {0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36}; 00060 const size_t psk_len = sizeof( psk ); 00061 const unsigned char psk_identity[] = {0x0F,0x0F}; 00062 const size_t psk_identity_len = sizeof( psk_identity ); 00063 00064 const uint8_t server_cert[] = "-----BEGIN CERTIFICATE-----\r\n" 00065 "MIIBmDCCAT6gAwIBAgIEVUCA0jAKBggqhkjOPQQDAjBLMQswCQYDVQQGEwJGSTEN\r\n" 00066 "MAsGA1UEBwwET3VsdTEMMAoGA1UECgwDQVJNMQwwCgYDVQQLDANJb1QxETAPBgNV\r\n" 00067 "BAMMCEFSTSBtYmVkMB4XDTE1MDQyOTA2NTc0OFoXDTE4MDQyOTA2NTc0OFowSzEL\r\n" 00068 "MAkGA1UEBhMCRkkxDTALBgNVBAcMBE91bHUxDDAKBgNVBAoMA0FSTTEMMAoGA1UE\r\n" 00069 "CwwDSW9UMREwDwYDVQQDDAhBUk0gbWJlZDBZMBMGByqGSM49AgEGCCqGSM49AwEH\r\n" 00070 "A0IABLuAyLSk0mA3awgFR5mw2RHth47tRUO44q/RdzFZnLsAsd18Esxd5LCpcT9w\r\n" 00071 "0tvNfBv4xJxGw0wcYrPDDb8/rjujEDAOMAwGA1UdEwQFMAMBAf8wCgYIKoZIzj0E\r\n" 00072 "AwIDSAAwRQIhAPAonEAkwixlJiyYRQQWpXtkMZax+VlEiS201BG0PpAzAiBh2RsD\r\n" 00073 "NxLKWwf4O7D6JasGBYf9+ZLwl0iaRjTjytO+Kw==\r\n" 00074 "-----END CERTIFICATE-----\r\n"; 00075 00076 const uint8_t cert[] = "-----BEGIN CERTIFICATE-----\r\n" 00077 "MIICTDCCAbWgAwIBAgIJAKI3S+LGklSGMA0GCSqGSIb3DQEBCwUAMD8xCzAJBgNV\r\n" 00078 "BAYTAkZJMRMwEQYDVQQIDApTb21lLVN0YXRlMQ0wCwYDVQQHDARPdWx1MQwwCgYD\r\n" 00079 "VQQKDANBUk0wHhcNMTUwNjExMTI0NTU1WhcNMTYwNjEwMTI0NTU1WjA/MQswCQYD\r\n" 00080 "VQQGEwJGSTETMBEGA1UECAwKU29tZS1TdGF0ZTENMAsGA1UEBwwET3VsdTEMMAoG\r\n" 00081 "A1UECgwDQVJNMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBt8ag12ILm2pn\r\n" 00082 "RHUc2yMBXpdzspDwVV3VDaNTRXypVUOq/nxJc6zr8yG/Pvga2/XVijWQDLABHVhV\r\n" 00083 "PoeulzXVSEHRR2bR2lhqulLzdUzPYJ+yJd4+082akoxdzoom8ms2LFlgiXO7lyBg\r\n" 00084 "1t74xjEQapCkr7Tdl2pD9OTUrcfoEwIDAQABo1AwTjAdBgNVHQ4EFgQUHK0O4iK7\r\n" 00085 "Rv8AsxAqRDlY3TSHWtUwHwYDVR0jBBgwFoAUHK0O4iK7Rv8AsxAqRDlY3TSHWtUw\r\n" 00086 "DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOBgQAPVLihYN65DEeYdf7gDAdP\r\n" 00087 "MNH5RxsQlHdktQkYWcUewe3hvOf8yZUBbmO5OeEQmy5Ca2H1QhUuw+kWHo1l/K9g\r\n" 00088 "DpR1xoDACeycBd/6tAHfaFzTEEDC9Ug839EAHDrbI4ihrPEbP6hyRIaUIM4IojXf\r\n" 00089 "zuqwOcobS3idv8a9Npuohw==\r\n" 00090 "-----END CERTIFICATE-----\r\n"; 00091 00092 const uint8_t key[] = "-----BEGIN PRIVATE KEY-----\r\n" 00093 "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMG3xqDXYgubamdE\r\n" 00094 "dRzbIwFel3OykPBVXdUNo1NFfKlVQ6r+fElzrOvzIb8++Brb9dWKNZAMsAEdWFU+\r\n" 00095 "h66XNdVIQdFHZtHaWGq6UvN1TM9gn7Il3j7TzZqSjF3OiibyazYsWWCJc7uXIGDW\r\n" 00096 "3vjGMRBqkKSvtN2XakP05NStx+gTAgMBAAECgYEAlRimNxsu6jIXJh4paSk8ewxb\r\n" 00097 "0v+n0GJT0uwpPakotofaihxOepYejIfX/IMxoDKVtmImsZUiTelRGGMFORpGomS7\r\n" 00098 "3Pb9awfn1yWiYja9MF7+pgFtXj6LxcB5rcxrP8s9rCs+kEq8yl/khGXmlwlpacUm\r\n" 00099 "Pdg1rEHriykk/5dlyKkCQQD9zpgQKN87bQNOVSgFSNsrwCjEN2MvVUC6ycqVYqQd\r\n" 00100 "oQZrO2lJuf57Jn7kRKJgfqqImwLYaohn5A9gKiqszo+fAkEAw2REr8L9aLoRPe5Q\r\n" 00101 "4+lYCmNmxJjBbJoRviSlxx+p5dxK+8H4J+bnFUWzORFJP0+Bjhii2UHGBoMjuP2s\r\n" 00102 "e8xDDQJADm0IrZp1Hb6TeodSkAJVGsaASq7PP2h8FmHT1uvVYi293Khy0pL0yPEm\r\n" 00103 "MzlHdW6dqMgOwFhFyonm6dNbu5i+BwJBALSPKfIKDMcEev5yBgo3Dga7iLJQPx4u\r\n" 00104 "2gqOLyvtlME3a4tu2fV1qUG9ITuxP7DjteMr3QFLiMVn1lETCpt0TNkCQGIvyjdV\r\n" 00105 "RzYnzLDMulXCsuRBjs0C75KoZrcPPQ416SZFtYASsOl3P0Ih5jgt/1aPmth3Yson\r\n" 00106 "GdnBOWyKzadLmz8=\r\n" 00107 "-----END PRIVATE KEY-----\r\n"; 00108 00109 const size_t server_cert_len = sizeof( server_cert ); 00110 const size_t cert_len = sizeof( cert ); 00111 const size_t key_len = sizeof( key ); 00112 00113 #ifdef TARGET_LIKE_LINUX 00114 00115 #if defined (BOOTSTRAP_ENABLED) 00116 void* wait_for_bootstrap(void* arg) { 00117 MbedClient *client; 00118 client = (MbedClient*) arg; 00119 if(client->bootstrap_successful()) { 00120 printf("Registering endpoint\n"); 00121 00122 // Create LWM2M device object specifying device resources 00123 // as per OMA LWM2M specification. 00124 M2MDevice* device_object = client->create_device_object(); 00125 00126 M2MObject* object = client->create_generic_object(); 00127 00128 // Add all the objects that you would like to register 00129 // into the list and pass the list for register API. 00130 M2MObjectList object_list; 00131 object_list.push_back(device_object); 00132 object_list.push_back(object); 00133 00134 // Issue register command. 00135 client->test_register(object_list); 00136 } 00137 return NULL; 00138 } 00139 #endif 00140 00141 void* wait_for_unregister(void* arg) { 00142 MbedClient *client; 00143 client = (MbedClient*) arg; 00144 if(client->unregister_successful()) { 00145 printf("Unregistered done --> exiting\n"); 00146 exit(1); 00147 } 00148 return NULL; 00149 } 00150 00151 void* send_observation(void* arg) { 00152 MbedClient *client; 00153 client = (MbedClient*) arg; 00154 static uint8_t counter = 0; 00155 while(1) { 00156 sleep(1); 00157 if(counter >= 10 && 00158 client->register_successful()) { 00159 printf("Sending observation\n"); 00160 client->update_resource(); 00161 counter = 0; 00162 } 00163 else 00164 counter++; 00165 } 00166 return NULL; 00167 } 00168 00169 static MbedClient *m2mclient = NULL; 00170 00171 static void ctrl_c_handle_function(void) 00172 { 00173 if(m2mclient && m2mclient->register_successful()) { 00174 printf("Unregistering endpoint\n"); 00175 m2mclient->test_unregister(); 00176 } 00177 } 00178 #else 00179 #ifdef SIXLOWPAN_INTERFACE 00180 volatile uint8_t mesh_network_state = MESH_DISCONNECTED; 00181 00182 void mesh_network_callback(mesh_connection_status_t mesh_state) 00183 { 00184 tr_info("Network established"); 00185 mesh_network_state = mesh_state; 00186 } 00187 #endif 00188 #endif 00189 00190 void trace_printer(const char* str) 00191 { 00192 printf("%s\r\n", str); 00193 } 00194 00195 #ifdef TARGET_LIKE_MBED 00196 void app_start(int /*argc*/, char* /*argv*/[]) { 00197 #else 00198 int main() { 00199 #endif 00200 00201 // Instantiate the class which implements 00202 // mbed Client API 00203 MbedClient mbed_client; 00204 00205 #ifdef TARGET_LIKE_LINUX 00206 pthread_t unregister_thread; 00207 pthread_t observation_thread; 00208 00209 m2mclient = &mbed_client; 00210 00211 mbed_trace_init(); 00212 mbed_trace_print_function_set( trace_printer ); 00213 mbed_trace_config_set(TRACE_MODE_COLOR|TRACE_ACTIVE_LEVEL_DEBUG|TRACE_CARRIAGE_RETURN); 00214 00215 signal(SIGINT, (signalhandler_t)ctrl_c_handle_function); 00216 00217 #else 00218 // This sets up the network interface configuration which will be used 00219 // by LWM2M Client API to communicate with mbed Device server. 00220 00221 #ifdef SIXLOWPAN_INTERFACE 00222 00223 Mesh6LoWPAN_ND *mesh_api = Mesh6LoWPAN_ND::getInstance(); 00224 int8_t status; 00225 00226 status = mesh_api->init(rf_device_register(), mesh_network_callback); 00227 if (status != MESH_ERROR_NONE) 00228 { 00229 tr_error("Mesh network initialization failed %d!", status); 00230 return 1; 00231 } 00232 00233 status = mesh_api->connect(); 00234 if (status != MESH_ERROR_NONE) 00235 { 00236 tr_error("Can't connect to mesh network!"); 00237 return 1; 00238 } 00239 00240 do 00241 { 00242 mesh_api->processEvent(); 00243 } while(mesh_network_state != MESH_CONNECTED); 00244 00245 00246 #else 00247 EthernetInterface eth; 00248 eth.init(); //Use DHCP 00249 eth.connect(); 00250 00251 lwipv4_socket_init(); 00252 #endif 00253 00254 // Set up Hardware interrupt button. 00255 InterruptIn obs_button(OBS_BUTTON); 00256 InterruptIn unreg_button(UNREG_BUTTON); 00257 00258 // On press of SW3 button on K64F board, example application 00259 // will call unregister API towards mbed Device Server 00260 unreg_button.fall(&mbed_client,&MbedClient::test_unregister); 00261 00262 // On press of SW2 button on K64F board, example application 00263 // will send observation towards mbed Device Server 00264 obs_button.fall(&mbed_client,&MbedClient::update_resource); 00265 00266 00267 #endif 00268 00269 // Create LWM2M Client API interface to manage bootstrap, 00270 // register and unregister 00271 00272 //These 2 are defining the secure connection usage 00273 M2MSecurity::SecurityModeType mode = M2MSecurity::NoSecurity; 00274 _have_secure_conn = (mode == M2MSecurity::Certificate || mode == M2MSecurity::Psk); 00275 // SecurityNotSet, 00276 // Psk = 0, 00277 // Certificate = 2, 00278 // NoSecurity = 3 00279 00280 mbed_client.create_interface(); 00281 00282 #ifdef TARGET_LIKE_LINUX 00283 00284 #if defined (BOOTSTRAP_ENABLED) 00285 pthread_t bootstrap_thread; 00286 // Create LWM2M bootstrap object specifying bootstrap server 00287 // information. 00288 M2MSecurity* security_object = mbed_client.create_bootstrap_object(_have_secure_conn); 00289 // Issue bootstrap command. 00290 mbed_client.test_bootstrap(security_object); 00291 00292 pthread_create(&bootstrap_thread, NULL, &wait_for_bootstrap, (void*) &mbed_client); 00293 pthread_join(bootstrap_thread, NULL); 00294 00295 #else 00296 M2MSecurity *register_object = mbed_client.create_register_object(_have_secure_conn); 00297 00298 if( mode == M2MSecurity::Certificate ){ 00299 register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate); 00300 register_object->set_resource_value(M2MSecurity::ServerPublicKey,server_cert,server_cert_len); 00301 register_object->set_resource_value(M2MSecurity::PublicKey,cert,cert_len); 00302 register_object->set_resource_value(M2MSecurity::Secretkey,key,key_len); 00303 }else if( mode == M2MSecurity::Psk ){ 00304 register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Psk); 00305 register_object->set_resource_value(M2MSecurity::ServerPublicKey,psk_identity,psk_identity_len); 00306 register_object->set_resource_value(M2MSecurity::PublicKey,psk_identity,psk_identity_len); 00307 register_object->set_resource_value(M2MSecurity::Secretkey,psk,psk_len); 00308 }else{ 00309 register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity); 00310 } 00311 00312 mbed_client.set_register_object(register_object); 00313 M2MDevice* device_object = mbed_client.create_device_object(); 00314 00315 M2MObject* object = mbed_client.create_generic_object(); 00316 00317 M2MObjectList object_list; 00318 object_list.push_back(device_object); 00319 object_list.push_back(object); 00320 00321 mbed_client.test_register(object_list); 00322 #endif 00323 00324 pthread_create(&observation_thread, NULL, &send_observation, (void*) &mbed_client); 00325 pthread_create(&unregister_thread, NULL, &wait_for_unregister, (void*) &mbed_client); 00326 pthread_join(unregister_thread, NULL); 00327 00328 #else 00329 00330 #if defined (BOOTSTRAP_ENABLED) 00331 // Create LWM2M bootstrap object specifying bootstrap server 00332 // information. 00333 M2MSecurity* security_object = mbed_client.create_bootstrap_object(_have_secure_conn); 00334 00335 // Issue bootstrap command. 00336 mbed_client.test_bootstrap(security_object); 00337 00338 // Wait till the bootstrap callback is called successfully. 00339 // Callback comes in bootstrap_done() 00340 #ifdef SIXLOWPAN_INTERFACE 00341 /* wait network to be established */ 00342 do { 00343 mesh_interface_run(); 00344 } while(!mbed_client.bootstrap_successful()); 00345 #else 00346 while (!mbed_client.bootstrap_successful()) { __WFI(); } 00347 #endif 00348 00349 #else 00350 00351 M2MSecurity *register_object = mbed_client.create_register_object(_have_secure_conn); 00352 00353 if( mode == M2MSecurity::Certificate ){ 00354 register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate); 00355 register_object->set_resource_value(M2MSecurity::ServerPublicKey,server_cert,server_cert_len); 00356 register_object->set_resource_value(M2MSecurity::PublicKey,cert,cert_len); 00357 register_object->set_resource_value(M2MSecurity::Secretkey,key,key_len); 00358 }else if( mode == M2MSecurity::Psk ){ 00359 register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Psk); 00360 register_object->set_resource_value(M2MSecurity::ServerPublicKey,psk_identity,psk_identity_len); 00361 register_object->set_resource_value(M2MSecurity::PublicKey,psk_identity,psk_identity_len); 00362 register_object->set_resource_value(M2MSecurity::Secretkey,psk,psk_len); 00363 }else{ 00364 register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity); 00365 } 00366 00367 mbed_client.set_register_object(register_object); 00368 00369 #endif 00370 00371 // Create LWM2M device object specifying device resources 00372 // as per OMA LWM2M specification. 00373 M2MDevice* device_object = mbed_client.create_device_object(); 00374 00375 M2MObject* object = mbed_client.create_generic_object(); 00376 00377 // Add all the objects that you would like to register 00378 // into the list and pass the list for register API. 00379 M2MObjectList object_list; 00380 object_list.push_back(device_object); 00381 object_list.push_back(object); 00382 00383 // Issue register command. 00384 00385 FunctionPointer1<void, M2MObjectList> fp(&mbed_client, &MbedClient::test_register); 00386 minar::Scheduler::postCallback(fp.bind(object_list)); 00387 00388 minar::Scheduler::start(); 00389 00390 // Wait till the register callback is called successfully. 00391 // Callback comes in object_registered() 00392 #ifdef SIXLOWPAN_INTERFACE 00393 /* wait network to be established */ 00394 do { 00395 mesh_api->processEvent(); 00396 } while(!mbed_client.register_successful()); 00397 #endif 00398 00399 // Wait for the unregister successful callback, 00400 // Callback comes in object_unregsitered(), this will be 00401 // waiting for user to press SW2 button on K64F board. 00402 #ifdef SIXLOWPAN_INTERFACE 00403 /* wait network to be established */ 00404 do { 00405 mesh_api->processEvent(); 00406 } while(!mbed_client.unregister_successful()); 00407 #endif 00408 00409 00410 #if defined(BOOTSTRAP_ENABLED) 00411 // This will turn on the LED on the board specifying that 00412 // the application has run successfully. 00413 notify_completion(mbed_client.unregister_successful() && 00414 mbed_client.register_successful() && 00415 mbed_client.bootstrap_successful()); 00416 00417 // Delete security object created for bootstrapping 00418 if(security_object) { 00419 delete security_object; 00420 } 00421 00422 #else 00423 00424 // Disconnect the connect and teardown the network interface 00425 #ifdef SIXLOWPAN_INTERFACE 00426 mesh_api->disconnect(); 00427 #else 00428 eth.disconnect(); 00429 #endif 00430 #endif //BOOTSTRAP_ENABLED 00431 00432 // Delete device object created for registering device 00433 // resources. 00434 if(device_object) { 00435 M2MDevice::delete_instance(); 00436 } 00437 if(object) { 00438 delete object; 00439 } 00440 00441 #endif //TARGET_LIKE_LINUX 00442 00443 #ifndef TARGET_LIKE_MBED 00444 return 0; 00445 #endif 00446 }
Generated on Tue Jul 12 2022 21:20:28 by
1.7.2