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