myki.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include "../../api/metroflip/metroflip_api.h"
  7. #include "../../metroflip_plugins.h"
  8. #define TAG "Metroflip:Scene:myki"
  9. static const MfDesfireApplicationId myki_app_id = {.data = {0x00, 0x11, 0xf2}};
  10. static const MfDesfireFileId myki_file_id = 0x0f;
  11. static uint8_t myki_calculate_luhn(uint64_t number) {
  12. // https://en.wikipedia.org/wiki/Luhn_algorithm
  13. // Drop existing check digit to form payload
  14. uint64_t payload = number / 10;
  15. int sum = 0;
  16. int position = 0;
  17. while(payload > 0) {
  18. int digit = payload % 10;
  19. if(position % 2 == 0) {
  20. digit *= 2;
  21. }
  22. if(digit > 9) {
  23. digit = (digit / 10) + (digit % 10);
  24. }
  25. sum += digit;
  26. payload /= 10;
  27. position++;
  28. }
  29. return (10 - (sum % 10)) % 10;
  30. }
  31. bool myki_parse(const NfcDevice* device, FuriString* parsed_data) {
  32. furi_assert(device);
  33. furi_assert(parsed_data);
  34. bool parsed = false;
  35. do {
  36. const MfDesfireData* data = nfc_device_get_data(device, NfcProtocolMfDesfire);
  37. const MfDesfireApplication* app = mf_desfire_get_application(data, &myki_app_id);
  38. if(app == NULL) break;
  39. typedef struct {
  40. uint32_t top;
  41. uint32_t bottom;
  42. } mykiFile;
  43. const MfDesfireFileSettings* file_settings =
  44. mf_desfire_get_file_settings(app, &myki_file_id);
  45. if(file_settings == NULL || file_settings->type != MfDesfireFileTypeStandard ||
  46. file_settings->data.size < sizeof(mykiFile))
  47. break;
  48. const MfDesfireFileData* file_data = mf_desfire_get_file_data(app, &myki_file_id);
  49. if(file_data == NULL) break;
  50. const mykiFile* myki_file = simple_array_cget_data(file_data->data);
  51. // All myki card numbers are prefixed with "308425"
  52. if(myki_file->top != 308425UL) break;
  53. // Card numbers are always 15 digits in length
  54. if(myki_file->bottom < 10000000UL || myki_file->bottom >= 100000000UL) break;
  55. uint64_t card_number = myki_file->top * 1000000000ULL + myki_file->bottom * 10UL;
  56. // Stored card number doesn't include check digit
  57. card_number += myki_calculate_luhn(card_number);
  58. furi_string_set(parsed_data, "\e#myki\nNo.: ");
  59. // Stylise card number according to the physical card
  60. char card_string[20];
  61. snprintf(card_string, sizeof(card_string), "%llu", card_number);
  62. // Digit count in each space-separated group
  63. static const uint8_t digit_count[] = {1, 5, 4, 4, 1};
  64. for(uint32_t i = 0, k = 0; i < COUNT_OF(digit_count); k += digit_count[i++]) {
  65. for(uint32_t j = 0; j < digit_count[i]; ++j) {
  66. furi_string_push_back(parsed_data, card_string[j + k]);
  67. }
  68. furi_string_push_back(parsed_data, ' ');
  69. }
  70. parsed = true;
  71. } while(false);
  72. return parsed;
  73. }
  74. static NfcCommand myki_poller_callback(NfcGenericEvent event, void* context) {
  75. furi_assert(event.protocol == NfcProtocolMfDesfire);
  76. Metroflip* app = context;
  77. NfcCommand command = NfcCommandContinue;
  78. FuriString* parsed_data = furi_string_alloc();
  79. Widget* widget = app->widget;
  80. furi_string_reset(app->text_box_store);
  81. const MfDesfirePollerEvent* mf_desfire_event = event.event_data;
  82. if(mf_desfire_event->type == MfDesfirePollerEventTypeReadSuccess) {
  83. nfc_device_set_data(
  84. app->nfc_device, NfcProtocolMfDesfire, nfc_poller_get_data(app->poller));
  85. if(!myki_parse(app->nfc_device, parsed_data)) {
  86. furi_string_reset(app->text_box_store);
  87. FURI_LOG_I(TAG, "Unknown card type");
  88. furi_string_printf(parsed_data, "\e#Unknown card\n");
  89. }
  90. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  91. widget_add_button_element(
  92. widget, GuiButtonTypeRight, "Exit", metroflip_exit_widget_callback, app);
  93. furi_string_free(parsed_data);
  94. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  95. metroflip_app_blink_stop(app);
  96. command = NfcCommandStop;
  97. } else if(mf_desfire_event->type == MfDesfirePollerEventTypeReadFailed) {
  98. view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventPollerSuccess);
  99. command = NfcCommandContinue;
  100. }
  101. return command;
  102. }
  103. static void myki_on_enter(Metroflip* app) {
  104. dolphin_deed(DolphinDeedNfcRead);
  105. // Setup view
  106. Popup* popup = app->popup;
  107. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  108. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  109. // Start worker
  110. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  111. nfc_scanner_alloc(app->nfc);
  112. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolMfDesfire);
  113. nfc_poller_start(app->poller, myki_poller_callback, app);
  114. metroflip_app_blink_start(app);
  115. }
  116. static bool myki_on_event(Metroflip* app, SceneManagerEvent event) {
  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. static void myki_on_exit(Metroflip* app) {
  143. widget_reset(app->widget);
  144. metroflip_app_blink_stop(app);
  145. if(app->poller) {
  146. nfc_poller_stop(app->poller);
  147. nfc_poller_free(app->poller);
  148. }
  149. }
  150. /* Actual implementation of app<>plugin interface */
  151. static const MetroflipPlugin myki_plugin = {
  152. .card_name = "Myki",
  153. .plugin_on_enter = myki_on_enter,
  154. .plugin_on_event = myki_on_event,
  155. .plugin_on_exit = myki_on_exit,
  156. };
  157. /* Plugin descriptor to comply with basic plugin specification */
  158. static const FlipperAppPluginDescriptor myki_plugin_descriptor = {
  159. .appid = METROFLIP_SUPPORTED_CARD_PLUGIN_APP_ID,
  160. .ep_api_version = METROFLIP_SUPPORTED_CARD_PLUGIN_API_VERSION,
  161. .entry_point = &myki_plugin,
  162. };
  163. /* Plugin entry point - must return a pointer to const descriptor */
  164. const FlipperAppPluginDescriptor* myki_plugin_ep(void) {
  165. return &myki_plugin_descriptor;
  166. }