clipper.c 24 KB

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