t5577_writer.c 34 KB

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