t5577_writer.c 35 KB

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