metroflip_scene_bip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Parser for bip card (Georgia).
  3. *
  4. * Copyright 2023 Leptoptilos <leptoptilos@icloud.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <flipper_application.h>
  20. #include "../metroflip_i.h"
  21. #include <nfc/protocols/mf_classic/mf_classic_poller_sync.h>
  22. #include <nfc/protocols/mf_classic/mf_classic.h>
  23. #include <nfc/protocols/mf_classic/mf_classic_poller.h>
  24. #include <dolphin/dolphin.h>
  25. #include <bit_lib.h>
  26. #include <furi_hal.h>
  27. #include <nfc/nfc.h>
  28. #include <nfc/nfc_device.h>
  29. #include <nfc/nfc_listener.h>
  30. #include <locale/locale.h>
  31. #define TAG "Metroflip:Scene:Bip"
  32. #define BIP_CARD_ID_SECTOR_NUMBER (0)
  33. #define BIP_BALANCE_SECTOR_NUMBER (8)
  34. #define BIP_TRIP_TIME_WINDOW_SECTOR_NUMBER (5)
  35. #define BIP_LAST_TOP_UPS_SECTOR_NUMBER (10)
  36. #define BIP_TRIPS_INFO_SECTOR_NUMBER (11)
  37. typedef struct {
  38. DateTime datetime;
  39. uint16_t amount;
  40. } BipTransaction;
  41. typedef struct {
  42. uint64_t a;
  43. uint64_t b;
  44. } MfClassicKeyPair;
  45. static const MfClassicKeyPair bip_1k_keys[] = {
  46. {.a = 0x3060206F5B0A, .b = 0xF1B9F5669CC8},
  47. {.a = 0x5EC39B022F2B, .b = 0xF662248E7E89},
  48. {.a = 0x5EC39B022F2B, .b = 0xF662248E7E89},
  49. {.a = 0x5EC39B022F2B, .b = 0xF662248E7E89},
  50. {.a = 0x5EC39B022F2B, .b = 0xF662248E7E89},
  51. {.a = 0x5EC39B022F2B, .b = 0xF662248E7E89},
  52. {.a = 0x5EC39B022F2B, .b = 0xF662248E7E89},
  53. {.a = 0x5EC39B022F2B, .b = 0xF662248E7E89},
  54. {.a = 0x3A09594C8587, .b = 0x62387B8D250D},
  55. {.a = 0xF238D78FF48F, .b = 0x9DC282D46217},
  56. {.a = 0xAFD0BA94D624, .b = 0x92EE4DC87191},
  57. {.a = 0xB35A0E4ACC09, .b = 0x756EF55E2507},
  58. {.a = 0x447AB7FD5A6B, .b = 0x932B9CB730EF},
  59. {.a = 0x1F1A0A111B5B, .b = 0xAD9E0A1CA2F7},
  60. {.a = 0xD58023BA2BDC, .b = 0x62CED42A6D87},
  61. {.a = 0x2548A443DF28, .b = 0x2ED3B15E7C0F}, /*
  62. {.a = 0x3a42f33af429, .b = 0x1fc235ac1309},
  63. {.a = 0x6338a371c0ed, .b = 0x243f160918d1},
  64. {.a = 0xf124c2578ad0, .b = 0x9afc42372af1},
  65. {.a = 0x32ac3b90ac13, .b = 0x682d401abb09},
  66. {.a = 0x4ad1e273eaf1, .b = 0x067db45454a9},
  67. {.a = 0xe2c42591368a, .b = 0x15fc4c7613fe},
  68. {.a = 0x2a3c347a1200, .b = 0x68d30288910a},
  69. {.a = 0x16f3d5ab1139, .b = 0xf59a36a2546d},
  70. {.a = 0x937a4fff3011, .b = 0x64e3c10394c2},
  71. {.a = 0x35c3d2caee88, .b = 0xb736412614af},
  72. {.a = 0x693143f10368, .b = 0x324f5df65310},
  73. {.a = 0xa3f97428dd01, .b = 0x643fb6de2217},
  74. {.a = 0x63f17a449af0, .b = 0x82f435dedf01},
  75. {.a = 0xc4652c54261c, .b = 0x0263de1278f3},
  76. {.a = 0xd49e2826664f, .b = 0x51284c3686a6},
  77. {.a = 0x3df14c8000a1, .b = 0x6a470d54127c},*/
  78. };
  79. static void bip_parse_datetime(const MfClassicBlock* block, DateTime* parsed_data) {
  80. furi_assert(block);
  81. furi_assert(parsed_data);
  82. parsed_data->day = (((block->data[1] << 8) + block->data[0]) >> 6) & 0x1f;
  83. parsed_data->month = (((block->data[1] << 8) + block->data[0]) >> 11) & 0xf;
  84. parsed_data->year = 2000 + ((((block->data[2] << 8) + block->data[1]) >> 7) & 0x1f);
  85. parsed_data->hour = (((block->data[3] << 8) + block->data[2]) >> 4) & 0x1f;
  86. parsed_data->minute = (((block->data[3] << 8) + block->data[2]) >> 9) & 0x3f;
  87. parsed_data->second = (((block->data[4] << 8) + block->data[3]) >> 7) & 0x3f;
  88. }
  89. static void bip_print_datetime(const DateTime* datetime, FuriString* str) {
  90. furi_assert(datetime);
  91. furi_assert(str);
  92. LocaleDateFormat date_format = locale_get_date_format();
  93. const char* separator = (date_format == LocaleDateFormatDMY) ? "." : "/";
  94. FuriString* date_str = furi_string_alloc();
  95. locale_format_date(date_str, datetime, date_format, separator);
  96. FuriString* time_str = furi_string_alloc();
  97. locale_format_time(time_str, datetime, locale_get_time_format(), true);
  98. furi_string_cat_printf(
  99. str, "%s %s", furi_string_get_cstr(date_str), furi_string_get_cstr(time_str));
  100. furi_string_free(date_str);
  101. furi_string_free(time_str);
  102. }
  103. static int datetime_cmp(const DateTime* dt_1, const DateTime* dt_2) {
  104. furi_assert(dt_1);
  105. furi_assert(dt_2);
  106. if(dt_1->year != dt_2->year) {
  107. return dt_1->year - dt_2->year;
  108. }
  109. if(dt_1->month != dt_2->month) {
  110. return dt_1->month - dt_2->month;
  111. }
  112. if(dt_1->day != dt_2->day) {
  113. return dt_1->day - dt_2->day;
  114. }
  115. if(dt_1->hour != dt_2->hour) {
  116. return dt_1->hour - dt_2->hour;
  117. }
  118. if(dt_1->minute != dt_2->minute) {
  119. return dt_1->minute - dt_2->minute;
  120. }
  121. if(dt_1->second != dt_2->second) {
  122. return dt_1->second - dt_2->second;
  123. }
  124. return 0;
  125. }
  126. static bool is_bip_block_empty(const MfClassicBlock* block) {
  127. furi_assert(block);
  128. // check if all but last byte are zero (last is checksum)
  129. for(size_t i = 0; i < sizeof(block->data) - 1; i++) {
  130. if(block->data[i] != 0) {
  131. return false;
  132. }
  133. }
  134. return true;
  135. }
  136. static bool
  137. bip_parse(const NfcDevice* device, FuriString* parsed_data, const MfClassicData* data) {
  138. furi_assert(device);
  139. furi_assert(parsed_data);
  140. struct {
  141. uint32_t card_id;
  142. uint16_t balance;
  143. uint16_t flags;
  144. DateTime trip_time_window;
  145. BipTransaction top_ups[3];
  146. BipTransaction charges[3];
  147. } bip_data = {0};
  148. bool parsed = false;
  149. do {
  150. // Get Card ID, little-endian 4 bytes at sector 0 block 1, bytes 4-7
  151. const uint8_t card_id_start_block_num =
  152. mf_classic_get_first_block_num_of_sector(BIP_CARD_ID_SECTOR_NUMBER);
  153. const uint8_t* block_start_ptr = &data->block[card_id_start_block_num + 1].data[0];
  154. bip_data.card_id = bit_lib_bytes_to_num_le(block_start_ptr + 4, 4);
  155. // Get balance, little-endian 2 bytes at sector 8 block 1, bytes 0-1
  156. const uint8_t balance_start_block_num =
  157. mf_classic_get_first_block_num_of_sector(BIP_BALANCE_SECTOR_NUMBER);
  158. block_start_ptr = &data->block[balance_start_block_num + 1].data[0];
  159. bip_data.balance = bit_lib_bytes_to_num_le(block_start_ptr, 2);
  160. // Get balance flags (negative balance, etc.), little-endian 2 bytes at sector 8 block 1, bytes 2-3
  161. bip_data.flags = bit_lib_bytes_to_num_le(block_start_ptr + 2, 2);
  162. // Get trip time window, proprietary format, at sector 5 block 1, bytes 0-7
  163. const uint8_t trip_time_window_start_block_num =
  164. mf_classic_get_first_block_num_of_sector(BIP_TRIP_TIME_WINDOW_SECTOR_NUMBER);
  165. const MfClassicBlock* trip_window_block_ptr =
  166. &data->block[trip_time_window_start_block_num + 1];
  167. bip_parse_datetime(trip_window_block_ptr, &bip_data.trip_time_window);
  168. // Last 3 top-ups: sector 10, ring-buffer of 3 blocks, timestamp in bytes 0-7, amount in bytes 9-10
  169. const uint8_t top_ups_start_block_num =
  170. mf_classic_get_first_block_num_of_sector(BIP_LAST_TOP_UPS_SECTOR_NUMBER);
  171. for(size_t i = 0; i < 3; i++) {
  172. const MfClassicBlock* block = &data->block[top_ups_start_block_num + i];
  173. if(is_bip_block_empty(block)) continue;
  174. BipTransaction* top_up = &bip_data.top_ups[i];
  175. bip_parse_datetime(block, &top_up->datetime);
  176. top_up->amount = bit_lib_bytes_to_num_le(&block->data[9], 2) >> 2;
  177. }
  178. // Last 3 charges (i.e. trips), sector 11, ring-buffer of 3 blocks, timestamp in bytes 0-7, amount in bytes 10-11
  179. const uint8_t trips_start_block_num =
  180. mf_classic_get_first_block_num_of_sector(BIP_TRIPS_INFO_SECTOR_NUMBER);
  181. for(size_t i = 0; i < 3; i++) {
  182. const MfClassicBlock* block = &data->block[trips_start_block_num + i];
  183. if(is_bip_block_empty(block)) continue;
  184. BipTransaction* charge = &bip_data.charges[i];
  185. bip_parse_datetime(block, &charge->datetime);
  186. charge->amount = bit_lib_bytes_to_num_le(&block->data[10], 2) >> 2;
  187. }
  188. // All data is now parsed and stored in bip_data, now print it
  189. // Print basic info
  190. furi_string_printf(
  191. parsed_data,
  192. "\e#Tarjeta Bip!\n"
  193. "Card Number: %lu\n"
  194. "Balance: $%hu (flags %hu)\n"
  195. "Current Trip Window Ends:\n @",
  196. bip_data.card_id,
  197. bip_data.balance,
  198. bip_data.flags);
  199. bip_print_datetime(&bip_data.trip_time_window, parsed_data);
  200. // Find newest top-up
  201. size_t newest_top_up = 0;
  202. for(size_t i = 1; i < 3; i++) {
  203. const DateTime* newest = &bip_data.top_ups[newest_top_up].datetime;
  204. const DateTime* current = &bip_data.top_ups[i].datetime;
  205. if(datetime_cmp(current, newest) > 0) {
  206. newest_top_up = i;
  207. }
  208. }
  209. // Print top-ups, newest first
  210. furi_string_cat_printf(parsed_data, "\n\e#Last Top-ups");
  211. for(size_t i = 0; i < 3; i++) {
  212. const BipTransaction* top_up = &bip_data.top_ups[(3u + newest_top_up - i) % 3];
  213. furi_string_cat_printf(parsed_data, "\n+$%d\n @", top_up->amount);
  214. bip_print_datetime(&top_up->datetime, parsed_data);
  215. }
  216. // Find newest charge
  217. size_t newest_charge = 0;
  218. for(size_t i = 1; i < 3; i++) {
  219. const DateTime* newest = &bip_data.charges[newest_charge].datetime;
  220. const DateTime* current = &bip_data.charges[i].datetime;
  221. if(datetime_cmp(current, newest) > 0) {
  222. newest_charge = i;
  223. }
  224. }
  225. // Print charges
  226. furi_string_cat_printf(parsed_data, "\n\e#Last Charges (Trips)");
  227. for(size_t i = 0; i < 3; i++) {
  228. const BipTransaction* charge = &bip_data.charges[(3u + newest_charge - i) % 3];
  229. furi_string_cat_printf(parsed_data, "\n-$%d\n @", charge->amount);
  230. bip_print_datetime(&charge->datetime, parsed_data);
  231. }
  232. parsed = true;
  233. } while(false);
  234. return parsed;
  235. }
  236. void metroflip_bip_widget_callback(GuiButtonType result, InputType type, void* context) {
  237. Metroflip* app = context;
  238. UNUSED(result);
  239. if(type == InputTypeShort) {
  240. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  241. }
  242. }
  243. static NfcCommand metroflip_scene_bip_poller_callback(NfcGenericEvent event, void* context) {
  244. furi_assert(context);
  245. furi_assert(event.event_data);
  246. furi_assert(event.protocol == NfcProtocolMfClassic);
  247. NfcCommand command = NfcCommandContinue;
  248. const MfClassicPollerEvent* mfc_event = event.event_data;
  249. Metroflip* app = context;
  250. if(mfc_event->type == MfClassicPollerEventTypeCardDetected) {
  251. view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventCardDetected);
  252. command = NfcCommandContinue;
  253. } else if(mfc_event->type == MfClassicPollerEventTypeCardLost) {
  254. view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventCardLost);
  255. app->sec_num = 0;
  256. command = NfcCommandStop;
  257. } else if(mfc_event->type == MfClassicPollerEventTypeRequestMode) {
  258. mfc_event->data->poller_mode.mode = MfClassicPollerModeRead;
  259. } else if(mfc_event->type == MfClassicPollerEventTypeRequestReadSector) {
  260. MfClassicKey key = {0};
  261. bit_lib_num_to_bytes_be(bip_1k_keys[app->sec_num].a, COUNT_OF(key.data), key.data);
  262. MfClassicKeyType key_type = MfClassicKeyTypeA;
  263. mfc_event->data->read_sector_request_data.sector_num = app->sec_num;
  264. mfc_event->data->read_sector_request_data.key = key;
  265. mfc_event->data->read_sector_request_data.key_type = key_type;
  266. mfc_event->data->read_sector_request_data.key_provided = true;
  267. if(app->sec_num == 16) {
  268. mfc_event->data->read_sector_request_data.key_provided = false;
  269. app->sec_num = 0;
  270. }
  271. app->sec_num++;
  272. } else if(mfc_event->type == MfClassicPollerEventTypeSuccess) {
  273. nfc_device_set_data(
  274. app->nfc_device, NfcProtocolMfClassic, nfc_poller_get_data(app->poller));
  275. const MfClassicData* mfc_data = nfc_device_get_data(app->nfc_device, NfcProtocolMfClassic);
  276. FuriString* parsed_data = furi_string_alloc();
  277. Widget* widget = app->widget;
  278. dolphin_deed(DolphinDeedNfcReadSuccess);
  279. furi_string_reset(app->text_box_store);
  280. bip_parse(app->nfc_device, parsed_data, mfc_data);
  281. metroflip_app_blink_stop(app);
  282. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  283. widget_add_button_element(
  284. widget, GuiButtonTypeRight, "Exit", metroflip_bip_widget_callback, app);
  285. furi_string_free(parsed_data);
  286. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  287. command = NfcCommandStop;
  288. } else if(mfc_event->type == MfClassicPollerEventTypeFail) {
  289. FURI_LOG_I(TAG, "fail");
  290. command = NfcCommandStop;
  291. }
  292. return command;
  293. }
  294. void metroflip_scene_bip_on_enter(void* context) {
  295. Metroflip* app = context;
  296. dolphin_deed(DolphinDeedNfcRead);
  297. app->sec_num = 0;
  298. // Setup view
  299. Popup* popup = app->popup;
  300. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  301. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  302. // Start worker
  303. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  304. nfc_scanner_alloc(app->nfc);
  305. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolMfClassic);
  306. nfc_poller_start(app->poller, metroflip_scene_bip_poller_callback, app);
  307. metroflip_app_blink_start(app);
  308. }
  309. bool metroflip_scene_bip_on_event(void* context, SceneManagerEvent event) {
  310. Metroflip* app = context;
  311. bool consumed = false;
  312. if(event.type == SceneManagerEventTypeCustom) {
  313. if(event.event == MetroflipCustomEventCardDetected) {
  314. Popup* popup = app->popup;
  315. popup_set_header(popup, "DON'T\nMOVE", 68, 30, AlignLeft, AlignTop);
  316. consumed = true;
  317. } else if(event.event == MetroflipCustomEventCardLost) {
  318. Popup* popup = app->popup;
  319. popup_set_header(popup, "Card \n lost", 68, 30, AlignLeft, AlignTop);
  320. consumed = true;
  321. } else if(event.event == MetroflipCustomEventWrongCard) {
  322. Popup* popup = app->popup;
  323. popup_set_header(popup, "WRONG \n CARD", 68, 30, AlignLeft, AlignTop);
  324. consumed = true;
  325. } else if(event.event == MetroflipCustomEventPollerFail) {
  326. Popup* popup = app->popup;
  327. popup_set_header(popup, "Failed", 68, 30, AlignLeft, AlignTop);
  328. consumed = true;
  329. }
  330. } else if(event.type == SceneManagerEventTypeBack) {
  331. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  332. consumed = true;
  333. }
  334. return consumed;
  335. }
  336. void metroflip_scene_bip_on_exit(void* context) {
  337. Metroflip* app = context;
  338. widget_reset(app->widget);
  339. if(app->poller) {
  340. nfc_poller_stop(app->poller);
  341. nfc_poller_free(app->poller);
  342. }
  343. // Clear view
  344. popup_reset(app->popup);
  345. metroflip_app_blink_stop(app);
  346. }