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:
Thu Apr 20 21:46:55 2017 +0000
Revision:
103:33724194f316
Parent:
102:4eb8eff3431f
Child:
104:bc8bb28f0f4c
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 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 102:4eb8eff3431f 56 // EasyConnect reference
ansond 103:33724194f316 57 extern NetworkInterface *easy_connect(bool log_messages);
ansond 102:4eb8eff3431f 58
ansond 54:dfee8691c83a 59 // endpoint instance
ansond 54:dfee8691c83a 60 static void *_endpoint_instance = NULL;
ansond 54:dfee8691c83a 61
ansond 54:dfee8691c83a 62 // LWIP network instance forward reference
ansond 61:d02cd5e2bb26 63 extern NetworkInterface *__network_interface;
ansond 54:dfee8691c83a 64
ansond 54:dfee8691c83a 65 // main loop cycle period
ansond 54:dfee8691c83a 66 static int _main_loop_iteration_wait_ms = MAIN_LOOP_WAIT_TIME_MS;
ansond 54:dfee8691c83a 67
ansond 54:dfee8691c83a 68 // endpoint shutdown indicator
ansond 54:dfee8691c83a 69 static volatile bool _shutdown_endpoint = false;
ansond 54:dfee8691c83a 70
ansond 54:dfee8691c83a 71 extern "C" {
ansond 54:dfee8691c83a 72
ansond 54:dfee8691c83a 73 /*********************** START LOCAL FUNCTIONS **************************/
ansond 54:dfee8691c83a 74
ansond 54:dfee8691c83a 75 // start shutting downt the endpoint
ansond 54:dfee8691c83a 76 void start_endpoint_shutdown(void) {
ansond 54:dfee8691c83a 77 if (_shutdown_endpoint == true) {
ansond 54:dfee8691c83a 78 Connector::Endpoint *ep = (Connector::Endpoint *)_endpoint_instance;
ansond 54:dfee8691c83a 79 if (ep != NULL && ep->isRegistered() == true) {
ansond 54:dfee8691c83a 80 logger.log("mbedEndpointNetwork(%s): shutdown requested. De-registering the endpoint...",NETWORK_TYPE);
ansond 54:dfee8691c83a 81 ep->de_register_endpoint();
ansond 54:dfee8691c83a 82 }
ansond 63:fc30c31a4d75 83
ansond 63:fc30c31a4d75 84 // Clean up
ansond 63:fc30c31a4d75 85 if (ep != NULL) {
ansond 63:fc30c31a4d75 86 delete ep;
ansond 63:fc30c31a4d75 87 _endpoint_instance = NULL;
ansond 63:fc30c31a4d75 88 }
ansond 54:dfee8691c83a 89 }
ansond 54:dfee8691c83a 90
ansond 54:dfee8691c83a 91 // ready to shutdown...
ansond 54:dfee8691c83a 92 logger.log("mbedEndpointNetwork(%s): endpoint shutdown. Bye!",NETWORK_TYPE);
ansond 54:dfee8691c83a 93 }
ansond 54:dfee8691c83a 94
ansond 54:dfee8691c83a 95 // setup shutdown button
ansond 54:dfee8691c83a 96 #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
ansond 94:25ce0592620f 97 #ifdef TARGET_K64F
ansond 54:dfee8691c83a 98 InterruptIn shutdown_button(MBED_CONF_APP_SHUTDOWN_PIN);
ansond 94:25ce0592620f 99 #endif
ansond 54:dfee8691c83a 100 void configure_deregistration_button(void) {
ansond 94:25ce0592620f 101 #ifdef TARGET_K64F
ansond 54:dfee8691c83a 102 logger.log("mbedEndpointNetwork(%s): configuring de-registration button...",NETWORK_TYPE);
ansond 54:dfee8691c83a 103 shutdown_button.fall(&net_shutdown_endpoint);
ansond 94:25ce0592620f 104 #endif
ansond 94:25ce0592620f 105 }
ansond 54:dfee8691c83a 106 #endif
ansond 54:dfee8691c83a 107
ansond 54:dfee8691c83a 108 // setup shutdown button
ansond 54:dfee8691c83a 109 void setup_deregistration_button(void) {
ansond 54:dfee8691c83a 110 #if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
ansond 54:dfee8691c83a 111 configure_deregistration_button();
ansond 54:dfee8691c83a 112 #endif
ansond 54:dfee8691c83a 113 }
ansond 54:dfee8691c83a 114
ansond 54:dfee8691c83a 115 // configure main loop parameters
ansond 54:dfee8691c83a 116 void configure_main_loop_params(Connector::Endpoint *endpoint) {
ansond 54:dfee8691c83a 117 // set the initial shutdown state
ansond 54:dfee8691c83a 118 _shutdown_endpoint = false;
ansond 54:dfee8691c83a 119 }
ansond 54:dfee8691c83a 120
ansond 54:dfee8691c83a 121 // perform an actvity in the main loop
ansond 54:dfee8691c83a 122 void peform_main_loop_activity(void) {
ansond 54:dfee8691c83a 123 // empty for now...
ansond 54:dfee8691c83a 124 ;
ansond 54:dfee8691c83a 125 }
ansond 54:dfee8691c83a 126
ansond 54:dfee8691c83a 127 // begin the main loop for processing endpoint events
ansond 54:dfee8691c83a 128 void begin_main_loop(void)
ansond 54:dfee8691c83a 129 {
ansond 54:dfee8691c83a 130 // DEBUG
ansond 54:dfee8691c83a 131 logger.log("mbedEndpointNetwork(%s): endpoint main loop beginning...",NETWORK_TYPE);
ansond 54:dfee8691c83a 132
ansond 54:dfee8691c83a 133 // enter our main loop (until the shutdown condition flags it...)
ansond 54:dfee8691c83a 134 while(_shutdown_endpoint == false) {
ansond 54:dfee8691c83a 135 Thread::wait(_main_loop_iteration_wait_ms);
ansond 54:dfee8691c83a 136 peform_main_loop_activity();
ansond 54:dfee8691c83a 137 }
ansond 54:dfee8691c83a 138
ansond 54:dfee8691c83a 139 // main loop has exited... start the endpoint shutdown...
ansond 54:dfee8691c83a 140 logger.log("mbedEndpointNetwork(%s): endpoint main loop exited. Starting endpoint shutdown...",NETWORK_TYPE);
ansond 54:dfee8691c83a 141 start_endpoint_shutdown();
ansond 54:dfee8691c83a 142 }
ansond 54:dfee8691c83a 143
ansond 54:dfee8691c83a 144 /************************ END LOCAL FUNCTIONS ***************************/
ansond 54:dfee8691c83a 145
ansond 54:dfee8691c83a 146 /*********************** START PUBLIC FUNCTIONS *************************/
ansond 54:dfee8691c83a 147
ansond 54:dfee8691c83a 148 // get the network type
ansond 54:dfee8691c83a 149 char *net_get_type() {
ansond 54:dfee8691c83a 150 return NETWORK_TYPE;
ansond 54:dfee8691c83a 151 }
ansond 54:dfee8691c83a 152
ansond 54:dfee8691c83a 153 // shutdown the endpoint
ansond 54:dfee8691c83a 154 void net_shutdown_endpoint() {
ansond 54:dfee8691c83a 155 _shutdown_endpoint = true;
ansond 54:dfee8691c83a 156 }
ansond 54:dfee8691c83a 157
ansond 54:dfee8691c83a 158 // called after the endpoint is configured...
ansond 54:dfee8691c83a 159 void net_plumb_network(void *p)
ansond 54:dfee8691c83a 160 {
ansond 54:dfee8691c83a 161 Connector::Endpoint *ep = NULL;
ansond 102:4eb8eff3431f 162 //Connector::Options *options = NULL;
ansond 54:dfee8691c83a 163
ansond 54:dfee8691c83a 164 // save
ansond 54:dfee8691c83a 165 _endpoint_instance = p;
ansond 54:dfee8691c83a 166
ansond 54:dfee8691c83a 167 // connected
ansond 54:dfee8691c83a 168 if (p != NULL) {
ansond 54:dfee8691c83a 169 ep = (Connector::Endpoint *)p;
ansond 102:4eb8eff3431f 170 //options = ep->getOptions();
ansond 54:dfee8691c83a 171 }
ansond 94:25ce0592620f 172
ansond 94:25ce0592620f 173 // connect (use easy-connect now...)
ansond 94:25ce0592620f 174 __network_interface = easy_connect(EASY_CONNECT_DEBUG);
ansond 54:dfee8691c83a 175
ansond 54:dfee8691c83a 176 // check the connection status..
ansond 94:25ce0592620f 177 if (__network_interface != NULL) {
ansond 61:d02cd5e2bb26 178 // success
ansond 54:dfee8691c83a 179 if (ep != NULL) {
ansond 54:dfee8691c83a 180 ep->isConnected(true);
ansond 54:dfee8691c83a 181
ansond 61:d02cd5e2bb26 182 // Debug
ansond 94:25ce0592620f 183 logger.log("mbedEndpointNetwork(%s): IP Address: %s",NETWORK_TYPE,__network_interface->get_ip_address());
ansond 61:d02cd5e2bb26 184 }
ansond 54:dfee8691c83a 185 }
ansond 54:dfee8691c83a 186 else {
ansond 94:25ce0592620f 187 // connection error
ansond 54:dfee8691c83a 188 __network_interface = NULL;
ansond 54:dfee8691c83a 189 if (ep != NULL) {
ansond 54:dfee8691c83a 190 ep->isConnected(false);
ansond 54:dfee8691c83a 191 }
ansond 54:dfee8691c83a 192
ansond 54:dfee8691c83a 193 // Debug
ansond 54:dfee8691c83a 194 logger.log("mbedEndpointNetwork(%s): CONNECTION FAILED",NETWORK_TYPE);
ansond 54:dfee8691c83a 195 }
ansond 54:dfee8691c83a 196 }
ansond 54:dfee8691c83a 197
ansond 54:dfee8691c83a 198 // finalize and run the endpoint main loop
ansond 54:dfee8691c83a 199 void net_finalize_and_run_endpoint_main_loop(void *p)
ansond 54:dfee8691c83a 200 {
ansond 54:dfee8691c83a 201 // cast
ansond 54:dfee8691c83a 202 Connector::Endpoint *ep = (Connector::Endpoint *)p;
ansond 54:dfee8691c83a 203
ansond 54:dfee8691c83a 204 // Initialize our main loop...
ansond 54:dfee8691c83a 205 configure_main_loop_params(ep);
ansond 54:dfee8691c83a 206
ansond 54:dfee8691c83a 207 // setup the shutdown button (if enabled for a given platform...)
ansond 54:dfee8691c83a 208 setup_deregistration_button();
ansond 54:dfee8691c83a 209
ansond 54:dfee8691c83a 210 // register the endpoint
ansond 54:dfee8691c83a 211 logger.log("mbedEndpointNetwork(%s): registering endpoint...",NETWORK_TYPE);
ansond 78:7fdf3322de58 212 #ifdef ENABLE_MBED_CLOUD_SUPPORT
ansond 78:7fdf3322de58 213 ep->register_endpoint(NULL,ep->getEndpointObjectList());
ansond 78:7fdf3322de58 214 #else
ansond 81:a2441163a06e 215 ep->register_endpoint(ep->getSecurityInstance(),ep->getEndpointObjectList());
ansond 78:7fdf3322de58 216 #endif
ansond 54:dfee8691c83a 217
ansond 54:dfee8691c83a 218 // Begin the endpoint's main loop
ansond 54:dfee8691c83a 219 begin_main_loop();
ansond 54:dfee8691c83a 220 }
ansond 54:dfee8691c83a 221
ansond 54:dfee8691c83a 222 /************************ END PUBLIC FUNCTIONS **************************/
ansond 54:dfee8691c83a 223
ansond 54:dfee8691c83a 224 }