blob: e4237bcc4fe567791f099b27c47da0159662fc97 [file] [log] [blame]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h> // Include signal header
#include "gsw_wifi_interface_sdk.h"
// Hostapd 和 wpa_supplicant 进程 ID
pid_t hostapd_pid = 0;
pid_t wpa_pid = 0;
// 辅助函数:将认证类型枚举转换为字符串
const char *auth_type_to_string(gsw_wifi_auth_e auth)
{
switch (auth)
{
case GSW_WIFI_AUTH_OPEN:
return "OPEN";
case GSW_WIFI_AUTH_WEP:
return "WEP";
case GSW_WIFI_AUTH_WPA_PSK:
return "WPA_PSK";
case GSW_WIFI_AUTH_WPA2_PSK:
return "WPA2_PSK";
case GSW_WIFI_AUTH_WPA3_PSK:
return "WPA3_PSK";
// 若有其他认证类型,可继续添加 case
default:
return "Unknown";
}
}
void handle_ctrl_c(int sig)
{
printf("\nExiting...\n");
gsw_wifi_sta_stop();
gsw_wifi_ap_stop();
gsw_wifi_disable();
exit(0);
}
// 添加AP回调函数
void ap_event_callback(void *arg, gsw_wifi_ap_status_e status)
{
if (status == GSW_WIFI_STATUS_CONNECT)
{
printf("AP mode is now running and ready for connections\n");
}
else if (status == GSW_WIFI_STATUS_DISCONNECT)
{
printf("AP mode has been stopped\n");
}
}
void ap_setting(void)
{
int ret = 0;
char ssid[33] = "mtbk-test";
char password[64] = "12345678";
gsw_wifi_auth_e auth = GSW_WIFI_AUTH_WPA2_PSK;
int frequency = 2;
gsw_wifi_bandwidth_type_e bandwidth = GSW_WIFI_BANDWIDTH_HT80;
int channel = 0;
int temp_auth, temp_bandwidth;
printf("AP mode configuration:\n");
printf("Enter SSID (default: mtbk-test): ");
if (scanf("%32s", ssid) != 1)
{
strcpy(ssid, "mtbk-test");
}
ret = gsw_wifi_ap_ssid_set(ssid);
if (ret != 0)
{
printf("Failed to set SSID\n");
gsw_wifi_ap_ssid_set("mtbk-test");
}
printf("Enter password (default: 12345678): ");
if (scanf("%63s", password) != 1)
{
strcpy(password, "12345678");
}
ret = gsw_wifi_ap_password_set(password);
if (ret != 0)
{
printf("Failed to set password\n");
gsw_wifi_ap_password_set("12345678");
}
printf("Enter auth type (0: OPEN, 1: WEP(Only support open), 2: WPA_PSK(wifi4 and above are not supported), 3: WPA2_PSK, 4: WPA3_PSK, default: WPA2_PSK): ");
if (scanf("%d", &temp_auth) == 1)
{
auth = (gsw_wifi_auth_e)temp_auth;
}
ret = gsw_wifi_ap_auth_set(auth);
if (ret != 0)
{
printf("Failed to set auth type\n");
gsw_wifi_ap_auth_set(GSW_WIFI_AUTH_WPA2_PSK);
}
printf("Enter frequency (1: 2.4G, 2: 5G, default: 2): ");
if (scanf("%d", &frequency) != 1)
{
frequency = 2;
}
ret = gsw_wifi_ap_frequency_set(frequency);
if (ret != 0)
{
printf("Failed to set frequency\n");
gsw_wifi_ap_frequency_set(2);
}
printf("Enter bandwidth (0: HT10, 1: HT20, 2: HT40, 3: HT80, default: HT40): ");
if (scanf("%d", &temp_bandwidth) == 1)
{
bandwidth = (gsw_wifi_bandwidth_type_e)temp_bandwidth;
}
ret = gsw_wifi_ap_bandwidth_set(bandwidth);
if (ret != 0)
{
printf("Failed to set bandwidth\n");
gsw_wifi_ap_bandwidth_set(GSW_WIFI_BANDWIDTH_HT40);
}
printf("Enter channel (0: auto, 2.4G: 1-13, 5G: 36-165, default: 0): ");
if (scanf("%d", &channel) != 1)
{
channel = 0;
}
ret = gsw_wifi_ap_channel_set(channel);
if (ret != 0)
{
printf("Failed to set channel\n");
gsw_wifi_ap_channel_set(0);
}
printf("AP mode configuration set.\n");
}
int main()
{
signal(SIGINT, handle_ctrl_c); // Register signal handler
int ret = 0;
gsw_wifi_ap_run_status_e ap_status;
// 加载 Wi-Fi 驱动并启动 AP 模式
gsw_wifi_sdk_interface_init();
if (gsw_wifi_enable() != 0)
{
printf("Failed to enable Wi-Fi driver\n");
return -1;
}
gsw_wifi_get_ap_status(&ap_status);
printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
begin:
printf("Do you want to set default AP mode? (y/n): ");
char ap_seting;
if (scanf(" %c", &ap_seting) != 1)
{
printf("Error reading input\n");
return -1;
}
if (ap_seting == 'y' || ap_seting == 'Y')
{
gsw_wifi_ap_ssid_set("mtbk-测试");
gsw_wifi_ap_password_set("12345678");
gsw_wifi_ap_auth_set(GSW_WIFI_AUTH_WPA2_PSK);
gsw_wifi_ap_frequency_set(2);
gsw_wifi_ap_bandwidth_set(GSW_WIFI_BANDWIDTH_HT40);
gsw_wifi_ap_channel_set(0);
}
else if (ap_seting == 'n' || ap_seting == 'N')
{
ap_setting();
}
else
{
printf("Invalid input\n");
return -1;
}
if (gsw_wifi_ap_start() != 0)
{
printf("Failed to start AP mode\n");
goto begin;
}
// 注册AP回调
if (gsw_wifi_reg_ap_event_callback(NULL, ap_event_callback) != 0)
{
printf("Failed to register AP event callback\n");
return -1;
}
gsw_wifi_get_ap_status(&ap_status);
printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
printf("Wi-Fi driver loaded and AP mode started.\n");
while (1)
{
char input[64];
printf("\nEnter command (1: start STA; 2: scan hotspots; 3: connect to hotspot; 4: ap get; q: exit; ap-status: check ap status; restart-ap .) ");
if (scanf("%63s", input) != 1)
{ // Check scanf return value
printf("Error reading input\n");
break;
}
if (strcmp(input, "q") == 0)
{
break;
}
else if (strcmp(input, "ap-status") == 0)
{
gsw_wifi_get_ap_status(&ap_status);
printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
}
else if (strcmp(input, "ap-stop") == 0)
{
printf("stop AP...\n");
gsw_wifi_ap_stop();
}
else if (strcmp(input, "ap-mac") == 0)
{
char tmpmac[18U];
unsigned char apmac[18U];
gsw_wifi_get_interface_mac(GSW_WIFI_INTERFACE_1, tmpmac);
sscanf(tmpmac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &apmac[0], &apmac[1], &apmac[2], &apmac[3], &apmac[4], &apmac[5]);
printf("connect ap mac: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", apmac[0], apmac[1], apmac[2], apmac[3], apmac[4], apmac[5]);
}
else if (strcmp(input, "sta-mac") == 0)
{
char tmpmac[18U];
unsigned char sta_mac[18U];
gsw_wifi_get_interface_mac(GSW_WIFI_INTERFACE_0, tmpmac);
sscanf(tmpmac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &sta_mac[0], &sta_mac[1], &sta_mac[2], &sta_mac[3], &sta_mac[4], &sta_mac[5]);
printf("connect sta mac: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", sta_mac[0], sta_mac[1], sta_mac[2], sta_mac[3], sta_mac[4], sta_mac[5]);
}
else if (strcmp(input, "ap-start") == 0)
{
printf("y/Y: no set ; other: reset\n");
if (scanf(" %c", &ap_seting) != 1)
{
printf("Error reading input\n");
}
if (ap_seting == 'y' || ap_seting == 'Y')
{
printf("ap is running, no need to set\n");
}
else
{
ap_setting();
}
gsw_wifi_ap_start();
gsw_wifi_get_ap_status(&ap_status);
printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
}
else if (strcmp(input, "restart-ap") == 0)
{
printf("Restarting AP...\n");
ap_setting();
gsw_wifi_ap_restart();
gsw_wifi_get_ap_status(&ap_status);
printf("ap status: %s\n", ap_status == GSW_WIFI_AP_STATUS_ENABLE ? "enable" : "disable");
}
else if (strcmp(input, "1") == 0)
{
// 启动 STA 模式
if (gsw_wifi_sta_start() != 0)
{
printf("Failed to start STA mode\n");
}
else
{
printf("STA mode started.\n");
}
}
else if (strcmp(input, "2") == 0)
{
// 搜索热点
gsw_scan_info_s scan_list[100];
int list_len;
if (gsw_wifi_get_scan_list(scan_list, 100, &list_len) == 0)
{
printf("Available hotspots:\n");
for (int i = 0; i < list_len; i++)
{
const char *auth_str = auth_type_to_string(scan_list[i].auth);
printf("%d. SSID: %s, BSSID: %s, RSSI: %d, Auth: %s\n", i + 1, scan_list[i].ssid, scan_list[i].mac, scan_list[i].rssi, auth_str);
}
}
else
{
printf("Failed to scan for hotspots\n");
}
}
else if (strcmp(input, "3") == 0)
{
// 输入对应的账户和密码进行连接,从扫描结果获取认证方式
gsw_scan_info_s scan_list[100];
int list_len;
if (gsw_wifi_get_scan_list(scan_list, 100, &list_len) == 0)
{
printf("Available hotspots:\n");
for (int i = 0; i < list_len; i++)
{
const char *auth_str = auth_type_to_string(scan_list[i].auth);
printf("%d. SSID: %s, BSSID: %s, RSSI: %d, Auth: %s\n", i + 1, scan_list[i].ssid, scan_list[i].mac, scan_list[i].rssi, auth_str);
}
int choice;
printf("Enter the number of the hotspot to connect: ");
if (scanf("%d", &choice) != 1)
{ // Check scanf return value
printf("Error reading input\n");
continue;
}
if (choice < 1 || choice > list_len)
{
printf("Invalid choice\n");
continue;
}
// 直接从扫描结果中获取认证方式
gsw_wifi_auth_e auth = scan_list[choice - 1].auth;
const char *auth_str = auth_type_to_string(auth);
printf("Got authentication type from scan result: %s\n", auth_str);
char password[64] = ""; // 默认空密码
if (auth != GSW_WIFI_AUTH_OPEN)
{
printf("Enter password for SSID %s: ", scan_list[choice - 1].ssid);
if (scanf("%63s", password) != 1)
{
printf("Error reading input\n");
continue;
}
}
else
{
printf("Open network, skipping password input.\n");
}
if (gsw_wifi_sta_connect(scan_list[choice - 1].ssid, auth, password) != 0)
{
printf("Failed to connect to SSID %s\n", scan_list[choice - 1].ssid);
}
else
{
printf("Attempting to connect to SSID %s\n", scan_list[choice - 1].ssid);
// 等待连接成功
sleep(5);
// 获取连接的 SSID
char connected_ssid[33];
if (gsw_wifi_get_sta_ssid(connected_ssid) == 0)
{
printf("Connected to SSID: %s\n", connected_ssid);
}
else
{
printf("Failed to connect\n");
}
}
}
else
{
printf("Failed to scan for hotspots\n");
}
}
else if (strcmp(input, "4") == 0)
{
// 获取AP相关信息并格式化输出
printf("\nAP Configuration:\n");
printf("-----------------\n");
char ssid[33];
if (gsw_wifi_ap_ssid_get(ssid) == 0)
{
printf("SSID: %-20s\n", ssid);
}
else
{
printf("SSID: Failed to get\n");
}
int frequency;
if (gsw_wifi_ap_frequency_get(&frequency) == 0)
{
printf("Frequency: %-15s\n", frequency == 1 ? "2.4GHz" : "5GHz");
}
else
{
printf("Frequency: Failed to get\n");
}
gsw_wifi_bandwidth_type_e bandwidth;
if (gsw_wifi_ap_bandwidth_get(&bandwidth) == 0)
{
const char *bw_str[] = {"10MHz", "20MHz", "40MHz", "80MHz"};
printf("Bandwidth: %-15s\n", bandwidth >= 0 && bandwidth <= 3 ? bw_str[bandwidth] : "Unknown");
}
else
{
printf("Bandwidth: Failed to get\n");
}
int channel;
if (gsw_wifi_ap_channel_get(&channel) == 0)
{
printf("Channel: %-18d\n", channel);
}
else
{
printf("Channel: Failed to get\n");
}
gsw_wifi_auth_e auth;
if (gsw_wifi_ap_auth_get(&auth) == 0)
{
printf("Authentication: %-10s\n", auth_type_to_string(auth));
}
else
{
printf("Authentication: Failed to get\n");
}
char password[65];
if (gsw_wifi_ap_password_get(password) == 0)
{
printf("Password: %-18s\n", password);
}
else
{
printf("Password: Failed to get\n");
}
printf("-----------------\n");
}
else
{
printf("Invalid command\n");
}
}
// 停止 AP 和 STA 模式,卸载 Wi-Fi 驱动
gsw_wifi_ap_stop();
gsw_wifi_sta_stop();
gsw_wifi_disable();
printf("Wi-Fi driver unloaded and AP/STA modes stopped.\n");
return 0;
}