t5577_writer.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. {
  239. // If apps_data directory doesn't exist, create it.
  240. if (!storage_dir_exists(storage, T5577_WRITER_APPS_DATA_FOLDER))
  241. {
  242. FURI_LOG_I(TAG, "Creating directory: %s", T5577_WRITER_APPS_DATA_FOLDER);
  243. storage_simply_mkdir(storage, T5577_WRITER_APPS_DATA_FOLDER);
  244. }
  245. else
  246. {
  247. FURI_LOG_I(TAG, "Directory exists: %s", T5577_WRITER_APPS_DATA_FOLDER);
  248. }
  249. // If t5577_writer directory doesn't exist, create it.
  250. if (!storage_dir_exists(storage, T5577_WRITER_FILE_FOLDER))
  251. {
  252. FURI_LOG_I(TAG, "Creating directory: %s", T5577_WRITER_FILE_FOLDER);
  253. storage_simply_mkdir(storage, T5577_WRITER_FILE_FOLDER);
  254. }
  255. else
  256. {
  257. FURI_LOG_I(TAG, "Directory exists: %s", T5577_WRITER_FILE_FOLDER);
  258. }
  259. }
  260. static const char* tag_name_entry_text = "Enter name";
  261. static const char* tag_name_default_value = "Tag_1";
  262. static void t5577_writer_file_saver(void* context) {
  263. T5577WriterApp* app = (T5577WriterApp*)context;
  264. T5577WriterModel* model = view_get_model(app->view_write);
  265. model->content[0] = 0; // clean up first block before deciding to write or save
  266. model->content[0] |= model->modulation.mod_page_zero;
  267. model->content[0] |= model->rf_clock.clock_page_zero;
  268. model->content[0] |= (model->user_block_num << LFRFID_T5577_MAXBLOCK_SHIFT);
  269. bool redraw = true;
  270. with_view_model(
  271. app->view_write,
  272. T5577WriterModel * model,
  273. {
  274. furi_string_set(model->tag_name_str, app->temp_buffer);
  275. },
  276. redraw);
  277. FuriString *buffer = furi_string_alloc();
  278. FuriString *file_path = furi_string_alloc();
  279. furi_string_printf(
  280. file_path, "%s/%s%s", T5577_WRITER_FILE_FOLDER, furi_string_get_cstr(model->tag_name_str), T5577_WRITER_FILE_EXTENSION);
  281. Storage *storage = furi_record_open(RECORD_STORAGE);
  282. ensure_dir_exists(storage);
  283. File *data_file = storage_file_alloc(storage);
  284. if (storage_file_open(
  285. data_file, furi_string_get_cstr(file_path), FSAM_WRITE, FSOM_OPEN_ALWAYS))
  286. {
  287. furi_string_printf(buffer, "Filetype: Flipper T5577 Raw File\n");
  288. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  289. furi_string_printf(buffer, "Version: 1.0\n");
  290. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  291. furi_string_printf(buffer, "Modulation: %s\n", model->modulation.modulation_name);
  292. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  293. furi_string_printf(buffer, "RF Clock: %u\n", model->rf_clock.rf_clock_num);
  294. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  295. furi_string_printf(buffer, "Number of User Blocks: %u\n", model->user_block_num);
  296. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  297. furi_string_printf(buffer, "\nRaw Data:\n");
  298. for (int i = 0; i < LFRFID_T5577_BLOCK_COUNT; i++)
  299. {
  300. furi_string_cat_printf(
  301. buffer,
  302. "Block %u: %08lX\n",
  303. i,
  304. model->content[i]);
  305. }
  306. furi_string_push_back(buffer, '\n');
  307. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  308. storage_file_close(data_file);
  309. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu); // maybe add a pop up later
  310. }
  311. }
  312. void t5577_writer_update_config_from_load(void* context) {
  313. T5577WriterApp* app = (T5577WriterApp*)context;
  314. T5577WriterModel* my_model = view_get_model(app->view_write);
  315. for (size_t i = 0; i < COUNT_OF(all_mods); i++) {
  316. if ((my_model->content[0] & all_mods[i].mod_page_zero) == all_mods[i].mod_page_zero) {
  317. my_model->modulation_index = (uint8_t)i;
  318. my_model->modulation = all_mods[my_model->modulation_index];
  319. }
  320. }
  321. for (size_t i = 0; i < COUNT_OF(all_rf_clocks); i++) {
  322. if ((my_model->content[0] & all_rf_clocks[i].clock_page_zero) == all_rf_clocks[i].clock_page_zero) {
  323. my_model->rf_clock_index = (uint8_t)i;
  324. my_model->rf_clock = all_rf_clocks[my_model->rf_clock_index];
  325. }
  326. }
  327. my_model->user_block_num = (my_model->content[0] >> LFRFID_T5577_MAXBLOCK_SHIFT) & 0xF;
  328. memset(my_model->data_loaded, true, sizeof(my_model->data_loaded)); // Everything is loaded
  329. }
  330. static const char* edit_block_data_config_label = "Block Data";
  331. void uint32_to_byte_buffer(uint32_t block_data, uint8_t byte_buffer[4]) {
  332. byte_buffer[0] = (block_data >> 24) & 0xFF;
  333. byte_buffer[1] = (block_data >> 16) & 0xFF;
  334. byte_buffer[2] = (block_data >> 8) & 0xFF;
  335. byte_buffer[3] = block_data & 0xFF;
  336. }
  337. uint32_t byte_buffer_to_uint32(uint8_t byte_buffer[4]) {
  338. uint32_t block_data = 0;
  339. block_data |= ((uint32_t)byte_buffer[0] << 24);
  340. block_data |= ((uint32_t)byte_buffer[1] << 16);
  341. block_data |= ((uint32_t)byte_buffer[2] << 8);
  342. block_data |= ((uint32_t)byte_buffer[3]);
  343. return block_data;
  344. }
  345. static void t5577_writer_content_byte_input_confirmed(void* context) {
  346. T5577WriterApp* app = (T5577WriterApp*)context;
  347. T5577WriterModel* my_model = view_get_model(app->view_write);
  348. my_model->content[my_model->edit_block_slc] = byte_buffer_to_uint32(app->bytes_buffer);
  349. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  350. }
  351. static void t5577_writer_content_byte_changed(void* context) {
  352. UNUSED(context);
  353. }
  354. static void t5577_writer_config_item_clicked(void* context, uint32_t index) {
  355. T5577WriterApp* app = (T5577WriterApp*)context;
  356. T5577WriterModel* my_model = view_get_model(app->view_write);
  357. FuriString *buffer = furi_string_alloc();
  358. furi_string_printf(buffer, "Enter Block %u Data", my_model->edit_block_slc);
  359. // Our hex input UI is the 5th in the config menue.
  360. if(index == 4) {
  361. // Header to display on the text input screen.
  362. byte_input_set_header_text(app->byte_input, furi_string_get_cstr(buffer));
  363. // Copy the current name into the temporary buffer.
  364. bool redraw = false;
  365. with_view_model(
  366. app->view_write,
  367. T5577WriterModel * model,
  368. {
  369. uint32_to_byte_buffer(model->content[model->edit_block_slc],app->bytes_buffer);
  370. },
  371. redraw);
  372. // Configure the text input. When user enters text and clicks OK, key_copier_setting_text_updated be called.
  373. byte_input_set_result_callback(
  374. app->byte_input,
  375. t5577_writer_content_byte_input_confirmed,
  376. t5577_writer_content_byte_changed,
  377. app,
  378. app->bytes_buffer,
  379. app->bytes_count
  380. );
  381. // Pressing the BACK button will reload the configure screen.
  382. view_set_previous_callback(byte_input_get_view(app->byte_input), t5577_writer_navigation_config_e_callback);
  383. // Show text input dialog.
  384. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewByteInput);
  385. }
  386. }
  387. static void t5577_writer_config_enter_callback(void* context) {
  388. T5577WriterApp* app = (T5577WriterApp*)context;
  389. T5577WriterModel* my_model = view_get_model(app->view_write);
  390. variable_item_list_reset(app->variable_item_list_config);
  391. // Recreate this view every time we enter it so that it's always updated
  392. app->mod_item = variable_item_list_add(
  393. app->variable_item_list_config,
  394. modulation_config_label,
  395. COUNT_OF(modulation_names),
  396. t5577_writer_modulation_change,
  397. app);
  398. app->clock_item = variable_item_list_add(
  399. app->variable_item_list_config,
  400. rf_clock_config_label,
  401. COUNT_OF(rf_clock_choices),
  402. t5577_writer_rf_clock_change,
  403. app);
  404. app->block_num_item = variable_item_list_add(
  405. app->variable_item_list_config,
  406. user_block_num_config_label,
  407. LFRFID_T5577_BLOCK_COUNT,
  408. t5577_writer_user_block_num_change,
  409. app);
  410. app->block_slc_item = variable_item_list_add(
  411. app->variable_item_list_config,
  412. edit_block_slc_config_label,
  413. LFRFID_T5577_BLOCK_COUNT - 1,
  414. t5577_writer_edit_block_slc_change,
  415. app);
  416. app->byte_buffer_item = variable_item_list_add(
  417. app->variable_item_list_config,
  418. edit_block_data_config_label,
  419. 1,
  420. NULL,
  421. app);
  422. variable_item_list_set_enter_callback(app->variable_item_list_config, t5577_writer_config_item_clicked, app);
  423. View* view_config_i = variable_item_list_get_view(app->variable_item_list_config);
  424. variable_item_set_current_value_index(app->mod_item,my_model->modulation_index);
  425. variable_item_set_current_value_index(app->clock_item,my_model->rf_clock_index);
  426. variable_item_set_current_value_index(app->block_num_item,my_model->user_block_num - 1);
  427. variable_item_set_current_value_index(app->block_slc_item,my_model->edit_block_slc - 1);
  428. t5577_writer_modulation_change(app->mod_item);
  429. t5577_writer_rf_clock_change(app->clock_item);
  430. t5577_writer_user_block_num_change(app->block_num_item);
  431. t5577_writer_edit_block_slc_change(app->block_slc_item);
  432. view_set_previous_callback(
  433. view_config_i,
  434. t5577_writer_navigation_submenu_callback);
  435. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_i); // delete the last one
  436. view_dispatcher_add_view(
  437. app->view_dispatcher,
  438. T5577WriterViewConfigure_i,
  439. view_config_i);
  440. view_dispatcher_switch_to_view(app->view_dispatcher,T5577WriterViewConfigure_i); // recreate it
  441. }
  442. void t5577_writer_view_load_callback(void* context) {
  443. T5577WriterApp* app = (T5577WriterApp*)context;
  444. T5577WriterModel* model = view_get_model(app->view_write);
  445. DialogsFileBrowserOptions browser_options;
  446. Storage* storage = furi_record_open(RECORD_STORAGE);
  447. ensure_dir_exists(storage);
  448. File* data_file = storage_file_alloc(storage);
  449. dialog_file_browser_set_basic_options(&browser_options, T5577_WRITER_FILE_EXTENSION, &I_icon);
  450. browser_options.base_path = T5577_WRITER_FILE_FOLDER;
  451. furi_string_set(app->file_path, browser_options.base_path);
  452. FuriString* buffer = furi_string_alloc();
  453. if(dialog_file_browser_show(app->dialogs, app->file_path, app->file_path, &browser_options)) {
  454. if(storage_file_open(
  455. data_file, furi_string_get_cstr(app->file_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  456. while(!storage_file_eof(data_file)) { // fill buffer with every line because ch == '\n'
  457. char ch;
  458. furi_string_reset(buffer);
  459. while(storage_file_read(data_file, &ch, 1) && !storage_file_eof(data_file)) {
  460. furi_string_push_back(buffer, ch);
  461. if(ch == '\n') {
  462. break;
  463. }
  464. }
  465. if(furi_string_start_with(buffer, "Block ")) {
  466. uint32_t row_data_buffer = 0;
  467. char row_data_char_buffer[] = "00000000";
  468. uint32_t row_num_buffer = 0;
  469. char row_num_char_buffer[] = "0";
  470. int length = furi_string_size(buffer);
  471. char ch;
  472. int i = 0;
  473. while(i < length) {
  474. if(furi_string_get_char(buffer, i) == ':') {
  475. row_num_char_buffer[0] = furi_string_get_char(buffer, i - 1);
  476. //the number before ":" is block num
  477. i += 2; // skip a space
  478. for(size_t j = 0; j < sizeof(row_data_char_buffer); j++) {
  479. ch = furi_string_get_char(buffer, i);
  480. row_data_char_buffer[j] = ch;
  481. i++;
  482. }
  483. break;
  484. }
  485. i++;
  486. }
  487. sscanf(row_num_char_buffer, "%lx", &row_num_buffer);
  488. sscanf(row_data_char_buffer, "%lx", &row_data_buffer);
  489. model->content[row_num_buffer] = row_data_buffer;
  490. }
  491. }
  492. storage_file_close(data_file);
  493. t5577_writer_update_config_from_load(app);
  494. }
  495. storage_file_free(data_file);
  496. furi_record_close(RECORD_STORAGE);
  497. }
  498. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  499. }
  500. /**
  501. * @brief Callback when item in configuration screen is clicked.
  502. * @details This function is called when user clicks OK on an item in the text input screen.
  503. * @param context The context - T5577WriterApp object.
  504. * @param index - The index of the item that was clicked.
  505. */
  506. static void t5577_writer_view_save_callback(void* context) {
  507. T5577WriterApp* app = (T5577WriterApp*)context;
  508. // Header to display on the text input screen.
  509. text_input_set_header_text(app->text_input, tag_name_entry_text);
  510. // Copy the current name into the temporary buffer.
  511. bool redraw = false;
  512. with_view_model(
  513. app->view_write,
  514. T5577WriterModel * model,
  515. {
  516. strncpy(
  517. app->temp_buffer,
  518. furi_string_get_cstr(model->tag_name_str),
  519. app->temp_buffer_size);
  520. },
  521. redraw);
  522. // Configure the text input. When user enters text and clicks OK, t5577_writer_setting_text_updated be called.
  523. bool clear_previous_text = false;
  524. text_input_set_result_callback(
  525. app->text_input,
  526. t5577_writer_file_saver,
  527. app,
  528. app->temp_buffer,
  529. app->temp_buffer_size,
  530. clear_previous_text);
  531. // Pressing the BACK button will return to the main menu.
  532. view_set_previous_callback(
  533. text_input_get_view(app->text_input), t5577_writer_navigation_submenu_callback);
  534. // Show text input dialog.
  535. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewTextInput);
  536. }
  537. static void t5577_writer_actual_writing(void* model) {
  538. T5577WriterModel* my_model = (T5577WriterModel*)model;
  539. my_model->content[0] = 0; // clear up block 0
  540. my_model->content[0] |= my_model->modulation.mod_page_zero;
  541. my_model->content[0] |= my_model->rf_clock.clock_page_zero;
  542. my_model->content[0] |= (my_model->user_block_num << LFRFID_T5577_MAXBLOCK_SHIFT);
  543. LFRFIDT5577* data = (LFRFIDT5577*)malloc(sizeof(LFRFIDT5577));
  544. data->blocks_to_write = my_model->user_block_num;
  545. for(size_t i = 0; i < data->blocks_to_write; i++) {
  546. data->block[i] = my_model->content[i];
  547. }
  548. t5577_write(data);
  549. free(data);
  550. }
  551. /**
  552. * @brief Callback for drawing the writing screen.
  553. * @details This function is called when the screen needs to be redrawn, so that the writing command is repeated.
  554. * @param canvas The canvas to draw on.
  555. * @param model The model - MyModel object.
  556. */
  557. static void t5577_writer_view_write_callback(Canvas* canvas, void* model) {
  558. T5577WriterModel* my_model = (T5577WriterModel*) model;
  559. if (my_model->writing_repeat_times < MAX_REPEAT_WRITING_FRAMES) {
  560. t5577_writer_actual_writing(model);
  561. canvas_set_bitmap_mode(canvas, true);
  562. canvas_draw_icon(canvas, 0, 8, &I_NFC_manual_60x50);
  563. canvas_draw_str_aligned(canvas, 97, 15, AlignCenter, AlignTop, "Writing");
  564. canvas_draw_str_aligned(canvas, 94, 27, AlignCenter, AlignTop, "Hold card next");
  565. canvas_draw_str_aligned(canvas, 93, 39, AlignCenter, AlignTop, "to Flipper's back");
  566. } else {
  567. canvas_set_bitmap_mode(canvas, true);
  568. canvas_draw_icon(canvas, 0, 7, &I_RFID_dolphinsuccess_108x57);
  569. canvas_set_font(canvas, FontPrimary);
  570. canvas_draw_str(canvas, 72, 20, "Finished!");
  571. }
  572. }
  573. /**
  574. * @brief Callback for timer elapsed.
  575. * @details This function is called when the timer is elapsed. We use this to queue a redraw event.
  576. * @param context The context - T5577WriterApp object.
  577. */
  578. static void t5577_writer_view_write_timer_callback(void* context) {
  579. T5577WriterApp* app = (T5577WriterApp*)context;
  580. T5577WriterModel* model = view_get_model(app->view_write);
  581. if (model->writing_repeat_times < MAX_REPEAT_WRITING_FRAMES + ENDING_WRITING_ICON_FRAMES){
  582. model->writing_repeat_times += 1;
  583. view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdRepeatWriting);
  584. } else {
  585. view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdMaxWriteRep);
  586. }
  587. }
  588. /**
  589. * @brief Callback when the user starts the writing screen.
  590. * @details This function is called when the user enters the writing screen. We start a timer to
  591. * redraw the screen periodically. (exactly like PsychToolBox lol)
  592. * @param context The context - T5577WriterApp object.
  593. */
  594. static void t5577_writer_view_write_enter_callback(void* context) {
  595. uint32_t repeat_writing_period = furi_ms_to_ticks(200);
  596. T5577WriterApp* app = (T5577WriterApp*)context;
  597. furi_assert(app->timer == NULL);
  598. app->timer =
  599. furi_timer_alloc(t5577_writer_view_write_timer_callback, FuriTimerTypePeriodic, context);
  600. furi_timer_start(app->timer, repeat_writing_period);
  601. dolphin_deed(DolphinDeedRfidEmulate);
  602. }
  603. /**
  604. * @brief Callback when the user exits the writing screen.
  605. * @details This function is called when the user exits the writing screen. We stop the timer.
  606. * @param context The context - T5577WriterApp object.
  607. */
  608. static void t5577_writer_view_write_exit_callback(void* context) {
  609. T5577WriterApp* app = (T5577WriterApp*)context;
  610. T5577WriterModel* model = view_get_model(app->view_write);
  611. furi_timer_stop(app->timer);
  612. furi_timer_free(app->timer);
  613. app->timer = NULL;
  614. model->writing_repeat_times = 0;
  615. }
  616. /**
  617. * @brief Callback for custom events.
  618. * @details This function is called when a custom event is sent to the view dispatcher.
  619. * @param event The event id - T5577WriterEventId value.
  620. * @param context The context - T5577WriterApp object.
  621. */
  622. static bool t5577_writer_view_write_custom_event_callback(uint32_t event, void* context) {
  623. T5577WriterApp* app = (T5577WriterApp*)context;
  624. switch(event) {
  625. case T5577WriterEventIdRepeatWriting:
  626. // Redraw screen by passing true to last parameter of with_view_model.
  627. {
  628. bool redraw = true;
  629. with_view_model(
  630. app->view_write, T5577WriterModel * _model, {UNUSED(_model);}, redraw);
  631. return true;
  632. }
  633. case T5577WriterEventIdMaxWriteRep:
  634. // Process the OK button. We go to the saving scene.
  635. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  636. return true;
  637. default:
  638. return false;
  639. }
  640. }
  641. /**
  642. * @brief Allocate the t5577_writer application.
  643. * @details This function allocates the t5577_writer application resources.
  644. * @return T5577WriterApp object.
  645. */
  646. static T5577WriterApp* t5577_writer_app_alloc() {
  647. T5577WriterApp* app = (T5577WriterApp*)malloc(sizeof(T5577WriterApp));
  648. Gui* gui = furi_record_open(RECORD_GUI);
  649. app->view_dispatcher = view_dispatcher_alloc();
  650. app->dialogs = furi_record_open(RECORD_DIALOGS);
  651. app->file_path = furi_string_alloc();
  652. view_dispatcher_enable_queue(app->view_dispatcher);
  653. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  654. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  655. app->submenu = submenu_alloc();
  656. submenu_add_item(
  657. app->submenu, "Write", T5577WriterSubmenuIndexWrite, t5577_writer_submenu_callback, app);
  658. submenu_add_item(
  659. app->submenu, "Config", T5577WriterSubmenuIndexConfigure, t5577_writer_submenu_callback, app);
  660. submenu_add_item(
  661. app->submenu, "Save", T5577WriterSubmenuIndexSave, t5577_writer_submenu_callback, app);
  662. submenu_add_item(
  663. app->submenu, "Load", T5577WriterSubmenuIndexLoad, t5577_writer_submenu_callback, app);
  664. submenu_add_item(
  665. app->submenu, "About", T5577WriterSubmenuIndexAbout, t5577_writer_submenu_callback, app);
  666. view_set_previous_callback(submenu_get_view(app->submenu), t5577_writer_navigation_exit_callback);
  667. view_dispatcher_add_view(
  668. app->view_dispatcher, T5577WriterViewSubmenu, submenu_get_view(app->submenu));
  669. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  670. app->text_input = text_input_alloc();
  671. view_dispatcher_add_view(
  672. app->view_dispatcher, T5577WriterViewTextInput, text_input_get_view(app->text_input));
  673. app->view_load = view_alloc();
  674. view_set_previous_callback(app->view_load, t5577_writer_navigation_submenu_callback);
  675. view_set_enter_callback(app->view_load, t5577_writer_view_load_callback);
  676. view_set_context(app->view_load, app);
  677. view_dispatcher_add_view(
  678. app->view_dispatcher,
  679. T5577WriterViewLoad,
  680. app->view_load);
  681. app->temp_buffer_size = 32;
  682. app->temp_buffer = (char*)malloc(app->temp_buffer_size);
  683. app->view_write = view_alloc();
  684. view_set_draw_callback(app->view_write, t5577_writer_view_write_callback);
  685. view_set_previous_callback(app->view_write, t5577_writer_navigation_submenu_callback);
  686. view_set_enter_callback(app->view_write, t5577_writer_view_write_enter_callback);
  687. view_set_exit_callback(app->view_write, t5577_writer_view_write_exit_callback);
  688. view_set_context(app->view_write, app);
  689. view_set_custom_callback(app->view_write, t5577_writer_view_write_custom_event_callback);
  690. view_allocate_model(app->view_write, ViewModelTypeLockFree, sizeof(T5577WriterModel));
  691. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewWrite, app->view_write);
  692. T5577WriterModel* model = view_get_model(app->view_write); // initialize model
  693. FuriString* tag_name_str = furi_string_alloc();
  694. furi_string_set_str(tag_name_str, tag_name_default_value);
  695. model->tag_name_str = tag_name_str;
  696. initialize_model(model);
  697. initialize_rf_clock_choices(rf_clock_choices);
  698. initialize_mod_names(modulation_names);
  699. app->view_save = view_alloc();
  700. view_set_previous_callback(app->view_save, t5577_writer_navigation_submenu_callback);
  701. view_set_enter_callback(app->view_save, t5577_writer_view_save_callback);
  702. view_set_context(app->view_save, app);
  703. view_dispatcher_add_view(
  704. app->view_dispatcher,
  705. T5577WriterViewSave,
  706. app->view_save);
  707. app->bytes_count = 4;
  708. memset(app->bytes_buffer, 0, sizeof(app->bytes_buffer));
  709. app->byte_input = byte_input_alloc();
  710. view_dispatcher_add_view(
  711. app->view_dispatcher, T5577WriterViewByteInput, byte_input_get_view(app->byte_input));
  712. app->variable_item_list_config = variable_item_list_alloc();
  713. app->view_config_e = view_alloc();
  714. view_set_previous_callback(
  715. app->view_config_e,
  716. t5577_writer_navigation_submenu_callback);
  717. view_set_enter_callback(app->view_config_e, t5577_writer_config_enter_callback);
  718. view_set_context(app->view_config_e, app);
  719. view_dispatcher_add_view(
  720. app->view_dispatcher,
  721. T5577WriterViewConfigure_e,
  722. app->view_config_e);
  723. View* view_buffer = view_alloc();
  724. view_dispatcher_add_view(
  725. app->view_dispatcher,
  726. T5577WriterViewConfigure_i,
  727. view_buffer);
  728. app->widget_about = widget_alloc();
  729. widget_add_text_scroll_element(
  730. app->widget_about,
  731. 0,
  732. 0,
  733. 128,
  734. 64,
  735. "T5577 Raw Writer v0.1\n\nAuthor: @Torron\n\nGithub: https://github.com/zinongli/T5577_Raw_Writer");
  736. view_set_previous_callback(
  737. widget_get_view(app->widget_about), t5577_writer_navigation_submenu_callback);
  738. view_dispatcher_add_view(
  739. app->view_dispatcher, T5577WriterViewAbout, widget_get_view(app->widget_about));
  740. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  741. #ifdef BACKLIGHT_ON
  742. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  743. #endif
  744. return app;
  745. }
  746. /**
  747. * @brief Free the t5577_writer application.
  748. * @details This function frees the t5577_writer application resources.
  749. * @param app The t5577_writer application object.
  750. */
  751. static void t5577_writer_app_free(T5577WriterApp* app) {
  752. #ifdef BACKLIGHT_ON
  753. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  754. #endif
  755. furi_record_close(RECORD_NOTIFICATION);
  756. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewTextInput);
  757. text_input_free(app->text_input);
  758. free(app->temp_buffer);
  759. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewAbout);
  760. widget_free(app->widget_about);
  761. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewWrite);
  762. view_free(app->view_write);
  763. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewLoad);
  764. view_free(app->view_load);
  765. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_i);
  766. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  767. view_dispatcher_remove_view(app->view_dispatcher,T5577WriterViewByteInput);
  768. variable_item_list_free(app->variable_item_list_config);
  769. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSave);
  770. view_free(app->view_save);
  771. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSubmenu);
  772. submenu_free(app->submenu);
  773. view_dispatcher_free(app->view_dispatcher);
  774. furi_record_close(RECORD_GUI);
  775. free(app);
  776. }
  777. /**
  778. * @brief Main function for t5577_writer application.
  779. * @details This function is the entry point for the t5577_writer application. It should be defined in
  780. * application.fam as the entry_point setting.
  781. * @param _p Input parameter - unused
  782. * @return 0 - Success
  783. */
  784. int32_t main_t5577_writer_app(void* _p) {
  785. UNUSED(_p);
  786. T5577WriterApp* app = t5577_writer_app_alloc();
  787. view_dispatcher_run(app->view_dispatcher);
  788. t5577_writer_app_free(app);
  789. return 0;
  790. }