mbed Connector Interface simplification API on top of mbed-client

Fork of mbedConnectorInterfaceV3 by Doug Anson

NOTE:

This repo has been replaced with https://github.com/ARMmbed/mbedConnectorInterface. No further updates will occur with this repo. Please use the github repo instead. Thanks!

Committer:
ansond
Date:
Wed Aug 10 00:13:44 2016 +0000
Revision:
61:d02cd5e2bb26
Parent:
56:3f233795dddf
Child:
63:fc30c31a4d75
updates

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 61:d02cd5e2bb26 34 #define NETWORK_TYPE "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 54:dfee8691c83a 38 #define NETWORK_TYPE "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 54:dfee8691c83a 42 #define NETWORK_TYPE "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 61:d02cd5e2bb26 46 #define NETWORK_TYPE "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 54:dfee8691c83a 78 }
ansond 54:dfee8691c83a 79
ansond 54:dfee8691c83a 80 // ready to shutdown...
ansond 54:dfee8691c83a 81 logger.log("mbedEndpointNetwork(%s): endpoint shutdown. Bye!",NETWORK_TYPE);
ansond 54:dfee8691c83a 82 }
ansond 54:dfee8691c83a 83
ansond 54:dfee8691c83a 84 // setup shutdown button
ansond 54:dfee8691c83a 85 #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
ansond 54:dfee8691c83a 86 InterruptIn shutdown_button(MBED_CONF_APP_SHUTDOWN_PIN);
ansond 54:dfee8691c83a 87 void configure_deregistration_button(void) {
ansond 54:dfee8691c83a 88 logger.log("mbedEndpointNetwork(%s): configuring de-registration button...",NETWORK_TYPE);
ansond 54:dfee8691c83a 89 shutdown_button.fall(&net_shutdown_endpoint);
ansond 54:dfee8691c83a 90 }
ansond 54:dfee8691c83a 91 #endif
ansond 54:dfee8691c83a 92
ansond 54:dfee8691c83a 93 // setup shutdown button
ansond 54:dfee8691c83a 94 void setup_deregistration_button(void) {
ansond 54:dfee8691c83a 95 #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
ansond 54:dfee8691c83a 96 configure_deregistration_button();
ansond 54:dfee8691c83a 97 #endif
ansond 54:dfee8691c83a 98 }
ansond 54:dfee8691c83a 99
ansond 54:dfee8691c83a 100 // configure main loop parameters
ansond 54:dfee8691c83a 101 void configure_main_loop_params(Connector::Endpoint *endpoint) {
ansond 54:dfee8691c83a 102 // set the initial shutdown state
ansond 54:dfee8691c83a 103 _shutdown_endpoint = false;
ansond 54:dfee8691c83a 104 }
ansond 54:dfee8691c83a 105
ansond 54:dfee8691c83a 106 // perform an actvity in the main loop
ansond 54:dfee8691c83a 107 void peform_main_loop_activity(void) {
ansond 54:dfee8691c83a 108 // empty for now...
ansond 54:dfee8691c83a 109 ;
ansond 54:dfee8691c83a 110 }
ansond 54:dfee8691c83a 111
ansond 54:dfee8691c83a 112 // begin the main loop for processing endpoint events
ansond 54:dfee8691c83a 113 void begin_main_loop(void)
ansond 54:dfee8691c83a 114 {
ansond 54:dfee8691c83a 115 // DEBUG
ansond 54:dfee8691c83a 116 logger.log("mbedEndpointNetwork(%s): endpoint main loop beginning...",NETWORK_TYPE);
ansond 54:dfee8691c83a 117
ansond 54:dfee8691c83a 118 // enter our main loop (until the shutdown condition flags it...)
ansond 54:dfee8691c83a 119 while(_shutdown_endpoint == false) {
ansond 54:dfee8691c83a 120 Thread::wait(_main_loop_iteration_wait_ms);
ansond 54:dfee8691c83a 121 peform_main_loop_activity();
ansond 54:dfee8691c83a 122 }
ansond 54:dfee8691c83a 123
ansond 54:dfee8691c83a 124 // main loop has exited... start the endpoint shutdown...
ansond 54:dfee8691c83a 125 logger.log("mbedEndpointNetwork(%s): endpoint main loop exited. Starting endpoint shutdown...",NETWORK_TYPE);
ansond 54:dfee8691c83a 126 start_endpoint_shutdown();
ansond 54:dfee8691c83a 127 }
ansond 54:dfee8691c83a 128
ansond 54:dfee8691c83a 129 /************************ END LOCAL FUNCTIONS ***************************/
ansond 54:dfee8691c83a 130
ansond 54:dfee8691c83a 131 /*********************** START PUBLIC FUNCTIONS *************************/
ansond 54:dfee8691c83a 132
ansond 54:dfee8691c83a 133 // get the network type
ansond 54:dfee8691c83a 134 char *net_get_type() {
ansond 54:dfee8691c83a 135 return NETWORK_TYPE;
ansond 54:dfee8691c83a 136 }
ansond 54:dfee8691c83a 137
ansond 54:dfee8691c83a 138 // shutdown the endpoint
ansond 54:dfee8691c83a 139 void net_shutdown_endpoint() {
ansond 54:dfee8691c83a 140 _shutdown_endpoint = true;
ansond 54:dfee8691c83a 141 }
ansond 54:dfee8691c83a 142
ansond 54:dfee8691c83a 143 // called after the endpoint is configured...
ansond 54:dfee8691c83a 144 void net_plumb_network(void *p)
ansond 54:dfee8691c83a 145 {
ansond 54:dfee8691c83a 146 int connected = 0;
ansond 54:dfee8691c83a 147 Connector::Endpoint *ep = NULL;
ansond 54:dfee8691c83a 148 Connector::Options *options = NULL;
ansond 54:dfee8691c83a 149
ansond 54:dfee8691c83a 150 // save
ansond 54:dfee8691c83a 151 _endpoint_instance = p;
ansond 54:dfee8691c83a 152
ansond 54:dfee8691c83a 153 // connected
ansond 54:dfee8691c83a 154 if (p != NULL) {
ansond 54:dfee8691c83a 155 ep = (Connector::Endpoint *)p;
ansond 54:dfee8691c83a 156 options = ep->getOptions();
ansond 54:dfee8691c83a 157 }
ansond 54:dfee8691c83a 158
ansond 54:dfee8691c83a 159 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
ansond 54:dfee8691c83a 160 // map security types
ansond 54:dfee8691c83a 161 nsapi_security_t security_opt = NSAPI_SECURITY_NONE;
ansond 54:dfee8691c83a 162 if (options->getWiFiAuthType() == WIFI_WPA_PERSONAL) {
ansond 54:dfee8691c83a 163 security_opt = NSAPI_SECURITY_WPA;
ansond 54:dfee8691c83a 164 }
ansond 54:dfee8691c83a 165 if (options->getWiFiAuthType() == WIFI_WPA2_PERSONAL) {
ansond 54:dfee8691c83a 166 security_opt = NSAPI_SECURITY_WPA2;
ansond 54:dfee8691c83a 167 }
ansond 54:dfee8691c83a 168 if (options->getWiFiAuthType() == WIFI_WEP) {
ansond 54:dfee8691c83a 169 security_opt = NSAPI_SECURITY_WEP;
ansond 54:dfee8691c83a 170 }
ansond 54:dfee8691c83a 171
ansond 54:dfee8691c83a 172 // Network Init (WIFI)...
ansond 54:dfee8691c83a 173 connected = network.connect(options->getWiFiSSID().c_str(),options->getWiFiAuthKey().c_str(),security_opt);
ansond 56:3f233795dddf 174 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND || MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
ansond 54:dfee8691c83a 175 // Set the IP Address type to IPV6
ansond 54:dfee8691c83a 176 ((Connector::OptionsBuilder *)options)->setIPAddressType(IP_ADDRESS_TYPE_IPV6);
ansond 54:dfee8691c83a 177
ansond 54:dfee8691c83a 178 // Network Init (Mesh)
ansond 54:dfee8691c83a 179 connected = network.connect();
ansond 54:dfee8691c83a 180 #else
ansond 54:dfee8691c83a 181 // not used... just removes a compiler warning...
ansond 54:dfee8691c83a 182 options->getConnectorURL();
ansond 54:dfee8691c83a 183
ansond 54:dfee8691c83a 184 // Network Init (Ethernet)
ansond 54:dfee8691c83a 185 connected = network.connect();
ansond 54:dfee8691c83a 186 #endif
ansond 54:dfee8691c83a 187
ansond 54:dfee8691c83a 188 // check the connection status..
ansond 54:dfee8691c83a 189 if (connected == 0) {
ansond 61:d02cd5e2bb26 190 // success
ansond 61:d02cd5e2bb26 191 __network_interface = (NetworkInterface *)&network;
ansond 54:dfee8691c83a 192 if (ep != NULL) {
ansond 54:dfee8691c83a 193 ep->isConnected(true);
ansond 54:dfee8691c83a 194
ansond 61:d02cd5e2bb26 195 // Debug
ansond 61:d02cd5e2bb26 196 logger.log("mbedEndpointNetwork(%s): IP Address: %s",NETWORK_TYPE,network.get_ip_address());
ansond 61:d02cd5e2bb26 197 }
ansond 54:dfee8691c83a 198 }
ansond 54:dfee8691c83a 199 else {
ansond 54:dfee8691c83a 200 __network_interface = NULL;
ansond 54:dfee8691c83a 201 if (ep != NULL) {
ansond 54:dfee8691c83a 202 ep->isConnected(false);
ansond 54:dfee8691c83a 203 }
ansond 54:dfee8691c83a 204
ansond 54:dfee8691c83a 205 // Debug
ansond 54:dfee8691c83a 206 logger.log("mbedEndpointNetwork(%s): CONNECTION FAILED",NETWORK_TYPE);
ansond 54:dfee8691c83a 207 }
ansond 54:dfee8691c83a 208 }
ansond 54:dfee8691c83a 209
ansond 54:dfee8691c83a 210 // finalize and run the endpoint main loop
ansond 54:dfee8691c83a 211 void net_finalize_and_run_endpoint_main_loop(void *p)
ansond 54:dfee8691c83a 212 {
ansond 54:dfee8691c83a 213 // cast
ansond 54:dfee8691c83a 214 Connector::Endpoint *ep = (Connector::Endpoint *)p;
ansond 54:dfee8691c83a 215
ansond 54:dfee8691c83a 216 // Initialize our main loop...
ansond 54:dfee8691c83a 217 configure_main_loop_params(ep);
ansond 54:dfee8691c83a 218
ansond 54:dfee8691c83a 219 // setup the shutdown button (if enabled for a given platform...)
ansond 54:dfee8691c83a 220 setup_deregistration_button();
ansond 54:dfee8691c83a 221
ansond 54:dfee8691c83a 222 // register the endpoint
ansond 54:dfee8691c83a 223 logger.log("mbedEndpointNetwork(%s): registering endpoint...",NETWORK_TYPE);
ansond 54:dfee8691c83a 224 ep->register_endpoint(ep->getEndpointSecurity(),ep->getEndpointObjectList());
ansond 54:dfee8691c83a 225
ansond 54:dfee8691c83a 226 // Begin the endpoint's main loop
ansond 54:dfee8691c83a 227 begin_main_loop();
ansond 54:dfee8691c83a 228 }
ansond 54:dfee8691c83a 229
ansond 54:dfee8691c83a 230 /************************ END PUBLIC FUNCTIONS **************************/
ansond 54:dfee8691c83a 231
ansond 54:dfee8691c83a 232 }