[FL-1061] iButton save and load from sd card (#394)

* SD App: fix queue adresses
* sd-filesystem: fix making path on file select event
* ibutton: add key reading from sd card
* ibutton: save ibutton key to sd card
* ibutton: add deleting keys from sd card
* ibutton: remove KeyStore from application
* ibutton: make directory if necessary on key save

Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
gornekich
2021-03-31 20:47:32 +03:00
committed by GitHub
parent 6375f21cf5
commit 5309bfae41
15 changed files with 132 additions and 256 deletions

View File

@@ -55,10 +55,19 @@ bool iButtonSceneDeleteConfirm::on_event(iButtonApp* app, iButtonEvent* event) {
if(event->type == iButtonEvent::Type::EventTypeDialogResult) {
if(event->payload.dialog_result == DialogExResultRight) {
KeyStore* store = app->get_key_store();
store->remove_key(app->get_stored_key_index());
app->switch_to_next_scene(iButtonApp::Scene::SceneDeleteSuccess);
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) {
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

@@ -25,7 +25,7 @@ bool iButtonSceneDeleteSuccess::on_event(iButtonApp* app, iButtonEvent* event) {
bool consumed = false;
if(event->type == iButtonEvent::Type::EventTypeBack) {
app->search_and_switch_to_previous_scene({iButtonApp::Scene::SceneSavedList});
app->search_and_switch_to_previous_scene({iButtonApp::Scene::SceneStart});
consumed = true;
}

View File

@@ -4,6 +4,7 @@
#include "../ibutton-event.h"
#include "../ibutton-key.h"
#include <callback-connector.h>
#include <filesystem-api.h>
void iButtonSceneSaveName::on_enter(iButtonApp* app) {
iButtonAppViewManager* view_manager = app->get_view_manager();
@@ -28,14 +29,23 @@ bool iButtonSceneSaveName::on_event(iButtonApp* app, iButtonEvent* event) {
bool consumed = false;
if(event->type == iButtonEvent::Type::EventTypeTextEditResult) {
KeyStore* store = app->get_key_store();
uint8_t key_index = store->add_key();
iButtonKey* key = app->get_key();
store->set_key_type(key_index, key->get_key_type());
store->set_key_name(key_index, app->get_text_store());
store->set_key_data(key_index, key->get_data(), key->get_size());
File key_file;
string_t key_file_name;
string_init_set_str(key_file_name, "ibutton/");
string_cat_str(key_file_name, app->get_text_store());
uint8_t key_data[IBUTTON_KEY_SIZE + 1];
key_data[0] = static_cast<uint8_t>(key->get_key_type());
memcpy(key_data + 1, key->get_data(), IBUTTON_KEY_SIZE);
// Create ibutton directory if necessary
app->get_fs_api()->common.mkdir("ibutton");
bool res = app->get_fs_api()->file.open(
&key_file, string_get_cstr(key_file_name), FSAM_WRITE, FSOM_CREATE_ALWAYS);
if(res) {
res = app->get_fs_api()->file.write(&key_file, key_data, IBUTTON_KEY_SIZE + 1);
res = app->get_fs_api()->file.close(&key_file);
}
string_clear(key_file_name);
app->switch_to_next_scene(iButtonApp::Scene::SceneSaveSuccess);
consumed = true;
}

View File

@@ -1,62 +0,0 @@
#include "ibutton-scene-saved.h"
#include "../ibutton-app.h"
#include "../ibutton-view-manager.h"
#include "../ibutton-event.h"
#include <callback-connector.h>
void iButtonSceneSavedList::on_enter(iButtonApp* app) {
iButtonAppViewManager* view_manager = app->get_view_manager();
Submenu* submenu = view_manager->get_submenu();
auto callback = cbc::obtain_connector(this, &iButtonSceneSavedList::submenu_callback);
KeyStore* store = app->get_key_store();
if(store->get_key_count() > 0) {
for(uint8_t i = 0; i < store->get_key_count(); i++) {
submenu_add_item(submenu, store->get_key_name(i), i, callback, app);
}
} else {
submenu_add_item(submenu, "Empty", 0, NULL, NULL);
}
view_manager->switch_to(iButtonAppViewManager::Type::iButtonAppViewSubmenu);
}
bool iButtonSceneSavedList::on_event(iButtonApp* app, iButtonEvent* event) {
bool consumed = false;
if(event->type == iButtonEvent::Type::EventTypeMenuSelected) {
uint8_t key_index = event->payload.menu_index;
// store data
iButtonKey* stored_key_data = app->get_key();
KeyStore* store = app->get_key_store();
app->set_stored_key_index(key_index);
stored_key_data->set_name(store->get_key_name(key_index));
stored_key_data->set_type(store->get_key_type(key_index));
stored_key_data->set_data(store->get_key_data(key_index), stored_key_data->get_size());
app->switch_to_next_scene(iButtonApp::Scene::SceneSavedKeyMenu);
consumed = true;
}
return consumed;
}
void iButtonSceneSavedList::on_exit(iButtonApp* app) {
iButtonAppViewManager* view = app->get_view_manager();
Submenu* submenu = view->get_submenu();
submenu_clean(submenu);
}
void iButtonSceneSavedList::submenu_callback(void* context, uint32_t index) {
iButtonApp* app = static_cast<iButtonApp*>(context);
iButtonEvent event;
event.type = iButtonEvent::Type::EventTypeMenuSelected;
event.payload.menu_index = index;
app->get_view_manager()->send_event(&event);
}

View File

@@ -1,12 +0,0 @@
#pragma once
#include "ibutton-scene-generic.h"
class iButtonSceneSavedList : public iButtonScene {
public:
void on_enter(iButtonApp* app) final;
bool on_event(iButtonApp* app, iButtonEvent* event) final;
void on_exit(iButtonApp* app) final;
private:
void submenu_callback(void* context, uint32_t index);
};

View File

@@ -3,6 +3,7 @@
#include "../ibutton-view-manager.h"
#include "../ibutton-event.h"
#include <callback-connector.h>
#include <filesystem-api.h>
typedef enum {
SubmenuIndexRead,
@@ -30,9 +31,40 @@ bool iButtonSceneStart::on_event(iButtonApp* app, iButtonEvent* event) {
case SubmenuIndexRead:
app->switch_to_next_scene(iButtonApp::Scene::SceneRead);
break;
case SubmenuIndexSaved:
app->switch_to_next_scene(iButtonApp::Scene::SceneSavedList);
break;
case SubmenuIndexSaved: {
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());
if(res) {
string_t key_str;
string_init_set_str(key_str, "ibutton/");
string_cat_str(key_str, app->get_file_name());
File key_file;
uint8_t key_data[IBUTTON_KEY_SIZE + 1] = {};
// Read data from file
// TODO handle false return
res = app->get_fs_api()->file.open(
&key_file, string_get_cstr(key_str), FSAM_READ, FSOM_OPEN_EXISTING);
res = app->get_fs_api()->file.read(&key_file, key_data, IBUTTON_KEY_SIZE + 1);
res = app->get_fs_api()->file.close(&key_file);
string_clear(key_str);
// Set key
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_SIZE);
app->switch_to_next_scene(iButtonApp::Scene::SceneSavedKeyMenu);
} else {
// TODO add error scene
app->switch_to_next_scene(iButtonApp::Scene::SceneStart);
}
}; break;
case SubmenuIndexAdd:
app->switch_to_next_scene(iButtonApp::Scene::SceneAddType);
break;