metroflip_scene_bip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 = 0x3a42f33af429, .b = 0x1fc235ac1309},
  47. {.a = 0x6338a371c0ed, .b = 0x243f160918d1},
  48. {.a = 0xf124c2578ad0, .b = 0x9afc42372af1},
  49. {.a = 0x32ac3b90ac13, .b = 0x682d401abb09},
  50. {.a = 0x4ad1e273eaf1, .b = 0x067db45454a9},
  51. {.a = 0xe2c42591368a, .b = 0x15fc4c7613fe},
  52. {.a = 0x2a3c347a1200, .b = 0x68d30288910a},
  53. {.a = 0x16f3d5ab1139, .b = 0xf59a36a2546d},
  54. {.a = 0x937a4fff3011, .b = 0x64e3c10394c2},
  55. {.a = 0x35c3d2caee88, .b = 0xb736412614af},
  56. {.a = 0x693143f10368, .b = 0x324f5df65310},
  57. {.a = 0xa3f97428dd01, .b = 0x643fb6de2217},
  58. {.a = 0x63f17a449af0, .b = 0x82f435dedf01},
  59. {.a = 0xc4652c54261c, .b = 0x0263de1278f3},
  60. {.a = 0xd49e2826664f, .b = 0x51284c3686a6},
  61. {.a = 0x3df14c8000a1, .b = 0x6a470d54127c},
  62. };
  63. static void bip_parse_datetime(const MfClassicBlock* block, DateTime* parsed_data) {
  64. furi_assert(block);
  65. furi_assert(parsed_data);
  66. parsed_data->day = (((block->data[1] << 8) + block->data[0]) >> 6) & 0x1f;
  67. parsed_data->month = (((block->data[1] << 8) + block->data[0]) >> 11) & 0xf;
  68. parsed_data->year = 2000 + ((((block->data[2] << 8) + block->data[1]) >> 7) & 0x1f);
  69. parsed_data->hour = (((block->data[3] << 8) + block->data[2]) >> 4) & 0x1f;
  70. parsed_data->minute = (((block->data[3] << 8) + block->data[2]) >> 9) & 0x3f;
  71. parsed_data->second = (((block->data[4] << 8) + block->data[3]) >> 7) & 0x3f;
  72. }
  73. static void bip_print_datetime(const DateTime* datetime, FuriString* str) {
  74. furi_assert(datetime);
  75. furi_assert(str);
  76. LocaleDateFormat date_format = locale_get_date_format();
  77. const char* separator = (date_format == LocaleDateFormatDMY) ? "." : "/";
  78. FuriString* date_str = furi_string_alloc();
  79. locale_format_date(date_str, datetime, date_format, separator);
  80. FuriString* time_str = furi_string_alloc();
  81. locale_format_time(time_str, datetime, locale_get_time_format(), true);
  82. furi_string_cat_printf(
  83. str, "%s %s", furi_string_get_cstr(date_str), furi_string_get_cstr(time_str));
  84. furi_string_free(date_str);
  85. furi_string_free(time_str);
  86. }
  87. static int datetime_cmp(const DateTime* dt_1, const DateTime* dt_2) {
  88. furi_assert(dt_1);
  89. furi_assert(dt_2);
  90. if(dt_1->year != dt_2->year) {
  91. return dt_1->year - dt_2->year;
  92. }
  93. if(dt_1->month != dt_2->month) {
  94. return dt_1->month - dt_2->month;
  95. }
  96. if(dt_1->day != dt_2->day) {
  97. return dt_1->day - dt_2->day;
  98. }
  99. if(dt_1->hour != dt_2->hour) {
  100. return dt_1->hour - dt_2->hour;
  101. }
  102. if(dt_1->minute != dt_2->minute) {
  103. return dt_1->minute - dt_2->minute;
  104. }
  105. if(dt_1->second != dt_2->second) {
  106. return dt_1->second - dt_2->second;
  107. }
  108. return 0;
  109. }
  110. static bool is_bip_block_empty(const MfClassicBlock* block) {
  111. furi_assert(block);
  112. // check if all but last byte are zero (last is checksum)
  113. for(size_t i = 0; i < sizeof(block->data) - 1; i++) {
  114. if(block->data[i] != 0) {
  115. return false;
  116. }
  117. }
  118. return true;
  119. }
  120. static bool
  121. bip_parse(const NfcDevice* device, FuriString* parsed_data, const MfClassicData* data) {
  122. furi_assert(device);
  123. furi_assert(parsed_data);
  124. struct {
  125. uint32_t card_id;
  126. uint16_t balance;
  127. uint16_t flags;
  128. DateTime trip_time_window;
  129. BipTransaction top_ups[3];
  130. BipTransaction charges[3];
  131. } bip_data = {0};
  132. bool parsed = false;
  133. do {
  134. // verify sector 0 key A
  135. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, 0);
  136. if(data->type != MfClassicType1k) break;
  137. uint64_t key = bit_lib_bytes_to_num_be(sec_tr->key_a.data, 6);
  138. if(key != bip_1k_keys[0].a) {
  139. break;
  140. }
  141. // Get Card ID, little-endian 4 bytes at sector 0 block 1, bytes 4-7
  142. const uint8_t card_id_start_block_num =
  143. mf_classic_get_first_block_num_of_sector(BIP_CARD_ID_SECTOR_NUMBER);
  144. const uint8_t* block_start_ptr = &data->block[card_id_start_block_num + 1].data[0];
  145. bip_data.card_id = bit_lib_bytes_to_num_le(block_start_ptr + 4, 4);
  146. // Get balance, little-endian 2 bytes at sector 8 block 1, bytes 0-1
  147. const uint8_t balance_start_block_num =
  148. mf_classic_get_first_block_num_of_sector(BIP_BALANCE_SECTOR_NUMBER);
  149. block_start_ptr = &data->block[balance_start_block_num + 1].data[0];
  150. bip_data.balance = bit_lib_bytes_to_num_le(block_start_ptr, 2);
  151. // Get balance flags (negative balance, etc.), little-endian 2 bytes at sector 8 block 1, bytes 2-3
  152. bip_data.flags = bit_lib_bytes_to_num_le(block_start_ptr + 2, 2);
  153. // Get trip time window, proprietary format, at sector 5 block 1, bytes 0-7
  154. const uint8_t trip_time_window_start_block_num =
  155. mf_classic_get_first_block_num_of_sector(BIP_TRIP_TIME_WINDOW_SECTOR_NUMBER);
  156. const MfClassicBlock* trip_window_block_ptr =
  157. &data->block[trip_time_window_start_block_num + 1];
  158. bip_parse_datetime(trip_window_block_ptr, &bip_data.trip_time_window);
  159. // Last 3 top-ups: sector 10, ring-buffer of 3 blocks, timestamp in bytes 0-7, amount in bytes 9-10
  160. const uint8_t top_ups_start_block_num =
  161. mf_classic_get_first_block_num_of_sector(BIP_LAST_TOP_UPS_SECTOR_NUMBER);
  162. for(size_t i = 0; i < 3; i++) {
  163. const MfClassicBlock* block = &data->block[top_ups_start_block_num + i];
  164. if(is_bip_block_empty(block)) continue;
  165. BipTransaction* top_up = &bip_data.top_ups[i];
  166. bip_parse_datetime(block, &top_up->datetime);
  167. top_up->amount = bit_lib_bytes_to_num_le(&block->data[9], 2) >> 2;
  168. }
  169. // Last 3 charges (i.e. trips), sector 11, ring-buffer of 3 blocks, timestamp in bytes 0-7, amount in bytes 10-11
  170. const uint8_t trips_start_block_num =
  171. mf_classic_get_first_block_num_of_sector(BIP_TRIPS_INFO_SECTOR_NUMBER);
  172. for(size_t i = 0; i < 3; i++) {
  173. const MfClassicBlock* block = &data->block[trips_start_block_num + i];
  174. if(is_bip_block_empty(block)) continue;
  175. BipTransaction* charge = &bip_data.charges[i];
  176. bip_parse_datetime(block, &charge->datetime);
  177. charge->amount = bit_lib_bytes_to_num_le(&block->data[10], 2) >> 2;
  178. }
  179. // All data is now parsed and stored in bip_data, now print it
  180. // Print basic info
  181. furi_string_printf(
  182. parsed_data,
  183. "\e#Tarjeta Bip!\n"
  184. "Card Number: %lu\n"
  185. "Balance: $%hu (flags %hu)\n"
  186. "Current Trip Window Ends:\n @",
  187. bip_data.card_id,
  188. bip_data.balance,
  189. bip_data.flags);
  190. bip_print_datetime(&bip_data.trip_time_window, parsed_data);
  191. // Find newest top-up
  192. size_t newest_top_up = 0;
  193. for(size_t i = 1; i < 3; i++) {
  194. const DateTime* newest = &bip_data.top_ups[newest_top_up].datetime;
  195. const DateTime* current = &bip_data.top_ups[i].datetime;
  196. if(datetime_cmp(current, newest) > 0) {
  197. newest_top_up = i;
  198. }
  199. }
  200. // Print top-ups, newest first
  201. furi_string_cat_printf(parsed_data, "\n\e#Last Top-ups");
  202. for(size_t i = 0; i < 3; i++) {
  203. const BipTransaction* top_up = &bip_data.top_ups[(3u + newest_top_up - i) % 3];
  204. furi_string_cat_printf(parsed_data, "\n+$%d\n @", top_up->amount);
  205. bip_print_datetime(&top_up->datetime, parsed_data);
  206. }
  207. // Find newest charge
  208. size_t newest_charge = 0;
  209. for(size_t i = 1; i < 3; i++) {
  210. const DateTime* newest = &bip_data.charges[newest_charge].datetime;
  211. const DateTime* current = &bip_data.charges[i].datetime;
  212. if(datetime_cmp(current, newest) > 0) {
  213. newest_charge = i;
  214. }
  215. }
  216. // Print charges
  217. furi_string_cat_printf(parsed_data, "\n\e#Last Charges (Trips)");
  218. for(size_t i = 0; i < 3; i++) {
  219. const BipTransaction* charge = &bip_data.charges[(3u + newest_charge - i) % 3];
  220. furi_string_cat_printf(parsed_data, "\n-$%d\n @", charge->amount);
  221. bip_print_datetime(&charge->datetime, parsed_data);
  222. }
  223. parsed = true;
  224. } while(false);
  225. return parsed;
  226. }
  227. void metroflip_bip_widget_callback(GuiButtonType result, InputType type, void* context) {
  228. Metroflip* app = context;
  229. UNUSED(result);
  230. if(type == InputTypeShort) {
  231. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  232. }
  233. }
  234. static NfcCommand metroflip_scene_bip_poller_callback(NfcGenericEvent event, void* context) {
  235. furi_assert(context);
  236. furi_assert(event.event_data);
  237. furi_assert(event.protocol == NfcProtocolMfClassic);
  238. NfcCommand command = NfcCommandContinue;
  239. const MfClassicPollerEvent* mfc_event = event.event_data;
  240. Metroflip* app = context;
  241. if(mfc_event->type == MfClassicPollerEventTypeCardDetected) {
  242. view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventCardDetected);
  243. command = NfcCommandContinue;
  244. } else if(mfc_event->type == MfClassicPollerEventTypeCardLost) {
  245. view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventCardLost);
  246. app->sec_num = 0;
  247. command = NfcCommandStop;
  248. } else if(mfc_event->type == MfClassicPollerEventTypeRequestMode) {
  249. mfc_event->data->poller_mode.mode = MfClassicPollerModeRead;
  250. } else if(mfc_event->type == MfClassicPollerEventTypeRequestReadSector) {
  251. MfClassicKey key = {0};
  252. bit_lib_num_to_bytes_be(bip_1k_keys[app->sec_num].a, COUNT_OF(key.data), key.data);
  253. MfClassicKeyType key_type = MfClassicKeyTypeA;
  254. mfc_event->data->read_sector_request_data.sector_num = app->sec_num;
  255. mfc_event->data->read_sector_request_data.key = key;
  256. mfc_event->data->read_sector_request_data.key_type = key_type;
  257. mfc_event->data->read_sector_request_data.key_provided = true;
  258. if(app->sec_num == 16) {
  259. mfc_event->data->read_sector_request_data.key_provided = false;
  260. app->sec_num = 0;
  261. }
  262. app->sec_num++;
  263. } else if(mfc_event->type == MfClassicPollerEventTypeSuccess) {
  264. nfc_device_set_data(
  265. app->nfc_device, NfcProtocolMfClassic, nfc_poller_get_data(app->poller));
  266. const MfClassicData* mfc_data = nfc_device_get_data(app->nfc_device, NfcProtocolMfClassic);
  267. FuriString* parsed_data = furi_string_alloc();
  268. Widget* widget = app->widget;
  269. dolphin_deed(DolphinDeedNfcReadSuccess);
  270. furi_string_reset(app->text_box_store);
  271. if(!bip_parse(app->nfc_device, parsed_data, mfc_data)) {
  272. furi_string_reset(app->text_box_store);
  273. FURI_LOG_I(TAG, "Unknown card type");
  274. furi_string_printf(parsed_data, "\e#Unknown card\n");
  275. }
  276. metroflip_app_blink_stop(app);
  277. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  278. widget_add_button_element(
  279. widget, GuiButtonTypeRight, "Exit", metroflip_bip_widget_callback, app);
  280. furi_string_free(parsed_data);
  281. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  282. command = NfcCommandStop;
  283. } else if(mfc_event->type == MfClassicPollerEventTypeFail) {
  284. FURI_LOG_I(TAG, "fail");
  285. command = NfcCommandStop;
  286. }
  287. return command;
  288. }
  289. void metroflip_scene_bip_on_enter(void* context) {
  290. Metroflip* app = context;
  291. dolphin_deed(DolphinDeedNfcRead);
  292. app->sec_num = 0;
  293. // Setup view
  294. Popup* popup = app->popup;
  295. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  296. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  297. // Start worker
  298. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  299. nfc_scanner_alloc(app->nfc);
  300. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolMfClassic);
  301. nfc_poller_start(app->poller, metroflip_scene_bip_poller_callback, app);
  302. metroflip_app_blink_start(app);
  303. }
  304. bool metroflip_scene_bip_on_event(void* context, SceneManagerEvent event) {
  305. Metroflip* app = context;
  306. bool consumed = false;
  307. if(event.type == SceneManagerEventTypeCustom) {
  308. if(event.event == MetroflipCustomEventCardDetected) {
  309. Popup* popup = app->popup;
  310. popup_set_header(popup, "DON'T\nMOVE", 68, 30, AlignLeft, AlignTop);
  311. consumed = true;
  312. } else if(event.event == MetroflipCustomEventCardLost) {
  313. Popup* popup = app->popup;
  314. popup_set_header(popup, "Card \n lost", 68, 30, AlignLeft, AlignTop);
  315. consumed = true;
  316. } else if(event.event == MetroflipCustomEventWrongCard) {
  317. Popup* popup = app->popup;
  318. popup_set_header(popup, "WRONG \n CARD", 68, 30, AlignLeft, AlignTop);
  319. consumed = true;
  320. } else if(event.event == MetroflipCustomEventPollerFail) {
  321. Popup* popup = app->popup;
  322. popup_set_header(popup, "Failed", 68, 30, AlignLeft, AlignTop);
  323. consumed = true;
  324. }
  325. } else if(event.type == SceneManagerEventTypeBack) {
  326. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  327. consumed = true;
  328. }
  329. return consumed;
  330. }
  331. void metroflip_scene_bip_on_exit(void* context) {
  332. Metroflip* app = context;
  333. widget_reset(app->widget);
  334. if(app->poller) {
  335. nfc_poller_stop(app->poller);
  336. nfc_poller_free(app->poller);
  337. }
  338. // Clear view
  339. popup_reset(app->popup);
  340. metroflip_app_blink_stop(app);
  341. }