blob: e4237bcc4fe567791f099b27c47da0159662fc97 [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 模式
zw.wangbb7f28c2025-06-27 19:41:19 +0800156 gsw_wifi_sdk_interface_init();
zw.wang00477802025-06-12 19:48:37 +0800157 if (gsw_wifi_enable() != 0)
158 {
159 printf("Failed to enable Wi-Fi driver\n");
160 return -1;
161 }
162 gsw_wifi_get_ap_status(&ap_status);
163 printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
164begin:
165 printf("Do you want to set default AP mode? (y/n): ");
166 char ap_seting;
167 if (scanf(" %c", &ap_seting) != 1)
168 {
169 printf("Error reading input\n");
170 return -1;
171 }
172
173 if (ap_seting == 'y' || ap_seting == 'Y')
174 {
175 gsw_wifi_ap_ssid_set("mtbk-测试");
176 gsw_wifi_ap_password_set("12345678");
177 gsw_wifi_ap_auth_set(GSW_WIFI_AUTH_WPA2_PSK);
178 gsw_wifi_ap_frequency_set(2);
179 gsw_wifi_ap_bandwidth_set(GSW_WIFI_BANDWIDTH_HT40);
180 gsw_wifi_ap_channel_set(0);
181 }
182 else if (ap_seting == 'n' || ap_seting == 'N')
183 {
184 ap_setting();
185 }
186 else
187 {
188 printf("Invalid input\n");
189 return -1;
190 }
191
192 if (gsw_wifi_ap_start() != 0)
193 {
194 printf("Failed to start AP mode\n");
195 goto begin;
196 }
197 // 注册AP回调
198 if (gsw_wifi_reg_ap_event_callback(NULL, ap_event_callback) != 0)
199 {
200 printf("Failed to register AP event callback\n");
201 return -1;
202 }
203 gsw_wifi_get_ap_status(&ap_status);
204 printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
205 printf("Wi-Fi driver loaded and AP mode started.\n");
206
207 while (1)
208 {
209 char input[64];
210 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 .) ");
211 if (scanf("%63s", input) != 1)
212 { // Check scanf return value
213 printf("Error reading input\n");
214 break;
215 }
216
217 if (strcmp(input, "q") == 0)
218 {
219 break;
220 }
221 else if (strcmp(input, "ap-status") == 0)
222 {
223 gsw_wifi_get_ap_status(&ap_status);
224 printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
225 }
zw.wang41cd4162025-06-20 17:50:41 +0800226 else if (strcmp(input, "ap-stop") == 0)
227 {
228 printf("stop AP...\n");
229 gsw_wifi_ap_stop();
230 }
zw.wangbb7f28c2025-06-27 19:41:19 +0800231 else if (strcmp(input, "ap-mac") == 0)
232 {
233 char tmpmac[18U];
234 unsigned char apmac[18U];
235 gsw_wifi_get_interface_mac(GSW_WIFI_INTERFACE_1, tmpmac);
236 sscanf(tmpmac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &apmac[0], &apmac[1], &apmac[2], &apmac[3], &apmac[4], &apmac[5]);
237 printf("connect ap mac: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", apmac[0], apmac[1], apmac[2], apmac[3], apmac[4], apmac[5]);
238 }
239 else if (strcmp(input, "sta-mac") == 0)
240 {
241 char tmpmac[18U];
242 unsigned char sta_mac[18U];
243 gsw_wifi_get_interface_mac(GSW_WIFI_INTERFACE_0, tmpmac);
244 sscanf(tmpmac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &sta_mac[0], &sta_mac[1], &sta_mac[2], &sta_mac[3], &sta_mac[4], &sta_mac[5]);
245 printf("connect sta mac: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", sta_mac[0], sta_mac[1], sta_mac[2], sta_mac[3], sta_mac[4], sta_mac[5]);
246 }
zw.wang41cd4162025-06-20 17:50:41 +0800247 else if (strcmp(input, "ap-start") == 0)
248 {
249 printf("y/Y: no set ; other: reset\n");
250 if (scanf(" %c", &ap_seting) != 1)
251 {
252 printf("Error reading input\n");
253 }
254
255 if (ap_seting == 'y' || ap_seting == 'Y')
256 {
257 printf("ap is running, no need to set\n");
258 }
259 else
260 {
261 ap_setting();
262 }
263 gsw_wifi_ap_start();
264 gsw_wifi_get_ap_status(&ap_status);
265 printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
266 }
zw.wang00477802025-06-12 19:48:37 +0800267 else if (strcmp(input, "restart-ap") == 0)
268 {
269 printf("Restarting AP...\n");
270 ap_setting();
271 gsw_wifi_ap_restart();
272 gsw_wifi_get_ap_status(&ap_status);
273 printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
274 }
275
276 else if (strcmp(input, "1") == 0)
277 {
278 // 启动 STA 模式
279 if (gsw_wifi_sta_start() != 0)
280 {
281 printf("Failed to start STA mode\n");
282 }
283 else
284 {
285 printf("STA mode started.\n");
286 }
287 }
288 else if (strcmp(input, "2") == 0)
289 {
290 // 搜索热点
291 gsw_scan_info_s scan_list[100];
292 int list_len;
293 if (gsw_wifi_get_scan_list(scan_list, 100, &list_len) == 0)
294 {
295 printf("Available hotspots:\n");
296 for (int i = 0; i < list_len; i++)
297 {
298 const char *auth_str = auth_type_to_string(scan_list[i].auth);
299 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);
300 }
301 }
302 else
303 {
304 printf("Failed to scan for hotspots\n");
305 }
306 }
307 else if (strcmp(input, "3") == 0)
308 {
309 // 输入对应的账户和密码进行连接,从扫描结果获取认证方式
310 gsw_scan_info_s scan_list[100];
311 int list_len;
312 if (gsw_wifi_get_scan_list(scan_list, 100, &list_len) == 0)
313 {
314 printf("Available hotspots:\n");
315 for (int i = 0; i < list_len; i++)
316 {
317 const char *auth_str = auth_type_to_string(scan_list[i].auth);
318 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);
319 }
320
321 int choice;
322 printf("Enter the number of the hotspot to connect: ");
323 if (scanf("%d", &choice) != 1)
324 { // Check scanf return value
325 printf("Error reading input\n");
326 continue;
327 }
328 if (choice < 1 || choice > list_len)
329 {
330 printf("Invalid choice\n");
331 continue;
332 }
333
334 // 直接从扫描结果中获取认证方式
335 gsw_wifi_auth_e auth = scan_list[choice - 1].auth;
336 const char *auth_str = auth_type_to_string(auth);
337 printf("Got authentication type from scan result: %s\n", auth_str);
338
339 char password[64] = ""; // 默认空密码
340 if (auth != GSW_WIFI_AUTH_OPEN)
341 {
342 printf("Enter password for SSID %s: ", scan_list[choice - 1].ssid);
343 if (scanf("%63s", password) != 1)
344 {
345 printf("Error reading input\n");
346 continue;
347 }
348 }
349 else
350 {
351 printf("Open network, skipping password input.\n");
352 }
353
354 if (gsw_wifi_sta_connect(scan_list[choice - 1].ssid, auth, password) != 0)
355 {
356 printf("Failed to connect to SSID %s\n", scan_list[choice - 1].ssid);
357 }
358 else
359 {
360 printf("Attempting to connect to SSID %s\n", scan_list[choice - 1].ssid);
361 // 等待连接成功
362 sleep(5);
363
364 // 获取连接的 SSID
365 char connected_ssid[33];
366 if (gsw_wifi_get_sta_ssid(connected_ssid) == 0)
367 {
368 printf("Connected to SSID: %s\n", connected_ssid);
369 }
370 else
371 {
372 printf("Failed to connect\n");
373 }
374 }
375 }
376 else
377 {
378 printf("Failed to scan for hotspots\n");
379 }
380 }
381 else if (strcmp(input, "4") == 0)
382 {
383 // 获取AP相关信息并格式化输出
384 printf("\nAP Configuration:\n");
385 printf("-----------------\n");
386
387 char ssid[33];
388 if (gsw_wifi_ap_ssid_get(ssid) == 0)
389 {
390 printf("SSID: %-20s\n", ssid);
391 }
392 else
393 {
394 printf("SSID: Failed to get\n");
395 }
396
397 int frequency;
398 if (gsw_wifi_ap_frequency_get(&frequency) == 0)
399 {
400 printf("Frequency: %-15s\n", frequency == 1 ? "2.4GHz" : "5GHz");
401 }
402 else
403 {
404 printf("Frequency: Failed to get\n");
405 }
406
407 gsw_wifi_bandwidth_type_e bandwidth;
408 if (gsw_wifi_ap_bandwidth_get(&bandwidth) == 0)
409 {
410 const char *bw_str[] = {"10MHz", "20MHz", "40MHz", "80MHz"};
411 printf("Bandwidth: %-15s\n", bandwidth >= 0 && bandwidth <= 3 ? bw_str[bandwidth] : "Unknown");
412 }
413 else
414 {
415 printf("Bandwidth: Failed to get\n");
416 }
417
418 int channel;
419 if (gsw_wifi_ap_channel_get(&channel) == 0)
420 {
421 printf("Channel: %-18d\n", channel);
422 }
423 else
424 {
425 printf("Channel: Failed to get\n");
426 }
427
428 gsw_wifi_auth_e auth;
429 if (gsw_wifi_ap_auth_get(&auth) == 0)
430 {
431 printf("Authentication: %-10s\n", auth_type_to_string(auth));
432 }
433 else
434 {
435 printf("Authentication: Failed to get\n");
436 }
437
438 char password[65];
439 if (gsw_wifi_ap_password_get(password) == 0)
440 {
441 printf("Password: %-18s\n", password);
442 }
443 else
444 {
445 printf("Password: Failed to get\n");
446 }
447
448 printf("-----------------\n");
449 }
450 else
451 {
452 printf("Invalid command\n");
453 }
454 }
455
456 // 停止 AP 和 STA 模式,卸载 Wi-Fi 驱动
457 gsw_wifi_ap_stop();
458 gsw_wifi_sta_stop();
459 gsw_wifi_disable();
460
461 printf("Wi-Fi driver unloaded and AP/STA modes stopped.\n");
462
463 return 0;
464}