blob: 6c653c38d9488ec2b6a76e91a5a376e51bc0235d [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;
zw.wanga8267052025-06-20 10:03:00 +080060 char ssid[33] = "mtbk-test";
zw.wang00477802025-06-12 19:48:37 +080061 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): ");
zw.wanga8267052025-06-20 10:03:00 +080070 if (scanf("%32s", ssid) != 1)
zw.wang00477802025-06-12 19:48:37 +080071 {
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 }
zw.wang41cd4162025-06-20 17:50:41 +0800225 else if (strcmp(input, "ap-stop") == 0)
226 {
227 printf("stop AP...\n");
228 gsw_wifi_ap_stop();
229 }
230 else if (strcmp(input, "ap-start") == 0)
231 {
232 printf("y/Y: no set ; other: reset\n");
233 if (scanf(" %c", &ap_seting) != 1)
234 {
235 printf("Error reading input\n");
236 }
237
238 if (ap_seting == 'y' || ap_seting == 'Y')
239 {
240 printf("ap is running, no need to set\n");
241 }
242 else
243 {
244 ap_setting();
245 }
246 gsw_wifi_ap_start();
247 gsw_wifi_get_ap_status(&ap_status);
248 printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
249 }
zw.wang00477802025-06-12 19:48:37 +0800250 else if (strcmp(input, "restart-ap") == 0)
251 {
252 printf("Restarting AP...\n");
253 ap_setting();
254 gsw_wifi_ap_restart();
255 gsw_wifi_get_ap_status(&ap_status);
256 printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
257 }
258
259 else if (strcmp(input, "1") == 0)
260 {
261 // 启动 STA 模式
262 if (gsw_wifi_sta_start() != 0)
263 {
264 printf("Failed to start STA mode\n");
265 }
266 else
267 {
268 printf("STA mode started.\n");
269 }
270 }
271 else if (strcmp(input, "2") == 0)
272 {
273 // 搜索热点
274 gsw_scan_info_s scan_list[100];
275 int list_len;
276 if (gsw_wifi_get_scan_list(scan_list, 100, &list_len) == 0)
277 {
278 printf("Available hotspots:\n");
279 for (int i = 0; i < list_len; i++)
280 {
281 const char *auth_str = auth_type_to_string(scan_list[i].auth);
282 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);
283 }
284 }
285 else
286 {
287 printf("Failed to scan for hotspots\n");
288 }
289 }
290 else if (strcmp(input, "3") == 0)
291 {
292 // 输入对应的账户和密码进行连接,从扫描结果获取认证方式
293 gsw_scan_info_s scan_list[100];
294 int list_len;
295 if (gsw_wifi_get_scan_list(scan_list, 100, &list_len) == 0)
296 {
297 printf("Available hotspots:\n");
298 for (int i = 0; i < list_len; i++)
299 {
300 const char *auth_str = auth_type_to_string(scan_list[i].auth);
301 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);
302 }
303
304 int choice;
305 printf("Enter the number of the hotspot to connect: ");
306 if (scanf("%d", &choice) != 1)
307 { // Check scanf return value
308 printf("Error reading input\n");
309 continue;
310 }
311 if (choice < 1 || choice > list_len)
312 {
313 printf("Invalid choice\n");
314 continue;
315 }
316
317 // 直接从扫描结果中获取认证方式
318 gsw_wifi_auth_e auth = scan_list[choice - 1].auth;
319 const char *auth_str = auth_type_to_string(auth);
320 printf("Got authentication type from scan result: %s\n", auth_str);
321
322 char password[64] = ""; // 默认空密码
323 if (auth != GSW_WIFI_AUTH_OPEN)
324 {
325 printf("Enter password for SSID %s: ", scan_list[choice - 1].ssid);
326 if (scanf("%63s", password) != 1)
327 {
328 printf("Error reading input\n");
329 continue;
330 }
331 }
332 else
333 {
334 printf("Open network, skipping password input.\n");
335 }
336
337 if (gsw_wifi_sta_connect(scan_list[choice - 1].ssid, auth, password) != 0)
338 {
339 printf("Failed to connect to SSID %s\n", scan_list[choice - 1].ssid);
340 }
341 else
342 {
343 printf("Attempting to connect to SSID %s\n", scan_list[choice - 1].ssid);
344 // 等待连接成功
345 sleep(5);
346
347 // 获取连接的 SSID
348 char connected_ssid[33];
349 if (gsw_wifi_get_sta_ssid(connected_ssid) == 0)
350 {
351 printf("Connected to SSID: %s\n", connected_ssid);
352 }
353 else
354 {
355 printf("Failed to connect\n");
356 }
357 }
358 }
359 else
360 {
361 printf("Failed to scan for hotspots\n");
362 }
363 }
364 else if (strcmp(input, "4") == 0)
365 {
366 // 获取AP相关信息并格式化输出
367 printf("\nAP Configuration:\n");
368 printf("-----------------\n");
369
370 char ssid[33];
371 if (gsw_wifi_ap_ssid_get(ssid) == 0)
372 {
373 printf("SSID: %-20s\n", ssid);
374 }
375 else
376 {
377 printf("SSID: Failed to get\n");
378 }
379
380 int frequency;
381 if (gsw_wifi_ap_frequency_get(&frequency) == 0)
382 {
383 printf("Frequency: %-15s\n", frequency == 1 ? "2.4GHz" : "5GHz");
384 }
385 else
386 {
387 printf("Frequency: Failed to get\n");
388 }
389
390 gsw_wifi_bandwidth_type_e bandwidth;
391 if (gsw_wifi_ap_bandwidth_get(&bandwidth) == 0)
392 {
393 const char *bw_str[] = {"10MHz", "20MHz", "40MHz", "80MHz"};
394 printf("Bandwidth: %-15s\n", bandwidth >= 0 && bandwidth <= 3 ? bw_str[bandwidth] : "Unknown");
395 }
396 else
397 {
398 printf("Bandwidth: Failed to get\n");
399 }
400
401 int channel;
402 if (gsw_wifi_ap_channel_get(&channel) == 0)
403 {
404 printf("Channel: %-18d\n", channel);
405 }
406 else
407 {
408 printf("Channel: Failed to get\n");
409 }
410
411 gsw_wifi_auth_e auth;
412 if (gsw_wifi_ap_auth_get(&auth) == 0)
413 {
414 printf("Authentication: %-10s\n", auth_type_to_string(auth));
415 }
416 else
417 {
418 printf("Authentication: Failed to get\n");
419 }
420
421 char password[65];
422 if (gsw_wifi_ap_password_get(password) == 0)
423 {
424 printf("Password: %-18s\n", password);
425 }
426 else
427 {
428 printf("Password: Failed to get\n");
429 }
430
431 printf("-----------------\n");
432 }
433 else
434 {
435 printf("Invalid command\n");
436 }
437 }
438
439 // 停止 AP 和 STA 模式,卸载 Wi-Fi 驱动
440 gsw_wifi_ap_stop();
441 gsw_wifi_sta_stop();
442 gsw_wifi_disable();
443
444 printf("Wi-Fi driver unloaded and AP/STA modes stopped.\n");
445
446 return 0;
447}