Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of http-example by
Diff: source/main-https-socket-reuse.cpp
- Revision:
- 16:1374b4c35897
- Parent:
- 12:4c226ca06191
- Child:
- 17:97b1dd566b07
diff -r 65a49b573ba5 -r 1374b4c35897 source/main-https-socket-reuse.cpp --- a/source/main-https-socket-reuse.cpp Thu Jul 27 15:08:59 2017 +0200 +++ b/source/main-https-socket-reuse.cpp Mon Sep 04 16:32:38 2017 +0100 @@ -9,6 +9,7 @@ #include "mbed.h" #include "easy-connect.h" #include "https_request.h" +#include "mbed_stats.h" Serial pc(USBTX, USBRX); @@ -55,14 +56,20 @@ mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); } -int main() { - pc.baud(115200); +void run() { + mbed_stats_heap_t heap_stats; + + mbed_stats_heap_get(&heap_stats); + printf("[1] Heap: %lu / %lu\n", heap_stats.current_size, heap_stats.reserved_size); NetworkInterface* network = easy_connect(true); if (!network) { - return 1; + return; } + mbed_stats_heap_get(&heap_stats); + printf("[2] Heap: %lu / %lu\n", heap_stats.current_size, heap_stats.reserved_size); + // Create a TLS socket (which holds a TCPSocket) printf("\n----- Setting up TLS connection -----\n"); @@ -70,9 +77,12 @@ socket->set_debug(true); if (socket->connect() != 0) { printf("TLS Connect failed %d\n", socket->error()); - return 1; + return; } + mbed_stats_heap_get(&heap_stats); + printf("[3] Heap: %lu / %lu\n", heap_stats.current_size, heap_stats.reserved_size); + // GET request to httpbin.org { HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418"); @@ -81,14 +91,20 @@ HttpResponse* get_res = get_req->send(); if (!get_res) { printf("HttpRequest failed (error code %d)\n", get_req->get_error()); - return 1; + return; } printf("\n----- HTTPS GET response -----\n"); dump_response(get_res); + mbed_stats_heap_get(&heap_stats); + printf("[4a] Heap: %lu / %lu\n", heap_stats.current_size, heap_stats.reserved_size); + delete get_req; } + mbed_stats_heap_get(&heap_stats); + printf("[4] Heap: %lu / %lu\n", heap_stats.current_size, heap_stats.reserved_size); + // POST request to httpbin.org { HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "https://httpbin.org/post"); @@ -100,7 +116,7 @@ HttpResponse* post_res = post_req->send(body, strlen(body)); if (!post_res) { printf("HttpRequest failed (error code %d)\n", post_req->get_error()); - return 1; + return; } printf("\n----- HTTPS POST response -----\n"); @@ -109,8 +125,23 @@ delete post_req; } + mbed_stats_heap_get(&heap_stats); + printf("[5] Heap: %lu / %lu\n", heap_stats.current_size, heap_stats.reserved_size); + delete socket; + mbed_stats_heap_get(&heap_stats); + printf("[6] Heap: %lu / %lu\n", heap_stats.current_size, heap_stats.reserved_size); + + Thread::wait(osWaitForever); +} + +int main() { + pc.baud(115200); + + Thread t(osPriorityNormal, 8 * 1024); + t.start(&run); + Thread::wait(osWaitForever); }