blob: e80c937b10dc2c82cf399542c920cbfaac54e84a [file] [log] [blame]
zw.wang00477802025-06-12 19:48:37 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include <pthread.h>
6#include <signal.h> // Include signal header
7
8#include "gsw_wifi_interface_sdk.h"
9
10// Hostapd 和 wpa_supplicant 进程 ID
11pid_t hostapd_pid = 0;
12pid_t wpa_pid = 0;
13
14// 辅助函数:将认证类型枚举转换为字符串
15const char *auth_type_to_string(gsw_wifi_auth_e auth)
16{
17 switch (auth)
18 {
19 case GSW_WIFI_AUTH_OPEN:
20 return "OPEN";
21 case GSW_WIFI_AUTH_WEP:
22 return "WEP";
23 case GSW_WIFI_AUTH_WPA_PSK:
24 return "WPA_PSK";
25 case GSW_WIFI_AUTH_WPA2_PSK:
26 return "WPA2_PSK";
27 case GSW_WIFI_AUTH_WPA3_PSK:
28 return "WPA3_PSK";
29 // 若有其他认证类型,可继续添加 case
30 default:
31 return "Unknown";
32 }
33}
34
35void handle_ctrl_c(int sig)
36{
37 printf("\nExiting...\n");
38 gsw_wifi_sta_stop();
39 gsw_wifi_ap_stop();
40 gsw_wifi_disable();
41 exit(0);
42}
43
44// 添加AP回调函数
45void ap_event_callback(void *arg, gsw_wifi_ap_status_e status)
46{
47 if (status == GSW_WIFI_STATUS_CONNECT)
48 {
49 printf("AP mode is now running and ready for connections\n");
50 }
51 else if (status == GSW_WIFI_STATUS_DISCONNECT)
52 {
53 printf("AP mode has been stopped\n");
54 }
55}
56
57void ap_setting(void)
58{
59 int ret = 0;
60 char ssid[32] = "mtbk-test";
61 char password[64] = "12345678";
62 gsw_wifi_auth_e auth = GSW_WIFI_AUTH_WPA2_PSK;
63 int frequency = 2;
64 gsw_wifi_bandwidth_type_e bandwidth = GSW_WIFI_BANDWIDTH_HT80;
65 int channel = 0;
66 int temp_auth, temp_bandwidth;
67
68 printf("AP mode configuration:\n");
69 printf("Enter SSID (default: mtbk-test): ");
70 if (scanf("%31s", ssid) != 1)
71 {
72 strcpy(ssid, "mtbk-test");
73 }
74
75 ret = gsw_wifi_ap_ssid_set(ssid);
76 if (ret != 0)
77 {
78 printf("Failed to set SSID\n");
79 gsw_wifi_ap_ssid_set("mtbk-test");
80 }
81
82 printf("Enter password (default: 12345678): ");
83 if (scanf("%63s", password) != 1)
84 {
85 strcpy(password, "12345678");
86 }
87
88 ret = gsw_wifi_ap_password_set(password);
89 if (ret != 0)
90 {
91 printf("Failed to set password\n");
92 gsw_wifi_ap_password_set("12345678");
93 }
94
95 printf("Enter auth type (0: OPEN, 1: WEP(Only support open), 2: WPA_PSK(wifi4 and above are not supported), 3: WPA2_PSK, 4: WPA3_PSK, default: WPA2_PSK): ");
96 if (scanf("%d", &temp_auth) == 1)
97 {
98 auth = (gsw_wifi_auth_e)temp_auth;
99 }
100
101 ret = gsw_wifi_ap_auth_set(auth);
102 if (ret != 0)
103 {
104 printf("Failed to set auth type\n");
105 gsw_wifi_ap_auth_set(GSW_WIFI_AUTH_WPA2_PSK);
106 }
107
108 printf("Enter frequency (1: 2.4G, 2: 5G, default: 2): ");
109 if (scanf("%d", &frequency) != 1)
110 {
111 frequency = 2;
112 }
113
114 ret = gsw_wifi_ap_frequency_set(frequency);
115 if (ret != 0)
116 {
117 printf("Failed to set frequency\n");
118 gsw_wifi_ap_frequency_set(2);
119 }
120
121 printf("Enter bandwidth (0: HT10, 1: HT20, 2: HT40, 3: HT80, default: HT40): ");
122 if (scanf("%d", &temp_bandwidth) == 1)
123 {
124 bandwidth = (gsw_wifi_bandwidth_type_e)temp_bandwidth;
125 }
126
127 ret = gsw_wifi_ap_bandwidth_set(bandwidth);
128 if (ret != 0)
129 {
130 printf("Failed to set bandwidth\n");
131 gsw_wifi_ap_bandwidth_set(GSW_WIFI_BANDWIDTH_HT40);
132 }
133
134 printf("Enter channel (0: auto, 2.4G: 1-13, 5G: 36-165, default: 0): ");
135 if (scanf("%d", &channel) != 1)
136 {
137 channel = 0;
138 }
139
140 ret = gsw_wifi_ap_channel_set(channel);
141 if (ret != 0)
142 {
143 printf("Failed to set channel\n");
144 gsw_wifi_ap_channel_set(0);
145 }
146
147 printf("AP mode configuration set.\n");
148}
149
150int main()
151{
152 signal(SIGINT, handle_ctrl_c); // Register signal handler
153 int ret = 0;
154 gsw_wifi_ap_run_status_e ap_status;
155 // 加载 Wi-Fi 驱动并启动 AP 模式
156 if (gsw_wifi_enable() != 0)
157 {
158 printf("Failed to enable Wi-Fi driver\n");
159 return -1;
160 }
161 gsw_wifi_get_ap_status(&ap_status);
162 printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
163begin:
164 printf("Do you want to set default AP mode? (y/n): ");
165 char ap_seting;
166 if (scanf(" %c", &ap_seting) != 1)
167 {
168 printf("Error reading input\n");
169 return -1;
170 }
171
172 if (ap_seting == 'y' || ap_seting == 'Y')
173 {
174 gsw_wifi_ap_ssid_set("mtbk-测试");
175 gsw_wifi_ap_password_set("12345678");
176 gsw_wifi_ap_auth_set(GSW_WIFI_AUTH_WPA2_PSK);
177 gsw_wifi_ap_frequency_set(2);
178 gsw_wifi_ap_bandwidth_set(GSW_WIFI_BANDWIDTH_HT40);
179 gsw_wifi_ap_channel_set(0);
180 }
181 else if (ap_seting == 'n' || ap_seting == 'N')
182 {
183 ap_setting();
184 }
185 else
186 {
187 printf("Invalid input\n");
188 return -1;
189 }
190
191 if (gsw_wifi_ap_start() != 0)
192 {
193 printf("Failed to start AP mode\n");
194 goto begin;
195 }
196 // 注册AP回调
197 if (gsw_wifi_reg_ap_event_callback(NULL, ap_event_callback) != 0)
198 {
199 printf("Failed to register AP event callback\n");
200 return -1;
201 }
202 gsw_wifi_get_ap_status(&ap_status);
203 printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
204 printf("Wi-Fi driver loaded and AP mode started.\n");
205
206 while (1)
207 {
208 char input[64];
209 printf("\nEnter command (1: start STA; 2: scan hotspots; 3: connect to hotspot; 4: ap get; q: exit; ap-status: check ap status; restart-ap .) ");
210 if (scanf("%63s", input) != 1)
211 { // Check scanf return value
212 printf("Error reading input\n");
213 break;
214 }
215
216 if (strcmp(input, "q") == 0)
217 {
218 break;
219 }
220 else if (strcmp(input, "ap-status") == 0)
221 {
222 gsw_wifi_get_ap_status(&ap_status);
223 printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
224 }
225 else if (strcmp(input, "restart-ap") == 0)
226 {
227 printf("Restarting AP...\n");
228 ap_setting();
229 gsw_wifi_ap_restart();
230 gsw_wifi_get_ap_status(&ap_status);
231 printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
232 }
233
234 else if (strcmp(input, "1") == 0)
235 {
236 // 启动 STA 模式
237 if (gsw_wifi_sta_start() != 0)
238 {
239 printf("Failed to start STA mode\n");
240 }
241 else
242 {
243 printf("STA mode started.\n");
244 }
245 }
246 else if (strcmp(input, "2") == 0)
247 {
248 // 搜索热点
249 gsw_scan_info_s scan_list[100];
250 int list_len;
251 if (gsw_wifi_get_scan_list(scan_list, 100, &list_len) == 0)
252 {
253 printf("Available hotspots:\n");
254 for (int i = 0; i < list_len; i++)
255 {
256 const char *auth_str = auth_type_to_string(scan_list[i].auth);
257 printf("%d. SSID: %s, BSSID: %s, RSSI: %d, Auth: %s\n", i + 1, scan_list[i].ssid, scan_list[i].mac, scan_list[i].rssi, auth_str);
258 }
259 }
260 else
261 {
262 printf("Failed to scan for hotspots\n");
263 }
264 }
265 else if (strcmp(input, "3") == 0)
266 {
267 // 输入对应的账户和密码进行连接,从扫描结果获取认证方式
268 gsw_scan_info_s scan_list[100];
269 int list_len;
270 if (gsw_wifi_get_scan_list(scan_list, 100, &list_len) == 0)
271 {
272 printf("Available hotspots:\n");
273 for (int i = 0; i < list_len; i++)
274 {
275 const char *auth_str = auth_type_to_string(scan_list[i].auth);
276 printf("%d. SSID: %s, BSSID: %s, RSSI: %d, Auth: %s\n", i + 1, scan_list[i].ssid, scan_list[i].mac, scan_list[i].rssi, auth_str);
277 }
278
279 int choice;
280 printf("Enter the number of the hotspot to connect: ");
281 if (scanf("%d", &choice) != 1)
282 { // Check scanf return value
283 printf("Error reading input\n");
284 continue;
285 }
286 if (choice < 1 || choice > list_len)
287 {
288 printf("Invalid choice\n");
289 continue;
290 }
291
292 // 直接从扫描结果中获取认证方式
293 gsw_wifi_auth_e auth = scan_list[choice - 1].auth;
294 const char *auth_str = auth_type_to_string(auth);
295 printf("Got authentication type from scan result: %s\n", auth_str);
296
297 char password[64] = ""; // 默认空密码
298 if (auth != GSW_WIFI_AUTH_OPEN)
299 {
300 printf("Enter password for SSID %s: ", scan_list[choice - 1].ssid);
301 if (scanf("%63s", password) != 1)
302 {
303 printf("Error reading input\n");
304 continue;
305 }
306 }
307 else
308 {
309 printf("Open network, skipping password input.\n");
310 }
311
312 if (gsw_wifi_sta_connect(scan_list[choice - 1].ssid, auth, password) != 0)
313 {
314 printf("Failed to connect to SSID %s\n", scan_list[choice - 1].ssid);
315 }
316 else
317 {
318 printf("Attempting to connect to SSID %s\n", scan_list[choice - 1].ssid);
319 // 等待连接成功
320 sleep(5);
321
322 // 获取连接的 SSID
323 char connected_ssid[33];
324 if (gsw_wifi_get_sta_ssid(connected_ssid) == 0)
325 {
326 printf("Connected to SSID: %s\n", connected_ssid);
327 }
328 else
329 {
330 printf("Failed to connect\n");
331 }
332 }
333 }
334 else
335 {
336 printf("Failed to scan for hotspots\n");
337 }
338 }
339 else if (strcmp(input, "4") == 0)
340 {
341 // 获取AP相关信息并格式化输出
342 printf("\nAP Configuration:\n");
343 printf("-----------------\n");
344
345 char ssid[33];
346 if (gsw_wifi_ap_ssid_get(ssid) == 0)
347 {
348 printf("SSID: %-20s\n", ssid);
349 }
350 else
351 {
352 printf("SSID: Failed to get\n");
353 }
354
355 int frequency;
356 if (gsw_wifi_ap_frequency_get(&frequency) == 0)
357 {
358 printf("Frequency: %-15s\n", frequency == 1 ? "2.4GHz" : "5GHz");
359 }
360 else
361 {
362 printf("Frequency: Failed to get\n");
363 }
364
365 gsw_wifi_bandwidth_type_e bandwidth;
366 if (gsw_wifi_ap_bandwidth_get(&bandwidth) == 0)
367 {
368 const char *bw_str[] = {"10MHz", "20MHz", "40MHz", "80MHz"};
369 printf("Bandwidth: %-15s\n", bandwidth >= 0 && bandwidth <= 3 ? bw_str[bandwidth] : "Unknown");
370 }
371 else
372 {
373 printf("Bandwidth: Failed to get\n");
374 }
375
376 int channel;
377 if (gsw_wifi_ap_channel_get(&channel) == 0)
378 {
379 printf("Channel: %-18d\n", channel);
380 }
381 else
382 {
383 printf("Channel: Failed to get\n");
384 }
385
386 gsw_wifi_auth_e auth;
387 if (gsw_wifi_ap_auth_get(&auth) == 0)
388 {
389 printf("Authentication: %-10s\n", auth_type_to_string(auth));
390 }
391 else
392 {
393 printf("Authentication: Failed to get\n");
394 }
395
396 char password[65];
397 if (gsw_wifi_ap_password_get(password) == 0)
398 {
399 printf("Password: %-18s\n", password);
400 }
401 else
402 {
403 printf("Password: Failed to get\n");
404 }
405
406 printf("-----------------\n");
407 }
408 else
409 {
410 printf("Invalid command\n");
411 }
412 }
413
414 // 停止 AP 和 STA 模式,卸载 Wi-Fi 驱动
415 gsw_wifi_ap_stop();
416 gsw_wifi_sta_stop();
417 gsw_wifi_disable();
418
419 printf("Wi-Fi driver unloaded and AP/STA modes stopped.\n");
420
421 return 0;
422}