metroflip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. // key cache
  19. app->mfc_key_cache = mf_classic_key_cache_alloc();
  20. // notifs
  21. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  22. // View Dispatcher and Scene Manager
  23. app->view_dispatcher = view_dispatcher_alloc();
  24. app->scene_manager = scene_manager_alloc(&metroflip_scene_handlers, app);
  25. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  26. view_dispatcher_set_custom_event_callback(
  27. app->view_dispatcher, metroflip_custom_event_callback);
  28. view_dispatcher_set_navigation_event_callback(
  29. app->view_dispatcher, metroflip_back_event_callback);
  30. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  31. // Custom Widget
  32. app->widget = widget_alloc();
  33. view_dispatcher_add_view(
  34. app->view_dispatcher, MetroflipViewWidget, widget_get_view(app->widget));
  35. // Gui Modules
  36. app->submenu = submenu_alloc();
  37. view_dispatcher_add_view(
  38. app->view_dispatcher, MetroflipViewSubmenu, submenu_get_view(app->submenu));
  39. app->text_input = text_input_alloc();
  40. view_dispatcher_add_view(
  41. app->view_dispatcher, MetroflipViewTextInput, text_input_get_view(app->text_input));
  42. app->byte_input = byte_input_alloc();
  43. view_dispatcher_add_view(
  44. app->view_dispatcher, MetroflipViewByteInput, byte_input_get_view(app->byte_input));
  45. app->popup = popup_alloc();
  46. view_dispatcher_add_view(app->view_dispatcher, MetroflipViewPopup, popup_get_view(app->popup));
  47. app->nfc_device = nfc_device_alloc();
  48. // TextBox
  49. app->text_box = text_box_alloc();
  50. view_dispatcher_add_view(
  51. app->view_dispatcher, MetroflipViewTextBox, text_box_get_view(app->text_box));
  52. app->text_box_store = furi_string_alloc();
  53. return app;
  54. }
  55. void metroflip_free(Metroflip* app) {
  56. furi_assert(app);
  57. //nfc device
  58. nfc_free(app->nfc);
  59. nfc_device_free(app->nfc_device);
  60. // key cache
  61. mf_classic_key_cache_free(app->mfc_key_cache);
  62. //notifs
  63. furi_record_close(RECORD_NOTIFICATION);
  64. app->notifications = NULL;
  65. // Gui modules
  66. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewSubmenu);
  67. submenu_free(app->submenu);
  68. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewTextInput);
  69. text_input_free(app->text_input);
  70. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewByteInput);
  71. byte_input_free(app->byte_input);
  72. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewPopup);
  73. popup_free(app->popup);
  74. // Custom Widget
  75. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewWidget);
  76. widget_free(app->widget);
  77. // TextBox
  78. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewTextBox);
  79. text_box_free(app->text_box);
  80. furi_string_free(app->text_box_store);
  81. // View Dispatcher and Scene Manager
  82. view_dispatcher_free(app->view_dispatcher);
  83. scene_manager_free(app->scene_manager);
  84. // Records
  85. furi_record_close(RECORD_GUI);
  86. free(app);
  87. }
  88. static const NotificationSequence metroflip_app_sequence_blink_start_blue = {
  89. &message_blink_start_10,
  90. &message_blink_set_color_blue,
  91. &message_do_not_reset,
  92. NULL,
  93. };
  94. static const NotificationSequence metroflip_app_sequence_blink_stop = {
  95. &message_blink_stop,
  96. NULL,
  97. };
  98. void metroflip_app_blink_start(Metroflip* metroflip) {
  99. notification_message(metroflip->notifications, &metroflip_app_sequence_blink_start_blue);
  100. }
  101. void metroflip_app_blink_stop(Metroflip* metroflip) {
  102. notification_message(metroflip->notifications, &metroflip_app_sequence_blink_stop);
  103. }
  104. void metroflip_exit_widget_callback(GuiButtonType result, InputType type, void* context) {
  105. Metroflip* app = context;
  106. UNUSED(result);
  107. if(type == InputTypeShort) {
  108. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  109. }
  110. }
  111. // Calypso
  112. void byte_to_binary(uint8_t byte, char* bits) {
  113. for(int i = 7; i >= 0; i--) {
  114. bits[7 - i] = (byte & (1 << i)) ? '1' : '0';
  115. }
  116. bits[8] = '\0';
  117. }
  118. int binary_to_decimal(const char binary[]) {
  119. int decimal = 0;
  120. int length = strlen(binary);
  121. for(int i = 0; i < length; i++) {
  122. decimal = decimal * 2 + (binary[i] - '0');
  123. }
  124. return decimal;
  125. }
  126. void locale_format_datetime_cat(FuriString* out, const DateTime* dt, bool time) {
  127. // helper to print datetimes
  128. FuriString* s = furi_string_alloc();
  129. LocaleDateFormat date_format = locale_get_date_format();
  130. const char* separator = (date_format == LocaleDateFormatDMY) ? "." : "/";
  131. locale_format_date(s, dt, date_format, separator);
  132. furi_string_cat(out, s);
  133. if(time) {
  134. locale_format_time(s, dt, locale_get_time_format(), false);
  135. furi_string_cat_printf(out, " ");
  136. furi_string_cat(out, s);
  137. }
  138. furi_string_free(s);
  139. }
  140. // read file
  141. uint8_t read_file[5] = {0x94, 0xb2, 0x01, 0x04, 0x1D};
  142. // ^^^
  143. // |||
  144. // FID
  145. // select app
  146. uint8_t select_app[8] = {0x94, 0xA4, 0x00, 0x00, 0x02, 0x20, 0x00, 0x00};
  147. // ^^^^^^^^^
  148. // |||||||||
  149. // AID: 20XX
  150. uint8_t apdu_success[2] = {0x90, 0x00};
  151. int bit_slice_to_dec(const char* bit_representation, int start, int end) {
  152. char bit_slice[end - start + 2];
  153. strncpy(bit_slice, bit_representation + start, end - start + 1);
  154. bit_slice[end - start + 1] = '\0';
  155. return binary_to_decimal(bit_slice);
  156. }
  157. extern int32_t metroflip(void* p) {
  158. UNUSED(p);
  159. Metroflip* app = metroflip_alloc();
  160. scene_manager_set_scene_state(app->scene_manager, MetroflipSceneStart, MetroflipSceneRavKav);
  161. scene_manager_next_scene(app->scene_manager, MetroflipSceneStart);
  162. view_dispatcher_run(app->view_dispatcher);
  163. metroflip_free(app);
  164. return 0;
  165. }
  166. void dec_to_bits(char dec_representation, char* bit_representation) {
  167. int decimal = dec_representation - '0';
  168. for(int i = 7; i >= 0; --i) {
  169. bit_representation[i] = (decimal & (1 << i)) ? '1' : '0';
  170. }
  171. }
  172. KeyfileManager manage_keyfiles(char uid_str[]) {
  173. Storage* storage = furi_record_open(RECORD_STORAGE);
  174. File* source = storage_file_alloc(storage);
  175. char source_path[64];
  176. FURI_LOG_I("TAG", "%s", uid_str);
  177. size_t source_required_size =
  178. strlen("/ext/nfc/.cache/") + strlen(uid_str) + strlen(".keys") + 1;
  179. snprintf(source_path, source_required_size, "/ext/nfc/.cache/%s.keys", uid_str);
  180. bool cache_file = storage_file_open(source, source_path, FSAM_READ, FSOM_OPEN_EXISTING);
  181. /*-----------------Open assets cache file (if exists)------------*/
  182. File* dest = storage_file_alloc(storage);
  183. char dest_path[64];
  184. size_t dest_required_size =
  185. strlen("/ext/nfc/assets/.") + strlen(uid_str) + strlen(".keys") + 1;
  186. snprintf(dest_path, dest_required_size, "/ext/nfc/assets/.%s.keys", uid_str);
  187. bool dest_cache_file = storage_file_open(dest, dest_path, FSAM_READ, FSOM_OPEN_EXISTING);
  188. /*-----------------Check cache file------------*/
  189. if(!cache_file) {
  190. /*-----------------Check assets cache file------------*/
  191. FURI_LOG_I("TAG", "cache dont exist, checking assets");
  192. if(!dest_cache_file) {
  193. FURI_LOG_I("TAG", "assets dont exist, prompting user to fix..");
  194. storage_file_close(source);
  195. storage_file_close(dest);
  196. return MISSING_KEYFILE;
  197. } else {
  198. size_t dest_file_length = storage_file_size(dest);
  199. FURI_LOG_I(
  200. "TAG", "assets exist, but cache doesnt, proceeding to copy assets to cache");
  201. // Close, then open both files
  202. storage_file_close(source);
  203. storage_file_close(dest);
  204. storage_file_open(
  205. source, source_path, FSAM_WRITE, FSOM_OPEN_ALWAYS); // create new file
  206. storage_file_open(
  207. dest, dest_path, FSAM_READ, FSOM_OPEN_EXISTING); // open existing assets keyfile
  208. FURI_LOG_I("TAG", "creating cache file at %s from %s", source_path, dest_path);
  209. /*-----Clone keyfile from assets to cache (creates temporary buffer)----*/
  210. uint8_t* cloned_buffer = malloc(dest_file_length);
  211. storage_file_read(dest, cloned_buffer, dest_file_length);
  212. storage_file_write(source, cloned_buffer, dest_file_length);
  213. free(cloned_buffer);
  214. storage_file_close(source);
  215. storage_file_close(dest);
  216. return SUCCESSFUL;
  217. }
  218. } else {
  219. size_t source_file_length = storage_file_size(source);
  220. if(source_file_length > 1216) {
  221. FURI_LOG_I("TAG", "cache exist, creating assets cache if not already exists");
  222. storage_file_close(dest);
  223. storage_file_close(source);
  224. storage_file_open(dest, dest_path, FSAM_WRITE, FSOM_OPEN_ALWAYS);
  225. storage_file_open(source, source_path, FSAM_READ, FSOM_OPEN_EXISTING);
  226. FURI_LOG_I("TAG", "creating assets cache");
  227. /*-----Clone keyfile from assets to cache (creates temporary buffer)----*/
  228. uint8_t* cloned_buffer = malloc(source_file_length);
  229. storage_file_read(source, cloned_buffer, source_file_length);
  230. storage_file_write(dest, cloned_buffer, source_file_length);
  231. free(cloned_buffer);
  232. storage_file_close(source);
  233. storage_file_close(dest);
  234. return SUCCESSFUL;
  235. } else {
  236. FURI_LOG_I("TAG", "incomplete cache file, aborting.");
  237. storage_file_close(source);
  238. storage_file_close(dest);
  239. return INCOMPLETE_KEYFILE;
  240. }
  241. }
  242. FURI_LOG_I("TAG", "proceeding to read");
  243. storage_file_close(source);
  244. storage_file_close(dest);
  245. }