Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Files at this revision

API Documentation at this revision

Comitter:
wiggly
Date:
Tue Aug 22 13:22:47 2017 +0100
Parent:
32:1a95b47c921a
Child:
34:651c23af382c
Commit message:
Initial modifications to get platform initialisation working with mbed OS 5

Changed in this revision

azure_c_shared_utility/platform.h Show annotated file Show diff for this revision Revisions of this file
azure_c_shared_utility/tcpsocketconnection_c.h Show annotated file Show diff for this revision Revisions of this file
platform_mbed.cpp Show annotated file Show diff for this revision Revisions of this file
tcpsocketconnection_c.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/azure_c_shared_utility/platform.h	Tue Aug 22 11:24:58 2017 +0000
+++ b/azure_c_shared_utility/platform.h	Tue Aug 22 13:22:47 2017 +0100
@@ -1,8 +1,6 @@
 // Copyright (c) Microsoft. All rights reserved.
 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
 
-// Fork for mbed5
-
 #ifndef PLATFORM_H
 #define PLATFORM_H
 
@@ -14,7 +12,9 @@
 #include "azure_c_shared_utility/xio.h"
 #include "azure_c_shared_utility/umock_c_prod.h"
 
-    MOCKABLE_FUNCTION(, int, platform_init);
+    typedef struct NetworkInterface NetworkInterface;
+
+    MOCKABLE_FUNCTION(, int, platform_init, NetworkInterface*, net);
     MOCKABLE_FUNCTION(, void, platform_deinit);
     MOCKABLE_FUNCTION(, const IO_INTERFACE_DESCRIPTION*, platform_get_default_tlsio);
     MOCKABLE_FUNCTION(, STRING_HANDLE, platform_get_platform_info);
--- a/azure_c_shared_utility/tcpsocketconnection_c.h	Tue Aug 22 11:24:58 2017 +0000
+++ b/azure_c_shared_utility/tcpsocketconnection_c.h	Tue Aug 22 13:22:47 2017 +0100
@@ -7,8 +7,7 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
-
-	typedef void* TCPSOCKETCONNECTION_HANDLE;
+    typedef void* TCPSOCKETCONNECTION_HANDLE;
 
 	TCPSOCKETCONNECTION_HANDLE tcpsocketconnection_create(void);
 	void tcpsocketconnection_set_blocking(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, bool blocking, unsigned int timeout);
--- a/platform_mbed.cpp	Tue Aug 22 11:24:58 2017 +0000
+++ b/platform_mbed.cpp	Tue Aug 22 13:22:47 2017 +0100
@@ -3,56 +3,34 @@
 
 #include "azure_c_shared_utility/platform.h"
 #include "EthernetInterface.h"
+#include "NetworkInterface.h"
 #include "NTPClient.h"
 #include "azure_c_shared_utility/optimize_size.h"
 #include "azure_c_shared_utility/xio.h"
 #include "azure_c_shared_utility/tlsio_wolfssl.h"
 
-int setupRealTime(void)
+int setupRealTime(NetworkInterface* net)
 {
-    int result;
+    int result = 0;
 
-    if (EthernetInterface::connect())
+    NTPClient ntp(*net);
+
+    if (ntp.setTime("1.pool.ntp.org") != 0)
     {
         result = __FAILURE__;
     }
-    else
-    {
-        NTPClient ntp;
-        if (ntp.setTime("0.pool.ntp.org") != 0)
-        {
-            result = __FAILURE__;
-        }
-        else
-        {
-            result = 0;
-        }
-        EthernetInterface::disconnect();
-    }
 
     return result;
 }
 
-int platform_init(void)
+int platform_init(NetworkInterface* net)
 {
-    int result;
+    int result = 0;
 
-    if (EthernetInterface::init())
+    if (setupRealTime(net) != 0)
     {
         result = __FAILURE__;
     }
-    else if (setupRealTime() != 0)
-    {
-        result = __FAILURE__;
-    } 
-    else if (EthernetInterface::connect())
-    {
-        result = __FAILURE__;
-    }
-    else
-    {
-        result = 0;
-    }
 
     return result;
 }
@@ -67,7 +45,4 @@
     return STRING_construct("(mbed)");
 }
 
-void platform_deinit(void)
-{
-    EthernetInterface::disconnect();
-}
+void platform_deinit(void) {}
--- a/tcpsocketconnection_c.cpp	Tue Aug 22 11:24:58 2017 +0000
+++ b/tcpsocketconnection_c.cpp	Tue Aug 22 13:22:47 2017 +0100
@@ -4,64 +4,107 @@
 #include "mbed.h"
 
 #include <stddef.h>
-#include "TCPSocketConnection.h"
+
 #include "azure_c_shared_utility/tcpsocketconnection_c.h"
 
+typedef struct {
+    TCPSocket* socket;
+    bool connected;
+} TCPSOCKETCONNECTION_HANDLE_T;
 
 TCPSOCKETCONNECTION_HANDLE tcpsocketconnection_create(void)
 {
-    return new TCPSocketConnection();
+    TCPSOCKETCONNECTION_HANDLE_T* conn = new TCPSOCKETCONNECTION_HANDLE_T;
+    conn->socket = new TCPSocket();
+    conn->connected = false;
+    return conn;
 }
 
 void tcpsocketconnection_set_blocking(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, bool blocking, unsigned int timeout)
 {
-	TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle;
-	tsc->set_blocking(blocking, timeout);
+    TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle;
+    tsc->socket->set_blocking(blocking);
+    tsc->socket->set_timeout(timeout);
 }
 
 void tcpsocketconnection_destroy(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle)
 {
-	delete (TCPSocketConnection*)tcpSocketConnectionHandle;
+    delete (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle;
 }
 
 int tcpsocketconnection_connect(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, const char* host, const int port)
 {
-	TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle;
-	return tsc->connect(host, port);
+    TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle;
+    // TODO: so, we set this but we really need to kill it if we get an error later too.....
+    auto rv = tsc->socket->connect(host, port);
+    if(rv == 0) { tsc->connected = true; }
+    return rv;
 }
 
 bool tcpsocketconnection_is_connected(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle)
 {
-	TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle;
-	return tsc->is_connected();
+    TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle;
+    return tsc->connected;
 }
 
 void tcpsocketconnection_close(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle)
 {
-	TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle;
-	tsc->close();
+    TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle;
+    tsc->socket->close();
 }
 
 int tcpsocketconnection_send(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, const char* data, int length)
 {
-	TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle;
-	return tsc->send((char*)data, length);
+    TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle;
+    return tsc->socket->send((char*)data, length);
 }
 
 int tcpsocketconnection_send_all(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, const char* data, int length)
 {
-	TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle;
-	return tsc->send_all((char*)data, length);
+    TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle;
+    // TODO: this is shonky and quickly done....
+
+    auto rv = tsc->socket->send((char*)data, length);
+
+    if(rv < 0 || rv == length) {
+        return rv;
+    }
+
+    auto remain = length - rv;
+
+    while(rv > 0 && remain > 0) {
+        data = data + rv;
+        rv = tsc->socket->send((char*)data, length);
+        remain = length - rv;
+    }
+
+    return rv;
 }
 
 int tcpsocketconnection_receive(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, char* data, int length)
 {
-	TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle;
-	return tsc->receive(data, length);
+    TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle;
+    return tsc->socket->recv(data, length);
 }
 
 int tcpsocketconnection_receive_all(TCPSOCKETCONNECTION_HANDLE tcpSocketConnectionHandle, char* data, int length)
 {
-	TCPSocketConnection* tsc = (TCPSocketConnection*)tcpSocketConnectionHandle;
-	return tsc->receive_all(data, length);
+    TCPSOCKETCONNECTION_HANDLE_T* tsc = (TCPSOCKETCONNECTION_HANDLE_T*)tcpSocketConnectionHandle;
+    // TODO: this is shonky and quickly done....
+
+    auto rv = tsc->socket->recv((char*)data, length);
+
+    if(rv < 0 || rv == length) {
+        return rv;
+    }
+
+    auto remain = length - rv;
+
+    while(rv > 0 && remain > 0) {
+        data = data + rv;
+        rv = tsc->socket->recv((char*)data, length);
+        remain = length - rv;
+    }
+
+    return rv;
 }