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 // < this demo make the wifi to connect to the specified AP
17 #include <unistd.h>
18 #include <hi_wifi_api.h>
19 #include <lwip/ip_addr.h>
20 #include <lwip/netifapi.h>
21 #include <hi_types_base.h>
22 #include <hi_task.h>
23 #include <hi_mem.h>
24 #include "iot_config.h"
25 #include "iot_log.h"
26 #include "wifi_device.h"
27 #include "cmsis_os2.h"
28 #include "wifi_device_config.h"
29 #include "lwip/api_shell.h"
30
31 #define APP_INIT_VAP_NUM 2
32 #define APP_INIT_USR_NUM 2
33
34 static struct netif *gLwipNetif = NULL;
35 static hi_bool gScanDone = HI_FALSE;
36 unsigned char wifiStatus = 0;
37
38 unsigned char wifiFirstConnecting = 0;
39 unsigned char wifiSecondConnecting = 0;
40 unsigned char wifiSecondConnected = 0;
41 static struct netif* g_iface = NULL;
42 void WifiStopSta(int netId);
43 static int WifiStartSta(void);
44 int cnetId = -1;
45 int g_connected = 0;
46
47 #define WIFI_CONNECT_STATUS ((unsigned char)0x02)
48
wifiReconnected(int netId)49 void wifiReconnected(int netId)
50 {
51 int recnetId = netId;
52 if (wifiFirstConnecting == WIFI_CONNECT_STATUS) {
53 wifiSecondConnecting = HI_TRUE;
54 wifiFirstConnecting = HI_FALSE;
55 WifiStopSta(recnetId);
56 ip4_addr_t ipAddr;
57 ip4_addr_t ipAny;
58 IP4_ADDR(&ipAny, 0, 0, 0, 0);
59 IP4_ADDR(&ipAddr, 0, 0, 0, 0);
60 recnetId = WifiStartSta();
61 netifapi_dhcp_start(gLwipNetif);
62 while (0 == memcmp(&ipAddr, &ipAny, sizeof(ip4_addr_t))) {
63 IOT_LOG_DEBUG("<Wifi reconnecting>:Wait the DHCP READY");
64 netifapi_netif_get_addr(gLwipNetif, &ipAddr, NULL, NULL);
65 hi_sleep(1000); /* 1000: cpu sleep 1000 ms */
66 }
67 wifiSecondConnected = HI_FALSE;
68 wifiFirstConnecting = WIFI_CONNECT_STATUS;
69 wifiStatus = HI_WIFI_EVT_CONNECTED;
70 }
71 }
72 /* clear netif's ip, gateway and netmask */
StaResetAddr(struct netif * lwipNetif)73 static void StaResetAddr(struct netif *lwipNetif)
74 {
75 ip4_addr_t st_gw;
76 ip4_addr_t st_ipaddr;
77 ip4_addr_t st_netmask;
78
79 if (lwipNetif == NULL) {
80 IOT_LOG_ERROR("hisi_reset_addr::Null param of netdev");
81 return;
82 }
83
84 IP4_ADDR(&st_gw, 0, 0, 0, 0);
85 IP4_ADDR(&st_ipaddr, 0, 0, 0, 0);
86 IP4_ADDR(&st_netmask, 0, 0, 0, 0);
87
88 netifapi_netif_set_addr(lwipNetif, &st_ipaddr, &st_netmask, &st_gw);
89 }
90
WpaEventCB(const hi_wifi_event * hisiEvent)91 static void WpaEventCB(const hi_wifi_event *hisiEvent)
92 {
93 if (hisiEvent == NULL)
94 return;
95 IOT_LOG_DEBUG("EVENT_TYPE:%d", hisiEvent->event);
96 switch (hisiEvent->event) {
97 case HI_WIFI_EVT_SCAN_DONE:
98 IOT_LOG_DEBUG("WiFi: Scan results available");
99 gScanDone = HI_TRUE;
100 break;
101 case HI_WIFI_EVT_CONNECTED:
102 IOT_LOG_DEBUG("WiFi: Connected");
103 netifapi_dhcp_start(gLwipNetif);
104 wifiStatus = HI_WIFI_EVT_CONNECTED;
105 if (wifiSecondConnected) {
106 wifiSecondConnected = HI_FALSE;
107 wifiFirstConnecting = WIFI_CONNECT_STATUS;
108 }
109 break;
110 case HI_WIFI_EVT_DISCONNECTED:
111 IOT_LOG_DEBUG("WiFi: Disconnected");
112 netifapi_dhcp_stop(gLwipNetif);
113 StaResetAddr(gLwipNetif);
114 wifiStatus = HI_WIFI_EVT_DISCONNECTED;
115 wifiReconnected(cnetId);
116 break;
117 case HI_WIFI_EVT_WPS_TIMEOUT:
118 IOT_LOG_DEBUG("WiFi: wps is timeout");
119 wifiStatus = HI_WIFI_EVT_WPS_TIMEOUT;
120 break;
121 default:
122 break;
123 }
124 }
125
StaStartConnect(void)126 static int StaStartConnect(void)
127 {
128 int ret;
129 errno_t rc;
130 hi_wifi_assoc_request assoc_req = {0};
131
132 /* copy SSID to assoc_req */
133 rc = memcpy_s(assoc_req.ssid, HI_WIFI_MAX_SSID_LEN + 1, CONFIG_AP_SSID, strlen(CONFIG_AP_SSID));
134 if (rc != EOK) {
135 return -1;
136 }
137
138 /*
139 * OPEN mode
140 * for WPA2-PSK mode:
141 * set assoc_req.auth as HI_WIFI_SECURITY_WPA2PSK,
142 * then memcpy(assoc_req.key, "12345678", 8).
143 */
144 assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK;
145 rc = memcpy_s(assoc_req.key, HI_WIFI_MAX_KEY_LEN + 1, CONFIG_AP_PWD, strlen(CONFIG_AP_PWD));
146 if (rc != EOK) {
147 return -1;
148 }
149
150 ret = hi_wifi_sta_connect(&assoc_req);
151 if (ret != HISI_OK) {
152 return -1;
153 }
154
155 return 0;
156 }
157
PrintLinkedInfo(WifiLinkedInfo * info)158 static void PrintLinkedInfo(WifiLinkedInfo* info)
159 {
160 int ret = 0;
161 if (!info) {
162 return;
163 }
164
165 static char macAddress[32] = {0};
166 unsigned char* mac = info->bssid;
167 if (snprintf_s(macAddress, sizeof(macAddress) + 1, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
168 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]) < 0) { /* mac地址从0,1,2,3,4,5位 */
169 return;
170 }
171 }
172
OnWifiConnectionChanged(int state,WifiLinkedInfo * info)173 static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
174 {
175 if (!info) {
176 return;
177 }
178
179 printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state);
180 PrintLinkedInfo(info);
181
182 if (state == WIFI_STATE_AVAILABLE) {
183 g_connected = 1;
184 } else {
185 g_connected = 0;
186 }
187 }
188
OnWifiScanStateChanged(int state,int size)189 static void OnWifiScanStateChanged(int state, int size)
190 {
191 printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size);
192 }
193
194 static WifiEvent g_defaultWifiEventListener = {
195 .OnWifiConnectionChanged = OnWifiConnectionChanged,
196 .OnWifiScanStateChanged = OnWifiScanStateChanged
197 };
198
WifiStartSta(void)199 static int WifiStartSta(void)
200 {
201 WifiDeviceConfig apConfig = {0};
202 strcpy_s(apConfig.ssid, sizeof(CONFIG_AP_SSID), CONFIG_AP_SSID);
203 strcpy_s(apConfig.preSharedKey, sizeof(CONFIG_AP_PWD), CONFIG_AP_PWD);
204 apConfig.securityType = WIFI_SEC_TYPE_PSK;
205
206 WifiErrorCode errCode;
207 int netId = -1;
208
209 errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
210 printf("RegisterWifiEvent: %d\r\n", errCode);
211
212 errCode = EnableWifi();
213 printf("EnableWifi: %d\r\n", errCode);
214
215 errCode = AddDeviceConfig(&apConfig, &netId);
216 printf("AddDeviceConfig: %d\r\n", errCode);
217
218 g_connected = 0;
219 errCode = ConnectTo(netId);
220 printf("ConnectTo(%d): %d\r\n", netId, errCode);
221
222 while (!g_connected) { // wait until connect to AP
223 osDelay(10); /* 10: os sleep 10ms */
224 }
225 printf("g_connected: %d\r\n", g_connected);
226
227 g_iface = netifapi_netif_find("wlan0");
228 if (g_iface) {
229 err_t ret = netifapi_dhcp_start(g_iface);
230 printf("netifapi_dhcp_start: %d\r\n", ret);
231
232 osDelay(100); // 100: os sleep 100ms wait DHCP server give me IP
233 ret = netifapi_netif_common(g_iface, dhcp_clients_info_show, NULL);
234 printf("netifapi_netif_common: %d\r\n", ret);
235 }
236 return netId;
237 }
238
WifiStopSta(int netId)239 void WifiStopSta(int netId)
240 {
241 int stopNetId = netId;
242
243 if (g_iface) {
244 err_t ret = netifapi_dhcp_stop(g_iface);
245 printf("netifapi_dhcp_stop: %d\r\n", ret);
246 }
247
248 WifiErrorCode errCode = Disconnect(); // disconnect with your AP
249 printf("Disconnect: %d\r\n", errCode);
250
251 errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
252 printf("UnRegisterWifiEvent: %d\r\n", errCode);
253
254 RemoveDevice(stopNetId); // remove AP config
255 printf("RemoveDevice: %d\r\n", errCode);
256
257 errCode = DisableWifi();
258 printf("DisableWifi: %d\r\n", errCode);
259 }
260
WifiStaReadyWait(void)261 void WifiStaReadyWait(void)
262 {
263 ip4_addr_t ipAddr;
264 ip4_addr_t ipAny;
265 IP4_ADDR(&ipAny, 0, 0, 0, 0);
266 IP4_ADDR(&ipAddr, 0, 0, 0, 0);
267 cnetId = WifiStartSta();
268 IOT_LOG_DEBUG("wifi sta dhcp done");
269 }