• 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 // /<this demo make the wifi to connect to the specified AP
17 
18 #include <unistd.h>
19 #include <hi_wifi_api.h>
20 #include <lwip/ip_addr.h>
21 #include <lwip/netifapi.h>
22 #include <hi_types_base.h>
23 #include <hi_task.h>
24 #include <hi_mem.h>
25 #include "wifi_device.h"
26 #include "cmsis_os2.h"
27 #include "wifi_device_config.h"
28 #include "lwip/api_shell.h"
29 #include "udp_config.h"
30 
31 #define APP_INIT_VAP_NUM    2
32 #define APP_INIT_USR_NUM    2
33 
34 static struct netif *g_lwipNetif = NULL;
35 static hi_bool  g_scanDone = HI_FALSE;
36 static struct netif* g_iface = NULL;
37 void WifiStaStop(int netId);
38 static int WifiStaStart(void);
39 int g_netId = -1;
40 int g_staConnect = 0;
41 #define MAC_ADDR_BUF_LEN (32)
42 #define WIFI_CONNECT_STATUS ((unsigned char)0x02)
43 /**
44  @brief  print wifi sta module link information
45  @param WifiLinkedInfo: wifi sta struct,include device's SSID,BSSID,IP address, and so on...
46  @param info : wifi sta link information
47 */
PrintLinkedInfo(const WifiLinkedInfo * info)48 static void PrintLinkedInfo(const WifiLinkedInfo* info)
49 {
50     int ret = 0;
51 
52     if (!info) {
53         return;
54     }
55 
56     static char macAddress[MAC_ADDR_BUF_LEN] = {0};
57     unsigned char* mac = info->bssid;
58     if (snprintf_s(macAddress, sizeof(macAddress) + 1, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
59         mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]) < 0) { /* mac地址从0,1,2,3,4,5位 */
60             return;
61     }
62 }
63 /**
64  @brief  The function will show the state changes during WiFi connection
65  @param WifiLinkedInfo: wifi sta struct,include device's SSID,BSSID,IP address, and so on...
66  @param info : wifi sta link information
67  @param state: wifi state
68 */
OnWifiConnectionChanged(int state,const WifiLinkedInfo * info)69 static void OnWifiConnectionChanged(int state, const WifiLinkedInfo* info)
70 {
71     if (!info) {
72         return;
73     }
74 
75     printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state);
76     PrintLinkedInfo(info);
77 
78     if (state == WIFI_STATE_AVALIABLE) {
79         g_staConnect = 1;
80     } else {
81         g_staConnect = 0;
82     }
83 }
84 /**
85     @brief The function will display the scan results during WiFi connection
86     @param state: wifi scan result state
87     @param size: wifi scan result buffer size
88 */
OnWifiScanStateChanged(int state,int size)89 static void OnWifiScanStateChanged(int state, int size)
90 {
91     printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size);
92 }
93 
94 static WifiEvent g_defaultWifiEventListener = {
95     .OnWifiConnectionChanged = OnWifiConnectionChanged,
96     .OnWifiScanStateChanged = OnWifiScanStateChanged
97 };
98 /**
99     @brief This function will start wifi station module, and WiFi will connect to the hotspot
100 */
101 
WifiStaStart(void)102 static int WifiStaStart(void)
103 {
104     WifiDeviceConfig apConfig = {0};
105     (void)strcpy_s(apConfig.ssid, strlen(AP_SSID) + 1, AP_SSID);
106     (void)strcpy_s(apConfig.preSharedKey, strlen(AP_PWD) + 1, AP_PWD);
107     apConfig.securityType = WIFI_SEC_TYPE_PSK;
108 
109     WifiErrorCode errCode;
110     int netId = -1;
111 
112     errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
113     printf("RegisterWifiEvent: %d\r\n", errCode);
114 
115     errCode = EnableWifi();
116     printf("EnableWifi: %d\r\n", errCode);
117 
118     errCode = AddDeviceConfig(&apConfig, &netId);
119     printf("AddDeviceConfig: %d\r\n", errCode);
120 
121     g_staConnect = 0;
122     errCode = ConnectTo(netId);
123     printf("ConnectTo(%d): %d\r\n", netId, errCode);
124 
125     while (!g_staConnect) { // wait until connect to AP
126         osDelay(10);    /* 连接10ms */
127     }
128     printf("g_staConnect: %d\r\n", g_staConnect);
129 
130     g_iface = netifapi_netif_find("wlan0");
131     if (g_iface) {
132         err_t ret = netifapi_dhcp_start(g_iface);
133         printf("netifapi_dhcp_start: %d\r\n", ret);
134 
135         osDelay(100); // wait DHCP server give me IP 100ms
136         ret = netifapi_netif_common(g_iface, dhcp_clients_info_show, NULL);
137         printf("netifapi_netif_common: %d\r\n", ret);
138     }
139     return netId;
140 }
141 /**
142     @brief This function will start wifi station module, and WiFi will connect to the hotspot
143            The function gets DHCP, and so on...
144 */
WifiStaModule(void)145 void WifiStaModule(void)
146 {
147     ip4_addr_t ipAddr;
148     ip4_addr_t ipAny;
149     IP4_ADDR(&ipAny, 0, 0, 0, 0);
150     IP4_ADDR(&ipAddr, 0, 0, 0, 0);
151     g_netId = WifiStaStart();
152     printf("wifi sta dhcp done\r\n");
153     return;
154 }
155