metroflip_scene_myki.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include <flipper_application.h>
  2. #include <lib/nfc/protocols/mf_desfire/mf_desfire.h>
  3. #include <stdio.h>
  4. #include "../metroflip_i.h"
  5. #include <nfc/protocols/mf_desfire/mf_desfire_poller.h>
  6. #define TAG "Metroflip:Scene:myki"
  7. static const MfDesfireApplicationId myki_app_id = {.data = {0x00, 0x11, 0xf2}};
  8. static const MfDesfireFileId myki_file_id = 0x0f;
  9. static uint8_t myki_calculate_luhn(uint64_t number) {
  10. // https://en.wikipedia.org/wiki/Luhn_algorithm
  11. // Drop existing check digit to form payload
  12. uint64_t payload = number / 10;
  13. int sum = 0;
  14. int position = 0;
  15. while(payload > 0) {
  16. int digit = payload % 10;
  17. if(position % 2 == 0) {
  18. digit *= 2;
  19. }
  20. if(digit > 9) {
  21. digit = (digit / 10) + (digit % 10);
  22. }
  23. sum += digit;
  24. payload /= 10;
  25. position++;
  26. }
  27. return (10 - (sum % 10)) % 10;
  28. }
  29. bool myki_parse(const NfcDevice* device, FuriString* parsed_data) {
  30. furi_assert(device);
  31. furi_assert(parsed_data);
  32. bool parsed = false;
  33. do {
  34. const MfDesfireData* data = nfc_device_get_data(device, NfcProtocolMfDesfire);
  35. const MfDesfireApplication* app = mf_desfire_get_application(data, &myki_app_id);
  36. if(app == NULL) break;
  37. typedef struct {
  38. uint32_t top;
  39. uint32_t bottom;
  40. } mykiFile;
  41. const MfDesfireFileSettings* file_settings =
  42. mf_desfire_get_file_settings(app, &myki_file_id);
  43. if(file_settings == NULL || file_settings->type != MfDesfireFileTypeStandard ||
  44. file_settings->data.size < sizeof(mykiFile))
  45. break;
  46. const MfDesfireFileData* file_data = mf_desfire_get_file_data(app, &myki_file_id);
  47. if(file_data == NULL) break;
  48. const mykiFile* myki_file = simple_array_cget_data(file_data->data);
  49. // All myki card numbers are prefixed with "308425"
  50. if(myki_file->top != 308425UL) break;
  51. // Card numbers are always 15 digits in length
  52. if(myki_file->bottom < 10000000UL || myki_file->bottom >= 100000000UL) break;
  53. uint64_t card_number = myki_file->top * 1000000000ULL + myki_file->bottom * 10UL;
  54. // Stored card number doesn't include check digit
  55. card_number += myki_calculate_luhn(card_number);
  56. furi_string_set(parsed_data, "\e#myki\nNo.: ");
  57. // Stylise card number according to the physical card
  58. char card_string[20];
  59. snprintf(card_string, sizeof(card_string), "%llu", card_number);
  60. // Digit count in each space-separated group
  61. static const uint8_t digit_count[] = {1, 5, 4, 4, 1};
  62. for(uint32_t i = 0, k = 0; i < COUNT_OF(digit_count); k += digit_count[i++]) {
  63. for(uint32_t j = 0; j < digit_count[i]; ++j) {
  64. furi_string_push_back(parsed_data, card_string[j + k]);
  65. }
  66. furi_string_push_back(parsed_data, ' ');
  67. }
  68. parsed = true;
  69. } while(false);
  70. return parsed;
  71. }
  72. static NfcCommand metroflip_scene_myki_poller_callback(NfcGenericEvent event, void* context) {
  73. furi_assert(event.protocol == NfcProtocolMfDesfire);
  74. Metroflip* app = context;
  75. NfcCommand command = NfcCommandContinue;
  76. FuriString* parsed_data = furi_string_alloc();
  77. Widget* widget = app->widget;
  78. furi_string_reset(app->text_box_store);
  79. const MfDesfirePollerEvent* mf_desfire_event = event.event_data;
  80. if(mf_desfire_event->type == MfDesfirePollerEventTypeReadSuccess) {
  81. nfc_device_set_data(
  82. app->nfc_device, NfcProtocolMfDesfire, nfc_poller_get_data(app->poller));
  83. if(!myki_parse(app->nfc_device, parsed_data)) {
  84. furi_string_reset(app->text_box_store);
  85. FURI_LOG_I(TAG, "Unknown card type");
  86. furi_string_printf(parsed_data, "\e#Unknown card\n");
  87. }
  88. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  89. widget_add_button_element(
  90. widget, GuiButtonTypeRight, "Exit", metroflip_exit_widget_callback, app);
  91. furi_string_free(parsed_data);
  92. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  93. metroflip_app_blink_stop(app);
  94. command = NfcCommandStop;
  95. } else if(mf_desfire_event->type == MfDesfirePollerEventTypeReadFailed) {
  96. view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventPollerSuccess);
  97. command = NfcCommandContinue;
  98. }
  99. return command;
  100. }
  101. void metroflip_scene_myki_on_enter(void* context) {
  102. Metroflip* app = context;
  103. dolphin_deed(DolphinDeedNfcRead);
  104. // Setup view
  105. Popup* popup = app->popup;
  106. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  107. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  108. // Start worker
  109. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  110. nfc_scanner_alloc(app->nfc);
  111. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolMfDesfire);
  112. nfc_poller_start(app->poller, metroflip_scene_myki_poller_callback, app);
  113. metroflip_app_blink_start(app);
  114. }
  115. bool metroflip_scene_myki_on_event(void* context, SceneManagerEvent event) {
  116. Metroflip* app = context;
  117. bool consumed = false;
  118. if(event.type == SceneManagerEventTypeCustom) {
  119. if(event.event == MetroflipCustomEventCardDetected) {
  120. Popup* popup = app->popup;
  121. popup_set_header(popup, "DON'T\nMOVE", 68, 30, AlignLeft, AlignTop);
  122. consumed = true;
  123. } else if(event.event == MetroflipCustomEventCardLost) {
  124. Popup* popup = app->popup;
  125. popup_set_header(popup, "Card \n lost", 68, 30, AlignLeft, AlignTop);
  126. consumed = true;
  127. } else if(event.event == MetroflipCustomEventWrongCard) {
  128. Popup* popup = app->popup;
  129. popup_set_header(popup, "WRONG \n CARD", 68, 30, AlignLeft, AlignTop);
  130. consumed = true;
  131. } else if(event.event == MetroflipCustomEventPollerFail) {
  132. Popup* popup = app->popup;
  133. popup_set_header(popup, "Failed", 68, 30, AlignLeft, AlignTop);
  134. consumed = true;
  135. }
  136. } else if(event.type == SceneManagerEventTypeBack) {
  137. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  138. consumed = true;
  139. }
  140. return consumed;
  141. }
  142. void metroflip_scene_myki_on_exit(void* context) {
  143. Metroflip* app = context;
  144. widget_reset(app->widget);
  145. metroflip_app_blink_stop(app);
  146. if(app->poller) {
  147. nfc_poller_stop(app->poller);
  148. nfc_poller_free(app->poller);
  149. }
  150. }