From b8b42d0bef495ebd24f5d34ccb6285c5cfb43612 Mon Sep 17 00:00:00 2001 From: Albert Kharisov Date: Wed, 16 Feb 2022 15:25:23 +0400 Subject: [PATCH] [FL-2260] IR CLI commands merge (#996) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: あく --- applications/irda/cli/irda_cli.cpp | 60 +++++++++++++++++------ applications/irda/helpers/irda_parser.cpp | 40 ++++++++------- lib/irda/encoder_decoder/irda.c | 8 +-- 3 files changed, 72 insertions(+), 36 deletions(-) diff --git a/applications/irda/cli/irda_cli.cpp b/applications/irda/cli/irda_cli.cpp index cdb44e8b..3a4121df 100644 --- a/applications/irda/cli/irda_cli.cpp +++ b/applications/irda/cli/irda_cli.cpp @@ -13,6 +13,17 @@ #include #include "../helpers/irda_parser.h" +static void irda_cli_start_ir_rx(Cli* cli, string_t args); +static void irda_cli_start_ir_tx(Cli* cli, string_t args); + +static const struct { + const char* cmd; + void (*process_function)(Cli* cli, string_t args); +} irda_cli_commands[] = { + {.cmd = "rx", .process_function = irda_cli_start_ir_rx}, + {.cmd = "tx", .process_function = irda_cli_start_ir_tx}, +}; + static void signal_received_callback(void* context, IrdaWorkerSignal* received_signal) { furi_assert(received_signal); char buf[100]; @@ -48,12 +59,7 @@ static void signal_received_callback(void* context, IrdaWorkerSignal* received_s } } -void irda_cli_start_ir_rx(Cli* cli, string_t args, void* context) { - if(furi_hal_irda_is_busy()) { - printf("IRDA is busy. Exit."); - return; - } - +static void irda_cli_start_ir_rx(Cli* cli, string_t args) { IrdaWorker* worker = irda_worker_alloc(); irda_worker_rx_start(worker); irda_worker_rx_set_received_signal_callback(worker, signal_received_callback, cli); @@ -68,7 +74,9 @@ void irda_cli_start_ir_rx(Cli* cli, string_t args, void* context) { } static void irda_cli_print_usage(void) { - printf("Usage:\r\n\tir_tx
\r\n"); + printf("Usage:\r\n"); + printf("\tir rx\r\n"); + printf("\tir tx
\r\n"); printf("\t and
are hex-formatted\r\n"); printf("\tAvailable protocols:"); for(int i = 0; irda_is_protocol_valid((IrdaProtocol)i); ++i) { @@ -131,12 +139,7 @@ static bool parse_signal_raw( return irda_parser_is_raw_signal_valid(*frequency, *duty_cycle, *timings_cnt); } -void irda_cli_start_ir_tx(Cli* cli, string_t args, void* context) { - if(furi_hal_irda_is_busy()) { - printf("IRDA is busy. Exit."); - return; - } - +static void irda_cli_start_ir_tx(Cli* cli, string_t args) { IrdaMessage message; const char* str = string_get_cstr(args); uint32_t frequency; @@ -156,11 +159,38 @@ void irda_cli_start_ir_tx(Cli* cli, string_t args, void* context) { free(timings); } +static void irda_cli_start_ir(Cli* cli, string_t args, void* context) { + if(furi_hal_irda_is_busy()) { + printf("IRDA is busy. Exit."); + return; + } + + size_t i = 0; + for(; i < COUNT_OF(irda_cli_commands); ++i) { + size_t size = strlen(irda_cli_commands[i].cmd); + bool cmd_found = !strncmp(string_get_cstr(args), irda_cli_commands[i].cmd, size); + if(cmd_found) { + if(string_size(args) == size) { + break; + } + if(string_get_cstr(args)[size] == ' ') { + string_right(args, size); + break; + } + } + } + + if(i < COUNT_OF(irda_cli_commands)) { + irda_cli_commands[i].process_function(cli, args); + } else { + irda_cli_print_usage(); + } +} + extern "C" void irda_on_system_start() { #ifdef SRV_CLI Cli* cli = (Cli*)furi_record_open("cli"); - cli_add_command(cli, "ir_rx", CliCommandFlagDefault, irda_cli_start_ir_rx, NULL); - cli_add_command(cli, "ir_tx", CliCommandFlagDefault, irda_cli_start_ir_tx, NULL); + cli_add_command(cli, "ir", CliCommandFlagDefault, irda_cli_start_ir, NULL); furi_record_close("cli"); #endif } diff --git a/applications/irda/helpers/irda_parser.cpp b/applications/irda/helpers/irda_parser.cpp index 3bfa4850..be0b17aa 100644 --- a/applications/irda/helpers/irda_parser.cpp +++ b/applications/irda/helpers/irda_parser.cpp @@ -96,26 +96,30 @@ bool irda_parser_is_parsed_signal_valid(const IrdaMessage* signal) { result = false; } - uint32_t address_length = irda_get_protocol_address_length(signal->protocol); - uint32_t address_mask = (1LU << address_length) - 1; - if(signal->address != (signal->address & address_mask)) { - FURI_LOG_E( - TAG, - "Address is out of range (mask 0x%08lX): 0x%lX\r\n", - address_mask, - signal->address); - result = false; + if(result) { + uint32_t address_length = irda_get_protocol_address_length(signal->protocol); + uint32_t address_mask = (1LU << address_length) - 1; + if(signal->address != (signal->address & address_mask)) { + FURI_LOG_E( + TAG, + "Address is out of range (mask 0x%08lX): 0x%lX\r\n", + address_mask, + signal->address); + result = false; + } } - uint32_t command_length = irda_get_protocol_command_length(signal->protocol); - uint32_t command_mask = (1LU << command_length) - 1; - if(signal->command != (signal->command & command_mask)) { - FURI_LOG_E( - TAG, - "Command is out of range (mask 0x%08lX): 0x%lX\r\n", - command_mask, - signal->command); - result = false; + if(result) { + uint32_t command_length = irda_get_protocol_command_length(signal->protocol); + uint32_t command_mask = (1LU << command_length) - 1; + if(signal->command != (signal->command & command_mask)) { + FURI_LOG_E( + TAG, + "Command is out of range (mask 0x%08lX): 0x%lX\r\n", + command_mask, + signal->command); + result = false; + } } return result; diff --git a/lib/irda/encoder_decoder/irda.c b/lib/irda/encoder_decoder/irda.c index f7cc2875..0fad0d89 100644 --- a/lib/irda/encoder_decoder/irda.c +++ b/lib/irda/encoder_decoder/irda.c @@ -268,9 +268,11 @@ IrdaProtocol irda_get_protocol_by_name(const char* protocol_name) { static const IrdaProtocolSpecification* irda_get_spec_by_protocol(IrdaProtocol protocol) { int index = irda_find_index_by_protocol(protocol); - furi_check(index >= 0); - const IrdaProtocolSpecification* spec = - irda_encoder_decoder[index].get_protocol_spec(protocol); + const IrdaProtocolSpecification* spec = NULL; + if(index >= 0) { + spec = irda_encoder_decoder[index].get_protocol_spec(protocol); + } + furi_assert(spec); return spec; }