2020-11-16 10:16:34 +00:00
|
|
|
#include "cli_i.h"
|
|
|
|
#include "cli_commands.h"
|
2022-04-29 12:02:17 +00:00
|
|
|
#include "cli_vcp.h"
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_version.h>
|
2021-07-18 18:09:00 +00:00
|
|
|
#include <loader/loader.h>
|
2020-11-16 10:16:34 +00:00
|
|
|
|
2022-04-29 12:02:17 +00:00
|
|
|
#define TAG "CliSrv"
|
|
|
|
|
2020-11-16 10:16:34 +00:00
|
|
|
Cli* cli_alloc() {
|
2022-02-18 19:53:46 +00:00
|
|
|
Cli* cli = malloc(sizeof(Cli));
|
2021-07-17 14:14:34 +00:00
|
|
|
|
2021-05-18 09:30:04 +00:00
|
|
|
CliCommandTree_init(cli->commands);
|
2020-11-16 10:16:34 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
cli->last_line = furi_string_alloc();
|
|
|
|
cli->line = furi_string_alloc();
|
2021-07-17 14:14:34 +00:00
|
|
|
|
2022-04-29 12:02:17 +00:00
|
|
|
cli->session = NULL;
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
cli->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
|
2020-11-16 10:16:34 +00:00
|
|
|
furi_check(cli->mutex);
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
cli->idle_sem = furi_semaphore_alloc(1, 0);
|
2022-04-29 12:02:17 +00:00
|
|
|
|
2020-11-16 10:16:34 +00:00
|
|
|
return cli;
|
|
|
|
}
|
|
|
|
|
2022-04-29 12:02:17 +00:00
|
|
|
void cli_putc(Cli* cli, char c) {
|
2021-07-17 14:14:34 +00:00
|
|
|
furi_assert(cli);
|
2022-04-29 12:02:17 +00:00
|
|
|
if(cli->session != NULL) {
|
|
|
|
cli->session->tx((uint8_t*)&c, 1);
|
|
|
|
}
|
2020-11-16 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
[FL-781] FURI, CLI, stdlib: stdout hooks, integration between subsystems, uniform printf usage (#311)
* FURI stdglue: stdout hooks, local and global, ISR safe printf. Uniform newlines for terminal/debug output. Power: prevent sleep while core 2 has not started.
* Furi record, stdglue: check mutex allocation
* remove unused test
* Furi stdglue: buferized output, dynamically allocated state. Furi record: dynamically allocated state. Input dump: proper line ending. Hal VCP: dynamically allocated state.
* Interrupt manager: explicitly init list.
* Makefile: cleanup rules, fix broken dfu upload. F4: add compiler stack protection options.
* BLE: call debug uart callback on transmission complete
* FreeRTOS: add configUSE_NEWLIB_REENTRANT
* API HAL Timebase: fix issue with idle thread stack corruption caused by systick interrupt. BT: cleanup debug info output. FreeRTOS: disable reentry for newlib.
* F4: update stack protection CFLAGS to match used compiller
* F4: disable compiller stack protection because of incompatibility with current compiller
* Makefile: return openocd logs to gdb
* BLE: fixed pin, moar power, ble trace info.
* Prevent sleep when connection is active
* Makefile: return serial port to upload rule, add workaround for mac os
* Furi: prevent usage of stack for cmsis functions.
* F4: add missing includes, add debugger breakpoints
* Applications: per app stack size.
* Furi: honor kernel state in stdglue
* FreeRTOS: remove unused hooks
* Cleanup and format sources
Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
2021-01-29 00:09:33 +00:00
|
|
|
char cli_getc(Cli* cli) {
|
|
|
|
furi_assert(cli);
|
2022-04-29 12:02:17 +00:00
|
|
|
char c = 0;
|
|
|
|
if(cli->session != NULL) {
|
2022-07-20 10:56:33 +00:00
|
|
|
if(cli->session->rx((uint8_t*)&c, 1, FuriWaitForever) == 0) {
|
2022-04-29 12:02:17 +00:00
|
|
|
cli_reset(cli);
|
2023-03-28 07:34:49 +00:00
|
|
|
furi_delay_tick(10);
|
2022-04-29 12:02:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-17 14:14:34 +00:00
|
|
|
cli_reset(cli);
|
2023-03-28 07:34:49 +00:00
|
|
|
furi_delay_tick(10);
|
[FL-781] FURI, CLI, stdlib: stdout hooks, integration between subsystems, uniform printf usage (#311)
* FURI stdglue: stdout hooks, local and global, ISR safe printf. Uniform newlines for terminal/debug output. Power: prevent sleep while core 2 has not started.
* Furi record, stdglue: check mutex allocation
* remove unused test
* Furi stdglue: buferized output, dynamically allocated state. Furi record: dynamically allocated state. Input dump: proper line ending. Hal VCP: dynamically allocated state.
* Interrupt manager: explicitly init list.
* Makefile: cleanup rules, fix broken dfu upload. F4: add compiler stack protection options.
* BLE: call debug uart callback on transmission complete
* FreeRTOS: add configUSE_NEWLIB_REENTRANT
* API HAL Timebase: fix issue with idle thread stack corruption caused by systick interrupt. BT: cleanup debug info output. FreeRTOS: disable reentry for newlib.
* F4: update stack protection CFLAGS to match used compiller
* F4: disable compiller stack protection because of incompatibility with current compiller
* Makefile: return openocd logs to gdb
* BLE: fixed pin, moar power, ble trace info.
* Prevent sleep when connection is active
* Makefile: return serial port to upload rule, add workaround for mac os
* Furi: prevent usage of stack for cmsis functions.
* F4: add missing includes, add debugger breakpoints
* Applications: per app stack size.
* Furi: honor kernel state in stdglue
* FreeRTOS: remove unused hooks
* Cleanup and format sources
Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
2021-01-29 00:09:33 +00:00
|
|
|
}
|
|
|
|
return c;
|
2020-11-17 17:08:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 08:22:52 +00:00
|
|
|
void cli_write(Cli* cli, const uint8_t* buffer, size_t size) {
|
2022-04-29 12:02:17 +00:00
|
|
|
furi_assert(cli);
|
|
|
|
if(cli->session != NULL) {
|
|
|
|
cli->session->tx(buffer, size);
|
|
|
|
}
|
2021-02-13 11:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t cli_read(Cli* cli, uint8_t* buffer, size_t size) {
|
2022-04-29 12:02:17 +00:00
|
|
|
furi_assert(cli);
|
|
|
|
if(cli->session != NULL) {
|
2022-07-20 10:56:33 +00:00
|
|
|
return cli->session->rx(buffer, size, FuriWaitForever);
|
2022-04-29 12:02:17 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t cli_read_timeout(Cli* cli, uint8_t* buffer, size_t size, uint32_t timeout) {
|
|
|
|
furi_assert(cli);
|
|
|
|
if(cli->session != NULL) {
|
|
|
|
return cli->session->rx(buffer, size, timeout);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-11-16 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 03:35:14 +00:00
|
|
|
bool cli_is_connected(Cli* cli) {
|
2022-04-29 12:02:17 +00:00
|
|
|
furi_assert(cli);
|
|
|
|
if(cli->session != NULL) {
|
2022-07-23 03:35:14 +00:00
|
|
|
return (cli->session->is_connected());
|
2021-07-18 18:52:04 +00:00
|
|
|
}
|
2022-04-29 12:02:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-23 03:35:14 +00:00
|
|
|
bool cli_cmd_interrupt_received(Cli* cli) {
|
2022-04-29 12:02:17 +00:00
|
|
|
furi_assert(cli);
|
2022-07-23 03:35:14 +00:00
|
|
|
char c = '\0';
|
|
|
|
if(cli_is_connected(cli)) {
|
|
|
|
if(cli->session->rx((uint8_t*)&c, 1, 0) == 1) {
|
|
|
|
return c == CliSymbolAsciiETX;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
2022-04-29 12:02:17 +00:00
|
|
|
}
|
|
|
|
return false;
|
2021-04-19 16:36:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 14:50:28 +00:00
|
|
|
void cli_print_usage(const char* cmd, const char* usage, const char* arg) {
|
|
|
|
furi_assert(cmd);
|
|
|
|
furi_assert(arg);
|
|
|
|
furi_assert(usage);
|
|
|
|
|
|
|
|
printf("%s: illegal option -- %s\r\nusage: %s %s", cmd, arg, cmd, usage);
|
|
|
|
}
|
|
|
|
|
2020-11-16 10:16:34 +00:00
|
|
|
void cli_motd() {
|
2021-07-17 14:14:34 +00:00
|
|
|
printf("\r\n"
|
|
|
|
" _.-------.._ -,\r\n"
|
|
|
|
" .-\"```\"--..,,_/ /`-, -, \\ \r\n"
|
|
|
|
" .:\" /:/ /'\\ \\ ,_..., `. | |\r\n"
|
|
|
|
" / ,----/:/ /`\\ _\\~`_-\"` _;\r\n"
|
|
|
|
" ' / /`\"\"\"'\\ \\ \\.~`_-' ,-\"'/ \r\n"
|
|
|
|
" | | | 0 | | .-' ,/` /\r\n"
|
|
|
|
" | ,..\\ \\ ,.-\"` ,/` /\r\n"
|
|
|
|
" ; : `/`\"\"\\` ,/--==,/-----,\r\n"
|
|
|
|
" | `-...| -.___-Z:_______J...---;\r\n"
|
|
|
|
" : ` _-'\r\n"
|
|
|
|
" _L_ _ ___ ___ ___ ___ ____--\"`___ _ ___\r\n"
|
|
|
|
"| __|| | |_ _|| _ \\| _ \\| __|| _ \\ / __|| | |_ _|\r\n"
|
|
|
|
"| _| | |__ | | | _/| _/| _| | / | (__ | |__ | |\r\n"
|
|
|
|
"|_| |____||___||_| |_| |___||_|_\\ \\___||____||___|\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"Welcome to Flipper Zero Command Line Interface!\r\n"
|
|
|
|
"Read Manual https://docs.flipperzero.one\r\n"
|
|
|
|
"\r\n");
|
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
const Version* firmware_version = furi_hal_version_get_firmware_version();
|
2021-07-17 14:14:34 +00:00
|
|
|
if(firmware_version) {
|
|
|
|
printf(
|
2022-04-13 20:50:25 +00:00
|
|
|
"Firmware version: %s %s (%s%s built on %s)\r\n",
|
2021-07-17 14:14:34 +00:00
|
|
|
version_get_gitbranch(firmware_version),
|
|
|
|
version_get_version(firmware_version),
|
|
|
|
version_get_githash(firmware_version),
|
2022-04-13 20:50:25 +00:00
|
|
|
version_get_dirty_flag(firmware_version) ? "-dirty" : "",
|
2021-07-17 14:14:34 +00:00
|
|
|
version_get_builddate(firmware_version));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cli_nl(Cli* cli) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(cli);
|
[FL-781] FURI, CLI, stdlib: stdout hooks, integration between subsystems, uniform printf usage (#311)
* FURI stdglue: stdout hooks, local and global, ISR safe printf. Uniform newlines for terminal/debug output. Power: prevent sleep while core 2 has not started.
* Furi record, stdglue: check mutex allocation
* remove unused test
* Furi stdglue: buferized output, dynamically allocated state. Furi record: dynamically allocated state. Input dump: proper line ending. Hal VCP: dynamically allocated state.
* Interrupt manager: explicitly init list.
* Makefile: cleanup rules, fix broken dfu upload. F4: add compiler stack protection options.
* BLE: call debug uart callback on transmission complete
* FreeRTOS: add configUSE_NEWLIB_REENTRANT
* API HAL Timebase: fix issue with idle thread stack corruption caused by systick interrupt. BT: cleanup debug info output. FreeRTOS: disable reentry for newlib.
* F4: update stack protection CFLAGS to match used compiller
* F4: disable compiller stack protection because of incompatibility with current compiller
* Makefile: return openocd logs to gdb
* BLE: fixed pin, moar power, ble trace info.
* Prevent sleep when connection is active
* Makefile: return serial port to upload rule, add workaround for mac os
* Furi: prevent usage of stack for cmsis functions.
* F4: add missing includes, add debugger breakpoints
* Applications: per app stack size.
* Furi: honor kernel state in stdglue
* FreeRTOS: remove unused hooks
* Cleanup and format sources
Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
2021-01-29 00:09:33 +00:00
|
|
|
printf("\r\n");
|
2020-11-16 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 14:14:34 +00:00
|
|
|
void cli_prompt(Cli* cli) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(cli);
|
2022-10-05 15:15:23 +00:00
|
|
|
printf("\r\n>: %s", furi_string_get_cstr(cli->line));
|
[FL-781] FURI, CLI, stdlib: stdout hooks, integration between subsystems, uniform printf usage (#311)
* FURI stdglue: stdout hooks, local and global, ISR safe printf. Uniform newlines for terminal/debug output. Power: prevent sleep while core 2 has not started.
* Furi record, stdglue: check mutex allocation
* remove unused test
* Furi stdglue: buferized output, dynamically allocated state. Furi record: dynamically allocated state. Input dump: proper line ending. Hal VCP: dynamically allocated state.
* Interrupt manager: explicitly init list.
* Makefile: cleanup rules, fix broken dfu upload. F4: add compiler stack protection options.
* BLE: call debug uart callback on transmission complete
* FreeRTOS: add configUSE_NEWLIB_REENTRANT
* API HAL Timebase: fix issue with idle thread stack corruption caused by systick interrupt. BT: cleanup debug info output. FreeRTOS: disable reentry for newlib.
* F4: update stack protection CFLAGS to match used compiller
* F4: disable compiller stack protection because of incompatibility with current compiller
* Makefile: return openocd logs to gdb
* BLE: fixed pin, moar power, ble trace info.
* Prevent sleep when connection is active
* Makefile: return serial port to upload rule, add workaround for mac os
* Furi: prevent usage of stack for cmsis functions.
* F4: add missing includes, add debugger breakpoints
* Applications: per app stack size.
* Furi: honor kernel state in stdglue
* FreeRTOS: remove unused hooks
* Cleanup and format sources
Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
2021-01-29 00:09:33 +00:00
|
|
|
fflush(stdout);
|
2020-11-16 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 14:14:34 +00:00
|
|
|
void cli_reset(Cli* cli) {
|
2021-12-01 15:44:39 +00:00
|
|
|
// cli->last_line is cleared and cli->line's buffer moved to cli->last_line
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_move(cli->last_line, cli->line);
|
2021-12-01 15:44:39 +00:00
|
|
|
// Reiniting cli->line
|
2022-10-05 15:15:23 +00:00
|
|
|
cli->line = furi_string_alloc();
|
2021-07-17 14:14:34 +00:00
|
|
|
cli->cursor_position = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cli_handle_backspace(Cli* cli) {
|
2022-06-28 13:03:49 +00:00
|
|
|
if(cli->cursor_position > 0) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_assert(furi_string_size(cli->line) > 0);
|
2021-07-17 14:14:34 +00:00
|
|
|
// Other side
|
|
|
|
printf("\e[D\e[1P");
|
|
|
|
fflush(stdout);
|
|
|
|
// Our side
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_replace_at(cli->line, cli->cursor_position - 1, 1, "");
|
2021-12-01 15:44:39 +00:00
|
|
|
|
2021-07-17 14:14:34 +00:00
|
|
|
cli->cursor_position--;
|
2020-11-16 10:16:34 +00:00
|
|
|
} else {
|
2022-04-29 12:02:17 +00:00
|
|
|
cli_putc(cli, CliSymbolAsciiBell);
|
2020-11-16 10:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 14:14:34 +00:00
|
|
|
static void cli_normalize_line(Cli* cli) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_trim(cli->line);
|
|
|
|
cli->cursor_position = furi_string_size(cli->line);
|
2021-07-17 14:14:34 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
static void cli_execute_command(Cli* cli, CliCommand* command, FuriString* args) {
|
2021-07-18 19:08:26 +00:00
|
|
|
if(!(command->flags & CliCommandFlagInsomniaSafe)) {
|
2021-08-08 18:03:25 +00:00
|
|
|
furi_hal_power_insomnia_enter();
|
2021-07-18 19:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that we running alone
|
|
|
|
if(!(command->flags & CliCommandFlagParallelSafe)) {
|
2022-07-26 12:21:51 +00:00
|
|
|
Loader* loader = furi_record_open(RECORD_LOADER);
|
2021-07-18 19:08:26 +00:00
|
|
|
bool safety_lock = loader_lock(loader);
|
|
|
|
if(safety_lock) {
|
|
|
|
// Execute command
|
|
|
|
command->callback(cli, args, command->context);
|
|
|
|
loader_unlock(loader);
|
|
|
|
} else {
|
|
|
|
printf("Other application is running, close it first");
|
|
|
|
}
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_LOADER);
|
2021-07-18 19:08:26 +00:00
|
|
|
} else {
|
|
|
|
// Execute command
|
|
|
|
command->callback(cli, args, command->context);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(command->flags & CliCommandFlagInsomniaSafe)) {
|
2021-08-08 18:03:25 +00:00
|
|
|
furi_hal_power_insomnia_exit();
|
2021-07-18 19:08:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 14:14:34 +00:00
|
|
|
static void cli_handle_enter(Cli* cli) {
|
|
|
|
cli_normalize_line(cli);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_size(cli->line) == 0) {
|
2021-07-17 14:14:34 +00:00
|
|
|
cli_prompt(cli);
|
2020-11-16 10:16:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-17 14:14:34 +00:00
|
|
|
// Command and args container
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* command;
|
|
|
|
command = furi_string_alloc();
|
|
|
|
FuriString* args;
|
|
|
|
args = furi_string_alloc();
|
2021-07-17 14:14:34 +00:00
|
|
|
|
|
|
|
// Split command and args
|
2022-10-05 15:15:23 +00:00
|
|
|
size_t ws = furi_string_search_char(cli->line, ' ');
|
|
|
|
if(ws == FURI_STRING_FAILURE) {
|
|
|
|
furi_string_set(command, cli->line);
|
2020-11-16 10:16:34 +00:00
|
|
|
} else {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set_n(command, cli->line, 0, ws);
|
|
|
|
furi_string_set_n(args, cli->line, ws, furi_string_size(cli->line));
|
|
|
|
furi_string_trim(args);
|
2020-11-16 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search for command
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_acquire(cli->mutex, FuriWaitForever) == FuriStatusOk);
|
2022-07-06 13:54:08 +00:00
|
|
|
CliCommand* cli_command_ptr = CliCommandTree_get(cli->commands, command);
|
|
|
|
|
2022-12-26 12:13:30 +00:00
|
|
|
if(cli_command_ptr) { //-V547
|
2022-07-06 13:54:08 +00:00
|
|
|
CliCommand cli_command;
|
|
|
|
memcpy(&cli_command, cli_command_ptr, sizeof(CliCommand));
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_release(cli->mutex) == FuriStatusOk);
|
2021-07-17 14:14:34 +00:00
|
|
|
cli_nl(cli);
|
2022-07-06 13:54:08 +00:00
|
|
|
cli_execute_command(cli, &cli_command, args);
|
2020-11-16 10:16:34 +00:00
|
|
|
} else {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_release(cli->mutex) == FuriStatusOk);
|
2021-07-17 14:14:34 +00:00
|
|
|
cli_nl(cli);
|
|
|
|
printf(
|
|
|
|
"`%s` command not found, use `help` or `?` to list all available commands",
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_get_cstr(command));
|
2022-04-29 12:02:17 +00:00
|
|
|
cli_putc(cli, CliSymbolAsciiBell);
|
2020-11-16 10:16:34 +00:00
|
|
|
}
|
2021-07-17 14:14:34 +00:00
|
|
|
|
2021-07-20 17:14:42 +00:00
|
|
|
cli_reset(cli);
|
2021-07-17 14:14:34 +00:00
|
|
|
cli_prompt(cli);
|
2020-11-16 10:16:34 +00:00
|
|
|
|
2021-07-17 14:14:34 +00:00
|
|
|
// Cleanup command and args
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(command);
|
|
|
|
furi_string_free(args);
|
2021-07-17 14:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cli_handle_autocomplete(Cli* cli) {
|
|
|
|
cli_normalize_line(cli);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_size(cli->line) == 0) {
|
2021-07-17 14:14:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cli_nl(cli);
|
|
|
|
|
|
|
|
// Prepare common base for autocomplete
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* common;
|
|
|
|
common = furi_string_alloc();
|
2021-07-17 14:14:34 +00:00
|
|
|
// Iterate throw commands
|
|
|
|
for
|
|
|
|
M_EACH(cli_command, cli->commands, CliCommandTree_t) {
|
|
|
|
// Process only if starts with line buffer
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_start_with(*cli_command->key_ptr, cli->line)) {
|
2021-07-17 14:14:34 +00:00
|
|
|
// Show autocomplete option
|
2022-10-05 15:15:23 +00:00
|
|
|
printf("%s\r\n", furi_string_get_cstr(*cli_command->key_ptr));
|
2021-07-17 14:14:34 +00:00
|
|
|
// Process common base for autocomplete
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_size(common) > 0) {
|
2021-07-17 14:14:34 +00:00
|
|
|
// Choose shortest string
|
2022-10-05 15:15:23 +00:00
|
|
|
const size_t key_size = furi_string_size(*cli_command->key_ptr);
|
|
|
|
const size_t common_size = furi_string_size(common);
|
2021-07-17 14:14:34 +00:00
|
|
|
const size_t min_size = key_size > common_size ? common_size : key_size;
|
|
|
|
size_t i = 0;
|
|
|
|
while(i < min_size) {
|
|
|
|
// Stop when do not match
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_get_char(*cli_command->key_ptr, i) !=
|
|
|
|
furi_string_get_char(common, i)) {
|
2021-07-17 14:14:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
// Cut right part if any
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_left(common, i);
|
2021-07-17 14:14:34 +00:00
|
|
|
} else {
|
|
|
|
// Start with something
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(common, *cli_command->key_ptr);
|
2021-07-17 14:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Replace line buffer if autocomplete better
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_size(common) > furi_string_size(cli->line)) {
|
|
|
|
furi_string_set(cli->line, common);
|
|
|
|
cli->cursor_position = furi_string_size(cli->line);
|
2021-07-17 14:14:34 +00:00
|
|
|
}
|
|
|
|
// Cleanup
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(common);
|
2021-07-17 14:14:34 +00:00
|
|
|
// Show prompt
|
|
|
|
cli_prompt(cli);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cli_handle_escape(Cli* cli, char c) {
|
|
|
|
if(c == 'A') {
|
|
|
|
// Use previous command if line buffer is empty
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_size(cli->line) == 0 && furi_string_cmp(cli->line, cli->last_line) != 0) {
|
2021-07-17 14:14:34 +00:00
|
|
|
// Set line buffer and cursor position
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(cli->line, cli->last_line);
|
|
|
|
cli->cursor_position = furi_string_size(cli->line);
|
2021-07-17 14:14:34 +00:00
|
|
|
// Show new line to user
|
2022-10-05 15:15:23 +00:00
|
|
|
printf("%s", furi_string_get_cstr(cli->line));
|
2021-07-17 14:14:34 +00:00
|
|
|
}
|
|
|
|
} else if(c == 'B') {
|
|
|
|
} else if(c == 'C') {
|
2022-10-05 15:15:23 +00:00
|
|
|
if(cli->cursor_position < furi_string_size(cli->line)) {
|
2021-07-17 14:14:34 +00:00
|
|
|
cli->cursor_position++;
|
|
|
|
printf("\e[C");
|
|
|
|
}
|
|
|
|
} else if(c == 'D') {
|
|
|
|
if(cli->cursor_position > 0) {
|
|
|
|
cli->cursor_position--;
|
|
|
|
printf("\e[D");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
2020-11-16 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cli_process_input(Cli* cli) {
|
2022-04-29 12:02:17 +00:00
|
|
|
char in_chr = cli_getc(cli);
|
|
|
|
size_t rx_len;
|
2020-11-16 10:16:34 +00:00
|
|
|
|
2022-04-29 12:02:17 +00:00
|
|
|
if(in_chr == CliSymbolAsciiTab) {
|
2021-07-17 14:14:34 +00:00
|
|
|
cli_handle_autocomplete(cli);
|
2022-04-29 12:02:17 +00:00
|
|
|
} else if(in_chr == CliSymbolAsciiSOH) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_delay_ms(33); // We are too fast, Minicom is not ready yet
|
2020-11-16 10:16:34 +00:00
|
|
|
cli_motd();
|
2021-07-17 14:14:34 +00:00
|
|
|
cli_prompt(cli);
|
2022-04-29 12:02:17 +00:00
|
|
|
} else if(in_chr == CliSymbolAsciiETX) {
|
2021-08-13 13:18:13 +00:00
|
|
|
cli_reset(cli);
|
|
|
|
cli_prompt(cli);
|
2022-04-29 12:02:17 +00:00
|
|
|
} else if(in_chr == CliSymbolAsciiEOT) {
|
2021-07-17 14:14:34 +00:00
|
|
|
cli_reset(cli);
|
2022-04-29 12:02:17 +00:00
|
|
|
} else if(in_chr == CliSymbolAsciiEsc) {
|
|
|
|
rx_len = cli_read(cli, (uint8_t*)&in_chr, 1);
|
|
|
|
if((rx_len > 0) && (in_chr == '[')) {
|
|
|
|
cli_read(cli, (uint8_t*)&in_chr, 1);
|
|
|
|
cli_handle_escape(cli, in_chr);
|
2020-11-16 10:16:34 +00:00
|
|
|
} else {
|
2022-04-29 12:02:17 +00:00
|
|
|
cli_putc(cli, CliSymbolAsciiBell);
|
2020-11-16 10:16:34 +00:00
|
|
|
}
|
2022-04-29 12:02:17 +00:00
|
|
|
} else if(in_chr == CliSymbolAsciiBackspace || in_chr == CliSymbolAsciiDel) {
|
2021-07-17 14:14:34 +00:00
|
|
|
cli_handle_backspace(cli);
|
2022-04-29 12:02:17 +00:00
|
|
|
} else if(in_chr == CliSymbolAsciiCR) {
|
2021-07-17 14:14:34 +00:00
|
|
|
cli_handle_enter(cli);
|
2022-12-26 12:13:30 +00:00
|
|
|
} else if(in_chr >= 0x20 && in_chr < 0x7F) { //-V560
|
2022-10-05 15:15:23 +00:00
|
|
|
if(cli->cursor_position == furi_string_size(cli->line)) {
|
|
|
|
furi_string_push_back(cli->line, in_chr);
|
2022-04-29 12:02:17 +00:00
|
|
|
cli_putc(cli, in_chr);
|
2021-07-17 14:14:34 +00:00
|
|
|
} else {
|
2022-10-05 15:15:23 +00:00
|
|
|
// Insert character to line buffer
|
|
|
|
const char in_str[2] = {in_chr, 0};
|
|
|
|
furi_string_replace_at(cli->line, cli->cursor_position, 0, in_str);
|
2021-12-01 15:44:39 +00:00
|
|
|
|
2021-07-17 14:14:34 +00:00
|
|
|
// Print character in replace mode
|
2022-04-29 12:02:17 +00:00
|
|
|
printf("\e[4h%c\e[4l", in_chr);
|
2021-07-17 14:14:34 +00:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
cli->cursor_position++;
|
2020-11-16 10:16:34 +00:00
|
|
|
} else {
|
2022-04-29 12:02:17 +00:00
|
|
|
cli_putc(cli, CliSymbolAsciiBell);
|
2020-11-16 10:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 18:09:00 +00:00
|
|
|
void cli_add_command(
|
|
|
|
Cli* cli,
|
|
|
|
const char* name,
|
|
|
|
CliCommandFlag flags,
|
|
|
|
CliCallback callback,
|
|
|
|
void* context) {
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* name_str;
|
|
|
|
name_str = furi_string_alloc_set(name);
|
|
|
|
furi_string_trim(name_str);
|
2020-11-17 17:08:31 +00:00
|
|
|
|
|
|
|
size_t name_replace;
|
|
|
|
do {
|
2022-10-05 15:15:23 +00:00
|
|
|
name_replace = furi_string_replace(name_str, " ", "_");
|
|
|
|
} while(name_replace != FURI_STRING_FAILURE);
|
2020-11-17 17:08:31 +00:00
|
|
|
|
2020-11-16 10:16:34 +00:00
|
|
|
CliCommand c;
|
|
|
|
c.callback = callback;
|
|
|
|
c.context = context;
|
2021-07-18 18:09:00 +00:00
|
|
|
c.flags = flags;
|
2020-11-16 10:16:34 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_acquire(cli->mutex, FuriWaitForever) == FuriStatusOk);
|
2021-05-18 09:30:04 +00:00
|
|
|
CliCommandTree_set_at(cli->commands, name_str, c);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_release(cli->mutex) == FuriStatusOk);
|
2020-11-17 17:08:31 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(name_str);
|
2020-11-16 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 14:24:33 +00:00
|
|
|
void cli_delete_command(Cli* cli, const char* name) {
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* name_str;
|
|
|
|
name_str = furi_string_alloc_set(name);
|
|
|
|
furi_string_trim(name_str);
|
2021-04-14 14:24:33 +00:00
|
|
|
|
|
|
|
size_t name_replace;
|
|
|
|
do {
|
2022-10-05 15:15:23 +00:00
|
|
|
name_replace = furi_string_replace(name_str, " ", "_");
|
|
|
|
} while(name_replace != FURI_STRING_FAILURE);
|
2021-04-14 14:24:33 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_acquire(cli->mutex, FuriWaitForever) == FuriStatusOk);
|
2021-05-18 09:30:04 +00:00
|
|
|
CliCommandTree_erase(cli->commands, name_str);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_release(cli->mutex) == FuriStatusOk);
|
2021-04-14 14:24:33 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(name_str);
|
2021-04-14 14:24:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-29 12:02:17 +00:00
|
|
|
void cli_session_open(Cli* cli, void* session) {
|
|
|
|
furi_assert(cli);
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_acquire(cli->mutex, FuriWaitForever) == FuriStatusOk);
|
2022-04-29 12:02:17 +00:00
|
|
|
cli->session = session;
|
|
|
|
if(cli->session != NULL) {
|
|
|
|
cli->session->init();
|
2022-08-03 16:00:17 +00:00
|
|
|
furi_thread_set_stdout_callback(cli->session->tx_stdout);
|
2022-04-29 12:02:17 +00:00
|
|
|
} else {
|
2022-08-03 16:00:17 +00:00
|
|
|
furi_thread_set_stdout_callback(NULL);
|
2022-04-29 12:02:17 +00:00
|
|
|
}
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_semaphore_release(cli->idle_sem);
|
|
|
|
furi_check(furi_mutex_release(cli->mutex) == FuriStatusOk);
|
2022-04-29 12:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cli_session_close(Cli* cli) {
|
|
|
|
furi_assert(cli);
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_acquire(cli->mutex, FuriWaitForever) == FuriStatusOk);
|
2022-04-29 12:02:17 +00:00
|
|
|
if(cli->session != NULL) {
|
|
|
|
cli->session->deinit();
|
|
|
|
}
|
|
|
|
cli->session = NULL;
|
2022-08-03 16:00:17 +00:00
|
|
|
furi_thread_set_stdout_callback(NULL);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_release(cli->mutex) == FuriStatusOk);
|
2022-04-29 12:02:17 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 16:54:42 +00:00
|
|
|
int32_t cli_srv(void* p) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(p);
|
2020-11-16 10:16:34 +00:00
|
|
|
Cli* cli = cli_alloc();
|
|
|
|
|
|
|
|
// Init basic cli commands
|
|
|
|
cli_commands_init(cli);
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_create(RECORD_CLI, cli);
|
2020-11-16 10:16:34 +00:00
|
|
|
|
2022-04-29 12:02:17 +00:00
|
|
|
if(cli->session != NULL) {
|
2022-08-03 16:00:17 +00:00
|
|
|
furi_thread_set_stdout_callback(cli->session->tx_stdout);
|
2022-04-29 12:02:17 +00:00
|
|
|
} else {
|
2022-08-03 16:00:17 +00:00
|
|
|
furi_thread_set_stdout_callback(NULL);
|
2022-04-29 12:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(furi_hal_rtc_get_boot_mode() == FuriHalRtcBootModeNormal) {
|
|
|
|
cli_session_open(cli, &cli_vcp);
|
|
|
|
} else {
|
2023-02-26 15:15:26 +00:00
|
|
|
FURI_LOG_W(TAG, "Skipping start in special boot mode");
|
2022-04-29 12:02:17 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 10:16:34 +00:00
|
|
|
while(1) {
|
2022-04-29 12:02:17 +00:00
|
|
|
if(cli->session != NULL) {
|
|
|
|
cli_process_input(cli);
|
|
|
|
} else {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_semaphore_acquire(cli->idle_sem, FuriWaitForever) == FuriStatusOk);
|
2022-04-29 12:02:17 +00:00
|
|
|
}
|
2020-11-16 10:16:34 +00:00
|
|
|
}
|
2021-02-12 17:24:34 +00:00
|
|
|
|
|
|
|
return 0;
|
2020-11-16 10:16:34 +00:00
|
|
|
}
|