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!

Revision:
54:dfee8691c83a
Child:
56:3f233795dddf
diff -r d2f9f94b6000 -r dfee8691c83a source/mbedEndpointNetwork.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/mbedEndpointNetwork.cpp	Tue Aug 09 17:18:49 2016 +0000
@@ -0,0 +1,233 @@
+/**
+ * @file    mbedEndpointNetwork.cpp
+ * @brief   mbed Connector Interface network low level functions and support (Ethernet, WiFi, Mesh (6LowPAN,Thread))
+ * @author  Doug Anson
+ * @version 1.0
+ * @see     
+ *
+ * Copyright (c) 2014
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Connector Endpoint
+#include "mbed-connector-interface/ConnectorEndpoint.h"
+
+// OptionsBuilder
+#include "mbed-connector-interface/OptionsBuilder.h"
+
+// Forward declarations of public functions in mbedEndpointNetwork
+#include "mbed-connector-interface/mbedEndpointNetworkImpl.h"
+
+// Network Selection
+#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
+	#define NETWORK_TYPE			"WIFI"
+	#include "ESP8266Interface.h"
+	ESP8266Interface network(MBED_CONF_APP_WIFI_TX,MBED_CONF_APP_WIFI_RX);
+#elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
+	#define NETWORK_TYPE			"Ethernet"
+	#include "EthernetInterface.h"
+	EthernetInterface network;
+#elif MBED_CONF_APP_NETWORK_INTERFACE == LOWPAN_ND
+	#define NETWORK_TYPE			"6LowPAN"
+	#define MESH
+	#include "NanostackInterface.h"
+	LoWPANNDInterface network;
+#elif MBED_CONF_APP_NETWORK_INTERFACE == THREAD
+	#define NETWORK_TYPE			"ThreadMesh"
+	#define MESH
+	#include "NanostackInterface.h"
+	ThreadInterface network;
+#endif
+
+// Logger instance
+extern Logger logger;
+
+// endpoint instance 
+static void *_endpoint_instance = NULL;
+
+// LWIP network instance forward reference
+extern void *__network_interface;
+
+// main loop cycle period
+static int _main_loop_iteration_wait_ms = MAIN_LOOP_WAIT_TIME_MS;
+
+// endpoint shutdown indicator
+static volatile bool _shutdown_endpoint = false;			
+
+extern "C" {
+
+/*********************** START LOCAL FUNCTIONS **************************/
+
+// start shutting downt the endpoint
+void start_endpoint_shutdown(void) {
+	if (_shutdown_endpoint == true) {
+		Connector::Endpoint *ep = (Connector::Endpoint *)_endpoint_instance;
+		if (ep != NULL && ep->isRegistered() == true) {
+			logger.log("mbedEndpointNetwork(%s): shutdown requested. De-registering the endpoint...",NETWORK_TYPE);
+			ep->de_register_endpoint();
+		}
+	}
+	
+	// ready to shutdown...
+	logger.log("mbedEndpointNetwork(%s): endpoint shutdown. Bye!",NETWORK_TYPE);
+}
+	
+// setup shutdown button
+#if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
+InterruptIn shutdown_button(MBED_CONF_APP_SHUTDOWN_PIN);
+void configure_deregistration_button(void) {
+	logger.log("mbedEndpointNetwork(%s): configuring de-registration button...",NETWORK_TYPE); 
+	shutdown_button.fall(&net_shutdown_endpoint);
+}	
+#endif
+
+// setup shutdown button
+void setup_deregistration_button(void) {
+#if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
+	configure_deregistration_button();
+#endif
+}
+
+// configure main loop parameters
+void configure_main_loop_params(Connector::Endpoint *endpoint) {
+	// set the initial shutdown state
+	_shutdown_endpoint = false;
+}
+
+// perform an actvity in the main loop
+void peform_main_loop_activity(void) {
+	// empty for now...
+	;
+}
+
+// begin the main loop for processing endpoint events
+void begin_main_loop(void) 
+{
+	// DEBUG
+	logger.log("mbedEndpointNetwork(%s): endpoint main loop beginning...",NETWORK_TYPE);
+	
+	// enter our main loop (until the shutdown condition flags it...)
+	while(_shutdown_endpoint == false) {
+		Thread::wait(_main_loop_iteration_wait_ms);
+		peform_main_loop_activity();
+	}
+	
+	// main loop has exited... start the endpoint shutdown...
+	logger.log("mbedEndpointNetwork(%s): endpoint main loop exited. Starting endpoint shutdown...",NETWORK_TYPE);
+	start_endpoint_shutdown();
+}
+
+/************************ END LOCAL FUNCTIONS ***************************/
+
+/*********************** START PUBLIC FUNCTIONS *************************/
+
+// get the network type
+char *net_get_type() {
+	return NETWORK_TYPE;
+}
+
+// shutdown the endpoint
+void net_shutdown_endpoint() {
+    _shutdown_endpoint = true;
+}
+	
+// called after the endpoint is configured...
+void net_plumb_network(void *p) 
+{
+    int connected = 0;
+    Connector::Endpoint *ep = NULL;
+    Connector::Options *options = NULL;
+    
+    // save 
+    _endpoint_instance = p;
+    
+    // connected
+    if (p != NULL) {
+		ep = (Connector::Endpoint *)p;
+		options = ep->getOptions();
+	}
+    
+#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
+	// map security types
+    nsapi_security_t security_opt = NSAPI_SECURITY_NONE;
+    if  (options->getWiFiAuthType() == WIFI_WPA_PERSONAL) {
+    	security_opt = NSAPI_SECURITY_WPA;
+    }
+    if  (options->getWiFiAuthType() == WIFI_WPA2_PERSONAL) {
+    	security_opt = NSAPI_SECURITY_WPA2;
+    }
+    if  (options->getWiFiAuthType() == WIFI_WEP) {
+    	security_opt = NSAPI_SECURITY_WEP;
+    }
+    
+	// Network Init (WIFI)...
+    connected = network.connect(options->getWiFiSSID().c_str(),options->getWiFiAuthKey().c_str(),security_opt);
+#elif MBED_CONF_APP_NETWORK_INTERFACE == LOWPAN_ND || MBED_CONF_APP_NETWORK_INTERFACE == THREAD
+	// Set the IP Address type to IPV6
+	((Connector::OptionsBuilder *)options)->setIPAddressType(IP_ADDRESS_TYPE_IPV6);
+	
+	// Network Init (Mesh)
+	connected = network.connect();
+#else
+	// not used... just removes a compiler warning...
+    options->getConnectorURL();
+    
+    // Network Init (Ethernet)
+    connected = network.connect();
+#endif
+
+	// check the connection status..
+	if (connected == 0) {
+    	__network_interface = (void *)&network;
+    	if (ep != NULL) {
+    		ep->isConnected(true);
+    	}
+    	
+    	// Debug
+   		logger.log("mbedEndpointNetwork(%s): IP Address: %s",NETWORK_TYPE,network.get_ip_address());
+   	}
+   	else {
+   		__network_interface = NULL;
+    	if (ep != NULL) {
+    		ep->isConnected(false);
+    	}
+    	
+    	// Debug
+   		logger.log("mbedEndpointNetwork(%s): CONNECTION FAILED",NETWORK_TYPE);
+   	}
+}
+
+// finalize and run the endpoint main loop
+void net_finalize_and_run_endpoint_main_loop(void *p) 
+{
+	// cast
+	Connector::Endpoint *ep = (Connector::Endpoint *)p;
+	
+	// Initialize our main loop... 
+    configure_main_loop_params(ep);
+    
+    // setup the shutdown button (if enabled for a given platform...)
+    setup_deregistration_button();
+
+	// register the endpoint
+	logger.log("mbedEndpointNetwork(%s): registering endpoint...",NETWORK_TYPE); 
+	ep->register_endpoint(ep->getEndpointSecurity(),ep->getEndpointObjectList());
+	       
+    // Begin the endpoint's main loop
+    begin_main_loop();
+}
+
+/************************ END PUBLIC FUNCTIONS **************************/
+
+}
\ No newline at end of file