From a841abcc8de1e5aa78b6e83b1cd80270dcf455e5 Mon Sep 17 00:00:00 2001 From: atomsmasha <37353991+atomsmasha@users.noreply.github.com> Date: Tue, 5 Jul 2022 14:15:53 +0800 Subject: [PATCH 01/11] LFRFID - Direct Emulation Adding a new root menu item to the 125 kHz/LFRFID app - "Direct Emulation" - Updating core app files to link new scenes --- applications/lfrfid/lfrfid_app.cpp | 437 +++++++++++++---------------- applications/lfrfid/lfrfid_app.h | 201 +++++++------ 2 files changed, 294 insertions(+), 344 deletions(-) diff --git a/applications/lfrfid/lfrfid_app.cpp b/applications/lfrfid/lfrfid_app.cpp index d2c92d24861..98a099a8908 100644 --- a/applications/lfrfid/lfrfid_app.cpp +++ b/applications/lfrfid/lfrfid_app.cpp @@ -1,241 +1,196 @@ -#include "lfrfid_app.h" -#include "assets_icons.h" -#include "furi/common_defines.h" -#include "m-string.h" -#include "scene/lfrfid_app_scene_start.h" -#include "scene/lfrfid_app_scene_read.h" -#include "scene/lfrfid_app_scene_read_success.h" -#include "scene/lfrfid_app_scene_retry_confirm.h" -#include "scene/lfrfid_app_scene_exit_confirm.h" -#include "scene/lfrfid_app_scene_read_menu.h" -#include "scene/lfrfid_app_scene_write.h" -#include "scene/lfrfid_app_scene_write_success.h" -#include "scene/lfrfid_app_scene_emulate.h" -#include "scene/lfrfid_app_scene_save_name.h" -#include "scene/lfrfid_app_scene_save_success.h" -#include "scene/lfrfid_app_scene_select_key.h" -#include "scene/lfrfid_app_scene_saved_key_menu.h" -#include "scene/lfrfid_app_scene_save_data.h" -#include "scene/lfrfid_app_scene_save_type.h" -#include "scene/lfrfid_app_scene_saved_info.h" -#include "scene/lfrfid_app_scene_delete_confirm.h" -#include "scene/lfrfid_app_scene_delete_success.h" -#include "scene/lfrfid_app_scene_rpc.h" - -#include -#include - -#include "rpc/rpc_app.h" - -const char* LfRfidApp::app_folder = "/any/lfrfid"; -const char* LfRfidApp::app_extension = ".rfid"; -const char* LfRfidApp::app_filetype = "Flipper RFID key"; - -LfRfidApp::LfRfidApp() - : scene_controller{this} - , notification{"notification"} - , storage{"storage"} - , dialogs{"dialogs"} - , text_store(40) { - string_init_set_str(file_path, app_folder); -} - -LfRfidApp::~LfRfidApp() { - string_clear(file_path); - if(rpc_ctx) { - rpc_system_app_set_callback(rpc_ctx, NULL, NULL); - } -} - -static bool rpc_command_callback(RpcAppSystemEvent event, const char* arg, void* context) { - furi_assert(context); - LfRfidApp* app = static_cast(context); - - bool result = false; - - if(event == RpcAppEventSessionClose) { - rpc_system_app_set_callback(app->rpc_ctx, NULL, NULL); - app->rpc_ctx = NULL; - LfRfidApp::Event event; - event.type = LfRfidApp::EventType::Exit; - app->view_controller.send_event(&event); - result = true; - } else if(event == RpcAppEventAppExit) { - LfRfidApp::Event event; - event.type = LfRfidApp::EventType::Exit; - app->view_controller.send_event(&event); - result = true; - } else if(event == RpcAppEventLoadFile) { - if(arg) { - string_set_str(app->file_path, arg); - if(app->load_key_data(app->file_path, &(app->worker.key), false)) { - LfRfidApp::Event event; - event.type = LfRfidApp::EventType::EmulateStart; - app->view_controller.send_event(&event); - app->worker.start_emulate(); - result = true; - } - } - } - - return result; -} - -void LfRfidApp::run(void* _args) { - const char* args = reinterpret_cast(_args); - - make_app_folder(); - - if(strlen(args)) { - uint32_t rpc_ctx_ptr = 0; - if(sscanf(args, "RPC %lX", &rpc_ctx_ptr) == 1) { - rpc_ctx = (RpcAppSystem*)rpc_ctx_ptr; - rpc_system_app_set_callback(rpc_ctx, rpc_command_callback, this); - scene_controller.add_scene(SceneType::Rpc, new LfRfidAppSceneRpc()); - scene_controller.process(100, SceneType::Rpc); - } else { - string_set_str(file_path, args); - load_key_data(file_path, &worker.key, true); - scene_controller.add_scene(SceneType::Emulate, new LfRfidAppSceneEmulate()); - scene_controller.process(100, SceneType::Emulate); - } - - } else { - scene_controller.add_scene(SceneType::Start, new LfRfidAppSceneStart()); - scene_controller.add_scene(SceneType::Read, new LfRfidAppSceneRead()); - scene_controller.add_scene(SceneType::RetryConfirm, new LfRfidAppSceneRetryConfirm()); - scene_controller.add_scene(SceneType::ExitConfirm, new LfRfidAppSceneExitConfirm()); - scene_controller.add_scene(SceneType::ReadSuccess, new LfRfidAppSceneReadSuccess()); - scene_controller.add_scene(SceneType::ReadKeyMenu, new LfRfidAppSceneReadKeyMenu()); - scene_controller.add_scene(SceneType::Write, new LfRfidAppSceneWrite()); - scene_controller.add_scene(SceneType::WriteSuccess, new LfRfidAppSceneWriteSuccess()); - scene_controller.add_scene(SceneType::Emulate, new LfRfidAppSceneEmulate()); - scene_controller.add_scene(SceneType::SaveName, new LfRfidAppSceneSaveName()); - scene_controller.add_scene(SceneType::SaveSuccess, new LfRfidAppSceneSaveSuccess()); - scene_controller.add_scene(SceneType::SelectKey, new LfRfidAppSceneSelectKey()); - scene_controller.add_scene(SceneType::SavedKeyMenu, new LfRfidAppSceneSavedKeyMenu()); - scene_controller.add_scene(SceneType::SaveData, new LfRfidAppSceneSaveData()); - scene_controller.add_scene(SceneType::SaveType, new LfRfidAppSceneSaveType()); - scene_controller.add_scene(SceneType::SavedInfo, new LfRfidAppSceneSavedInfo()); - scene_controller.add_scene(SceneType::DeleteConfirm, new LfRfidAppSceneDeleteConfirm()); - scene_controller.add_scene(SceneType::DeleteSuccess, new LfRfidAppSceneDeleteSuccess()); - scene_controller.process(100); - } -} - -bool LfRfidApp::save_key(RfidKey* key) { - bool result = false; - - make_app_folder(); - - if(string_end_with_str_p(file_path, app_extension)) { - size_t filename_start = string_search_rchar(file_path, '/'); - string_left(file_path, filename_start); - } - - string_cat_printf(file_path, "/%s%s", key->get_name(), app_extension); - - result = save_key_data(file_path, key); - return result; -} - -bool LfRfidApp::load_key_from_file_select(bool need_restore) { - if(!need_restore) { - string_set_str(file_path, app_folder); - } - - bool result = dialog_file_browser_show( - dialogs, file_path, file_path, app_extension, true, &I_125_10px, true); - - if(result) { - result = load_key_data(file_path, &worker.key, true); - } - - return result; -} - -bool LfRfidApp::delete_key(RfidKey* key) { - UNUSED(key); - return storage_simply_remove(storage, string_get_cstr(file_path)); -} - -bool LfRfidApp::load_key_data(string_t path, RfidKey* key, bool show_dialog) { - FlipperFormat* file = flipper_format_file_alloc(storage); - bool result = false; - string_t str_result; - string_init(str_result); - - do { - if(!flipper_format_file_open_existing(file, string_get_cstr(path))) break; - - // header - uint32_t version; - if(!flipper_format_read_header(file, str_result, &version)) break; - if(string_cmp_str(str_result, app_filetype) != 0) break; - if(version != 1) break; - - // key type - LfrfidKeyType type; - RfidKey loaded_key; - - if(!flipper_format_read_string(file, "Key type", str_result)) break; - if(!lfrfid_key_get_string_type(string_get_cstr(str_result), &type)) break; - loaded_key.set_type(type); - - // key data - uint8_t key_data[loaded_key.get_type_data_count()] = {}; - if(!flipper_format_read_hex(file, "Data", key_data, loaded_key.get_type_data_count())) - break; - loaded_key.set_data(key_data, loaded_key.get_type_data_count()); - - path_extract_filename(path, str_result, true); - loaded_key.set_name(string_get_cstr(str_result)); - - *key = loaded_key; - result = true; - } while(0); - - flipper_format_free(file); - string_clear(str_result); - - if((!result) && (show_dialog)) { - dialog_message_show_storage_error(dialogs, "Cannot load\nkey file"); - } - - return result; -} - -bool LfRfidApp::save_key_data(string_t path, RfidKey* key) { - FlipperFormat* file = flipper_format_file_alloc(storage); - bool result = false; - - do { - if(!flipper_format_file_open_always(file, string_get_cstr(path))) break; - if(!flipper_format_write_header_cstr(file, app_filetype, 1)) break; - if(!flipper_format_write_comment_cstr(file, "Key type can be EM4100, H10301 or I40134")) - break; - if(!flipper_format_write_string_cstr( - file, "Key type", lfrfid_key_get_type_string(key->get_type()))) - break; - if(!flipper_format_write_comment_cstr( - file, "Data size for EM4100 is 5, for H10301 is 3, for I40134 is 3")) - break; - if(!flipper_format_write_hex(file, "Data", key->get_data(), key->get_type_data_count())) - break; - result = true; - } while(0); - - flipper_format_free(file); - - if(!result) { - dialog_message_show_storage_error(dialogs, "Cannot save\nkey file"); - } - - return result; -} - -void LfRfidApp::make_app_folder() { - if(!storage_simply_mkdir(storage, app_folder)) { - dialog_message_show_storage_error(dialogs, "Cannot create\napp folder"); - } -} +#include "lfrfid_app.h" +#include "assets_icons.h" +#include "furi/common_defines.h" +#include "m-string.h" +#include "scene/lfrfid_app_scene_start.h" +#include "scene/lfrfid_app_scene_read.h" +#include "scene/lfrfid_app_scene_read_success.h" +#include "scene/lfrfid_app_scene_retry_confirm.h" +#include "scene/lfrfid_app_scene_exit_confirm.h" +#include "scene/lfrfid_app_scene_read_menu.h" +#include "scene/lfrfid_app_scene_write.h" +#include "scene/lfrfid_app_scene_write_success.h" +#include "scene/lfrfid_app_scene_emulate.h" +#include "scene/lfrfid_app_scene_save_name.h" +#include "scene/lfrfid_app_scene_save_success.h" +#include "scene/lfrfid_app_scene_select_key.h" +#include "scene/lfrfid_app_scene_saved_key_menu.h" +#include "scene/lfrfid_app_scene_save_data.h" +#include "scene/lfrfid_app_scene_save_type.h" +#include "scene/lfrfid_app_scene_saved_info.h" +#include "scene/lfrfid_app_scene_delete_confirm.h" +#include "scene/lfrfid_app_scene_delete_success.h" +#include "scene/lfrfid_app_scene_emu_menu.h" +#include "scene/lfrfid_app_scene_emu_data.h" + +#include +#include + +const char* LfRfidApp::app_folder = "/any/lfrfid"; +const char* LfRfidApp::app_extension = ".rfid"; +const char* LfRfidApp::app_filetype = "Flipper RFID key"; + +LfRfidApp::LfRfidApp() + : scene_controller{this} + , notification{"notification"} + , storage{"storage"} + , dialogs{"dialogs"} + , text_store(40) { + string_init_set_str(file_path, app_folder); +} + +LfRfidApp::~LfRfidApp() { + string_clear(file_path); +} + +void LfRfidApp::run(void* _args) { + const char* args = reinterpret_cast(_args); + + make_app_folder(); + + if(strlen(args)) { + string_set_str(file_path, args); + load_key_data(file_path, &worker.key); + scene_controller.add_scene(SceneType::Emulate, new LfRfidAppSceneEmulate()); + scene_controller.process(100, SceneType::Emulate); + } else { + scene_controller.add_scene(SceneType::Start, new LfRfidAppSceneStart()); + scene_controller.add_scene(SceneType::Read, new LfRfidAppSceneRead()); + scene_controller.add_scene(SceneType::RetryConfirm, new LfRfidAppSceneRetryConfirm()); + scene_controller.add_scene(SceneType::ExitConfirm, new LfRfidAppSceneExitConfirm()); + scene_controller.add_scene(SceneType::ReadSuccess, new LfRfidAppSceneReadSuccess()); + scene_controller.add_scene(SceneType::ReadKeyMenu, new LfRfidAppSceneReadKeyMenu()); + scene_controller.add_scene(SceneType::Write, new LfRfidAppSceneWrite()); + scene_controller.add_scene(SceneType::WriteSuccess, new LfRfidAppSceneWriteSuccess()); + scene_controller.add_scene(SceneType::Emulate, new LfRfidAppSceneEmulate()); + scene_controller.add_scene(SceneType::SaveName, new LfRfidAppSceneSaveName()); + scene_controller.add_scene(SceneType::SaveSuccess, new LfRfidAppSceneSaveSuccess()); + scene_controller.add_scene(SceneType::SelectKey, new LfRfidAppSceneSelectKey()); + scene_controller.add_scene(SceneType::SavedKeyMenu, new LfRfidAppSceneSavedKeyMenu()); + scene_controller.add_scene(SceneType::SaveData, new LfRfidAppSceneSaveData()); + scene_controller.add_scene(SceneType::SaveType, new LfRfidAppSceneSaveType()); + scene_controller.add_scene(SceneType::SavedInfo, new LfRfidAppSceneSavedInfo()); + scene_controller.add_scene(SceneType::DeleteConfirm, new LfRfidAppSceneDeleteConfirm()); + scene_controller.add_scene(SceneType::DeleteSuccess, new LfRfidAppSceneDeleteSuccess()); + scene_controller.add_scene(SceneType::EmuMenu, new LfRfidAppSceneEmuMenu()); + scene_controller.add_scene(SceneType::EmuData, new LfRfidAppSceneEmuData()); + scene_controller.process(100); + } +} + +bool LfRfidApp::save_key(RfidKey* key) { + bool result = false; + + make_app_folder(); + + if(string_end_with_str_p(file_path, app_extension)) { + size_t filename_start = string_search_rchar(file_path, '/'); + string_left(file_path, filename_start); + } + + string_cat_printf(file_path, "/%s%s", key->get_name(), app_extension); + + result = save_key_data(file_path, key); + return result; +} + +bool LfRfidApp::load_key_from_file_select(bool need_restore) { + if(!need_restore) { + string_set_str(file_path, app_folder); + } + + bool result = dialog_file_browser_show( + dialogs, file_path, file_path, app_extension, true, &I_125_10px, true); + + if(result) { + result = load_key_data(file_path, &worker.key); + } + + return result; +} + +bool LfRfidApp::delete_key(RfidKey* key) { + UNUSED(key); + return storage_simply_remove(storage, string_get_cstr(file_path)); +} + +bool LfRfidApp::load_key_data(string_t path, RfidKey* key) { + FlipperFormat* file = flipper_format_file_alloc(storage); + bool result = false; + string_t str_result; + string_init(str_result); + + do { + if(!flipper_format_file_open_existing(file, string_get_cstr(path))) break; + + // header + uint32_t version; + if(!flipper_format_read_header(file, str_result, &version)) break; + if(string_cmp_str(str_result, app_filetype) != 0) break; + if(version != 1) break; + + // key type + LfrfidKeyType type; + RfidKey loaded_key; + + if(!flipper_format_read_string(file, "Key type", str_result)) break; + if(!lfrfid_key_get_string_type(string_get_cstr(str_result), &type)) break; + loaded_key.set_type(type); + + // key data + uint8_t key_data[loaded_key.get_type_data_count()] = {}; + if(!flipper_format_read_hex(file, "Data", key_data, loaded_key.get_type_data_count())) + break; + loaded_key.set_data(key_data, loaded_key.get_type_data_count()); + + path_extract_filename(path, str_result, true); + loaded_key.set_name(string_get_cstr(str_result)); + + *key = loaded_key; + result = true; + } while(0); + + flipper_format_free(file); + string_clear(str_result); + + if(!result) { + dialog_message_show_storage_error(dialogs, "Cannot load\nkey file"); + } + + return result; +} + +bool LfRfidApp::save_key_data(string_t path, RfidKey* key) { + FlipperFormat* file = flipper_format_file_alloc(storage); + bool result = false; + + do { + if(!flipper_format_file_open_always(file, string_get_cstr(path))) break; + if(!flipper_format_write_header_cstr(file, app_filetype, 1)) break; + if(!flipper_format_write_comment_cstr(file, "Key type can be EM4100, H10301 or I40134")) + break; + if(!flipper_format_write_string_cstr( + file, "Key type", lfrfid_key_get_type_string(key->get_type()))) + break; + if(!flipper_format_write_comment_cstr( + file, "Data size for EM4100 is 5, for H10301 is 3, for I40134 is 3")) + break; + if(!flipper_format_write_hex(file, "Data", key->get_data(), key->get_type_data_count())) + break; + result = true; + } while(0); + + flipper_format_free(file); + + if(!result) { + dialog_message_show_storage_error(dialogs, "Cannot save\nkey file"); + } + + return result; +} + +void LfRfidApp::make_app_folder() { + if(!storage_simply_mkdir(storage, app_folder)) { + dialog_message_show_storage_error(dialogs, "Cannot create\napp folder"); + } +} \ No newline at end of file diff --git a/applications/lfrfid/lfrfid_app.h b/applications/lfrfid/lfrfid_app.h index 3372552fc8d..9e95448805f 100644 --- a/applications/lfrfid/lfrfid_app.h +++ b/applications/lfrfid/lfrfid_app.h @@ -1,103 +1,98 @@ -#pragma once -#include "m-string.h" -#include -#include - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include "view/container_vm.h" - -#include -#include -#include - -#include "helpers/rfid_worker.h" -#include "rpc/rpc_app.h" - -class LfRfidApp { -public: - enum class EventType : uint8_t { - GENERIC_EVENT_ENUM_VALUES, - Next, - MenuSelected, - Stay, - Retry, - Exit, - EmulateStart, - }; - - enum class SceneType : uint8_t { - GENERIC_SCENE_ENUM_VALUES, - Read, - ReadSuccess, - RetryConfirm, - ExitConfirm, - ReadKeyMenu, - Write, - WriteSuccess, - Emulate, - SaveName, - SaveSuccess, - SelectKey, - SavedKeyMenu, - SaveData, - SaveType, - SavedInfo, - DeleteConfirm, - DeleteSuccess, - Rpc, - }; - - class Event { - public: - union { - int32_t menu_index; - } payload; - - EventType type; - }; - - SceneController, LfRfidApp> scene_controller; - ViewController - view_controller; - - ~LfRfidApp(); - LfRfidApp(); - - RecordController notification; - RecordController storage; - RecordController dialogs; - - RfidWorker worker; - - TextStore text_store; - - string_t file_path; - - RpcAppSystem* rpc_ctx; - - void run(void* args); - - static const char* app_folder; - static const char* app_extension; - static const char* app_filetype; - - bool save_key(RfidKey* key); - bool load_key_from_file_select(bool need_restore); - bool delete_key(RfidKey* key); - - bool load_key_data(string_t path, RfidKey* key, bool show_dialog); - bool save_key_data(string_t path, RfidKey* key); - - void make_app_folder(); - //bool rpc_command_callback(RpcAppSystemEvent event, const char* arg, void* context); -}; +#pragma once +#include "m-string.h" +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include "view/container_vm.h" + +#include +#include +#include + +#include "helpers/rfid_worker.h" + +class LfRfidApp { +public: + enum class EventType : uint8_t { + GENERIC_EVENT_ENUM_VALUES, + Next, + MenuSelected, + Stay, + Retry, + }; + + enum class SceneType : uint8_t { + GENERIC_SCENE_ENUM_VALUES, + Read, + ReadSuccess, + RetryConfirm, + ExitConfirm, + ReadKeyMenu, + Write, + WriteSuccess, + Emulate, + SaveName, + SaveSuccess, + SelectKey, + SavedKeyMenu, + SaveData, + SaveType, + SavedInfo, + DeleteConfirm, + DeleteSuccess, + EmuMenu, + EmuData, + }; + + class Event { + public: + union { + int32_t menu_index; + } payload; + + EventType type; + }; + + SceneController, LfRfidApp> scene_controller; + ViewController + view_controller; + + ~LfRfidApp(); + LfRfidApp(); + + RecordController notification; + RecordController storage; + RecordController dialogs; + + RfidWorker worker; + + TextStore text_store; + + string_t file_path; + + void run(void* args); + + static const char* app_folder; + static const char* app_extension; + static const char* app_filetype; + + bool save_key(RfidKey* key); + bool load_key_from_file_select(bool need_restore); + bool delete_key(RfidKey* key); + + bool load_key_data(string_t path, RfidKey* key); + bool save_key_data(string_t path, RfidKey* key); + + void make_app_folder(); +}; \ No newline at end of file From 44bab7dbf8252a57d4703b36a035ba3396ac27fc Mon Sep 17 00:00:00 2001 From: atomsmasha <37353991+atomsmasha@users.noreply.github.com> Date: Tue, 5 Jul 2022 14:18:40 +0800 Subject: [PATCH 02/11] LFRFID - Direct Emulation Adding a new root menu item to the 125 kHz/LFRFID app - "Direct Emulation" - New scenes to support the feature - Adding new menu item to lfrfid_app_scene_start.cpp --- .../scene/lfrfid_app_scene_emu_data.cpp | 37 ++++++ .../lfrfid/scene/lfrfid_app_scene_emu_data.h | 23 ++++ .../scene/lfrfid_app_scene_emu_menu.cpp | 53 ++++++++ .../lfrfid/scene/lfrfid_app_scene_emu_menu.h | 15 +++ .../lfrfid/scene/lfrfid_app_scene_start.cpp | 114 +++++++++--------- 5 files changed, 182 insertions(+), 60 deletions(-) create mode 100644 applications/lfrfid/scene/lfrfid_app_scene_emu_data.cpp create mode 100644 applications/lfrfid/scene/lfrfid_app_scene_emu_data.h create mode 100644 applications/lfrfid/scene/lfrfid_app_scene_emu_menu.cpp create mode 100644 applications/lfrfid/scene/lfrfid_app_scene_emu_menu.h diff --git a/applications/lfrfid/scene/lfrfid_app_scene_emu_data.cpp b/applications/lfrfid/scene/lfrfid_app_scene_emu_data.cpp new file mode 100644 index 00000000000..804d07294b8 --- /dev/null +++ b/applications/lfrfid/scene/lfrfid_app_scene_emu_data.cpp @@ -0,0 +1,37 @@ +#include "lfrfid_app_scene_emu_data.h" +#include + +void LfRfidAppSceneEmuData::on_enter(LfRfidApp* app, bool /*need_restore*/) { + auto byte_input = app->view_controller.get(); + + byte_input->set_header_text("Enter the data in hex"); + + byte_input->set_result_callback( + save_callback, NULL, app, key_data, app->worker.key.get_type_data_count()); + + app->view_controller.switch_to(); +} + +bool LfRfidAppSceneEmuData::on_event(LfRfidApp* app, LfRfidApp::Event* event) { + bool consumed = false; + RfidKey& key = app->worker.key; + + if(event->type == LfRfidApp::EventType::Next) { + key.set_data(key_data, key.get_type_data_count()); + DOLPHIN_DEED(DolphinDeedRfidAdd); + app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::Emulate); + } + + return consumed; +} + +void LfRfidAppSceneEmuData::on_exit(LfRfidApp* app) { + app->view_controller.get()->clean(); +} + +void LfRfidAppSceneEmuData::save_callback(void* context) { + LfRfidApp* app = static_cast(context); + LfRfidApp::Event event; + event.type = LfRfidApp::EventType::Next; + app->view_controller.send_event(&event); +} \ No newline at end of file diff --git a/applications/lfrfid/scene/lfrfid_app_scene_emu_data.h b/applications/lfrfid/scene/lfrfid_app_scene_emu_data.h new file mode 100644 index 00000000000..4d1312d1a57 --- /dev/null +++ b/applications/lfrfid/scene/lfrfid_app_scene_emu_data.h @@ -0,0 +1,23 @@ +#pragma once +#include "../lfrfid_app.h" + +class LfRfidAppSceneEmuData : public GenericScene { +public: + void on_enter(LfRfidApp* app, bool /*need_restore*/) final; + bool on_event(LfRfidApp* app, LfRfidApp::Event* event) final; + void on_exit(LfRfidApp* app) final; + +private: + static void save_callback(void* context); + + uint8_t key_data[LFRFID_KEY_SIZE] = { + 0xBB, + 0xBB, + 0xBB, + 0xBB, + 0xBB, + 0xBB, + 0xBB, + 0xBB, + }; +}; \ No newline at end of file diff --git a/applications/lfrfid/scene/lfrfid_app_scene_emu_menu.cpp b/applications/lfrfid/scene/lfrfid_app_scene_emu_menu.cpp new file mode 100644 index 00000000000..3983eba69b6 --- /dev/null +++ b/applications/lfrfid/scene/lfrfid_app_scene_emu_menu.cpp @@ -0,0 +1,53 @@ +#include "lfrfid_app_scene_emu_menu.h" + +void LfRfidAppSceneEmuMenu::on_enter(LfRfidApp* app, bool need_restore) { + auto submenu = app->view_controller.get(); + + for(uint8_t i = 0; i <= keys_count; i++) { + string_init_printf( + submenu_name[i], + "%s %s", + lfrfid_key_get_manufacturer_string(static_cast(i)), + lfrfid_key_get_type_string(static_cast(i))); + submenu->add_item(string_get_cstr(submenu_name[i]), i, submenu_callback, app); + } + + if(need_restore) { + submenu->set_selected_item(submenu_item_selected); + } + + app->view_controller.switch_to(); + + // clear key name + app->worker.key.set_name(""); +} + +bool LfRfidAppSceneEmuMenu::on_event(LfRfidApp* app, LfRfidApp::Event* event) { + bool consumed = false; + + if(event->type == LfRfidApp::EventType::MenuSelected) { + submenu_item_selected = event->payload.menu_index; + app->worker.key.set_type(static_cast(event->payload.menu_index)); + app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::EmuData); // edit here + consumed = true; + } + + return consumed; +} + +void LfRfidAppSceneEmuMenu::on_exit(LfRfidApp* app) { + app->view_controller.get()->clean(); + for(uint8_t i = 0; i <= keys_count; i++) { + string_clear(submenu_name[i]); + } +} + +void LfRfidAppSceneEmuMenu::submenu_callback(void* context, uint32_t index) { + LfRfidApp* app = static_cast(context); + LfRfidApp::Event event; + + event.type = LfRfidApp::EventType::MenuSelected; + event.payload.menu_index = index; + + app->view_controller.send_event(&event); +} \ No newline at end of file diff --git a/applications/lfrfid/scene/lfrfid_app_scene_emu_menu.h b/applications/lfrfid/scene/lfrfid_app_scene_emu_menu.h new file mode 100644 index 00000000000..8d9e63793cf --- /dev/null +++ b/applications/lfrfid/scene/lfrfid_app_scene_emu_menu.h @@ -0,0 +1,15 @@ +#pragma once +#include "../lfrfid_app.h" + +class LfRfidAppSceneEmuMenu : public GenericScene { +public: + void on_enter(LfRfidApp* app, bool need_restore) final; + bool on_event(LfRfidApp* app, LfRfidApp::Event* event) final; + void on_exit(LfRfidApp* app) final; + +private: + static void submenu_callback(void* context, uint32_t index); + uint32_t submenu_item_selected = 0; + static const uint8_t keys_count = static_cast(LfrfidKeyType::KeyIoProxXSF); + string_t submenu_name[keys_count + 1]; +}; \ No newline at end of file diff --git a/applications/lfrfid/scene/lfrfid_app_scene_start.cpp b/applications/lfrfid/scene/lfrfid_app_scene_start.cpp index f5afad5c988..13a99c3385f 100644 --- a/applications/lfrfid/scene/lfrfid_app_scene_start.cpp +++ b/applications/lfrfid/scene/lfrfid_app_scene_start.cpp @@ -1,60 +1,54 @@ -#include "lfrfid_app_scene_start.h" - -typedef enum { - SubmenuRead, - SubmenuSaved, - SubmenuAddManually, -} SubmenuIndex; - -void LfRfidAppSceneStart::on_enter(LfRfidApp* app, bool need_restore) { - auto submenu = app->view_controller.get(); - - submenu->add_item("Read", SubmenuRead, submenu_callback, app); - submenu->add_item("Saved", SubmenuSaved, submenu_callback, app); - submenu->add_item("Add Manually", SubmenuAddManually, submenu_callback, app); - - if(need_restore) { - submenu->set_selected_item(submenu_item_selected); - } - - app->view_controller.switch_to(); - - // clear key - app->worker.key.clear(); -} - -bool LfRfidAppSceneStart::on_event(LfRfidApp* app, LfRfidApp::Event* event) { - bool consumed = false; - - if(event->type == LfRfidApp::EventType::MenuSelected) { - submenu_item_selected = event->payload.menu_index; - switch(event->payload.menu_index) { - case SubmenuRead: - app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::Read); - break; - case SubmenuSaved: - app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::SelectKey); - break; - case SubmenuAddManually: - app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::SaveType); - break; - } - consumed = true; - } - - return consumed; -} - -void LfRfidAppSceneStart::on_exit(LfRfidApp* app) { - app->view_controller.get()->clean(); -} - -void LfRfidAppSceneStart::submenu_callback(void* context, uint32_t index) { - LfRfidApp* app = static_cast(context); - LfRfidApp::Event event; - - event.type = LfRfidApp::EventType::MenuSelected; - event.payload.menu_index = index; - - app->view_controller.send_event(&event); -} +#include "lfrfid_app_scene_start.h" +typedef enum { + SubmenuRead, + SubmenuSaved, + SubmenuAddManually, + SubMenuEmulateDirectly, +} SubmenuIndex; + +void LfRfidAppSceneStart::on_enter(LfRfidApp* app, bool need_restore) { + auto submenu = app->view_controller.get(); + submenu->add_item("Read", SubmenuRead, submenu_callback, app); + submenu->add_item("Saved", SubmenuSaved, submenu_callback, app); + submenu->add_item("Add Manually", SubmenuAddManually, submenu_callback, app); + submenu->add_item("Emulate Directly", SubMenuEmulateDirectly, submenu_callback, app); + + if(need_restore) { + submenu->set_selected_item(submenu_item_selected); + } + app->view_controller.switch_to(); + // clear key + app->worker.key.clear(); +} +bool LfRfidAppSceneStart::on_event(LfRfidApp* app, LfRfidApp::Event* event) { + bool consumed = false; + if(event->type == LfRfidApp::EventType::MenuSelected) { + submenu_item_selected = event->payload.menu_index; + switch(event->payload.menu_index) { + case SubmenuRead: + app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::Read); + break; + case SubmenuSaved: + app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::SelectKey); + break; + case SubmenuAddManually: + app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::SaveType); + break; + case SubMenuEmulateDirectly: + app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::EmuMenu); + break; + } + consumed = true; + } + return consumed; +} +void LfRfidAppSceneStart::on_exit(LfRfidApp* app) { + app->view_controller.get()->clean(); +} +void LfRfidAppSceneStart::submenu_callback(void* context, uint32_t index) { + LfRfidApp* app = static_cast(context); + LfRfidApp::Event event; + event.type = LfRfidApp::EventType::MenuSelected; + event.payload.menu_index = index; + app->view_controller.send_event(&event); +} \ No newline at end of file From 6c68e13331f47b57c8e9e8a9c0ec6f5f06629cd6 Mon Sep 17 00:00:00 2001 From: atomsmasha <37353991+atomsmasha@users.noreply.github.com> Date: Tue, 5 Jul 2022 14:21:30 +0800 Subject: [PATCH 03/11] Update lfrfid_app.cpp adding newline to EOF --- applications/lfrfid/lfrfid_app.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/lfrfid/lfrfid_app.cpp b/applications/lfrfid/lfrfid_app.cpp index 98a099a8908..ba7c07b486b 100644 --- a/applications/lfrfid/lfrfid_app.cpp +++ b/applications/lfrfid/lfrfid_app.cpp @@ -193,4 +193,4 @@ void LfRfidApp::make_app_folder() { if(!storage_simply_mkdir(storage, app_folder)) { dialog_message_show_storage_error(dialogs, "Cannot create\napp folder"); } -} \ No newline at end of file +} From fa9dc9074e56149134d0e028e04e6025956fb97d Mon Sep 17 00:00:00 2001 From: atomsmasha <37353991+atomsmasha@users.noreply.github.com> Date: Tue, 5 Jul 2022 14:21:50 +0800 Subject: [PATCH 04/11] Update lfrfid_app.h adding newline to EOF --- applications/lfrfid/lfrfid_app.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/lfrfid/lfrfid_app.h b/applications/lfrfid/lfrfid_app.h index 9e95448805f..a4f4d5915d1 100644 --- a/applications/lfrfid/lfrfid_app.h +++ b/applications/lfrfid/lfrfid_app.h @@ -95,4 +95,4 @@ class LfRfidApp { bool save_key_data(string_t path, RfidKey* key); void make_app_folder(); -}; \ No newline at end of file +}; From 0d3960dbc2b3530ca26f6c6730b35992011702aa Mon Sep 17 00:00:00 2001 From: atomsmasha <37353991+atomsmasha@users.noreply.github.com> Date: Tue, 5 Jul 2022 14:22:12 +0800 Subject: [PATCH 05/11] Update lfrfid_app_scene_emu_data.cpp adding newline to EOF --- applications/lfrfid/scene/lfrfid_app_scene_emu_data.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/lfrfid/scene/lfrfid_app_scene_emu_data.cpp b/applications/lfrfid/scene/lfrfid_app_scene_emu_data.cpp index 804d07294b8..88901a95870 100644 --- a/applications/lfrfid/scene/lfrfid_app_scene_emu_data.cpp +++ b/applications/lfrfid/scene/lfrfid_app_scene_emu_data.cpp @@ -34,4 +34,4 @@ void LfRfidAppSceneEmuData::save_callback(void* context) { LfRfidApp::Event event; event.type = LfRfidApp::EventType::Next; app->view_controller.send_event(&event); -} \ No newline at end of file +} From f1325a82d70e0c6c37665c8b8423c015860cae0f Mon Sep 17 00:00:00 2001 From: atomsmasha <37353991+atomsmasha@users.noreply.github.com> Date: Tue, 5 Jul 2022 14:22:23 +0800 Subject: [PATCH 06/11] Update lfrfid_app_scene_emu_data.h adding newline to EOF --- applications/lfrfid/scene/lfrfid_app_scene_emu_data.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/lfrfid/scene/lfrfid_app_scene_emu_data.h b/applications/lfrfid/scene/lfrfid_app_scene_emu_data.h index 4d1312d1a57..3034d5ff289 100644 --- a/applications/lfrfid/scene/lfrfid_app_scene_emu_data.h +++ b/applications/lfrfid/scene/lfrfid_app_scene_emu_data.h @@ -20,4 +20,4 @@ class LfRfidAppSceneEmuData : public GenericScene { 0xBB, 0xBB, }; -}; \ No newline at end of file +}; From d19f0f7120977b420a5ae88d40c32c3e03c4e318 Mon Sep 17 00:00:00 2001 From: atomsmasha <37353991+atomsmasha@users.noreply.github.com> Date: Tue, 5 Jul 2022 14:22:31 +0800 Subject: [PATCH 07/11] Update lfrfid_app_scene_emu_menu.cpp adding newline to EOF --- applications/lfrfid/scene/lfrfid_app_scene_emu_menu.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/lfrfid/scene/lfrfid_app_scene_emu_menu.cpp b/applications/lfrfid/scene/lfrfid_app_scene_emu_menu.cpp index 3983eba69b6..47114a72fe0 100644 --- a/applications/lfrfid/scene/lfrfid_app_scene_emu_menu.cpp +++ b/applications/lfrfid/scene/lfrfid_app_scene_emu_menu.cpp @@ -50,4 +50,4 @@ void LfRfidAppSceneEmuMenu::submenu_callback(void* context, uint32_t index) { event.payload.menu_index = index; app->view_controller.send_event(&event); -} \ No newline at end of file +} From f91e773fb27fef6005ebae9cd09cd70c8e83bfa3 Mon Sep 17 00:00:00 2001 From: atomsmasha <37353991+atomsmasha@users.noreply.github.com> Date: Tue, 5 Jul 2022 14:22:42 +0800 Subject: [PATCH 08/11] Update lfrfid_app_scene_emu_menu.h adding newline to EOF --- applications/lfrfid/scene/lfrfid_app_scene_emu_menu.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/lfrfid/scene/lfrfid_app_scene_emu_menu.h b/applications/lfrfid/scene/lfrfid_app_scene_emu_menu.h index 8d9e63793cf..cb675535480 100644 --- a/applications/lfrfid/scene/lfrfid_app_scene_emu_menu.h +++ b/applications/lfrfid/scene/lfrfid_app_scene_emu_menu.h @@ -12,4 +12,4 @@ class LfRfidAppSceneEmuMenu : public GenericScene { uint32_t submenu_item_selected = 0; static const uint8_t keys_count = static_cast(LfrfidKeyType::KeyIoProxXSF); string_t submenu_name[keys_count + 1]; -}; \ No newline at end of file +}; From 4267d5478f5fdc0e8dea62b64bd3a8926e25497a Mon Sep 17 00:00:00 2001 From: atomsmasha <37353991+atomsmasha@users.noreply.github.com> Date: Tue, 5 Jul 2022 14:22:54 +0800 Subject: [PATCH 09/11] Update lfrfid_app_scene_start.cpp adding newline to EOF --- applications/lfrfid/scene/lfrfid_app_scene_start.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/lfrfid/scene/lfrfid_app_scene_start.cpp b/applications/lfrfid/scene/lfrfid_app_scene_start.cpp index 13a99c3385f..97bbfabecd3 100644 --- a/applications/lfrfid/scene/lfrfid_app_scene_start.cpp +++ b/applications/lfrfid/scene/lfrfid_app_scene_start.cpp @@ -51,4 +51,4 @@ void LfRfidAppSceneStart::submenu_callback(void* context, uint32_t index) { event.type = LfRfidApp::EventType::MenuSelected; event.payload.menu_index = index; app->view_controller.send_event(&event); -} \ No newline at end of file +} From 3c5b08fefafc1b9a8dd59f2920594d3392a4590d Mon Sep 17 00:00:00 2001 From: atomsmasha <37353991+atomsmasha@users.noreply.github.com> Date: Tue, 5 Jul 2022 19:53:24 +0800 Subject: [PATCH 10/11] Update lfrfid_app.h merging in my changes - didn't see that dev branch had been updated by @skotopes recently --- applications/lfrfid/lfrfid_app.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/applications/lfrfid/lfrfid_app.h b/applications/lfrfid/lfrfid_app.h index a4f4d5915d1..b733de5cab8 100644 --- a/applications/lfrfid/lfrfid_app.h +++ b/applications/lfrfid/lfrfid_app.h @@ -21,6 +21,7 @@ #include #include "helpers/rfid_worker.h" +#include "rpc/rpc_app.h" class LfRfidApp { public: @@ -30,6 +31,8 @@ class LfRfidApp { MenuSelected, Stay, Retry, + Exit, + EmulateStart, }; enum class SceneType : uint8_t { @@ -51,6 +54,7 @@ class LfRfidApp { SavedInfo, DeleteConfirm, DeleteSuccess, + Rpc, EmuMenu, EmuData, }; @@ -81,6 +85,8 @@ class LfRfidApp { string_t file_path; + RpcAppSystem* rpc_ctx; + void run(void* args); static const char* app_folder; @@ -91,8 +97,9 @@ class LfRfidApp { bool load_key_from_file_select(bool need_restore); bool delete_key(RfidKey* key); - bool load_key_data(string_t path, RfidKey* key); + bool load_key_data(string_t path, RfidKey* key, bool show_dialog); bool save_key_data(string_t path, RfidKey* key); void make_app_folder(); + //bool rpc_command_callback(RpcAppSystemEvent event, const char* arg, void* context); }; From 3c923e484c54a695c333e2db22d5920777fc0cac Mon Sep 17 00:00:00 2001 From: atomsmasha <37353991+atomsmasha@users.noreply.github.com> Date: Tue, 5 Jul 2022 19:55:24 +0800 Subject: [PATCH 11/11] LFRFID - Direct Emu - merging in changes after #1350 merging in my changes - didn't see that dev branch had been updated by @skotopes recently --- applications/lfrfid/lfrfid_app.cpp | 63 ++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/applications/lfrfid/lfrfid_app.cpp b/applications/lfrfid/lfrfid_app.cpp index ba7c07b486b..cf5d1263617 100644 --- a/applications/lfrfid/lfrfid_app.cpp +++ b/applications/lfrfid/lfrfid_app.cpp @@ -20,12 +20,15 @@ #include "scene/lfrfid_app_scene_saved_info.h" #include "scene/lfrfid_app_scene_delete_confirm.h" #include "scene/lfrfid_app_scene_delete_success.h" +#include "scene/lfrfid_app_scene_rpc.h" #include "scene/lfrfid_app_scene_emu_menu.h" #include "scene/lfrfid_app_scene_emu_data.h" #include #include +#include "rpc/rpc_app.h" + const char* LfRfidApp::app_folder = "/any/lfrfid"; const char* LfRfidApp::app_extension = ".rfid"; const char* LfRfidApp::app_filetype = "Flipper RFID key"; @@ -41,6 +44,43 @@ LfRfidApp::LfRfidApp() LfRfidApp::~LfRfidApp() { string_clear(file_path); + if(rpc_ctx) { + rpc_system_app_set_callback(rpc_ctx, NULL, NULL); + } +} + +static bool rpc_command_callback(RpcAppSystemEvent event, const char* arg, void* context) { + furi_assert(context); + LfRfidApp* app = static_cast(context); + + bool result = false; + + if(event == RpcAppEventSessionClose) { + rpc_system_app_set_callback(app->rpc_ctx, NULL, NULL); + app->rpc_ctx = NULL; + LfRfidApp::Event event; + event.type = LfRfidApp::EventType::Exit; + app->view_controller.send_event(&event); + result = true; + } else if(event == RpcAppEventAppExit) { + LfRfidApp::Event event; + event.type = LfRfidApp::EventType::Exit; + app->view_controller.send_event(&event); + result = true; + } else if(event == RpcAppEventLoadFile) { + if(arg) { + string_set_str(app->file_path, arg); + if(app->load_key_data(app->file_path, &(app->worker.key), false)) { + LfRfidApp::Event event; + event.type = LfRfidApp::EventType::EmulateStart; + app->view_controller.send_event(&event); + app->worker.start_emulate(); + result = true; + } + } + } + + return result; } void LfRfidApp::run(void* _args) { @@ -49,10 +89,19 @@ void LfRfidApp::run(void* _args) { make_app_folder(); if(strlen(args)) { - string_set_str(file_path, args); - load_key_data(file_path, &worker.key); - scene_controller.add_scene(SceneType::Emulate, new LfRfidAppSceneEmulate()); - scene_controller.process(100, SceneType::Emulate); + uint32_t rpc_ctx_ptr = 0; + if(sscanf(args, "RPC %lX", &rpc_ctx_ptr) == 1) { + rpc_ctx = (RpcAppSystem*)rpc_ctx_ptr; + rpc_system_app_set_callback(rpc_ctx, rpc_command_callback, this); + scene_controller.add_scene(SceneType::Rpc, new LfRfidAppSceneRpc()); + scene_controller.process(100, SceneType::Rpc); + } else { + string_set_str(file_path, args); + load_key_data(file_path, &worker.key, true); + scene_controller.add_scene(SceneType::Emulate, new LfRfidAppSceneEmulate()); + scene_controller.process(100, SceneType::Emulate); + } + } else { scene_controller.add_scene(SceneType::Start, new LfRfidAppSceneStart()); scene_controller.add_scene(SceneType::Read, new LfRfidAppSceneRead()); @@ -103,7 +152,7 @@ bool LfRfidApp::load_key_from_file_select(bool need_restore) { dialogs, file_path, file_path, app_extension, true, &I_125_10px, true); if(result) { - result = load_key_data(file_path, &worker.key); + result = load_key_data(file_path, &worker.key, true); } return result; @@ -114,7 +163,7 @@ bool LfRfidApp::delete_key(RfidKey* key) { return storage_simply_remove(storage, string_get_cstr(file_path)); } -bool LfRfidApp::load_key_data(string_t path, RfidKey* key) { +bool LfRfidApp::load_key_data(string_t path, RfidKey* key, bool show_dialog) { FlipperFormat* file = flipper_format_file_alloc(storage); bool result = false; string_t str_result; @@ -153,7 +202,7 @@ bool LfRfidApp::load_key_data(string_t path, RfidKey* key) { flipper_format_free(file); string_clear(str_result); - if(!result) { + if((!result) && (show_dialog)) { dialog_message_show_storage_error(dialogs, "Cannot load\nkey file"); }