metroflip_scene_clipper.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. FURI_LOG_I("TAG", "ya");
  223. device_description = clipper_types[i].type;
  224. if(app != NULL) break;
  225. }
  226. FURI_LOG_I("TAG", "ya2");
  227. // If no matching application was found, abort this parser.
  228. if(app == NULL) break;
  229. FURI_LOG_I("TAG", "ya3");
  230. ClipperCardInfo info;
  231. const uint8_t* id_data;
  232. if(!get_file_contents(
  233. app, &clipper_identity_file_id, MfDesfireFileTypeStandard, 5, &id_data))
  234. break;
  235. FURI_LOG_I("TAG", "ya4");
  236. if(!decode_id_file(id_data, &info)) break;
  237. FURI_LOG_I("TAG", "ya5");
  238. const uint8_t* cash_data;
  239. if(!get_file_contents(app, &clipper_ecash_file_id, MfDesfireFileTypeBackup, 32, &cash_data))
  240. break;
  241. FURI_LOG_I("TAG", "ya6");
  242. if(!decode_cash_file(cash_data, &info)) break;
  243. FURI_LOG_I("TAG", "ya7");
  244. int16_t balance_usd;
  245. uint16_t balance_cents;
  246. bool _balance_is_negative;
  247. decode_usd(info.balance_cents, &_balance_is_negative, &balance_usd, &balance_cents);
  248. FURI_LOG_I("TAG", "ya8");
  249. furi_string_cat_printf(
  250. parsed_data,
  251. "\e#Clipper\n"
  252. "Serial: %" PRIu32 "\n"
  253. "Balance: $%d.%02u\n"
  254. "Type: %s\n"
  255. "\e#Last Update\n",
  256. info.serial_number,
  257. balance_usd,
  258. balance_cents,
  259. device_description);
  260. if(info.last_updated_tm_1900 != 0)
  261. furi_string_cat_timestamp(
  262. parsed_data, "Date: ", "\nTime: ", info.last_updated_tm_1900);
  263. else
  264. furi_string_cat_str(parsed_data, "Never");
  265. furi_string_cat_printf(
  266. parsed_data,
  267. "\nTerminal: 0x%04x\n"
  268. "Transaction Id: %u\n"
  269. "Counter: %u\n",
  270. info.last_terminal_id,
  271. info.last_txn_id,
  272. info.counter);
  273. const uint8_t *history_index, *history;
  274. if(!get_file_contents(
  275. app, &clipper_histidx_file_id, MfDesfireFileTypeBackup, 16, &history_index))
  276. break;
  277. if(!get_file_contents(
  278. app, &clipper_history_file_id, MfDesfireFileTypeStandard, 512, &history))
  279. break;
  280. if(!dump_ride_history(history_index, history, 512, parsed_data)) break;
  281. parsed = true;
  282. } while(false);
  283. return parsed;
  284. }
  285. static bool get_file_contents(
  286. const MfDesfireApplication* app,
  287. const MfDesfireFileId* id,
  288. MfDesfireFileType type,
  289. size_t min_size,
  290. const uint8_t** out) {
  291. const MfDesfireFileSettings* settings = mf_desfire_get_file_settings(app, id);
  292. if(settings == NULL) return false;
  293. if(settings->type != type) return false;
  294. const MfDesfireFileData* file_data = mf_desfire_get_file_data(app, id);
  295. if(file_data == NULL) return false;
  296. if(simple_array_get_count(file_data->data) < min_size) return false;
  297. *out = simple_array_cget_data(file_data->data);
  298. return true;
  299. }
  300. static bool decode_id_file(const uint8_t* ef8_data, ClipperCardInfo* info) {
  301. // Identity file (8)
  302. //
  303. // Byte view
  304. //
  305. // 0 1 2 3 4 5 6 7 8
  306. // +----+----.----.----.----+----.----.----+
  307. // 0x00 | uk | card_id | unknown |
  308. // +----+----.----.----.----+----.----.----+
  309. // 0x08 | unknown |
  310. // +----.----.----.----.----.----.----.----+
  311. // 0x10 ...
  312. //
  313. //
  314. // Field Datatype Description
  315. // ----- -------- -----------
  316. // uk ?8?? Unknown, 8-bit byte
  317. // card_id U32BE Card identifier
  318. //
  319. info->serial_number = bit_lib_bytes_to_num_be(&ef8_data[1], 4);
  320. return true;
  321. }
  322. static bool decode_cash_file(const uint8_t* ef2_data, ClipperCardInfo* info) {
  323. // ECash file (2)
  324. //
  325. // Byte view
  326. //
  327. // 0 1 2 3 4 5 6 7 8
  328. // +----.----+----.----+----.----.----.----+
  329. // 0x00 | unk00 | counter | timestamp_1900 |
  330. // +----.----+----.----+----.----.----.----+
  331. // 0x08 | term_id | unk01 |
  332. // +----.----+----.----+----.----.----.----+
  333. // 0x10 | txn_id | balance | unknown |
  334. // +----.----+----.----+----.----.----.----+
  335. // 0x18 | unknown |
  336. // +---------------------------------------+
  337. //
  338. // Field Datatype Description
  339. // ----- -------- -----------
  340. // unk00 U8[2] Unknown bytes
  341. // counter U16BE Unknown, appears to be a counter
  342. // timestamp_1900 U32BE Timestamp of last transaction, in seconds
  343. // since 1900-01-01 GMT.
  344. // unk01 U8[6] Unknown bytes
  345. // txn_id U16BE Id of last transaction.
  346. // balance S16BE Card cash balance, in cents.
  347. // Cards can obtain negative balances in this
  348. // system, so balances are signed integers.
  349. // Maximum card balance is therefore
  350. // $327.67.
  351. // unk02 U8[12] Unknown bytes.
  352. //
  353. info->counter = get_u16be(&ef2_data[2]);
  354. info->last_updated_tm_1900 = get_u32be(&ef2_data[4]);
  355. info->last_terminal_id = get_u16be(&ef2_data[8]);
  356. info->last_txn_id = get_u16be(&ef2_data[0x10]);
  357. info->balance_cents = get_i16be(&ef2_data[0x12]);
  358. return true;
  359. }
  360. static bool dump_ride_history(
  361. const uint8_t* index_file,
  362. const uint8_t* history_file,
  363. size_t len,
  364. FuriString* parsed_data) {
  365. static const size_t kRideRecordSize = 0x20;
  366. for(size_t i = 0; i < 16; i++) {
  367. uint8_t record_num = index_file[i];
  368. if(record_num == 0xff) break;
  369. size_t record_offset = record_num * kRideRecordSize;
  370. if(record_offset + kRideRecordSize > len) break;
  371. const uint8_t* record = &history_file[record_offset];
  372. if(!dump_ride_event(record, parsed_data)) break;
  373. }
  374. return true;
  375. }
  376. static bool dump_ride_event(const uint8_t* record, FuriString* parsed_data) {
  377. // Ride record
  378. //
  379. // 0 1 2 3 4 5 6 7 8
  380. // +----+----+----.----+----.----+----.----+
  381. // 0x00 |0x10| ? | agency | ? | fare |
  382. // +----.----+----.----+----.----.----.----+
  383. // 0x08 | ? | vehicle | time_on |
  384. // +----.----.----.----+----.----+----.----+
  385. // 0x10 | time_off | zone_on | zone_off|
  386. // +----+----.----.----.----+----+----+----+
  387. // 0x18 | ? | ? | ? | ? | ? |
  388. // +----+----.----.----.----+----+----+----+
  389. //
  390. // Field Datatype Description
  391. // ----- -------- -----------
  392. // agency U16BE Transportation agency identifier.
  393. // Known ids:
  394. // 1 == AC Transit
  395. // 4 == BART
  396. // 18 == SF MUNI
  397. // fare I16BE Fare deducted, in cents.
  398. // vehicle U16BE Vehicle id (0 == not provided)
  399. // time_on U32BE Boarding time, in seconds since 1900-01-01 GMT.
  400. // time_off U32BE Off-boarding time, if present, in seconds
  401. // since 1900-01-01 GMT. Set to zero if no offboard
  402. // has been recorded.
  403. // zone_on U16BE Id of boarding zone or station. Agency-specific.
  404. // zone_off U16BE Id of offboarding zone or station. Agency-
  405. // specific.
  406. if(record[0] != 0x10) return false;
  407. uint16_t agency_id = get_u16be(&record[2]);
  408. if(agency_id == 0)
  409. // Likely empty record. Skip.
  410. return false;
  411. const char* agency_name;
  412. bool ok = get_map_item(agency_id, agency_names, kNumAgencies, &agency_name);
  413. if(!ok) agency_name = "Unknown";
  414. uint16_t vehicle_id = get_u16be(&record[0x0a]);
  415. int16_t fare_raw_cents = get_i16be(&record[6]);
  416. bool _fare_is_negative;
  417. int16_t fare_usd;
  418. uint16_t fare_cents;
  419. decode_usd(fare_raw_cents, &_fare_is_negative, &fare_usd, &fare_cents);
  420. uint32_t time_on_raw = get_u32be(&record[0x0c]);
  421. uint32_t time_off_raw = get_u32be(&record[0x10]);
  422. uint16_t zone_id_on = get_u16be(&record[0x14]);
  423. uint16_t zone_id_off = get_u16be(&record[0x16]);
  424. const char *zone_on, *zone_off;
  425. if(!get_agency_zone_name(agency_id, zone_id_on, &zone_on)) {
  426. zone_on = "Unknown";
  427. }
  428. if(!get_agency_zone_name(agency_id, zone_id_off, &zone_off)) {
  429. zone_off = "Unknown";
  430. }
  431. furi_string_cat_str(parsed_data, "\e#Ride Record\n");
  432. furi_string_cat_timestamp(parsed_data, "Date: ", "\nTime: ", time_on_raw);
  433. furi_string_cat_printf(
  434. parsed_data,
  435. "\n"
  436. "Fare: $%d.%02u\n"
  437. "Agency: %s (%04x)\n"
  438. "On: %s (%04x)\n",
  439. fare_usd,
  440. fare_cents,
  441. agency_name,
  442. agency_id,
  443. zone_on,
  444. zone_id_on);
  445. if(vehicle_id != 0) {
  446. furi_string_cat_printf(parsed_data, "Vehicle id: %d\n", vehicle_id);
  447. }
  448. if(time_off_raw != 0) {
  449. furi_string_cat_printf(parsed_data, "Off: %s (%04x)\n", zone_off, zone_id_off);
  450. furi_string_cat_timestamp(parsed_data, "Date Off: ", "\nTime Off: ", time_off_raw);
  451. furi_string_cat_str(parsed_data, "\n");
  452. }
  453. return true;
  454. }
  455. static bool get_map_item(uint16_t id, const IdMapping* map, size_t sz, const char** out) {
  456. for(size_t i = 0; i < sz; i++) {
  457. if(map[i].id == id) {
  458. *out = map[i].name;
  459. return true;
  460. }
  461. }
  462. return false;
  463. }
  464. static bool get_agency_zone_name(uint16_t agency_id, uint16_t zone_id, const char** out) {
  465. for(size_t i = 0; i < kNumAgencyZoneMaps; i++) {
  466. if(agency_zone_map[i].agency_id == agency_id) {
  467. return get_map_item(
  468. zone_id, agency_zone_map[i].zone_map, agency_zone_map[i].zone_count, out);
  469. }
  470. }
  471. return false;
  472. }
  473. // Split a balance/fare amount from raw cents to dollars and cents portion,
  474. // automatically adjusting the cents portion so that it is always positive,
  475. // for easier display.
  476. static void
  477. decode_usd(int16_t amount_cents, bool* out_is_negative, int16_t* out_usd, uint16_t* out_cents) {
  478. *out_usd = amount_cents / 100;
  479. if(amount_cents >= 0) {
  480. *out_is_negative = false;
  481. *out_cents = amount_cents % 100;
  482. } else {
  483. *out_is_negative = true;
  484. *out_cents = (amount_cents * -1) % 100;
  485. }
  486. }
  487. void metroflip_clipper_widget_callback(GuiButtonType result, InputType type, void* context) {
  488. Metroflip* app = context;
  489. UNUSED(result);
  490. if(type == InputTypeShort) {
  491. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  492. }
  493. }
  494. // Decode a raw 1900-based timestamp and append a human-readable form to a
  495. // FuriString.
  496. static void furi_string_cat_timestamp(
  497. FuriString* str,
  498. const char* date_hdr,
  499. const char* time_hdr,
  500. uint32_t tmst_1900) {
  501. DateTime tm;
  502. datetime_timestamp_to_datetime(tmst_1900, &tm);
  503. FuriString* date_str = furi_string_alloc();
  504. locale_format_date(date_str, &tm, locale_get_date_format(), "-");
  505. FuriString* time_str = furi_string_alloc();
  506. locale_format_time(time_str, &tm, locale_get_time_format(), true);
  507. furi_string_cat_printf(
  508. str,
  509. "%s%s%s%s (UTC)",
  510. date_hdr,
  511. furi_string_get_cstr(date_str),
  512. time_hdr,
  513. furi_string_get_cstr(time_str));
  514. furi_string_free(date_str);
  515. furi_string_free(time_str);
  516. }
  517. static NfcCommand metroflip_scene_clipper_poller_callback(NfcGenericEvent event, void* context) {
  518. furi_assert(event.protocol == NfcProtocolMfDesfire);
  519. Metroflip* app = context;
  520. NfcCommand command = NfcCommandContinue;
  521. FuriString* parsed_data = furi_string_alloc();
  522. Widget* widget = app->widget;
  523. furi_string_reset(app->text_box_store);
  524. const MfDesfirePollerEvent* mf_desfire_event = event.event_data;
  525. if(mf_desfire_event->type == MfDesfirePollerEventTypeReadSuccess) {
  526. nfc_device_set_data(
  527. app->nfc_device, NfcProtocolMfDesfire, nfc_poller_get_data(app->poller));
  528. clipper_parse(app->nfc_device, parsed_data);
  529. widget_add_text_scroll_element(
  530. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  531. widget_add_button_element(
  532. widget, GuiButtonTypeRight, "Exit", metroflip_clipper_widget_callback, app);
  533. furi_string_free(parsed_data);
  534. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  535. metroflip_app_blink_stop(app);
  536. command = NfcCommandStop;
  537. } else if(mf_desfire_event->type == MfDesfirePollerEventTypeReadFailed) {
  538. view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventPollerSuccess);
  539. command = NfcCommandReset;
  540. }
  541. return command;
  542. }
  543. void metroflip_scene_clipper_on_enter(void* context) {
  544. Metroflip* app = context;
  545. dolphin_deed(DolphinDeedNfcRead);
  546. // Setup view
  547. Popup* popup = app->popup;
  548. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  549. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  550. // Start worker
  551. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  552. nfc_scanner_alloc(app->nfc);
  553. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolMfDesfire);
  554. nfc_poller_start(app->poller, metroflip_scene_clipper_poller_callback, app);
  555. metroflip_app_blink_start(app);
  556. }
  557. bool metroflip_scene_clipper_on_event(void* context, SceneManagerEvent event) {
  558. Metroflip* app = context;
  559. bool consumed = false;
  560. if(event.type == SceneManagerEventTypeCustom) {
  561. if(event.event == MetroflipCustomEventCardDetected) {
  562. Popup* popup = app->popup;
  563. popup_set_header(popup, "DON'T\nMOVE", 68, 30, AlignLeft, AlignTop);
  564. consumed = true;
  565. } else if(event.event == MetroflipCustomEventCardLost) {
  566. Popup* popup = app->popup;
  567. popup_set_header(popup, "Card \n lost", 68, 30, AlignLeft, AlignTop);
  568. consumed = true;
  569. } else if(event.event == MetroflipCustomEventWrongCard) {
  570. Popup* popup = app->popup;
  571. popup_set_header(popup, "WRONG \n CARD", 68, 30, AlignLeft, AlignTop);
  572. consumed = true;
  573. } else if(event.event == MetroflipCustomEventPollerFail) {
  574. Popup* popup = app->popup;
  575. popup_set_header(popup, "Failed", 68, 30, AlignLeft, AlignTop);
  576. consumed = true;
  577. }
  578. } else if(event.type == SceneManagerEventTypeBack) {
  579. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  580. consumed = true;
  581. }
  582. return consumed;
  583. }
  584. void metroflip_scene_clipper_on_exit(void* context) {
  585. Metroflip* app = context;
  586. widget_reset(app->widget);
  587. metroflip_app_blink_stop(app);
  588. if(app->poller) {
  589. nfc_poller_stop(app->poller);
  590. nfc_poller_free(app->poller);
  591. }
  592. }