use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Committer:
Maggie17
Date:
Mon Nov 28 15:52:56 2016 +0000
Revision:
90:b738617379a6
Parent:
81:a2441163a06e
first release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 54:dfee8691c83a 1 /**
ansond 54:dfee8691c83a 2 * @file mbedEndpointNetwork.cpp
ansond 54:dfee8691c83a 3 * @brief mbed Connector Interface network low level functions and support (Ethernet, WiFi, Mesh (6LowPAN,Thread))
ansond 54:dfee8691c83a 4 * @author Doug Anson
ansond 54:dfee8691c83a 5 * @version 1.0
ansond 54:dfee8691c83a 6 * @see
ansond 54:dfee8691c83a 7 *
ansond 54:dfee8691c83a 8 * Copyright (c) 2014
ansond 54:dfee8691c83a 9 *
ansond 54:dfee8691c83a 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 54:dfee8691c83a 11 * you may not use this file except in compliance with the License.
ansond 54:dfee8691c83a 12 * You may obtain a copy of the License at
ansond 54:dfee8691c83a 13 *
ansond 54:dfee8691c83a 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 54:dfee8691c83a 15 *
ansond 54:dfee8691c83a 16 * Unless required by applicable law or agreed to in writing, software
ansond 54:dfee8691c83a 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 54:dfee8691c83a 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 54:dfee8691c83a 19 * See the License for the specific language governing permissions and
ansond 54:dfee8691c83a 20 * limitations under the License.
ansond 54:dfee8691c83a 21 */
ansond 54:dfee8691c83a 22
ansond 54:dfee8691c83a 23 // Connector Endpoint
ansond 54:dfee8691c83a 24 #include "mbed-connector-interface/ConnectorEndpoint.h"
ansond 54:dfee8691c83a 25
ansond 54:dfee8691c83a 26 // OptionsBuilder
ansond 54:dfee8691c83a 27 #include "mbed-connector-interface/OptionsBuilder.h"
ansond 54:dfee8691c83a 28
ansond 54:dfee8691c83a 29 // Forward declarations of public functions in mbedEndpointNetwork
ansond 54:dfee8691c83a 30 #include "mbed-connector-interface/mbedEndpointNetworkImpl.h"
ansond 54:dfee8691c83a 31
ansond 54:dfee8691c83a 32 // Network Selection
ansond 54:dfee8691c83a 33 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
ansond 64:f6e3ecaa8c3a 34 #define NETWORK_TYPE (char *)"WiFi"
ansond 54:dfee8691c83a 35 #include "ESP8266Interface.h"
ansond 54:dfee8691c83a 36 ESP8266Interface network(MBED_CONF_APP_WIFI_TX,MBED_CONF_APP_WIFI_RX);
ansond 54:dfee8691c83a 37 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
ansond 64:f6e3ecaa8c3a 38 #define NETWORK_TYPE (char *)"Ethernet"
ansond 54:dfee8691c83a 39 #include "EthernetInterface.h"
ansond 54:dfee8691c83a 40 EthernetInterface network;
ansond 56:3f233795dddf 41 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
ansond 64:f6e3ecaa8c3a 42 #define NETWORK_TYPE (char *)"6LowPAN"
ansond 54:dfee8691c83a 43 #include "NanostackInterface.h"
ansond 54:dfee8691c83a 44 LoWPANNDInterface network;
ansond 56:3f233795dddf 45 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
ansond 64:f6e3ecaa8c3a 46 #define NETWORK_TYPE (char *)"Thread"
ansond 54:dfee8691c83a 47 #include "NanostackInterface.h"
ansond 54:dfee8691c83a 48 ThreadInterface network;
ansond 54:dfee8691c83a 49 #endif
ansond 54:dfee8691c83a 50
ansond 54:dfee8691c83a 51 // Logger instance
ansond 54:dfee8691c83a 52 extern Logger logger;
ansond 54:dfee8691c83a 53
ansond 54:dfee8691c83a 54 // endpoint instance
ansond 54:dfee8691c83a 55 static void *_endpoint_instance = NULL;
ansond 54:dfee8691c83a 56
ansond 54:dfee8691c83a 57 // LWIP network instance forward reference
ansond 61:d02cd5e2bb26 58 extern NetworkInterface *__network_interface;
ansond 54:dfee8691c83a 59
ansond 54:dfee8691c83a 60 // main loop cycle period
ansond 54:dfee8691c83a 61 static int _main_loop_iteration_wait_ms = MAIN_LOOP_WAIT_TIME_MS;
ansond 54:dfee8691c83a 62
ansond 54:dfee8691c83a 63 // endpoint shutdown indicator
ansond 54:dfee8691c83a 64 static volatile bool _shutdown_endpoint = false;
ansond 54:dfee8691c83a 65
ansond 54:dfee8691c83a 66 extern "C" {
ansond 54:dfee8691c83a 67
ansond 54:dfee8691c83a 68 /*********************** START LOCAL FUNCTIONS **************************/
ansond 54:dfee8691c83a 69
ansond 54:dfee8691c83a 70 // start shutting downt the endpoint
ansond 54:dfee8691c83a 71 void start_endpoint_shutdown(void) {
ansond 54:dfee8691c83a 72 if (_shutdown_endpoint == true) {
ansond 54:dfee8691c83a 73 Connector::Endpoint *ep = (Connector::Endpoint *)_endpoint_instance;
ansond 54:dfee8691c83a 74 if (ep != NULL && ep->isRegistered() == true) {
ansond 54:dfee8691c83a 75 logger.log("mbedEndpointNetwork(%s): shutdown requested. De-registering the endpoint...",NETWORK_TYPE);
ansond 54:dfee8691c83a 76 ep->de_register_endpoint();
ansond 54:dfee8691c83a 77 }
ansond 63:fc30c31a4d75 78
ansond 63:fc30c31a4d75 79 // Clean up
ansond 63:fc30c31a4d75 80 if (ep != NULL) {
ansond 63:fc30c31a4d75 81 delete ep;
ansond 63:fc30c31a4d75 82 _endpoint_instance = NULL;
ansond 63:fc30c31a4d75 83 }
ansond 54:dfee8691c83a 84 }
ansond 54:dfee8691c83a 85
ansond 54:dfee8691c83a 86 // ready to shutdown...
ansond 54:dfee8691c83a 87 logger.log("mbedEndpointNetwork(%s): endpoint shutdown. Bye!",NETWORK_TYPE);
ansond 54:dfee8691c83a 88 }
ansond 54:dfee8691c83a 89
ansond 54:dfee8691c83a 90 // setup shutdown button
ansond 54:dfee8691c83a 91 #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
ansond 54:dfee8691c83a 92 InterruptIn shutdown_button(MBED_CONF_APP_SHUTDOWN_PIN);
ansond 54:dfee8691c83a 93 void configure_deregistration_button(void) {
ansond 54:dfee8691c83a 94 logger.log("mbedEndpointNetwork(%s): configuring de-registration button...",NETWORK_TYPE);
ansond 54:dfee8691c83a 95 shutdown_button.fall(&net_shutdown_endpoint);
ansond 54:dfee8691c83a 96 }
ansond 54:dfee8691c83a 97 #endif
ansond 54:dfee8691c83a 98
ansond 54:dfee8691c83a 99 // setup shutdown button
ansond 54:dfee8691c83a 100 void setup_deregistration_button(void) {
ansond 54:dfee8691c83a 101 #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
ansond 54:dfee8691c83a 102 configure_deregistration_button();
ansond 54:dfee8691c83a 103 #endif
ansond 54:dfee8691c83a 104 }
ansond 54:dfee8691c83a 105
ansond 54:dfee8691c83a 106 // configure main loop parameters
ansond 54:dfee8691c83a 107 void configure_main_loop_params(Connector::Endpoint *endpoint) {
ansond 54:dfee8691c83a 108 // set the initial shutdown state
ansond 54:dfee8691c83a 109 _shutdown_endpoint = false;
ansond 54:dfee8691c83a 110 }
ansond 54:dfee8691c83a 111
ansond 54:dfee8691c83a 112 // perform an actvity in the main loop
ansond 54:dfee8691c83a 113 void peform_main_loop_activity(void) {
ansond 54:dfee8691c83a 114 // empty for now...
ansond 54:dfee8691c83a 115 ;
ansond 54:dfee8691c83a 116 }
ansond 54:dfee8691c83a 117
ansond 54:dfee8691c83a 118 // begin the main loop for processing endpoint events
ansond 54:dfee8691c83a 119 void begin_main_loop(void)
ansond 54:dfee8691c83a 120 {
ansond 54:dfee8691c83a 121 // DEBUG
ansond 54:dfee8691c83a 122 logger.log("mbedEndpointNetwork(%s): endpoint main loop beginning...",NETWORK_TYPE);
ansond 54:dfee8691c83a 123
ansond 54:dfee8691c83a 124 // enter our main loop (until the shutdown condition flags it...)
ansond 54:dfee8691c83a 125 while(_shutdown_endpoint == false) {
ansond 54:dfee8691c83a 126 Thread::wait(_main_loop_iteration_wait_ms);
ansond 54:dfee8691c83a 127 peform_main_loop_activity();
ansond 54:dfee8691c83a 128 }
ansond 54:dfee8691c83a 129
ansond 54:dfee8691c83a 130 // main loop has exited... start the endpoint shutdown...
ansond 54:dfee8691c83a 131 logger.log("mbedEndpointNetwork(%s): endpoint main loop exited. Starting endpoint shutdown...",NETWORK_TYPE);
ansond 54:dfee8691c83a 132 start_endpoint_shutdown();
ansond 54:dfee8691c83a 133 }
ansond 54:dfee8691c83a 134
ansond 54:dfee8691c83a 135 /************************ END LOCAL FUNCTIONS ***************************/
ansond 54:dfee8691c83a 136
ansond 54:dfee8691c83a 137 /*********************** START PUBLIC FUNCTIONS *************************/
ansond 54:dfee8691c83a 138
ansond 54:dfee8691c83a 139 // get the network type
ansond 54:dfee8691c83a 140 char *net_get_type() {
ansond 54:dfee8691c83a 141 return NETWORK_TYPE;
ansond 54:dfee8691c83a 142 }
ansond 54:dfee8691c83a 143
ansond 54:dfee8691c83a 144 // shutdown the endpoint
ansond 54:dfee8691c83a 145 void net_shutdown_endpoint() {
ansond 54:dfee8691c83a 146 _shutdown_endpoint = true;
ansond 54:dfee8691c83a 147 }
ansond 54:dfee8691c83a 148
ansond 54:dfee8691c83a 149 // called after the endpoint is configured...
ansond 54:dfee8691c83a 150 void net_plumb_network(void *p)
ansond 54:dfee8691c83a 151 {
ansond 54:dfee8691c83a 152 int connected = 0;
ansond 54:dfee8691c83a 153 Connector::Endpoint *ep = NULL;
ansond 54:dfee8691c83a 154 Connector::Options *options = NULL;
ansond 54:dfee8691c83a 155
ansond 54:dfee8691c83a 156 // save
ansond 54:dfee8691c83a 157 _endpoint_instance = p;
ansond 54:dfee8691c83a 158
ansond 54:dfee8691c83a 159 // connected
ansond 54:dfee8691c83a 160 if (p != NULL) {
ansond 54:dfee8691c83a 161 ep = (Connector::Endpoint *)p;
ansond 54:dfee8691c83a 162 options = ep->getOptions();
ansond 54:dfee8691c83a 163 }
ansond 54:dfee8691c83a 164
ansond 54:dfee8691c83a 165 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
ansond 54:dfee8691c83a 166 // map security types
ansond 54:dfee8691c83a 167 nsapi_security_t security_opt = NSAPI_SECURITY_NONE;
ansond 54:dfee8691c83a 168 if (options->getWiFiAuthType() == WIFI_WPA_PERSONAL) {
ansond 54:dfee8691c83a 169 security_opt = NSAPI_SECURITY_WPA;
ansond 54:dfee8691c83a 170 }
ansond 54:dfee8691c83a 171 if (options->getWiFiAuthType() == WIFI_WPA2_PERSONAL) {
ansond 54:dfee8691c83a 172 security_opt = NSAPI_SECURITY_WPA2;
ansond 54:dfee8691c83a 173 }
ansond 54:dfee8691c83a 174 if (options->getWiFiAuthType() == WIFI_WEP) {
ansond 54:dfee8691c83a 175 security_opt = NSAPI_SECURITY_WEP;
ansond 54:dfee8691c83a 176 }
ansond 54:dfee8691c83a 177
ansond 54:dfee8691c83a 178 // Network Init (WIFI)...
ansond 54:dfee8691c83a 179 connected = network.connect(options->getWiFiSSID().c_str(),options->getWiFiAuthKey().c_str(),security_opt);
ansond 56:3f233795dddf 180 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND || MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
ansond 54:dfee8691c83a 181 // Set the IP Address type to IPV6
ansond 54:dfee8691c83a 182 ((Connector::OptionsBuilder *)options)->setIPAddressType(IP_ADDRESS_TYPE_IPV6);
ansond 54:dfee8691c83a 183
ansond 54:dfee8691c83a 184 // Network Init (Mesh)
ansond 54:dfee8691c83a 185 connected = network.connect();
ansond 54:dfee8691c83a 186 #else
ansond 54:dfee8691c83a 187 // not used... just removes a compiler warning...
ansond 54:dfee8691c83a 188 options->getConnectorURL();
ansond 54:dfee8691c83a 189
ansond 54:dfee8691c83a 190 // Network Init (Ethernet)
ansond 54:dfee8691c83a 191 connected = network.connect();
ansond 54:dfee8691c83a 192 #endif
ansond 54:dfee8691c83a 193
ansond 54:dfee8691c83a 194 // check the connection status..
ansond 54:dfee8691c83a 195 if (connected == 0) {
ansond 61:d02cd5e2bb26 196 // success
ansond 61:d02cd5e2bb26 197 __network_interface = (NetworkInterface *)&network;
ansond 54:dfee8691c83a 198 if (ep != NULL) {
ansond 54:dfee8691c83a 199 ep->isConnected(true);
ansond 54:dfee8691c83a 200
ansond 61:d02cd5e2bb26 201 // Debug
ansond 61:d02cd5e2bb26 202 logger.log("mbedEndpointNetwork(%s): IP Address: %s",NETWORK_TYPE,network.get_ip_address());
ansond 61:d02cd5e2bb26 203 }
ansond 54:dfee8691c83a 204 }
ansond 54:dfee8691c83a 205 else {
ansond 54:dfee8691c83a 206 __network_interface = NULL;
ansond 54:dfee8691c83a 207 if (ep != NULL) {
ansond 54:dfee8691c83a 208 ep->isConnected(false);
ansond 54:dfee8691c83a 209 }
ansond 54:dfee8691c83a 210
ansond 54:dfee8691c83a 211 // Debug
ansond 54:dfee8691c83a 212 logger.log("mbedEndpointNetwork(%s): CONNECTION FAILED",NETWORK_TYPE);
ansond 54:dfee8691c83a 213 }
ansond 54:dfee8691c83a 214 }
ansond 54:dfee8691c83a 215
ansond 54:dfee8691c83a 216 // finalize and run the endpoint main loop
ansond 54:dfee8691c83a 217 void net_finalize_and_run_endpoint_main_loop(void *p)
ansond 54:dfee8691c83a 218 {
ansond 54:dfee8691c83a 219 // cast
ansond 54:dfee8691c83a 220 Connector::Endpoint *ep = (Connector::Endpoint *)p;
ansond 54:dfee8691c83a 221
ansond 54:dfee8691c83a 222 // Initialize our main loop...
ansond 54:dfee8691c83a 223 configure_main_loop_params(ep);
ansond 54:dfee8691c83a 224
ansond 54:dfee8691c83a 225 // setup the shutdown button (if enabled for a given platform...)
ansond 54:dfee8691c83a 226 setup_deregistration_button();
ansond 54:dfee8691c83a 227
ansond 54:dfee8691c83a 228 // register the endpoint
ansond 54:dfee8691c83a 229 logger.log("mbedEndpointNetwork(%s): registering endpoint...",NETWORK_TYPE);
ansond 78:7fdf3322de58 230 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 78:7fdf3322de58 231 ep->register_endpoint(NULL,ep->getEndpointObjectList());
ansond 78:7fdf3322de58 232 #else
ansond 81:a2441163a06e 233 ep->register_endpoint(ep->getSecurityInstance(),ep->getEndpointObjectList());
ansond 78:7fdf3322de58 234 #endif
ansond 54:dfee8691c83a 235
ansond 54:dfee8691c83a 236 // Begin the endpoint's main loop
ansond 54:dfee8691c83a 237 begin_main_loop();
ansond 54:dfee8691c83a 238 }
ansond 54:dfee8691c83a 239
ansond 54:dfee8691c83a 240 /************************ END PUBLIC FUNCTIONS **************************/
ansond 54:dfee8691c83a 241
ansond 54:dfee8691c83a 242 }