• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 
16 #include "wifi_device.h"
17 #include "cmsis_os2.h"
18 
19 #include "lwip/netifapi.h"
20 #include "lwip/api_shell.h"
21 
PrintLinkedInfo(const WifiLinkedInfo * info)22 static void PrintLinkedInfo(const WifiLinkedInfo* info)
23 {
24     int ret = 0;
25 
26     if (!info) {
27         return;
28     }
29 
30     static char macAddress[32] = {0};
31     unsigned char* mac = info->bssid;
32     if (snprintf_s(macAddress, sizeof(macAddress) + 1, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
33         mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]) < 0) { /* mac地址从0,1,2,3,4,5位 */
34             return;
35     }
36 }
37 
38 static volatile int g_connected = 0;
39 
OnWifiConnectionChanged(int state,const WifiLinkedInfo * info)40 static void OnWifiConnectionChanged(int state, const WifiLinkedInfo* info)
41 {
42     if (!info) {
43         return;
44     }
45 
46     printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state);
47     PrintLinkedInfo(info);
48 
49     if (state == WIFI_STATE_AVAILABLE) {
50         g_connected = 1;
51     } else {
52         g_connected = 0;
53     }
54 }
55 
OnWifiScanStateChanged(int state,int size)56 static void OnWifiScanStateChanged(int state, int size)
57 {
58     printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size);
59 }
60 
61 static WifiEvent g_defaultWifiEventListener = {
62     .OnWifiConnectionChanged = OnWifiConnectionChanged,
63     .OnWifiScanStateChanged = OnWifiScanStateChanged
64 };
65 
66 static struct netif* g_iface = NULL;
67 
ConnectToHotspotconst(const WifiDeviceConfig * apConfig)68 int ConnectToHotspotconst (const WifiDeviceConfig* apConfig)
69 {
70     WifiErrorCode errCode;
71     int netId = -1;
72 
73     errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
74     printf("RegisterWifiEvent: %d\r\n", errCode);
75 
76     errCode = EnableWifi();
77     printf("EnableWifi: %d\r\n", errCode);
78 
79     errCode = AddDeviceConfig(apConfig, &netId);
80     printf("AddDeviceConfig: %d\r\n", errCode);
81 
82     g_connected = 0;
83     errCode = ConnectTo(netId);
84     printf("ConnectTo(%d): %d\r\n", netId, errCode);
85 
86     while (!g_connected) { // wait until connect to AP
87         osDelay(10); /* OS Sleep 10 ms */
88     }
89     printf("g_connected: %d\r\n", g_connected);
90 
91     g_iface = netifapi_netif_find("wlan0");
92     if (g_iface) {
93         err_t ret = netifapi_dhcp_start(g_iface);
94         printf("netifapi_dhcp_start: %d\r\n", ret);
95 
96         osDelay(100); // 100 : OS Sleep 100ms ,and wait DHCP server give me IP
97         ret = netifapi_netif_common(g_iface, dhcp_clients_info_show, NULL);
98         printf("netifapi_netif_common: %d\r\n", ret);
99     }
100     return netId;
101 }
102 
DisconnectWithHotspot(int netId)103 void DisconnectWithHotspot(int netId)
104 {
105     if (g_iface) {
106         err_t ret = netifapi_dhcp_stop(g_iface);
107         printf("netifapi_dhcp_stop: %d\r\n", ret);
108     }
109 
110     WifiErrorCode errCode = Disconnect(); // disconnect with your AP
111     printf("Disconnect: %d\r\n", errCode);
112 
113     errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
114     printf("UnRegisterWifiEvent: %d\r\n", errCode);
115 
116     RemoveDevice(netId); // remove AP config
117     printf("RemoveDevice: %d\r\n", errCode);
118 
119     errCode = DisableWifi();
120     printf("DisableWifi: %d\r\n", errCode);
121 }
122 
123