Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 91 additions & 2 deletions components/drivers/wlan/dev_wlan_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Date Author Notes
* 2018-08-13 tyx the first version
* 2024-03-24 Evlers fixed a duplicate issue with the wifi scan command
* 2026-08-11 Kai fixed silent failure of wifi cmd when wlan device mode is not bound
*/

#include <rtthread.h>
Expand Down Expand Up @@ -88,6 +89,39 @@ static const struct wifi_cmd_des debug_tab[] = {
};
#endif

/* Check that the STA/AP device is bound to its mode before issuing
* commands that would otherwise fail silently at the mgnt layer.
* The mode can only be bound from the shell when RT_WLAN_CMD_DEBUG
* is enabled ('wifi -d mode ...'); otherwise tell the user to bind
* it in code via rt_wlan_set_mode(). */
static int wifi_check_sta_mode(void)
{
if (rt_wlan_get_mode(RT_WLAN_DEVICE_STA_NAME) != RT_WLAN_STATION)
{
#ifdef RT_WLAN_CMD_DEBUG
LOG_E("sta mode not set, run 'wifi -d mode sta wlan0' first!");
#else
LOG_E("sta mode not set, bind it in code: rt_wlan_set_mode(RT_WLAN_DEVICE_STA_NAME, RT_WLAN_STATION)!");
#endif
return -1;
}
return 0;
}

static int wifi_check_ap_mode(void)
{
if (rt_wlan_get_mode(RT_WLAN_DEVICE_AP_NAME) != RT_WLAN_AP)
{
#ifdef RT_WLAN_CMD_DEBUG
LOG_E("ap mode not set, run 'wifi -d mode ap wlan1' first!");
#else
LOG_E("ap mode not set, bind it in code: rt_wlan_set_mode(RT_WLAN_DEVICE_AP_NAME, RT_WLAN_AP)!");
#endif
return -1;
}
return 0;
}

static int wifi_help(int argc, char *argv[])
{
rt_kprintf("wifi\n");
Expand All @@ -111,7 +145,9 @@ static int wifi_status(int argc, char *argv[])
struct rt_wlan_info info;

if (argc > 2)
{
return -1;
}

if (rt_wlan_is_connected() == 1)
{
Expand Down Expand Up @@ -200,7 +236,9 @@ static rt_err_t wifi_scan_result_cache(struct rt_wlan_info *info)
rt_base_t level;

if ((info == RT_NULL) || (info->ssid.len == 0))
{
return -RT_EINVAL;
}

LOG_D("ssid:%s len:%d mac:%02x:%02x:%02x:%02x:%02x:%02x", info->ssid.val, info->ssid.len,
info->bssid[0], info->bssid[1], info->bssid[2], info->bssid[3], info->bssid[4], info->bssid[5]);
Expand Down Expand Up @@ -274,10 +312,14 @@ static rt_err_t wifi_scan_result_cache(struct rt_wlan_info *info)

/* Insert the end */
if (insert == -1)
{
insert = scan_result.num;
}

if (scan_result.num >= RT_WLAN_SCAN_CACHE_NUM)
{
return RT_EOK;
}

/* malloc memory */
ptable = rt_malloc(sizeof(struct rt_wlan_info) * (scan_result.num + 1));
Expand Down Expand Up @@ -425,7 +467,9 @@ static int wifi_scan(int argc, char *argv[])
int i = 0;

if (argc > 3)
{
return -1;
}

if (argc == 3)
{
Expand All @@ -434,6 +478,11 @@ static int wifi_scan(int argc, char *argv[])
info = &filter;
}

if (wifi_check_sta_mode() != 0)
{
return 0;
}

ret = rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT, user_ap_info_callback, &i);
if (ret != RT_EOK)
{
Expand Down Expand Up @@ -470,6 +519,10 @@ static int wifi_join(int argc, char *argv[])
struct rt_wlan_cfg_info cfg_info;

rt_memset(&cfg_info, 0, sizeof(cfg_info));
if (wifi_check_sta_mode() != 0)
{
return 0;
}
if (argc == 2)
{
#ifdef RT_WLAN_CFG_ENABLE
Expand All @@ -478,7 +531,9 @@ static int wifi_join(int argc, char *argv[])
{
ssid = (char *)(&cfg_info.info.ssid.val[0]);
if (cfg_info.key.len)
{
key = (char *)(&cfg_info.key.val[0]);
}
}
else
#endif
Expand Down Expand Up @@ -509,6 +564,7 @@ static int wifi_ap(int argc, char *argv[])
{
const char *ssid = RT_NULL;
const char *key = RT_NULL;
rt_err_t err;

if (argc == 3)
{
Expand All @@ -524,7 +580,15 @@ static int wifi_ap(int argc, char *argv[])
return -1;
}

rt_wlan_start_ap(ssid, key);
if (wifi_check_ap_mode() != 0)
{
return 0;
}
err = rt_wlan_start_ap(ssid, key);
if (err != RT_EOK)
{
LOG_E("start ap failed:%d!", err);
}
return 0;
}

Expand All @@ -534,7 +598,13 @@ static int wifi_list_sta(int argc, char *argv[])
int num, i;

if (argc > 2)
{
return -1;
}
if (wifi_check_ap_mode() != 0)
{
return 0;
}
num = rt_wlan_ap_get_sta_num();
sta_info = rt_malloc(sizeof(struct rt_wlan_info) * num);
if (sta_info == RT_NULL)
Expand All @@ -561,18 +631,29 @@ static int wifi_disconnect(int argc, char *argv[])
return -1;
}

if (wifi_check_sta_mode() != 0)
{
return 0;
}

rt_wlan_disconnect();
return 0;
}

static int wifi_ap_stop(int argc, char *argv[])
{
rt_err_t err;

if (argc != 2)
{
return -1;
}

rt_wlan_ap_stop();
err = rt_wlan_ap_stop();
if (err != RT_EOK)
{
LOG_E("ap stop failed:%d!", err);
}
return 0;
}

Expand Down Expand Up @@ -695,7 +776,9 @@ static int wifi_debug_set_mode(int argc, char *argv[])
rt_wlan_mode_t mode;

if (argc != 3)
{
return -1;
}

if (rt_strcmp("sta", argv[1]) == 0)
{
Expand All @@ -710,7 +793,9 @@ static int wifi_debug_set_mode(int argc, char *argv[])
mode = RT_WLAN_NONE;
}
else
{
return -1;
}

rt_wlan_set_mode(argv[2], mode);
return 0;
Expand All @@ -737,9 +822,13 @@ static int wifi_debug_set_autoconnect(int argc, char *argv[])
if (argc == 2)
{
if (rt_strcmp(argv[1], "enable") == 0)
{
rt_wlan_config_autoreconnect(RT_TRUE);
}
else if (rt_strcmp(argv[1], "disable") == 0)
{
rt_wlan_config_autoreconnect(RT_FALSE);
}
}
else
{
Expand Down