From 07d784f7fee836d24a094a554f40a513bcfe819d Mon Sep 17 00:00:00 2001 From: yuanjm Date: Tue, 19 Jan 2021 17:51:32 +0800 Subject: [PATCH] examples: Add OTA bind specified interface example Closes https://github.com/espressif/esp-idf/issues/6090 --- .../system/ota/simple_ota_example/README.md | 4 ++- .../simple_ota_example/main/Kconfig.projbuild | 25 +++++++++++++++++++ .../main/simple_ota_example.c | 25 +++++++++++++++++-- .../ota/simple_ota_example/sdkconfig.ci | 1 + 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/examples/system/ota/simple_ota_example/README.md b/examples/system/ota/simple_ota_example/README.md index a2629e1009..31a340974d 100644 --- a/examples/system/ota/simple_ota_example/README.md +++ b/examples/system/ota/simple_ota_example/README.md @@ -4,4 +4,6 @@ This example is based on `esp_https_ota` component's APIs. ## Configuration -Refer README.md in the parent directory for setup details +Refer README.md in the parent directory for setup details. + +Example also supports binding to specific interface (either "Ethernet" or "WiFi Station"), which will allow firmware upgrade to happen through specific interface (in case multiple networking interfaces are enabled). Please see more on this through example configuration in `idf.py menuconfig -> Example Configuration -> Support firmware upgrade bind specificied interface->Choose OTA data bind interface`. diff --git a/examples/system/ota/simple_ota_example/main/Kconfig.projbuild b/examples/system/ota/simple_ota_example/main/Kconfig.projbuild index 2c2eb0c583..588d22b6c8 100644 --- a/examples/system/ota/simple_ota_example/main/Kconfig.projbuild +++ b/examples/system/ota/simple_ota_example/main/Kconfig.projbuild @@ -17,4 +17,29 @@ menu "Example Configuration" help This allows you to skip the validation of OTA server certificate CN field. + config EXAMPLE_FIRMWARE_UPGRADE_BIND_IF + bool "Support firmware upgrade bind specified interface" + default n + help + This allows you to bind specified interface in OTA example. + + choice EXAMPLE_FIRMWARE_UPGRADE_BIND_IF_TYPE + prompt "Choose OTA data bind interface" + default EXAMPLE_FIRMWARE_UPGRADE_BIND_IF_STA + depends on EXAMPLE_FIRMWARE_UPGRADE_BIND_IF + help + Select which interface type of OTA data go through. + + config EXAMPLE_FIRMWARE_UPGRADE_BIND_IF_STA + bool "Bind wifi station interface" + depends on EXAMPLE_CONNECT_WIFI + help + Select wifi station interface to pass the OTA data. + + config EXAMPLE_FIRMWARE_UPGRADE_BIND_IF_ETH + bool "Bind ethernet interface" + depends on EXAMPLE_CONNECT_ETHERNET + help + Select ethernet interface to pass the OTA data. + endchoice endmenu diff --git a/examples/system/ota/simple_ota_example/main/simple_ota_example.c b/examples/system/ota/simple_ota_example/main/simple_ota_example.c index 1f73b4d832..30730c3135 100644 --- a/examples/system/ota/simple_ota_example/main/simple_ota_example.c +++ b/examples/system/ota/simple_ota_example/main/simple_ota_example.c @@ -20,11 +20,20 @@ #include "nvs.h" #include "nvs_flash.h" #include "protocol_examples_common.h" - +#include #if CONFIG_EXAMPLE_CONNECT_WIFI #include "esp_wifi.h" #endif +#ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF +/* The interface name value can refer to if_desc in esp_netif_defaults.h */ +#if CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF_ETH +static const char *bind_interface_name = "eth"; +#elif CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF_STA +static const char *bind_interface_name = "sta"; +#endif +#endif + static const char *TAG = "simple_ota_example"; extern const uint8_t server_cert_pem_start[] asm("_binary_ca_cert_pem_start"); extern const uint8_t server_cert_pem_end[] asm("_binary_ca_cert_pem_end"); @@ -62,12 +71,24 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt) void simple_ota_example_task(void *pvParameter) { ESP_LOGI(TAG, "Starting OTA example"); - +#ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF + esp_netif_t *netif = get_example_netif_from_desc(bind_interface_name); + if (netif == NULL) { + ESP_LOGE(TAG, "Can't find netif from interface description"); + abort(); + } + struct ifreq ifr; + esp_netif_get_netif_impl_name(netif, ifr.ifr_name); + ESP_LOGI(TAG, "Bind interface name is %s", ifr.ifr_name); +#endif esp_http_client_config_t config = { .url = CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL, .cert_pem = (char *)server_cert_pem_start, .event_handler = _http_event_handler, .keep_alive_enable = true, +#ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF + .if_name = &ifr, +#endif }; #ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL_FROM_STDIN diff --git a/examples/system/ota/simple_ota_example/sdkconfig.ci b/examples/system/ota/simple_ota_example/sdkconfig.ci index 8367dac01f..278e33dd00 100644 --- a/examples/system/ota/simple_ota_example/sdkconfig.ci +++ b/examples/system/ota/simple_ota_example/sdkconfig.ci @@ -1,2 +1,3 @@ CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL="FROM_STDIN" CONFIG_EXAMPLE_SKIP_COMMON_NAME_CHECK=y +CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF=y