|
@@ -2,7 +2,7 @@
|
|
* File : wlan_cmd.c
|
|
* File : wlan_cmd.c
|
|
* Wi-Fi common commands
|
|
* Wi-Fi common commands
|
|
* This file is part of RT-Thread RTOS
|
|
* This file is part of RT-Thread RTOS
|
|
- * COPYRIGHT (C) 2006 - 2016, RT-Thread Development Team
|
|
|
|
|
|
+ * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
|
*
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* it under the terms of the GNU General Public License as published by
|
|
@@ -40,6 +40,13 @@ static char wifi_ssid[32] = {0};
|
|
static char wifi_key[32] = {0};
|
|
static char wifi_key[32] = {0};
|
|
static int network_mode = WIFI_STATION;
|
|
static int network_mode = WIFI_STATION;
|
|
|
|
|
|
|
|
+#define WLAN_DEBUG 1
|
|
|
|
+#if WLAN_DEBUG
|
|
|
|
+#define WLAN_DBG(...) rt_kprintf("[WLAN]"),rt_kprintf(__VA_ARGS__)
|
|
|
|
+#else
|
|
|
|
+#define WLAN_DBG(...)
|
|
|
|
+#endif
|
|
|
|
+
|
|
#ifndef WIFI_SETTING_FN
|
|
#ifndef WIFI_SETTING_FN
|
|
#define WIFI_SETTING_FN "/appfs/setting.json"
|
|
#define WIFI_SETTING_FN "/appfs/setting.json"
|
|
#endif
|
|
#endif
|
|
@@ -69,7 +76,7 @@ int wifi_set_mode(int mode)
|
|
return network_mode;
|
|
return network_mode;
|
|
}
|
|
}
|
|
|
|
|
|
-int wifi_set_setting(const char* ssid, const char* pwd)
|
|
|
|
|
|
+int wifi_set_setting(const char *ssid, const char *pwd)
|
|
{
|
|
{
|
|
if (!ssid) return -1;
|
|
if (!ssid) return -1;
|
|
|
|
|
|
@@ -87,13 +94,13 @@ int wifi_set_setting(const char* ssid, const char* pwd)
|
|
}
|
|
}
|
|
|
|
|
|
#ifdef PKG_USING_CJSON
|
|
#ifdef PKG_USING_CJSON
|
|
-int wifi_read_cfg(const char* filename)
|
|
|
|
|
|
+int wifi_read_cfg(const char *filename)
|
|
{
|
|
{
|
|
int fd;
|
|
int fd;
|
|
cJSON *json = RT_NULL;
|
|
cJSON *json = RT_NULL;
|
|
|
|
|
|
- fd = open(filename,O_RDONLY, 0);
|
|
|
|
- if(fd < 0)
|
|
|
|
|
|
+ fd = open(filename, O_RDONLY, 0);
|
|
|
|
+ if (fd < 0)
|
|
{
|
|
{
|
|
/* no setting file */
|
|
/* no setting file */
|
|
return -1;
|
|
return -1;
|
|
@@ -106,7 +113,7 @@ int wifi_read_cfg(const char* filename)
|
|
length = lseek(fd, 0, SEEK_END);
|
|
length = lseek(fd, 0, SEEK_END);
|
|
if (length)
|
|
if (length)
|
|
{
|
|
{
|
|
- char *json_str = (char *) rt_malloc (length);
|
|
|
|
|
|
+ char *json_str = (char *) rt_malloc(length);
|
|
if (json_str)
|
|
if (json_str)
|
|
{
|
|
{
|
|
lseek(fd, 0, SEEK_SET);
|
|
lseek(fd, 0, SEEK_SET);
|
|
@@ -149,7 +156,7 @@ int wifi_read_cfg(const char* filename)
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
-int wifi_save_cfg(const char* filename)
|
|
|
|
|
|
+int wifi_save_cfg(const char *filename)
|
|
{
|
|
{
|
|
int fd;
|
|
int fd;
|
|
cJSON *json = RT_NULL;
|
|
cJSON *json = RT_NULL;
|
|
@@ -162,7 +169,7 @@ int wifi_save_cfg(const char* filename)
|
|
length = lseek(fd, 0, SEEK_END);
|
|
length = lseek(fd, 0, SEEK_END);
|
|
if (length)
|
|
if (length)
|
|
{
|
|
{
|
|
- char *json_str = (char *) rt_malloc (length);
|
|
|
|
|
|
+ char *json_str = (char *) rt_malloc(length);
|
|
if (json_str)
|
|
if (json_str)
|
|
{
|
|
{
|
|
lseek(fd, 0, SEEK_SET);
|
|
lseek(fd, 0, SEEK_SET);
|
|
@@ -244,9 +251,9 @@ int wifi_save_cfg(const char* filename)
|
|
|
|
|
|
int wifi_save_setting(void)
|
|
int wifi_save_setting(void)
|
|
{
|
|
{
|
|
- #ifdef PKG_USING_CJSON
|
|
|
|
|
|
+#ifdef PKG_USING_CJSON
|
|
wifi_save_cfg(WIFI_SETTING_FN);
|
|
wifi_save_cfg(WIFI_SETTING_FN);
|
|
- #endif
|
|
|
|
|
|
+#endif
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
@@ -265,7 +272,7 @@ int wifi_softap_setup_netif(struct netif *netif)
|
|
{
|
|
{
|
|
char name[8];
|
|
char name[8];
|
|
memset(name, 0, sizeof(name));
|
|
memset(name, 0, sizeof(name));
|
|
- strncpy(name, netif->name, sizeof(name)>sizeof(netif->name)? sizeof(netif->name) : sizeof(name));
|
|
|
|
|
|
+ strncpy(name, netif->name, sizeof(name) > sizeof(netif->name) ? sizeof(netif->name) : sizeof(name));
|
|
dhcpd_start(name);
|
|
dhcpd_start(name);
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|
|
@@ -279,15 +286,15 @@ int wifi_default(void)
|
|
int result = 0;
|
|
int result = 0;
|
|
struct rt_wlan_device *wlan;
|
|
struct rt_wlan_device *wlan;
|
|
|
|
|
|
- #ifdef PKG_USING_CJSON
|
|
|
|
|
|
+#ifdef PKG_USING_CJSON
|
|
/* read default setting for wifi */
|
|
/* read default setting for wifi */
|
|
wifi_read_cfg(WIFI_SETTING_FN);
|
|
wifi_read_cfg(WIFI_SETTING_FN);
|
|
- #endif
|
|
|
|
|
|
+#endif
|
|
|
|
|
|
if (network_mode == WIFI_STATION)
|
|
if (network_mode == WIFI_STATION)
|
|
{
|
|
{
|
|
/* get wlan device */
|
|
/* get wlan device */
|
|
- wlan = (struct rt_wlan_device*)rt_device_find(WIFI_DEVICE_STA_NAME);
|
|
|
|
|
|
+ wlan = (struct rt_wlan_device *)rt_device_find(WIFI_DEVICE_STA_NAME);
|
|
if (!wlan)
|
|
if (!wlan)
|
|
{
|
|
{
|
|
rt_kprintf("no wlan:%s device\n", WIFI_DEVICE_STA_NAME);
|
|
rt_kprintf("no wlan:%s device\n", WIFI_DEVICE_STA_NAME);
|
|
@@ -296,7 +303,7 @@ int wifi_default(void)
|
|
|
|
|
|
/* wifi station */
|
|
/* wifi station */
|
|
rt_wlan_info_init(&info, WIFI_STATION, SECURITY_WPA2_MIXED_PSK, wifi_ssid);
|
|
rt_wlan_info_init(&info, WIFI_STATION, SECURITY_WPA2_MIXED_PSK, wifi_ssid);
|
|
- result =rt_wlan_init(wlan, WIFI_STATION);
|
|
|
|
|
|
+ result = rt_wlan_init(wlan, WIFI_STATION);
|
|
if (result == RT_EOK)
|
|
if (result == RT_EOK)
|
|
{
|
|
{
|
|
result = rt_wlan_connect(wlan, &info, wifi_key);
|
|
result = rt_wlan_connect(wlan, &info, wifi_key);
|
|
@@ -306,7 +313,7 @@ int wifi_default(void)
|
|
{
|
|
{
|
|
/* wifi AP */
|
|
/* wifi AP */
|
|
/* get wlan device */
|
|
/* get wlan device */
|
|
- wlan = (struct rt_wlan_device*)rt_device_find(WIFI_DEVICE_AP_NAME);
|
|
|
|
|
|
+ wlan = (struct rt_wlan_device *)rt_device_find(WIFI_DEVICE_AP_NAME);
|
|
if (!wlan)
|
|
if (!wlan)
|
|
{
|
|
{
|
|
rt_kprintf("no wlan:%s device\n", WIFI_DEVICE_AP_NAME);
|
|
rt_kprintf("no wlan:%s device\n", WIFI_DEVICE_AP_NAME);
|
|
@@ -317,7 +324,7 @@ int wifi_default(void)
|
|
info.channel = 11;
|
|
info.channel = 11;
|
|
|
|
|
|
/* wifi soft-AP */
|
|
/* wifi soft-AP */
|
|
- result =rt_wlan_init(wlan, WIFI_AP);
|
|
|
|
|
|
+ result = rt_wlan_init(wlan, WIFI_AP);
|
|
if (result == RT_EOK)
|
|
if (result == RT_EOK)
|
|
{
|
|
{
|
|
result = rt_wlan_softap(wlan, &info, wifi_key);
|
|
result = rt_wlan_softap(wlan, &info, wifi_key);
|
|
@@ -334,14 +341,14 @@ static void wifi_usage(void)
|
|
rt_kprintf("wifi - Do the default wifi action\n");
|
|
rt_kprintf("wifi - Do the default wifi action\n");
|
|
rt_kprintf("wifi wlan_dev scan\n");
|
|
rt_kprintf("wifi wlan_dev scan\n");
|
|
rt_kprintf("wifi wlan_dev join SSID PASSWORD\n");
|
|
rt_kprintf("wifi wlan_dev join SSID PASSWORD\n");
|
|
- rt_kprintf("wifi wlan_dev ap SSID [PASSWORD]\n");
|
|
|
|
|
|
+ rt_kprintf("wifi wlan_dev ap SSID [PASSWORD]\n");
|
|
rt_kprintf("wifi wlan_dev up\n");
|
|
rt_kprintf("wifi wlan_dev up\n");
|
|
rt_kprintf("wifi wlan_dev down\n");
|
|
rt_kprintf("wifi wlan_dev down\n");
|
|
rt_kprintf("wifi wlan_dev rssi\n");
|
|
rt_kprintf("wifi wlan_dev rssi\n");
|
|
rt_kprintf("wifi wlan_dev status\n");
|
|
rt_kprintf("wifi wlan_dev status\n");
|
|
}
|
|
}
|
|
|
|
|
|
-int wifi(int argc, char** argv)
|
|
|
|
|
|
+int wifi(int argc, char **argv)
|
|
{
|
|
{
|
|
struct rt_wlan_device *wlan;
|
|
struct rt_wlan_device *wlan;
|
|
|
|
|
|
@@ -368,15 +375,15 @@ int wifi(int argc, char** argv)
|
|
|
|
|
|
network_mode = WIFI_STATION;
|
|
network_mode = WIFI_STATION;
|
|
|
|
|
|
- #ifdef PKG_USING_CJSON
|
|
|
|
|
|
+#ifdef PKG_USING_CJSON
|
|
wifi_save_cfg(WIFI_SETTING_FN);
|
|
wifi_save_cfg(WIFI_SETTING_FN);
|
|
- #endif
|
|
|
|
|
|
+#endif
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
/* get wlan device */
|
|
/* get wlan device */
|
|
- wlan = (struct rt_wlan_device*)rt_device_find(argv[1]);
|
|
|
|
|
|
+ wlan = (struct rt_wlan_device *)rt_device_find(argv[1]);
|
|
if (!wlan)
|
|
if (!wlan)
|
|
{
|
|
{
|
|
rt_kprintf("no wlan:%s device\n", argv[1]);
|
|
rt_kprintf("no wlan:%s device\n", argv[1]);
|
|
@@ -397,6 +404,7 @@ int wifi(int argc, char** argv)
|
|
/* TODO: use easy-join to replace */
|
|
/* TODO: use easy-join to replace */
|
|
rt_wlan_info_init(&info, WIFI_STATION, SECURITY_WPA2_MIXED_PSK, argv[3]);
|
|
rt_wlan_info_init(&info, WIFI_STATION, SECURITY_WPA2_MIXED_PSK, argv[3]);
|
|
rt_wlan_connect(wlan, &info, argv[4]);
|
|
rt_wlan_connect(wlan, &info, argv[4]);
|
|
|
|
+ rt_wlan_info_deinit(&info);
|
|
}
|
|
}
|
|
else if (strcmp(argv[2], "up") == 0)
|
|
else if (strcmp(argv[2], "up") == 0)
|
|
{
|
|
{
|
|
@@ -406,36 +414,28 @@ int wifi(int argc, char** argv)
|
|
else if (strcmp(argv[2], "down") == 0)
|
|
else if (strcmp(argv[2], "down") == 0)
|
|
{
|
|
{
|
|
rt_wlan_disconnect(wlan);
|
|
rt_wlan_disconnect(wlan);
|
|
|
|
+ rt_wlan_info_deinit(&info);
|
|
}
|
|
}
|
|
else if (strcmp(argv[2], "scan") == 0)
|
|
else if (strcmp(argv[2], "scan") == 0)
|
|
{
|
|
{
|
|
- struct rt_wlan_info *infos;
|
|
|
|
|
|
+ struct rt_wlan_scan_result *scan_result = RT_NULL;
|
|
|
|
|
|
- infos = (struct rt_wlan_info*)rt_malloc(sizeof(struct rt_wlan_info) * 12);
|
|
|
|
- if (infos)
|
|
|
|
|
|
+ rt_wlan_scan(wlan, &scan_result);
|
|
|
|
+ if (scan_result)
|
|
{
|
|
{
|
|
int index, num;
|
|
int index, num;
|
|
|
|
|
|
- memset(infos, 0x0, sizeof(struct rt_wlan_info) * 12);
|
|
|
|
- num = rt_wlan_scan(wlan, infos, 12);
|
|
|
|
-
|
|
|
|
- for (index = 0; index < num; index ++)
|
|
|
|
- {
|
|
|
|
- rt_kprintf("----Wi-Fi AP[%d] Information----\n", index);
|
|
|
|
- rt_kprintf("SSID: %-.32s\n", infos[index].ssid);
|
|
|
|
- rt_kprintf("rssi: %d\n", infos[index].rssi);
|
|
|
|
- rt_kprintf(" chn: %d\n", infos[index].channel);
|
|
|
|
- rt_kprintf("rate: %d\n", infos[index].datarate);
|
|
|
|
- rt_kprintf("\n");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /* de-initialize info */
|
|
|
|
|
|
+ num = scan_result->ap_num;
|
|
|
|
+ rt_kprintf("----Wi-Fi APInformation----\n");
|
|
for (index = 0; index < num; index ++)
|
|
for (index = 0; index < num; index ++)
|
|
{
|
|
{
|
|
- rt_wlan_info_deinit(&infos[index]);
|
|
|
|
|
|
+ rt_kprintf("SSID:%-.32s, ", scan_result->ap_table[index].ssid);
|
|
|
|
+ rt_kprintf("rssi:%d, ", scan_result->ap_table[index].rssi);
|
|
|
|
+ rt_kprintf("chn:%d, ", scan_result->ap_table[index].channel);
|
|
|
|
+ rt_kprintf("rate:%d\n", scan_result->ap_table[index].datarate);
|
|
}
|
|
}
|
|
- rt_free(infos);
|
|
|
|
}
|
|
}
|
|
|
|
+ rt_wlan_release_scan_result(&scan_result);
|
|
}
|
|
}
|
|
else if (strcmp(argv[2], "rssi") == 0)
|
|
else if (strcmp(argv[2], "rssi") == 0)
|
|
{
|
|
{
|
|
@@ -454,7 +454,7 @@ int wifi(int argc, char** argv)
|
|
rt_wlan_info_init(&info, WIFI_AP, SECURITY_OPEN, argv[3]);
|
|
rt_wlan_info_init(&info, WIFI_AP, SECURITY_OPEN, argv[3]);
|
|
info.channel = 11;
|
|
info.channel = 11;
|
|
|
|
|
|
- result =rt_wlan_init(wlan, WIFI_AP);
|
|
|
|
|
|
+ result = rt_wlan_init(wlan, WIFI_AP);
|
|
/* start soft ap */
|
|
/* start soft ap */
|
|
result = rt_wlan_softap(wlan, &info, NULL);
|
|
result = rt_wlan_softap(wlan, &info, NULL);
|
|
if (result == RT_EOK)
|
|
if (result == RT_EOK)
|
|
@@ -468,19 +468,19 @@ int wifi(int argc, char** argv)
|
|
rt_wlan_info_init(&info, WIFI_AP, SECURITY_WPA2_AES_PSK, argv[3]);
|
|
rt_wlan_info_init(&info, WIFI_AP, SECURITY_WPA2_AES_PSK, argv[3]);
|
|
info.channel = 11;
|
|
info.channel = 11;
|
|
|
|
|
|
- result =rt_wlan_init(wlan, WIFI_AP);
|
|
|
|
|
|
+ result = rt_wlan_init(wlan, WIFI_AP);
|
|
/* start soft ap */
|
|
/* start soft ap */
|
|
result = rt_wlan_softap(wlan, &info, argv[4]);
|
|
result = rt_wlan_softap(wlan, &info, argv[4]);
|
|
if (result == RT_EOK)
|
|
if (result == RT_EOK)
|
|
{
|
|
{
|
|
network_mode = WIFI_AP;
|
|
network_mode = WIFI_AP;
|
|
- }
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
wifi_usage();
|
|
wifi_usage();
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
if (result != RT_EOK)
|
|
if (result != RT_EOK)
|
|
{
|
|
{
|
|
rt_kprintf("wifi start failed! result=%d\n", result);
|
|
rt_kprintf("wifi start failed! result=%d\n", result);
|
|
@@ -496,13 +496,13 @@ int wifi(int argc, char** argv)
|
|
|
|
|
|
rt_kprintf("Wi-Fi AP: %-.32s\n", wlan->info->ssid);
|
|
rt_kprintf("Wi-Fi AP: %-.32s\n", wlan->info->ssid);
|
|
rt_kprintf("MAC Addr: %02x:%02x:%02x:%02x:%02x:%02x\n", wlan->info->bssid[0],
|
|
rt_kprintf("MAC Addr: %02x:%02x:%02x:%02x:%02x:%02x\n", wlan->info->bssid[0],
|
|
- wlan->info->bssid[1],
|
|
|
|
- wlan->info->bssid[2],
|
|
|
|
- wlan->info->bssid[3],
|
|
|
|
- wlan->info->bssid[4],
|
|
|
|
- wlan->info->bssid[5]);
|
|
|
|
|
|
+ wlan->info->bssid[1],
|
|
|
|
+ wlan->info->bssid[2],
|
|
|
|
+ wlan->info->bssid[3],
|
|
|
|
+ wlan->info->bssid[4],
|
|
|
|
+ wlan->info->bssid[5]);
|
|
rt_kprintf(" Channel: %d\n", wlan->info->channel);
|
|
rt_kprintf(" Channel: %d\n", wlan->info->channel);
|
|
- rt_kprintf("DataRate: %dMbps\n", wlan->info->datarate/1000);
|
|
|
|
|
|
+ rt_kprintf("DataRate: %dMbps\n", wlan->info->datarate / 1000);
|
|
rt_kprintf(" RSSI: %d\n", rssi);
|
|
rt_kprintf(" RSSI: %d\n", rssi);
|
|
}
|
|
}
|
|
else
|
|
else
|