1
mirror of https://github.com/DarkFlippers/unleashed-firmware.git synced 2025-12-12 04:34:43 +04:00
Files
unleashed-firmware/applications/main/nfc/cli/nfc_cli_commands.c
RebornedBrain eea53491de [FL-3569] NFC CLI commands (#4158)
* 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 completions

* more key combos

* commands in fals

* move commands out of flash

* ci: fix errors

* speedup cli file transfer

* merge fixups

* fix f18

* 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

* Now cli_shell can be customized with another motd and another command set

* Added custom motd callback definition

* Now user can alloc and free his own cli command set

* cli_vcp can now restart shell with another command set

* Help command modified to show available commands from different command sets

* Api adjustement

* Reworked nfc_cli to start new shell with another command set

* Revert custom shell changes from vcp

* Custom motd callback moved to cli_shell

* Cli Shell now can be started from ongoing cli command

* Help command moved to a separate function so it can be used for custom shell

* Now nfc command spawns separate shell for further nfc commands

* cli_shell: give up pipe to command thread

* fix formatting

* cli_shell: separate into toolbox

* speaker_debug: fix

* fix: format

* Merge branch 'portasynthinca3/3928-3929-cli-fals-threads' into portasynthinca3/3965-cli_shell-toolbox

* fix merge

* fix. merge.

* fix formatting

* fix: cmd flags

* fix: formatting

* Added basic command descriptor structs and macros

* Basic nfc commands definitions added

* Nfc cli commands collection and functions added

* Raw skeleton of nfc cli processor added

* cli: increase default stack depth

* New callbacks for ctx alloc / free added

* nfc_cli moved to cli folder

* Some more logic for command processor

* Scanner command no works via command_processor

* plugin manifest adj

* Argument descriptors were removed, now only keys left

* Some helper command function implemented

* Command processor logic now mostly works

* Added all parsers and dummy implementation of raw cmd

* Now processor checks duplicated keys and treat them as errors

* Some renamings

* Arguments processing moved to separate function

* Now command processor can reuse context of previuos command for the next one if it's allowed

* can_reuse callback added for checking if context can be reused

* command processor is now freed on nfc cli exit

* Some cleanups

* First working version of raw command

* Now input data are placed directly to bit buffer

* Added tag

* Introduced request/response structs

* Moved raw command to a separate folder

* Moved some common types to header

* Added protocol specific handlers for iso14a and felica

* Opened felica crc header for referencing

* Added handler for iso14443_3b

* Opened iso15693_3_poller for referencing

* Added iso15693_3 handler for raw command

* NfcCliRawError enum introduced for response result

* Refactored handlers implementation

* Formatting functions now added as helpers

* New printing result logic

* Not present error value added to enum

* Timeout added to raw command

* Command processor now supports multivalue keys

* Apdu command implementation added

* NfcScanner moved to helpers and command now uses it

* Helper now can format protocol names

* Dump command added

* Added some more functions to scanner helper

* Dump main logic simplified

* Dump handlers moved to protocols folder

* Protocol parser added to simplify searching protocol by name

* Protocol and key arguments added to dump command

* Cleanups

* Apdu now parses protocol using helper parser

* Raw now parses protocol using helper parser

* Wrong naming fix

* Emulate command added to cli

* Description added to action descriptor and command macros

* Description field added to all commands

* Removed unnecessary enum for commands

* Added functions for formatting command and action info

* Proper error messages and help added

* Fix for unsupported single action command

* Function renamed to more appropriate

* Field command moved to all other commands

* Cleanups

* Nfc commands modified with new cli shell

* Removed previous nfc_cli.c after merge

* Removed nfc_cli.h header

* Some renamings and cleanups

* Some comments and instructions added

* Some comments and instructions added

* TODOs removed

* Fix for missing parse callback

* Added not implemented dummy for mfu actions, for now

* Fix name mismatch

* Remove unneeded header

* Mfu command moved to separate folder, also raw info action logic added

* Dictionary with id/vendors added to assets. It is used by nfc_cli_mfu_info_get_vendor function

* One more unneeded header removed

* Moved mfu info action to a separate file

* Info action now uses sync mfu poller

* mfu rdbl action added

* wrbl action added for mfu command

* Some formatting for rdbl command

* Function for formatting mfu errors added

* All mfu actions now show errors in the same way

* Fix error with sync poller. Previously when read failed function returned ErrorNone, now it processes iso14a error to get proper value

* Make PVS happy

* Nfc cli now doesn't start if desktop app is running

* Make action description look more common

* Scanner now has -t key and can show detected protocol hierarchies

* Apdu now checks max input payload data

* Proper format

* Proper error handling added to dump command

* Timeout key added dump command

* Fix merge issue

* formatting

* Pragma pack replaced with FURI_PACKED

* Fix felica memory leak

---------

Co-authored-by: Anna Antonenko <portasynthinca3@gmail.com>
Co-authored-by: Georgii Surkov <georgii.surkov@outlook.com>
Co-authored-by: あく <alleteam@gmail.com>
Co-authored-by: hedger <hedger@users.noreply.github.com>
Co-authored-by: hedger <hedger@nanode.su>
2025-09-29 14:34:49 +04:00

162 lines
5.2 KiB
C

#include "nfc_cli_commands.h"
#include "nfc_cli_command_base_i.h"
/** Include new commands here */
#include "commands/raw/nfc_cli_command_raw.h"
#include "commands/apdu/nfc_cli_command_apdu.h"
#include "commands/dump/nfc_cli_command_dump.h"
#include "commands/mfu/nfc_cli_command_mfu.h"
#include "commands/nfc_cli_command_emulate.h"
#include "commands/nfc_cli_command_scanner.h"
#include "commands/nfc_cli_command_field.h"
#define TAG "NfcCliCommands"
/** Add new commands here */
static const NfcCliCommandDescriptor* nfc_cli_commands[] = {
&apdu_cmd,
&raw_cmd,
&emulate_cmd,
&mfu_cmd,
&scanner_cmd,
&dump_cmd,
&field_cmd,
};
size_t nfc_cli_command_get_count() {
return COUNT_OF(nfc_cli_commands);
}
const NfcCliActionDescriptor*
nfc_cli_command_get_action_by_name(const NfcCliCommandDescriptor* cmd, const FuriString* name) {
furi_assert(cmd);
furi_assert(name);
for(size_t i = 0; i < cmd->action_count; i++) {
const NfcCliActionDescriptor* action = cmd->actions[i];
if(furi_string_equal_str(name, action->name)) return action;
}
return NULL;
}
const NfcCliCommandDescriptor* nfc_cli_command_get_by_index(size_t index) {
furi_assert(index < COUNT_OF(nfc_cli_commands));
return nfc_cli_commands[index];
}
bool nfc_cli_command_has_multiple_actions(const NfcCliCommandDescriptor* cmd) {
furi_assert(cmd);
furi_check(cmd->action_count > 0);
return (cmd->action_count > 1);
}
const char* nfc_cli_command_get_name(const NfcCliCommandDescriptor* cmd) {
furi_assert(cmd);
return cmd->name;
}
CliCommandExecuteCallback nfc_cli_command_get_execute(const NfcCliCommandDescriptor* cmd) {
furi_assert(cmd);
return cmd->callback;
}
static inline const NfcCliKeyDescriptor* nfc_cli_action_get_key_by_name(
const NfcCliActionDescriptor* action,
const FuriString* name,
bool long_name) {
for(size_t i = 0; i < action->key_count; i++) {
const NfcCliKeyDescriptor* key = &action->keys[i];
const char* buf = long_name ? key->long_name : key->short_name;
if((buf != NULL) && furi_string_equal_str(name, buf)) return key;
}
return NULL;
}
const NfcCliKeyDescriptor*
nfc_cli_action_get_key_descriptor(const NfcCliActionDescriptor* action, FuriString* argument) {
furi_assert(action);
furi_assert(argument);
return nfc_cli_action_get_key_by_name(action, argument, furi_string_size(argument) > 1);
}
size_t nfc_cli_action_get_required_keys_count(const NfcCliActionDescriptor* action) {
furi_assert(action);
size_t required_key_count = 0;
for(size_t i = 0; i < action->key_count; i++) {
const NfcCliKeyDescriptor* key = &action->keys[i];
if(!key->features.required) continue;
required_key_count++;
}
return required_key_count;
}
static int nfc_cli_action_format_key_name(const NfcCliKeyDescriptor* key, FuriString* output) {
int len = 0;
FuriString* name = furi_string_alloc();
if(key->short_name && key->long_name) {
len = furi_string_printf(name, "-%s, --%s", key->short_name, key->long_name);
} else if(key->short_name && (key->long_name == NULL)) {
len = furi_string_printf(name, "-%s", key->short_name);
} else if((key->short_name == NULL) && key->long_name) {
len = furi_string_printf(name, "--%s", key->long_name);
}
const char* color = key->features.required ? ANSI_FLIPPER_BRAND_ORANGE : ANSI_RESET;
furi_string_printf(output, "%s%s%s", color, furi_string_get_cstr(name), ANSI_RESET);
furi_string_free(name);
return len;
}
void nfc_cli_action_format_info(const NfcCliActionDescriptor* action, FuriString* output) {
furi_assert(action);
furi_assert(output);
furi_string_printf(
output,
action->description ? "%s - %s\r\n\n" : "%s\r\n\n",
action->name,
action->description);
if(action->key_count == 0) return;
FuriString* buf = furi_string_alloc();
furi_string_cat_printf(
output,
ANSI_FG_BR_GREEN "Keys " ANSI_FLIPPER_BRAND_ORANGE "(required) " ANSI_RESET
"(optional):\r\n");
for(size_t i = 0; i < action->key_count; i++) {
const NfcCliKeyDescriptor* key = &action->keys[i];
int len = nfc_cli_action_format_key_name(key, buf);
furi_string_cat_printf(output, "%s", furi_string_get_cstr(buf));
if(key->description) {
const int offset = 20;
furi_string_cat_printf(
output, ANSI_CURSOR_RIGHT_BY("%d") "%s", offset - len, key->description);
}
furi_string_cat_printf(output, "\r\n");
}
furi_string_free(buf);
}
void nfc_cli_command_format_info(const NfcCliCommandDescriptor* cmd, FuriString* output) {
furi_assert(cmd);
furi_assert(output);
furi_string_printf(output, "%s - %s\r\n", cmd->name, cmd->description);
if(cmd->action_count > 1) {
furi_string_cat_printf(output, "Possible actions: \r\n");
for(size_t i = 0; i < cmd->action_count; i++) {
const NfcCliActionDescriptor* action = cmd->actions[i];
furi_string_cat_printf(
output,
action->description ? "\t%s\t-\t%s\r\n" : "%s\r\n",
action->name,
action->description);
}
}
}