1 /*
2 * Copyright (c) 2020 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 <unistd.h>
17 #include "hi_wifi_api.h"
18 #include "wifi_sta.h"
19 #include "lwip/ip_addr.h"
20 #include "lwip/netifapi.h"
21
22 #define APP_INIT_VAP_NUM 2
23 #define APP_INIT_USR_NUM 2
24
25 static struct netif *g_lwip_netif = NULL;
26
27 /* clear netif's ip, gateway and netmask */
hi_sta_reset_addr(struct netif * pst_lwip_netif)28 void hi_sta_reset_addr(struct netif *pst_lwip_netif)
29 {
30 ip4_addr_t st_gw;
31 ip4_addr_t st_ipaddr;
32 ip4_addr_t st_netmask;
33
34 if (pst_lwip_netif == NULL) {
35 printf("hisi_reset_addr::Null param of netdev\r\n");
36 return;
37 }
38
39 IP4_ADDR(&st_gw, 0, 0, 0, 0);
40 IP4_ADDR(&st_ipaddr, 0, 0, 0, 0);
41 IP4_ADDR(&st_netmask, 0, 0, 0, 0);
42
43 netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw);
44 }
45
wifi_wpa_event_cb(const hi_wifi_event * hisi_event)46 void wifi_wpa_event_cb(const hi_wifi_event *hisi_event)
47 {
48 if (hisi_event == NULL)
49 return;
50
51 switch (hisi_event->event) {
52 case HI_WIFI_EVT_SCAN_DONE:
53 printf("WiFi: Scan results available\n");
54 break;
55 case HI_WIFI_EVT_CONNECTED:
56 printf("WiFi: Connected\n");
57 netifapi_dhcp_start(g_lwip_netif);
58 break;
59 case HI_WIFI_EVT_DISCONNECTED:
60 printf("WiFi: Disconnected\n");
61 netifapi_dhcp_stop(g_lwip_netif);
62 hi_sta_reset_addr(g_lwip_netif);
63 break;
64 case HI_WIFI_EVT_WPS_TIMEOUT:
65 printf("WiFi: wps is timeout\n");
66 break;
67 default:
68 break;
69 }
70 }
71
hi_wifi_start_connect(void)72 int hi_wifi_start_connect(void)
73 {
74 int ret;
75 errno_t rc;
76 hi_wifi_assoc_request assoc_req = {0};
77
78 /* copy SSID to assoc_req */
79 rc = memcpy_s(assoc_req.ssid, HI_WIFI_MAX_SSID_LEN + 1, "Hisilicon", 9); /* 9:ssid length */
80 if (rc != EOK) {
81 return -1;
82 }
83
84 /*
85 * OPEN mode
86 * for WPA2-PSK mode:
87 * set assoc_req.auth as HI_WIFI_SECURITY_WPA2PSK,
88 * then memcpy(assoc_req.key, "12345678", 8).
89 */
90 assoc_req.auth = HI_WIFI_SECURITY_OPEN;
91
92 ret = hi_wifi_sta_connect(&assoc_req);
93 if (ret != HISI_OK) {
94 return -1;
95 }
96
97 return 0;
98 }
99
hi_wifi_start_sta(void)100 int hi_wifi_start_sta(void)
101 {
102 int ret;
103 char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0};
104 int len = sizeof(ifname);
105 const unsigned char wifi_vap_res_num = APP_INIT_VAP_NUM;
106 const unsigned char wifi_user_res_num = APP_INIT_USR_NUM;
107 unsigned int num = WIFI_SCAN_AP_LIMIT;
108
109 ret = hi_wifi_init(wifi_vap_res_num, wifi_user_res_num);
110 if (ret != HISI_OK) {
111 return -1;
112 }
113
114 ret = hi_wifi_sta_start(ifname, &len);
115 if (ret != HISI_OK) {
116 return -1;
117 }
118
119 /* register call back function to receive wifi event, etc scan results event,
120 * connected event, disconnected event.
121 */
122 ret = hi_wifi_register_event_callback(wifi_wpa_event_cb);
123 if (ret != HISI_OK) {
124 printf("register wifi event callback failed\n");
125 }
126
127 /* acquire netif for IP operation */
128 g_lwip_netif = netifapi_netif_find(ifname);
129 if (g_lwip_netif == NULL) {
130 printf("%s: get netif failed\n", __FUNCTION__);
131 return -1;
132 }
133
134 /* start scan, scan results event will be received soon */
135 ret = hi_wifi_sta_scan();
136 if (ret != HISI_OK) {
137 return -1;
138 }
139
140 sleep(5); /* sleep 5s, waiting for scan result. */
141
142 hi_wifi_ap_info *pst_results = malloc(sizeof(hi_wifi_ap_info) * WIFI_SCAN_AP_LIMIT);
143 if (pst_results == NULL) {
144 return -1;
145 }
146
147 ret = hi_wifi_sta_scan_results(pst_results, &num);
148 if (ret != HISI_OK) {
149 free(pst_results);
150 return -1;
151 }
152
153 for (unsigned int loop = 0; (loop < num) && (loop < WIFI_SCAN_AP_LIMIT); loop++) {
154 printf("SSID: %s\n", pst_results[loop].ssid);
155 }
156 free(pst_results);
157
158 /* if received scan results, select one SSID to connect */
159 ret = hi_wifi_start_connect();
160 if (ret != 0) {
161 return -1;
162 }
163
164 return 0;
165 }
166
hi_wifi_stop_sta(void)167 void hi_wifi_stop_sta(void)
168 {
169 int ret;
170
171 ret = hi_wifi_sta_stop();
172 if (ret != HISI_OK) {
173 printf("failed to stop sta\n");
174 }
175
176 ret = hi_wifi_deinit();
177 if (ret != HISI_OK) {
178 printf("failed to deinit wifi\n");
179 }
180
181 g_lwip_netif = NULL;
182 }
183