1010#include "siwx917_wifi.h"
1111#include "siwx917_wifi_socket.h"
1212
13+ #include "sl_rsi_utility.h"
1314#include "sl_net_constants.h"
1415#include "sl_wifi_types.h"
1516#include "sl_wifi_callback_framework.h"
1819
1920LOG_MODULE_REGISTER (siwx917_wifi );
2021
22+ NET_BUF_POOL_FIXED_DEFINE (siwx917_tx_pool , 1 , NET_ETH_MTU , 0 , NULL );
23+
2124static unsigned int siwx917_on_join (sl_wifi_event_t event ,
2225 char * result , uint32_t result_size , void * arg )
2326{
@@ -32,6 +35,9 @@ static unsigned int siwx917_on_join(sl_wifi_event_t event,
3235
3336 wifi_mgmt_raise_connect_result_event (sidev -> iface , WIFI_STATUS_CONN_SUCCESS );
3437 sidev -> state = WIFI_STATE_COMPLETED ;
38+ #ifndef CONFIG_WIFI_SIWX917_NET_STACK_OFFLOAD
39+ net_eth_carrier_on (sidev -> iface );
40+ #endif
3541
3642 siwx917_on_join_ipv4 (sidev );
3743 siwx917_on_join_ipv6 (sidev );
@@ -124,6 +130,9 @@ static int siwx917_disconnect(const struct device *dev)
124130 if (ret ) {
125131 return - EIO ;
126132 }
133+ #ifndef CONFIG_WIFI_SIWX917_NET_STACK_OFFLOAD
134+ net_eth_carrier_off (sidev -> iface );
135+ #endif
127136 sidev -> state = WIFI_STATE_INACTIVE ;
128137 return 0 ;
129138}
@@ -220,6 +229,83 @@ static int siwx917_status(const struct device *dev, struct wifi_iface_status *st
220229 return 0 ;
221230}
222231
232+ #ifndef CONFIG_WIFI_SIWX917_NET_STACK_OFFLOAD
233+
234+ static int siwx917_send (const struct device * dev , struct net_pkt * pkt )
235+ {
236+ size_t pkt_len = net_pkt_get_len (pkt );
237+ struct net_buf * buf = NULL ;
238+ int ret ;
239+
240+ if (net_pkt_get_len (pkt ) > NET_ETH_MTU ) {
241+ LOG_ERR ("unexpected buffer size" );
242+ return - ENOBUFS ;
243+ }
244+ buf = net_buf_alloc (& siwx917_tx_pool , K_FOREVER );
245+ if (!buf ) {
246+ return - ENOBUFS ;
247+ }
248+ if (net_pkt_read (pkt , buf -> data , pkt_len )) {
249+ net_buf_unref (buf );
250+ return - ENOBUFS ;
251+ }
252+ net_buf_add (buf , pkt_len );
253+
254+ ret = sl_wifi_send_raw_data_frame (SL_WIFI_CLIENT_INTERFACE , buf -> data , pkt_len );
255+ if (ret ) {
256+ return - EIO ;
257+ }
258+
259+ net_pkt_unref (pkt );
260+ net_buf_unref (buf );
261+
262+ return 0 ;
263+ }
264+
265+ /* Receive callback. Keep the name as it is declared weak in WiseConnect */
266+ sl_status_t sl_si91x_host_process_data_frame (sl_wifi_interface_t interface ,
267+ sl_wifi_buffer_t * buffer )
268+ {
269+ sl_si91x_packet_t * si_pkt = sl_si91x_host_get_buffer_data (buffer , 0 , NULL );
270+ struct net_if * iface = net_if_get_default ();
271+ struct net_pkt * pkt ;
272+ int ret ;
273+
274+ pkt = net_pkt_rx_alloc_with_buffer (iface , buffer -> length , AF_UNSPEC , 0 , K_NO_WAIT );
275+ if (!pkt ) {
276+ LOG_ERR ("net_pkt_rx_alloc_with_buffer() failed" );
277+ return SL_STATUS_FAIL ;
278+ }
279+ ret = net_pkt_write (pkt , si_pkt -> data , si_pkt -> length );
280+ if (ret < 0 ) {
281+ LOG_ERR ("net_pkt_write(): %d" , ret );
282+ goto unref ;
283+ }
284+ ret = net_recv_data (iface , pkt );
285+ if (ret < 0 ) {
286+ LOG_ERR ("net_recv_data((): %d" , ret );
287+ goto unref ;
288+ }
289+ return 0 ;
290+
291+ unref :
292+ net_pkt_unref (pkt );
293+ return SL_STATUS_FAIL ;
294+ }
295+
296+ #endif
297+
298+ static void siwx917_ethernet_init (struct net_if * iface )
299+ {
300+ #ifndef CONFIG_WIFI_SIWX917_NET_STACK_OFFLOAD
301+ struct ethernet_context * eth_ctx ;
302+
303+ eth_ctx = net_if_l2_data (iface );
304+ eth_ctx -> eth_if_type = L2_ETH_IF_TYPE_WIFI ;
305+ ethernet_init (iface );
306+ #endif
307+ }
308+
223309static void siwx917_iface_init (struct net_if * iface )
224310{
225311 struct siwx917_dev * sidev = iface -> if_dev -> dev -> data ;
@@ -228,7 +314,6 @@ static void siwx917_iface_init(struct net_if *iface)
228314 sidev -> state = WIFI_STATE_INTERFACE_DISABLED ;
229315 sidev -> iface = iface ;
230316
231- siwx917_sock_init (iface );
232317 sl_wifi_set_scan_callback (siwx917_on_scan , sidev );
233318 sl_wifi_set_join_callback (siwx917_on_join , sidev );
234319
@@ -239,6 +324,8 @@ static void siwx917_iface_init(struct net_if *iface)
239324 }
240325 net_if_set_link_addr (iface , sidev -> macaddr .octet , sizeof (sidev -> macaddr .octet ),
241326 NET_LINK_ETHERNET );
327+ siwx917_sock_init (iface );
328+ siwx917_ethernet_init (iface );
242329
243330 sidev -> state = WIFI_STATE_INACTIVE ;
244331}
@@ -257,10 +344,19 @@ static const struct wifi_mgmt_ops siwx917_mgmt = {
257344
258345static const struct net_wifi_mgmt_offload siwx917_api = {
259346 .wifi_iface .iface_api .init = siwx917_iface_init ,
347+ #ifdef CONFIG_WIFI_SIWX917_NET_STACK_OFFLOAD
260348 .wifi_iface .get_type = siwx917_get_type ,
349+ #else
350+ .wifi_iface .send = siwx917_send ,
351+ #endif
261352 .wifi_mgmt_api = & siwx917_mgmt ,
262353};
263354
264355static struct siwx917_dev siwx917_dev ;
356+ #ifdef CONFIG_WIFI_SIWX917_NET_STACK_OFFLOAD
265357NET_DEVICE_DT_INST_OFFLOAD_DEFINE (0 , siwx917_dev_init , NULL , & siwx917_dev , NULL ,
266358 CONFIG_WIFI_INIT_PRIORITY , & siwx917_api , NET_ETH_MTU );
359+ #else
360+ ETH_NET_DEVICE_DT_INST_DEFINE (0 , siwx917_dev_init , NULL , & siwx917_dev , NULL ,
361+ CONFIG_WIFI_INIT_PRIORITY , & siwx917_api , NET_ETH_MTU );
362+ #endif
0 commit comments