mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-19 04:44:47 -07:00
[FL-3954, FL-3955] New CLI architecture (#4111)
* feat: FuriThread stdin * ci: fix f18 * feat: stdio callback context * feat: FuriPipe * POTENTIALLY EXPLOSIVE pipe welding * fix: non-explosive welding * Revert welding * docs: furi_pipe * feat: pipe event loop integration * update f18 sdk * f18 * docs: make doxygen happy * fix: event loop not triggering when pipe attached to stdio * fix: partial stdout in pipe * allow simultaneous in and out subscription in event loop * feat: vcp i/o * feat: cli ansi stuffs and history * feat: more line editing * working but slow cli rewrite * restore previous speed after 4 days of debugging 🥲 * fix: cli_app_should_stop * fix: cli and event_loop memory leaks * style: remove commented out code * ci: fix pvs warnings * fix: unit tests, event_loop crash * ci: fix build * ci: silence pvs warning * feat: cli gpio * ci: fix formatting * Fix memory leak during event loop unsubscription * Event better memory leak fix * feat: cli completions * Merge remote-tracking branch 'origin/dev' into portasynthinca3/3928-cli-threads * merge fixups * temporarily exclude speaker_debug app * pvs and unit tests fixups * feat: commands in fals * move commands out of flash, code cleanup * ci: fix errors * fix: run commands in buffer when stopping session * speedup cli file transfer * fix f18 * separate cli_shell into modules * fix pvs warning * fix qflipper refusing to connect * remove temp debug logs * remove erroneous conclusion * Fix memory leak during event loop unsubscription * Event better memory leak fix * unit test for the fix * improve thread stdio callback signatures * pipe stdout timeout * update api symbols * fix f18, formatting * fix pvs warnings * increase stack size, hope to fix unit tests * cli: revert flag changes * cli: fix formatting * cli, fbt: loopback perf benchmark * thread, event_loop: subscribing to thread flags * cli: signal internal events using thread flags, improve performance * fix f18, formatting * event_loop: fix crash * storage_cli: increase write_chunk buffer size again * cli: explanation for order=0 * thread, event_loop: thread flags callback refactor * cli: increase stack size * cli: rename cli_app_should_stop -> cli_is_pipe_broken_or_is_etx_next_char * cli: use plain array instead of mlib for history * cli: prepend file name to static fns * cli: fix formatting * cli_shell: increase stack size * cli: fix rpc lockup * cli: better lockup fix * cli: fix f18 * fix merge --------- Co-authored-by: Georgii Surkov <georgii.surkov@outlook.com> Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include <storage/storage.h>
|
||||
#include <storage/storage_sd_api.h>
|
||||
#include <power/power_service/power.h>
|
||||
#include <toolbox/pipe.h>
|
||||
|
||||
#define MAX_NAME_LENGTH 255
|
||||
|
||||
@@ -19,8 +20,8 @@ static void storage_cli_print_error(FS_Error error) {
|
||||
printf("Storage error: %s\r\n", storage_error_get_desc(error));
|
||||
}
|
||||
|
||||
static void storage_cli_info(Cli* cli, FuriString* path, FuriString* args) {
|
||||
UNUSED(cli);
|
||||
static void storage_cli_info(PipeSide* pipe, FuriString* path, FuriString* args) {
|
||||
UNUSED(pipe);
|
||||
UNUSED(args);
|
||||
Storage* api = furi_record_open(RECORD_STORAGE);
|
||||
|
||||
@@ -69,13 +70,14 @@ static void storage_cli_info(Cli* cli, FuriString* path, FuriString* args) {
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
static void storage_cli_format(Cli* cli, FuriString* path, FuriString* args) {
|
||||
static void storage_cli_format(PipeSide* pipe, FuriString* path, FuriString* args) {
|
||||
UNUSED(pipe);
|
||||
UNUSED(args);
|
||||
if(furi_string_cmp_str(path, STORAGE_INT_PATH_PREFIX) == 0) {
|
||||
storage_cli_print_error(FSE_NOT_IMPLEMENTED);
|
||||
} else if(furi_string_cmp_str(path, STORAGE_EXT_PATH_PREFIX) == 0) {
|
||||
printf("Formatting SD card, All data will be lost! Are you sure (y/n)?\r\n");
|
||||
char answer = cli_getc(cli);
|
||||
char answer = getchar();
|
||||
if(answer == 'y' || answer == 'Y') {
|
||||
Storage* api = furi_record_open(RECORD_STORAGE);
|
||||
printf("Formatting, please wait...\r\n");
|
||||
@@ -96,8 +98,8 @@ static void storage_cli_format(Cli* cli, FuriString* path, FuriString* args) {
|
||||
}
|
||||
}
|
||||
|
||||
static void storage_cli_list(Cli* cli, FuriString* path, FuriString* args) {
|
||||
UNUSED(cli);
|
||||
static void storage_cli_list(PipeSide* pipe, FuriString* path, FuriString* args) {
|
||||
UNUSED(pipe);
|
||||
UNUSED(args);
|
||||
if(furi_string_cmp_str(path, "/") == 0) {
|
||||
printf("\t[D] int\r\n");
|
||||
@@ -134,13 +136,13 @@ static void storage_cli_list(Cli* cli, FuriString* path, FuriString* args) {
|
||||
}
|
||||
}
|
||||
|
||||
static void storage_cli_tree(Cli* cli, FuriString* path, FuriString* args) {
|
||||
static void storage_cli_tree(PipeSide* pipe, FuriString* path, FuriString* args) {
|
||||
UNUSED(args);
|
||||
if(furi_string_cmp_str(path, "/") == 0) {
|
||||
furi_string_set(path, STORAGE_INT_PATH_PREFIX);
|
||||
storage_cli_tree(cli, path, NULL);
|
||||
storage_cli_tree(pipe, path, NULL);
|
||||
furi_string_set(path, STORAGE_EXT_PATH_PREFIX);
|
||||
storage_cli_tree(cli, path, NULL);
|
||||
storage_cli_tree(pipe, path, NULL);
|
||||
} else {
|
||||
Storage* api = furi_record_open(RECORD_STORAGE);
|
||||
DirWalk* dir_walk = dir_walk_alloc(api);
|
||||
@@ -176,8 +178,8 @@ static void storage_cli_tree(Cli* cli, FuriString* path, FuriString* args) {
|
||||
}
|
||||
}
|
||||
|
||||
static void storage_cli_read(Cli* cli, FuriString* path, FuriString* args) {
|
||||
UNUSED(cli);
|
||||
static void storage_cli_read(PipeSide* pipe, FuriString* path, FuriString* args) {
|
||||
UNUSED(pipe);
|
||||
UNUSED(args);
|
||||
Storage* api = furi_record_open(RECORD_STORAGE);
|
||||
File* file = storage_file_alloc(api);
|
||||
@@ -208,7 +210,8 @@ static void storage_cli_read(Cli* cli, FuriString* path, FuriString* args) {
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
static void storage_cli_write(Cli* cli, FuriString* path, FuriString* args) {
|
||||
static void storage_cli_write(PipeSide* pipe, FuriString* path, FuriString* args) {
|
||||
UNUSED(pipe);
|
||||
UNUSED(args);
|
||||
Storage* api = furi_record_open(RECORD_STORAGE);
|
||||
File* file = storage_file_alloc(api);
|
||||
@@ -222,9 +225,9 @@ static void storage_cli_write(Cli* cli, FuriString* path, FuriString* args) {
|
||||
uint32_t read_index = 0;
|
||||
|
||||
while(true) {
|
||||
uint8_t symbol = cli_getc(cli);
|
||||
uint8_t symbol = getchar();
|
||||
|
||||
if(symbol == CliSymbolAsciiETX) {
|
||||
if(symbol == CliKeyETX) {
|
||||
size_t write_size = read_index % buffer_size;
|
||||
|
||||
if(write_size > 0) {
|
||||
@@ -263,7 +266,8 @@ static void storage_cli_write(Cli* cli, FuriString* path, FuriString* args) {
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
static void storage_cli_read_chunks(Cli* cli, FuriString* path, FuriString* args) {
|
||||
static void storage_cli_read_chunks(PipeSide* pipe, FuriString* path, FuriString* args) {
|
||||
UNUSED(pipe);
|
||||
Storage* api = furi_record_open(RECORD_STORAGE);
|
||||
File* file = storage_file_alloc(api);
|
||||
|
||||
@@ -280,7 +284,7 @@ static void storage_cli_read_chunks(Cli* cli, FuriString* path, FuriString* args
|
||||
uint8_t* data = malloc(buffer_size);
|
||||
while(file_size > 0) {
|
||||
printf("\r\nReady?\r\n");
|
||||
cli_getc(cli);
|
||||
getchar();
|
||||
|
||||
size_t read_size = storage_file_read(file, data, buffer_size);
|
||||
for(size_t i = 0; i < read_size; i++) {
|
||||
@@ -302,31 +306,32 @@ static void storage_cli_read_chunks(Cli* cli, FuriString* path, FuriString* args
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
static void storage_cli_write_chunk(Cli* cli, FuriString* path, FuriString* args) {
|
||||
static void storage_cli_write_chunk(PipeSide* pipe, FuriString* path, FuriString* args) {
|
||||
Storage* api = furi_record_open(RECORD_STORAGE);
|
||||
File* file = storage_file_alloc(api);
|
||||
|
||||
uint32_t buffer_size;
|
||||
if(strint_to_uint32(furi_string_get_cstr(args), NULL, &buffer_size, 10) !=
|
||||
uint32_t need_to_read;
|
||||
if(strint_to_uint32(furi_string_get_cstr(args), NULL, &need_to_read, 10) !=
|
||||
StrintParseNoError) {
|
||||
storage_cli_print_usage();
|
||||
} else {
|
||||
if(storage_file_open(file, furi_string_get_cstr(path), FSAM_WRITE, FSOM_OPEN_APPEND)) {
|
||||
printf("Ready\r\n");
|
||||
const size_t buffer_size = 1024;
|
||||
uint8_t* buffer = malloc(buffer_size);
|
||||
|
||||
if(buffer_size) {
|
||||
uint8_t* buffer = malloc(buffer_size);
|
||||
while(need_to_read) {
|
||||
size_t read_this_time = pipe_receive(pipe, buffer, MIN(buffer_size, need_to_read));
|
||||
size_t wrote_this_time = storage_file_write(file, buffer, read_this_time);
|
||||
|
||||
size_t read_bytes = cli_read(cli, buffer, buffer_size);
|
||||
|
||||
size_t written_size = storage_file_write(file, buffer, read_bytes);
|
||||
|
||||
if(written_size != buffer_size) {
|
||||
if(wrote_this_time != read_this_time) {
|
||||
storage_cli_print_error(storage_file_get_error(file));
|
||||
break;
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
need_to_read -= read_this_time;
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
} else {
|
||||
storage_cli_print_error(storage_file_get_error(file));
|
||||
}
|
||||
@@ -337,8 +342,8 @@ static void storage_cli_write_chunk(Cli* cli, FuriString* path, FuriString* args
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
static void storage_cli_stat(Cli* cli, FuriString* path, FuriString* args) {
|
||||
UNUSED(cli);
|
||||
static void storage_cli_stat(PipeSide* pipe, FuriString* path, FuriString* args) {
|
||||
UNUSED(pipe);
|
||||
UNUSED(args);
|
||||
Storage* api = furi_record_open(RECORD_STORAGE);
|
||||
|
||||
@@ -379,8 +384,8 @@ static void storage_cli_stat(Cli* cli, FuriString* path, FuriString* args) {
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
static void storage_cli_timestamp(Cli* cli, FuriString* path, FuriString* args) {
|
||||
UNUSED(cli);
|
||||
static void storage_cli_timestamp(PipeSide* pipe, FuriString* path, FuriString* args) {
|
||||
UNUSED(pipe);
|
||||
UNUSED(args);
|
||||
Storage* api = furi_record_open(RECORD_STORAGE);
|
||||
|
||||
@@ -396,8 +401,8 @@ static void storage_cli_timestamp(Cli* cli, FuriString* path, FuriString* args)
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
static void storage_cli_copy(Cli* cli, FuriString* old_path, FuriString* args) {
|
||||
UNUSED(cli);
|
||||
static void storage_cli_copy(PipeSide* pipe, FuriString* old_path, FuriString* args) {
|
||||
UNUSED(pipe);
|
||||
Storage* api = furi_record_open(RECORD_STORAGE);
|
||||
FuriString* new_path;
|
||||
new_path = furi_string_alloc();
|
||||
@@ -417,8 +422,8 @@ static void storage_cli_copy(Cli* cli, FuriString* old_path, FuriString* args) {
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
static void storage_cli_remove(Cli* cli, FuriString* path, FuriString* args) {
|
||||
UNUSED(cli);
|
||||
static void storage_cli_remove(PipeSide* pipe, FuriString* path, FuriString* args) {
|
||||
UNUSED(pipe);
|
||||
UNUSED(args);
|
||||
Storage* api = furi_record_open(RECORD_STORAGE);
|
||||
FS_Error error = storage_common_remove(api, furi_string_get_cstr(path));
|
||||
@@ -430,8 +435,8 @@ static void storage_cli_remove(Cli* cli, FuriString* path, FuriString* args) {
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
static void storage_cli_rename(Cli* cli, FuriString* old_path, FuriString* args) {
|
||||
UNUSED(cli);
|
||||
static void storage_cli_rename(PipeSide* pipe, FuriString* old_path, FuriString* args) {
|
||||
UNUSED(pipe);
|
||||
Storage* api = furi_record_open(RECORD_STORAGE);
|
||||
FuriString* new_path;
|
||||
new_path = furi_string_alloc();
|
||||
@@ -451,8 +456,8 @@ static void storage_cli_rename(Cli* cli, FuriString* old_path, FuriString* args)
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
static void storage_cli_mkdir(Cli* cli, FuriString* path, FuriString* args) {
|
||||
UNUSED(cli);
|
||||
static void storage_cli_mkdir(PipeSide* pipe, FuriString* path, FuriString* args) {
|
||||
UNUSED(pipe);
|
||||
UNUSED(args);
|
||||
Storage* api = furi_record_open(RECORD_STORAGE);
|
||||
FS_Error error = storage_common_mkdir(api, furi_string_get_cstr(path));
|
||||
@@ -464,8 +469,8 @@ static void storage_cli_mkdir(Cli* cli, FuriString* path, FuriString* args) {
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
static void storage_cli_md5(Cli* cli, FuriString* path, FuriString* args) {
|
||||
UNUSED(cli);
|
||||
static void storage_cli_md5(PipeSide* pipe, FuriString* path, FuriString* args) {
|
||||
UNUSED(pipe);
|
||||
UNUSED(args);
|
||||
Storage* api = furi_record_open(RECORD_STORAGE);
|
||||
File* file = storage_file_alloc(api);
|
||||
@@ -491,8 +496,8 @@ static bool tar_extract_file_callback(const char* name, bool is_directory, void*
|
||||
return true;
|
||||
}
|
||||
|
||||
static void storage_cli_extract(Cli* cli, FuriString* old_path, FuriString* args) {
|
||||
UNUSED(cli);
|
||||
static void storage_cli_extract(PipeSide* pipe, FuriString* old_path, FuriString* args) {
|
||||
UNUSED(pipe);
|
||||
FuriString* new_path = furi_string_alloc();
|
||||
|
||||
if(!args_read_probably_quoted_string_and_trim(args, new_path)) {
|
||||
@@ -526,7 +531,7 @@ static void storage_cli_extract(Cli* cli, FuriString* old_path, FuriString* args
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
typedef void (*StorageCliCommandCallback)(Cli* cli, FuriString* path, FuriString* args);
|
||||
typedef void (*StorageCliCommandCallback)(PipeSide* pipe, FuriString* path, FuriString* args);
|
||||
|
||||
typedef struct {
|
||||
const char* command;
|
||||
@@ -631,7 +636,7 @@ static void storage_cli_print_usage(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void storage_cli(Cli* cli, FuriString* args, void* context) {
|
||||
void storage_cli(PipeSide* pipe, FuriString* args, void* context) {
|
||||
UNUSED(context);
|
||||
FuriString* cmd;
|
||||
FuriString* path;
|
||||
@@ -653,7 +658,7 @@ void storage_cli(Cli* cli, FuriString* args, void* context) {
|
||||
for(; i < COUNT_OF(storage_cli_commands); ++i) {
|
||||
const StorageCliCommand* command_descr = &storage_cli_commands[i];
|
||||
if(furi_string_cmp_str(cmd, command_descr->command) == 0) {
|
||||
command_descr->impl(cli, path, args);
|
||||
command_descr->impl(pipe, path, args);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -667,11 +672,12 @@ void storage_cli(Cli* cli, FuriString* args, void* context) {
|
||||
furi_string_free(cmd);
|
||||
}
|
||||
|
||||
static void storage_cli_factory_reset(Cli* cli, FuriString* args, void* context) {
|
||||
static void storage_cli_factory_reset(PipeSide* pipe, FuriString* args, void* context) {
|
||||
UNUSED(pipe);
|
||||
UNUSED(args);
|
||||
UNUSED(context);
|
||||
printf("All data will be lost! Are you sure (y/n)?\r\n");
|
||||
char c = cli_getc(cli);
|
||||
char c = getchar();
|
||||
if(c == 'y' || c == 'Y') {
|
||||
printf("Data will be wiped after reboot.\r\n");
|
||||
|
||||
@@ -688,7 +694,7 @@ static void storage_cli_factory_reset(Cli* cli, FuriString* args, void* context)
|
||||
void storage_on_system_start(void) {
|
||||
#ifdef SRV_CLI
|
||||
Cli* cli = furi_record_open(RECORD_CLI);
|
||||
cli_add_command(cli, RECORD_STORAGE, CliCommandFlagParallelSafe, storage_cli, NULL);
|
||||
cli_add_command_ex(cli, "storage", CliCommandFlagParallelSafe, storage_cli, NULL, 512);
|
||||
cli_add_command(
|
||||
cli, "factory_reset", CliCommandFlagParallelSafe, storage_cli_factory_reset, NULL);
|
||||
furi_record_close(RECORD_CLI);
|
||||
|
||||
Reference in New Issue
Block a user