[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>
This commit is contained in:
Georgii Surkov
2023-03-02 16:23:33 +03:00
committed by GitHub
parent 4359e2eaa9
commit 806428efeb
91 changed files with 3826 additions and 2166 deletions

View File

@@ -1,11 +1,15 @@
#include <furi.h>
#include <furi_hal.h>
#include <stdarg.h>
#include <cli/cli.h>
#include <lib/toolbox/args.h>
#include <one_wire/ibutton/ibutton_worker.h>
#include <toolbox/args.h>
#include <one_wire/one_wire_host.h>
#include <one_wire/ibutton/ibutton_key.h>
#include <one_wire/ibutton/ibutton_worker.h>
#include <one_wire/ibutton/ibutton_protocols.h>
static void ibutton_cli(Cli* cli, FuriString* args, void* context);
static void onewire_cli(Cli* cli, FuriString* args, void* context);
@@ -22,7 +26,7 @@ void ibutton_on_system_start() {
#endif
}
void ibutton_cli_print_usage() {
static void ibutton_cli_print_usage() {
printf("Usage:\r\n");
printf("ikey read\r\n");
printf("ikey emulate <key_type> <key_data>\r\n");
@@ -34,30 +38,52 @@ void ibutton_cli_print_usage() {
printf("\t<key_data> are hex-formatted\r\n");
};
bool ibutton_cli_get_key_type(FuriString* data, iButtonKeyType* type) {
static bool ibutton_cli_parse_key(iButtonProtocols* protocols, iButtonKey* key, FuriString* args) {
bool result = false;
FuriString* name = furi_string_alloc();
if(furi_string_cmp_str(data, "Dallas") == 0 || furi_string_cmp_str(data, "dallas") == 0) {
result = true;
*type = iButtonKeyDS1990;
} else if(furi_string_cmp_str(data, "Cyfral") == 0 || furi_string_cmp_str(data, "cyfral") == 0) {
result = true;
*type = iButtonKeyCyfral;
} else if(furi_string_cmp_str(data, "Metakom") == 0 || furi_string_cmp_str(data, "metakom") == 0) {
result = true;
*type = iButtonKeyMetakom;
}
do {
// Read protocol name
if(!args_read_string_and_trim(args, name)) break;
// Make the protocol name uppercase
const char first = furi_string_get_char(name, 0);
furi_string_set_char(name, 0, toupper((int)first));
const iButtonProtocolId id =
ibutton_protocols_get_id_by_name(protocols, furi_string_get_cstr(name));
if(id == iButtonProtocolIdInvalid) break;
ibutton_key_set_protocol_id(key, id);
// Get the data pointer
iButtonEditableData data;
ibutton_protocols_get_editable_data(protocols, key, &data);
// Read data
if(!args_read_hex_bytes(args, data.ptr, data.size)) break;
result = true;
} while(false);
furi_string_free(name);
return result;
}
void ibutton_cli_print_key_data(iButtonKey* key) {
const uint8_t* key_data = ibutton_key_get_data_p(key);
iButtonKeyType type = ibutton_key_get_type(key);
static void ibutton_cli_print_key(iButtonProtocols* protocols, iButtonKey* key) {
const char* name = ibutton_protocols_get_name(protocols, ibutton_key_get_protocol_id(key));
printf("%s ", ibutton_key_get_string_by_type(type));
for(size_t i = 0; i < ibutton_key_get_size_by_type(type); i++) {
printf("%02X", key_data[i]);
if(strncmp(name, "DS", 2) == 0) {
name = "Dallas";
}
printf("%s ", name);
iButtonEditableData data;
ibutton_protocols_get_editable_data(protocols, key, &data);
for(size_t i = 0; i < data.size; i++) {
printf("%02X", data.ptr[i]);
}
printf("\r\n");
@@ -71,9 +97,10 @@ static void ibutton_cli_worker_read_cb(void* context) {
furi_event_flag_set(event, EVENT_FLAG_IBUTTON_COMPLETE);
}
void ibutton_cli_read(Cli* cli) {
iButtonKey* key = ibutton_key_alloc();
iButtonWorker* worker = ibutton_worker_alloc();
static void ibutton_cli_read(Cli* cli) {
iButtonProtocols* protocols = ibutton_protocols_alloc();
iButtonWorker* worker = ibutton_worker_alloc(protocols);
iButtonKey* key = ibutton_key_alloc(ibutton_protocols_get_max_data_size(protocols));
FuriEventFlag* event = furi_event_flag_alloc();
ibutton_worker_start_thread(worker);
@@ -81,32 +108,25 @@ void ibutton_cli_read(Cli* cli) {
printf("Reading iButton...\r\nPress Ctrl+C to abort\r\n");
ibutton_worker_read_start(worker, key);
while(true) {
uint32_t flags =
furi_event_flag_wait(event, EVENT_FLAG_IBUTTON_COMPLETE, FuriFlagWaitAny, 100);
if(flags & EVENT_FLAG_IBUTTON_COMPLETE) {
ibutton_cli_print_key_data(key);
if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
if(!ibutton_key_dallas_crc_is_valid(key)) {
printf("Warning: invalid CRC\r\n");
}
if(!ibutton_key_dallas_is_1990_key(key)) {
printf("Warning: not a key\r\n");
}
}
ibutton_cli_print_key(protocols, key);
break;
}
if(cli_cmd_interrupt_received(cli)) break;
}
ibutton_worker_stop(worker);
ibutton_worker_stop(worker);
ibutton_worker_stop_thread(worker);
ibutton_worker_free(worker);
ibutton_key_free(key);
ibutton_worker_free(worker);
ibutton_protocols_free(protocols);
furi_event_flag_free(event);
};
@@ -124,48 +144,33 @@ static void ibutton_cli_worker_write_cb(void* context, iButtonWorkerWriteResult
}
void ibutton_cli_write(Cli* cli, FuriString* args) {
iButtonKey* key = ibutton_key_alloc();
iButtonWorker* worker = ibutton_worker_alloc();
iButtonKeyType type;
iButtonWriteContext write_context;
uint8_t key_data[IBUTTON_KEY_DATA_SIZE];
FuriString* data;
iButtonProtocols* protocols = ibutton_protocols_alloc();
iButtonWorker* worker = ibutton_worker_alloc(protocols);
iButtonKey* key = ibutton_key_alloc(ibutton_protocols_get_max_data_size(protocols));
iButtonWriteContext write_context;
write_context.event = furi_event_flag_alloc();
data = furi_string_alloc();
ibutton_worker_start_thread(worker);
ibutton_worker_write_set_callback(worker, ibutton_cli_worker_write_cb, &write_context);
do {
if(!args_read_string_and_trim(args, data)) {
if(!ibutton_cli_parse_key(protocols, key, args)) {
ibutton_cli_print_usage();
break;
}
if(!ibutton_cli_get_key_type(data, &type)) {
if(!(ibutton_protocols_get_features(protocols, ibutton_key_get_protocol_id(key)) &
iButtonProtocolFeatureWriteBlank)) {
ibutton_cli_print_usage();
break;
}
if(type != iButtonKeyDS1990) {
ibutton_cli_print_usage();
break;
}
if(!args_read_hex_bytes(args, key_data, ibutton_key_get_size_by_type(type))) {
ibutton_cli_print_usage();
break;
}
ibutton_key_set_type(key, type);
ibutton_key_set_data(key, key_data, ibutton_key_get_size_by_type(type));
printf("Writing key ");
ibutton_cli_print_key_data(key);
ibutton_cli_print_key(protocols, key);
printf("Press Ctrl+C to abort\r\n");
ibutton_worker_write_start(worker, key);
ibutton_worker_write_blank_start(worker, key);
while(true) {
uint32_t flags = furi_event_flag_wait(
write_context.event, EVENT_FLAG_IBUTTON_COMPLETE, FuriFlagWaitAny, 100);
@@ -183,64 +188,53 @@ void ibutton_cli_write(Cli* cli, FuriString* args) {
if(cli_cmd_interrupt_received(cli)) break;
}
ibutton_worker_stop(worker);
} while(false);
furi_string_free(data);
ibutton_worker_stop(worker);
ibutton_worker_stop_thread(worker);
ibutton_worker_free(worker);
ibutton_key_free(key);
ibutton_worker_free(worker);
ibutton_protocols_free(protocols);
furi_event_flag_free(write_context.event);
};
}
void ibutton_cli_emulate(Cli* cli, FuriString* args) {
iButtonKey* key = ibutton_key_alloc();
iButtonWorker* worker = ibutton_worker_alloc();
iButtonKeyType type;
uint8_t key_data[IBUTTON_KEY_DATA_SIZE];
FuriString* data;
iButtonProtocols* protocols = ibutton_protocols_alloc();
iButtonWorker* worker = ibutton_worker_alloc(protocols);
iButtonKey* key = ibutton_key_alloc(ibutton_protocols_get_max_data_size(protocols));
data = furi_string_alloc();
ibutton_worker_start_thread(worker);
do {
if(!args_read_string_and_trim(args, data)) {
if(!ibutton_cli_parse_key(protocols, key, args)) {
ibutton_cli_print_usage();
break;
}
if(!ibutton_cli_get_key_type(data, &type)) {
ibutton_cli_print_usage();
break;
}
if(!args_read_hex_bytes(args, key_data, ibutton_key_get_size_by_type(type))) {
ibutton_cli_print_usage();
break;
}
ibutton_key_set_type(key, type);
ibutton_key_set_data(key, key_data, ibutton_key_get_size_by_type(type));
printf("Emulating key ");
ibutton_cli_print_key_data(key);
ibutton_cli_print_key(protocols, key);
printf("Press Ctrl+C to abort\r\n");
ibutton_worker_emulate_start(worker, key);
while(!cli_cmd_interrupt_received(cli)) {
furi_delay_ms(100);
};
ibutton_worker_stop(worker);
} while(false);
furi_string_free(data);
ibutton_worker_stop(worker);
ibutton_worker_stop_thread(worker);
ibutton_worker_free(worker);
ibutton_key_free(key);
ibutton_worker_free(worker);
ibutton_protocols_free(protocols);
};
static void ibutton_cli(Cli* cli, FuriString* args, void* context) {
void ibutton_cli(Cli* cli, FuriString* args, void* context) {
UNUSED(cli);
UNUSED(context);
FuriString* cmd;
cmd = furi_string_alloc();
@@ -264,7 +258,7 @@ static void ibutton_cli(Cli* cli, FuriString* args, void* context) {
furi_string_free(cmd);
}
void onewire_cli_print_usage() {
static void onewire_cli_print_usage() {
printf("Usage:\r\n");
printf("onewire search\r\n");
};
@@ -281,7 +275,7 @@ static void onewire_cli_search(Cli* cli) {
furi_hal_power_enable_otg();
while(!done) {
if(onewire_host_search(onewire, address, NORMAL_SEARCH) != 1) {
if(onewire_host_search(onewire, address, OneWireHostSearchModeNormal) != 1) {
printf("Search finished\r\n");
onewire_host_reset_search(onewire);
done = true;