V.06 11/3

Dependencies:   FT6206 SDFileSystem SPI_TFT_ILI9341 TFT_fonts

Fork of ATT_AWS_IoT_demo by attiot

Revision:
15:6f2798e45099
Child:
16:02008a2a2569
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AWS_openssl/aws_iot_src/protocol/mqtt/aws_iot_embedded_client_wrapper/platform_mbed_os/openssl/network.cpp	Thu Dec 01 18:05:38 2016 +0000
@@ -0,0 +1,201 @@
+/**
+ * @file timer.c
+ * @brief mbed-os implementation of the network interface needed for AWS.
+ */
+#include "network_interface.h"
+#include "EthernetInterface.h"
+
+#include "aws_iot_config.h"
+#include "aws_iot_log.h"
+
+#include "mbedtls/net_sockets.h"
+
+//=====================================================================================================================
+//
+// Uses Avnet Sheild (AT&T wireless LTE)
+//
+//=====================================================================================================================
+#ifdef USING_AVNET_SHIELD
+#include "WNCInterface.h"
+#include "WNCTCPSocketConnection.h"
+
+// Expose serial for WNC boot
+extern MODSERIAL pc;
+
+// Network socket
+WNCTCPSocketConnection* _tcpsocket;
+
+int net_modem_boot()
+{
+    INFO("Booting WNC modem...");
+    int rc = -1;
+    WNCInterface eth_iface;
+    
+    INFO("...Using Avnet AT&T wireless Shield");
+    rc = eth_iface.init(NULL, &pc);                   
+    INFO("WNC Module %s initialized (%02X).", rc?"IS":"IS NOT", rc);
+    if( !rc ) {
+        ERROR("DHCP failed.");
+        return rc;
+    }
+    
+    eth_iface.connect();
+    INFO("...IP Address: %s ", eth_iface.getIPAddress());
+    return rc;
+}
+
+void mbedtls_net_init( mbedtls_net_context *ctx )
+{
+    DEBUG("...mbedtls_net_init()");
+                 
+    _tcpsocket = new WNCTCPSocketConnection;
+    
+    return;
+}
+
+int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto )
+{
+    DEBUG("...mbedtls_net_connect");
+    int ret = -1;
+    
+    /* Connect to the server */
+    INFO("Connecting with %s\r\n", host);
+    ret = _tcpsocket->connect(host, AWS_IOT_MQTT_PORT); //TODO
+     
+    return ret;
+}
+
+int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len, uint32_t timeout )
+{
+    DEBUG("...mbedtls_net_recv_timeout len: %d, timeout: %d", len, timeout);   
+    return (int)_tcpsocket->receive((char*)buf, len);
+}
+
+int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
+{
+    DEBUG("...mbedtls_net_send");
+    return _tcpsocket->send((char*)buf, len);
+}
+
+int mbedtls_net_set_block( mbedtls_net_context *ctx )
+{
+    _tcpsocket->set_blocking (false,1500);
+    return 0;
+}
+
+void mbedtls_net_free( mbedtls_net_context *ctx )
+{
+    DEBUG("...TODO: mbedtls_net_free");
+    return; 
+}
+
+
+//=====================================================================================================================
+//
+// Uses FRDM-K64F wired ethernet
+//
+//=====================================================================================================================
+#else
+// Network Socket
+TCPSocket* _tcpsocket;
+
+int net_modem_boot()
+{
+    // Do nothing   
+}
+
+void mbedtls_net_init( mbedtls_net_context *ctx )
+{
+    DEBUG("...mbedtls_net_init()");  
+    EthernetInterface eth_iface;
+    
+    eth_iface.connect();
+    INFO("...Using FRDM-K64F Ethernet LWIP");
+    const char *ip_addr = eth_iface.get_ip_address();
+    if (ip_addr) {
+        INFO("...Client IP Address is %s", ip_addr);
+    } else {
+        INFO("...No Client IP Address");
+    }  
+    _tcpsocket = new TCPSocket(&eth_iface);
+    
+    //ctx->fd = -1;
+    return;
+}
+
+int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto )
+{
+    DEBUG("...mbedtls_net_connect");
+    int ret;
+    uint16_t _port = AWS_IOT_MQTT_PORT; // TODO
+    
+    INFO("...Connecting with %s", host);
+    ret = _tcpsocket->connect(host, _port);
+    _tcpsocket->bind(host, _port);
+    if (ret != NSAPI_ERROR_OK) {
+        ERROR("Failed to connect");
+        return ret;
+    }
+    
+    INFO("...Connected to Amazon!");   
+    return ret;
+}
+
+int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len, uint32_t timeout )
+{
+    DEBUG("...mbedtls_net_recv_timeout len: %d, timeout: %d", len, timeout);
+    
+    int recv = -1;
+    //TCPSocket *socket = static_cast<TCPSocket *>(ctx);
+    //socket->set_blocking(false);   
+    //recv = socket->recv(buf, len);
+    //_tcpsocket->set_blocking(true);
+    _tcpsocket->set_timeout(timeout);
+    recv = _tcpsocket->recv(buf, len);
+        
+    if(NSAPI_ERROR_WOULD_BLOCK == recv ||
+       recv == 0){     
+        DEBUG("...NSAPI_ERROR_WOULD_BLOCK");
+        return 0;
+    }else if(recv < 0){
+        ERROR("...RECV FAIL");
+        return -1;
+    }else{
+        DEBUG("...RECV OK: %d, %d", len, recv);
+        return recv;
+    }
+}
+
+int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
+{
+    DEBUG("...mbedtls_net_send");
+    
+    int size = -1;
+    //TCPSocket *socket = static_cast<TCPSocket *>(ctx);
+    //size = socket->send(buf, len);
+    size = _tcpsocket->send(buf, len);
+
+    if(NSAPI_ERROR_WOULD_BLOCK == size){
+        DEBUG("...SEND OK, len = %d", len);
+        return len;
+    }else if(size < 0){
+        ERROR("...SEND FAIL");
+        return -1;
+    }else{
+        DEBUG("...SEND OK, size = %d", size);
+        return size;
+    }
+}
+
+int mbedtls_net_set_block( mbedtls_net_context *ctx )
+{
+    _tcpsocket->set_blocking(false);
+    return 0;
+}
+
+void mbedtls_net_free( mbedtls_net_context *ctx )
+{
+    DEBUG("...TODO: mbedtls_net_free");
+    return; 
+}
+#endif
\ No newline at end of file