Skip to content

Commit

Permalink
SNTP fixes for IDF 5.1.3 build (#2925)
Browse files Browse the repository at this point in the history
Co-authored-by: nfbot <nanoframework@outlook.com>
  • Loading branch information
AdrianSoundy and nfbot authored May 10, 2024
1 parent a2ffcea commit 62b69e5
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 44 deletions.
1 change: 1 addition & 0 deletions targets/ESP32/_IDF/sdkconfig.default.esp32c3
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ CONFIG_LWIP_DHCP_OPTIONS_LEN=80
CONFIG_LWIP_DHCPS=n
CONFIG_LWIP_IPV6=n
CONFIG_LWIP_MAX_LISTENING_TCP=8
CONFIG_LWIP_SNTP_MAX_SERVERS=2

CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC=y
CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH=y
Expand Down
2 changes: 1 addition & 1 deletion targets/ESP32/_IDF/sdkconfig.default.esp32s2
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ CONFIG_LWIP_MAX_RAW_PCBS=16
#
# SNTP
#
CONFIG_LWIP_SNTP_MAX_SERVERS=1
CONFIG_LWIP_SNTP_MAX_SERVERS=2
# CONFIG_LWIP_DHCP_GET_NTP_SRV is not set
CONFIG_LWIP_SNTP_UPDATE_DELAY=3600000
# end of SNTP
Expand Down
1 change: 1 addition & 0 deletions targets/ESP32/_IDF/sdkconfig.default.esp32s3
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ CONFIG_LWIP_DHCP_OPTIONS_LEN=80
CONFIG_LWIP_DHCPS=n
CONFIG_LWIP_IPV6=n
CONFIG_LWIP_MAX_LISTENING_TCP=8
CONFIG_LWIP_SNTP_MAX_SERVERS=2

CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC=y
CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH=y
Expand Down
1 change: 1 addition & 0 deletions targets/ESP32/_IDF/sdkconfig.default_ble.esp32s3
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ CONFIG_LWIP_DHCP_OPTIONS_LEN=80
CONFIG_LWIP_DHCPS=n
CONFIG_LWIP_IPV6=n
CONFIG_LWIP_MAX_LISTENING_TCP=8
CONFIG_LWIP_SNTP_MAX_SERVERS=2

CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC=y
CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH=y
Expand Down
1 change: 1 addition & 0 deletions targets/ESP32/_IDF/sdkconfig.default_ble_rev3.esp32
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ CONFIG_LWIP_DHCPS=n
CONFIG_LWIP_IPV6=n
CONFIG_LWIP_MAX_ACTIVE_TCP=10
CONFIG_LWIP_MAX_LISTENING_TCP=5
CONFIG_LWIP_SNTP_MAX_SERVERS=2

CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC=y
CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH=y
Expand Down
1 change: 1 addition & 0 deletions targets/ESP32/_IDF/sdkconfig.default_nopsram_ble.esp32
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ CONFIG_LWIP_DHCP_OPTIONS_LEN=80
CONFIG_LWIP_DHCPS=n
CONFIG_LWIP_IPV6=n
CONFIG_LWIP_MAX_LISTENING_TCP=8
CONFIG_LWIP_SNTP_MAX_SERVERS=2

CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC=y
CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH=y
Expand Down
1 change: 1 addition & 0 deletions targets/ESP32/_IDF/sdkconfig.default_nopsram_rev3.esp32
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ CONFIG_LWIP_DHCP_OPTIONS_LEN=80
CONFIG_LWIP_DHCPS=n
CONFIG_LWIP_IPV6=n
CONFIG_LWIP_MAX_LISTENING_TCP=8
CONFIG_LWIP_SNTP_MAX_SERVERS=2

CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC=y
CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH=y
Expand Down
1 change: 1 addition & 0 deletions targets/ESP32/_IDF/sdkconfig.default_pico
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ CONFIG_LWIP_DHCP_OPTIONS_LEN=80
CONFIG_LWIP_DHCPS=n
CONFIG_LWIP_IPV6=n
CONFIG_LWIP_MAX_LISTENING_TCP=8
CONFIG_LWIP_SNTP_MAX_SERVERS=2

CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC=y
CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH=y
Expand Down
1 change: 1 addition & 0 deletions targets/ESP32/_IDF/sdkconfig.default_pico_ble_rev3
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ CONFIG_LWIP_DHCP_OPTIONS_LEN=80
CONFIG_LWIP_DHCPS=n
CONFIG_LWIP_IPV6=n
CONFIG_LWIP_MAX_LISTENING_TCP=8
CONFIG_LWIP_SNTP_MAX_SERVERS=2

CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC=y
CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH=y
Expand Down
1 change: 1 addition & 0 deletions targets/ESP32/_IDF/sdkconfig.default_rev3.esp32c3
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ CONFIG_LWIP_DHCP_OPTIONS_LEN=80
CONFIG_LWIP_DHCPS=n
CONFIG_LWIP_IPV6=n
CONFIG_LWIP_MAX_LISTENING_TCP=8
CONFIG_LWIP_SNTP_MAX_SERVERS=2

CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC=y
CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH=y
Expand Down
9 changes: 9 additions & 0 deletions targets/ESP32/_IDF/sdkconfig.default_rev3_ipv6.esp32
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,35 @@ CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="targets/ESP32/_IDF/${TARGET_SERIES_SHORT}/partitions_nanoclr_4mb.csv"
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE=y

CONFIG_ADC_SUPPRESS_DEPRECATE_WARN=y
CONFIG_ADC_CALI_SUPPRESS_DEPRECATE_WARN=y
CONFIG_UART_ISR_IN_IRAM=y
CONFIG_PCNT_SUPPRESS_DEPRECATE_WARN=y
CONFIG_RMT_SUPPRESS_DEPRECATE_WARN=y
CONFIG_I2S_SUPPRESS_DEPRECATE_WARN=y
CONFIG_DAC_SUPPRESS_DEPRECATE_WARN=y

CONFIG_ETH_SPI_ETHERNET_DM9051=y
CONFIG_ETH_SPI_ETHERNET_W5500=y
CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL=y

CONFIG_ESP32_REV_MIN_3=y

CONFIG_PM_ENABLE=y
CONFIG_SPIRAM=y
CONFIG_SPIRAM_IGNORE_NOTFOUND=y

CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y
CONFIG_ESP_TASK_WDT_INIT=n
CONFIG_ESP_WIFI_TASK_PINNED_TO_CORE_1=y

CONFIG_FATFS_LFN_HEAP=y

CONFIG_FREERTOS_IDLE_TASK_STACKSIZE=2304
CONFIG_FREERTOS_TIMER_TASK_PRIORITY=5
CONFIG_FREERTOS_USE_TICKLESS_IDLE=y

CONFIG_LWIP_LOCAL_HOSTNAME="nanodevice"
CONFIG_LWIP_MAX_SOCKETS=16
CONFIG_LWIP_SO_LINGER=y
Expand All @@ -36,6 +44,7 @@ CONFIG_LWIP_DHCPS=n
CONFIG_LWIP_IPV6=y
CONFIG_LWIP_MAX_LISTENING_TCP=8
CONFIG_LWIP_SNTP_MAX_SERVERS=2

CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC=y
CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH=y
CONFIG_MBEDTLS_CMAC_C=y
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ CONFIG_LWIP_DHCP_OPTIONS_LEN=80
CONFIG_LWIP_DHCPS=n
CONFIG_LWIP_IPV6=n
CONFIG_LWIP_MAX_LISTENING_TCP=8
CONFIG_LWIP_SNTP_MAX_SERVERS=2

CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC=y
CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH=y
Expand Down
92 changes: 49 additions & 43 deletions targets/ESP32/_common/targetHAL_Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
#endif

extern "C" void set_signal_sock_function(void (*funcPtr)());
extern esp_netif_t * WifiStationEspNetif;
extern esp_netif_t *WifiStationEspNetif;

#define WIFI_EVENT_TYPE_SCAN_COMPLETE 1

//#define PRINT_NET_EVENT 1
// #define PRINT_NET_EVENT 1

// buffer with host name
char hostName[18] = "nanodevice_";
Expand Down Expand Up @@ -98,23 +98,25 @@ static void PostThreadEvent(OpenThreadEventType event, uint16_t data1, uint32_t

static void initialize_sntp()
{
esp_sntp_config_t config = {
.smooth_sync = false,
.server_from_dhcp = false,
.wait_for_sync = true,
.start = true,
.sync_cb = NULL,
.renew_servers_after_new_IP = false,
.ip_event_to_renew = (ip_event_t)0,
.index_of_first_server = 0,
.num_of_servers = 2
};
esp_sntp_config_t config = {
.smooth_sync = false,
.server_from_dhcp = false,
.wait_for_sync = true,
.start = true,
.sync_cb = NULL,
.renew_servers_after_new_IP = false,
.ip_event_to_renew = (ip_event_t)0,
.index_of_first_server = 0,
.num_of_servers = CONFIG_LWIP_SNTP_MAX_SERVERS};

config.servers[0] = SNTP_SERVER0_DEFAULT_ADDRESS;

#if (CONFIG_LWIP_SNTP_MAX_SERVERS > 1)
config.servers[1] = SNTP_SERVER1_DEFAULT_ADDRESS;
#endif

esp_netif_sntp_init(&config);
}
}

static void compose_esp32_hostname()
{
Expand Down Expand Up @@ -150,7 +152,7 @@ static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_
ets_printf("Event %d, ID: %d\n", event_base, event_id);
#endif

#if defined(CONFIG_SOC_WIFI_SUPPORTED)
#if defined(CONFIG_SOC_WIFI_SUPPORTED)
if (event_base == WIFI_EVENT)
{
switch (event_id)
Expand Down Expand Up @@ -183,8 +185,8 @@ static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_
#if LWIP_IPV6
{
// Note: Did use esp_netif_create_ip6_linklocal originally but this failed because
// it tests for Netif to be up and didn't seem to be. Calling netif_create_ip6_linklocal_address directly
// seems to work fine.
// it tests for Netif to be up and didn't seem to be. Calling netif_create_ip6_linklocal_address
// directly seems to work fine.
struct netif *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF")->lwip_netif;
netif_create_ip6_linklocal_address(netif, 1);
}
Expand Down Expand Up @@ -301,9 +303,9 @@ static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_
break;
}
}
else
else
#endif
if (event_base == IP_EVENT)
if (event_base == IP_EVENT)
{
switch (event_id)
{
Expand Down Expand Up @@ -390,22 +392,22 @@ static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_
#if HAL_USE_THREAD == TRUE
static void thread_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
// Possible events
// OPENTHREAD_EVENT_START,
// OPENTHREAD_EVENT_STOP,
// OPENTHREAD_EVENT_DETACHED,
// OPENTHREAD_EVENT_ATTACHED,
// OPENTHREAD_EVENT_ROLE_CHANGED,
// OPENTHREAD_EVENT_IF_UP,
// OPENTHREAD_EVENT_IF_DOWN,
// OPENTHREAD_EVENT_GOT_IP6,
// OPENTHREAD_EVENT_LOST_IP6,
// OPENTHREAD_EVENT_MULTICAST_GROUP_JOIN,
// OPENTHREAD_EVENT_MULTICAST_GROUP_LEAVE,
// OPENTHREAD_EVENT_TREL_ADD_IP6,
// OPENTHREAD_EVENT_TREL_REMOVE_IP6,
// Possible events
// OPENTHREAD_EVENT_START,
// OPENTHREAD_EVENT_STOP,
// OPENTHREAD_EVENT_DETACHED,
// OPENTHREAD_EVENT_ATTACHED,
// OPENTHREAD_EVENT_ROLE_CHANGED,
// OPENTHREAD_EVENT_IF_UP,
// OPENTHREAD_EVENT_IF_DOWN,
// OPENTHREAD_EVENT_GOT_IP6,
// OPENTHREAD_EVENT_LOST_IP6,
// OPENTHREAD_EVENT_MULTICAST_GROUP_JOIN,
// OPENTHREAD_EVENT_MULTICAST_GROUP_LEAVE,
// OPENTHREAD_EVENT_TREL_ADD_IP6,
// OPENTHREAD_EVENT_TREL_REMOVE_IP6,
// OPENTHREAD_EVENT_TREL_MULTICAST_GROUP_JOIN,
// OPENTHREAD_EVENT_SET_DNS_SERVER,
// OPENTHREAD_EVENT_SET_DNS_SERVER,

switch (event_id)
{
Expand Down Expand Up @@ -470,20 +472,23 @@ static void thread_event_handler(void *arg, esp_event_base_t event_base, int32_t

case OPENTHREAD_EVENT_ROLE_CHANGED:
#ifdef PRINT_NET_EVENT
{
esp_openthread_role_changed_event_t * optevent = (esp_openthread_role_changed_event_t *)event_data;
ets_printf("OPENTHREAD_EVENT_ROLE_CHANGED %d->%d\n", optevent->previous_role, optevent->current_role);
{
esp_openthread_role_changed_event_t *optevent = (esp_openthread_role_changed_event_t *)event_data;
ets_printf("OPENTHREAD_EVENT_ROLE_CHANGED %d->%d\n", optevent->previous_role, optevent->current_role);

PostThreadEvent(OpenThreadEventType_RoleChanged, 0, (optevent->previous_role << 8) + optevent->current_role);
}
PostThreadEvent(
OpenThreadEventType_RoleChanged,
0,
(optevent->previous_role << 8) + optevent->current_role);
}
#endif
break;
break;

default:
#ifdef PRINT_NET_EVENT
ets_printf("Thread Event %d, ID: %d\n", event_base, event_id);
#endif
break;
break;
}
}
#endif
Expand All @@ -502,7 +507,7 @@ void nanoHAL_Network_Initialize()
// create the default event loop
ESP_ERROR_CHECK(esp_event_loop_create_default());

#if defined(CONFIG_SOC_WIFI_SUPPORTED)
#if defined(CONFIG_SOC_WIFI_SUPPORTED)
// register the handler for WIFI events
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL));
#endif
Expand All @@ -517,7 +522,8 @@ void nanoHAL_Network_Initialize()

#if HAL_USE_THREAD == TRUE
// register the event handler for OpenThread events
ESP_ERROR_CHECK(esp_event_handler_instance_register(OPENTHREAD_EVENT, ESP_EVENT_ANY_ID, &thread_event_handler, NULL, NULL));
ESP_ERROR_CHECK(
esp_event_handler_instance_register(OPENTHREAD_EVENT, ESP_EVENT_ANY_ID, &thread_event_handler, NULL, NULL));
#endif
}

Expand Down

0 comments on commit 62b69e5

Please sign in to comment.