Merge branch 'fix/sntp_getreachability' into 'master'

fix(netif): Add missing SNTP get-reachablitiy API

Closes DOC-7124

See merge request espressif/esp-idf!28659
This commit is contained in:
David Čermák 2024-03-01 16:22:33 +08:00
commit 9e0ad656c9
6 changed files with 81 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -107,6 +107,17 @@ void esp_netif_sntp_deinit(void);
*/
esp_err_t esp_netif_sntp_sync_wait(TickType_t tout);
/**
* @brief Returns SNTP server's reachability shift register as described in RFC 5905.
*
* @param index Index of the SERVER
* @param reachability reachability shift register
* @return ESP_OK on success,
* ESP_ERR_INVALID_STATE if SNTP not initialized
* ESP_ERR_INVALID_ARG if invalid arguments
*/
esp_err_t esp_netif_sntp_reachability(unsigned int index, unsigned int *reachability);
/**
* @}
*/

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -178,3 +178,15 @@ esp_err_t esp_netif_sntp_start(void)
{
return esp_netif_tcpip_exec(sntp_start_api, NULL);
}
esp_err_t esp_netif_sntp_reachability(unsigned int index, unsigned int *reachability)
{
if (index >= SNTP_MAX_SERVERS || reachability == NULL) {
return ESP_ERR_INVALID_ARG;
}
if (s_storage == NULL || sntp_enabled() == 0) {
return ESP_ERR_INVALID_STATE;
}
*reachability = sntp_getreachability(index);
return ESP_OK;
}

View File

@ -57,6 +57,11 @@ TEST(esp_netif, init_and_destroy_sntp)
// Can initialize again once it's destroyed
TEST_ESP_OK(esp_netif_sntp_init(&config));
// Test the reachability API
size_t reachability = 0;
// Invalid state is expected since SNTP service didn't start
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, esp_netif_sntp_reachability(0, &reachability));
esp_netif_sntp_deinit();
}

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -243,6 +243,20 @@ const ip_addr_t* esp_sntp_getserver(u8_t idx)
return sntp_getserver(idx);
}
uint8_t esp_sntp_getreachability(uint8_t idx)
{
#if SNTP_MONITOR_SERVER_REACHABILITY
return sntp_getreachability(idx);
#endif
LWIP_ERROR("sntp_getreachability() in not enabled in lwipopts", false, );
return 0;
}
esp_sntp_operatingmode_t esp_sntp_getoperatingmode(void)
{
return (esp_sntp_operatingmode_t)sntp_getoperatingmode();
}
#if LWIP_DHCP_GET_NTP_SRV
static void do_servermode_dhcp(void* ctx)
{

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -218,6 +218,20 @@ const ip_addr_t* esp_sntp_getserver(u8_t idx);
*/
bool esp_sntp_enabled(void);
/**
* @brief Gets the server reachability shift register as described in RFC 5905.
* @param idx Index of the SNTP server
* @return reachability shift register
*/
uint8_t esp_sntp_getreachability(uint8_t idx);
/**
* @brief Get the configured operating mode
*
* @return operating mode enum
*/
esp_sntp_operatingmode_t esp_sntp_getoperatingmode(void);
#if LWIP_DHCP_GET_NTP_SRV
/**
* @brief Enable acquiring SNTP server from DHCP
@ -269,6 +283,18 @@ const ip_addr_t* sntp_getserver(u8_t idx)
return esp_sntp_getserver(idx);
}
static inline __attribute__((deprecated("use esp_sntp_getreachability() instead")))
uint8_t sntp_getreachability(uint8_t idx)
{
return esp_sntp_getreachability(idx);
}
static inline __attribute__((deprecated("use esp_sntp_getoperatingmode() instead")))
esp_sntp_operatingmode_t sntp_getoperatingmode(void)
{
return esp_sntp_getoperatingmode();
}
#endif /* ESP_LWIP_COMPONENT_BUILD */

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -294,8 +294,16 @@ void test_sntp_timestamps(int year, bool msb_flag)
localtime_r(&now, &timeinfo);
TEST_ASSERT_EQUAL(year, 1900 + timeinfo.tm_year);
// Check that the server 0 was reachable
TEST_ASSERT_EQUAL(1, esp_sntp_getreachability(0));
// close the SNTP and the fake server
esp_sntp_stop();
// Test some other SNTP APIs
TEST_ASSERT_EQUAL(0, esp_sntp_getreachability(0));
TEST_ASSERT_EQUAL(ESP_SNTP_OPMODE_POLL, esp_sntp_getoperatingmode());
const ip_addr_t *server_ip = esp_sntp_getserver(0);
TEST_ASSERT_EQUAL(PP_HTONL(IPADDR_LOOPBACK), server_ip->u_addr.ip4.addr);
close(sock);
}