lfrfid.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include "lfrfid_i.h"
  2. #include <dolphin/dolphin.h>
  3. static bool lfrfid_debug_custom_event_callback(void* context, uint32_t event) {
  4. furi_assert(context);
  5. LfRfid* app = context;
  6. return scene_manager_handle_custom_event(app->scene_manager, event);
  7. }
  8. static bool lfrfid_debug_back_event_callback(void* context) {
  9. furi_assert(context);
  10. LfRfid* app = context;
  11. return scene_manager_handle_back_event(app->scene_manager);
  12. }
  13. static void rpc_command_callback(RpcAppSystemEvent rpc_event, void* context) {
  14. furi_assert(context);
  15. LfRfid* app = (LfRfid*)context;
  16. if(rpc_event == RpcAppEventSessionClose) {
  17. view_dispatcher_send_custom_event(app->view_dispatcher, LfRfidEventRpcSessionClose);
  18. // Detach RPC
  19. rpc_system_app_set_callback(app->rpc_ctx, NULL, NULL);
  20. app->rpc_ctx = NULL;
  21. } else if(rpc_event == RpcAppEventAppExit) {
  22. view_dispatcher_send_custom_event(app->view_dispatcher, LfRfidEventExit);
  23. } else if(rpc_event == RpcAppEventLoadFile) {
  24. view_dispatcher_send_custom_event(app->view_dispatcher, LfRfidEventRpcLoadFile);
  25. } else {
  26. rpc_system_app_confirm(app->rpc_ctx, rpc_event, false);
  27. }
  28. }
  29. static LfRfid* lfrfid_alloc() {
  30. LfRfid* lfrfid = malloc(sizeof(LfRfid));
  31. lfrfid->storage = furi_record_open(RECORD_STORAGE);
  32. lfrfid->dialogs = furi_record_open(RECORD_DIALOGS);
  33. lfrfid->file_name = furi_string_alloc();
  34. lfrfid->raw_file_name = furi_string_alloc();
  35. lfrfid->file_path = furi_string_alloc_set(LFRFID_APP_FOLDER);
  36. lfrfid->dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
  37. size_t size = protocol_dict_get_max_data_size(lfrfid->dict);
  38. lfrfid->new_key_data = (uint8_t*)malloc(size);
  39. lfrfid->old_key_data = (uint8_t*)malloc(size);
  40. lfrfid->lfworker = lfrfid_worker_alloc(lfrfid->dict);
  41. lfrfid->view_dispatcher = view_dispatcher_alloc();
  42. lfrfid->scene_manager = scene_manager_alloc(&lfrfid_scene_handlers, lfrfid);
  43. view_dispatcher_enable_queue(lfrfid->view_dispatcher);
  44. view_dispatcher_set_event_callback_context(lfrfid->view_dispatcher, lfrfid);
  45. view_dispatcher_set_custom_event_callback(
  46. lfrfid->view_dispatcher, lfrfid_debug_custom_event_callback);
  47. view_dispatcher_set_navigation_event_callback(
  48. lfrfid->view_dispatcher, lfrfid_debug_back_event_callback);
  49. // Open GUI record
  50. lfrfid->gui = furi_record_open(RECORD_GUI);
  51. // Open Notification record
  52. lfrfid->notifications = furi_record_open(RECORD_NOTIFICATION);
  53. // Submenu
  54. lfrfid->submenu = submenu_alloc();
  55. view_dispatcher_add_view(
  56. lfrfid->view_dispatcher, LfRfidViewSubmenu, submenu_get_view(lfrfid->submenu));
  57. // Dialog
  58. lfrfid->dialog_ex = dialog_ex_alloc();
  59. view_dispatcher_add_view(
  60. lfrfid->view_dispatcher, LfRfidViewDialogEx, dialog_ex_get_view(lfrfid->dialog_ex));
  61. // Popup
  62. lfrfid->popup = popup_alloc();
  63. view_dispatcher_add_view(
  64. lfrfid->view_dispatcher, LfRfidViewPopup, popup_get_view(lfrfid->popup));
  65. // Widget
  66. lfrfid->widget = widget_alloc();
  67. view_dispatcher_add_view(
  68. lfrfid->view_dispatcher, LfRfidViewWidget, widget_get_view(lfrfid->widget));
  69. // Text Input
  70. lfrfid->text_input = text_input_alloc();
  71. view_dispatcher_add_view(
  72. lfrfid->view_dispatcher, LfRfidViewTextInput, text_input_get_view(lfrfid->text_input));
  73. // Byte Input
  74. lfrfid->byte_input = byte_input_alloc();
  75. view_dispatcher_add_view(
  76. lfrfid->view_dispatcher, LfRfidViewByteInput, byte_input_get_view(lfrfid->byte_input));
  77. // Read custom view
  78. lfrfid->read_view = lfrfid_view_read_alloc();
  79. view_dispatcher_add_view(
  80. lfrfid->view_dispatcher, LfRfidViewRead, lfrfid_view_read_get_view(lfrfid->read_view));
  81. return lfrfid;
  82. } //-V773
  83. static void lfrfid_free(LfRfid* lfrfid) {
  84. furi_assert(lfrfid);
  85. furi_string_free(lfrfid->raw_file_name);
  86. furi_string_free(lfrfid->file_name);
  87. furi_string_free(lfrfid->file_path);
  88. protocol_dict_free(lfrfid->dict);
  89. lfrfid_worker_free(lfrfid->lfworker);
  90. if(lfrfid->rpc_ctx) {
  91. rpc_system_app_set_callback(lfrfid->rpc_ctx, NULL, NULL);
  92. rpc_system_app_send_exited(lfrfid->rpc_ctx);
  93. }
  94. free(lfrfid->new_key_data);
  95. free(lfrfid->old_key_data);
  96. // Submenu
  97. view_dispatcher_remove_view(lfrfid->view_dispatcher, LfRfidViewSubmenu);
  98. submenu_free(lfrfid->submenu);
  99. // DialogEx
  100. view_dispatcher_remove_view(lfrfid->view_dispatcher, LfRfidViewDialogEx);
  101. dialog_ex_free(lfrfid->dialog_ex);
  102. // Popup
  103. view_dispatcher_remove_view(lfrfid->view_dispatcher, LfRfidViewPopup);
  104. popup_free(lfrfid->popup);
  105. // Widget
  106. view_dispatcher_remove_view(lfrfid->view_dispatcher, LfRfidViewWidget);
  107. widget_free(lfrfid->widget);
  108. // TextInput
  109. view_dispatcher_remove_view(lfrfid->view_dispatcher, LfRfidViewTextInput);
  110. text_input_free(lfrfid->text_input);
  111. // ByteInput
  112. view_dispatcher_remove_view(lfrfid->view_dispatcher, LfRfidViewByteInput);
  113. byte_input_free(lfrfid->byte_input);
  114. // Read custom view
  115. view_dispatcher_remove_view(lfrfid->view_dispatcher, LfRfidViewRead);
  116. lfrfid_view_read_free(lfrfid->read_view);
  117. // View Dispatcher
  118. view_dispatcher_free(lfrfid->view_dispatcher);
  119. // Scene Manager
  120. scene_manager_free(lfrfid->scene_manager);
  121. // GUI
  122. furi_record_close(RECORD_GUI);
  123. lfrfid->gui = NULL;
  124. // Notifications
  125. furi_record_close(RECORD_NOTIFICATION);
  126. lfrfid->notifications = NULL;
  127. furi_record_close(RECORD_STORAGE);
  128. furi_record_close(RECORD_DIALOGS);
  129. free(lfrfid);
  130. }
  131. int32_t lfrfid_app(void* p) {
  132. LfRfid* app = lfrfid_alloc();
  133. char* args = p;
  134. lfrfid_make_app_folder(app);
  135. if(args && strlen(args)) {
  136. uint32_t rpc_ctx_ptr = 0;
  137. if(sscanf(args, "RPC %lX", &rpc_ctx_ptr) == 1) {
  138. app->rpc_ctx = (RpcAppSystem*)rpc_ctx_ptr;
  139. rpc_system_app_set_callback(app->rpc_ctx, rpc_command_callback, app);
  140. rpc_system_app_send_started(app->rpc_ctx);
  141. view_dispatcher_attach_to_gui(
  142. app->view_dispatcher, app->gui, ViewDispatcherTypeDesktop);
  143. scene_manager_next_scene(app->scene_manager, LfRfidSceneRpc);
  144. DOLPHIN_DEED(DolphinDeedRfidEmulate);
  145. } else {
  146. furi_string_set(app->file_path, args);
  147. lfrfid_load_key_data(app, app->file_path, true);
  148. view_dispatcher_attach_to_gui(
  149. app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  150. scene_manager_next_scene(app->scene_manager, LfRfidSceneEmulate);
  151. DOLPHIN_DEED(DolphinDeedRfidEmulate);
  152. }
  153. } else {
  154. view_dispatcher_attach_to_gui(
  155. app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  156. scene_manager_next_scene(app->scene_manager, LfRfidSceneStart);
  157. }
  158. view_dispatcher_run(app->view_dispatcher);
  159. lfrfid_free(app);
  160. return 0;
  161. }
  162. bool lfrfid_save_key(LfRfid* app) {
  163. furi_assert(app);
  164. bool result = false;
  165. lfrfid_make_app_folder(app);
  166. if(furi_string_end_with(app->file_path, LFRFID_APP_EXTENSION)) {
  167. size_t filename_start = furi_string_search_rchar(app->file_path, '/');
  168. furi_string_left(app->file_path, filename_start);
  169. }
  170. furi_string_cat_printf(
  171. app->file_path, "/%s%s", furi_string_get_cstr(app->file_name), LFRFID_APP_EXTENSION);
  172. result = lfrfid_save_key_data(app, app->file_path);
  173. return result;
  174. }
  175. bool lfrfid_load_key_from_file_select(LfRfid* app) {
  176. furi_assert(app);
  177. DialogsFileBrowserOptions browser_options;
  178. dialog_file_browser_set_basic_options(&browser_options, LFRFID_APP_EXTENSION, &I_125_10px);
  179. browser_options.base_path = LFRFID_APP_FOLDER;
  180. // Input events and views are managed by file_browser
  181. bool result =
  182. dialog_file_browser_show(app->dialogs, app->file_path, app->file_path, &browser_options);
  183. if(result) {
  184. result = lfrfid_load_key_data(app, app->file_path, true);
  185. }
  186. return result;
  187. }
  188. bool lfrfid_delete_key(LfRfid* app) {
  189. furi_assert(app);
  190. return storage_simply_remove(app->storage, furi_string_get_cstr(app->file_path));
  191. }
  192. bool lfrfid_load_key_data(LfRfid* app, FuriString* path, bool show_dialog) {
  193. bool result = false;
  194. do {
  195. app->protocol_id = lfrfid_dict_file_load(app->dict, furi_string_get_cstr(path));
  196. if(app->protocol_id == PROTOCOL_NO) break;
  197. path_extract_filename(path, app->file_name, true);
  198. result = true;
  199. } while(0);
  200. if((!result) && (show_dialog)) {
  201. dialog_message_show_storage_error(app->dialogs, "Cannot load\nkey file");
  202. }
  203. return result;
  204. }
  205. bool lfrfid_save_key_data(LfRfid* app, FuriString* path) {
  206. bool result = lfrfid_dict_file_save(app->dict, app->protocol_id, furi_string_get_cstr(path));
  207. if(!result) {
  208. dialog_message_show_storage_error(app->dialogs, "Cannot save\nkey file");
  209. }
  210. return result;
  211. }
  212. void lfrfid_make_app_folder(LfRfid* app) {
  213. furi_assert(app);
  214. if(!storage_simply_mkdir(app->storage, LFRFID_APP_FOLDER)) {
  215. dialog_message_show_storage_error(app->dialogs, "Cannot create\napp folder");
  216. }
  217. }
  218. void lfrfid_text_store_set(LfRfid* app, const char* text, ...) {
  219. furi_assert(app);
  220. va_list args;
  221. va_start(args, text);
  222. vsnprintf(app->text_store, LFRFID_TEXT_STORE_SIZE, text, args);
  223. va_end(args);
  224. }
  225. void lfrfid_text_store_clear(LfRfid* app) {
  226. furi_assert(app);
  227. memset(app->text_store, 0, sizeof(app->text_store));
  228. }
  229. void lfrfid_popup_timeout_callback(void* context) {
  230. LfRfid* app = context;
  231. view_dispatcher_send_custom_event(app->view_dispatcher, LfRfidEventPopupClosed);
  232. }
  233. void lfrfid_widget_callback(GuiButtonType result, InputType type, void* context) {
  234. LfRfid* app = context;
  235. if(type == InputTypeShort) {
  236. view_dispatcher_send_custom_event(app->view_dispatcher, result);
  237. }
  238. }
  239. void lfrfid_text_input_callback(void* context) {
  240. LfRfid* app = context;
  241. view_dispatcher_send_custom_event(app->view_dispatcher, LfRfidEventNext);
  242. }