use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Revision:
81:a2441163a06e
Parent:
77:cee832ba6dd0
Child:
82:e085176f6719
--- a/source/ConnectorEndpoint.cpp	Fri Nov 04 22:40:30 2016 +0000
+++ b/source/ConnectorEndpoint.cpp	Fri Nov 04 23:18:57 2016 +0000
@@ -104,6 +104,7 @@
     this->m_registered = false;
     this->m_csi = NULL;
     this->m_oim = NULL;
+    this->m_endpoint_security = NULL;
     this->m_endpoint_interface = NULL;
 }
 
@@ -113,9 +114,7 @@
     this->m_logger = ep.m_logger;
     this->m_options = ep.m_options;
     this->m_endpoint_interface = ep.m_endpoint_interface;
-#ifndef ENABLE_MBED_CLOUD_SUPPORT
     this->m_endpoint_security = ep.m_endpoint_security;
-#endif
     this->m_endpoint_object_list = ep.m_endpoint_object_list;
     this->m_device_manager = ep.m_device_manager;
     this->m_connected = ep.m_connected;
@@ -160,12 +159,17 @@
 	return this->m_options;
 }
 
-#ifndef ENABLE_MBED_CLOUD_SUPPORT
-// get our Server
-M2MSecurity *Endpoint::getEndpointSecurity() {
+// get our endpoint security instance
+M2MSecurity *Endpoint::getSecurityInstance() {
 	return this->m_endpoint_security;
 }
-#endif
+
+// set our endpoint security instance
+void Endpoint::setSecurityInstance(M2MSecurity *security) {
+	if (security != NULL) {
+		this->m_endpoint_security = security;
+	}
+}
 
 // get our ObjectList
 M2MObjectList Endpoint::getEndpointObjectList() {
@@ -203,12 +207,14 @@
 	// bind LWIP network interface pointer...                                          
 	if (__network_interface != NULL && this->m_endpoint_interface != NULL) {
 		this->logger()->log("Connector::Endpoint: binding LWIP network instance (Cloud)...");
+		this->m_endpoint_interface->on_registered(&Connector::Endpoint::on_registered);
+		this->m_endpoint_interface->on_unregistered(&Connector::Endpoint::on_unregistered);
+		this->m_endpoint_interface->on_error(&Connector::Endpoint::on_error);
+		this->m_endpoint_interface->set_update_callback(this);
 		this->m_endpoint_interface->setup(__network_interface);
 	}
 }
-#endif
-
-#ifndef ENABLE_MBED_CLOUD_SUPPORT
+#else
 // mbed-client: create our interface
 void Endpoint::createConnectorEndpointInterface() {
 	// get the CoAP listening port
@@ -265,9 +271,14 @@
 		this->m_endpoint_interface->set_platform_network_handler((void *)__network_interface);
 	}
 }
+#endif
 
-// mbed-client: createEndpointInstance()
-M2MSecurity *Endpoint::createEndpointInstance()  {
+// mbed-client: createEndpointSecurityInstance()
+M2MSecurity *Endpoint::createEndpointSecurityInstance()  {
+#ifdef ENABLE_MBED_CLOUD_SUPPORT
+		// internalized... not used.
+		return NULL;
+#else
         // Creates register server object with mbed device server address and other parameters
         M2MSecurity *server = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer);
         if (server != NULL)  {
@@ -280,9 +291,89 @@
             server->set_resource_value(M2MSecurity::Secretkey,this->m_options->getClientKey(),this->m_options->getClientKeySize());
         }
         return server;
+#endif
 }
-#endif
 
+#ifdef ENABLE_MBED_CLOUD_SUPPORT
+// mbed-cloud-client: Callback from mbed client stack if any error is encountered
+void Endpoint::error(int error_code) {
+	char *error = "No Error";
+	switch(error_code) {
+        case 0x01:
+            error = "MbedCloudClient::IdentityError";
+            break;
+        case 0x02:
+            error = "MbedCloudClient::IdentityInvalidParameter";
+            break;
+        case 0x03:
+            error = "MbedCloudClient::IdentityOutofMemory";
+            break;
+        case 0x04:
+            error = "MbedCloudClient::IdentityProvisioningError";
+            break;
+        case 0x05:
+            error = "MbedCloudClient::IdentityInvalidSessionID";
+            break;
+        case 0x06:
+            error = "MbedCloudClient::IdentityNetworkError";
+            break;
+        case 0x07:
+            error = "MbedCloudClient::IdentityInvalidMessageType";
+            break;
+        case 0x08:
+            error = "MbedCloudClient::IdentityInvalidMessageSize";
+            break;
+        case 0x09:
+            error = "MbedCloudClient::IdentityCertOrKeyNotFound";
+            break;
+        case 0x0A:
+            error = "MbedCloudClient::IdentityRetransmissionError";
+            break;
+        case 0x30:
+            error = "MbedCloudClient::ConnectErrorNone";
+            break;
+        case 0x31:
+            error = "MbedCloudClient::ConnectAlreadyExists";
+            break;
+        case 0x32:
+            error = "MbedCloudClient::ConnectBootstrapFailed";
+			break;
+        case 0x33:
+            error = "MbedCloudClient::ConnectInvalidParameters";
+            break;
+        case 0x34:
+            error = "MbedCloudClient::ConnectNotRegistered";
+            break;
+        case 0x35:
+            error = "MbedCloudClient::ConnectTimeout";
+            break;
+        case 0x36:
+            error = "MbedCloudClient::ConnectNetworkError";
+            break;
+        case 0x37:
+            error = "MbedCloudClient::ConnectResponseParseFailed";
+            break;
+        case 0x38:
+            error = "MbedCloudClient::ConnectUnknownError";
+            break;
+        case 0x39:
+            error = "MbedCloudClient::ConnectMemoryFail";
+            break;
+        case 0x3A:
+            error = "MbedCloudClient::ConnectNotAllowed";
+            break;
+        case 0x3B:
+            error = "MbedCloudClient::ConnectSecureConnectionFailed";
+            break;
+        case 0x3C:
+            error = "MbedCloudClient::ConnectDnsResolvingFailed";
+            break;
+        default:
+            error = "UNKNOWN";
+    }
+    this->logger()->log("Connector::Endpoint(Cloud) Error(%x): %s",error_code,error);
+}
+#else
 // mbed-client: Callback from mbed client stack if any error is encountered
 void Endpoint::error(M2MInterface::Error error) {
         switch(error){
@@ -320,10 +411,14 @@
                 break;
         }
 }
+#endif
+
 // re-register the endpoint
 void Endpoint::re_register_endpoint() {
 	if (this->m_endpoint_interface != NULL)  {
 #ifdef ENABLE_MBED_CLOUD_SUPPORT
+		// DEBUG
+		this->logger()->log("Connector::Endpoint(Cloud): re-register endpoint...");
 #else
 		this->m_endpoint_interface->update_registration(this->m_endpoint_security,this->m_options->getLifetime());
 #endif
@@ -334,6 +429,8 @@
 void Endpoint::de_register_endpoint(void) {
 	if (this->m_endpoint_interface != NULL) {
 #ifdef ENABLE_MBED_CLOUD_SUPPORT
+		// DEBUG
+		this->logger()->log("Connector::Endpoint(Cloud): de-registering endpoint...");
 		this->m_endpoint_interface->close();
 #else
 		// de-register endpoint
@@ -352,6 +449,7 @@
 void Endpoint::register_endpoint(M2MSecurity *endpoint_security, M2MObjectList endpoint_objects) {
 #ifdef ENABLE_MBED_CLOUD_SUPPORT
 	if (this->m_endpoint_interface != NULL) {
+		this->logger()->log("Connector::Endpoint(Cloud): registering endpoint...");
 		this->m_endpoint_interface->add_objects(endpoint_objects);
 	}
 #else
@@ -363,6 +461,50 @@
 #endif
 }
 
+#ifdef ENABLE_MBED_CLOUD_SUPPORT
+// object registered
+void Endpoint::object_registered()  {	    	           
+	this->logger()->log("Connector::Endpoint(Cloud): endpoint registered.");
+	this->m_connected = true;
+	this->m_registered = true;
+	if (this->m_csi != NULL) {
+		this->m_csi->object_registered((void *)this);
+	}
+}
+
+// registration updated
+void Endpoint::registration_updated()  {
+	this->logger()->log("Connector::Endpoint(Cloud): endpoint re-registered.");
+	this->m_connected = true;
+	this->m_registered = true;
+	if (this->m_csi != NULL) {
+		this->m_csi->registration_updated((void *)this);
+	}
+}
+
+// object unregistered
+void Endpoint::object_unregistered()  {
+	// DEBUG
+	this->logger()->log("Connector::Endpoint(Cloud): 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);
+	}
+	
+	// halt the main event loop... we are done. 
+	net_shutdown_endpoint();
+}
+
+#else
+
 // object registered
 void Endpoint::object_registered(M2MSecurity *security, const M2MServer &server)  {	    	           
 	this->logger()->log("Connector::Endpoint: endpoint registered.");
@@ -403,6 +545,7 @@
 	// halt the main event loop... we are done. 
 	net_shutdown_endpoint();
 }
+#endif
 
 // bootstrap done
 void Endpoint::bootstrap_done(M2MSecurity *server) {
@@ -454,11 +597,9 @@
 {	
     // initialize as an mbed-client
     this->createEndpointInterface();
-
-#ifndef ENABLE_MBED_CLOUD_SUPPORT    
+    
     // Create our server instance
-    this->m_endpoint_security = this->createEndpointInstance();
-#endif
+    this->setSecurityInstance(this->createEndpointSecurityInstance());
     
     // We now have to bind our device resources
     if (this->m_device_manager != NULL) {