mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2026-03-22 22:39:27 +00:00
applied platformio structure
This commit is contained in:
17
lib/platform_console/CMakeLists.txt
Normal file
17
lib/platform_console/CMakeLists.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
idf_component_register( SRCS
|
||||
cmd_i2ctools.c
|
||||
cmd_nvs.c
|
||||
cmd_ota.c
|
||||
cmd_system.c
|
||||
cmd_wifi.c
|
||||
platform_console.c
|
||||
cmd_config.c
|
||||
INCLUDE_DIRS .
|
||||
REQUIRES nvs_flash
|
||||
PRIV_REQUIRES console app_update tools services spi_flash platform_config vfs pthread wifi-manager platform_config newlib telnet display squeezelite tools)
|
||||
|
||||
set_source_files_properties(cmd_config.c
|
||||
PROPERTIES COMPILE_FLAGS
|
||||
-Wno-unused-function
|
||||
)
|
||||
|
||||
10
lib/platform_console/app_recovery/CMakeLists.txt
Normal file
10
lib/platform_console/app_recovery/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
idf_component_register( SRC_DIRS .
|
||||
INCLUDE_DIRS .
|
||||
PRIV_REQUIRES bootloader_support
|
||||
)
|
||||
|
||||
target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--undefined=esp_app_desc")
|
||||
idf_build_get_property(project_ver PROJECT_VER)
|
||||
message("******************* ${project_ver}")
|
||||
string(SUBSTRING "${project_ver}" 0 31 PROJECT_VER_CUT)
|
||||
set_source_files_properties(recovery.c PROPERTIES COMPILE_DEFINITIONS "PROJECT_VER=\"${PROJECT_VER_CUT}\"")
|
||||
6
lib/platform_console/app_recovery/application_name.h
Normal file
6
lib/platform_console/app_recovery/application_name.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include "sdkconfig.h"
|
||||
#ifndef CONFIG_PROJECT_NAME
|
||||
#pragma message "Defaulting project name."
|
||||
#define CONFIG_PROJECT_NAME "Squeezelite-ESP32"
|
||||
#endif
|
||||
52
lib/platform_console/app_recovery/recovery.c
Normal file
52
lib/platform_console/app_recovery/recovery.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "application_name.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_app_format.h"
|
||||
|
||||
extern esp_err_t process_recovery_ota(const char * bin_url, char * bin_buffer, uint32_t length);
|
||||
|
||||
const __attribute__((section(".rodata_desc"))) esp_app_desc_t esp_app_desc = {
|
||||
.magic_word = ESP_APP_DESC_MAGIC_WORD,
|
||||
.version = PROJECT_VER,
|
||||
.project_name = CONFIG_PROJECT_NAME,
|
||||
.idf_ver = IDF_VER,
|
||||
|
||||
#ifdef CONFIG_BOOTLOADER_APP_SECURE_VERSION
|
||||
.secure_version = CONFIG_BOOTLOADER_APP_SECURE_VERSION,
|
||||
#else
|
||||
.secure_version = 0,
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_APP_COMPILE_TIME_DATE
|
||||
.time = __TIME__,
|
||||
.date = __DATE__,
|
||||
#else
|
||||
.time = "",
|
||||
.date = "",
|
||||
#endif
|
||||
};
|
||||
|
||||
void register_optional_cmd(void) {
|
||||
}
|
||||
|
||||
int main(int argc, char **argv){
|
||||
return 1;
|
||||
}
|
||||
|
||||
void register_squeezelite(){
|
||||
}
|
||||
|
||||
void register_external(void) {
|
||||
}
|
||||
|
||||
void deregister_external(void) {
|
||||
}
|
||||
|
||||
void decode_restore(int external) {
|
||||
}
|
||||
|
||||
esp_err_t start_ota(const char * bin_url, char * bin_buffer, uint32_t length)
|
||||
{
|
||||
return process_recovery_ota(bin_url,bin_buffer,length);
|
||||
}
|
||||
20
lib/platform_console/app_squeezelite/CMakeLists.txt
Normal file
20
lib/platform_console/app_squeezelite/CMakeLists.txt
Normal file
@@ -0,0 +1,20 @@
|
||||
idf_build_get_property(idf_path IDF_PATH)
|
||||
idf_component_register( SRCS cmd_squeezelite.c
|
||||
INCLUDE_DIRS .
|
||||
PRIV_REQUIRES spi_flash bootloader_support partition_table bootloader_support console codecs squeezelite newlib pthread tools platform_config display tools services)
|
||||
|
||||
|
||||
target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--undefined=feof")
|
||||
target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--undefined=log")
|
||||
target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--undefined=fdopen")
|
||||
target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--undefined=fileno")
|
||||
target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--undefined=fstat")
|
||||
target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--undefined=lround")
|
||||
target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--undefined=esp_app_desc")
|
||||
|
||||
idf_build_get_property(project_ver PROJECT_VER)
|
||||
message("******************* ${project_ver}")
|
||||
string(SUBSTRING "${project_ver}" 0 31 PROJECT_VER_CUT)
|
||||
set_source_files_properties(cmd_squeezelite.c PROPERTIES COMPILE_DEFINITIONS "PROJECT_VER=\"${PROJECT_VER_CUT}\" ")
|
||||
|
||||
|
||||
6
lib/platform_console/app_squeezelite/application_name.h
Normal file
6
lib/platform_console/app_squeezelite/application_name.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include "sdkconfig.h"
|
||||
#ifndef CONFIG_PROJECT_NAME
|
||||
#pragma message "Defaulting project name."
|
||||
#define CONFIG_PROJECT_NAME "Squeezelite-ESP32"
|
||||
#endif
|
||||
173
lib/platform_console/app_squeezelite/cmd_squeezelite.c
Normal file
173
lib/platform_console/app_squeezelite/cmd_squeezelite.c
Normal file
@@ -0,0 +1,173 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "application_name.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_console.h"
|
||||
#include "../cmd_system.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "pthread.h"
|
||||
#include "platform_esp32.h"
|
||||
#include "platform_config.h"
|
||||
#include "esp_app_format.h"
|
||||
#include "tools.h"
|
||||
#include "messaging.h"
|
||||
|
||||
extern esp_err_t process_recovery_ota(const char * bin_url, char * bin_buffer, uint32_t length);
|
||||
static const char * TAG = "squeezelite_cmd";
|
||||
#define SQUEEZELITE_THREAD_STACK_SIZE (8*1024)
|
||||
|
||||
const __attribute__((section(".rodata_desc"))) esp_app_desc_t esp_app_desc = {
|
||||
|
||||
.magic_word = ESP_APP_DESC_MAGIC_WORD,
|
||||
.version = PROJECT_VER,
|
||||
.project_name = CONFIG_PROJECT_NAME,
|
||||
.idf_ver = IDF_VER,
|
||||
|
||||
#ifdef CONFIG_BOOTLOADER_APP_SECURE_VERSION
|
||||
.secure_version = CONFIG_BOOTLOADER_APP_SECURE_VERSION,
|
||||
#else
|
||||
.secure_version = 0,
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_APP_COMPILE_TIME_DATE
|
||||
.time = __TIME__,
|
||||
.date = __DATE__,
|
||||
#else
|
||||
.time = "",
|
||||
.date = "",
|
||||
#endif
|
||||
};
|
||||
|
||||
extern void register_audio_config(void);
|
||||
extern void register_rotary_config(void);
|
||||
extern void register_ledvu_config(void);
|
||||
extern void register_nvs();
|
||||
|
||||
void register_optional_cmd(void) {
|
||||
#if CONFIG_WITH_CONFIG_UI
|
||||
register_rotary_config();
|
||||
#endif
|
||||
register_audio_config();
|
||||
register_ledvu_config();
|
||||
register_nvs();
|
||||
}
|
||||
|
||||
extern int squeezelite_main(int argc, char **argv);
|
||||
|
||||
static int launchsqueezelite(int argc, char **argv);
|
||||
|
||||
/** Arguments used by 'squeezelite' function */
|
||||
static struct {
|
||||
struct arg_str *parameters;
|
||||
struct arg_end *end;
|
||||
} squeezelite_args;
|
||||
static struct {
|
||||
int argc;
|
||||
char ** argv;
|
||||
} thread_parms ;
|
||||
|
||||
#define ADDITIONAL_SQUEEZELITE_ARGS 5
|
||||
static void squeezelite_thread(void *arg){
|
||||
ESP_LOGV(TAG ,"Number of args received: %u",thread_parms.argc );
|
||||
ESP_LOGV(TAG ,"Values:");
|
||||
for(int i = 0;i<thread_parms.argc; i++){
|
||||
ESP_LOGV(TAG ," %s",thread_parms.argv[i]);
|
||||
}
|
||||
ESP_LOGI(TAG ,"Calling squeezelite");
|
||||
int ret = squeezelite_main(thread_parms.argc, thread_parms.argv);
|
||||
|
||||
cmd_send_messaging("cfg-audio-tmpl",ret > 1 ? MESSAGING_ERROR : MESSAGING_WARNING,"squeezelite exited with error code %d\n", ret);
|
||||
|
||||
if (ret <= 1) {
|
||||
int wait = 60;
|
||||
wait_for_commit();
|
||||
cmd_send_messaging("cfg-audio-tmpl",MESSAGING_ERROR,"Rebooting in %d sec\n", wait);
|
||||
vTaskDelay( pdMS_TO_TICKS(wait * 1000));
|
||||
esp_restart();
|
||||
} else {
|
||||
cmd_send_messaging("cfg-audio-tmpl",MESSAGING_ERROR,"Correct command line and reboot\n");
|
||||
vTaskSuspend(NULL);
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, "Exited from squeezelite's main(). Freeing argv structure.");
|
||||
|
||||
for(int i=0;i<thread_parms.argc;i++) free(thread_parms.argv[i]);
|
||||
free(thread_parms.argv);
|
||||
}
|
||||
|
||||
static int launchsqueezelite(int argc, char **argv) {
|
||||
static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
|
||||
static EXT_RAM_ATTR StackType_t xStack[SQUEEZELITE_THREAD_STACK_SIZE] __attribute__ ((aligned (4)));
|
||||
static bool isRunning = false;
|
||||
|
||||
if (isRunning) {
|
||||
ESP_LOGE(TAG,"Squeezelite already running. Exiting!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG ,"Begin");
|
||||
isRunning = true;
|
||||
|
||||
ESP_LOGV(TAG, "Parameters:");
|
||||
for(int i = 0;i<argc; i++){
|
||||
ESP_LOGV(TAG, " %s",argv[i]);
|
||||
}
|
||||
ESP_LOGV(TAG,"Saving args in thread structure");
|
||||
|
||||
thread_parms.argc=0;
|
||||
thread_parms.argv = malloc_init_external(sizeof(char**)*(argc+ADDITIONAL_SQUEEZELITE_ARGS));
|
||||
|
||||
for(int i=0;i<argc;i++){
|
||||
ESP_LOGD(TAG ,"assigning parm %u : %s",i,argv[i]);
|
||||
thread_parms.argv[thread_parms.argc++]=strdup(argv[i]);
|
||||
}
|
||||
|
||||
if(argc==1){
|
||||
// There isn't a default configuration that would actually work
|
||||
// if no parameter is passed.
|
||||
ESP_LOGV(TAG ,"Adding argv value at %u. Prev value: %s",thread_parms.argc,thread_parms.argv[thread_parms.argc-1]);
|
||||
thread_parms.argv[thread_parms.argc++]=strdup("-?");
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG,"Starting Squeezelite Thread");
|
||||
xTaskCreateStaticPinnedToCore(squeezelite_thread, "squeezelite", SQUEEZELITE_THREAD_STACK_SIZE,
|
||||
NULL, CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT, xStack, &xTaskBuffer, CONFIG_PTHREAD_TASK_CORE_DEFAULT);
|
||||
ESP_LOGD(TAG ,"Back to console thread!");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_squeezelite() {
|
||||
squeezelite_args.parameters = arg_str0(NULL, NULL, "<parms>", "command line for squeezelite. -h for help, --defaults to launch with default values.");
|
||||
squeezelite_args.end = arg_end(1);
|
||||
const esp_console_cmd_t launch_squeezelite = {
|
||||
.command = "squeezelite",
|
||||
.help = "Starts squeezelite",
|
||||
.hint = NULL,
|
||||
.func = &launchsqueezelite,
|
||||
.argtable = &squeezelite_args
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&launch_squeezelite) );
|
||||
}
|
||||
|
||||
esp_err_t start_ota(const char * bin_url, char * bin_buffer, uint32_t length) {
|
||||
if(!bin_url){
|
||||
ESP_LOGE(TAG,"missing URL parameter. Unable to start OTA");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
ESP_LOGW(TAG, "Called to update the firmware from url: %s",bin_url);
|
||||
if(config_set_value(NVS_TYPE_STR, "fwurl", bin_url) != ESP_OK){
|
||||
ESP_LOGE(TAG,"Failed to save the OTA url into nvs cache");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if(!wait_for_commit()){
|
||||
ESP_LOGW(TAG,"Unable to commit configuration. ");
|
||||
}
|
||||
|
||||
ESP_LOGW(TAG, "Rebooting to recovery to complete the installation");
|
||||
return guided_factory();
|
||||
return ESP_OK;
|
||||
}
|
||||
1417
lib/platform_console/cmd_config.c
Normal file
1417
lib/platform_console/cmd_config.c
Normal file
File diff suppressed because it is too large
Load Diff
19
lib/platform_console/cmd_config.h
Normal file
19
lib/platform_console/cmd_config.h
Normal file
@@ -0,0 +1,19 @@
|
||||
/* cmd_i2ctools.h
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "stdbool.h"
|
||||
#include "stdio.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
void register_config_cmd(void);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
23
lib/platform_console/cmd_decl.h
Normal file
23
lib/platform_console/cmd_decl.h
Normal file
@@ -0,0 +1,23 @@
|
||||
/* Console example — declarations of command registration functions.
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "cmd_system.h"
|
||||
#include "cmd_wifi.h"
|
||||
#include "cmd_nvs.h"
|
||||
#include "cmd_i2ctools.h"
|
||||
#include "cmd_ota.h"
|
||||
#include "cmd_config.h"
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
1119
lib/platform_console/cmd_i2ctools.c
Normal file
1119
lib/platform_console/cmd_i2ctools.c
Normal file
File diff suppressed because it is too large
Load Diff
20
lib/platform_console/cmd_i2ctools.h
Normal file
20
lib/platform_console/cmd_i2ctools.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/* cmd_i2ctools.h
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "esp_err.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void register_i2ctools(void);
|
||||
esp_err_t cmd_i2ctools_scan_bus(FILE *f,int sda, int scl);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
611
lib/platform_console/cmd_nvs.c
Normal file
611
lib/platform_console/cmd_nvs.c
Normal file
@@ -0,0 +1,611 @@
|
||||
/* Console example — NVS commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include "nvs_flash.h"
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_console.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_err.h"
|
||||
#include "cmd_nvs.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_utilities.h"
|
||||
#include "platform_console.h"
|
||||
#include "messaging.h"
|
||||
#include "tools.h"
|
||||
#include "trace.h"
|
||||
|
||||
extern esp_err_t network_wifi_erase_legacy();
|
||||
extern esp_err_t network_wifi_erase_known_ap();
|
||||
|
||||
static const char *ARG_TYPE_STR = "type can be: i8, u8, i16, u16 i32, u32 i64, u64, str, blob";
|
||||
static const char * TAG = "cmd_nvs";
|
||||
|
||||
EXT_RAM_ATTR static struct {
|
||||
struct arg_str *key;
|
||||
struct arg_str *type;
|
||||
struct arg_str *value;
|
||||
struct arg_end *end;
|
||||
} set_args;
|
||||
|
||||
EXT_RAM_ATTR static struct {
|
||||
struct arg_str *key;
|
||||
struct arg_str *type;
|
||||
struct arg_end *end;
|
||||
} get_args;
|
||||
|
||||
EXT_RAM_ATTR static struct {
|
||||
struct arg_str *key;
|
||||
struct arg_end *end;
|
||||
} erase_args;
|
||||
|
||||
EXT_RAM_ATTR static struct {
|
||||
struct arg_str *namespace;
|
||||
struct arg_end *end;
|
||||
} erase_all_args;
|
||||
|
||||
EXT_RAM_ATTR static struct {
|
||||
struct arg_str *partition;
|
||||
struct arg_str *namespace;
|
||||
struct arg_str *type;
|
||||
struct arg_end *end;
|
||||
} list_args;
|
||||
|
||||
EXT_RAM_ATTR static struct {
|
||||
struct arg_lit *legacy;
|
||||
struct arg_lit *ap_list;
|
||||
struct arg_end *end;
|
||||
} wifi_erase_args;
|
||||
|
||||
|
||||
static esp_err_t store_blob(nvs_handle nvs, const char *key, const char *str_values)
|
||||
{
|
||||
uint8_t value;
|
||||
size_t str_len = strlen(str_values);
|
||||
size_t blob_len = str_len / 2;
|
||||
|
||||
if (str_len % 2) {
|
||||
log_send_messaging(MESSAGING_ERROR, "Blob data must contain even number of characters");
|
||||
return ESP_ERR_NVS_TYPE_MISMATCH;
|
||||
}
|
||||
|
||||
char *blob = (char *)malloc_init_external(blob_len);
|
||||
if (blob == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
for (int i = 0, j = 0; i < str_len; i++) {
|
||||
char ch = str_values[i];
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
value = ch - '0';
|
||||
} else if (ch >= 'A' && ch <= 'F') {
|
||||
value = ch - 'A' + 10;
|
||||
} else if (ch >= 'a' && ch <= 'f') {
|
||||
value = ch - 'a' + 10;
|
||||
} else {
|
||||
log_send_messaging(MESSAGING_ERROR, "Blob data contain invalid character");
|
||||
free(blob);
|
||||
return ESP_ERR_NVS_TYPE_MISMATCH;
|
||||
}
|
||||
|
||||
if (i & 1) {
|
||||
blob[j++] += value;
|
||||
} else {
|
||||
blob[j] = value << 4;
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t err = nvs_set_blob(nvs, key, blob, blob_len);
|
||||
free(blob);
|
||||
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_commit(nvs);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static esp_err_t set_value_in_nvs(const char *key, const char *str_type, const char *str_value)
|
||||
{
|
||||
esp_err_t err;
|
||||
nvs_handle nvs;
|
||||
bool range_error = false;
|
||||
|
||||
nvs_type_t type = str_to_type(str_type);
|
||||
|
||||
if (type == NVS_TYPE_ANY) {
|
||||
return ESP_ERR_NVS_TYPE_MISMATCH;
|
||||
}
|
||||
|
||||
err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (type == NVS_TYPE_I8) {
|
||||
int32_t value = strtol(str_value, NULL, 0);
|
||||
if (value < INT8_MIN || value > INT8_MAX || errno == ERANGE) {
|
||||
range_error = true;
|
||||
} else {
|
||||
err = nvs_set_i8(nvs, key, (int8_t)value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U8) {
|
||||
uint32_t value = strtoul(str_value, NULL, 0);
|
||||
if (value > UINT8_MAX || errno == ERANGE) {
|
||||
range_error = true;
|
||||
} else {
|
||||
err = nvs_set_u8(nvs, key, (uint8_t)value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_I16) {
|
||||
int32_t value = strtol(str_value, NULL, 0);
|
||||
if (value < INT16_MIN || value > INT16_MAX || errno == ERANGE) {
|
||||
range_error = true;
|
||||
} else {
|
||||
err = nvs_set_i16(nvs, key, (int16_t)value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U16) {
|
||||
uint32_t value = strtoul(str_value, NULL, 0);
|
||||
if (value > UINT16_MAX || errno == ERANGE) {
|
||||
range_error = true;
|
||||
} else {
|
||||
err = nvs_set_u16(nvs, key, (uint16_t)value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_I32) {
|
||||
int32_t value = strtol(str_value, NULL, 0);
|
||||
if (errno != ERANGE) {
|
||||
err = nvs_set_i32(nvs, key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U32) {
|
||||
uint32_t value = strtoul(str_value, NULL, 0);
|
||||
if (errno != ERANGE) {
|
||||
err = nvs_set_u32(nvs, key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_I64) {
|
||||
int64_t value = strtoll(str_value, NULL, 0);
|
||||
if (errno != ERANGE) {
|
||||
err = nvs_set_i64(nvs, key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U64) {
|
||||
uint64_t value = strtoull(str_value, NULL, 0);
|
||||
if (errno != ERANGE) {
|
||||
err = nvs_set_u64(nvs, key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_STR) {
|
||||
err = nvs_set_str(nvs, key, str_value);
|
||||
} else if (type == NVS_TYPE_BLOB) {
|
||||
err = store_blob(nvs, key, str_value);
|
||||
}
|
||||
|
||||
if (range_error || errno == ERANGE) {
|
||||
nvs_close(nvs);
|
||||
return ESP_ERR_NVS_VALUE_TOO_LONG;
|
||||
}
|
||||
|
||||
if (err == ESP_OK) {
|
||||
log_send_messaging(MESSAGING_INFO, "Set value ok. Committing '%s'", key);
|
||||
err = nvs_commit(nvs);
|
||||
if (err == ESP_OK) {
|
||||
log_send_messaging(MESSAGING_INFO, "Value stored under key '%s'", key);
|
||||
}
|
||||
}
|
||||
|
||||
nvs_close(nvs);
|
||||
return err;
|
||||
}
|
||||
|
||||
static esp_err_t get_value_from_nvs(const char *key, const char *str_type)
|
||||
{
|
||||
nvs_handle nvs;
|
||||
esp_err_t err;
|
||||
|
||||
nvs_type_t type = str_to_type(str_type);
|
||||
|
||||
if (type == NVS_TYPE_ANY) {
|
||||
return ESP_ERR_NVS_TYPE_MISMATCH;
|
||||
}
|
||||
|
||||
err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (type == NVS_TYPE_I8) {
|
||||
int8_t value;
|
||||
err = nvs_get_i8(nvs, key, &value);
|
||||
if (err == ESP_OK) {
|
||||
log_send_messaging(MESSAGING_INFO,"Value associated with key '%s' is %d \n", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U8) {
|
||||
uint8_t value;
|
||||
err = nvs_get_u8(nvs, key, &value);
|
||||
if (err == ESP_OK) {
|
||||
log_send_messaging(MESSAGING_INFO,"Value associated with key '%s' is %u \n", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_I16) {
|
||||
int16_t value;
|
||||
err = nvs_get_i16(nvs, key, &value);
|
||||
if (err == ESP_OK) {
|
||||
log_send_messaging(MESSAGING_INFO,"Value associated with key '%s' is %d \n", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U16) {
|
||||
uint16_t value;
|
||||
if ((err = nvs_get_u16(nvs, key, &value)) == ESP_OK) {
|
||||
log_send_messaging(MESSAGING_INFO,"Value associated with key '%s' is %u", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_I32) {
|
||||
int32_t value;
|
||||
if ((err = nvs_get_i32(nvs, key, &value)) == ESP_OK) {
|
||||
log_send_messaging(MESSAGING_INFO,"Value associated with key '%s' is %d \n", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U32) {
|
||||
uint32_t value;
|
||||
if ((err = nvs_get_u32(nvs, key, &value)) == ESP_OK) {
|
||||
log_send_messaging(MESSAGING_INFO,"Value associated with key '%s' is %u \n", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_I64) {
|
||||
int64_t value;
|
||||
if ((err = nvs_get_i64(nvs, key, &value)) == ESP_OK) {
|
||||
log_send_messaging(MESSAGING_INFO,"Value associated with key '%s' is %lld \n", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_U64) {
|
||||
uint64_t value;
|
||||
if ( (err = nvs_get_u64(nvs, key, &value)) == ESP_OK) {
|
||||
log_send_messaging(MESSAGING_INFO,"Value associated with key '%s' is %llu \n", key, value);
|
||||
}
|
||||
} else if (type == NVS_TYPE_STR) {
|
||||
size_t len=0;
|
||||
if ( (err = nvs_get_str(nvs, key, NULL, &len)) == ESP_OK) {
|
||||
char *str = (char *)malloc_init_external(len);
|
||||
if ( (err = nvs_get_str(nvs, key, str, &len)) == ESP_OK) {
|
||||
log_send_messaging(MESSAGING_INFO,"String associated with key '%s' is %s \n", key, str);
|
||||
}
|
||||
free(str);
|
||||
}
|
||||
} else if (type == NVS_TYPE_BLOB) {
|
||||
size_t len;
|
||||
if ( (err = nvs_get_blob(nvs, key, NULL, &len)) == ESP_OK) {
|
||||
char *blob = (char *)malloc_init_external(len);
|
||||
if ( (err = nvs_get_blob(nvs, key, blob, &len)) == ESP_OK) {
|
||||
log_send_messaging(MESSAGING_INFO,"Blob associated with key '%s' is %d bytes long: \n", key, len);
|
||||
print_blob(blob, len);
|
||||
}
|
||||
free(blob);
|
||||
}
|
||||
}
|
||||
|
||||
nvs_close(nvs);
|
||||
return err;
|
||||
}
|
||||
|
||||
static esp_err_t erase(const char *key)
|
||||
{
|
||||
nvs_handle nvs;
|
||||
|
||||
esp_err_t err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_erase_key(nvs, key);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_commit(nvs);
|
||||
if (err == ESP_OK) {
|
||||
log_send_messaging(MESSAGING_INFO, "Value with key '%s' erased", key);
|
||||
}
|
||||
}
|
||||
nvs_close(nvs);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static esp_err_t erase_all(const char *name)
|
||||
{
|
||||
nvs_handle nvs;
|
||||
|
||||
esp_err_t err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_erase_all(nvs);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_commit(nvs);
|
||||
}
|
||||
}
|
||||
|
||||
log_send_messaging(MESSAGING_INFO, "Namespace '%s' was %s erased", name, (err == ESP_OK) ? "" : "not");
|
||||
nvs_close(nvs);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static int set_value(int argc, char **argv)
|
||||
{
|
||||
ESP_LOGD(TAG, "%s %u - Parsing keys ",__func__,__LINE__);
|
||||
int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&set_args);
|
||||
if (nerrors != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *key = set_args.key->sval[0];
|
||||
const char *type = set_args.type->sval[0];
|
||||
const char *values = set_args.value->sval[0];
|
||||
cmd_send_messaging(argv[0],MESSAGING_INFO, "Setting '%s' (type %s)", key,type);
|
||||
esp_err_t err = set_value_in_nvs(key, type, values);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR, "%s", esp_err_to_name(err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
static int get_value(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&get_args);
|
||||
if (nerrors != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *key = get_args.key->sval[0];
|
||||
const char *type = get_args.type->sval[0];
|
||||
|
||||
esp_err_t err = get_value_from_nvs(key, type);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR, "%s", esp_err_to_name(err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int erase_value(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&erase_args);
|
||||
if (nerrors != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *key = erase_args.key->sval[0];
|
||||
|
||||
esp_err_t err = erase(key);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR, "%s", esp_err_to_name(err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int erase_namespace(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&erase_all_args);
|
||||
if (nerrors != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *name = erase_all_args.namespace->sval[0];
|
||||
|
||||
esp_err_t err = erase_all(name);
|
||||
if (err != ESP_OK) {
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR, "%s", esp_err_to_name(err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int erase_network_manager(int argc, char **argv)
|
||||
{
|
||||
nvs_handle nvs;
|
||||
esp_err_t err = nvs_open("config", NVS_READWRITE, &nvs);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_erase_all(nvs);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_commit(nvs);
|
||||
}
|
||||
}
|
||||
nvs_close(nvs);
|
||||
if (err != ESP_OK) {
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR, "System configuration was not erased. %s", esp_err_to_name(err));
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
cmd_send_messaging(argv[0],MESSAGING_WARNING, "system configuration was erased. Please reboot.");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int wifi_erase_config(int argc, char **argv)
|
||||
{
|
||||
esp_err_t err=ESP_OK;
|
||||
esp_err_t err_ap_list=ESP_OK;
|
||||
bool done = false;
|
||||
int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&wifi_erase_args);
|
||||
if (nerrors != 0) {
|
||||
return 1;
|
||||
}
|
||||
if(wifi_erase_args.ap_list->count>0){
|
||||
err_ap_list = network_wifi_erase_known_ap();
|
||||
if (err_ap_list != ESP_OK) {
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR, "Could not erase legacy wifi configuration: %s", esp_err_to_name(err));
|
||||
}
|
||||
else {
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR, "Legacy wifi configuration was erased");
|
||||
}
|
||||
done = true;
|
||||
}
|
||||
if(wifi_erase_args.legacy->count>0){
|
||||
err = network_wifi_erase_legacy();
|
||||
if (err != ESP_OK) {
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR, "Could not erase known ap list : %s", esp_err_to_name(err));
|
||||
}
|
||||
else {
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR, "Known access point list was erased");
|
||||
}
|
||||
done = true;
|
||||
}
|
||||
if(!done){
|
||||
cmd_send_messaging(argv[0],MESSAGING_WARNING, "Please specify at least one configuration type to erase.", esp_err_to_name(err));
|
||||
}
|
||||
return (err_ap_list==ESP_OK && err==ESP_OK)?0:1;
|
||||
}
|
||||
|
||||
static int list(const char *part, const char *name, const char *str_type)
|
||||
{
|
||||
nvs_type_t type = str_to_type(str_type);
|
||||
|
||||
nvs_iterator_t it = nvs_entry_find(part, NULL, type);
|
||||
if (it == NULL) {
|
||||
log_send_messaging(MESSAGING_ERROR, "No such enty was found");
|
||||
return 1;
|
||||
}
|
||||
|
||||
do {
|
||||
nvs_entry_info_t info;
|
||||
nvs_entry_info(it, &info);
|
||||
it = nvs_entry_next(it);
|
||||
|
||||
log_send_messaging(MESSAGING_INFO, "namespace '%s', key '%s', type '%s' \n",
|
||||
info.namespace_name, info.key, type_to_str(info.type));
|
||||
} while (it != NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
static int list_entries(int argc, char **argv)
|
||||
{
|
||||
list_args.partition->sval[0] = "";
|
||||
list_args.namespace->sval[0] = "";
|
||||
list_args.type->sval[0] = "";
|
||||
|
||||
int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&list_args);
|
||||
if (nerrors != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *part = list_args.partition->sval[0];
|
||||
const char *name = list_args.namespace->sval[0];
|
||||
const char *type = list_args.type->sval[0];
|
||||
|
||||
return list(part, name, type);
|
||||
}
|
||||
void register_nvs()
|
||||
{
|
||||
set_args.key = arg_str1(NULL, NULL, "<key>", "key of the value to be set");
|
||||
set_args.type = arg_str1(NULL, NULL, "<type>", ARG_TYPE_STR);
|
||||
set_args.value = arg_str1("v", "value", "<value>", "value to be stored");
|
||||
set_args.end = arg_end(2);
|
||||
|
||||
get_args.key = arg_str1(NULL, NULL, "<key>", "key of the value to be read");
|
||||
get_args.type = arg_str1(NULL, NULL, "<type>", ARG_TYPE_STR);
|
||||
get_args.end = arg_end(2);
|
||||
|
||||
erase_args.key = arg_str1(NULL, NULL, "<key>", "key of the value to be erased");
|
||||
erase_args.end = arg_end(2);
|
||||
|
||||
erase_all_args.namespace = arg_str1(NULL, NULL, "<namespace>", "namespace to be erased");
|
||||
erase_all_args.end = arg_end(2);
|
||||
|
||||
wifi_erase_args.ap_list = arg_lit0("a","ap_list","Erases Known access points list");
|
||||
wifi_erase_args.legacy = arg_lit0("l","legacy","Erases legacy access point storage");
|
||||
wifi_erase_args.end = arg_end(1);
|
||||
|
||||
list_args.partition = arg_str1(NULL, NULL, "<partition>", "partition name");
|
||||
list_args.namespace = arg_str0("n", "namespace", "<namespace>", "namespace name");
|
||||
list_args.type = arg_str0("t", "type", "<type>", ARG_TYPE_STR);
|
||||
list_args.end = arg_end(2);
|
||||
const esp_console_cmd_t set_cmd = {
|
||||
.command = "nvs_set",
|
||||
.help = "Set variable in selected namespace. Blob type must be comma separated list of hex values. \n"
|
||||
"Examples:\n"
|
||||
" nvs_set VarName i32 -v 123 \n"
|
||||
" nvs_set VarName srt -v YourString \n"
|
||||
" nvs_set VarName blob -v 0123456789abcdef \n",
|
||||
.hint = NULL,
|
||||
.func = &set_value,
|
||||
.argtable = &set_args
|
||||
};
|
||||
|
||||
const esp_console_cmd_t get_cmd = {
|
||||
.command = "nvs_get",
|
||||
.help = "Get variable from selected namespace. \n"
|
||||
"Example: nvs_get VarName i32",
|
||||
.hint = NULL,
|
||||
.func = &get_value,
|
||||
.argtable = &get_args
|
||||
};
|
||||
|
||||
const esp_console_cmd_t erase_cmd = {
|
||||
.command = "nvs_erase",
|
||||
.help = "Erase variable from current namespace",
|
||||
.hint = NULL,
|
||||
.func = &erase_value,
|
||||
.argtable = &erase_args
|
||||
};
|
||||
|
||||
const esp_console_cmd_t erase_namespace_cmd = {
|
||||
.command = "nvs_erase_namespace",
|
||||
.help = "Erases specified namespace",
|
||||
.hint = NULL,
|
||||
.func = &erase_namespace,
|
||||
.argtable = &erase_all_args
|
||||
};
|
||||
const esp_console_cmd_t erase_config_cmd = {
|
||||
.command = "wifi_erase_config",
|
||||
.help = "Erases all stored access points from flash",
|
||||
.hint = NULL,
|
||||
.func = &wifi_erase_config,
|
||||
.argtable = &wifi_erase_args
|
||||
};
|
||||
|
||||
const esp_console_cmd_t erase_networkmanager_cmd = {
|
||||
.command = "nvs_erase_configuration",
|
||||
.help = "Erases system's configuration",
|
||||
.hint = NULL,
|
||||
.func = &erase_network_manager,
|
||||
.argtable = NULL
|
||||
};
|
||||
|
||||
|
||||
const esp_console_cmd_t list_entries_cmd = {
|
||||
.command = "nvs_list",
|
||||
.help = "List stored key-value pairs stored in NVS."
|
||||
"Namespace and type can be specified to print only those key-value pairs.\n"
|
||||
"Following command list variables stored inside 'nvs' partition, under namespace 'storage' with type uint32_t"
|
||||
"Example: nvs_list nvs -n storage -t u32 \n",
|
||||
.hint = NULL,
|
||||
.func = &list_entries,
|
||||
.argtable = &list_args
|
||||
};
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("registering list_entries_cmd");
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&list_entries_cmd));
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("registering set_cmd");
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&set_cmd));
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("registering get_cmd");
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&get_cmd));
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("registering erase_cmd");
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&erase_cmd));
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("registering erase_namespace_cmd");
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&erase_namespace_cmd));
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("registering erase_config_cmd");
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&erase_networkmanager_cmd));
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("registering erase_config_cmd");
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&erase_config_cmd));
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("Done");
|
||||
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
extern }
|
||||
#endif
|
||||
22
lib/platform_console/cmd_nvs.h
Normal file
22
lib/platform_console/cmd_nvs.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/* Console example — declarations of command registration functions.
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#pragma once
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Register NVS functions
|
||||
void register_nvs();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
74
lib/platform_console/cmd_ota.c
Normal file
74
lib/platform_console/cmd_ota.c
Normal file
@@ -0,0 +1,74 @@
|
||||
/* Console example — various system commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include "cmd_ota.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_console.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_sleep.h"
|
||||
#include "esp_spi_flash.h"
|
||||
#include "driver/rtc_io.h"
|
||||
#include "driver/uart.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "soc/rtc_cntl_reg.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "platform_console.h"
|
||||
#include "messaging.h"
|
||||
|
||||
static const char * TAG = "ota";
|
||||
extern esp_err_t start_ota(const char * bin_url);
|
||||
static struct {
|
||||
struct arg_str *url;
|
||||
struct arg_end *end;
|
||||
} ota_args;
|
||||
/* 'heap' command prints minumum heap size */
|
||||
static int perform_ota_update(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&ota_args);
|
||||
if (nerrors != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *url = ota_args.url->sval[0];
|
||||
|
||||
esp_err_t err=ESP_OK;
|
||||
ESP_LOGI(TAG, "Starting ota: %s", url);
|
||||
start_ota(url);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "%s", esp_err_to_name(err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void register_ota_cmd()
|
||||
{
|
||||
ota_args.url= arg_str1(NULL, NULL, "<url>", "url of the binary app file");
|
||||
ota_args.end = arg_end(2);
|
||||
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "update",
|
||||
.help = "Updates the application binary from the provided URL",
|
||||
.hint = NULL,
|
||||
.func = &perform_ota_update,
|
||||
.argtable = &ota_args
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
|
||||
|
||||
|
||||
19
lib/platform_console/cmd_ota.h
Normal file
19
lib/platform_console/cmd_ota.h
Normal file
@@ -0,0 +1,19 @@
|
||||
/* Console example — various system commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Register system functions
|
||||
void register_ota_cmd();
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
854
lib/platform_console/cmd_system.c
Normal file
854
lib/platform_console/cmd_system.c
Normal file
@@ -0,0 +1,854 @@
|
||||
/* Console example — various system commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_console.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_spi_flash.h"
|
||||
#include "driver/rtc_io.h"
|
||||
#include "driver/uart.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "soc/rtc_cntl_reg.h"
|
||||
#include "esp_rom_uart.h"
|
||||
#include "cmd_system.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_partition.h"
|
||||
#include "esp_ota_ops.h"
|
||||
#include "platform_esp32.h"
|
||||
#include "platform_config.h"
|
||||
#include "esp_sleep.h"
|
||||
#include "messaging.h"
|
||||
#include "platform_console.h"
|
||||
#include "tools.h"
|
||||
|
||||
#ifdef CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
|
||||
#pragma message("Runtime stats enabled")
|
||||
#define WITH_TASKS_INFO 1
|
||||
#else
|
||||
#pragma message("Runtime stats disabled")
|
||||
#endif
|
||||
EXT_RAM_ATTR static struct {
|
||||
struct arg_str *name;
|
||||
struct arg_end *end;
|
||||
} name_args;
|
||||
EXT_RAM_ATTR static struct {
|
||||
#if CONFIG_CSPOT_SINK
|
||||
struct arg_lit *cspot;
|
||||
#endif
|
||||
struct arg_lit *btspeaker;
|
||||
struct arg_lit *airplay;
|
||||
struct arg_str *telnet;
|
||||
|
||||
#if WITH_TASKS_INFO
|
||||
struct arg_lit *stats;
|
||||
#endif
|
||||
struct arg_end *end;
|
||||
} set_services_args;
|
||||
static const char * TAG = "cmd_system";
|
||||
|
||||
//static void register_setbtsource();
|
||||
static void register_free();
|
||||
static void register_setdevicename();
|
||||
static void register_heap();
|
||||
static void register_dump_heap();
|
||||
static void register_abort();
|
||||
static void register_version();
|
||||
static void register_restart();
|
||||
#if CONFIG_WITH_CONFIG_UI
|
||||
static void register_deep_sleep();
|
||||
static void register_light_sleep();
|
||||
#endif
|
||||
static void register_factory_boot();
|
||||
static void register_restart_ota();
|
||||
static void register_set_services();
|
||||
#if WITH_TASKS_INFO
|
||||
static void register_tasks();
|
||||
#endif
|
||||
extern BaseType_t network_manager_task;
|
||||
FILE * system_open_memstream(const char * cmdname,char **buf,size_t *buf_size){
|
||||
FILE *f = open_memstream(buf, buf_size);
|
||||
if (f == NULL) {
|
||||
cmd_send_messaging(cmdname,MESSAGING_ERROR,"Unable to open memory stream.");
|
||||
}
|
||||
return f;
|
||||
}
|
||||
void register_system()
|
||||
{
|
||||
|
||||
register_setdevicename();
|
||||
register_set_services();
|
||||
register_free();
|
||||
register_heap();
|
||||
register_dump_heap();
|
||||
register_abort();
|
||||
register_version();
|
||||
register_restart();
|
||||
register_factory_boot();
|
||||
register_restart_ota();
|
||||
#if WITH_TASKS_INFO
|
||||
register_tasks();
|
||||
#endif
|
||||
#if CONFIG_WITH_CONFIG_UI
|
||||
register_deep_sleep();
|
||||
register_light_sleep();
|
||||
#endif
|
||||
}
|
||||
void simple_restart()
|
||||
{
|
||||
log_send_messaging(MESSAGING_WARNING,"Rebooting.");
|
||||
if(!wait_for_commit()){
|
||||
log_send_messaging(MESSAGING_WARNING,"Unable to commit configuration. ");
|
||||
}
|
||||
vTaskDelay(750/ portTICK_PERIOD_MS);
|
||||
esp_restart();
|
||||
}
|
||||
/* 'version' command */
|
||||
static int get_version(int argc, char **argv)
|
||||
{
|
||||
esp_chip_info_t info;
|
||||
esp_chip_info(&info);
|
||||
cmd_send_messaging(argv[0],MESSAGING_INFO,
|
||||
"IDF Version:%s\r\n"
|
||||
"Chip info:\r\n"
|
||||
"\tmodel:%s\r\n"
|
||||
"\tcores:%d\r\n"
|
||||
"\tfeature:%s%s%s%s%d%s\r\n"
|
||||
"\trevision number:%d\r\n",
|
||||
esp_get_idf_version(), info.model == CHIP_ESP32 ? "ESP32" : "Unknow", info.cores,
|
||||
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", info.revision);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void register_version()
|
||||
{
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "version",
|
||||
.help = "Get version of chip and SDK",
|
||||
.hint = NULL,
|
||||
.func = &get_version,
|
||||
};
|
||||
cmd_to_json(&cmd);
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
|
||||
/* 'abort' command */
|
||||
static int cmd_abort(int argc, char **argv)
|
||||
{
|
||||
cmd_send_messaging(argv[0],MESSAGING_INFO,"ABORT!\r\n");
|
||||
|
||||
abort();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void register_abort()
|
||||
{
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "abort",
|
||||
.help = "Crash now!",
|
||||
.hint = NULL,
|
||||
.func = &cmd_abort,
|
||||
};
|
||||
cmd_to_json(&cmd);
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
|
||||
esp_err_t guided_boot(esp_partition_subtype_t partition_subtype)
|
||||
{
|
||||
if(is_recovery_running){
|
||||
if(partition_subtype ==ESP_PARTITION_SUBTYPE_APP_FACTORY){
|
||||
// log_send_messaging(MESSAGING_WARNING,"RECOVERY application is already active");
|
||||
simple_restart();
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(partition_subtype !=ESP_PARTITION_SUBTYPE_APP_FACTORY){
|
||||
// log_send_messaging(MESSAGING_WARNING,"SQUEEZELITE application is already active");
|
||||
simple_restart();
|
||||
}
|
||||
}
|
||||
esp_err_t err = ESP_OK;
|
||||
// log_send_messaging(MESSAGING_INFO, "Looking for partition type %u",partition_subtype);
|
||||
const esp_partition_t *partition;
|
||||
esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP, partition_subtype, NULL);
|
||||
|
||||
if(it == NULL){
|
||||
log_send_messaging(MESSAGING_ERROR,"Reboot failed. Partitions error");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGD(TAG, "Found partition. Getting info.");
|
||||
partition = (esp_partition_t *) esp_partition_get(it);
|
||||
ESP_LOGD(TAG, "Releasing partition iterator");
|
||||
esp_partition_iterator_release(it);
|
||||
if(partition != NULL){
|
||||
log_send_messaging(MESSAGING_INFO, "Rebooting to %s", partition->label);
|
||||
err=esp_ota_set_boot_partition(partition);
|
||||
if(err!=ESP_OK){
|
||||
log_send_messaging(MESSAGING_ERROR,"Unable to select partition for reboot: %s",esp_err_to_name(err));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log_send_messaging(MESSAGING_ERROR,"partition type %u not found! Unable to reboot to recovery.",partition_subtype);
|
||||
|
||||
}
|
||||
ESP_LOGD(TAG, "Yielding to other processes");
|
||||
taskYIELD();
|
||||
simple_restart();
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static int restart(int argc, char **argv)
|
||||
{
|
||||
simple_restart();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
esp_err_t guided_restart_ota(){
|
||||
log_send_messaging(MESSAGING_WARNING,"Booting to Squeezelite");
|
||||
guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0);
|
||||
return ESP_FAIL; // return fail. This should never return... we're rebooting!
|
||||
}
|
||||
esp_err_t guided_factory(){
|
||||
log_send_messaging(MESSAGING_WARNING,"Booting to recovery");
|
||||
guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
|
||||
return ESP_FAIL; // return fail. This should never return... we're rebooting!
|
||||
}
|
||||
static int restart_factory(int argc, char **argv)
|
||||
{
|
||||
cmd_send_messaging(argv[0],MESSAGING_WARNING, "Booting to Recovery");
|
||||
guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
|
||||
return 0; // return fail. This should never return... we're rebooting!
|
||||
}
|
||||
static int restart_ota(int argc, char **argv)
|
||||
{
|
||||
cmd_send_messaging(argv[0],MESSAGING_WARNING, "Booting to Squeezelite");
|
||||
guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0);
|
||||
return 0; // return fail. This should never return... we're rebooting!
|
||||
}
|
||||
static void register_restart()
|
||||
{
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "restart",
|
||||
.help = "Reboot system",
|
||||
.hint = NULL,
|
||||
.func = &restart,
|
||||
};
|
||||
#if CONFIG_WITH_CONFIG_UI
|
||||
cmd_to_json(&cmd);
|
||||
#endif
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
static void register_restart_ota()
|
||||
{
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "restart_ota",
|
||||
.help = "Reboot system to Squeezelite",
|
||||
.hint = NULL,
|
||||
.func = &restart_ota,
|
||||
};
|
||||
#if CONFIG_WITH_CONFIG_UI
|
||||
cmd_to_json(&cmd);
|
||||
#endif
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
|
||||
static void register_factory_boot()
|
||||
{
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "recovery",
|
||||
.help = "Reboot system to Recovery",
|
||||
.hint = NULL,
|
||||
.func = &restart_factory,
|
||||
};
|
||||
#if CONFIG_WITH_CONFIG_UI
|
||||
cmd_to_json(&cmd);
|
||||
#endif
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
/** 'free' command prints available heap memory */
|
||||
|
||||
static int free_mem(int argc, char **argv)
|
||||
{
|
||||
cmd_send_messaging(argv[0],MESSAGING_INFO,"%d", esp_get_free_heap_size());
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void register_free()
|
||||
{
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "free",
|
||||
.help = "Get free heap memory",
|
||||
.hint = NULL,
|
||||
.func = &free_mem,
|
||||
};
|
||||
#if CONFIG_WITH_CONFIG_UI
|
||||
cmd_to_json(&cmd);
|
||||
#endif
|
||||
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
static int dump_heap(int argc, char **argv)
|
||||
{
|
||||
ESP_LOGD(TAG, "Dumping heap");
|
||||
heap_caps_dump_all();
|
||||
return 0;
|
||||
}
|
||||
/* 'heap' command prints minumum heap size */
|
||||
static int heap_size(int argc, char **argv)
|
||||
{
|
||||
// ESP_LOGI(TAG,"Heap internal:%zu (min:%zu) (largest block:%zu)\nexternal:%zu (min:%zu) (largest block:%zd)\ndma :%zu (min:%zu) (largest block:%zd)",
|
||||
// heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
|
||||
// heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
|
||||
// heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL),
|
||||
// heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
|
||||
// heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM),
|
||||
// heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM),
|
||||
// heap_caps_get_free_size(MALLOC_CAP_DMA),
|
||||
// heap_caps_get_minimum_free_size(MALLOC_CAP_DMA),
|
||||
// heap_caps_get_largest_free_block(MALLOC_CAP_DMA));
|
||||
cmd_send_messaging(argv[0],MESSAGING_INFO,"Heap internal:%zu (min:%zu) (largest block:%zu)\nexternal:%zu (min:%zu) (largest block:%zd)\ndma :%zu (min:%zu) (largest block:%zd)",
|
||||
heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
|
||||
heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
|
||||
heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL),
|
||||
heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
|
||||
heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM),
|
||||
heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM),
|
||||
heap_caps_get_free_size(MALLOC_CAP_DMA),
|
||||
heap_caps_get_minimum_free_size(MALLOC_CAP_DMA),
|
||||
heap_caps_get_largest_free_block(MALLOC_CAP_DMA));
|
||||
return 0;
|
||||
}
|
||||
cJSON * setdevicename_cb(){
|
||||
char * default_host_name = config_alloc_get_str("host_name",NULL,"Squeezelite");
|
||||
cJSON * values = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(values,"name",default_host_name);
|
||||
free(default_host_name);
|
||||
return values;
|
||||
}
|
||||
static int setnamevar(char * nvsname, FILE *f, char * value){
|
||||
esp_err_t err=ESP_OK;
|
||||
if((err=config_set_value(NVS_TYPE_STR, nvsname, value))!=ESP_OK){
|
||||
fprintf(f,"Unable to set %s=%s. %s\n",nvsname,value,esp_err_to_name(err));
|
||||
}
|
||||
return err==ESP_OK?0:1;
|
||||
}
|
||||
typedef enum {
|
||||
SCANNING,
|
||||
PROCESSING_NAME
|
||||
} scanstate_t;
|
||||
int set_cspot_player_name(FILE * f,const char * name){
|
||||
int ret=0;
|
||||
cJSON * cspot_config = config_alloc_get_cjson("cspot_config");
|
||||
if(cspot_config==NULL){
|
||||
fprintf(f,"Unable to get cspot_config\n");
|
||||
return 1;
|
||||
}
|
||||
cJSON * player_name = cJSON_GetObjectItemCaseSensitive(cspot_config,"deviceName");
|
||||
if(player_name==NULL){
|
||||
fprintf(f,"Unable to get deviceName\n");
|
||||
ret=1;
|
||||
}
|
||||
if(strcmp(player_name->valuestring,name)==0){
|
||||
fprintf(f,"CSpot device name not changed.\n");
|
||||
ret=0;
|
||||
}
|
||||
else{
|
||||
cJSON_SetValuestring(player_name,name);
|
||||
if(setnamevar("cspot_config",f,cJSON_Print(cspot_config))!=0){
|
||||
fprintf(f,"Unable to set cspot_config\n");
|
||||
ret=1;
|
||||
}
|
||||
else{
|
||||
fprintf(f,"CSpot device name set to %s\n",name);
|
||||
}
|
||||
}
|
||||
cJSON_Delete(cspot_config);
|
||||
return ret;
|
||||
}
|
||||
int set_squeezelite_player_name(FILE * f,const char * name){
|
||||
char * nvs_config= config_alloc_get(NVS_TYPE_STR, "autoexec1");
|
||||
char **argv = NULL;
|
||||
esp_err_t err=ESP_OK;
|
||||
int nerrors=0;
|
||||
bool bFoundParm=false;
|
||||
scanstate_t state=SCANNING;
|
||||
char * newCommandLine = NULL;
|
||||
char * parm = " -n ";
|
||||
char * cleaned_name = strdup(name);
|
||||
for(char * p=cleaned_name;*p!='\0';p++){
|
||||
if(*p == ' '){
|
||||
*p='_'; // no spaces allowed
|
||||
}
|
||||
}
|
||||
if(nvs_config && strlen(nvs_config)>0){
|
||||
// allocate enough memory to hold the new command line
|
||||
size_t cmdLength = strlen(nvs_config) + strlen(cleaned_name) + strlen(parm) +1 ;
|
||||
newCommandLine = malloc_init_external(cmdLength);
|
||||
ESP_LOGD(TAG,"Parsing command %s",nvs_config);
|
||||
argv = (char **) malloc_init_external(22* sizeof(char *));
|
||||
if (argv == NULL) {
|
||||
FREE_AND_NULL(nvs_config);
|
||||
return 1;
|
||||
}
|
||||
size_t argc = esp_console_split_argv(nvs_config, argv,22);
|
||||
for(int i=0;i<argc;i++) {
|
||||
if(i>0){
|
||||
strcat(newCommandLine," ");
|
||||
}
|
||||
switch (state)
|
||||
{
|
||||
case SCANNING:
|
||||
strcat(newCommandLine,argv[i]);
|
||||
if(strcasecmp(argv[i],"--name")==0 || strcasecmp(argv[i],"-n")==0 ){
|
||||
state = PROCESSING_NAME;
|
||||
}
|
||||
break;
|
||||
case PROCESSING_NAME:
|
||||
bFoundParm=true;
|
||||
strcat(newCommandLine,cleaned_name);
|
||||
state = SCANNING;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!bFoundParm){
|
||||
strcat(newCommandLine,parm);
|
||||
strcat(newCommandLine,name);
|
||||
}
|
||||
fprintf(f,"Squeezelite player name changed to %s\n",newCommandLine);
|
||||
if((err=config_set_value(NVS_TYPE_STR, "autoexec1",newCommandLine))!=ESP_OK){
|
||||
nerrors++;
|
||||
fprintf(f,"Failed updating squeezelite command. %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
FREE_AND_NULL(nvs_config);
|
||||
FREE_AND_NULL(argv);
|
||||
free(cleaned_name);
|
||||
return nerrors;
|
||||
|
||||
}
|
||||
static int setdevicename(int argc, char **argv)
|
||||
{
|
||||
char * name = NULL;
|
||||
int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&name_args);
|
||||
if (nerrors != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Check "--name" option */
|
||||
if (name_args.name->count) {
|
||||
name=strdup_psram(name_args.name->sval[0]);
|
||||
}
|
||||
else {
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR,"Name must be specified.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *buf = NULL;
|
||||
size_t buf_size = 0;
|
||||
FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
|
||||
if (f == NULL) {
|
||||
return 1;
|
||||
}
|
||||
nerrors+=setnamevar("a2dp_dev_name", f, name);
|
||||
nerrors+=setnamevar("airplay_name", f, name);
|
||||
nerrors+=setnamevar("ap_ssid", f, name);
|
||||
nerrors+=setnamevar("bt_name", f, name);
|
||||
nerrors+=setnamevar("host_name", f, name);
|
||||
nerrors+=set_squeezelite_player_name(f, name);
|
||||
nerrors+=set_cspot_player_name(f, name);
|
||||
if(nerrors==0){
|
||||
fprintf(f,"Device name changed to %s\n",name);
|
||||
}
|
||||
if(!nerrors ){
|
||||
fprintf(f,"Done.\n");
|
||||
}
|
||||
FREE_AND_NULL(name);
|
||||
fflush (f);
|
||||
cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
|
||||
fclose(f);
|
||||
FREE_AND_NULL(buf);
|
||||
return nerrors;
|
||||
|
||||
}
|
||||
|
||||
static void register_heap()
|
||||
{
|
||||
const esp_console_cmd_t heap_cmd = {
|
||||
.command = "heap",
|
||||
.help = "Get minimum size of free heap memory",
|
||||
.hint = NULL,
|
||||
.func = &heap_size,
|
||||
};
|
||||
#if CONFIG_WITH_CONFIG_UI
|
||||
cmd_to_json(&heap_cmd);
|
||||
#endif
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&heap_cmd) );
|
||||
|
||||
}
|
||||
|
||||
static void register_dump_heap()
|
||||
{
|
||||
const esp_console_cmd_t heap_cmd = {
|
||||
.command = "dump_heap",
|
||||
.help = "Dumps the content of the heap to serial output",
|
||||
.hint = NULL,
|
||||
.func = &dump_heap,
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&heap_cmd) );
|
||||
|
||||
}
|
||||
|
||||
static void register_setdevicename()
|
||||
{
|
||||
char * default_host_name = config_alloc_get_str("host_name",NULL,"Squeezelite");
|
||||
name_args.name = arg_str0("n", "name", default_host_name, "New Name");
|
||||
name_args.end = arg_end(8);
|
||||
const esp_console_cmd_t set_name= {
|
||||
.command = CFG_TYPE_SYST("name"),
|
||||
.help="Device Name",
|
||||
.hint = NULL,
|
||||
.func = &setdevicename,
|
||||
.argtable = &name_args
|
||||
};
|
||||
|
||||
cmd_to_json_with_cb(&set_name,&setdevicename_cb);
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&set_name));
|
||||
}
|
||||
/** 'tasks' command prints the list of tasks and related information */
|
||||
#if WITH_TASKS_INFO
|
||||
|
||||
static int tasks_info(int argc, char **argv)
|
||||
{
|
||||
const size_t bytes_per_task = 40; /* see vTaskList description */
|
||||
char *task_list_buffer = malloc_init_external(uxTaskGetNumberOfTasks() * bytes_per_task);
|
||||
if (task_list_buffer == NULL) {
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR, "failed to allocate buffer for vTaskList output");
|
||||
return 1;
|
||||
}
|
||||
cmd_send_messaging(argv[0],MESSAGING_INFO,"Task Name\tStatus\tPrio\tHWM\tTask#"
|
||||
#ifdef CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
|
||||
"\tAffinity"
|
||||
#endif
|
||||
"\n");
|
||||
vTaskList(task_list_buffer);
|
||||
cmd_send_messaging(argv[0],MESSAGING_INFO,"%s", task_list_buffer);
|
||||
free(task_list_buffer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void register_tasks()
|
||||
{
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "tasks",
|
||||
.help = "Get information about running tasks",
|
||||
.hint = NULL,
|
||||
.func = &tasks_info,
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
|
||||
#endif // WITH_TASKS_INFO
|
||||
|
||||
|
||||
/** 'deep_sleep' command puts the chip into deep sleep mode */
|
||||
#if CONFIG_WITH_CONFIG_UI
|
||||
static struct {
|
||||
struct arg_int *wakeup_time;
|
||||
struct arg_int *wakeup_gpio_num;
|
||||
struct arg_int *wakeup_gpio_level;
|
||||
struct arg_end *end;
|
||||
} deep_sleep_args;
|
||||
|
||||
|
||||
static int deep_sleep(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&deep_sleep_args);
|
||||
if (nerrors != 0) {
|
||||
return 1;
|
||||
}
|
||||
if (deep_sleep_args.wakeup_time->count) {
|
||||
uint64_t timeout = 1000ULL * deep_sleep_args.wakeup_time->ival[0];
|
||||
cmd_send_messaging(argv[0],MESSAGING_INFO, "Enabling timer wakeup, timeout=%lluus", timeout);
|
||||
ESP_ERROR_CHECK( esp_sleep_enable_timer_wakeup(timeout) );
|
||||
}
|
||||
if (deep_sleep_args.wakeup_gpio_num->count) {
|
||||
int io_num = deep_sleep_args.wakeup_gpio_num->ival[0];
|
||||
if (!rtc_gpio_is_valid_gpio(io_num)) {
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR, "GPIO %d is not an RTC IO", io_num);
|
||||
return 1;
|
||||
}
|
||||
int level = 0;
|
||||
if (deep_sleep_args.wakeup_gpio_level->count) {
|
||||
level = deep_sleep_args.wakeup_gpio_level->ival[0];
|
||||
if (level != 0 && level != 1) {
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR, "Invalid wakeup level: %d", level);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
cmd_send_messaging(argv[0],MESSAGING_INFO, "Enabling wakeup on GPIO%d, wakeup on %s level",
|
||||
io_num, level ? "HIGH" : "LOW");
|
||||
|
||||
ESP_ERROR_CHECK( esp_sleep_enable_ext1_wakeup(1ULL << io_num, level) );
|
||||
}
|
||||
rtc_gpio_isolate(GPIO_NUM_12);
|
||||
esp_deep_sleep_start();
|
||||
return 0; // this code will never run. deep sleep will cause the system to restart
|
||||
}
|
||||
|
||||
static void register_deep_sleep()
|
||||
{
|
||||
deep_sleep_args.wakeup_time =
|
||||
arg_int0("t", "time", "<t>", "Wake up time, ms");
|
||||
deep_sleep_args.wakeup_gpio_num =
|
||||
arg_int0(NULL, "io", "<n>",
|
||||
"If specified, wakeup using GPIO with given number");
|
||||
deep_sleep_args.wakeup_gpio_level =
|
||||
arg_int0(NULL, "io_level", "<0|1>", "GPIO level to trigger wakeup");
|
||||
deep_sleep_args.end = arg_end(3);
|
||||
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "deep_sleep",
|
||||
.help = "Enter deep sleep mode. ",
|
||||
.hint = NULL,
|
||||
.func = &deep_sleep,
|
||||
.argtable = &deep_sleep_args
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
#endif
|
||||
|
||||
static int enable_disable(FILE * f,char * nvs_name, struct arg_lit *arg){
|
||||
esp_err_t err = config_set_value(NVS_TYPE_STR, nvs_name, arg->count>0?"Y":"N");
|
||||
const char * name = arg->hdr.longopts?arg->hdr.longopts:arg->hdr.glossary;
|
||||
|
||||
if(err!=ESP_OK){
|
||||
fprintf(f,"Error %s %s. %s\n",arg->count>0?"Enabling":"Disabling", name, esp_err_to_name(err));
|
||||
}
|
||||
else {
|
||||
fprintf(f,"%s %s\n",arg->count>0?"Enabled":"Disabled",name);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
static int do_set_services(int argc, char **argv)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&set_services_args);
|
||||
if (nerrors != 0) {
|
||||
return 1;
|
||||
}
|
||||
char *buf = NULL;
|
||||
size_t buf_size = 0;
|
||||
FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
|
||||
if (f == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
nerrors += enable_disable(f,"enable_airplay",set_services_args.airplay);
|
||||
nerrors += enable_disable(f,"enable_bt_sink",set_services_args.btspeaker);
|
||||
#if CONFIG_CSPOT_SINK
|
||||
nerrors += enable_disable(f,"enable_cspot",set_services_args.cspot);
|
||||
#endif
|
||||
|
||||
if(set_services_args.telnet->count>0){
|
||||
if(strcasecmp(set_services_args.telnet->sval[0],"Disabled") == 0){
|
||||
err = config_set_value(NVS_TYPE_STR, "telnet_enable", "N");
|
||||
}
|
||||
else if(strcasecmp(set_services_args.telnet->sval[0],"Telnet Only") == 0){
|
||||
err = config_set_value(NVS_TYPE_STR, "telnet_enable", "Y");
|
||||
}
|
||||
else if(strcasecmp(set_services_args.telnet->sval[0],"Telnet and Serial") == 0){
|
||||
err = config_set_value(NVS_TYPE_STR, "telnet_enable", "D");
|
||||
}
|
||||
|
||||
if(err!=ESP_OK){
|
||||
nerrors++;
|
||||
fprintf(f,"Error setting telnet to %s. %s\n",set_services_args.telnet->sval[0], esp_err_to_name(err));
|
||||
}
|
||||
else {
|
||||
fprintf(f,"Telnet service changed to %s\n",set_services_args.telnet->sval[0]);
|
||||
}
|
||||
}
|
||||
|
||||
#if WITH_TASKS_INFO
|
||||
nerrors += enable_disable(f,"stats",set_services_args.stats);
|
||||
#endif
|
||||
if(!nerrors ){
|
||||
fprintf(f,"Done.\n");
|
||||
}
|
||||
fflush (f);
|
||||
cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
|
||||
fclose(f);
|
||||
FREE_AND_NULL(buf);
|
||||
return nerrors;
|
||||
}
|
||||
|
||||
|
||||
cJSON * set_services_cb(){
|
||||
cJSON * values = cJSON_CreateObject();
|
||||
char * p=NULL;
|
||||
console_set_bool_parameter(values,"enable_bt_sink",set_services_args.btspeaker);
|
||||
console_set_bool_parameter(values,"enable_airplay",set_services_args.airplay);
|
||||
#if CONFIG_CSPOT_SINK
|
||||
console_set_bool_parameter(values,"enable_cspot",set_services_args.cspot);
|
||||
#endif
|
||||
#if WITH_TASKS_INFO
|
||||
console_set_bool_parameter(values,"stats",set_services_args.stats);
|
||||
#endif
|
||||
if ((p = config_alloc_get(NVS_TYPE_STR, "telnet_enable")) != NULL) {
|
||||
if(strcasestr("YX",p)!=NULL){
|
||||
cJSON_AddStringToObject(values,set_services_args.telnet->hdr.longopts,"Telnet Only");
|
||||
}
|
||||
else if(strcasestr("D",p)!=NULL){
|
||||
cJSON_AddStringToObject(values,set_services_args.telnet->hdr.longopts,"Telnet and Serial");
|
||||
}
|
||||
else {
|
||||
cJSON_AddStringToObject(values,set_services_args.telnet->hdr.longopts,"Disabled");
|
||||
}
|
||||
FREE_AND_NULL(p);
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
static void register_set_services(){
|
||||
set_services_args.airplay = arg_lit0(NULL, "AirPlay", "AirPlay");
|
||||
#if CONFIG_CSPOT_SINK
|
||||
set_services_args.cspot = arg_lit0(NULL, "cspot", "Spotify (cspot)");
|
||||
#endif
|
||||
set_services_args.btspeaker = arg_lit0(NULL, "BT_Speaker", "Bluetooth Speaker");
|
||||
set_services_args.telnet= arg_str0("t", "telnet","Disabled|Telnet Only|Telnet and Serial","Telnet server. Use only for troubleshooting");
|
||||
#if WITH_TASKS_INFO
|
||||
set_services_args.stats= arg_lit0(NULL, "stats", "System Statistics. Use only for troubleshooting");
|
||||
#endif
|
||||
set_services_args.end=arg_end(2);
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = CFG_TYPE_SYST("services"),
|
||||
.help = "Services",
|
||||
.argtable = &set_services_args,
|
||||
.hint = NULL,
|
||||
.func = &do_set_services,
|
||||
};
|
||||
cmd_to_json_with_cb(&cmd,&set_services_cb);
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
|
||||
#if CONFIG_WITH_CONFIG_UI
|
||||
static struct {
|
||||
struct arg_int *wakeup_time;
|
||||
struct arg_int *wakeup_gpio_num;
|
||||
struct arg_int *wakeup_gpio_level;
|
||||
struct arg_end *end;
|
||||
} light_sleep_args;
|
||||
|
||||
static int light_sleep(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&light_sleep_args);
|
||||
if (nerrors != 0) {
|
||||
return 1;
|
||||
}
|
||||
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
|
||||
if (light_sleep_args.wakeup_time->count) {
|
||||
uint64_t timeout = 1000ULL * light_sleep_args.wakeup_time->ival[0];
|
||||
cmd_send_messaging(argv[0],MESSAGING_INFO, "Enabling timer wakeup, timeout=%lluus", timeout);
|
||||
ESP_ERROR_CHECK( esp_sleep_enable_timer_wakeup(timeout) );
|
||||
}
|
||||
int io_count = light_sleep_args.wakeup_gpio_num->count;
|
||||
if (io_count != light_sleep_args.wakeup_gpio_level->count) {
|
||||
cmd_send_messaging(argv[0],MESSAGING_INFO, "Should have same number of 'io' and 'io_level' arguments");
|
||||
return 1;
|
||||
}
|
||||
for (int i = 0; i < io_count; ++i) {
|
||||
int io_num = light_sleep_args.wakeup_gpio_num->ival[i];
|
||||
int level = light_sleep_args.wakeup_gpio_level->ival[i];
|
||||
if (level != 0 && level != 1) {
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR, "Invalid wakeup level: %d", level);
|
||||
return 1;
|
||||
}
|
||||
cmd_send_messaging(argv[0],MESSAGING_INFO, "Enabling wakeup on GPIO%d, wakeup on %s level",
|
||||
io_num, level ? "HIGH" : "LOW");
|
||||
|
||||
ESP_ERROR_CHECK( gpio_wakeup_enable(io_num, level ? GPIO_INTR_HIGH_LEVEL : GPIO_INTR_LOW_LEVEL) );
|
||||
}
|
||||
if (io_count > 0) {
|
||||
ESP_ERROR_CHECK( esp_sleep_enable_gpio_wakeup() );
|
||||
}
|
||||
if (CONFIG_ESP_CONSOLE_UART_NUM <= UART_NUM_1) {
|
||||
cmd_send_messaging(argv[0],MESSAGING_INFO, "Enabling UART wakeup (press ENTER to exit light sleep)");
|
||||
ESP_ERROR_CHECK( uart_set_wakeup_threshold(CONFIG_ESP_CONSOLE_UART_NUM, 3) );
|
||||
ESP_ERROR_CHECK( esp_sleep_enable_uart_wakeup(CONFIG_ESP_CONSOLE_UART_NUM) );
|
||||
}
|
||||
fflush(stdout);
|
||||
esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
|
||||
esp_light_sleep_start();
|
||||
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
|
||||
const char *cause_str;
|
||||
switch (cause) {
|
||||
case ESP_SLEEP_WAKEUP_GPIO:
|
||||
cause_str = "GPIO";
|
||||
break;
|
||||
case ESP_SLEEP_WAKEUP_UART:
|
||||
cause_str = "UART";
|
||||
break;
|
||||
case ESP_SLEEP_WAKEUP_TIMER:
|
||||
cause_str = "timer";
|
||||
break;
|
||||
default:
|
||||
cause_str = "unknown";
|
||||
printf("%d\n", cause);
|
||||
}
|
||||
cmd_send_messaging(argv[0],MESSAGING_INFO, "Woke up from: %s", cause_str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void register_light_sleep()
|
||||
{
|
||||
light_sleep_args.wakeup_time =
|
||||
arg_int0("t", "time", "<t>", "Wake up time, ms");
|
||||
light_sleep_args.wakeup_gpio_num =
|
||||
arg_intn(NULL, "io", "<n>", 0, 8,
|
||||
"If specified, wakeup using GPIO with given number");
|
||||
light_sleep_args.wakeup_gpio_level =
|
||||
arg_intn(NULL, "io_level", "<0|1>", 0, 8, "GPIO level to trigger wakeup");
|
||||
light_sleep_args.end = arg_end(3);
|
||||
|
||||
const esp_console_cmd_t cmd = {
|
||||
.command = "light_sleep",
|
||||
.help = "Enter light sleep mode. "
|
||||
"Two wakeup modes are supported: timer and GPIO. "
|
||||
"Multiple GPIO pins can be specified using pairs of "
|
||||
"'io' and 'io_level' arguments. "
|
||||
"Will also wake up on UART input.",
|
||||
.hint = NULL,
|
||||
.func = &light_sleep,
|
||||
.argtable = &light_sleep_args
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
||||
}
|
||||
#endif
|
||||
24
lib/platform_console/cmd_system.h
Normal file
24
lib/platform_console/cmd_system.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/* Console example — various system commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#pragma once
|
||||
#include "esp_system.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Register system functions
|
||||
void register_system();
|
||||
esp_err_t guided_factory();
|
||||
esp_err_t guided_restart_ota();
|
||||
void simple_restart();
|
||||
FILE * system_open_memstream(const char * cmdname,char **buf,size_t *buf_size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
213
lib/platform_console/cmd_wifi.c
Normal file
213
lib/platform_console/cmd_wifi.c
Normal file
@@ -0,0 +1,213 @@
|
||||
/* Console example — WiFi commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
// cmd_wifi has been replaced by wifi-manager
|
||||
/* Console example <20> WiFi commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include "cmd_wifi.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cmd_decl.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_console.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/timers.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_event.h"
|
||||
#include "led.h"
|
||||
extern bool bypass_network_manager;
|
||||
#define JOIN_TIMEOUT_MS (10000)
|
||||
#include "platform_console.h"
|
||||
|
||||
|
||||
extern EventGroupHandle_t network_event_group;
|
||||
extern const int CONNECTED_BIT;
|
||||
//static const char * TAG = "cmd_wifi";
|
||||
/** Arguments used by 'join' function */
|
||||
static struct {
|
||||
struct arg_int *timeout;
|
||||
struct arg_str *ssid;
|
||||
struct arg_str *password;
|
||||
struct arg_end *end;
|
||||
} join_args;
|
||||
|
||||
|
||||
|
||||
// todo: implement access point config - cmd_to_json(&i2cdetect_cmd);
|
||||
|
||||
|
||||
///** Arguments used by 'join' function */
|
||||
//static struct {
|
||||
// struct arg_int *autoconnect;
|
||||
// struct arg_end *end;
|
||||
//} auto_connect_args;
|
||||
|
||||
static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
{
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
led_blink_pushed(LED_GREEN, 250, 250);
|
||||
esp_wifi_connect();
|
||||
xEventGroupClearBits(network_event_group, CONNECTED_BIT);
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
led_unpush(LED_GREEN);
|
||||
xEventGroupSetBits(network_event_group, CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
//bool wait_for_wifi(){
|
||||
//
|
||||
// bool connected=(xEventGroupGetBits(wifi_event_group) & CONNECTED_BIT)!=0;
|
||||
//
|
||||
// if(!connected){
|
||||
// ESP_LOGD(TAG,"Waiting for WiFi...");
|
||||
// connected = (xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
|
||||
// pdFALSE, pdTRUE, JOIN_TIMEOUT_MS / portTICK_PERIOD_MS)& CONNECTED_BIT)!=0;
|
||||
// if(!connected){
|
||||
// ESP_LOGD(TAG,"wifi timeout.");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// ESP_LOGI(TAG,"WiFi Connected!");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// return connected;
|
||||
//
|
||||
//}
|
||||
static void initialise_wifi(void)
|
||||
{
|
||||
static bool initialized = false;
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
esp_netif_init();
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
|
||||
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler, NULL) );
|
||||
ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
|
||||
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
|
||||
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
|
||||
ESP_ERROR_CHECK( esp_wifi_start() );
|
||||
initialized = true;
|
||||
led_blink(LED_GREEN, 250, 250);
|
||||
}
|
||||
|
||||
static void wifi_join(void *arg)
|
||||
{
|
||||
const char *ssid = join_args.ssid->sval[0];
|
||||
const char *pass = join_args.password->sval[0];
|
||||
int timeout_ms = join_args.timeout->ival[0];
|
||||
|
||||
initialise_wifi();
|
||||
wifi_config_t wifi_config = { 0 };
|
||||
strncpy((char *) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
|
||||
wifi_config.sta.ssid[sizeof(wifi_config.sta.ssid) - 1] = '\0';
|
||||
if (pass) {
|
||||
strncpy((char *) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
|
||||
wifi_config.sta.password[sizeof(wifi_config.sta.password) - 1] = '\0';
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
|
||||
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
|
||||
ESP_ERROR_CHECK( esp_wifi_connect() );
|
||||
|
||||
int bits = xEventGroupWaitBits(network_event_group, CONNECTED_BIT,
|
||||
pdFALSE, pdTRUE, timeout_ms / portTICK_PERIOD_MS);
|
||||
|
||||
if (bits & CONNECTED_BIT) {
|
||||
ESP_LOGI(__func__, "Connected");
|
||||
} else {
|
||||
ESP_LOGW(__func__, "Connection timed out");
|
||||
}
|
||||
}
|
||||
|
||||
//static int set_auto_connect(int argc, char **argv)
|
||||
//{
|
||||
// int nerrors = arg_parse(argc, argv, (void **) &join_args);
|
||||
// if (nerrors != 0) {
|
||||
// arg_print_errors(stderr, join_args.end, argv[0]);
|
||||
// return 1;
|
||||
// }
|
||||
// ESP_LOGI(__func__, "Connecting to '%s'",
|
||||
// join_args.ssid->sval[0]);
|
||||
//
|
||||
// /* set default value*/
|
||||
// if (join_args.timeout->count == 0) {
|
||||
// join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
|
||||
// }
|
||||
//
|
||||
// bool connected = wifi_join(join_args.ssid->sval[0],
|
||||
// join_args.password->sval[0],
|
||||
// join_args.timeout->ival[0]);
|
||||
// if (!connected) {
|
||||
// ESP_LOGW(__func__, "Connection timed out");
|
||||
// return 1;
|
||||
// }
|
||||
// ESP_LOGI(__func__, "Connected");
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
static int connect(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&join_args);
|
||||
if (nerrors != 0) {
|
||||
return 1;
|
||||
}
|
||||
ESP_LOGI(__func__, "Connecting to '%s'",
|
||||
join_args.ssid->sval[0]);
|
||||
|
||||
/* set default value*/
|
||||
if (join_args.timeout->count == 0) {
|
||||
join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
|
||||
}
|
||||
|
||||
// need to use that trick to make sure we use internal stack
|
||||
xTimerStart(xTimerCreate("wifi_join", 1, pdFALSE, NULL, wifi_join), portMAX_DELAY);
|
||||
|
||||
return 0;
|
||||
}
|
||||
void register_wifi_join()
|
||||
{
|
||||
join_args.timeout = arg_int0(NULL, "timeout", "<t>", "Connection timeout, ms");
|
||||
join_args.ssid = arg_str1(NULL, NULL, "<ssid>", "SSID of AP");
|
||||
join_args.password = arg_str0(NULL, NULL, "<pass>", "PSK of AP");
|
||||
join_args.end = arg_end(2);
|
||||
|
||||
const esp_console_cmd_t join_cmd = {
|
||||
.command = "join",
|
||||
.help = "Join WiFi AP as a station",
|
||||
.hint = NULL,
|
||||
.func = &connect,
|
||||
.argtable = &join_args
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&join_cmd) );
|
||||
}
|
||||
|
||||
void register_wifi()
|
||||
{
|
||||
#ifdef WIFI_CMDLINE
|
||||
register_wifi_join();
|
||||
if(bypass_network_manager){
|
||||
initialise_wifi();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
18
lib/platform_console/cmd_wifi.h
Normal file
18
lib/platform_console/cmd_wifi.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/* Console example — declarations of command registration functions.
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Register WiFi functions
|
||||
void register_wifi();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
12
lib/platform_console/component.mk
Normal file
12
lib/platform_console/component.mk
Normal file
@@ -0,0 +1,12 @@
|
||||
#
|
||||
# Component Makefile
|
||||
#
|
||||
# This Makefile should, at the very least, just include $(SDK_PATH)/Makefile. By default,
|
||||
# this will take the sources in the src/ directory, compile them and link them into
|
||||
# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable,
|
||||
# please read the SDK documents if you need to do this.
|
||||
#
|
||||
|
||||
COMPONENT_ADD_INCLUDEDIRS := .
|
||||
COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/components/tools/
|
||||
COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/main
|
||||
22
lib/platform_console/library.json
Normal file
22
lib/platform_console/library.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "platform-console",
|
||||
"version": "1.0.0",
|
||||
"description": "Platform console component for ESP32",
|
||||
"keywords": "console, platform, esp32",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/squeezelite-esp32/squeezelite-esp32"
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "SqueezeESP32 Contributors",
|
||||
"maintainer": true
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"frameworks": ["espidf"],
|
||||
"platforms": ["espressif32"],
|
||||
"build": {
|
||||
"flags": ["-DCORE_DEBUG_LEVEL=3"]
|
||||
}
|
||||
}
|
||||
479
lib/platform_console/platform_console.c
Normal file
479
lib/platform_console/platform_console.c
Normal file
@@ -0,0 +1,479 @@
|
||||
/* Console example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include "platform_console.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_console.h"
|
||||
#include "esp_vfs_dev.h"
|
||||
#include "driver/uart.h"
|
||||
#include "linenoise/linenoise.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "pthread.h"
|
||||
#include "platform_esp32.h"
|
||||
#include "cmd_decl.h"
|
||||
#include "trace.h"
|
||||
#include "platform_config.h"
|
||||
#include "telnet.h"
|
||||
#include "tools.h"
|
||||
|
||||
#include "messaging.h"
|
||||
|
||||
#include "config.h"
|
||||
static pthread_t thread_console;
|
||||
static void * console_thread();
|
||||
void console_start();
|
||||
static const char * TAG = "console";
|
||||
extern bool bypass_network_manager;
|
||||
extern void register_squeezelite();
|
||||
|
||||
static EXT_RAM_ATTR QueueHandle_t uart_queue;
|
||||
static EXT_RAM_ATTR struct {
|
||||
uint8_t _buf[512];
|
||||
StaticRingbuffer_t _ringbuf;
|
||||
RingbufHandle_t handle;
|
||||
QueueSetHandle_t queue_set;
|
||||
} stdin_redir;
|
||||
|
||||
/* Prompt to be printed before each line.
|
||||
* This can be customized, made dynamic, etc.
|
||||
*/
|
||||
const char* prompt = LOG_COLOR_I "squeezelite-esp32> " LOG_RESET_COLOR;
|
||||
const char* recovery_prompt = LOG_COLOR_E "recovery-squeezelite-esp32> " LOG_RESET_COLOR;
|
||||
|
||||
/* Console command history can be stored to and loaded from a file.
|
||||
* The easiest way to do this is to use FATFS filesystem on top of
|
||||
* wear_levelling library.
|
||||
*/
|
||||
|
||||
#define MOUNT_PATH "/data"
|
||||
#define HISTORY_PATH MOUNT_PATH "/history.txt"
|
||||
static esp_err_t run_command(char * line);
|
||||
#define ADD_TO_JSON(o,t,n) if (t->n) cJSON_AddStringToObject(o,QUOTE(n),t->n);
|
||||
#define ADD_PARMS_TO_CMD(o,t,n) { cJSON * parms = ParmsToJSON(&t.n->hdr); if(parms) cJSON_AddItemToObject(o,QUOTE(n),parms); }
|
||||
cJSON * cmdList;
|
||||
cJSON * values_fn_list;
|
||||
cJSON * get_cmd_list(){
|
||||
cJSON * element;
|
||||
cJSON * values=cJSON_CreateObject();
|
||||
cJSON * list = cJSON_CreateObject();
|
||||
cJSON_AddItemReferenceToObject(list,"commands",cmdList);
|
||||
cJSON_AddItemToObject(list,"values",values);
|
||||
cJSON_ArrayForEach(element,cmdList){
|
||||
cJSON * name = cJSON_GetObjectItem(element,"name");
|
||||
cJSON * vals_fn = cJSON_GetObjectItem(values_fn_list,cJSON_GetStringValue(name));
|
||||
if(vals_fn!=NULL ){
|
||||
parm_values_fn_t *parm_values_fn = (parm_values_fn_t *)strtoul(cJSON_GetStringValue(vals_fn), NULL, 16);;
|
||||
|
||||
if(parm_values_fn){
|
||||
cJSON_AddItemToObject(values,cJSON_GetStringValue(name),parm_values_fn());
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
void console_set_bool_parameter(cJSON * root,char * nvs_name, struct arg_lit *arg){
|
||||
char * p=NULL;
|
||||
if(!root) {
|
||||
ESP_LOGE(TAG,"Invalid json parameter. Cannot set %s from %s",arg->hdr.longopts?arg->hdr.longopts:arg->hdr.glossary,nvs_name);
|
||||
return;
|
||||
}
|
||||
if ((p = config_alloc_get(NVS_TYPE_STR, nvs_name)) != NULL) {
|
||||
cJSON_AddBoolToObject(root,arg->hdr.longopts,strcmp(p,"1") == 0 || strcasecmp(p,"y") == 0);
|
||||
FREE_AND_NULL(p);
|
||||
}
|
||||
}
|
||||
struct arg_end *getParmsEnd(struct arg_hdr * * argtable){
|
||||
if(!argtable) return NULL;
|
||||
struct arg_hdr * *table = (struct arg_hdr * *)argtable;
|
||||
int tabindex = 0;
|
||||
while (!(table[tabindex]->flag & ARG_TERMINATOR))
|
||||
{
|
||||
tabindex++;
|
||||
}
|
||||
return (struct arg_end *)table[tabindex];
|
||||
}
|
||||
cJSON * ParmsToJSON(struct arg_hdr * * argtable){
|
||||
if(!argtable) return NULL;
|
||||
cJSON * arg_list = cJSON_CreateArray();
|
||||
struct arg_hdr * *table = (struct arg_hdr * *)argtable;
|
||||
int tabindex = 0;
|
||||
while (!(table[tabindex]->flag & ARG_TERMINATOR))
|
||||
{
|
||||
cJSON * entry = cJSON_CreateObject();
|
||||
ADD_TO_JSON(entry,table[tabindex],datatype);
|
||||
ADD_TO_JSON(entry,table[tabindex],glossary);
|
||||
ADD_TO_JSON(entry,table[tabindex],longopts);
|
||||
ADD_TO_JSON(entry,table[tabindex],shortopts);
|
||||
cJSON_AddBoolToObject(entry, "checkbox", (table[tabindex]->flag & ARG_HASOPTVALUE)==0 && (table[tabindex]->flag & ARG_HASVALUE)==0 && (table[tabindex]->longopts || table[tabindex]->shortopts) );
|
||||
cJSON_AddBoolToObject(entry, "remark", (table[tabindex]->flag & ARG_HASOPTVALUE)==0 && (table[tabindex]->flag & ARG_HASVALUE)==0 && (!table[tabindex]->longopts && !table[tabindex]->shortopts));
|
||||
cJSON_AddBoolToObject(entry, "hasvalue", table[tabindex]->flag & ARG_HASVALUE);
|
||||
cJSON_AddNumberToObject(entry,"mincount",table[tabindex]->mincount);
|
||||
cJSON_AddNumberToObject(entry,"maxcount",table[tabindex]->maxcount);
|
||||
cJSON_AddItemToArray(arg_list, entry);
|
||||
tabindex++;
|
||||
}
|
||||
return arg_list;
|
||||
}
|
||||
|
||||
esp_err_t cmd_to_json(const esp_console_cmd_t *cmd){
|
||||
return cmd_to_json_with_cb(cmd, NULL);
|
||||
}
|
||||
|
||||
esp_err_t cmd_to_json_with_cb(const esp_console_cmd_t *cmd, parm_values_fn_t parm_values_fn){
|
||||
if(!cmdList){
|
||||
cmdList=cJSON_CreateArray();
|
||||
}
|
||||
if(!values_fn_list){
|
||||
values_fn_list=cJSON_CreateObject();
|
||||
}
|
||||
|
||||
if (cmd->command == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (strchr(cmd->command, ' ') != NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
cJSON * jsoncmd = cJSON_CreateObject();
|
||||
ADD_TO_JSON(jsoncmd,cmd,help);
|
||||
ADD_TO_JSON(jsoncmd,cmd,hint);
|
||||
if(parm_values_fn){
|
||||
char addr[11]={0};
|
||||
snprintf(addr,sizeof(addr),"%lx",(unsigned long)parm_values_fn);
|
||||
cJSON_AddStringToObject(values_fn_list,cmd->command,addr);
|
||||
}
|
||||
cJSON_AddBoolToObject(jsoncmd,"hascb",parm_values_fn!=NULL);
|
||||
|
||||
if(cmd->argtable){
|
||||
cJSON_AddItemToObject(jsoncmd,"argtable",ParmsToJSON(cmd->argtable));
|
||||
}
|
||||
if (cmd->hint) {
|
||||
cJSON_AddStringToObject(jsoncmd, "hint", cmd->hint);
|
||||
}
|
||||
else if (cmd->argtable) {
|
||||
/* Generate hint based on cmd->argtable */
|
||||
char *buf = NULL;
|
||||
size_t buf_size = 0;
|
||||
FILE *f = open_memstream(&buf, &buf_size);
|
||||
if (f != NULL) {
|
||||
arg_print_syntax(f, cmd->argtable, NULL);
|
||||
fflush(f);
|
||||
fclose(f);
|
||||
}
|
||||
cJSON_AddStringToObject(jsoncmd, "hint", buf);
|
||||
FREE_AND_NULL(buf);
|
||||
}
|
||||
cJSON_AddStringToObject(jsoncmd, "name", cmd->command);
|
||||
char * b=cJSON_Print(jsoncmd);
|
||||
if(b){
|
||||
ESP_LOGD(TAG,"Adding command table %s",b);
|
||||
free(b);
|
||||
}
|
||||
cJSON_AddItemToArray(cmdList, jsoncmd);
|
||||
return ESP_OK;
|
||||
}
|
||||
int arg_parse_msg(int argc, char **argv, struct arg_hdr ** args){
|
||||
int nerrors = arg_parse(argc, argv, (void **)args);
|
||||
|
||||
if (nerrors != 0) {
|
||||
char *buf = NULL;
|
||||
size_t buf_size = 0;
|
||||
FILE *f = open_memstream(&buf, &buf_size);
|
||||
if (f != NULL) {
|
||||
arg_print_errors(f, getParmsEnd(args), argv[0]);
|
||||
fflush (f);
|
||||
cmd_send_messaging(argv[0],MESSAGING_ERROR,"%s", buf);
|
||||
}
|
||||
fclose(f);
|
||||
FREE_AND_NULL(buf);
|
||||
}
|
||||
return nerrors;
|
||||
}
|
||||
void process_autoexec(){
|
||||
int i=1;
|
||||
char autoexec_name[21]={0};
|
||||
char * autoexec_value=NULL;
|
||||
uint8_t autoexec_flag=0;
|
||||
|
||||
char * str_flag = config_alloc_get(NVS_TYPE_STR, "autoexec");
|
||||
if(!bypass_network_manager){
|
||||
ESP_LOGW(TAG, "Processing autoexec commands while network manager active. Wifi related commands will be ignored.");
|
||||
}
|
||||
if(is_recovery_running){
|
||||
ESP_LOGD(TAG, "Processing autoexec commands in recovery mode. Squeezelite commands will be ignored.");
|
||||
}
|
||||
if(str_flag !=NULL ){
|
||||
autoexec_flag=atoi(str_flag);
|
||||
ESP_LOGI(TAG,"autoexec is set to %s auto-process", autoexec_flag>0?"perform":"skip");
|
||||
if(autoexec_flag == 1) {
|
||||
do {
|
||||
snprintf(autoexec_name,sizeof(autoexec_name)-1,"autoexec%u",i++);
|
||||
ESP_LOGD(TAG,"Getting command name %s", autoexec_name);
|
||||
autoexec_value= config_alloc_get(NVS_TYPE_STR, autoexec_name);
|
||||
if(autoexec_value!=NULL ){
|
||||
if(!bypass_network_manager && strstr(autoexec_value, "join ")!=NULL ){
|
||||
ESP_LOGW(TAG,"Ignoring wifi join command.");
|
||||
}
|
||||
else if(is_recovery_running && !strstr(autoexec_value, "squeezelite " ) ){
|
||||
ESP_LOGW(TAG,"Ignoring command. ");
|
||||
}
|
||||
else {
|
||||
ESP_LOGI(TAG,"Running command %s = %s", autoexec_name, autoexec_value);
|
||||
run_command(autoexec_value);
|
||||
}
|
||||
ESP_LOGD(TAG,"Freeing memory for command %s name", autoexec_name);
|
||||
free(autoexec_value);
|
||||
}
|
||||
else {
|
||||
ESP_LOGD(TAG,"No matching command found for name %s", autoexec_name);
|
||||
break;
|
||||
}
|
||||
} while(1);
|
||||
}
|
||||
free(str_flag);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGD(TAG,"No matching command found for name autoexec.");
|
||||
}
|
||||
}
|
||||
|
||||
static ssize_t stdin_read(int fd, void* data, size_t size) {
|
||||
size_t bytes = -1;
|
||||
|
||||
while (1) {
|
||||
QueueSetMemberHandle_t activated = xQueueSelectFromSet(stdin_redir.queue_set, portMAX_DELAY);
|
||||
|
||||
if (activated == uart_queue) {
|
||||
uart_event_t event;
|
||||
|
||||
xQueueReceive(uart_queue, &event, 0);
|
||||
|
||||
if (event.type == UART_DATA) {
|
||||
bytes = uart_read_bytes(CONFIG_ESP_CONSOLE_UART_NUM, data, size < event.size ? size : event.size, 0);
|
||||
// we have to do our own line ending translation here
|
||||
for (int i = 0; i < bytes; i++) if (((char*)data)[i] == '\r') ((char*)data)[i] = '\n';
|
||||
break;
|
||||
}
|
||||
} else if (xRingbufferCanRead(stdin_redir.handle, activated)) {
|
||||
char *p = xRingbufferReceiveUpTo(stdin_redir.handle, &bytes, 0, size);
|
||||
// we might receive strings, replace null by \n
|
||||
for (int i = 0; i < bytes; i++) if (p[i] == '\0' || p[i] == '\r') p[i] = '\n';
|
||||
memcpy(data, p, bytes);
|
||||
vRingbufferReturnItem(stdin_redir.handle, p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
static int stdin_dummy(const char * path, int flags, int mode) { return 0; }
|
||||
|
||||
void initialize_console() {
|
||||
/* Minicom, screen, idf_monitor send CR when ENTER key is pressed (unused if we redirect stdin) */
|
||||
esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
|
||||
/* Move the caret to the beginning of the next line on '\n' */
|
||||
esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
|
||||
|
||||
/* Configure UART. Note that REF_TICK is used so that the baud rate remains
|
||||
* correct while APB frequency is changing in light sleep mode.
|
||||
*/
|
||||
const uart_config_t uart_config = { .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1,
|
||||
};
|
||||
ESP_ERROR_CHECK(uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config));
|
||||
|
||||
/* Install UART driver for interrupt-driven reads and writes */
|
||||
ESP_ERROR_CHECK( uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 3, &uart_queue, 0));
|
||||
|
||||
/* Tell VFS to use UART driver */
|
||||
esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
|
||||
|
||||
/* re-direct stdin to our own driver so we can gather data from various sources */
|
||||
stdin_redir.queue_set = xQueueCreateSet(2);
|
||||
stdin_redir.handle = xRingbufferCreateStatic(sizeof(stdin_redir._buf), RINGBUF_TYPE_BYTEBUF, stdin_redir._buf, &stdin_redir._ringbuf);
|
||||
xRingbufferAddToQueueSetRead(stdin_redir.handle, stdin_redir.queue_set);
|
||||
xQueueAddToSet(uart_queue, stdin_redir.queue_set);
|
||||
|
||||
esp_vfs_t vfs = { };
|
||||
vfs.flags = ESP_VFS_FLAG_DEFAULT;
|
||||
vfs.open = stdin_dummy;
|
||||
vfs.read = stdin_read;
|
||||
|
||||
ESP_ERROR_CHECK(esp_vfs_register("/dev/redirect", &vfs, NULL));
|
||||
freopen("/dev/redirect", "r", stdin);
|
||||
|
||||
/* Disable buffering on stdin */
|
||||
setvbuf(stdin, NULL, _IONBF, 0);
|
||||
|
||||
/* Initialize the console */
|
||||
esp_console_config_t console_config = { .max_cmdline_args = 28,
|
||||
.max_cmdline_length = 600,
|
||||
#if CONFIG_LOG_COLORS
|
||||
.hint_color = atoi(LOG_COLOR_CYAN)
|
||||
#endif
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_init(&console_config));
|
||||
|
||||
/* Configure linenoise line completion library */
|
||||
/* Enable multiline editing. If not set, long commands will scroll within
|
||||
* single line.
|
||||
*/
|
||||
linenoiseSetMultiLine(1);
|
||||
|
||||
/* Tell linenoise where to get command completions and hints */
|
||||
linenoiseSetCompletionCallback(&esp_console_get_completion);
|
||||
linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
|
||||
|
||||
/* Set command history size */
|
||||
linenoiseHistorySetMaxLen(100);
|
||||
|
||||
/* Load command history from filesystem */
|
||||
//linenoiseHistoryLoad(HISTORY_PATH);
|
||||
}
|
||||
|
||||
bool console_push(const char *data, size_t size) {
|
||||
return xRingbufferSend(stdin_redir.handle, data, size, pdMS_TO_TICKS(100)) == pdPASS;
|
||||
}
|
||||
|
||||
void console_start() {
|
||||
/* we always run console b/c telnet sends commands to stdin */
|
||||
initialize_console();
|
||||
|
||||
/* Register commands */
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("Registering help command");
|
||||
esp_console_register_help_command();
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("Registering system commands");
|
||||
register_system();
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("Registering config commands");
|
||||
register_config_cmd();
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("Registering nvs commands");
|
||||
register_nvs();
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("Registering wifi commands");
|
||||
register_wifi();
|
||||
|
||||
if(!is_recovery_running){
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("Registering squeezelite commands");
|
||||
register_squeezelite();
|
||||
}
|
||||
else {
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("Registering recovery commands");
|
||||
register_ota_cmd();
|
||||
}
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("Registering i2c commands");
|
||||
register_i2ctools();
|
||||
|
||||
printf("\n");
|
||||
if(is_recovery_running){
|
||||
printf("****************************************************************\n"
|
||||
"RECOVERY APPLICATION\n"
|
||||
"This mode is used to flash Squeezelite into the OTA partition\n"
|
||||
"****\n\n");
|
||||
}
|
||||
printf("Type 'help' to get the list of commands.\n"
|
||||
"Use UP/DOWN arrows to navigate through command history.\n"
|
||||
"Press TAB when typing command name to auto-complete.\n"
|
||||
"\n");
|
||||
if(!is_recovery_running){
|
||||
printf("To automatically execute lines at startup:\n"
|
||||
"\tSet NVS variable autoexec (U8) = 1 to enable, 0 to disable automatic execution.\n"
|
||||
"\tSet NVS variable autoexec[1~9] (string)to a command that should be executed automatically\n");
|
||||
}
|
||||
printf("\n\n");
|
||||
|
||||
/* Figure out if the terminal supports escape sequences */
|
||||
int probe_status = linenoiseProbe();
|
||||
if (probe_status) { /* zero indicates success */
|
||||
printf("\n****************************\n"
|
||||
"Your terminal application does not support escape sequences.\n"
|
||||
"Line editing and history features are disabled.\n"
|
||||
"On Windows, try using Putty instead.\n"
|
||||
"****************************\n");
|
||||
linenoiseSetDumbMode(1);
|
||||
#if CONFIG_LOG_COLORS
|
||||
/* Since the terminal doesn't support escape sequences,
|
||||
* don't use color codes in the prompt.
|
||||
*/
|
||||
if(is_recovery_running){
|
||||
recovery_prompt= "recovery-squeezelite-esp32>";
|
||||
}
|
||||
prompt = "squeezelite-esp32> ";
|
||||
#endif //CONFIG_LOG_COLORS
|
||||
}
|
||||
esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
|
||||
cfg.thread_name= "console";
|
||||
cfg.inherit_cfg = true;
|
||||
cfg.stack_size = 4*1024;
|
||||
if(is_recovery_running){
|
||||
prompt = recovery_prompt;
|
||||
}
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("Creating console thread with stack size of 4096 bytes");
|
||||
esp_pthread_set_cfg(&cfg);
|
||||
pthread_create(&thread_console, NULL, console_thread, NULL);
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("Console thread created");
|
||||
|
||||
}
|
||||
|
||||
static esp_err_t run_command(char * line){
|
||||
/* Try to run the command */
|
||||
int ret;
|
||||
esp_err_t err = esp_console_run(line, &ret);
|
||||
|
||||
if (err == ESP_ERR_NOT_FOUND) {
|
||||
ESP_LOGE(TAG,"Unrecognized command: %s", line);
|
||||
} else if (err == ESP_ERR_INVALID_ARG) {
|
||||
// command was empty
|
||||
} else if (err != ESP_OK && ret != ESP_OK) {
|
||||
ESP_LOGW(TAG,"Command returned non-zero error code: 0x%x (%s)", ret,
|
||||
esp_err_to_name(err));
|
||||
} else if (err == ESP_OK && ret != ESP_OK) {
|
||||
ESP_LOGW(TAG,"Command returned in error");
|
||||
err = ESP_FAIL;
|
||||
} else if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG,"Internal error: %s", esp_err_to_name(err));
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
static void * console_thread() {
|
||||
if(!is_recovery_running){
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("Running autoexec");
|
||||
process_autoexec();
|
||||
MEMTRACE_PRINT_DELTA_MESSAGE("Autoexec done");
|
||||
}
|
||||
/* Main loop */
|
||||
while (1) {
|
||||
/* Get a line using linenoise.
|
||||
* The line is returned when ENTER is pressed.
|
||||
*/
|
||||
char* line = linenoise(prompt);
|
||||
if (line == NULL) { /* Ignore empty lines */
|
||||
continue;
|
||||
}
|
||||
/* Add the command to the history */
|
||||
linenoiseHistoryAdd(line);
|
||||
|
||||
/* Save command history to filesystem */
|
||||
linenoiseHistorySave(HISTORY_PATH);
|
||||
printf("\n");
|
||||
run_command(line);
|
||||
/* linenoise allocates line buffer on the heap, so need to free it */
|
||||
linenoiseFree(line);
|
||||
taskYIELD();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
29
lib/platform_console/platform_console.h
Normal file
29
lib/platform_console/platform_console.h
Normal file
@@ -0,0 +1,29 @@
|
||||
/* Console example — declarations of command registration functions.
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#pragma once
|
||||
#include "esp_console.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
#include "cJSON.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#define CFG_TYPE_HW(a) "cfg-hw-" a
|
||||
#define CFG_TYPE_AUDIO(a) "cfg-audio-" a
|
||||
#define CFG_TYPE_SYST(a) "cfg-syst-" a
|
||||
#define CFG_TYPE_FW(a) "cfg-fw-" a
|
||||
#define CFG_TYPE_GEN(a) "cfg-gen-" a
|
||||
typedef cJSON * parm_values_fn_t(void);
|
||||
esp_err_t cmd_to_json(const esp_console_cmd_t *cmd);
|
||||
esp_err_t cmd_to_json_with_cb(const esp_console_cmd_t *cmd, parm_values_fn_t parm_values_fn);
|
||||
int arg_parse_msg(int argc, char **argv, struct arg_hdr ** args);
|
||||
void console_set_bool_parameter(cJSON * root,char * nvs_name, struct arg_lit *arg);
|
||||
cJSON * get_cmd_list();
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
3
lib/platform_console/test/CMakeLists.txt
Normal file
3
lib/platform_console/test/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "test_system.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES unity tools console json platform_console platform_config )
|
||||
189
lib/platform_console/test/test_system.c
Normal file
189
lib/platform_console/test/test_system.c
Normal file
@@ -0,0 +1,189 @@
|
||||
/* test_mean.c: Implementation of a testable component.
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
#include "unity.h"
|
||||
#include "platform_console.h"
|
||||
#include "platform_esp32.h"
|
||||
#include "platform_config.h"
|
||||
#include "string.h"
|
||||
struct arg_lit *arglit;
|
||||
struct arg_int *argint;
|
||||
struct arg_str *argstr;
|
||||
struct arg_end *end;
|
||||
|
||||
extern int is_output_gpio(struct arg_int * gpio, FILE * f, int * gpio_out, bool mandatory);
|
||||
extern void initialize_console();
|
||||
extern esp_err_t run_command(char * line);
|
||||
static char *buf = NULL;
|
||||
static char * s_tmp_line_buf=NULL;
|
||||
static size_t buf_size = 0;
|
||||
static FILE * f;
|
||||
static size_t argc=1;
|
||||
static char ** argv=NULL;
|
||||
static bool config_initialized=false;
|
||||
void init_console(){
|
||||
if(config_initialized) return;
|
||||
initialize_console();
|
||||
config_initialized=true;
|
||||
}
|
||||
|
||||
/****************************************************************************************
|
||||
*
|
||||
*/
|
||||
void open_mem_stream_file(){
|
||||
f = open_memstream(&buf, &buf_size);
|
||||
}
|
||||
|
||||
/****************************************************************************************
|
||||
*
|
||||
*/
|
||||
void close_flush_all(void * argtable, int count,bool print){
|
||||
fflush (f);
|
||||
if(print){
|
||||
printf("%s", buf);
|
||||
}
|
||||
fclose(f);
|
||||
free(buf);
|
||||
arg_freetable(argtable,count);
|
||||
free(argv);
|
||||
}
|
||||
|
||||
/****************************************************************************************
|
||||
*
|
||||
*/
|
||||
int alloc_split_command_line(char * cmdline){
|
||||
argv = (char **) calloc(22, sizeof(char *));
|
||||
if(!s_tmp_line_buf){
|
||||
s_tmp_line_buf= calloc(strlen(cmdline), 1);
|
||||
}
|
||||
strlcpy(s_tmp_line_buf, cmdline, 22);
|
||||
argc = esp_console_split_argv(s_tmp_line_buf, argv,22);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/****************************************************************************************
|
||||
*
|
||||
*/
|
||||
int alloc_split_parse_command_line(char * cmdline, void ** args){
|
||||
alloc_split_command_line(cmdline);
|
||||
return arg_parse(argc, argv,args);
|
||||
}
|
||||
|
||||
/****************************************************************************************
|
||||
*
|
||||
*/
|
||||
TEST_CASE("Invalid GPIO detected", "[config][ui]")
|
||||
{
|
||||
char * cmdline = "test -i 55\n";
|
||||
void *argtable[] = {
|
||||
argint = arg_int1("i","int","<gpio>","GPIO number"),
|
||||
end = arg_end(6)
|
||||
};
|
||||
open_mem_stream_file();
|
||||
alloc_split_parse_command_line(cmdline, &argtable);
|
||||
int out_val = 0;
|
||||
TEST_ASSERT_EQUAL_INT_MESSAGE(1,is_output_gpio(argtable[0], f, &out_val, true),"Invalid GPIO not detected");
|
||||
TEST_ASSERT_EQUAL_INT_MESSAGE(-1,out_val,"GPIO Should be set to -1");
|
||||
fflush (f);
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE("Invalid int gpio: [55] is not a GPIO\n",buf,"Invalid GPIO message wrong");
|
||||
close_flush_all(argtable,sizeof(argtable)/sizeof(argtable[0]),false);
|
||||
}
|
||||
|
||||
/****************************************************************************************
|
||||
*
|
||||
*/
|
||||
TEST_CASE("Input Only GPIO detected", "[config][ui]")
|
||||
{
|
||||
char * cmdline = "test -i 35\n";
|
||||
void *argtable[] = {
|
||||
argint = arg_int1("i","int","<gpio>","GPIO number"),
|
||||
end = arg_end(6)
|
||||
};
|
||||
open_mem_stream_file();
|
||||
alloc_split_parse_command_line(cmdline, &argtable);
|
||||
int out_val = 0;
|
||||
TEST_ASSERT_EQUAL_INT_MESSAGE(1,is_output_gpio(argtable[0], f, &out_val, true),"Input only GPIO not detected");
|
||||
TEST_ASSERT_EQUAL_INT_MESSAGE(-1,out_val,"GPIO Should be set to -1");
|
||||
fflush (f);
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE("Invalid int gpio: [35] has input capabilities only\n",buf,"Missing GPIO message wrong");
|
||||
close_flush_all(argtable,sizeof(argtable)/sizeof(argtable[0]),false);
|
||||
}
|
||||
|
||||
/****************************************************************************************
|
||||
*
|
||||
*/
|
||||
TEST_CASE("Valid GPIO Processed", "[config][ui]")
|
||||
{
|
||||
char * cmdline = "test -i 33\n";
|
||||
void *argtable[] = {
|
||||
argint = arg_int1("i","int","<gpio>","GPIO number"),
|
||||
end = arg_end(6)
|
||||
};
|
||||
open_mem_stream_file();
|
||||
alloc_split_parse_command_line(cmdline, &argtable);
|
||||
int out_val = 0;
|
||||
TEST_ASSERT_EQUAL_INT_MESSAGE(0,is_output_gpio(argtable[0], f, &out_val, true),"Valid GPIO not recognized");
|
||||
TEST_ASSERT_EQUAL_INT_MESSAGE(33,out_val,"GPIO Should be set to 33");
|
||||
fflush (f);
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE("",buf,"Valid GPIO shouldn't produce a message");
|
||||
close_flush_all(argtable,sizeof(argtable)/sizeof(argtable[0]),false);
|
||||
}
|
||||
|
||||
/****************************************************************************************
|
||||
*
|
||||
*/
|
||||
TEST_CASE("Missing mandatory GPIO detected", "[config][ui]")
|
||||
{
|
||||
char * cmdline = "test \n";
|
||||
void *argtable[] = {
|
||||
argint = arg_int1("i","int","<gpio>","GPIO number"),
|
||||
end = arg_end(6)
|
||||
};
|
||||
open_mem_stream_file();
|
||||
alloc_split_parse_command_line(cmdline, &argtable);
|
||||
int out_val = 0;
|
||||
TEST_ASSERT_EQUAL_INT_MESSAGE(1,is_output_gpio(argtable[0], f, &out_val, true),"Missing GPIO not detected");
|
||||
fflush (f);
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE("Missing: int\n",buf,"Missing GPIO parameter message wrong");
|
||||
TEST_ASSERT_EQUAL_INT_MESSAGE(-1,out_val,"GPIO Should be set to -1");
|
||||
close_flush_all(argtable,sizeof(argtable)/sizeof(argtable[0]),false);
|
||||
}
|
||||
/****************************************************************************************
|
||||
*
|
||||
*/
|
||||
TEST_CASE("Missing mandatory parameter detected", "[config][ui]")
|
||||
{
|
||||
char * cmdline = "test \n";
|
||||
void *argtable[] = {
|
||||
argint = arg_int1("i","int","<gpio>","GPIO number"),
|
||||
end = arg_end(6)
|
||||
};
|
||||
open_mem_stream_file();
|
||||
alloc_split_parse_command_line(cmdline, &argtable);
|
||||
int out_val = 0;
|
||||
TEST_ASSERT_EQUAL_INT_MESSAGE(1,is_output_gpio(argtable[0], f, &out_val, true),"Missing parameter not detected");
|
||||
fflush (f);
|
||||
TEST_ASSERT_EQUAL_STRING_MESSAGE("Missing: int\n",buf,"Missing parameter message wrong");
|
||||
TEST_ASSERT_EQUAL_INT_MESSAGE(-1,out_val,"GPIO Should be set to -1");
|
||||
close_flush_all(argtable,sizeof(argtable)/sizeof(argtable[0]),false);
|
||||
}
|
||||
/****************************************************************************************
|
||||
*
|
||||
*/
|
||||
TEST_CASE("dac config command", "[config_cmd]")
|
||||
{
|
||||
config_set_value(NVS_TYPE_STR, "dac_config", "");
|
||||
esp_err_t err=run_command("cfg-hw-dac\n");
|
||||
char * nvs_value = config_alloc_get_str("dac_config", NULL,NULL);
|
||||
TEST_ASSERT_NOT_NULL(nvs_value);
|
||||
TEST_ASSERT_EQUAL_MESSAGE(ESP_OK,err,"Running command failed");
|
||||
free(nvs_value);
|
||||
}
|
||||
Reference in New Issue
Block a user