1 /***************************************************************************/ /**
2  * @file
3  * @brief
4  *******************************************************************************
5  * # License
6  * <b>Copyright 2019 Silicon Laboratories Inc. www.silabs.com</b>
7  *******************************************************************************
8  *
9  * SPDX-License-Identifier: Zlib
10  *
11  * The licensor of this software is Silicon Laboratories Inc.
12  *
13  * This software is provided 'as-is', without any express or implied
14  * warranty. In no event will the authors be held liable for any damages
15  * arising from the use of this software.
16  *
17  * Permission is granted to anyone to use this software for any purpose,
18  * including commercial applications, and to alter it and redistribute it
19  * freely, subject to the following restrictions:
20  *
21  * 1. The origin of this software must not be misrepresented; you must not
22  *    claim that you wrote the original software. If you use this software
23  *    in a product, an acknowledgment in the product documentation would be
24  *    appreciated but is not required.
25  * 2. Altered source versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.
27  * 3. This notice may not be removed or altered from any source distribution.
28  *
29  ******************************************************************************/
30 #include "sl_net_types.h"
31 #include "sl_net_rsi_utility.h"
32 #include "sl_si91x_core_utilities.h"
33 #include "sl_si91x_driver.h"
34 #include "sl_si91x_constants.h"
35 
36 // SL_NET_EVENT_COUNT is assumed to be a constant representing the number of network events
37 static sl_net_event_handler_t net_event_handler = NULL;
38 
sl_si91x_register_event_handler(sl_net_event_handler_t function)39 sl_status_t sl_si91x_register_event_handler(sl_net_event_handler_t function)
40 {
41   net_event_handler = function;
42   return SL_STATUS_OK;
43 }
44 
sl_si91x_default_handler(sl_net_event_t event,sl_wifi_buffer_t * buffer)45 sl_status_t sl_si91x_default_handler(sl_net_event_t event, sl_wifi_buffer_t *buffer)
46 {
47   sl_si91x_packet_t *packet                         = sl_si91x_host_get_buffer_data(buffer, 0, NULL);
48   sl_status_t status                                = convert_and_save_firmware_status(get_si91x_frame_status(packet));
49   sl_ip_address_t ip                                = { 0 };
50   sl_net_ip_configuration_t ip_config               = { 0 };
51   const sl_si91x_rsp_ipv4_params_t *ipv4_parameters = NULL;
52   const sl_si91x_rsp_ipv6_params_t *ipv6_parameters = NULL;
53   void *data;
54 
55   // Check if there's a valid event handler registered for this event
56   if (net_event_handler == NULL) {
57     return SL_STATUS_FAIL; // If no event handler is registered, return failure
58   }
59 
60   // Depending on the event type, prepare data for the event handler
61   switch (event) {
62     case SL_NET_DNS_RESOLVE_EVENT: {
63       data = &ip;
64 
65       // Convert the SI91x DNS response to an IP address structure
66       convert_si91x_dns_response(&ip, (sl_si91x_dns_response_t *)packet->data);
67       break;
68     }
69     case SL_NET_OTA_FW_UPDATE_EVENT:
70     case SL_NET_PING_RESPONSE_EVENT:
71     case SL_NET_DHCP_NOTIFICATION_EVENT: {
72       data = &packet->data; // Use packet data directly for certain events
73       break;
74     }
75     case SL_NET_IP_ADDRESS_CHANGE_EVENT: {
76 
77       data                = &ip_config;
78       ip_config.host_name = NULL;
79       ip_config.mode      = SL_IP_MANAGEMENT_DHCP;
80 
81       if (packet->command == RSI_WLAN_RSP_IPCONFV6) {
82         ipv6_parameters = (sl_si91x_rsp_ipv6_params_t *)packet->data;
83         ip_config.type  = SL_IPV6;
84 
85         if (NULL != ipv6_parameters) {
86           memcpy(ip_config.ip.v6.link_local_address.bytes,
87                  (const uint8_t *)ipv6_parameters->link_local_address,
88                  sizeof(ipv6_parameters->link_local_address));
89           memcpy(ip_config.ip.v6.global_address.bytes,
90                  (const uint8_t *)ipv6_parameters->global_address,
91                  sizeof(ipv6_parameters->global_address));
92           memcpy(ip_config.ip.v6.gateway.bytes,
93                  (const uint8_t *)ipv6_parameters->gateway_address,
94                  sizeof(ipv6_parameters->gateway_address));
95         }
96 
97       } else {
98         ipv4_parameters = (sl_si91x_rsp_ipv4_params_t *)packet->data;
99         ip_config.type  = SL_IPV4;
100 
101         if (NULL != ipv4_parameters) {
102           memcpy(ip_config.ip.v4.ip_address.bytes, ipv4_parameters->ipaddr, sizeof(ipv4_parameters->ipaddr));
103           memcpy(ip_config.ip.v4.netmask.bytes, ipv4_parameters->netmask, sizeof(ipv4_parameters->netmask));
104           memcpy(ip_config.ip.v4.gateway.bytes, ipv4_parameters->gateway, sizeof(ipv4_parameters->gateway));
105         }
106       }
107       break;
108     }
109     default: {
110       SL_DEBUG_LOG("\r\nUnsupported event\r\n");
111       return SL_STATUS_FAIL; // Return failure for unsupported events
112     }
113   }
114   // Call the registered event handler function with event details
115   net_event_handler(event, status, data, packet->length);
116 
117   return SL_STATUS_OK;
118 }
119