blob: 63d00cd75593ef1024d1db5cf0e850e283a774e2 [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 <sys/wait.h>
6#include <pthread.h>
7
8#include "gsw_wifi_interface_sdk.h"
9#include <dlfcn.h>
10#define MAX_COMMAND_LEN 512
11#define MAX_SSID_LEN 32
12#define MIN_PASSWORD_LEN 8
13#define MAX_PASSWORD_LEN 64
14#define MAX_IP_LEN 32
15#define MAX_MAC_LEN 32
16
17#define WLAN_STA_DEV "wlan0-vxd"
18#define WLAN_AP_DEV "wlan0"
19
20#define HOSTAPD_CONF_PATH "/etc/wifi/hostapd.conf"
21#define HOSTAPD_LOG_PATH "/etc/wifi/hostapd.log"
22#define WPA_SUPPLICANT_CONF_PATH "/etc/wifi/wpa_supplicant.conf"
23#define AIC8800_BSP_DRIVER_PATH "aic8800_bsp"
24#define AIC8800_FDRV_DRIVER_PATH "aic8800_fdrv"
25#define CTRL_INTERFACE "/var/run/hostapd"
26
27#define WPA_SUPPLICANT_CTRL_PATH "/var/run/wpa_supplicant"
28
29#define VALID_2G_CHANNELS {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
30#define VALID_5G_CHANNELS {36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 149, 153, 157, 161, 165}
31
32#ifndef LOG_ERR_LEVEL
33#define LOG_ERR_LEVEL 3 /* error conditions */
34#endif
35#ifndef LOG_WARN_LEVEL
36#define LOG_WARN_LEVEL 4 /* warning conditions */
37#endif
38#ifndef LOG_INFO_LEVEL
39#define LOG_INFO_LEVEL 6 /* informational */
40#endif
41#ifndef LOG_DEBUG_LEVEL
42#define LOG_DEBUG_LEVEL 7 /* debug-level messages */
43#endif
44#ifndef LOG_VERBOSE_LEVEL
45#define LOG_VERBOSE_LEVEL 8
46#endif
47
48#define GSW_UART "[HAL][GSW_WIFI]"
49
50#define LOGV(fmt, args...) \
51 do \
52 { \
53 char *file_ptr_1001 = __FILE__; \
54 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
55 char line_1001[10] = {0}; \
56 sprintf(line_1001, "%d", __LINE__); \
57 while (ptr_1001 >= file_ptr_1001 && *ptr_1001) \
58 { \
59 if (*ptr_1001 == '/') \
60 break; \
61 ptr_1001--; \
62 } \
63 fun_ptr_log(LOG_VERBOSE_LEVEL, "%s#%s: " GSW_UART "" fmt, ptr_1001 + 1, line_1001, ##args); \
64 } while (0)
65
66#define LOGI(fmt, args...) \
67 do \
68 { \
69 char *file_ptr_1001 = __FILE__; \
70 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
71 char line_1001[10] = {0}; \
72 sprintf(line_1001, "%d", __LINE__); \
73 while (ptr_1001 >= file_ptr_1001 && *ptr_1001) \
74 { \
75 if (*ptr_1001 == '/') \
76 break; \
77 ptr_1001--; \
78 } \
79 fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: " GSW_UART "" fmt, ptr_1001 + 1, line_1001, ##args); \
80 } while (0)
81
82#define LOGD(fmt, args...) \
83 do \
84 { \
85 char *file_ptr_1001 = __FILE__; \
86 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
87 char line_1001[10] = {0}; \
88 sprintf(line_1001, "%d", __LINE__); \
89 while (ptr_1001 >= file_ptr_1001 && *ptr_1001) \
90 { \
91 if (*ptr_1001 == '/') \
92 break; \
93 ptr_1001--; \
94 } \
95 fun_ptr_log(LOG_DEBUG_LEVEL, "%s#%s: " GSW_UART "" fmt, ptr_1001 + 1, line_1001, ##args); \
96 } while (0)
97
98#define LOGW(fmt, args...) \
99 do \
100 { \
101 char *file_ptr_1001 = __FILE__; \
102 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
103 char line_1001[10] = {0}; \
104 sprintf(line_1001, "%d", __LINE__); \
105 while (ptr_1001 >= file_ptr_1001 && *ptr_1001) \
106 { \
107 if (*ptr_1001 == '/') \
108 break; \
109 ptr_1001--; \
110 } \
111 fun_ptr_log(LOG_WARN_LEVEL, "%s#%s: " GSW_UART "" fmt, ptr_1001 + 1, line_1001, ##args); \
112 } while (0)
113
114#define LOGE(fmt, args...) \
115 do \
116 { \
117 char *file_ptr_1001 = __FILE__; \
118 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
119 char line_1001[10] = {0}; \
120 sprintf(line_1001, "%d", __LINE__); \
121 while (ptr_1001 >= file_ptr_1001 && *ptr_1001) \
122 { \
123 if (*ptr_1001 == '/') \
124 break; \
125 ptr_1001--; \
126 } \
127 fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: " GSW_UART "" fmt, ptr_1001 + 1, line_1001, ##args); \
128 } while (0)
129
130typedef void (*mbtk_log)(int level, const char *format, ...);
131static mbtk_log fun_ptr_log = NULL;
132void *dlHandle_wifi = NULL;
133char *lynqLib_wifi = "/lib/libmbtk_lib.so";
134
135static int handle()
136{
137 if (dlHandle_wifi == NULL || fun_ptr_log == NULL)
138 {
139 dlHandle_wifi = dlopen(lynqLib_wifi, RTLD_NOW);
140 fun_ptr_log = (mbtk_log)dlsym(dlHandle_wifi, "mbtk_log");
141 if (fun_ptr_log == NULL || dlHandle_wifi == NULL)
142 {
143 return GSW_HAL_NORMAL_FAIL;
144 }
145 }
146 return GSW_HAL_SUCCESS;
147}
148
149// 辅助函数:执行系统命令
150int execute_command(const char *cmd)
151{
152 int status = system(cmd);
153 if (status == -1)
154 {
155 LOGE("Failed to execute command");
156 return GSW_HAL_NORMAL_FAIL;
157 }
158 if (WIFEXITED(status))
159 {
160 return WEXITSTATUS(status);
161 }
162 else
163 {
164 // 子进程异常退出,比如被信号终止
165 return GSW_HAL_NORMAL_FAIL;
166 }
167}
168
169// 辅助函数:读取文件内容到缓冲区
170int read_file(const char *path, char *buffer, size_t buffer_len)
171{
172 FILE *file = fopen(path, "r");
173 if (!file)
174 return GSW_HAL_NORMAL_FAIL;
175 size_t length = fread(buffer, 1, buffer_len - 1, file);
176 if (length == 0)
177 {
178 fclose(file);
179 return GSW_HAL_NORMAL_FAIL;
180 }
181 buffer[length] = '\0';
182 fclose(file);
183 return (int)length;
184}
185
186// 新增:静态互斥锁用于保护文件写入操作
187static pthread_mutex_t write_file_mutex = PTHREAD_MUTEX_INITIALIZER;
188
189int write_file(const char *path, const char *buffer)
190{
191 // 加锁:确保同一时间只有一个线程执行文件写入
192 pthread_mutex_lock(&write_file_mutex);
193
194 FILE *file = fopen(path, "w");
195 if (!file)
196 {
197 // 解锁后返回错误
198 pthread_mutex_unlock(&write_file_mutex);
199 return GSW_HAL_NORMAL_FAIL;
200 }
201
202 fputs(buffer, file);
203 fclose(file);
204
205 // 解锁:释放锁资源
206 pthread_mutex_unlock(&write_file_mutex);
207 return GSW_HAL_SUCCESS;
208}
209
210// 辅助函数:检查 hostapd 是否正在运行
211int is_hostapd_running()
212{
213 char cmd[MAX_COMMAND_LEN];
214 snprintf(cmd, sizeof(cmd), "ps | grep 'hostapd' | grep -v -E 'grep|global'");
215 FILE *output = popen(cmd, "r");
216 if (!output)
217 {
218 return GSW_HAL_NORMAL_FAIL;
219 }
220 char buffer[1]; // 只需检查是否有输出,读取一个字符即可
221 int result = fread(buffer, 1, 1, output);
222 pclose(output);
223 if (result > 0)
224 {
225 return GSW_HAL_SUCCESS; // 找到指定配置的hostapd进程
226 }
227 return GSW_HAL_NORMAL_FAIL; // 未找到
228}
229
230int gsw_wifi_ap_start()
231{
232 if (handle())
233 return GSW_HAL_NORMAL_FAIL;
234 // 强制终止所有hostapd进程(关键新增:避免残留进程占用资源)
235 execute_command("killall -9 hostapd >/dev/null 2>&1"); // 9信号强制终止
236 sleep(1); // 等待进程完全退出
237
238 // 清理网络桥接接口(关键新增:避免br-lan绑定冲突)
239 char brctl_cmd[MAX_COMMAND_LEN];
240 snprintf(brctl_cmd, sizeof(brctl_cmd), "brctl delif br-lan %s >/dev/null 2>&1", WLAN_AP_DEV);
241 execute_command(brctl_cmd);
242
243 // 检查并删除残留的控制接口文件(原有逻辑保留)
244 char ctrl_path[MAX_COMMAND_LEN];
245 snprintf(ctrl_path, sizeof(ctrl_path), "%s/%s", CTRL_INTERFACE, WLAN_AP_DEV);
246 if (access(ctrl_path, F_OK) == 0)
247 {
248 if (remove(ctrl_path) != 0)
249 {
250 LOGE("Failed to remove old hostapd control interface file");
251 return GSW_HAL_NORMAL_FAIL;
252 }
253 }
254
255 // 启动hostapd(原有逻辑保留)
256 char cmd[MAX_COMMAND_LEN];
257 snprintf(cmd, sizeof(cmd), "echo ap starting > %s ;hostapd -q %s >> %s &", HOSTAPD_LOG_PATH, HOSTAPD_CONF_PATH, HOSTAPD_LOG_PATH);
258 int status = execute_command(cmd);
259 sleep(8);
260
261 char log_buffer[1024] = {0};
262 if (read_file(HOSTAPD_LOG_PATH, log_buffer, sizeof(log_buffer)) != -1)
263 {
264 // 检查日志中是否包含关键错误信息(示例:"Failed"、"error")
265 if (strstr(log_buffer, "failed") || strstr(log_buffer, "error") || strstr(log_buffer, "wasn't started"))
266 {
267 LOGE("Hostapd start failed, check log: %s", log_buffer);
268 return GSW_HAL_NORMAL_FAIL;
269 }
270 if(is_hostapd_running() == GSW_HAL_SUCCESS)
271 {
272 LOGI("Hostapd start success");
273 }
274 else
275 {
276 LOGE("Hostapd start failed, check log: %s", log_buffer);
277 return GSW_HAL_NORMAL_FAIL;
278 }
279 }
280 else
281 {
282 if (is_hostapd_running() != GSW_HAL_SUCCESS)
283 {
284 LOGE("Failed to read hostapd log file: %s", HOSTAPD_LOG_PATH);
285 return GSW_HAL_NORMAL_FAIL;
286 }
287 }
288 return status;
289}
290
291// 辅助函数:判断 wpa_supplicant 服务是否起来
292int is_wpa_supplicant_running()
293{
294 char cmd[MAX_COMMAND_LEN];
295 snprintf(cmd, sizeof(cmd), "ps | grep wpa_supplicant | grep -v grep");
296 FILE *output = popen(cmd, "r");
297 if (!output)
298 {
299 return GSW_HAL_NORMAL_FAIL;
300 }
301 char buffer[1]; // 只需检查是否有输出,读取一个字符即可
302 int result = fread(buffer, 1, 1, output);
303 pclose(output);
304 if (result > 0)
305 {
306 return GSW_HAL_SUCCESS; // wpa_supplicant 服务存在
307 }
308 return GSW_HAL_NORMAL_FAIL; // wpa_supplicant 服务不存在
309}
310
311typedef struct
312{
313 void *arg;
314 GSW_AP_CALLBACK_FUNC_PTR gsw_cb;
315 pthread_t thread_id;
316 int running;
317} AP_Event_Context;
318
319static AP_Event_Context ap_event_context;
320int gsw_wifi_ap_stop()
321{
322 if (handle())
323 return GSW_HAL_NORMAL_FAIL;
324 if (is_hostapd_running() != 0)
325 {
326 LOGI("Hostapd is not running, no need to stop.\n");
327 return GSW_HAL_SUCCESS;
328 }
329
330 // 先停止事件监听线程
331 if (ap_event_context.running)
332 {
333 ap_event_context.running = 0;
334 if (ap_event_context.thread_id != 0)
335 {
336 pthread_join(ap_event_context.thread_id, NULL);
337 ap_event_context.thread_id = 0;
338 }
339 }
340
341 char cmd[MAX_COMMAND_LEN];
342 snprintf(cmd, sizeof(cmd), "killall hostapd");
343 int status = execute_command(cmd);
344
345 // 关闭对应的 hostapd 后,删除对应的文件
346 char path[MAX_COMMAND_LEN];
347 snprintf(path, sizeof(path), "%s/%s", CTRL_INTERFACE, WLAN_AP_DEV);
348 if (access(path, F_OK) == 0)
349 {
350 if (remove(path) != 0)
351 {
352 LOGE("Failed to remove hostapd control interface file");
353 }
354 }
355
356 return status;
357}
358
359int gsw_wifi_ap_restart(void)
360{
361 if (handle())
362 return GSW_HAL_NORMAL_FAIL;
363 int ret = 0;
364 char cmd[MAX_COMMAND_LEN];
365 snprintf(cmd, sizeof(cmd), "killall hostapd");
366 int status = execute_command(cmd);
367 LOGI("killall hostapd status = %d\n", status);
368 // 关闭对应的 hostapd 后,删除对应的文件
369 char path[MAX_COMMAND_LEN];
370 snprintf(path, sizeof(path), "%s/%s", CTRL_INTERFACE, WLAN_AP_DEV);
371 if (access(path, F_OK) == 0)
372 {
373 if (remove(path) != 0)
374 {
375 LOGE("Failed to remove hostapd control interface file");
376 }
377 }
378 ret = gsw_wifi_ap_start();
379 return ret;
380}
381
382int gsw_wifi_ap_ssid_set(char *ssid)
383{
384 if (handle())
385 return GSW_HAL_NORMAL_FAIL;
386 char buffer[4096] = {0};
387 char new_config[4096] = {0};
388 if (ssid == NULL) {
389 LOGE("Password cannot be NULL");
390 return GSW_HAL_NORMAL_FAIL;
391 }
392 size_t ssid_len = strlen(ssid);
393 if (ssid_len > MAX_SSID_LEN) {
394 LOGE("Password length must be 8-64 characters, current length: %zu", ssid_len);
395 return GSW_HAL_NORMAL_FAIL;
396 }
397 // 读取现有配置
398 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
399 {
400 // 如果文件不存在,创建新配置(修改默认配置内容)
401 FILE *conf = fopen(HOSTAPD_CONF_PATH, "w");
402 if (!conf)
403 return GSW_HAL_NORMAL_FAIL;
404
405 // 写入用户指定的完整默认配置
406 fprintf(conf, "ctrl_interface=%s\n", CTRL_INTERFACE);
407 fprintf(conf, "ctrl_interface_group=0\n");
408 fprintf(conf, "interface=%s\n", WLAN_AP_DEV);
409 fprintf(conf, "driver=nl80211\n");
410 fprintf(conf, "bridge=br-lan\n");
411 fprintf(conf, "ssid=%s\n", ssid); // 使用传入的ssid
zw.wang3e022be2025-06-18 09:39:23 +0800412 fprintf(conf, "hw_mode=g\n");
zw.wang00477802025-06-12 19:48:37 +0800413 fprintf(conf, "ieee80211d=1\n");
zw.wang3e022be2025-06-18 09:39:23 +0800414 fprintf(conf, "channel=0\n");
zw.wang00477802025-06-12 19:48:37 +0800415 fprintf(conf, "auth_algs=1\n");
416 fprintf(conf, "wme_enabled=1\n");
417 fprintf(conf, "noscan=0\n");
418 fprintf(conf, "beacon_int=100\n");
zw.wang3e022be2025-06-18 09:39:23 +0800419 fprintf(conf, "wpa=3\n");
zw.wang00477802025-06-12 19:48:37 +0800420 fprintf(conf, "wpa_passphrase=12345678\n");
421 fprintf(conf, "ieee80211n=1\n");
422 fprintf(conf, "ieee80211ac=1\n");
423 fprintf(conf, "ieee80211ax=1\n");
zw.wang3e022be2025-06-18 09:39:23 +0800424 fprintf(conf, "vht_oper_chwidth=0\n");
425 fprintf(conf, "vht_oper_centr_freq_seg0_idx=0\n");
426 fprintf(conf, "he_oper_chwidth=0\n");
427 fprintf(conf, "he_oper_centr_freq_seg0_idx=0\n");
zw.wang00477802025-06-12 19:48:37 +0800428 fprintf(conf, "he_basic_mcs_nss_set=65534\n");
zw.wang3e022be2025-06-18 09:39:23 +0800429 fprintf(conf, "he_su_beamformee=0\n");
zw.wang00477802025-06-12 19:48:37 +0800430 fprintf(conf, "he_twt_required=0\n");
431 fprintf(conf, "vht_capab=[SHORT-GI-80][VHT40+][VHT40-][MAX-A-MPDU-LEN-EXP7][RX-STBC-1][RX-LDPC]\n");
432 fprintf(conf, "ht_capab=[SHORT-GI-20][SHORT-GI-40][HT40+][HT40-][LDPC][RX-STBC1]\n");
433 fprintf(conf, "wpa_key_mgmt=WPA-PSK\n");
zw.wang3e022be2025-06-18 09:39:23 +0800434 fprintf(conf, "wpa_pairwise=TKIP CCMP\n");
zw.wang00477802025-06-12 19:48:37 +0800435 fprintf(conf, "rsn_pairwise=CCMP\n");
zw.wang00477802025-06-12 19:48:37 +0800436 fprintf(conf, "ignore_broadcast_ssid=0\n");
437 fprintf(conf, "country_code=CN\n");
438 fprintf(conf, "max_num_sta=32\n");
439 fprintf(conf, "macaddr_acl=0\n");
440 fprintf(conf, "deny_mac_file=/etc/wifi/hostapd.deny\n");
441 fprintf(conf, "accept_mac_file=/etc/wifi/hostapd.accept\n");
442 fprintf(conf, "sae_pwe=2\n");
443
444 fclose(conf);
445 return GSW_HAL_SUCCESS;
446 }
447
448 // 处理每一行(新增ctrl_interface和ctrl_interface_group的检查)
449 char *line = strtok(buffer, "\n");
450 int ssid_processed = 0; // 标记是否已处理过ssid行
451 while (line)
452 {
453 if (!ssid_processed && strncmp(line, "ssid=", 5) == 0)
454 {
455 char ssid_line[64];
456 snprintf(ssid_line, sizeof(ssid_line), "ssid=%s", ssid);
457 strcat(new_config, ssid_line);
458 strcat(new_config, "\n");
459 ssid_processed = 1;
460 line = strtok(NULL, "\n");
461 continue;
462 }
463 else if (strncmp(line, "ssid=", 5) == 0)
464 {
465 line = strtok(NULL, "\n");
466 continue;
467 }
468 else if (strncmp(line, "ctrl_interface=", 15) == 0)
469 {
470 char ctrl_line[128];
471 snprintf(ctrl_line, sizeof(ctrl_line), "ctrl_interface=%s", CTRL_INTERFACE);
472 strcat(new_config, ctrl_line);
473 strcat(new_config, "\n");
474 }
475 else if (strncmp(line, "ctrl_interface_group=", 21) == 0)
476 {
477 char ctrl_group_line[64];
478 snprintf(ctrl_group_line, sizeof(ctrl_group_line), "ctrl_interface_group=0");
479 strcat(new_config, ctrl_group_line);
480 strcat(new_config, "\n");
481 }
482 else
483 {
484 strcat(new_config, line);
485 strcat(new_config, "\n");
486 }
487 line = strtok(NULL, "\n");
488 }
489
490 if (!ssid_processed)
491 {
492 char ssid_line[64];
493 snprintf(ssid_line, sizeof(ssid_line), "ssid=%s\n", ssid);
494 strcat(new_config, ssid_line);
495 }
496
497 return write_file(HOSTAPD_CONF_PATH, new_config);
498}
499
500int gsw_wifi_ap_ssid_get(char *ssid)
501{
502 if (handle())
503 return GSW_HAL_NORMAL_FAIL;
504 char buffer[1024] = {0};
505 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
506 {
507 return GSW_HAL_NORMAL_FAIL;
508 }
509 char *ssid_line = strstr(buffer, "ssid=");
510 if (ssid_line)
511 {
512 ssid_line += 5; // 跳过 "ssid="
513 char *end = strchr(ssid_line, '\n');
514 if (end)
515 *end = '\0';
516 strncpy(ssid, ssid_line, MAX_SSID_LEN);
517 }
518 return GSW_HAL_SUCCESS;
519}
520
521int gsw_wifi_ap_frequency_set(int gsw_wifi_frequency)
522{
523 if (handle())
524 return GSW_HAL_NORMAL_FAIL;
525 char buffer[4096] = {0};
526 char new_config[4096] = {0};
527
528 gsw_wifi_bandwidth_type_e gsw_wifi_bandwidth;
529 gsw_wifi_ap_bandwidth_get(&gsw_wifi_bandwidth);
530 if (gsw_wifi_bandwidth == GSW_WIFI_BANDWIDTH_HT80 && gsw_wifi_frequency == 1)
531 {
532 LOGE("HT80 cannot be set to 2.4G");
533 return GSW_HAL_NORMAL_FAIL;
534 }
535 // 读取现有配置
536 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
537 {
538 return GSW_HAL_NORMAL_FAIL;
539 }
540
541 // 处理每一行
542 char *line = strtok(buffer, "\n");
543 while (line)
544 {
545 // 如果是hw_mode行,替换为新值
546 if (strstr(line, "hw_mode=") != NULL)
547 {
548 char mode_line[32];
549 snprintf(mode_line, sizeof(mode_line), "hw_mode=%c", gsw_wifi_frequency == 1 ? 'g' : 'a');
550 strcat(new_config, mode_line);
551 }
552 else
553 {
554 strcat(new_config, line);
555 }
556 strcat(new_config, "\n");
557 line = strtok(NULL, "\n");
558 }
559
560 // 如果没有找到hw_mode行,添加新的设置
561 if (strstr(new_config, "hw_mode=") == NULL)
562 {
563 char mode_line[32];
564 snprintf(mode_line, sizeof(mode_line), "hw_mode=%c\n", gsw_wifi_frequency == 1 ? 'g' : 'a');
565 strcat(new_config, mode_line);
566 }
567
568 // 写回文件
569 return write_file(HOSTAPD_CONF_PATH, new_config);
570}
571
572int gsw_wifi_ap_frequency_get(int *gsw_wifi_frequency)
573{
574 if (handle())
575 return GSW_HAL_NORMAL_FAIL;
576 char buffer[1024] = {0};
577 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
578 {
579 return GSW_HAL_NORMAL_FAIL;
580 }
581 char *mode_line = strstr(buffer, "hw_mode=");
582 if (mode_line)
583 {
584 mode_line += 8; // 跳过 "hw_mode="
585 *gsw_wifi_frequency = (mode_line[0] == 'g') ? 1 : 2;
586 }
587 return GSW_HAL_SUCCESS;
588}
589
590int gsw_wifi_ap_bandwidth_set(gsw_wifi_bandwidth_type_e bandwidth)
591{
592 if (handle())
593 return GSW_HAL_NORMAL_FAIL;
594 char buffer[4096] = {0};
595 char new_config[4096] = {0};
596
597 int current_freq;
598 if (gsw_wifi_ap_frequency_get(&current_freq) != 0)
599 {
600 LOGI("Failed to get current frequency\n");
601 return GSW_HAL_NORMAL_FAIL;
602 }
603 if (current_freq == 1 && bandwidth == GSW_WIFI_BANDWIDTH_HT80) // 1表示2.4GHz
604 {
605 LOGI("2.4GHz band does not support 80MHz bandwidth");
606 return GSW_HAL_NORMAL_FAIL;
607 }
608 // 读取现有配置
609 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
610 return GSW_HAL_NORMAL_FAIL;
611
612 // 定义各带宽类型对应的替换规则(新增三个参数)
613 const char *ht_capab = NULL;
614 const char *vht_capab = NULL;
615 const char *vht_oper_chwidth = NULL;
616 const char *he_oper_chwidth = NULL;
617 int vht_oper_centr_freq_seg0_idx_val = 0;
618 int he_oper_centr_freq_seg0_idx_val = 0;
619 int he_su_beamformee_val = 0;
620
621 // 根据带宽类型初始化替换值(补充新增参数逻辑)
622 switch (bandwidth)
623 {
624 case GSW_WIFI_BANDWIDTH_HT20:
625 ht_capab = "ht_capab=[HT20][SHORT-GI-20][LDPC][RX-STBC1]";
626 vht_capab = "vht_capab=[VHT20][SHORT-GI-20][MAX-A-MPDU-LEN-EXP7]";
627 vht_oper_chwidth = "vht_oper_chwidth=0";
628 he_oper_chwidth = "he_oper_chwidth=0";
629 vht_oper_centr_freq_seg0_idx_val = 0;
630 he_oper_centr_freq_seg0_idx_val = 0;
631 he_su_beamformee_val = 0;
632 break;
633 case GSW_WIFI_BANDWIDTH_HT40:
634 ht_capab = "ht_capab=[SHORT-GI-20][SHORT-GI-40][HT40+][HT40-][LDPC][RX-STBC1]";
635 vht_capab = "vht_capab=[SHORT-GI-80][VHT40+][VHT40-][MAX-A-MPDU-LEN-EXP7][RX-STBC-1][RX-LDPC]";
636 vht_oper_chwidth = "vht_oper_chwidth=0";
637 he_oper_chwidth = "he_oper_chwidth=0";
638 vht_oper_centr_freq_seg0_idx_val = 0;
639 he_oper_centr_freq_seg0_idx_val = 0;
640 he_su_beamformee_val = 0;
641 break;
642 case GSW_WIFI_BANDWIDTH_HT80:
643 ht_capab = "ht_capab=[HT20][HT40+][HT40-][SHORT-GI-40][LDPC][RX-STBC1]";
644 vht_capab = "vht_capab=[VHT40][VHT80][SHORT-GI-80][MAX-A-MPDU-LEN-EXP7]";
645 vht_oper_chwidth = "vht_oper_chwidth=1";
646 he_oper_chwidth = "he_oper_chwidth=1";
647 vht_oper_centr_freq_seg0_idx_val = 42;
648 he_oper_centr_freq_seg0_idx_val = 42;
649 he_su_beamformee_val = 1;
650 break;
651 default:
652 return GSW_HAL_NORMAL_FAIL;
653 }
654
655 // 新增标记:确保只处理第一个ht_capab行
656 int ht_capab_processed = 0;
657
658 // 逐行处理配置文件(增加新增参数的匹配)
659 char *line = strtok(buffer, "\n");
660 while (line)
661 {
662 // 处理ht_capab行(仅替换第一个以"ht_capab="开头的行)
663 if (!ht_capab_processed && strncmp(line, "ht_capab=", 9) == 0)
664 {
665 if (ht_capab)
666 {
667 strcat(new_config, ht_capab);
668 ht_capab_processed = 1; // 标记已处理
669 }
670 else
671 {
672 strcat(new_config, line);
673 }
674 strcat(new_config, "\n");
675 line = strtok(NULL, "\n");
676 continue;
677 }
678 else if (strncmp(line, "ht_capab=", 9) == 0)
679 {
680 line = strtok(NULL, "\n");
681 continue;
682 }
683 else if (strncmp(line, "vht_oper_centr_freq_seg0_idx=", 25) == 0)
684 {
685 char vht_centr_line[64];
686 snprintf(vht_centr_line, sizeof(vht_centr_line), "vht_oper_centr_freq_seg0_idx=%d", vht_oper_centr_freq_seg0_idx_val);
687 strcat(new_config, vht_centr_line);
688 strcat(new_config, "\n");
689 }
690 else if (strncmp(line, "he_oper_centr_freq_seg0_idx=", 25) == 0)
691 {
692 char he_centr_line[64];
693 snprintf(he_centr_line, sizeof(he_centr_line), "he_oper_centr_freq_seg0_idx=%d", he_oper_centr_freq_seg0_idx_val);
694 strcat(new_config, he_centr_line);
695 strcat(new_config, "\n");
696 }
697 else if (strncmp(line, "he_su_beamformee=", 17) == 0)
698 {
699 char he_beam_line[32];
700 snprintf(he_beam_line, sizeof(he_beam_line), "he_su_beamformee=%d", he_su_beamformee_val);
701 strcat(new_config, he_beam_line);
702 strcat(new_config, "\n");
703 }
704 else if (strncmp(line, "vht_capab=", 10) == 0)
705 {
706 if (vht_capab)
707 strcat(new_config, vht_capab);
708 else
709 strcat(new_config, line);
710 strcat(new_config, "\n");
711 }
712 else if (strncmp(line, "vht_oper_chwidth=", 17) == 0)
713 {
714 if (vht_oper_chwidth)
715 strcat(new_config, vht_oper_chwidth);
716 else
717 strcat(new_config, line);
718 strcat(new_config, "\n");
719 }
720 else if (strncmp(line, "he_oper_chwidth=", 16) == 0)
721 {
722 if (he_oper_chwidth)
723 strcat(new_config, he_oper_chwidth);
724 else
725 strcat(new_config, line);
726 strcat(new_config, "\n");
727 }
728 else
729 {
730 strcat(new_config, line);
731 strcat(new_config, "\n");
732 }
733 line = strtok(NULL, "\n");
734 }
735
736 // 如果原文件无ht_capab行,补充新行(保持原有逻辑)
737 if (!ht_capab_processed && ht_capab)
738 {
739 strcat(new_config, ht_capab);
740 strcat(new_config, "\n");
741 }
742
743 // 检查vht_oper_centr_freq_seg0_idx是否已处理
744 if (!strstr(new_config, "vht_oper_centr_freq_seg0_idx="))
745 {
746 char vht_centr_line[64];
747 snprintf(vht_centr_line, sizeof(vht_centr_line), "vht_oper_centr_freq_seg0_idx=%d\n", vht_oper_centr_freq_seg0_idx_val);
748 strcat(new_config, vht_centr_line);
749 }
750 // 检查he_oper_centr_freq_seg0_idx是否已处理
751 if (!strstr(new_config, "he_oper_centr_freq_seg0_idx="))
752 {
753 char he_centr_line[64];
754 snprintf(he_centr_line, sizeof(he_centr_line), "he_oper_centr_freq_seg0_idx=%d\n", he_oper_centr_freq_seg0_idx_val);
755 strcat(new_config, he_centr_line);
756 }
757 // 检查he_su_beamformee是否已处理
758 if (!strstr(new_config, "he_su_beamformee="))
759 {
760 char he_beam_line[32];
761 snprintf(he_beam_line, sizeof(he_beam_line), "he_su_beamformee=%d\n", he_su_beamformee_val);
762 strcat(new_config, he_beam_line);
763 }
764
765 return write_file(HOSTAPD_CONF_PATH, new_config);
766}
767
768
769int gsw_wifi_ap_bandwidth_get(gsw_wifi_bandwidth_type_e *bandwidth)
770{
771 if (handle())
772 return GSW_HAL_NORMAL_FAIL;
773 char buffer[1024] = {0};
774 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
775 {
776 return GSW_HAL_NORMAL_FAIL;
777 }
778
779 // 精确解析ht_capab参数
780 char *ht_line = strstr(buffer, "ht_capab=");
781 char *vht_line = strstr(buffer, "vht_capab=");
782 char *vht_width = strstr(buffer, "vht_oper_chwidth=1");
783
784 // 优先检查80MHz配置
785 if (vht_width && vht_line && strstr(vht_line, "VHT80"))
786 {
787 *bandwidth = GSW_WIFI_BANDWIDTH_HT80;
788 }
789 else if (ht_line && strstr(ht_line, "HT40+"))
790 {
791 *bandwidth = GSW_WIFI_BANDWIDTH_HT40;
792 }
793 else
794 {
795 *bandwidth = GSW_WIFI_BANDWIDTH_HT20;
796 }
797
798 return GSW_HAL_SUCCESS;
799}
800
801int gsw_wifi_ap_channel_set(int channel)
802{
803 if (handle())
804 return GSW_HAL_NORMAL_FAIL;
805 // 新增频率和信道校验
806 int current_freq;
807 if (gsw_wifi_ap_frequency_get(&current_freq) != 0)
808 {
809 LOGE("Failed to get current frequency\n");
810 return GSW_HAL_NORMAL_FAIL;
811 }
812 // 定义各频段支持的信道范围
813 const int valid_2g_channels[] = VALID_2G_CHANNELS;
814 const int valid_5g_channels[] = VALID_5G_CHANNELS;
815
816 int valid = 0;
817 if (current_freq == 1)
818 { // 2.4GHz
819 for (int i = 0; i < sizeof(valid_2g_channels) / sizeof(int); i++)
820 {
821 if (channel == valid_2g_channels[i])
822 {
823 valid = 1;
824 break;
825 }
826 }
827 }
828 else
829 { // 5GHz
830 for (int i = 0; i < sizeof(valid_5g_channels) / sizeof(int); i++)
831 {
832 if (channel == valid_5g_channels[i])
833 {
834 valid = 1;
835 break;
836 }
837 }
838 }
839
840 if (!valid && channel != 0)
841 { // 允许0表示自动选择
842 LOGI("Invalid channel %d for %s band\n",
843 channel, (current_freq == 1) ? "2.4GHz" : "5GHz");
844 return GSW_HAL_NORMAL_FAIL;
845 }
846
847 // 修改为读取整个文件并替换channel行
848 char buffer[4096] = {0};
849 char new_config[4096] = {0};
850 int noscan_set = 0; // 新增:标记是否已处理noscan行
851
852 // 读取现有配置
853 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
854 {
855 return GSW_HAL_NORMAL_FAIL;
856 }
857
858 // 处理每一行
859 char *line = strtok(buffer, "\n");
860 while (line)
861 {
862 // 新增:处理noscan行
863 if (strstr(line, "noscan=") != NULL)
864 {
865 char noscan_line[32];
866 snprintf(noscan_line, sizeof(noscan_line), "noscan=%d", (channel == 0) ? 0 : 1);
867 strcat(new_config, noscan_line);
868 strcat(new_config, "\n");
869 noscan_set = 1;
870 }
871 // 如果是channel行,替换为新值
872 else if (strstr(line, "channel=") != NULL)
873 {
874 char channel_line[32];
875 snprintf(channel_line, sizeof(channel_line), "channel=%d", channel);
876 strcat(new_config, channel_line);
877 strcat(new_config, "\n");
878 }
879 // 其他行保持不变
880 else
881 {
882 strcat(new_config, line);
883 strcat(new_config, "\n");
884 }
885 line = strtok(NULL, "\n");
886 }
887
888 // 如果没有找到channel行,添加新的channel设置
889 if (strstr(new_config, "channel=") == NULL)
890 {
891 char channel_line[32];
892 snprintf(channel_line, sizeof(channel_line), "channel=%d\n", channel);
893 strcat(new_config, channel_line);
894 }
895
896 // 新增:如果没有找到noscan行,根据channel值补充
897 if (!noscan_set)
898 {
899 char noscan_line[32];
900 snprintf(noscan_line, sizeof(noscan_line), "noscan=%d\n", (channel == 0) ? 0 : 1);
901 strcat(new_config, noscan_line);
902 }
903
904 // 写回文件
905 return write_file(HOSTAPD_CONF_PATH, new_config);
906}
907
908
909int gsw_wifi_ap_channel_get(int *channel)
910{
911 if (handle())
912 return GSW_HAL_NORMAL_FAIL;
913 if (channel == NULL)
914 {
915 LOGI("channel is NULL\n");
916 return GSW_HAL_NORMAL_FAIL;
917 }
918 char buffer[1024] = {0};
919 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
920 {
921 return GSW_HAL_NORMAL_FAIL;
922 }
923 char *channel_line = strstr(buffer, "channel=");
924 if (channel_line)
925 {
926 channel_line += 8; // 跳过 "channel="
927 *channel = atoi(channel_line);
928 }
929 return GSW_HAL_SUCCESS;
930}
931
932int gsw_wifi_ap_auth_set(gsw_wifi_auth_e auth)
933{
934 if (handle())
935 return GSW_HAL_NORMAL_FAIL;
936 char buffer[4096] = {0};
937 char new_config[4096] = {0};
938 int wpa_set = 0, key_mgmt_set = 0, wpa_pairwise_set = 0, rsn_pairwise_set = 0, ieee80211w_set = 0, auth_algs_set = 0;
939
940 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
941 {
942 return GSW_HAL_NORMAL_FAIL;
943 }
944
945 char *line = strtok(buffer, "\n");
946 while (line)
947 {
948 // 匹配行首为"wpa="的行(长度4)
949 if (strncmp(line, "wpa=", 4) == 0)
950 {
951 char wpa_line[32];
952 if (auth == GSW_WIFI_AUTH_OPEN || auth == GSW_WIFI_AUTH_WEP)
953 snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 0);
954 else if (auth == GSW_WIFI_AUTH_WPA_PSK)
955 snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 1);
956 else if (auth == GSW_WIFI_AUTH_WPA2_PSK)
957 {
958 snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 3);
959 }
960 else if (auth == GSW_WIFI_AUTH_WPA3_PSK)
961 snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 2);
962 else
963 snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 2);
964 strcat(new_config, wpa_line);
965 strcat(new_config, "\n");
966 wpa_set = 1;
967 }
968 else if (strncmp(line, "wpa_key_mgmt=", 13) == 0)
969 {
970 const char *key_mgmt = (auth == GSW_WIFI_AUTH_WPA3_PSK) ? "SAE WPA-PSK" : "WPA-PSK";
971 char key_mgmt_line[64];
972 snprintf(key_mgmt_line, sizeof(key_mgmt_line), "wpa_key_mgmt=%s", key_mgmt);
973 strcat(new_config, key_mgmt_line);
974 strcat(new_config, "\n");
975 key_mgmt_set = 1;
976 }
977 else if (strncmp(line, "auth_algs=", 9) == 0)
978 {
979 char auth_algs_line[32];
980 snprintf(auth_algs_line, sizeof(auth_algs_line), "auth_algs=%d", (auth == GSW_WIFI_AUTH_WEP) ? 3 : 1);
981 strcat(new_config, auth_algs_line);
982 strcat(new_config, "\n");
983 auth_algs_set = 1;
984 }
985 else if (strncmp(line, "wpa_pairwise=", 13) == 0)
986 {
987 const char *pairwise = NULL;
988 if (auth == GSW_WIFI_AUTH_WPA_PSK)
989 pairwise = "TKIP";
990 else if (auth == GSW_WIFI_AUTH_WPA2_PSK)
991 pairwise = "TKIP CCMP";
992 else if (auth == GSW_WIFI_AUTH_WPA3_PSK)
993 pairwise = "CCMP";
994 else
995 pairwise = "TKIP CCMP";
996 char pairwise_line[64];
997 snprintf(pairwise_line, sizeof(pairwise_line), "wpa_pairwise=%s", pairwise);
998 strcat(new_config, pairwise_line);
999 strcat(new_config, "\n");
1000 wpa_pairwise_set = 1;
1001 }
1002 else if (strncmp(line, "rsn_pairwise=", 13) == 0)
1003 {
1004 if (auth == GSW_WIFI_AUTH_WPA2_PSK || auth == GSW_WIFI_AUTH_WPA3_PSK)
1005 {
1006 strcat(new_config, "rsn_pairwise=CCMP\n");
1007 }
1008 rsn_pairwise_set = 1;
1009 }
1010 else if (strncmp(line, "ieee80211w=", 11) == 0)
1011 {
1012 if (auth == GSW_WIFI_AUTH_WPA3_PSK)
1013 {
1014 strcat(new_config, "ieee80211w=2\n"); // WPA3 强制 MFP
1015 ieee80211w_set = 1;
1016 }
1017 else if (auth == GSW_WIFI_AUTH_WPA2_PSK)
1018 {
1019 strcat(new_config, "ieee80211w=1\n"); // WPA2 可选 MFP
1020 ieee80211w_set = 1;
1021 }
1022 // 其他模式不添加该行(直接跳过)
1023 }
1024 else
1025 {
1026 strcat(new_config, line);
1027 strcat(new_config, "\n");
1028 }
1029 line = strtok(NULL, "\n");
1030 }
1031
1032 // 补全未匹配的配置项
1033 if (!wpa_set)
1034 {
1035 if (auth == GSW_WIFI_AUTH_OPEN || auth == GSW_WIFI_AUTH_WEP)
1036 strcat(new_config, "wpa=0");
1037 else if (auth == GSW_WIFI_AUTH_WPA_PSK)
1038 strcat(new_config, "wpa=1");
1039 else if (auth == GSW_WIFI_AUTH_WPA2_PSK)
1040 {
1041 strcat(new_config, "wpa=3");
1042 }
1043 else if (auth == GSW_WIFI_AUTH_WPA3_PSK)
1044 strcat(new_config, "wpa=2");
1045 else
1046 strcat(new_config, "wpa=2");
1047 strcat(new_config, "\n");
1048 }
1049 if (!key_mgmt_set)
1050 {
1051 const char *key_mgmt = (auth == GSW_WIFI_AUTH_WPA3_PSK) ? "WPA-PSK SAE" : "WPA-PSK";
1052 strcat(new_config, "wpa_key_mgmt=");
1053 strcat(new_config, key_mgmt);
1054 strcat(new_config, "\n");
1055 }
1056 if (!auth_algs_set)
1057 {
1058 char auth_algs_line[32];
1059 snprintf(auth_algs_line, sizeof(auth_algs_line), "auth_algs=%d\n", (auth == GSW_WIFI_AUTH_WEP) ? 3 : 1);
1060 strcat(new_config, auth_algs_line);
1061 }
1062 if (!wpa_pairwise_set)
1063 {
1064 const char *pairwise = NULL;
1065 if (auth == GSW_WIFI_AUTH_WPA_PSK)
1066 pairwise = "TKIP";
1067 else if (auth == GSW_WIFI_AUTH_WPA2_PSK)
1068 pairwise = "TKIP CCMP";
1069 else if (auth == GSW_WIFI_AUTH_WPA3_PSK)
1070 pairwise = "CCMP";
1071 else
1072 pairwise = "TKIP CCMP";
1073 strcat(new_config, "wpa_pairwise=");
1074 strcat(new_config, pairwise);
1075 strcat(new_config, "\n");
1076 }
1077 if (!rsn_pairwise_set)
1078 {
1079 if (auth == GSW_WIFI_AUTH_WPA2_PSK || auth == GSW_WIFI_AUTH_WPA3_PSK)
1080 {
1081 strcat(new_config, "rsn_pairwise=CCMP\n");
1082 }
1083 }
1084 if (!ieee80211w_set && (auth == GSW_WIFI_AUTH_WPA3_PSK || auth == GSW_WIFI_AUTH_WPA2_PSK))
1085 {
1086 // 仅当需要时补全 ieee80211w 行(其他模式不添加)
1087 strcat(new_config, (auth == GSW_WIFI_AUTH_WPA3_PSK) ? "ieee80211w=2\n" : "ieee80211w=1\n");
1088 }
1089
1090 return write_file(HOSTAPD_CONF_PATH, new_config);
1091}
1092
1093int gsw_wifi_ap_auth_get(gsw_wifi_auth_e *auth)
1094{
1095 if (handle())
1096 return GSW_HAL_NORMAL_FAIL;
1097 char buffer[1024] = {0};
1098 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
1099 {
1100 return GSW_HAL_NORMAL_FAIL;
1101 }
1102
1103 char *auth_algs_line = strstr(buffer, "auth_algs=");
1104 if (auth_algs_line)
1105 {
1106 auth_algs_line += 10; // 跳过 "auth_algs="
1107 int auth_algs_val = atoi(auth_algs_line);
1108 if (auth_algs_val == 3)
1109 {
1110 *auth = GSW_WIFI_AUTH_WEP;
1111 return GSW_HAL_SUCCESS;
1112 }
1113 }
1114 // 优先通过 wpa_key_mgmt 判断 WPA3(包含 SAE 关键字)
1115 char *key_mgmt_line = strstr(buffer, "wpa_key_mgmt=");
1116 if (key_mgmt_line)
1117 {
1118 key_mgmt_line += 12; // 跳过 "wpa_key_mgmt="
1119 if (strstr(key_mgmt_line, "SAE"))
1120 {
1121 *auth = GSW_WIFI_AUTH_WPA3_PSK;
1122 return GSW_HAL_SUCCESS;
1123 }
1124 }
1125
1126 // 若未找到 SAE,再通过 wpa= 判断 WPA2 或开放模式
1127 char *wpa_line = strstr(buffer, "wpa=");
1128 if (wpa_line)
1129 {
1130 wpa_line += 4;
1131 int wpa_val = atoi(wpa_line);
1132 if(wpa_val > 1)
1133 {
1134 *auth = GSW_WIFI_AUTH_WPA2_PSK;
1135 }
1136 else if (wpa_val == 1)
1137 {
1138 *auth = GSW_WIFI_AUTH_WPA_PSK;
1139 }
1140 else
1141 {
1142 *auth = GSW_WIFI_AUTH_OPEN;
1143 }
1144 }
1145 else
1146 {
1147 *auth = GSW_WIFI_AUTH_OPEN; // 默认开放模式
1148 }
1149 return GSW_HAL_SUCCESS;
1150}
1151
1152int gsw_wifi_ap_password_set(char *password)
1153{
1154 if (handle())
1155 return GSW_HAL_NORMAL_FAIL;
1156 char buffer[4096] = {0};
1157 char new_config[4096] = {0};
1158 int has_passphrase = 0; // 标记是否已处理过密码行
1159 if (password == NULL) {
1160 LOGE("Password cannot be NULL");
1161 return GSW_HAL_NORMAL_FAIL;
1162 }
1163 size_t pass_len = strlen(password);
1164 if (pass_len < MIN_PASSWORD_LEN || pass_len > MAX_PASSWORD_LEN) {
1165 LOGE("Password length must be 8-63 characters, current length: %zu", pass_len);
1166 return GSW_HAL_NORMAL_FAIL;
1167 }
1168 // 读取现有配置文件内容
1169 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
1170 {
1171 return GSW_HAL_NORMAL_FAIL;
1172 }
1173
1174 // 逐行处理配置文件(修改:增加密码行存在标记)
1175 char *line = strtok(buffer, "\n");
1176 while (line)
1177 {
1178 // 匹配到原密码行时替换为新密码,并标记已处理
1179 if (strstr(line, "wpa_passphrase=") != NULL)
1180 {
1181 char new_pass_line[128];
1182 snprintf(new_pass_line, sizeof(new_pass_line), "wpa_passphrase=%s", password);
1183 strcat(new_config, new_pass_line);
1184 has_passphrase = 1; // 标记存在密码行
1185 }
1186 else
1187 {
1188 strcat(new_config, line);
1189 }
1190 strcat(new_config, "\n"); // 保留换行符
1191 line = strtok(NULL, "\n");
1192 }
1193
1194 // 若原文件无密码行,添加新密码行到末尾
1195 if (!has_passphrase)
1196 {
1197 char new_pass_line[128];
1198 snprintf(new_pass_line, sizeof(new_pass_line), "wpa_passphrase=%s\n", password);
1199 strcat(new_config, new_pass_line);
1200 }
1201
1202 // 写回配置文件
1203 return write_file(HOSTAPD_CONF_PATH, new_config);
1204}
1205
1206int gsw_wifi_ap_password_get(char *password)
1207{
1208 if (handle())
1209 return GSW_HAL_NORMAL_FAIL;
1210 char buffer[4096] = {0}; // 使用更大的缓冲区
1211 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
1212 {
1213 LOGI("Failed to read hostapd config file\n");
1214 return GSW_HAL_NORMAL_FAIL;
1215 }
1216
1217 // 查找密码行
1218 char *passphrase_line = strstr(buffer, "wpa_passphrase=");
1219 if (!passphrase_line)
1220 {
1221 LOGI("No password line found in config\n");
1222 return GSW_HAL_NORMAL_FAIL;
1223 }
1224
1225 passphrase_line += 15; // 跳过 "wpa_passphrase="
1226
1227 // 查找行结束位置
1228 char *end = strchr(passphrase_line, '\n');
1229 if (end)
1230 {
1231 *end = '\0'; // 确保字符串正确终止
1232 }
1233
1234 // 检查密码长度是否合法
1235 size_t pass_len = strlen(passphrase_line);
1236 if (pass_len == 0 || pass_len >= MAX_PASSWORD_LEN)
1237 {
1238 LOGI("Invalid password length: %zu\n", pass_len);
1239 return GSW_HAL_NORMAL_FAIL;
1240 }
1241
1242 // 复制密码
1243 strncpy(password, passphrase_line, MAX_PASSWORD_LEN);
1244 password[MAX_PASSWORD_LEN] = '\0'; // 确保终止
1245
1246 return GSW_HAL_SUCCESS;
1247}
1248
1249/**
1250 * @brief 获取 AP 模式的运行状态
1251 * @param [out] ap_status 用于存储 AP 模式运行状态的指针
1252 * @retval 0 表示函数执行成功
1253 */
1254int gsw_wifi_get_ap_status(gsw_wifi_ap_run_status_e *ap_status)
1255{
1256 if (handle())
1257 return GSW_HAL_NORMAL_FAIL;
1258 int result = is_hostapd_running();
1259 if (result == 0)
1260 {
1261 *ap_status = GSW_WIFI_AP_STATUS_ENABLE;
1262 }
1263 else
1264 {
1265 *ap_status = GSW_WIFI_AP_STATUS_DISABLE;
1266 }
1267 return GSW_HAL_SUCCESS;
1268}
1269
1270/* STA 模式相关函数实现 */
1271
1272// 辅助函数:更新 wpa_supplicant 配置文件
1273int update_wpa_supplicant_conf()
1274{
1275 char new_config[4096] = {0};
1276
1277 // 直接写入必需配置项(覆盖原有文件),新增WPA3支持的关键配置
1278 const char *required_configs[] = {
1279 "ctrl_interface=/var/run/wpa_supplicant", // 接口路径
1280 "ctrl_interface_group=root", // 组设置
1281 "update_config=1", // 允许自动更新配置
1282 "ap_scan=1", // 主动扫描AP
1283 // 网络块配置调整:使用数值19表示SAE组(替代SUITE_B_192文本标识)
1284 "network={\n key_mgmt=SAE\n pairwise=CCMP\n ieee80211w=2\n }"
1285 };
1286 int config_count = sizeof(required_configs) / sizeof(required_configs[0]);
1287
1288 // 添加所有必需的配置项(确保顺序)
1289 for (int i = 0; i < config_count; i++)
1290 {
1291 strcat(new_config, required_configs[i]);
1292 strcat(new_config, "\n");
1293 }
1294
1295 // 写回文件(完全覆盖)
1296 return write_file(WPA_SUPPLICANT_CONF_PATH, new_config);
1297}
1298
1299int gsw_wifi_sta_start()
1300{
1301 if (handle())
1302 return GSW_HAL_NORMAL_FAIL;
1303 // 更新 wpa_supplicant 配置文件
1304 if (update_wpa_supplicant_conf() != 0)
1305 {
1306 LOGI("Failed to update wpa_supplicant configuration file.\n");
1307 return GSW_HAL_NORMAL_FAIL;
1308 }
1309
1310 char cmd[MAX_COMMAND_LEN];
1311 snprintf(cmd, sizeof(cmd), "wpa_supplicant -Dnl80211 -i%s -c%s -B", WLAN_STA_DEV, WPA_SUPPLICANT_CONF_PATH);
1312 int status = execute_command(cmd);
1313 sleep(5);
1314 memset(cmd, 0, sizeof(cmd));
1315 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan > /dev/null", WLAN_STA_DEV);
1316 execute_command(cmd);
1317 memset(cmd, 0, sizeof(cmd));
1318 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan_results > /dev/null", WLAN_STA_DEV);
1319 execute_command(cmd);
1320 return status;
1321}
1322
1323int gsw_wifi_sta_stop()
1324{
1325 if (handle())
1326 return GSW_HAL_NORMAL_FAIL;
1327 if (is_wpa_supplicant_running() != 0)
1328 {
1329 LOGI("wpa_supplicant is not running, no need to stop.\n");
1330 return GSW_HAL_SUCCESS;
1331 }
1332
1333 char cmd[MAX_COMMAND_LEN];
1334 snprintf(cmd, sizeof(cmd), "killall wpa_supplicant");
1335 return execute_command(cmd);
1336}
1337
1338int gsw_wifi_get_sta_status(gsw_wifi_sta_run_status_e *sta_status)
1339{
1340 if (handle())
1341 return GSW_HAL_NORMAL_FAIL;
1342 if (is_wpa_supplicant_running() == 0)
1343 {
1344 *sta_status = GSW_WIFI_STA_STATUS_ENABLE;
1345 }
1346 else
1347 {
1348 *sta_status = GSW_WIFI_STA_STATUS_DISABLE;
1349 }
1350 return GSW_HAL_SUCCESS;
1351}
1352
1353// 修改涉及 wpa_cli 命令的函数
1354int gsw_wifi_get_sta_ssid(char *sta_ssid)
1355{
1356 if (handle())
1357 return GSW_HAL_NORMAL_FAIL;
1358 if (is_wpa_supplicant_running() != 0)
1359 {
1360 return GSW_HAL_NORMAL_FAIL;
1361 }
1362 char cmd[MAX_COMMAND_LEN];
1363 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'ssid=' | cut -d '=' -f 2", WLAN_STA_DEV);
1364 FILE *output = popen(cmd, "r");
1365 if (!output)
1366 return GSW_HAL_NORMAL_FAIL;
1367 char buffer[MAX_SSID_LEN + 1];
1368 if (fgets(buffer, sizeof(buffer), output) == NULL)
1369 {
1370 pclose(output);
1371 return GSW_HAL_NORMAL_FAIL;
1372 }
1373 pclose(output);
1374 buffer[strcspn(buffer, "\n")] = '\0';
1375 strncpy(sta_ssid, buffer, MAX_SSID_LEN);
1376 return GSW_HAL_SUCCESS;
1377}
1378
1379int gsw_wifi_get_sta_auth(gsw_wifi_auth_e *auth)
1380{
1381 if (handle())
1382 return GSW_HAL_NORMAL_FAIL;
1383 if (is_wpa_supplicant_running() != 0)
1384 {
1385 LOGI("wpa_supplicant is not running.\n");
1386 return GSW_HAL_NORMAL_FAIL;
1387 }
1388 char cmd[MAX_COMMAND_LEN];
1389 // 修改命令,获取 key_mgmt 字段
1390 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'key_mgmt=' | cut -d '=' -f 2", WLAN_STA_DEV);
1391 LOGI("Executing command: %s\n", cmd);
1392 FILE *output = popen(cmd, "r");
1393 if (!output)
1394 {
1395 LOGI("Failed to execute command: %s\n", cmd);
1396 return GSW_HAL_NORMAL_FAIL;
1397 }
1398 char buffer[32];
1399 if (fgets(buffer, sizeof(buffer), output) == NULL)
1400 {
1401 LOGI("No output from command: %s\n", cmd);
1402 pclose(output);
1403 return GSW_HAL_NORMAL_FAIL;
1404 }
1405 pclose(output);
1406 buffer[strcspn(buffer, "\n")] = '\0';
1407 LOGI("Command output: %s\n", buffer);
1408
1409 if (strstr(buffer, "WPA2-PSK"))
1410 {
1411 *auth = GSW_WIFI_AUTH_WPA2_PSK;
1412 }
1413 else if (strstr(buffer, "WPA-PSK"))
1414 {
1415 *auth = GSW_WIFI_AUTH_WPA_PSK;
1416 }
1417 else if (strstr(buffer, "SAE"))
1418 {
1419 *auth = GSW_WIFI_AUTH_WPA3_PSK;
1420 }
1421 else if (strstr(buffer, "NONE"))
1422 {
1423 *auth = GSW_WIFI_AUTH_OPEN;
1424 }
1425 else
1426 {
1427 LOGI("Unknown authentication type: %s. Assuming WPA2-PSK.\n", buffer);
1428 *auth = GSW_WIFI_AUTH_WPA2_PSK;
1429 }
1430 return GSW_HAL_SUCCESS;
1431}
1432
1433int gsw_wifi_sta_connect(char *ssid, gsw_wifi_auth_e auth, char *password)
1434{
1435 if (handle())
1436 return GSW_HAL_NORMAL_FAIL;
1437 if (is_wpa_supplicant_running() != 0)
1438 {
1439 return GSW_HAL_NORMAL_FAIL;
1440 }
1441
1442 char quoted_ssid[MAX_SSID_LEN + 3]; // 为引号和字符串结尾留空间
1443 char quoted_password[MAX_PASSWORD_LEN + 3];
1444
1445 // 添加引号
1446 snprintf(quoted_ssid, sizeof(quoted_ssid), "\\\"%s\\\"", ssid);
1447 if (auth != GSW_WIFI_AUTH_OPEN)
1448 {
1449 snprintf(quoted_password, sizeof(quoted_password), "\\\"%s\\\"", password);
1450 }
1451
1452 LOGI("SSID = %s\n", quoted_ssid);
1453
1454 char cmd[MAX_COMMAND_LEN];
1455 int status;
1456
1457 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1458 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s remove_network 0", WLAN_STA_DEV);
1459 LOGI("Executing command type: Remove network\n");
1460 status = execute_command(cmd);
1461 if (status != 0)
1462 {
1463 return status;
1464 }
1465
1466 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1467 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s ap_scan 1", WLAN_STA_DEV);
1468 LOGI("Executing command type: Set AP scan\n");
1469 status = execute_command(cmd);
1470 if (status != 0)
1471 {
1472 return status;
1473 }
1474
1475 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1476 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s add_network", WLAN_STA_DEV);
1477 LOGI("Executing command type: Add network\n");
1478 status = execute_command(cmd);
1479 if (status != 0)
1480 {
1481 return status;
1482 }
1483
1484 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1485 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 ssid %s", WLAN_STA_DEV, quoted_ssid);
1486 LOGI("Executing command type: Set SSID\n");
1487 status = execute_command(cmd);
1488 if (status != 0)
1489 {
1490 return status;
1491 }
1492
1493 if (auth != GSW_WIFI_AUTH_OPEN)
1494 {
1495 const char *key_mgmt;
1496 switch (auth)
1497 {
1498 case GSW_WIFI_AUTH_WEP:
1499 key_mgmt = "NONE";
1500 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1501 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 wep_key0 %s", WLAN_STA_DEV, quoted_password);
1502 LOGI("Executing command type: Set WEP key\n");
1503 status = execute_command(cmd);
1504 if (status != 0)
1505 return status;
1506
1507 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV(注意原代码中可能存在拼写错误 "weep_tx_keyidx" 应为 "wep_tx_keyidx")
1508 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 wep_tx_keyidx 0", WLAN_STA_DEV);
1509 LOGI("Executing command type: Set WEP TX key index\n");
1510 status = execute_command(cmd);
1511 if (status != 0)
1512 return status;
1513 break;
1514
1515 case GSW_WIFI_AUTH_WPA_PSK:
1516 key_mgmt = "WPA-PSK";
1517 break;
1518 case GSW_WIFI_AUTH_WPA2_PSK:
1519 key_mgmt = "WPA-PSK";
1520 break;
1521 case GSW_WIFI_AUTH_WPA3_PSK:
1522 key_mgmt = "SAE";
1523 break;
1524 default:
1525 key_mgmt = "WPA-PSK";
1526 break;
1527 }
1528
1529 if (auth != GSW_WIFI_AUTH_WEP)
1530 {
1531 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1532 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 key_mgmt %s", WLAN_STA_DEV, key_mgmt);
1533 LOGI("Executing command type: Set key management\n");
1534 status = execute_command(cmd);
1535 if (status != 0)
1536 return status;
1537
1538 if (auth == GSW_WIFI_AUTH_WPA3_PSK)
1539 {
1540 // WPA3 特有配置:确保在设置key_mgmt后配置SAE参数
1541 // 设置椭圆曲线组(使用NIST P-256,组号19)
1542 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 sae_groups 19", WLAN_STA_DEV);
1543 status = execute_command(cmd);
1544 if (status != 0)
1545 return status;
1546
1547 // 强制MFP保护(WPA3要求必须启用)
1548 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 sae_require_mfp 1", WLAN_STA_DEV);
1549 status = execute_command(cmd);
1550 if (status != 0)
1551 return status;
1552
1553 // 使用SAE+CCMP加密组合(确保与AP端匹配)
1554 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 pairwise CCMP", WLAN_STA_DEV);
1555 status = execute_command(cmd);
1556 if (status != 0)
1557 return status;
1558 }
1559
1560 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1561 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 psk %s", WLAN_STA_DEV, quoted_password);
1562 LOGI("Executing command type: Set PSK\n");
1563 status = execute_command(cmd);
1564 if (status != 0)
1565 return status;
1566 }
1567 }
1568 else
1569 {
1570 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1571 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 key_mgmt NONE", WLAN_STA_DEV);
1572 LOGI("Executing command type: Set key management for open network\n");
1573 status = execute_command(cmd);
1574 if (status != 0)
1575 return status;
1576 }
1577
1578 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1579 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s select_network 0", WLAN_STA_DEV);
1580 LOGI("Executing command type: Select network\n");
1581 status = execute_command(cmd);
1582 if (status != 0)
1583 {
1584 return status;
1585 }
1586
1587 // udhcpc 命令无需修改(已使用 WLAN_STA_DEV)
1588 snprintf(cmd, sizeof(cmd), "udhcpc -i %s -n", WLAN_STA_DEV);
1589 LOGI("Executing command type: Request DHCP IP\n");
1590 status = execute_command(cmd);
1591 return status;
1592}
1593
1594int gsw_wifi_sta_forget_ap(char *ssid, gsw_wifi_auth_e auth)
1595{
1596 if (handle())
1597 return GSW_HAL_NORMAL_FAIL;
1598 if (is_wpa_supplicant_running() != 0)
1599 {
1600 return GSW_HAL_NORMAL_FAIL;
1601 }
1602 char cmd[MAX_COMMAND_LEN];
1603 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s remove_network $(wpa_cli -i %s list_networks | grep '%s' | awk '{print $1}')", WLAN_STA_DEV, WLAN_STA_DEV, ssid);
1604 return execute_command(cmd);
1605}
1606
1607int gsw_wifi_sta_start_scan(void)
1608{
1609 if (handle())
1610 return GSW_HAL_NORMAL_FAIL;
1611 if (is_wpa_supplicant_running() != 0)
1612 {
1613 return GSW_HAL_NORMAL_FAIL;
1614 }
1615 char cmd[MAX_COMMAND_LEN];
1616 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan", WLAN_STA_DEV);
1617 return execute_command(cmd);
1618}
1619
1620int gsw_wifi_get_interface_ip(gsw_wifi_index_e idx, char *ip)
1621{
1622 if (handle())
1623 return GSW_HAL_NORMAL_FAIL;
1624 char cmd[MAX_COMMAND_LEN];
1625 snprintf(cmd, sizeof(cmd), "ip addr show dev %s | grep 'inet ' | awk '{print $2}' | cut -d '/' -f 1", idx == GSW_WIFI_INTERFACE_0 ? WLAN_STA_DEV : WLAN_AP_DEV);
1626 FILE *output = popen(cmd, "r");
1627 if (!output)
1628 return GSW_HAL_NORMAL_FAIL;
1629 char buffer[MAX_IP_LEN + 1];
1630 if (fgets(buffer, sizeof(buffer), output) == NULL)
1631 {
1632 pclose(output);
1633 return GSW_HAL_NORMAL_FAIL;
1634 }
1635 pclose(output);
1636 buffer[strcspn(buffer, "\n")] = '\0';
1637 strncpy(ip, buffer, MAX_IP_LEN);
1638 return GSW_HAL_SUCCESS;
1639}
1640
1641int gsw_wifi_get_interface_mac(gsw_wifi_index_e idx, char *mac)
1642{
1643 if (handle())
1644 return GSW_HAL_NORMAL_FAIL;
1645 char cmd[MAX_COMMAND_LEN];
1646 snprintf(cmd, sizeof(cmd), "ip link show dev %s | grep 'link/ether' | awk '{print $2}'", idx == GSW_WIFI_INTERFACE_0 ? WLAN_STA_DEV : WLAN_AP_DEV);
1647 FILE *output = popen(cmd, "r");
1648 if (!output)
1649 return GSW_HAL_NORMAL_FAIL;
1650 char buffer[MAX_MAC_LEN + 1];
1651 if (fgets(buffer, sizeof(buffer), output) == NULL)
1652 {
1653 pclose(output);
1654 return GSW_HAL_NORMAL_FAIL;
1655 }
1656 pclose(output);
1657 buffer[strcspn(buffer, "\n")] = '\0';
1658 strncpy(mac, buffer, MAX_MAC_LEN);
1659 return GSW_HAL_SUCCESS;
1660}
1661
1662int gsw_wifi_get_connect_ap_mac(char *mac)
1663{
1664 if (handle())
1665 return GSW_HAL_NORMAL_FAIL;
1666 char cmd[MAX_COMMAND_LEN];
1667 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'bssid=' | cut -d '=' -f 2", WLAN_STA_DEV);
1668 FILE *output = popen(cmd, "r");
1669 if (!output)
1670 return GSW_HAL_NORMAL_FAIL;
1671 char buffer[MAX_MAC_LEN + 1];
1672 if (fgets(buffer, sizeof(buffer), output) == NULL)
1673 {
1674 pclose(output);
1675 return GSW_HAL_NORMAL_FAIL;
1676 }
1677 pclose(output);
1678 buffer[strcspn(buffer, "\n")] = '\0';
1679 strncpy(mac, buffer, MAX_MAC_LEN);
1680 return GSW_HAL_SUCCESS;
1681}
1682
1683int gsw_wifi_get_connect_ap_rssi(int *rssi)
1684{
1685 if (handle())
1686 return GSW_HAL_NORMAL_FAIL;
1687 char cmd[MAX_COMMAND_LEN];
1688 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'signal=' | cut -d '=' -f 2", WLAN_STA_DEV);
1689 FILE *output = popen(cmd, "r");
1690 if (!output)
1691 return GSW_HAL_NORMAL_FAIL;
1692 char buffer[16];
1693 if (fgets(buffer, sizeof(buffer), output) == NULL)
1694 {
1695 pclose(output);
1696 return GSW_HAL_NORMAL_FAIL;
1697 }
1698 pclose(output);
1699 buffer[strcspn(buffer, "\n")] = '\0';
1700 *rssi = atoi(buffer);
1701 return GSW_HAL_SUCCESS;
1702}
1703
1704int gsw_wifi_get_connect_ap_band(gsw_wifi_band_e *band)
1705{
1706 if (handle())
1707 return GSW_HAL_NORMAL_FAIL;
1708 char cmd[MAX_COMMAND_LEN];
1709 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'frequency=' | cut -d '=' -f 2", WLAN_STA_DEV);
1710 FILE *output = popen(cmd, "r");
1711 if (!output)
1712 return GSW_HAL_NORMAL_FAIL;
1713 char buffer[32];
1714 if (fgets(buffer, sizeof(buffer), output) == NULL)
1715 {
1716 pclose(output);
1717 return GSW_HAL_NORMAL_FAIL;
1718 }
1719 pclose(output);
1720 buffer[strcspn(buffer, "\n")] = '\0';
1721 int freq = atoi(buffer);
1722 if (freq < 2500)
1723 {
1724 *band = GSW_WIFI_2G_band;
1725 }
1726 else
1727 {
1728 *band = GSW_WIFI_5G_band;
1729 }
1730 return GSW_HAL_SUCCESS;
1731}
1732
1733int gsw_wifi_get_connect_ap_ip(char *ip)
1734{
1735 if (handle())
1736 return GSW_HAL_NORMAL_FAIL;
1737 char cmd[MAX_COMMAND_LEN];
1738 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'ip_address=' | cut -d '=' -f 2", WLAN_STA_DEV);
1739 FILE *output = popen(cmd, "r");
1740 if (!output)
1741 return GSW_HAL_NORMAL_FAIL;
1742 char buffer[MAX_IP_LEN + 1];
1743 if (fgets(buffer, sizeof(buffer), output) == NULL)
1744 {
1745 pclose(output);
1746 return GSW_HAL_NORMAL_FAIL;
1747 }
1748 pclose(output);
1749 buffer[strcspn(buffer, "\n")] = '\0';
1750 strncpy(ip, buffer, MAX_IP_LEN);
1751 return GSW_HAL_SUCCESS;
1752}
1753
1754int gsw_wifi_get_sta_available_ap(gsw_ap_detail_info_s *info)
1755{
1756 if (handle())
1757 return GSW_HAL_NORMAL_FAIL;
1758 if (is_wpa_supplicant_running() != 0)
1759 {
1760 return GSW_HAL_NORMAL_FAIL;
1761 }
1762 char cmd[MAX_COMMAND_LEN];
1763 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan > /dev/null", WLAN_STA_DEV);
1764 execute_command(cmd);
1765 sleep(1);
1766 memset(cmd, 0, sizeof(cmd));
1767 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan_results | awk '{print $1,$2,$3,$4,$5}' | sort -k3 -nr | head -n 1", WLAN_STA_DEV);
1768 FILE *output = popen(cmd, "r");
1769 if (!output)
1770 return GSW_HAL_NORMAL_FAIL;
1771 char buffer[256];
1772 if (fgets(buffer, sizeof(buffer), output) == NULL)
1773 {
1774 pclose(output);
1775 return GSW_HAL_NORMAL_FAIL;
1776 }
1777 pclose(output);
1778 buffer[strcspn(buffer, "\n")] = '\0';
1779 sscanf(buffer, "%x %d %s %s %s", &info->base_info.auth, &info->rssi, info->base_info.ap_ssid, info->base_info.ap_ip, info->base_info.ap_mac);
1780 info->status = GSW_WIFI_AP_STATUS_ENABLE;
1781 return GSW_HAL_SUCCESS;
1782}
1783
1784int gsw_wifi_get_ap_device_list(gsw_ap_info_s *ap_info, gsw_device_info_s *device_list, int len, int *dev_len)
1785{
1786 if (handle())
1787 return GSW_HAL_NORMAL_FAIL;
1788 char cmd[MAX_COMMAND_LEN];
1789 snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s all_sta", WLAN_AP_DEV);
1790 FILE *output = popen(cmd, "r");
1791 if (!output)
1792 return GSW_HAL_NORMAL_FAIL;
1793 char buffer[4096];
1794 size_t length = fread(buffer, 1, sizeof(buffer) - 1, output);
1795 if (length == 0)
1796 {
1797 pclose(output);
1798 return GSW_HAL_NORMAL_FAIL;
1799 }
1800 buffer[length] = '\0';
1801 pclose(output);
1802 // 解析 hostapd_cli 输出,填充 device_list
1803 *dev_len = 0;
1804 return GSW_HAL_SUCCESS;
1805}
1806
1807int gsw_wifi_get_sta_saved_ap(gsw_saved_ap_info_s *list, int len, int *list_len)
1808{
1809 if (handle())
1810 return GSW_HAL_NORMAL_FAIL;
1811 if (is_wpa_supplicant_running() != 0)
1812 {
1813 return GSW_HAL_NORMAL_FAIL;
1814 }
1815 char cmd[MAX_COMMAND_LEN];
1816 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s list_networks", WLAN_STA_DEV);
1817 FILE *output = popen(cmd, "r");
1818 if (!output)
1819 return GSW_HAL_NORMAL_FAIL;
1820 char buffer[4096];
1821 size_t length = fread(buffer, 1, sizeof(buffer) - 1, output);
1822 if (length == 0)
1823 {
1824 pclose(output);
1825 return GSW_HAL_NORMAL_FAIL;
1826 }
1827 buffer[length] = '\0';
1828 pclose(output);
1829 // 解析 wpa_cli 输出,填充 list
1830 *list_len = 0;
1831 return GSW_HAL_SUCCESS;
1832}
1833
1834int gsw_wifi_get_scan_list(gsw_scan_info_s *list, int len, int *list_len)
1835{
1836 if (handle())
1837 return GSW_HAL_NORMAL_FAIL;
1838 if (is_wpa_supplicant_running() != 0)
1839 {
1840 return GSW_HAL_NORMAL_FAIL;
1841 }
1842 char cmd[MAX_COMMAND_LEN];
1843 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan > /dev/null", WLAN_STA_DEV);
1844 execute_command(cmd);
1845 sleep(1);
1846 memset(cmd, 0, sizeof(cmd));
1847 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan_results", WLAN_STA_DEV);
1848 FILE *output = popen(cmd, "r");
1849 if (!output)
1850 return GSW_HAL_NORMAL_FAIL;
1851 char buffer[4096];
1852 size_t length = fread(buffer, 1, sizeof(buffer) - 1, output);
1853 if (length == 0)
1854 {
1855 pclose(output);
1856 return GSW_HAL_NORMAL_FAIL;
1857 }
1858 buffer[length] = '\0';
1859 pclose(output);
1860
1861 // 解析 wpa_cli 输出,填充 list
1862 *list_len = 0;
1863 char *line = strtok(buffer, "\n");
1864 // 跳过标题行
1865 if (line)
1866 line = strtok(NULL, "\n");
1867
1868 while (line && *list_len < len)
1869 {
1870 gsw_scan_info_s *info = &list[*list_len];
1871 int freq; // 临时变量用于存储频率,以判断频段
1872 char flags[256]; // 临时变量用于存储认证标志
1873
1874 // 假设输出格式为:bssid freq signal flags ssid
1875 // 解析每一行数据
1876 sscanf(line, "%31s %d %d %255s %32[^\n]", info->mac, &freq, &info->rssi, flags, info->ssid);
1877
1878 // 根据频率判断频段
1879 if (freq < 2500)
1880 {
1881 info->band = GSW_WIFI_2G_band;
1882 }
1883 else
1884 {
1885 info->band = GSW_WIFI_5G_band;
1886 }
1887
1888 // 根据 flags 判断认证方式
1889 if (strstr(flags, "SAE") != NULL || strstr(flags, "WPA3") != NULL)
1890 {
1891 info->auth = GSW_WIFI_AUTH_WPA3_PSK;
1892 }
1893 else if (strstr(flags, "WPA2") != NULL)
1894 {
1895 info->auth = GSW_WIFI_AUTH_WPA2_PSK;
1896 }
1897 else if (strstr(flags, "WPA") != NULL)
1898 {
1899 info->auth = GSW_WIFI_AUTH_WPA_PSK;
1900 }
1901 else
1902 {
1903 info->auth = GSW_WIFI_AUTH_OPEN;
1904 }
1905
1906 (*list_len)++;
1907 line = strtok(NULL, "\n");
1908 }
1909 return GSW_HAL_SUCCESS;
1910}
1911
1912void *ap_event_listener(void *arg)
1913{
1914 char list_cmd[MAX_COMMAND_LEN];
1915 snprintf(list_cmd, sizeof(list_cmd), "hostapd_cli -i %s all_sta | grep -E '^[0-9a-f:]{17}$|connected_time=' | \
1916 awk '/^[0-9a-f:]{17}$/ { mac=$0; getline; if ($0 ~ /connected_time=/) print mac }'",
1917 WLAN_AP_DEV);
1918
1919 // 保存上一次的设备MAC地址列表
1920 char prev_macs[GSW_WIFI_AP_DEVICE_NUM][32] = {0};
1921 int prev_count = 0;
1922 while (ap_event_context.running)
1923 {
1924 if (is_hostapd_running() != 0)
1925 {
1926 sleep(5);
1927 continue;
1928 }
1929
1930 // 使用list_cmd获取当前设备MAC列表
1931 char curr_macs[GSW_WIFI_AP_DEVICE_NUM][32] = {0};
1932 int curr_count = 0;
1933 FILE *list_output = popen(list_cmd, "r");
1934 if (list_output)
1935 {
1936 char buffer[1024];
1937 if (fread(buffer, 1, sizeof(buffer) - 1, list_output) > 0)
1938 {
1939 buffer[sizeof(buffer) - 1] = '\0';
1940 char *line = strtok(buffer, "\n");
1941 while (line && curr_count < GSW_WIFI_AP_DEVICE_NUM)
1942 {
1943 if (strchr(line, ':'))
1944 {
1945 strncpy(curr_macs[curr_count], line, 17); // 复制MAC地址
1946 curr_count++;
1947 }
1948 line = strtok(NULL, "\n");
1949 }
1950 }
1951 pclose(list_output);
1952 }
1953
1954 // 数量变化时直接触发事件(保持原有逻辑)
1955 if (curr_count > prev_count && ap_event_context.gsw_cb)
1956 {
1957 ap_event_context.gsw_cb(ap_event_context.arg, GSW_WIFI_STATUS_CONNECT);
1958 }
1959 else if (curr_count < prev_count && ap_event_context.gsw_cb)
1960 {
1961 ap_event_context.gsw_cb(ap_event_context.arg, GSW_WIFI_STATUS_DISCONNECT);
1962 }
1963 else if (curr_count == prev_count && ap_event_context.gsw_cb)
1964 {
1965 int mac_updated = 0;
1966 // 遍历检查是否有MAC地址变化
1967 for (int i = 0; i < curr_count; i++)
1968 {
1969 int found = 0;
1970 for (int j = 0; j < prev_count; j++)
1971 {
1972 if (strcasecmp(curr_macs[i], prev_macs[j]) == 0)
1973 {
1974 found = 1;
1975 break;
1976 }
1977 }
1978 // 发现新MAC地址(当前存在但之前不存在)
1979 if (!found)
1980 {
1981 LOGI("New device connected: %s\n", curr_macs[i]);
1982 mac_updated++;
1983 break;
1984 }
1985 }
1986 // 若有更新则触发连接事件(可根据需求扩展为断开事件)
1987 if (mac_updated)
1988 {
1989 ap_event_context.gsw_cb(ap_event_context.arg, GSW_WIFI_STATUS_DISCONNECT);
1990 ap_event_context.gsw_cb(ap_event_context.arg, GSW_WIFI_STATUS_CONNECT);
1991 }
1992 }
1993
1994 memcpy(prev_macs, curr_macs, sizeof(prev_macs));
1995 prev_count = curr_count;
1996
1997 sleep(2); // 定期轮询
1998 }
1999 return NULL;
2000}
2001
2002/**
2003 * @brief register ap event notification function
2004 * @param [in] arg as gsw_cb function parameter, can be NULL
2005 * @param [in] gsw_cb callback
2006 * @retval 0: success
2007 * @retval other: fail
2008 */
2009int gsw_wifi_reg_ap_event_callback(void *arg, GSW_AP_CALLBACK_FUNC_PTR gsw_cb)
2010{
2011 if (handle())
2012 return GSW_HAL_NORMAL_FAIL;
2013 if (ap_event_context.running)
2014 {
2015 return GSW_HAL_NORMAL_FAIL; // 已经注册了回调
2016 }
2017 ap_event_context.arg = arg;
2018 ap_event_context.gsw_cb = gsw_cb;
2019 ap_event_context.running = 1;
2020 if (pthread_create(&ap_event_context.thread_id, NULL, ap_event_listener, NULL) != 0)
2021 {
2022 ap_event_context.running = 0;
2023 return GSW_HAL_NORMAL_FAIL;
2024 }
2025 return GSW_HAL_SUCCESS;
2026}
2027
2028/**
2029 * @brief unregister ap callback function
2030 * @param [in] arg reserved parameter, can be NULL
2031 * @retval 0: success
2032 * @retval other: fail
2033 */
2034int gsw_wifi_unreg_ap_event_callback(void *arg)
2035{
2036 if (handle())
2037 return GSW_HAL_NORMAL_FAIL;
2038 ap_event_context.running = 0;
2039
2040 // 检查线程是否存在
2041 if (ap_event_context.thread_id != 0)
2042 {
2043 if (pthread_join(ap_event_context.thread_id, NULL) != 0)
2044 {
2045 return GSW_HAL_NORMAL_FAIL;
2046 }
2047 ap_event_context.thread_id = 0; // 重置线程ID
2048 }
2049
2050 ap_event_context.arg = NULL;
2051 ap_event_context.gsw_cb = NULL;
2052 return GSW_HAL_SUCCESS;
2053}
2054
2055int gsw_wifi_reg_sta_event_callback(void *arg, GSW_STA_CALLBACK_FUNC_PTR gsw_cb)
2056{
2057 if (handle())
2058 return GSW_HAL_NORMAL_FAIL;
2059 return GSW_HAL_SUCCESS;
2060}
2061
2062int gsw_wifi_unreg_sta_event_callback(void *arg)
2063{
2064 if (handle())
2065 return GSW_HAL_NORMAL_FAIL;
2066 return GSW_HAL_SUCCESS;
2067}
2068
2069int gsw_wifi_sdk_interface_init()
2070{
2071 if (handle())
2072 return GSW_HAL_NORMAL_FAIL;
2073 return GSW_HAL_SUCCESS;
2074}
2075
2076int is_module_loaded(const char *module_name)
2077{
2078 if (handle())
2079 return GSW_HAL_NORMAL_FAIL;
2080 char cmd[MAX_COMMAND_LEN];
2081 snprintf(cmd, sizeof(cmd), "lsmod | grep %s", module_name);
2082 FILE *output = popen(cmd, "r");
2083 if (!output)
2084 return GSW_HAL_NORMAL_FAIL;
2085 char buffer[256];
2086 if (fgets(buffer, sizeof(buffer), output) == NULL)
2087 {
2088 pclose(output);
2089 return GSW_HAL_SUCCESS;
2090 }
2091 pclose(output);
2092 return 1;
2093}
2094/**
2095 * @brief load wifi driver
2096 * @retval 0: success
2097 * @retval other: fail
2098 */
2099int gsw_wifi_enable(void)
2100{
2101 if (handle())
2102 return GSW_HAL_NORMAL_FAIL;
2103 if (!is_module_loaded("cfg80211"))
2104 {
2105 if (execute_command("modprobe cfg80211") != 0)
2106 {
2107 return GSW_HAL_NORMAL_FAIL;
2108 }
2109 }
2110 else
2111 {
2112 LOGI("cfg80211 has insmod.\n");
2113 }
2114
2115 if (!is_module_loaded("aic8800_bsp"))
2116 {
2117 char cmd[MAX_COMMAND_LEN];
2118 snprintf(cmd, sizeof(cmd), "modprobe %s", AIC8800_BSP_DRIVER_PATH);
2119 if (execute_command(cmd) != 0)
2120 {
2121 return GSW_HAL_NORMAL_FAIL;
2122 }
2123 }
2124 else
2125 {
2126 LOGI("aic8800_bsp has insmod.\n");
2127 }
2128
2129 if (!is_module_loaded("aic8800_fdrv"))
2130 {
2131 char cmd[MAX_COMMAND_LEN];
2132 snprintf(cmd, sizeof(cmd), "modprobe %s", AIC8800_FDRV_DRIVER_PATH);
2133 if (execute_command(cmd) != 0)
2134 {
2135 return GSW_HAL_NORMAL_FAIL;
2136 }
2137 }
2138 else
2139 {
2140 LOGI("aic8800_fdrv has insmod.\n");
2141 }
2142
2143 return GSW_HAL_SUCCESS;
2144}
2145
2146/**
2147 * @brief uninstall wifi driver
2148 * @retval 0: success
2149 * @retval other: fail
2150 */
2151int gsw_wifi_disable(void)
2152{
2153 if (handle())
2154 return GSW_HAL_NORMAL_FAIL;
2155 if (is_module_loaded("aic8800_fdrv"))
2156 {
2157 if (execute_command("rmmod aic8800_fdrv") != 0)
2158 {
2159 return GSW_HAL_NORMAL_FAIL;
2160 }
2161 }
2162 else
2163 {
2164 LOGI("aic8800_fdrv not insmod.\n");
2165 }
2166
2167 if (is_module_loaded("aic8800_bsp"))
2168 {
2169 if (execute_command("rmmod aic8800_bsp") != 0)
2170 {
2171 return GSW_HAL_NORMAL_FAIL;
2172 }
2173 }
2174 else
2175 {
2176 LOGI("aic8800_bsp not insmod.\n");
2177 }
2178
2179 if (is_module_loaded("cfg80211"))
2180 {
2181 if (execute_command("rmmod cfg80211") != 0)
2182 {
2183 return GSW_HAL_NORMAL_FAIL;
2184 }
2185 }
2186 else
2187 {
2188 LOGI("cfg80211 not insmod.\n");
2189 }
2190
2191 return GSW_HAL_SUCCESS;
2192}