Vinay Shrivastav / Mbed OS AAT_LWM2M_K64F

Dependencies:   FXOS8700Q-driver MODSERIAL

Fork of mbed-os-example-client by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  * @file    main.cpp
00003  * @brief   Vins Netra 2 client
00004  * @author  Vinay Shrivastava
00005  * @version 1.0
00006  * @see
00007  *
00008  * Copyright (c) 2016
00009  * 
00010  * Licensed under the Apache License, Version 2.0 (the License); you may
00011  * not use this file except in compliance with the License.
00012  * You may obtain a copy of the License at
00013  *
00014  * http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing, software
00017  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00018  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  * See the License for the specific language governing permissions and
00020  * limitations under the License.
00021  */
00022 
00023 #include <string>
00024 #include <sstream>
00025 #include <vector>
00026 #include "security.h"
00027 #include "mbed.h"
00028 #include "rtos.h"
00029 #include "resources.h"
00030 
00031 
00032 // Network interaction must be performed outside of interrupt context
00033 Semaphore updates(0);
00034 volatile bool registered = false;
00035 volatile bool clicked = false;
00036 volatile bool update_trigger = false;
00037 osThreadId mainThread;
00038 
00039 void unregister() {
00040     registered = false;
00041     updates.release();
00042 }
00043 
00044 void button_clicked() {
00045     clicked = true;
00046     updates.release();
00047 }
00048 
00049 void trigger_update(){
00050     update_trigger = true;
00051 }   
00052 
00053 // debug printf function
00054 void trace_printer(const char* str) {
00055     printf("%s\r\n", str);
00056 }
00057 
00058 // Entry point to the program
00059 int main() {
00060 
00061     unsigned int seed;
00062     size_t len;
00063 
00064 #ifdef MBEDTLS_ENTROPY_HARDWARE_ALT
00065     // Used to randomize source port
00066     mbedtls_hardware_poll(NULL, (unsigned char *) &seed, sizeof seed, &len);
00067 
00068 #elif defined MBEDTLS_TEST_NULL_ENTROPY
00069 
00070 #warning "mbedTLS security feature is disabled. Connection will not be secure !! Implement proper hardware entropy for your selected hardware."
00071     // Used to randomize source port
00072     mbedtls_null_entropy_poll( NULL,(unsigned char *) &seed, sizeof seed, &len);
00073 
00074 #else
00075 
00076 #error "This hardware does not have entropy, endpoint will not register to Connector.\
00077 You need to enable NULL ENTROPY for your application, but if this configuration change is made then no security is offered by mbed TLS.\
00078 Add MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY in mbed_app.json macros to register your endpoint."
00079 
00080 #endif
00081 
00082     srand(seed);
00083     red_led = 1;
00084     blue_led = 1;
00085     //status_ticker.attach_us(blinky, 250000);
00086     // Keep track of the main thread
00087     mainThread = osThreadGetId();
00088 
00089     // Sets the console baud-rate
00090     output.baud(115200);
00091 
00092     output.printf("\r\nStarting mbed Client example in ");
00093 #if defined (MESH) || (MBED_CONF_LWIP_IPV6_ENABLED==true)
00094     output.printf("IPv6 mode\r\n");
00095 #else
00096     output.printf("IPv4 mode\r\n");
00097 #endif
00098 
00099     mbed_trace_init();
00100     mbed_trace_print_function_set(trace_printer);
00101 
00102     NetworkInterface *network_interface = 0;
00103     int connect_success = -1;
00104 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
00105     output.printf("\n\rConnecting to WiFi...\r\n");
00106     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00107     network_interface = &wifi;
00108 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00109     output.printf("\n\rConnecting to ethernet...\r\n");
00110     connect_success = eth.connect();
00111     network_interface = &eth;
00112 #endif
00113 #ifdef MESH
00114     output.printf("\n\rConnecting to Mesh...\r\n");
00115     mesh.initialize(&rf_phy);
00116     connect_success = mesh.connect();
00117     network_interface = &mesh;
00118 #endif
00119     if(connect_success == 0) {
00120     output.printf("\n\rConnected to Network successfully\r\n");
00121     } else {
00122         output.printf("\n\rConnection to Network Failed %d! Exiting application....\r\n", connect_success);
00123         return 0;
00124     }
00125     const char *ip_addr = network_interface->get_ip_address();
00126     if (ip_addr) {
00127         output.printf("IP address %s\r\n", ip_addr);
00128     } else {
00129         output.printf("No IP address\r\n");
00130     }
00131 
00132     // create resources
00133     ButtonResource button_resource;
00134     LedResource led_resource;
00135     BigPayloadResource big_payload_resource;
00136     GnssCustomResource gnss_resource;
00137     AccelResource accel_resource;
00138     
00139     // enable accelerometer
00140     accel.enable(); 
00141     output.printf("Initializied accelerometer\r\n");
00142     
00143     // Set gps UART baud rate
00144     gps.baud(115200);
00145     
00146     // Check GNSS UART connectivity
00147     if(gps.readable()) output.printf("GNSS UART interface connected. \n");
00148     else output.printf("WARNING: GNSS UART interface not connected or powered ON. \n"); 
00149 
00150 #ifdef TARGET_K64F
00151     // On press of SW3 button on K64F board, example application
00152     // will call unregister API towards mbed Device Connector
00153     //unreg_button.fall(&mbed_client,&MbedClient::test_unregister);
00154     unreg_button.fall(&unregister);
00155 
00156     // Observation Button (SW2) press will send update of endpoint resource values to connector
00157     obs_button.fall(&button_clicked);
00158 #endif
00159 
00160     // Send update of endpoint resource values to connector every # seconds periodically
00161     timer.attach(&trigger_update, 0.5);
00162 
00163     // Create endpoint interface to manage register and unregister
00164     mbed_client.create_interface(MBED_SERVER_ADDRESS, network_interface);
00165 
00166     // Create Objects of varying types, see simpleclient.h for more details on implementation.
00167     M2MSecurity* register_object = mbed_client.create_register_object(); // server object specifying connector info
00168     M2MDevice*   device_object   = mbed_client.create_device_object();   // device resources object
00169 
00170     // Create list of Objects to register
00171     M2MObjectList object_list;
00172 
00173     // Add objects to list
00174     object_list.push_back(device_object);
00175     object_list.push_back(accel_resource.get_object());      
00176     object_list.push_back(gnss_resource.get_object());  
00177    
00178     //object_list.push_back(button_resource.get_object());
00179     //object_list.push_back(led_resource.get_object());
00180     //object_list.push_back(big_payload_resource.get_object());
00181 
00182     // Set endpoint registration object
00183     mbed_client.set_register_object(register_object);
00184 
00185     // Register with mbed Device Connector
00186     mbed_client.test_register(register_object, object_list);
00187     registered = true;
00188     updates.wait(5000);
00189 
00190     while (true) {
00191         updates.wait(2000);
00192         if(registered) {
00193             if(!clicked && !update_trigger) {
00194                 mbed_client.test_update_register();
00195             }
00196         }else {
00197             break;
00198         }
00199         if(clicked) {
00200            clicked = false;
00201             button_resource.handle_button_click();
00202         }
00203         if(update_trigger)
00204         {
00205             update_trigger = false;
00206             // Call resource refresh functions 
00207             // gnss_resource
00208             accel_resource.handle_accel_update();
00209             if(gps.readable()) gnss_resource.handle_gnss_update();
00210         }
00211     }
00212 
00213     mbed_client.test_unregister();
00214     timer.detach();
00215     //status_ticker.detach();
00216 }