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