myki.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 MfDesfireData* data, FuriString* parsed_data) {
  32. furi_assert(parsed_data);
  33. bool parsed = false;
  34. do {
  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 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. const MfDesfireData* data = nfc_device_get_data(app->nfc_device, NfcProtocolMfDesfire);
  84. if(!myki_parse(data, parsed_data)) {
  85. furi_string_reset(app->text_box_store);
  86. FURI_LOG_I(TAG, "Unknown card type");
  87. furi_string_printf(parsed_data, "\e#Unknown card\n");
  88. }
  89. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  90. widget_add_button_element(
  91. widget, GuiButtonTypeRight, "Exit", metroflip_exit_widget_callback, app);
  92. widget_add_button_element(
  93. widget, GuiButtonTypeCenter, "Save", metroflip_save_widget_callback, app);
  94. furi_string_free(parsed_data);
  95. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  96. metroflip_app_blink_stop(app);
  97. command = NfcCommandStop;
  98. } else if(mf_desfire_event->type == MfDesfirePollerEventTypeReadFailed) {
  99. view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventPollerSuccess);
  100. command = NfcCommandContinue;
  101. }
  102. return command;
  103. }
  104. static void myki_on_enter(Metroflip* app) {
  105. dolphin_deed(DolphinDeedNfcRead);
  106. if(app->data_loaded) {
  107. Storage* storage = furi_record_open(RECORD_STORAGE);
  108. FlipperFormat* ff = flipper_format_file_alloc(storage);
  109. if(flipper_format_file_open_existing(ff, app->file_path)) {
  110. MfDesfireData* data = mf_desfire_alloc();
  111. mf_desfire_load(data, ff, 2);
  112. FuriString* parsed_data = furi_string_alloc();
  113. Widget* widget = app->widget;
  114. furi_string_reset(app->text_box_store);
  115. if(!myki_parse(data, parsed_data)) {
  116. furi_string_reset(app->text_box_store);
  117. FURI_LOG_I(TAG, "Unknown card type");
  118. furi_string_printf(parsed_data, "\e#Unknown card\n");
  119. }
  120. widget_add_text_scroll_element(
  121. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  122. widget_add_button_element(
  123. widget, GuiButtonTypeRight, "Exit", metroflip_exit_widget_callback, app);
  124. widget_add_button_element(
  125. widget, GuiButtonTypeCenter, "Delete", metroflip_delete_widget_callback, app);
  126. mf_desfire_free(data);
  127. furi_string_free(parsed_data);
  128. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  129. }
  130. flipper_format_free(ff);
  131. } else {
  132. // Setup view
  133. Popup* popup = app->popup;
  134. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  135. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  136. // Start worker
  137. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  138. nfc_scanner_alloc(app->nfc);
  139. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolMfDesfire);
  140. nfc_poller_start(app->poller, myki_poller_callback, app);
  141. metroflip_app_blink_start(app);
  142. }
  143. }
  144. static bool myki_on_event(Metroflip* app, SceneManagerEvent event) {
  145. bool consumed = false;
  146. if(event.type == SceneManagerEventTypeCustom) {
  147. if(event.event == MetroflipCustomEventCardDetected) {
  148. Popup* popup = app->popup;
  149. popup_set_header(popup, "DON'T\nMOVE", 68, 30, AlignLeft, AlignTop);
  150. consumed = true;
  151. } else if(event.event == MetroflipCustomEventCardLost) {
  152. Popup* popup = app->popup;
  153. popup_set_header(popup, "Card \n lost", 68, 30, AlignLeft, AlignTop);
  154. consumed = true;
  155. } else if(event.event == MetroflipCustomEventWrongCard) {
  156. Popup* popup = app->popup;
  157. popup_set_header(popup, "WRONG \n CARD", 68, 30, AlignLeft, AlignTop);
  158. consumed = true;
  159. } else if(event.event == MetroflipCustomEventPollerFail) {
  160. Popup* popup = app->popup;
  161. popup_set_header(popup, "Failed", 68, 30, AlignLeft, AlignTop);
  162. consumed = true;
  163. }
  164. } else if(event.type == SceneManagerEventTypeBack) {
  165. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  166. scene_manager_set_scene_state(app->scene_manager, MetroflipSceneStart, MetroflipSceneAuto);
  167. consumed = true;
  168. }
  169. return consumed;
  170. }
  171. static void myki_on_exit(Metroflip* app) {
  172. widget_reset(app->widget);
  173. metroflip_app_blink_stop(app);
  174. if(app->poller && !app->data_loaded) {
  175. nfc_poller_stop(app->poller);
  176. nfc_poller_free(app->poller);
  177. }
  178. }
  179. /* Actual implementation of app<>plugin interface */
  180. static const MetroflipPlugin myki_plugin = {
  181. .card_name = "Myki",
  182. .plugin_on_enter = myki_on_enter,
  183. .plugin_on_event = myki_on_event,
  184. .plugin_on_exit = myki_on_exit,
  185. };
  186. /* Plugin descriptor to comply with basic plugin specification */
  187. static const FlipperAppPluginDescriptor myki_plugin_descriptor = {
  188. .appid = METROFLIP_SUPPORTED_CARD_PLUGIN_APP_ID,
  189. .ep_api_version = METROFLIP_SUPPORTED_CARD_PLUGIN_API_VERSION,
  190. .entry_point = &myki_plugin,
  191. };
  192. /* Plugin entry point - must return a pointer to const descriptor */
  193. const FlipperAppPluginDescriptor* myki_plugin_ep(void) {
  194. return &myki_plugin_descriptor;
  195. }