1 /***************************************************************************/ /**
2  * @file  sl_net.c
3  *******************************************************************************
4  * # License
5  * <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b>
6  *******************************************************************************
7  *
8  * SPDX-License-Identifier: Zlib
9  *
10  * The licensor of this software is Silicon Laboratories Inc.
11  *
12  * This software is provided 'as-is', without any express or implied
13  * warranty. In no event will the authors be held liable for any damages
14  * arising from the use of this software.
15  *
16  * Permission is granted to anyone to use this software for any purpose,
17  * including commercial applications, and to alter it and redistribute it
18  * freely, subject to the following restrictions:
19  *
20  * 1. The origin of this software must not be misrepresented; you must not
21  *    claim that you wrote the original software. If you use this software
22  *    in a product, an acknowledgment in the product documentation would be
23  *    appreciated but is not required.
24  * 2. Altered source versions must be plainly marked as such, and must not be
25  *    misrepresented as being the original software.
26  * 3. This notice may not be removed or altered from any source distribution.
27  *
28  ******************************************************************************/
29 
30 #include "sl_net.h"
31 #include "sl_net_constants.h"
32 #include "sl_wifi_device.h"
33 #include "sl_net_default_values.h"
34 
35 #ifndef NETWORK_INTERFACE_VALID
36 #error Need to define NETWORK_INTERFACE_VALID in sl_board_configuration.h
37 #endif
38 
39 #if NETWORK_INTERFACE_VALID(SL_NET_WIFI_CLIENT_INTERFACE) || NETWORK_INTERFACE_VALID(SL_NET_WIFI_AP_INTERFACE)
40 #include "sl_wifi_device.h"
41 #endif
42 
sl_net_init(sl_net_interface_t interface,const void * configuration,void * network_context,sl_net_event_handler_t event_handler)43 sl_status_t sl_net_init(sl_net_interface_t interface,
44                         const void *configuration,
45                         void *network_context,
46                         sl_net_event_handler_t event_handler)
47 {
48   switch (SL_NET_INTERFACE_TYPE(interface)) {
49 #if NETWORK_INTERFACE_VALID(SL_NET_WIFI_CLIENT_INTERFACE)
50     case SL_NET_WIFI_CLIENT_INTERFACE:
51       if (configuration == NULL) {
52         configuration = (const void *)&sl_wifi_default_client_configuration;
53       }
54 
55       sl_net_set_credential(SL_NET_DEFAULT_WIFI_CLIENT_CREDENTIAL_ID,
56                             default_wifi_client_credential.type,
57                             (const void *)default_wifi_client_credential.data,
58                             default_wifi_client_credential.data_length);
59       sl_net_set_profile(SL_NET_WIFI_CLIENT_INTERFACE,
60                          SL_NET_DEFAULT_WIFI_CLIENT_PROFILE_ID,
61                          &DEFAULT_WIFI_CLIENT_PROFILE);
62       return sl_net_wifi_client_init(interface, configuration, network_context, event_handler);
63 #endif
64 #if NETWORK_INTERFACE_VALID(SL_NET_WIFI_AP_INTERFACE)
65     case SL_NET_WIFI_AP_INTERFACE:
66       if (configuration == NULL) {
67         configuration = (const void *)&sl_wifi_default_ap_configuration;
68       }
69 
70       sl_net_set_credential(SL_NET_DEFAULT_WIFI_AP_CREDENTIAL_ID,
71                             default_wifi_ap_credential.type,
72                             (const void *)default_wifi_ap_credential.data,
73                             default_wifi_ap_credential.data_length);
74       sl_net_set_profile(SL_NET_WIFI_AP_INTERFACE,
75                          SL_NET_DEFAULT_WIFI_AP_PROFILE_ID,
76                          &DEFAULT_WIFI_ACCESS_POINT_PROFILE);
77       return sl_net_wifi_ap_init(interface, configuration, network_context, event_handler);
78 #endif
79 #if NETWORK_INTERFACE_VALID(SL_NET_ETHERNET_INTERFACE)
80     case SL_NET_ETHERNET_INTERFACE:
81       return sl_net_ethernet_init(interface, configuration, network_context, event_handler);
82       break;
83 #endif
84 #if NETWORK_INTERFACE_VALID(SL_NET_THREAD_INTERFACE)
85     case SL_NET_THREAD_INTERFACE:
86       return sl_net_thread_init(interface, configuration, network_context, event_handler);
87       break;
88 #endif
89     default:
90       return SL_STATUS_NOT_SUPPORTED;
91   }
92 }
93 
sl_net_deinit(sl_net_interface_t interface)94 sl_status_t sl_net_deinit(sl_net_interface_t interface)
95 {
96   switch (SL_NET_INTERFACE_TYPE(interface)) {
97 #if NETWORK_INTERFACE_VALID(SL_NET_WIFI_CLIENT_INTERFACE)
98     case SL_NET_WIFI_CLIENT_INTERFACE:
99       return sl_net_wifi_client_deinit(interface);
100 #endif
101 #if NETWORK_INTERFACE_VALID(SL_NET_WIFI_AP_INTERFACE)
102     case SL_NET_WIFI_AP_INTERFACE:
103       return sl_net_wifi_ap_deinit(interface);
104 #endif
105 #if NETWORK_INTERFACE_VALID(SL_NET_ETHERNET_INTERFACE)
106     case SL_NET_ETHERNET_INTERFACE:
107       return sl_net_ethernet_deinit(interface);
108       break;
109 #endif
110 #if NETWORK_INTERFACE_VALID(SL_NET_THREAD_INTERFACE)
111     case SL_NET_THREAD_INTERFACE:
112       return sl_net_thread_deinit(interface);
113       break;
114 #endif
115     default:
116       return SL_STATUS_NOT_SUPPORTED;
117   }
118 }
119 
sl_net_up(sl_net_interface_t interface,sl_net_profile_id_t profile_id)120 sl_status_t sl_net_up(sl_net_interface_t interface, sl_net_profile_id_t profile_id)
121 {
122   switch (SL_NET_INTERFACE_TYPE(interface)) {
123 #if NETWORK_INTERFACE_VALID(SL_NET_WIFI_CLIENT_INTERFACE)
124     case SL_NET_WIFI_CLIENT_INTERFACE:
125       return sl_net_wifi_client_up(interface, profile_id);
126 #endif
127 #if NETWORK_INTERFACE_VALID(SL_NET_WIFI_AP_INTERFACE)
128     case SL_NET_WIFI_AP_INTERFACE:
129       return sl_net_wifi_ap_up(interface, profile_id);
130 #endif
131 #if NETWORK_INTERFACE_VALID(SL_NET_ETHERNET_INTERFACE)
132     case SL_NET_ETHERNET_INTERFACE:
133       return sl_net_ethernet_up(interface, profile_id);
134       break;
135 #endif
136 #if NETWORK_INTERFACE_VALID(SL_NET_THREAD_INTERFACE)
137     case SL_NET_THREAD_INTERFACE:
138       return sl_net_thread_up(interface, profile_id);
139       break;
140 #endif
141     default:
142       return SL_STATUS_NOT_SUPPORTED;
143   }
144 }
145 
sl_net_down(sl_net_interface_t interface)146 sl_status_t sl_net_down(sl_net_interface_t interface)
147 {
148   switch (SL_NET_INTERFACE_TYPE(interface)) {
149 #if NETWORK_INTERFACE_VALID(SL_NET_WIFI_CLIENT_INTERFACE)
150     case SL_NET_WIFI_CLIENT_INTERFACE:
151       return sl_net_wifi_client_down(interface);
152 #endif
153 #if NETWORK_INTERFACE_VALID(SL_NET_WIFI_AP_INTERFACE)
154     case SL_NET_WIFI_AP_INTERFACE:
155       return sl_net_wifi_ap_down(interface);
156 #endif
157 #if NETWORK_INTERFACE_VALID(SL_NET_ETHERNET_INTERFACE)
158     case SL_NET_ETHERNET_INTERFACE:
159       return sl_net_ethernet_down(interface);
160       break;
161 #endif
162 #if NETWORK_INTERFACE_VALID(SL_NET_THREAD_INTERFACE)
163     case SL_NET_THREAD_INTERFACE:
164       return sl_net_thread_down(interface);
165       break;
166 #endif
167     default:
168       return SL_STATUS_NOT_SUPPORTED;
169   }
170 }
171 
sl_net_inet_addr(const char * addr,uint32_t * value)172 sl_status_t sl_net_inet_addr(const char *addr, uint32_t *value)
173 {
174   uint8_t ip_bytes[5] = { 0 };
175   int i;
176   int digits;
177   int j;
178 
179   if ((NULL == addr) || (NULL == value)) {
180     return SL_STATUS_INVALID_PARAMETER;
181   }
182 
183   digits = 0;
184 
185   // Iterate through the characters in the IP address string
186   for (i = 0, j = 0; 0 != addr[i]; i++) {
187     if ('.' == addr[i]) {
188       ++j;
189       digits = 0;
190       continue;
191     }
192 
193     // Check if the character is a digit (0-9)
194     if ((addr[i] < '0') || (addr[i] > '9')) {
195       return SL_STATUS_INVALID_PARAMETER;
196     }
197 
198     // Convert character to numeric value and update IP bytes
199     ip_bytes[j] = (ip_bytes[j] * 10) + (uint8_t)(addr[i] - '0');
200 
201     digits++;
202     if (digits > 3) {
203       return SL_STATUS_INVALID_PARAMETER;
204     }
205   }
206 
207   // Ensure that there are exactly three '.' separators in the IP address
208   if (j != 3) {
209     return SL_STATUS_INVALID_PARAMETER;
210   }
211 
212   // Calculate the 32-bit integer value of the IP address
213   *value = (uint32_t)ip_bytes[0];
214   *value |= (uint32_t)(ip_bytes[1] << 8);
215   *value |= (uint32_t)(ip_bytes[2] << 16);
216   *value |= (uint32_t)(ip_bytes[3] << 24);
217 
218   return SL_STATUS_OK;
219 }
220