2021-06-02 15:16:05 +00:00
|
|
|
#include "irda-app-remote-manager.hpp"
|
2021-06-09 13:04:49 +00:00
|
|
|
#include "filesystem-api.h"
|
2021-06-02 15:16:05 +00:00
|
|
|
#include "furi.h"
|
2021-06-09 13:04:49 +00:00
|
|
|
#include "furi/check.h"
|
|
|
|
#include "gui/modules/button_menu.h"
|
|
|
|
#include "irda.h"
|
|
|
|
#include <cstdio>
|
2021-06-02 15:16:05 +00:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2021-06-25 13:52:27 +00:00
|
|
|
#include "irda-app-file-parser.hpp"
|
2021-06-02 15:16:05 +00:00
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
const char* IrdaAppRemoteManager::irda_directory = "irda";
|
|
|
|
const char* IrdaAppRemoteManager::irda_extension = ".ir";
|
|
|
|
static const std::string default_remote_name = "remote";
|
|
|
|
|
|
|
|
static bool find_string(const std::vector<std::string>& strings, const std::string& match_string) {
|
|
|
|
for(const auto& string : strings) {
|
|
|
|
if(!string.compare(match_string)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string
|
2021-07-01 00:45:20 +00:00
|
|
|
find_vacant_name(const std::vector<std::string>& strings, const std::string& name) {
|
2021-06-09 13:04:49 +00:00
|
|
|
// if suggested name is occupied, try another one (name2, name3, etc)
|
|
|
|
if(find_string(strings, name)) {
|
|
|
|
int i = 1;
|
|
|
|
while(find_string(strings, name + std::to_string(++i)))
|
|
|
|
;
|
|
|
|
return name + std::to_string(i);
|
|
|
|
} else {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 16:43:54 +00:00
|
|
|
bool IrdaAppRemoteManager::add_button(const char* button_name, const IrdaAppSignal& signal) {
|
|
|
|
remote->buttons.emplace_back(button_name, signal);
|
2021-06-09 13:04:49 +00:00
|
|
|
return store();
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
bool IrdaAppRemoteManager::add_remote_with_button(
|
2021-06-02 15:16:05 +00:00
|
|
|
const char* button_name,
|
2021-07-16 16:43:54 +00:00
|
|
|
const IrdaAppSignal& signal) {
|
2021-06-09 13:04:49 +00:00
|
|
|
furi_check(button_name != nullptr);
|
2021-06-02 15:16:05 +00:00
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
std::vector<std::string> remote_list;
|
|
|
|
bool result = get_remote_list(remote_list);
|
|
|
|
if(!result) return false;
|
2021-06-02 15:16:05 +00:00
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
auto new_name = find_vacant_name(remote_list, default_remote_name);
|
|
|
|
|
|
|
|
remote = std::make_unique<IrdaAppRemote>(new_name);
|
2021-07-16 16:43:54 +00:00
|
|
|
return add_button(button_name, signal);
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> IrdaAppRemoteManager::get_button_list(void) const {
|
|
|
|
std::vector<std::string> name_vector;
|
2021-06-09 13:04:49 +00:00
|
|
|
name_vector.reserve(remote->buttons.size());
|
2021-06-02 15:16:05 +00:00
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
for(const auto& it : remote->buttons) {
|
2021-06-02 15:16:05 +00:00
|
|
|
name_vector.emplace_back(it.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy elision
|
|
|
|
return name_vector;
|
|
|
|
}
|
|
|
|
|
2021-07-16 16:43:54 +00:00
|
|
|
const IrdaAppSignal& IrdaAppRemoteManager::get_button_data(size_t index) const {
|
2021-06-09 13:04:49 +00:00
|
|
|
furi_check(remote.get() != nullptr);
|
|
|
|
auto& buttons = remote->buttons;
|
|
|
|
furi_check(index < buttons.size());
|
|
|
|
|
2021-07-16 16:43:54 +00:00
|
|
|
return buttons.at(index).signal;
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 16:50:18 +00:00
|
|
|
std::string IrdaAppRemoteManager::make_full_name(const std::string& remote_name) const {
|
|
|
|
return std::string("/") + irda_directory + "/" + remote_name + irda_extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string IrdaAppRemoteManager::make_remote_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;
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IrdaAppRemoteManager::delete_remote() {
|
|
|
|
FS_Error fs_res;
|
2021-06-25 13:52:27 +00:00
|
|
|
IrdaAppFileParser file_parser;
|
2021-06-02 15:16:05 +00:00
|
|
|
|
2021-07-16 16:50:18 +00:00
|
|
|
fs_res = file_parser.get_fs_api().common.remove(make_full_name(remote->name).c_str());
|
2021-06-09 13:04:49 +00:00
|
|
|
if(fs_res != FSE_OK) {
|
2021-06-25 13:52:27 +00:00
|
|
|
file_parser.get_sd_api().show_error(
|
|
|
|
file_parser.get_sd_api().context, "Error deleting file");
|
2021-06-09 13:04:49 +00:00
|
|
|
return false;
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
2021-06-09 13:04:49 +00:00
|
|
|
remote.reset();
|
|
|
|
return true;
|
|
|
|
}
|
2021-06-02 15:16:05 +00:00
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
bool IrdaAppRemoteManager::delete_button(uint32_t index) {
|
|
|
|
furi_check(remote.get() != nullptr);
|
|
|
|
auto& buttons = remote->buttons;
|
|
|
|
furi_check(index < buttons.size());
|
|
|
|
|
|
|
|
buttons.erase(buttons.begin() + index);
|
|
|
|
return store();
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
std::string IrdaAppRemoteManager::get_button_name(uint32_t index) {
|
|
|
|
furi_check(remote.get() != nullptr);
|
|
|
|
auto& buttons = remote->buttons;
|
|
|
|
furi_check(index < buttons.size());
|
|
|
|
return buttons[index].name;
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
std::string IrdaAppRemoteManager::get_remote_name() {
|
|
|
|
furi_check(remote.get() != nullptr);
|
|
|
|
return remote->name;
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
int IrdaAppRemoteManager::find_remote_name(const std::vector<std::string>& strings) {
|
|
|
|
int i = 0;
|
|
|
|
for(const auto& str : strings) {
|
|
|
|
if(!str.compare(remote->name)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return -1;
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
bool IrdaAppRemoteManager::rename_remote(const char* str) {
|
|
|
|
furi_check(str != nullptr);
|
|
|
|
furi_check(remote.get() != nullptr);
|
|
|
|
|
|
|
|
if(!remote->name.compare(str)) return true;
|
|
|
|
|
|
|
|
std::vector<std::string> remote_list;
|
|
|
|
bool result = get_remote_list(remote_list);
|
|
|
|
if(!result) return false;
|
|
|
|
|
|
|
|
auto new_name = find_vacant_name(remote_list, str);
|
2021-06-25 13:52:27 +00:00
|
|
|
IrdaAppFileParser file_parser;
|
|
|
|
FS_Error fs_err = file_parser.get_fs_api().common.rename(
|
2021-07-16 16:50:18 +00:00
|
|
|
make_full_name(remote->name).c_str(), make_full_name(new_name).c_str());
|
2021-06-09 13:04:49 +00:00
|
|
|
remote->name = new_name;
|
|
|
|
if(fs_err != FSE_OK) {
|
2021-06-25 13:52:27 +00:00
|
|
|
file_parser.get_sd_api().show_error(
|
|
|
|
file_parser.get_sd_api().context, "Error renaming\nremote file");
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
return fs_err == FSE_OK;
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
bool IrdaAppRemoteManager::rename_button(uint32_t index, const char* str) {
|
|
|
|
furi_check(remote.get() != nullptr);
|
|
|
|
auto& buttons = remote->buttons;
|
|
|
|
furi_check(index < buttons.size());
|
|
|
|
|
|
|
|
buttons[index].name = str;
|
|
|
|
return store();
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
size_t IrdaAppRemoteManager::get_number_of_buttons() {
|
|
|
|
furi_check(remote.get() != nullptr);
|
|
|
|
return remote->buttons.size();
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
bool IrdaAppRemoteManager::store(void) {
|
|
|
|
File file;
|
|
|
|
std::string dirname(std::string("/") + irda_directory);
|
|
|
|
|
2021-06-25 13:52:27 +00:00
|
|
|
IrdaAppFileParser file_parser;
|
|
|
|
FS_Error fs_err = file_parser.get_fs_api().common.mkdir(dirname.c_str());
|
2021-06-09 13:04:49 +00:00
|
|
|
if((fs_err != FSE_OK) && (fs_err != FSE_EXIST)) {
|
2021-06-25 13:52:27 +00:00
|
|
|
file_parser.get_sd_api().show_error(
|
|
|
|
file_parser.get_sd_api().context, "Can't create directory");
|
2021-06-09 13:04:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-25 13:52:27 +00:00
|
|
|
bool res = file_parser.get_fs_api().file.open(
|
2021-07-16 16:50:18 +00:00
|
|
|
&file, make_full_name(remote->name).c_str(), FSAM_WRITE, FSOM_CREATE_ALWAYS);
|
2021-06-09 13:04:49 +00:00
|
|
|
|
|
|
|
if(!res) {
|
2021-06-25 13:52:27 +00:00
|
|
|
file_parser.get_sd_api().show_error(
|
|
|
|
file_parser.get_sd_api().context, "Cannot create\nnew remote file");
|
2021-06-09 13:04:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const auto& button : remote->buttons) {
|
2021-07-16 16:43:54 +00:00
|
|
|
bool result = file_parser.store_signal(&file, button.signal, button.name.c_str());
|
|
|
|
if(!result) {
|
2021-06-25 13:52:27 +00:00
|
|
|
file_parser.get_sd_api().show_error(
|
|
|
|
file_parser.get_sd_api().context, "Cannot write\nto key file");
|
|
|
|
file_parser.get_fs_api().file.close(&file);
|
2021-06-09 13:04:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 13:52:27 +00:00
|
|
|
file_parser.get_fs_api().file.close(&file);
|
|
|
|
file_parser.get_sd_api().check_error(file_parser.get_sd_api().context);
|
2021-06-09 13:04:49 +00:00
|
|
|
|
|
|
|
return true;
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
bool IrdaAppRemoteManager::get_remote_list(std::vector<std::string>& remote_names) const {
|
|
|
|
bool fs_res = false;
|
|
|
|
char name[128];
|
|
|
|
File dir;
|
|
|
|
std::string dirname(std::string("/") + irda_directory);
|
|
|
|
remote_names.clear();
|
|
|
|
|
2021-06-25 13:52:27 +00:00
|
|
|
IrdaAppFileParser file_parser;
|
|
|
|
fs_res = file_parser.get_fs_api().dir.open(&dir, dirname.c_str());
|
2021-06-09 13:04:49 +00:00
|
|
|
if(!fs_res) {
|
|
|
|
if(!check_fs()) {
|
2021-06-25 13:52:27 +00:00
|
|
|
file_parser.get_sd_api().show_error(
|
|
|
|
file_parser.get_sd_api().context, "Cannot open\napplication directory");
|
2021-06-09 13:04:49 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true; // SD ok, but no files written yet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 13:52:27 +00:00
|
|
|
while(file_parser.get_fs_api().dir.read(&dir, nullptr, name, sizeof(name)) && strlen(name)) {
|
2021-06-09 13:04:49 +00:00
|
|
|
std::string filename(name);
|
|
|
|
auto extension_index = filename.rfind(irda_extension);
|
|
|
|
if((extension_index == std::string::npos) ||
|
|
|
|
(extension_index + strlen(irda_extension) != filename.size())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
remote_names.push_back(filename.erase(extension_index));
|
|
|
|
}
|
2021-06-25 13:52:27 +00:00
|
|
|
file_parser.get_fs_api().dir.close(&dir);
|
2021-06-09 13:04:49 +00:00
|
|
|
|
|
|
|
return true;
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 16:50:18 +00:00
|
|
|
bool IrdaAppRemoteManager::load(const std::string& name_arg, bool fullpath) {
|
2021-06-09 13:04:49 +00:00
|
|
|
bool fs_res = false;
|
2021-06-25 13:52:27 +00:00
|
|
|
IrdaAppFileParser file_parser;
|
2021-06-09 13:04:49 +00:00
|
|
|
File file;
|
2021-07-16 16:50:18 +00:00
|
|
|
std::string full_filename;
|
|
|
|
std::string remote_name;
|
|
|
|
|
|
|
|
if(fullpath) {
|
|
|
|
full_filename = name_arg;
|
|
|
|
remote_name = make_remote_name(name_arg);
|
|
|
|
} else {
|
|
|
|
full_filename = make_full_name(name_arg);
|
|
|
|
remote_name = name_arg;
|
|
|
|
}
|
2021-06-09 13:04:49 +00:00
|
|
|
|
2021-06-25 13:52:27 +00:00
|
|
|
fs_res = file_parser.get_fs_api().file.open(
|
2021-07-16 16:50:18 +00:00
|
|
|
&file, full_filename.c_str(), FSAM_READ, FSOM_OPEN_EXISTING);
|
2021-06-09 13:04:49 +00:00
|
|
|
if(!fs_res) {
|
2021-06-25 13:52:27 +00:00
|
|
|
file_parser.get_sd_api().show_error(
|
|
|
|
file_parser.get_sd_api().context, "Error opening file");
|
2021-06-09 13:04:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-16 16:50:18 +00:00
|
|
|
remote = std::make_unique<IrdaAppRemote>(remote_name);
|
2021-06-09 13:04:49 +00:00
|
|
|
|
|
|
|
while(1) {
|
2021-07-16 16:43:54 +00:00
|
|
|
auto file_signal = file_parser.read_signal(&file);
|
|
|
|
if(!file_signal.get()) break;
|
|
|
|
remote->buttons.emplace_back(file_signal->name, file_signal->signal);
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
2021-06-25 13:52:27 +00:00
|
|
|
file_parser.get_fs_api().file.close(&file);
|
2021-06-09 13:04:49 +00:00
|
|
|
|
|
|
|
return true;
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
bool IrdaAppRemoteManager::check_fs() const {
|
2021-06-25 13:52:27 +00:00
|
|
|
// TODO: [FL-1431] Add return value to file_parser.get_sd_api().check_error() and replace get_fs_info().
|
|
|
|
IrdaAppFileParser file_parser;
|
|
|
|
auto fs_err = file_parser.get_fs_api().common.get_fs_info(nullptr, nullptr);
|
|
|
|
if(fs_err != FSE_OK)
|
|
|
|
file_parser.get_sd_api().show_error(file_parser.get_sd_api().context, "SD card not found");
|
2021-06-09 13:04:49 +00:00
|
|
|
return fs_err == FSE_OK;
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|