Skip to content

Commit

Permalink
esp_netif/lwip: Fix core-locking config
Browse files Browse the repository at this point in the history
* Fix thread safety issues in non-core locking
* Add option to verify thread safety issues in lwip (core-lock assertion)
* Make esp_sntp.h thread safe API
* Fix sntp examples
* Fix openthread libs

Closes #9908
Closes #10502
Closes #10466
  • Loading branch information
david-cermak committed Jan 17, 2023
1 parent 9e24739 commit a71fa82
Show file tree
Hide file tree
Showing 43 changed files with 1,189 additions and 309 deletions.
2 changes: 2 additions & 0 deletions components/esp_netif/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ endif()

set(srcs_lwip
"lwip/esp_netif_lwip.c"
"lwip/esp_netif_sntp.c"
"lwip/esp_netif_lwip_defaults.c"
"lwip/netif/wlanif.c"
"lwip/netif/ethernetif.c"
Expand Down Expand Up @@ -55,3 +56,4 @@ endif()


target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
target_compile_definitions(${COMPONENT_LIB} PRIVATE ESP_NETIF_COMPONENT_BUILD)
49 changes: 49 additions & 0 deletions components/esp_netif/include/esp_netif.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,34 @@ esp_err_t esp_netif_bridge_fdb_add(esp_netif_t *esp_netif_br, uint8_t *addr, uin
esp_err_t esp_netif_bridge_fdb_remove(esp_netif_t *esp_netif_br, uint8_t *addr);
#endif // CONFIG_ESP_NETIF_BRIDGE_EN

/**
* @brief Cause the TCP/IP stack to join a IPv6 multicast group
*
* @param[in] esp_netif Handle to esp-netif instance
* @param[in] addr The multicast group to join
*
* @return
* - ESP_OK
* - ESP_ERR_ESP_NETIF_INVALID_PARAMS
* - ESP_ERR_ESP_NETIF_MLD6_FAILED
* - ESP_ERR_NO_MEM
*/
esp_err_t esp_netif_join_ip6_multicast_group(esp_netif_t *esp_netif, const esp_ip6_addr_t *addr);

/**
* @brief Cause the TCP/IP stack to leave a IPv6 multicast group
*
* @param[in] esp_netif Handle to esp-netif instance
* @param[in] addr The multicast group to leave
*
* @return
* - ESP_OK
* - ESP_ERR_ESP_NETIF_INVALID_PARAMS
* - ESP_ERR_ESP_NETIF_MLD6_FAILED
* - ESP_ERR_NO_MEM
*/
esp_err_t esp_netif_leave_ip6_multicast_group(esp_netif_t *esp_netif, const esp_ip6_addr_t *addr);

/**
* @}
*/
Expand Down Expand Up @@ -940,6 +968,27 @@ void esp_netif_netstack_buf_ref(void *netstack_buf);
*/
void esp_netif_netstack_buf_free(void *netstack_buf);

/**
* @}
*/

/** @addtogroup ESP_NETIF_TCPIP_EXEC
* @{
*/

/**
* @brief TCPIP thread safe callback used with esp_netif_tcpip_exec()
*/
typedef esp_err_t (*esp_netif_callback_fn)(void *ctx);

/**
* @brief Utility to execute the supplied callback in TCP/IP context
* @param fn Pointer to the callback
* @param ctx Parameter to the callback
* @return The error code (esp_err_t) returned by the callback
*/
esp_err_t esp_netif_tcpip_exec(esp_netif_callback_fn fn, void *ctx);

/**
* @}
*/
Expand Down
116 changes: 116 additions & 0 deletions components/esp_netif/include/esp_netif_sntp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <stdbool.h>
#include <stdint.h>
#include "freertos/FreeRTOS.h"
#include "esp_err.h"
#include "esp_netif_types.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @defgroup ESP_NETIF_SNTP_API ESP-NETIF SNTP API
* @brief SNTP API for underlying TCP/IP stack
*
*/

/** @addtogroup ESP_NETIF_SNTP_API
* @{
*/


/**
* @brief Time sync notification function
*/
typedef void (*esp_sntp_time_cb_t)(struct timeval *tv);

/**
* @brief Utility macro for providing multiple servers in parentheses
*/
#define ESP_SNTP_SERVER_LIST(...) { __VA_ARGS__ }

/**
* @brief Default configuration to init SNTP with multiple servers
* @param servers_in_list Number of servers in the list
* @param list_of_servers List of servers (use ESP_SNTP_SERVER_LIST(...))
*
*/
#define ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE(servers_in_list, list_of_servers) { \
.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 = 0, \
.index_of_first_server = 0, \
.num_of_servers = (servers_in_list), \
.servers = list_of_servers, \
}

/**
* @brief Default configuration with a single server
*/
#define ESP_NETIF_SNTP_DEFAULT_CONFIG(server) \
ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE(1, {server})

/**
* @brief SNTP configuration struct
*/
typedef struct esp_sntp_config {
bool smooth_sync; ///< set to true if smooth sync required
bool server_from_dhcp; ///< set to true to request NTP server config from DHCP
bool wait_for_sync; ///< if true, we create a semaphore to signal time sync event
bool start; ///< set to true to automatically start the SNTP service
esp_sntp_time_cb_t sync_cb; ///< optionally sets callback function on time sync event
bool renew_servers_after_new_IP; ///< this is used to refresh server list if NTP provided by DHCP (which cleans other pre-configured servers)
ip_event_t ip_event_to_renew; ///< set the IP event id on which we refresh server list (if renew_servers_after_new_IP=true)
size_t index_of_first_server; ///< refresh server list after this server (if renew_servers_after_new_IP=true)
size_t num_of_servers; ///< number of preconfigured NTP servers
const char* servers[CONFIG_LWIP_SNTP_MAX_SERVERS]; ///< list of servers
} esp_sntp_config_t;

/**
* @brief Initialize SNTP with supplied config struct
* @param config Config struct
* @return ESP_OK on success
*/
esp_err_t esp_netif_sntp_init(const esp_sntp_config_t * config);

/**
* @brief Start SNTP service
* if it wasn't started during init (config.start = false)
* or restart it if already started
* @return ESP_OK on success
*/
esp_err_t esp_netif_sntp_start(void);

/**
* @brief Deinitialize esp_netif SNTP module
*/
void esp_netif_sntp_deinit(void);

/**
* @brief Wait for time sync event
* @param tout Specified timeout in RTOS ticks
* @return ESP_TIMEOUT if sync event didn't came withing the timeout
* ESP_ERR_NOT_FINISHED if the sync event came, but we're in smooth update mode and still in progress (SNTP_SYNC_STATUS_IN_PROGRESS)
* ESP_OK if time sync'ed
*/
esp_err_t esp_netif_sntp_sync_wait(TickType_t tout);

/**
* @}
*/

#ifdef __cplusplus
}
#endif
Loading

0 comments on commit a71fa82

Please sign in to comment.