Преглед изворни кода

Merge branch 'bugfix/fix_https_request_example' into 'master'

example: Updated https_request example to fail when time is not synced.

See merge request espressif/esp-idf!19701
Mahavir Jain пре 3 година
родитељ
комит
13a34affc6

+ 1 - 1
examples/protocols/https_request/main/https_request_example_main.c

@@ -280,7 +280,7 @@ void app_main(void)
     }
 
     const esp_timer_create_args_t nvs_update_timer_args = {
-            .callback = &fetch_and_store_time_in_nvs,
+            .callback = (void *)&fetch_and_store_time_in_nvs,
     };
 
     esp_timer_handle_t nvs_update_timer;

+ 2 - 2
examples/protocols/https_request/main/include/time_sync.h

@@ -1,5 +1,5 @@
 /*
- * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  *
  * SPDX-License-Identifier: Apache-2.0
  */
@@ -20,7 +20,7 @@ esp_err_t update_time_from_nvs(void);
  * @brief Fetch the current time from SNTP and stores it in NVS.
  *
  */
-void fetch_and_store_time_in_nvs(void*);
+esp_err_t fetch_and_store_time_in_nvs(void*);
 
 #ifdef __cplusplus
 }

+ 19 - 7
examples/protocols/https_request/main/time_sync.c

@@ -1,5 +1,5 @@
 /*
- * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  *
  * SPDX-License-Identifier: Apache-2.0
  */
@@ -34,14 +34,15 @@ void initialize_sntp(void)
 {
     ESP_LOGI(TAG, "Initializing SNTP");
     sntp_setoperatingmode(SNTP_OPMODE_POLL);
-    sntp_setservername(0, "pool.ntp.org");
+    sntp_setservername(0, "time.windows.com");
+    sntp_setservername(1, "pool.ntp.org");
 #ifdef CONFIG_SNTP_TIME_SYNC_METHOD_SMOOTH
     sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
 #endif
     sntp_init();
 }
 
-static void obtain_time(void)
+static esp_err_t obtain_time(void)
 {
     /**
      * NTP server address could be aquired via DHCP,
@@ -58,12 +59,18 @@ static void obtain_time(void)
         ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
         vTaskDelay(2000 / portTICK_PERIOD_MS);
     }
+    if (retry == retry_count) {
+        return ESP_FAIL;
+    }
+    return ESP_OK;
 }
 
-void fetch_and_store_time_in_nvs(void *args)
+esp_err_t fetch_and_store_time_in_nvs(void *args)
 {
     initialize_sntp();
-    obtain_time();
+    if (obtain_time() != ESP_OK) {
+        return ESP_FAIL;
+    }
 
     nvs_handle_t my_handle;
     esp_err_t err;
@@ -97,6 +104,7 @@ exit:
     } else {
         ESP_LOGI(TAG, "Updated time in NVS");
     }
+    return err;
 }
 
 esp_err_t update_time_from_nvs(void)
@@ -114,8 +122,12 @@ esp_err_t update_time_from_nvs(void)
 
     err = nvs_get_i64(my_handle, "timestamp", &timestamp);
     if (err == ESP_ERR_NVS_NOT_FOUND) {
-        fetch_and_store_time_in_nvs(NULL);
-        err = ESP_OK;
+        ESP_LOGI(TAG, "Time not found in NVS. Syncing time from SNTP server.");
+        if (fetch_and_store_time_in_nvs(NULL) != ESP_OK) {
+            err = ESP_FAIL;
+        } else {
+            err = ESP_OK;
+        }
     } else if (err == ESP_OK) {
         struct timeval get_nvs_time;
         get_nvs_time.tv_sec = timestamp;

+ 3 - 0
examples/protocols/https_request/pytest_https_request.py

@@ -61,6 +61,7 @@ def start_https_server(server_file: str, key_file: str, server_ip: str, server_p
 @pytest.mark.esp32s3
 @pytest.mark.ethernet
 @pytest.mark.parametrize('config', ['cli_ses_tkt',], indirect=True)
+@pytest.mark.parametrize('erase_nvs', ['y'], indirect=True)
 def test_examples_protocol_https_request_cli_session_tickets(dut: Dut) -> None:
     logging.info("Testing for \"esp_tls client session tickets\"")
 
@@ -123,6 +124,7 @@ def test_examples_protocol_https_request_cli_session_tickets(dut: Dut) -> None:
 @pytest.mark.esp32s3
 @pytest.mark.ethernet
 @pytest.mark.parametrize('config', ['ssldyn',], indirect=True)
+@pytest.mark.parametrize('erase_nvs', ['y'], indirect=True)
 def test_examples_protocol_https_request_dynamic_buffers(dut: Dut) -> None:
     # Check for connection using crt bundle with mbedtls dynamic resource enabled
     # check and log bin size
@@ -153,6 +155,7 @@ def test_examples_protocol_https_request_dynamic_buffers(dut: Dut) -> None:
 
 @pytest.mark.supported_targets
 @pytest.mark.ethernet
+@pytest.mark.parametrize('erase_nvs', ['y'], indirect=True)
 def test_examples_protocol_https_request(dut: Dut) -> None:
 
     """

+ 1 - 0
examples/protocols/https_request/sdkconfig.defaults

@@ -1 +1,2 @@
 CONFIG_MBEDTLS_HAVE_TIME_DATE=y
+CONFIG_LWIP_SNTP_MAX_SERVERS=2