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.
Fork of mbedConnectorInterfaceWithDM by
mbedEndpointNetwork.cpp
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 // Network Selection 00033 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI 00034 #define NETWORK_TYPE (char *)"WiFi" 00035 #include "ESP8266Interface.h" 00036 ESP8266Interface network(MBED_CONF_APP_WIFI_TX,MBED_CONF_APP_WIFI_RX); 00037 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET 00038 #define NETWORK_TYPE (char *)"Ethernet" 00039 #include "EthernetInterface.h" 00040 EthernetInterface network; 00041 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND 00042 #define NETWORK_TYPE (char *)"6LowPAN" 00043 #include "NanostackInterface.h" 00044 LoWPANNDInterface network; 00045 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD 00046 #define NETWORK_TYPE (char *)"Thread" 00047 #include "NanostackInterface.h" 00048 ThreadInterface network; 00049 #endif 00050 00051 // Logger instance 00052 extern Logger logger; 00053 00054 // endpoint instance 00055 static void *_endpoint_instance = NULL; 00056 00057 // LWIP network instance forward reference 00058 extern NetworkInterface *__network_interface; 00059 00060 // main loop cycle period 00061 static int _main_loop_iteration_wait_ms = MAIN_LOOP_WAIT_TIME_MS; 00062 00063 // endpoint shutdown indicator 00064 static volatile bool _shutdown_endpoint = false; 00065 00066 extern "C" { 00067 00068 /*********************** START LOCAL FUNCTIONS **************************/ 00069 00070 // start shutting downt the endpoint 00071 void start_endpoint_shutdown(void) { 00072 if (_shutdown_endpoint == true) { 00073 Connector::Endpoint *ep = (Connector::Endpoint *)_endpoint_instance; 00074 if (ep != NULL && ep->isRegistered() == true) { 00075 logger.log("mbedEndpointNetwork(%s): shutdown requested. De-registering the endpoint...",NETWORK_TYPE); 00076 ep->de_register_endpoint(); 00077 } 00078 00079 // Clean up 00080 if (ep != NULL) { 00081 delete ep; 00082 _endpoint_instance = NULL; 00083 } 00084 } 00085 00086 // ready to shutdown... 00087 logger.log("mbedEndpointNetwork(%s): endpoint shutdown. Bye!",NETWORK_TYPE); 00088 } 00089 00090 // setup shutdown button 00091 #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true 00092 InterruptIn shutdown_button(MBED_CONF_APP_SHUTDOWN_PIN); 00093 void configure_deregistration_button(void) { 00094 logger.log("mbedEndpointNetwork(%s): configuring de-registration button...",NETWORK_TYPE); 00095 shutdown_button.fall(&net_shutdown_endpoint); 00096 } 00097 #endif 00098 00099 // setup shutdown button 00100 void setup_deregistration_button(void) { 00101 #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true 00102 configure_deregistration_button(); 00103 #endif 00104 } 00105 00106 // configure main loop parameters 00107 void configure_main_loop_params(Connector::Endpoint *endpoint) { 00108 // set the initial shutdown state 00109 _shutdown_endpoint = false; 00110 } 00111 00112 // perform an actvity in the main loop 00113 void peform_main_loop_activity(void) { 00114 // empty for now... 00115 ; 00116 } 00117 00118 // begin the main loop for processing endpoint events 00119 void begin_main_loop(void) 00120 { 00121 // DEBUG 00122 logger.log("mbedEndpointNetwork(%s): endpoint main loop beginning...",NETWORK_TYPE); 00123 00124 // enter our main loop (until the shutdown condition flags it...) 00125 while(_shutdown_endpoint == false) { 00126 Thread::wait(_main_loop_iteration_wait_ms); 00127 peform_main_loop_activity(); 00128 } 00129 00130 // main loop has exited... start the endpoint shutdown... 00131 logger.log("mbedEndpointNetwork(%s): endpoint main loop exited. Starting endpoint shutdown...",NETWORK_TYPE); 00132 start_endpoint_shutdown(); 00133 } 00134 00135 /************************ END LOCAL FUNCTIONS ***************************/ 00136 00137 /*********************** START PUBLIC FUNCTIONS *************************/ 00138 00139 // get the network type 00140 char *net_get_type() { 00141 return NETWORK_TYPE; 00142 } 00143 00144 // shutdown the endpoint 00145 void net_shutdown_endpoint() { 00146 _shutdown_endpoint = true; 00147 } 00148 00149 // called after the endpoint is configured... 00150 void net_plumb_network(void *p) 00151 { 00152 int connected = 0; 00153 Connector::Endpoint *ep = NULL; 00154 Connector::Options *options = NULL; 00155 00156 // save 00157 _endpoint_instance = p; 00158 00159 // connected 00160 if (p != NULL) { 00161 ep = (Connector::Endpoint *)p; 00162 options = ep->getOptions(); 00163 } 00164 00165 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI 00166 // map security types 00167 nsapi_security_t security_opt = NSAPI_SECURITY_NONE; 00168 if (options->getWiFiAuthType() == WIFI_WPA_PERSONAL) { 00169 security_opt = NSAPI_SECURITY_WPA; 00170 } 00171 if (options->getWiFiAuthType() == WIFI_WPA2_PERSONAL) { 00172 security_opt = NSAPI_SECURITY_WPA2; 00173 } 00174 if (options->getWiFiAuthType() == WIFI_WEP) { 00175 security_opt = NSAPI_SECURITY_WEP; 00176 } 00177 00178 // Network Init (WIFI)... 00179 connected = network.connect(options->getWiFiSSID().c_str(),options->getWiFiAuthKey().c_str(),security_opt); 00180 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND || MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD 00181 // Set the IP Address type to IPV6 00182 ((Connector::OptionsBuilder *)options)->setIPAddressType(IP_ADDRESS_TYPE_IPV6); 00183 00184 // Network Init (Mesh) 00185 connected = network.connect(); 00186 #else 00187 // not used... just removes a compiler warning... 00188 options->getConnectorURL(); 00189 00190 // Network Init (Ethernet) 00191 connected = network.connect(); 00192 #endif 00193 00194 // check the connection status.. 00195 if (connected == 0) { 00196 // success 00197 __network_interface = (NetworkInterface *)&network; 00198 if (ep != NULL) { 00199 ep->isConnected(true); 00200 00201 // Debug 00202 logger.log("mbedEndpointNetwork(%s): IP Address: %s",NETWORK_TYPE,network.get_ip_address()); 00203 } 00204 } 00205 else { 00206 __network_interface = NULL; 00207 if (ep != NULL) { 00208 ep->isConnected(false); 00209 } 00210 00211 // Debug 00212 logger.log("mbedEndpointNetwork(%s): CONNECTION FAILED",NETWORK_TYPE); 00213 } 00214 } 00215 00216 // finalize and run the endpoint main loop 00217 void net_finalize_and_run_endpoint_main_loop(void *p) 00218 { 00219 // cast 00220 Connector::Endpoint *ep = (Connector::Endpoint *)p; 00221 00222 // Initialize our main loop... 00223 configure_main_loop_params(ep); 00224 00225 // setup the shutdown button (if enabled for a given platform...) 00226 setup_deregistration_button(); 00227 00228 // register the endpoint 00229 logger.log("mbedEndpointNetwork(%s): registering endpoint...",NETWORK_TYPE); 00230 #ifdef ENABLE_MBED_CLOUD_SUPPORT 00231 ep->register_endpoint(NULL,ep->getEndpointObjectList()); 00232 #else 00233 ep->register_endpoint(ep->getSecurityInstance(),ep->getEndpointObjectList()); 00234 #endif 00235 00236 // Begin the endpoint's main loop 00237 begin_main_loop(); 00238 } 00239 00240 /************************ END PUBLIC FUNCTIONS **************************/ 00241 00242 }
Generated on Wed Jul 13 2022 01:24:02 by
