metroflip.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "metroflip_i.h"
  2. static bool metroflip_custom_event_callback(void* context, uint32_t event) {
  3. furi_assert(context);
  4. Metroflip* app = context;
  5. return scene_manager_handle_custom_event(app->scene_manager, event);
  6. }
  7. static bool metroflip_back_event_callback(void* context) {
  8. furi_assert(context);
  9. Metroflip* app = context;
  10. return scene_manager_handle_back_event(app->scene_manager);
  11. }
  12. Metroflip* metroflip_alloc() {
  13. Metroflip* app = malloc(sizeof(Metroflip));
  14. app->gui = furi_record_open(RECORD_GUI);
  15. //nfc device
  16. app->nfc = nfc_alloc();
  17. app->nfc_device = nfc_device_alloc();
  18. // notifs
  19. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  20. // View Dispatcher and Scene Manager
  21. app->view_dispatcher = view_dispatcher_alloc();
  22. app->scene_manager = scene_manager_alloc(&metroflip_scene_handlers, app);
  23. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  24. view_dispatcher_set_custom_event_callback(
  25. app->view_dispatcher, metroflip_custom_event_callback);
  26. view_dispatcher_set_navigation_event_callback(
  27. app->view_dispatcher, metroflip_back_event_callback);
  28. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  29. // Custom Widget
  30. app->widget = widget_alloc();
  31. view_dispatcher_add_view(
  32. app->view_dispatcher, MetroflipViewWidget, widget_get_view(app->widget));
  33. // Gui Modules
  34. app->submenu = submenu_alloc();
  35. view_dispatcher_add_view(
  36. app->view_dispatcher, MetroflipViewSubmenu, submenu_get_view(app->submenu));
  37. app->text_input = text_input_alloc();
  38. view_dispatcher_add_view(
  39. app->view_dispatcher, MetroflipViewTextInput, text_input_get_view(app->text_input));
  40. app->byte_input = byte_input_alloc();
  41. view_dispatcher_add_view(
  42. app->view_dispatcher, MetroflipViewByteInput, byte_input_get_view(app->byte_input));
  43. app->popup = popup_alloc();
  44. view_dispatcher_add_view(app->view_dispatcher, MetroflipViewPopup, popup_get_view(app->popup));
  45. app->nfc_device = nfc_device_alloc();
  46. // TextBox
  47. app->text_box = text_box_alloc();
  48. view_dispatcher_add_view(
  49. app->view_dispatcher, MetroflipViewTextBox, text_box_get_view(app->text_box));
  50. app->text_box_store = furi_string_alloc();
  51. return app;
  52. }
  53. void metroflip_free(Metroflip* app) {
  54. furi_assert(app);
  55. //nfc device
  56. nfc_free(app->nfc);
  57. nfc_device_free(app->nfc_device);
  58. //notifs
  59. furi_record_close(RECORD_NOTIFICATION);
  60. app->notifications = NULL;
  61. // Gui modules
  62. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewSubmenu);
  63. submenu_free(app->submenu);
  64. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewTextInput);
  65. text_input_free(app->text_input);
  66. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewByteInput);
  67. byte_input_free(app->byte_input);
  68. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewPopup);
  69. popup_free(app->popup);
  70. // Custom Widget
  71. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewWidget);
  72. widget_free(app->widget);
  73. // TextBox
  74. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewTextBox);
  75. text_box_free(app->text_box);
  76. furi_string_free(app->text_box_store);
  77. // View Dispatcher and Scene Manager
  78. view_dispatcher_free(app->view_dispatcher);
  79. scene_manager_free(app->scene_manager);
  80. // Records
  81. furi_record_close(RECORD_GUI);
  82. free(app);
  83. }
  84. static const NotificationSequence metroflip_app_sequence_blink_start_blue = {
  85. &message_blink_start_10,
  86. &message_blink_set_color_blue,
  87. &message_do_not_reset,
  88. NULL,
  89. };
  90. static const NotificationSequence metroflip_app_sequence_blink_stop = {
  91. &message_blink_stop,
  92. NULL,
  93. };
  94. void metroflip_app_blink_start(Metroflip* metroflip) {
  95. notification_message(metroflip->notifications, &metroflip_app_sequence_blink_start_blue);
  96. }
  97. void metroflip_app_blink_stop(Metroflip* metroflip) {
  98. notification_message(metroflip->notifications, &metroflip_app_sequence_blink_stop);
  99. }
  100. void metroflip_exit_widget_callback(GuiButtonType result, InputType type, void* context) {
  101. Metroflip* app = context;
  102. UNUSED(result);
  103. if(type == InputTypeShort) {
  104. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  105. }
  106. }
  107. // Calypso
  108. void byte_to_binary(uint8_t byte, char* bits) {
  109. for(int i = 7; i >= 0; i--) {
  110. bits[7 - i] = (byte & (1 << i)) ? '1' : '0';
  111. }
  112. bits[8] = '\0';
  113. }
  114. int binary_to_decimal(const char binary[]) {
  115. int decimal = 0;
  116. int length = strlen(binary);
  117. for(int i = 0; i < length; i++) {
  118. decimal = decimal * 2 + (binary[i] - '0');
  119. }
  120. return decimal;
  121. }
  122. void locale_format_datetime_cat(FuriString* out, const DateTime* dt, bool time) {
  123. // helper to print datetimes
  124. FuriString* s = furi_string_alloc();
  125. LocaleDateFormat date_format = locale_get_date_format();
  126. const char* separator = (date_format == LocaleDateFormatDMY) ? "." : "/";
  127. locale_format_date(s, dt, date_format, separator);
  128. furi_string_cat(out, s);
  129. if(time) {
  130. locale_format_time(s, dt, locale_get_time_format(), false);
  131. furi_string_cat_printf(out, " ");
  132. furi_string_cat(out, s);
  133. }
  134. furi_string_free(s);
  135. }
  136. // read file
  137. uint8_t read_file[5] = {0x94, 0xb2, 0x01, 0x04, 0x1D};
  138. // ^^^
  139. // |||
  140. // FID
  141. // select app
  142. uint8_t select_app[8] = {0x94, 0xA4, 0x00, 0x00, 0x02, 0x20, 0x00, 0x00};
  143. // ^^^^^^^^^
  144. // |||||||||
  145. // AID: 20XX
  146. uint8_t apdu_success[2] = {0x90, 0x00};
  147. int bit_slice_to_dec(const char* bit_representation, int start, int end) {
  148. char bit_slice[end - start + 2];
  149. strncpy(bit_slice, bit_representation + start, end - start + 1);
  150. bit_slice[end - start + 1] = '\0';
  151. return binary_to_decimal(bit_slice);
  152. }
  153. extern int32_t metroflip(void* p) {
  154. UNUSED(p);
  155. Metroflip* app = metroflip_alloc();
  156. scene_manager_set_scene_state(app->scene_manager, MetroflipSceneStart, MetroflipSceneRavKav);
  157. scene_manager_next_scene(app->scene_manager, MetroflipSceneStart);
  158. view_dispatcher_run(app->view_dispatcher);
  159. metroflip_free(app);
  160. return 0;
  161. }
  162. void dec_to_bits(char dec_representation, char* bit_representation) {
  163. int decimal = dec_representation - '0';
  164. for(int i = 7; i >= 0; --i) {
  165. bit_representation[i] = (decimal & (1 << i)) ? '1' : '0';
  166. }
  167. }