This Socket Example Program Implements a TCP Socket, UDP Socket, and HTTPClient socket then uses each of these to perform basic I/O.

Dependencies:   JSON M2XStreamClient-JMF WNCInterface mbed-rtos mbed

See the README for details on this example program. NOTE: When started, the program can take up to 40 seconds before it will respond. This delay is the time required for the WNC Data Module to connect with the network.

Revision:
10:51937195d6d1
Parent:
9:667c078c40c6
--- a/main.cpp	Fri Dec 02 16:53:32 2016 +0000
+++ b/main.cpp	Sun Dec 04 21:37:34 2016 +0000
@@ -1,3 +1,26 @@
+/* =====================================================================
+   Copyright © 2016, Avnet (R)
+
+   Contributors:
+     * James M Flynn, www.em.avnet.com 
+ 
+   Licensed under the Apache License, Version 2.0 (the "License"); 
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, 
+   software distributed under the License is distributed on an 
+   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
+   either express or implied. See the License for the specific 
+   language governing permissions and limitations under the License.
+
+    @file          main.cpp / WNCInterface_HTTPS_Example
+    @version       1.0
+    @date          Dec 2016
+
+======================================================================== */
 
 #include "mbed.h"
  
@@ -9,38 +32,47 @@
 
 #define DEBUG
 #define MBED_PLATFORM
+
 #include "HTTPClient.h"
 
-#define STR_SIZE    1024
-#define STREAM_CNT  5
-
+#define STREAM_CNT  15          //when we test streaming, this is how many times to stream the string
+#define STR_SIZE    125*(STREAM_CNT+1) //use a fixed size string buffer based on the streaming data count
 #define CRLF    "\n\r"
 
-MODSERIAL pc(USBTX,USBRX,256,256);
+//
+// This example is setup to use MBED OS (5.2).  It sets up a thread to call the different tests
+// because mbed tls is stack intensive and there is no way to modify the default stack size for
+// main.  So when we create the test task, give it a large stack (x4 the default size).  After 
+// this the main() task will just spin.
+//
 
 void https_test_thread(void);
 
 int main() {
     Thread http_test(osPriorityNormal, DEFAULT_STACK_SIZE*4, NULL);
+
+    printf("Testing HTTPClient and HTTPSClient using the WNCInterface & Socket software" CRLF);
+    
     http_test.start(https_test_thread);
     while (true) {
         osDelay(500);
     }
 }
 
-void test_http(void);
-void test_https(void);
+//
+// The two test functions do the same set of tests, the first one uses standard HTTP methods while
+// the second test uses HTTP methods in conjunction with a SSL/TLS connection to verify certificates.
+//
+
+void test_http(void);       //function tests the standard HTTPClient class
+void test_https(void);      //function to test the HTTPSClient class
         
 void https_test_thread(void) {
     int ret;
 
     WNCInterface wnc;
     
-    //pc.baud(9600);
-    printf("STARTING WNCInterface & Socket Test" CRLF);
-    
     ret = wnc.init();  
-                       
     printf("WNC Module %s initialized (%02X)." CRLF, ret?"IS":"IS NOT", ret);
     if( !ret ) {
         printf(" - - - - - - - ALL DONE - - - - - - - " CRLF);
@@ -60,16 +92,18 @@
 
 
 
+//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
+// test the HTTP client class
+//
 void test_http(void) {
-//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
-//demonstrate HTTPClient operations
-//
-    printf(CRLF CRLF">>>  Do the HTTPClient Tests  <<< "CRLF);
-    printf(         "-----------------------------------------" CRLF CRLF);
     HTTPClient  http;
     char str[STR_SIZE];
     int ret;
     
+    printf(">>>>>>>>>>>><<<<<<<<<<<<" CRLF);
+    printf(">>>  TEST HTTPClient <<< "CRLF);
+    printf(">>>>>>>>>>>><<<<<<<<<<<<" CRLF CRLF);
+
     //GET data
     printf(" ** Fetch a page... **" CRLF);
 
@@ -143,23 +177,27 @@
     else {
       printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -http.getHTTPResponseCode());
       }
- 
+
 }
 
 
-void test_https(void) {
+//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
+// test the HTTPS client class
 //
-// Now do HTTPS exchange
-//
-    HTTPSClient *https = new HTTPSClient;
+void test_https(void) {
+    HTTPSClient *https = new HTTPSClient;  //each time you call a Host, it sets the CA Certificate
+                                           //for that host.  Because we call two different hosts it
+                                           //is easier to create the class on the heap rather than
+                                           //statically...
     char str[STR_SIZE];
     int ret;
     
     https->setHost("developer.mbed.org");
-    https->addRootCACertificate(SSL_CA_PEM);
+    https->addRootCACertificate(SSL_CA_PEM);  //this is the CA certificate for mbed.org
 
-    printf(CRLF CRLF">>>  Do HTTPSClient Tests  <<< "CRLF);
-    printf(         "-------------------------------" CRLF CRLF);
+    printf(">>>>>>>>>>>><<<<<<<<<<<<" CRLF);
+    printf(">>> TEST HTTPSClient <<<" CRLF);
+    printf(">>>>>>>>>>>><<<<<<<<<<<<" CRLF CRLF);
  
     //GET data
 
@@ -174,11 +212,10 @@
     else 
       printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
 
-    delete https;
-
-    https = new HTTPSClient;
+    delete https;                                 //go ahead and delete the mbed.org hppts client 
+    https = new HTTPSClient;                      //and create one for httbin.org
     https->setHost("httpbin.org");
-    https->addRootCACertificate(SSL_CA_HTTPBIN);
+    https->addRootCACertificate(SSL_CA_HTTPBIN);  //set the CA certificate for httpbin.org
 
     HTTPMap map;
     HTTPText inText(str, STR_SIZE);
@@ -210,12 +247,13 @@
       printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
    
     printf(CRLF CRLF ">>>>HTTPS:stream data..." CRLF);
-    if( (ret=https->get("https://httpbin.org:443/stream/" INTSTR(STREAM_CNT), str,sizeof(str))) == HTTP_OK) {
+    if( (ret=https->get("https://httpbin.org:443/stream/" INTSTR(STREAM_CNT), str, STR_SIZE)) == HTTP_OK) {
         printf(CRLF "STREAM successfull - returned %d characters" CRLF, strlen(str));
         printf("<----->" CRLF "Result:" CRLF "%s" CRLF "<----->" CRLF, str);
         }
     else
         printf(CRLF "STREAM FAILED!, returned %d - HTTP return code = -x%04X" CRLF, ret, -https->getHTTPResponseCode());
+
     //DELETE data
     printf(CRLF CRLF ">>>>HTTPS:Delete data..." CRLF);
     ret = https->del("https://httpbin.org/delete", &inText);
@@ -227,7 +265,6 @@
     else {
       printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
       }
-    
    
     printf(CRLF CRLF ">>>>HTTPS:Status..." CRLF);
     ret = https->get("https://httpbin.org:443/get?show_env=1", str, STR_SIZE);
@@ -239,6 +276,7 @@
     else {
       printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
       }
-      
-    delete https;
+
+    delete https;  //all done, delete the httpbin.org https client
 }
+