t5577_writer.c 34 KB

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