]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Demo/FreeRTOS_IoT_Libraries/https/common/DemoTasks/SimpleHTTPSExamples.c
a543fae8307e21ad312fbd455f9010114baf0910
[freertos] / FreeRTOS-Labs / Demo / FreeRTOS_IoT_Libraries / https / common / DemoTasks / SimpleHTTPSExamples.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * This file contains the common functions for plain text, basic TLS, and mutual\r
30  * authentication HTTPS demos. Aside from the difference in security level during\r
31  * connect, the three demos perform the same interaction with a HTTPS host. The\r
32  * demo creates a singe application task that loops through a set of examples\r
33  * demonstrating how to the connect to the server, send a few types of requests to\r
34  * the server (GET, HEAD, PUT, POST), and disconnect from the server again. All\r
35  * of the responses and their associated HTTPS response status codes are printed\r
36  * to the console.\r
37  *\r
38  * The plain text HTTP demo does not authenticate the server nor the client. The\r
39  * basic TLS HTTPS demo builds on top of the plain text demo, adding broker\r
40  * authentication and encryption. The mutual authentication HTTPS demo builds on\r
41  * top of the basic TLS demo, enabling both server and client authentication.\r
42  *\r
43  * For more information regarding the HTTPS library and the demo, please refer to:\r
44  * https://www.freertos.org/https/index.html\r
45  */\r
46 \r
47 /* Standard includes. */\r
48 #include <string.h>\r
49 #include <stdio.h>\r
50 \r
51 /* Kernel includes. */\r
52 #include "FreeRTOS.h"\r
53 #include "task.h"\r
54 \r
55 /* FreeRTOS+TCP includes. */\r
56 #include "FreeRTOS_IP.h"\r
57 \r
58 /* IoT SDK includes. */\r
59 #include "iot_https_client.h"\r
60 #include "iot_taskpool_freertos.h"\r
61 #include "platform/iot_network_freertos.h"\r
62 \r
63 /* HTTPS Demo Select */\r
64 #include "demo_config.h"\r
65 \r
66 /* Select HTTPS profile based on the setting in demo_config.h */\r
67 #if ( democonfigPROFILE_USE_AWS_IOT == 1 )\r
68         #include "aws_iot_demo_profile.h"\r
69 #else\r
70         #include "https_demo_profile.h"\r
71 #endif\r
72 \r
73 /* Preprocessor check iot configuration */\r
74 #include "aws_iot_setup_check.h"\r
75 \r
76 /*\r
77  * Set the connection profile based on settings in demo_config.h. For more\r
78  * information on each variable, please refer to the respective *_profile.h\r
79  * file in FreeRTOS-Labs\Demo\FreeRTOS_IoT_Libraries\include.\r
80  *\r
81  * Note that if you are running the https_tls_mutual_auth demo please make sure\r
82  * to visit the following link for setup:\r
83  * https://www.freertos.org/https/preconfiguredexamplesMA.html\r
84  */\r
85 #if defined( AWS_IOT_DEMO_PROFILE_H )\r
86         #define httpsexampleHTTPS_SERVER_ADDRESS                awsiotdemoprofileAWS_ENDPOINT\r
87         #define httpsexampleHTTPS_SERVER_PORT                   awsiotdemoprofileAWS_HTTPS_PORT\r
88         #define httpsexampleHTTPS_SERVER_CERTIFICATE    awsiotdemoprofileAWS_CERTIFICATE_PEM\r
89         #define httpsexampleCLIENT_CERTIFICATE_PEM              awsiotdemoprofileCLIENT_CERTIFICATE_PEM\r
90         #define httpsexampleCLIENT_PRIVATE_KEY_PEM              awsiotdemoprofileCLIENT_PRIVATE_KEY_PEM\r
91 #elif defined( HTTPS_DEMO_PROFILE_H )\r
92         #define httpsexampleHTTPS_SERVER_ADDRESS                httpsdemoprofileSERVER_ADDRESS\r
93         #define httpsexampleHTTPS_SERVER_PORT                   httpsdemoprofileSERVER_PORT\r
94         #define httpsexampleHTTPS_SERVER_CERTIFICATE    httpsdemoprofileSERVER_CERTIFICATE_PEM\r
95 #endif\r
96 \r
97 /*\r
98  * Each preconfigured host supports different request paths. In this demo, we\r
99  * are using httpbin and AWS IoT.\r
100  *\r
101  * In the demos that uses httpbin.org see http://httpbin.org/#/HTTP_Methods for\r
102  * details on supported REST API.\r
103  **/\r
104 #if defined( AWS_IOT_DEMO_PROFILE_H )\r
105         #define httpsexampleHTTPS_POST_PATH        "/topics/" awsiotdemoprofileCLIENT_IDENTIFIER\r
106 #elif defined( HTTPS_DEMO_PROFILE_H )\r
107         #define httpsexampleHTTPS_GET_PATH         "/ip"\r
108         #define httpsexampleHTTPS_HEAD_PATH        "/ip"\r
109         #define httpsexampleHTTPS_PUT_PATH         "/put"\r
110         #define httpsexampleHTTPS_POST_PATH        "/post"\r
111 #endif\r
112 \r
113 /**\r
114  * @brief The length in bytes of the user buffer to store the HTTPS Client library\r
115  * connection context.\r
116  *\r
117  * The minimum size can be found in @ref connectionUserBufferMinimumSize.\r
118  */\r
119 #define httpsexampleCONNECTION_USER_BUFFER_LENGTH        ( 512 )\r
120 \r
121 /**\r
122  * @brief The length in bytes of the user buffer to store the HTTPS Client library\r
123  * request context.\r
124  *\r
125  * The minimum size can be found in @ref requestUserBufferMinimumSize.\r
126  */\r
127 #define httpsexampleREQUEST_USER_BUFFER_LENGTH           ( 512 )\r
128 \r
129 /**\r
130  * @brief The length in bytes of the user buffer to store the HTTPS Client library\r
131  * response context.\r
132  */\r
133 #define httpsexampleRESPONSE_USER_BUFFER_LENGTH          ( 512 )\r
134 \r
135 /**\r
136  * @brief Some text to send as the request body for a PUT and POST request in\r
137  * this examples.\r
138  */\r
139 #define httpsexampleREQUEST_BODY_TEXT                            "Hello, world!"\r
140 \r
141 /**\r
142  * @brief The length in bytes of the buffer used to receive the response body.\r
143  */\r
144 #define httpsexampleRESPONSE_BODY_BUFFER_LENGTH          ( 512 )\r
145 \r
146 /**\r
147  * @brief Timeout in ms for HTTPS operations in this example.\r
148  */\r
149 #define httpsexampleHTTPS_TIMEOUT_MS                             ( 5000 )\r
150 \r
151 /*-----------------------------------------------------------*/\r
152 \r
153 /**\r
154  * @brief The HTTPS connection handle used in this example.\r
155  */\r
156 static IotHttpsConnectionHandle_t xHTTPSConnection = IOT_HTTPS_CONNECTION_HANDLE_INITIALIZER;\r
157 \r
158 /**\r
159  * @brief A buffer used to store the HTTPS Client library's connection context.\r
160  */\r
161 static uint8_t ucHTTPSConnectionUserBuffer[ httpsexampleCONNECTION_USER_BUFFER_LENGTH ] = { 0 };\r
162 \r
163 /**\r
164  * @brief A buffer used to store the HTTPS Client library's request context and\r
165  * some headers.\r
166  */\r
167 static uint8_t ucHTTPSRequestUserBuffer[ httpsexampleREQUEST_USER_BUFFER_LENGTH ] = { 0 };\r
168 \r
169 /**\r
170  * @brief A buffer used to store the HTTPS Client library's response context and\r
171  * some headers.\r
172  */\r
173 static uint8_t ucHTTPSResponseUserBuffer[ httpsexampleRESPONSE_USER_BUFFER_LENGTH ] = { 0 };\r
174 \r
175 /**\r
176  * @brief The request body.\r
177  */\r
178 static char cHTTPSRequestBodyBuffer[] = httpsexampleREQUEST_BODY_TEXT;\r
179 \r
180 /**\r
181  * @brief A buffer used to receive the HTTPS Client library's response body.\r
182  */\r
183 static uint8_t ucHTTPSResponseBodyBuffer[ httpsexampleRESPONSE_BODY_BUFFER_LENGTH ] = { 0 };\r
184 \r
185 /**\r
186  * @brief The task used to demonstrate the HTTPS Client API.\r
187  *\r
188  * @param[in] pvParameters Parameters as passed at the time of task creation. Not\r
189  *            used in this example.\r
190  */\r
191 static void prvHTTPSDemoTask( void * pvParameters );\r
192 \r
193 /**\r
194  * @brief Connects to the HTTPS server as specified in httpsexampleHTTPS_SERVER_ADDRESS\r
195  * and httpsexampleHTTPS_SERVER_PORT.\r
196  */\r
197 static void prvHTTPSConnect( void );\r
198 \r
199 /**\r
200  * @brief Creates and sends an HTTPS request to the HTTPS server specified in\r
201  * httpsexampleHTTPS_SERVER_ADDRESS and httpsexampleHTTPS_SERVER_PORT.\r
202  *\r
203  * @param[in] xMethod The HTTPS method to use. Please #IotHttpsMethod_t for\r
204  *            possible parameters.\r
205  * @param[in] pcPath The path for the HTTPS request.\r
206  * @param[in] ulPathLength The length of the input pcPath.\r
207  */\r
208 static void prvHTTPSRequest( IotHttpsMethod_t xMethod,\r
209                                                          const char * pcPath,\r
210                                                          uint32_t ulPathLength );\r
211 \r
212 /**\r
213  * @brief Disconnects from the HTTPS server.\r
214  */\r
215 static void prvHTTPSDisconnect( void );\r
216 \r
217 /**\r
218 * @brief Initializes the IoT libraries used by this demo.\r
219 */\r
220 static void prvInitialiseLibraries( void );\r
221 \r
222 /*-----------------------------------------------------------*/\r
223 \r
224 /**\r
225  * @brief Parameters used to create the system task pool.\r
226  */\r
227 static const IotTaskPoolInfo_t xTaskPoolParameters =\r
228 {\r
229         /* Minimum number of threads in a task pool.\r
230          * Note the slimmed down version of the task\r
231          * pool used by this library does not auto-scale\r
232          * the number of tasks in the pool so in this\r
233          * case this sets the number of tasks in the\r
234          * pool. */\r
235         1,\r
236 \r
237         /* Maximum number of threads in a task pool.\r
238          * Note the slimmed down version of the task\r
239          * pool used by this library does not auto-scale\r
240          * the number of tasks in the pool so in this\r
241          * case this parameter is just ignored. */\r
242         1,\r
243 \r
244         /* Stack size for every task pool thread - in\r
245          * bytes, hence multiplying by the number of bytes\r
246          * in a word as configMINIMAL_STACK_SIZE is\r
247          * specified in words. */\r
248         configMINIMAL_STACK_SIZE * sizeof( portSTACK_TYPE ),\r
249         /* Priority for every task pool thread. */\r
250         tskIDLE_PRIORITY,\r
251 };\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 static const IotHttpsConnectionInfo_t xConnectionInfo =\r
255 {\r
256         /* No connection to the HTTPS server has been established yet and we want to\r
257          * establish a new connection. */\r
258         .pAddress = httpsexampleHTTPS_SERVER_ADDRESS,\r
259         .addressLen = sizeof( httpsexampleHTTPS_SERVER_ADDRESS ) - 1,\r
260         .port = httpsexampleHTTPS_SERVER_PORT,\r
261         .userBuffer.pBuffer = ucHTTPSConnectionUserBuffer,\r
262         .userBuffer.bufferLen = sizeof( ucHTTPSConnectionUserBuffer ),\r
263 \r
264         /* Use FreeRTOS+TCP network. */\r
265         .pNetworkInterface = IOT_NETWORK_INTERFACE_FREERTOS,\r
266 \r
267         /* The HTTPS Client library uses TLS by default as indicated by the "S"\r
268          * postfixed to "HTTP" in the name of the library and its types and\r
269          * functions. There are no configurations in the flags to enable TLS. */\r
270         .flags = 0,\r
271 \r
272         /* Optional TLS extensions. For this demo, they are disabled. */\r
273         .pAlpnProtocols = NULL,\r
274         .alpnProtocolsLen = 0,\r
275 \r
276         /* Provide the certificate for authenticating the server. */\r
277         #if ( democonfigENABLE_TLS == 1 )\r
278                 .pCaCert = httpsexampleHTTPS_SERVER_CERTIFICATE,\r
279                 .caCertLen = sizeof( httpsexampleHTTPS_SERVER_CERTIFICATE ),\r
280         #else\r
281                 .pCaCert = NULL,\r
282                 .caCertLen = 0,\r
283         #endif\r
284 \r
285         /* The HTTPS server at httpbin.org:443 does not require client certificates,\r
286          * but AWS IoT does.\r
287          * If the server were to require a client certificate, the following members\r
288          * need to be set. */\r
289         #if ( democonfigENABLE_MUTUAL_AUTH == 1 )\r
290                 .pClientCert = httpsexampleCLIENT_CERTIFICATE_PEM,\r
291                 .clientCertLen = sizeof( httpsexampleCLIENT_CERTIFICATE_PEM ),\r
292                 .pPrivateKey = httpsexampleCLIENT_PRIVATE_KEY_PEM,\r
293                 .privateKeyLen = sizeof( httpsexampleCLIENT_PRIVATE_KEY_PEM )\r
294         #else\r
295                 .pClientCert = NULL,\r
296                 .clientCertLen = 0,\r
297                 .pPrivateKey = NULL,\r
298                 .privateKeyLen = 0\r
299         #endif\r
300 };\r
301 /*-----------------------------------------------------------*/\r
302 \r
303 void vStartSimpleHTTPSDemo( void )\r
304 {\r
305         /* This example uses a single application task, which in turn is used to\r
306         * connect, send a few requests, and disconnect from the HTTPS server. */\r
307         xTaskCreate( prvHTTPSDemoTask,         /* Function that implements the task. */\r
308                                  "HTTPSDemo",              /* Text name for the task - only used for debugging. */\r
309                                  democonfigDEMO_STACKSIZE, /* Size of stack (in words, not bytes) to allocate for the task. */\r
310                                  NULL,                     /* Task parameter - not used in this case. */\r
311                                  tskIDLE_PRIORITY,         /* Task priority, must be between 0 and configMAX_PRIORITIES - 1. */\r
312                                  NULL );                   /* Used to pass out a handle to the created task - not used in this case. */\r
313 }\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 static void prvHTTPSDemoTask( void * pvParameters )\r
317 {\r
318         /* Remove compiler warnings about unused parameters. */\r
319         ( void ) pvParameters;\r
320 \r
321         /* One time initialization of the libraries used by this demo. */\r
322         prvInitialiseLibraries();\r
323 \r
324         for( ; ; )\r
325         {\r
326                 /****************************** Connect. ******************************/\r
327 \r
328                 /* Establish a connection to the HTTPS server. This example connects to\r
329                  * the HTTPS server as specified in httpsexampleHTTPS_SERVER_ADDRESS and\r
330                  * httpsexampleHTTPS_SERVER_PORT at the top of this file. Please change\r
331                  * it to the HTTPS server you want to connect to. */\r
332                 configPRINTF( ( "Attempt to connect to %s:%d\r\n", httpsexampleHTTPS_SERVER_ADDRESS, httpsexampleHTTPS_SERVER_PORT ) );\r
333                 prvHTTPSConnect();\r
334                 configPRINTF( ( "Connected to %s:%d\r\n", httpsexampleHTTPS_SERVER_ADDRESS, httpsexampleHTTPS_SERVER_PORT ) );\r
335 \r
336                 /*********************** Send HTTPS requests. ************************/\r
337 \r
338                 /* The client is now connected to the server. This example will send a\r
339                  * GET, HEAD, PUT, and POST request. For AWS IoT profile, the example will\r
340                  * only send a POST request.\r
341                  **/\r
342                 #if defined( httpsexampleHTTPS_GET_PATH )\r
343                         configPRINTF( ( "Sending HTTPS GET request...\r\n" ) );\r
344                         prvHTTPSRequest( IOT_HTTPS_METHOD_GET,\r
345                                                          httpsexampleHTTPS_GET_PATH,\r
346                                                          sizeof( httpsexampleHTTPS_GET_PATH ) - 1 );\r
347                 #endif\r
348                 #if defined( httpsexampleHTTPS_HEAD_PATH )\r
349                         configPRINTF( ( "Sending HTTPS HEAD request...\r\n" ) );\r
350                         prvHTTPSRequest( IOT_HTTPS_METHOD_HEAD,\r
351                                                          httpsexampleHTTPS_HEAD_PATH,\r
352                                                          sizeof( httpsexampleHTTPS_HEAD_PATH ) - 1 );\r
353                 #endif\r
354                 #if defined( httpsexampleHTTPS_PUT_PATH )\r
355                         configPRINTF( ( "Sending HTTPS PUT request...\r\n" ) );\r
356                         prvHTTPSRequest( IOT_HTTPS_METHOD_PUT,\r
357                                                          httpsexampleHTTPS_PUT_PATH,\r
358                                                          sizeof( httpsexampleHTTPS_PUT_PATH ) - 1 );\r
359                 #endif\r
360                 #if defined( httpsexampleHTTPS_POST_PATH )\r
361                         configPRINTF( ( "Sending HTTPS POST request...\r\n" ) );\r
362                         prvHTTPSRequest( IOT_HTTPS_METHOD_POST,\r
363                                                          httpsexampleHTTPS_POST_PATH,\r
364                                                          sizeof( httpsexampleHTTPS_POST_PATH ) - 1 );\r
365                 #endif\r
366 \r
367                 /**************************** Disconnect. *****************************/\r
368 \r
369                 prvHTTPSDisconnect();\r
370                 configPRINTF( ( "Disconnected from %s:%d\r\n\r\n", httpsexampleHTTPS_SERVER_ADDRESS, httpsexampleHTTPS_SERVER_PORT ) );\r
371 \r
372                 /* Wait between iterations to avoid server throttling. */\r
373 \r
374                 configPRINTF( ( "prvHTTPSDemoTask() completed an iteration successfully. "\r
375                                                 "Total free heap is %u\r\n",\r
376                                                 xPortGetFreeHeapSize() ) );\r
377                 configPRINTF( ( "Demo completed successfully.\r\n" ) );\r
378                 configPRINTF( ( "Short delay before starting the next iteration.... \r\n\r\n" ) );\r
379                 vTaskDelay( pdMS_TO_TICKS( httpsexampleHTTPS_TIMEOUT_MS ) );\r
380         }\r
381 }\r
382 \r
383 /*-----------------------------------------------------------*/\r
384 \r
385 static void prvHTTPSConnect( void )\r
386 {\r
387 IotHttpsReturnCode_t xHTTPSClientResult;\r
388 \r
389         /* Establish the connection to the HTTPS server - It is a blocking call and\r
390          * will return only when the connection is complete or a timeout occurs. */\r
391         xHTTPSClientResult = IotHttpsClient_Connect( &( xHTTPSConnection ),\r
392                                                                                                  &( xConnectionInfo ) );\r
393         configASSERT( xHTTPSClientResult == IOT_HTTPS_OK );\r
394 }\r
395 \r
396 /*-----------------------------------------------------------*/\r
397 \r
398 static void prvHTTPSRequest( IotHttpsMethod_t xMethod,\r
399                                                          const char * pcPath,\r
400                                                          uint32_t ulPathLength )\r
401 {\r
402 IotHttpsReturnCode_t xHTTPSClientResult;\r
403 IotHttpsRequestInfo_t xHTTPSRequestInfo = IOT_HTTPS_REQUEST_INFO_INITIALIZER;\r
404 IotHttpsResponseInfo_t xHTTPSResponseInfo = IOT_HTTPS_RESPONSE_INFO_INITIALIZER;\r
405 IotHttpsRequestHandle_t xHTTPSRequest = IOT_HTTPS_REQUEST_HANDLE_INITIALIZER;\r
406 IotHttpsResponseHandle_t xHTTPSResponse = IOT_HTTPS_RESPONSE_HANDLE_INITIALIZER;\r
407 IotHttpsSyncInfo_t xHTTPSSyncRequestInfo = IOT_HTTPS_SYNC_INFO_INITIALIZER;\r
408 IotHttpsSyncInfo_t xHTTPSSyncResponseInfo = IOT_HTTPS_SYNC_INFO_INITIALIZER;\r
409 uint16_t usResponseStatus = 0;\r
410 \r
411         configASSERT( pcPath != NULL );\r
412 \r
413         /************************** HTTPS request setup. ***************************/\r
414 \r
415         if( ( xMethod == IOT_HTTPS_METHOD_PUT ) || ( xMethod == IOT_HTTPS_METHOD_POST ) )\r
416         {\r
417                 xHTTPSSyncRequestInfo.pBody = ( uint8_t * ) cHTTPSRequestBodyBuffer;\r
418                 xHTTPSSyncRequestInfo.bodyLen = sizeof( cHTTPSRequestBodyBuffer );\r
419         }\r
420         else\r
421         {\r
422                 xHTTPSSyncRequestInfo.pBody = NULL;\r
423                 xHTTPSSyncRequestInfo.bodyLen = 0;\r
424         }\r
425 \r
426         xHTTPSRequestInfo.pHost = httpsexampleHTTPS_SERVER_ADDRESS;\r
427         xHTTPSRequestInfo.hostLen = sizeof( httpsexampleHTTPS_SERVER_ADDRESS ) - 1;\r
428         xHTTPSRequestInfo.pPath = pcPath;\r
429         xHTTPSRequestInfo.pathLen = ulPathLength;\r
430         xHTTPSRequestInfo.method = xMethod;\r
431         xHTTPSRequestInfo.isNonPersistent = false;\r
432         xHTTPSRequestInfo.userBuffer.pBuffer = ucHTTPSRequestUserBuffer;\r
433         xHTTPSRequestInfo.userBuffer.bufferLen = sizeof( ucHTTPSRequestUserBuffer ) - 1;\r
434         xHTTPSRequestInfo.isAsync = false;\r
435         xHTTPSRequestInfo.u.pSyncInfo = &xHTTPSSyncRequestInfo;\r
436 \r
437         xHTTPSClientResult = IotHttpsClient_InitializeRequest( &( xHTTPSRequest ),\r
438                                                                                                                    &( xHTTPSRequestInfo ) );\r
439         configASSERT( xHTTPSClientResult == IOT_HTTPS_OK );\r
440 \r
441         /************************** HTTPS response setup. **************************/\r
442 \r
443         memset( ucHTTPSResponseBodyBuffer, 0, sizeof( ucHTTPSResponseBodyBuffer ) );\r
444 \r
445         if( xMethod == IOT_HTTPS_METHOD_HEAD )\r
446         {\r
447                 xHTTPSSyncResponseInfo.pBody = NULL;\r
448                 xHTTPSSyncResponseInfo.bodyLen = 0;\r
449         }\r
450         else\r
451         {\r
452                 xHTTPSSyncResponseInfo.pBody = ucHTTPSResponseBodyBuffer;\r
453                 xHTTPSSyncResponseInfo.bodyLen = sizeof( ucHTTPSResponseBodyBuffer );\r
454         }\r
455 \r
456         xHTTPSResponseInfo.userBuffer.pBuffer = ucHTTPSResponseUserBuffer;\r
457         xHTTPSResponseInfo.userBuffer.bufferLen = sizeof( ucHTTPSResponseUserBuffer );\r
458         xHTTPSResponseInfo.pSyncInfo = &xHTTPSSyncResponseInfo;\r
459 \r
460         /*************************** Send HTTPS request. ***************************/\r
461 \r
462         /* This synchronous send function blocks until the full response is received\r
463          * from the network. */\r
464         xHTTPSClientResult = IotHttpsClient_SendSync( xHTTPSConnection,\r
465                                                                                                   xHTTPSRequest,\r
466                                                                                                   &( xHTTPSResponse ),\r
467                                                                                                   &( xHTTPSResponseInfo ),\r
468                                                                                                   httpsexampleHTTPS_TIMEOUT_MS );\r
469         configASSERT( xHTTPSClientResult == IOT_HTTPS_OK );\r
470 \r
471         /* The response status is only available if the httpsexampleRESPONSE_USER_BUFFER\r
472          * is large enough to fit not only the HTTPS Client response context, but\r
473          * also the Status-Line of the response. The Status-Line and the response\r
474          * headers are stored in the provided ucHTTPSResponseUserBuffer right after\r
475          * the HTTPS Client response context. */\r
476         xHTTPSClientResult = IotHttpsClient_ReadResponseStatus( xHTTPSResponse,\r
477                                                                                                                         &usResponseStatus );\r
478         configASSERT( xHTTPSClientResult == IOT_HTTPS_OK );\r
479 \r
480         configPRINTF( ( "Response status: %d\r\n", usResponseStatus ) );\r
481         configPRINTF( ( "Response body: \r\n%s\r\n", ucHTTPSResponseBodyBuffer ) );\r
482 \r
483         /* The response body may be too large for the print buffer. These extra\r
484          * carriage returns and newlines help with clobbering. */\r
485         configPRINTF( ( "\r\n\r\n" ) );\r
486 }\r
487 \r
488 /*-----------------------------------------------------------*/\r
489 \r
490 static void prvHTTPSDisconnect( void )\r
491 {\r
492 IotHttpsReturnCode_t xHTTPSClientResult;\r
493 \r
494         /* The application must always explicitly disconnect from the server with\r
495          * this API if the last request sent on the connection was a persistent\r
496          * request. */\r
497         xHTTPSClientResult = IotHttpsClient_Disconnect( xHTTPSConnection );\r
498         configASSERT( xHTTPSClientResult == IOT_HTTPS_OK );\r
499 }\r
500 \r
501 \r
502 static void prvInitialiseLibraries( void )\r
503 {\r
504 IotTaskPoolError_t xTaskPoolResult;\r
505 IotHttpsReturnCode_t xHTTPSClientResult;\r
506 IotNetworkError_t xNetworkResult;\r
507 \r
508         /* The HTTPS Client library needs a task pool, so create the system task pool. */\r
509         xTaskPoolResult = IotTaskPool_CreateSystemTaskPool( &( xTaskPoolParameters ) );\r
510         configASSERT( xTaskPoolResult == IOT_TASKPOOL_SUCCESS );\r
511 \r
512         /* Initialize the network stack abstraction for FreeRTOS. */\r
513         xNetworkResult = IotNetworkFreeRTOS_Init();\r
514         configASSERT( xNetworkResult == IOT_NETWORK_SUCCESS );\r
515 \r
516         /* HTTPS Client library must be initialized before it can be used. This is\r
517          * just one time initialization. */\r
518         xHTTPSClientResult = IotHttpsClient_Init();\r
519         configASSERT( xHTTPSClientResult == IOT_HTTPS_OK );\r
520 }\r