t5577_writer.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <gui/view.h>
  5. #include <gui/view_dispatcher.h>
  6. #include <gui/modules/popup.h>
  7. #include <gui/modules/submenu.h>
  8. #include <gui/modules/text_input.h>
  9. #include <gui/modules/byte_input.h>
  10. #include <gui/modules/widget.h>
  11. #include <gui/modules/variable_item_list.h>
  12. #include <notification/notification.h>
  13. #include <notification/notification_messages.h>
  14. #include "t5577_writer_icons.h"
  15. #include <applications/services/storage/storage.h>
  16. #include <applications/services/dialogs/dialogs.h>
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <t5577_config.h>
  21. #include <t5577_writer.h>
  22. #include <dolphin/dolphin.h>
  23. #define TAG "T5577 Writer"
  24. #define MAX_REPEAT_WRITING_FRAMES 10
  25. #define ENDING_WRITING_ICON_FRAMES 5
  26. typedef enum {
  27. T5577WriterSubmenuIndexLoad,
  28. T5577WriterSubmenuIndexSave,
  29. T5577WriterSubmenuIndexConfigure,
  30. T5577WriterSubmenuIndexWrite,
  31. T5577WriterSubmenuIndexAbout,
  32. } T5577WriterSubmenuIndex;
  33. // Each view is a screen we show the user.
  34. typedef enum {
  35. T5577WriterViewSubmenu, // The menu when the app starts
  36. T5577WriterViewTextInput, // Input for configuring text settings
  37. T5577WriterViewByteInput,
  38. T5577WriterViewLoad,
  39. T5577WriterViewSave,
  40. T5577WriterViewPopup,
  41. T5577WriterViewConfigure_i, // The configuration screen
  42. T5577WriterViewConfigure_e, // The configuration screen
  43. T5577WriterViewWrite, // The main screen
  44. T5577WriterViewAbout, // The about screen with directions, link to social channel, etc.
  45. } T5577WriterView;
  46. typedef enum {
  47. T5577WriterEventIdRepeatWriting = 0, // Custom event to redraw the screen
  48. T5577WriterEventIdMaxWriteRep = 42, // Custom event to process OK button getting pressed down
  49. } T5577WriterEventId;
  50. typedef struct {
  51. ViewDispatcher* view_dispatcher; // Switches between our views
  52. NotificationApp* notifications; // Used for controlling the backlight
  53. Submenu* submenu; // The application menu
  54. TextInput* text_input; // The text input screen
  55. Popup* popup;
  56. VariableItemList* variable_item_list_config; // The configuration screen
  57. View* view_config_e; // The configuration screen
  58. View* view_save;
  59. View* view_write; // The main screen
  60. Widget* widget_about; // The about screen
  61. View* view_load; // The load view
  62. VariableItem* mod_item; //
  63. VariableItem* clock_item; //
  64. VariableItem* block_num_item; //
  65. VariableItem* block_slc_item; //
  66. VariableItem* byte_buffer_item; //
  67. ByteInput* byte_input; // The byte input view
  68. uint8_t bytes_buffer[4];
  69. uint8_t bytes_count;
  70. char* temp_buffer; // Temporary buffer for text input
  71. uint32_t temp_buffer_size; // Size of temporary buffer
  72. DialogsApp* dialogs;
  73. FuriString* file_path;
  74. FuriTimer* timer; // Timer for redrawing the screen
  75. ViewNavigationCallback config_enter_callback;
  76. } T5577WriterApp;
  77. typedef struct {
  78. uint8_t modulation_index; // The index for total number of pins
  79. uint8_t rf_clock_index; // The index for total number of pins
  80. FuriString* tag_name_str; // The name setting
  81. uint8_t user_block_num; // The total number of pins we are adjusting
  82. uint32_t content[LFRFID_T5577_BLOCK_COUNT]; // The cutting content
  83. t5577_modulation modulation;
  84. t5577_rf_clock rf_clock;
  85. bool data_loaded[3];
  86. uint8_t edit_block_slc;
  87. uint8_t writing_repeat_times;
  88. } T5577WriterModel;
  89. void initialize_config(T5577WriterModel* model) {
  90. model->modulation_index = 0;
  91. memcpy(&model->modulation, &all_mods[model->modulation_index], sizeof(t5577_modulation));
  92. model->rf_clock_index = 0;
  93. memcpy(&model->rf_clock, &all_mods[model->rf_clock_index], sizeof(t5577_rf_clock));
  94. }
  95. void initialize_model(T5577WriterModel* model) {
  96. initialize_config(model);
  97. model->user_block_num = 1;
  98. model->edit_block_slc = 1;
  99. model->writing_repeat_times = 0;
  100. for(uint32_t i = 0; i < LFRFID_T5577_BLOCK_COUNT; i++) {
  101. model->content[i] = 0;
  102. }
  103. memset(model->data_loaded, false, sizeof(model->data_loaded));
  104. }
  105. uint8_t rf_clock_choices[COUNT_OF(all_rf_clocks)];
  106. void initialize_rf_clock_choices(uint8_t* rf_clock_choices) {
  107. // Populate the rf_clock_choices array
  108. for(size_t i = 0; i < COUNT_OF(all_rf_clocks); i++) {
  109. rf_clock_choices[i] = all_rf_clocks[i].rf_clock_num;
  110. }
  111. }
  112. char* modulation_names[COUNT_OF(all_mods)];
  113. void initialize_mod_names(char** modulation_names) {
  114. // Populate the modulation_names array
  115. for(size_t i = 0; i < COUNT_OF(all_mods); i++) {
  116. modulation_names[i] = all_mods[i].modulation_name;
  117. }
  118. }
  119. /**
  120. * @brief Callback for exiting the application.
  121. * @details This function is called when user press back button. We return VIEW_NONE to
  122. * indicate that we want to exit the application.
  123. * @param _context The context - unused
  124. * @return next view id
  125. */
  126. static uint32_t t5577_writer_navigation_exit_callback(void* _context) {
  127. UNUSED(_context);
  128. return VIEW_NONE;
  129. }
  130. /**
  131. * @brief Callback for returning to submenu.
  132. * @details This function is called when user press back button. We return VIEW_NONE to
  133. * indicate that we want to navigate to the submenu.
  134. * @param _context The context - unused
  135. * @return next view id
  136. */
  137. static uint32_t t5577_writer_navigation_submenu_callback(void* _context) {
  138. UNUSED(_context);
  139. return T5577WriterViewSubmenu;
  140. }
  141. static uint32_t t5577_writer_navigation_config_e_callback(void* _context) {
  142. UNUSED(_context);
  143. return T5577WriterViewConfigure_e;
  144. }
  145. /**
  146. * @brief Handle submenu item selection.
  147. * @details This function is called when user selects an item from the submenu.
  148. * @param context The context - T5577WriterApp object.
  149. * @param index The T5577WriterSubmenuIndex item that was clicked.
  150. */
  151. static void t5577_writer_submenu_callback(void* context, uint32_t index) {
  152. T5577WriterApp* app = (T5577WriterApp*)context;
  153. switch(index) {
  154. case T5577WriterSubmenuIndexLoad:
  155. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewLoad);
  156. break;
  157. case T5577WriterSubmenuIndexSave:
  158. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSave);
  159. break;
  160. case T5577WriterSubmenuIndexConfigure:
  161. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  162. break;
  163. case T5577WriterSubmenuIndexWrite:
  164. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewWrite);
  165. break;
  166. case T5577WriterSubmenuIndexAbout:
  167. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewAbout);
  168. break;
  169. default:
  170. break;
  171. }
  172. }
  173. static const char* modulation_config_label = "Modulation";
  174. static void t5577_writer_modulation_change(VariableItem* item) {
  175. T5577WriterApp* app = variable_item_get_context(item);
  176. T5577WriterModel* model = view_get_model(app->view_write);
  177. if(model->data_loaded[0]) {
  178. variable_item_set_current_value_index(item, model->modulation_index);
  179. } else {
  180. uint8_t modulation_index = variable_item_get_current_value_index(item);
  181. model->modulation_index = modulation_index;
  182. model->modulation = all_mods[modulation_index];
  183. }
  184. model->data_loaded[0] = false;
  185. variable_item_set_current_value_text(item, modulation_names[model->modulation_index]);
  186. }
  187. static const char* rf_clock_config_label = "RF Clock";
  188. static void t5577_writer_rf_clock_change(VariableItem* item) {
  189. T5577WriterApp* app = variable_item_get_context(item);
  190. T5577WriterModel* model = view_get_model(app->view_write);
  191. if(model->data_loaded[1]) {
  192. variable_item_set_current_value_index(item, model->rf_clock_index);
  193. } else {
  194. uint8_t rf_clock_index = variable_item_get_current_value_index(item);
  195. model->rf_clock_index = rf_clock_index;
  196. model->rf_clock = all_rf_clocks[rf_clock_index];
  197. }
  198. model->data_loaded[1] = false;
  199. FuriString* buffer = furi_string_alloc();
  200. furi_string_printf(buffer, "%u", rf_clock_choices[model->rf_clock_index]);
  201. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  202. furi_string_free(buffer);
  203. }
  204. static const char* user_block_num_config_label = "Num of Blocks";
  205. static void t5577_writer_user_block_num_change(VariableItem* item) {
  206. T5577WriterApp* app = variable_item_get_context(item);
  207. T5577WriterModel* model = view_get_model(app->view_write);
  208. if(model->data_loaded[2]) {
  209. variable_item_set_current_value_index(item, model->user_block_num - 1);
  210. } else {
  211. uint8_t user_block_num_index = variable_item_get_current_value_index(item);
  212. model->user_block_num = user_block_num_index + 1;
  213. }
  214. model->data_loaded[2] = false;
  215. FuriString* buffer = furi_string_alloc();
  216. furi_string_printf(buffer, "%u", model->user_block_num);
  217. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  218. for(uint8_t i = model->user_block_num; i < LFRFID_T5577_BLOCK_COUNT; i++) {
  219. model->content[i] = 0; // pad the unneeded blocks with zeros
  220. }
  221. furi_string_free(buffer);
  222. }
  223. static const char* edit_block_slc_config_label = "Edit Block";
  224. static void t5577_writer_edit_block_slc_change(VariableItem* item) {
  225. T5577WriterApp* app = variable_item_get_context(item);
  226. T5577WriterModel* model = view_get_model(app->view_write);
  227. uint8_t edit_block_slc_index = variable_item_get_current_value_index(item);
  228. model->edit_block_slc = edit_block_slc_index + 1;
  229. variable_item_set_current_value_index(item, model->edit_block_slc - 1);
  230. FuriString* buffer = furi_string_alloc();
  231. furi_string_printf(buffer, "%u", model->edit_block_slc);
  232. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  233. furi_string_printf(buffer, "%08lX", model->content[model->edit_block_slc]);
  234. variable_item_set_current_value_text(app->byte_buffer_item, furi_string_get_cstr(buffer));
  235. furi_string_free(buffer);
  236. }
  237. void ensure_dir_exists(Storage* storage) {
  238. // If apps_data directory doesn't exist, create it.
  239. if(!storage_dir_exists(storage, T5577_WRITER_APPS_DATA_FOLDER)) {
  240. FURI_LOG_I(TAG, "Creating directory: %s", T5577_WRITER_APPS_DATA_FOLDER);
  241. storage_simply_mkdir(storage, T5577_WRITER_APPS_DATA_FOLDER);
  242. } else {
  243. FURI_LOG_I(TAG, "Directory exists: %s", T5577_WRITER_APPS_DATA_FOLDER);
  244. }
  245. // If t5577_writer directory doesn't exist, create it.
  246. if(!storage_dir_exists(storage, T5577_WRITER_FILE_FOLDER)) {
  247. FURI_LOG_I(TAG, "Creating directory: %s", T5577_WRITER_FILE_FOLDER);
  248. storage_simply_mkdir(storage, T5577_WRITER_FILE_FOLDER);
  249. } else {
  250. FURI_LOG_I(TAG, "Directory exists: %s", T5577_WRITER_FILE_FOLDER);
  251. }
  252. }
  253. static const char* tag_name_entry_text = "Enter name";
  254. static const char* tag_name_default_value = "Tag_1";
  255. static void t5577_writer_file_saver(void* context) {
  256. T5577WriterApp* app = (T5577WriterApp*)context;
  257. T5577WriterModel* model = view_get_model(app->view_write);
  258. model->content[0] = 0; // clean up first block before deciding to write or save
  259. model->content[0] |= model->modulation.mod_page_zero;
  260. model->content[0] |= model->rf_clock.clock_page_zero;
  261. model->content[0] |= (model->user_block_num << LFRFID_T5577_MAXBLOCK_SHIFT);
  262. bool redraw = true;
  263. with_view_model(
  264. app->view_write,
  265. T5577WriterModel * model,
  266. { furi_string_set(model->tag_name_str, app->temp_buffer); },
  267. redraw);
  268. FuriString* buffer = furi_string_alloc();
  269. FuriString* file_path = furi_string_alloc();
  270. furi_string_printf(
  271. file_path,
  272. "%s/%s%s",
  273. T5577_WRITER_FILE_FOLDER,
  274. furi_string_get_cstr(model->tag_name_str),
  275. T5577_WRITER_FILE_EXTENSION);
  276. Storage* storage = furi_record_open(RECORD_STORAGE);
  277. ensure_dir_exists(storage);
  278. File* data_file = storage_file_alloc(storage);
  279. if(storage_file_open(
  280. data_file, furi_string_get_cstr(file_path), FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
  281. furi_string_printf(buffer, "Filetype: Flipper T5577 Raw File\n");
  282. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  283. furi_string_printf(buffer, "Version: 1.0\n");
  284. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  285. furi_string_printf(buffer, "Modulation: %s\n", model->modulation.modulation_name);
  286. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  287. furi_string_printf(buffer, "RF Clock: %u\n", model->rf_clock.rf_clock_num);
  288. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  289. furi_string_printf(buffer, "Number of User Blocks: %u\n", model->user_block_num);
  290. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  291. furi_string_printf(buffer, "\nRaw Data:\n");
  292. for(int i = 0; i < LFRFID_T5577_BLOCK_COUNT; i++) {
  293. furi_string_cat_printf(buffer, "Block %u: %08lX\n", i, model->content[i]);
  294. }
  295. furi_string_push_back(buffer, '\n');
  296. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  297. storage_file_close(data_file);
  298. view_dispatcher_switch_to_view(
  299. app->view_dispatcher, T5577WriterViewSubmenu); // maybe add a pop up later
  300. }
  301. }
  302. void t5577_writer_update_config_from_load(void* context) {
  303. T5577WriterApp* app = (T5577WriterApp*)context;
  304. T5577WriterModel* my_model = view_get_model(app->view_write);
  305. for(size_t i = 0; i < COUNT_OF(all_mods); i++) {
  306. if((my_model->content[0] & all_mods[i].mod_page_zero) == all_mods[i].mod_page_zero) {
  307. my_model->modulation_index = (uint8_t)i;
  308. my_model->modulation = all_mods[my_model->modulation_index];
  309. }
  310. }
  311. for(size_t i = 0; i < COUNT_OF(all_rf_clocks); i++) {
  312. if((my_model->content[0] & all_rf_clocks[i].clock_page_zero) ==
  313. all_rf_clocks[i].clock_page_zero) {
  314. my_model->rf_clock_index = (uint8_t)i;
  315. my_model->rf_clock = all_rf_clocks[my_model->rf_clock_index];
  316. }
  317. }
  318. my_model->user_block_num = (my_model->content[0] >> LFRFID_T5577_MAXBLOCK_SHIFT) & 0xF;
  319. memset(my_model->data_loaded, true, sizeof(my_model->data_loaded)); // Everything is loaded
  320. }
  321. static const char* edit_block_data_config_label = "Block Data";
  322. void uint32_to_byte_buffer(uint32_t block_data, uint8_t byte_buffer[4]) {
  323. byte_buffer[0] = (block_data >> 24) & 0xFF;
  324. byte_buffer[1] = (block_data >> 16) & 0xFF;
  325. byte_buffer[2] = (block_data >> 8) & 0xFF;
  326. byte_buffer[3] = block_data & 0xFF;
  327. }
  328. uint32_t byte_buffer_to_uint32(uint8_t byte_buffer[4]) {
  329. uint32_t block_data = 0;
  330. block_data |= ((uint32_t)byte_buffer[0] << 24);
  331. block_data |= ((uint32_t)byte_buffer[1] << 16);
  332. block_data |= ((uint32_t)byte_buffer[2] << 8);
  333. block_data |= ((uint32_t)byte_buffer[3]);
  334. return block_data;
  335. }
  336. static void t5577_writer_content_byte_input_confirmed(void* context) {
  337. T5577WriterApp* app = (T5577WriterApp*)context;
  338. T5577WriterModel* my_model = view_get_model(app->view_write);
  339. my_model->content[my_model->edit_block_slc] = byte_buffer_to_uint32(app->bytes_buffer);
  340. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  341. }
  342. static void t5577_writer_content_byte_changed(void* context) {
  343. UNUSED(context);
  344. }
  345. static void t5577_writer_config_item_clicked(void* context, uint32_t index) {
  346. T5577WriterApp* app = (T5577WriterApp*)context;
  347. T5577WriterModel* my_model = view_get_model(app->view_write);
  348. FuriString* buffer = furi_string_alloc();
  349. furi_string_printf(buffer, "Enter Block %u Data", my_model->edit_block_slc);
  350. // Our hex input UI is the 5th in the config menue.
  351. if(index == 4) {
  352. // Header to display on the text input screen.
  353. byte_input_set_header_text(app->byte_input, furi_string_get_cstr(buffer));
  354. // Copy the current name into the temporary buffer.
  355. bool redraw = false;
  356. with_view_model(
  357. app->view_write,
  358. T5577WriterModel * model,
  359. { uint32_to_byte_buffer(model->content[model->edit_block_slc], app->bytes_buffer); },
  360. redraw);
  361. // Configure the text input. When user enters text and clicks OK, key_copier_setting_text_updated be called.
  362. byte_input_set_result_callback(
  363. app->byte_input,
  364. t5577_writer_content_byte_input_confirmed,
  365. t5577_writer_content_byte_changed,
  366. app,
  367. app->bytes_buffer,
  368. app->bytes_count);
  369. // Pressing the BACK button will reload the configure screen.
  370. view_set_previous_callback(
  371. byte_input_get_view(app->byte_input), t5577_writer_navigation_config_e_callback);
  372. // Show text input dialog.
  373. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewByteInput);
  374. }
  375. }
  376. static void t5577_writer_config_enter_callback(void* context) {
  377. T5577WriterApp* app = (T5577WriterApp*)context;
  378. T5577WriterModel* my_model = view_get_model(app->view_write);
  379. variable_item_list_reset(app->variable_item_list_config);
  380. // Recreate this view every time we enter it so that it's always updated
  381. app->mod_item = variable_item_list_add(
  382. app->variable_item_list_config,
  383. modulation_config_label,
  384. COUNT_OF(modulation_names),
  385. t5577_writer_modulation_change,
  386. app);
  387. app->clock_item = variable_item_list_add(
  388. app->variable_item_list_config,
  389. rf_clock_config_label,
  390. COUNT_OF(rf_clock_choices),
  391. t5577_writer_rf_clock_change,
  392. app);
  393. app->block_num_item = variable_item_list_add(
  394. app->variable_item_list_config,
  395. user_block_num_config_label,
  396. LFRFID_T5577_BLOCK_COUNT,
  397. t5577_writer_user_block_num_change,
  398. app);
  399. app->block_slc_item = variable_item_list_add(
  400. app->variable_item_list_config,
  401. edit_block_slc_config_label,
  402. LFRFID_T5577_BLOCK_COUNT - 1,
  403. t5577_writer_edit_block_slc_change,
  404. app);
  405. app->byte_buffer_item = variable_item_list_add(
  406. app->variable_item_list_config, edit_block_data_config_label, 1, NULL, app);
  407. variable_item_list_set_enter_callback(
  408. app->variable_item_list_config, t5577_writer_config_item_clicked, app);
  409. View* view_config_i = variable_item_list_get_view(app->variable_item_list_config);
  410. variable_item_set_current_value_index(app->mod_item, my_model->modulation_index);
  411. variable_item_set_current_value_index(app->clock_item, my_model->rf_clock_index);
  412. variable_item_set_current_value_index(app->block_num_item, my_model->user_block_num - 1);
  413. variable_item_set_current_value_index(app->block_slc_item, my_model->edit_block_slc - 1);
  414. t5577_writer_modulation_change(app->mod_item);
  415. t5577_writer_rf_clock_change(app->clock_item);
  416. t5577_writer_user_block_num_change(app->block_num_item);
  417. t5577_writer_edit_block_slc_change(app->block_slc_item);
  418. view_set_previous_callback(view_config_i, t5577_writer_navigation_submenu_callback);
  419. view_dispatcher_remove_view(
  420. app->view_dispatcher, T5577WriterViewConfigure_i); // delete the last one
  421. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewConfigure_i, view_config_i);
  422. view_dispatcher_switch_to_view(
  423. app->view_dispatcher, T5577WriterViewConfigure_i); // recreate it
  424. }
  425. void t5577_writer_view_load_callback(void* context) {
  426. T5577WriterApp* app = (T5577WriterApp*)context;
  427. T5577WriterModel* model = view_get_model(app->view_write);
  428. DialogsFileBrowserOptions browser_options;
  429. Storage* storage = furi_record_open(RECORD_STORAGE);
  430. ensure_dir_exists(storage);
  431. File* data_file = storage_file_alloc(storage);
  432. dialog_file_browser_set_basic_options(&browser_options, T5577_WRITER_FILE_EXTENSION, &I_icon);
  433. browser_options.base_path = T5577_WRITER_FILE_FOLDER;
  434. furi_string_set(app->file_path, browser_options.base_path);
  435. FuriString* buffer = furi_string_alloc();
  436. if(dialog_file_browser_show(app->dialogs, app->file_path, app->file_path, &browser_options)) {
  437. if(storage_file_open(
  438. data_file, furi_string_get_cstr(app->file_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  439. while(!storage_file_eof(data_file)) { // fill buffer with every line because ch == '\n'
  440. char ch;
  441. furi_string_reset(buffer);
  442. while(storage_file_read(data_file, &ch, 1) && !storage_file_eof(data_file)) {
  443. furi_string_push_back(buffer, ch);
  444. if(ch == '\n') {
  445. break;
  446. }
  447. }
  448. if(furi_string_start_with(buffer, "Block ")) {
  449. uint32_t row_data_buffer = 0;
  450. char row_data_char_buffer[] = "00000000";
  451. uint32_t row_num_buffer = 0;
  452. char row_num_char_buffer[] = "0";
  453. int length = furi_string_size(buffer);
  454. char ch;
  455. int i = 0;
  456. while(i < length) {
  457. if(furi_string_get_char(buffer, i) == ':') {
  458. row_num_char_buffer[0] = furi_string_get_char(buffer, i - 1);
  459. //the number before ":" is block num
  460. i += 2; // skip a space
  461. for(size_t j = 0; j < sizeof(row_data_char_buffer); j++) {
  462. ch = furi_string_get_char(buffer, i);
  463. row_data_char_buffer[j] = ch;
  464. i++;
  465. }
  466. break;
  467. }
  468. i++;
  469. }
  470. sscanf(row_num_char_buffer, "%lx", &row_num_buffer);
  471. sscanf(row_data_char_buffer, "%lx", &row_data_buffer);
  472. model->content[row_num_buffer] = row_data_buffer;
  473. }
  474. }
  475. storage_file_close(data_file);
  476. t5577_writer_update_config_from_load(app);
  477. }
  478. storage_file_free(data_file);
  479. furi_record_close(RECORD_STORAGE);
  480. }
  481. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  482. }
  483. /**
  484. * @brief Callback when item in configuration screen is clicked.
  485. * @details This function is called when user clicks OK on an item in the text input screen.
  486. * @param context The context - T5577WriterApp object.
  487. * @param index - The index of the item that was clicked.
  488. */
  489. static void t5577_writer_view_save_callback(void* context) {
  490. T5577WriterApp* app = (T5577WriterApp*)context;
  491. // Header to display on the text input screen.
  492. text_input_set_header_text(app->text_input, tag_name_entry_text);
  493. // Copy the current name into the temporary buffer.
  494. bool redraw = false;
  495. with_view_model(
  496. app->view_write,
  497. T5577WriterModel * model,
  498. {
  499. strncpy(
  500. app->temp_buffer,
  501. furi_string_get_cstr(model->tag_name_str),
  502. app->temp_buffer_size);
  503. },
  504. redraw);
  505. // Configure the text input. When user enters text and clicks OK, t5577_writer_setting_text_updated be called.
  506. bool clear_previous_text = false;
  507. text_input_set_result_callback(
  508. app->text_input,
  509. t5577_writer_file_saver,
  510. app,
  511. app->temp_buffer,
  512. app->temp_buffer_size,
  513. clear_previous_text);
  514. // Pressing the BACK button will return to the main menu.
  515. view_set_previous_callback(
  516. text_input_get_view(app->text_input), t5577_writer_navigation_submenu_callback);
  517. // Show text input dialog.
  518. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewTextInput);
  519. }
  520. static void t5577_writer_actual_writing(void* model) {
  521. T5577WriterModel* my_model = (T5577WriterModel*)model;
  522. my_model->content[0] = 0; // clear up block 0
  523. my_model->content[0] |= my_model->modulation.mod_page_zero;
  524. my_model->content[0] |= my_model->rf_clock.clock_page_zero;
  525. my_model->content[0] |= (my_model->user_block_num << LFRFID_T5577_MAXBLOCK_SHIFT);
  526. LFRFIDT5577* data = (LFRFIDT5577*)malloc(sizeof(LFRFIDT5577));
  527. data->blocks_to_write = my_model->user_block_num;
  528. for(size_t i = 0; i < data->blocks_to_write; i++) {
  529. data->block[i] = my_model->content[i];
  530. }
  531. t5577_write(data);
  532. free(data);
  533. }
  534. /**
  535. * @brief Callback for drawing the writing screen.
  536. * @details This function is called when the screen needs to be redrawn, so that the writing command is repeated.
  537. * @param canvas The canvas to draw on.
  538. * @param model The model - MyModel object.
  539. */
  540. static void t5577_writer_view_write_callback(Canvas* canvas, void* model) {
  541. T5577WriterModel* my_model = (T5577WriterModel*)model;
  542. if(my_model->writing_repeat_times < MAX_REPEAT_WRITING_FRAMES) {
  543. t5577_writer_actual_writing(model);
  544. canvas_set_bitmap_mode(canvas, true);
  545. canvas_draw_icon(canvas, 0, 8, &I_NFC_manual_60x50);
  546. canvas_draw_str_aligned(canvas, 97, 15, AlignCenter, AlignTop, "Writing");
  547. canvas_draw_str_aligned(canvas, 94, 27, AlignCenter, AlignTop, "Hold card next");
  548. canvas_draw_str_aligned(canvas, 93, 39, AlignCenter, AlignTop, "to Flipper's back");
  549. } else {
  550. canvas_set_bitmap_mode(canvas, true);
  551. canvas_draw_icon(canvas, 0, 9, &I_DolphinSuccess_91x55);
  552. canvas_set_font(canvas, FontPrimary);
  553. canvas_draw_str(canvas, 75, 16, "Finished!");
  554. }
  555. }
  556. /**
  557. * @brief Callback for timer elapsed.
  558. * @details This function is called when the timer is elapsed. We use this to queue a redraw event.
  559. * @param context The context - T5577WriterApp object.
  560. */
  561. static void t5577_writer_view_write_timer_callback(void* context) {
  562. T5577WriterApp* app = (T5577WriterApp*)context;
  563. T5577WriterModel* model = view_get_model(app->view_write);
  564. if(model->writing_repeat_times < MAX_REPEAT_WRITING_FRAMES + ENDING_WRITING_ICON_FRAMES) {
  565. model->writing_repeat_times += 1;
  566. view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdRepeatWriting);
  567. } else {
  568. view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdMaxWriteRep);
  569. }
  570. }
  571. /**
  572. * @brief Callback when the user starts the writing screen.
  573. * @details This function is called when the user enters the writing screen. We start a timer to
  574. * redraw the screen periodically. (exactly like PsychToolBox lol)
  575. * @param context The context - T5577WriterApp object.
  576. */
  577. static void t5577_writer_view_write_enter_callback(void* context) {
  578. uint32_t repeat_writing_period = furi_ms_to_ticks(200);
  579. T5577WriterApp* app = (T5577WriterApp*)context;
  580. furi_assert(app->timer == NULL);
  581. app->timer =
  582. furi_timer_alloc(t5577_writer_view_write_timer_callback, FuriTimerTypePeriodic, context);
  583. furi_timer_start(app->timer, repeat_writing_period);
  584. dolphin_deed(DolphinDeedRfidEmulate);
  585. }
  586. /**
  587. * @brief Callback when the user exits the writing screen.
  588. * @details This function is called when the user exits the writing screen. We stop the timer.
  589. * @param context The context - T5577WriterApp object.
  590. */
  591. static void t5577_writer_view_write_exit_callback(void* context) {
  592. T5577WriterApp* app = (T5577WriterApp*)context;
  593. T5577WriterModel* model = view_get_model(app->view_write);
  594. furi_timer_stop(app->timer);
  595. furi_timer_free(app->timer);
  596. app->timer = NULL;
  597. model->writing_repeat_times = 0;
  598. }
  599. /**
  600. * @brief Callback for custom events.
  601. * @details This function is called when a custom event is sent to the view dispatcher.
  602. * @param event The event id - T5577WriterEventId value.
  603. * @param context The context - T5577WriterApp object.
  604. */
  605. static bool t5577_writer_view_write_custom_event_callback(uint32_t event, void* context) {
  606. T5577WriterApp* app = (T5577WriterApp*)context;
  607. switch(event) {
  608. case T5577WriterEventIdRepeatWriting:
  609. // Redraw screen by passing true to last parameter of with_view_model.
  610. {
  611. bool redraw = true;
  612. with_view_model(
  613. app->view_write, T5577WriterModel * _model, { UNUSED(_model); }, redraw);
  614. return true;
  615. }
  616. case T5577WriterEventIdMaxWriteRep:
  617. // Process the OK button. We go to the saving scene.
  618. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  619. return true;
  620. default:
  621. return false;
  622. }
  623. }
  624. /**
  625. * @brief Allocate the t5577_writer application.
  626. * @details This function allocates the t5577_writer application resources.
  627. * @return T5577WriterApp object.
  628. */
  629. static T5577WriterApp* t5577_writer_app_alloc() {
  630. T5577WriterApp* app = (T5577WriterApp*)malloc(sizeof(T5577WriterApp));
  631. Gui* gui = furi_record_open(RECORD_GUI);
  632. app->view_dispatcher = view_dispatcher_alloc();
  633. app->dialogs = furi_record_open(RECORD_DIALOGS);
  634. app->file_path = furi_string_alloc();
  635. view_dispatcher_enable_queue(app->view_dispatcher);
  636. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  637. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  638. app->submenu = submenu_alloc();
  639. submenu_add_item(
  640. app->submenu, "Write", T5577WriterSubmenuIndexWrite, t5577_writer_submenu_callback, app);
  641. submenu_add_item(
  642. app->submenu,
  643. "Config",
  644. T5577WriterSubmenuIndexConfigure,
  645. t5577_writer_submenu_callback,
  646. app);
  647. submenu_add_item(
  648. app->submenu, "Save", T5577WriterSubmenuIndexSave, t5577_writer_submenu_callback, app);
  649. submenu_add_item(
  650. app->submenu, "Load", T5577WriterSubmenuIndexLoad, t5577_writer_submenu_callback, app);
  651. submenu_add_item(
  652. app->submenu, "About", T5577WriterSubmenuIndexAbout, t5577_writer_submenu_callback, app);
  653. view_set_previous_callback(
  654. submenu_get_view(app->submenu), t5577_writer_navigation_exit_callback);
  655. view_dispatcher_add_view(
  656. app->view_dispatcher, T5577WriterViewSubmenu, submenu_get_view(app->submenu));
  657. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  658. app->text_input = text_input_alloc();
  659. view_dispatcher_add_view(
  660. app->view_dispatcher, T5577WriterViewTextInput, text_input_get_view(app->text_input));
  661. app->view_load = view_alloc();
  662. view_set_previous_callback(app->view_load, t5577_writer_navigation_submenu_callback);
  663. view_set_enter_callback(app->view_load, t5577_writer_view_load_callback);
  664. view_set_context(app->view_load, app);
  665. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewLoad, app->view_load);
  666. app->temp_buffer_size = 32;
  667. app->temp_buffer = (char*)malloc(app->temp_buffer_size);
  668. app->view_write = view_alloc();
  669. view_set_draw_callback(app->view_write, t5577_writer_view_write_callback);
  670. view_set_previous_callback(app->view_write, t5577_writer_navigation_submenu_callback);
  671. view_set_enter_callback(app->view_write, t5577_writer_view_write_enter_callback);
  672. view_set_exit_callback(app->view_write, t5577_writer_view_write_exit_callback);
  673. view_set_context(app->view_write, app);
  674. view_set_custom_callback(app->view_write, t5577_writer_view_write_custom_event_callback);
  675. view_allocate_model(app->view_write, ViewModelTypeLockFree, sizeof(T5577WriterModel));
  676. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewWrite, app->view_write);
  677. T5577WriterModel* model = view_get_model(app->view_write); // initialize model
  678. FuriString* tag_name_str = furi_string_alloc();
  679. furi_string_set_str(tag_name_str, tag_name_default_value);
  680. model->tag_name_str = tag_name_str;
  681. initialize_model(model);
  682. initialize_rf_clock_choices(rf_clock_choices);
  683. initialize_mod_names(modulation_names);
  684. app->view_save = view_alloc();
  685. view_set_previous_callback(app->view_save, t5577_writer_navigation_submenu_callback);
  686. view_set_enter_callback(app->view_save, t5577_writer_view_save_callback);
  687. view_set_context(app->view_save, app);
  688. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewSave, app->view_save);
  689. app->bytes_count = 4;
  690. memset(app->bytes_buffer, 0, sizeof(app->bytes_buffer));
  691. app->byte_input = byte_input_alloc();
  692. view_dispatcher_add_view(
  693. app->view_dispatcher, T5577WriterViewByteInput, byte_input_get_view(app->byte_input));
  694. app->variable_item_list_config = variable_item_list_alloc();
  695. app->view_config_e = view_alloc();
  696. view_set_previous_callback(app->view_config_e, t5577_writer_navigation_submenu_callback);
  697. view_set_enter_callback(app->view_config_e, t5577_writer_config_enter_callback);
  698. view_set_context(app->view_config_e, app);
  699. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewConfigure_e, app->view_config_e);
  700. View* view_buffer = view_alloc();
  701. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewConfigure_i, view_buffer);
  702. app->widget_about = widget_alloc();
  703. widget_add_text_scroll_element(
  704. app->widget_about,
  705. 0,
  706. 0,
  707. 128,
  708. 64,
  709. "T5577 Raw Writer v0.1\n\nAuthor: @Torron\n\nGithub: https://github.com/zinongli/T5577_Raw_Writer");
  710. view_set_previous_callback(
  711. widget_get_view(app->widget_about), t5577_writer_navigation_submenu_callback);
  712. view_dispatcher_add_view(
  713. app->view_dispatcher, T5577WriterViewAbout, widget_get_view(app->widget_about));
  714. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  715. #ifdef BACKLIGHT_ON
  716. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  717. #endif
  718. return app;
  719. }
  720. /**
  721. * @brief Free the t5577_writer application.
  722. * @details This function frees the t5577_writer application resources.
  723. * @param app The t5577_writer application object.
  724. */
  725. static void t5577_writer_app_free(T5577WriterApp* app) {
  726. #ifdef BACKLIGHT_ON
  727. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  728. #endif
  729. furi_record_close(RECORD_NOTIFICATION);
  730. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewTextInput);
  731. text_input_free(app->text_input);
  732. free(app->temp_buffer);
  733. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewAbout);
  734. widget_free(app->widget_about);
  735. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewWrite);
  736. view_free(app->view_write);
  737. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewLoad);
  738. view_free(app->view_load);
  739. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_i);
  740. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  741. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewByteInput);
  742. variable_item_list_free(app->variable_item_list_config);
  743. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSave);
  744. view_free(app->view_save);
  745. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSubmenu);
  746. submenu_free(app->submenu);
  747. view_dispatcher_free(app->view_dispatcher);
  748. furi_record_close(RECORD_GUI);
  749. free(app);
  750. }
  751. /**
  752. * @brief Main function for t5577_writer application.
  753. * @details This function is the entry point for the t5577_writer application. It should be defined in
  754. * application.fam as the entry_point setting.
  755. * @param _p Input parameter - unused
  756. * @return 0 - Success
  757. */
  758. int32_t main_t5577_writer_app(void* _p) {
  759. UNUSED(_p);
  760. T5577WriterApp* app = t5577_writer_app_alloc();
  761. view_dispatcher_run(app->view_dispatcher);
  762. t5577_writer_app_free(app);
  763. return 0;
  764. }