use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Revision:
54:dfee8691c83a
Parent:
46:62da4ce20276
Child:
59:dd395412bd19
diff -r d2f9f94b6000 -r dfee8691c83a source/ConnectorEndpoint.cpp
--- a/source/ConnectorEndpoint.cpp	Mon Aug 08 19:02:00 2016 +0000
+++ b/source/ConnectorEndpoint.cpp	Tue Aug 09 17:18:49 2016 +0000
@@ -32,62 +32,54 @@
 // Device Manager support
 #include "mbed-connector-interface/DeviceManager.h"
 
-// DEBUG
-#ifndef NDEBUG
-#define DEBUG_OUT(...) { printf(__VA_ARGS__); }
-#else
-#define DEBUG_OUT(...) /* nothing */
-#endif
-
 // our endpoint instance
 static Connector::Endpoint *__endpoint = NULL;
 
-// LWIP_INTERFACE
-#if defined(MCI_LWIP_INTERFACE)
+// LWIP Network interface instance
 void *__network_interface = NULL;
-#endif
 
 // Connector namespace
 namespace Connector {
 
 // STATIC: Plumb the network
 void Endpoint::plumbNetwork(void *device_manager,bool canActAsRouterNode) {
+	// create our endpoint instance...
 	if (__endpoint == NULL) {
 		// initialize our endpoint instance
-		DEBUG_OUT("Endpoint::plumbNetwork: initializing endpoint instance...\r\n");
+		printf("Connector::Endpoint::plumbNetwork: initializing endpoint instance...\r\n");
 		__endpoint = (Connector::Endpoint *)utils_init_endpoint(canActAsRouterNode);
 	}
 	
 	// set the device manager
 	if (device_manager != NULL) {
 		// device manager has been supplied
-		DEBUG_OUT("Endpoint::plumbNetwork: setting a device manager...\r\n");
+		printf("Connector::Endpoint::plumbNetwork: setting a device manager...\r\n");
 		__endpoint->setDeviceManager(device_manager);
 	}
 	else {
 		// no device manager supplied
-		DEBUG_OUT("Endpoint::plumbNetwork: no device manager supplied (OK)\r\n");
+		printf("Connector::Endpoint::plumbNetwork: no device manager supplied (OK)\r\n");
 	}
 
-    // configure the endpoint now...
-    DEBUG_OUT("Endpoint::plumbNetwork: configuring endpoint...\r\n");
+    // configure the endpoint...
+    printf("Connector::Endpoint::plumbNetwork: configuring endpoint...\r\n");
     utils_configure_endpoint((void *)__endpoint);
     
-    // plumb network
-	DEBUG_OUT("Endpoint::plumbNetwork: plumbing network...\r\n");
+    // plumb the endpoint's network...
+	printf("Connector::Endpoint::plumbNetwork: plumbing network...\r\n");
 	net_plumb_network((void *)__endpoint);
 }
 
 // STATIC: Finalize the endpoint's configuration and begin the endpoint's main even loop (static, not tied into Logger)
 void Endpoint::start()
 {
-    // complete setup of our endpoint...
-    DEBUG_OUT("Endpoint::start: building out endpoint...\r\n");
+    // build out the endpoint with its configuration...
+    printf("Connector::Endpoint::start: building out endpoint...\r\n");
     utils_build_endpoint((void *)__endpoint);
     
-    // register the endpoint
-    DEBUG_OUT("Endpoint::start: completing endpoint build out..\r\n");
-	net_perform_endpoint_registration(__endpoint);
+    // finalize the endpoint and start its main loop
+    printf("Endpoint::start: finalize and run the endpoint main loop..\r\n");
+	net_finalize_and_run_endpoint_main_loop((void *)__endpoint);
 }
 
 // STATIC: Set the ConnectionStatusInterface Implementation instance
@@ -193,10 +185,10 @@
 	if (this->m_options->getIPAddressType() == IP_ADDRESS_TYPE_IPV6) ip_address_type = M2MInterface::LwIP_IPv6;
 	
 	// DEBUG
-	if (network_protocol == M2MInterface::TCP) this->logger()->log("Endpoint: Underlying Protocol: TCP");
-	if (network_protocol == M2MInterface::UDP) this->logger()->log("Endpoint: Underlying Protocol: UDP");
-	if (ip_address_type == M2MInterface::LwIP_IPv4) this->logger()->log("Endpoint: IP Address Type: IPv4");
-	if (ip_address_type == M2MInterface::LwIP_IPv6) this->logger()->log("Endpoint: IP Address Type: IPv6");
+	if (network_protocol == M2MInterface::TCP) this->logger()->log("Connector::Endpoint: Underlying Protocol: TCP");
+	if (network_protocol == M2MInterface::UDP) this->logger()->log("Connector::Endpoint: Underlying Protocol: UDP");
+	if (ip_address_type == M2MInterface::LwIP_IPv4) this->logger()->log("Connector::Endpoint: IP Address Type: IPv4");
+	if (ip_address_type == M2MInterface::LwIP_IPv6) this->logger()->log("Connector::Endpoint: IP Address Type: IPv6");
 	
 	// Create the  M2M Interface instance
     this->m_endpoint_interface = M2MInterfaceFactory::create_interface(*this,
@@ -208,12 +200,10 @@
                                               network_protocol,									// CoAP over UDP or TCP...
                                               ip_address_type);									// IPv4 addressing or IPv6 addressing
                                               
-#if defined (MCI_LWIP_INTERFACE)
 	if (__network_interface != NULL && this->m_endpoint_interface != NULL) {
-		this->logger()->log("Endpoint: Morpheus: binding network...");
+		this->logger()->log("Connector::Endpoint: binding LWIP network instance...");
 		this->m_endpoint_interface->set_platform_network_handler(__network_interface);
 	}
-#endif
 }
 
 // mbed-client: createEndpointInstance()
@@ -236,34 +226,34 @@
 void Endpoint::error(M2MInterface::Error error) {
         switch(error){
             case M2MInterface::AlreadyExists:
-                DEBUG_OUT("[ERROR:] M2MInterface::AlreadyExists");
+                this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::AlreadyExists");
                 break;
             case M2MInterface::BootstrapFailed:
-               DEBUG_OUT("[ERROR:] M2MInterface::BootstrapFailed");
+                this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::BootstrapFailed");
                 break;
             case M2MInterface::InvalidParameters:
-               DEBUG_OUT("[ERROR:] M2MInterface::InvalidParameters");
+                this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::InvalidParameters");
                 break;
             case M2MInterface::NotRegistered:
-                DEBUG_OUT("[ERROR:] M2MInterface::NotRegistered");
+                this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::NotRegistered");
                 break;
             case M2MInterface::Timeout:
-                DEBUG_OUT("[ERROR:] M2MInterface::Timeout");
+                this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::Timeout");
                 break;
             case M2MInterface::NetworkError:
-                DEBUG_OUT("[ERROR:] M2MInterface::NetworkError");
+                this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::NetworkError");
                 break;
             case M2MInterface::ResponseParseFailed:
-                DEBUG_OUT("[ERROR:] M2MInterface::ResponseParseFailed");
+                this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::ResponseParseFailed");
                 break;
             case M2MInterface::UnknownError:
-               DEBUG_OUT("[ERROR:] M2MInterface::UnknownError");
+                this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::UnknownError");
                 break;
             case M2MInterface::MemoryFail:
-                DEBUG_OUT("[ERROR:] M2MInterface::MemoryFail");
+                this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::MemoryFail");
                 break;
             case M2MInterface::NotAllowed:
-               DEBUG_OUT("[ERROR:] M2MInterface::NotAllowed");
+                this->logger()->log("Connector::Endpoint(ERROR): M2MInterface::NotAllowed");
                 break;
             default:
                 break;
@@ -274,7 +264,7 @@
 void Endpoint::register_endpoint(M2MSecurity *endpoint_security, M2MObjectList endpoint_objects) {
     if (this->m_endpoint_interface != NULL && endpoint_security != NULL && endpoint_objects.size() > 0)  {
     	// register  endpoint
-    	this->logger()->log("Registering endpoint...");
+    	this->logger()->log("Connector::Endpoint: registering endpoint...");
         this->m_endpoint_interface->register_object(endpoint_security, endpoint_objects);
     }
 }
@@ -290,7 +280,7 @@
 void Endpoint::de_register_endpoint(void) {
 	if (this->m_endpoint_interface != NULL) {
 		// de-register endpoint
-		this->logger()->log("De-registering endpoint...");
+		this->logger()->log("Connector::Endpoint: de-registering endpoint...");
 		this->m_endpoint_interface->unregister_object(NULL);
 		if (this->m_csi != NULL) {
 			this->m_csi->begin_object_unregistering((void *)this);
@@ -300,16 +290,9 @@
 
 // object registered
 void Endpoint::object_registered(M2MSecurity *security, const M2MServer &server)  {	    	           
-	this->logger()->log("Endpoint registered...");
+	this->logger()->log("Connector::Endpoint: endpoint registered.");
 	this->m_connected = true;
 	this->m_registered = true;
-	
-#if defined (ENDPOINT_EARLY_CONFIG_DEREG_BUTTON)
-	// setup the shutdown button (if enabled for a given platform...)
-    this->logger()->log("Configuring optional de-registration button...");
-    net_setup_deregistration_button((void *)this);
-#endif
-        
 	if (this->m_csi != NULL) {
 		this->m_csi->object_registered((void *)this,(void *)security,(void *)&server);
 	}
@@ -317,7 +300,7 @@
 
 // registration updated
 void Endpoint::registration_updated(M2MSecurity *security, const M2MServer &server)  {
-	this->logger()->log("Endpoint re-registered...");
+	this->logger()->log("Connector::Endpoint: endpoint re-registered.");
 	this->m_connected = true;
 	this->m_registered = true;
 	if (this->m_csi != NULL) {
@@ -327,17 +310,28 @@
 
 // object unregistered
 void Endpoint::object_unregistered(M2MSecurity *server)  {
-	// ready to exit
-	this->logger()->log("Endpoint de-registered... Ready to exit...");
+	// DEBUG
+	this->logger()->log("Connector::Endpoint: endpoint de-registered.");
+	
+	// no longer connected/registered
 	this->m_registered = false;
+	this->m_connected = false;
+	
+	// stop all observers...
+	this->stopObservations();
+	
+	// invoke ConnectionHandler if we have one...
 	if (this->m_csi != NULL) {
 		this->m_csi->object_unregistered((void *)this,(void *)server);
 	}
+	
+	// halt the main event loop... we are done. 
+	net_shutdown_endpoint();
 }
 
 // bootstrap done
 void Endpoint::bootstrap_done(M2MSecurity *server) {
-	this->logger()->log("Endpoint bootstrapped...");
+	this->logger()->log("Connector::Endpoint: endpoint bootstrapped.");
 	if (this->m_csi != NULL) {
 		this->m_csi->bootstrapped((void *)this,(void *)server);
 	}
@@ -392,7 +386,7 @@
     // We now have to bind our device resources
     if (this->m_device_manager != NULL) {
     	// DEBUG
-	    this->logger()->log("Endpoint::build(): plumbing the device management objects and resources...");
+	    this->logger()->log("Connector::Endpoint::build(): plumbing the device management objects and resources...");
 	    	
     	// bind the device manager
 	    ((DeviceManager *)this->m_device_manager)->bind();
@@ -400,47 +394,47 @@
 	    // push back the Device Resources Object 
 	    if (this->m_options->getDeviceResourcesObject() != NULL) {
 	    	// DEBUG
-	    	this->logger()->log("Endpoint::build(): plumbing device resources object...");
+	    	this->logger()->log("Connector::Endpoint::build(): plumbing device resources object...");
 	    	
 	    	// push back the device resources object
 	    	this->m_endpoint_object_list.push_back((M2MObject *)this->m_options->getDeviceResourcesObject());
 	    }
 	    else {
 	    	// unable to plumb device manager
-	    	this->logger()->log("Endpoint::build(): Unable to plumb device resources. Not installing device resource object...");
+	    	this->logger()->log("Connector::Endpoint::build(): Unable to plumb device resources. Not installing device resource object...");
 	    }
 	    
 	    // push back the Firmware Resources Object 
 	    if (this->m_options->getFirmwareResourcesObject() != NULL) {
 	    	// DEBUG
-	    	this->logger()->log("Endpoint::build(): plumbing firmware resources object...");
+	    	this->logger()->log("Connector::Endpoint::build(): plumbing firmware resources object...");
 	    	
 	    	// push back the firmware resources object
 	    	this->m_endpoint_object_list.push_back((M2MObject *)this->m_options->getFirmwareResourcesObject());
 	    }
 	    else {
 	    	// unable to plumb firmware manager
-	    	this->logger()->log("Endpoint::build(): Unable to plumb firmware resources. Not installing firmware resource object...");
+	    	this->logger()->log("Connector::Endpoint::build(): Unable to plumb firmware resources. Not installing firmware resource object...");
 	    }
     }
     else {
     	// no device manager installed
-	    this->logger()->log("Endpoint::build(): No device manager installed.");
+	    this->logger()->log("Connector::Endpoint::build(): No device manager installed.");
     }
         
     // Loop through Static Resources and bind each of them...
-    this->logger()->log("Endpoint::build(): adding static resources...");
+    this->logger()->log("Connector::Endpoint::build(): adding static resources...");
     const StaticResourcesList *static_resources = this->m_options->getStaticResourceList();
     for(int i=0; i<(int)static_resources->size(); ++i) {
-        this->logger()->log("Endpoint::build(): binding static resource: [%s]...",static_resources->at(i)->getFullName().c_str());
+        this->logger()->log("Connector::Endpoint::build(): binding static resource: [%s]...",static_resources->at(i)->getFullName().c_str());
         static_resources->at(i)->bind(this);
     }
 
     // Loop through Dynamic Resources and bind each of them...
-    this->logger()->log("Endpoint::build(): adding dynamic resources...");
+    this->logger()->log("Connector::Endpoint::build(): adding dynamic resources...");
     const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList();
     for(int i=0; i<(int)dynamic_resources->size(); ++i) {
-        this->logger()->log("Endpoint::build(): binding dynamic resource: [%s]...",dynamic_resources->at(i)->getFullName().c_str());
+        this->logger()->log("Connector::Endpoint::build(): binding dynamic resource: [%s]...",dynamic_resources->at(i)->getFullName().c_str());
         dynamic_resources->at(i)->bind(this);
     }
 
@@ -460,6 +454,20 @@
     }
 }
 
+// stop underlying observation mechanisms
+void Endpoint::stopObservations() {
+	const DynamicResourcesList *dynamic_resources = this->m_options->getDynamicResourceList();
+    for(int i=0; i<(int)dynamic_resources->size(); ++i) {
+        if (dynamic_resources->at(i)->isObservable() == true) {
+        	ResourceObserver *observer = (ResourceObserver *)dynamic_resources->at(i)->getObserver();
+        	if (observer != NULL) {
+        		this->logger()->log("Connector::Endpoint::stopObservations(): stopping resource observer for: [%s]...",dynamic_resources->at(i)->getFullName().c_str());
+        		observer->halt();
+        	}
+        }
+    }
+}
+
 // underlying network is connected (SET)
 void Endpoint::isConnected(bool connected) {
 	this->m_connected = connected;