nfc_maker.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "nfc_maker.h"
  2. const NfcDataGeneratorType ntag_generators[NtagMAX] = {
  3. [Ntag203] = NfcDataGeneratorTypeNTAG203,
  4. [Ntag213] = NfcDataGeneratorTypeNTAG213,
  5. [Ntag215] = NfcDataGeneratorTypeNTAG215,
  6. [Ntag216] = NfcDataGeneratorTypeNTAG216,
  7. [NtagI2C1K] = NfcDataGeneratorTypeNTAGI2C1k,
  8. [NtagI2C2K] = NfcDataGeneratorTypeNTAGI2C2k,
  9. };
  10. const char* ntag_names[NtagMAX] = {
  11. [Ntag203] = "NTAG203",
  12. [Ntag213] = "NTAG213",
  13. [Ntag215] = "NTAG215",
  14. [Ntag216] = "NTAG216",
  15. [NtagI2C1K] = "NTAG I2C 1K",
  16. [NtagI2C2K] = "NTAG I2C 2K",
  17. };
  18. const size_t ntag_sizes[NtagMAX] = {
  19. [Ntag203] = 0x12 * NTAG_DATA_AREA_UNIT_SIZE,
  20. [Ntag213] = 0x12 * NTAG_DATA_AREA_UNIT_SIZE,
  21. [Ntag215] = 0x3E * NTAG_DATA_AREA_UNIT_SIZE,
  22. [Ntag216] = 0x6D * NTAG_DATA_AREA_UNIT_SIZE,
  23. [NtagI2C1K] = 0x6D * NTAG_DATA_AREA_UNIT_SIZE,
  24. [NtagI2C2K] = 0xEA * NTAG_DATA_AREA_UNIT_SIZE,
  25. };
  26. static bool nfc_maker_custom_event_callback(void* context, uint32_t event) {
  27. furi_assert(context);
  28. NfcMaker* app = context;
  29. return scene_manager_handle_custom_event(app->scene_manager, event);
  30. }
  31. static bool nfc_maker_back_event_callback(void* context) {
  32. furi_assert(context);
  33. NfcMaker* app = context;
  34. return scene_manager_handle_back_event(app->scene_manager);
  35. }
  36. NfcMaker* nfc_maker_alloc() {
  37. NfcMaker* app = malloc(sizeof(NfcMaker));
  38. app->gui = furi_record_open(RECORD_GUI);
  39. // View Dispatcher and Scene Manager
  40. app->view_dispatcher = view_dispatcher_alloc();
  41. app->scene_manager = scene_manager_alloc(&nfc_maker_scene_handlers, app);
  42. view_dispatcher_enable_queue(app->view_dispatcher);
  43. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  44. view_dispatcher_set_custom_event_callback(
  45. app->view_dispatcher, nfc_maker_custom_event_callback);
  46. view_dispatcher_set_navigation_event_callback(
  47. app->view_dispatcher, nfc_maker_back_event_callback);
  48. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  49. // Gui Modules
  50. app->submenu = submenu_alloc();
  51. view_dispatcher_add_view(
  52. app->view_dispatcher, NfcMakerViewSubmenu, submenu_get_view(app->submenu));
  53. app->text_input = text_input_alloc();
  54. view_dispatcher_add_view(
  55. app->view_dispatcher, NfcMakerViewTextInput, text_input_get_view(app->text_input));
  56. app->byte_input = byte_input_alloc();
  57. view_dispatcher_add_view(
  58. app->view_dispatcher, NfcMakerViewByteInput, byte_input_get_view(app->byte_input));
  59. app->popup = popup_alloc();
  60. view_dispatcher_add_view(app->view_dispatcher, NfcMakerViewPopup, popup_get_view(app->popup));
  61. // Nfc Device
  62. app->nfc_device = nfc_device_alloc();
  63. app->ndef_buffer = malloc(MAX_NDEF_LEN);
  64. return app;
  65. }
  66. void nfc_maker_free(NfcMaker* app) {
  67. furi_assert(app);
  68. // Nfc Device
  69. nfc_device_free(app->nfc_device);
  70. free(app->ndef_buffer);
  71. // Gui modules
  72. view_dispatcher_remove_view(app->view_dispatcher, NfcMakerViewSubmenu);
  73. submenu_free(app->submenu);
  74. view_dispatcher_remove_view(app->view_dispatcher, NfcMakerViewTextInput);
  75. text_input_free(app->text_input);
  76. view_dispatcher_remove_view(app->view_dispatcher, NfcMakerViewByteInput);
  77. byte_input_free(app->byte_input);
  78. view_dispatcher_remove_view(app->view_dispatcher, NfcMakerViewPopup);
  79. popup_free(app->popup);
  80. // View Dispatcher and Scene Manager
  81. view_dispatcher_free(app->view_dispatcher);
  82. scene_manager_free(app->scene_manager);
  83. // Records
  84. furi_record_close(RECORD_GUI);
  85. free(app);
  86. }
  87. extern int32_t nfc_maker(void* p) {
  88. UNUSED(p);
  89. NfcMaker* app = nfc_maker_alloc();
  90. scene_manager_set_scene_state(app->scene_manager, NfcMakerSceneStart, NfcMakerSceneHttps);
  91. scene_manager_next_scene(app->scene_manager, NfcMakerSceneStart);
  92. view_dispatcher_run(app->view_dispatcher);
  93. nfc_maker_free(app);
  94. return 0;
  95. }