NuMaker connection with AWS IoT thru MQTT/HTTPS

Dependencies:   MQTT

Files at this revision

API Documentation at this revision

Comitter:
ccli8
Date:
Tue Jan 09 15:46:49 2018 +0800
Parent:
2:319965cafbdd
Child:
4:dc23eeba885a
Commit message:
Fix HTTPS/Ethernet failed

Changed in this revision

TLSSocket.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/TLSSocket.h	Thu Jan 04 13:22:10 2018 +0800
+++ b/TLSSocket.h	Tue Jan 09 15:46:49 2018 +0800
@@ -155,19 +155,15 @@
 
        /* Start the handshake, the rest will be done in onReceive() */
         if (_debug) mbedtls_printf("Starting the TLS handshake...\r\n");
-        ret = mbedtls_ssl_handshake(&_ssl);
+        do {
+            ret = mbedtls_ssl_handshake(&_ssl);
+        } while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE);
         if (ret < 0) {
-            if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
-                ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
-                print_mbedtls_error("mbedtls_ssl_handshake", ret);
-                onError(_tcpsocket, -1);
-            }
-            else {
-                _error = ret;
-            }
-            return _error;
+            print_mbedtls_error("mbedtls_ssl_handshake", ret);
+            onError(_tcpsocket, ret);
+            return ret;
         }
-
+            
         /* It also means the handshake is done, time to print info */
         if (_debug) mbedtls_printf("TLS connection to %s:%d established\r\n", _hostname, _port);
 
@@ -382,7 +378,7 @@
         size = socket->send(buf, len);
 
         if(NSAPI_ERROR_WOULD_BLOCK == size) {
-            return len;
+            return MBEDTLS_ERR_SSL_WANT_WRITE;
         }
         else if (size < 0){
             return -1;
--- a/main.cpp	Thu Jan 04 13:22:10 2018 +0800
+++ b/main.cpp	Tue Jan 09 15:46:49 2018 +0800
@@ -274,7 +274,7 @@
             printf("Connecting with %s:%d\n", _domain, _port);
             tls_rc = _tlssocket->connect(_domain, _port);
             if (tls_rc != NSAPI_ERROR_OK) {
-                printf("Connects with %s:%d failed\n", _domain, _port);
+                printf("Connects with %s:%d failed: %d\n", _domain, _port, tls_rc);
                 break;
             }
             printf("Connects with %s:%d OK\n", _domain, _port);
@@ -512,7 +512,7 @@
             printf("Connecting with %s:%d\n", _domain, _port);
             tls_rc = _tlssocket->connect(_domain, _port);
             if (tls_rc != NSAPI_ERROR_OK) {
-                printf("Connects with %s:%d failed\n", _domain, _port);
+                printf("Connects with %s:%d failed: %d\n", _domain, _port, tls_rc);
                 break;
             }
             printf("Connects with %s:%d OK\n\n", _domain, _port);
@@ -735,5 +735,5 @@
     mbed_stats_heap_t heap_stats;
     mbed_stats_heap_get(&heap_stats);
     printf("\nCurrent heap size: %lu\n", heap_stats.current_size);
-    printf("Max heap size: %lu\n", heap_stats.max_size);
+    printf("Max heap size: %lu\n\n", heap_stats.max_size);
 }