[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 16 deletions

View File

@ -1,7 +1,8 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
static const uint8_t IBUTTON_KEY_SIZE = 8; static const uint8_t IBUTTON_KEY_DATA_SIZE = 8;
static const uint8_t IBUTTON_KEY_NAME_SIZE = 64;
enum class iButtonKeyType : uint8_t { enum class iButtonKeyType : uint8_t {
KeyDallas, KeyDallas,

View File

@ -2,7 +2,7 @@
#include <furi.h> #include <furi.h>
uint8_t iButtonKey::get_size() { uint8_t iButtonKey::get_size() {
return IBUTTON_KEY_SIZE; return IBUTTON_KEY_DATA_SIZE;
} }
void iButtonKey::set_data(uint8_t* _data, uint8_t _data_count) { void iButtonKey::set_data(uint8_t* _data, uint8_t _data_count) {
@ -13,6 +13,10 @@ void iButtonKey::set_data(uint8_t* _data, uint8_t _data_count) {
memcpy(data, _data, _data_count); memcpy(data, _data, _data_count);
} }
void iButtonKey::clear_data() {
memset(data, 0, get_size());
}
uint8_t* iButtonKey::get_data() { uint8_t* iButtonKey::get_data() {
return data; return data;
} }
@ -36,10 +40,10 @@ uint8_t iButtonKey::get_type_data_size() {
} }
void iButtonKey::set_name(const char* _name) { void iButtonKey::set_name(const char* _name) {
name = _name; strlcpy(name, _name, IBUTTON_KEY_NAME_SIZE);
} }
const char* iButtonKey::get_name() { char* iButtonKey::get_name() {
return name; return name;
} }

View File

@ -7,11 +7,12 @@ public:
uint8_t get_size(); uint8_t get_size();
void set_data(uint8_t* data, uint8_t data_count); void set_data(uint8_t* data, uint8_t data_count);
void clear_data();
uint8_t* get_data(); uint8_t* get_data();
uint8_t get_type_data_size(); uint8_t get_type_data_size();
void set_name(const char* name); void set_name(const char* name);
const char* get_name(); char* get_name();
void set_type(iButtonKeyType key_type); void set_type(iButtonKeyType key_type);
iButtonKeyType get_key_type(); iButtonKeyType get_key_type();
@ -19,8 +20,8 @@ public:
iButtonKey(); iButtonKey();
private: private:
uint8_t data[IBUTTON_KEY_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0}; uint8_t data[IBUTTON_KEY_DATA_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0};
const char* name = {0}; char name[IBUTTON_KEY_NAME_SIZE] = {0};
iButtonKeyType type = iButtonKeyType::KeyDallas; iButtonKeyType type = iButtonKeyType::KeyDallas;
}; };

View File

@ -37,6 +37,8 @@ bool iButtonSceneAddType::on_event(iButtonApp* app, iButtonEvent* event) {
app->get_key()->set_type(iButtonKeyType::KeyMetakom); app->get_key()->set_type(iButtonKeyType::KeyMetakom);
break; break;
} }
app->get_key()->set_name("");
app->get_key()->clear_data();
app->switch_to_next_scene(iButtonApp::Scene::SceneAddValue); app->switch_to_next_scene(iButtonApp::Scene::SceneAddValue);
consumed = true; consumed = true;
} }

View File

@ -34,18 +34,27 @@ bool iButtonSceneSaveName::on_event(iButtonApp* app, iButtonEvent* event) {
iButtonKey* key = app->get_key(); iButtonKey* key = app->get_key();
File key_file; File key_file;
string_t key_file_name; 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 // Create ibutton directory if necessary
app->get_fs_api()->common.mkdir("ibutton"); 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( bool res = app->get_fs_api()->file.open(
&key_file, string_get_cstr(key_file_name), FSAM_WRITE, FSOM_CREATE_ALWAYS); &key_file, string_get_cstr(key_file_name), FSAM_WRITE, FSOM_CREATE_ALWAYS);
// TODO process file system errors from file system service // TODO process file system errors from file system service
if(res) { 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); res = app->get_fs_api()->file.close(&key_file);
app->switch_to_next_scene(iButtonApp::Scene::SceneSaveSuccess); app->switch_to_next_scene(iButtonApp::Scene::SceneSaveSuccess);
} else { } else {

View File

@ -43,12 +43,12 @@ bool iButtonSceneStart::on_event(iButtonApp* app, iButtonEvent* event) {
string_init_set_str(key_str, "ibutton/"); string_init_set_str(key_str, "ibutton/");
string_cat_str(key_str, app->get_file_name()); string_cat_str(key_str, app->get_file_name());
File key_file; File key_file;
uint8_t key_data[IBUTTON_KEY_SIZE + 1] = {}; uint8_t key_data[IBUTTON_KEY_DATA_SIZE + 1] = {};
// Read data from file // Read data from file
// TODO handle false return // TODO handle false return
res = app->get_fs_api()->file.open( res = app->get_fs_api()->file.open(
&key_file, string_get_cstr(key_str), FSAM_READ, FSOM_OPEN_EXISTING); &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.read(&key_file, key_data, IBUTTON_KEY_DATA_SIZE + 1);
res = app->get_fs_api()->file.close(&key_file); res = app->get_fs_api()->file.close(&key_file);
string_clear(key_str); string_clear(key_str);
// Set key // Set key
@ -58,7 +58,7 @@ bool iButtonSceneStart::on_event(iButtonApp* app, iButtonEvent* event) {
} }
app->get_key()->set_name(app->get_file_name()); app->get_key()->set_name(app->get_file_name());
app->get_key()->set_type(key_type); app->get_key()->set_type(key_type);
app->get_key()->set_data(key_data + 1, IBUTTON_KEY_SIZE); app->get_key()->set_data(key_data + 1, IBUTTON_KEY_DATA_SIZE);
app->switch_to_next_scene(iButtonApp::Scene::SceneSavedKeyMenu); app->switch_to_next_scene(iButtonApp::Scene::SceneSavedKeyMenu);
} else { } else {
// TODO add error scene // TODO add error scene