t5577_multiwriter.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "core/core_defines.h"
  2. #include "t5577_multiwriter_i.h"
  3. #include <dolphin/dolphin.h>
  4. static bool t5577_multiwriter_debug_custom_event_callback(void* context, uint32_t event) {
  5. furi_assert(context);
  6. LfRfid* app = context;
  7. return scene_manager_handle_custom_event(app->scene_manager, event);
  8. }
  9. static bool t5577_multiwriter_debug_back_event_callback(void* context) {
  10. furi_assert(context);
  11. LfRfid* app = context;
  12. return scene_manager_handle_back_event(app->scene_manager);
  13. }
  14. static LfRfid* t5577_multiwriter_alloc() {
  15. LfRfid* t5577_multiwriter = malloc(sizeof(LfRfid));
  16. t5577_multiwriter->storage = furi_record_open(RECORD_STORAGE);
  17. t5577_multiwriter->dialogs = furi_record_open(RECORD_DIALOGS);
  18. t5577_multiwriter->file_name = furi_string_alloc();
  19. t5577_multiwriter->file_path = furi_string_alloc_set(LFRFID_APP_FOLDER);
  20. t5577_multiwriter->dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
  21. size_t size = protocol_dict_get_max_data_size(t5577_multiwriter->dict);
  22. t5577_multiwriter->new_key_data = (uint8_t*)malloc(size);
  23. t5577_multiwriter->old_key_data = (uint8_t*)malloc(size);
  24. t5577_multiwriter->lfworker = lfrfid_worker_alloc(t5577_multiwriter->dict);
  25. t5577_multiwriter->view_dispatcher = view_dispatcher_alloc();
  26. t5577_multiwriter->scene_manager =
  27. scene_manager_alloc(&t5577_multiwriter_scene_handlers, t5577_multiwriter);
  28. view_dispatcher_enable_queue(t5577_multiwriter->view_dispatcher);
  29. view_dispatcher_set_event_callback_context(
  30. t5577_multiwriter->view_dispatcher, t5577_multiwriter);
  31. view_dispatcher_set_custom_event_callback(
  32. t5577_multiwriter->view_dispatcher, t5577_multiwriter_debug_custom_event_callback);
  33. view_dispatcher_set_navigation_event_callback(
  34. t5577_multiwriter->view_dispatcher, t5577_multiwriter_debug_back_event_callback);
  35. // Open GUI record
  36. t5577_multiwriter->gui = furi_record_open(RECORD_GUI);
  37. // Open Notification record
  38. t5577_multiwriter->notifications = furi_record_open(RECORD_NOTIFICATION);
  39. // Submenu
  40. t5577_multiwriter->submenu = submenu_alloc();
  41. view_dispatcher_add_view(
  42. t5577_multiwriter->view_dispatcher,
  43. LfRfidViewSubmenu,
  44. submenu_get_view(t5577_multiwriter->submenu));
  45. // Dialog
  46. t5577_multiwriter->dialog_ex = dialog_ex_alloc();
  47. view_dispatcher_add_view(
  48. t5577_multiwriter->view_dispatcher,
  49. LfRfidViewDialogEx,
  50. dialog_ex_get_view(t5577_multiwriter->dialog_ex));
  51. // Popup
  52. t5577_multiwriter->popup = popup_alloc();
  53. view_dispatcher_add_view(
  54. t5577_multiwriter->view_dispatcher,
  55. LfRfidViewPopup,
  56. popup_get_view(t5577_multiwriter->popup));
  57. // Widget
  58. t5577_multiwriter->widget = widget_alloc();
  59. view_dispatcher_add_view(
  60. t5577_multiwriter->view_dispatcher,
  61. LfRfidViewWidget,
  62. widget_get_view(t5577_multiwriter->widget));
  63. // Text Input
  64. t5577_multiwriter->text_input = text_input_alloc();
  65. view_dispatcher_add_view(
  66. t5577_multiwriter->view_dispatcher,
  67. LfRfidViewTextInput,
  68. text_input_get_view(t5577_multiwriter->text_input));
  69. // Byte Input
  70. t5577_multiwriter->byte_input = byte_input_alloc();
  71. view_dispatcher_add_view(
  72. t5577_multiwriter->view_dispatcher,
  73. LfRfidViewByteInput,
  74. byte_input_get_view(t5577_multiwriter->byte_input));
  75. return t5577_multiwriter;
  76. } //-V773
  77. static void t5577_multiwriter_free(LfRfid* t5577_multiwriter) {
  78. furi_assert(t5577_multiwriter);
  79. furi_string_free(t5577_multiwriter->file_name);
  80. furi_string_free(t5577_multiwriter->file_path);
  81. protocol_dict_free(t5577_multiwriter->dict);
  82. lfrfid_worker_free(t5577_multiwriter->lfworker);
  83. free(t5577_multiwriter->new_key_data);
  84. free(t5577_multiwriter->old_key_data);
  85. // Submenu
  86. view_dispatcher_remove_view(t5577_multiwriter->view_dispatcher, LfRfidViewSubmenu);
  87. submenu_free(t5577_multiwriter->submenu);
  88. // DialogEx
  89. view_dispatcher_remove_view(t5577_multiwriter->view_dispatcher, LfRfidViewDialogEx);
  90. dialog_ex_free(t5577_multiwriter->dialog_ex);
  91. // Popup
  92. view_dispatcher_remove_view(t5577_multiwriter->view_dispatcher, LfRfidViewPopup);
  93. popup_free(t5577_multiwriter->popup);
  94. // Widget
  95. view_dispatcher_remove_view(t5577_multiwriter->view_dispatcher, LfRfidViewWidget);
  96. widget_free(t5577_multiwriter->widget);
  97. // TextInput
  98. view_dispatcher_remove_view(t5577_multiwriter->view_dispatcher, LfRfidViewTextInput);
  99. text_input_free(t5577_multiwriter->text_input);
  100. // ByteInput
  101. view_dispatcher_remove_view(t5577_multiwriter->view_dispatcher, LfRfidViewByteInput);
  102. byte_input_free(t5577_multiwriter->byte_input);
  103. // View Dispatcher
  104. view_dispatcher_free(t5577_multiwriter->view_dispatcher);
  105. // Scene Manager
  106. scene_manager_free(t5577_multiwriter->scene_manager);
  107. // GUI
  108. furi_record_close(RECORD_GUI);
  109. t5577_multiwriter->gui = NULL;
  110. // Notifications
  111. furi_record_close(RECORD_NOTIFICATION);
  112. t5577_multiwriter->notifications = NULL;
  113. furi_record_close(RECORD_STORAGE);
  114. furi_record_close(RECORD_DIALOGS);
  115. free(t5577_multiwriter);
  116. }
  117. int32_t t5577_multiwriter_app(void* p) {
  118. LfRfid* app = t5577_multiwriter_alloc();
  119. UNUSED(p);
  120. t5577_multiwriter_make_app_folder(app);
  121. {
  122. view_dispatcher_attach_to_gui(
  123. app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  124. scene_manager_next_scene(app->scene_manager, LfRfidSceneStart);
  125. }
  126. view_dispatcher_run(app->view_dispatcher);
  127. t5577_multiwriter_free(app);
  128. return 0;
  129. }
  130. bool t5577_multiwriter_load_key_from_file_select(LfRfid* app) {
  131. furi_assert(app);
  132. DialogsFileBrowserOptions browser_options;
  133. dialog_file_browser_set_basic_options(
  134. &browser_options, LFRFID_APP_FILENAME_EXTENSION, &I_125_10px);
  135. browser_options.base_path = LFRFID_APP_FOLDER;
  136. // Input events and views are managed by file_browser
  137. bool result =
  138. dialog_file_browser_show(app->dialogs, app->file_path, app->file_path, &browser_options);
  139. if(result) {
  140. result = t5577_multiwriter_load_key_data(app, app->file_path, true);
  141. }
  142. return result;
  143. }
  144. bool t5577_multiwriter_load_key_data(LfRfid* app, FuriString* path, bool show_dialog) {
  145. bool result = false;
  146. do {
  147. app->protocol_id = lfrfid_dict_file_load(app->dict, furi_string_get_cstr(path));
  148. if(app->protocol_id == PROTOCOL_NO) break;
  149. if(app->protocol_id != LFRFIDProtocolEM4100) break;
  150. path_extract_filename(path, app->file_name, true);
  151. result = true;
  152. } while(0);
  153. if((!result) && (show_dialog)) {
  154. dialog_message_show_storage_error(app->dialogs, "Unsupported\nlfrfid protocol!");
  155. }
  156. return result;
  157. }
  158. void t5577_multiwriter_make_app_folder(LfRfid* app) {
  159. furi_assert(app);
  160. if(!storage_simply_mkdir(app->storage, LFRFID_APP_FOLDER)) {
  161. dialog_message_show_storage_error(app->dialogs, "Cannot create\napp folder");
  162. }
  163. }
  164. void t5577_multiwriter_text_store_set(LfRfid* app, const char* text, ...) {
  165. furi_assert(app);
  166. va_list args;
  167. va_start(args, text);
  168. vsnprintf(app->text_store, LFRFID_TEXT_STORE_SIZE, text, args);
  169. va_end(args);
  170. }
  171. void t5577_multiwriter_text_store_clear(LfRfid* app) {
  172. furi_assert(app);
  173. memset(app->text_store, 0, sizeof(app->text_store));
  174. }
  175. void t5577_multiwriter_popup_timeout_callback(void* context) {
  176. LfRfid* app = context;
  177. view_dispatcher_send_custom_event(app->view_dispatcher, LfRfidEventPopupClosed);
  178. }
  179. void t5577_multiwriter_widget_callback(GuiButtonType result, InputType type, void* context) {
  180. LfRfid* app = context;
  181. if(type == InputTypeShort) {
  182. view_dispatcher_send_custom_event(app->view_dispatcher, result);
  183. }
  184. }
  185. void t5577_multiwriter_text_input_callback(void* context) {
  186. LfRfid* app = context;
  187. view_dispatcher_send_custom_event(app->view_dispatcher, LfRfidEventNext);
  188. }