flipperzero-firmware/applications/cli/cli.c

255 lines
6.7 KiB
C
Raw Normal View History

#include "cli_i.h"
#include "cli_commands.h"
#include <version.h>
#include <api-hal-version.h>
Cli* cli_alloc() {
Cli* cli = furi_alloc(sizeof(Cli));
CliCommandTree_init(cli->commands);
cli->mutex = osMutexNew(NULL);
furi_check(cli->mutex);
cli_reset_state(cli);
return cli;
}
void cli_free(Cli* cli) {
free(cli);
}
void cli_reset_state(Cli* cli) {
// Release allocated buffer, reset state
string_clear(cli->line);
string_init(cli->line);
}
void cli_putc(char c) {
api_hal_vcp_tx((uint8_t*)&c, 1);
}
[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);
char c;
if(api_hal_vcp_rx((uint8_t*)&c, 1) == 0) {
cli_reset_state(cli);
}
return c;
}
[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
void cli_stdout_callback(void* _cookie, const char* data, size_t size) {
api_hal_vcp_tx((const uint8_t*)data, size);
}
void cli_write(Cli* cli, uint8_t* buffer, size_t size) {
return api_hal_vcp_tx(buffer, size);
}
size_t cli_read(Cli* cli, uint8_t* buffer, size_t size) {
return api_hal_vcp_rx(buffer, size);
}
bool cli_cmd_interrupt_received(Cli* cli) {
char c;
api_hal_vcp_rx_with_timeout((uint8_t*)&c, 1, 1);
return c == CliSymbolAsciiETX;
}
void cli_print_version(const Version* version) {
if(version) {
printf("\tVersion:\t%s\r\n", version_get_version(version));
printf("\tBuild date:\t%s\r\n", version_get_builddate(version));
printf(
"\tGit Commit:\t%s (%s)\r\n",
version_get_githash(version),
version_get_gitbranchnum(version));
printf("\tGit Branch:\t%s\r\n", version_get_gitbranch(version));
} else {
printf("\tNo build info\r\n");
}
}
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);
}
void cli_motd() {
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");
printf("You are now connected to Flipper Command Line Interface.\r\n\r\n");
printf("Bootloader\r\n");
cli_print_version(api_hal_version_get_boot_version());
printf("Firmware\r\n");
cli_print_version(api_hal_version_get_fw_version());
}
void cli_nl() {
[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");
}
void cli_prompt() {
[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>: ");
fflush(stdout);
}
void cli_backspace(Cli* cli) {
size_t s = string_size(cli->line);
if(s > 0) {
s--;
string_left(cli->line, s);
cli_putc(CliSymbolAsciiBackspace);
cli_putc(CliSymbolAsciiSpace);
cli_putc(CliSymbolAsciiBackspace);
} else {
cli_putc(CliSymbolAsciiBell);
}
}
void cli_enter(Cli* cli) {
// Normalize input
string_strim(cli->line);
if(string_size(cli->line) == 0) {
cli_prompt();
return;
}
// Get first word as command name
string_t command;
string_init(command);
size_t ws = string_search_char(cli->line, ' ');
if(ws == STRING_FAILURE) {
string_set(command, cli->line);
string_clear(cli->line);
string_init(cli->line);
} else {
string_set_n(command, cli->line, 0, ws);
string_right(cli->line, ws);
string_strim(cli->line);
}
// Search for command
furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
CliCommand* cli_command = CliCommandTree_get(cli->commands, command);
furi_check(osMutexRelease(cli->mutex) == osOK);
if(cli_command) {
cli_nl();
cli_command->callback(cli, cli->line, cli_command->context);
cli_prompt();
} else {
cli_nl();
[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("Command not found: ");
printf(string_get_cstr(command));
cli_prompt();
cli_putc(CliSymbolAsciiBell);
}
string_clear(command);
// Always finish with clean state
cli_reset_state(cli);
}
void cli_process_input(Cli* 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
char c = cli_getc(cli);
size_t r;
if(c == CliSymbolAsciiTab) {
cli_putc(CliSymbolAsciiBell);
} else if(c == CliSymbolAsciiSOH) {
cli_motd();
cli_prompt();
} else if(c == CliSymbolAsciiEOT) {
cli_reset_state(cli);
} else if(c == CliSymbolAsciiEsc) {
r = api_hal_vcp_rx((uint8_t*)&c, 1);
if(r && c == '[') {
api_hal_vcp_rx((uint8_t*)&c, 1);
} else {
cli_putc(CliSymbolAsciiBell);
}
} else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
cli_backspace(cli);
} else if(c == CliSymbolAsciiCR) {
cli_enter(cli);
} else if(c >= 0x20 && c < 0x7F) {
string_push_back(cli->line, c);
cli_putc(c);
} else {
cli_putc(CliSymbolAsciiBell);
}
}
void cli_add_command(Cli* cli, const char* name, CliCallback callback, void* context) {
string_t name_str;
string_init_set_str(name_str, name);
string_strim(name_str);
size_t name_replace;
do {
name_replace = string_replace_str(name_str, " ", "_");
} while(name_replace != STRING_FAILURE);
CliCommand c;
c.callback = callback;
c.context = context;
furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
CliCommandTree_set_at(cli->commands, name_str, c);
furi_check(osMutexRelease(cli->mutex) == osOK);
string_clear(name_str);
}
void cli_delete_command(Cli* cli, const char* name) {
string_t name_str;
string_init_set_str(name_str, name);
string_strim(name_str);
size_t name_replace;
do {
name_replace = string_replace_str(name_str, " ", "_");
} while(name_replace != STRING_FAILURE);
furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
CliCommandTree_erase(cli->commands, name_str);
furi_check(osMutexRelease(cli->mutex) == osOK);
string_clear(name_str);
}
int32_t cli_task(void* p) {
Cli* cli = cli_alloc();
// Init basic cli commands
cli_commands_init(cli);
[FL-140] Core api dynamic records (#296) * SYSTEM: tickless mode with deep sleep. * Move FreeRTOS ticks to lptim2 * API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source. * API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode. * NFC: support for tickless mode. * API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications. * BLE: prevent sleep while core2 starting * HAL: nap while in insomnia mode * init records work * try to implement record delete * tests and flapp * flapp subsystem * new core functions to get app stat, simplify core code * fix thread termination * add strdup to core * fix tests * Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage * Refactoring: update furi record api usage, cleanup code * Fix broken merge for freertos apps * Core, Target: fix compilation warnings * Drop firmware target local * HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode. * SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial. * delete old app example-ipc * delete old app fatfs list * fix strobe app, add input header * delete old display driver * comment old app qr-code * fix sd-card test, add forced widget update * remove unused new core test * increase heap to 128k * comment and assert old core tests * fix syntax Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 16:09:26 +00:00
furi_record_create("cli", 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
furi_stdglue_set_thread_stdout_callback(cli_stdout_callback);
while(1) {
cli_process_input(cli);
}
return 0;
}