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