diff --git a/examples/protocols/esp_http_client/CMakeLists.txt b/examples/protocols/esp_http_client/CMakeLists.txt index 3059963611..a30836152e 100644 --- a/examples/protocols/esp_http_client/CMakeLists.txt +++ b/examples/protocols/esp_http_client/CMakeLists.txt @@ -2,9 +2,14 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -# (Not part of the boilerplate) -# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. -set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +if(${IDF_TARGET} STREQUAL "linux") + set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/" + "$ENV{IDF_PATH}/examples/protocols/linux_stubs/esp_stubs") + set(COMPONENTS main) +else() + set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +endif() include($ENV{IDF_PATH}/tools/cmake/project.cmake) + project(esp_http_client_example) diff --git a/examples/protocols/esp_http_client/main/CMakeLists.txt b/examples/protocols/esp_http_client/main/CMakeLists.txt index 12347781a6..783d70fa84 100644 --- a/examples/protocols/esp_http_client/main/CMakeLists.txt +++ b/examples/protocols/esp_http_client/main/CMakeLists.txt @@ -1,7 +1,12 @@ # Embed the server root certificate into the final binary # # (If this was a component, we would set COMPONENT_EMBED_TXTFILES here.) +set(requires "") +if(${IDF_TARGET} STREQUAL "linux") + list(APPEND requires esp_stubs esp-tls esp_http_client) +endif() idf_component_register(SRCS "esp_http_client_example.c" INCLUDE_DIRS "." + REQUIRES ${requires} EMBED_TXTFILES howsmyssl_com_root_cert.pem postman_root_cert.pem) 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 a24724a089..e461edf180 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 @@ -9,10 +9,7 @@ #include #include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" #include "esp_log.h" -#include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" #include "esp_netif.h" @@ -22,6 +19,12 @@ #include "esp_crt_bundle.h" #endif +#if !CONFIG_IDF_TARGET_LINUX +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_system.h" +#endif + #include "esp_http_client.h" #define MAX_HTTP_RECV_BUFFER 512 @@ -144,7 +147,7 @@ static void http_rest_with_url(void) // GET esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -160,7 +163,7 @@ static void http_rest_with_url(void) esp_http_client_set_post_field(client, post_data, strlen(post_data)); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -172,7 +175,7 @@ static void http_rest_with_url(void) esp_http_client_set_method(client, HTTP_METHOD_PUT); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP PUT Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP PUT Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -185,7 +188,7 @@ static void http_rest_with_url(void) esp_http_client_set_post_field(client, NULL, 0); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP PATCH Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP PATCH Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -197,7 +200,7 @@ static void http_rest_with_url(void) esp_http_client_set_method(client, HTTP_METHOD_DELETE); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP DELETE Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP DELETE Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -209,7 +212,7 @@ static void http_rest_with_url(void) esp_http_client_set_method(client, HTTP_METHOD_HEAD); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP HEAD Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP HEAD Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -232,7 +235,7 @@ static void http_rest_with_hostname_path(void) // GET esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -246,7 +249,7 @@ static void http_rest_with_hostname_path(void) esp_http_client_set_post_field(client, post_data, strlen(post_data)); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -258,7 +261,7 @@ static void http_rest_with_hostname_path(void) esp_http_client_set_method(client, HTTP_METHOD_PUT); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP PUT Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP PUT Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -271,7 +274,7 @@ static void http_rest_with_hostname_path(void) esp_http_client_set_post_field(client, NULL, 0); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP PATCH Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP PATCH Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -283,7 +286,7 @@ static void http_rest_with_hostname_path(void) esp_http_client_set_method(client, HTTP_METHOD_DELETE); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP DELETE Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP DELETE Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -295,7 +298,7 @@ static void http_rest_with_hostname_path(void) esp_http_client_set_method(client, HTTP_METHOD_HEAD); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP HEAD Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP HEAD Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -325,7 +328,7 @@ static void http_auth_basic(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Basic Auth Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP Basic Auth Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -344,7 +347,7 @@ static void http_auth_basic_redirect(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Basic Auth redirect Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP Basic Auth redirect Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -365,7 +368,7 @@ static void http_auth_digest(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Digest Auth Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP Digest Auth Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -387,7 +390,7 @@ static void https_with_url(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -410,7 +413,7 @@ static void https_with_hostname_path(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -429,7 +432,7 @@ static void http_relative_redirect(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Relative path redirect Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP Relative path redirect Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -448,7 +451,7 @@ static void http_absolute_redirect(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Absolute path redirect Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP Absolute path redirect Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -468,7 +471,7 @@ static void http_absolute_redirect_manual(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Absolute path redirect (manual) Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP Absolute path redirect (manual) Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -488,7 +491,7 @@ static void http_redirect_to_https(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP redirect to HTTPS Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP redirect to HTTPS Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -508,7 +511,7 @@ static void http_download_chunk(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP chunk encoding Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP chunk encoding Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -544,7 +547,7 @@ static void http_perform_as_stream_reader(void) buffer[read_len] = 0; ESP_LOGD(TAG, "read_len = %d", read_len); } - ESP_LOGI(TAG, "HTTP Stream reader Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP Stream reader Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); esp_http_client_close(client); @@ -576,7 +579,7 @@ static void https_async(void) } } if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -595,7 +598,7 @@ static void https_with_invalid_url(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -631,7 +634,7 @@ static void http_native_request(void) } else { int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER); if (data_read >= 0) { - ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); ESP_LOG_BUFFER_HEX(TAG, output_buffer, data_read); @@ -661,7 +664,7 @@ static void http_native_request(void) } else { int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER); if (data_read >= 0) { - ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); ESP_LOG_BUFFER_HEX(TAG, output_buffer, strlen(output_buffer)); @@ -687,7 +690,7 @@ static void http_partial_download(void) esp_http_client_set_header(client, "Range", "bytes=10-"); esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -698,7 +701,7 @@ static void http_partial_download(void) esp_http_client_set_header(client, "Range", "bytes=-10"); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -709,7 +712,7 @@ static void http_partial_download(void) esp_http_client_set_header(client, "Range", "bytes=11-20"); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Status = %d, content_length = %lld", + ESP_LOGI(TAG, "HTTP Status = %d, content_length = %"PRIu64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -749,7 +752,9 @@ static void http_test_task(void *pvParameters) #endif ESP_LOGI(TAG, "Finish http example"); +#if !CONFIG_IDF_TARGET_LINUX vTaskDelete(NULL); +#endif } void app_main(void) @@ -760,6 +765,7 @@ void app_main(void) ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); + ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); @@ -770,5 +776,9 @@ void app_main(void) ESP_ERROR_CHECK(example_connect()); ESP_LOGI(TAG, "Connected to AP, begin http example"); +#if CONFIG_IDF_TARGET_LINUX + http_test_task(NULL); +#else xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL); +#endif } diff --git a/examples/protocols/linux_stubs/esp_stubs/CMakeLists.txt b/examples/protocols/linux_stubs/esp_stubs/CMakeLists.txt new file mode 100644 index 0000000000..f15cfe95d0 --- /dev/null +++ b/examples/protocols/linux_stubs/esp_stubs/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS esp_stubs.c + INCLUDE_DIRS "include") diff --git a/examples/protocols/linux_stubs/esp_stubs/esp_stubs.c b/examples/protocols/linux_stubs/esp_stubs/esp_stubs.c new file mode 100644 index 0000000000..33f0cfd0ff --- /dev/null +++ b/examples/protocols/linux_stubs/esp_stubs/esp_stubs.c @@ -0,0 +1,42 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include +#include "esp_err.h" +#include "esp_log.h" + +extern void app_main(void); + +esp_err_t esp_event_loop_create_default(void) +{ + return ESP_OK; +} + +esp_err_t esp_netif_init(void) +{ + return ESP_OK; +} + +esp_err_t example_connect(void) +{ + return ESP_OK; +} + +esp_err_t nvs_flash_init(void) +{ + return ESP_OK; +} + +esp_err_t nvs_flash_erase(void) +{ + return ESP_OK; +} + +int main() +{ + app_main(); + + return 0; +} diff --git a/examples/protocols/linux_stubs/esp_stubs/include/esp_event.h b/examples/protocols/linux_stubs/esp_stubs/include/esp_event.h new file mode 100644 index 0000000000..e909e800f6 --- /dev/null +++ b/examples/protocols/linux_stubs/esp_stubs/include/esp_event.h @@ -0,0 +1,8 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include "esp_err.h" + +esp_err_t esp_event_loop_create_default(void); diff --git a/examples/protocols/linux_stubs/esp_stubs/include/esp_netif.h b/examples/protocols/linux_stubs/esp_stubs/include/esp_netif.h new file mode 100644 index 0000000000..5f94c2afb7 --- /dev/null +++ b/examples/protocols/linux_stubs/esp_stubs/include/esp_netif.h @@ -0,0 +1,11 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include +#include +#include +#include "esp_err.h" + +esp_err_t esp_netif_init(void); diff --git a/examples/protocols/linux_stubs/esp_stubs/include/nvs_flash.h b/examples/protocols/linux_stubs/esp_stubs/include/nvs_flash.h new file mode 100644 index 0000000000..87ebc96a33 --- /dev/null +++ b/examples/protocols/linux_stubs/esp_stubs/include/nvs_flash.h @@ -0,0 +1,14 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include "esp_err.h" + +#define ESP_ERR_NVS_BASE 0x1100 /*!< Starting number of error codes */ +#define ESP_ERR_NVS_NO_FREE_PAGES (ESP_ERR_NVS_BASE + 0x0d) /*!< NVS partition doesn't contain any empty pages. This may happen if NVS partition was truncated. Erase the whole partition and call nvs_flash_init again. */ +#define ESP_ERR_NVS_NEW_VERSION_FOUND (ESP_ERR_NVS_BASE + 0x10) /*!< NVS partition contains data in new format and cannot be recognized by this version of code */ + +esp_err_t nvs_flash_init(void); + +esp_err_t nvs_flash_erase(void); diff --git a/examples/protocols/linux_stubs/esp_stubs/include/protocol_examples_common.h b/examples/protocols/linux_stubs/esp_stubs/include/protocol_examples_common.h new file mode 100644 index 0000000000..416377a82d --- /dev/null +++ b/examples/protocols/linux_stubs/esp_stubs/include/protocol_examples_common.h @@ -0,0 +1,8 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include "esp_err.h" + +esp_err_t example_connect(void);