Skip to content

Commit

Permalink
[FL-1929, FL-2164] IR App migrate to FFF (flipperdevices#949)
Browse files Browse the repository at this point in the history
* IR app move to FFF
* [FL-2164] Hide unimplemented submenus
* Fix brute force fail
* Fix FFF endless reading
* Reformat TV bruteforce lib to FFF
* fixes & cleanup
* Infrared: switch to constexpr.

Co-authored-by: Aleksandr Kutuzov <[email protected]>
  • Loading branch information
albkharisov and skotopes authored Jan 10, 2022
1 parent 389ff92 commit 990a065
Show file tree
Hide file tree
Showing 22 changed files with 1,979 additions and 916 deletions.
53 changes: 7 additions & 46 deletions applications/irda/cli/irda_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <m-string.h>
#include <irda_transmit.h>
#include <sys/types.h>
#include "../helpers/irda_parser.h"

static void signal_received_callback(void* context, IrdaWorkerSignal* received_signal) {
furi_assert(received_signal);
Expand Down Expand Up @@ -83,41 +84,17 @@ static void irda_cli_print_usage(void) {
}

static bool parse_message(const char* str, IrdaMessage* message) {
uint32_t command = 0;
uint32_t address = 0;
char protocol_name[32];
int parsed = sscanf(str, "%31s %lX %lX", protocol_name, &address, &command);
int parsed = sscanf(str, "%31s %lX %lX", protocol_name, &message->address, &message->command);

if(parsed != 3) {
return false;
}

IrdaProtocol protocol = irda_get_protocol_by_name(protocol_name);

if(!irda_is_protocol_valid(protocol)) {
return false;
}

uint32_t address_length = irda_get_protocol_address_length(protocol);
uint32_t address_mask = (1LU << address_length) - 1;
if(address != (address & address_mask)) {
printf("Address out of range (mask 0x%08lX): 0x%lX\r\n", address_mask, address);
return false;
}

uint32_t command_length = irda_get_protocol_command_length(protocol);
uint32_t command_mask = (1LU << command_length) - 1;
if(command != (command & command_mask)) {
printf("Command out of range (mask 0x%08lX): 0x%lX\r\n", command_mask, command);
return false;
}

message->protocol = protocol;
message->address = address;
message->command = command;
message->protocol = irda_get_protocol_by_name(protocol_name);
message->repeat = false;

return true;
return irda_parser_is_parsed_signal_valid(message);
}

static bool parse_signal_raw(
Expand All @@ -135,14 +112,6 @@ static bool parse_signal_raw(
*duty_cycle = (float)atoi(duty_cycle_str) / 100;
str += strlen(frequency_str) + strlen(duty_cycle_str) + 10;

if((*frequency > IRDA_MAX_FREQUENCY) || (*frequency < IRDA_MIN_FREQUENCY)) {
return false;
}

if((*duty_cycle <= 0) || (*duty_cycle > 1)) {
return false;
}

uint32_t timings_cnt_max = *timings_cnt;
*timings_cnt = 0;

Expand All @@ -159,15 +128,7 @@ static bool parse_signal_raw(
++*timings_cnt;
}

if(*timings_cnt > 0) {
printf("\r\nTransmit:");
for(size_t i = 0; i < *timings_cnt; ++i) {
printf(" %ld", timings[i]);
}
printf("\r\n");
}

return (parsed == 2) && (*timings_cnt > 0);
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) {
Expand All @@ -180,8 +141,8 @@ void irda_cli_start_ir_tx(Cli* cli, string_t args, void* context) {
const char* str = string_get_cstr(args);
uint32_t frequency;
float duty_cycle;
uint32_t* timings = (uint32_t*)furi_alloc(sizeof(uint32_t) * 512);
uint32_t timings_cnt = 512;
uint32_t timings_cnt = MAX_TIMINGS_AMOUNT;
uint32_t* timings = (uint32_t*)furi_alloc(sizeof(uint32_t) * timings_cnt);

if(parse_message(str, &message)) {
irda_send(&message, 1);
Expand Down
145 changes: 145 additions & 0 deletions applications/irda/helpers/irda_parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@

#include "../irda_app_signal.h"
#include "irda.h"
#include "irda/helpers/irda_parser.h"
#include "irda_worker.h"
#include "m-string.h"
#include <flipper_file.h>
#include <memory>
#include <string>
#include <furi_hal_irda.h>

#define TAG "IrdaParser"

bool irda_parser_save_signal(FlipperFile* ff, const IrdaAppSignal& signal, const std::string& name) {
furi_assert(ff);
furi_assert(!name.empty());

bool result = false;

do {
if(!flipper_file_write_comment_cstr(ff, "")) break;
if(!flipper_file_write_string_cstr(ff, "name", name.c_str())) break;
if(signal.is_raw()) {
furi_assert(signal.get_raw_signal().timings_cnt <= MAX_TIMINGS_AMOUNT);
auto raw_signal = signal.get_raw_signal();
if(!flipper_file_write_string_cstr(ff, "type", "raw")) break;
if(!flipper_file_write_uint32(ff, "frequency", &raw_signal.frequency, 1)) break;
if(!flipper_file_write_float(ff, "duty_cycle", &raw_signal.duty_cycle, 1)) break;
if(!flipper_file_write_uint32(ff, "data", raw_signal.timings, raw_signal.timings_cnt))
break;
} else {
auto parsed_signal = signal.get_message();
const char* protocol_name = irda_get_protocol_name(parsed_signal.protocol);
if(!flipper_file_write_string_cstr(ff, "type", "parsed")) break;
if(!flipper_file_write_string_cstr(ff, "protocol", protocol_name)) break;
if(!flipper_file_write_hex(ff, "address", (uint8_t*)&parsed_signal.address, 4)) break;
if(!flipper_file_write_hex(ff, "command", (uint8_t*)&parsed_signal.command, 4)) break;
}
result = true;
} while(0);

return result;
}

bool irda_parser_read_signal(FlipperFile* ff, IrdaAppSignal& signal, std::string& name) {
furi_assert(ff);

bool result = false;
string_t read_string;
string_init(read_string);

do {
if(!flipper_file_read_string(ff, "name", read_string)) break;
name = string_get_cstr(read_string);
if(!flipper_file_read_string(ff, "type", read_string)) break;
if(!string_cmp_str(read_string, "raw")) {
uint32_t* timings = nullptr;
uint32_t timings_cnt = 0;
uint32_t frequency = 0;
float duty_cycle = 0;

if(!flipper_file_read_uint32(ff, "frequency", &frequency, 1)) break;
if(!flipper_file_read_float(ff, "duty_cycle", &duty_cycle, 1)) break;
if(!flipper_file_get_value_count(ff, "data", &timings_cnt)) break;
if(timings_cnt > MAX_TIMINGS_AMOUNT) break;
timings = (uint32_t*)furi_alloc(sizeof(uint32_t) * timings_cnt);
if(flipper_file_read_uint32(ff, "data", timings, timings_cnt)) {
signal.set_raw_signal(timings, timings_cnt, frequency, duty_cycle);
result = true;
}
free(timings);
} else if(!string_cmp_str(read_string, "parsed")) {
IrdaMessage parsed_signal;
if(!flipper_file_read_string(ff, "protocol", read_string)) break;
parsed_signal.protocol = irda_get_protocol_by_name(string_get_cstr(read_string));
if(!flipper_file_read_hex(ff, "address", (uint8_t*)&parsed_signal.address, 4)) break;
if(!flipper_file_read_hex(ff, "command", (uint8_t*)&parsed_signal.command, 4)) break;
if(!irda_parser_is_parsed_signal_valid(&parsed_signal)) break;
signal.set_message(&parsed_signal);
result = true;
} else {
FURI_LOG_E(TAG, "Unknown type of signal (allowed - raw/parsed) ");
}
} while(0);

string_clear(read_string);
return result;
}

bool irda_parser_is_parsed_signal_valid(const IrdaMessage* signal) {
furi_assert(signal);
bool result = true;

if(!irda_is_protocol_valid(signal->protocol)) {
FURI_LOG_E(TAG, "Unknown protocol");
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;
}

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;
}

bool irda_parser_is_raw_signal_valid(uint32_t frequency, float duty_cycle, uint32_t timings_cnt) {
bool result = true;

if((frequency > IRDA_MAX_FREQUENCY) || (frequency < IRDA_MIN_FREQUENCY)) {
FURI_LOG_E(
TAG,
"Frequency is out of range (%lX - %lX): %lX",
IRDA_MIN_FREQUENCY,
IRDA_MAX_FREQUENCY,
frequency);
result = false;
} else if((duty_cycle <= 0) || (duty_cycle > 1)) {
FURI_LOG_E(TAG, "Duty cycle is out of range (0 - 1): %f", duty_cycle);
result = false;
} else if((timings_cnt <= 0) || (timings_cnt > MAX_TIMINGS_AMOUNT)) {
FURI_LOG_E(
TAG, "Timings amount is out of range (0 - %lX): %lX", MAX_TIMINGS_AMOUNT, timings_cnt);
result = false;
}

return result;
}
10 changes: 10 additions & 0 deletions applications/irda/helpers/irda_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include "../irda_app_signal.h"
#include <flipper_file.h>
#include <string>

bool irda_parser_save_signal(FlipperFile* ff, const IrdaAppSignal& signal, const std::string& name);
bool irda_parser_read_signal(FlipperFile* ff, IrdaAppSignal& signal, std::string& name);
bool irda_parser_is_parsed_signal_valid(const IrdaMessage* signal);
bool irda_parser_is_raw_signal_valid(uint32_t frequency, float duty_cycle, uint32_t timings_cnt);
9 changes: 3 additions & 6 deletions applications/irda/irda_app.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "irda_app.h"
#include "irda/irda_app_file_parser.h"
#include <irda_worker.h>
#include <furi.h>
#include <gui/gui.h>
Expand All @@ -13,11 +12,9 @@ int32_t IrdaApp::run(void* args) {
bool exit = false;

if(args) {
std::string remote_name;
{
IrdaAppFileParser file_parser;
remote_name = file_parser.make_name(static_cast<const char*>(args));
}
std::string full_name = static_cast<const char*>(args);
std::string remote_name(full_name, full_name.find_last_of('/') + 1, full_name.size());
remote_name.erase(remote_name.find_last_of('.'));
bool result = remote_manager.load(remote_name);
if(result) {
current_scene = IrdaApp::Scene::Remote;
Expand Down
10 changes: 8 additions & 2 deletions applications/irda/irda_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,15 @@ class IrdaApp {
IrdaApp();
~IrdaApp();

static constexpr const char* irda_directory = "/any/irda";
static constexpr const char* irda_extension = ".ir";
static constexpr const uint32_t max_raw_timings_in_signal = 512;
static constexpr const uint32_t max_line_length =
(9 + 1) * IrdaApp::max_raw_timings_in_signal + 100;

private:
static inline const uint8_t text_store_size = 128;
static inline const uint8_t text_store_max = 2;
static constexpr const uint8_t text_store_size = 128;
static constexpr const uint8_t text_store_max = 2;
char text_store[text_store_max][text_store_size + 1];
bool learn_new_remote;
EditElement element;
Expand Down
Loading

0 comments on commit 990a065

Please sign in to comment.