USB VCP Cli (#237)
* Core: ring buffer. * Api: usb vcp. F3: vcp glue code. * Applications: cli draft version. * Cli: basic working version, includes help and version commands * HAL: vcp on f2 * Makefile: update openocd conf * F3: vcp rx with freertos stream * Cli: help * Cli: standard commands, api-hal-uid * Power: cli poweroff.
This commit is contained in:
173
applications/cli/cli.c
Normal file
173
applications/cli/cli.c
Normal file
@@ -0,0 +1,173 @@
|
||||
#include "cli_i.h"
|
||||
#include "cli_commands.h"
|
||||
|
||||
#include <api-hal-vcp.h>
|
||||
|
||||
Cli* cli_alloc() {
|
||||
Cli* cli = furi_alloc(sizeof(Cli));
|
||||
CliCommandDict_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) {
|
||||
string_clear(cli->line);
|
||||
string_init(cli->line);
|
||||
}
|
||||
|
||||
void cli_putc(char c) {
|
||||
api_hal_vcp_tx((uint8_t*)&c, 1);
|
||||
}
|
||||
|
||||
void cli_print(const char* str) {
|
||||
api_hal_vcp_tx((uint8_t*)str, strlen(str));
|
||||
}
|
||||
|
||||
void cli_print_version() {
|
||||
cli_print("Build date:" BUILD_DATE ". "
|
||||
"Git Commit:" GIT_COMMIT ". "
|
||||
"Git Branch:" GIT_BRANCH ". "
|
||||
"Commit Number:" GIT_BRANCH_NUM ".");
|
||||
}
|
||||
|
||||
void cli_motd() {
|
||||
cli_print("Flipper cli.\r\n");
|
||||
cli_print_version();
|
||||
}
|
||||
|
||||
void cli_nl() {
|
||||
cli_print("\r\n");
|
||||
}
|
||||
|
||||
void cli_prompt() {
|
||||
cli_print("\r\n>: ");
|
||||
}
|
||||
|
||||
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 = CliCommandDict_get(cli->commands, command);
|
||||
furi_check(osMutexRelease(cli->mutex) == osOK);
|
||||
if(cli_command) {
|
||||
cli_nl();
|
||||
cli_command->callback(cli->line, cli_command->context);
|
||||
cli_prompt();
|
||||
} else {
|
||||
cli_nl();
|
||||
cli_print("Command not found: ");
|
||||
cli_print(string_get_cstr(command));
|
||||
cli_prompt();
|
||||
cli_putc(CliSymbolAsciiBell);
|
||||
}
|
||||
|
||||
// Always finish with clean state
|
||||
cli_reset_state(cli);
|
||||
}
|
||||
|
||||
void cli_process_input(Cli* cli) {
|
||||
char c;
|
||||
size_t r;
|
||||
|
||||
r = api_hal_vcp_rx((uint8_t*)&c, 1);
|
||||
if(r == 0) {
|
||||
cli_reset_state(cli);
|
||||
}
|
||||
|
||||
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);
|
||||
CliCommand c;
|
||||
c.callback = callback;
|
||||
c.context = context;
|
||||
|
||||
furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
|
||||
CliCommandDict_set_at(cli->commands, name_str, c);
|
||||
furi_check(osMutexRelease(cli->mutex) == osOK);
|
||||
}
|
||||
|
||||
void cli_task(void* p) {
|
||||
Cli* cli = cli_alloc();
|
||||
|
||||
// Init basic cli commands
|
||||
cli_commands_init(cli);
|
||||
|
||||
if(!furi_create("cli", cli)) {
|
||||
printf("[cli_task] cannot create the cli record\n");
|
||||
furiac_exit(NULL);
|
||||
}
|
||||
|
||||
furiac_ready();
|
||||
|
||||
while(1) {
|
||||
cli_process_input(cli);
|
||||
}
|
||||
}
|
42
applications/cli/cli.h
Normal file
42
applications/cli/cli.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <m-string.h>
|
||||
|
||||
/* Cli type
|
||||
* Anonymous structure. Use cli_i.h if you need to go deeper.
|
||||
*/
|
||||
typedef struct Cli Cli;
|
||||
|
||||
/* Cli callback function pointer.
|
||||
* Implement this interface and use add_cli_command
|
||||
* @param args - string with what was passed after command
|
||||
* @param context - pointer to whatever you gave us on cli_add_command
|
||||
*/
|
||||
typedef void (*CliCallback)(string_t args, void* context);
|
||||
|
||||
/* Add cli command
|
||||
* Registers you command callback
|
||||
* @param cli - pointer to cli instance
|
||||
* @param name - command name
|
||||
* @param callback - callback function
|
||||
* @param context - pointer to whatever we need to pass to callback
|
||||
*/
|
||||
void cli_add_command(Cli* cli, const char* name, CliCallback callback, void* context);
|
||||
|
||||
/* Read terminal input.
|
||||
* Do it only from inside of callback.
|
||||
* @param buffer - buffer pointer to char buffer
|
||||
* @param size - size of buffer in bytes
|
||||
*/
|
||||
void cli_read(char* buffer, size_t size);
|
||||
|
||||
/* Print to terminal
|
||||
* Do it only from inside of callback.
|
||||
* @param buffer - pointer to null terminated string to print.
|
||||
*/
|
||||
void cli_print(const char* buffer);
|
||||
|
||||
/* New line
|
||||
* Send new ine sequence
|
||||
*/
|
||||
void cli_nl();
|
54
applications/cli/cli_commands.c
Normal file
54
applications/cli/cli_commands.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#include "cli_commands.h"
|
||||
#include <api-hal.h>
|
||||
|
||||
void cli_command_help(string_t args, void* context) {
|
||||
(void)args;
|
||||
Cli* cli = context;
|
||||
cli_print("Commands we have:");
|
||||
|
||||
furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
|
||||
CliCommandDict_it_t it;
|
||||
for(CliCommandDict_it(it, cli->commands); !CliCommandDict_end_p(it); CliCommandDict_next(it)) {
|
||||
CliCommandDict_itref_t* ref = CliCommandDict_ref(it);
|
||||
cli_print(" ");
|
||||
cli_print(string_get_cstr(ref->key));
|
||||
};
|
||||
furi_check(osMutexRelease(cli->mutex) == osOK);
|
||||
|
||||
if(string_size(args) > 0) {
|
||||
cli_nl();
|
||||
cli_print("Also I have no clue what '");
|
||||
cli_print(string_get_cstr(args));
|
||||
cli_print("' is.");
|
||||
}
|
||||
}
|
||||
|
||||
void cli_command_version(string_t args, void* context) {
|
||||
(void)args;
|
||||
(void)context;
|
||||
cli_print_version();
|
||||
}
|
||||
|
||||
void cli_command_uuid(string_t args, void* context) {
|
||||
(void)args;
|
||||
(void)context;
|
||||
size_t uid_size = api_hal_uid_size();
|
||||
const uint8_t* uid = api_hal_uid();
|
||||
|
||||
string_t byte_str;
|
||||
string_init(byte_str);
|
||||
string_cat_printf(byte_str, "UID:");
|
||||
for(size_t i = 0; i < uid_size; i++) {
|
||||
uint8_t uid_byte = uid[i];
|
||||
string_cat_printf(byte_str, "%02X", uid_byte);
|
||||
}
|
||||
cli_print(string_get_cstr(byte_str));
|
||||
}
|
||||
|
||||
void cli_commands_init(Cli* cli) {
|
||||
cli_add_command(cli, "help", cli_command_help, cli);
|
||||
cli_add_command(cli, "?", cli_command_help, cli);
|
||||
cli_add_command(cli, "version", cli_command_version, cli);
|
||||
cli_add_command(cli, "!", cli_command_version, cli);
|
||||
cli_add_command(cli, "uid", cli_command_uuid, cli);
|
||||
}
|
5
applications/cli/cli_commands.h
Normal file
5
applications/cli/cli_commands.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "cli_i.h"
|
||||
|
||||
void cli_commands_init(Cli* cli);
|
42
applications/cli/cli_i.h
Normal file
42
applications/cli/cli_i.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include "cli.h"
|
||||
|
||||
#include <flipper.h>
|
||||
#include <flipper_v2.h>
|
||||
|
||||
#include <m-dict.h>
|
||||
|
||||
#define CLI_LINE_SIZE_MAX
|
||||
|
||||
typedef struct {
|
||||
CliCallback callback;
|
||||
void* context;
|
||||
} CliCommand;
|
||||
|
||||
DICT_DEF2(CliCommandDict, string_t, STRING_OPLIST, CliCommand, M_POD_OPLIST)
|
||||
|
||||
typedef enum {
|
||||
CliSymbolAsciiSOH = 0x01,
|
||||
CliSymbolAsciiEOT = 0x04,
|
||||
CliSymbolAsciiBell = 0x07,
|
||||
CliSymbolAsciiBackspace = 0x08,
|
||||
CliSymbolAsciiTab = 0x09,
|
||||
CliSymbolAsciiCR = 0x0D,
|
||||
CliSymbolAsciiEsc = 0x1B,
|
||||
CliSymbolAsciiUS = 0x1F,
|
||||
CliSymbolAsciiSpace = 0x20,
|
||||
CliSymbolAsciiDel = 0x7F,
|
||||
} CliSymbols;
|
||||
|
||||
struct Cli {
|
||||
CliCommandDict_t commands;
|
||||
osMutexId_t mutex;
|
||||
string_t line;
|
||||
};
|
||||
|
||||
Cli* cli_alloc();
|
||||
void cli_free(Cli* cli);
|
||||
void cli_reset_state(Cli* cli);
|
||||
void cli_print_version();
|
||||
void cli_putc(char c);
|
Reference in New Issue
Block a user