mbed Connector Interface simplification API on top of mbed-client
Fork of mbedConnectorInterfaceV3 by
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!
source/mbedEndpointNetwork.cpp@56:3f233795dddf, 2016-08-09 (annotated)
- Committer:
- ansond
- Date:
- Tue Aug 09 18:47:59 2016 +0000
- Revision:
- 56:3f233795dddf
- Parent:
- 54:dfee8691c83a
- Child:
- 61:d02cd5e2bb26
updated
Who changed what in which revision?
User | Revision | Line number | New 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 | 54:dfee8691c83a | 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 | #define MESH |
ansond | 54:dfee8691c83a | 44 | #include "NanostackInterface.h" |
ansond | 54:dfee8691c83a | 45 | LoWPANNDInterface network; |
ansond | 56:3f233795dddf | 46 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD |
ansond | 54:dfee8691c83a | 47 | #define NETWORK_TYPE "ThreadMesh" |
ansond | 54:dfee8691c83a | 48 | #define MESH |
ansond | 54:dfee8691c83a | 49 | #include "NanostackInterface.h" |
ansond | 54:dfee8691c83a | 50 | ThreadInterface network; |
ansond | 54:dfee8691c83a | 51 | #endif |
ansond | 54:dfee8691c83a | 52 | |
ansond | 54:dfee8691c83a | 53 | // Logger instance |
ansond | 54:dfee8691c83a | 54 | extern Logger logger; |
ansond | 54:dfee8691c83a | 55 | |
ansond | 54:dfee8691c83a | 56 | // endpoint instance |
ansond | 54:dfee8691c83a | 57 | static void *_endpoint_instance = NULL; |
ansond | 54:dfee8691c83a | 58 | |
ansond | 54:dfee8691c83a | 59 | // LWIP network instance forward reference |
ansond | 54:dfee8691c83a | 60 | extern void *__network_interface; |
ansond | 54:dfee8691c83a | 61 | |
ansond | 54:dfee8691c83a | 62 | // main loop cycle period |
ansond | 54:dfee8691c83a | 63 | static int _main_loop_iteration_wait_ms = MAIN_LOOP_WAIT_TIME_MS; |
ansond | 54:dfee8691c83a | 64 | |
ansond | 54:dfee8691c83a | 65 | // endpoint shutdown indicator |
ansond | 54:dfee8691c83a | 66 | static volatile bool _shutdown_endpoint = false; |
ansond | 54:dfee8691c83a | 67 | |
ansond | 54:dfee8691c83a | 68 | extern "C" { |
ansond | 54:dfee8691c83a | 69 | |
ansond | 54:dfee8691c83a | 70 | /*********************** START LOCAL FUNCTIONS **************************/ |
ansond | 54:dfee8691c83a | 71 | |
ansond | 54:dfee8691c83a | 72 | // start shutting downt the endpoint |
ansond | 54:dfee8691c83a | 73 | void start_endpoint_shutdown(void) { |
ansond | 54:dfee8691c83a | 74 | if (_shutdown_endpoint == true) { |
ansond | 54:dfee8691c83a | 75 | Connector::Endpoint *ep = (Connector::Endpoint *)_endpoint_instance; |
ansond | 54:dfee8691c83a | 76 | if (ep != NULL && ep->isRegistered() == true) { |
ansond | 54:dfee8691c83a | 77 | logger.log("mbedEndpointNetwork(%s): shutdown requested. De-registering the endpoint...",NETWORK_TYPE); |
ansond | 54:dfee8691c83a | 78 | ep->de_register_endpoint(); |
ansond | 54:dfee8691c83a | 79 | } |
ansond | 54:dfee8691c83a | 80 | } |
ansond | 54:dfee8691c83a | 81 | |
ansond | 54:dfee8691c83a | 82 | // ready to shutdown... |
ansond | 54:dfee8691c83a | 83 | logger.log("mbedEndpointNetwork(%s): endpoint shutdown. Bye!",NETWORK_TYPE); |
ansond | 54:dfee8691c83a | 84 | } |
ansond | 54:dfee8691c83a | 85 | |
ansond | 54:dfee8691c83a | 86 | // setup shutdown button |
ansond | 54:dfee8691c83a | 87 | #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true |
ansond | 54:dfee8691c83a | 88 | InterruptIn shutdown_button(MBED_CONF_APP_SHUTDOWN_PIN); |
ansond | 54:dfee8691c83a | 89 | void configure_deregistration_button(void) { |
ansond | 54:dfee8691c83a | 90 | logger.log("mbedEndpointNetwork(%s): configuring de-registration button...",NETWORK_TYPE); |
ansond | 54:dfee8691c83a | 91 | shutdown_button.fall(&net_shutdown_endpoint); |
ansond | 54:dfee8691c83a | 92 | } |
ansond | 54:dfee8691c83a | 93 | #endif |
ansond | 54:dfee8691c83a | 94 | |
ansond | 54:dfee8691c83a | 95 | // setup shutdown button |
ansond | 54:dfee8691c83a | 96 | void setup_deregistration_button(void) { |
ansond | 54:dfee8691c83a | 97 | #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true |
ansond | 54:dfee8691c83a | 98 | configure_deregistration_button(); |
ansond | 54:dfee8691c83a | 99 | #endif |
ansond | 54:dfee8691c83a | 100 | } |
ansond | 54:dfee8691c83a | 101 | |
ansond | 54:dfee8691c83a | 102 | // configure main loop parameters |
ansond | 54:dfee8691c83a | 103 | void configure_main_loop_params(Connector::Endpoint *endpoint) { |
ansond | 54:dfee8691c83a | 104 | // set the initial shutdown state |
ansond | 54:dfee8691c83a | 105 | _shutdown_endpoint = false; |
ansond | 54:dfee8691c83a | 106 | } |
ansond | 54:dfee8691c83a | 107 | |
ansond | 54:dfee8691c83a | 108 | // perform an actvity in the main loop |
ansond | 54:dfee8691c83a | 109 | void peform_main_loop_activity(void) { |
ansond | 54:dfee8691c83a | 110 | // empty for now... |
ansond | 54:dfee8691c83a | 111 | ; |
ansond | 54:dfee8691c83a | 112 | } |
ansond | 54:dfee8691c83a | 113 | |
ansond | 54:dfee8691c83a | 114 | // begin the main loop for processing endpoint events |
ansond | 54:dfee8691c83a | 115 | void begin_main_loop(void) |
ansond | 54:dfee8691c83a | 116 | { |
ansond | 54:dfee8691c83a | 117 | // DEBUG |
ansond | 54:dfee8691c83a | 118 | logger.log("mbedEndpointNetwork(%s): endpoint main loop beginning...",NETWORK_TYPE); |
ansond | 54:dfee8691c83a | 119 | |
ansond | 54:dfee8691c83a | 120 | // enter our main loop (until the shutdown condition flags it...) |
ansond | 54:dfee8691c83a | 121 | while(_shutdown_endpoint == false) { |
ansond | 54:dfee8691c83a | 122 | Thread::wait(_main_loop_iteration_wait_ms); |
ansond | 54:dfee8691c83a | 123 | peform_main_loop_activity(); |
ansond | 54:dfee8691c83a | 124 | } |
ansond | 54:dfee8691c83a | 125 | |
ansond | 54:dfee8691c83a | 126 | // main loop has exited... start the endpoint shutdown... |
ansond | 54:dfee8691c83a | 127 | logger.log("mbedEndpointNetwork(%s): endpoint main loop exited. Starting endpoint shutdown...",NETWORK_TYPE); |
ansond | 54:dfee8691c83a | 128 | start_endpoint_shutdown(); |
ansond | 54:dfee8691c83a | 129 | } |
ansond | 54:dfee8691c83a | 130 | |
ansond | 54:dfee8691c83a | 131 | /************************ END LOCAL FUNCTIONS ***************************/ |
ansond | 54:dfee8691c83a | 132 | |
ansond | 54:dfee8691c83a | 133 | /*********************** START PUBLIC FUNCTIONS *************************/ |
ansond | 54:dfee8691c83a | 134 | |
ansond | 54:dfee8691c83a | 135 | // get the network type |
ansond | 54:dfee8691c83a | 136 | char *net_get_type() { |
ansond | 54:dfee8691c83a | 137 | return NETWORK_TYPE; |
ansond | 54:dfee8691c83a | 138 | } |
ansond | 54:dfee8691c83a | 139 | |
ansond | 54:dfee8691c83a | 140 | // shutdown the endpoint |
ansond | 54:dfee8691c83a | 141 | void net_shutdown_endpoint() { |
ansond | 54:dfee8691c83a | 142 | _shutdown_endpoint = true; |
ansond | 54:dfee8691c83a | 143 | } |
ansond | 54:dfee8691c83a | 144 | |
ansond | 54:dfee8691c83a | 145 | // called after the endpoint is configured... |
ansond | 54:dfee8691c83a | 146 | void net_plumb_network(void *p) |
ansond | 54:dfee8691c83a | 147 | { |
ansond | 54:dfee8691c83a | 148 | int connected = 0; |
ansond | 54:dfee8691c83a | 149 | Connector::Endpoint *ep = NULL; |
ansond | 54:dfee8691c83a | 150 | Connector::Options *options = NULL; |
ansond | 54:dfee8691c83a | 151 | |
ansond | 54:dfee8691c83a | 152 | // save |
ansond | 54:dfee8691c83a | 153 | _endpoint_instance = p; |
ansond | 54:dfee8691c83a | 154 | |
ansond | 54:dfee8691c83a | 155 | // connected |
ansond | 54:dfee8691c83a | 156 | if (p != NULL) { |
ansond | 54:dfee8691c83a | 157 | ep = (Connector::Endpoint *)p; |
ansond | 54:dfee8691c83a | 158 | options = ep->getOptions(); |
ansond | 54:dfee8691c83a | 159 | } |
ansond | 54:dfee8691c83a | 160 | |
ansond | 54:dfee8691c83a | 161 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI |
ansond | 54:dfee8691c83a | 162 | // map security types |
ansond | 54:dfee8691c83a | 163 | nsapi_security_t security_opt = NSAPI_SECURITY_NONE; |
ansond | 54:dfee8691c83a | 164 | if (options->getWiFiAuthType() == WIFI_WPA_PERSONAL) { |
ansond | 54:dfee8691c83a | 165 | security_opt = NSAPI_SECURITY_WPA; |
ansond | 54:dfee8691c83a | 166 | } |
ansond | 54:dfee8691c83a | 167 | if (options->getWiFiAuthType() == WIFI_WPA2_PERSONAL) { |
ansond | 54:dfee8691c83a | 168 | security_opt = NSAPI_SECURITY_WPA2; |
ansond | 54:dfee8691c83a | 169 | } |
ansond | 54:dfee8691c83a | 170 | if (options->getWiFiAuthType() == WIFI_WEP) { |
ansond | 54:dfee8691c83a | 171 | security_opt = NSAPI_SECURITY_WEP; |
ansond | 54:dfee8691c83a | 172 | } |
ansond | 54:dfee8691c83a | 173 | |
ansond | 54:dfee8691c83a | 174 | // Network Init (WIFI)... |
ansond | 54:dfee8691c83a | 175 | connected = network.connect(options->getWiFiSSID().c_str(),options->getWiFiAuthKey().c_str(),security_opt); |
ansond | 56:3f233795dddf | 176 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND || MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD |
ansond | 54:dfee8691c83a | 177 | // Set the IP Address type to IPV6 |
ansond | 54:dfee8691c83a | 178 | ((Connector::OptionsBuilder *)options)->setIPAddressType(IP_ADDRESS_TYPE_IPV6); |
ansond | 54:dfee8691c83a | 179 | |
ansond | 54:dfee8691c83a | 180 | // Network Init (Mesh) |
ansond | 54:dfee8691c83a | 181 | connected = network.connect(); |
ansond | 54:dfee8691c83a | 182 | #else |
ansond | 54:dfee8691c83a | 183 | // not used... just removes a compiler warning... |
ansond | 54:dfee8691c83a | 184 | options->getConnectorURL(); |
ansond | 54:dfee8691c83a | 185 | |
ansond | 54:dfee8691c83a | 186 | // Network Init (Ethernet) |
ansond | 54:dfee8691c83a | 187 | connected = network.connect(); |
ansond | 54:dfee8691c83a | 188 | #endif |
ansond | 54:dfee8691c83a | 189 | |
ansond | 54:dfee8691c83a | 190 | // check the connection status.. |
ansond | 54:dfee8691c83a | 191 | if (connected == 0) { |
ansond | 54:dfee8691c83a | 192 | __network_interface = (void *)&network; |
ansond | 54:dfee8691c83a | 193 | if (ep != NULL) { |
ansond | 54:dfee8691c83a | 194 | ep->isConnected(true); |
ansond | 54:dfee8691c83a | 195 | } |
ansond | 54:dfee8691c83a | 196 | |
ansond | 54:dfee8691c83a | 197 | // Debug |
ansond | 54:dfee8691c83a | 198 | logger.log("mbedEndpointNetwork(%s): IP Address: %s",NETWORK_TYPE,network.get_ip_address()); |
ansond | 54:dfee8691c83a | 199 | } |
ansond | 54:dfee8691c83a | 200 | else { |
ansond | 54:dfee8691c83a | 201 | __network_interface = NULL; |
ansond | 54:dfee8691c83a | 202 | if (ep != NULL) { |
ansond | 54:dfee8691c83a | 203 | ep->isConnected(false); |
ansond | 54:dfee8691c83a | 204 | } |
ansond | 54:dfee8691c83a | 205 | |
ansond | 54:dfee8691c83a | 206 | // Debug |
ansond | 54:dfee8691c83a | 207 | logger.log("mbedEndpointNetwork(%s): CONNECTION FAILED",NETWORK_TYPE); |
ansond | 54:dfee8691c83a | 208 | } |
ansond | 54:dfee8691c83a | 209 | } |
ansond | 54:dfee8691c83a | 210 | |
ansond | 54:dfee8691c83a | 211 | // finalize and run the endpoint main loop |
ansond | 54:dfee8691c83a | 212 | void net_finalize_and_run_endpoint_main_loop(void *p) |
ansond | 54:dfee8691c83a | 213 | { |
ansond | 54:dfee8691c83a | 214 | // cast |
ansond | 54:dfee8691c83a | 215 | Connector::Endpoint *ep = (Connector::Endpoint *)p; |
ansond | 54:dfee8691c83a | 216 | |
ansond | 54:dfee8691c83a | 217 | // Initialize our main loop... |
ansond | 54:dfee8691c83a | 218 | configure_main_loop_params(ep); |
ansond | 54:dfee8691c83a | 219 | |
ansond | 54:dfee8691c83a | 220 | // setup the shutdown button (if enabled for a given platform...) |
ansond | 54:dfee8691c83a | 221 | setup_deregistration_button(); |
ansond | 54:dfee8691c83a | 222 | |
ansond | 54:dfee8691c83a | 223 | // register the endpoint |
ansond | 54:dfee8691c83a | 224 | logger.log("mbedEndpointNetwork(%s): registering endpoint...",NETWORK_TYPE); |
ansond | 54:dfee8691c83a | 225 | ep->register_endpoint(ep->getEndpointSecurity(),ep->getEndpointObjectList()); |
ansond | 54:dfee8691c83a | 226 | |
ansond | 54:dfee8691c83a | 227 | // Begin the endpoint's main loop |
ansond | 54:dfee8691c83a | 228 | begin_main_loop(); |
ansond | 54:dfee8691c83a | 229 | } |
ansond | 54:dfee8691c83a | 230 | |
ansond | 54:dfee8691c83a | 231 | /************************ END PUBLIC FUNCTIONS **************************/ |
ansond | 54:dfee8691c83a | 232 | |
ansond | 54:dfee8691c83a | 233 | } |