metroflip_scene_itso.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* itso.c - Parser for ITSO cards (United Kingdom). */
  2. #include "../metroflip_i.h"
  3. #include <flipper_application.h>
  4. #include <lib/nfc/protocols/mf_desfire/mf_desfire.h>
  5. #include <lib/nfc/protocols/mf_desfire/mf_desfire_poller.h>
  6. #include <lib/toolbox/strint.h>
  7. #include <applications/services/locale/locale.h>
  8. #include <datetime.h>
  9. #define TAG "Metroflip:Scene:ITSO"
  10. static const MfDesfireApplicationId itso_app_id = {.data = {0x16, 0x02, 0xa0}};
  11. static const MfDesfireFileId itso_file_id = 0x0f;
  12. int64_t swap_int64(int64_t val) {
  13. val = ((val << 8) & 0xFF00FF00FF00FF00ULL) | ((val >> 8) & 0x00FF00FF00FF00FFULL);
  14. val = ((val << 16) & 0xFFFF0000FFFF0000ULL) | ((val >> 16) & 0x0000FFFF0000FFFFULL);
  15. return (val << 32) | ((val >> 32) & 0xFFFFFFFFULL);
  16. }
  17. uint64_t swap_uint64(uint64_t val) {
  18. val = ((val << 8) & 0xFF00FF00FF00FF00ULL) | ((val >> 8) & 0x00FF00FF00FF00FFULL);
  19. val = ((val << 16) & 0xFFFF0000FFFF0000ULL) | ((val >> 16) & 0x0000FFFF0000FFFFULL);
  20. return (val << 32) | (val >> 32);
  21. }
  22. bool itso_parse(const NfcDevice* device, FuriString* parsed_data) {
  23. furi_assert(device);
  24. furi_assert(parsed_data);
  25. bool parsed = false;
  26. do {
  27. const MfDesfireData* data = nfc_device_get_data(device, NfcProtocolMfDesfire);
  28. const MfDesfireApplication* app = mf_desfire_get_application(data, &itso_app_id);
  29. if(app == NULL) break;
  30. typedef struct {
  31. uint64_t part1;
  32. uint64_t part2;
  33. uint64_t part3;
  34. uint64_t part4;
  35. } ItsoFile;
  36. const MfDesfireFileSettings* file_settings =
  37. mf_desfire_get_file_settings(app, &itso_file_id);
  38. if(file_settings == NULL || file_settings->type != MfDesfireFileTypeStandard ||
  39. file_settings->data.size < sizeof(ItsoFile))
  40. break;
  41. const MfDesfireFileData* file_data = mf_desfire_get_file_data(app, &itso_file_id);
  42. if(file_data == NULL) break;
  43. const ItsoFile* itso_file = simple_array_cget_data(file_data->data);
  44. uint64_t x1 = swap_uint64(itso_file->part1);
  45. uint64_t x2 = swap_uint64(itso_file->part2);
  46. char cardBuff[32];
  47. char dateBuff[18];
  48. snprintf(cardBuff, sizeof(cardBuff), "%llx%llx", x1, x2);
  49. snprintf(dateBuff, sizeof(dateBuff), "%llx", x2);
  50. char* cardp = cardBuff + 4;
  51. cardp[18] = '\0';
  52. // All itso card numbers are prefixed with "633597"
  53. if(strncmp(cardp, "633597", 6) != 0) break;
  54. char* datep = dateBuff + 12;
  55. dateBuff[17] = '\0';
  56. // DateStamp is defined in BS EN 1545 - Days passed since 01/01/1997
  57. uint32_t dateStamp;
  58. if(strint_to_uint32(datep, NULL, &dateStamp, 16) != StrintParseNoError) {
  59. return false;
  60. }
  61. uint32_t unixTimestamp = dateStamp * 24 * 60 * 60 + 852076800U;
  62. furi_string_set(parsed_data, "\e#ITSO Card\n");
  63. // Digit count in each space-separated group
  64. static const uint8_t digit_count[] = {6, 4, 4, 4};
  65. for(uint32_t i = 0, k = 0; i < COUNT_OF(digit_count); k += digit_count[i++]) {
  66. for(uint32_t j = 0; j < digit_count[i]; ++j) {
  67. furi_string_push_back(parsed_data, cardp[j + k]);
  68. }
  69. furi_string_push_back(parsed_data, ' ');
  70. }
  71. DateTime timestamp = {0};
  72. datetime_timestamp_to_datetime(unixTimestamp, &timestamp);
  73. FuriString* timestamp_str = furi_string_alloc();
  74. locale_format_date(timestamp_str, &timestamp, locale_get_date_format(), "-");
  75. furi_string_cat(parsed_data, "\nExpiry: ");
  76. furi_string_cat(parsed_data, timestamp_str);
  77. furi_string_free(timestamp_str);
  78. parsed = true;
  79. } while(false);
  80. return parsed;
  81. }
  82. static NfcCommand metroflip_scene_itso_poller_callback(NfcGenericEvent event, void* context) {
  83. furi_assert(event.protocol == NfcProtocolMfDesfire);
  84. Metroflip* app = context;
  85. NfcCommand command = NfcCommandContinue;
  86. FuriString* parsed_data = furi_string_alloc();
  87. Widget* widget = app->widget;
  88. furi_string_reset(app->text_box_store);
  89. const MfDesfirePollerEvent* mf_desfire_event = event.event_data;
  90. if(mf_desfire_event->type == MfDesfirePollerEventTypeReadSuccess) {
  91. nfc_device_set_data(
  92. app->nfc_device, NfcProtocolMfDesfire, nfc_poller_get_data(app->poller));
  93. if(!itso_parse(app->nfc_device, parsed_data)) {
  94. furi_string_reset(app->text_box_store);
  95. FURI_LOG_I(TAG, "Unknown card type");
  96. furi_string_printf(parsed_data, "\e#Unknown card\n");
  97. }
  98. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  99. widget_add_button_element(
  100. widget, GuiButtonTypeRight, "Exit", metroflip_exit_widget_callback, app);
  101. furi_string_free(parsed_data);
  102. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  103. metroflip_app_blink_stop(app);
  104. command = NfcCommandStop;
  105. } else if(mf_desfire_event->type == MfDesfirePollerEventTypeReadFailed) {
  106. view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventPollerSuccess);
  107. command = NfcCommandContinue;
  108. }
  109. return command;
  110. }
  111. void metroflip_scene_itso_on_enter(void* context) {
  112. Metroflip* app = context;
  113. dolphin_deed(DolphinDeedNfcRead);
  114. // Setup view
  115. Popup* popup = app->popup;
  116. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  117. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  118. // Start worker
  119. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  120. nfc_scanner_alloc(app->nfc);
  121. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolMfDesfire);
  122. nfc_poller_start(app->poller, metroflip_scene_itso_poller_callback, app);
  123. metroflip_app_blink_start(app);
  124. }
  125. bool metroflip_scene_itso_on_event(void* context, SceneManagerEvent event) {
  126. Metroflip* app = context;
  127. bool consumed = false;
  128. if(event.type == SceneManagerEventTypeCustom) {
  129. if(event.event == MetroflipCustomEventCardDetected) {
  130. Popup* popup = app->popup;
  131. popup_set_header(popup, "DON'T\nMOVE", 68, 30, AlignLeft, AlignTop);
  132. consumed = true;
  133. } else if(event.event == MetroflipCustomEventCardLost) {
  134. Popup* popup = app->popup;
  135. popup_set_header(popup, "Card \n lost", 68, 30, AlignLeft, AlignTop);
  136. consumed = true;
  137. } else if(event.event == MetroflipCustomEventWrongCard) {
  138. Popup* popup = app->popup;
  139. popup_set_header(popup, "WRONG \n CARD", 68, 30, AlignLeft, AlignTop);
  140. consumed = true;
  141. } else if(event.event == MetroflipCustomEventPollerFail) {
  142. Popup* popup = app->popup;
  143. popup_set_header(popup, "Failed", 68, 30, AlignLeft, AlignTop);
  144. consumed = true;
  145. }
  146. } else if(event.type == SceneManagerEventTypeBack) {
  147. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  148. consumed = true;
  149. }
  150. return consumed;
  151. }
  152. void metroflip_scene_itso_on_exit(void* context) {
  153. Metroflip* app = context;
  154. widget_reset(app->widget);
  155. metroflip_app_blink_stop(app);
  156. if(app->poller) {
  157. nfc_poller_stop(app->poller);
  158. nfc_poller_free(app->poller);
  159. }
  160. }