unleashed-firmware/lib/flipper_format/flipper_format.c
Georgii Surkov 806428efeb
[FL-3070] iButton system and app refactoring (#2388)
* Add 1-wire thermometer example app stub
* Working 1-wire thermometer app
* Refactor app to use threads
* Clean up code, add comments
* Add CRC checking
* Increase update period
* Fix error in fbt
* Revert the old update period
* Use settable pin in onewire_host
* Use settable pin for onewire_slave
* Clear EXTI flag after callback, make private methods static in onewire_slave
* Do not hardcode GPIO pin number
* Remove iButton hal from furi_hal_rfid
* Remove most of furi_hal_ibutton
* Add some of furi_hal_ibutton back
* Slightly neater code
* Update CODEOWNERS
* Add furi_hal_gpio_get_ext_pin_number
* Create README.md
* Temporary get Metakom and Cyfral keys out of the way
* Better enum name
* Syncing work, does not compile
* Syncing work, now compiles
* Working read impl for DS1990 and DS1992
* Add the ability to display extended key data
* Get rid of DialogEx
* Add save and load API
* Better iButtonKey encapsulation
* Fix crash
* Load key code boilerplate
* More load key code boilerplate
* Minor code cleanup
* Implement loading and saving DS1990 keys
* Implement the Info scene
* Implement loading & saving for DS1992
* Implement read error scene stub
* Implement delete confirmation screen
* Better error messages (protocol-dependent)
* Minor old code cleanup
* Remove iButtonDevice, add command callback to iButtonSlave
* Implement draft emulation for DS1990
* Better emulation for DS1990
* Initial emulation implementation for DS1992
* Better common command definitions
* Use common submenu callback, add protocol list
* Improve ViewData screen
* Improve scene_add_type
* Add stubs for write functionality
* Improve naming consistency
* Implement writing a DS1992 onto another one
* Improve DS1992 write code
* Improve DS1992 write code once more
* Prepare write_blank for DS1990, delete ibutton_writer
* Implement writing DS1990 onto blanks
* Fix reading DS1990
* Partially implement writing DS1992 onto blanks
* Implement GUI for writing keys
* Implement GUI for emulating keys
* Reduce memory usage for pretty_format
* Automatically truncate data more than 256 bytes
* Initial implementation of DS1996 (not tested)
* Fix crash due to missing virtual function
* Improve emulation code
* Improve DS1992 emulation code
* Correct return value for onewire_slave_send
* Correct return value for onewire_slave_receive
* Implement emulation for DS1992 & DS1996
* Better constant names
* Simplify & optimise the emulation code
* Remove duplicate code
* Add skip rom command emulation
* Show loading animation for large keys
* Implement manual adding & editing of keys
* Use buffered file streams to speed up saving & loading
* Reset key name before adding a new one
* Sync a buffered file stream before saving
* Use the DSGeneric protocol as a fallback option
* Implement emulation via RPC
* Refactor iButton code in preparation for comparator keys
* Refactor iButton code in preparation for comparator keys once more
* Make some functions static
* Make protocols not rely on one_wire classes
* Improve ProtocolDict usage
* Improve ProtocolDict usage more
* Implement reading Metakom & Cyfral keys
* Rename some files
* Better file structure
* Implement a unified interface for misc protocols
* Implement a unified interface for dallas protocols
* Concrete types for Dallas protocols
* Implement a unified interface for all key types
* Improved type naming
* Improved private types
* Proper types in protocol definitions
* Implement emulation for Cyfral & Metakom keys
* Implement save&load for Metakom & Cyfral keys
* Better type names
* Rename files, better names
* Allocate iButtonProtocols like a normal class
* Reset the key each time the start scene is selected
* Improve comments and constants
* Add ibutton_protocols to SDK headers
* Add ibutton_key to SDK headers
* Add ibutton_key to SDK headers
* Implement reading via cli
* Implement emulation via cli
* Implement writing Dallas blanks via cli
* Correctly revert the editing if cancelled by the user
* Correct committing mishap
* Elide the long text on the info screen
* Change key name for data in Misc keys
* Update iButtonFileFormat.md
* Remember the key's folder
* Save menu position in ReadKeyMenu and SavedKeyMenu
* Correct use of preselected path in file browser

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2023-03-02 22:23:33 +09:00

644 lines
19 KiB
C

#include <core/check.h>
#include <toolbox/stream/stream.h>
#include <toolbox/stream/string_stream.h>
#include <toolbox/stream/file_stream.h>
#include <toolbox/stream/buffered_file_stream.h>
#include "flipper_format.h"
#include "flipper_format_i.h"
#include "flipper_format_stream.h"
#include "flipper_format_stream_i.h"
/********************************** Private **********************************/
struct FlipperFormat {
Stream* stream;
bool strict_mode;
};
static const char* const flipper_format_filetype_key = "Filetype";
static const char* const flipper_format_version_key = "Version";
Stream* flipper_format_get_raw_stream(FlipperFormat* flipper_format) {
return flipper_format->stream;
}
/********************************** Public **********************************/
FlipperFormat* flipper_format_string_alloc() {
FlipperFormat* flipper_format = malloc(sizeof(FlipperFormat));
flipper_format->stream = string_stream_alloc();
flipper_format->strict_mode = false;
return flipper_format;
}
FlipperFormat* flipper_format_file_alloc(Storage* storage) {
FlipperFormat* flipper_format = malloc(sizeof(FlipperFormat));
flipper_format->stream = file_stream_alloc(storage);
flipper_format->strict_mode = false;
return flipper_format;
}
FlipperFormat* flipper_format_buffered_file_alloc(Storage* storage) {
FlipperFormat* flipper_format = malloc(sizeof(FlipperFormat));
flipper_format->stream = buffered_file_stream_alloc(storage);
flipper_format->strict_mode = false;
return flipper_format;
}
bool flipper_format_file_open_existing(FlipperFormat* flipper_format, const char* path) {
furi_assert(flipper_format);
return file_stream_open(flipper_format->stream, path, FSAM_READ_WRITE, FSOM_OPEN_EXISTING);
}
bool flipper_format_buffered_file_open_existing(FlipperFormat* flipper_format, const char* path) {
furi_assert(flipper_format);
return buffered_file_stream_open(
flipper_format->stream, path, FSAM_READ_WRITE, FSOM_OPEN_EXISTING);
}
bool flipper_format_file_open_append(FlipperFormat* flipper_format, const char* path) {
furi_assert(flipper_format);
bool result =
file_stream_open(flipper_format->stream, path, FSAM_READ_WRITE, FSOM_OPEN_APPEND);
// Add EOL if it is not there
if(stream_size(flipper_format->stream) >= 1) {
do {
char last_char;
result = false;
if(!stream_seek(flipper_format->stream, -1, StreamOffsetFromEnd)) break;
uint16_t bytes_were_read =
stream_read(flipper_format->stream, (uint8_t*)&last_char, 1);
if(bytes_were_read != 1) break;
if(last_char != flipper_format_eoln) {
if(!flipper_format_stream_write_eol(flipper_format->stream)) break;
}
result = true;
} while(false);
} else {
stream_seek(flipper_format->stream, 0, StreamOffsetFromEnd);
}
return result;
}
bool flipper_format_file_open_always(FlipperFormat* flipper_format, const char* path) {
furi_assert(flipper_format);
return file_stream_open(flipper_format->stream, path, FSAM_READ_WRITE, FSOM_CREATE_ALWAYS);
}
bool flipper_format_buffered_file_open_always(FlipperFormat* flipper_format, const char* path) {
furi_assert(flipper_format);
return buffered_file_stream_open(
flipper_format->stream, path, FSAM_READ_WRITE, FSOM_CREATE_ALWAYS);
}
bool flipper_format_file_open_new(FlipperFormat* flipper_format, const char* path) {
furi_assert(flipper_format);
return file_stream_open(flipper_format->stream, path, FSAM_READ_WRITE, FSOM_CREATE_NEW);
}
bool flipper_format_file_close(FlipperFormat* flipper_format) {
furi_assert(flipper_format);
return file_stream_close(flipper_format->stream);
}
bool flipper_format_buffered_file_close(FlipperFormat* flipper_format) {
furi_assert(flipper_format);
return buffered_file_stream_close(flipper_format->stream);
}
void flipper_format_free(FlipperFormat* flipper_format) {
furi_assert(flipper_format);
stream_free(flipper_format->stream);
free(flipper_format);
}
void flipper_format_set_strict_mode(FlipperFormat* flipper_format, bool strict_mode) {
flipper_format->strict_mode = strict_mode;
}
bool flipper_format_rewind(FlipperFormat* flipper_format) {
furi_assert(flipper_format);
return stream_rewind(flipper_format->stream);
}
bool flipper_format_seek_to_end(FlipperFormat* flipper_format) {
furi_assert(flipper_format);
return stream_seek(flipper_format->stream, 0, StreamOffsetFromEnd);
}
bool flipper_format_key_exist(FlipperFormat* flipper_format, const char* key) {
size_t pos = stream_tell(flipper_format->stream);
stream_seek(flipper_format->stream, 0, StreamOffsetFromStart);
bool result = flipper_format_stream_seek_to_key(flipper_format->stream, key, false);
stream_seek(flipper_format->stream, pos, StreamOffsetFromStart);
return result;
}
bool flipper_format_read_header(
FlipperFormat* flipper_format,
FuriString* filetype,
uint32_t* version) {
furi_assert(flipper_format);
return flipper_format_read_string(flipper_format, flipper_format_filetype_key, filetype) &&
flipper_format_read_uint32(flipper_format, flipper_format_version_key, version, 1);
}
bool flipper_format_write_header(
FlipperFormat* flipper_format,
FuriString* filetype,
const uint32_t version) {
furi_assert(flipper_format);
return flipper_format_write_header_cstr(
flipper_format, furi_string_get_cstr(filetype), version);
}
bool flipper_format_write_header_cstr(
FlipperFormat* flipper_format,
const char* filetype,
const uint32_t version) {
furi_assert(flipper_format);
return flipper_format_write_string_cstr(
flipper_format, flipper_format_filetype_key, filetype) &&
flipper_format_write_uint32(flipper_format, flipper_format_version_key, &version, 1);
}
bool flipper_format_get_value_count(
FlipperFormat* flipper_format,
const char* key,
uint32_t* count) {
furi_assert(flipper_format);
return flipper_format_stream_get_value_count(
flipper_format->stream, key, count, flipper_format->strict_mode);
}
bool flipper_format_read_string(FlipperFormat* flipper_format, const char* key, FuriString* data) {
furi_assert(flipper_format);
return flipper_format_stream_read_value_line(
flipper_format->stream, key, FlipperStreamValueStr, data, 1, flipper_format->strict_mode);
}
bool flipper_format_write_string(FlipperFormat* flipper_format, const char* key, FuriString* data) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueStr,
.data = furi_string_get_cstr(data),
.data_size = 1,
};
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
return result;
}
bool flipper_format_write_string_cstr(
FlipperFormat* flipper_format,
const char* key,
const char* data) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueStr,
.data = data,
.data_size = 1,
};
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
return result;
}
bool flipper_format_read_hex_uint64(
FlipperFormat* flipper_format,
const char* key,
uint64_t* data,
const uint16_t data_size) {
furi_assert(flipper_format);
return flipper_format_stream_read_value_line(
flipper_format->stream,
key,
FlipperStreamValueHexUint64,
data,
data_size,
flipper_format->strict_mode);
}
bool flipper_format_write_hex_uint64(
FlipperFormat* flipper_format,
const char* key,
const uint64_t* data,
const uint16_t data_size) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueHexUint64,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
return result;
}
bool flipper_format_read_uint32(
FlipperFormat* flipper_format,
const char* key,
uint32_t* data,
const uint16_t data_size) {
furi_assert(flipper_format);
return flipper_format_stream_read_value_line(
flipper_format->stream,
key,
FlipperStreamValueUint32,
data,
data_size,
flipper_format->strict_mode);
}
bool flipper_format_write_uint32(
FlipperFormat* flipper_format,
const char* key,
const uint32_t* data,
const uint16_t data_size) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueUint32,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
return result;
}
bool flipper_format_read_int32(
FlipperFormat* flipper_format,
const char* key,
int32_t* data,
const uint16_t data_size) {
return flipper_format_stream_read_value_line(
flipper_format->stream,
key,
FlipperStreamValueInt32,
data,
data_size,
flipper_format->strict_mode);
}
bool flipper_format_write_int32(
FlipperFormat* flipper_format,
const char* key,
const int32_t* data,
const uint16_t data_size) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueInt32,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
return result;
}
bool flipper_format_read_bool(
FlipperFormat* flipper_format,
const char* key,
bool* data,
const uint16_t data_size) {
return flipper_format_stream_read_value_line(
flipper_format->stream,
key,
FlipperStreamValueBool,
data,
data_size,
flipper_format->strict_mode);
}
bool flipper_format_write_bool(
FlipperFormat* flipper_format,
const char* key,
const bool* data,
const uint16_t data_size) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueBool,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
return result;
}
bool flipper_format_read_float(
FlipperFormat* flipper_format,
const char* key,
float* data,
const uint16_t data_size) {
return flipper_format_stream_read_value_line(
flipper_format->stream,
key,
FlipperStreamValueFloat,
data,
data_size,
flipper_format->strict_mode);
}
bool flipper_format_write_float(
FlipperFormat* flipper_format,
const char* key,
const float* data,
const uint16_t data_size) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueFloat,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
return result;
}
bool flipper_format_read_hex(
FlipperFormat* flipper_format,
const char* key,
uint8_t* data,
const uint16_t data_size) {
return flipper_format_stream_read_value_line(
flipper_format->stream,
key,
FlipperStreamValueHex,
data,
data_size,
flipper_format->strict_mode);
}
bool flipper_format_write_hex(
FlipperFormat* flipper_format,
const char* key,
const uint8_t* data,
const uint16_t data_size) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueHex,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
return result;
}
bool flipper_format_write_comment(FlipperFormat* flipper_format, FuriString* data) {
furi_assert(flipper_format);
return flipper_format_write_comment_cstr(flipper_format, furi_string_get_cstr(data));
}
bool flipper_format_write_comment_cstr(FlipperFormat* flipper_format, const char* data) {
furi_assert(flipper_format);
return flipper_format_stream_write_comment_cstr(flipper_format->stream, data);
}
bool flipper_format_delete_key(FlipperFormat* flipper_format, const char* key) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueIgnore,
.data = NULL,
.data_size = 0,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}
bool flipper_format_update_string(FlipperFormat* flipper_format, const char* key, FuriString* data) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueStr,
.data = furi_string_get_cstr(data),
.data_size = 1,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}
bool flipper_format_update_string_cstr(
FlipperFormat* flipper_format,
const char* key,
const char* data) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueStr,
.data = data,
.data_size = 1,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}
bool flipper_format_update_uint32(
FlipperFormat* flipper_format,
const char* key,
const uint32_t* data,
const uint16_t data_size) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueUint32,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}
bool flipper_format_update_int32(
FlipperFormat* flipper_format,
const char* key,
const int32_t* data,
const uint16_t data_size) {
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueInt32,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}
bool flipper_format_update_bool(
FlipperFormat* flipper_format,
const char* key,
const bool* data,
const uint16_t data_size) {
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueBool,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}
bool flipper_format_update_float(
FlipperFormat* flipper_format,
const char* key,
const float* data,
const uint16_t data_size) {
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueFloat,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}
bool flipper_format_update_hex(
FlipperFormat* flipper_format,
const char* key,
const uint8_t* data,
const uint16_t data_size) {
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueHex,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}
bool flipper_format_insert_or_update_string(
FlipperFormat* flipper_format,
const char* key,
FuriString* data) {
bool result = false;
if(!flipper_format_key_exist(flipper_format, key)) {
flipper_format_seek_to_end(flipper_format);
result = flipper_format_write_string(flipper_format, key, data);
} else {
result = flipper_format_update_string(flipper_format, key, data);
}
return result;
}
bool flipper_format_insert_or_update_string_cstr(
FlipperFormat* flipper_format,
const char* key,
const char* data) {
bool result = false;
if(!flipper_format_key_exist(flipper_format, key)) {
flipper_format_seek_to_end(flipper_format);
result = flipper_format_write_string_cstr(flipper_format, key, data);
} else {
result = flipper_format_update_string_cstr(flipper_format, key, data);
}
return result;
}
bool flipper_format_insert_or_update_uint32(
FlipperFormat* flipper_format,
const char* key,
const uint32_t* data,
const uint16_t data_size) {
bool result = false;
if(!flipper_format_key_exist(flipper_format, key)) {
flipper_format_seek_to_end(flipper_format);
result = flipper_format_write_uint32(flipper_format, key, data, data_size);
} else {
result = flipper_format_update_uint32(flipper_format, key, data, data_size);
}
return result;
}
bool flipper_format_insert_or_update_int32(
FlipperFormat* flipper_format,
const char* key,
const int32_t* data,
const uint16_t data_size) {
bool result = false;
if(!flipper_format_key_exist(flipper_format, key)) {
flipper_format_seek_to_end(flipper_format);
result = flipper_format_write_int32(flipper_format, key, data, data_size);
} else {
result = flipper_format_update_int32(flipper_format, key, data, data_size);
}
return result;
}
bool flipper_format_insert_or_update_bool(
FlipperFormat* flipper_format,
const char* key,
const bool* data,
const uint16_t data_size) {
bool result = false;
if(!flipper_format_key_exist(flipper_format, key)) {
flipper_format_seek_to_end(flipper_format);
result = flipper_format_write_bool(flipper_format, key, data, data_size);
} else {
result = flipper_format_update_bool(flipper_format, key, data, data_size);
}
return result;
}
bool flipper_format_insert_or_update_float(
FlipperFormat* flipper_format,
const char* key,
const float* data,
const uint16_t data_size) {
bool result = false;
if(!flipper_format_key_exist(flipper_format, key)) {
flipper_format_seek_to_end(flipper_format);
result = flipper_format_write_float(flipper_format, key, data, data_size);
} else {
result = flipper_format_update_float(flipper_format, key, data, data_size);
}
return result;
}
bool flipper_format_insert_or_update_hex(
FlipperFormat* flipper_format,
const char* key,
const uint8_t* data,
const uint16_t data_size) {
bool result = false;
if(!flipper_format_key_exist(flipper_format, key)) {
flipper_format_seek_to_end(flipper_format);
result = flipper_format_write_hex(flipper_format, key, data, data_size);
} else {
result = flipper_format_update_hex(flipper_format, key, data, data_size);
}
return result;
}