[FL-1307] iButton key files: ASCII encoding and .ibtn extension (#493)

* GUI module submenu: fix documentation
* GUI module submenu: add submenu_set_selected_item fn
* App iButton: use submenu_set_selected_item to store and set selected item in submenu
* App iButton: swap write and emulate in "saved key menu" scene
* App iButton: file select can now switch to the previous selected file
* App iButton: swap write and emulate indexes in "saved key menu" scene
* Gui module file_select: work with separate extension
* iButton app: separate file managment, file error handling
* SD card api: custom error message
* iButton app: better file error handling

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
SG
2021-05-27 02:50:16 +10:00
committed by GitHub
parent 63e9207c44
commit 7e6a97c3a3
11 changed files with 297 additions and 101 deletions

View File

@@ -55,18 +55,8 @@ bool iButtonSceneDeleteConfirm::on_event(iButtonApp* app, iButtonEvent* event) {
if(event->type == iButtonEvent::Type::EventTypeDialogResult) {
if(event->payload.dialog_result == DialogExResultRight) {
iButtonKey* key = app->get_key();
string_t key_file_name;
string_init_set_str(key_file_name, "ibutton/");
string_cat_str(key_file_name, key->get_name());
bool res =
(app->get_fs_api()->common.remove(string_get_cstr(key_file_name)) == FSE_OK);
string_clear(key_file_name);
if(res) {
if(app->delete_key()) {
app->switch_to_next_scene(iButtonApp::Scene::SceneDeleteSuccess);
} else {
// TODO error file path
// app->switch_to_next_scene(iButtonApp::Scene::SceneDeleteFail);
}
} else {
app->switch_to_previous_scene();

View File

@@ -31,40 +31,14 @@ bool iButtonSceneSaveName::on_event(iButtonApp* app, iButtonEvent* event) {
bool consumed = false;
if(event->type == iButtonEvent::Type::EventTypeTextEditResult) {
iButtonKey* key = app->get_key();
File key_file;
string_t key_file_name;
// Create ibutton directory if necessary
app->get_fs_api()->common.mkdir("ibutton");
// First remove key if it was saved
string_init_set_str(key_file_name, "ibutton/");
string_cat_str(key_file_name, key->get_name());
app->get_fs_api()->common.remove(string_get_cstr(key_file_name));
// Save the key
key->set_name(app->get_text_store());
string_set_str(key_file_name, "ibutton/");
string_cat_str(key_file_name, app->get_text_store());
uint8_t key_data[IBUTTON_KEY_DATA_SIZE + 1];
key_data[0] = static_cast<uint8_t>(key->get_key_type());
memcpy(key_data + 1, key->get_data(), IBUTTON_KEY_DATA_SIZE);
bool res = app->get_fs_api()->file.open(
&key_file, string_get_cstr(key_file_name), FSAM_WRITE, FSOM_CREATE_ALWAYS);
// TODO process file system errors from file system service
if(res) {
res = app->get_fs_api()->file.write(&key_file, key_data, IBUTTON_KEY_DATA_SIZE + 1);
res = app->get_fs_api()->file.close(&key_file);
if(app->save_key(app->get_text_store())) {
app->switch_to_next_scene(iButtonApp::Scene::SceneSaveSuccess);
} else {
app->get_sd_ex_api()->check_error(app->get_sd_ex_api()->context);
app->search_and_switch_to_previous_scene(
{iButtonApp::Scene::SceneReadedKeyMenu,
iButtonApp::Scene::SceneSavedKeyMenu,
iButtonApp::Scene::SceneAddType});
}
string_clear(key_file_name);
consumed = true;
}

View File

@@ -4,40 +4,8 @@
#include "../ibutton-key.h"
void iButtonSceneSelectKey::on_enter(iButtonApp* app) {
// Input events and views are managed by file_select
bool res = app->get_sd_ex_api()->file_select(
app->get_sd_ex_api()->context,
"ibutton",
"*",
app->get_file_name(),
app->get_file_name_size(),
app->get_key()->get_name());
// Process file_select return
if(res) {
// Get key file path
string_t key_str;
string_init_set_str(key_str, "ibutton/");
string_cat_str(key_str, app->get_file_name());
// Read data from file
File key_file;
uint8_t key_data[IBUTTON_KEY_DATA_SIZE + 1] = {};
// TODO process false result from file system service
app->get_fs_api()->file.open(
&key_file, string_get_cstr(key_str), FSAM_READ, FSOM_OPEN_EXISTING);
app->get_fs_api()->file.read(&key_file, key_data, IBUTTON_KEY_DATA_SIZE + 1);
app->get_fs_api()->file.close(&key_file);
string_clear(key_str);
// Set key data
iButtonKeyType key_type = static_cast<iButtonKeyType>(key_data[0]);
if(key_type > iButtonKeyType::KeyMetakom) {
app->switch_to_next_scene(iButtonApp::Scene::SceneStart);
}
app->get_key()->set_name(app->get_file_name());
app->get_key()->set_type(key_type);
app->get_key()->set_data(key_data + 1, IBUTTON_KEY_DATA_SIZE);
if(app->load_key()) {
app->switch_to_next_scene(iButtonApp::Scene::SceneSavedKeyMenu);
} else {
app->switch_to_previous_scene();