1 // Copyright (C) 2022 Beken Corporation
2 //
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 "stdbool.h"
16 #include "wifi_hotspot.h"
17 #include <common/bk_err.h>
18 #include <modules/wifi.h>
19 #include <components/netif_types.h>
20
21
22 #define WLAN_DEFAULT_IP "192.168.0.1"
23 #define WLAN_DEFAULT_GW "192.168.0.1"
24 #define WLAN_DEFAULT_MASK "255.255.255.0"
25
26 #define RSSI_LEVEL_4_2_G (-65)
27 #define RSSI_LEVEL_3_2_G (-75)
28 #define RSSI_LEVEL_2_2_G (-82)
29 #define RSSI_LEVEL_1_2_G (-88)
30
31 #define RSSI_LEVEL_4_5_G (-65)
32 #define RSSI_LEVEL_3_5_G (-72)
33 #define RSSI_LEVEL_2_5_G (-79)
34 #define RSSI_LEVEL_1_5_G (-85)
35
36
37 static int g_wifiApStatus = WIFI_HOTSPOT_NOT_ACTIVE;
38 static unsigned char g_wifiApCfgFlag = 0;
39 static unsigned char g_band = 0;
40
SetHotspotParam(char * ssid,char * key,unsigned char security,unsigned char channel)41 static void SetHotspotParam(char *ssid, char *key,unsigned char security, unsigned char channel)
42 {
43 netif_ip4_config_t config = {0};
44 wifi_ap_config_t ap_cfg = WIFI_DEFAULT_AP_CONFIG();
45
46 strcpy(config.ip, WLAN_DEFAULT_IP);
47 strcpy(config.mask, WLAN_DEFAULT_MASK);
48 strcpy(config.gateway, WLAN_DEFAULT_GW);
49 strcpy(config.dns, WLAN_DEFAULT_GW);
50
51 bk_netif_set_ip4_config(NETIF_IF_AP, &config);
52 if(ssid != NULL)
53 strcpy(ap_cfg.ssid, ssid);
54 if(key != NULL)
55 strcpy(ap_cfg.password, key);
56
57 ap_cfg.security = security;
58 ap_cfg.channel = channel;
59 printf("xxx channel = %d\n", channel);
60
61 bk_wifi_ap_set_config(&ap_cfg);
62 }
63
64 extern void DispatchApStartEvent(int state);
EnableHotspot(void)65 WifiErrorCode EnableHotspot(void)
66 {
67
68 if (LockWifiGlobalLock() != WIFI_SUCCESS)
69 {
70 return ERROR_WIFI_UNKNOWN;
71 }
72 if (g_wifiApStatus == WIFI_HOTSPOT_ACTIVE)
73 {
74 if (UnlockWifiGlobalLock() != WIFI_SUCCESS)
75 {
76 return ERROR_WIFI_UNKNOWN;
77 }
78 return ERROR_WIFI_BUSY;
79 }
80 if(g_wifiApCfgFlag != 1)
81 return ERROR_WIFI_NOT_AVAILABLE;
82
83 bk_wifi_ap_start();
84 g_wifiApStatus = WIFI_HOTSPOT_ACTIVE;
85 DispatchApStartEvent(WIFI_STATE_AVAILABLE);
86
87 if (UnlockWifiGlobalLock() != WIFI_SUCCESS)
88 {
89 return ERROR_WIFI_UNKNOWN;
90 }
91 return WIFI_SUCCESS;
92 }
93
DisableHotspot(void)94 WifiErrorCode DisableHotspot(void)
95 {
96 if (LockWifiGlobalLock() != WIFI_SUCCESS)
97 {
98 return ERROR_WIFI_UNKNOWN;
99 }
100 if (g_wifiApStatus == WIFI_HOTSPOT_NOT_ACTIVE)
101 {
102 if (UnlockWifiGlobalLock() != WIFI_SUCCESS)
103 {
104 return ERROR_WIFI_UNKNOWN;
105 }
106
107 return ERROR_WIFI_NOT_STARTED;
108 }
109
110 bk_wifi_ap_stop();
111 DispatchApStartEvent(WIFI_STATE_NOT_AVAILABLE);
112
113 g_wifiApStatus = WIFI_HOTSPOT_NOT_ACTIVE;
114 if (UnlockWifiGlobalLock() != WIFI_SUCCESS)
115 {
116 return ERROR_WIFI_UNKNOWN;
117 }
118
119 return WIFI_SUCCESS;
120 }
121
SetHotspotConfig(const HotspotConfig * config)122 WifiErrorCode SetHotspotConfig(const HotspotConfig* config)
123 {
124 if (config == NULL)
125 {
126 return ERROR_WIFI_INVALID_ARGS;
127 }
128
129
130 if (LockWifiGlobalLock() != WIFI_SUCCESS)
131 {
132 return ERROR_WIFI_UNKNOWN;
133 }
134
135 SetHotspotParam(config->ssid,config->preSharedKey,HoSecToBKSec(config->securityType),config->channelNum); //lin : maybe 11
136
137 g_wifiApCfgFlag = 1;
138 if (UnlockWifiGlobalLock() != WIFI_SUCCESS)
139 {
140 return ERROR_WIFI_UNKNOWN;
141 }
142
143 return WIFI_SUCCESS;
144 }
145
GetHotspotConfig(HotspotConfig * result)146 WifiErrorCode GetHotspotConfig(HotspotConfig* result)
147 {
148 wifi_ap_config_t ap_info={0};
149
150 if (result == NULL)
151 {
152 return ERROR_WIFI_INVALID_ARGS;
153 }
154
155 if (LockWifiGlobalLock() != WIFI_SUCCESS)
156 {
157 return ERROR_WIFI_UNKNOWN;
158 }
159 bk_wifi_ap_get_config(&ap_info);
160
161 strcpy(result->preSharedKey, ap_info.password);
162 strcpy(result->ssid, ap_info.ssid);
163 result->channelNum = ap_info.channel;
164 result->securityType = BKSecToHoSec(ap_info.security);
165 result->band = g_band;
166
167 if (UnlockWifiGlobalLock() != WIFI_SUCCESS)
168 {
169 return ERROR_WIFI_UNKNOWN;
170 }
171 return WIFI_SUCCESS;
172 }
173
IsHotspotActive(void)174 int IsHotspotActive(void)
175 {
176 if (LockWifiGlobalLock() != WIFI_SUCCESS)
177 {
178 return ERROR_WIFI_UNKNOWN;
179 }
180
181 if (UnlockWifiGlobalLock() != WIFI_SUCCESS)
182 {
183 return ERROR_WIFI_UNKNOWN;
184 }
185
186 return g_wifiApStatus;
187 }
188
189 extern WifiErrorCode GetApInfo(StationInfo* result, unsigned int* size);
190
GetStationList(StationInfo * result,unsigned int * size)191 WifiErrorCode GetStationList(StationInfo* result, unsigned int* size)
192 {
193 if (result == NULL || size == NULL || *size == 0)
194 {
195 return ERROR_WIFI_INVALID_ARGS;
196 }
197
198 return GetApInfo(result, size);
199 }
200
SetBand(int band)201 WifiErrorCode SetBand(int band)
202 {
203 if(band != HOTSPOT_BAND_TYPE_2G)
204 return ERROR_WIFI_NOT_SUPPORTED;
205 g_band = band;
206 return WIFI_SUCCESS;
207 }
208
GetBand(int * result)209 WifiErrorCode GetBand(int* result)
210 {
211 if (result == NULL)
212 {
213 return ERROR_WIFI_INVALID_ARGS;
214 }
215 if(g_band == 0)
216 return ERROR_WIFI_NOT_AVAILABLE;
217
218 *result = HOTSPOT_BAND_TYPE_2G;
219 return WIFI_SUCCESS;
220 }
221
GetSignalLevel(int rssi,int band)222 int GetSignalLevel(int rssi, int band)
223 {
224
225 if (band == HOTSPOT_BAND_TYPE_5G)
226 {
227 if (rssi >= RSSI_LEVEL_4_5_G)
228 {
229 return RSSI_LEVEL_4;
230 }
231 if (rssi >= RSSI_LEVEL_3_5_G)
232 {
233 return RSSI_LEVEL_3;
234 }
235 if (rssi >= RSSI_LEVEL_2_5_G)
236 {
237 return RSSI_LEVEL_2;
238 }
239 if (rssi >= RSSI_LEVEL_1_5_G)
240 {
241 return RSSI_LEVEL_1;
242 }
243 }
244 if(band == HOTSPOT_BAND_TYPE_2G)
245 {
246
247 if (rssi >= RSSI_LEVEL_4_2_G)
248 {
249 return RSSI_LEVEL_4;
250 }
251 if (rssi >= RSSI_LEVEL_3_2_G)
252 {
253 return RSSI_LEVEL_3;
254 }
255 if (rssi >= RSSI_LEVEL_2_2_G)
256 {
257 return RSSI_LEVEL_2;
258 }
259 if (rssi >= RSSI_LEVEL_1_2_G)
260 {
261 return RSSI_LEVEL_1;
262 }
263 }
264
265 return ERROR_WIFI_INVALID_ARGS;
266 }
267
GetHotspotChannel(void)268 int GetHotspotChannel(void)
269 {
270 if (LockWifiGlobalLock() != WIFI_SUCCESS)
271 {
272 return ERROR_WIFI_UNKNOWN;
273 }
274 if (g_wifiApStatus == WIFI_HOTSPOT_NOT_ACTIVE)
275 {
276 if (UnlockWifiGlobalLock() != WIFI_SUCCESS)
277 {
278 return ERROR_WIFI_UNKNOWN;
279 }
280
281 return ERROR_WIFI_NOT_STARTED;
282 }
283
284 HotspotConfig ap_config = {0};
285 GetHotspotConfig(&ap_config);
286
287 if (UnlockWifiGlobalLock() != WIFI_SUCCESS)
288 {
289 return ERROR_WIFI_UNKNOWN;
290 }
291
292 return ap_config.channelNum;
293 }
294
GetHotspotInterfaceName(char * result,int size)295 WifiErrorCode GetHotspotInterfaceName(char* result, int size)
296 {
297 if (LockWifiGlobalLock() != WIFI_SUCCESS)
298 {
299 return ERROR_WIFI_UNKNOWN;
300 }
301 if (g_wifiApStatus == WIFI_HOTSPOT_NOT_ACTIVE)
302 {
303 if (UnlockWifiGlobalLock() != WIFI_SUCCESS)
304 {
305 return ERROR_WIFI_UNKNOWN;
306 }
307 return ERROR_WIFI_NOT_STARTED;
308 }
309 HotspotConfig ap_config = {0};
310 GetHotspotConfig(&ap_config);
311 memcpy(result, ap_config.ssid, size);
312
313 if (UnlockWifiGlobalLock() != WIFI_SUCCESS)
314 {
315 return ERROR_WIFI_UNKNOWN;
316 }
317 return WIFI_SUCCESS;
318 }
319
test_setHotspot(char * ssid,char * key,char security)320 void test_setHotspot(char *ssid,char *key,char security)
321 {
322 HotspotConfig cfg={0};
323 if(ssid == NULL)
324 return;
325
326 memcpy(cfg.ssid, ssid, strlen(ssid));
327 if(key != NULL)
328 {
329 memcpy(cfg.preSharedKey,key,strlen(key));
330 if(security > WIFI_SEC_TYPE_SAE)
331 security = WIFI_SEC_TYPE_OPEN;
332 cfg.securityType = security;
333 }
334 else
335 {
336 cfg.securityType = WIFI_SEC_TYPE_OPEN;
337 }
338 cfg.channelNum = 11;//default
339 SetHotspotConfig(&cfg);
340 }
341