use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Revision:
10:3f79b5e67c22
Parent:
8:f950fb1b78c0
Child:
11:3d4434825cd7
--- a/source/Utils.cpp	Wed Feb 24 14:50:03 2016 +0000
+++ b/source/Utils.cpp	Fri Mar 04 19:16:57 2016 +0000
@@ -25,6 +25,9 @@
 #include "mbed-connector-interface/OptionsBuilder.h"
 #include "mbed-connector-interface/mbedEndpointNetwork.h"
 
+// Maximum CoAP URL length
+#define MAX_CONN_URL_LENGTH		128
+
 // External references (defined in main.cpp)
 Connector::Options *configure_endpoint(Connector::OptionsBuilder &builder);
 extern Logger logger;
@@ -97,4 +100,43 @@
 		Connector::Endpoint *ep = (Connector::Endpoint *)p;
 	    ep->build_endpoint();
 	}
+}
+
+// parse out the CoAP port number from the connection URL
+uint16_t extract_port_from_url(char *url,uint16_t default_port) 
+{
+	uint16_t port = default_port;
+	
+	if (url != NULL && strlen(url) > 0) {
+		char buffer[MAX_CONN_URL_LENGTH+1];
+		char uri[MAX_CONN_URL_LENGTH+1];
+		char path[MAX_CONN_URL_LENGTH+1];
+		
+		// initialize the buffer
+		memset(buffer,0,MAX_CONN_URL_LENGTH+1);
+		memset(uri,0,MAX_CONN_URL_LENGTH+1);
+		memset(path,0,MAX_CONN_URL_LENGTH+1);
+		int length = strlen(url); 
+		
+		// truncate if needed
+		if (length >MAX_CONN_URL_LENGTH) length = MAX_CONN_URL_LENGTH;
+		
+		// make a copy...
+		memcpy(buffer,url,length);
+		
+		// remove the forward slashes and colons
+		for(int i=0;i<length;++i) {
+			if (buffer[i] == ':') buffer[i] = ' ';
+			if (buffer[i] == '/') buffer[i] = ' ';
+		}
+		
+		// parse
+		sscanf(buffer,"%s %s %d",uri,path,(int *)&port);
+	}
+	
+	// DEBUG
+	logger.log("Endpoint: CoAP port: %u",port);
+	
+	// return the port
+	return port; 
 }
\ No newline at end of file