| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <unistd.h> |
| #include <sys/wait.h> |
| #include <pthread.h> |
| |
| #include "gsw_wifi_interface_sdk.h" |
| #include <dlfcn.h> |
| #define MAX_COMMAND_LEN 512 |
| #define MAX_SSID_LEN 32 |
| #define MIN_SSID_LEN 6 |
| #define MIN_PASSWORD_LEN 8 |
| #define MAX_PASSWORD_LEN 64 |
| #define MAX_IP_LEN 32 |
| #define MAX_MAC_LEN 32 |
| |
| #define WLAN_STA_DEV "wlan0-vxd" |
| #define WLAN_AP_DEV "wlan0" |
| |
| #define HOSTAPD_CONF_PATH "/etc/wifi/hostapd.conf" |
| #define HOSTAPD_LOG_PATH "/etc/wifi/hostapd.log" |
| #define WPA_SUPPLICANT_CONF_PATH "/etc/wifi/wpa_supplicant.conf" |
| #define AIC8800_BSP_DRIVER_PATH "aic8800_bsp" |
| #define AIC8800_FDRV_DRIVER_PATH "aic8800_fdrv" |
| #define CTRL_INTERFACE "/var/run/hostapd" |
| |
| #define WPA_SUPPLICANT_CTRL_PATH "/var/run/wpa_supplicant" |
| |
| #define VALID_2G_CHANNELS {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13} |
| #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} |
| |
| #include "gsw_log_interface.h" |
| #define GSW_WIFI "[HAL][GSW_WIFI]" |
| |
| static inline int handle() |
| { |
| return GSW_HAL_SUCCESS; |
| } |
| |
| // 辅助函数:执行系统命令 |
| int execute_command(const char *cmd) |
| { |
| int status = system(cmd); |
| if (status == -1) |
| { |
| LOGE(GSW_WIFI,"Failed to execute command"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| if (WIFEXITED(status)) |
| { |
| return WEXITSTATUS(status); |
| } |
| else |
| { |
| // 子进程异常退出,比如被信号终止 |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| } |
| |
| // 辅助函数:读取文件内容到缓冲区 |
| int read_file(const char *path, char *buffer, size_t buffer_len) |
| { |
| FILE *file = fopen(path, "r"); |
| if (!file) |
| return GSW_HAL_NORMAL_FAIL; |
| size_t length = fread(buffer, 1, buffer_len - 1, file); |
| if (length == 0) |
| { |
| fclose(file); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| buffer[length] = '\0'; |
| fclose(file); |
| return (int)length; |
| } |
| |
| // 新增:静态互斥锁用于保护文件写入操作 |
| static pthread_mutex_t write_file_mutex = PTHREAD_MUTEX_INITIALIZER; |
| |
| int write_file(const char *path, const char *buffer) |
| { |
| // 加锁:确保同一时间只有一个线程执行文件写入 |
| pthread_mutex_lock(&write_file_mutex); |
| |
| FILE *file = fopen(path, "w"); |
| if (!file) |
| { |
| // 解锁后返回错误 |
| pthread_mutex_unlock(&write_file_mutex); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| |
| fputs(buffer, file); |
| fclose(file); |
| |
| // 解锁:释放锁资源 |
| pthread_mutex_unlock(&write_file_mutex); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| // 辅助函数:检查 hostapd 是否正在运行 |
| int is_hostapd_running() |
| { |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "ps | grep 'hostapd' | grep -v -E 'grep|global'"); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char buffer[1]; // 只需检查是否有输出,读取一个字符即可 |
| int result = fread(buffer, 1, 1, output); |
| pclose(output); |
| if (result > 0) |
| { |
| return GSW_HAL_SUCCESS; // 找到指定配置的hostapd进程 |
| } |
| return GSW_HAL_NORMAL_FAIL; // 未找到 |
| } |
| |
| int gsw_wifi_ap_start() |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| LOGI(GSW_WIFI,"gsw_wifi_ap_start\n"); |
| // 强制终止所有hostapd进程(关键新增:避免残留进程占用资源) |
| execute_command("killall -9 hostapd >/dev/null 2>&1"); // 9信号强制终止 |
| sleep(1); // 等待进程完全退出 |
| |
| // 清理网络桥接接口(关键新增:避免br-lan绑定冲突) |
| char brctl_cmd[MAX_COMMAND_LEN]; |
| snprintf(brctl_cmd, sizeof(brctl_cmd), "brctl delif br-lan %s >/dev/null 2>&1", WLAN_AP_DEV); |
| execute_command(brctl_cmd); |
| |
| // 检查并删除残留的控制接口文件(原有逻辑保留) |
| char ctrl_path[MAX_COMMAND_LEN]; |
| snprintf(ctrl_path, sizeof(ctrl_path), "%s/%s", CTRL_INTERFACE, WLAN_AP_DEV); |
| if (access(ctrl_path, F_OK) == 0) |
| { |
| if (remove(ctrl_path) != 0) |
| { |
| LOGE(GSW_WIFI,"Failed to remove old hostapd control interface file"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| } |
| |
| // 启动hostapd(原有逻辑保留) |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "echo ap starting > %s ;hostapd -q %s >> %s &", HOSTAPD_LOG_PATH, HOSTAPD_CONF_PATH, HOSTAPD_LOG_PATH); |
| int status = execute_command(cmd); |
| sleep(8); |
| |
| char log_buffer[1024] = {0}; |
| if (read_file(HOSTAPD_LOG_PATH, log_buffer, sizeof(log_buffer)) != -1) |
| { |
| // 检查日志中是否包含关键错误信息(示例:"Failed"、"error") |
| if (strstr(log_buffer, "failed") || strstr(log_buffer, "error") || strstr(log_buffer, "wasn't started")) |
| { |
| LOGE(GSW_WIFI,"Hostapd start failed, check log: %s", log_buffer); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| if(is_hostapd_running() == GSW_HAL_SUCCESS) |
| { |
| LOGI(GSW_WIFI,"Hostapd start success"); |
| } |
| else |
| { |
| LOGE(GSW_WIFI,"Hostapd start failed, check log: %s", log_buffer); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| } |
| else |
| { |
| if (is_hostapd_running() != GSW_HAL_SUCCESS) |
| { |
| LOGE(GSW_WIFI,"Failed to read hostapd log file: %s", HOSTAPD_LOG_PATH); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| } |
| LOGI(GSW_WIFI,"gsw_wifi_ap_start ret = %d\n", status); |
| return status; |
| } |
| |
| // 辅助函数:判断 wpa_supplicant 服务是否起来 |
| int is_wpa_supplicant_running() |
| { |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "ps | grep wpa_supplicant | grep -v grep"); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char buffer[1]; // 只需检查是否有输出,读取一个字符即可 |
| int result = fread(buffer, 1, 1, output); |
| pclose(output); |
| if (result > 0) |
| { |
| return GSW_HAL_SUCCESS; // wpa_supplicant 服务存在 |
| } |
| return GSW_HAL_NORMAL_FAIL; // wpa_supplicant 服务不存在 |
| } |
| |
| typedef struct |
| { |
| void *arg; |
| GSW_AP_CALLBACK_FUNC_PTR gsw_cb; |
| pthread_t thread_id; |
| int running; |
| } AP_Event_Context; |
| |
| static AP_Event_Context ap_event_context; |
| int gsw_wifi_ap_stop() |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| if (is_hostapd_running() != 0) |
| { |
| LOGI(GSW_WIFI,"Hostapd is not running, no need to stop.\n"); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| LOGI(GSW_WIFI,"Hostapd is running, stop it.\n"); |
| // 先停止事件监听线程 |
| /*if (ap_event_context.running) |
| { |
| ap_event_context.running = 0; |
| if (ap_event_context.thread_id != 0) |
| { |
| pthread_join(ap_event_context.thread_id, NULL); |
| ap_event_context.thread_id = 0; |
| } |
| }*/ |
| |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "killall hostapd"); |
| int status = execute_command(cmd); |
| |
| // 关闭对应的 hostapd 后,删除对应的文件 |
| char path[MAX_COMMAND_LEN]; |
| snprintf(path, sizeof(path), "%s/%s", CTRL_INTERFACE, WLAN_AP_DEV); |
| if (access(path, F_OK) == 0) |
| { |
| if (remove(path) != 0) |
| { |
| LOGE(GSW_WIFI,"Failed to remove hostapd control interface file"); |
| } |
| } |
| LOGI(GSW_WIFI,"gsw_wifi_ap_stop ret = %d\n", status); |
| return status; |
| } |
| |
| int gsw_wifi_ap_restart(void) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| int ret = 0; |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "killall hostapd"); |
| int status = execute_command(cmd); |
| LOGI(GSW_WIFI,"killall hostapd status = %d\n", status); |
| // 关闭对应的 hostapd 后,删除对应的文件 |
| char path[MAX_COMMAND_LEN]; |
| snprintf(path, sizeof(path), "%s/%s", CTRL_INTERFACE, WLAN_AP_DEV); |
| if (access(path, F_OK) == 0) |
| { |
| if (remove(path) != 0) |
| { |
| LOGE(GSW_WIFI,"Failed to remove hostapd control interface file"); |
| } |
| } |
| ret = gsw_wifi_ap_start(); |
| LOGI(GSW_WIFI,"gsw_wifi_ap_restart ret = %d\n", ret); |
| return ret; |
| } |
| |
| int gsw_wifi_ap_ssid_set(char *ssid) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[4096] = {0}; |
| char new_config[4096] = {0}; |
| if (ssid == NULL) { |
| LOGE(GSW_WIFI,"Password cannot be NULL"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| size_t ssid_len = strlen(ssid); |
| if (ssid_len > MAX_SSID_LEN || ssid_len < MIN_SSID_LEN) { |
| LOGE(GSW_WIFI,"SSID length must be 6-32 characters, current length: %zu", ssid_len); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| LOGI(GSW_WIFI,"AP set ssid: %s\n", ssid); |
| // 读取现有配置 |
| if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1) |
| { |
| // 如果文件不存在,创建新配置(修改默认配置内容) |
| FILE *conf = fopen(HOSTAPD_CONF_PATH, "w"); |
| if (!conf) |
| return GSW_HAL_NORMAL_FAIL; |
| |
| // 写入用户指定的完整默认配置 |
| fprintf(conf, "ctrl_interface=%s\n", CTRL_INTERFACE); |
| fprintf(conf, "ctrl_interface_group=0\n"); |
| fprintf(conf, "interface=%s\n", WLAN_AP_DEV); |
| fprintf(conf, "driver=nl80211\n"); |
| fprintf(conf, "bridge=br-lan\n"); |
| fprintf(conf, "ssid=%s\n", ssid); // 使用传入的ssid |
| fprintf(conf, "hw_mode=g\n"); |
| fprintf(conf, "ieee80211d=1\n"); |
| fprintf(conf, "channel=0\n"); |
| fprintf(conf, "auth_algs=1\n"); |
| fprintf(conf, "wme_enabled=1\n"); |
| fprintf(conf, "noscan=0\n"); |
| fprintf(conf, "beacon_int=100\n"); |
| fprintf(conf, "wpa=3\n"); |
| fprintf(conf, "wpa_passphrase=12345678\n"); |
| fprintf(conf, "ieee80211n=1\n"); |
| fprintf(conf, "ieee80211ac=1\n"); |
| fprintf(conf, "ieee80211ax=1\n"); |
| fprintf(conf, "vht_oper_chwidth=0\n"); |
| fprintf(conf, "vht_oper_centr_freq_seg0_idx=0\n"); |
| fprintf(conf, "he_oper_chwidth=0\n"); |
| fprintf(conf, "he_oper_centr_freq_seg0_idx=0\n"); |
| fprintf(conf, "he_basic_mcs_nss_set=65534\n"); |
| fprintf(conf, "he_su_beamformee=0\n"); |
| fprintf(conf, "he_twt_required=0\n"); |
| fprintf(conf, "vht_capab=[SHORT-GI-80][VHT40+][VHT40-][MAX-A-MPDU-LEN-EXP7][RX-STBC-1][RX-LDPC]\n"); |
| fprintf(conf, "ht_capab=[SHORT-GI-20][SHORT-GI-40][HT40+][HT40-][LDPC][RX-STBC1]\n"); |
| fprintf(conf, "wpa_key_mgmt=WPA-PSK\n"); |
| fprintf(conf, "wpa_pairwise=TKIP CCMP\n"); |
| fprintf(conf, "rsn_pairwise=CCMP\n"); |
| fprintf(conf, "ignore_broadcast_ssid=0\n"); |
| fprintf(conf, "country_code=CN\n"); |
| fprintf(conf, "max_num_sta=32\n"); |
| fprintf(conf, "macaddr_acl=0\n"); |
| fprintf(conf, "deny_mac_file=/etc/wifi/hostapd.deny\n"); |
| fprintf(conf, "accept_mac_file=/etc/wifi/hostapd.accept\n"); |
| fprintf(conf, "sae_pwe=2\n"); |
| |
| fclose(conf); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| // 处理每一行(新增ctrl_interface和ctrl_interface_group的检查) |
| char *line = strtok(buffer, "\n"); |
| int ssid_processed = 0; // 标记是否已处理过ssid行 |
| while (line) |
| { |
| if (!ssid_processed && strncmp(line, "ssid=", 5) == 0) |
| { |
| char ssid_line[64]; |
| snprintf(ssid_line, sizeof(ssid_line), "ssid=%s", ssid); |
| strcat(new_config, ssid_line); |
| strcat(new_config, "\n"); |
| ssid_processed = 1; |
| line = strtok(NULL, "\n"); |
| continue; |
| } |
| else if (strncmp(line, "ssid=", 5) == 0) |
| { |
| line = strtok(NULL, "\n"); |
| continue; |
| } |
| else if (strncmp(line, "ctrl_interface=", 15) == 0) |
| { |
| char ctrl_line[128]; |
| snprintf(ctrl_line, sizeof(ctrl_line), "ctrl_interface=%s", CTRL_INTERFACE); |
| strcat(new_config, ctrl_line); |
| strcat(new_config, "\n"); |
| } |
| else if (strncmp(line, "ctrl_interface_group=", 21) == 0) |
| { |
| char ctrl_group_line[64]; |
| snprintf(ctrl_group_line, sizeof(ctrl_group_line), "ctrl_interface_group=0"); |
| strcat(new_config, ctrl_group_line); |
| strcat(new_config, "\n"); |
| } |
| else |
| { |
| strcat(new_config, line); |
| strcat(new_config, "\n"); |
| } |
| line = strtok(NULL, "\n"); |
| } |
| |
| if (!ssid_processed) |
| { |
| char ssid_line[64]; |
| snprintf(ssid_line, sizeof(ssid_line), "ssid=%s\n", ssid); |
| strcat(new_config, ssid_line); |
| } |
| |
| return write_file(HOSTAPD_CONF_PATH, new_config); |
| } |
| |
| int gsw_wifi_ap_ssid_get(char *ssid) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| if (ssid == NULL) { |
| LOGE(GSW_WIFI,"ssid cannot be NULL"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char buffer[1024] = {0}; |
| if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char *ssid_line = strstr(buffer, "ssid="); |
| if (ssid_line) |
| { |
| ssid_line += 5; // 跳过 "ssid=" |
| char *end = strchr(ssid_line, '\n'); |
| if (end) |
| *end = '\0'; |
| strncpy(ssid, ssid_line, MAX_SSID_LEN); |
| } |
| LOGI(GSW_WIFI,"AP get ssid: %s\n", ssid); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_ap_frequency_set(int gsw_wifi_frequency) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[4096] = {0}; |
| char new_config[4096] = {0}; |
| LOGI(GSW_WIFI,"AP set frequency: %d\n", gsw_wifi_frequency); |
| gsw_wifi_bandwidth_type_e gsw_wifi_bandwidth; |
| gsw_wifi_ap_bandwidth_get(&gsw_wifi_bandwidth); |
| if (gsw_wifi_bandwidth == GSW_WIFI_BANDWIDTH_HT80 && gsw_wifi_frequency == 1) |
| { |
| LOGE(GSW_WIFI,"HT80 cannot be set to 2.4G"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| // 读取现有配置 |
| if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| |
| // 处理每一行 |
| char *line = strtok(buffer, "\n"); |
| while (line) |
| { |
| // 如果是hw_mode行,替换为新值 |
| if (strstr(line, "hw_mode=") != NULL) |
| { |
| char mode_line[32]; |
| snprintf(mode_line, sizeof(mode_line), "hw_mode=%c", gsw_wifi_frequency == 1 ? 'g' : 'a'); |
| strcat(new_config, mode_line); |
| } |
| else |
| { |
| strcat(new_config, line); |
| } |
| strcat(new_config, "\n"); |
| line = strtok(NULL, "\n"); |
| } |
| |
| // 如果没有找到hw_mode行,添加新的设置 |
| if (strstr(new_config, "hw_mode=") == NULL) |
| { |
| char mode_line[32]; |
| snprintf(mode_line, sizeof(mode_line), "hw_mode=%c\n", gsw_wifi_frequency == 1 ? 'g' : 'a'); |
| strcat(new_config, mode_line); |
| } |
| |
| // 写回文件 |
| return write_file(HOSTAPD_CONF_PATH, new_config); |
| } |
| |
| int gsw_wifi_ap_frequency_get(int *gsw_wifi_frequency) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| if (gsw_wifi_frequency == NULL) { |
| LOGE(GSW_WIFI,"gsw_wifi_frequency cannot be NULL"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char buffer[1024] = {0}; |
| if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char *mode_line = strstr(buffer, "hw_mode="); |
| if (mode_line) |
| { |
| mode_line += 8; // 跳过 "hw_mode=" |
| *gsw_wifi_frequency = (mode_line[0] == 'g') ? 1 : 2; |
| } |
| LOGI(GSW_WIFI,"AP get frequency: %d\n", *gsw_wifi_frequency); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_ap_bandwidth_set(gsw_wifi_bandwidth_type_e bandwidth) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[4096] = {0}; |
| char new_config[4096] = {0}; |
| LOGI(GSW_WIFI,"AP set bandwidth: %d\n", bandwidth); |
| int current_freq; |
| if (gsw_wifi_ap_frequency_get(¤t_freq) != 0) |
| { |
| LOGI(GSW_WIFI,"Failed to get current frequency\n"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| if (current_freq == 1 && bandwidth == GSW_WIFI_BANDWIDTH_HT80) // 1表示2.4GHz |
| { |
| LOGI(GSW_WIFI,"2.4GHz band does not support 80MHz bandwidth"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| // 读取现有配置 |
| if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1) |
| return GSW_HAL_NORMAL_FAIL; |
| |
| // 定义各带宽类型对应的替换规则(新增三个参数) |
| const char *ht_capab = NULL; |
| const char *vht_capab = NULL; |
| const char *vht_oper_chwidth = NULL; |
| const char *he_oper_chwidth = NULL; |
| int vht_oper_centr_freq_seg0_idx_val = 0; |
| int he_oper_centr_freq_seg0_idx_val = 0; |
| int he_su_beamformee_val = 0; |
| |
| // 根据带宽类型初始化替换值(补充新增参数逻辑) |
| switch (bandwidth) |
| { |
| case GSW_WIFI_BANDWIDTH_HT20: |
| ht_capab = "ht_capab=[HT20][SHORT-GI-20][LDPC][RX-STBC1]"; |
| vht_capab = "vht_capab=[VHT20][SHORT-GI-20][MAX-A-MPDU-LEN-EXP7]"; |
| vht_oper_chwidth = "vht_oper_chwidth=0"; |
| he_oper_chwidth = "he_oper_chwidth=0"; |
| vht_oper_centr_freq_seg0_idx_val = 0; |
| he_oper_centr_freq_seg0_idx_val = 0; |
| he_su_beamformee_val = 0; |
| break; |
| case GSW_WIFI_BANDWIDTH_HT40: |
| ht_capab = "ht_capab=[SHORT-GI-20][SHORT-GI-40][HT40+][HT40-][LDPC][RX-STBC1]"; |
| vht_capab = "vht_capab=[SHORT-GI-80][VHT40+][VHT40-][MAX-A-MPDU-LEN-EXP7][RX-STBC-1][RX-LDPC]"; |
| vht_oper_chwidth = "vht_oper_chwidth=0"; |
| he_oper_chwidth = "he_oper_chwidth=0"; |
| vht_oper_centr_freq_seg0_idx_val = 0; |
| he_oper_centr_freq_seg0_idx_val = 0; |
| he_su_beamformee_val = 0; |
| break; |
| case GSW_WIFI_BANDWIDTH_HT80: |
| ht_capab = "ht_capab=[HT20][HT40+][HT40-][SHORT-GI-40][LDPC][RX-STBC1]"; |
| vht_capab = "vht_capab=[VHT40][VHT80][SHORT-GI-80][MAX-A-MPDU-LEN-EXP7]"; |
| vht_oper_chwidth = "vht_oper_chwidth=1"; |
| he_oper_chwidth = "he_oper_chwidth=1"; |
| vht_oper_centr_freq_seg0_idx_val = 42; |
| he_oper_centr_freq_seg0_idx_val = 42; |
| he_su_beamformee_val = 1; |
| break; |
| default: |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| |
| // 新增标记:确保只处理第一个ht_capab行 |
| int ht_capab_processed = 0; |
| |
| // 逐行处理配置文件(增加新增参数的匹配) |
| char *line = strtok(buffer, "\n"); |
| while (line) |
| { |
| // 处理ht_capab行(仅替换第一个以"ht_capab="开头的行) |
| if (!ht_capab_processed && strncmp(line, "ht_capab=", 9) == 0) |
| { |
| if (ht_capab) |
| { |
| strcat(new_config, ht_capab); |
| ht_capab_processed = 1; // 标记已处理 |
| } |
| else |
| { |
| strcat(new_config, line); |
| } |
| strcat(new_config, "\n"); |
| line = strtok(NULL, "\n"); |
| continue; |
| } |
| else if (strncmp(line, "ht_capab=", 9) == 0) |
| { |
| line = strtok(NULL, "\n"); |
| continue; |
| } |
| else if (strncmp(line, "vht_oper_centr_freq_seg0_idx=", 25) == 0) |
| { |
| char vht_centr_line[64]; |
| snprintf(vht_centr_line, sizeof(vht_centr_line), "vht_oper_centr_freq_seg0_idx=%d", vht_oper_centr_freq_seg0_idx_val); |
| strcat(new_config, vht_centr_line); |
| strcat(new_config, "\n"); |
| } |
| else if (strncmp(line, "he_oper_centr_freq_seg0_idx=", 25) == 0) |
| { |
| char he_centr_line[64]; |
| snprintf(he_centr_line, sizeof(he_centr_line), "he_oper_centr_freq_seg0_idx=%d", he_oper_centr_freq_seg0_idx_val); |
| strcat(new_config, he_centr_line); |
| strcat(new_config, "\n"); |
| } |
| else if (strncmp(line, "he_su_beamformee=", 17) == 0) |
| { |
| char he_beam_line[32]; |
| snprintf(he_beam_line, sizeof(he_beam_line), "he_su_beamformee=%d", he_su_beamformee_val); |
| strcat(new_config, he_beam_line); |
| strcat(new_config, "\n"); |
| } |
| else if (strncmp(line, "vht_capab=", 10) == 0) |
| { |
| if (vht_capab) |
| strcat(new_config, vht_capab); |
| else |
| strcat(new_config, line); |
| strcat(new_config, "\n"); |
| } |
| else if (strncmp(line, "vht_oper_chwidth=", 17) == 0) |
| { |
| if (vht_oper_chwidth) |
| strcat(new_config, vht_oper_chwidth); |
| else |
| strcat(new_config, line); |
| strcat(new_config, "\n"); |
| } |
| else if (strncmp(line, "he_oper_chwidth=", 16) == 0) |
| { |
| if (he_oper_chwidth) |
| strcat(new_config, he_oper_chwidth); |
| else |
| strcat(new_config, line); |
| strcat(new_config, "\n"); |
| } |
| else |
| { |
| strcat(new_config, line); |
| strcat(new_config, "\n"); |
| } |
| line = strtok(NULL, "\n"); |
| } |
| |
| // 如果原文件无ht_capab行,补充新行(保持原有逻辑) |
| if (!ht_capab_processed && ht_capab) |
| { |
| strcat(new_config, ht_capab); |
| strcat(new_config, "\n"); |
| } |
| |
| // 检查vht_oper_centr_freq_seg0_idx是否已处理 |
| if (!strstr(new_config, "vht_oper_centr_freq_seg0_idx=")) |
| { |
| char vht_centr_line[64]; |
| snprintf(vht_centr_line, sizeof(vht_centr_line), "vht_oper_centr_freq_seg0_idx=%d\n", vht_oper_centr_freq_seg0_idx_val); |
| strcat(new_config, vht_centr_line); |
| } |
| // 检查he_oper_centr_freq_seg0_idx是否已处理 |
| if (!strstr(new_config, "he_oper_centr_freq_seg0_idx=")) |
| { |
| char he_centr_line[64]; |
| snprintf(he_centr_line, sizeof(he_centr_line), "he_oper_centr_freq_seg0_idx=%d\n", he_oper_centr_freq_seg0_idx_val); |
| strcat(new_config, he_centr_line); |
| } |
| // 检查he_su_beamformee是否已处理 |
| if (!strstr(new_config, "he_su_beamformee=")) |
| { |
| char he_beam_line[32]; |
| snprintf(he_beam_line, sizeof(he_beam_line), "he_su_beamformee=%d\n", he_su_beamformee_val); |
| strcat(new_config, he_beam_line); |
| } |
| |
| return write_file(HOSTAPD_CONF_PATH, new_config); |
| } |
| |
| |
| int gsw_wifi_ap_bandwidth_get(gsw_wifi_bandwidth_type_e *bandwidth) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[1024] = {0}; |
| if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| |
| // 精确解析ht_capab参数 |
| char *ht_line = strstr(buffer, "ht_capab="); |
| char *vht_line = strstr(buffer, "vht_capab="); |
| char *vht_width = strstr(buffer, "vht_oper_chwidth=1"); |
| |
| // 优先检查80MHz配置 |
| if (vht_width && vht_line && strstr(vht_line, "VHT80")) |
| { |
| *bandwidth = GSW_WIFI_BANDWIDTH_HT80; |
| } |
| else if (ht_line && strstr(ht_line, "HT40+")) |
| { |
| *bandwidth = GSW_WIFI_BANDWIDTH_HT40; |
| } |
| else |
| { |
| *bandwidth = GSW_WIFI_BANDWIDTH_HT20; |
| } |
| LOGI(GSW_WIFI,"AP get bandwidth: %d\n", *bandwidth); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_ap_channel_set(int channel) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| // 新增频率和信道校验 |
| LOGI(GSW_WIFI,"AP set channel: %d\n", channel); |
| int current_freq; |
| if (gsw_wifi_ap_frequency_get(¤t_freq) != 0) |
| { |
| LOGE(GSW_WIFI,"Failed to get current frequency\n"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| // 定义各频段支持的信道范围 |
| const int valid_2g_channels[] = VALID_2G_CHANNELS; |
| const int valid_5g_channels[] = VALID_5G_CHANNELS; |
| |
| int valid = 0; |
| if (current_freq == 1) |
| { // 2.4GHz |
| for (int i = 0; i < sizeof(valid_2g_channels) / sizeof(int); i++) |
| { |
| if (channel == valid_2g_channels[i]) |
| { |
| valid = 1; |
| break; |
| } |
| } |
| } |
| else |
| { // 5GHz |
| for (int i = 0; i < sizeof(valid_5g_channels) / sizeof(int); i++) |
| { |
| if (channel == valid_5g_channels[i]) |
| { |
| valid = 1; |
| break; |
| } |
| } |
| } |
| |
| if (!valid && channel != 0) |
| { // 允许0表示自动选择 |
| LOGI(GSW_WIFI,"Invalid channel %d for %s band\n", |
| channel, (current_freq == 1) ? "2.4GHz" : "5GHz"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| |
| // 修改为读取整个文件并替换channel行 |
| char buffer[4096] = {0}; |
| char new_config[4096] = {0}; |
| int noscan_set = 0; // 新增:标记是否已处理noscan行 |
| |
| // 读取现有配置 |
| if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| |
| // 处理每一行 |
| char *line = strtok(buffer, "\n"); |
| while (line) |
| { |
| // 新增:处理noscan行 |
| if (strstr(line, "noscan=") != NULL) |
| { |
| char noscan_line[32]; |
| snprintf(noscan_line, sizeof(noscan_line), "noscan=%d", (channel == 0) ? 0 : 1); |
| strcat(new_config, noscan_line); |
| strcat(new_config, "\n"); |
| noscan_set = 1; |
| } |
| // 如果是channel行,替换为新值 |
| else if (strstr(line, "channel=") != NULL) |
| { |
| char channel_line[32]; |
| snprintf(channel_line, sizeof(channel_line), "channel=%d", channel); |
| strcat(new_config, channel_line); |
| strcat(new_config, "\n"); |
| } |
| // 其他行保持不变 |
| else |
| { |
| strcat(new_config, line); |
| strcat(new_config, "\n"); |
| } |
| line = strtok(NULL, "\n"); |
| } |
| |
| // 如果没有找到channel行,添加新的channel设置 |
| if (strstr(new_config, "channel=") == NULL) |
| { |
| char channel_line[32]; |
| snprintf(channel_line, sizeof(channel_line), "channel=%d\n", channel); |
| strcat(new_config, channel_line); |
| } |
| |
| // 新增:如果没有找到noscan行,根据channel值补充 |
| if (!noscan_set) |
| { |
| char noscan_line[32]; |
| snprintf(noscan_line, sizeof(noscan_line), "noscan=%d\n", (channel == 0) ? 0 : 1); |
| strcat(new_config, noscan_line); |
| } |
| |
| // 写回文件 |
| return write_file(HOSTAPD_CONF_PATH, new_config); |
| } |
| |
| |
| int gsw_wifi_ap_channel_get(int *channel) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| if (channel == NULL) |
| { |
| LOGI(GSW_WIFI,"channel is NULL\n"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char buffer[1024] = {0}; |
| if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char *channel_line = strstr(buffer, "channel="); |
| if (channel_line) |
| { |
| channel_line += 8; // 跳过 "channel=" |
| *channel = atoi(channel_line); |
| } |
| LOGI(GSW_WIFI,"AP get channel: %d\n", *channel); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_ap_auth_set(gsw_wifi_auth_e auth) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[4096] = {0}; |
| char new_config[4096] = {0}; |
| int wpa_set = 0, key_mgmt_set = 0, wpa_pairwise_set = 0, rsn_pairwise_set = 0, ieee80211w_set = 0, auth_algs_set = 0; |
| |
| if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| LOGI(GSW_WIFI,"AP set auth: %d\n", auth); |
| char *line = strtok(buffer, "\n"); |
| while (line) |
| { |
| // 匹配行首为"wpa="的行(长度4) |
| if (strncmp(line, "wpa=", 4) == 0) |
| { |
| char wpa_line[32]; |
| if (auth == GSW_WIFI_AUTH_OPEN || auth == GSW_WIFI_AUTH_WEP) |
| snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 0); |
| else if (auth == GSW_WIFI_AUTH_WPA_PSK) |
| snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 1); |
| else if (auth == GSW_WIFI_AUTH_WPA2_PSK) |
| { |
| snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 3); |
| } |
| else if (auth == GSW_WIFI_AUTH_WPA3_PSK) |
| snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 2); |
| else |
| snprintf(wpa_line, sizeof(wpa_line), "wpa=%d", 2); |
| strcat(new_config, wpa_line); |
| strcat(new_config, "\n"); |
| wpa_set = 1; |
| } |
| else if (strncmp(line, "wpa_key_mgmt=", 13) == 0) |
| { |
| const char *key_mgmt = (auth == GSW_WIFI_AUTH_WPA3_PSK) ? "SAE WPA-PSK" : "WPA-PSK"; |
| char key_mgmt_line[64]; |
| snprintf(key_mgmt_line, sizeof(key_mgmt_line), "wpa_key_mgmt=%s", key_mgmt); |
| strcat(new_config, key_mgmt_line); |
| strcat(new_config, "\n"); |
| key_mgmt_set = 1; |
| } |
| else if (strncmp(line, "auth_algs=", 9) == 0) |
| { |
| char auth_algs_line[32]; |
| snprintf(auth_algs_line, sizeof(auth_algs_line), "auth_algs=%d", (auth == GSW_WIFI_AUTH_WEP) ? 3 : 1); |
| strcat(new_config, auth_algs_line); |
| strcat(new_config, "\n"); |
| auth_algs_set = 1; |
| } |
| else if (strncmp(line, "wpa_pairwise=", 13) == 0) |
| { |
| const char *pairwise = NULL; |
| if (auth == GSW_WIFI_AUTH_WPA_PSK) |
| pairwise = "TKIP"; |
| else if (auth == GSW_WIFI_AUTH_WPA2_PSK) |
| pairwise = "TKIP CCMP"; |
| else if (auth == GSW_WIFI_AUTH_WPA3_PSK) |
| pairwise = "CCMP"; |
| else |
| pairwise = "TKIP CCMP"; |
| char pairwise_line[64]; |
| snprintf(pairwise_line, sizeof(pairwise_line), "wpa_pairwise=%s", pairwise); |
| strcat(new_config, pairwise_line); |
| strcat(new_config, "\n"); |
| wpa_pairwise_set = 1; |
| } |
| else if (strncmp(line, "rsn_pairwise=", 13) == 0) |
| { |
| if (auth == GSW_WIFI_AUTH_WPA2_PSK || auth == GSW_WIFI_AUTH_WPA3_PSK) |
| { |
| strcat(new_config, "rsn_pairwise=CCMP\n"); |
| } |
| rsn_pairwise_set = 1; |
| } |
| else if (strncmp(line, "ieee80211w=", 11) == 0) |
| { |
| if (auth == GSW_WIFI_AUTH_WPA3_PSK) |
| { |
| strcat(new_config, "ieee80211w=2\n"); // WPA3 强制 MFP |
| ieee80211w_set = 1; |
| } |
| else if (auth == GSW_WIFI_AUTH_WPA2_PSK) |
| { |
| strcat(new_config, "ieee80211w=1\n"); // WPA2 可选 MFP |
| ieee80211w_set = 1; |
| } |
| // 其他模式不添加该行(直接跳过) |
| } |
| else |
| { |
| strcat(new_config, line); |
| strcat(new_config, "\n"); |
| } |
| line = strtok(NULL, "\n"); |
| } |
| |
| // 补全未匹配的配置项 |
| if (!wpa_set) |
| { |
| if (auth == GSW_WIFI_AUTH_OPEN || auth == GSW_WIFI_AUTH_WEP) |
| strcat(new_config, "wpa=0"); |
| else if (auth == GSW_WIFI_AUTH_WPA_PSK) |
| strcat(new_config, "wpa=1"); |
| else if (auth == GSW_WIFI_AUTH_WPA2_PSK) |
| { |
| strcat(new_config, "wpa=3"); |
| } |
| else if (auth == GSW_WIFI_AUTH_WPA3_PSK) |
| strcat(new_config, "wpa=2"); |
| else |
| strcat(new_config, "wpa=2"); |
| strcat(new_config, "\n"); |
| } |
| if (!key_mgmt_set) |
| { |
| const char *key_mgmt = (auth == GSW_WIFI_AUTH_WPA3_PSK) ? "WPA-PSK SAE" : "WPA-PSK"; |
| strcat(new_config, "wpa_key_mgmt="); |
| strcat(new_config, key_mgmt); |
| strcat(new_config, "\n"); |
| } |
| if (!auth_algs_set) |
| { |
| char auth_algs_line[32]; |
| snprintf(auth_algs_line, sizeof(auth_algs_line), "auth_algs=%d\n", (auth == GSW_WIFI_AUTH_WEP) ? 3 : 1); |
| strcat(new_config, auth_algs_line); |
| } |
| if (!wpa_pairwise_set) |
| { |
| const char *pairwise = NULL; |
| if (auth == GSW_WIFI_AUTH_WPA_PSK) |
| pairwise = "TKIP"; |
| else if (auth == GSW_WIFI_AUTH_WPA2_PSK) |
| pairwise = "TKIP CCMP"; |
| else if (auth == GSW_WIFI_AUTH_WPA3_PSK) |
| pairwise = "CCMP"; |
| else |
| pairwise = "TKIP CCMP"; |
| strcat(new_config, "wpa_pairwise="); |
| strcat(new_config, pairwise); |
| strcat(new_config, "\n"); |
| } |
| if (!rsn_pairwise_set) |
| { |
| if (auth == GSW_WIFI_AUTH_WPA2_PSK || auth == GSW_WIFI_AUTH_WPA3_PSK) |
| { |
| strcat(new_config, "rsn_pairwise=CCMP\n"); |
| } |
| } |
| if (!ieee80211w_set && (auth == GSW_WIFI_AUTH_WPA3_PSK || auth == GSW_WIFI_AUTH_WPA2_PSK)) |
| { |
| // 仅当需要时补全 ieee80211w 行(其他模式不添加) |
| strcat(new_config, (auth == GSW_WIFI_AUTH_WPA3_PSK) ? "ieee80211w=2\n" : "ieee80211w=1\n"); |
| } |
| |
| return write_file(HOSTAPD_CONF_PATH, new_config); |
| } |
| |
| int gsw_wifi_ap_auth_get(gsw_wifi_auth_e *auth) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[1024] = {0}; |
| if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| |
| char *auth_algs_line = strstr(buffer, "auth_algs="); |
| if (auth_algs_line) |
| { |
| auth_algs_line += 10; // 跳过 "auth_algs=" |
| int auth_algs_val = atoi(auth_algs_line); |
| if (auth_algs_val == 3) |
| { |
| *auth = GSW_WIFI_AUTH_WEP; |
| return GSW_HAL_SUCCESS; |
| } |
| } |
| // 优先通过 wpa_key_mgmt 判断 WPA3(包含 SAE 关键字) |
| char *key_mgmt_line = strstr(buffer, "wpa_key_mgmt="); |
| if (key_mgmt_line) |
| { |
| key_mgmt_line += 12; // 跳过 "wpa_key_mgmt=" |
| if (strstr(key_mgmt_line, "SAE")) |
| { |
| *auth = GSW_WIFI_AUTH_WPA3_PSK; |
| return GSW_HAL_SUCCESS; |
| } |
| } |
| |
| // 若未找到 SAE,再通过 wpa= 判断 WPA2 或开放模式 |
| char *wpa_line = strstr(buffer, "wpa="); |
| if (wpa_line) |
| { |
| wpa_line += 4; |
| int wpa_val = atoi(wpa_line); |
| if(wpa_val > 1) |
| { |
| *auth = GSW_WIFI_AUTH_WPA2_PSK; |
| } |
| else if (wpa_val == 1) |
| { |
| *auth = GSW_WIFI_AUTH_WPA_PSK; |
| } |
| else |
| { |
| *auth = GSW_WIFI_AUTH_OPEN; |
| } |
| } |
| else |
| { |
| *auth = GSW_WIFI_AUTH_OPEN; // 默认开放模式 |
| } |
| LOGI(GSW_WIFI,"AP get auth: %d\n", *auth); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_ap_password_set(char *password) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[4096] = {0}; |
| char new_config[4096] = {0}; |
| int has_passphrase = 0; // 标记是否已处理过密码行 |
| if (password == NULL) { |
| LOGE(GSW_WIFI,"Password cannot be NULL"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| LOGI(GSW_WIFI,"AP set password\n"); |
| size_t pass_len = strlen(password); |
| if (pass_len < MIN_PASSWORD_LEN || pass_len > MAX_PASSWORD_LEN) { |
| LOGE(GSW_WIFI,"Password length must be 8-63 characters, current length: %zu", pass_len); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| // 读取现有配置文件内容 |
| if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| |
| // 逐行处理配置文件(修改:增加密码行存在标记) |
| char *line = strtok(buffer, "\n"); |
| while (line) |
| { |
| // 匹配到原密码行时替换为新密码,并标记已处理 |
| if (strstr(line, "wpa_passphrase=") != NULL) |
| { |
| char new_pass_line[128]; |
| snprintf(new_pass_line, sizeof(new_pass_line), "wpa_passphrase=%s", password); |
| strcat(new_config, new_pass_line); |
| has_passphrase = 1; // 标记存在密码行 |
| } |
| else |
| { |
| strcat(new_config, line); |
| } |
| strcat(new_config, "\n"); // 保留换行符 |
| line = strtok(NULL, "\n"); |
| } |
| |
| // 若原文件无密码行,添加新密码行到末尾 |
| if (!has_passphrase) |
| { |
| char new_pass_line[128]; |
| snprintf(new_pass_line, sizeof(new_pass_line), "wpa_passphrase=%s\n", password); |
| strcat(new_config, new_pass_line); |
| } |
| |
| // 写回配置文件 |
| return write_file(HOSTAPD_CONF_PATH, new_config); |
| } |
| |
| int gsw_wifi_ap_password_get(char *password) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[4096] = {0}; // 使用更大的缓冲区 |
| if (read_file(HOSTAPD_CONF_PATH, buffer, sizeof(buffer)) == -1) |
| { |
| LOGI(GSW_WIFI,"Failed to read hostapd config file\n"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| |
| // 查找密码行 |
| char *passphrase_line = strstr(buffer, "wpa_passphrase="); |
| if (!passphrase_line) |
| { |
| LOGI(GSW_WIFI,"No password line found in config\n"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| |
| passphrase_line += 15; // 跳过 "wpa_passphrase=" |
| |
| // 查找行结束位置 |
| char *end = strchr(passphrase_line, '\n'); |
| if (end) |
| { |
| *end = '\0'; // 确保字符串正确终止 |
| } |
| |
| // 检查密码长度是否合法 |
| size_t pass_len = strlen(passphrase_line); |
| if (pass_len == 0 || pass_len >= MAX_PASSWORD_LEN) |
| { |
| LOGI(GSW_WIFI,"Invalid password length: %zu\n", pass_len); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| |
| // 复制密码 |
| strncpy(password, passphrase_line, MAX_PASSWORD_LEN); |
| password[MAX_PASSWORD_LEN] = '\0'; // 确保终止 |
| LOGI(GSW_WIFI,"AP get password\n"); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| /** |
| * @brief 获取 AP 模式的运行状态 |
| * @param [out] ap_status 用于存储 AP 模式运行状态的指针 |
| * @retval 0 表示函数执行成功 |
| */ |
| int gsw_wifi_get_ap_status(gsw_wifi_ap_run_status_e *ap_status) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| int result = is_hostapd_running(); |
| if (result == 0) |
| { |
| *ap_status = GSW_WIFI_AP_STATUS_ENABLE; |
| } |
| else |
| { |
| *ap_status = GSW_WIFI_AP_STATUS_DISABLE; |
| } |
| return GSW_HAL_SUCCESS; |
| } |
| |
| /* STA 模式相关函数实现 */ |
| |
| // 辅助函数:更新 wpa_supplicant 配置文件 |
| int update_wpa_supplicant_conf() |
| { |
| char new_config[4096] = {0}; |
| |
| // 直接写入必需配置项(覆盖原有文件),新增WPA3支持的关键配置 |
| const char *required_configs[] = { |
| "ctrl_interface=/var/run/wpa_supplicant", // 接口路径 |
| "ctrl_interface_group=root", // 组设置 |
| "update_config=1", // 允许自动更新配置 |
| "ap_scan=1", // 主动扫描AP |
| // 网络块配置调整:使用数值19表示SAE组(替代SUITE_B_192文本标识) |
| "network={\n key_mgmt=SAE\n pairwise=CCMP\n ieee80211w=2\n }" |
| }; |
| int config_count = sizeof(required_configs) / sizeof(required_configs[0]); |
| |
| // 添加所有必需的配置项(确保顺序) |
| for (int i = 0; i < config_count; i++) |
| { |
| strcat(new_config, required_configs[i]); |
| strcat(new_config, "\n"); |
| } |
| |
| // 写回文件(完全覆盖) |
| return write_file(WPA_SUPPLICANT_CONF_PATH, new_config); |
| } |
| |
| int gsw_wifi_sta_start() |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| LOGI(GSW_WIFI,"Starting wpa_supplicant...\n"); |
| // 更新 wpa_supplicant 配置文件 |
| if (update_wpa_supplicant_conf() != 0) |
| { |
| LOGI(GSW_WIFI,"Failed to update wpa_supplicant configuration file.\n"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "wpa_supplicant -Dnl80211 -i%s -c%s -B", WLAN_STA_DEV, WPA_SUPPLICANT_CONF_PATH); |
| int status = execute_command(cmd); |
| sleep(5); |
| memset(cmd, 0, sizeof(cmd)); |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan > /dev/null", WLAN_STA_DEV); |
| execute_command(cmd); |
| memset(cmd, 0, sizeof(cmd)); |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan_results > /dev/null", WLAN_STA_DEV); |
| execute_command(cmd); |
| LOGI(GSW_WIFI,"wpa_supplicant starting success\n"); |
| return status; |
| } |
| |
| int gsw_wifi_sta_stop() |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| LOGI(GSW_WIFI,"Stopping wpa_supplicant...\n"); |
| if (is_wpa_supplicant_running() != 0) |
| { |
| LOGI(GSW_WIFI,"wpa_supplicant is not running, no need to stop.\n"); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "killall wpa_supplicant"); |
| LOGI(GSW_WIFI,"Stoping success\n"); |
| return execute_command(cmd); |
| } |
| |
| int gsw_wifi_get_sta_status(gsw_wifi_sta_run_status_e *sta_status) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| if (is_wpa_supplicant_running() == 0) |
| { |
| *sta_status = GSW_WIFI_STA_STATUS_ENABLE; |
| } |
| else |
| { |
| *sta_status = GSW_WIFI_STA_STATUS_DISABLE; |
| } |
| return GSW_HAL_SUCCESS; |
| } |
| |
| // 修改涉及 wpa_cli 命令的函数 |
| int gsw_wifi_get_sta_ssid(char *sta_ssid) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| if (is_wpa_supplicant_running() != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'ssid=' | cut -d '=' -f 2", WLAN_STA_DEV); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[MAX_SSID_LEN + 1]; |
| if (fgets(buffer, sizeof(buffer), output) == NULL) |
| { |
| pclose(output); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| pclose(output); |
| buffer[strcspn(buffer, "\n")] = '\0'; |
| strncpy(sta_ssid, buffer, MAX_SSID_LEN); |
| LOGI(GSW_WIFI,"SSID: %s\n", sta_ssid); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_get_sta_auth(gsw_wifi_auth_e *auth) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| if (is_wpa_supplicant_running() != 0) |
| { |
| LOGI(GSW_WIFI,"wpa_supplicant is not running.\n"); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char cmd[MAX_COMMAND_LEN]; |
| // 修改命令,获取 key_mgmt 字段 |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'key_mgmt=' | cut -d '=' -f 2", WLAN_STA_DEV); |
| LOGI(GSW_WIFI,"Executing command: %s\n", cmd); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| { |
| LOGI(GSW_WIFI,"Failed to execute command: %s\n", cmd); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char buffer[32]; |
| if (fgets(buffer, sizeof(buffer), output) == NULL) |
| { |
| LOGI(GSW_WIFI,"No output from command: %s\n", cmd); |
| pclose(output); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| pclose(output); |
| buffer[strcspn(buffer, "\n")] = '\0'; |
| LOGI(GSW_WIFI,"Command output: %s\n", buffer); |
| |
| if (strstr(buffer, "WPA2-PSK")) |
| { |
| *auth = GSW_WIFI_AUTH_WPA2_PSK; |
| } |
| else if (strstr(buffer, "WPA-PSK")) |
| { |
| *auth = GSW_WIFI_AUTH_WPA_PSK; |
| } |
| else if (strstr(buffer, "SAE")) |
| { |
| *auth = GSW_WIFI_AUTH_WPA3_PSK; |
| } |
| else if (strstr(buffer, "NONE")) |
| { |
| *auth = GSW_WIFI_AUTH_OPEN; |
| } |
| else |
| { |
| LOGI(GSW_WIFI,"Unknown authentication type: %s. Assuming WPA2-PSK.\n", buffer); |
| *auth = GSW_WIFI_AUTH_WPA2_PSK; |
| } |
| LOGI(GSW_WIFI,"Authentication type: %s\n", buffer); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_sta_connect(char *ssid, gsw_wifi_auth_e auth, char *password) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| if (is_wpa_supplicant_running() != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| |
| char quoted_ssid[MAX_SSID_LEN + 3]; // 为引号和字符串结尾留空间 |
| char quoted_password[MAX_PASSWORD_LEN + 3]; |
| |
| // 添加引号 |
| snprintf(quoted_ssid, sizeof(quoted_ssid), "\\\"%s\\\"", ssid); |
| if (auth != GSW_WIFI_AUTH_OPEN) |
| { |
| snprintf(quoted_password, sizeof(quoted_password), "\\\"%s\\\"", password); |
| } |
| |
| LOGI(GSW_WIFI,"SSID = %s\n", quoted_ssid); |
| |
| char cmd[MAX_COMMAND_LEN]; |
| int status; |
| |
| // 修改:-p 改为 -i 并使用 WLAN_STA_DEV |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s remove_network 0", WLAN_STA_DEV); |
| LOGI(GSW_WIFI,"Executing command type: Remove network\n"); |
| status = execute_command(cmd); |
| if (status != 0) |
| { |
| return status; |
| } |
| |
| // 修改:-p 改为 -i 并使用 WLAN_STA_DEV |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s ap_scan 1", WLAN_STA_DEV); |
| LOGI(GSW_WIFI,"Executing command type: Set AP scan\n"); |
| status = execute_command(cmd); |
| if (status != 0) |
| { |
| return status; |
| } |
| |
| // 修改:-p 改为 -i 并使用 WLAN_STA_DEV |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s add_network", WLAN_STA_DEV); |
| LOGI(GSW_WIFI,"Executing command type: Add network\n"); |
| status = execute_command(cmd); |
| if (status != 0) |
| { |
| return status; |
| } |
| |
| // 修改:-p 改为 -i 并使用 WLAN_STA_DEV |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 ssid %s", WLAN_STA_DEV, quoted_ssid); |
| LOGI(GSW_WIFI,"Executing command type: Set SSID\n"); |
| status = execute_command(cmd); |
| if (status != 0) |
| { |
| return status; |
| } |
| |
| if (auth != GSW_WIFI_AUTH_OPEN) |
| { |
| const char *key_mgmt; |
| switch (auth) |
| { |
| case GSW_WIFI_AUTH_WEP: |
| key_mgmt = "NONE"; |
| // 修改:-p 改为 -i 并使用 WLAN_STA_DEV |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 wep_key0 %s", WLAN_STA_DEV, quoted_password); |
| LOGI(GSW_WIFI,"Executing command type: Set WEP key\n"); |
| status = execute_command(cmd); |
| if (status != 0) |
| return status; |
| |
| // 修改:-p 改为 -i 并使用 WLAN_STA_DEV(注意原代码中可能存在拼写错误 "weep_tx_keyidx" 应为 "wep_tx_keyidx") |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 wep_tx_keyidx 0", WLAN_STA_DEV); |
| LOGI(GSW_WIFI,"Executing command type: Set WEP TX key index\n"); |
| status = execute_command(cmd); |
| if (status != 0) |
| return status; |
| break; |
| |
| case GSW_WIFI_AUTH_WPA_PSK: |
| key_mgmt = "WPA-PSK"; |
| break; |
| case GSW_WIFI_AUTH_WPA2_PSK: |
| key_mgmt = "WPA-PSK"; |
| break; |
| case GSW_WIFI_AUTH_WPA3_PSK: |
| key_mgmt = "SAE"; |
| break; |
| default: |
| key_mgmt = "WPA-PSK"; |
| break; |
| } |
| |
| if (auth != GSW_WIFI_AUTH_WEP) |
| { |
| // 修改:-p 改为 -i 并使用 WLAN_STA_DEV |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 key_mgmt %s", WLAN_STA_DEV, key_mgmt); |
| LOGI(GSW_WIFI,"Executing command type: Set key management\n"); |
| status = execute_command(cmd); |
| if (status != 0) |
| return status; |
| |
| if (auth == GSW_WIFI_AUTH_WPA3_PSK) |
| { |
| // WPA3 特有配置:确保在设置key_mgmt后配置SAE参数 |
| // 设置椭圆曲线组(使用NIST P-256,组号19) |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 sae_groups 19", WLAN_STA_DEV); |
| status = execute_command(cmd); |
| if (status != 0) |
| return status; |
| |
| // 强制MFP保护(WPA3要求必须启用) |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 sae_require_mfp 1", WLAN_STA_DEV); |
| status = execute_command(cmd); |
| if (status != 0) |
| return status; |
| |
| // 使用SAE+CCMP加密组合(确保与AP端匹配) |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 pairwise CCMP", WLAN_STA_DEV); |
| status = execute_command(cmd); |
| if (status != 0) |
| return status; |
| } |
| |
| // 修改:-p 改为 -i 并使用 WLAN_STA_DEV |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 psk %s", WLAN_STA_DEV, quoted_password); |
| LOGI(GSW_WIFI,"Executing command type: Set PSK\n"); |
| status = execute_command(cmd); |
| if (status != 0) |
| return status; |
| } |
| } |
| else |
| { |
| // 修改:-p 改为 -i 并使用 WLAN_STA_DEV |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s set_network 0 key_mgmt NONE", WLAN_STA_DEV); |
| LOGI(GSW_WIFI,"Executing command type: Set key management for open network\n"); |
| status = execute_command(cmd); |
| if (status != 0) |
| return status; |
| } |
| |
| // 修改:-p 改为 -i 并使用 WLAN_STA_DEV |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s select_network 0", WLAN_STA_DEV); |
| LOGI(GSW_WIFI,"Executing command type: Select network\n"); |
| status = execute_command(cmd); |
| if (status != 0) |
| { |
| return status; |
| } |
| |
| // udhcpc 命令无需修改(已使用 WLAN_STA_DEV) |
| snprintf(cmd, sizeof(cmd), "udhcpc -i %s -n", WLAN_STA_DEV); |
| LOGI(GSW_WIFI,"Executing command type: Request DHCP IP\n"); |
| status = execute_command(cmd); |
| return status; |
| } |
| |
| int gsw_wifi_sta_forget_ap(char *ssid, gsw_wifi_auth_e auth) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| if (is_wpa_supplicant_running() != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char cmd[MAX_COMMAND_LEN]; |
| 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); |
| return execute_command(cmd); |
| } |
| |
| int gsw_wifi_sta_start_scan(void) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| if (is_wpa_supplicant_running() != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan", WLAN_STA_DEV); |
| return execute_command(cmd); |
| } |
| |
| int gsw_wifi_get_interface_ip(gsw_wifi_index_e idx, char *ip) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char cmd[MAX_COMMAND_LEN]; |
| 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); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[MAX_IP_LEN + 1]; |
| if (fgets(buffer, sizeof(buffer), output) == NULL) |
| { |
| pclose(output); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| pclose(output); |
| buffer[strcspn(buffer, "\n")] = '\0'; |
| strncpy(ip, buffer, MAX_IP_LEN); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_get_interface_mac(gsw_wifi_index_e idx, char *mac) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char cmd[MAX_COMMAND_LEN]; |
| 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); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[MAX_MAC_LEN + 1]; |
| if (fgets(buffer, sizeof(buffer), output) == NULL) |
| { |
| pclose(output); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| pclose(output); |
| buffer[strcspn(buffer, "\n")] = '\0'; // 移除换行符 |
| |
| // 关键修改:确保MAC地址仅保留17位有效字符(标准MAC格式为xx:xx:xx:xx:xx:xx,共17字符) |
| strncpy(mac, buffer, 17); // 限制复制17个字符 |
| mac[17] = '\0'; // 显式添加字符串结束符 |
| |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_get_connect_ap_mac(char *mac) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'bssid=' | cut -d '=' -f 2", WLAN_STA_DEV); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[MAX_MAC_LEN + 1]; |
| if (fgets(buffer, sizeof(buffer), output) == NULL) |
| { |
| pclose(output); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| pclose(output); |
| buffer[strcspn(buffer, "\n")] = '\0'; |
| strncpy(mac, buffer, MAX_MAC_LEN); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_get_connect_ap_rssi(int *rssi) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'signal=' | cut -d '=' -f 2", WLAN_STA_DEV); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[16]; |
| if (fgets(buffer, sizeof(buffer), output) == NULL) |
| { |
| pclose(output); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| pclose(output); |
| buffer[strcspn(buffer, "\n")] = '\0'; |
| *rssi = atoi(buffer); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_get_connect_ap_band(gsw_wifi_band_e *band) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'frequency=' | cut -d '=' -f 2", WLAN_STA_DEV); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[32]; |
| if (fgets(buffer, sizeof(buffer), output) == NULL) |
| { |
| pclose(output); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| pclose(output); |
| buffer[strcspn(buffer, "\n")] = '\0'; |
| int freq = atoi(buffer); |
| if (freq < 2500) |
| { |
| *band = GSW_WIFI_2G_band; |
| } |
| else |
| { |
| *band = GSW_WIFI_5G_band; |
| } |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_get_connect_ap_ip(char *ip) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s status | grep 'ip_address=' | cut -d '=' -f 2", WLAN_STA_DEV); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[MAX_IP_LEN + 1]; |
| if (fgets(buffer, sizeof(buffer), output) == NULL) |
| { |
| pclose(output); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| pclose(output); |
| buffer[strcspn(buffer, "\n")] = '\0'; |
| strncpy(ip, buffer, MAX_IP_LEN); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_get_sta_available_ap(gsw_ap_detail_info_s *info) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| if (is_wpa_supplicant_running() != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan > /dev/null", WLAN_STA_DEV); |
| execute_command(cmd); |
| sleep(1); |
| memset(cmd, 0, sizeof(cmd)); |
| 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); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[256]; |
| if (fgets(buffer, sizeof(buffer), output) == NULL) |
| { |
| pclose(output); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| pclose(output); |
| buffer[strcspn(buffer, "\n")] = '\0'; |
| 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); |
| info->status = GSW_WIFI_AP_STATUS_ENABLE; |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_get_ap_device_list(gsw_ap_info_s *ap_info, gsw_device_info_s *device_list, int len, int *dev_len) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "hostapd_cli -i %s all_sta", WLAN_AP_DEV); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[4096]; |
| size_t length = fread(buffer, 1, sizeof(buffer) - 1, output); |
| if (length == 0) |
| { |
| pclose(output); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| buffer[length] = '\0'; |
| pclose(output); |
| // 解析 hostapd_cli 输出,填充 device_list |
| *dev_len = 0; |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_get_sta_saved_ap(gsw_saved_ap_info_s *list, int len, int *list_len) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| if (is_wpa_supplicant_running() != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s list_networks", WLAN_STA_DEV); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[4096]; |
| size_t length = fread(buffer, 1, sizeof(buffer) - 1, output); |
| if (length == 0) |
| { |
| pclose(output); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| buffer[length] = '\0'; |
| pclose(output); |
| // 解析 wpa_cli 输出,填充 list |
| *list_len = 0; |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_get_scan_list(gsw_scan_info_s *list, int len, int *list_len) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| if (is_wpa_supplicant_running() != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan > /dev/null", WLAN_STA_DEV); |
| execute_command(cmd); |
| sleep(1); |
| memset(cmd, 0, sizeof(cmd)); |
| snprintf(cmd, sizeof(cmd), "wpa_cli -i %s scan_results", WLAN_STA_DEV); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[4096]; |
| size_t length = fread(buffer, 1, sizeof(buffer) - 1, output); |
| if (length == 0) |
| { |
| pclose(output); |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| buffer[length] = '\0'; |
| pclose(output); |
| |
| // 解析 wpa_cli 输出,填充 list |
| *list_len = 0; |
| char *line = strtok(buffer, "\n"); |
| // 跳过标题行 |
| if (line) |
| line = strtok(NULL, "\n"); |
| |
| while (line && *list_len < len) |
| { |
| gsw_scan_info_s *info = &list[*list_len]; |
| int freq; // 临时变量用于存储频率,以判断频段 |
| char flags[256]; // 临时变量用于存储认证标志 |
| |
| // 假设输出格式为:bssid freq signal flags ssid |
| // 解析每一行数据 |
| sscanf(line, "%31s %d %d %255s %32[^\n]", info->mac, &freq, &info->rssi, flags, info->ssid); |
| |
| // 根据频率判断频段 |
| if (freq < 2500) |
| { |
| info->band = GSW_WIFI_2G_band; |
| } |
| else |
| { |
| info->band = GSW_WIFI_5G_band; |
| } |
| |
| // 根据 flags 判断认证方式 |
| if (strstr(flags, "SAE") != NULL || strstr(flags, "WPA3") != NULL) |
| { |
| info->auth = GSW_WIFI_AUTH_WPA3_PSK; |
| } |
| else if (strstr(flags, "WPA2") != NULL) |
| { |
| info->auth = GSW_WIFI_AUTH_WPA2_PSK; |
| } |
| else if (strstr(flags, "WPA") != NULL) |
| { |
| info->auth = GSW_WIFI_AUTH_WPA_PSK; |
| } |
| else |
| { |
| info->auth = GSW_WIFI_AUTH_OPEN; |
| } |
| |
| (*list_len)++; |
| line = strtok(NULL, "\n"); |
| } |
| return GSW_HAL_SUCCESS; |
| } |
| |
| void *ap_event_listener(void *arg) |
| { |
| char list_cmd[MAX_COMMAND_LEN]; |
| snprintf(list_cmd, sizeof(list_cmd), "hostapd_cli -i %s all_sta | grep -E '^[0-9a-f:]{17}$|connected_time=' | \ |
| awk '/^[0-9a-f:]{17}$/ { mac=$0; getline; if ($0 ~ /connected_time=/) print mac }'", |
| WLAN_AP_DEV); |
| |
| // 保存上一次的设备MAC地址列表 |
| char prev_macs[GSW_WIFI_AP_DEVICE_NUM][32] = {0}; |
| int prev_count = 0; |
| while (ap_event_context.running && ap_event_context.gsw_cb) |
| { |
| if (is_hostapd_running() != 0) |
| { |
| sleep(5); |
| continue; |
| } |
| |
| // 使用list_cmd获取当前设备MAC列表 |
| char curr_macs[GSW_WIFI_AP_DEVICE_NUM][32] = {0}; |
| int curr_count = 0; |
| FILE *list_output = popen(list_cmd, "r"); |
| if (list_output) |
| { |
| char buffer[1024]; |
| if (fread(buffer, 1, sizeof(buffer) - 1, list_output) > 0) |
| { |
| buffer[sizeof(buffer) - 1] = '\0'; |
| char *line = strtok(buffer, "\n"); |
| while (line && curr_count < GSW_WIFI_AP_DEVICE_NUM) |
| { |
| if (strchr(line, ':')) |
| { |
| strncpy(curr_macs[curr_count], line, 17); // 复制MAC地址 |
| curr_count++; |
| } |
| line = strtok(NULL, "\n"); |
| } |
| } |
| pclose(list_output); |
| } |
| |
| // 数量变化时直接触发事件(保持原有逻辑) |
| if (curr_count > prev_count && ap_event_context.gsw_cb) |
| { |
| ap_event_context.gsw_cb(ap_event_context.arg, GSW_WIFI_STATUS_CONNECT); |
| } |
| else if (curr_count < prev_count && ap_event_context.gsw_cb) |
| { |
| ap_event_context.gsw_cb(ap_event_context.arg, GSW_WIFI_STATUS_DISCONNECT); |
| } |
| else if (curr_count == prev_count && ap_event_context.gsw_cb) |
| { |
| int mac_updated = 0; |
| // 遍历检查是否有MAC地址变化 |
| for (int i = 0; i < curr_count; i++) |
| { |
| int found = 0; |
| for (int j = 0; j < prev_count; j++) |
| { |
| if (strcasecmp(curr_macs[i], prev_macs[j]) == 0) |
| { |
| found = 1; |
| break; |
| } |
| } |
| // 发现新MAC地址(当前存在但之前不存在) |
| if (!found) |
| { |
| LOGI(GSW_WIFI,"New device connected: %s\n", curr_macs[i]); |
| mac_updated++; |
| break; |
| } |
| } |
| // 若有更新则触发连接事件(可根据需求扩展为断开事件) |
| if (mac_updated) |
| { |
| ap_event_context.gsw_cb(ap_event_context.arg, GSW_WIFI_STATUS_DISCONNECT); |
| ap_event_context.gsw_cb(ap_event_context.arg, GSW_WIFI_STATUS_CONNECT); |
| } |
| } |
| |
| memcpy(prev_macs, curr_macs, sizeof(prev_macs)); |
| prev_count = curr_count; |
| |
| sleep(2); // 定期轮询 |
| } |
| return NULL; |
| } |
| |
| /** |
| * @brief register ap event notification function |
| * @param [in] arg as gsw_cb function parameter, can be NULL |
| * @param [in] gsw_cb callback |
| * @retval 0: success |
| * @retval other: fail |
| */ |
| int gsw_wifi_reg_ap_event_callback(void *arg, GSW_AP_CALLBACK_FUNC_PTR gsw_cb) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| LOGI(GSW_WIFI,"%s - start.\n",__func__); |
| if (ap_event_context.running) |
| { |
| return GSW_HAL_NORMAL_FAIL; // 已经注册了回调 |
| } |
| if (arg == NULL) |
| { |
| LOGE(GSW_WIFI,"%s - arg is NULL.\n",__func__); |
| } |
| if (gsw_cb == NULL) |
| { |
| LOGE(GSW_WIFI,"%s - gsw_cb is NULL.\n",__func__); |
| return GSW_HAL_NORMAL_FAIL; // 回调函数不能为空 |
| } |
| ap_event_context.arg = arg; |
| ap_event_context.gsw_cb = gsw_cb; |
| ap_event_context.running = 1; |
| if (pthread_create(&ap_event_context.thread_id, NULL, ap_event_listener, NULL) != 0) |
| { |
| ap_event_context.running = 0; |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| LOGI(GSW_WIFI,"%s - end.\n",__func__); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| /** |
| * @brief unregister ap callback function |
| * @param [in] arg reserved parameter, can be NULL |
| * @retval 0: success |
| * @retval other: fail |
| */ |
| int gsw_wifi_unreg_ap_event_callback(void *arg) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| LOGI(GSW_WIFI,"%s - start.\n",__func__); |
| ap_event_context.running = 0; |
| |
| // 检查线程是否存在 |
| if (ap_event_context.thread_id != 0) |
| { |
| if (pthread_join(ap_event_context.thread_id, NULL) != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| ap_event_context.thread_id = 0; // 重置线程ID |
| } |
| |
| ap_event_context.arg = NULL; |
| ap_event_context.gsw_cb = NULL; |
| LOGI(GSW_WIFI,"%s - end.\n",__func__); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_reg_sta_event_callback(void *arg, GSW_STA_CALLBACK_FUNC_PTR gsw_cb) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_unreg_sta_event_callback(void *arg) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_sdk_interface_init() |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int is_module_loaded(const char *module_name) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "lsmod | grep %s", module_name); |
| FILE *output = popen(cmd, "r"); |
| if (!output) |
| return GSW_HAL_NORMAL_FAIL; |
| char buffer[256]; |
| if (fgets(buffer, sizeof(buffer), output) == NULL) |
| { |
| pclose(output); |
| return GSW_HAL_SUCCESS; |
| } |
| pclose(output); |
| return 1; |
| } |
| /** |
| * @brief load wifi driver |
| * @retval 0: success |
| * @retval other: fail |
| */ |
| int gsw_wifi_enable(void) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| LOGI(GSW_WIFI,"%s - start.\n",__func__); |
| if (execute_command("echo 1 > /sys/devices/platform/mbtk-sdh/pwr_ctrl") != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| sleep(1); |
| if (!is_module_loaded("cfg80211")) |
| { |
| if (execute_command("modprobe cfg80211") != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| } |
| else |
| { |
| LOGI(GSW_WIFI,"cfg80211 has insmod.\n"); |
| } |
| |
| if (!is_module_loaded("aic8800_bsp")) |
| { |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "modprobe %s", AIC8800_BSP_DRIVER_PATH); |
| if (execute_command(cmd) != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| } |
| else |
| { |
| LOGI(GSW_WIFI,"aic8800_bsp has insmod.\n"); |
| } |
| |
| if (!is_module_loaded("aic8800_fdrv")) |
| { |
| char cmd[MAX_COMMAND_LEN]; |
| snprintf(cmd, sizeof(cmd), "modprobe %s", AIC8800_FDRV_DRIVER_PATH); |
| if (execute_command(cmd) != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| } |
| else |
| { |
| LOGI(GSW_WIFI,"aic8800_fdrv has insmod.\n"); |
| } |
| LOGI(GSW_WIFI,"%s - end.\n",__func__); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| /** |
| * @brief uninstall wifi driver |
| * @retval 0: success |
| * @retval other: fail |
| */ |
| int gsw_wifi_disable(void) |
| { |
| if (handle()) |
| return GSW_HAL_NORMAL_FAIL; |
| LOGI(GSW_WIFI,"%s - start.\n",__func__); |
| if (is_module_loaded("aic8800_fdrv")) |
| { |
| if (execute_command("rmmod aic8800_fdrv") != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| } |
| else |
| { |
| LOGI(GSW_WIFI,"aic8800_fdrv not insmod.\n"); |
| } |
| |
| if (is_module_loaded("aic8800_bsp")) |
| { |
| if (execute_command("rmmod aic8800_bsp") != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| } |
| else |
| { |
| LOGI(GSW_WIFI,"aic8800_bsp not insmod.\n"); |
| } |
| |
| if (is_module_loaded("cfg80211")) |
| { |
| if (execute_command("rmmod cfg80211") != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| } |
| else |
| { |
| LOGI(GSW_WIFI,"cfg80211 not insmod.\n"); |
| } |
| if (execute_command("echo 0 > /sys/devices/platform/mbtk-sdh/pwr_ctrl") != 0) |
| { |
| return GSW_HAL_NORMAL_FAIL; |
| } |
| sleep(1); |
| LOGI(GSW_WIFI,"%s - end.\n",__func__); |
| return GSW_HAL_SUCCESS; |
| } |
| |
| int gsw_wifi_sta_disconnect(char *ssid) |
| { |
| return GSW_HAL_SUCCESS; |
| } |