2022-01-10 17:13:41 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
#include "../infrared_app_signal.h"
|
|
|
|
#include "infrared.h"
|
|
|
|
#include "infrared/helpers/infrared_parser.h"
|
|
|
|
#include "infrared_worker.h"
|
2022-01-10 17:13:41 +00:00
|
|
|
#include "m-string.h"
|
2022-02-18 19:53:46 +00:00
|
|
|
#include <flipper_format/flipper_format.h>
|
2022-01-10 17:13:41 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2022-02-25 15:22:58 +00:00
|
|
|
#include <furi_hal_infrared.h>
|
2022-01-10 17:13:41 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
#define TAG "InfraredParser"
|
2022-01-10 17:13:41 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
bool infrared_parser_save_signal(
|
2022-02-18 19:53:46 +00:00
|
|
|
FlipperFormat* ff,
|
2022-02-25 15:22:58 +00:00
|
|
|
const InfraredAppSignal& signal,
|
2022-02-18 19:53:46 +00:00
|
|
|
const std::string& name) {
|
2022-01-10 17:13:41 +00:00
|
|
|
furi_assert(ff);
|
|
|
|
furi_assert(!name.empty());
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
do {
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_comment_cstr(ff, "")) break;
|
|
|
|
if(!flipper_format_write_string_cstr(ff, "name", name.c_str())) break;
|
2022-01-10 17:13:41 +00:00
|
|
|
if(signal.is_raw()) {
|
|
|
|
furi_assert(signal.get_raw_signal().timings_cnt <= MAX_TIMINGS_AMOUNT);
|
|
|
|
auto raw_signal = signal.get_raw_signal();
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_string_cstr(ff, "type", "raw")) break;
|
|
|
|
if(!flipper_format_write_uint32(ff, "frequency", &raw_signal.frequency, 1)) break;
|
|
|
|
if(!flipper_format_write_float(ff, "duty_cycle", &raw_signal.duty_cycle, 1)) break;
|
|
|
|
if(!flipper_format_write_uint32(ff, "data", raw_signal.timings, raw_signal.timings_cnt))
|
2022-01-10 17:13:41 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
auto parsed_signal = signal.get_message();
|
2022-02-25 15:22:58 +00:00
|
|
|
const char* protocol_name = infrared_get_protocol_name(parsed_signal.protocol);
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_string_cstr(ff, "type", "parsed")) break;
|
|
|
|
if(!flipper_format_write_string_cstr(ff, "protocol", protocol_name)) break;
|
|
|
|
if(!flipper_format_write_hex(ff, "address", (uint8_t*)&parsed_signal.address, 4))
|
|
|
|
break;
|
|
|
|
if(!flipper_format_write_hex(ff, "command", (uint8_t*)&parsed_signal.command, 4))
|
|
|
|
break;
|
2022-01-10 17:13:41 +00:00
|
|
|
}
|
|
|
|
result = true;
|
|
|
|
} while(0);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
bool infrared_parser_read_signal(FlipperFormat* ff, InfraredAppSignal& signal, std::string& name) {
|
2022-01-10 17:13:41 +00:00
|
|
|
furi_assert(ff);
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
string_t read_string;
|
|
|
|
string_init(read_string);
|
|
|
|
|
|
|
|
do {
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_string(ff, "name", read_string)) break;
|
2022-01-10 17:13:41 +00:00
|
|
|
name = string_get_cstr(read_string);
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_string(ff, "type", read_string)) break;
|
2022-01-10 17:13:41 +00:00
|
|
|
if(!string_cmp_str(read_string, "raw")) {
|
|
|
|
uint32_t* timings = nullptr;
|
|
|
|
uint32_t timings_cnt = 0;
|
|
|
|
uint32_t frequency = 0;
|
|
|
|
float duty_cycle = 0;
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_uint32(ff, "frequency", &frequency, 1)) break;
|
|
|
|
if(!flipper_format_read_float(ff, "duty_cycle", &duty_cycle, 1)) break;
|
|
|
|
if(!flipper_format_get_value_count(ff, "data", &timings_cnt)) break;
|
2022-01-10 17:13:41 +00:00
|
|
|
if(timings_cnt > MAX_TIMINGS_AMOUNT) break;
|
2022-02-18 19:53:46 +00:00
|
|
|
timings = (uint32_t*)malloc(sizeof(uint32_t) * timings_cnt);
|
|
|
|
if(flipper_format_read_uint32(ff, "data", timings, timings_cnt)) {
|
2022-01-10 17:13:41 +00:00
|
|
|
signal.set_raw_signal(timings, timings_cnt, frequency, duty_cycle);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
free(timings);
|
|
|
|
} else if(!string_cmp_str(read_string, "parsed")) {
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredMessage parsed_signal;
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_string(ff, "protocol", read_string)) break;
|
2022-02-25 15:22:58 +00:00
|
|
|
parsed_signal.protocol = infrared_get_protocol_by_name(string_get_cstr(read_string));
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_hex(ff, "address", (uint8_t*)&parsed_signal.address, 4)) break;
|
|
|
|
if(!flipper_format_read_hex(ff, "command", (uint8_t*)&parsed_signal.command, 4)) break;
|
2022-02-25 15:22:58 +00:00
|
|
|
if(!infrared_parser_is_parsed_signal_valid(&parsed_signal)) break;
|
2022-01-10 17:13:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
bool infrared_parser_is_parsed_signal_valid(const InfraredMessage* signal) {
|
2022-01-10 17:13:41 +00:00
|
|
|
furi_assert(signal);
|
|
|
|
bool result = true;
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
if(!infrared_is_protocol_valid(signal->protocol)) {
|
2022-01-10 17:13:41 +00:00
|
|
|
FURI_LOG_E(TAG, "Unknown protocol");
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
2022-02-16 11:25:23 +00:00
|
|
|
if(result) {
|
2022-02-25 15:22:58 +00:00
|
|
|
uint32_t address_length = infrared_get_protocol_address_length(signal->protocol);
|
2022-02-16 11:25:23 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-01-10 17:13:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 11:25:23 +00:00
|
|
|
if(result) {
|
2022-02-25 15:22:58 +00:00
|
|
|
uint32_t command_length = infrared_get_protocol_command_length(signal->protocol);
|
2022-02-16 11:25:23 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-01-10 17:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
bool infrared_parser_is_raw_signal_valid(
|
|
|
|
uint32_t frequency,
|
|
|
|
float duty_cycle,
|
|
|
|
uint32_t timings_cnt) {
|
2022-01-10 17:13:41 +00:00
|
|
|
bool result = true;
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
if((frequency > INFRARED_MAX_FREQUENCY) || (frequency < INFRARED_MIN_FREQUENCY)) {
|
2022-01-10 17:13:41 +00:00
|
|
|
FURI_LOG_E(
|
|
|
|
TAG,
|
|
|
|
"Frequency is out of range (%lX - %lX): %lX",
|
2022-02-25 15:22:58 +00:00
|
|
|
INFRARED_MIN_FREQUENCY,
|
|
|
|
INFRARED_MAX_FREQUENCY,
|
2022-01-10 17:13:41 +00:00
|
|
|
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;
|
|
|
|
}
|