passy.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include "passy_i.h"
  2. #define TAG "Passy"
  3. #define PASSY_MRZ_INFO_FILENAME "mrz_info"
  4. bool passy_load_mrz_info(Passy* passy) {
  5. const char* file_header = "MRZ Info";
  6. const uint32_t file_version = 1;
  7. bool parsed = false;
  8. FlipperFormat* file = flipper_format_file_alloc(passy->storage);
  9. FuriString* path = furi_string_alloc();
  10. FuriString* temp_str = furi_string_alloc();
  11. uint32_t version = 0;
  12. do {
  13. furi_string_printf(
  14. path, "%s/%s%s", STORAGE_APP_DATA_PATH_PREFIX, PASSY_MRZ_INFO_FILENAME, ".txt");
  15. // Open file
  16. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
  17. if(!flipper_format_read_header(file, temp_str, &version)) break;
  18. if(!furi_string_equal_str(temp_str, file_header) || (version != file_version)) {
  19. break;
  20. }
  21. // passport number
  22. // dob
  23. // doe
  24. if(!flipper_format_read_string(file, "Passport Number", temp_str)) break;
  25. strncpy(
  26. passy->passport_number,
  27. furi_string_get_cstr(temp_str),
  28. PASSY_PASSPORT_NUMBER_MAX_LENGTH);
  29. if(!flipper_format_read_string(file, "Date of Birth", temp_str)) break;
  30. strncpy(passy->date_of_birth, furi_string_get_cstr(temp_str), PASSY_DOB_MAX_LENGTH);
  31. if(!flipper_format_read_string(file, "Date of Expiry", temp_str)) break;
  32. strncpy(passy->date_of_expiry, furi_string_get_cstr(temp_str), PASSY_DOE_MAX_LENGTH);
  33. parsed = true;
  34. } while(false);
  35. if(parsed) {
  36. FURI_LOG_I(TAG, "MRZ Info loaded");
  37. }
  38. furi_string_free(path);
  39. furi_string_free(temp_str);
  40. flipper_format_free(file);
  41. return parsed;
  42. }
  43. bool passy_save_mrz_info(Passy* passy) {
  44. bool saved = false;
  45. const char* file_header = "MRZ Info";
  46. const uint32_t file_version = 1;
  47. FlipperFormat* file = flipper_format_file_alloc(passy->storage);
  48. FuriString* temp_str = furi_string_alloc();
  49. do {
  50. furi_string_printf(
  51. temp_str, "%s/%s%s", STORAGE_APP_DATA_PATH_PREFIX, PASSY_MRZ_INFO_FILENAME, ".txt");
  52. // Open file
  53. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  54. // Write header
  55. if(!flipper_format_write_header_cstr(file, file_header, file_version)) break;
  56. furi_string_set_str(temp_str, passy->passport_number);
  57. if(!flipper_format_write_string(file, "Passport Number", temp_str)) break;
  58. furi_string_set_str(temp_str, passy->date_of_birth);
  59. if(!flipper_format_write_string(file, "Date of Birth", temp_str)) break;
  60. furi_string_set_str(temp_str, passy->date_of_expiry);
  61. if(!flipper_format_write_string(file, "Date of Expiry", temp_str)) break;
  62. saved = true;
  63. } while(false);
  64. furi_string_free(temp_str);
  65. flipper_format_free(file);
  66. return saved;
  67. }
  68. bool passy_delete_mrz_info(Passy* passy) {
  69. furi_assert(passy);
  70. bool deleted = false;
  71. FuriString* file_path;
  72. file_path = furi_string_alloc();
  73. do {
  74. furi_string_printf(
  75. file_path, "%s/%s%s", STORAGE_APP_DATA_PATH_PREFIX, PASSY_MRZ_INFO_FILENAME, ".txt");
  76. if(!storage_simply_remove(passy->storage, furi_string_get_cstr(file_path))) break;
  77. memset(passy->passport_number, 0, sizeof(passy->passport_number));
  78. memset(passy->date_of_birth, 0, sizeof(passy->date_of_birth));
  79. memset(passy->date_of_expiry, 0, sizeof(passy->date_of_expiry));
  80. deleted = true;
  81. } while(false);
  82. if(!deleted) {
  83. dialog_message_show_storage_error(passy->dialogs, "Can not remove file");
  84. }
  85. furi_string_free(file_path);
  86. return deleted;
  87. }
  88. bool passy_custom_event_callback(void* context, uint32_t event) {
  89. furi_assert(context);
  90. Passy* passy = context;
  91. return scene_manager_handle_custom_event(passy->scene_manager, event);
  92. }
  93. bool passy_back_event_callback(void* context) {
  94. furi_assert(context);
  95. Passy* passy = context;
  96. return scene_manager_handle_back_event(passy->scene_manager);
  97. }
  98. void passy_tick_event_callback(void* context) {
  99. furi_assert(context);
  100. Passy* passy = context;
  101. scene_manager_handle_tick_event(passy->scene_manager);
  102. }
  103. Passy* passy_alloc() {
  104. Passy* passy = malloc(sizeof(Passy));
  105. passy->view_dispatcher = view_dispatcher_alloc();
  106. passy->scene_manager = scene_manager_alloc(&passy_scene_handlers, passy);
  107. view_dispatcher_set_event_callback_context(passy->view_dispatcher, passy);
  108. view_dispatcher_set_custom_event_callback(passy->view_dispatcher, passy_custom_event_callback);
  109. view_dispatcher_set_navigation_event_callback(
  110. passy->view_dispatcher, passy_back_event_callback);
  111. view_dispatcher_set_tick_event_callback(
  112. passy->view_dispatcher, passy_tick_event_callback, 100);
  113. passy->nfc = nfc_alloc();
  114. // Nfc device
  115. passy->nfc_device = nfc_device_alloc();
  116. nfc_device_set_loading_callback(passy->nfc_device, passy_show_loading_popup, passy);
  117. // Open GUI record
  118. passy->gui = furi_record_open(RECORD_GUI);
  119. view_dispatcher_attach_to_gui(
  120. passy->view_dispatcher, passy->gui, ViewDispatcherTypeFullscreen);
  121. // Open Notification record
  122. passy->notifications = furi_record_open(RECORD_NOTIFICATION);
  123. // Submenu
  124. passy->submenu = submenu_alloc();
  125. view_dispatcher_add_view(
  126. passy->view_dispatcher, PassyViewMenu, submenu_get_view(passy->submenu));
  127. // Popup
  128. passy->popup = popup_alloc();
  129. view_dispatcher_add_view(passy->view_dispatcher, PassyViewPopup, popup_get_view(passy->popup));
  130. // Loading
  131. passy->loading = loading_alloc();
  132. view_dispatcher_add_view(
  133. passy->view_dispatcher, PassyViewLoading, loading_get_view(passy->loading));
  134. // Text Input
  135. passy->text_input = text_input_alloc();
  136. view_dispatcher_add_view(
  137. passy->view_dispatcher, PassyViewTextInput, text_input_get_view(passy->text_input));
  138. // Number Input
  139. passy->number_input = number_input_alloc();
  140. view_dispatcher_add_view(
  141. passy->view_dispatcher, PassyViewNumberInput, number_input_get_view(passy->number_input));
  142. // TextBox
  143. passy->text_box = text_box_alloc();
  144. view_dispatcher_add_view(
  145. passy->view_dispatcher, PassyViewTextBox, text_box_get_view(passy->text_box));
  146. passy->text_box_store = furi_string_alloc();
  147. // Custom Widget
  148. passy->widget = widget_alloc();
  149. view_dispatcher_add_view(
  150. passy->view_dispatcher, PassyViewWidget, widget_get_view(passy->widget));
  151. passy->storage = furi_record_open(RECORD_STORAGE);
  152. passy->dialogs = furi_record_open(RECORD_DIALOGS);
  153. passy->load_path = furi_string_alloc();
  154. passy->DG1 = bit_buffer_alloc(PASSY_DG1_MAX_LENGTH);
  155. passy->COM = bit_buffer_alloc(PASSY_DG1_MAX_LENGTH);
  156. return passy;
  157. }
  158. void passy_free(Passy* passy) {
  159. furi_assert(passy);
  160. nfc_free(passy->nfc);
  161. // Nfc device
  162. nfc_device_free(passy->nfc_device);
  163. // Submenu
  164. view_dispatcher_remove_view(passy->view_dispatcher, PassyViewMenu);
  165. submenu_free(passy->submenu);
  166. // Popup
  167. view_dispatcher_remove_view(passy->view_dispatcher, PassyViewPopup);
  168. popup_free(passy->popup);
  169. // Loading
  170. view_dispatcher_remove_view(passy->view_dispatcher, PassyViewLoading);
  171. loading_free(passy->loading);
  172. // TextInput
  173. view_dispatcher_remove_view(passy->view_dispatcher, PassyViewTextInput);
  174. text_input_free(passy->text_input);
  175. // NumberInput
  176. view_dispatcher_remove_view(passy->view_dispatcher, PassyViewNumberInput);
  177. number_input_free(passy->number_input);
  178. // TextBox
  179. view_dispatcher_remove_view(passy->view_dispatcher, PassyViewTextBox);
  180. text_box_free(passy->text_box);
  181. furi_string_free(passy->text_box_store);
  182. // Custom Widget
  183. view_dispatcher_remove_view(passy->view_dispatcher, PassyViewWidget);
  184. widget_free(passy->widget);
  185. // View Dispatcher
  186. view_dispatcher_free(passy->view_dispatcher);
  187. // Scene Manager
  188. scene_manager_free(passy->scene_manager);
  189. // GUI
  190. furi_record_close(RECORD_GUI);
  191. passy->gui = NULL;
  192. // Notifications
  193. furi_record_close(RECORD_NOTIFICATION);
  194. passy->notifications = NULL;
  195. furi_string_free(passy->load_path);
  196. furi_record_close(RECORD_STORAGE);
  197. furi_record_close(RECORD_DIALOGS);
  198. bit_buffer_free(passy->DG1);
  199. bit_buffer_free(passy->COM);
  200. free(passy);
  201. }
  202. void passy_text_store_set(Passy* passy, const char* text, ...) {
  203. va_list args;
  204. va_start(args, text);
  205. vsnprintf(passy->text_store, sizeof(passy->text_store), text, args);
  206. va_end(args);
  207. }
  208. void passy_text_store_clear(Passy* passy) {
  209. memset(passy->text_store, 0, sizeof(passy->text_store));
  210. }
  211. static const NotificationSequence passy_sequence_blink_start_blue = {
  212. &message_blink_start_10,
  213. &message_blink_set_color_blue,
  214. &message_do_not_reset,
  215. NULL,
  216. };
  217. static const NotificationSequence passy_sequence_blink_stop = {
  218. &message_blink_stop,
  219. NULL,
  220. };
  221. void passy_blink_start(Passy* passy) {
  222. notification_message(passy->notifications, &passy_sequence_blink_start_blue);
  223. }
  224. void passy_blink_stop(Passy* passy) {
  225. notification_message(passy->notifications, &passy_sequence_blink_stop);
  226. }
  227. void passy_show_loading_popup(void* context, bool show) {
  228. Passy* passy = context;
  229. if(show) {
  230. // Raise timer priority so that animations can play
  231. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  232. view_dispatcher_switch_to_view(passy->view_dispatcher, PassyViewLoading);
  233. } else {
  234. // Restore default timer priority
  235. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  236. }
  237. }
  238. int32_t passy_app(void* p) {
  239. UNUSED(p);
  240. Passy* passy = passy_alloc();
  241. scene_manager_next_scene(passy->scene_manager, PassySceneMainMenu);
  242. view_dispatcher_run(passy->view_dispatcher);
  243. passy_free(passy);
  244. return 0;
  245. }