Skip to content

Commit

Permalink
[Silabs] Move LwIP specific functions out of the WifiInterface (#37581)
Browse files Browse the repository at this point in the history
* Clean up WifiIntercace by moving LWIP APIs

* Finish cleanup

* Fix build

* Apply suggestions from code review
  • Loading branch information
mkardous-silabs authored Feb 21, 2025
1 parent 0d6b2fb commit 1740546
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 339 deletions.
1 change: 1 addition & 0 deletions src/platform/silabs/wifi/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ source_set("wifi-platform") {
"${silabs_platform_dir}/wifi/lwip-support/ethernetif.cpp",
"${silabs_platform_dir}/wifi/lwip-support/ethernetif.h",
"${silabs_platform_dir}/wifi/lwip-support/lwip_netif.cpp",
"${silabs_platform_dir}/wifi/lwip-support/lwip_netif.h",
]
}

Expand Down
29 changes: 1 addition & 28 deletions src/platform/silabs/wifi/SiWx/WifiInterfaceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,7 @@ void HandleDHCPPolling(void)
uint8_t dhcp_state = dhcpclient_poll(sta_netif);
if (dhcp_state == DHCP_ADDRESS_ASSIGNED && !HasNotifiedIPv4Change())
{
wfx_dhcp_got_ipv4((uint32_t) sta_netif->ip_addr.u_addr.ip4.addr);
NotifyIPv4Change(true);
GotIPv4Address((uint32_t) sta_netif->ip_addr.u_addr.ip4.addr);
event = WifiPlatformEvent::kStationDhcpDone;
PostWifiPlatformEvent(event);
NotifyConnectivity();
Expand Down Expand Up @@ -845,29 +844,3 @@ CHIP_ERROR ConfigureBroadcastFilter(bool enableBroadcastFilter)
return CHIP_NO_ERROR;
}
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER

#if CHIP_DEVICE_CONFIG_ENABLE_IPV4
/********************************************************************************************
* @fn void wfx_dhcp_got_ipv4(uint32_t ip)
* @brief
* Acquire the new ip address
* @param[in] ip: internet protocol
* @return
* None
**********************************************************************************************/
void wfx_dhcp_got_ipv4(uint32_t ip)
{
/*
* Acquire the new IP address
*/
wfx_rsi.ip4_addr[0] = (ip) &0xFF;
wfx_rsi.ip4_addr[1] = (ip >> 8) & 0xFF;
wfx_rsi.ip4_addr[2] = (ip >> 16) & 0xFF;
wfx_rsi.ip4_addr[3] = (ip >> 24) & 0xFF;
ChipLogDetail(DeviceLayer, "DHCP OK: IP=%d.%d.%d.%d", wfx_rsi.ip4_addr[0], wfx_rsi.ip4_addr[1], wfx_rsi.ip4_addr[2],
wfx_rsi.ip4_addr[3]);
/* Notify the Connectivity Manager - via the app */
wfx_rsi.dev_state.Set(WifiState::kStationDhcpDone).Set(WifiState::kStationReady);
NotifyIPv4Change(true);
}
#endif /* CHIP_DEVICE_CONFIG_ENABLE_IPV4 */
3 changes: 3 additions & 0 deletions src/platform/silabs/wifi/WifiInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ using namespace chip::DeviceLayer;

#define CONVERT_SEC_TO_MS(TimeInS) (TimeInS * 1000)

// TODO: We shouldn't need to have access to a global variable in the interface here
extern WfxRsi_t wfx_rsi;

// TODO: This is a workaround because we depend on the platform lib which depends on the platform implementation.
// As such we can't depend on the platform here as well
extern void HandleWFXSystemEvent(sl_wfx_generic_message_t * eventData);
Expand Down
39 changes: 0 additions & 39 deletions src/platform/silabs/wifi/WifiInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
#include "sl_status.h"
#include <stdbool.h>

/* LwIP includes. */
#include "lwip/ip_addr.h"
#include "lwip/netif.h"
#include "lwip/netifapi.h"
#include "lwip/tcpip.h"

#if (SLI_SI91X_MCU_INTERFACE | EXP_BOARD)
#include "rsi_common_apis.h"
#include "sl_si91x_types.h"
Expand Down Expand Up @@ -172,9 +166,6 @@ typedef struct wfx_rsi_s
uint8_t ip4_addr[4]; /* Not sure if this is enough */
} WfxRsi_t;

// TODO: We shouldn't need to have access to a global variable in the interface here
extern WfxRsi_t wfx_rsi;

/* Updated functions */

/**
Expand Down Expand Up @@ -399,34 +390,4 @@ void wfx_cancel_scan(void);
*/
void sl_matter_wifi_task_started(void);

/* Implemented for LWIP */
void wfx_lwip_set_sta_link_up(void);
void wfx_lwip_set_sta_link_down(void);
void sl_matter_lwip_start(void);
struct netif * wfx_get_netif(sl_wfx_interface_t interface);

#if CHIP_DEVICE_CONFIG_ENABLE_IPV4
void wfx_dhcp_got_ipv4(uint32_t);
#endif /* CHIP_DEVICE_CONFIG_ENABLE_IPV4 */
void wfx_retry_connection(uint16_t retryAttempt);

#ifdef RS911X_WIFI
#if !(EXP_BOARD) // for RS9116
void * wfx_rsi_alloc_pkt(void);
/* RSI for LWIP */
void wfx_rsi_pkt_add_data(void * p, uint8_t * buf, uint16_t len, uint16_t off);
int32_t wfx_rsi_send_data(void * p, uint16_t len);
#endif //!(EXP_BOARD)
#endif // RS911X_WIFI

#ifdef __cplusplus
extern "C" {
#endif

#ifdef WF200_WIFI
void sl_wfx_host_gpio_init(void);
#endif /* WF200_WIFI */

#ifdef __cplusplus
}
#endif
129 changes: 100 additions & 29 deletions src/platform/silabs/wifi/lwip-support/ethernetif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,26 @@
#include <stdlib.h>
#include <string.h>

#ifndef WF200_WIFI
#include "FreeRTOS.h"
#include "event_groups.h"
#include "task.h"
#if (SLI_SI91X_MCU_INTERFACE | EXP_BOARD)
#ifdef __cplusplus
// LwIP includes - Includes must be before the rsi headers due to redefination errors
#include "lwip/ethip6.h"
#include "lwip/timeouts.h"
#include "netif/etharp.h"

#ifdef RS911X_WIFI
extern "C" {
#endif
#include "cmsis_os2.h"
#include "sl_net.h"
#include "sl_si91x_driver.h"
#include "sl_si91x_host_interface.h"
#include "sl_si91x_types.h"
#include "sl_wifi.h"
#include "sl_wifi_callback_framework.h"
#include "sl_wifi_constants.h"
#include "sl_wifi_types.h"
#ifdef __cplusplus
#include "rsi_driver.h"
#include "rsi_pkt_mgmt.h"
}
#endif
#endif // (SLI_SI91X_MCU_INTERFACE | EXP_BOARD)
#endif // WF200_WIFI
#endif // RS911X_WIFI

#include <platform/silabs/wifi/WifiInterface.h>
#ifdef WF200_WIFI
#include "sl_wfx.h"
#endif
/* LwIP includes. */
#include "ethernetif.h"
#include "lwip/ethip6.h"
#include "lwip/timeouts.h"
#include "netif/etharp.h"

#include <lib/support/logging/CHIPLogging.h>
#include <platform/silabs/wifi/WifiInterface.h>
#include <platform/silabs/wifi/lwip-support/ethernetif.h>
#include <platform/silabs/wifi/lwip-support/lwip_netif.h>

StaticSemaphore_t xEthernetIfSemaBuffer;

Expand Down Expand Up @@ -286,7 +272,7 @@ void sl_wfx_host_received_frame_callback(sl_wfx_received_ind_t * rx_buffer)
{

/* Send received frame to station interface */
if ((netif = wfx_get_netif(SL_WFX_STA_INTERFACE)) != NULL)
if ((netif = chip::DeviceLayer::Silabs::Lwip::GetNetworkInterface(SL_WFX_STA_INTERFACE)) != NULL)
{
uint8_t * buffer;
uint16_t len;
Expand Down Expand Up @@ -316,7 +302,92 @@ void sl_wfx_host_received_frame_callback(sl_wfx_received_ind_t * rx_buffer)
}

#else /* For RS911x - using LWIP */

static SemaphoreHandle_t ethout_sem;

/**
* @brief Allocates packets for the data about to be sent
*
* TODO: Validate if this warning still applied and fix the associated code
* WARNING - Taken from RSI and broken up
* This is my own RSI stuff for not copying code and allocating an extra
* level of indirection - when using LWIP buffers
* see also: int32_t rsi_wlan_send_data_xx(uint8_t *buffer, uint32_t length)
*
* @return void* pointer to the allocated packet buffer
*/
static void * wfx_rsi_alloc_pkt(void)
{
rsi_pkt_t * pkt;

// Allocate packet to send data
if ((pkt = rsi_pkt_alloc(&rsi_driver_cb->wlan_cb->wlan_tx_pool)) == NULL)
{
return (void *) 0;
}

return (void *) pkt;
}

/**
* @brief Adds the buffer data to the allocated packet.
* the packet must be allocated before adding data to it.
*
* @param[in,out] p pointer to the allocated packet
* @param[in] buf pointer to the data buffer to copy to the packet
* @param[in] len length of the data buffer
* @param[in] off the offset at which to put the data in the packet
*/
void wfx_rsi_pkt_add_data(void * p, uint8_t * buf, uint16_t len, uint16_t off)
{
rsi_pkt_t * pkt;

pkt = (rsi_pkt_t *) p;
memcpy(((char *) pkt->data) + off, buf, len);
}

/**
* @brief Triggers the packet to sent on the wire
*
* @param p pointer to the packet to send
* @param len length of the packet to send
*
* @return int32_t RSI_ERROR_NONE, if the packet was succesfully sent out
* RSI_ERROR_RESPONSE_TIMEOUT, if we are unable to acquire the semaphore of the status
* other error (< 0) if we were unable to send out the the packet
*/
int32_t wfx_rsi_send_data(void * p, uint16_t len)
{
int32_t status;
uint8_t * host_desc;
rsi_pkt_t * pkt;

pkt = (rsi_pkt_t *) p;
host_desc = pkt->desc;
memset(host_desc, 0, RSI_HOST_DESC_LENGTH);
rsi_uint16_to_2bytes(host_desc, (len & 0xFFF));

// Fill packet type
host_desc[1] |= (RSI_WLAN_DATA_Q << 4);
host_desc[2] |= 0x01;

rsi_enqueue_pkt(&rsi_driver_cb->wlan_tx_q, pkt);

#ifndef RSI_SEND_SEM_BITMAP
rsi_driver_cb_non_rom->send_wait_bitmap |= BIT(0);
#endif
// Set TX packet pending event
rsi_set_event(RSI_TX_EVENT);

if (rsi_wait_on_wlan_semaphore(&rsi_driver_cb_non_rom->send_data_sem, RSI_SEND_DATA_RESPONSE_WAIT_TIME) != RSI_ERROR_NONE)
{
return RSI_ERROR_RESPONSE_TIMEOUT;
}
status = rsi_wlan_get_status();

return status;
}

/*****************************************************************************
* @fn static err_t low_level_output(struct netif *netif, struct pbuf *p)
* @brief
Expand Down Expand Up @@ -441,7 +512,7 @@ sl_status_t sl_si91x_host_process_data_frame(sl_wifi_interface_t interface, sl_w
/* get the network interface for STATION interface,
* and forward the received frame buffer to LWIP
*/
if ((ifp = wfx_get_netif(SL_WFX_STA_INTERFACE)) != (struct netif *) 0)
if ((ifp = GetNetworkInterface(SL_WFX_STA_INTERFACE)) != (struct netif *) 0)
{
low_level_input(ifp, rsi_pkt->data, rsi_pkt->length);
}
Expand All @@ -465,7 +536,7 @@ void wfx_host_received_sta_frame_cb(uint8_t * buf, int len)
/* get the network interface for STATION interface,
* and forward the received frame buffer to LWIP
*/
if ((ifp = wfx_get_netif(SL_WFX_STA_INTERFACE)) != (struct netif *) 0)
if ((ifp = chip::DeviceLayer::Silabs::Lwip::GetNetworkInterface(SL_WFX_STA_INTERFACE)) != (struct netif *) 0)
{
low_level_input(ifp, buf, len);
}
Expand Down
Loading

0 comments on commit 1740546

Please sign in to comment.