From 7395caa7ce043b68dd761fc8904fa54cdc9b91a8 Mon Sep 17 00:00:00 2001 From: gornekich Date: Wed, 16 Feb 2022 14:15:40 +0300 Subject: [PATCH] [FL-2255], [FL-2259] NFC and Input CLI commands refactoring (#995) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * nfc: refactore cli commands * input: refactore cli commands Co-authored-by: あく --- applications/input/input.c | 90 +---------------------- applications/input/input_cli.c | 127 +++++++++++++++++++++++++++++++++ applications/input/input_i.h | 3 + applications/nfc/nfc_cli.c | 61 ++++++++++++---- applications/nfc/nfc_cli.h | 9 --- 5 files changed, 178 insertions(+), 112 deletions(-) create mode 100644 applications/input/input_cli.c delete mode 100644 applications/nfc/nfc_cli.h diff --git a/applications/input/input.c b/applications/input/input.c index 85f985be..55a10113 100644 --- a/applications/input/input.c +++ b/applications/input/input.c @@ -40,91 +40,6 @@ void input_isr(void* _ctx) { osThreadFlagsSet(input->thread, INPUT_THREAD_FLAG_ISR); } -#ifdef SRV_CLI -void input_cli_send(Cli* cli, string_t args, void* context) { - InputEvent event; - - // Get first word as key name - string_t key_name; - string_init(key_name); - size_t ws = string_search_char(args, ' '); - if(ws == STRING_FAILURE) { - printf("Invalid arguments. Use `input_send KEY TYPE`."); - string_clear(key_name); - return; - } else { - string_set_n(key_name, args, 0, ws); - string_right(args, ws); - string_strim(args); - } - // Check key name and set event key - if(!string_cmp(key_name, "up")) { - event.key = InputKeyUp; - } else if(!string_cmp(key_name, "down")) { - event.key = InputKeyDown; - } else if(!string_cmp(key_name, "left")) { - event.key = InputKeyLeft; - } else if(!string_cmp(key_name, "right")) { - event.key = InputKeyRight; - } else if(!string_cmp(key_name, "ok")) { - event.key = InputKeyOk; - } else if(!string_cmp(key_name, "back")) { - event.key = InputKeyBack; - } else { - printf("Invalid key name. Valid keys: `up`, `down`, `left`, `right`, `back`, `ok`."); - string_clear(key_name); - return; - } - string_clear(key_name); - // Check the rest of args string and set event type - if(!string_cmp(args, "press")) { - event.type = InputTypePress; - } else if(!string_cmp(args, "release")) { - event.type = InputTypeRelease; - } else if(!string_cmp(args, "short")) { - event.type = InputTypeShort; - } else if(!string_cmp(args, "long")) { - event.type = InputTypeLong; - } else { - printf("Ivalid type. Valid types: `press`, `release`, `short`, `long`."); - return; - } - // Publish input event - furi_pubsub_publish(input->event_pubsub, &event); -} - -static void input_cli_dump_events_callback(const void* value, void* ctx) { - furi_assert(value); - furi_assert(ctx); - osMessageQueueId_t input_queue = ctx; - osMessageQueuePut(input_queue, value, 0, osWaitForever); -} - -static void input_cli_dump(Cli* cli, string_t args, void* context) { - osMessageQueueId_t input_queue = osMessageQueueNew(8, sizeof(InputEvent), NULL); - FuriPubSubSubscription* input_subscription = - furi_pubsub_subscribe(input->event_pubsub, input_cli_dump_events_callback, input_queue); - - bool stop = false; - InputEvent input_event; - while(!stop) { - if(osMessageQueueGet(input_queue, &input_event, NULL, 100) == osOK) { - printf( - "key: %s type: %s\r\n", - input_get_key_name(input_event.key), - input_get_type_name(input_event.type)); - } - - if(cli_cmd_interrupt_received(cli)) { - stop = true; - } - } - - furi_pubsub_unsubscribe(input->event_pubsub, input_subscription); - osMessageQueueDelete(input_queue); -} -#endif - const char* input_get_key_name(InputKey key) { for(size_t i = 0; i < input_pins_count; i++) { if(input_pins[i].key == key) { @@ -159,10 +74,7 @@ int32_t input_srv() { #ifdef SRV_CLI input->cli = furi_record_open("cli"); if(input->cli) { - cli_add_command( - input->cli, "input_send", CliCommandFlagParallelSafe, input_cli_send, NULL); - cli_add_command( - input->cli, "input_dump", CliCommandFlagParallelSafe, input_cli_dump, NULL); + cli_add_command(input->cli, "input", CliCommandFlagParallelSafe, input_cli, input); } #endif diff --git a/applications/input/input_cli.c b/applications/input/input_cli.c new file mode 100644 index 00000000..c244ece5 --- /dev/null +++ b/applications/input/input_cli.c @@ -0,0 +1,127 @@ +#include "input_i.h" + +#include +#include +#include + +static void input_cli_usage() { + printf("Usage:\r\n"); + printf("input \r\n"); + printf("Cmd list:\r\n"); + printf("\tdump\t\t\t - dump input events\r\n"); + printf("\tsend \t - send input event\r\n"); +} + +static void input_cli_dump_events_callback(const void* value, void* ctx) { + furi_assert(value); + furi_assert(ctx); + osMessageQueueId_t input_queue = ctx; + osMessageQueuePut(input_queue, value, 0, osWaitForever); +} + +static void input_cli_dump(Cli* cli, string_t args, Input* input) { + osMessageQueueId_t input_queue = osMessageQueueNew(8, sizeof(InputEvent), NULL); + FuriPubSubSubscription* input_subscription = + furi_pubsub_subscribe(input->event_pubsub, input_cli_dump_events_callback, input_queue); + + bool stop = false; + InputEvent input_event; + while(!stop) { + if(osMessageQueueGet(input_queue, &input_event, NULL, 100) == osOK) { + printf( + "key: %s type: %s\r\n", + input_get_key_name(input_event.key), + input_get_type_name(input_event.type)); + } + + if(cli_cmd_interrupt_received(cli)) { + stop = true; + } + } + + furi_pubsub_unsubscribe(input->event_pubsub, input_subscription); + osMessageQueueDelete(input_queue); +} + +static void input_cli_send_print_usage() { + printf("Invalid arguments. Usage:\r\n"); + printf("\tinput send \r\n"); + printf("\t\t \t - one of 'up', 'down', 'left', 'right', 'back', 'ok'\r\n"); + printf("\t\t \t - one of 'press', 'release', 'short', 'long'\r\n"); +} + +void input_cli_send(Cli* cli, string_t args, Input* input) { + InputEvent event; + string_t key_str; + string_init(key_str); + bool parsed = false; + + do { + // Parse Key + if(!args_read_string_and_trim(args, key_str)) { + break; + } + if(!string_cmp(key_str, "up")) { + event.key = InputKeyUp; + } else if(!string_cmp(key_str, "down")) { + event.key = InputKeyDown; + } else if(!string_cmp(key_str, "left")) { + event.key = InputKeyLeft; + } else if(!string_cmp(key_str, "right")) { + event.key = InputKeyRight; + } else if(!string_cmp(key_str, "ok")) { + event.key = InputKeyOk; + } else if(!string_cmp(key_str, "back")) { + event.key = InputKeyBack; + } else { + break; + } + // Parse Type + if(!string_cmp(args, "press")) { + event.type = InputTypePress; + } else if(!string_cmp(args, "release")) { + event.type = InputTypeRelease; + } else if(!string_cmp(args, "short")) { + event.type = InputTypeShort; + } else if(!string_cmp(args, "long")) { + event.type = InputTypeLong; + } else { + break; + } + parsed = true; + } while(false); + + if(parsed) { + furi_pubsub_publish(input->event_pubsub, &event); + } else { + input_cli_send_print_usage(); + } + string_clear(key_str); +} + +void input_cli(Cli* cli, string_t args, void* context) { + furi_assert(cli); + furi_assert(context); + Input* input = context; + string_t cmd; + string_init(cmd); + + do { + if(!args_read_string_and_trim(args, cmd)) { + input_cli_usage(); + break; + } + if(string_cmp_str(cmd, "dump") == 0) { + input_cli_dump(cli, args, input); + break; + } + if(string_cmp_str(cmd, "send") == 0) { + input_cli_send(cli, args, input); + break; + } + + input_cli_usage(); + } while(false); + + string_clear(cmd); +} diff --git a/applications/input/input_i.h b/applications/input/input_i.h index 78c31165..4bd777ef 100644 --- a/applications/input/input_i.h +++ b/applications/input/input_i.h @@ -44,3 +44,6 @@ void input_press_timer_callback(void* arg); /** Input interrupt handler */ void input_isr(void* _ctx); + +/** Input CLI command handler */ +void input_cli(Cli* cli, string_t args, void* context); diff --git a/applications/nfc/nfc_cli.c b/applications/nfc/nfc_cli.c index 94367fcd..a2f816e4 100755 --- a/applications/nfc/nfc_cli.c +++ b/applications/nfc/nfc_cli.c @@ -1,21 +1,22 @@ -#include "nfc_cli.h" -#include "nfc_types.h" #include #include +#include +#include -void nfc_on_system_start() { -#ifdef SRV_CLI - Cli* cli = furi_record_open("cli"); - cli_add_command(cli, "nfc_detect", CliCommandFlagDefault, nfc_cli_detect, NULL); - cli_add_command(cli, "nfc_emulate", CliCommandFlagDefault, nfc_cli_emulate, NULL); - furi_record_close("cli"); -#endif +#include "nfc_types.h" + +static void nfc_cli_print_usage() { + printf("Usage:\r\n"); + printf("nfc \r\n"); + printf("Cmd list:\r\n"); + printf("\tdetect\t - detect nfc device\r\n"); + printf("\temulate\t - emulate predefined nfca card\r\n"); } -void nfc_cli_detect(Cli* cli, string_t args, void* context) { +void nfc_cli_detect(Cli* cli, string_t args) { // Check if nfc worker is not busy if(furi_hal_nfc_is_busy()) { - printf("Nfc is busy"); + printf("Nfc is busy\r\n"); return; } rfalNfcDevice* dev_list; @@ -45,15 +46,15 @@ void nfc_cli_detect(Cli* cli, string_t args, void* context) { furi_hal_nfc_deactivate(); } -void nfc_cli_emulate(Cli* cli, string_t args, void* context) { +void nfc_cli_emulate(Cli* cli, string_t args) { // Check if nfc worker is not busy if(furi_hal_nfc_is_busy()) { - printf("Nfc is busy"); + printf("Nfc is busy\r\n"); return; } furi_hal_nfc_exit_sleep(); - printf("Emulating NFC-A Type: T2T UID: CF72D440 SAK: 20 ATQA: 00/04\r\n"); + printf("Emulating NFC-A Type: T2T UID: 36 9C E7 B1 0A C1 34 SAK: 00 ATQA: 00/44\r\n"); printf("Press Ctrl+C to abort\r\n"); NfcDeviceCommonData params = { @@ -74,3 +75,35 @@ void nfc_cli_emulate(Cli* cli, string_t args, void* context) { } furi_hal_nfc_deactivate(); } + +static void nfc_cli(Cli* cli, string_t args, void* context) { + string_t cmd; + string_init(cmd); + + do { + if(!args_read_string_and_trim(args, cmd)) { + nfc_cli_print_usage(); + break; + } + if(string_cmp_str(cmd, "detect") == 0) { + nfc_cli_detect(cli, args); + break; + } + if(string_cmp_str(cmd, "emulate") == 0) { + nfc_cli_emulate(cli, args); + break; + } + + nfc_cli_print_usage(); + } while(false); + + string_clear(cmd); +} + +void nfc_on_system_start() { +#ifdef SRV_CLI + Cli* cli = furi_record_open("cli"); + cli_add_command(cli, "nfc", CliCommandFlagDefault, nfc_cli, NULL); + furi_record_close("cli"); +#endif +} diff --git a/applications/nfc/nfc_cli.h b/applications/nfc/nfc_cli.h deleted file mode 100644 index df722dff..00000000 --- a/applications/nfc/nfc_cli.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include - -void nfc_on_system_start(); - -void nfc_cli_detect(Cli* cli, string_t args, void* context); - -void nfc_cli_emulate(Cli* cli, string_t args, void* context);