thingspark example
Dependencies: MbedJSONValue mbed-http HTS221
Revision 31:66704f6f17c5, committed 2018-10-30
- Comitter:
- Jan Jongboom
- Date:
- Tue Oct 30 11:07:10 2018 +0800
- Branch:
- mbed-os-5.10
- Parent:
- 30:4825e4f38844
- Child:
- 32:5fa61ebc2689
- Commit message:
- Update to new mbed-http, which uses TLSSocket
Changed in this revision
--- a/mbed-http.lib Mon Oct 29 14:34:43 2018 +0800 +++ b/mbed-http.lib Tue Oct 30 11:07:10 2018 +0800 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/sandbox/code/mbed-http/#9a04ed79d67e +https://developer.mbed.org/teams/sandbox/code/mbed-http/#fa4d71265625
--- a/mbed_app.json Mon Oct 29 14:34:43 2018 +0800
+++ b/mbed_app.json Tue Oct 30 11:07:10 2018 +0800
@@ -5,9 +5,12 @@
}
},
"macros": [
+ "MBEDTLS_MPI_MAX_SIZE=1024",
+ "MBEDTLS_MPI_WINDOW_SIZE=1",
"MBEDTLS_USER_CONFIG_FILE=\"mbedtls_entropy_config.h\"",
"MBEDTLS_TEST_NULL_ENTROPY",
- "MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES"
+ "MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES",
+ "MBED_HEAP_STATS_ENABLED=1"
],
"target_overrides": {
"*": {
@@ -15,8 +18,9 @@
"platform.stdio-convert-newlines": true,
"mbed-mesh-api.6lowpan-nd-channel-page": 0,
"mbed-mesh-api.6lowpan-nd-channel": 12,
- "mbed-trace.enable": 0,
+ "mbed-trace.enable": 1,
"mbed-http.http-buffer-size": 2048,
+ "tls-socket.debug-level": 0,
"nsapi.default-wifi-security": "WPA_WPA2",
"nsapi.default-wifi-ssid": "\"SSID\"",
"nsapi.default-wifi-password": "\"Password\""
--- a/mbedtls_entropy_config.h Mon Oct 29 14:34:43 2018 +0800 +++ b/mbedtls_entropy_config.h Tue Oct 30 11:07:10 2018 +0800 @@ -26,31 +26,3 @@ #undef MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES #undef MBEDTLS_TEST_NULL_ENTROPY #endif - -#if DEMO == DEMO_HTTPS - -#if !defined(MBEDTLS_ENTROPY_HARDWARE_ALT) && \ - !defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_TEST_NULL_ENTROPY) -#error "This hardware does not have an entropy source." -#endif /* !MBEDTLS_ENTROPY_HARDWARE_ALT && !MBEDTLS_ENTROPY_NV_SEED && - * !MBEDTLS_TEST_NULL_ENTROPY */ - -#if !defined(MBEDTLS_SHA1_C) -#define MBEDTLS_SHA1_C -#endif /* !MBEDTLS_SHA1_C */ - -#if !defined(MBEDTLS_RSA_C) -#define MBEDTLS_RSA_C -#endif /* !MBEDTLS_RSA_C */ - -/* - * This value is sufficient for handling 2048 bit RSA keys. - * - * Set this value higher to enable handling larger keys, but be aware that this - * will increase the stack usage. - */ -#define MBEDTLS_MPI_MAX_SIZE 1024 - -#define MBEDTLS_MPI_WINDOW_SIZE 1 - -#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/simconfig.json Tue Oct 30 11:07:10 2018 +0800
@@ -0,0 +1,5 @@
+{
+ "ignore": [
+ "./mbed-http"
+ ]
+}
\ No newline at end of file
--- a/source/main-http-ipv6.cpp Mon Oct 29 14:34:43 2018 +0800
+++ b/source/main-http-ipv6.cpp Tue Oct 30 11:07:10 2018 +0800
@@ -3,7 +3,7 @@
#if DEMO == DEMO_HTTP_IPV6
#include "mbed.h"
-#include "easy-connect.h"
+#include "network-helper.h"
#include "http_request.h"
void dump_response(HttpResponse* res) {
--- a/source/main-http-socket-reuse.cpp Mon Oct 29 14:34:43 2018 +0800
+++ b/source/main-http-socket-reuse.cpp Tue Oct 30 11:07:10 2018 +0800
@@ -78,6 +78,7 @@
delete post_req;
}
+ socket->close();
delete socket;
wait(osWaitForever);
--- a/source/main-http.cpp Mon Oct 29 14:34:43 2018 +0800
+++ b/source/main-http.cpp Tue Oct 30 11:07:10 2018 +0800
@@ -5,6 +5,7 @@
#include "mbed.h"
#include "http_request.h"
#include "network-helper.h"
+#include "mbed_mem_trace.h"
void dump_response(HttpResponse* res) {
printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
--- a/source/main-https-chunked-request.cpp Mon Oct 29 14:34:43 2018 +0800
+++ b/source/main-https-chunked-request.cpp Tue Oct 30 11:07:10 2018 +0800
@@ -8,6 +8,7 @@
#if DEMO == DEMO_HTTPS_CHUNKED_REQUEST
#include "mbed.h"
+#include "mbed_trace.h"
#include "https_request.h"
#include "network-helper.h"
@@ -34,13 +35,13 @@
"-----END CERTIFICATE-----\n";
void dump_response(HttpResponse* res) {
- mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
+ printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
- mbedtls_printf("Headers:\n");
+ printf("Headers:\n");
for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
- mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
+ printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
}
- mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+ printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
}
// Spread the message out over 3 different chunks
@@ -53,7 +54,7 @@
int chunk_ix = 0;
// Callback function, grab the next chunk and return it
-const void * get_chunk(size_t* out_size) {
+const void * get_chunk(uint32_t* out_size) {
// If you don't have any data left, set out_size to 0 and return a null pointer
if (chunk_ix == (sizeof(chunks) / sizeof(chunks[0]))) {
*out_size = 0;
@@ -73,11 +74,16 @@
return 1;
}
- // POST request to httpbin.org
+ mbed_trace_init();
+
+ // This example also logs the raw request, you can do this by calling 'set_request_log_buffer' on the request
+ uint8_t *request_buffer = (uint8_t*)calloc(2048, 1);
+
+ // POST request to reqres.in
{
HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://reqres.in/api/users");
- post_req->set_debug(true);
post_req->set_header("Content-Type", "application/json");
+ post_req->set_request_log_buffer(request_buffer, 2048);
// If you pass a callback here, the Transfer-Encoding header is automatically set to chunked
HttpResponse* post_res = post_req->send(&get_chunk);
@@ -86,6 +92,15 @@
return 1;
}
+ // Log the raw request that went over the line (if you decode the hex you can see the chunked parts)
+ // e.g. in Node.js (take the output from below):
+ // '50 4f 53 54 20'.split(' ').map(c=>parseInt(c,16)).map(c=>String.fromCharCode(c)).join('')
+ printf("\n----- Request buffer -----\n");
+ for (size_t ix = 0; ix < post_req->get_request_log_buffer_length(); ix++) {
+ printf("%02x ", request_buffer[ix]);
+ }
+ printf("\n");
+
printf("\n----- HTTPS POST response -----\n");
dump_response(post_res);
--- a/source/main-https-socket-reuse.cpp Mon Oct 29 14:34:43 2018 +0800
+++ b/source/main-https-socket-reuse.cpp Tue Oct 30 11:07:10 2018 +0800
@@ -7,6 +7,7 @@
#if DEMO == DEMO_HTTPS_SOCKET_REUSE
#include "mbed.h"
+#include "mbed_trace.h"
#include "https_request.h"
#include "network-helper.h"
@@ -44,13 +45,13 @@
"-----END CERTIFICATE-----\n";
void dump_response(HttpResponse* res) {
- mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
+ printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
- mbedtls_printf("Headers:\n");
+ printf("Headers:\n");
for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
- mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
+ printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
}
- mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+ printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
}
int main() {
@@ -60,20 +61,30 @@
return 1;
}
+ mbed_trace_init();
+
// Create a TLS socket (which holds a TCPSocket)
printf("\n----- Setting up TLS connection -----\n");
- TLSSocket* socket = new TLSSocket(network, "httpbin.org", 443, SSL_CA_PEM);
- socket->set_debug(true);
- if (socket->connect() != 0) {
- printf("TLS Connect failed %d\n", socket->error());
+ nsapi_error_t r;
+
+ TLSSocket* socket = new TLSSocket();
+ if ((r = socket->open(network)) != NSAPI_ERROR_OK) {
+ printf("TLS socket open failed (%d)\n", r);
+ return 1;
+ }
+ if ((r = socket->set_root_ca_cert(SSL_CA_PEM)) != NSAPI_ERROR_OK) {
+ printf("TLS socket set_root_ca_cert failed (%d)\n", r);
+ return 1;
+ }
+ if ((r = socket->connect("httpbin.org", 443)) != NSAPI_ERROR_OK) {
+ printf("TLS socket connect failed (%d)\n", r);
return 1;
}
// GET request to httpbin.org
{
HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418");
- get_req->set_debug(true);
HttpResponse* get_res = get_req->send();
if (!get_res) {
@@ -89,7 +100,6 @@
// POST request to httpbin.org
{
HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "https://httpbin.org/post");
- post_req->set_debug(true);
post_req->set_header("Content-Type", "application/json");
const char body[] = "{\"hello\":\"world\"}";
@@ -106,6 +116,7 @@
delete post_req;
}
+ socket->close();
delete socket;
wait(osWaitForever);
--- a/source/main-https.cpp Mon Oct 29 14:34:43 2018 +0800
+++ b/source/main-https.cpp Tue Oct 30 11:07:10 2018 +0800
@@ -1,8 +1,11 @@
+#define MBED_CONF_MBED_TRACE_ENABLE 1
+
#include "select-demo.h"
#if DEMO == DEMO_HTTPS
#include "mbed.h"
+#include "mbed_trace.h"
#include "https_request.h"
#include "network-helper.h"
@@ -61,13 +64,13 @@
"-----END CERTIFICATE-----\n";
void dump_response(HttpResponse* res) {
- mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
+ printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
- mbedtls_printf("Headers:\n");
+ printf("Headers:\n");
for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
- mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
+ printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
}
- mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+ printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
}
int main() {
@@ -77,17 +80,18 @@
return 1;
}
+ mbed_trace_init();
+
// GET request to os.mbed.com
{
printf("\n----- HTTPS GET request -----\n");
HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt");
- get_req->set_debug(true);
HttpResponse* get_res = get_req->send();
if (!get_res) {
printf("HttpRequest failed (error code %d)\n", get_req->get_error());
- goto demo2;
+ return 1;
}
printf("\n----- HTTPS GET response -----\n");
dump_response(get_res);
@@ -95,13 +99,11 @@
delete get_req;
}
-demo2:
// POST request to httpbin.org
{
printf("\n----- HTTPS POST request -----\n");
HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://httpbin.org/post");
- post_req->set_debug(true);
post_req->set_header("Content-Type", "application/json");
const char body[] = "{\"hello\":\"world\"}";
--- a/source/network-helper.h Mon Oct 29 14:34:43 2018 +0800
+++ b/source/network-helper.h Tue Oct 30 11:07:10 2018 +0800
@@ -2,6 +2,9 @@
#define _MBED_HTTP_EXAMPLE_H_
#include "mbed.h"
+#ifdef TARGET_SIMULATOR
+#include "EthernetInterface.h"
+#endif
/**
* Connect to the network using the default networking interface,
@@ -11,7 +14,11 @@
NetworkInterface *connect_to_default_network_interface() {
printf("[NWKH] Connecting to network...\n");
+#ifdef TARGET_SIMULATOR
+ NetworkInterface* network = new EthernetInterface();
+#else
NetworkInterface* network = NetworkInterface::get_default_instance();
+#endif
nsapi_error_t connect_status = network->connect();