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.

Committer:
JMF
Date:
Thu Mar 09 01:00:49 2017 +0000
Revision:
13:e29f5b4c6186
Parent:
10:51937195d6d1
Fixed several syntax errors that were uncovered when mbed introduced a new compiler.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 10:51937195d6d1 1 /* =====================================================================
JMF 10:51937195d6d1 2 Copyright © 2016, Avnet (R)
JMF 10:51937195d6d1 3
JMF 10:51937195d6d1 4 Contributors:
JMF 10:51937195d6d1 5 * James M Flynn, www.em.avnet.com
JMF 10:51937195d6d1 6
JMF 10:51937195d6d1 7 Licensed under the Apache License, Version 2.0 (the "License");
JMF 10:51937195d6d1 8 you may not use this file except in compliance with the License.
JMF 10:51937195d6d1 9 You may obtain a copy of the License at
JMF 10:51937195d6d1 10
JMF 10:51937195d6d1 11 http://www.apache.org/licenses/LICENSE-2.0
JMF 10:51937195d6d1 12
JMF 10:51937195d6d1 13 Unless required by applicable law or agreed to in writing,
JMF 10:51937195d6d1 14 software distributed under the License is distributed on an
JMF 10:51937195d6d1 15 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
JMF 10:51937195d6d1 16 either express or implied. See the License for the specific
JMF 10:51937195d6d1 17 language governing permissions and limitations under the License.
JMF 10:51937195d6d1 18
JMF 10:51937195d6d1 19 @file main.cpp / WNCInterface_HTTPS_Example
JMF 10:51937195d6d1 20 @version 1.0
JMF 10:51937195d6d1 21 @date Dec 2016
JMF 10:51937195d6d1 22
JMF 10:51937195d6d1 23 ======================================================================== */
JMF 0:50979ffa39b6 24
JMF 0:50979ffa39b6 25 #include "mbed.h"
JMF 9:667c078c40c6 26
JMF 0:50979ffa39b6 27 #include "WNCInterface.h"
JMF 0:50979ffa39b6 28
JMF 0:50979ffa39b6 29 #include "Socket/Socket.h"
JMF 0:50979ffa39b6 30 #include "Socket/TCPSocketConnection.h"
JMF 0:50979ffa39b6 31 #include "Socket/UDPSocket.h"
JMF 0:50979ffa39b6 32
JMF 0:50979ffa39b6 33 #define DEBUG
JMF 0:50979ffa39b6 34 #define MBED_PLATFORM
JMF 10:51937195d6d1 35
JMF 0:50979ffa39b6 36 #include "HTTPClient.h"
JMF 0:50979ffa39b6 37
JMF 10:51937195d6d1 38 #define STREAM_CNT 15 //when we test streaming, this is how many times to stream the string
JMF 10:51937195d6d1 39 #define STR_SIZE 125*(STREAM_CNT+1) //use a fixed size string buffer based on the streaming data count
JMF 0:50979ffa39b6 40 #define CRLF "\n\r"
JMF 0:50979ffa39b6 41
JMF 10:51937195d6d1 42 //
JMF 10:51937195d6d1 43 // This example is setup to use MBED OS (5.2). It sets up a thread to call the different tests
JMF 10:51937195d6d1 44 // because mbed tls is stack intensive and there is no way to modify the default stack size for
JMF 10:51937195d6d1 45 // main. So when we create the test task, give it a large stack (x4 the default size). After
JMF 10:51937195d6d1 46 // this the main() task will just spin.
JMF 10:51937195d6d1 47 //
JMF 9:667c078c40c6 48
JMF 9:667c078c40c6 49 void https_test_thread(void);
JMF 9:667c078c40c6 50
JMF 0:50979ffa39b6 51 int main() {
JMF 9:667c078c40c6 52 Thread http_test(osPriorityNormal, DEFAULT_STACK_SIZE*4, NULL);
JMF 10:51937195d6d1 53
JMF 10:51937195d6d1 54 printf("Testing HTTPClient and HTTPSClient using the WNCInterface & Socket software" CRLF);
JMF 10:51937195d6d1 55
JMF 9:667c078c40c6 56 http_test.start(https_test_thread);
JMF 9:667c078c40c6 57 while (true) {
JMF 9:667c078c40c6 58 osDelay(500);
JMF 9:667c078c40c6 59 }
JMF 9:667c078c40c6 60 }
JMF 9:667c078c40c6 61
JMF 10:51937195d6d1 62 //
JMF 10:51937195d6d1 63 // The two test functions do the same set of tests, the first one uses standard HTTP methods while
JMF 10:51937195d6d1 64 // the second test uses HTTP methods in conjunction with a SSL/TLS connection to verify certificates.
JMF 10:51937195d6d1 65 //
JMF 10:51937195d6d1 66
JMF 10:51937195d6d1 67 void test_http(void); //function tests the standard HTTPClient class
JMF 10:51937195d6d1 68 void test_https(void); //function to test the HTTPSClient class
JMF 9:667c078c40c6 69
JMF 9:667c078c40c6 70 void https_test_thread(void) {
JMF 0:50979ffa39b6 71 int ret;
JMF 9:667c078c40c6 72
JMF 0:50979ffa39b6 73 WNCInterface wnc;
JMF 2:8fb99c9f5075 74
JMF 9:667c078c40c6 75 ret = wnc.init();
JMF 9:667c078c40c6 76 printf("WNC Module %s initialized (%02X)." CRLF, ret?"IS":"IS NOT", ret);
JMF 0:50979ffa39b6 77 if( !ret ) {
JMF 0:50979ffa39b6 78 printf(" - - - - - - - ALL DONE - - - - - - - " CRLF);
JMF 0:50979ffa39b6 79 while(1);
JMF 9:667c078c40c6 80 }
JMF 0:50979ffa39b6 81
JMF 0:50979ffa39b6 82 ret = wnc.connect();
JMF 0:50979ffa39b6 83 printf("IP Address: %s " CRLF CRLF, wnc.getIPAddress());
JMF 0:50979ffa39b6 84
JMF 9:667c078c40c6 85 test_http();
JMF 9:667c078c40c6 86 test_https();
JMF 0:50979ffa39b6 87
JMF 0:50979ffa39b6 88 wnc.disconnect();
JMF 0:50979ffa39b6 89 printf(" - - - - - - - ALL DONE - - - - - - - " CRLF);
JMF 0:50979ffa39b6 90 while(1) {}
JMF 0:50979ffa39b6 91 }
JMF 0:50979ffa39b6 92
JMF 9:667c078c40c6 93
JMF 9:667c078c40c6 94
JMF 10:51937195d6d1 95 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
JMF 10:51937195d6d1 96 // test the HTTP client class
JMF 10:51937195d6d1 97 //
JMF 9:667c078c40c6 98 void test_http(void) {
JMF 9:667c078c40c6 99 HTTPClient http;
JMF 9:667c078c40c6 100 char str[STR_SIZE];
JMF 9:667c078c40c6 101 int ret;
JMF 9:667c078c40c6 102
JMF 10:51937195d6d1 103 printf(">>>>>>>>>>>><<<<<<<<<<<<" CRLF);
JMF 10:51937195d6d1 104 printf(">>> TEST HTTPClient <<< "CRLF);
JMF 10:51937195d6d1 105 printf(">>>>>>>>>>>><<<<<<<<<<<<" CRLF CRLF);
JMF 10:51937195d6d1 106
JMF 9:667c078c40c6 107 //GET data
JMF 9:667c078c40c6 108 printf(" ** Fetch a page... **" CRLF);
JMF 9:667c078c40c6 109
JMF 9:667c078c40c6 110 ret = http.get("https://developer.mbed.org/media/uploads/mbed_official/hello.txt", str, STR_SIZE);
JMF 9:667c078c40c6 111 if (!ret) {
JMF 9:667c078c40c6 112 printf("Page fetched successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 113 printf("<----->" CRLF "Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 114 }
JMF 9:667c078c40c6 115 else
JMF 9:667c078c40c6 116 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 9:667c078c40c6 117
JMF 9:667c078c40c6 118 //POST data
JMF 9:667c078c40c6 119 HTTPMap map;
JMF 9:667c078c40c6 120 HTTPText inText(str, STR_SIZE);
JMF 9:667c078c40c6 121
JMF 9:667c078c40c6 122 map.put("Hello", "World");
JMF 9:667c078c40c6 123 map.put("test", "1234");
JMF 9:667c078c40c6 124
JMF 9:667c078c40c6 125 printf(CRLF CRLF " ** Post data... **" CRLF);
JMF 9:667c078c40c6 126 ret = http.post("http://httpbin.org/post", map, &inText);
JMF 9:667c078c40c6 127 if (!ret) {
JMF 9:667c078c40c6 128 printf("Executed POST successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 129 printf("<----->" CRLF );
JMF 9:667c078c40c6 130 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 131 }
JMF 9:667c078c40c6 132 else
JMF 9:667c078c40c6 133 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 9:667c078c40c6 134
JMF 9:667c078c40c6 135 //PUT data
JMF 9:667c078c40c6 136
JMF 9:667c078c40c6 137 strcpy(str, "This is a PUT test!");
JMF 9:667c078c40c6 138 HTTPText outText(str);
JMF 9:667c078c40c6 139 printf(CRLF CRLF " ** Put data... **" CRLF);
JMF 9:667c078c40c6 140
JMF 9:667c078c40c6 141 ret = http.put("http://httpbin.org/put", outText, &inText);
JMF 9:667c078c40c6 142 if (!ret) {
JMF 9:667c078c40c6 143 printf("Executed PUT successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 144 printf("<----->" CRLF );
JMF 9:667c078c40c6 145 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 146 }
JMF 9:667c078c40c6 147 else
JMF 9:667c078c40c6 148 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 9:667c078c40c6 149
JMF 9:667c078c40c6 150 //DELETE data
JMF 9:667c078c40c6 151 printf(CRLF CRLF " ** Delete data... **" CRLF);
JMF 9:667c078c40c6 152 ret = http.del("http://httpbin.org/delete", &inText);
JMF 9:667c078c40c6 153 if (!ret) {
JMF 9:667c078c40c6 154 printf("Executed DELETE successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 155 printf("<----->" CRLF );
JMF 9:667c078c40c6 156 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 157 }
JMF 9:667c078c40c6 158 else
JMF 9:667c078c40c6 159 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 9:667c078c40c6 160
JMF 9:667c078c40c6 161 printf(CRLF CRLF " ** HTTP:stream data " INTSTR(STREAM_CNT) " times... **" CRLF);
JMF 9:667c078c40c6 162 if( (ret=http.get("http://httpbin.org:80/stream/" INTSTR(STREAM_CNT), str,sizeof(str))) == HTTP_OK) {
JMF 9:667c078c40c6 163 printf(CRLF "STREAM successfull - returned %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 164 printf("<----->" CRLF "Result:" CRLF "%s" CRLF "<----->" CRLF CRLF, str);
JMF 9:667c078c40c6 165 }
JMF 9:667c078c40c6 166 else
JMF 9:667c078c40c6 167 printf(CRLF "STREAM FAILED!, returned %d" CRLF, ret);
JMF 9:667c078c40c6 168
JMF 9:667c078c40c6 169
JMF 9:667c078c40c6 170 printf(CRLF CRLF ">>>>HTTP:Status..." CRLF);
JMF 9:667c078c40c6 171 ret = http.get("http://httpbin.org/get?show_env=1", str, STR_SIZE);
JMF 9:667c078c40c6 172 if (!ret) {
JMF 9:667c078c40c6 173 printf("Executed STATUS successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 174 printf("<----->" CRLF );
JMF 9:667c078c40c6 175 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 176 }
JMF 9:667c078c40c6 177 else {
JMF 9:667c078c40c6 178 printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -http.getHTTPResponseCode());
JMF 9:667c078c40c6 179 }
JMF 10:51937195d6d1 180
JMF 9:667c078c40c6 181 }
JMF 9:667c078c40c6 182
JMF 9:667c078c40c6 183
JMF 10:51937195d6d1 184 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
JMF 10:51937195d6d1 185 // test the HTTPS client class
JMF 9:667c078c40c6 186 //
JMF 10:51937195d6d1 187 void test_https(void) {
JMF 10:51937195d6d1 188 HTTPSClient *https = new HTTPSClient; //each time you call a Host, it sets the CA Certificate
JMF 10:51937195d6d1 189 //for that host. Because we call two different hosts it
JMF 10:51937195d6d1 190 //is easier to create the class on the heap rather than
JMF 10:51937195d6d1 191 //statically...
JMF 9:667c078c40c6 192 char str[STR_SIZE];
JMF 9:667c078c40c6 193 int ret;
JMF 9:667c078c40c6 194
JMF 9:667c078c40c6 195 https->setHost("developer.mbed.org");
JMF 10:51937195d6d1 196 https->addRootCACertificate(SSL_CA_PEM); //this is the CA certificate for mbed.org
JMF 9:667c078c40c6 197
JMF 10:51937195d6d1 198 printf(">>>>>>>>>>>><<<<<<<<<<<<" CRLF);
JMF 10:51937195d6d1 199 printf(">>> TEST HTTPSClient <<<" CRLF);
JMF 10:51937195d6d1 200 printf(">>>>>>>>>>>><<<<<<<<<<<<" CRLF CRLF);
JMF 9:667c078c40c6 201
JMF 9:667c078c40c6 202 //GET data
JMF 9:667c078c40c6 203
JMF 9:667c078c40c6 204 memset(str,0x00,STR_SIZE);
JMF 9:667c078c40c6 205 printf(CRLF " ** HTTPS:Fetch a page... **" CRLF);
JMF 9:667c078c40c6 206
JMF 9:667c078c40c6 207 ret = https->get("https://developer.mbed.org/media/uploads/mbed_official/hello.txt", str, STR_SIZE);
JMF 9:667c078c40c6 208 if (!ret){
JMF 9:667c078c40c6 209 printf("Page fetched successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 210 printf("<----->" CRLF "Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 211 }
JMF 9:667c078c40c6 212 else
JMF 9:667c078c40c6 213 printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
JMF 9:667c078c40c6 214
JMF 10:51937195d6d1 215 delete https; //go ahead and delete the mbed.org hppts client
JMF 10:51937195d6d1 216 https = new HTTPSClient; //and create one for httbin.org
JMF 9:667c078c40c6 217 https->setHost("httpbin.org");
JMF 10:51937195d6d1 218 https->addRootCACertificate(SSL_CA_HTTPBIN); //set the CA certificate for httpbin.org
JMF 9:667c078c40c6 219
JMF 9:667c078c40c6 220 HTTPMap map;
JMF 9:667c078c40c6 221 HTTPText inText(str, STR_SIZE);
JMF 9:667c078c40c6 222
JMF 9:667c078c40c6 223 //POST data
JMF 9:667c078c40c6 224 map.put("https Testing", "https doing a Post");
JMF 9:667c078c40c6 225 printf(CRLF CRLF ">>>>HTTPS:Post data..." CRLF);
JMF 9:667c078c40c6 226 ret = https->post("https://httpbin.org:443/post", map, &inText);
JMF 9:667c078c40c6 227 if (!ret) {
JMF 9:667c078c40c6 228 printf("Executed POST successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 229 printf("<----->" CRLF );
JMF 9:667c078c40c6 230 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 231 }
JMF 9:667c078c40c6 232 else
JMF 9:667c078c40c6 233 printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
JMF 9:667c078c40c6 234
JMF 9:667c078c40c6 235 //PUT data
JMF 9:667c078c40c6 236 memset(str,0x00,STR_SIZE);
JMF 9:667c078c40c6 237 strcpy(str, "This is an HTTPS PUT test!");
JMF 9:667c078c40c6 238 HTTPText outText(str);
JMF 9:667c078c40c6 239 printf(CRLF CRLF ">>>>HTTPS:Put data..." CRLF);
JMF 9:667c078c40c6 240 ret = https->put("https://httpbin.org:443/put", outText, &inText);
JMF 9:667c078c40c6 241 if (!ret) {
JMF 9:667c078c40c6 242 printf("Executed PUT successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 243 printf("<----->" CRLF );
JMF 9:667c078c40c6 244 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 245 }
JMF 9:667c078c40c6 246 else
JMF 9:667c078c40c6 247 printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
JMF 9:667c078c40c6 248
JMF 9:667c078c40c6 249 printf(CRLF CRLF ">>>>HTTPS:stream data..." CRLF);
JMF 10:51937195d6d1 250 if( (ret=https->get("https://httpbin.org:443/stream/" INTSTR(STREAM_CNT), str, STR_SIZE)) == HTTP_OK) {
JMF 9:667c078c40c6 251 printf(CRLF "STREAM successfull - returned %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 252 printf("<----->" CRLF "Result:" CRLF "%s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 253 }
JMF 9:667c078c40c6 254 else
JMF 9:667c078c40c6 255 printf(CRLF "STREAM FAILED!, returned %d - HTTP return code = -x%04X" CRLF, ret, -https->getHTTPResponseCode());
JMF 10:51937195d6d1 256
JMF 9:667c078c40c6 257 //DELETE data
JMF 9:667c078c40c6 258 printf(CRLF CRLF ">>>>HTTPS:Delete data..." CRLF);
JMF 9:667c078c40c6 259 ret = https->del("https://httpbin.org/delete", &inText);
JMF 9:667c078c40c6 260 if (!ret) {
JMF 9:667c078c40c6 261 printf("Executed DELETE successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 262 printf("<----->" CRLF );
JMF 9:667c078c40c6 263 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 264 }
JMF 9:667c078c40c6 265 else {
JMF 9:667c078c40c6 266 printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
JMF 9:667c078c40c6 267 }
JMF 9:667c078c40c6 268
JMF 9:667c078c40c6 269 printf(CRLF CRLF ">>>>HTTPS:Status..." CRLF);
JMF 9:667c078c40c6 270 ret = https->get("https://httpbin.org:443/get?show_env=1", str, STR_SIZE);
JMF 9:667c078c40c6 271 if (!ret) {
JMF 9:667c078c40c6 272 printf("Executed STATUS successfully - read %d characters" CRLF, strlen(str));
JMF 9:667c078c40c6 273 printf("<----->" CRLF );
JMF 9:667c078c40c6 274 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 9:667c078c40c6 275 }
JMF 9:667c078c40c6 276 else {
JMF 9:667c078c40c6 277 printf("Error - ret = %d - HTTP return code = -0x%04X" CRLF, ret, -https->getHTTPResponseCode());
JMF 9:667c078c40c6 278 }
JMF 10:51937195d6d1 279
JMF 10:51937195d6d1 280 delete https; //all done, delete the httpbin.org https client
JMF 9:667c078c40c6 281 }
JMF 10:51937195d6d1 282