Sample program showing how to connect GR-PEACH into Watson IoT through mbed Connector and Watson's Connector Bridge

Dependencies:   AsciiFont DisplayApp GR-PEACH_video LCD_shield_config LWIPBP3595Interface_STA_for_mbed-os USBDevice

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 // Network Selection
00033 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
00034     #define NETWORK_TYPE            (char *)"WiFi"
00035 #if 1 //bp3595
00036     #include "LWIPBP3595Interface.h"
00037     LWIPBP3595Interface network;
00038     DigitalOut usb1en(P3_8);
00039 #else //#if 1   
00040     #include "ESP8266Interface.h"
00041     ESP8266Interface network(MBED_CONF_APP_WIFI_TX,MBED_CONF_APP_WIFI_RX);
00042 #endif
00043 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00044     #define NETWORK_TYPE            (char *)"Ethernet"
00045     #include "EthernetInterface.h"
00046     EthernetInterface network;
00047 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
00048     #define NETWORK_TYPE            (char *)"6LowPAN"
00049     #include "NanostackInterface.h"
00050     LoWPANNDInterface network;
00051 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
00052     #define NETWORK_TYPE            (char *)"Thread"
00053     #include "NanostackInterface.h"
00054     ThreadInterface network;
00055 #endif
00056 
00057 // Logger instance
00058 extern Logger logger;
00059 
00060 // endpoint instance 
00061 static void *_endpoint_instance = NULL;
00062 
00063 // LWIP network instance forward reference
00064 extern NetworkInterface *__network_interface;
00065 
00066 // main loop cycle period
00067 static int _main_loop_iteration_wait_ms = MAIN_LOOP_WAIT_TIME_MS;
00068 
00069 // endpoint shutdown indicator
00070 static volatile bool _shutdown_endpoint = false;            
00071 
00072 extern "C" {
00073 
00074 /*********************** START LOCAL FUNCTIONS **************************/
00075 
00076 // start shutting downt the endpoint
00077 void start_endpoint_shutdown(void) {
00078     if (_shutdown_endpoint == true) {
00079         Connector::Endpoint *ep = (Connector::Endpoint *)_endpoint_instance;
00080         if (ep != NULL && ep->isRegistered() == true) {
00081             logger.logging("mbedEndpointNetwork(%s): shutdown requested. De-registering the endpoint...",NETWORK_TYPE);
00082             ep->de_register_endpoint();
00083         }
00084         
00085         // Clean up
00086         if (ep != NULL) {
00087             delete ep;
00088             _endpoint_instance = NULL;
00089         }
00090     }
00091     
00092     // ready to shutdown...
00093     logger.logging("mbedEndpointNetwork(%s): endpoint shutdown. Bye!",NETWORK_TYPE);
00094 }
00095     
00096 // setup shutdown button
00097 #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
00098 InterruptIn shutdown_button(MBED_CONF_APP_SHUTDOWN_PIN);
00099 void configure_deregistration_button(void) {
00100     logger.logging("mbedEndpointNetwork(%s): configuring de-registration button...",NETWORK_TYPE); 
00101     shutdown_button.fall(&net_shutdown_endpoint);
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.logging("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.logging("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     int connected = 0;
00159     Connector::Endpoint *ep = NULL;
00160     Connector::Options *options = NULL;
00161     
00162     // save 
00163     _endpoint_instance = p;
00164     
00165     // connected
00166     if (p != NULL) {
00167         ep = (Connector::Endpoint *)p;
00168         options = ep->getOptions();
00169     }
00170     
00171 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
00172 #if 1 //BP3595
00173     usb1en = 1;
00174     Thread::wait(5);
00175     usb1en = 0;
00176     Thread::wait(5);
00177     connected = network.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, MBED_CONF_APP_WIFI_SECURITY);
00178 #else //#if 1
00179     // map security types
00180     nsapi_security_t security_opt = NSAPI_SECURITY_NONE;
00181     if  (options->getWiFiAuthType() == WIFI_WPA_PERSONAL) {
00182         security_opt = NSAPI_SECURITY_WPA;
00183     }
00184     if  (options->getWiFiAuthType() == WIFI_WPA2_PERSONAL) {
00185         security_opt = NSAPI_SECURITY_WPA2;
00186     }
00187     if  (options->getWiFiAuthType() == WIFI_WEP) {
00188         security_opt = NSAPI_SECURITY_WEP;
00189     }
00190     
00191     // Network Init (WIFI)...
00192     connected = network.connect(options->getWiFiSSID().c_str(),options->getWiFiAuthKey().c_str(),security_opt);
00193 #endif
00194 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND || MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
00195     // Set the IP Address type to IPV6
00196     ((Connector::OptionsBuilder *)options)->setIPAddressType(IP_ADDRESS_TYPE_IPV6);
00197     
00198     // Network Init (Mesh)
00199     connected = network.connect();
00200 #else
00201     // not used... just removes a compiler warning...
00202     options->getConnectorURL();
00203     
00204     // Network Init (Ethernet)
00205     connected = network.connect();
00206 #endif
00207 
00208     // check the connection status..
00209     if (connected == 0) {
00210         // success
00211         __network_interface = (NetworkInterface *)&network;
00212         if (ep != NULL) {
00213             ep->isConnected(true);
00214         
00215             // Debug
00216             logger.logging("mbedEndpointNetwork(%s): IP Address: %s",NETWORK_TYPE,network.get_ip_address());
00217         }
00218     }
00219     else {
00220         __network_interface = NULL;
00221         if (ep != NULL) {
00222             ep->isConnected(false);
00223         }
00224         
00225         // Debug
00226         logger.logging("mbedEndpointNetwork(%s): CONNECTION FAILED",NETWORK_TYPE);
00227     }
00228 }
00229 
00230 // finalize and run the endpoint main loop
00231 void net_finalize_and_run_endpoint_main_loop(void *p) 
00232 {
00233     // cast
00234     Connector::Endpoint *ep = (Connector::Endpoint *)p;
00235     
00236     // Initialize our main loop... 
00237     configure_main_loop_params(ep);
00238     
00239     // setup the shutdown button (if enabled for a given platform...)
00240     setup_deregistration_button();
00241 
00242     // register the endpoint
00243     logger.logging("mbedEndpointNetwork(%s): registering endpoint...",NETWORK_TYPE); 
00244     ep->register_endpoint(ep->getEndpointSecurity(),ep->getEndpointObjectList());
00245            
00246     // Begin the endpoint's main loop
00247     begin_main_loop();
00248 }
00249 
00250 /************************ END PUBLIC FUNCTIONS **************************/
00251 
00252 }