metroflip_scene_clipper.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. * clipper.c - Parser for Clipper cards (San Francisco, California).
  3. *
  4. * Based on research, some of which dates to 2007!
  5. *
  6. * Copyright 2024 Jeremy Cooper <jeremy.gthb@baymoo.org>
  7. *
  8. * This program is free software: you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <flipper_application.h>
  22. #include "../metroflip_i.h"
  23. #include <nfc/protocols/mf_desfire/mf_desfire_poller.h>
  24. #include <lib/nfc/protocols/mf_desfire/mf_desfire.h>
  25. #include <bit_lib.h>
  26. #include <datetime.h>
  27. #include <locale/locale.h>
  28. #include <inttypes.h>
  29. //
  30. // Table of application ids observed in the wild, and their sources.
  31. //
  32. static const struct {
  33. const MfDesfireApplicationId app;
  34. const char* type;
  35. } clipper_types[] = {
  36. // Application advertised on classic, plastic cards.
  37. {.app = {.data = {0x90, 0x11, 0xf2}}, .type = "Card"},
  38. // Application advertised on a mobile device.
  39. {.app = {.data = {0x91, 0x11, 0xf2}}, .type = "Mobile Device"},
  40. };
  41. static const size_t kNumCardTypes = sizeof(clipper_types) / sizeof(clipper_types[0]);
  42. struct IdMapping_struct {
  43. uint16_t id;
  44. const char* name;
  45. };
  46. typedef struct IdMapping_struct IdMapping;
  47. #define COUNT(_array) sizeof(_array) / sizeof(_array[0])
  48. //
  49. // Known transportation agencies and their identifiers.
  50. //
  51. static const IdMapping agency_names[] = {
  52. {.id = 0x0001, .name = "AC Transit"},
  53. {.id = 0x0004, .name = "BART"},
  54. {.id = 0x0006, .name = "Caltrain"},
  55. {.id = 0x0008, .name = "CCTA"},
  56. {.id = 0x000b, .name = "GGT"},
  57. {.id = 0x000f, .name = "SamTrans"},
  58. {.id = 0x0011, .name = "VTA"},
  59. {.id = 0x0012, .name = "Muni"},
  60. {.id = 0x0019, .name = "GG Ferry"},
  61. {.id = 0x001b, .name = "SF Bay Ferry"},
  62. };
  63. static const size_t kNumAgencies = COUNT(agency_names);
  64. //
  65. // Known station names for various agencies.
  66. //
  67. static const IdMapping bart_zones[] = {
  68. {.id = 0x0001, .name = "Colma"},
  69. {.id = 0x0002, .name = "Daly City"},
  70. {.id = 0x0003, .name = "Balboa Park"},
  71. {.id = 0x0004, .name = "Glen Park"},
  72. {.id = 0x0005, .name = "24th St Mission"},
  73. {.id = 0x0006, .name = "16th St Mission"},
  74. {.id = 0x0007, .name = "Civic Center/UN Plaza"},
  75. {.id = 0x0008, .name = "Powell St"},
  76. {.id = 0x0009, .name = "Montgomery St"},
  77. {.id = 0x000a, .name = "Embarcadero"},
  78. {.id = 0x000b, .name = "West Oakland"},
  79. {.id = 0x000c, .name = "12th St/Oakland City Center"},
  80. {.id = 0x000d, .name = "19th St/Oakland"},
  81. {.id = 0x000e, .name = "MacArthur"},
  82. {.id = 0x000f, .name = "Rockridge"},
  83. {.id = 0x0010, .name = "Orinda"},
  84. {.id = 0x0011, .name = "Lafayette"},
  85. {.id = 0x0012, .name = "Walnut Creek"},
  86. {.id = 0x0013, .name = "Pleasant Hill/Contra Costa Centre"},
  87. {.id = 0x0014, .name = "Concord"},
  88. {.id = 0x0015, .name = "North Concord/Martinez"},
  89. {.id = 0x0016, .name = "Pittsburg/Bay Point"},
  90. {.id = 0x0017, .name = "Ashby"},
  91. {.id = 0x0018, .name = "Downtown Berkeley"},
  92. {.id = 0x0019, .name = "North Berkeley"},
  93. {.id = 0x001a, .name = "El Cerrito Plaza"},
  94. {.id = 0x001b, .name = "El Cerrito Del Norte"},
  95. {.id = 0x001c, .name = "Richmond"},
  96. {.id = 0x001d, .name = "Lake Merrit"},
  97. {.id = 0x001e, .name = "Fruitvale"},
  98. {.id = 0x001f, .name = "Coliseum"},
  99. {.id = 0x0021, .name = "San Leandro"},
  100. {.id = 0x0022, .name = "Hayward"},
  101. {.id = 0x0023, .name = "South Hayward"},
  102. {.id = 0x0024, .name = "Union City"},
  103. {.id = 0x0025, .name = "Fremont"},
  104. {.id = 0x0026, .name = "Castro Valley"},
  105. {.id = 0x0027, .name = "Dublin/Pleasanton"},
  106. {.id = 0x0028, .name = "South San Francisco"},
  107. {.id = 0x0029, .name = "San Bruno"},
  108. {.id = 0x002a, .name = "SFO Airport"},
  109. {.id = 0x002b, .name = "Millbrae"},
  110. {.id = 0x002c, .name = "West Dublin/Pleasanton"},
  111. {.id = 0x002d, .name = "OAK Airport"},
  112. {.id = 0x002e, .name = "Warm Springs/South Fremont"},
  113. {.id = 0x002f, .name = "Milpitas"},
  114. {.id = 0x0030, .name = "Berryessa/North San Jose"},
  115. };
  116. static const size_t kNumBARTZones = COUNT(bart_zones);
  117. static const IdMapping muni_zones[] = {
  118. {.id = 0x0000, .name = "City Street"},
  119. {.id = 0x0005, .name = "Embarcadero"},
  120. {.id = 0x0006, .name = "Montgomery"},
  121. {.id = 0x0007, .name = "Powell"},
  122. {.id = 0x0008, .name = "Civic Center"},
  123. {.id = 0x0009, .name = "Van Ness"}, // Guessed
  124. {.id = 0x000a, .name = "Church"},
  125. {.id = 0x000b, .name = "Castro"},
  126. {.id = 0x000c, .name = "Forest Hill"}, // Guessed
  127. {.id = 0x000d, .name = "West Portal"},
  128. };
  129. static const size_t kNumMUNIZones = COUNT(muni_zones);
  130. static const IdMapping actransit_zones[] = {
  131. {.id = 0x0000, .name = "City Street"},
  132. };
  133. static const size_t kNumACTransitZones = COUNT(actransit_zones);
  134. // Instead of persisting individual Station IDs, Caltrain saves Zone numbers.
  135. // https://www.caltrain.com/stations-zones
  136. static const IdMapping caltrain_zones[] = {
  137. {.id = 0x0001, .name = "Zone 1"},
  138. {.id = 0x0002, .name = "Zone 2"},
  139. {.id = 0x0003, .name = "Zone 3"},
  140. {.id = 0x0004, .name = "Zone 4"},
  141. {.id = 0x0005, .name = "Zone 5"},
  142. {.id = 0x0006, .name = "Zone 6"},
  143. };
  144. static const size_t kNumCaltrainZones = COUNT(caltrain_zones);
  145. //
  146. // Full agency+zone mapping.
  147. //
  148. static const struct {
  149. uint16_t agency_id;
  150. const IdMapping* zone_map;
  151. size_t zone_count;
  152. } agency_zone_map[] = {
  153. {.agency_id = 0x0001, .zone_map = actransit_zones, .zone_count = kNumACTransitZones},
  154. {.agency_id = 0x0004, .zone_map = bart_zones, .zone_count = kNumBARTZones},
  155. {.agency_id = 0x0006, .zone_map = caltrain_zones, .zone_count = kNumCaltrainZones},
  156. {.agency_id = 0x0012, .zone_map = muni_zones, .zone_count = kNumMUNIZones}};
  157. static const size_t kNumAgencyZoneMaps = COUNT(agency_zone_map);
  158. // File ids of important files on the card.
  159. static const MfDesfireFileId clipper_ecash_file_id = 2;
  160. static const MfDesfireFileId clipper_histidx_file_id = 6;
  161. static const MfDesfireFileId clipper_identity_file_id = 8;
  162. static const MfDesfireFileId clipper_history_file_id = 14;
  163. struct ClipperCardInfo_struct {
  164. uint32_t serial_number;
  165. uint16_t counter;
  166. uint16_t last_txn_id;
  167. uint32_t last_updated_tm_1900;
  168. uint16_t last_terminal_id;
  169. int16_t balance_cents;
  170. };
  171. typedef struct ClipperCardInfo_struct ClipperCardInfo;
  172. // Forward declarations for helper functions.
  173. static void furi_string_cat_timestamp(
  174. FuriString* str,
  175. const char* date_hdr,
  176. const char* time_hdr,
  177. uint32_t tmst_1900);
  178. static bool get_file_contents(
  179. const MfDesfireApplication* app,
  180. const MfDesfireFileId* id,
  181. MfDesfireFileType type,
  182. size_t min_size,
  183. const uint8_t** out);
  184. static bool decode_id_file(const uint8_t* ef8_data, ClipperCardInfo* info);
  185. static bool decode_cash_file(const uint8_t* ef2_data, ClipperCardInfo* info);
  186. static bool get_map_item(uint16_t id, const IdMapping* map, size_t sz, const char** out);
  187. static bool get_agency_zone_name(uint16_t agency_id, uint16_t zone_id, const char** out);
  188. static void
  189. decode_usd(int16_t amount_cents, bool* out_is_negative, int16_t* out_usd, uint16_t* out_cents);
  190. static bool dump_ride_history(
  191. const uint8_t* index_file,
  192. const uint8_t* history_file,
  193. size_t len,
  194. FuriString* parsed_data);
  195. static bool dump_ride_event(const uint8_t* record, FuriString* parsed_data);
  196. // Unmarshal a 32-bit integer, big endian, unsigned
  197. static inline uint32_t get_u32be(const uint8_t* field) {
  198. return bit_lib_bytes_to_num_be(field, 4);
  199. }
  200. // Unmarshal a 16-bit integer, big endian, unsigned
  201. static uint16_t get_u16be(const uint8_t* field) {
  202. return bit_lib_bytes_to_num_be(field, 2);
  203. }
  204. // Unmarshal a 16-bit integer, big endian, signed, two's-complement
  205. static int16_t get_i16be(const uint8_t* field) {
  206. uint16_t raw = get_u16be(field);
  207. if(raw > 0x7fff)
  208. return -((uint32_t)0x10000 - raw);
  209. else
  210. return raw;
  211. }
  212. static bool clipper_parse(const NfcDevice* device, FuriString* parsed_data) {
  213. furi_assert(device);
  214. furi_assert(parsed_data);
  215. bool parsed = false;
  216. do {
  217. const MfDesfireData* data = nfc_device_get_data(device, NfcProtocolMfDesfire);
  218. const MfDesfireApplication* app = NULL;
  219. const char* device_description = NULL;
  220. for(size_t i = 0; i < kNumCardTypes; i++) {
  221. app = mf_desfire_get_application(data, &clipper_types[i].app);
  222. device_description = clipper_types[i].type;
  223. if(app != NULL) break;
  224. }
  225. // If no matching application was found, abort this parser.
  226. if(app == NULL) break;
  227. ClipperCardInfo info;
  228. const uint8_t* id_data;
  229. if(!get_file_contents(
  230. app, &clipper_identity_file_id, MfDesfireFileTypeStandard, 5, &id_data))
  231. break;
  232. if(!decode_id_file(id_data, &info)) break;
  233. const uint8_t* cash_data;
  234. if(!get_file_contents(app, &clipper_ecash_file_id, MfDesfireFileTypeBackup, 32, &cash_data))
  235. break;
  236. if(!decode_cash_file(cash_data, &info)) break;
  237. int16_t balance_usd;
  238. uint16_t balance_cents;
  239. bool _balance_is_negative;
  240. decode_usd(info.balance_cents, &_balance_is_negative, &balance_usd, &balance_cents);
  241. furi_string_cat_printf(
  242. parsed_data,
  243. "\e#Clipper\n"
  244. "Serial: %" PRIu32 "\n"
  245. "Balance: $%d.%02u\n"
  246. "Type: %s\n"
  247. "\e#Last Update\n",
  248. info.serial_number,
  249. balance_usd,
  250. balance_cents,
  251. device_description);
  252. if(info.last_updated_tm_1900 != 0)
  253. furi_string_cat_timestamp(
  254. parsed_data, "Date: ", "\nTime: ", info.last_updated_tm_1900);
  255. else
  256. furi_string_cat_str(parsed_data, "Never");
  257. furi_string_cat_printf(
  258. parsed_data,
  259. "\nTerminal: 0x%04x\n"
  260. "Transaction Id: %u\n"
  261. "Counter: %u\n",
  262. info.last_terminal_id,
  263. info.last_txn_id,
  264. info.counter);
  265. const uint8_t *history_index, *history;
  266. if(!get_file_contents(
  267. app, &clipper_histidx_file_id, MfDesfireFileTypeBackup, 16, &history_index))
  268. break;
  269. if(!get_file_contents(
  270. app, &clipper_history_file_id, MfDesfireFileTypeStandard, 512, &history))
  271. break;
  272. if(!dump_ride_history(history_index, history, 512, parsed_data)) break;
  273. parsed = true;
  274. } while(false);
  275. return parsed;
  276. }
  277. static bool get_file_contents(
  278. const MfDesfireApplication* app,
  279. const MfDesfireFileId* id,
  280. MfDesfireFileType type,
  281. size_t min_size,
  282. const uint8_t** out) {
  283. const MfDesfireFileSettings* settings = mf_desfire_get_file_settings(app, id);
  284. if(settings == NULL) return false;
  285. if(settings->type != type) return false;
  286. const MfDesfireFileData* file_data = mf_desfire_get_file_data(app, id);
  287. if(file_data == NULL) return false;
  288. if(simple_array_get_count(file_data->data) < min_size) return false;
  289. *out = simple_array_cget_data(file_data->data);
  290. return true;
  291. }
  292. static bool decode_id_file(const uint8_t* ef8_data, ClipperCardInfo* info) {
  293. // Identity file (8)
  294. //
  295. // Byte view
  296. //
  297. // 0 1 2 3 4 5 6 7 8
  298. // +----+----.----.----.----+----.----.----+
  299. // 0x00 | uk | card_id | unknown |
  300. // +----+----.----.----.----+----.----.----+
  301. // 0x08 | unknown |
  302. // +----.----.----.----.----.----.----.----+
  303. // 0x10 ...
  304. //
  305. //
  306. // Field Datatype Description
  307. // ----- -------- -----------
  308. // uk ?8?? Unknown, 8-bit byte
  309. // card_id U32BE Card identifier
  310. //
  311. info->serial_number = bit_lib_bytes_to_num_be(&ef8_data[1], 4);
  312. return true;
  313. }
  314. static bool decode_cash_file(const uint8_t* ef2_data, ClipperCardInfo* info) {
  315. // ECash file (2)
  316. //
  317. // Byte view
  318. //
  319. // 0 1 2 3 4 5 6 7 8
  320. // +----.----+----.----+----.----.----.----+
  321. // 0x00 | unk00 | counter | timestamp_1900 |
  322. // +----.----+----.----+----.----.----.----+
  323. // 0x08 | term_id | unk01 |
  324. // +----.----+----.----+----.----.----.----+
  325. // 0x10 | txn_id | balance | unknown |
  326. // +----.----+----.----+----.----.----.----+
  327. // 0x18 | unknown |
  328. // +---------------------------------------+
  329. //
  330. // Field Datatype Description
  331. // ----- -------- -----------
  332. // unk00 U8[2] Unknown bytes
  333. // counter U16BE Unknown, appears to be a counter
  334. // timestamp_1900 U32BE Timestamp of last transaction, in seconds
  335. // since 1900-01-01 GMT.
  336. // unk01 U8[6] Unknown bytes
  337. // txn_id U16BE Id of last transaction.
  338. // balance S16BE Card cash balance, in cents.
  339. // Cards can obtain negative balances in this
  340. // system, so balances are signed integers.
  341. // Maximum card balance is therefore
  342. // $327.67.
  343. // unk02 U8[12] Unknown bytes.
  344. //
  345. info->counter = get_u16be(&ef2_data[2]);
  346. info->last_updated_tm_1900 = get_u32be(&ef2_data[4]);
  347. info->last_terminal_id = get_u16be(&ef2_data[8]);
  348. info->last_txn_id = get_u16be(&ef2_data[0x10]);
  349. info->balance_cents = get_i16be(&ef2_data[0x12]);
  350. return true;
  351. }
  352. static bool dump_ride_history(
  353. const uint8_t* index_file,
  354. const uint8_t* history_file,
  355. size_t len,
  356. FuriString* parsed_data) {
  357. static const size_t kRideRecordSize = 0x20;
  358. for(size_t i = 0; i < 16; i++) {
  359. uint8_t record_num = index_file[i];
  360. if(record_num == 0xff) break;
  361. size_t record_offset = record_num * kRideRecordSize;
  362. if(record_offset + kRideRecordSize > len) break;
  363. const uint8_t* record = &history_file[record_offset];
  364. if(!dump_ride_event(record, parsed_data)) break;
  365. }
  366. return true;
  367. }
  368. static bool dump_ride_event(const uint8_t* record, FuriString* parsed_data) {
  369. // Ride record
  370. //
  371. // 0 1 2 3 4 5 6 7 8
  372. // +----+----+----.----+----.----+----.----+
  373. // 0x00 |0x10| ? | agency | ? | fare |
  374. // +----.----+----.----+----.----.----.----+
  375. // 0x08 | ? | vehicle | time_on |
  376. // +----.----.----.----+----.----+----.----+
  377. // 0x10 | time_off | zone_on | zone_off|
  378. // +----+----.----.----.----+----+----+----+
  379. // 0x18 | ? | ? | ? | ? | ? |
  380. // +----+----.----.----.----+----+----+----+
  381. //
  382. // Field Datatype Description
  383. // ----- -------- -----------
  384. // agency U16BE Transportation agency identifier.
  385. // Known ids:
  386. // 1 == AC Transit
  387. // 4 == BART
  388. // 18 == SF MUNI
  389. // fare I16BE Fare deducted, in cents.
  390. // vehicle U16BE Vehicle id (0 == not provided)
  391. // time_on U32BE Boarding time, in seconds since 1900-01-01 GMT.
  392. // time_off U32BE Off-boarding time, if present, in seconds
  393. // since 1900-01-01 GMT. Set to zero if no offboard
  394. // has been recorded.
  395. // zone_on U16BE Id of boarding zone or station. Agency-specific.
  396. // zone_off U16BE Id of offboarding zone or station. Agency-
  397. // specific.
  398. if(record[0] != 0x10) return false;
  399. uint16_t agency_id = get_u16be(&record[2]);
  400. if(agency_id == 0)
  401. // Likely empty record. Skip.
  402. return false;
  403. const char* agency_name;
  404. bool ok = get_map_item(agency_id, agency_names, kNumAgencies, &agency_name);
  405. if(!ok) agency_name = "Unknown";
  406. uint16_t vehicle_id = get_u16be(&record[0x0a]);
  407. int16_t fare_raw_cents = get_i16be(&record[6]);
  408. bool _fare_is_negative;
  409. int16_t fare_usd;
  410. uint16_t fare_cents;
  411. decode_usd(fare_raw_cents, &_fare_is_negative, &fare_usd, &fare_cents);
  412. uint32_t time_on_raw = get_u32be(&record[0x0c]);
  413. uint32_t time_off_raw = get_u32be(&record[0x10]);
  414. uint16_t zone_id_on = get_u16be(&record[0x14]);
  415. uint16_t zone_id_off = get_u16be(&record[0x16]);
  416. const char *zone_on, *zone_off;
  417. if(!get_agency_zone_name(agency_id, zone_id_on, &zone_on)) {
  418. zone_on = "Unknown";
  419. }
  420. if(!get_agency_zone_name(agency_id, zone_id_off, &zone_off)) {
  421. zone_off = "Unknown";
  422. }
  423. furi_string_cat_str(parsed_data, "\e#Ride Record\n");
  424. furi_string_cat_timestamp(parsed_data, "Date: ", "\nTime: ", time_on_raw);
  425. furi_string_cat_printf(
  426. parsed_data,
  427. "\n"
  428. "Fare: $%d.%02u\n"
  429. "Agency: %s (%04x)\n"
  430. "On: %s (%04x)\n",
  431. fare_usd,
  432. fare_cents,
  433. agency_name,
  434. agency_id,
  435. zone_on,
  436. zone_id_on);
  437. if(vehicle_id != 0) {
  438. furi_string_cat_printf(parsed_data, "Vehicle id: %d\n", vehicle_id);
  439. }
  440. if(time_off_raw != 0) {
  441. furi_string_cat_printf(parsed_data, "Off: %s (%04x)\n", zone_off, zone_id_off);
  442. furi_string_cat_timestamp(parsed_data, "Date Off: ", "\nTime Off: ", time_off_raw);
  443. furi_string_cat_str(parsed_data, "\n");
  444. }
  445. return true;
  446. }
  447. static bool get_map_item(uint16_t id, const IdMapping* map, size_t sz, const char** out) {
  448. for(size_t i = 0; i < sz; i++) {
  449. if(map[i].id == id) {
  450. *out = map[i].name;
  451. return true;
  452. }
  453. }
  454. return false;
  455. }
  456. static bool get_agency_zone_name(uint16_t agency_id, uint16_t zone_id, const char** out) {
  457. for(size_t i = 0; i < kNumAgencyZoneMaps; i++) {
  458. if(agency_zone_map[i].agency_id == agency_id) {
  459. return get_map_item(
  460. zone_id, agency_zone_map[i].zone_map, agency_zone_map[i].zone_count, out);
  461. }
  462. }
  463. return false;
  464. }
  465. // Split a balance/fare amount from raw cents to dollars and cents portion,
  466. // automatically adjusting the cents portion so that it is always positive,
  467. // for easier display.
  468. static void
  469. decode_usd(int16_t amount_cents, bool* out_is_negative, int16_t* out_usd, uint16_t* out_cents) {
  470. *out_usd = amount_cents / 100;
  471. if(amount_cents >= 0) {
  472. *out_is_negative = false;
  473. *out_cents = amount_cents % 100;
  474. } else {
  475. *out_is_negative = true;
  476. *out_cents = (amount_cents * -1) % 100;
  477. }
  478. }
  479. // Decode a raw 1900-based timestamp and append a human-readable form to a
  480. // FuriString.
  481. static void furi_string_cat_timestamp(
  482. FuriString* str,
  483. const char* date_hdr,
  484. const char* time_hdr,
  485. uint32_t tmst_1900) {
  486. DateTime tm;
  487. datetime_timestamp_to_datetime(tmst_1900, &tm);
  488. FuriString* date_str = furi_string_alloc();
  489. locale_format_date(date_str, &tm, locale_get_date_format(), "-");
  490. FuriString* time_str = furi_string_alloc();
  491. locale_format_time(time_str, &tm, locale_get_time_format(), true);
  492. furi_string_cat_printf(
  493. str,
  494. "%s%s%s%s (UTC)",
  495. date_hdr,
  496. furi_string_get_cstr(date_str),
  497. time_hdr,
  498. furi_string_get_cstr(time_str));
  499. furi_string_free(date_str);
  500. furi_string_free(time_str);
  501. }
  502. static NfcCommand metroflip_scene_clipper_poller_callback(NfcGenericEvent event, void* context) {
  503. furi_assert(event.protocol == NfcProtocolMfDesfire);
  504. Metroflip* app = context;
  505. NfcCommand command = NfcCommandContinue;
  506. FuriString* parsed_data = furi_string_alloc();
  507. Widget* widget = app->widget;
  508. furi_string_reset(app->text_box_store);
  509. const MfDesfirePollerEvent* mf_desfire_event = event.event_data;
  510. if(mf_desfire_event->type == MfDesfirePollerEventTypeReadSuccess) {
  511. nfc_device_set_data(
  512. app->nfc_device, NfcProtocolMfDesfire, nfc_poller_get_data(app->poller));
  513. clipper_parse(app->nfc_device, parsed_data);
  514. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  515. widget_add_button_element(
  516. widget, GuiButtonTypeRight, "Exit", metroflip_exit_widget_callback, app);
  517. furi_string_free(parsed_data);
  518. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  519. metroflip_app_blink_stop(app);
  520. command = NfcCommandStop;
  521. } else if(mf_desfire_event->type == MfDesfirePollerEventTypeReadFailed) {
  522. view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventPollerSuccess);
  523. command = NfcCommandReset;
  524. }
  525. return command;
  526. }
  527. void metroflip_scene_clipper_on_enter(void* context) {
  528. Metroflip* app = context;
  529. dolphin_deed(DolphinDeedNfcRead);
  530. // Setup view
  531. Popup* popup = app->popup;
  532. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  533. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  534. // Start worker
  535. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  536. nfc_scanner_alloc(app->nfc);
  537. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolMfDesfire);
  538. nfc_poller_start(app->poller, metroflip_scene_clipper_poller_callback, app);
  539. metroflip_app_blink_start(app);
  540. }
  541. bool metroflip_scene_clipper_on_event(void* context, SceneManagerEvent event) {
  542. Metroflip* app = context;
  543. bool consumed = false;
  544. if(event.type == SceneManagerEventTypeCustom) {
  545. if(event.event == MetroflipCustomEventCardDetected) {
  546. Popup* popup = app->popup;
  547. popup_set_header(popup, "DON'T\nMOVE", 68, 30, AlignLeft, AlignTop);
  548. consumed = true;
  549. } else if(event.event == MetroflipCustomEventCardLost) {
  550. Popup* popup = app->popup;
  551. popup_set_header(popup, "Card \n lost", 68, 30, AlignLeft, AlignTop);
  552. consumed = true;
  553. } else if(event.event == MetroflipCustomEventWrongCard) {
  554. Popup* popup = app->popup;
  555. popup_set_header(popup, "WRONG \n CARD", 68, 30, AlignLeft, AlignTop);
  556. consumed = true;
  557. } else if(event.event == MetroflipCustomEventPollerFail) {
  558. Popup* popup = app->popup;
  559. popup_set_header(popup, "Failed", 68, 30, AlignLeft, AlignTop);
  560. consumed = true;
  561. }
  562. } else if(event.type == SceneManagerEventTypeBack) {
  563. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  564. consumed = true;
  565. }
  566. return consumed;
  567. }
  568. void metroflip_scene_clipper_on_exit(void* context) {
  569. Metroflip* app = context;
  570. widget_reset(app->widget);
  571. metroflip_app_blink_stop(app);
  572. if(app->poller) {
  573. nfc_poller_stop(app->poller);
  574. nfc_poller_free(app->poller);
  575. }
  576. }