wifi test

Dependencies:   X_NUCLEO_IKS01A2 mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main-x.cpp Source File

main-x.cpp

00001 
00002 
00003 #include "select-demo.h"
00004 
00005 /* =====================================================================
00006    Copyright © 2016, Avnet (R)
00007    Contributors:
00008      * James M Flynn, www.em.avnet.com 
00009  
00010    Licensed under the Apache License, Version 2.0 (the "License"); 
00011    you may not use this file except in compliance with the License.
00012    You may obtain a copy of the License at
00013     http://www.apache.org/licenses/LICENSE-2.0
00014    Unless required by applicable law or agreed to in writing, 
00015    software distributed under the License is distributed on an 
00016    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
00017    either express or implied. See the License for the specific 
00018    language governing permissions and limitations under the License.
00019     @file          main.cpp / WNC14A2AInterface_HTTP_exampel
00020     @version       1.0
00021     @date          Dec 2016
00022 ======================================================================== */
00023 
00024 #if DEMO == DEMO_HTTPx
00025 
00026 #include "easy-connect.h"
00027 #include "http_request.h"
00028 #include "https_request.h"
00029 
00030 #define STREAM_CNT  10          //when we test streaming, this is how many times to stream the string
00031 #define STR_SIZE    150*(STREAM_CNT+1) //use a fixed size string buffer based on the streaming data count
00032 
00033 #define TOSTR(x) #x
00034 #define INTSTR(x) TOSTR(x)
00035 
00036 //
00037 // We do the same thing for both http and https, so create a macro to eaze the typing...
00038 //
00039 
00040 
00041 /* List of trusted root CA certificates
00042  * currently one: Let's Encrypt, the CA for httpbin.org
00043  *
00044  * To add more root certificates, just concatenate them.
00045  */
00046 const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
00047     "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n"
00048     "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n"
00049     "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n"
00050     "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n"
00051     "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n"
00052     "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n"
00053     "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n"
00054     "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n"
00055     "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n"
00056     "a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n"
00057     "/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n"
00058     "AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n"
00059     "CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n"
00060     "bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n"
00061     "c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n"
00062     "VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n"
00063     "ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n"
00064     "MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n"
00065     "Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n"
00066     "AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n"
00067     "uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n"
00068     "wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n"
00069     "X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n"
00070     "PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n"
00071     "KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n"
00072     "-----END CERTIFICATE-----\n";
00073 
00074 //
00075 // This example is setup to use MBED OS (5.2).  It sets up a thread to call the different tests
00076 // so the default stack size can be varied.  TLS is stack intensive and needs additional stack
00077 // to work effeciently. 
00078 //
00079 
00080 //
00081 // The two test functions do the same set of tests, the first one uses standard HTTP methods while
00082 // the second test uses HTTPS.
00083 //
00084 
00085 void https_test_thread(void);             //Thread that runs the two tests
00086 void test_http(NetworkInterface *net);    //function makes standard HTTP calls
00087 void test_https(NetworkInterface *net);   //function makes standard HTTPS calls
00088 
00089 Thread http_test(osPriorityNormal, 4*1024, NULL);
00090 
00091 int main() {
00092 
00093     printf("Test HTTP and HTTPS interface\n");
00094     http_test.start(https_test_thread);
00095     wait(5);
00096     http_test.join();
00097     printf(" - - - - - - - ALL DONE - - - - - - - \n");
00098 }
00099 
00100 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
00101 // this is the Thread that calls each of the two tests.
00102 //
00103 void https_test_thread(void) {
00104 
00105     NetworkInterface *network = easy_connect(true);
00106 
00107     printf(" software.\n");
00108     if (!network) {
00109         printf("Unable to connect to network!\n");
00110         return;
00111         }
00112     printf("My IP Address is: %s \n\n", network->get_ip_address());
00113 
00114     test_http(network);
00115     test_https(network);
00116 
00117     network->disconnect();
00118 }
00119 
00120 
00121 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
00122 // Utility function to print out responses 
00123 //
00124 void dump_response(HttpResponse* res) 
00125 {
00126     printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
00127 
00128     printf("Headers:\n");
00129     for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
00130         printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
00131     }
00132     printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
00133 }
00134 
00135 void stream_callback(const char *data, size_t len)
00136 {
00137     printf("Chunk Received:\n");
00138     for( size_t x=0; x<len; x++ )
00139         printf("%c",data[x]);
00140     printf("\n");
00141 }
00142 
00143 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
00144 // test the HTTP client class
00145 //
00146 void test_http(NetworkInterface *net) 
00147 {
00148     TCPSocket *socket = new TCPSocket();
00149 
00150     printf(">>>>>>>>>>>><<<<<<<<<<<<\n");
00151     printf(">>>  TEST HTTPClient <<<\n");
00152     printf(">>>>>>>>>>>><<<<<<<<<<<<\n\n");
00153 
00154     nsapi_error_t open_result = socket->open(net);
00155     if (open_result != 0) {
00156         printf("Opening TCPSocket failed... %d\n", open_result);
00157         return;
00158         }
00159 
00160     nsapi_error_t connect_result = socket->connect("developer.mbed.org", 80);
00161     if (connect_result != 0) {
00162         printf("Connecting over TCPSocket failed... %d\n", connect_result);
00163         return;
00164         }
00165     else
00166         printf("Connected over TCP to developer.mbed.org\n\n");
00167 
00168     printf(" >>>First, lets get a page from http://developer.mbed.org\n");
00169     {
00170         HttpRequest* get_req = new HttpRequest(socket,HTTP_GET,"https://os.mbed.com/media/uploads/mbed_official/hello.txt");
00171         HttpResponse* get_res = get_req->send();
00172         if (!get_res) {
00173             printf("HttpRequest failed (error code %d)\n", get_req->get_error());
00174             return;
00175             }
00176 
00177         printf("\n----- RESPONSE: -----\n");
00178         dump_response(get_res);
00179         delete get_req;
00180     }
00181     delete socket;
00182  
00183     socket = new TCPSocket();
00184 
00185     open_result = socket->open(net);
00186     if (open_result != 0) {
00187         printf("Opening TCPSocket failed... %d\n", open_result);
00188         return;
00189         }
00190 
00191     connect_result = socket->connect("httpbin.org", 80);
00192     if (connect_result != 0) {
00193         printf("Connecting over TCPSocket http://httpbin.org... %d\n", connect_result);
00194         return;
00195         }
00196     else
00197         printf("Connected over TCP to httpbin.org\n\n");
00198 
00199     printf("\n\n >>>Post data... **\n");
00200     {
00201         HttpRequest* post_req = new HttpRequest(socket, HTTP_POST, "http://httpbin.org/post");
00202         post_req->set_header("Content-Type", "application/json");
00203         const char body[] = "{\"hello\":\"world\"},"
00204                             "{\"test\":\"1234\"}";
00205 
00206         HttpResponse* post_res = post_req->send(body, strlen(body));
00207         if (!post_res) {
00208             printf("HttpRequest failed (error code %d)\n", post_req->get_error());
00209             return;
00210         }
00211 
00212         printf("\n----- RESPONSE: -----\n");
00213         dump_response(post_res);
00214         delete post_req;
00215     }
00216 
00217     printf("\n\n >>>Put data... \n");
00218     {
00219         HttpRequest* put_req = new HttpRequest(socket, HTTP_PUT, "http://httpbin.org/put");
00220         put_req->set_header("Content-Type", "application/json");
00221 
00222         const char body[] = "This is a PUT test!";
00223 
00224         HttpResponse* put_res = put_req->send(body, strlen(body));
00225         if (!put_res) {
00226             printf("HttpRequest failed (error code %d)\n", put_req->get_error());
00227             return;
00228         }
00229 
00230         printf("\n----- RESPONSE: -----\n");
00231         dump_response(put_res);
00232         delete put_req;
00233     }
00234 
00235     printf("\n\n >>>Delete data... \n");
00236     {
00237         HttpRequest* del_req = new HttpRequest(socket, HTTP_DELETE, "http://httpbin.org/delete");
00238         del_req->set_header("Content-Type", "application/json");
00239 
00240         HttpResponse* del_res = del_req->send();
00241         if (!del_res) {
00242             printf("HttpRequest failed (error code %d)\n", del_req->get_error());
00243             return;
00244         }
00245 
00246         printf("\n----- RESPONSE: -----\n");
00247         dump_response(del_res);
00248         delete del_req;
00249     }
00250 
00251     printf("\n\n >>>HTTP:stream, send http://httpbin.org/stream/" INTSTR(STREAM_CNT) "... \n");
00252     {
00253         HttpRequest* stream_req = new HttpRequest(socket, HTTP_GET, "http://httpbin.org/stream/" INTSTR(STREAM_CNT),
00254                                                   stream_callback );
00255         stream_req->send();
00256         delete stream_req;
00257     }
00258 
00259     printf("\n\n >>>HTTP:Status...\n");
00260     {
00261         HttpRequest* get_req = new HttpRequest(socket,HTTP_GET,"http://httpbin.org/get?show_env=1");
00262         HttpResponse* get_res = get_req->send();
00263         if (!get_res) {
00264             printf("HttpRequest failed (error code %d)\n", get_req->get_error());
00265             return;
00266             }
00267 
00268         printf("\n----- RESPONSE: -----\n");
00269         dump_response(get_res);
00270         delete get_req;
00271     }
00272     delete socket;
00273 }
00274 
00275 
00276 void dump_httpsresponse(HttpResponse* res) 
00277 {
00278     mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
00279 
00280     mbedtls_printf("Headers:\n");
00281     for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
00282         mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
00283     }
00284     mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
00285 }
00286 
00287 void stream_httpscallback(const char *data, size_t len)
00288 {
00289     printf("Chunk Received:\n");
00290     for( size_t x=0; x<len; x++ )
00291         printf("%c",data[x]);
00292     printf("\n");
00293 }
00294 
00295 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
00296 // test the HTTPS client class
00297 //
00298 void test_https(NetworkInterface *net) 
00299 {
00300 
00301     printf(">>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<\n");
00302     printf(">>>  TEST HTTPS - set up TLS connection  <<<\n");
00303     printf(">>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<\n\n");
00304 
00305     TLSSocket* socket = new TLSSocket(net, "httpbin.org", 443, SSL_CA_PEM);
00306     socket->set_debug(true);
00307     if (socket->connect() != 0) {
00308         printf("TLS Connect failed %d\n", socket->error());
00309         return;
00310     }
00311 
00312     printf("\n\n >>>Post data... **\n");
00313     {
00314         HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "http://httpbin.org/post");
00315         post_req->set_header("Content-Type", "application/json");
00316         const char body[] = "{\"hello\":\"world\"},"
00317                             "{\"test\":\"1234\"}";
00318 
00319         HttpResponse* post_res = post_req->send(body, strlen(body));
00320         if (!post_res) {
00321             printf("HttpsRequest failed (error code %d)\n", post_req->get_error());
00322             return;
00323         }
00324 
00325         printf("\n----- RESPONSE: -----\n");
00326         dump_httpsresponse(post_res);
00327         delete post_req;
00328     }
00329 
00330     printf("\n\n >>>Put data... \n");
00331     {
00332         HttpsRequest* put_req = new HttpsRequest(socket, HTTP_PUT, "http://httpbin.org/put");
00333         put_req->set_header("Content-Type", "application/json");
00334 
00335         const char body[] = "This is a PUT test!";
00336 
00337         HttpResponse* put_res = put_req->send(body, strlen(body));
00338         if (!put_res) {
00339             printf("HttpsRequest failed (error code %d)\n", put_req->get_error());
00340             return;
00341         }
00342 
00343         printf("\n----- RESPONSE: -----\n");
00344         dump_httpsresponse(put_res);
00345         delete put_req;
00346     }
00347 
00348     printf("\n\n >>>Delete data... \n");
00349     {
00350         HttpsRequest* del_req = new HttpsRequest(socket, HTTP_DELETE, "http://httpbin.org/delete");
00351         del_req->set_header("Content-Type", "application/json");
00352 
00353         HttpResponse* del_res = del_req->send();
00354         if (!del_res) {
00355             printf("HttpsRequest failed (error code %d)\n", del_req->get_error());
00356             return;
00357         }
00358 
00359         printf("\n----- RESPONSE: -----\n");
00360         dump_httpsresponse(del_res);
00361         delete del_req;
00362     }
00363 
00364     printf("\n\n >>>HTTPS:stream, send http://httpbin.org/stream/" INTSTR(STREAM_CNT)"... \n");
00365     {
00366         HttpsRequest* stream_req = new HttpsRequest(socket, HTTP_GET, "http://httpbin.org/stream/" INTSTR(STREAM_CNT), 
00367                                                   stream_httpscallback );
00368         stream_req->send();
00369         delete stream_req;
00370     }
00371 
00372     printf("\n\n >>>HTTPS:Status...\n");
00373     {
00374         HttpsRequest* get_req = new HttpsRequest(socket,HTTP_GET,"http://httpbin.org/get?show_env=1");
00375         HttpResponse* get_res = get_req->send();
00376         if (!get_res) {
00377             printf("HttpsRequest failed (error code %d)\n", get_req->get_error());
00378             return;
00379             }
00380 
00381         printf("\n----- RESPONSE: -----\n");
00382         dump_httpsresponse(get_res);
00383         delete get_req;
00384     }
00385     delete socket;
00386 }
00387 
00388 #endif // DEMO