1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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 "hal_trace.h"
16 #include <stdlib.h>
17 #include <string.h>
18 #include "wifi_event.h"
19 #include "station_info.h"
20 #include "wifi_scan_info.h"
21 #include "wifi_error_code.h"
22 #include "wifi_linked_info.h"
23 #include "wifi_device_config.h"
24 #include "bwifi_interface.h"
25 #include "wifi_hotspot_config.h"
26 #include "cmsis_os.h"
27 #include "bwifi_interface.h"
28 #define IFNAMSIZ IF_NAMESIZE
29 #include "lwip/tcpip.h"
30 #include "cmsis_os2.h"
31
32 #define WIFI_MAX_CONFIG_BITMAP_SIZE (((WIFI_MAX_CONFIG_SIZE) >> 3) + (WIFI_MAX_CONFIG_SIZE % 8? 1 : 0))
33 #ifdef HMOS_DEBUG
34 #define hmos_printf(...) printf(__VA_ARGS__)
35 #else
36 #define hmos_printf(...)
37 #endif
38
39 static WifiEvent g_staEventHandler = {0};
40 static bool _station_static_ip = false;
41 static osTimerId_t _dhcp_timer_id;
42 static struct ip_info _current_ip;
43 extern struct netif if_wifi;
44 static u8 _mac_addr[ETH_ALEN];
45
46 WifiErrorCode RegisterWifiEvent(WifiEvent *event);
47
48 typedef enum {
49 HMOS_ON_WIFI_CONNECTION_CHANGED = 0,
50 HMOS_ON_WIFI_SCAN_STATE_CHANGED = 1,
51 HMOS_ON_HOTSPOT_STATE_CHANGED = 2,
52 HMOS_ON_HOTSPOT_STA_JOIN_CHANGED = 3,
53 HMOS_ON_HOTSPOT_STA_LEAVE_CHANGED = 4,
54 HMOS_ON_WIFI_CB_ID_MAX
55 } HalHmosWifiCallId;
56
57 typedef enum {
58 SCAN_INIT = 0,
59 SCAN_REQUEST = 1,
60 SCAN_TRIGGER = 2,
61 SCAN_DONE = 3,
62 } HalHmosScanState;
63
64 typedef union {
65 int connect_info;
66 struct bwifi_scan_config scan_info;
67 int hotspot_state_info;
68 int hotspot_sta_join;
69 int hotspot_sta_leve;
70 } HalHmosWifiCmdInfo;
71
72 typedef union {
73 struct {
74 int state;
75 WifiLinkedInfo info;
76 } wifi_connection_changed;
77 struct {
78 int state;
79 int size;
80 } wifi_scan_sate_changed;
81 struct {
82 int state;
83 } hotspot_state_changed;
84 struct {
85 StationInfo info;
86 } hotspot_sta_join;
87 struct {
88 StationInfo info;
89 } hotspot_sta_leave;
90 } HalHmosWifiEventInfo;
91
92
93 typedef struct hal_hmos_bwifi_event {
94 HalHmosWifiCallId event_id; /**< event ID */
95 HalHmosWifiEventInfo event_info; /**< event information */
96 } HalHmosEventInfo;
97
98 typedef struct hal_hmos_bwifi_cmd {
99 HalHmosWifiCallId cmd_id; /**< cmd ID */
100 HalHmosWifiCmdInfo cmd_info; /**< cmd information */
101 } HalHmosCmdInfo;
102
103 typedef struct {
104 int scan_size;
105 int networkId; /* connected AP save it */
106 uint8_t wifi_config_map[WIFI_MAX_CONFIG_BITMAP_SIZE];
107 WifiDeviceConfig wifi_config[WIFI_MAX_CONFIG_SIZE];
108 } HalHmosWifiConfig;
109
110 typedef struct {
111 volatile HalHmosScanState scan_state;
112 uint8_t wifi_init;
113 uint8_t hidden;
114 uint8_t hmos_event_thread_init;
115 osMutexId hal_hmos_mutex_id;
116 HalHmosWifiConfig hmos_config_info;
117 WifiLinkedInfo info;
118 } HalHmosWifiInfo;
119
120 static WifiEvent *g_HalHmosEvent[WIFI_MAX_EVENT_SIZE] = {0};
121 static HalHmosWifiInfo g_HalHmosWifiInfo = {0};
122 static osThreadId _HalHmosWifiEventThreadId = NULL;
123 static osMailQId _HalHmosWifiEventCallId = NULL;
124
125 osSemaphoreDef(_hal_hmos_wait_sem);
126 osMailQDef(HalHmosWifiCallbackQueue, 6, HalHmosCmdInfo);
127 osMutexDef(HalHmosWifiMutex);
128
129 static void HalHmosStaEventConnect(WIFI_USER_EVT_ID evt_id, void *arg);
130 static void HalHmosStaEventDisconn(WIFI_USER_EVT_ID evt_id, void *arg);
131 static void HalHmosApEventEnabled(WIFI_USER_EVT_ID evt_id, void *arg);
132 static void HalHmosApEventDisabled(WIFI_USER_EVT_ID evt_id, void *arg);
133 static void HalHmosApEventStaJoin(WIFI_USER_EVT_ID evt_id, void *arg);
134 static void HalHmosApEventStaLeave(WIFI_USER_EVT_ID evt_id, void *arg);
135 extern void ethernetif_input(u16_t devnum, void *p_buf, int size);
HalHmosWifiLock(void)136 void HalHmosWifiLock(void)
137 {
138 if (g_HalHmosWifiInfo.hal_hmos_mutex_id == NULL) {
139 g_HalHmosWifiInfo.hal_hmos_mutex_id = osMutexCreate((osMutex(HalHmosWifiMutex)));
140 }
141
142 if (g_HalHmosWifiInfo.hal_hmos_mutex_id != NULL)
143 osMutexWait(g_HalHmosWifiInfo.hal_hmos_mutex_id, osWaitForever);
144 }
HalHmosWifiUnLock(void)145 void HalHmosWifiUnLock(void)
146 {
147 if (g_HalHmosWifiInfo.hal_hmos_mutex_id != NULL)
148 osMutexRelease(g_HalHmosWifiInfo.hal_hmos_mutex_id);
149 }
150
net_intf_status_change_cb(struct netif * netif)151 static void net_intf_status_change_cb(struct netif *netif)
152 {
153 if (netif_is_up(netif) && !ip_addr_isany(&netif->ip_addr)) {
154 uint32_t ip = ((ip4_addr_t *)&netif->ip_addr)->addr;
155 uint32_t gw = ((ip4_addr_t *)&netif->gw)->addr;
156 uint32_t mask = ((ip4_addr_t *)&netif->netmask)->addr;
157 printf("net_intf_status_change_cb **ip = %s\n", inet_ntoa(ip));
158 printf("net_intf_status_change_cb **netmask = %s\n", inet_ntoa(mask));
159 printf("net_intf_status_change_cb **gw = %s\n", inet_ntoa(gw));
160 bwifi_change_current_status(BWIFI_STATUS_GOT_IP);
161 }
162 }
163
WifiConnectionChangedHandler(int state,WifiLinkedInfo * info)164 static void WifiConnectionChangedHandler(int state, WifiLinkedInfo *info)
165 {
166 struct netif *p_netif = &if_wifi;
167 if (p_netif == NULL)
168 return;
169 if (state == WIFI_STATE_NOT_AVAILABLE) {
170 /* clear IP address in case that dhcp_stop not do it */
171 netifapi_netif_set_addr(p_netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
172 dns_setserver(0, IP4_ADDR_ANY);
173 netif_set_link_down(p_netif);
174 netif_set_down(p_netif);
175 if (_station_static_ip == false) {
176 osTimerStop(_dhcp_timer_id);
177 dhcp_stop(p_netif);
178 }
179 } else if ( state == WIFI_STATE_AVAILABLE) {
180 netif_set_link_up(p_netif);
181 netif_set_up(p_netif);
182 if (_station_static_ip == true) {
183 netifapi_netif_set_addr(p_netif, &_current_ip.ip, &_current_ip.netmask, &_current_ip.gw);
184 printf("**ip = %s\n", inet_ntoa(_current_ip.ip));
185 printf("**netmask = %s\n", inet_ntoa(_current_ip.netmask));
186 printf("**gw = %s\n", inet_ntoa(_current_ip.gw));
187 } else {
188 dhcp_start(p_netif);
189 osTimerStop(_dhcp_timer_id);
190 osTimerStart(_dhcp_timer_id, 30 * 1000);
191 }
192 }
193 }
194
HalHmosWifiEventCall(HalHmosEventInfo * event,WifiEvent * hmos_fun_cb)195 static void HalHmosWifiEventCall(HalHmosEventInfo *event, WifiEvent *hmos_fun_cb)
196 {
197
198 if (event->event_id == HMOS_ON_WIFI_CONNECTION_CHANGED &&
199 hmos_fun_cb->OnWifiConnectionChanged != NULL) {
200 hmos_fun_cb->OnWifiConnectionChanged(event->event_info.wifi_connection_changed.state,
201 &(event->event_info.wifi_connection_changed.info));
202 } else if (event->event_id == HMOS_ON_WIFI_SCAN_STATE_CHANGED &&
203 hmos_fun_cb->OnWifiScanStateChanged != NULL) {
204 if (event->event_info.wifi_scan_sate_changed.size < 0)
205 event->event_info.wifi_scan_sate_changed.size = 0;
206 hmos_fun_cb->OnWifiScanStateChanged(event->event_info.wifi_scan_sate_changed.state,
207 event->event_info.wifi_scan_sate_changed.size);
208 } else if (event->event_id == HMOS_ON_HOTSPOT_STATE_CHANGED &&
209 hmos_fun_cb->OnHotspotStateChanged != NULL) {
210 hmos_fun_cb->OnHotspotStateChanged(event->event_info.hotspot_state_changed.state);
211 } else if (event->event_id == HMOS_ON_HOTSPOT_STA_JOIN_CHANGED &&
212 hmos_fun_cb->OnHotspotStaJoin != NULL) {
213 hmos_fun_cb->OnHotspotStaJoin(&(event->event_info.hotspot_sta_join.info));
214 } else if (event->event_id == HMOS_ON_HOTSPOT_STA_LEAVE_CHANGED &&
215 hmos_fun_cb->OnHotspotStaLeave != NULL) {
216 hmos_fun_cb->OnHotspotStaLeave(&(event->event_info.hotspot_sta_leave.info));
217 } else {
218 hmos_printf("HalHmosWifiEventThread invalid id or no cb function\n\r");
219 }
220 }
221
HalHmosWifiEventThread(void const * argument)222 static void HalHmosWifiEventThread(void const *argument)
223 {
224 int i = 0;
225 HalHmosWifiConfig *hmos_config_info = &(g_HalHmosWifiInfo.hmos_config_info);
226 HalHmosEventInfo event;
227 printf("%s start\n", __func__);
228 while (1) {
229 osEvent evt = osMailGet(_HalHmosWifiEventCallId, osWaitForever);
230 printf("%s, id:0x%x\n", __func__, evt.status);
231
232 if (evt.status == osEventMail) {
233 HalHmosCmdInfo *cmd = (HalHmosCmdInfo *)evt.value.p;
234 memset(&event, 0, sizeof(HalHmosEventInfo));
235 event.event_id = cmd->cmd_id;
236 if (cmd->cmd_id == HMOS_ON_WIFI_SCAN_STATE_CHANGED) {
237 event.event_info.wifi_scan_sate_changed.state = WIFI_STATE_AVAILABLE;
238 g_HalHmosWifiInfo.scan_state = SCAN_TRIGGER;
239 if (cmd->cmd_info.scan_info.ssids == NULL && cmd->cmd_info.scan_info.channels == NULL) {
240 hmos_config_info->scan_size = bwifi_scan();
241 } else {
242
243 struct bwifi_ssid *ssid_info = cmd->cmd_info.scan_info.ssids;
244 struct bwifi_ssid *ssid_free_info = cmd->cmd_info.scan_info.ssids;
245 hmos_config_info->scan_size = bwifi_config_scan(&(cmd->cmd_info.scan_info));
246 /* malloc it in the AdvanceScan */
247 while (ssid_info != NULL) {
248 ssid_info = ssid_info->next;
249 free(ssid_free_info);
250 ssid_free_info = ssid_info;
251 }
252
253 if (cmd->cmd_info.scan_info.channels != NULL)
254 free(cmd->cmd_info.scan_info.channels);
255 }
256 g_HalHmosWifiInfo.scan_state = SCAN_DONE;
257 event.event_info.wifi_scan_sate_changed.size = hmos_config_info->scan_size;
258 }
259 for (i = 0; i < WIFI_MAX_EVENT_SIZE; i++) {
260 if (g_HalHmosEvent[i] != NULL)
261 HalHmosWifiEventCall(&event, g_HalHmosEvent[i]);
262 }
263 osMailFree(_HalHmosWifiEventCallId, cmd);
264 }
265
266 }
267 osThreadExit();
268 }
269 osThreadDef(HalHmosWifiEventThread, osPriorityHigh, 1, 4 * 1024, "hal_hmos_wifi_event");
270
HalHmosWifiEventInit(void)271 WifiErrorCode HalHmosWifiEventInit(void)
272 {
273 if (g_HalHmosWifiInfo.hmos_event_thread_init == true)
274 return WIFI_SUCCESS;
275
276 _HalHmosWifiEventCallId = osMailCreate(osMailQ(HalHmosWifiCallbackQueue), NULL);
277 if (_HalHmosWifiEventCallId == NULL) {
278 hmos_printf("create wifi event mail queue fail\n");
279 return ERROR_WIFI_UNKNOWN;
280 }
281
282 _HalHmosWifiEventThreadId = osThreadCreate(osThread(HalHmosWifiEventThread), NULL);
283 if (_HalHmosWifiEventThreadId == NULL) {
284 hmos_printf("create wifi event thread fail\n");
285 return ERROR_WIFI_UNKNOWN;
286 }
287 bwifi_reg_user_evt_handler(WIFI_USER_EVT_CONNECTED, HalHmosStaEventConnect);
288 bwifi_reg_user_evt_handler(WIFI_USER_EVT_DISCONNECTED, HalHmosStaEventDisconn);
289 bwifi_reg_user_evt_handler(WIFI_USER_EVT_AP_ENABLED, HalHmosApEventEnabled);
290 bwifi_reg_user_evt_handler(WIFI_USER_EVT_AP_DISABLED, HalHmosApEventDisabled);
291 bwifi_reg_user_evt_handler(WIFI_USER_EVT_AP_STA_CONNECTED, HalHmosApEventStaJoin);
292 bwifi_reg_user_evt_handler(WIFI_USER_EVT_AP_STA_DISCONNECTED, HalHmosApEventStaLeave);
293
294 g_HalHmosWifiInfo.hmos_event_thread_init = true;
295 return WIFI_SUCCESS;
296 }
297
HalHmosGetNewNetId(HalHmosWifiConfig * config)298 static int HalHmosGetNewNetId(HalHmosWifiConfig *config)
299 {
300 int netId = 0;
301 while (netId < WIFI_MAX_CONFIG_SIZE) {
302 if ((config->wifi_config_map[netId >> 3] & (1 << (netId % 8))) == 0)
303 break;
304 netId++;
305 }
306 return netId;
307 }
308
HalHmosAddNetIdConfig(HalHmosWifiConfig * config,int netId,const WifiDeviceConfig * save_config)309 static void HalHmosAddNetIdConfig(HalHmosWifiConfig *config, int netId, const WifiDeviceConfig *save_config)
310 {
311 memcpy(&(config->wifi_config[netId]), save_config, sizeof(WifiDeviceConfig));
312 config->wifi_config[netId].netId = netId;
313 config->wifi_config_map[netId >> 3] |= (1 << (netId % 8));
314 hmos_printf("%s ssid==%s netid=%d\n\r", __func__, config->wifi_config[netId].ssid, netId);
315 }
316
HalHmosRemoveNetId(HalHmosWifiConfig * config,int netId)317 static void HalHmosRemoveNetId(HalHmosWifiConfig *config, int netId)
318 {
319 config->wifi_config_map[netId >> 3] &= ( ~(1 << (netId % 8)));
320 }
321
HalHmosSendEvent(HalHmosWifiCallId event_id,HalHmosWifiCmdInfo * info)322 static int HalHmosSendEvent(HalHmosWifiCallId event_id, HalHmosWifiCmdInfo *info)
323 {
324 HalHmosCmdInfo *data;
325 hmos_printf("%s, id:%d\n", __func__, event_id);
326
327 if (event_id >= HMOS_ON_WIFI_CB_ID_MAX)
328 return BWIFI_R_INVALID_ARG;
329 data = (HalHmosCmdInfo *)osMailAlloc(_HalHmosWifiEventCallId, 0);
330
331 if (data == NULL)
332 return BWIFI_R_COMMON_FAIL;
333
334 if (event_id == HMOS_ON_WIFI_SCAN_STATE_CHANGED)
335 g_HalHmosWifiInfo.scan_state = SCAN_REQUEST;
336 memset(data, 0, sizeof(HalHmosCmdInfo));
337 data->cmd_id = event_id;
338 if (info != NULL) {
339 memcpy(&(data->cmd_info), info, sizeof(HalHmosWifiCmdInfo));
340 }
341
342 if (osMailPut(_HalHmosWifiEventCallId, (void *)data))
343 return BWIFI_R_COMMON_FAIL;
344
345 return BWIFI_R_OK;
346 }
347
HalHmosStaEventConnect(WIFI_USER_EVT_ID evt_id,void * arg)348 static void HalHmosStaEventConnect(WIFI_USER_EVT_ID evt_id, void *arg)
349 {
350 WifiLinkedInfo *info = &(g_HalHmosWifiInfo.info);
351 hmos_printf("F:%s L:%d event:%d, arg=%d\n", __func__, __LINE__, evt_id, (*(uint8_t *)arg));
352
353 if (evt_id == WIFI_USER_EVT_CONNECTED) {
354 int i = 0;
355 HalHmosEventInfo event;
356 event.event_id = HMOS_ON_WIFI_CONNECTION_CHANGED;
357
358 event.event_info.wifi_connection_changed.state = WIFI_STATE_AVAILABLE;
359 info->rssi = bwifi_get_current_rssi();
360 info->disconnectedReason = 0;
361 info->connState = WIFI_CONNECTED;
362 memcpy(info->bssid, arg, WIFI_MAC_LEN);
363 memcpy(&(event.event_info.wifi_connection_changed.info), info, sizeof(WifiLinkedInfo));
364
365 hmos_printf("connected status=%d bssid=0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:\n\r",
366 event.event_info.wifi_connection_changed.info.connState, info->bssid[0],
367 info->bssid[1], info->bssid[2], info->bssid[3], info->bssid[4], info->bssid[5]);
368
369 for (i = 0; i < WIFI_MAX_EVENT_SIZE; i++) {
370 if (g_HalHmosEvent[i] != NULL)
371 HalHmosWifiEventCall(&event, g_HalHmosEvent[i]);
372 }
373
374 }
375 }
376
HalHmosStaEventDisconn(WIFI_USER_EVT_ID evt_id,void * arg)377 static void HalHmosStaEventDisconn(WIFI_USER_EVT_ID evt_id, void *arg)
378 {
379 WifiLinkedInfo *info = &(g_HalHmosWifiInfo.info);
380
381 hmos_printf("F:%s L:%d event:%d,arg=%d\n", __func__, __LINE__, evt_id, (*(u16 *)arg));
382
383 if (evt_id == WIFI_USER_EVT_DISCONNECTED) {
384 int i;
385 HalHmosEventInfo event;
386
387 event.event_id = HMOS_ON_WIFI_CONNECTION_CHANGED;
388 event.event_info.wifi_connection_changed.state = WIFI_STATE_AVAILABLE;
389 info->connState = WIFI_DISCONNECTED;
390 info->disconnectedReason = (unsigned short)(*(u16 *)arg);
391 info->disconnectedReason &= 0x7fff;
392 memcpy(&(event.event_info.wifi_connection_changed.info), info, sizeof(WifiLinkedInfo));
393 hmos_printf("disconn status=%d reason=%d\n\r",
394 event.event_info.wifi_connection_changed.info.connState, info->disconnectedReason);
395
396 for (i = 0; i < WIFI_MAX_EVENT_SIZE; i++) {
397 if (g_HalHmosEvent[i] != NULL)
398 HalHmosWifiEventCall(&event, g_HalHmosEvent[i]);
399 }
400
401 }
402 }
403
HalHmosApEventEnabled(WIFI_USER_EVT_ID evt_id,void * arg)404 static void HalHmosApEventEnabled(WIFI_USER_EVT_ID evt_id, void *arg)
405 {
406 WifiLinkedInfo *info = &(g_HalHmosWifiInfo.info);
407 hmos_printf("F:%s L:%d event:%d \n", __func__, __LINE__, evt_id);
408
409 if (evt_id == WIFI_USER_EVT_AP_ENABLED) {
410 int i;
411 HalHmosEventInfo event;
412
413 event.event_id = HMOS_ON_HOTSPOT_STATE_CHANGED;
414 event.event_info.hotspot_state_changed.state = WIFI_STATE_AVAILABLE;
415 memcpy(&(event.event_info.wifi_connection_changed.info), info, sizeof(WifiLinkedInfo));
416
417 for (i = 0; i < WIFI_MAX_EVENT_SIZE; i++) {
418 if (g_HalHmosEvent[i] != NULL)
419 HalHmosWifiEventCall(&event, g_HalHmosEvent[i]);
420 }
421
422 }
423 }
424
HalHmosApEventDisabled(WIFI_USER_EVT_ID evt_id,void * arg)425 static void HalHmosApEventDisabled(WIFI_USER_EVT_ID evt_id, void *arg)
426 {
427 WifiLinkedInfo *info = &(g_HalHmosWifiInfo.info);
428 hmos_printf("F:%s L:%d event:%d \n", __func__, __LINE__, evt_id);
429
430 if (evt_id == WIFI_USER_EVT_AP_DISABLED) {
431 int i;
432 HalHmosEventInfo event;
433
434 event.event_id = HMOS_ON_HOTSPOT_STATE_CHANGED;
435 event.event_info.hotspot_state_changed.state = WIFI_STATE_NOT_AVAILABLE;
436 memcpy(&(event.event_info.wifi_connection_changed.info), info, sizeof(WifiLinkedInfo));
437
438 for (i = 0; i < WIFI_MAX_EVENT_SIZE; i++) {
439 if (g_HalHmosEvent[i] != NULL)
440 HalHmosWifiEventCall(&event, g_HalHmosEvent[i]);
441 }
442
443 }
444 }
445
HalHmosApEventStaJoin(WIFI_USER_EVT_ID evt_id,void * arg)446 static void HalHmosApEventStaJoin(WIFI_USER_EVT_ID evt_id, void *arg)
447 {
448 BWIFI_AP_STA_INFO_T *info = (BWIFI_AP_STA_INFO_T *)arg;
449 if (info != NULL) {
450 hmos_printf("F:%s L:%d event:%d, arg=%p\n", __func__, __LINE__, evt_id, info);
451
452 if (evt_id == WIFI_USER_EVT_AP_STA_CONNECTED) {
453 int i;
454 HalHmosEventInfo event;
455
456 event.event_id = HMOS_ON_HOTSPOT_STA_JOIN_CHANGED;
457 event.event_info.hotspot_sta_join.info.disconnectedReason = info->reason;
458 event.event_info.hotspot_sta_join.info.disconnectedReason &= 0x7fff;
459 memcpy(event.event_info.hotspot_sta_join.info.macAddress, info->mac, WIFI_MAC_LEN);
460
461 for (i = 0; i < WIFI_MAX_EVENT_SIZE; i++) {
462 if (g_HalHmosEvent[i] != NULL)
463 HalHmosWifiEventCall(&event, g_HalHmosEvent[i]);
464 }
465 }
466 }
467 }
468
HalHmosApEventStaLeave(WIFI_USER_EVT_ID evt_id,void * arg)469 static void HalHmosApEventStaLeave(WIFI_USER_EVT_ID evt_id, void *arg)
470 {
471 BWIFI_AP_STA_INFO_T *info = (BWIFI_AP_STA_INFO_T *)arg;
472 if (info != NULL) {
473 hmos_printf("F:%s L:%d event:%d, arg=%p\n", __func__, __LINE__, evt_id, info);
474
475 if (evt_id == WIFI_USER_EVT_AP_STA_DISCONNECTED) {
476 int i;
477 HalHmosEventInfo event;
478
479 event.event_id = HMOS_ON_HOTSPOT_STA_LEAVE_CHANGED;
480 event.event_info.hotspot_sta_join.info.disconnectedReason = info->reason;
481 memcpy(event.event_info.hotspot_sta_join.info.macAddress, info->mac, WIFI_MAC_LEN);
482
483 for (i = 0; i < WIFI_MAX_EVENT_SIZE; i++) {
484 if (g_HalHmosEvent[i] != NULL)
485 HalHmosWifiEventCall(&event, g_HalHmosEvent[i]);
486 }
487 }
488 }
489 }
490
HalHmosSecTypeConvert(BWIFI_SEC_TYPE_T security_type)491 static WifiSecurityType HalHmosSecTypeConvert(BWIFI_SEC_TYPE_T security_type)
492 {
493 switch (security_type) {
494 case BWIFI_SECURITY_NONE:
495 return WIFI_SEC_TYPE_OPEN;
496 case BWIFI_SECURITY_WEP40:
497 case BWIFI_SECURITY_WEP104:
498 return WIFI_SEC_TYPE_WEP;
499 case BWIFI_SECURITY_WPA:
500 case BWIFI_SECURITY_WPA2:
501 case BWIFI_SECURITY_WPA_WPA2:
502 return WIFI_SEC_TYPE_PSK;
503 case BWIFI_SECURITY_WPA3_SAE:
504 case BWIFI_SECURITY_WPA3_SAE_WPA2:
505 return WIFI_SEC_TYPE_SAE;
506 default:
507 return WIFI_SEC_TYPE_INVALID;
508 }
509 }
510
HalHmosConvertChanToFreq(u8 chan)511 static int HalHmosConvertChanToFreq(u8 chan)
512 {
513 int freq = 0;
514
515 if (chan >= 1 && chan <= 13)
516 freq = 2407 + chan * 5;
517 else if (chan == 14)
518 freq = 2484;
519 else if (chan >= 182 && chan <= 196)
520 freq = 4000 + chan * 5;
521 else if (chan >= 36 && chan <= 169)
522 freq = 5000 + chan * 5;
523
524 return freq;
525 }
HalHmosConvertFreqToChan(int freq)526 static int HalHmosConvertFreqToChan(int freq)
527 {
528 int chan = 0;
529
530 if (freq >= 2412 && freq <= 2472)
531 chan = (freq - 2407) / 5;
532 else if (freq == 2484)
533 chan = 14;
534 else if (freq >= 4900 && freq < 5000)
535 chan = (freq - 4000) / 5;
536 else if (freq >= 5000 && freq < 5900)
537 chan = (freq - 5000) / 5;
538 else if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4)
539 chan = (freq - 56160) / 2160;
540
541 return chan;
542 }
543
HalHmosChannToBand(u8 chan)544 static int HalHmosChannToBand(u8 chan)
545 {
546 if (chan <= 14)
547 return HOTSPOT_BAND_TYPE_2G;
548
549 return HOTSPOT_BAND_TYPE_5G;
550 }
551
HalHmosGetNidDeviceConfigs(WifiDeviceConfig * result,int nid)552 static WifiErrorCode HalHmosGetNidDeviceConfigs(WifiDeviceConfig *result, int nid)
553 {
554 int i = 0;
555 WifiErrorCode ret = ERROR_WIFI_BUSY;
556 HalHmosWifiConfig *hmos_config_info = &(g_HalHmosWifiInfo.hmos_config_info);
557
558 HalHmosWifiLock();
559 for (i = 0; i < WIFI_MAX_CONFIG_SIZE; i++) {
560 if (hmos_config_info->wifi_config_map[i >> 3] & (1 << (i % 8))) {
561 if (hmos_config_info->wifi_config[i].netId == nid) {
562 memcpy(result, &(hmos_config_info->wifi_config[i]), sizeof(WifiDeviceConfig));
563 ret = WIFI_SUCCESS;
564 }
565
566 }
567
568 }
569 HalHmosWifiUnLock();
570
571 return ret;
572 }
573
EnableWifi(void)574 WifiErrorCode EnableWifi(void)
575 {
576 WifiErrorCode ret;
577
578 if (WIFI_SUCCESS != HalHmosWifiEventInit())
579 return ERROR_WIFI_UNKNOWN;
580
581 if (g_HalHmosWifiInfo.wifi_init == true)
582 return ERROR_WIFI_BUSY;
583
584 HalHmosWifiLock();
585 bwifi_reg_eth_input_handler(ethernetif_input);
586 ret = bwifi_init();
587 if (ret == WIFI_SUCCESS) {
588 extern struct netif if_wifi;
589 struct netif *p_netif = &if_wifi;
590
591 tcpip_init(NULL, NULL);
592
593 extern int bwifi_get_own_mac(u8 *addr);
594 bwifi_get_own_mac(_mac_addr);
595 lwip_netif_mac_addr_init(p_netif, _mac_addr, ETH_ALEN);
596
597 extern err_t ethernetif_init(struct netif *netif);
598 if (netif_add(p_netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4, NULL, ethernetif_init, tcpip_input) == 0) {
599 printf("netif add wifi interface failed\n");
600 return -1;
601 }
602
603 g_HalHmosWifiInfo.wifi_init = true;
604
605 netif_set_default(p_netif);
606
607 netif_set_status_callback(p_netif, net_intf_status_change_cb);
608
609 g_staEventHandler.OnWifiConnectionChanged = WifiConnectionChangedHandler;
610 int error = RegisterWifiEvent(&g_staEventHandler);
611 if (error != WIFI_SUCCESS) {
612 printf("[sample] RegisterWifiEvent fail, error = %d\n", error);
613 return -1;
614 }
615 }
616
617 HalHmosWifiUnLock();
618
619 return ret;
620 }
621
DisableWifi(void)622 WifiErrorCode DisableWifi(void)
623 {
624 WifiErrorCode ret = ERROR_WIFI_BUSY;
625 int disconn_return = 0;
626
627 HalHmosWifiLock();
628 disconn_return = bwifi_disconnect();
629 if (disconn_return == BWIFI_R_OK || disconn_return == BWIFI_R_STATUS_ERROR) {
630 if(g_HalHmosWifiInfo.wifi_init == false){
631 ret = ERROR_WIFI_NOT_STARTED;
632 } else {
633 g_HalHmosWifiInfo.wifi_init = false;
634 ret = WIFI_SUCCESS;
635 }
636 }
637
638 HalHmosWifiUnLock();
639
640 return ret;
641 }
642
IsWifiActive(void)643 int IsWifiActive(void)
644 {
645 return (g_HalHmosWifiInfo.wifi_init == true ? WIFI_STA_ACTIVE : WIFI_STA_NOT_ACTIVE);
646 }
647
Scan(void)648 WifiErrorCode Scan(void)
649 {
650 WifiErrorCode ret = ERROR_WIFI_BUSY;
651
652
653 if (IsWifiActive() != WIFI_STA_ACTIVE)
654 return ERROR_WIFI_IFACE_INVALID;
655
656 if (g_HalHmosWifiInfo.scan_state == SCAN_REQUEST ||
657 g_HalHmosWifiInfo.scan_state == SCAN_TRIGGER)
658 return ERROR_WIFI_BUSY;
659
660 HalHmosWifiLock();
661 ret = ((HalHmosSendEvent(HMOS_ON_WIFI_SCAN_STATE_CHANGED, NULL) == 0) ? WIFI_SUCCESS : ERROR_WIFI_BUSY);
662 HalHmosWifiUnLock();
663
664 return ret;
665 }
666
GetScanInfoList(WifiScanInfo * result,unsigned int * size)667 WifiErrorCode GetScanInfoList(WifiScanInfo *result, unsigned int *size)
668 {
669 int i;
670 int scan_result_len = *size;
671 struct bwifi_bss_info *b_result = NULL;
672 HalHmosWifiConfig *hmos_config_info = &(g_HalHmosWifiInfo.hmos_config_info);
673 WifiErrorCode ret = ERROR_WIFI_BUSY;
674
675 if (IsWifiActive() != WIFI_STA_ACTIVE)
676 return ERROR_WIFI_IFACE_INVALID;
677
678 if (scan_result_len >= WIFI_SCAN_HOTSPOT_LIMIT) {
679
680 if (hmos_config_info->scan_size > 0 &&
681 scan_result_len > hmos_config_info->scan_size)
682 scan_result_len = hmos_config_info->scan_size;
683
684 b_result = malloc(scan_result_len * sizeof(struct bwifi_bss_info));
685 if (b_result != NULL) {
686 scan_result_len = bwifi_get_scan_result(b_result, scan_result_len);
687 for (i = 0; i < scan_result_len; i++) {
688 memcpy((result + i)->ssid, (b_result + i)->ssid, WIFI_MAX_SSID_LEN);
689 memcpy((result + i)->bssid, (b_result + i)->bssid, WIFI_MAC_LEN);
690 (result + i)->band = HalHmosChannToBand((b_result + i)->channel);
691 (result + i)->frequency = HalHmosConvertChanToFreq((b_result + i)->channel);
692 (result + i)->securityType = HalHmosSecTypeConvert((b_result + i)->security_type);
693 (result + i)->rssi = (b_result + i)->rssi;
694 hmos_printf("%s ssid=%s channel=%d sec=%d\n\r", __func__,
695 (result + i)->ssid, (result + i)->frequency, (result + i)->securityType);
696 }
697
698 free(b_result);
699 *size = scan_result_len;
700 ret = WIFI_SUCCESS;
701 }
702 }
703 return ret;
704 }
705
AddDeviceConfig(const WifiDeviceConfig * config,int * result)706 WifiErrorCode AddDeviceConfig(const WifiDeviceConfig *config, int *result)
707 {
708 int netId;
709 WifiErrorCode ret = ERROR_WIFI_BUSY;
710
711 HalHmosWifiLock();
712 netId = HalHmosGetNewNetId(&(g_HalHmosWifiInfo.hmos_config_info));
713 if (netId < WIFI_MAX_CONFIG_SIZE) {
714 HalHmosAddNetIdConfig(&(g_HalHmosWifiInfo.hmos_config_info), netId, config);
715 *result = netId;
716 ret = WIFI_SUCCESS;
717 }
718 HalHmosWifiUnLock();
719 return ret;
720 }
721
GetDeviceConfigs(WifiDeviceConfig * result,unsigned int * size)722 WifiErrorCode GetDeviceConfigs(WifiDeviceConfig *result, unsigned int *size)
723 {
724 int i = 0 ;
725 int cnt = 0;
726 HalHmosWifiConfig *hmos_config_info = &(g_HalHmosWifiInfo.hmos_config_info);
727
728 if (g_HalHmosWifiInfo.hmos_config_info.wifi_config_map[0 >> 3] == 0)
729 return ERROR_WIFI_NOT_AVAILABLE;
730
731 HalHmosWifiLock();
732 for (i = 0; i < WIFI_MAX_CONFIG_SIZE; i++) {
733 hmos_printf("%s i=%d cnt=%d\n\r", __func__, i, cnt);
734 if (hmos_config_info->wifi_config_map[i >> 3] & (1 << (i % 8))) {
735 memcpy(result + cnt, &(hmos_config_info->wifi_config[i]), sizeof(WifiDeviceConfig));
736 cnt++;
737 }
738 }
739 *size = cnt;
740 HalHmosWifiUnLock();
741 return WIFI_SUCCESS;
742 }
743
RemoveDevice(int networkId)744 WifiErrorCode RemoveDevice(int networkId)
745 {
746 WifiErrorCode ret = ERROR_WIFI_INVALID_ARGS;
747
748 HalHmosWifiLock();
749 if (networkId < WIFI_MAX_CONFIG_SIZE) {
750 HalHmosRemoveNetId(&(g_HalHmosWifiInfo.hmos_config_info), networkId);
751 ret = WIFI_SUCCESS;
752 }
753
754 HalHmosWifiUnLock();
755 return ret;
756 }
757
ConnectTo(int networkId)758 WifiErrorCode ConnectTo(int networkId)
759 {
760 WifiLinkedInfo *info = &(g_HalHmosWifiInfo.info);
761 HalHmosWifiConfig *hmos_config_info = &(g_HalHmosWifiInfo.hmos_config_info);
762 WifiErrorCode ret = ERROR_WIFI_UNKNOWN;
763 WifiDeviceConfig result;
764
765 if (IsWifiActive() != WIFI_STA_ACTIVE)
766 return ERROR_WIFI_IFACE_INVALID;
767
768 HalHmosWifiLock();
769 if (WIFI_SUCCESS == HalHmosGetNidDeviceConfigs(&result, networkId)) {
770 hmos_printf("%s %d ssid=%s,psk=%s,wepid=%d ,hidder=0\n\r",
771 __func__, __LINE__, result.ssid, result.preSharedKey,
772 result.securityType == WIFI_SEC_TYPE_WEP ? 1 : 0);
773 hmos_printf("bssid=0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n\r",
774 result.bssid[0], result.bssid[1], result.bssid[2],
775 result.bssid[3], result.bssid[4], result.bssid[5]);
776 hmos_config_info->networkId = networkId;
777 memset(info, 0, sizeof(WifiLinkedInfo));
778 memcpy(info->bssid, result.bssid, WIFI_MAC_LEN);
779 strcpy(info->ssid, result.ssid);
780 info->rssi = bwifi_get_current_rssi();
781
782 if (BWIFI_R_OK == bwifi_connect_to_ssid(info->ssid, result.preSharedKey,
783 result.securityType == WIFI_SEC_TYPE_WEP ? 1 : 0,
784 g_HalHmosWifiInfo.hidden, info->bssid))
785 ret = WIFI_SUCCESS;
786
787 }
788 HalHmosWifiUnLock();
789 return ret;
790 }
791
Disconnect(void)792 WifiErrorCode Disconnect(void)
793 {
794 WifiErrorCode ret;
795 if (IsWifiActive() != WIFI_STA_ACTIVE)
796 return ERROR_WIFI_IFACE_INVALID;
797 HalHmosWifiLock();
798 ret = ( bwifi_disconnect() == BWIFI_R_OK ? WIFI_SUCCESS : ERROR_WIFI_UNKNOWN);
799 if (ret == WIFI_SUCCESS)
800 g_HalHmosWifiInfo.scan_state = SCAN_INIT;
801 HalHmosWifiUnLock();
802 return ret;
803 }
804
GetLinkedInfo(WifiLinkedInfo * result)805 WifiErrorCode GetLinkedInfo(WifiLinkedInfo *result)
806 {
807 WifiLinkedInfo *info = &(g_HalHmosWifiInfo.info);
808 if (result == NULL)
809 return ERROR_WIFI_INVALID_ARGS;
810 info->rssi = bwifi_get_current_rssi();
811 memcpy(result, info, sizeof(WifiLinkedInfo));
812 hmos_printf("%s ssid=%s rssi=%d stat=%d\n\r", __func__,
813 info->ssid, info->rssi, info->connState);
814 return WIFI_SUCCESS;
815 }
816
RegisterWifiEvent(WifiEvent * event)817 WifiErrorCode RegisterWifiEvent(WifiEvent *event)
818 {
819 WifiErrorCode ret = ERROR_WIFI_UNKNOWN;
820
821 if (event == NULL) {
822 hmos_printf("%s e1\n\r", __func__);
823 return ERROR_WIFI_INVALID_ARGS;
824 }
825
826 if (WIFI_SUCCESS != HalHmosWifiEventInit())
827 return ERROR_WIFI_UNKNOWN;
828
829 for (int i = 0; i < WIFI_MAX_EVENT_SIZE; i++) {
830 if (g_HalHmosEvent[i] == event) {
831 hmos_printf("%s e2\n\r", __func__);
832 return ERROR_WIFI_INVALID_ARGS;
833 }
834 if (g_HalHmosEvent[i] == NULL) {
835 HalHmosWifiLock();
836 g_HalHmosEvent[i] = event;
837 ret = WIFI_SUCCESS;
838 i = WIFI_MAX_EVENT_SIZE;
839 HalHmosWifiUnLock();
840 }
841 }
842
843 return ret;
844 }
845
UnRegisterWifiEvent(const WifiEvent * event)846 WifiErrorCode UnRegisterWifiEvent(const WifiEvent *event)
847 {
848 if (event == NULL)
849 return ERROR_WIFI_INVALID_ARGS;
850
851 for (int i = 0; i < WIFI_MAX_EVENT_SIZE; i++) {
852 if (g_HalHmosEvent[i] == event) {
853 HalHmosWifiLock();
854 g_HalHmosEvent[i] = NULL;
855 HalHmosWifiUnLock();
856 return WIFI_SUCCESS;
857 }
858 }
859
860 return ERROR_WIFI_UNKNOWN;
861 }
862
GetDeviceMacAddress(unsigned char * result)863 WifiErrorCode GetDeviceMacAddress(unsigned char *result)
864 {
865 if (result == NULL)
866 return ERROR_WIFI_INVALID_ARGS;
867
868 if (BWIFI_R_OK != bwifi_get_own_mac((uint8_t *)result))
869 return ERROR_WIFI_UNKNOWN;
870
871 return WIFI_SUCCESS;
872 }
873
AdvanceScan(WifiScanParams * params)874 WifiErrorCode AdvanceScan(WifiScanParams *params)
875 {
876 int chan[2] = {0};
877 WifiErrorCode ret = ERROR_WIFI_UNKNOWN;
878 struct bwifi_ssid *scan_ssid = NULL;
879 HalHmosWifiCmdInfo scan_config = {0};
880 WifiScanParams params_tmp = {0};
881 char tmp[] = "wifi_service_xts";
882
883 if (params == NULL || memcmp(params, ¶ms_tmp, sizeof(WifiScanParams)) == 0)
884 return ERROR_WIFI_UNKNOWN;
885
886 params->scanType = (params->scanType == 255 ? WIFI_BAND_SCAN : params->scanType);
887 params_tmp.scanType = params->scanType;
888 if (memcmp(params, ¶ms_tmp, sizeof(WifiScanParams)) == 0 && params_tmp.scanType != WIFI_BAND_SCAN)
889 return ERROR_WIFI_UNKNOWN;
890
891 memset(¶ms_tmp, 0, sizeof(WifiScanParams));
892 strcpy(¶ms_tmp.ssid, tmp);
893 params->scanType = (params->scanType == 255 ? WIFI_BAND_SCAN : params->scanType);
894 params_tmp.scanType = params->scanType;
895 if (memcmp(params, ¶ms_tmp, sizeof(WifiScanParams)) == 0 && params_tmp.scanType != WIFI_BAND_SCAN)
896 return ERROR_WIFI_UNKNOWN;
897
898 if (IsWifiActive() != WIFI_STA_ACTIVE)
899 return ERROR_WIFI_IFACE_INVALID;
900
901 if (g_HalHmosWifiInfo.scan_state == SCAN_REQUEST ||
902 g_HalHmosWifiInfo.scan_state == SCAN_TRIGGER)
903 return ERROR_WIFI_BUSY;
904
905 HalHmosWifiLock();
906 if (params != NULL) {
907 if (params->scanType == WIFI_FREQ_SCAN) {
908 chan[0] = HalHmosConvertFreqToChan(params->freqs);
909 /* free it by the thread HalHmosWifiEventThread */
910 scan_config.scan_info.channels = (int *)malloc(sizeof(chan));
911 if (scan_config.scan_info.channels != NULL) {
912 hmos_printf("%s chann =%p\n\r", __func__, scan_config.scan_info.channels);
913 memcpy(scan_config.scan_info.channels, chan, sizeof(chan));
914 }
915 } else if (params->scanType == WIFI_SSID_SCAN) {
916 /* free it by the thread HalHmosWifiEventThread */
917 scan_ssid = (struct bwifi_ssid *)malloc(sizeof(struct bwifi_ssid));
918 if (scan_ssid != NULL) {
919
920 memcpy(scan_ssid->ssid, params->ssid, params->ssidLen);
921 scan_config.scan_info.ssids = scan_ssid;
922 scan_ssid->next = NULL;
923 scan_ssid->ssid[(int)(0xff & params->ssidLen)] = '\0';
924 hmos_printf("%s ssid =%p ssid=%s\n\r", __func__, scan_ssid, scan_ssid->ssid);
925 ret = WIFI_SUCCESS;
926 }
927 } else {
928 printf("WIFI_BSSID_SCAN and WIFI_BAND_SCAN not support\n");
929 ret = ERROR_WIFI_INVALID_ARGS;
930 }
931 }
932 ret = ((HalHmosSendEvent(HMOS_ON_WIFI_SCAN_STATE_CHANGED, &scan_config) == 0) ? WIFI_SUCCESS : ERROR_WIFI_BUSY);
933 HalHmosWifiUnLock();
934
935 if (ret == ERROR_WIFI_BUSY) {
936 if (scan_config.scan_info.channels != NULL)
937 free(scan_config.scan_info.channels);
938 if (scan_ssid != NULL)
939 free(scan_ssid);
940 }
941
942 return ret;
943 }
944
SetCountryCode(const char * countryCode)945 WifiErrorCode SetCountryCode(const char *countryCode)
946 {
947 WifiErrorCode ret = ERROR_WIFI_UNKNOWN;
948
949 if (countryCode == NULL || strlen(countryCode) > BWIFI_MAX_COUNTRY_CODE_SIZE)
950 return ERROR_WIFI_INVALID_ARGS;
951
952 HalHmosWifiLock();
953 if (bwifi_set_country_code((char *)countryCode) == BWIFI_R_OK)
954 ret = WIFI_SUCCESS;
955 HalHmosWifiUnLock();
956
957 hmos_printf("%s=%s,ret=%d\n\r", __func__, countryCode, ret);
958 return ret;
959 }
960
GetCountryCode(char * countryCode,unsigned int * len)961 WifiErrorCode GetCountryCode(char *countryCode, unsigned int *len)
962 {
963 WifiErrorCode ret = ERROR_WIFI_UNKNOWN;
964
965 if (countryCode == NULL)
966 return ERROR_WIFI_INVALID_ARGS;
967
968 HalHmosWifiLock();
969 if (bwifi_get_country_code(countryCode) == BWIFI_R_OK)
970 ret = WIFI_SUCCESS;
971 HalHmosWifiUnLock();
972 hmos_printf("%s=%s\n\r", __func__, countryCode);
973 return ret;
974 }
975
976