W801_SDK_dev_env/app/nano_shell_command.c
2022-06-17 08:21:36 +02:00

246 lines
6.7 KiB
C

#include <stdlib.h>
#include <string.h>
#include "command/command.h"
#include "wm_include.h"
#include "FreeRTOS.h"
#include "task.h"
extern int shell_printf(const char *format, ...);
extern int wm_printf(const char *fmt,...);
void tls_wifi_client_event_cb(u8 *mac, enum tls_wifi_client_event_type event)
{
struct tls_sta_info_t *mac_addr = (struct tls_sta_info_t *)mac;
shell_printf("Client event(%d), MAC : %M\n", event, mac_addr);
}
void wifi_scan_result_cb(void)
{
u16 buffer_size = sizeof(struct tls_scan_bss_t) + sizeof(struct tls_bss_info_t) * 20;
u8 *buf = tls_mem_alloc(buffer_size);
if(buf == NULL)
{
shell_printf("Failed to allocate result buffer\n");
return;
}
struct tls_scan_bss_t *scan_result = (struct tls_scan_bss_t *)buf;
struct tls_bss_info_t *station_list = scan_result->bss;
tls_wifi_get_scan_rslt(buf, buffer_size);
shell_printf("Found %u nearby station(s) - info size(%u/%u)\n",
scan_result->count,
scan_result->length,
buffer_size);
for(u8 i = 0; i < scan_result->count; i++)
{
station_list[i].ssid[station_list[i].ssid_len] = '\0';
shell_printf("station %u :\nSSID : %s\nBSSID : %02X:%02X:%02X:%02X:%02X:%02X\nRSSI : %d dB\nChannel : %u\nMax DR : %u Mbps\nMode %u\nAuth :%u\nWPS supported : %u\n\n",
i,
(char *)station_list[i].ssid,
station_list[i].bssid[0], station_list[i].bssid[1], station_list[i].bssid[2], station_list[i].bssid[3], station_list[i].bssid[4], station_list[i].bssid[5],
(s8)station_list[i].rssi,
station_list[i].channel,
station_list[i].max_data_rate,
station_list[i].mode,
station_list[i].privacy,
station_list[i].wps_support);
}
tls_mem_free(buf);
}
int _task_list(const shell_cmd_t *pcmd, int argc, char *const argv[])
{
char *buf = NULL;
buf = tls_mem_alloc(1024);
if(NULL == buf)
return 0;
#if configUSE_TRACE_FACILITY
vTaskList((signed char *)buf);
#endif
shell_printf("\n%s\nbuf_len : %d\n", buf, strlen(buf));
tls_mem_free(buf);
buf = NULL;
return 0;
}
int _reset(const shell_cmd_t *pcmd, int argc, char *const argv[])
{
tls_sys_reset();
return 0;
}
int _soft_ap(const shell_cmd_t *pcmd, int argc, char *const argv[])
{
if(argc > 1)
{
if(strcmp(argv[1], "state") == 0)
{
shell_printf("SOFT AP state : %u\n", tls_wifi_softap_get_state());
}
else if(strcmp(argv[1], "create") == 0)
{
struct tls_softap_info_t ap_info;
struct tls_ip_info_t ip_info;
tls_wifi_disconnect();
strncpy((char *)ap_info.ssid, argv[2], 32);
ap_info.ssid[32] = '\0';
ap_info.encrypt = IEEE80211_ENCRYT_TKIP_WPA2;
ap_info.channel = 5;
ap_info.keyinfo.format = 1;
ap_info.keyinfo.index = 1;
ap_info.keyinfo.key_len = strlen(argv[3]);
strncpy((char *)ap_info.keyinfo.key, argv[3], 63);
ip_info.ip_addr[0] = 192;
ip_info.ip_addr[1] = 168;
ip_info.ip_addr[2] = 1;
ip_info.ip_addr[3] = 1;
ip_info.netmask[0] = 255;
ip_info.netmask[1] = 255;
ip_info.netmask[2] = 255;
ip_info.netmask[3] = 0;
ip_info.dnsname[0] = '\0';
int result = tls_wifi_softap_create(&ap_info, &ip_info);
shell_printf("Create AP with SSID : %s, key(%d) : %s -> %d\n", ap_info.ssid, ap_info.keyinfo.key_len, ap_info.keyinfo.key, result);
shell_printf("Registering client event callback\n");
tls_wifi_softap_client_event_register(&(tls_wifi_client_event_cb));
}
else if(strcmp(argv[1], "destroy") == 0)
{
//tls_wifi_softap_destroy();
tls_wifi_softap_client_event_register(NULL);
tls_wifi_disconnect();
shell_printf("Stopping SOFT AP\n");
}
else
{
shell_printf("Unknown soft_ap action\n");
}
}
else
{
shell_printf("List of soft_ap actions :\nstate\ncreate <SSID> <PWD>\ndestroy\n");
}
return 0;
}
int _station(const shell_cmd_t *pcmd, int argc, char *const argv[])
{
if(argc > 1)
{
if(strcmp(argv[1], "scan") == 0)
{
tls_wifi_scan_result_cb_register(&(wifi_scan_result_cb));
if(tls_wifi_scan() == WM_SUCCESS)
{
shell_printf("Scanning nearby stations...\n");
}
else
{
shell_printf("Failed to start wifi scan\n");
}
}
else if(strcmp(argv[1], "state") == 0)
{
shell_printf("Station state : %u\n", tls_wifi_get_state());
}
else if(strcmp(argv[1], "connect") == 0)
{
shell_printf("Connecting to %s with pwd : %s\n", argv[2], argv[3]);
if(tls_wifi_connect((u8 *)argv[2], strlen(argv[2]), (u8 *)argv[3], strlen(argv[3])) == WM_SUCCESS)
{
shell_printf("Connected\n");
}
else
{
shell_printf("Failed to connect !\n");
}
}
else if(strcmp(argv[1], "disconnect") == 0)
{
shell_printf("Disconnecting from current station\n");
tls_wifi_disconnect();
}
else
{
shell_printf("Unknown station action\n");
}
}
else
{
shell_printf("List of station actions :\nscan\nstate\nconnect <SSID> <PWD>\ndisconnect\n");
}
return 0;
}
int _cpu_temp(const shell_cmd_t *pcmd, int argc, char *const argv[])
{
int temperature = adc_temp();
shell_printf("CPU temp is %d.%03d\n", temperature/1000, temperature%1000);
return 0;
}
int _wifi_sleep(const shell_cmd_t *pcmd, int argc, char *const argv[])
{
if(argc > 1)
{
if(strcmp(argv[1], "query") == 0)
{
shell_printf("power saving : 0x%X, psm chip sleep : 0x%X\n",
tls_wifi_get_psflag(),
tls_wifi_get_psm_chipsleep_flag());
}
else if(strcmp(argv[1], "set") == 0)
{
}
else
{
shell_printf("Unknown wifi_sleep action\n");
}
}
else
{
shell_printf("List of wifi_sleep actions :\nquery\nset\n");
}
return 0;
}
NANO_SHELL_ADD_CMD(task_list,
_task_list,
"List all tasks",
" Use this command to list all defined tasks\r\n");
NANO_SHELL_ADD_CMD(reset,
_reset,
"Reset the system",
" Use this command reset the system\r\n");
NANO_SHELL_ADD_CMD(soft_ap,
_soft_ap,
"Command to control SOFT AP",
" Use this command to control the SOFT AP subsystem\r\n");
NANO_SHELL_ADD_CMD(station,
_station,
"Command to control STATION mode",
" Use this command to connect to a WiFi access point\r\n");
NANO_SHELL_ADD_CMD(cpu_temp,
_cpu_temp,
"Command to read the CPU temperature",
" Use this command to read the CPU temperature\r\n");
NANO_SHELL_ADD_CMD(wifi_sleep,
_wifi_sleep,
"Command to control WiFi sleep",
" Use this command to control WiFi sleep feature\r\n");