Doug Anson / mbedConnectorInterfaceWithDM

Fork of mbedConnectorInterfaceV3 by Doug Anson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbedEndpointNetwork.cpp Source File

mbedEndpointNetwork.cpp

Go to the documentation of this file.
00001 /**
00002  * @file    mbedEndpointNetwork.cpp
00003  * @brief   mbed Connector Interface network low level functions and support (Ethernet, WiFi, Mesh (6LowPAN,Thread))
00004  * @author  Doug Anson
00005  * @version 1.0
00006  * @see     
00007  *
00008  * Copyright (c) 2014
00009  *
00010  * Licensed under the Apache License, Version 2.0 (the "License");
00011  * you may 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,
00018  * WITHOUT 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 // Connector Endpoint
00024 #include "mbed-connector-interface/ConnectorEndpoint.h"
00025 
00026 // OptionsBuilder
00027 #include "mbed-connector-interface/OptionsBuilder.h"
00028 
00029 // Forward declarations of public functions in mbedEndpointNetwork
00030 #include "mbed-connector-interface/mbedEndpointNetworkImpl.h"
00031 
00032 // Enable/Disable easy-connect debugging
00033 #define EASY_CONNECT_DEBUG          false
00034 
00035 // Network Selection (via easy-connect library now...)
00036 #include "easy-connect.h"
00037 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
00038 #define NETWORK_TYPE            (char *)"WiFi-ESP8266"
00039 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
00040 #define NETWORK_TYPE            (char *)"WiFi-ODIN"
00041 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00042 #define NETWORK_TYPE            (char *)"Ethernet"
00043 #elif MBED_CONF_APP_NETWORK_INTERFACE == CELL
00044 #define NETWORK_TYPE            (char *)"Cellular"
00045 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
00046 #define NETWORK_TYPE            (char *)"6LowPAN"
00047 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
00048 #define NETWORK_TYPE            (char *)"Thread"
00049 else
00050 #define NETWORK_TYPE            (char *)"UNKNOWN"
00051 #endif
00052 
00053 // Logger instance
00054 extern Logger logger;
00055 
00056 // endpoint instance 
00057 static void *_endpoint_instance = NULL;
00058 
00059 // LWIP network instance forward reference
00060 extern NetworkInterface *__network_interface;
00061 
00062 // main loop cycle period
00063 static int _main_loop_iteration_wait_ms = MAIN_LOOP_WAIT_TIME_MS;
00064 
00065 // endpoint shutdown indicator
00066 static volatile bool _shutdown_endpoint = false;            
00067 
00068 extern "C" {
00069 
00070 /*********************** START LOCAL FUNCTIONS **************************/
00071 
00072 // start shutting downt the endpoint
00073 void start_endpoint_shutdown(void) {
00074     if (_shutdown_endpoint == true) {
00075         Connector::Endpoint *ep = (Connector::Endpoint *)_endpoint_instance;
00076         if (ep != NULL && ep->isRegistered() == true) {
00077             logger.log("mbedEndpointNetwork(%s): shutdown requested. De-registering the endpoint...",NETWORK_TYPE);
00078             ep->de_register_endpoint();
00079         }
00080         
00081         // Clean up
00082         if (ep != NULL) {
00083             delete ep;
00084             _endpoint_instance = NULL;
00085         }
00086     }
00087     
00088     // ready to shutdown...
00089     logger.log("mbedEndpointNetwork(%s): endpoint shutdown. Bye!",NETWORK_TYPE);
00090 }
00091     
00092 // setup shutdown button
00093 #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
00094 #ifdef TARGET_K64F
00095 InterruptIn shutdown_button(MBED_CONF_APP_SHUTDOWN_PIN);
00096 #endif
00097 void configure_deregistration_button(void) {
00098 #ifdef TARGET_K64F
00099     logger.log("mbedEndpointNetwork(%s): configuring de-registration button...",NETWORK_TYPE); 
00100     shutdown_button.fall(&net_shutdown_endpoint);
00101 #endif
00102 }
00103 #endif
00104 
00105 // setup shutdown button
00106 void setup_deregistration_button(void) {
00107 #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
00108     configure_deregistration_button();
00109 #endif
00110 }
00111 
00112 // configure main loop parameters
00113 void configure_main_loop_params(Connector::Endpoint *endpoint) {
00114     // set the initial shutdown state
00115     _shutdown_endpoint = false;
00116 }
00117 
00118 // perform an actvity in the main loop
00119 void peform_main_loop_activity(void) {
00120     // empty for now...
00121     ;
00122 }
00123 
00124 // begin the main loop for processing endpoint events
00125 void begin_main_loop(void) 
00126 {
00127     // DEBUG
00128     logger.log("mbedEndpointNetwork(%s): endpoint main loop beginning...",NETWORK_TYPE);
00129     
00130     // enter our main loop (until the shutdown condition flags it...)
00131     while(_shutdown_endpoint == false) {
00132         Thread::wait(_main_loop_iteration_wait_ms);
00133         peform_main_loop_activity();
00134     }
00135     
00136     // main loop has exited... start the endpoint shutdown...
00137     logger.log("mbedEndpointNetwork(%s): endpoint main loop exited. Starting endpoint shutdown...",NETWORK_TYPE);
00138     start_endpoint_shutdown();
00139 }
00140 
00141 /************************ END LOCAL FUNCTIONS ***************************/
00142 
00143 /*********************** START PUBLIC FUNCTIONS *************************/
00144 
00145 // get the network type
00146 char *net_get_type() {
00147     return NETWORK_TYPE;
00148 }
00149 
00150 // shutdown the endpoint
00151 void net_shutdown_endpoint() {
00152     _shutdown_endpoint = true;
00153 }
00154     
00155 // called after the endpoint is configured...
00156 void net_plumb_network(void *p) 
00157 {
00158     Connector::Endpoint *ep = NULL;
00159     //Connector::Options *options = NULL;
00160     
00161     // save 
00162     _endpoint_instance = p;
00163     
00164     // connected
00165     if (p != NULL) {
00166         ep = (Connector::Endpoint *)p;
00167         //options = ep->getOptions();
00168     }
00169 
00170     // connect (use easy-connect now...)
00171     __network_interface = easy_connect(EASY_CONNECT_DEBUG);
00172 
00173     // check the connection status..
00174     if (__network_interface != NULL) {
00175         // success
00176         if (ep != NULL) {
00177             ep->isConnected(true);
00178         
00179             // Debug
00180             logger.log("mbedEndpointNetwork(%s): IP Address: %s",NETWORK_TYPE,__network_interface->get_ip_address());
00181         }
00182     }
00183     else {
00184         // connection error
00185         __network_interface = NULL;
00186         if (ep != NULL) {
00187             ep->isConnected(false);
00188         }
00189         
00190         // Debug
00191         logger.log("mbedEndpointNetwork(%s): CONNECTION FAILED",NETWORK_TYPE);
00192     }
00193 }
00194 
00195 // finalize and run the endpoint main loop
00196 void net_finalize_and_run_endpoint_main_loop(void *p) 
00197 {
00198     // cast
00199     Connector::Endpoint *ep = (Connector::Endpoint *)p;
00200     
00201     // Initialize our main loop... 
00202     configure_main_loop_params(ep);
00203     
00204     // setup the shutdown button (if enabled for a given platform...)
00205     setup_deregistration_button();
00206 
00207     // register the endpoint
00208     logger.log("mbedEndpointNetwork(%s): registering endpoint...",NETWORK_TYPE); 
00209 #ifdef ENABLE_MBED_CLOUD_SUPPORT
00210     ep->register_endpoint(NULL,ep->getEndpointObjectList());
00211 #else
00212     ep->register_endpoint(ep->getSecurityInstance(),ep->getEndpointObjectList());
00213 #endif
00214            
00215     // Begin the endpoint's main loop
00216     begin_main_loop();
00217 }
00218 
00219 /************************ END PUBLIC FUNCTIONS **************************/
00220 
00221 }