Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_https_request_example' into 'master'
Browse files Browse the repository at this point in the history
example: Updated https_request example to fail when time is not synced.

See merge request espressif/esp-idf!19701
  • Loading branch information
mahavirj committed Sep 21, 2022
2 parents 9657e68 + c351548 commit 13a34af
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions 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
*/
Expand All @@ -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
}
Expand Down
26 changes: 19 additions & 7 deletions 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
*/
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -97,6 +104,7 @@ void fetch_and_store_time_in_nvs(void *args)
} else {
ESP_LOGI(TAG, "Updated time in NVS");
}
return err;
}

esp_err_t update_time_from_nvs(void)
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions examples/protocols/https_request/pytest_https_request.py
Expand Up @@ -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\"")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:

"""
Expand Down
1 change: 1 addition & 0 deletions examples/protocols/https_request/sdkconfig.defaults
@@ -1 +1,2 @@
CONFIG_MBEDTLS_HAVE_TIME_DATE=y
CONFIG_LWIP_SNTP_MAX_SERVERS=2

0 comments on commit 13a34af

Please sign in to comment.