flipperzero-firmware/applications/irda/irda-app-file-parser.cpp

319 lines
9.0 KiB
C++
Raw Normal View History

#include "irda-app-file-parser.h"
#include "furi/check.h"
#include "irda-app-remote-manager.h"
#include "irda-app-signal.h"
#include "m-string.h"
#include <text-store.h>
#include <irda.h>
#include <cstdio>
#include <stdint.h>
#include <string>
#include <string_view>
#include <furi.h>
#include <file-worker-cpp.h>
uint32_t const IrdaAppFileParser::max_line_length = ((9 + 1) * 512 + 100);
[FL-1191][FL-1524] Filesystem rework (#568) * FS-Api: removed datetime manipulation functions and most of the file flags * Filesystem: common proxy api * Filesystem: renamed to Storage. Work has begun on a glue layer. Added functions for reentrance. * Storage: sd mount and sd file open * Storage: sd file close * Storage: temporary test app * Storage: free filedata on close * Storage: sd file read and write * Storage: added internal storage (LittleFS) * Storage: renamed internal commands * Storage: seek, tell, truncate, size, sync, eof * Storage: error descriptions * Storage: directory management api (open, close, read, rewind) * Storage: common management api (stat, fs_stat, remove, rename, mkdir) * Dolphin app and Notifications app now use raw storage. * Storage: storage statuses renamed. Implemented sd card icon. * Storage: added raw sd-card api. * Storage settings: work started * Assets: use new icons approach * Storage settings: working storage settings * Storage: completely redesigned api, no longer sticking out FS_Api * Storage: more simplified api, getting error_id from file is hidden from user, pointer to api is hidden inside file * Storage: cli info and format commands * Storage-cli: file list * Storage: a simpler and more reliable api * FatFS: slightly lighter and faster config. Also disabled reentrancy and file locking functions. They moved to a storage service. * Storage-cli: accommodate to the new cli api. * Storage: filesystem api is separated into internal and common api. * Cli: added the ability to print the list of free heap blocks * Storage: uses a list instead of an array to store the StorageFile. Rewrote api calls to use semaphores instead of thread flags. * Storage settings: added the ability to benchmark the SD card. * Gui module file select: uses new storage api * Apps: removed deprecated sd_card_test application * Args lib: support for enquoted arguments * Dialogs: a new gui app for simple non-asynchronous apps * Dialogs: view holder for easy single view work * File worker: use new storage api * IButton and lfrrfid apps: save keys to any storage * Apps: fix ibutton and lfrfid stack, remove sd_card_test. * SD filesystem: app removed * File worker: fixed api pointer type * Subghz: loading assets using the new storage api * NFC: use the new storage api * Dialogs: the better api for the message element * Archive: use new storage api * Irda: changed assest path, changed app path * FileWorker: removed unused file_buf_cnt * Storage: copying and renaming files now works between storages * Storage cli: read, copy, remove, rename commands * Archive: removed commented code * Storage cli: write command * Applications: add SRV_STORAGE and SRV_DIALOGS * Internal-storage: removed * Storage: improved api * Storage app: changed api pointer from StorageApp to Storage * Storage: better file_id handling * Storage: more consistent errors * Loader: support for NULL icons * Storage: do nothing with the lfs file or directory if it is not open * Storage: fix typo * Storage: minor float usage cleanup, rename some symbols. * Storage: compact doxygen comments. Co-authored-by: あく <alleteam@gmail.com>
2021-07-23 12:20:19 +00:00
const char* IrdaAppFileParser::irda_directory = "/any/irda";
const char* IrdaAppFileParser::irda_extension = ".ir";
uint32_t const IrdaAppFileParser::max_raw_timings_in_signal = 512;
bool IrdaAppFileParser::open_irda_file_read(const char* name) {
std::string full_filename;
if(name[0] != '/')
full_filename = make_full_name(name);
else
full_filename = name;
return file_worker.open(full_filename.c_str(), FSAM_READ, FSOM_OPEN_EXISTING);
}
bool IrdaAppFileParser::open_irda_file_write(const char* name) {
std::string dirname(irda_directory);
auto full_filename = make_full_name(name);
if(!file_worker.mkdir(dirname.c_str())) return false;
return file_worker.open(full_filename.c_str(), FSAM_WRITE, FSOM_CREATE_ALWAYS);
}
size_t IrdaAppFileParser::stringify_message(
const IrdaAppSignal& signal,
const char* name,
char* buf,
size_t buf_size) {
auto message = signal.get_message();
auto protocol = message.protocol;
size_t written = 0;
written += sniprintf(
buf,
buf_size,
"%.31s %.31s A:%0*lX C:%0*lX\n",
name,
irda_get_protocol_name(protocol),
irda_get_protocol_address_length(protocol),
message.address,
irda_get_protocol_command_length(protocol),
message.command);
furi_assert(written < buf_size);
if(written >= buf_size) {
written = 0;
}
return written;
}
size_t IrdaAppFileParser::stringify_raw_signal(
const IrdaAppSignal& signal,
const char* name,
char* buf,
size_t buf_size) {
size_t written = 0;
int duty_cycle = 100 * IRDA_COMMON_DUTY_CYCLE;
written += sniprintf(
&buf[written],
max_line_length - written,
"%.31s RAW F:%d DC:%d",
name,
IRDA_COMMON_CARRIER_FREQUENCY,
duty_cycle);
auto& raw_signal = signal.get_raw_signal();
for(size_t i = 0; i < raw_signal.timings_cnt; ++i) {
written += sniprintf(&buf[written], buf_size - written, " %ld", raw_signal.timings[i]);
if(written > buf_size) {
return false;
}
}
written += snprintf(&buf[written], buf_size - written, "\n");
furi_assert(written < buf_size);
if(written >= buf_size) {
written = 0;
}
return written;
}
bool IrdaAppFileParser::save_signal(const IrdaAppSignal& signal, const char* name) {
char* buf = new char[max_line_length];
size_t buf_cnt = 0;
bool write_result = false;
if(signal.is_raw()) {
buf_cnt = stringify_raw_signal(signal, name, buf, max_line_length);
} else {
buf_cnt = stringify_message(signal, name, buf, max_line_length);
}
if(buf_cnt) {
write_result = file_worker.write(buf, buf_cnt);
}
delete[] buf;
return write_result;
}
std::unique_ptr<IrdaAppFileParser::IrdaFileSignal> IrdaAppFileParser::read_signal(void) {
string_t line;
string_init(line);
string_reserve(line, max_line_length);
std::unique_ptr<IrdaAppFileParser::IrdaFileSignal> file_signal;
while(!file_signal &&
file_worker.read_until_buffered(line, file_buf, &file_buf_cnt, sizeof(file_buf))) {
if(string_empty_p(line)) {
continue;
}
auto c_str = string_get_cstr(line);
file_signal = parse_signal(c_str);
if(!file_signal) {
file_signal = parse_signal_raw(c_str);
}
}
string_clear(line);
return file_signal;
}
std::unique_ptr<IrdaAppFileParser::IrdaFileSignal>
IrdaAppFileParser::parse_signal(const std::string& str) const {
char protocol_name[32];
uint32_t address;
uint32_t command;
auto irda_file_signal = std::make_unique<IrdaFileSignal>();
int parsed = std::sscanf(
str.c_str(),
"%31s %31s A:%lX C:%lX",
irda_file_signal->name,
protocol_name,
&address,
&command);
if(parsed != 4) {
return nullptr;
}
IrdaProtocol protocol = irda_get_protocol_by_name(protocol_name);
if(!irda_is_protocol_valid((IrdaProtocol)protocol)) {
return nullptr;
}
int address_length = irda_get_protocol_address_length(protocol);
uint32_t address_mask = (1LU << (4 * address_length)) - 1;
if(address != (address & address_mask)) {
return nullptr;
}
int command_length = irda_get_protocol_command_length(protocol);
uint32_t command_mask = (1LU << (4 * command_length)) - 1;
if(command != (command & command_mask)) {
return nullptr;
}
IrdaMessage message = {
.protocol = protocol,
.address = address,
.command = command,
.repeat = false,
};
irda_file_signal->signal.set_message(&message);
return irda_file_signal;
}
const char* find_first_not_of(const char* str, char symbol) {
const char* str_start = nullptr;
while(str != str_start) {
str_start = str;
str = strchr(str, symbol);
}
return str;
}
std::unique_ptr<IrdaAppFileParser::IrdaFileSignal>
IrdaAppFileParser::parse_signal_raw(const std::string& string) const {
uint32_t frequency;
uint32_t duty_cycle;
int str_len = string.size();
std::string_view str(string.c_str());
auto irda_file_signal = std::make_unique<IrdaFileSignal>();
int parsed = std::sscanf(
str.data(), "%31s RAW F:%ld DC:%ld", irda_file_signal->name, &frequency, &duty_cycle);
if((parsed != 3) || (frequency > 42000) || (frequency < 32000) || (duty_cycle == 0) ||
(duty_cycle >= 100)) {
return nullptr;
}
char dummy[100] = {0};
int header_len = 0;
header_len = sniprintf(
dummy,
sizeof(dummy),
"%.31s RAW F:%ld DC:%ld",
irda_file_signal->name,
frequency,
duty_cycle);
furi_assert(header_len < str_len);
str.remove_prefix(header_len);
/* move allocated timings into raw signal object */
IrdaAppSignal::RawSignal raw_signal = {
.timings_cnt = 0, .timings = new uint32_t[max_raw_timings_in_signal]};
bool result = false;
while(!str.empty()) {
char buf[10];
size_t index = str.find_first_not_of(' ', 1);
if(index == std::string_view::npos) {
break;
}
str.remove_prefix(index);
parsed = std::sscanf(str.data(), "%9s", buf);
if(parsed != 1) {
result = false;
furi_assert(0);
break;
}
str.remove_prefix(strlen(buf));
int value = atoi(buf);
if(value <= 0) {
result = false;
furi_assert(0);
break;
}
raw_signal.timings[raw_signal.timings_cnt] = value;
++raw_signal.timings_cnt;
result = true;
if(raw_signal.timings_cnt >= max_raw_timings_in_signal) {
result = false;
furi_assert(0);
break;
}
}
if(result) {
/* copy timings instead of moving them to occupy less than max_raw_timings_in_signal */
irda_file_signal->signal.copy_raw_signal(raw_signal.timings, raw_signal.timings_cnt);
} else {
(void)irda_file_signal.release();
}
delete[] raw_signal.timings;
return irda_file_signal;
}
bool IrdaAppFileParser::is_irda_file_exist(const char* name, bool* exist) {
std::string full_path = make_full_name(name);
return file_worker.is_file_exist(full_path.c_str(), exist);
}
std::string IrdaAppFileParser::make_full_name(const std::string& remote_name) const {
return std::string("") + irda_directory + "/" + remote_name + irda_extension;
}
std::string IrdaAppFileParser::make_name(const std::string& full_name) const {
std::string str(full_name, full_name.find_last_of('/') + 1, full_name.size());
str.erase(str.find_last_of('.'));
return str;
}
bool IrdaAppFileParser::remove_irda_file(const char* name) {
std::string full_filename = make_full_name(name);
return file_worker.remove(full_filename.c_str());
}
bool IrdaAppFileParser::rename_irda_file(const char* old_name, const char* new_name) {
std::string old_filename = make_full_name(old_name);
std::string new_filename = make_full_name(new_name);
return file_worker.rename(old_filename.c_str(), new_filename.c_str());
}
bool IrdaAppFileParser::close() {
return file_worker.close();
}
bool IrdaAppFileParser::check_errors() {
return file_worker.check_errors();
}
std::string IrdaAppFileParser::file_select(const char* selected) {
TextStore* filename_ts = new TextStore(128);
bool result;
result = file_worker.file_select(
irda_directory, irda_extension, filename_ts->text, filename_ts->text_size, selected);
delete filename_ts;
return result ? std::string(filename_ts->text) : std::string();
}