itso.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "../../api/metroflip/metroflip_api.h"
  8. #include "../../metroflip_plugins.h"
  9. #include <applications/services/locale/locale.h>
  10. #include <datetime.h>
  11. #define TAG "Metroflip:Scene:ITSO"
  12. static const MfDesfireApplicationId itso_app_id = {.data = {0x16, 0x02, 0xa0}};
  13. static const MfDesfireFileId itso_file_id = 0x0f;
  14. int64_t swap_int64(int64_t val) {
  15. val = ((val << 8) & 0xFF00FF00FF00FF00ULL) | ((val >> 8) & 0x00FF00FF00FF00FFULL);
  16. val = ((val << 16) & 0xFFFF0000FFFF0000ULL) | ((val >> 16) & 0x0000FFFF0000FFFFULL);
  17. return (val << 32) | ((val >> 32) & 0xFFFFFFFFULL);
  18. }
  19. uint64_t swap_uint64(uint64_t val) {
  20. val = ((val << 8) & 0xFF00FF00FF00FF00ULL) | ((val >> 8) & 0x00FF00FF00FF00FFULL);
  21. val = ((val << 16) & 0xFFFF0000FFFF0000ULL) | ((val >> 16) & 0x0000FFFF0000FFFFULL);
  22. return (val << 32) | (val >> 32);
  23. }
  24. bool itso_parse(const NfcDevice* device, FuriString* parsed_data) {
  25. furi_assert(device);
  26. furi_assert(parsed_data);
  27. bool parsed = false;
  28. do {
  29. const MfDesfireData* data = nfc_device_get_data(device, NfcProtocolMfDesfire);
  30. const MfDesfireApplication* app = mf_desfire_get_application(data, &itso_app_id);
  31. if(app == NULL) break;
  32. typedef struct {
  33. uint64_t part1;
  34. uint64_t part2;
  35. uint64_t part3;
  36. uint64_t part4;
  37. } ItsoFile;
  38. const MfDesfireFileSettings* file_settings =
  39. mf_desfire_get_file_settings(app, &itso_file_id);
  40. if(file_settings == NULL || file_settings->type != MfDesfireFileTypeStandard ||
  41. file_settings->data.size < sizeof(ItsoFile))
  42. break;
  43. const MfDesfireFileData* file_data = mf_desfire_get_file_data(app, &itso_file_id);
  44. if(file_data == NULL) break;
  45. const ItsoFile* itso_file = simple_array_cget_data(file_data->data);
  46. uint64_t x1 = swap_uint64(itso_file->part1);
  47. uint64_t x2 = swap_uint64(itso_file->part2);
  48. char cardBuff[32];
  49. char dateBuff[18];
  50. snprintf(cardBuff, sizeof(cardBuff), "%llx%llx", x1, x2);
  51. snprintf(dateBuff, sizeof(dateBuff), "%llx", x2);
  52. char* cardp = cardBuff + 4;
  53. cardp[18] = '\0';
  54. // All itso card numbers are prefixed with "633597"
  55. if(strncmp(cardp, "633597", 6) != 0) break;
  56. char* datep = dateBuff + 12;
  57. dateBuff[17] = '\0';
  58. // DateStamp is defined in BS EN 1545 - Days passed since 01/01/1997
  59. uint32_t dateStamp;
  60. if(strint_to_uint32(datep, NULL, &dateStamp, 16) != StrintParseNoError) {
  61. return false;
  62. }
  63. uint32_t unixTimestamp = dateStamp * 24 * 60 * 60 + 852076800U;
  64. furi_string_set(parsed_data, "\e#ITSO Card\n");
  65. // Digit count in each space-separated group
  66. static const uint8_t digit_count[] = {6, 4, 4, 4};
  67. for(uint32_t i = 0, k = 0; i < COUNT_OF(digit_count); k += digit_count[i++]) {
  68. for(uint32_t j = 0; j < digit_count[i]; ++j) {
  69. furi_string_push_back(parsed_data, cardp[j + k]);
  70. }
  71. furi_string_push_back(parsed_data, ' ');
  72. }
  73. DateTime timestamp = {0};
  74. datetime_timestamp_to_datetime(unixTimestamp, &timestamp);
  75. FuriString* timestamp_str = furi_string_alloc();
  76. locale_format_date(timestamp_str, &timestamp, locale_get_date_format(), "-");
  77. furi_string_cat(parsed_data, "\nExpiry: ");
  78. furi_string_cat(parsed_data, timestamp_str);
  79. furi_string_free(timestamp_str);
  80. parsed = true;
  81. } while(false);
  82. return parsed;
  83. }
  84. static NfcCommand itso_poller_callback(NfcGenericEvent event, void* context) {
  85. furi_assert(event.protocol == NfcProtocolMfDesfire);
  86. Metroflip* app = context;
  87. NfcCommand command = NfcCommandContinue;
  88. FuriString* parsed_data = furi_string_alloc();
  89. Widget* widget = app->widget;
  90. furi_string_reset(app->text_box_store);
  91. const MfDesfirePollerEvent* mf_desfire_event = event.event_data;
  92. if(mf_desfire_event->type == MfDesfirePollerEventTypeReadSuccess) {
  93. nfc_device_set_data(
  94. app->nfc_device, NfcProtocolMfDesfire, nfc_poller_get_data(app->poller));
  95. if(!itso_parse(app->nfc_device, parsed_data)) {
  96. furi_string_reset(app->text_box_store);
  97. FURI_LOG_I(TAG, "Unknown card type");
  98. furi_string_printf(parsed_data, "\e#Unknown card\n");
  99. }
  100. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  101. widget_add_button_element(
  102. widget, GuiButtonTypeRight, "Exit", metroflip_exit_widget_callback, app);
  103. furi_string_free(parsed_data);
  104. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  105. metroflip_app_blink_stop(app);
  106. command = NfcCommandStop;
  107. } else if(mf_desfire_event->type == MfDesfirePollerEventTypeReadFailed) {
  108. view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventPollerSuccess);
  109. command = NfcCommandContinue;
  110. }
  111. return command;
  112. }
  113. static void itso_on_enter(Metroflip* app) {
  114. dolphin_deed(DolphinDeedNfcRead);
  115. // Setup view
  116. Popup* popup = app->popup;
  117. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  118. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  119. // Start worker
  120. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  121. nfc_scanner_alloc(app->nfc);
  122. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolMfDesfire);
  123. nfc_poller_start(app->poller, itso_poller_callback, app);
  124. metroflip_app_blink_start(app);
  125. }
  126. static bool itso_on_event(Metroflip* app, SceneManagerEvent event) {
  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. static void itso_on_exit(Metroflip* app) {
  153. widget_reset(app->widget);
  154. metroflip_app_blink_stop(app);
  155. if(app->poller) {
  156. nfc_poller_stop(app->poller);
  157. nfc_poller_free(app->poller);
  158. }
  159. }
  160. /* Actual implementation of app<>plugin interface */
  161. static const MetroflipPlugin itso_plugin = {
  162. .card_name = "ITSO",
  163. .plugin_on_enter = itso_on_enter,
  164. .plugin_on_event = itso_on_event,
  165. .plugin_on_exit = itso_on_exit,
  166. };
  167. /* Plugin descriptor to comply with basic plugin specification */
  168. static const FlipperAppPluginDescriptor itso_plugin_descriptor = {
  169. .appid = METROFLIP_SUPPORTED_CARD_PLUGIN_APP_ID,
  170. .ep_api_version = METROFLIP_SUPPORTED_CARD_PLUGIN_API_VERSION,
  171. .entry_point = &itso_plugin,
  172. };
  173. /* Plugin entry point - must return a pointer to const descriptor */
  174. const FlipperAppPluginDescriptor* itso_plugin_ep(void) {
  175. return &itso_plugin_descriptor;
  176. }