Avnet / Mbed 2 deprecated WNCInterface_tls_client_example

Dependencies:   WNCInterface mbed-rtos mbed

Revision:
16:3bac55ad2049
Parent:
13:1ae41c231014
--- a/main.cpp	Fri Oct 28 13:30:20 2016 +0100
+++ b/main.cpp	Wed Nov 02 19:20:20 2016 +0000
@@ -33,10 +33,10 @@
 #define DEBUG_LEVEL 0
 
 #include "mbed.h"
-#include "NetworkStack.h"
+//#include "NetworkStack.h"
 
-#include "EthernetInterface.h"
-#include "TCPSocket.h"
+#include "WNCInterface.h"
+#include "TCPSocketConnection.h"
 
 #include "mbedtls/platform.h"
 #include "mbedtls/ssl.h"
@@ -44,6 +44,8 @@
 #include "mbedtls/ctr_drbg.h"
 #include "mbedtls/error.h"
 
+#define CRLF    "\r\n"
+
 #if DEBUG_LEVEL > 0
 #include "mbedtls/debug.h"
 #endif
@@ -55,7 +57,7 @@
 const int RECV_BUFFER_SIZE = 600;
 
 const char HTTPS_PATH[] = "/media/uploads/mbed_official/hello.txt";
-const size_t HTTPS_PATH_LEN = sizeof(HTTPS_PATH) - 1;
+//const size_t HTTPS_PATH_LEN = sizeof(HTTPS_PATH) - 1;
 
 /* Test related data */
 const char *HTTPS_OK_STR = "200 OK";
@@ -106,7 +108,7 @@
      * @param[in] domain The domain name to fetch from
      * @param[in] port The port of the HTTPS server
      */
-    HelloHTTPS(const char * domain, const uint16_t port, NetworkInterface *net_iface) :
+    HelloHTTPS(const char * domain, const uint16_t port, WNCInterface *net_iface) :
             _domain(domain), _port(port)
     {
 
@@ -115,7 +117,7 @@
         _got200 = false;
         _bpos = 0;
         _request_sent = 0;
-        _tcpsocket = new TCPSocket(net_iface);
+        _tcpsocket = new TCPSocketConnection;
 
         mbedtls_entropy_init(&_entropy);
         mbedtls_ctr_drbg_init(&_ctr_drbg);
@@ -208,12 +210,8 @@
         /* Connect to the server */
         mbedtls_printf("Connecting with %s\r\n", _domain);
         ret = _tcpsocket->connect(_domain, _port);
-        if (ret != NSAPI_ERROR_OK) {
-            mbedtls_printf("Failed to connect\r\n");
-            onError(_tcpsocket, -1);
-            return;
-        }
-
+        _tcpsocket->set_blocking (false,1500);
+        
        /* Start the handshake, the rest will be done in onReceive() */
         mbedtls_printf("Starting the TLS handshake...\r\n");
         ret = mbedtls_ssl_handshake(&_ssl);
@@ -237,7 +235,7 @@
         }
 
         /* It also means the handshake is done, time to print info */
-        printf("TLS connection to %s established\r\n", HTTPS_SERVER_NAME);
+        mbedtls_printf("TLS connection to %s established\r\n", HTTPS_SERVER_NAME);
 
         const uint32_t buf_size = 1024;
         char *buf = new char[buf_size];
@@ -249,10 +247,10 @@
         if( flags != 0 )
         {
             mbedtls_x509_crt_verify_info(buf, buf_size, "\r  ! ", flags);
-            printf("Certificate verification failed:\r\n%s\r\r\n", buf);
+            mbedtls_printf("Certificate verification failed:\r\n%s\r\r\n", buf);
         }
         else
-            printf("Certificate verification passed\r\n\r\n");
+            mbedtls_printf("Certificate verification passed\r\n\r\n");
 
 
         /* Read data out of the socket */
@@ -284,28 +282,6 @@
         _tcpsocket->close();
         delete[] buf;
     }
-    /**
-     * Check if the test has completed.
-     * @return Returns true if done, false otherwise.
-     */
-    bool done() {
-        return _error || (_got200 && _gothello);
-    }
-    /**
-     * Check if there was an error
-     * @return Returns true if there was an error, false otherwise.
-     */
-    bool error() {
-        return _error;
-    }
-    /**
-     * Closes the TCP socket
-     */
-    void close() {
-        _tcpsocket->close();
-        while (!_disconnected)
-            __WFI();
-    }
 protected:
     /**
      * Helper for pretty-printing mbed TLS error codes
@@ -368,44 +344,26 @@
      * Receive callback for mbed TLS
      */
     static int ssl_recv(void *ctx, unsigned char *buf, size_t len) {
-        int recv = -1;
-        TCPSocket *socket = static_cast<TCPSocket *>(ctx);
-        recv = socket->recv(buf, len);
-
-        if(NSAPI_ERROR_WOULD_BLOCK == recv){
-            return MBEDTLS_ERR_SSL_WANT_READ;
-        }else if(recv < 0){
-            return -1;
-        }else{
-            return recv;
-        }
+        TCPSocketConnection *socket = static_cast<TCPSocketConnection *>(ctx);
+        return (int)socket->receive((char*)buf, len);
    }
 
     /**
      * Send callback for mbed TLS
      */
     static int ssl_send(void *ctx, const unsigned char *buf, size_t len) {
-       int size = -1;
-        TCPSocket *socket = static_cast<TCPSocket *>(ctx);
-        size = socket->send(buf, len);
-
-        if(NSAPI_ERROR_WOULD_BLOCK == size){
-            return len;
-        }else if(size < 0){
-            return -1;
-        }else{
-            return size;
-        }
+        TCPSocketConnection *socket = static_cast<TCPSocketConnection *>(ctx);
+        return socket->send((char*)buf, len);
     }
 
-    void onError(TCPSocket *s, int error) {
-        printf("MBED: Socket Error: %d\r\n", error);
+    void onError(TCPSocketConnection *s, int error) {
+        mbedtls_printf("MBED: Socket Error: %d\r\n", error);
         s->close();
         _error = true;
     }
 
 protected:
-    TCPSocket* _tcpsocket;
+    TCPSocketConnection* _tcpsocket;
 
     const char *_domain;            /**< The domain name of the HTTPS server */
     const uint16_t _port;           /**< The HTTPS server port */
@@ -424,25 +382,54 @@
     mbedtls_ssl_config _ssl_conf;
 };
 
+MODSERIAL pc(USBTX,USBRX,256,256);   
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int myprintf( const char* format, ... ) {
+    extern MODSERIAL pc;
+    string buff;
+
+    va_list valist;
+    buff.reserve(2048);
+    va_start(valist, format);
+    vsprintf(&buff[0],format,valist);
+    pc.puts(buff.c_str());
+    va_end(valist);
+    return 0;
+}    
+
+#ifdef __cplusplus
+}
+#endif
 /**
  * The main loop of the HTTPS Hello World test
  */
 int main() {
+    int ret;
     /* The default 9600 bps is too slow to print full TLS debug info and could
      * cause the other party to time out. */
 
     /* Inititalise with DHCP, connect, and start up the stack */
-    EthernetInterface eth_iface;
+    WNCInterface eth_iface;
+
+    pc.baud(115200);
+    pc.printf(CRLF "Starting HTTPS File Download Example for TLS use WNC Data Module" CRLF);
+    
+    ret = eth_iface.init(NULL, &pc);                     
+    pc.printf("WNC Module %s initialized (%02X)." CRLF, ret?"IS":"IS NOT", ret);
+    if( !ret ) {
+        pc.printf(" - - - - - - - ALL DONE - - - - - - - " CRLF);
+        while(1);
+    }
+        
     eth_iface.connect();
-    mbedtls_printf("Using Ethernet LWIP\r\n");
-    const char *ip_addr = eth_iface.get_ip_address();
-    if (ip_addr) {
-        mbedtls_printf("Client IP Address is %s\r\n", ip_addr);
-    } else {
-        mbedtls_printf("No Client IP Address\r\n");
-    }
+    pc.printf("IP Address: %s " CRLF CRLF, eth_iface.getIPAddress());    
 
     HelloHTTPS *hello = new HelloHTTPS(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT, &eth_iface);
     hello->startTest(HTTPS_PATH);
+    pc.printf("  >>> DONE  <<<\r\n");
     delete hello;
 }