forked from electro/esp-irblaster
initial version made by removing all csp stuff from cspemu
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/12/08.
|
||||
//
|
||||
#include "settings.h"
|
||||
#include "console/cmd_common.h"
|
||||
#include <console/cmddef.h>
|
||||
|
||||
|
||||
static int cmd_dump(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_end *end;
|
||||
} cmd_args;
|
||||
|
||||
if (reg) {
|
||||
cmd_args.end = arg_end(1);
|
||||
|
||||
reg->argtable = &cmd_args;
|
||||
reg->command = "dump";
|
||||
reg->help = "Dump node info";
|
||||
return 0;
|
||||
}
|
||||
|
||||
console_printf("WiFi enabled: %d\r\n", g_Settings.wifi_enabled);
|
||||
|
||||
// TODO show more settings
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_cmd_dump(void)
|
||||
{
|
||||
console_cmd_register(cmd_dump, "dump");
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/12/08.
|
||||
//
|
||||
|
||||
#include <string.h>
|
||||
#include <linenoise/linenoise.h>
|
||||
#include <settings.h>
|
||||
#include <utils.h>
|
||||
#include <nvs_flash.h>
|
||||
|
||||
#include "console/cmd_common.h"
|
||||
#include <console/cmddef.h>
|
||||
|
||||
|
||||
static int cmd_factory_reset(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_str *magic;
|
||||
struct arg_end *end;
|
||||
} cmd_args;
|
||||
|
||||
if (reg) {
|
||||
cmd_args.magic = arg_str1(NULL, NULL, "<passphrase>", "Passphrase to prevent accidental erase. Must be 'confirm'");
|
||||
cmd_args.end = arg_end(2);
|
||||
|
||||
reg->argtable = &cmd_args;
|
||||
reg->command = "factory_reset";
|
||||
reg->help = "Wipe non-volatile data memory, restoring everything to defaults.";
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (streq(cmd_args.magic->sval[0], "confirm")) {
|
||||
nvs_flash_erase();
|
||||
|
||||
console_printf("Non-volatile memory erased.\r\n"
|
||||
"Restarting to apply changes...\r\n\r\n");
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
esp_restart();
|
||||
} else {
|
||||
console_printf("Incorrect passphrase.\r\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_cmd_factory_reset(void)
|
||||
{
|
||||
console_cmd_register(cmd_factory_reset, "factory_reset");
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/12/08.
|
||||
//
|
||||
|
||||
#include <esp_heap_caps.h>
|
||||
#include <esp_system.h>
|
||||
|
||||
#include "console/cmd_common.h"
|
||||
#include <console/cmddef.h>
|
||||
|
||||
|
||||
/* 'heap' command prints minumum heap size */
|
||||
static int cmd_heap(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_end *end;
|
||||
} cmd_args;
|
||||
|
||||
if (reg) {
|
||||
cmd_args.end = arg_end(1);
|
||||
|
||||
reg->argtable = &cmd_args;
|
||||
reg->command = "heap";
|
||||
reg->help = "Get emulator heap usage stats (for debug)";
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t heap_size = heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT);
|
||||
uint32_t current_free = esp_get_free_heap_size();
|
||||
console_printf("free heap: %u bytes\r\n"
|
||||
" lowest: %u bytes\r\n", current_free, heap_size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_cmd_heap(void)
|
||||
{
|
||||
console_cmd_register(cmd_heap, "heap");
|
||||
}
|
||||
@@ -0,0 +1,341 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/event_groups.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <settings.h>
|
||||
#include <lwip/inet.h>
|
||||
|
||||
#include "console/cmd_common.h"
|
||||
#include <console/cmddef.h>
|
||||
#include <application.h>
|
||||
#include <common_utils/utils.h>
|
||||
#include <sntp_cli.h>
|
||||
#include <ping.h>
|
||||
#include <lwip/netdb.h>
|
||||
|
||||
static int cmd_ip_status(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
EMPTY_CMD_SETUP("Show IP status");
|
||||
|
||||
if (!g_Settings.wifi_enabled || !g_State.wifi_inited) {
|
||||
console_color_printf(COLOR_RED, "WiFi interface is disabled\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
wifi_config_t config;
|
||||
if (ESP_OK == esp_wifi_get_config(ESP_IF_WIFI_STA, &config)) {
|
||||
if (config.sta.ssid[0]) {
|
||||
EventBits_t bits = xEventGroupGetBits(g_wifi_event_group);
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
console_color_printf(COLOR_GREEN, "Connected to SSID \"%s\"\n", config.sta.ssid);
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
console_color_printf(COLOR_RED, "WiFi connection failed.\n");
|
||||
console_printf("Saved SSID = \"%s\"\n", config.sta.ssid);
|
||||
return 0;
|
||||
} else {
|
||||
console_color_printf(COLOR_RED, "Not connected, retries may be in progress.\n");
|
||||
console_printf("Saved SSID = \"%s\"\n", config.sta.ssid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
tcpip_adapter_ip_info_t ipinfo;
|
||||
if (ESP_OK == tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ipinfo)) {
|
||||
// ! inet_ntoa uses a global static buffer, cant use multiple in one printf call
|
||||
console_printf("DHCP: %s\n", g_Settings.dhcp_enable ? MSG_ENABLED : MSG_DISABLED " (static IP)");
|
||||
console_printf("IP: %s\n", inet_ntoa(ipinfo.ip.addr));
|
||||
console_printf("Mask: %s\n", inet_ntoa(ipinfo.netmask.addr));
|
||||
console_printf("Gateway: %s\n", inet_ntoa(ipinfo.gw.addr));
|
||||
|
||||
if (!g_Settings.dhcp_enable) {
|
||||
console_printf("DNS: %s\n", inet_ntoa(g_Settings.static_dns));
|
||||
}
|
||||
|
||||
uint8_t mac[6];
|
||||
if (ESP_OK == esp_wifi_get_mac(ESP_IF_WIFI_STA, mac)) {
|
||||
console_printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
}
|
||||
} else {
|
||||
console_color_printf(COLOR_RED, "No IP address assigned!\n");
|
||||
}
|
||||
}
|
||||
else {
|
||||
console_color_printf(COLOR_RED, "SSID not configured!\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_ip_pingwd(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_str *cmd;
|
||||
struct arg_end *end;
|
||||
} cmd_args;
|
||||
|
||||
if (reg) {
|
||||
cmd_args.cmd = arg_str0(NULL, NULL, "<cmd>", "Enable or disable. Omit to check current state");
|
||||
cmd_args.end = arg_end(1);
|
||||
|
||||
reg->argtable = &cmd_args;
|
||||
reg->help = "Enable/disable ping watchdog, or check the current setting. The watchdog periodically pings the gateway and restarts WiFi on failure.";
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd_args.cmd->count) {
|
||||
int b = parse_boolean_arg(cmd_args.cmd->sval[0]);
|
||||
if (b < 0) return CONSOLE_ERR_INVALID_ARG;
|
||||
g_Settings.dhcp_wd_enable = b;
|
||||
settings_persist(SETTINGS_dhcp_wd_enable);
|
||||
}
|
||||
|
||||
console_printf("Ping WD = %s\n", g_Settings.dhcp_wd_enable? MSG_ENABLED : MSG_DISABLED);
|
||||
|
||||
if (cmd_args.cmd->count) {
|
||||
console_printf("Restart to apply changes\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void ping_success_print_cb(int bytes, const char *ip, int seq, int elapsed_ms) {
|
||||
console_printf("Rx %d bytes from %s: icmp_seq=%d time=%d ms\n", bytes, ip, seq, (int) elapsed_ms);
|
||||
}
|
||||
|
||||
static void ping_fail_print_cb(int seq) {
|
||||
console_printf("Request timeout for icmp_seq %d\n", seq);
|
||||
}
|
||||
|
||||
static int cmd_ip_ping(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_str *ip;
|
||||
struct arg_int *num;
|
||||
struct arg_int *timeout;
|
||||
struct arg_int *interval;
|
||||
struct arg_int *size;
|
||||
struct arg_end *end;
|
||||
} args;
|
||||
|
||||
if (reg) {
|
||||
args.ip = arg_str1(NULL, NULL, "<IP>", "Target IP");
|
||||
args.num = arg_int0("n", NULL, "<num>", "Ping count (def 1)");
|
||||
args.timeout = arg_int0("t", NULL, "<ms>", "Timeout (def 3000)");
|
||||
args.interval = arg_int0("i", NULL, "<ms>", "Interval (def 1000)");
|
||||
args.size = arg_int0("s", NULL, "<bytes>", "Payload size (def 32)");
|
||||
args.end = arg_end(5);
|
||||
|
||||
reg->argtable = &args;
|
||||
reg->help = "Ping a host using ICMP";
|
||||
return 0;
|
||||
}
|
||||
|
||||
ping_opts_t opts = PING_CONFIG_DEFAULT();
|
||||
opts.success_cb = ping_success_print_cb;
|
||||
opts.fail_cb = ping_fail_print_cb;
|
||||
opts.count = args.num->count ? args.num->ival[0] : 1;
|
||||
opts.payload_size = args.size->count ? args.size->ival[0] : 32;
|
||||
opts.interval_ms = args.interval->count ? args.interval->ival[0] : 1000;
|
||||
opts.timeout_ms = args.timeout->count ? args.timeout->ival[0] : 3000;
|
||||
|
||||
if (0 == inet_aton(args.ip->sval[0], &opts.ip_addr)) {
|
||||
// we could have received a domain name here
|
||||
struct hostent * ent = gethostbyname(args.ip->sval[0]);
|
||||
if (!ent) {
|
||||
console_println("Could not resolve");
|
||||
return CONSOLE_ERR_IO;
|
||||
}
|
||||
|
||||
memcpy(&opts.ip_addr, ent->h_addr_list[0], sizeof(ip4_addr_t));
|
||||
console_printf("Resolved as %s\n", inet_ntoa(opts.ip_addr));
|
||||
}
|
||||
|
||||
ping_result_t result = {};
|
||||
esp_err_t ret = ping(&opts, &result);
|
||||
|
||||
if (ret != ESP_OK) {
|
||||
console_println("Ping error");
|
||||
return ret;
|
||||
} else {
|
||||
console_printf("%d tx, %d rx, %.1f%% loss, latency min %d ms, max %d ms\n",
|
||||
result.sent,
|
||||
result.received,
|
||||
result.loss_pt,
|
||||
result.min_time_ms,
|
||||
result.max_time_ms);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_ip_static_set(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_str *ip;
|
||||
struct arg_str *gw;
|
||||
struct arg_str *mask;
|
||||
struct arg_str *dns;
|
||||
struct arg_str *cmd;
|
||||
struct arg_end *end;
|
||||
} args;
|
||||
|
||||
if (reg) {
|
||||
args.ip = arg_str0("a", NULL, "<IP>", "Set IP address");
|
||||
args.gw = arg_str0("g", NULL, "<GW>", "Set gateway address");
|
||||
args.mask = arg_str0("n", NULL, "<MASK>", "Set netmask");
|
||||
args.dns = arg_str0("d", NULL, "<DNS>", "Set DNS server IP");
|
||||
args.cmd = arg_str0(NULL, NULL, "{enable|disable}", "Enable or disable static IP");
|
||||
args.end = arg_end(1);
|
||||
|
||||
reg->argtable = &args;
|
||||
reg->help = "Configure, enable/disable, or check config of static IP.";
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool any_change = false;
|
||||
|
||||
if (args.ip->count) {
|
||||
uint32_t a = 0;
|
||||
if (!inet_aton(args.ip->sval[0], &a)) {
|
||||
console_println("Invalid IP");
|
||||
return CONSOLE_ERR_INVALID_ARG;
|
||||
}
|
||||
g_Settings.static_ip = a; // aton output is already in network byte order
|
||||
settings_persist(SETTINGS_static_ip);
|
||||
any_change = true;
|
||||
}
|
||||
|
||||
if (args.gw->count) {
|
||||
uint32_t a = 0;
|
||||
if (!inet_aton(args.gw->sval[0], &a)) {
|
||||
console_println("Invalid GW");
|
||||
return CONSOLE_ERR_INVALID_ARG;
|
||||
}
|
||||
g_Settings.static_ip_gw = a; // aton output is already in network byte order
|
||||
settings_persist(SETTINGS_static_ip_gw);
|
||||
any_change = true;
|
||||
}
|
||||
|
||||
if (args.mask->count) {
|
||||
uint32_t a = 0;
|
||||
if (!inet_aton(args.mask->sval[0], &a)) {
|
||||
console_println("Invalid mask");
|
||||
return CONSOLE_ERR_INVALID_ARG;
|
||||
}
|
||||
g_Settings.static_ip_mask = a; // aton output is already in network byte order
|
||||
settings_persist(SETTINGS_static_ip_mask);
|
||||
any_change = true;
|
||||
}
|
||||
|
||||
if (args.dns->count) {
|
||||
uint32_t a = 0;
|
||||
if (!inet_aton(args.dns->sval[0], &a)) {
|
||||
console_println("Invalid DNS IP");
|
||||
return CONSOLE_ERR_INVALID_ARG;
|
||||
}
|
||||
g_Settings.static_dns = a; // aton output is already in network byte order
|
||||
settings_persist(SETTINGS_static_dns);
|
||||
any_change = true;
|
||||
}
|
||||
|
||||
if (args.cmd->count) {
|
||||
int b = parse_boolean_arg(args.cmd->sval[0]);
|
||||
if (b < 0) return CONSOLE_ERR_INVALID_ARG;
|
||||
g_Settings.dhcp_enable = !b;
|
||||
settings_persist(SETTINGS_dhcp_enable);
|
||||
any_change = true;
|
||||
}
|
||||
|
||||
console_printf("Static IP: %s\n", g_Settings.dhcp_enable ? MSG_DISABLED : MSG_ENABLED);
|
||||
console_printf("- IP: %s\n", inet_ntoa(g_Settings.static_ip));
|
||||
console_printf("- Mask: %s\n", inet_ntoa(g_Settings.static_ip_mask));
|
||||
console_printf("- Gateway: %s\n", inet_ntoa(g_Settings.static_ip_gw));
|
||||
console_printf("- DNS: %s\n", inet_ntoa(g_Settings.static_dns));
|
||||
|
||||
if (any_change) {
|
||||
console_println("Restart to apply changes.");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_ip_ntp(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_str *cmd;
|
||||
struct arg_str *addr;
|
||||
struct arg_end *end;
|
||||
} cmd_args;
|
||||
|
||||
if (reg) {
|
||||
cmd_args.cmd = arg_str0(NULL, NULL, "<cmd>", "Enable or disable autostart. Omit to check current state. start = start now");
|
||||
cmd_args.addr = arg_str0("s", NULL, "<server>", "Set NTP server");
|
||||
cmd_args.end = arg_end(2);
|
||||
|
||||
reg->argtable = &cmd_args;
|
||||
reg->help = "Check or modify NTP client setting, or start it manually.";
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd_args.addr->count) {
|
||||
strncpy(g_Settings.ntp_srv, cmd_args.addr->sval[0], NTP_SRV_LEN);
|
||||
g_Settings.ntp_srv[NTP_SRV_LEN-1] = 0;
|
||||
settings_persist(SETTINGS_ntp_srv);
|
||||
console_printf("NTP server set to %s\n", g_Settings.ntp_srv);
|
||||
}
|
||||
|
||||
if (cmd_args.cmd->count) {
|
||||
if (streq(cmd_args.cmd->sval[0], "start")) {
|
||||
bool started = sntp_cli_start();
|
||||
if (started) {
|
||||
console_printf("NTP client started manually.\n");
|
||||
} else {
|
||||
console_color_printf(COLOR_RED, "Start failed. Client may be already running.\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int b = parse_boolean_arg(cmd_args.cmd->sval[0]);
|
||||
if (b < 0) return CONSOLE_ERR_INVALID_ARG;
|
||||
g_Settings.ntp_enable = b;
|
||||
|
||||
settings_persist(SETTINGS_ntp_enable);
|
||||
}
|
||||
|
||||
console_printf("Client status: %s\n", g_Settings.ntp_enable? MSG_ENABLED : MSG_DISABLED);
|
||||
console_printf("NTP server: %s\n", g_Settings.ntp_srv);
|
||||
|
||||
/* show the current date */
|
||||
time_t now;
|
||||
struct tm timeinfo;
|
||||
time(&now);
|
||||
localtime_r(&now, &timeinfo);
|
||||
|
||||
if(timeinfo.tm_year < (2016 - 1900)) {
|
||||
console_printf("Device time is not valid.\n");
|
||||
} else {
|
||||
char strftime_buf[64];
|
||||
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
|
||||
console_printf("The current UTC date/time is: %s\n", strftime_buf);
|
||||
}
|
||||
|
||||
if (cmd_args.cmd->count) {
|
||||
// if it was "start", we returned early.
|
||||
console_printf("Restart to apply changes\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_cmd_ip(void)
|
||||
{
|
||||
console_group_add("ip", "IP status and settings");
|
||||
console_cmd_register(cmd_ip_status, "ip status");
|
||||
console_cmd_register(cmd_ip_pingwd, "ip wd");
|
||||
console_cmd_register(cmd_ip_ntp, "ip ntp");
|
||||
console_cmd_register(cmd_ip_ping, "ip ping");
|
||||
console_cmd_register(cmd_ip_static_set, "ip static");
|
||||
|
||||
// this may be used for shortcuts like "ip in"
|
||||
console_cmd_add_alias_fn(cmd_ip_status, "ip info");
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// Set telnet pw
|
||||
//
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/event_groups.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <settings.h>
|
||||
|
||||
#include "console/cmd_common.h"
|
||||
#include <console/cmddef.h>
|
||||
#include <application.h>
|
||||
#include <console/prefix_match.h>
|
||||
|
||||
static int cmd_clear(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
EMPTY_CMD_SETUP("Clear access password");
|
||||
console_printf("Access password cleared.\n");
|
||||
g_Settings.console_pw[0] = 0;
|
||||
settings_persist(SETTINGS_console_pw);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_set(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_str *pw;
|
||||
struct arg_end *end;
|
||||
} args;
|
||||
|
||||
if (reg) {
|
||||
args.pw = arg_str1(NULL, NULL, "<password>", EXPENDABLE_STRING("New password"));
|
||||
args.end = arg_end(1);
|
||||
|
||||
reg->argtable = &args;
|
||||
reg->help = EXPENDABLE_STRING("Set access password");
|
||||
return CONSOLE_OK;
|
||||
}
|
||||
|
||||
strncpy(g_Settings.console_pw, args.pw->sval[0], CONSOLE_PW_LEN);
|
||||
console_printf("Access pw set to: \"%.*s\"\n", CONSOLE_PW_LEN, args.pw->sval[0]);
|
||||
settings_persist(SETTINGS_console_pw);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_cmd_pw(void)
|
||||
{
|
||||
console_group_add("pw", "Access password");
|
||||
|
||||
console_cmd_register(cmd_set, "pw set");
|
||||
console_cmd_register(cmd_clear, "pw clear");
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/12/08.
|
||||
//
|
||||
|
||||
#include "console/cmd_common.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "application.h"
|
||||
#include <console/cmddef.h>
|
||||
|
||||
|
||||
/** 'restart' command restarts the program */
|
||||
static int cmd_restart(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_end *end;
|
||||
} cmd_args;
|
||||
|
||||
if (reg) {
|
||||
cmd_args.end = arg_end(1);
|
||||
|
||||
reg->argtable = &cmd_args;
|
||||
reg->command = "restart";
|
||||
reg->help = "Restart the emulator";
|
||||
return 0;
|
||||
}
|
||||
|
||||
console_printf("Restarting...\r\n");
|
||||
|
||||
// try to cleanly close all connections
|
||||
telnetsrv_kick_all();
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
void register_cmd_restart(void)
|
||||
{
|
||||
console_cmd_register(cmd_restart, "restart");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/12/08.
|
||||
//
|
||||
|
||||
#include <esp_log.h>
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include "console/cmd_common.h"
|
||||
#include <console/cmddef.h>
|
||||
|
||||
|
||||
static const char* TAG = "cmd_tasks";
|
||||
|
||||
static int cmd_tasks_info(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_end *end;
|
||||
} cmd_args;
|
||||
|
||||
if (reg) {
|
||||
cmd_args.end = arg_end(1);
|
||||
|
||||
reg->argtable = &cmd_args;
|
||||
reg->command = "ps";
|
||||
reg->help = "List running emulator tasks (for debug)";
|
||||
return 0;
|
||||
}
|
||||
|
||||
const size_t bytes_per_task = 40; /* see vTaskList description */
|
||||
char *task_list_buffer = calloc(uxTaskGetNumberOfTasks() * bytes_per_task, 1);
|
||||
if (task_list_buffer == NULL) {
|
||||
ESP_LOGE(TAG, "failed to allocate buffer for vTaskList output");
|
||||
return 1;
|
||||
}
|
||||
console_fputs("\x1b[1mTask Name\tStatus\tPrio\tHWM\tTask#");
|
||||
#ifdef CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
|
||||
console_fputs("\tAffinity");
|
||||
#endif
|
||||
console_fputs("\x1b[m\r\n");
|
||||
vTaskList(task_list_buffer);
|
||||
console_fputs(task_list_buffer);
|
||||
free(task_list_buffer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_cmd_tasks()
|
||||
{
|
||||
console_cmd_register(cmd_tasks_info, "ps");
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/12/08.
|
||||
//
|
||||
|
||||
#include "console/cmd_common.h"
|
||||
|
||||
#include <esp_spi_flash.h>
|
||||
#include <esp_system.h>
|
||||
|
||||
#include "application.h"
|
||||
#include <console/cmddef.h>
|
||||
|
||||
|
||||
/** 'version' command */
|
||||
static int cmd_version(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_end *end;
|
||||
} cmd_args;
|
||||
|
||||
if (reg) {
|
||||
cmd_args.end = arg_end(1);
|
||||
|
||||
reg->argtable = &cmd_args;
|
||||
reg->command = "version";
|
||||
reg->help = "Get version of the chip, SDK, and firmware.";
|
||||
return 0;
|
||||
}
|
||||
|
||||
esp_chip_info_t info;
|
||||
esp_chip_info(&info);
|
||||
console_printf("IDF Version: %s\n", esp_get_idf_version());
|
||||
console_printf("Firmware: %s\n", APP_VERSION GIT_COUNT);
|
||||
console_printf(" git: %s\n", GIT_HASH);
|
||||
console_printf(" builded: %s\n", BUILD_TIMESTAMP);
|
||||
console_printf("Chip model: %s\n", info.model == CHIP_ESP32 ? "ESP32" : "Unknow");
|
||||
console_printf(" cores: %d\n", info.cores);
|
||||
console_printf(" feature: %s%s%s%s%d%s\n",
|
||||
info.features & CHIP_FEATURE_WIFI_BGN ? "/802.11bgn" : "",
|
||||
info.features & CHIP_FEATURE_BLE ? "/BLE" : "",
|
||||
info.features & CHIP_FEATURE_BT ? "/BT" : "",
|
||||
info.features & CHIP_FEATURE_EMB_FLASH ? "/Embedded-Flash:" : "/External-Flash:",
|
||||
spi_flash_get_chip_size() / (1024 * 1024), "MB");
|
||||
console_printf("rev.number: %d\n", info.revision);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_cmd_version(void)
|
||||
{
|
||||
console_cmd_register(cmd_version, "version");
|
||||
}
|
||||
@@ -0,0 +1,360 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/12/08.
|
||||
//
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/event_groups.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <settings.h>
|
||||
|
||||
#include "console/cmd_common.h"
|
||||
#include <console/cmddef.h>
|
||||
#include <application.h>
|
||||
#include <console/prefix_match.h>
|
||||
|
||||
|
||||
static int cmd_disable(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
EMPTY_CMD_SETUP("Disable WiFi");
|
||||
console_printf("WiFi "MSG_DISABLED"\nRestart to apply.\n");
|
||||
g_Settings.wifi_enabled = false;
|
||||
settings_persist(SETTINGS_wifi_enabled);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_enable(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
EMPTY_CMD_SETUP("Enable WiFi");
|
||||
console_printf("WiFi "MSG_ENABLED"\nRestart to apply.\n");
|
||||
g_Settings.wifi_enabled = true;
|
||||
settings_persist(SETTINGS_wifi_enabled);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const char *en_dis_cmds[] = {
|
||||
[0] = "disable",
|
||||
[1] = "enable",
|
||||
NULL
|
||||
};
|
||||
|
||||
static int cmd_ap_conf(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_str *cmd;
|
||||
struct arg_str *ssid;
|
||||
struct arg_str *pw;
|
||||
struct arg_str *ip;
|
||||
struct arg_end *end;
|
||||
} args;
|
||||
|
||||
if (reg) {
|
||||
args.cmd = arg_str0(NULL, NULL, "{enable|disable}", EXPENDABLE_STRING("Command"));
|
||||
args.ssid = arg_str0("s", NULL, "<SSID>", EXPENDABLE_STRING("Set AP SSID"));
|
||||
args.pw = arg_str0("p", NULL, "<PWD>", EXPENDABLE_STRING("Set AP WPA2 password. Empty for open."));
|
||||
args.ip = arg_str0("a", NULL, "<IP>", "Set IP address (server + gateway). Always /24");
|
||||
args.end = arg_end(4);
|
||||
|
||||
reg->argtable = &args;
|
||||
reg->help = EXPENDABLE_STRING("Configure WiFi AP mode");
|
||||
return CONSOLE_OK;
|
||||
}
|
||||
|
||||
if (!g_State.wifi_inited) {
|
||||
console_printf("\x1b[31;1mWiFi is disabled!\x1b[22m\nEnable with `wifi enable`, restart to apply.\x1b[m\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (args.cmd->count) {
|
||||
int match = prefix_match(args.cmd->sval[0], en_dis_cmds, 0);
|
||||
|
||||
switch (match) {
|
||||
case 0:
|
||||
console_printf("AP mode "MSG_DISABLED"\nRestart to apply.\n");
|
||||
g_Settings.ap_enabled = false;
|
||||
settings_persist(SETTINGS_ap_enabled);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
console_printf("AP mode "MSG_ENABLED"\nRestart to apply.\n");
|
||||
g_Settings.ap_enabled = true;
|
||||
settings_persist(SETTINGS_ap_enabled);
|
||||
break;
|
||||
|
||||
default:
|
||||
return CONSOLE_ERR_INVALID_ARG;
|
||||
}
|
||||
} else {
|
||||
// No cmd
|
||||
console_printf("AP mode: %s\n", g_Settings.ap_enabled? MSG_ENABLED: MSG_DISABLED);
|
||||
}
|
||||
|
||||
if (args.ip->count) {
|
||||
uint32_t a = 0;
|
||||
if (!inet_aton(args.ip->sval[0], &a)) {
|
||||
console_println("Invalid IP");
|
||||
return CONSOLE_ERR_INVALID_ARG;
|
||||
}
|
||||
g_Settings.ap_ip = a; // aton output is already in network byte order
|
||||
settings_persist(SETTINGS_ap_ip);
|
||||
|
||||
console_println("AP IP changed, restart to apply.\n");
|
||||
}
|
||||
|
||||
bool changed = false;
|
||||
wifi_config_t apconf = {};
|
||||
ESP_ERROR_CHECK(esp_wifi_get_config(ESP_IF_WIFI_AP, &apconf));
|
||||
|
||||
if (args.ssid->count) {
|
||||
//apconf.ap.authmode = WIFI_AUTH_OPEN;
|
||||
strcpy((char*)apconf.ap.ssid, args.ssid->sval[0]);
|
||||
apconf.ap.ssid_len = strlen(args.ssid->sval[0]);
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (args.pw->count) {
|
||||
size_t len = strlen(args.pw->sval[0]);
|
||||
if (len < 8 && len != 0) {
|
||||
console_println("AP pw must be 8 chars or more!");
|
||||
return CONSOLE_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
strcpy((char*)apconf.ap.password, args.pw->sval[0]);
|
||||
if (len == 0) {
|
||||
// if no pw is set, the AP will be open
|
||||
apconf.ap.authmode = WIFI_AUTH_OPEN;
|
||||
} else {
|
||||
apconf.ap.authmode = WIFI_AUTH_WPA2_PSK;
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
esp_err_t rv = esp_wifi_set_config(ESP_IF_WIFI_AP, &apconf);
|
||||
if (rv != ESP_OK) {
|
||||
console_printf("Error set config: %s\n", esp_err_to_name(rv));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
console_printf("AP SSID: \"%s\"\n", apconf.ap.ssid);
|
||||
console_printf("AP PW: \"%s\"\n", apconf.ap.password);
|
||||
console_printf("AP own IP: %s/24\n", inet_ntoa(g_Settings.ap_ip));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_sta_conf(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_str *cmd;
|
||||
struct arg_end *end;
|
||||
} args;
|
||||
|
||||
if (reg) {
|
||||
args.cmd = arg_str0(NULL, NULL, "{enable|disable}", EXPENDABLE_STRING("Command"));
|
||||
args.end = arg_end(1);
|
||||
|
||||
reg->argtable = &args;
|
||||
reg->help = EXPENDABLE_STRING("Configure WiFi STA mode");
|
||||
return CONSOLE_OK;
|
||||
}
|
||||
|
||||
if (!g_State.wifi_inited) {
|
||||
console_printf("\x1b[31;1mWiFi is disabled!\x1b[22m\nEnable with `wifi enable`, restart to apply.\x1b[m\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (args.cmd->count) {
|
||||
int match = prefix_match(args.cmd->sval[0], en_dis_cmds, 0);
|
||||
|
||||
switch (match) {
|
||||
case 0:
|
||||
console_printf("STA mode "MSG_DISABLED"\nRestart to apply.\n");
|
||||
g_Settings.sta_enabled = false;
|
||||
settings_persist(SETTINGS_sta_enabled);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
console_printf("STA mode "MSG_ENABLED"\nRestart to apply.\n");
|
||||
g_Settings.sta_enabled = true;
|
||||
settings_persist(SETTINGS_sta_enabled);
|
||||
break;
|
||||
|
||||
default:
|
||||
return CONSOLE_ERR_INVALID_ARG;
|
||||
}
|
||||
} else {
|
||||
// No cmd
|
||||
console_printf("STA mode: %s\n", g_Settings.sta_enabled? MSG_ENABLED: MSG_DISABLED);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Disconnect from WiFi and forget creds */
|
||||
static int cmd_sta_forget(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_end *end;
|
||||
} args;
|
||||
|
||||
if (reg) {
|
||||
args.end = arg_end(1);
|
||||
|
||||
reg->argtable = &args;
|
||||
reg->command = "wifi forget";
|
||||
reg->help = "Disconnect from WiFi AP and erase stored credentials";
|
||||
return 0;
|
||||
}
|
||||
|
||||
console_printf("Removing saved WiFi credentials and disconnecting.\n");
|
||||
|
||||
if (!g_State.wifi_inited) {
|
||||
console_printf("\x1b[31;1mWiFi is disabled!\x1b[22m\nEnable with `wifi enable`, restart to apply.\x1b[m\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
wifi_config_t wificonf;
|
||||
esp_wifi_get_config(WIFI_IF_STA, &wificonf);
|
||||
wificonf.sta.ssid[0] = 0;
|
||||
wificonf.sta.password[0] = 0;
|
||||
esp_wifi_set_config(WIFI_IF_STA, &wificonf);
|
||||
esp_wifi_disconnect();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define DEF_WIFI_TIMEOUT 10000
|
||||
|
||||
static bool wifi_join(const char* ssid, const char* pass, int timeout_ms)
|
||||
{
|
||||
wifi_config_t wifi_config = {};
|
||||
strncpy((char*) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
|
||||
if (pass) {
|
||||
strncpy((char*) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
|
||||
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
|
||||
|
||||
xEventGroupClearBits(g_wifi_event_group, WIFI_CONNECTED_BIT|WIFI_FAIL_BIT);
|
||||
|
||||
ESP_ERROR_CHECK( esp_wifi_disconnect() );
|
||||
ESP_ERROR_CHECK( esp_wifi_connect() );
|
||||
|
||||
int bits = xEventGroupWaitBits(g_wifi_event_group, WIFI_CONNECTED_BIT|WIFI_FAIL_BIT,
|
||||
/* clear */ 0, /* wait for all */0, pdMS_TO_TICKS(timeout_ms));
|
||||
|
||||
return (bits & EG_WIFI_CONNECTED_BIT) != 0;
|
||||
}
|
||||
|
||||
static int cmd_join(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_int *timeout;
|
||||
struct arg_str *ssid;
|
||||
struct arg_str *password;
|
||||
struct arg_end *end;
|
||||
} cmd_args;
|
||||
|
||||
if (reg) {
|
||||
cmd_args.timeout = arg_int0("t", "timeout", "<t>", "Connection timeout, ms");
|
||||
cmd_args.ssid = arg_str1(NULL, NULL, "<ssid>", "SSID of AP");
|
||||
cmd_args.password = arg_str0(NULL, NULL, "<pass>", "PSK of AP");
|
||||
cmd_args.end = arg_end(2);
|
||||
|
||||
reg->argtable = &cmd_args;
|
||||
reg->command = "wifi join";
|
||||
reg->help = "Join WiFi AP as a station";
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!g_State.wifi_inited) {
|
||||
console_printf("\x1b[31;1mWiFi is disabled!\x1b[22m\n"
|
||||
"Enable with `wifi enable`, restart to apply.\x1b[m\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
console_printf("Connecting to '%s'\n", cmd_args.ssid->sval[0]);
|
||||
|
||||
int tmeo = cmd_args.timeout->count ? cmd_args.timeout->ival[0] : DEF_WIFI_TIMEOUT;
|
||||
|
||||
bool connected = wifi_join(cmd_args.ssid->sval[0],
|
||||
cmd_args.password->sval[0],
|
||||
tmeo);
|
||||
if (!connected) {
|
||||
console_printf("Connection timed out\n");
|
||||
|
||||
// erase config
|
||||
wifi_config_t wifi_config = {};
|
||||
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
|
||||
|
||||
return 1;
|
||||
}
|
||||
console_printf("Connected\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_wifi_status(console_ctx_t *ctx, cmd_signature_t *reg)
|
||||
{
|
||||
static struct {
|
||||
struct arg_end *end;
|
||||
} cmd_args;
|
||||
|
||||
if (reg) {
|
||||
cmd_args.end = arg_end(1);
|
||||
|
||||
reg->argtable = &cmd_args;
|
||||
reg->command = "wifi status";
|
||||
reg->help = "Check WiFi / IP status";
|
||||
return 0;
|
||||
}
|
||||
|
||||
console_printf("WiFi support: %s\n", g_Settings.wifi_enabled ? MSG_ENABLED : MSG_DISABLED);
|
||||
console_printf("STA mode: %s\n", g_Settings.sta_enabled? MSG_ENABLED: MSG_DISABLED);
|
||||
console_printf("AP mode: %s\n", g_Settings.ap_enabled? MSG_ENABLED: MSG_DISABLED);
|
||||
|
||||
console_printf("\n");
|
||||
|
||||
if (g_Settings.wifi_enabled) {
|
||||
wifi_config_t config;
|
||||
if (ESP_OK == esp_wifi_get_config(ESP_IF_WIFI_STA, &config)) {
|
||||
if (config.sta.ssid[0]) {
|
||||
console_printf("Configured to connect to SSID \"%s\".\n"
|
||||
"Use `wifi forget` to drop saved credentials.\n\n",
|
||||
config.sta.ssid);
|
||||
|
||||
tcpip_adapter_ip_info_t ipinfo;
|
||||
if (ESP_OK == tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ipinfo)) {
|
||||
// ! inet_ntoa uses a global static buffer, cant use multiple in one printf call
|
||||
console_printf("IP: %s, ", inet_ntoa(ipinfo.ip.addr));
|
||||
console_printf("Mask: %s, ", inet_ntoa(ipinfo.netmask.addr));
|
||||
console_printf("Gateway: %s\n", inet_ntoa(ipinfo.gw.addr));
|
||||
} else {
|
||||
console_printf("No IP!\n");
|
||||
}
|
||||
}
|
||||
else {
|
||||
console_printf("No network SSID configured.\n"
|
||||
"Use `wifi join` to connect to a WiFi network.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_cmd_wifi(void)
|
||||
{
|
||||
console_group_add("wifi", "WiFi configuration");
|
||||
|
||||
console_cmd_register(cmd_enable, "wifi enable");
|
||||
console_cmd_register(cmd_disable, "wifi disable");
|
||||
|
||||
console_cmd_register(cmd_sta_conf, "wifi sta");
|
||||
console_cmd_register(cmd_sta_forget, "wifi forget");
|
||||
console_cmd_register(cmd_join, "wifi join");
|
||||
|
||||
console_cmd_register(cmd_ap_conf, "wifi ap");
|
||||
|
||||
console_cmd_register(cmd_wifi_status, "wifi status");
|
||||
}
|
||||
Reference in New Issue
Block a user