forked from espressif/esp-idf
protocols: Updated examples to use correct API
- `http_request` and `https_x509_bundle`
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
/* HTTPS GET Example using plain mbedTLS sockets
|
/*
|
||||||
|
* HTTPS GET Example using plain Mbed TLS sockets
|
||||||
*
|
*
|
||||||
* Contacts the howsmyssl.com API via TLS v1.2 and reads a JSON
|
* Contacts the howsmyssl.com API via TLS v1.2 and reads a JSON
|
||||||
* response.
|
* response.
|
||||||
*
|
*
|
||||||
* Adapted from the ssl_client1 example in mbedtls.
|
* Adapted from the ssl_client1 example in Mbed TLS.
|
||||||
*
|
*
|
||||||
* SPDX-FileCopyrightText: 2006-2016 ARM Limited, All Rights Reserved
|
* SPDX-FileCopyrightText: The Mbed TLS Contributors
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*
|
*
|
||||||
@@ -91,13 +92,17 @@ static void https_get_request(esp_tls_cfg_t cfg, const char *WEB_SERVER_URL, con
|
|||||||
char buf[512];
|
char buf[512];
|
||||||
int ret, len;
|
int ret, len;
|
||||||
|
|
||||||
struct esp_tls *tls = esp_tls_conn_http_new(WEB_SERVER_URL, &cfg);
|
esp_tls_t *tls = esp_tls_init();
|
||||||
|
if (!tls) {
|
||||||
|
ESP_LOGE(TAG, "Failed to allocate esp_tls handle!");
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
if (tls != NULL) {
|
if (esp_tls_conn_http_new_sync(WEB_SERVER_URL, &cfg, tls) == 1) {
|
||||||
ESP_LOGI(TAG, "Connection established...");
|
ESP_LOGI(TAG, "Connection established...");
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGE(TAG, "Connection failed...");
|
ESP_LOGE(TAG, "Connection failed...");
|
||||||
goto exit;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CONFIG_EXAMPLE_CLIENT_SESSION_TICKETS
|
#ifdef CONFIG_EXAMPLE_CLIENT_SESSION_TICKETS
|
||||||
@@ -107,6 +112,7 @@ static void https_get_request(esp_tls_cfg_t cfg, const char *WEB_SERVER_URL, con
|
|||||||
tls_client_session = esp_tls_get_client_session(tls);
|
tls_client_session = esp_tls_get_client_session(tls);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
size_t written_bytes = 0;
|
size_t written_bytes = 0;
|
||||||
do {
|
do {
|
||||||
ret = esp_tls_conn_write(tls,
|
ret = esp_tls_conn_write(tls,
|
||||||
@@ -117,27 +123,22 @@ static void https_get_request(esp_tls_cfg_t cfg, const char *WEB_SERVER_URL, con
|
|||||||
written_bytes += ret;
|
written_bytes += ret;
|
||||||
} else if (ret != ESP_TLS_ERR_SSL_WANT_READ && ret != ESP_TLS_ERR_SSL_WANT_WRITE) {
|
} else if (ret != ESP_TLS_ERR_SSL_WANT_READ && ret != ESP_TLS_ERR_SSL_WANT_WRITE) {
|
||||||
ESP_LOGE(TAG, "esp_tls_conn_write returned: [0x%02X](%s)", ret, esp_err_to_name(ret));
|
ESP_LOGE(TAG, "esp_tls_conn_write returned: [0x%02X](%s)", ret, esp_err_to_name(ret));
|
||||||
goto exit;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
} while (written_bytes < strlen(REQUEST));
|
} while (written_bytes < strlen(REQUEST));
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Reading HTTP response...");
|
ESP_LOGI(TAG, "Reading HTTP response...");
|
||||||
|
|
||||||
do {
|
do {
|
||||||
len = sizeof(buf) - 1;
|
len = sizeof(buf) - 1;
|
||||||
bzero(buf, sizeof(buf));
|
memset(buf, 0x00, sizeof(buf));
|
||||||
ret = esp_tls_conn_read(tls, (char *)buf, len);
|
ret = esp_tls_conn_read(tls, (char *)buf, len);
|
||||||
|
|
||||||
if (ret == ESP_TLS_ERR_SSL_WANT_WRITE || ret == ESP_TLS_ERR_SSL_WANT_READ) {
|
if (ret == ESP_TLS_ERR_SSL_WANT_WRITE || ret == ESP_TLS_ERR_SSL_WANT_READ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
} else if (ret < 0) {
|
||||||
|
|
||||||
if (ret < 0) {
|
|
||||||
ESP_LOGE(TAG, "esp_tls_conn_read returned [-0x%02X](%s)", -ret, esp_err_to_name(ret));
|
ESP_LOGE(TAG, "esp_tls_conn_read returned [-0x%02X](%s)", -ret, esp_err_to_name(ret));
|
||||||
break;
|
break;
|
||||||
}
|
} else if (ret == 0) {
|
||||||
|
|
||||||
if (ret == 0) {
|
|
||||||
ESP_LOGI(TAG, "connection closed");
|
ESP_LOGI(TAG, "connection closed");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -151,8 +152,9 @@ static void https_get_request(esp_tls_cfg_t cfg, const char *WEB_SERVER_URL, con
|
|||||||
putchar('\n'); // JSON output doesn't have a newline at end
|
putchar('\n'); // JSON output doesn't have a newline at end
|
||||||
} while (1);
|
} while (1);
|
||||||
|
|
||||||
exit:
|
cleanup:
|
||||||
esp_tls_conn_destroy(tls);
|
esp_tls_conn_destroy(tls);
|
||||||
|
exit:
|
||||||
for (int countdown = 10; countdown >= 0; countdown--) {
|
for (int countdown = 10; countdown >= 0; countdown--) {
|
||||||
ESP_LOGI(TAG, "%d...", countdown);
|
ESP_LOGI(TAG, "%d...", countdown);
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
@@ -251,7 +253,7 @@ static void https_request_task(void *pvparameters)
|
|||||||
#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
|
#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
|
||||||
https_get_request_using_crt_bundle();
|
https_get_request_using_crt_bundle();
|
||||||
#endif
|
#endif
|
||||||
printf("Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size());
|
ESP_LOGI(TAG, "Minimum free heap size: %d bytes", esp_get_minimum_free_heap_size());
|
||||||
https_get_request_using_cacert_buf();
|
https_get_request_using_cacert_buf();
|
||||||
https_get_request_using_global_ca_store();
|
https_get_request_using_global_ca_store();
|
||||||
ESP_LOGI(TAG, "Finish https_request example");
|
ESP_LOGI(TAG, "Finish https_request example");
|
||||||
@@ -260,7 +262,7 @@ static void https_request_task(void *pvparameters)
|
|||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
ESP_ERROR_CHECK( nvs_flash_init() );
|
ESP_ERROR_CHECK(nvs_flash_init());
|
||||||
ESP_ERROR_CHECK(esp_netif_init());
|
ESP_ERROR_CHECK(esp_netif_init());
|
||||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
|
|
||||||
|
@@ -56,16 +56,20 @@ static void https_get_task(void *pvParameters)
|
|||||||
{
|
{
|
||||||
while (1) {
|
while (1) {
|
||||||
int conn_count = 0;
|
int conn_count = 0;
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Connecting to %d URLs", MAX_URLS);
|
ESP_LOGI(TAG, "Connecting to %d URLs", MAX_URLS);
|
||||||
|
|
||||||
for (int i = 0; i < MAX_URLS; i++) {
|
for (int i = 0; i < MAX_URLS; i++) {
|
||||||
esp_tls_cfg_t cfg = {
|
esp_tls_cfg_t cfg = {
|
||||||
.crt_bundle_attach = esp_crt_bundle_attach,
|
.crt_bundle_attach = esp_crt_bundle_attach,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct esp_tls *tls = esp_tls_conn_http_new(web_urls[i], &cfg);
|
esp_tls_t *tls = esp_tls_init();
|
||||||
|
if (!tls) {
|
||||||
|
ESP_LOGE(TAG, "Failed to allocate esp_tls handle!");
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
if (tls != NULL) {
|
if (esp_tls_conn_http_new_sync(web_urls[i], &cfg, tls) == 1) {
|
||||||
ESP_LOGI(TAG, "Connection established to %s", web_urls[i]);
|
ESP_LOGI(TAG, "Connection established to %s", web_urls[i]);
|
||||||
conn_count++;
|
conn_count++;
|
||||||
} else {
|
} else {
|
||||||
@@ -73,6 +77,7 @@ static void https_get_task(void *pvParameters)
|
|||||||
}
|
}
|
||||||
|
|
||||||
esp_tls_conn_destroy(tls);
|
esp_tls_conn_destroy(tls);
|
||||||
|
end:
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,8 +88,8 @@ static void https_get_task(void *pvParameters)
|
|||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
ESP_ERROR_CHECK( nvs_flash_init() );
|
ESP_ERROR_CHECK(nvs_flash_init());
|
||||||
esp_netif_init();
|
ESP_ERROR_CHECK(esp_netif_init());
|
||||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
|
|
||||||
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
||||||
|
Reference in New Issue
Block a user