From 0aec63c18e1d573289845f66d7101c7e7ea9a039 Mon Sep 17 00:00:00 2001 From: Jitin George Date: Wed, 29 Aug 2018 16:23:48 +0530 Subject: [PATCH] examples/protocols/esp_http_client: Add example for asynchronous HTTP request --- components/esp-tls/esp_tls.c | 14 +++----- .../main/esp_http_client_example.c | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/components/esp-tls/esp_tls.c b/components/esp-tls/esp_tls.c index 5abefaf34..3ead8ef2e 100644 --- a/components/esp-tls/esp_tls.c +++ b/components/esp-tls/esp_tls.c @@ -33,8 +33,6 @@ static const char *TAG = "esp-tls"; #define ESP_LOGE(TAG, ...) printf(__VA_ARGS__); #endif -#define DEFAULT_TIMEOUT_MS 0 - static struct addrinfo *resolve_host_name(const char *host, size_t hostlen) { struct addrinfo hints; @@ -301,15 +299,12 @@ static int esp_tls_low_level_conn(const char *hostname, int hostlen, int port, c tls->wset = tls->rset; } tls->conn_state = ESP_TLS_CONNECTING; - case ESP_TLS_CONNECTING: /* fall-through */ + /* falls through */ + case ESP_TLS_CONNECTING: if (cfg->non_block) { ESP_LOGD(TAG, "connecting..."); struct timeval tv; - if (cfg->timeout_ms) { - ms_to_timeval(cfg->timeout_ms, &tv); - } else { - ms_to_timeval(DEFAULT_TIMEOUT_MS, &tv); - } + ms_to_timeval(cfg->timeout_ms, &tv); /* In case of non-blocking I/O, we use the select() API to check whether connection has been estbalished or not*/ @@ -339,7 +334,8 @@ static int esp_tls_low_level_conn(const char *hostname, int hostlen, int port, c tls->read = tls_read; tls->write = tls_write; tls->conn_state = ESP_TLS_HANDSHAKE; - case ESP_TLS_HANDSHAKE: /* fall-through */ + /* falls through */ + case ESP_TLS_HANDSHAKE: ESP_LOGD(TAG, "handshake in progress..."); ret = mbedtls_ssl_handshake(&tls->ssl); if (ret == 0) { diff --git a/examples/protocols/esp_http_client/main/esp_http_client_example.c b/examples/protocols/esp_http_client/main/esp_http_client_example.c index f862ddc70..ac5e1942c 100644 --- a/examples/protocols/esp_http_client/main/esp_http_client_example.c +++ b/examples/protocols/esp_http_client/main/esp_http_client_example.c @@ -342,6 +342,38 @@ static void http_perform_as_stream_reader() free(buffer); } +static void https_async() +{ + esp_http_client_config_t config = { + .url = "https://postman-echo.com/post", + .event_handler = _http_event_handler, + .is_async = true, + .timeout_ms = 5000, + }; + esp_http_client_handle_t client = esp_http_client_init(&config); + esp_err_t err; + const char *post_data = "Using a Palantír requires a person with great strength of will and wisdom. The Palantíri were meant to " + "be used by the Dúnedain to communicate throughout the Realms in Exile. During the War of the Ring, " + "the Palantíri were used by many individuals. Sauron used the Ithil-stone to take advantage of the users " + "of the other two stones, the Orthanc-stone and Anor-stone, but was also susceptible to deception himself."; + esp_http_client_set_method(client, HTTP_METHOD_POST); + esp_http_client_set_post_field(client, post_data, strlen(post_data)); + while (1) { + err = esp_http_client_perform(client); + if (err != ESP_ERR_HTTP_EAGAIN) { + break; + } + } + if (err == ESP_OK) { + ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %d", + esp_http_client_get_status_code(client), + esp_http_client_get_content_length(client)); + } else { + ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err)); + } + esp_http_client_cleanup(client); +} + static void http_test_task(void *pvParameters) { app_wifi_wait_connected(); @@ -356,6 +388,7 @@ static void http_test_task(void *pvParameters) http_redirect_to_https(); http_download_chunk(); http_perform_as_stream_reader(); + https_async(); ESP_LOGI(TAG, "Finish http example"); vTaskDelete(NULL); }