blob: 3937ab40de34bee85fbb229e795f0b920fc469c1 [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);
239
240 // 关闭对应的 hostapd 后,删除对应的文件
241 char path[MAX_COMMAND_LEN];
242 snprintf(path, sizeof(path), "%s/%s", CTRL_INTERFACE, WLAN_AP_DEV);
243 if (access(path, F_OK) == 0)
244 {
245 if (remove(path) != 0)
246 {
zw.wangbd342b92025-07-21 11:24:16 +0800247 LOGE(GSW_WIFI,"Failed to remove hostapd control interface file");
zw.wang00477802025-06-12 19:48:37 +0800248 }
249 }
zw.wangbd342b92025-07-21 11:24:16 +0800250 LOGI(GSW_WIFI,"gsw_wifi_ap_stop ret = %d\n", status);
zw.wang00477802025-06-12 19:48:37 +0800251 return status;
252}
253
254int gsw_wifi_ap_restart(void)
255{
256 if (handle())
257 return GSW_HAL_NORMAL_FAIL;
258 int ret = 0;
259 char cmd[MAX_COMMAND_LEN];
260 snprintf(cmd, sizeof(cmd), "killall hostapd");
261 int status = execute_command(cmd);
zw.wangbd342b92025-07-21 11:24:16 +0800262 LOGI(GSW_WIFI,"killall hostapd status = %d\n", status);
zw.wang00477802025-06-12 19:48:37 +0800263 // 关闭对应的 hostapd 后,删除对应的文件
264 char path[MAX_COMMAND_LEN];
265 snprintf(path, sizeof(path), "%s/%s", CTRL_INTERFACE, WLAN_AP_DEV);
266 if (access(path, F_OK) == 0)
267 {
268 if (remove(path) != 0)
269 {
zw.wangbd342b92025-07-21 11:24:16 +0800270 LOGE(GSW_WIFI,"Failed to remove hostapd control interface file");
zw.wang00477802025-06-12 19:48:37 +0800271 }
272 }
273 ret = gsw_wifi_ap_start();
zw.wangbd342b92025-07-21 11:24:16 +0800274 LOGI(GSW_WIFI,"gsw_wifi_ap_restart ret = %d\n", ret);
zw.wang00477802025-06-12 19:48:37 +0800275 return ret;
276}
277
278int gsw_wifi_ap_ssid_set(char *ssid)
279{
280 if (handle())
281 return GSW_HAL_NORMAL_FAIL;
282 char buffer[4096] = {0};
283 char new_config[4096] = {0};
284 if (ssid == NULL) {
zw.wangbd342b92025-07-21 11:24:16 +0800285 LOGE(GSW_WIFI,"Password cannot be NULL");
zw.wang00477802025-06-12 19:48:37 +0800286 return GSW_HAL_NORMAL_FAIL;
287 }
288 size_t ssid_len = strlen(ssid);
zw.wanga8267052025-06-20 10:03:00 +0800289 if (ssid_len > MAX_SSID_LEN || ssid_len < MIN_SSID_LEN) {
zw.wangbd342b92025-07-21 11:24:16 +0800290 LOGE(GSW_WIFI,"SSID length must be 6-32 characters, current length: %zu", ssid_len);
zw.wang00477802025-06-12 19:48:37 +0800291 return GSW_HAL_NORMAL_FAIL;
292 }
zw.wangbd342b92025-07-21 11:24:16 +0800293 LOGI(GSW_WIFI,"AP set ssid: %s\n", ssid);
zw.wang00477802025-06-12 19:48:37 +0800294 // 读取现有配置
295 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
296 {
297 // 如果文件不存在,创建新配置(修改默认配置内容)
298 FILE *conf = fopen(HOSTAPD_CONF_PATH, "w");
299 if (!conf)
300 return GSW_HAL_NORMAL_FAIL;
301
302 // 写入用户指定的完整默认配置
303 fprintf(conf, "ctrl_interface=%s\n", CTRL_INTERFACE);
304 fprintf(conf, "ctrl_interface_group=0\n");
305 fprintf(conf, "interface=%s\n", WLAN_AP_DEV);
306 fprintf(conf, "driver=nl80211\n");
307 fprintf(conf, "bridge=br-lan\n");
308 fprintf(conf, "ssid=%s\n", ssid); // 使用传入的ssid
zw.wang3e022be2025-06-18 09:39:23 +0800309 fprintf(conf, "hw_mode=g\n");
zw.wang00477802025-06-12 19:48:37 +0800310 fprintf(conf, "ieee80211d=1\n");
zw.wang3e022be2025-06-18 09:39:23 +0800311 fprintf(conf, "channel=0\n");
zw.wang00477802025-06-12 19:48:37 +0800312 fprintf(conf, "auth_algs=1\n");
313 fprintf(conf, "wme_enabled=1\n");
314 fprintf(conf, "noscan=0\n");
315 fprintf(conf, "beacon_int=100\n");
zw.wang3e022be2025-06-18 09:39:23 +0800316 fprintf(conf, "wpa=3\n");
zw.wang00477802025-06-12 19:48:37 +0800317 fprintf(conf, "wpa_passphrase=12345678\n");
318 fprintf(conf, "ieee80211n=1\n");
319 fprintf(conf, "ieee80211ac=1\n");
320 fprintf(conf, "ieee80211ax=1\n");
zw.wang3e022be2025-06-18 09:39:23 +0800321 fprintf(conf, "vht_oper_chwidth=0\n");
322 fprintf(conf, "vht_oper_centr_freq_seg0_idx=0\n");
323 fprintf(conf, "he_oper_chwidth=0\n");
324 fprintf(conf, "he_oper_centr_freq_seg0_idx=0\n");
zw.wang00477802025-06-12 19:48:37 +0800325 fprintf(conf, "he_basic_mcs_nss_set=65534\n");
zw.wang3e022be2025-06-18 09:39:23 +0800326 fprintf(conf, "he_su_beamformee=0\n");
zw.wang00477802025-06-12 19:48:37 +0800327 fprintf(conf, "he_twt_required=0\n");
328 fprintf(conf, "vht_capab=[SHORT-GI-80][VHT40+][VHT40-][MAX-A-MPDU-LEN-EXP7][RX-STBC-1][RX-LDPC]\n");
329 fprintf(conf, "ht_capab=[SHORT-GI-20][SHORT-GI-40][HT40+][HT40-][LDPC][RX-STBC1]\n");
330 fprintf(conf, "wpa_key_mgmt=WPA-PSK\n");
zw.wang3e022be2025-06-18 09:39:23 +0800331 fprintf(conf, "wpa_pairwise=TKIP CCMP\n");
zw.wang00477802025-06-12 19:48:37 +0800332 fprintf(conf, "rsn_pairwise=CCMP\n");
zw.wang00477802025-06-12 19:48:37 +0800333 fprintf(conf, "ignore_broadcast_ssid=0\n");
334 fprintf(conf, "country_code=CN\n");
335 fprintf(conf, "max_num_sta=32\n");
336 fprintf(conf, "macaddr_acl=0\n");
337 fprintf(conf, "deny_mac_file=/etc/wifi/hostapd.deny\n");
338 fprintf(conf, "accept_mac_file=/etc/wifi/hostapd.accept\n");
339 fprintf(conf, "sae_pwe=2\n");
340
341 fclose(conf);
342 return GSW_HAL_SUCCESS;
343 }
344
345 // 处理每一行(新增ctrl_interface和ctrl_interface_group的检查)
346 char *line = strtok(buffer, "\n");
347 int ssid_processed = 0; // 标记是否已处理过ssid行
348 while (line)
349 {
350 if (!ssid_processed && strncmp(line, "ssid=", 5) == 0)
351 {
352 char ssid_line[64];
353 snprintf(ssid_line, sizeof(ssid_line), "ssid=%s", ssid);
354 strcat(new_config, ssid_line);
355 strcat(new_config, "\n");
356 ssid_processed = 1;
357 line = strtok(NULL, "\n");
358 continue;
359 }
360 else if (strncmp(line, "ssid=", 5) == 0)
361 {
362 line = strtok(NULL, "\n");
363 continue;
364 }
365 else if (strncmp(line, "ctrl_interface=", 15) == 0)
366 {
367 char ctrl_line[128];
368 snprintf(ctrl_line, sizeof(ctrl_line), "ctrl_interface=%s", CTRL_INTERFACE);
369 strcat(new_config, ctrl_line);
370 strcat(new_config, "\n");
371 }
372 else if (strncmp(line, "ctrl_interface_group=", 21) == 0)
373 {
374 char ctrl_group_line[64];
375 snprintf(ctrl_group_line, sizeof(ctrl_group_line), "ctrl_interface_group=0");
376 strcat(new_config, ctrl_group_line);
377 strcat(new_config, "\n");
378 }
379 else
380 {
381 strcat(new_config, line);
382 strcat(new_config, "\n");
383 }
384 line = strtok(NULL, "\n");
385 }
386
387 if (!ssid_processed)
388 {
389 char ssid_line[64];
390 snprintf(ssid_line, sizeof(ssid_line), "ssid=%s\n", ssid);
391 strcat(new_config, ssid_line);
392 }
393
394 return write_file(HOSTAPD_CONF_PATH, new_config);
395}
396
397int gsw_wifi_ap_ssid_get(char *ssid)
398{
399 if (handle())
400 return GSW_HAL_NORMAL_FAIL;
zw.wangbb7f28c2025-06-27 19:41:19 +0800401 if (ssid == NULL) {
zw.wangbd342b92025-07-21 11:24:16 +0800402 LOGE(GSW_WIFI,"ssid cannot be NULL");
zw.wangbb7f28c2025-06-27 19:41:19 +0800403 return GSW_HAL_NORMAL_FAIL;
404 }
zw.wang00477802025-06-12 19:48:37 +0800405 char buffer[1024] = {0};
406 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
407 {
408 return GSW_HAL_NORMAL_FAIL;
409 }
410 char *ssid_line = strstr(buffer, "ssid=");
411 if (ssid_line)
412 {
413 ssid_line += 5; // 跳过 "ssid="
414 char *end = strchr(ssid_line, '\n');
415 if (end)
416 *end = '\0';
417 strncpy(ssid, ssid_line, MAX_SSID_LEN);
418 }
zw.wangbd342b92025-07-21 11:24:16 +0800419 LOGI(GSW_WIFI,"AP get ssid: %s\n", ssid);
zw.wang00477802025-06-12 19:48:37 +0800420 return GSW_HAL_SUCCESS;
421}
422
423int gsw_wifi_ap_frequency_set(int gsw_wifi_frequency)
424{
425 if (handle())
426 return GSW_HAL_NORMAL_FAIL;
427 char buffer[4096] = {0};
428 char new_config[4096] = {0};
zw.wangbd342b92025-07-21 11:24:16 +0800429 LOGI(GSW_WIFI,"AP set frequency: %d\n", gsw_wifi_frequency);
zw.wang00477802025-06-12 19:48:37 +0800430 gsw_wifi_bandwidth_type_e gsw_wifi_bandwidth;
431 gsw_wifi_ap_bandwidth_get(&gsw_wifi_bandwidth);
432 if (gsw_wifi_bandwidth == GSW_WIFI_BANDWIDTH_HT80 && gsw_wifi_frequency == 1)
433 {
zw.wangbd342b92025-07-21 11:24:16 +0800434 LOGE(GSW_WIFI,"HT80 cannot be set to 2.4G");
zw.wang00477802025-06-12 19:48:37 +0800435 return GSW_HAL_NORMAL_FAIL;
436 }
437 // 读取现有配置
438 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
439 {
440 return GSW_HAL_NORMAL_FAIL;
441 }
442
443 // 处理每一行
444 char *line = strtok(buffer, "\n");
445 while (line)
446 {
447 // 如果是hw_mode行,替换为新值
448 if (strstr(line, "hw_mode=") != NULL)
449 {
450 char mode_line[32];
451 snprintf(mode_line, sizeof(mode_line), "hw_mode=%c", gsw_wifi_frequency == 1 ? 'g' : 'a');
452 strcat(new_config, mode_line);
453 }
454 else
455 {
456 strcat(new_config, line);
457 }
458 strcat(new_config, "\n");
459 line = strtok(NULL, "\n");
460 }
461
462 // 如果没有找到hw_mode行,添加新的设置
463 if (strstr(new_config, "hw_mode=") == NULL)
464 {
465 char mode_line[32];
466 snprintf(mode_line, sizeof(mode_line), "hw_mode=%c\n", gsw_wifi_frequency == 1 ? 'g' : 'a');
467 strcat(new_config, mode_line);
468 }
469
470 // 写回文件
471 return write_file(HOSTAPD_CONF_PATH, new_config);
472}
473
474int gsw_wifi_ap_frequency_get(int *gsw_wifi_frequency)
475{
476 if (handle())
477 return GSW_HAL_NORMAL_FAIL;
zw.wangbb7f28c2025-06-27 19:41:19 +0800478 if (gsw_wifi_frequency == NULL) {
zw.wangbd342b92025-07-21 11:24:16 +0800479 LOGE(GSW_WIFI,"gsw_wifi_frequency cannot be NULL");
zw.wangbb7f28c2025-06-27 19:41:19 +0800480 return GSW_HAL_NORMAL_FAIL;
481 }
zw.wang00477802025-06-12 19:48:37 +0800482 char buffer[1024] = {0};
483 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
484 {
485 return GSW_HAL_NORMAL_FAIL;
486 }
487 char *mode_line = strstr(buffer, "hw_mode=");
488 if (mode_line)
489 {
490 mode_line += 8; // 跳过 "hw_mode="
491 *gsw_wifi_frequency = (mode_line[0] == 'g') ? 1 : 2;
492 }
zw.wangbd342b92025-07-21 11:24:16 +0800493 LOGI(GSW_WIFI,"AP get frequency: %d\n", *gsw_wifi_frequency);
zw.wang00477802025-06-12 19:48:37 +0800494 return GSW_HAL_SUCCESS;
495}
496
497int gsw_wifi_ap_bandwidth_set(gsw_wifi_bandwidth_type_e bandwidth)
498{
499 if (handle())
500 return GSW_HAL_NORMAL_FAIL;
501 char buffer[4096] = {0};
502 char new_config[4096] = {0};
zw.wangbd342b92025-07-21 11:24:16 +0800503 LOGI(GSW_WIFI,"AP set bandwidth: %d\n", bandwidth);
zw.wang00477802025-06-12 19:48:37 +0800504 int current_freq;
505 if (gsw_wifi_ap_frequency_get(&current_freq) != 0)
506 {
zw.wangbd342b92025-07-21 11:24:16 +0800507 LOGI(GSW_WIFI,"Failed to get current frequency\n");
zw.wang00477802025-06-12 19:48:37 +0800508 return GSW_HAL_NORMAL_FAIL;
509 }
510 if (current_freq == 1 && bandwidth == GSW_WIFI_BANDWIDTH_HT80) // 1表示2.4GHz
511 {
zw.wangbd342b92025-07-21 11:24:16 +0800512 LOGI(GSW_WIFI,"2.4GHz band does not support 80MHz bandwidth");
zw.wang00477802025-06-12 19:48:37 +0800513 return GSW_HAL_NORMAL_FAIL;
514 }
515 // 读取现有配置
516 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
517 return GSW_HAL_NORMAL_FAIL;
518
519 // 定义各带宽类型对应的替换规则(新增三个参数)
520 const char *ht_capab = NULL;
521 const char *vht_capab = NULL;
522 const char *vht_oper_chwidth = NULL;
523 const char *he_oper_chwidth = NULL;
524 int vht_oper_centr_freq_seg0_idx_val = 0;
525 int he_oper_centr_freq_seg0_idx_val = 0;
526 int he_su_beamformee_val = 0;
527
528 // 根据带宽类型初始化替换值(补充新增参数逻辑)
529 switch (bandwidth)
530 {
531 case GSW_WIFI_BANDWIDTH_HT20:
532 ht_capab = "ht_capab=[HT20][SHORT-GI-20][LDPC][RX-STBC1]";
533 vht_capab = "vht_capab=[VHT20][SHORT-GI-20][MAX-A-MPDU-LEN-EXP7]";
534 vht_oper_chwidth = "vht_oper_chwidth=0";
535 he_oper_chwidth = "he_oper_chwidth=0";
536 vht_oper_centr_freq_seg0_idx_val = 0;
537 he_oper_centr_freq_seg0_idx_val = 0;
538 he_su_beamformee_val = 0;
539 break;
540 case GSW_WIFI_BANDWIDTH_HT40:
541 ht_capab = "ht_capab=[SHORT-GI-20][SHORT-GI-40][HT40+][HT40-][LDPC][RX-STBC1]";
542 vht_capab = "vht_capab=[SHORT-GI-80][VHT40+][VHT40-][MAX-A-MPDU-LEN-EXP7][RX-STBC-1][RX-LDPC]";
543 vht_oper_chwidth = "vht_oper_chwidth=0";
544 he_oper_chwidth = "he_oper_chwidth=0";
545 vht_oper_centr_freq_seg0_idx_val = 0;
546 he_oper_centr_freq_seg0_idx_val = 0;
547 he_su_beamformee_val = 0;
548 break;
549 case GSW_WIFI_BANDWIDTH_HT80:
550 ht_capab = "ht_capab=[HT20][HT40+][HT40-][SHORT-GI-40][LDPC][RX-STBC1]";
551 vht_capab = "vht_capab=[VHT40][VHT80][SHORT-GI-80][MAX-A-MPDU-LEN-EXP7]";
552 vht_oper_chwidth = "vht_oper_chwidth=1";
553 he_oper_chwidth = "he_oper_chwidth=1";
554 vht_oper_centr_freq_seg0_idx_val = 42;
555 he_oper_centr_freq_seg0_idx_val = 42;
556 he_su_beamformee_val = 1;
557 break;
558 default:
559 return GSW_HAL_NORMAL_FAIL;
560 }
561
562 // 新增标记:确保只处理第一个ht_capab行
563 int ht_capab_processed = 0;
564
565 // 逐行处理配置文件(增加新增参数的匹配)
566 char *line = strtok(buffer, "\n");
567 while (line)
568 {
569 // 处理ht_capab行(仅替换第一个以"ht_capab="开头的行)
570 if (!ht_capab_processed && strncmp(line, "ht_capab=", 9) == 0)
571 {
572 if (ht_capab)
573 {
574 strcat(new_config, ht_capab);
575 ht_capab_processed = 1; // 标记已处理
576 }
577 else
578 {
579 strcat(new_config, line);
580 }
581 strcat(new_config, "\n");
582 line = strtok(NULL, "\n");
583 continue;
584 }
585 else if (strncmp(line, "ht_capab=", 9) == 0)
586 {
587 line = strtok(NULL, "\n");
588 continue;
589 }
590 else if (strncmp(line, "vht_oper_centr_freq_seg0_idx=", 25) == 0)
591 {
592 char vht_centr_line[64];
593 snprintf(vht_centr_line, sizeof(vht_centr_line), "vht_oper_centr_freq_seg0_idx=%d", vht_oper_centr_freq_seg0_idx_val);
594 strcat(new_config, vht_centr_line);
595 strcat(new_config, "\n");
596 }
597 else if (strncmp(line, "he_oper_centr_freq_seg0_idx=", 25) == 0)
598 {
599 char he_centr_line[64];
600 snprintf(he_centr_line, sizeof(he_centr_line), "he_oper_centr_freq_seg0_idx=%d", he_oper_centr_freq_seg0_idx_val);
601 strcat(new_config, he_centr_line);
602 strcat(new_config, "\n");
603 }
604 else if (strncmp(line, "he_su_beamformee=", 17) == 0)
605 {
606 char he_beam_line[32];
607 snprintf(he_beam_line, sizeof(he_beam_line), "he_su_beamformee=%d", he_su_beamformee_val);
608 strcat(new_config, he_beam_line);
609 strcat(new_config, "\n");
610 }
611 else if (strncmp(line, "vht_capab=", 10) == 0)
612 {
613 if (vht_capab)
614 strcat(new_config, vht_capab);
615 else
616 strcat(new_config, line);
617 strcat(new_config, "\n");
618 }
619 else if (strncmp(line, "vht_oper_chwidth=", 17) == 0)
620 {
621 if (vht_oper_chwidth)
622 strcat(new_config, vht_oper_chwidth);
623 else
624 strcat(new_config, line);
625 strcat(new_config, "\n");
626 }
627 else if (strncmp(line, "he_oper_chwidth=", 16) == 0)
628 {
629 if (he_oper_chwidth)
630 strcat(new_config, he_oper_chwidth);
631 else
632 strcat(new_config, line);
633 strcat(new_config, "\n");
634 }
635 else
636 {
637 strcat(new_config, line);
638 strcat(new_config, "\n");
639 }
640 line = strtok(NULL, "\n");
641 }
642
643 // 如果原文件无ht_capab行,补充新行(保持原有逻辑)
644 if (!ht_capab_processed && ht_capab)
645 {
646 strcat(new_config, ht_capab);
647 strcat(new_config, "\n");
648 }
649
650 // 检查vht_oper_centr_freq_seg0_idx是否已处理
651 if (!strstr(new_config, "vht_oper_centr_freq_seg0_idx="))
652 {
653 char vht_centr_line[64];
654 snprintf(vht_centr_line, sizeof(vht_centr_line), "vht_oper_centr_freq_seg0_idx=%d\n", vht_oper_centr_freq_seg0_idx_val);
655 strcat(new_config, vht_centr_line);
656 }
657 // 检查he_oper_centr_freq_seg0_idx是否已处理
658 if (!strstr(new_config, "he_oper_centr_freq_seg0_idx="))
659 {
660 char he_centr_line[64];
661 snprintf(he_centr_line, sizeof(he_centr_line), "he_oper_centr_freq_seg0_idx=%d\n", he_oper_centr_freq_seg0_idx_val);
662 strcat(new_config, he_centr_line);
663 }
664 // 检查he_su_beamformee是否已处理
665 if (!strstr(new_config, "he_su_beamformee="))
666 {
667 char he_beam_line[32];
668 snprintf(he_beam_line, sizeof(he_beam_line), "he_su_beamformee=%d\n", he_su_beamformee_val);
669 strcat(new_config, he_beam_line);
670 }
671
672 return write_file(HOSTAPD_CONF_PATH, new_config);
673}
674
675
676int gsw_wifi_ap_bandwidth_get(gsw_wifi_bandwidth_type_e *bandwidth)
677{
678 if (handle())
679 return GSW_HAL_NORMAL_FAIL;
680 char buffer[1024] = {0};
681 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
682 {
683 return GSW_HAL_NORMAL_FAIL;
684 }
685
686 // 精确解析ht_capab参数
687 char *ht_line = strstr(buffer, "ht_capab=");
688 char *vht_line = strstr(buffer, "vht_capab=");
689 char *vht_width = strstr(buffer, "vht_oper_chwidth=1");
690
691 // 优先检查80MHz配置
692 if (vht_width && vht_line && strstr(vht_line, "VHT80"))
693 {
694 *bandwidth = GSW_WIFI_BANDWIDTH_HT80;
695 }
696 else if (ht_line && strstr(ht_line, "HT40+"))
697 {
698 *bandwidth = GSW_WIFI_BANDWIDTH_HT40;
699 }
700 else
701 {
702 *bandwidth = GSW_WIFI_BANDWIDTH_HT20;
703 }
zw.wangbd342b92025-07-21 11:24:16 +0800704 LOGI(GSW_WIFI,"AP get bandwidth: %d\n", *bandwidth);
zw.wang00477802025-06-12 19:48:37 +0800705 return GSW_HAL_SUCCESS;
706}
707
708int gsw_wifi_ap_channel_set(int channel)
709{
710 if (handle())
711 return GSW_HAL_NORMAL_FAIL;
712 // 新增频率和信道校验
zw.wangbd342b92025-07-21 11:24:16 +0800713 LOGI(GSW_WIFI,"AP set channel: %d\n", channel);
zw.wang00477802025-06-12 19:48:37 +0800714 int current_freq;
715 if (gsw_wifi_ap_frequency_get(&current_freq) != 0)
716 {
zw.wangbd342b92025-07-21 11:24:16 +0800717 LOGE(GSW_WIFI,"Failed to get current frequency\n");
zw.wang00477802025-06-12 19:48:37 +0800718 return GSW_HAL_NORMAL_FAIL;
719 }
720 // 定义各频段支持的信道范围
721 const int valid_2g_channels[] = VALID_2G_CHANNELS;
722 const int valid_5g_channels[] = VALID_5G_CHANNELS;
723
724 int valid = 0;
725 if (current_freq == 1)
726 { // 2.4GHz
727 for (int i = 0; i < sizeof(valid_2g_channels) / sizeof(int); i++)
728 {
729 if (channel == valid_2g_channels[i])
730 {
731 valid = 1;
732 break;
733 }
734 }
735 }
736 else
737 { // 5GHz
738 for (int i = 0; i < sizeof(valid_5g_channels) / sizeof(int); i++)
739 {
740 if (channel == valid_5g_channels[i])
741 {
742 valid = 1;
743 break;
744 }
745 }
746 }
747
748 if (!valid && channel != 0)
749 { // 允许0表示自动选择
zw.wangbd342b92025-07-21 11:24:16 +0800750 LOGI(GSW_WIFI,"Invalid channel %d for %s band\n",
zw.wang00477802025-06-12 19:48:37 +0800751 channel, (current_freq == 1) ? "2.4GHz" : "5GHz");
752 return GSW_HAL_NORMAL_FAIL;
753 }
754
755 // 修改为读取整个文件并替换channel行
756 char buffer[4096] = {0};
757 char new_config[4096] = {0};
758 int noscan_set = 0; // 新增:标记是否已处理noscan行
759
760 // 读取现有配置
761 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
762 {
763 return GSW_HAL_NORMAL_FAIL;
764 }
765
766 // 处理每一行
767 char *line = strtok(buffer, "\n");
768 while (line)
769 {
770 // 新增:处理noscan行
771 if (strstr(line, "noscan=") != NULL)
772 {
773 char noscan_line[32];
774 snprintf(noscan_line, sizeof(noscan_line), "noscan=%d", (channel == 0) ? 0 : 1);
775 strcat(new_config, noscan_line);
776 strcat(new_config, "\n");
777 noscan_set = 1;
778 }
779 // 如果是channel行,替换为新值
780 else if (strstr(line, "channel=") != NULL)
781 {
782 char channel_line[32];
783 snprintf(channel_line, sizeof(channel_line), "channel=%d", channel);
784 strcat(new_config, channel_line);
785 strcat(new_config, "\n");
786 }
787 // 其他行保持不变
788 else
789 {
790 strcat(new_config, line);
791 strcat(new_config, "\n");
792 }
793 line = strtok(NULL, "\n");
794 }
795
796 // 如果没有找到channel行,添加新的channel设置
797 if (strstr(new_config, "channel=") == NULL)
798 {
799 char channel_line[32];
800 snprintf(channel_line, sizeof(channel_line), "channel=%d\n", channel);
801 strcat(new_config, channel_line);
802 }
803
804 // 新增:如果没有找到noscan行,根据channel值补充
805 if (!noscan_set)
806 {
807 char noscan_line[32];
808 snprintf(noscan_line, sizeof(noscan_line), "noscan=%d\n", (channel == 0) ? 0 : 1);
809 strcat(new_config, noscan_line);
810 }
811
812 // 写回文件
813 return write_file(HOSTAPD_CONF_PATH, new_config);
814}
815
816
817int gsw_wifi_ap_channel_get(int *channel)
818{
819 if (handle())
820 return GSW_HAL_NORMAL_FAIL;
821 if (channel == NULL)
822 {
zw.wangbd342b92025-07-21 11:24:16 +0800823 LOGI(GSW_WIFI,"channel is NULL\n");
zw.wang00477802025-06-12 19:48:37 +0800824 return GSW_HAL_NORMAL_FAIL;
825 }
826 char buffer[1024] = {0};
827 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
828 {
829 return GSW_HAL_NORMAL_FAIL;
830 }
831 char *channel_line = strstr(buffer, "channel=");
832 if (channel_line)
833 {
834 channel_line += 8; // 跳过 "channel="
835 *channel = atoi(channel_line);
836 }
zw.wangbd342b92025-07-21 11:24:16 +0800837 LOGI(GSW_WIFI,"AP get channel: %d\n", *channel);
zw.wang00477802025-06-12 19:48:37 +0800838 return GSW_HAL_SUCCESS;
839}
840
841int gsw_wifi_ap_auth_set(gsw_wifi_auth_e auth)
842{
843 if (handle())
844 return GSW_HAL_NORMAL_FAIL;
845 char buffer[4096] = {0};
846 char new_config[4096] = {0};
847 int wpa_set = 0, key_mgmt_set = 0, wpa_pairwise_set = 0, rsn_pairwise_set = 0, ieee80211w_set = 0, auth_algs_set = 0;
848
849 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
850 {
851 return GSW_HAL_NORMAL_FAIL;
852 }
zw.wangbd342b92025-07-21 11:24:16 +0800853 LOGI(GSW_WIFI,"AP set auth: %d\n", auth);
zw.wang00477802025-06-12 19:48:37 +0800854 char *line = strtok(buffer, "\n");
855 while (line)
856 {
857 // 匹配行首为"wpa="的行(长度4)
858 if (strncmp(line, "wpa=", 4) == 0)
859 {
860 char wpa_line[32];
861 if (auth == GSW_WIFI_AUTH_OPEN || auth == GSW_WIFI_AUTH_WEP)
862 snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 0);
863 else if (auth == GSW_WIFI_AUTH_WPA_PSK)
864 snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 1);
865 else if (auth == GSW_WIFI_AUTH_WPA2_PSK)
866 {
867 snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 3);
868 }
869 else if (auth == GSW_WIFI_AUTH_WPA3_PSK)
870 snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 2);
871 else
872 snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 2);
873 strcat(new_config, wpa_line);
874 strcat(new_config, "\n");
875 wpa_set = 1;
876 }
877 else if (strncmp(line, "wpa_key_mgmt=", 13) == 0)
878 {
879 const char *key_mgmt = (auth == GSW_WIFI_AUTH_WPA3_PSK) ? "SAE WPA-PSK" : "WPA-PSK";
880 char key_mgmt_line[64];
881 snprintf(key_mgmt_line, sizeof(key_mgmt_line), "wpa_key_mgmt=%s", key_mgmt);
882 strcat(new_config, key_mgmt_line);
883 strcat(new_config, "\n");
884 key_mgmt_set = 1;
885 }
886 else if (strncmp(line, "auth_algs=", 9) == 0)
887 {
888 char auth_algs_line[32];
889 snprintf(auth_algs_line, sizeof(auth_algs_line), "auth_algs=%d", (auth == GSW_WIFI_AUTH_WEP) ? 3 : 1);
890 strcat(new_config, auth_algs_line);
891 strcat(new_config, "\n");
892 auth_algs_set = 1;
893 }
894 else if (strncmp(line, "wpa_pairwise=", 13) == 0)
895 {
896 const char *pairwise = NULL;
897 if (auth == GSW_WIFI_AUTH_WPA_PSK)
898 pairwise = "TKIP";
899 else if (auth == GSW_WIFI_AUTH_WPA2_PSK)
900 pairwise = "TKIP CCMP";
901 else if (auth == GSW_WIFI_AUTH_WPA3_PSK)
902 pairwise = "CCMP";
903 else
904 pairwise = "TKIP CCMP";
905 char pairwise_line[64];
906 snprintf(pairwise_line, sizeof(pairwise_line), "wpa_pairwise=%s", pairwise);
907 strcat(new_config, pairwise_line);
908 strcat(new_config, "\n");
909 wpa_pairwise_set = 1;
910 }
911 else if (strncmp(line, "rsn_pairwise=", 13) == 0)
912 {
913 if (auth == GSW_WIFI_AUTH_WPA2_PSK || auth == GSW_WIFI_AUTH_WPA3_PSK)
914 {
915 strcat(new_config, "rsn_pairwise=CCMP\n");
916 }
917 rsn_pairwise_set = 1;
918 }
919 else if (strncmp(line, "ieee80211w=", 11) == 0)
920 {
921 if (auth == GSW_WIFI_AUTH_WPA3_PSK)
922 {
923 strcat(new_config, "ieee80211w=2\n"); // WPA3 强制 MFP
924 ieee80211w_set = 1;
925 }
926 else if (auth == GSW_WIFI_AUTH_WPA2_PSK)
927 {
928 strcat(new_config, "ieee80211w=1\n"); // WPA2 可选 MFP
929 ieee80211w_set = 1;
930 }
931 // 其他模式不添加该行(直接跳过)
932 }
933 else
934 {
935 strcat(new_config, line);
936 strcat(new_config, "\n");
937 }
938 line = strtok(NULL, "\n");
939 }
940
941 // 补全未匹配的配置项
942 if (!wpa_set)
943 {
944 if (auth == GSW_WIFI_AUTH_OPEN || auth == GSW_WIFI_AUTH_WEP)
945 strcat(new_config, "wpa=0");
946 else if (auth == GSW_WIFI_AUTH_WPA_PSK)
947 strcat(new_config, "wpa=1");
948 else if (auth == GSW_WIFI_AUTH_WPA2_PSK)
949 {
950 strcat(new_config, "wpa=3");
951 }
952 else if (auth == GSW_WIFI_AUTH_WPA3_PSK)
953 strcat(new_config, "wpa=2");
954 else
955 strcat(new_config, "wpa=2");
956 strcat(new_config, "\n");
957 }
958 if (!key_mgmt_set)
959 {
960 const char *key_mgmt = (auth == GSW_WIFI_AUTH_WPA3_PSK) ? "WPA-PSK SAE" : "WPA-PSK";
961 strcat(new_config, "wpa_key_mgmt=");
962 strcat(new_config, key_mgmt);
963 strcat(new_config, "\n");
964 }
965 if (!auth_algs_set)
966 {
967 char auth_algs_line[32];
968 snprintf(auth_algs_line, sizeof(auth_algs_line), "auth_algs=%d\n", (auth == GSW_WIFI_AUTH_WEP) ? 3 : 1);
969 strcat(new_config, auth_algs_line);
970 }
971 if (!wpa_pairwise_set)
972 {
973 const char *pairwise = NULL;
974 if (auth == GSW_WIFI_AUTH_WPA_PSK)
975 pairwise = "TKIP";
976 else if (auth == GSW_WIFI_AUTH_WPA2_PSK)
977 pairwise = "TKIP CCMP";
978 else if (auth == GSW_WIFI_AUTH_WPA3_PSK)
979 pairwise = "CCMP";
980 else
981 pairwise = "TKIP CCMP";
982 strcat(new_config, "wpa_pairwise=");
983 strcat(new_config, pairwise);
984 strcat(new_config, "\n");
985 }
986 if (!rsn_pairwise_set)
987 {
988 if (auth == GSW_WIFI_AUTH_WPA2_PSK || auth == GSW_WIFI_AUTH_WPA3_PSK)
989 {
990 strcat(new_config, "rsn_pairwise=CCMP\n");
991 }
992 }
993 if (!ieee80211w_set && (auth == GSW_WIFI_AUTH_WPA3_PSK || auth == GSW_WIFI_AUTH_WPA2_PSK))
994 {
995 // 仅当需要时补全 ieee80211w 行(其他模式不添加)
996 strcat(new_config, (auth == GSW_WIFI_AUTH_WPA3_PSK) ? "ieee80211w=2\n" : "ieee80211w=1\n");
997 }
998
999 return write_file(HOSTAPD_CONF_PATH, new_config);
1000}
1001
1002int gsw_wifi_ap_auth_get(gsw_wifi_auth_e *auth)
1003{
1004 if (handle())
1005 return GSW_HAL_NORMAL_FAIL;
1006 char buffer[1024] = {0};
1007 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
1008 {
1009 return GSW_HAL_NORMAL_FAIL;
1010 }
1011
1012 char *auth_algs_line = strstr(buffer, "auth_algs=");
1013 if (auth_algs_line)
1014 {
1015 auth_algs_line += 10; // 跳过 "auth_algs="
1016 int auth_algs_val = atoi(auth_algs_line);
1017 if (auth_algs_val == 3)
1018 {
1019 *auth = GSW_WIFI_AUTH_WEP;
1020 return GSW_HAL_SUCCESS;
1021 }
1022 }
1023 // 优先通过 wpa_key_mgmt 判断 WPA3(包含 SAE 关键字)
1024 char *key_mgmt_line = strstr(buffer, "wpa_key_mgmt=");
1025 if (key_mgmt_line)
1026 {
1027 key_mgmt_line += 12; // 跳过 "wpa_key_mgmt="
1028 if (strstr(key_mgmt_line, "SAE"))
1029 {
1030 *auth = GSW_WIFI_AUTH_WPA3_PSK;
1031 return GSW_HAL_SUCCESS;
1032 }
1033 }
1034
1035 // 若未找到 SAE,再通过 wpa= 判断 WPA2 或开放模式
1036 char *wpa_line = strstr(buffer, "wpa=");
1037 if (wpa_line)
1038 {
1039 wpa_line += 4;
1040 int wpa_val = atoi(wpa_line);
1041 if(wpa_val > 1)
1042 {
1043 *auth = GSW_WIFI_AUTH_WPA2_PSK;
1044 }
1045 else if (wpa_val == 1)
1046 {
1047 *auth = GSW_WIFI_AUTH_WPA_PSK;
1048 }
1049 else
1050 {
1051 *auth = GSW_WIFI_AUTH_OPEN;
1052 }
1053 }
1054 else
1055 {
1056 *auth = GSW_WIFI_AUTH_OPEN; // 默认开放模式
1057 }
zw.wangbd342b92025-07-21 11:24:16 +08001058 LOGI(GSW_WIFI,"AP get auth: %d\n", *auth);
zw.wang00477802025-06-12 19:48:37 +08001059 return GSW_HAL_SUCCESS;
1060}
1061
1062int gsw_wifi_ap_password_set(char *password)
1063{
1064 if (handle())
1065 return GSW_HAL_NORMAL_FAIL;
1066 char buffer[4096] = {0};
1067 char new_config[4096] = {0};
1068 int has_passphrase = 0; // 标记是否已处理过密码行
1069 if (password == NULL) {
zw.wangbd342b92025-07-21 11:24:16 +08001070 LOGE(GSW_WIFI,"Password cannot be NULL");
zw.wang00477802025-06-12 19:48:37 +08001071 return GSW_HAL_NORMAL_FAIL;
1072 }
zw.wangbd342b92025-07-21 11:24:16 +08001073 LOGI(GSW_WIFI,"AP set password\n");
zw.wang00477802025-06-12 19:48:37 +08001074 size_t pass_len = strlen(password);
1075 if (pass_len < MIN_PASSWORD_LEN || pass_len > MAX_PASSWORD_LEN) {
zw.wangbd342b92025-07-21 11:24:16 +08001076 LOGE(GSW_WIFI,"Password length must be 8-63 characters, current length: %zu", pass_len);
zw.wang00477802025-06-12 19:48:37 +08001077 return GSW_HAL_NORMAL_FAIL;
1078 }
1079 // 读取现有配置文件内容
1080 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
1081 {
1082 return GSW_HAL_NORMAL_FAIL;
1083 }
1084
1085 // 逐行处理配置文件(修改:增加密码行存在标记)
1086 char *line = strtok(buffer, "\n");
1087 while (line)
1088 {
1089 // 匹配到原密码行时替换为新密码,并标记已处理
1090 if (strstr(line, "wpa_passphrase=") != NULL)
1091 {
1092 char new_pass_line[128];
1093 snprintf(new_pass_line, sizeof(new_pass_line), "wpa_passphrase=%s", password);
1094 strcat(new_config, new_pass_line);
1095 has_passphrase = 1; // 标记存在密码行
1096 }
1097 else
1098 {
1099 strcat(new_config, line);
1100 }
1101 strcat(new_config, "\n"); // 保留换行符
1102 line = strtok(NULL, "\n");
1103 }
1104
1105 // 若原文件无密码行,添加新密码行到末尾
1106 if (!has_passphrase)
1107 {
1108 char new_pass_line[128];
1109 snprintf(new_pass_line, sizeof(new_pass_line), "wpa_passphrase=%s\n", password);
1110 strcat(new_config, new_pass_line);
1111 }
1112
1113 // 写回配置文件
1114 return write_file(HOSTAPD_CONF_PATH, new_config);
1115}
1116
1117int gsw_wifi_ap_password_get(char *password)
1118{
1119 if (handle())
1120 return GSW_HAL_NORMAL_FAIL;
1121 char buffer[4096] = {0}; // 使用更大的缓冲区
1122 if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1)
1123 {
zw.wangbd342b92025-07-21 11:24:16 +08001124 LOGI(GSW_WIFI,"Failed to read hostapd config file\n");
zw.wang00477802025-06-12 19:48:37 +08001125 return GSW_HAL_NORMAL_FAIL;
1126 }
1127
1128 // 查找密码行
1129 char *passphrase_line = strstr(buffer, "wpa_passphrase=");
1130 if (!passphrase_line)
1131 {
zw.wangbd342b92025-07-21 11:24:16 +08001132 LOGI(GSW_WIFI,"No password line found in config\n");
zw.wang00477802025-06-12 19:48:37 +08001133 return GSW_HAL_NORMAL_FAIL;
1134 }
1135
1136 passphrase_line += 15; // 跳过 "wpa_passphrase="
1137
1138 // 查找行结束位置
1139 char *end = strchr(passphrase_line, '\n');
1140 if (end)
1141 {
1142 *end = '\0'; // 确保字符串正确终止
1143 }
1144
1145 // 检查密码长度是否合法
1146 size_t pass_len = strlen(passphrase_line);
1147 if (pass_len == 0 || pass_len >= MAX_PASSWORD_LEN)
1148 {
zw.wangbd342b92025-07-21 11:24:16 +08001149 LOGI(GSW_WIFI,"Invalid password length: %zu\n", pass_len);
zw.wang00477802025-06-12 19:48:37 +08001150 return GSW_HAL_NORMAL_FAIL;
1151 }
1152
1153 // 复制密码
1154 strncpy(password, passphrase_line, MAX_PASSWORD_LEN);
1155 password[MAX_PASSWORD_LEN] = '\0'; // 确保终止
zw.wangbd342b92025-07-21 11:24:16 +08001156 LOGI(GSW_WIFI,"AP get password\n");
zw.wang00477802025-06-12 19:48:37 +08001157 return GSW_HAL_SUCCESS;
1158}
1159
1160/**
1161 * @brief 获取 AP 模式的运行状态
1162 * @param [out] ap_status 用于存储 AP 模式运行状态的指针
1163 * @retval 0 表示函数执行成功
1164 */
1165int gsw_wifi_get_ap_status(gsw_wifi_ap_run_status_e *ap_status)
1166{
1167 if (handle())
1168 return GSW_HAL_NORMAL_FAIL;
1169 int result = is_hostapd_running();
1170 if (result == 0)
1171 {
1172 *ap_status = GSW_WIFI_AP_STATUS_ENABLE;
1173 }
1174 else
1175 {
1176 *ap_status = GSW_WIFI_AP_STATUS_DISABLE;
1177 }
1178 return GSW_HAL_SUCCESS;
1179}
1180
1181/* STA 模式相关函数实现 */
1182
1183// 辅助函数:更新 wpa_supplicant 配置文件
1184int update_wpa_supplicant_conf()
1185{
1186 char new_config[4096] = {0};
1187
1188 // 直接写入必需配置项(覆盖原有文件),新增WPA3支持的关键配置
1189 const char *required_configs[] = {
1190 "ctrl_interface=/var/run/wpa_supplicant", // 接口路径
1191 "ctrl_interface_group=root", // 组设置
1192 "update_config=1", // 允许自动更新配置
1193 "ap_scan=1", // 主动扫描AP
1194 // 网络块配置调整:使用数值19表示SAE组(替代SUITE_B_192文本标识)
1195 "network={\n key_mgmt=SAE\n pairwise=CCMP\n ieee80211w=2\n }"
1196 };
1197 int config_count = sizeof(required_configs) / sizeof(required_configs[0]);
1198
1199 // 添加所有必需的配置项(确保顺序)
1200 for (int i = 0; i < config_count; i++)
1201 {
1202 strcat(new_config, required_configs[i]);
1203 strcat(new_config, "\n");
1204 }
1205
1206 // 写回文件(完全覆盖)
1207 return write_file(WPA_SUPPLICANT_CONF_PATH, new_config);
1208}
1209
1210int gsw_wifi_sta_start()
1211{
1212 if (handle())
1213 return GSW_HAL_NORMAL_FAIL;
zw.wangbd342b92025-07-21 11:24:16 +08001214 LOGI(GSW_WIFI,"Starting wpa_supplicant...\n");
zw.wang00477802025-06-12 19:48:37 +08001215 // 更新 wpa_supplicant 配置文件
1216 if (update_wpa_supplicant_conf() != 0)
1217 {
zw.wangbd342b92025-07-21 11:24:16 +08001218 LOGI(GSW_WIFI,"Failed to update wpa_supplicant configuration file.\n");
zw.wang00477802025-06-12 19:48:37 +08001219 return GSW_HAL_NORMAL_FAIL;
1220 }
1221
1222 char cmd[MAX_COMMAND_LEN];
1223 snprintf(cmd, sizeof(cmd), "wpa_supplicant -Dnl80211 -i%s -c%s -B", WLAN_STA_DEV, WPA_SUPPLICANT_CONF_PATH);
1224 int status = execute_command(cmd);
1225 sleep(5);
1226 memset(cmd, 0, sizeof(cmd));
1227 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan > /dev/null", WLAN_STA_DEV);
1228 execute_command(cmd);
1229 memset(cmd, 0, sizeof(cmd));
1230 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan_results > /dev/null", WLAN_STA_DEV);
1231 execute_command(cmd);
zw.wangbd342b92025-07-21 11:24:16 +08001232 LOGI(GSW_WIFI,"wpa_supplicant starting success\n");
zw.wang00477802025-06-12 19:48:37 +08001233 return status;
1234}
1235
1236int gsw_wifi_sta_stop()
1237{
1238 if (handle())
1239 return GSW_HAL_NORMAL_FAIL;
zw.wangbd342b92025-07-21 11:24:16 +08001240 LOGI(GSW_WIFI,"Stopping wpa_supplicant...\n");
zw.wang00477802025-06-12 19:48:37 +08001241 if (is_wpa_supplicant_running() != 0)
1242 {
zw.wangbd342b92025-07-21 11:24:16 +08001243 LOGI(GSW_WIFI,"wpa_supplicant is not running, no need to stop.\n");
zw.wang00477802025-06-12 19:48:37 +08001244 return GSW_HAL_SUCCESS;
1245 }
1246
1247 char cmd[MAX_COMMAND_LEN];
1248 snprintf(cmd, sizeof(cmd), "killall wpa_supplicant");
zw.wangbd342b92025-07-21 11:24:16 +08001249 LOGI(GSW_WIFI,"Stoping success\n");
zw.wang00477802025-06-12 19:48:37 +08001250 return execute_command(cmd);
1251}
1252
1253int gsw_wifi_get_sta_status(gsw_wifi_sta_run_status_e *sta_status)
1254{
1255 if (handle())
1256 return GSW_HAL_NORMAL_FAIL;
1257 if (is_wpa_supplicant_running() == 0)
1258 {
1259 *sta_status = GSW_WIFI_STA_STATUS_ENABLE;
1260 }
1261 else
1262 {
1263 *sta_status = GSW_WIFI_STA_STATUS_DISABLE;
1264 }
1265 return GSW_HAL_SUCCESS;
1266}
1267
1268// 修改涉及 wpa_cli 命令的函数
1269int gsw_wifi_get_sta_ssid(char *sta_ssid)
1270{
1271 if (handle())
1272 return GSW_HAL_NORMAL_FAIL;
1273 if (is_wpa_supplicant_running() != 0)
1274 {
1275 return GSW_HAL_NORMAL_FAIL;
1276 }
1277 char cmd[MAX_COMMAND_LEN];
1278 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'ssid=' | cut -d '=' -f 2", WLAN_STA_DEV);
1279 FILE *output = popen(cmd, "r");
1280 if (!output)
1281 return GSW_HAL_NORMAL_FAIL;
1282 char buffer[MAX_SSID_LEN + 1];
1283 if (fgets(buffer, sizeof(buffer), output) == NULL)
1284 {
1285 pclose(output);
1286 return GSW_HAL_NORMAL_FAIL;
1287 }
1288 pclose(output);
1289 buffer[strcspn(buffer, "\n")] = '\0';
1290 strncpy(sta_ssid, buffer, MAX_SSID_LEN);
zw.wangbd342b92025-07-21 11:24:16 +08001291 LOGI(GSW_WIFI,"SSID: %s\n", sta_ssid);
zw.wang00477802025-06-12 19:48:37 +08001292 return GSW_HAL_SUCCESS;
1293}
1294
1295int gsw_wifi_get_sta_auth(gsw_wifi_auth_e *auth)
1296{
1297 if (handle())
1298 return GSW_HAL_NORMAL_FAIL;
1299 if (is_wpa_supplicant_running() != 0)
1300 {
zw.wangbd342b92025-07-21 11:24:16 +08001301 LOGI(GSW_WIFI,"wpa_supplicant is not running.\n");
zw.wang00477802025-06-12 19:48:37 +08001302 return GSW_HAL_NORMAL_FAIL;
1303 }
1304 char cmd[MAX_COMMAND_LEN];
1305 // 修改命令,获取 key_mgmt 字段
1306 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 +08001307 LOGI(GSW_WIFI,"Executing command: %s\n", cmd);
zw.wang00477802025-06-12 19:48:37 +08001308 FILE *output = popen(cmd, "r");
1309 if (!output)
1310 {
zw.wangbd342b92025-07-21 11:24:16 +08001311 LOGI(GSW_WIFI,"Failed to execute command: %s\n", cmd);
zw.wang00477802025-06-12 19:48:37 +08001312 return GSW_HAL_NORMAL_FAIL;
1313 }
1314 char buffer[32];
1315 if (fgets(buffer, sizeof(buffer), output) == NULL)
1316 {
zw.wangbd342b92025-07-21 11:24:16 +08001317 LOGI(GSW_WIFI,"No output from command: %s\n", cmd);
zw.wang00477802025-06-12 19:48:37 +08001318 pclose(output);
1319 return GSW_HAL_NORMAL_FAIL;
1320 }
1321 pclose(output);
1322 buffer[strcspn(buffer, "\n")] = '\0';
zw.wangbd342b92025-07-21 11:24:16 +08001323 LOGI(GSW_WIFI,"Command output: %s\n", buffer);
zw.wang00477802025-06-12 19:48:37 +08001324
1325 if (strstr(buffer, "WPA2-PSK"))
1326 {
1327 *auth = GSW_WIFI_AUTH_WPA2_PSK;
1328 }
1329 else if (strstr(buffer, "WPA-PSK"))
1330 {
1331 *auth = GSW_WIFI_AUTH_WPA_PSK;
1332 }
1333 else if (strstr(buffer, "SAE"))
1334 {
1335 *auth = GSW_WIFI_AUTH_WPA3_PSK;
1336 }
1337 else if (strstr(buffer, "NONE"))
1338 {
1339 *auth = GSW_WIFI_AUTH_OPEN;
1340 }
1341 else
1342 {
zw.wangbd342b92025-07-21 11:24:16 +08001343 LOGI(GSW_WIFI,"Unknown authentication type: %s. Assuming WPA2-PSK.\n", buffer);
zw.wang00477802025-06-12 19:48:37 +08001344 *auth = GSW_WIFI_AUTH_WPA2_PSK;
1345 }
zw.wangbd342b92025-07-21 11:24:16 +08001346 LOGI(GSW_WIFI,"Authentication type: %s\n", buffer);
zw.wang00477802025-06-12 19:48:37 +08001347 return GSW_HAL_SUCCESS;
1348}
1349
1350int gsw_wifi_sta_connect(char *ssid, gsw_wifi_auth_e auth, char *password)
1351{
1352 if (handle())
1353 return GSW_HAL_NORMAL_FAIL;
1354 if (is_wpa_supplicant_running() != 0)
1355 {
1356 return GSW_HAL_NORMAL_FAIL;
1357 }
1358
1359 char quoted_ssid[MAX_SSID_LEN + 3]; // 为引号和字符串结尾留空间
1360 char quoted_password[MAX_PASSWORD_LEN + 3];
1361
1362 // 添加引号
1363 snprintf(quoted_ssid, sizeof(quoted_ssid), "\\\"%s\\\"", ssid);
1364 if (auth != GSW_WIFI_AUTH_OPEN)
1365 {
1366 snprintf(quoted_password, sizeof(quoted_password), "\\\"%s\\\"", password);
1367 }
1368
zw.wangbd342b92025-07-21 11:24:16 +08001369 LOGI(GSW_WIFI,"SSID = %s\n", quoted_ssid);
zw.wang00477802025-06-12 19:48:37 +08001370
1371 char cmd[MAX_COMMAND_LEN];
1372 int status;
1373
1374 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1375 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s remove_network 0", WLAN_STA_DEV);
zw.wangbd342b92025-07-21 11:24:16 +08001376 LOGI(GSW_WIFI,"Executing command type: Remove network\n");
zw.wang00477802025-06-12 19:48:37 +08001377 status = execute_command(cmd);
1378 if (status != 0)
1379 {
1380 return status;
1381 }
1382
1383 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1384 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s ap_scan 1", WLAN_STA_DEV);
zw.wangbd342b92025-07-21 11:24:16 +08001385 LOGI(GSW_WIFI,"Executing command type: Set AP scan\n");
zw.wang00477802025-06-12 19:48:37 +08001386 status = execute_command(cmd);
1387 if (status != 0)
1388 {
1389 return status;
1390 }
1391
1392 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1393 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s add_network", WLAN_STA_DEV);
zw.wangbd342b92025-07-21 11:24:16 +08001394 LOGI(GSW_WIFI,"Executing command type: Add network\n");
zw.wang00477802025-06-12 19:48:37 +08001395 status = execute_command(cmd);
1396 if (status != 0)
1397 {
1398 return status;
1399 }
1400
1401 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1402 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 +08001403 LOGI(GSW_WIFI,"Executing command type: Set SSID\n");
zw.wang00477802025-06-12 19:48:37 +08001404 status = execute_command(cmd);
1405 if (status != 0)
1406 {
1407 return status;
1408 }
1409
1410 if (auth != GSW_WIFI_AUTH_OPEN)
1411 {
1412 const char *key_mgmt;
1413 switch (auth)
1414 {
1415 case GSW_WIFI_AUTH_WEP:
1416 key_mgmt = "NONE";
1417 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1418 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 +08001419 LOGI(GSW_WIFI,"Executing command type: Set WEP key\n");
zw.wang00477802025-06-12 19:48:37 +08001420 status = execute_command(cmd);
1421 if (status != 0)
1422 return status;
1423
1424 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV(注意原代码中可能存在拼写错误 "weep_tx_keyidx" 应为 "wep_tx_keyidx")
1425 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 +08001426 LOGI(GSW_WIFI,"Executing command type: Set WEP TX key index\n");
zw.wang00477802025-06-12 19:48:37 +08001427 status = execute_command(cmd);
1428 if (status != 0)
1429 return status;
1430 break;
1431
1432 case GSW_WIFI_AUTH_WPA_PSK:
1433 key_mgmt = "WPA-PSK";
1434 break;
1435 case GSW_WIFI_AUTH_WPA2_PSK:
1436 key_mgmt = "WPA-PSK";
1437 break;
1438 case GSW_WIFI_AUTH_WPA3_PSK:
1439 key_mgmt = "SAE";
1440 break;
1441 default:
1442 key_mgmt = "WPA-PSK";
1443 break;
1444 }
1445
1446 if (auth != GSW_WIFI_AUTH_WEP)
1447 {
1448 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1449 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 +08001450 LOGI(GSW_WIFI,"Executing command type: Set key management\n");
zw.wang00477802025-06-12 19:48:37 +08001451 status = execute_command(cmd);
1452 if (status != 0)
1453 return status;
1454
1455 if (auth == GSW_WIFI_AUTH_WPA3_PSK)
1456 {
1457 // WPA3 特有配置:确保在设置key_mgmt后配置SAE参数
1458 // 设置椭圆曲线组(使用NIST P-256,组号19)
1459 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 sae_groups 19", WLAN_STA_DEV);
1460 status = execute_command(cmd);
1461 if (status != 0)
1462 return status;
1463
1464 // 强制MFP保护(WPA3要求必须启用)
1465 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 sae_require_mfp 1", WLAN_STA_DEV);
1466 status = execute_command(cmd);
1467 if (status != 0)
1468 return status;
1469
1470 // 使用SAE+CCMP加密组合(确保与AP端匹配)
1471 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 pairwise CCMP", WLAN_STA_DEV);
1472 status = execute_command(cmd);
1473 if (status != 0)
1474 return status;
1475 }
1476
1477 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1478 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 +08001479 LOGI(GSW_WIFI,"Executing command type: Set PSK\n");
zw.wang00477802025-06-12 19:48:37 +08001480 status = execute_command(cmd);
1481 if (status != 0)
1482 return status;
1483 }
1484 }
1485 else
1486 {
1487 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1488 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 key_mgmt NONE", WLAN_STA_DEV);
zw.wangbd342b92025-07-21 11:24:16 +08001489 LOGI(GSW_WIFI,"Executing command type: Set key management for open network\n");
zw.wang00477802025-06-12 19:48:37 +08001490 status = execute_command(cmd);
1491 if (status != 0)
1492 return status;
1493 }
1494
1495 // 修改:-p 改为 -i 并使用 WLAN_STA_DEV
1496 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s select_network 0", WLAN_STA_DEV);
zw.wangbd342b92025-07-21 11:24:16 +08001497 LOGI(GSW_WIFI,"Executing command type: Select network\n");
zw.wang00477802025-06-12 19:48:37 +08001498 status = execute_command(cmd);
1499 if (status != 0)
1500 {
1501 return status;
1502 }
1503
1504 // udhcpc 命令无需修改(已使用 WLAN_STA_DEV)
1505 snprintf(cmd, sizeof(cmd), "udhcpc -i %s -n", WLAN_STA_DEV);
zw.wangbd342b92025-07-21 11:24:16 +08001506 LOGI(GSW_WIFI,"Executing command type: Request DHCP IP\n");
zw.wang00477802025-06-12 19:48:37 +08001507 status = execute_command(cmd);
1508 return status;
1509}
1510
1511int gsw_wifi_sta_forget_ap(char *ssid, gsw_wifi_auth_e auth)
1512{
1513 if (handle())
1514 return GSW_HAL_NORMAL_FAIL;
1515 if (is_wpa_supplicant_running() != 0)
1516 {
1517 return GSW_HAL_NORMAL_FAIL;
1518 }
1519 char cmd[MAX_COMMAND_LEN];
1520 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);
1521 return execute_command(cmd);
1522}
1523
1524int gsw_wifi_sta_start_scan(void)
1525{
1526 if (handle())
1527 return GSW_HAL_NORMAL_FAIL;
1528 if (is_wpa_supplicant_running() != 0)
1529 {
1530 return GSW_HAL_NORMAL_FAIL;
1531 }
1532 char cmd[MAX_COMMAND_LEN];
1533 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan", WLAN_STA_DEV);
1534 return execute_command(cmd);
1535}
1536
1537int gsw_wifi_get_interface_ip(gsw_wifi_index_e idx, char *ip)
1538{
1539 if (handle())
1540 return GSW_HAL_NORMAL_FAIL;
1541 char cmd[MAX_COMMAND_LEN];
1542 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);
1543 FILE *output = popen(cmd, "r");
1544 if (!output)
1545 return GSW_HAL_NORMAL_FAIL;
1546 char buffer[MAX_IP_LEN + 1];
1547 if (fgets(buffer, sizeof(buffer), output) == NULL)
1548 {
1549 pclose(output);
1550 return GSW_HAL_NORMAL_FAIL;
1551 }
1552 pclose(output);
1553 buffer[strcspn(buffer, "\n")] = '\0';
1554 strncpy(ip, buffer, MAX_IP_LEN);
1555 return GSW_HAL_SUCCESS;
1556}
1557
1558int gsw_wifi_get_interface_mac(gsw_wifi_index_e idx, char *mac)
1559{
1560 if (handle())
1561 return GSW_HAL_NORMAL_FAIL;
1562 char cmd[MAX_COMMAND_LEN];
1563 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);
1564 FILE *output = popen(cmd, "r");
1565 if (!output)
1566 return GSW_HAL_NORMAL_FAIL;
1567 char buffer[MAX_MAC_LEN + 1];
1568 if (fgets(buffer, sizeof(buffer), output) == NULL)
1569 {
1570 pclose(output);
1571 return GSW_HAL_NORMAL_FAIL;
1572 }
1573 pclose(output);
zw.wangbb7f28c2025-06-27 19:41:19 +08001574 buffer[strcspn(buffer, "\n")] = '\0'; // 移除换行符
1575
1576 // 关键修改:确保MAC地址仅保留17位有效字符(标准MAC格式为xx:xx:xx:xx:xx:xx,共17字符)
1577 strncpy(mac, buffer, 17); // 限制复制17个字符
1578 mac[17] = '\0'; // 显式添加字符串结束符
1579
zw.wang00477802025-06-12 19:48:37 +08001580 return GSW_HAL_SUCCESS;
1581}
1582
1583int gsw_wifi_get_connect_ap_mac(char *mac)
1584{
1585 if (handle())
1586 return GSW_HAL_NORMAL_FAIL;
1587 char cmd[MAX_COMMAND_LEN];
1588 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'bssid=' | cut -d '=' -f 2", WLAN_STA_DEV);
1589 FILE *output = popen(cmd, "r");
1590 if (!output)
1591 return GSW_HAL_NORMAL_FAIL;
1592 char buffer[MAX_MAC_LEN + 1];
1593 if (fgets(buffer, sizeof(buffer), output) == NULL)
1594 {
1595 pclose(output);
1596 return GSW_HAL_NORMAL_FAIL;
1597 }
1598 pclose(output);
1599 buffer[strcspn(buffer, "\n")] = '\0';
1600 strncpy(mac, buffer, MAX_MAC_LEN);
1601 return GSW_HAL_SUCCESS;
1602}
1603
1604int gsw_wifi_get_connect_ap_rssi(int *rssi)
1605{
1606 if (handle())
1607 return GSW_HAL_NORMAL_FAIL;
1608 char cmd[MAX_COMMAND_LEN];
1609 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'signal=' | cut -d '=' -f 2", WLAN_STA_DEV);
1610 FILE *output = popen(cmd, "r");
1611 if (!output)
1612 return GSW_HAL_NORMAL_FAIL;
1613 char buffer[16];
1614 if (fgets(buffer, sizeof(buffer), output) == NULL)
1615 {
1616 pclose(output);
1617 return GSW_HAL_NORMAL_FAIL;
1618 }
1619 pclose(output);
1620 buffer[strcspn(buffer, "\n")] = '\0';
1621 *rssi = atoi(buffer);
1622 return GSW_HAL_SUCCESS;
1623}
1624
1625int gsw_wifi_get_connect_ap_band(gsw_wifi_band_e *band)
1626{
1627 if (handle())
1628 return GSW_HAL_NORMAL_FAIL;
1629 char cmd[MAX_COMMAND_LEN];
1630 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'frequency=' | cut -d '=' -f 2", WLAN_STA_DEV);
1631 FILE *output = popen(cmd, "r");
1632 if (!output)
1633 return GSW_HAL_NORMAL_FAIL;
1634 char buffer[32];
1635 if (fgets(buffer, sizeof(buffer), output) == NULL)
1636 {
1637 pclose(output);
1638 return GSW_HAL_NORMAL_FAIL;
1639 }
1640 pclose(output);
1641 buffer[strcspn(buffer, "\n")] = '\0';
1642 int freq = atoi(buffer);
1643 if (freq < 2500)
1644 {
1645 *band = GSW_WIFI_2G_band;
1646 }
1647 else
1648 {
1649 *band = GSW_WIFI_5G_band;
1650 }
1651 return GSW_HAL_SUCCESS;
1652}
1653
1654int gsw_wifi_get_connect_ap_ip(char *ip)
1655{
1656 if (handle())
1657 return GSW_HAL_NORMAL_FAIL;
1658 char cmd[MAX_COMMAND_LEN];
1659 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'ip_address=' | cut -d '=' -f 2", WLAN_STA_DEV);
1660 FILE *output = popen(cmd, "r");
1661 if (!output)
1662 return GSW_HAL_NORMAL_FAIL;
1663 char buffer[MAX_IP_LEN + 1];
1664 if (fgets(buffer, sizeof(buffer), output) == NULL)
1665 {
1666 pclose(output);
1667 return GSW_HAL_NORMAL_FAIL;
1668 }
1669 pclose(output);
1670 buffer[strcspn(buffer, "\n")] = '\0';
1671 strncpy(ip, buffer, MAX_IP_LEN);
1672 return GSW_HAL_SUCCESS;
1673}
1674
1675int gsw_wifi_get_sta_available_ap(gsw_ap_detail_info_s *info)
1676{
1677 if (handle())
1678 return GSW_HAL_NORMAL_FAIL;
1679 if (is_wpa_supplicant_running() != 0)
1680 {
1681 return GSW_HAL_NORMAL_FAIL;
1682 }
1683 char cmd[MAX_COMMAND_LEN];
1684 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan > /dev/null", WLAN_STA_DEV);
1685 execute_command(cmd);
1686 sleep(1);
1687 memset(cmd, 0, sizeof(cmd));
1688 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);
1689 FILE *output = popen(cmd, "r");
1690 if (!output)
1691 return GSW_HAL_NORMAL_FAIL;
1692 char buffer[256];
1693 if (fgets(buffer, sizeof(buffer), output) == NULL)
1694 {
1695 pclose(output);
1696 return GSW_HAL_NORMAL_FAIL;
1697 }
1698 pclose(output);
1699 buffer[strcspn(buffer, "\n")] = '\0';
hong.liud2417072025-06-27 07:10:37 -07001700 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 +08001701 info->status = GSW_WIFI_AP_STATUS_ENABLE;
1702 return GSW_HAL_SUCCESS;
1703}
1704
1705int gsw_wifi_get_ap_device_list(gsw_ap_info_s *ap_info, gsw_device_info_s *device_list, int len, int *dev_len)
1706{
1707 if (handle())
1708 return GSW_HAL_NORMAL_FAIL;
1709 char cmd[MAX_COMMAND_LEN];
1710 snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s all_sta", WLAN_AP_DEV);
1711 FILE *output = popen(cmd, "r");
1712 if (!output)
1713 return GSW_HAL_NORMAL_FAIL;
1714 char buffer[4096];
1715 size_t length = fread(buffer, 1, sizeof(buffer) - 1, output);
1716 if (length == 0)
1717 {
1718 pclose(output);
1719 return GSW_HAL_NORMAL_FAIL;
1720 }
1721 buffer[length] = '\0';
1722 pclose(output);
1723 // 解析 hostapd_cli 输出,填充 device_list
1724 *dev_len = 0;
1725 return GSW_HAL_SUCCESS;
1726}
1727
1728int gsw_wifi_get_sta_saved_ap(gsw_saved_ap_info_s *list, int len, int *list_len)
1729{
1730 if (handle())
1731 return GSW_HAL_NORMAL_FAIL;
1732 if (is_wpa_supplicant_running() != 0)
1733 {
1734 return GSW_HAL_NORMAL_FAIL;
1735 }
1736 char cmd[MAX_COMMAND_LEN];
1737 snprintf(cmd, sizeof(cmd), "wpa_cli -i %s list_networks", WLAN_STA_DEV);
1738 FILE *output = popen(cmd, "r");
1739 if (!output)
1740 return GSW_HAL_NORMAL_FAIL;
1741 char buffer[4096];
1742 size_t length = fread(buffer, 1, sizeof(buffer) - 1, output);
1743 if (length == 0)
1744 {
1745 pclose(output);
1746 return GSW_HAL_NORMAL_FAIL;
1747 }
1748 buffer[length] = '\0';
1749 pclose(output);
1750 // 解析 wpa_cli 输出,填充 list
1751 *list_len = 0;
1752 return GSW_HAL_SUCCESS;
1753}
1754
1755int gsw_wifi_get_scan_list(gsw_scan_info_s *list, int len, int *list_len)
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", WLAN_STA_DEV);
1769 FILE *output = popen(cmd, "r");
1770 if (!output)
1771 return GSW_HAL_NORMAL_FAIL;
1772 char buffer[4096];
1773 size_t length = fread(buffer, 1, sizeof(buffer) - 1, output);
1774 if (length == 0)
1775 {
1776 pclose(output);
1777 return GSW_HAL_NORMAL_FAIL;
1778 }
1779 buffer[length] = '\0';
1780 pclose(output);
1781
1782 // 解析 wpa_cli 输出,填充 list
1783 *list_len = 0;
1784 char *line = strtok(buffer, "\n");
1785 // 跳过标题行
1786 if (line)
1787 line = strtok(NULL, "\n");
1788
1789 while (line && *list_len < len)
1790 {
1791 gsw_scan_info_s *info = &list[*list_len];
1792 int freq; // 临时变量用于存储频率,以判断频段
1793 char flags[256]; // 临时变量用于存储认证标志
1794
1795 // 假设输出格式为:bssid freq signal flags ssid
1796 // 解析每一行数据
1797 sscanf(line, "%31s %d %d %255s %32[^\n]", info->mac, &freq, &info->rssi, flags, info->ssid);
1798
1799 // 根据频率判断频段
1800 if (freq < 2500)
1801 {
1802 info->band = GSW_WIFI_2G_band;
1803 }
1804 else
1805 {
1806 info->band = GSW_WIFI_5G_band;
1807 }
1808
1809 // 根据 flags 判断认证方式
1810 if (strstr(flags, "SAE") != NULL || strstr(flags, "WPA3") != NULL)
1811 {
1812 info->auth = GSW_WIFI_AUTH_WPA3_PSK;
1813 }
1814 else if (strstr(flags, "WPA2") != NULL)
1815 {
1816 info->auth = GSW_WIFI_AUTH_WPA2_PSK;
1817 }
1818 else if (strstr(flags, "WPA") != NULL)
1819 {
1820 info->auth = GSW_WIFI_AUTH_WPA_PSK;
1821 }
1822 else
1823 {
1824 info->auth = GSW_WIFI_AUTH_OPEN;
1825 }
1826
1827 (*list_len)++;
1828 line = strtok(NULL, "\n");
1829 }
1830 return GSW_HAL_SUCCESS;
1831}
1832
1833void *ap_event_listener(void *arg)
1834{
1835 char list_cmd[MAX_COMMAND_LEN];
1836 snprintf(list_cmd, sizeof(list_cmd), "hostapd_cli -i %s all_sta | grep -E '^[0-9a-f:]{17}$|connected_time=' | \
1837 awk '/^[0-9a-f:]{17}$/ { mac=$0; getline; if ($0 ~ /connected_time=/) print mac }'",
1838 WLAN_AP_DEV);
1839
1840 // 保存上一次的设备MAC地址列表
1841 char prev_macs[GSW_WIFI_AP_DEVICE_NUM][32] = {0};
1842 int prev_count = 0;
zw.wangbb7f28c2025-06-27 19:41:19 +08001843 while (ap_event_context.running && ap_event_context.gsw_cb)
zw.wang00477802025-06-12 19:48:37 +08001844 {
1845 if (is_hostapd_running() != 0)
1846 {
1847 sleep(5);
1848 continue;
1849 }
1850
1851 // 使用list_cmd获取当前设备MAC列表
1852 char curr_macs[GSW_WIFI_AP_DEVICE_NUM][32] = {0};
1853 int curr_count = 0;
1854 FILE *list_output = popen(list_cmd, "r");
1855 if (list_output)
1856 {
1857 char buffer[1024];
1858 if (fread(buffer, 1, sizeof(buffer) - 1, list_output) > 0)
1859 {
1860 buffer[sizeof(buffer) - 1] = '\0';
1861 char *line = strtok(buffer, "\n");
1862 while (line && curr_count < GSW_WIFI_AP_DEVICE_NUM)
1863 {
1864 if (strchr(line, ':'))
1865 {
1866 strncpy(curr_macs[curr_count], line, 17); // 复制MAC地址
1867 curr_count++;
1868 }
1869 line = strtok(NULL, "\n");
1870 }
1871 }
1872 pclose(list_output);
1873 }
1874
1875 // 数量变化时直接触发事件(保持原有逻辑)
1876 if (curr_count > prev_count && ap_event_context.gsw_cb)
1877 {
1878 ap_event_context.gsw_cb(ap_event_context.arg, GSW_WIFI_STATUS_CONNECT);
1879 }
1880 else if (curr_count < prev_count && ap_event_context.gsw_cb)
1881 {
1882 ap_event_context.gsw_cb(ap_event_context.arg, GSW_WIFI_STATUS_DISCONNECT);
1883 }
1884 else if (curr_count == prev_count && ap_event_context.gsw_cb)
1885 {
1886 int mac_updated = 0;
1887 // 遍历检查是否有MAC地址变化
1888 for (int i = 0; i < curr_count; i++)
1889 {
1890 int found = 0;
1891 for (int j = 0; j < prev_count; j++)
1892 {
1893 if (strcasecmp(curr_macs[i], prev_macs[j]) == 0)
1894 {
1895 found = 1;
1896 break;
1897 }
1898 }
1899 // 发现新MAC地址(当前存在但之前不存在)
1900 if (!found)
1901 {
zw.wangbd342b92025-07-21 11:24:16 +08001902 LOGI(GSW_WIFI,"New device connected: %s\n", curr_macs[i]);
zw.wang00477802025-06-12 19:48:37 +08001903 mac_updated++;
1904 break;
1905 }
1906 }
1907 // 若有更新则触发连接事件(可根据需求扩展为断开事件)
1908 if (mac_updated)
1909 {
1910 ap_event_context.gsw_cb(ap_event_context.arg, GSW_WIFI_STATUS_DISCONNECT);
1911 ap_event_context.gsw_cb(ap_event_context.arg, GSW_WIFI_STATUS_CONNECT);
1912 }
1913 }
1914
1915 memcpy(prev_macs, curr_macs, sizeof(prev_macs));
1916 prev_count = curr_count;
1917
1918 sleep(2); // 定期轮询
1919 }
1920 return NULL;
1921}
1922
1923/**
1924 * @brief register ap event notification function
1925 * @param [in] arg as gsw_cb function parameter, can be NULL
1926 * @param [in] gsw_cb callback
1927 * @retval 0: success
1928 * @retval other: fail
1929 */
1930int gsw_wifi_reg_ap_event_callback(void *arg, GSW_AP_CALLBACK_FUNC_PTR gsw_cb)
1931{
1932 if (handle())
1933 return GSW_HAL_NORMAL_FAIL;
zw.wangbd342b92025-07-21 11:24:16 +08001934 LOGI(GSW_WIFI,"%s - start.\n",__func__);
zw.wang00477802025-06-12 19:48:37 +08001935 if (ap_event_context.running)
1936 {
1937 return GSW_HAL_NORMAL_FAIL; // 已经注册了回调
1938 }
zw.wangbb7f28c2025-06-27 19:41:19 +08001939 if (arg == NULL)
1940 {
zw.wangbd342b92025-07-21 11:24:16 +08001941 LOGE(GSW_WIFI,"%s - arg is NULL.\n",__func__);
zw.wangbb7f28c2025-06-27 19:41:19 +08001942 }
1943 if (gsw_cb == NULL)
1944 {
zw.wangbd342b92025-07-21 11:24:16 +08001945 LOGE(GSW_WIFI,"%s - gsw_cb is NULL.\n",__func__);
zw.wangbb7f28c2025-06-27 19:41:19 +08001946 return GSW_HAL_NORMAL_FAIL; // 回调函数不能为空
1947 }
zw.wang00477802025-06-12 19:48:37 +08001948 ap_event_context.arg = arg;
1949 ap_event_context.gsw_cb = gsw_cb;
1950 ap_event_context.running = 1;
1951 if (pthread_create(&ap_event_context.thread_id, NULL, ap_event_listener, NULL) != 0)
1952 {
1953 ap_event_context.running = 0;
1954 return GSW_HAL_NORMAL_FAIL;
1955 }
zw.wangbd342b92025-07-21 11:24:16 +08001956 LOGI(GSW_WIFI,"%s - end.\n",__func__);
zw.wang00477802025-06-12 19:48:37 +08001957 return GSW_HAL_SUCCESS;
1958}
1959
1960/**
1961 * @brief unregister ap callback function
1962 * @param [in] arg reserved parameter, can be NULL
1963 * @retval 0: success
1964 * @retval other: fail
1965 */
1966int gsw_wifi_unreg_ap_event_callback(void *arg)
1967{
1968 if (handle())
1969 return GSW_HAL_NORMAL_FAIL;
zw.wangbd342b92025-07-21 11:24:16 +08001970 LOGI(GSW_WIFI,"%s - start.\n",__func__);
zw.wang00477802025-06-12 19:48:37 +08001971 ap_event_context.running = 0;
1972
1973 // 检查线程是否存在
1974 if (ap_event_context.thread_id != 0)
1975 {
1976 if (pthread_join(ap_event_context.thread_id, NULL) != 0)
1977 {
1978 return GSW_HAL_NORMAL_FAIL;
1979 }
1980 ap_event_context.thread_id = 0; // 重置线程ID
1981 }
1982
1983 ap_event_context.arg = NULL;
1984 ap_event_context.gsw_cb = NULL;
zw.wangbd342b92025-07-21 11:24:16 +08001985 LOGI(GSW_WIFI,"%s - end.\n",__func__);
zw.wang00477802025-06-12 19:48:37 +08001986 return GSW_HAL_SUCCESS;
1987}
1988
1989int gsw_wifi_reg_sta_event_callback(void *arg, GSW_STA_CALLBACK_FUNC_PTR gsw_cb)
1990{
1991 if (handle())
1992 return GSW_HAL_NORMAL_FAIL;
1993 return GSW_HAL_SUCCESS;
1994}
1995
1996int gsw_wifi_unreg_sta_event_callback(void *arg)
1997{
1998 if (handle())
1999 return GSW_HAL_NORMAL_FAIL;
2000 return GSW_HAL_SUCCESS;
2001}
2002
2003int gsw_wifi_sdk_interface_init()
2004{
2005 if (handle())
2006 return GSW_HAL_NORMAL_FAIL;
2007 return GSW_HAL_SUCCESS;
2008}
2009
2010int is_module_loaded(const char *module_name)
2011{
2012 if (handle())
2013 return GSW_HAL_NORMAL_FAIL;
2014 char cmd[MAX_COMMAND_LEN];
2015 snprintf(cmd, sizeof(cmd), "lsmod | grep %s", module_name);
2016 FILE *output = popen(cmd, "r");
2017 if (!output)
2018 return GSW_HAL_NORMAL_FAIL;
2019 char buffer[256];
2020 if (fgets(buffer, sizeof(buffer), output) == NULL)
2021 {
2022 pclose(output);
2023 return GSW_HAL_SUCCESS;
2024 }
2025 pclose(output);
2026 return 1;
2027}
2028/**
2029 * @brief load wifi driver
2030 * @retval 0: success
2031 * @retval other: fail
2032 */
2033int gsw_wifi_enable(void)
2034{
2035 if (handle())
2036 return GSW_HAL_NORMAL_FAIL;
zw.wangbd342b92025-07-21 11:24:16 +08002037 LOGI(GSW_WIFI,"%s - start.\n",__func__);
zw.wang89c18242025-06-24 19:07:10 +08002038 if (execute_command("echo 1 > /sys/devices/platform/mbtk-sdh/pwr_ctrl") != 0)
2039 {
2040 return GSW_HAL_NORMAL_FAIL;
2041 }
2042 sleep(1);
zw.wang00477802025-06-12 19:48:37 +08002043 if (!is_module_loaded("cfg80211"))
2044 {
2045 if (execute_command("modprobe cfg80211") != 0)
2046 {
2047 return GSW_HAL_NORMAL_FAIL;
2048 }
2049 }
2050 else
2051 {
zw.wangbd342b92025-07-21 11:24:16 +08002052 LOGI(GSW_WIFI,"cfg80211 has insmod.\n");
zw.wang00477802025-06-12 19:48:37 +08002053 }
2054
2055 if (!is_module_loaded("aic8800_bsp"))
2056 {
2057 char cmd[MAX_COMMAND_LEN];
2058 snprintf(cmd, sizeof(cmd), "modprobe %s", AIC8800_BSP_DRIVER_PATH);
2059 if (execute_command(cmd) != 0)
2060 {
2061 return GSW_HAL_NORMAL_FAIL;
2062 }
2063 }
2064 else
2065 {
zw.wangbd342b92025-07-21 11:24:16 +08002066 LOGI(GSW_WIFI,"aic8800_bsp has insmod.\n");
zw.wang00477802025-06-12 19:48:37 +08002067 }
2068
2069 if (!is_module_loaded("aic8800_fdrv"))
2070 {
2071 char cmd[MAX_COMMAND_LEN];
2072 snprintf(cmd, sizeof(cmd), "modprobe %s", AIC8800_FDRV_DRIVER_PATH);
2073 if (execute_command(cmd) != 0)
2074 {
2075 return GSW_HAL_NORMAL_FAIL;
2076 }
2077 }
2078 else
2079 {
zw.wangbd342b92025-07-21 11:24:16 +08002080 LOGI(GSW_WIFI,"aic8800_fdrv has insmod.\n");
zw.wang00477802025-06-12 19:48:37 +08002081 }
zw.wangbd342b92025-07-21 11:24:16 +08002082 LOGI(GSW_WIFI,"%s - end.\n",__func__);
zw.wang00477802025-06-12 19:48:37 +08002083 return GSW_HAL_SUCCESS;
2084}
2085
2086/**
2087 * @brief uninstall wifi driver
2088 * @retval 0: success
2089 * @retval other: fail
2090 */
2091int gsw_wifi_disable(void)
2092{
2093 if (handle())
2094 return GSW_HAL_NORMAL_FAIL;
zw.wangbd342b92025-07-21 11:24:16 +08002095 LOGI(GSW_WIFI,"%s - start.\n",__func__);
zw.wang00477802025-06-12 19:48:37 +08002096 if (is_module_loaded("aic8800_fdrv"))
2097 {
2098 if (execute_command("rmmod aic8800_fdrv") != 0)
2099 {
2100 return GSW_HAL_NORMAL_FAIL;
2101 }
2102 }
2103 else
2104 {
zw.wangbd342b92025-07-21 11:24:16 +08002105 LOGI(GSW_WIFI,"aic8800_fdrv not insmod.\n");
zw.wang00477802025-06-12 19:48:37 +08002106 }
2107
2108 if (is_module_loaded("aic8800_bsp"))
2109 {
2110 if (execute_command("rmmod aic8800_bsp") != 0)
2111 {
2112 return GSW_HAL_NORMAL_FAIL;
2113 }
2114 }
2115 else
2116 {
zw.wangbd342b92025-07-21 11:24:16 +08002117 LOGI(GSW_WIFI,"aic8800_bsp not insmod.\n");
zw.wang00477802025-06-12 19:48:37 +08002118 }
2119
2120 if (is_module_loaded("cfg80211"))
2121 {
2122 if (execute_command("rmmod cfg80211") != 0)
2123 {
2124 return GSW_HAL_NORMAL_FAIL;
2125 }
2126 }
2127 else
2128 {
zw.wangbd342b92025-07-21 11:24:16 +08002129 LOGI(GSW_WIFI,"cfg80211 not insmod.\n");
zw.wang00477802025-06-12 19:48:37 +08002130 }
zw.wang89c18242025-06-24 19:07:10 +08002131 if (execute_command("echo 0 > /sys/devices/platform/mbtk-sdh/pwr_ctrl") != 0)
2132 {
2133 return GSW_HAL_NORMAL_FAIL;
2134 }
2135 sleep(1);
zw.wangbd342b92025-07-21 11:24:16 +08002136 LOGI(GSW_WIFI,"%s - end.\n",__func__);
zw.wang00477802025-06-12 19:48:37 +08002137 return GSW_HAL_SUCCESS;
hong.liud2417072025-06-27 07:10:37 -07002138}
2139
2140int gsw_wifi_sta_disconnect(char *ssid)
2141{
2142 return GSW_HAL_SUCCESS;
zw.wang00477802025-06-12 19:48:37 +08002143}