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@127:b4a661ff6fb9, 2017-09-26 (annotated)
- Committer:
- ansond
- Date:
- Tue Sep 26 16:01:31 2017 +0000
- Revision:
- 127:b4a661ff6fb9
- Parent:
- 104:bc8bb28f0f4c
minor re-ordering of FCC init
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 | 94:25ce0592620f | 32 | // Enable/Disable easy-connect debugging |
ansond | 94:25ce0592620f | 33 | #define EASY_CONNECT_DEBUG false |
ansond | 94:25ce0592620f | 34 | |
ansond | 94:25ce0592620f | 35 | // Network Selection (via easy-connect library now...) |
ansond | 94:25ce0592620f | 36 | #include "easy-connect.h" |
ansond | 94:25ce0592620f | 37 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266 |
ansond | 94:25ce0592620f | 38 | #define NETWORK_TYPE (char *)"WiFi-ESP8266" |
ansond | 94:25ce0592620f | 39 | #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN |
ansond | 94:25ce0592620f | 40 | #define NETWORK_TYPE (char *)"WiFi-ODIN" |
ansond | 54:dfee8691c83a | 41 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
ansond | 94:25ce0592620f | 42 | #define NETWORK_TYPE (char *)"Ethernet" |
ansond | 94:25ce0592620f | 43 | #elif MBED_CONF_APP_NETWORK_INTERFACE == CELL |
ansond | 94:25ce0592620f | 44 | #define NETWORK_TYPE (char *)"Cellular" |
ansond | 56:3f233795dddf | 45 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND |
ansond | 94:25ce0592620f | 46 | #define NETWORK_TYPE (char *)"6LowPAN" |
ansond | 56:3f233795dddf | 47 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD |
ansond | 94:25ce0592620f | 48 | #define NETWORK_TYPE (char *)"Thread" |
ansond | 94:25ce0592620f | 49 | else |
ansond | 94:25ce0592620f | 50 | #define NETWORK_TYPE (char *)"UNKNOWN" |
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 | 61:d02cd5e2bb26 | 60 | extern NetworkInterface *__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 | 63:fc30c31a4d75 | 80 | |
ansond | 63:fc30c31a4d75 | 81 | // Clean up |
ansond | 63:fc30c31a4d75 | 82 | if (ep != NULL) { |
ansond | 63:fc30c31a4d75 | 83 | delete ep; |
ansond | 63:fc30c31a4d75 | 84 | _endpoint_instance = NULL; |
ansond | 63:fc30c31a4d75 | 85 | } |
ansond | 54:dfee8691c83a | 86 | } |
ansond | 54:dfee8691c83a | 87 | |
ansond | 54:dfee8691c83a | 88 | // ready to shutdown... |
ansond | 54:dfee8691c83a | 89 | logger.log("mbedEndpointNetwork(%s): endpoint shutdown. Bye!",NETWORK_TYPE); |
ansond | 54:dfee8691c83a | 90 | } |
ansond | 54:dfee8691c83a | 91 | |
ansond | 54:dfee8691c83a | 92 | // setup shutdown button |
ansond | 54:dfee8691c83a | 93 | #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true |
ansond | 94:25ce0592620f | 94 | #ifdef TARGET_K64F |
ansond | 54:dfee8691c83a | 95 | InterruptIn shutdown_button(MBED_CONF_APP_SHUTDOWN_PIN); |
ansond | 94:25ce0592620f | 96 | #endif |
ansond | 54:dfee8691c83a | 97 | void configure_deregistration_button(void) { |
ansond | 94:25ce0592620f | 98 | #ifdef TARGET_K64F |
ansond | 54:dfee8691c83a | 99 | logger.log("mbedEndpointNetwork(%s): configuring de-registration button...",NETWORK_TYPE); |
ansond | 54:dfee8691c83a | 100 | shutdown_button.fall(&net_shutdown_endpoint); |
ansond | 94:25ce0592620f | 101 | #endif |
ansond | 94:25ce0592620f | 102 | } |
ansond | 54:dfee8691c83a | 103 | #endif |
ansond | 54:dfee8691c83a | 104 | |
ansond | 54:dfee8691c83a | 105 | // setup shutdown button |
ansond | 54:dfee8691c83a | 106 | void setup_deregistration_button(void) { |
ansond | 54:dfee8691c83a | 107 | #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true |
ansond | 54:dfee8691c83a | 108 | configure_deregistration_button(); |
ansond | 54:dfee8691c83a | 109 | #endif |
ansond | 54:dfee8691c83a | 110 | } |
ansond | 54:dfee8691c83a | 111 | |
ansond | 54:dfee8691c83a | 112 | // configure main loop parameters |
ansond | 54:dfee8691c83a | 113 | void configure_main_loop_params(Connector::Endpoint *endpoint) { |
ansond | 54:dfee8691c83a | 114 | // set the initial shutdown state |
ansond | 54:dfee8691c83a | 115 | _shutdown_endpoint = false; |
ansond | 54:dfee8691c83a | 116 | } |
ansond | 54:dfee8691c83a | 117 | |
ansond | 54:dfee8691c83a | 118 | // perform an actvity in the main loop |
ansond | 54:dfee8691c83a | 119 | void peform_main_loop_activity(void) { |
ansond | 54:dfee8691c83a | 120 | // empty for now... |
ansond | 54:dfee8691c83a | 121 | ; |
ansond | 54:dfee8691c83a | 122 | } |
ansond | 54:dfee8691c83a | 123 | |
ansond | 54:dfee8691c83a | 124 | // begin the main loop for processing endpoint events |
ansond | 54:dfee8691c83a | 125 | void begin_main_loop(void) |
ansond | 54:dfee8691c83a | 126 | { |
ansond | 54:dfee8691c83a | 127 | // DEBUG |
ansond | 54:dfee8691c83a | 128 | logger.log("mbedEndpointNetwork(%s): endpoint main loop beginning...",NETWORK_TYPE); |
ansond | 54:dfee8691c83a | 129 | |
ansond | 54:dfee8691c83a | 130 | // enter our main loop (until the shutdown condition flags it...) |
ansond | 54:dfee8691c83a | 131 | while(_shutdown_endpoint == false) { |
ansond | 54:dfee8691c83a | 132 | Thread::wait(_main_loop_iteration_wait_ms); |
ansond | 54:dfee8691c83a | 133 | peform_main_loop_activity(); |
ansond | 54:dfee8691c83a | 134 | } |
ansond | 54:dfee8691c83a | 135 | |
ansond | 54:dfee8691c83a | 136 | // main loop has exited... start the endpoint shutdown... |
ansond | 54:dfee8691c83a | 137 | logger.log("mbedEndpointNetwork(%s): endpoint main loop exited. Starting endpoint shutdown...",NETWORK_TYPE); |
ansond | 54:dfee8691c83a | 138 | start_endpoint_shutdown(); |
ansond | 54:dfee8691c83a | 139 | } |
ansond | 54:dfee8691c83a | 140 | |
ansond | 54:dfee8691c83a | 141 | /************************ END LOCAL FUNCTIONS ***************************/ |
ansond | 54:dfee8691c83a | 142 | |
ansond | 54:dfee8691c83a | 143 | /*********************** START PUBLIC FUNCTIONS *************************/ |
ansond | 54:dfee8691c83a | 144 | |
ansond | 54:dfee8691c83a | 145 | // get the network type |
ansond | 54:dfee8691c83a | 146 | char *net_get_type() { |
ansond | 54:dfee8691c83a | 147 | return NETWORK_TYPE; |
ansond | 54:dfee8691c83a | 148 | } |
ansond | 54:dfee8691c83a | 149 | |
ansond | 54:dfee8691c83a | 150 | // shutdown the endpoint |
ansond | 54:dfee8691c83a | 151 | void net_shutdown_endpoint() { |
ansond | 54:dfee8691c83a | 152 | _shutdown_endpoint = true; |
ansond | 54:dfee8691c83a | 153 | } |
ansond | 54:dfee8691c83a | 154 | |
ansond | 54:dfee8691c83a | 155 | // called after the endpoint is configured... |
ansond | 54:dfee8691c83a | 156 | void net_plumb_network(void *p) |
ansond | 54:dfee8691c83a | 157 | { |
ansond | 54:dfee8691c83a | 158 | Connector::Endpoint *ep = NULL; |
ansond | 102:4eb8eff3431f | 159 | //Connector::Options *options = NULL; |
ansond | 54:dfee8691c83a | 160 | |
ansond | 54:dfee8691c83a | 161 | // save |
ansond | 54:dfee8691c83a | 162 | _endpoint_instance = p; |
ansond | 54:dfee8691c83a | 163 | |
ansond | 54:dfee8691c83a | 164 | // connected |
ansond | 54:dfee8691c83a | 165 | if (p != NULL) { |
ansond | 54:dfee8691c83a | 166 | ep = (Connector::Endpoint *)p; |
ansond | 102:4eb8eff3431f | 167 | //options = ep->getOptions(); |
ansond | 54:dfee8691c83a | 168 | } |
ansond | 94:25ce0592620f | 169 | |
ansond | 94:25ce0592620f | 170 | // connect (use easy-connect now...) |
ansond | 94:25ce0592620f | 171 | __network_interface = easy_connect(EASY_CONNECT_DEBUG); |
ansond | 54:dfee8691c83a | 172 | |
ansond | 54:dfee8691c83a | 173 | // check the connection status.. |
ansond | 94:25ce0592620f | 174 | if (__network_interface != NULL) { |
ansond | 61:d02cd5e2bb26 | 175 | // success |
ansond | 54:dfee8691c83a | 176 | if (ep != NULL) { |
ansond | 54:dfee8691c83a | 177 | ep->isConnected(true); |
ansond | 54:dfee8691c83a | 178 | |
ansond | 61:d02cd5e2bb26 | 179 | // Debug |
ansond | 94:25ce0592620f | 180 | logger.log("mbedEndpointNetwork(%s): IP Address: %s",NETWORK_TYPE,__network_interface->get_ip_address()); |
ansond | 61:d02cd5e2bb26 | 181 | } |
ansond | 54:dfee8691c83a | 182 | } |
ansond | 54:dfee8691c83a | 183 | else { |
ansond | 94:25ce0592620f | 184 | // connection error |
ansond | 54:dfee8691c83a | 185 | __network_interface = NULL; |
ansond | 54:dfee8691c83a | 186 | if (ep != NULL) { |
ansond | 54:dfee8691c83a | 187 | ep->isConnected(false); |
ansond | 54:dfee8691c83a | 188 | } |
ansond | 54:dfee8691c83a | 189 | |
ansond | 54:dfee8691c83a | 190 | // Debug |
ansond | 54:dfee8691c83a | 191 | logger.log("mbedEndpointNetwork(%s): CONNECTION FAILED",NETWORK_TYPE); |
ansond | 54:dfee8691c83a | 192 | } |
ansond | 54:dfee8691c83a | 193 | } |
ansond | 54:dfee8691c83a | 194 | |
ansond | 54:dfee8691c83a | 195 | // finalize and run the endpoint main loop |
ansond | 54:dfee8691c83a | 196 | void net_finalize_and_run_endpoint_main_loop(void *p) |
ansond | 54:dfee8691c83a | 197 | { |
ansond | 54:dfee8691c83a | 198 | // cast |
ansond | 54:dfee8691c83a | 199 | Connector::Endpoint *ep = (Connector::Endpoint *)p; |
ansond | 54:dfee8691c83a | 200 | |
ansond | 54:dfee8691c83a | 201 | // Initialize our main loop... |
ansond | 54:dfee8691c83a | 202 | configure_main_loop_params(ep); |
ansond | 54:dfee8691c83a | 203 | |
ansond | 54:dfee8691c83a | 204 | // setup the shutdown button (if enabled for a given platform...) |
ansond | 54:dfee8691c83a | 205 | setup_deregistration_button(); |
ansond | 54:dfee8691c83a | 206 | |
ansond | 54:dfee8691c83a | 207 | // register the endpoint |
ansond | 54:dfee8691c83a | 208 | logger.log("mbedEndpointNetwork(%s): registering endpoint...",NETWORK_TYPE); |
ansond | 78:7fdf3322de58 | 209 | #ifdef ENABLE_MBED_CLOUD_SUPPORT |
ansond | 78:7fdf3322de58 | 210 | ep->register_endpoint(NULL,ep->getEndpointObjectList()); |
ansond | 78:7fdf3322de58 | 211 | #else |
ansond | 81:a2441163a06e | 212 | ep->register_endpoint(ep->getSecurityInstance(),ep->getEndpointObjectList()); |
ansond | 78:7fdf3322de58 | 213 | #endif |
ansond | 54:dfee8691c83a | 214 | |
ansond | 54:dfee8691c83a | 215 | // Begin the endpoint's main loop |
ansond | 54:dfee8691c83a | 216 | begin_main_loop(); |
ansond | 54:dfee8691c83a | 217 | } |
ansond | 54:dfee8691c83a | 218 | |
ansond | 54:dfee8691c83a | 219 | /************************ END PUBLIC FUNCTIONS **************************/ |
ansond | 54:dfee8691c83a | 220 | |
ansond | 54:dfee8691c83a | 221 | } |