[FL-1293] iButton key rename (#479)

* ibutton: remove existing key file before saving new
* ibutton: save key name in iButtunKey object
* ibutton: rename IBUTTON_KEY_SIZE -> IBUTTON_KEY_DATA_SIZE
* ibutton: clear key when enter one manually
* ibutton: change strcpy -> strlcpy

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
gornekich
2021-05-21 12:52:47 +03:00
committed by GitHub
parent f1198950e9
commit 1242327e14
6 changed files with 33 additions and 16 deletions

View File

@@ -34,18 +34,27 @@ bool iButtonSceneSaveName::on_event(iButtonApp* app, iButtonEvent* event) {
iButtonKey* key = app->get_key();
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");
// 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_SIZE + 1);
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);
app->switch_to_next_scene(iButtonApp::Scene::SceneSaveSuccess);
} else {