1 /*
2 // Copyright (C) 2022 Beken Corporation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <components/netif.h>
16 #include <components/log.h>
17 #include <common/bk_err.h>
18 #include <os/mem.h>
19 #include <os/str.h>
20 #include <common/sys_config.h>
21 #if CONFIG_LWIP
22 #include "lwip/inet.h"
23 #include "net.h"
24 #endif
25 #include <components/event.h>
26 #include "bk_private/bk_wifi.h"
27 #if CONFIG_IPV6
28 #include "netif.h"
29 #endif
30
31 //TODO refactor BK netif module
32
netif_wifi_event_cb(void * arg,event_module_t event_module,int event_id,void * event_data)33 bk_err_t netif_wifi_event_cb(void *arg, event_module_t event_module,
34 int event_id, void *event_data)
35 {
36 if (event_module != EVENT_MOD_WIFI) {
37 return BK_OK;
38 }
39
40 switch (event_id) {
41 case EVENT_WIFI_STA_CONNECTED:
42 #if CONFIG_LWIP
43 sta_ip_start();
44 #endif
45 break;
46 case EVENT_WIFI_STA_DISCONNECTED:
47 #if CONFIG_LWIP
48 sta_ip_down();
49 #endif
50 break;
51 default:
52 return BK_OK;
53 }
54
55 return BK_OK;
56 }
57
bk_netif_init(void)58 bk_err_t bk_netif_init(void)
59 {
60 #if (!CONFIG_RTT) && CONFIG_NO_HOSTED
61 extern void net_wlan_initial(void);
62 net_wlan_initial();
63 #endif
64
65 BK_LOG_ON_ERR(bk_event_register_cb(EVENT_MOD_WIFI, EVENT_ID_ALL,
66 netif_wifi_event_cb, NULL));
67 return BK_OK;
68 }
69
70 #if CONFIG_LWIP
netif_validate_ip4_config(const netif_ip4_config_t * ip4_config)71 bk_err_t netif_validate_ip4_config(const netif_ip4_config_t *ip4_config)
72 {
73 //TODO add code to validate the IP address
74 // 0. Validate IP string format
75 // 1. IP of STA/AP can't on the same subnet
76 // 2. multicast checking
77 // 3. ...
78 if (!ip4_config) {
79 return BK_ERR_NULL_PARAM;
80 }
81
82 return BK_OK;
83 }
84
bk_netif_set_ip4_config(netif_if_t ifx,const netif_ip4_config_t * ip4_config)85 bk_err_t bk_netif_set_ip4_config(netif_if_t ifx, const netif_ip4_config_t *ip4_config)
86 {
87 netif_ip4_config_t *config = (netif_ip4_config_t*)ip4_config;
88 int ret;
89
90 ret = netif_validate_ip4_config(ip4_config);
91 if (ret != BK_OK) {
92 return ret;
93 }
94
95 if (ifx == NETIF_IF_STA) {
96 //TODO optimize ip_address_set
97 ip_address_set(1 /*STA*/, 0/*static IP*/, config->ip, config->mask, config->gateway, config->dns);
98 } else if (ifx == NETIF_IF_AP) {
99 ip_address_set(0 /*AP*/, 0/*static IP*/, config->ip, config->mask, config->gateway, config->dns);
100 } else {
101 return BK_ERR_NETIF_IF;
102 }
103
104 return BK_OK;
105 }
106
bk_netif_get_ip4_config(netif_if_t ifx,netif_ip4_config_t * ip4_config)107 bk_err_t bk_netif_get_ip4_config(netif_if_t ifx, netif_ip4_config_t *ip4_config)
108 {
109 struct wlan_ip_config addr;
110
111 if (!ip4_config) {
112 return BK_ERR_NULL_PARAM;
113 }
114
115 os_memset(&addr, 0, sizeof(struct wlan_ip_config));
116 if (ifx == NETIF_IF_STA) {
117 net_get_if_addr(&addr, net_get_sta_handle());
118 } else if (ifx == NETIF_IF_AP) {
119 net_get_if_addr(&addr, net_get_uap_handle());
120 } else {
121 return BK_ERR_NETIF_IF;
122 }
123
124 os_strcpy(ip4_config->ip, inet_ntoa(addr.ipv4.address));
125 os_strcpy(ip4_config->mask, inet_ntoa(addr.ipv4.netmask));
126 os_strcpy(ip4_config->gateway, inet_ntoa(addr.ipv4.gw));
127 os_strcpy(ip4_config->dns, inet_ntoa(addr.ipv4.dns1));
128
129 return BK_OK;
130 }
131
132 #if CONFIG_IPV6
133
134 struct interface {
135 struct netif netif;
136 ip_addr_t ipaddr;
137 ip_addr_t nmask;
138 ip_addr_t gw;
139 };
140
bk_netif_get_ip6_addr_info(netif_if_t ifx)141 bk_err_t bk_netif_get_ip6_addr_info(netif_if_t ifx)
142 {
143 int i;
144 int num = 0;
145 struct netif *netif;
146 struct interface *if_handle;
147
148 if (ifx == NETIF_IF_STA) {
149 if_handle = net_get_sta_handle();
150 } else if (ifx == NETIF_IF_AP) {
151 if_handle = net_get_uap_handle();
152 } else {
153 return BK_ERR_NETIF_IF;
154 }
155
156 netif = &if_handle->netif;
157
158 for (i = 0; i < MAX_IPV6_ADDRESSES; i++) {
159 u8 *ipv6_addr;
160 ipv6_addr = (u8*)ip_2_ip6(&if_handle->netif.ip6_addr[i])->addr;///&addr->ipv6[i].address;
161
162 bk_printf("ipv6_addr[%d] %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\r\n", i,
163 ipv6_addr[0], ipv6_addr[1], ipv6_addr[2], ipv6_addr[3],
164 ipv6_addr[4], ipv6_addr[5], ipv6_addr[6], ipv6_addr[7],
165 ipv6_addr[8], ipv6_addr[9], ipv6_addr[10], ipv6_addr[11],
166 ipv6_addr[12], ipv6_addr[13], ipv6_addr[14], ipv6_addr[15]);
167 bk_printf("ipv6_state[%d] 0x%x\r\n", i, netif->ip6_addr_state[i]);
168 num++;
169 }
170
171 return num;
172 }
173 #endif
174
175 //TODO currently we only set the DHCP client flag
bk_netif_dhcpc_start(netif_if_t ifx)176 bk_err_t bk_netif_dhcpc_start(netif_if_t ifx)
177 {
178 char *empty_ip = "";
179
180 if (ifx != NETIF_IF_STA) {
181 return BK_ERR_NETIF_IF;
182 }
183
184 ip_address_set(1/*STA*/, 1/*DHCP enabled*/, empty_ip, empty_ip, empty_ip, empty_ip);
185 return BK_OK;
186 }
187 #endif
188