metroflip_scene_navigo.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. #include "../metroflip_i.h"
  2. #include <datetime.h>
  3. #include <dolphin/dolphin.h>
  4. #include <notification/notification_messages.h>
  5. #include <locale/locale.h>
  6. #include "navigo.h"
  7. #include <nfc/protocols/iso14443_4b/iso14443_4b_poller.h>
  8. #define TAG "Metroflip:Scene:Navigo"
  9. int select_new_app(
  10. int new_app_directory,
  11. int new_app,
  12. BitBuffer* tx_buffer,
  13. BitBuffer* rx_buffer,
  14. Iso14443_4bPoller* iso14443_4b_poller,
  15. Metroflip* app,
  16. MetroflipPollerEventType* stage) {
  17. select_app[5] = new_app_directory;
  18. select_app[6] = new_app;
  19. bit_buffer_reset(tx_buffer);
  20. bit_buffer_append_bytes(tx_buffer, select_app, sizeof(select_app));
  21. int error = iso14443_4b_poller_send_block(iso14443_4b_poller, tx_buffer, rx_buffer);
  22. if(error != Iso14443_4bErrorNone) {
  23. FURI_LOG_I(TAG, "Select File: iso14443_4b_poller_send_block error %d", error);
  24. *stage = MetroflipPollerEventTypeFail;
  25. view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventPollerFail);
  26. return error;
  27. }
  28. return 0;
  29. }
  30. int read_new_file(
  31. int new_file,
  32. BitBuffer* tx_buffer,
  33. BitBuffer* rx_buffer,
  34. Iso14443_4bPoller* iso14443_4b_poller,
  35. Metroflip* app,
  36. MetroflipPollerEventType* stage) {
  37. read_file[2] = new_file;
  38. bit_buffer_reset(tx_buffer);
  39. bit_buffer_append_bytes(tx_buffer, read_file, sizeof(read_file));
  40. Iso14443_4bError error =
  41. iso14443_4b_poller_send_block(iso14443_4b_poller, tx_buffer, rx_buffer);
  42. if(error != Iso14443_4bErrorNone) {
  43. FURI_LOG_I(TAG, "Read File: iso14443_4b_poller_send_block error %d", error);
  44. *stage = MetroflipPollerEventTypeFail;
  45. view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventPollerFail);
  46. return error;
  47. }
  48. return 0;
  49. }
  50. int check_response(
  51. BitBuffer* rx_buffer,
  52. Metroflip* app,
  53. MetroflipPollerEventType* stage,
  54. size_t* response_length) {
  55. *response_length = bit_buffer_get_size_bytes(rx_buffer);
  56. if(bit_buffer_get_byte(rx_buffer, *response_length - 2) != apdu_success[0] ||
  57. bit_buffer_get_byte(rx_buffer, *response_length - 1) != apdu_success[1]) {
  58. FURI_LOG_I(
  59. TAG,
  60. "Select profile app/file failed: %02x%02x",
  61. bit_buffer_get_byte(rx_buffer, *response_length - 2),
  62. bit_buffer_get_byte(rx_buffer, *response_length - 1));
  63. *stage = MetroflipPollerEventTypeFail;
  64. view_dispatcher_send_custom_event(
  65. app->view_dispatcher, MetroflipCustomEventPollerFileNotFound);
  66. return 1;
  67. }
  68. return 0;
  69. }
  70. const char* get_country(int country_num) {
  71. switch(country_num) {
  72. case 250:
  73. return "France";
  74. case 124:
  75. return "Canada";
  76. default: {
  77. char* country = malloc(4 * sizeof(char));
  78. snprintf(country, 4, "%d", country_num);
  79. return country;
  80. }
  81. }
  82. }
  83. const char* get_network(int country_num, int network_num) {
  84. switch(country_num) {
  85. case 250:
  86. switch(network_num) {
  87. case 901:
  88. return "IDFM";
  89. default:
  90. return "Unknown";
  91. }
  92. case 124:
  93. switch(network_num) {
  94. case 1:
  95. return "STM";
  96. default:
  97. return "Unknown";
  98. }
  99. default:
  100. return "Unknown";
  101. }
  102. }
  103. const char* get_transport_type(int type) {
  104. switch(type) {
  105. case BUS_URBAIN:
  106. return "Bus Urbain";
  107. case BUS_INTERURBAIN:
  108. return "Bus Interurbain";
  109. case METRO:
  110. return "Metro";
  111. case TRAM:
  112. return "Tram";
  113. case TRAIN:
  114. return "Train";
  115. case PARKING:
  116. return "Parking";
  117. default:
  118. return "Unknown";
  119. }
  120. }
  121. const char* get_service_provider(int provider) {
  122. switch(provider) {
  123. case 2:
  124. return "SNCF";
  125. case 3:
  126. return "RATP";
  127. case 4:
  128. return "IDF Mobilites";
  129. case 10:
  130. return "IDF Mobilites";
  131. case 115:
  132. return "CSO (VEOLIA)";
  133. case 116:
  134. return "R'Bus (VEOLIA)";
  135. case 156:
  136. return "Phebus";
  137. case 175:
  138. return "RATP (Veolia Transport Nanterre)";
  139. default:
  140. return "Unknown";
  141. }
  142. }
  143. const char* get_transition_type(int transition) {
  144. switch(transition) {
  145. case 1:
  146. return "Validation - entry";
  147. case 2:
  148. return "Validation - exit";
  149. case 4:
  150. return "Controle volant (a bord)";
  151. case 5:
  152. return "Test validation";
  153. case 6:
  154. return "Interchange validation - entry";
  155. case 7:
  156. return "Interchange validation - exit";
  157. case 9:
  158. return "Validation cancelled";
  159. case 10:
  160. return "Validation - entry";
  161. case 13:
  162. return "Distribution";
  163. case 15:
  164. return "Invalidation";
  165. default: {
  166. return "Unknown";
  167. }
  168. }
  169. }
  170. const char* get_navigo_type(int type) {
  171. switch(type) {
  172. case NAVIGO_EASY:
  173. return "Navigo Easy";
  174. case NAVIGO_DECOUVERTE:
  175. return "Navigo Decouverte";
  176. case NAVIGO_STANDARD:
  177. return "Navigo Standard";
  178. case NAVIGO_INTEGRAL:
  179. return "Navigo Integral";
  180. case IMAGINE_R:
  181. return "Imagine R";
  182. default:
  183. return "Navigo";
  184. }
  185. }
  186. const char* get_tariff(int tariff) {
  187. switch(tariff) {
  188. case 0x0000:
  189. return "Navigo Mois"; // Theoric
  190. case 0x0001:
  191. return "Navigo Semaine"; // Theoric
  192. case 0x0002:
  193. return "Navigo Annuel";
  194. case 0x0003:
  195. return "Navigo Jour"; // Theoric
  196. case 0x0004:
  197. return "Imagine R Junior";
  198. case 0x0005:
  199. return "Imagine R Etudiant";
  200. case 0x000D:
  201. return "Navigo Jeunes Week-end";
  202. case 0x0015:
  203. return "Paris-Visite"; // Theoric
  204. case 0x1000:
  205. return "Navigo Liberte+";
  206. case 0x4015:
  207. return "Paris-Visite (Enfant)"; // Theoric
  208. case 0x5000:
  209. return "Tickets T+";
  210. case 0x5004:
  211. return "Tickets OrlyBus"; // Theoric
  212. case 0x5005:
  213. return "Tickets RoissyBus"; // Theoric
  214. case 0x5006:
  215. return "Bus-Tram"; // Theoric
  216. case 0x5008:
  217. return "Metro-Train-RER"; // Theoric
  218. case 0x500b:
  219. return "Paris <> Aeroports"; // Theoric
  220. case 0x5010:
  221. return "Tickets T+ (Reduit)"; // Theoric
  222. case 0x5016:
  223. return "Bus-Tram (Reduit)"; // Theoric
  224. case 0x5018:
  225. return "Metro-Train-RER (Reduit)"; // Theoric
  226. case 0x501b:
  227. return "Paris <> Aeroports (Reduit)"; // Theoric
  228. default: {
  229. char* tariff_str = malloc(6 * sizeof(char));
  230. snprintf(tariff_str, 6, "%d", tariff);
  231. return tariff_str;
  232. }
  233. }
  234. }
  235. bool is_ticket_count_available(int tariff) {
  236. return tariff >= 0x5000 && tariff <= 0x501b;
  237. }
  238. const char* get_pay_method(int pay_method) {
  239. switch(pay_method) {
  240. case 0x30:
  241. return "Apple Pay";
  242. case 0x80:
  243. return "Debit PME";
  244. case 0x90:
  245. return "Cash";
  246. case 0xA0:
  247. return "Mobility Check";
  248. case 0xB3:
  249. return "Payment Card";
  250. case 0xA4:
  251. return "Check";
  252. case 0xA5:
  253. return "Vacation Check";
  254. case 0xB7:
  255. return "Telepayment";
  256. case 0xD0:
  257. return "Remote Payment";
  258. case 0xD7:
  259. return "Voucher, Prepayment, Exchange Voucher, Travel Voucher";
  260. case 0xD9:
  261. return "Discount Voucher";
  262. default:
  263. return "Unknown";
  264. }
  265. }
  266. const char* get_zones(int* zones) {
  267. if(zones[0] && zones[4]) {
  268. return "All Zones (1-5)";
  269. } else if(zones[0] && zones[3]) {
  270. return "Zones 1-4";
  271. } else if(zones[0] && zones[2]) {
  272. return "Zones 1-3";
  273. } else if(zones[0] && zones[1]) {
  274. return "Zones 1-2";
  275. } else if(zones[0]) {
  276. return "Zone 1";
  277. } else if(zones[1] && zones[4]) {
  278. return "Zones 2-5";
  279. } else if(zones[1] && zones[3]) {
  280. return "Zones 2-4";
  281. } else if(zones[1] && zones[2]) {
  282. return "Zones 2-3";
  283. } else if(zones[1]) {
  284. return "Zone 2";
  285. } else if(zones[2] && zones[4]) {
  286. return "Zones 3-5";
  287. } else if(zones[2] && zones[3]) {
  288. return "Zones 3-4";
  289. } else if(zones[2]) {
  290. return "Zone 3";
  291. } else if(zones[3] && zones[4]) {
  292. return "Zones 4-5";
  293. } else if(zones[3]) {
  294. return "Zone 4";
  295. } else if(zones[4]) {
  296. return "Zone 5";
  297. } else {
  298. return "Unknown";
  299. }
  300. }
  301. const char* get_intercode_version(int version) {
  302. // version is a 6 bits int
  303. // if the first 3 bits are 000, it's a 1.x version
  304. // if the first 3 bits are 001, it's a 2.x version
  305. // else, it's unknown
  306. int major = (version >> 3) & 0x07;
  307. if(major == 0) {
  308. return "Intercode I";
  309. } else if(major == 1) {
  310. return "Intercode II";
  311. }
  312. return "Unknown";
  313. }
  314. int get_intercode_subversion(int version) {
  315. // subversion is a 3 bits int
  316. return version & 0x07;
  317. }
  318. const char* get_metro_station(int station_group_id, int station_id) {
  319. // Use NAVIGO_H constants
  320. if(station_group_id < 32 && station_id < 16) {
  321. return METRO_STATION_LIST[station_group_id][station_id];
  322. }
  323. // cast station_group_id-station_id to a string
  324. char* station = malloc(12 * sizeof(char));
  325. if(!station) {
  326. return "Unknown";
  327. }
  328. snprintf(station, 10, "%d-%d", station_group_id, station_id);
  329. return station;
  330. }
  331. const char* get_train_line(int station_group_id) {
  332. if(station_group_id < 77) {
  333. return TRAIN_LINES_LIST[station_group_id];
  334. }
  335. return "Unknown";
  336. }
  337. const char* get_train_station(int station_group_id, int station_id) {
  338. if(station_group_id < 77 && station_id < 19) {
  339. return TRAIN_STATION_LIST[station_group_id][station_id];
  340. }
  341. // cast station_group_id-station_id to a string
  342. char* station = malloc(12 * sizeof(char));
  343. if(!station) {
  344. return "Unknown";
  345. }
  346. snprintf(station, 10, "%d-%d", station_group_id, station_id);
  347. return station;
  348. }
  349. void show_event_info(
  350. NavigoCardEvent* event,
  351. NavigoCardContract* contracts,
  352. FuriString* parsed_data) {
  353. if(event->used_contract == 0) {
  354. furi_string_cat_printf(parsed_data, "No event data\n");
  355. return;
  356. }
  357. if(event->transport_type == BUS_URBAIN || event->transport_type == BUS_INTERURBAIN ||
  358. event->transport_type == METRO || event->transport_type == TRAM) {
  359. if(event->route_number_available) {
  360. if(event->transport_type == METRO && event->route_number == 103) {
  361. furi_string_cat_printf(
  362. parsed_data,
  363. "%s 3 bis\n%s\n",
  364. get_transport_type(event->transport_type),
  365. get_transition_type(event->transition));
  366. } else {
  367. furi_string_cat_printf(
  368. parsed_data,
  369. "%s %d\n%s\n",
  370. get_transport_type(event->transport_type),
  371. event->route_number,
  372. get_transition_type(event->transition));
  373. }
  374. } else {
  375. furi_string_cat_printf(
  376. parsed_data,
  377. "%s\n%s\n",
  378. get_transport_type(event->transport_type),
  379. get_transition_type(event->transition));
  380. }
  381. furi_string_cat_printf(
  382. parsed_data, "Transporter: %s\n", get_service_provider(event->service_provider));
  383. if(event->transport_type == METRO) {
  384. furi_string_cat_printf(
  385. parsed_data,
  386. "Station: %s\nSector: %s\n",
  387. get_metro_station(event->station_group_id, event->station_id),
  388. get_metro_station(event->station_group_id, 0));
  389. } else {
  390. furi_string_cat_printf(
  391. parsed_data, "Station ID: %d-%d\n", event->station_group_id, event->station_id);
  392. }
  393. if(event->location_gate_available) {
  394. furi_string_cat_printf(parsed_data, "Gate: %d\n", event->location_gate);
  395. }
  396. if(event->device_available) {
  397. if(event->transport_type == BUS_URBAIN || event->transport_type == BUS_INTERURBAIN) {
  398. const char* side = event->side == 0 ? "right" : "left";
  399. furi_string_cat_printf(parsed_data, "Door: %d\nSide: %s\n", event->door, side);
  400. } else {
  401. furi_string_cat_printf(parsed_data, "Device: %d\n", event->device);
  402. }
  403. }
  404. if(event->mission_available) {
  405. furi_string_cat_printf(parsed_data, "Mission: %d\n", event->mission);
  406. }
  407. if(event->vehicle_id_available) {
  408. furi_string_cat_printf(parsed_data, "Vehicle: %d\n", event->vehicle_id);
  409. }
  410. if(event->used_contract_available) {
  411. furi_string_cat_printf(
  412. parsed_data,
  413. "Contract: %d - %s\n",
  414. event->used_contract,
  415. get_tariff(contracts[event->used_contract - 1].tariff));
  416. }
  417. locale_format_datetime_cat(parsed_data, &event->date, true);
  418. furi_string_cat_printf(parsed_data, "\n");
  419. } else if(event->transport_type == TRAIN) {
  420. if(event->route_number_available) {
  421. furi_string_cat_printf(
  422. parsed_data,
  423. "RER %c\n%s\n",
  424. (65 + event->route_number - 17),
  425. get_transition_type(event->transition));
  426. } else {
  427. furi_string_cat_printf(
  428. parsed_data,
  429. "%s %s\n%s\n",
  430. get_transport_type(event->transport_type),
  431. get_train_line(event->station_group_id),
  432. get_transition_type(event->transition));
  433. }
  434. furi_string_cat_printf(
  435. parsed_data, "Transporter: %s\n", get_service_provider(event->service_provider));
  436. furi_string_cat_printf(
  437. parsed_data,
  438. "Station: %s\n",
  439. get_train_station(event->station_group_id, event->station_id));
  440. if(event->route_number_available) {
  441. furi_string_cat_printf(parsed_data, "Route: %d\n", event->route_number);
  442. }
  443. if(event->location_gate_available) {
  444. furi_string_cat_printf(parsed_data, "Gate: %d\n", event->location_gate);
  445. }
  446. if(event->device_available) {
  447. furi_string_cat_printf(parsed_data, "Device: %d\n", event->device);
  448. }
  449. if(event->mission_available) {
  450. furi_string_cat_printf(parsed_data, "Mission: %d\n", event->mission);
  451. }
  452. if(event->vehicle_id_available) {
  453. furi_string_cat_printf(parsed_data, "Vehicle: %d\n", event->vehicle_id);
  454. }
  455. if(event->used_contract_available) {
  456. furi_string_cat_printf(
  457. parsed_data,
  458. "Contract: %d - %s\n",
  459. event->used_contract,
  460. get_tariff(contracts[event->used_contract - 1].tariff));
  461. }
  462. locale_format_datetime_cat(parsed_data, &event->date, true);
  463. furi_string_cat_printf(parsed_data, "\n");
  464. } else {
  465. furi_string_cat_printf(
  466. parsed_data,
  467. "%s - %s\n",
  468. get_transport_type(event->transport_type),
  469. get_transition_type(event->transition));
  470. furi_string_cat_printf(
  471. parsed_data, "Transporter: %s\n", get_service_provider(event->service_provider));
  472. furi_string_cat_printf(
  473. parsed_data, "Station ID: %d-%d\n", event->station_group_id, event->station_id);
  474. if(event->location_gate_available) {
  475. furi_string_cat_printf(parsed_data, "Gate: %d\n", event->location_gate);
  476. }
  477. if(event->device_available) {
  478. furi_string_cat_printf(parsed_data, "Device: %d\n", event->device);
  479. }
  480. if(event->mission_available) {
  481. furi_string_cat_printf(parsed_data, "Mission: %d\n", event->mission);
  482. }
  483. if(event->vehicle_id_available) {
  484. furi_string_cat_printf(parsed_data, "Vehicle: %d\n", event->vehicle_id);
  485. }
  486. if(event->used_contract_available) {
  487. furi_string_cat_printf(
  488. parsed_data,
  489. "Contract: %d - %s\n",
  490. event->used_contract,
  491. get_tariff(contracts[event->used_contract - 1].tariff));
  492. }
  493. locale_format_datetime_cat(parsed_data, &event->date, true);
  494. furi_string_cat_printf(parsed_data, "\n");
  495. }
  496. }
  497. void show_contract_info(NavigoCardContract* contract, FuriString* parsed_data) {
  498. furi_string_cat_printf(parsed_data, "Type: %s\n", get_tariff(contract->tariff));
  499. if(is_ticket_count_available(contract->tariff)) {
  500. furi_string_cat_printf(parsed_data, "Remaining Tickets: %d\n", contract->counter.count);
  501. }
  502. if(contract->serial_number_available) {
  503. furi_string_cat_printf(parsed_data, "TCN Number: %d\n", contract->serial_number);
  504. }
  505. if(contract->pay_method_available) {
  506. furi_string_cat_printf(
  507. parsed_data, "Payment Method: %s\n", get_pay_method(contract->pay_method));
  508. }
  509. if(contract->price_amount_available) {
  510. furi_string_cat_printf(parsed_data, "Amount: %.2f EUR\n", contract->price_amount);
  511. }
  512. if(contract->end_date_available) {
  513. furi_string_cat_printf(parsed_data, "Valid\nfrom: ");
  514. locale_format_datetime_cat(parsed_data, &contract->start_date, false);
  515. furi_string_cat_printf(parsed_data, "\nto: ");
  516. locale_format_datetime_cat(parsed_data, &contract->end_date, false);
  517. furi_string_cat_printf(parsed_data, "\n");
  518. } else {
  519. furi_string_cat_printf(parsed_data, "Valid from\n");
  520. locale_format_datetime_cat(parsed_data, &contract->start_date, false);
  521. furi_string_cat_printf(parsed_data, "\n");
  522. }
  523. if(contract->zones_available) {
  524. furi_string_cat_printf(parsed_data, "%s\n", get_zones(contract->zones));
  525. }
  526. furi_string_cat_printf(parsed_data, "Sold on: ");
  527. locale_format_datetime_cat(parsed_data, &contract->sale_date, false);
  528. furi_string_cat_printf(parsed_data, "\n");
  529. furi_string_cat_printf(
  530. parsed_data, "Sales Agent: %s\n", get_service_provider(contract->sale_agent));
  531. furi_string_cat_printf(parsed_data, "Sales Terminal: %d\n", contract->sale_device);
  532. furi_string_cat_printf(parsed_data, "Status: %d\n", contract->status);
  533. furi_string_cat_printf(parsed_data, "Authenticity Code: %d\n", contract->authenticator);
  534. }
  535. void show_environment_info(NavigoCardEnv* environment, FuriString* parsed_data) {
  536. furi_string_cat_printf(
  537. parsed_data,
  538. "App Version: %s - v%d\n",
  539. get_intercode_version(environment->app_version),
  540. get_intercode_subversion(environment->app_version));
  541. furi_string_cat_printf(parsed_data, "Country: %s\n", get_country(environment->country_num));
  542. furi_string_cat_printf(
  543. parsed_data,
  544. "Network: %s\n",
  545. get_network(environment->country_num, environment->network_num));
  546. furi_string_cat_printf(parsed_data, "End of validity:\n");
  547. locale_format_datetime_cat(parsed_data, &environment->end_dt, false);
  548. furi_string_cat_printf(parsed_data, "\n");
  549. }
  550. void update_page_info(void* context, FuriString* parsed_data) {
  551. Metroflip* app = context;
  552. NavigoContext* ctx = app->navigo_context;
  553. if(ctx->page_id == 0) {
  554. furi_string_cat_printf(
  555. parsed_data,
  556. "\e#%s %u:\n",
  557. get_navigo_type(ctx->card->holder.card_status),
  558. ctx->card->card_number);
  559. furi_string_cat_printf(parsed_data, "\e#Contract 1:\n");
  560. show_contract_info(&ctx->card->contracts[0], parsed_data);
  561. } else if(ctx->page_id == 1) {
  562. furi_string_cat_printf(
  563. parsed_data,
  564. "\e#%s %u:\n",
  565. get_navigo_type(ctx->card->holder.card_status),
  566. ctx->card->card_number);
  567. furi_string_cat_printf(parsed_data, "\e#Contract 2:\n");
  568. show_contract_info(&ctx->card->contracts[1], parsed_data);
  569. } else if(ctx->page_id == 2) {
  570. furi_string_cat_printf(parsed_data, "\e#Environment:\n");
  571. show_environment_info(&ctx->card->environment, parsed_data);
  572. } else if(ctx->page_id == 3) {
  573. furi_string_cat_printf(parsed_data, "\e#Event 1:\n");
  574. show_event_info(&ctx->card->events[0], ctx->card->contracts, parsed_data);
  575. } else if(ctx->page_id == 4) {
  576. furi_string_cat_printf(parsed_data, "\e#Event 2:\n");
  577. show_event_info(&ctx->card->events[1], ctx->card->contracts, parsed_data);
  578. } else if(ctx->page_id == 5) {
  579. furi_string_cat_printf(parsed_data, "\e#Event 3:\n");
  580. show_event_info(&ctx->card->events[2], ctx->card->contracts, parsed_data);
  581. }
  582. }
  583. void update_widget_elements(void* context) {
  584. Metroflip* app = context;
  585. NavigoContext* ctx = app->navigo_context;
  586. Widget* widget = app->widget;
  587. if(ctx->page_id < 5) {
  588. widget_add_button_element(
  589. widget, GuiButtonTypeRight, "Next", metroflip_next_button_widget_callback, context);
  590. } else {
  591. widget_add_button_element(
  592. widget, GuiButtonTypeRight, "Exit", metroflip_next_button_widget_callback, context);
  593. }
  594. if(ctx->page_id > 0) {
  595. widget_add_button_element(
  596. widget, GuiButtonTypeLeft, "Back", metroflip_back_button_widget_callback, context);
  597. }
  598. }
  599. void metroflip_back_button_widget_callback(GuiButtonType result, InputType type, void* context) {
  600. Metroflip* app = context;
  601. NavigoContext* ctx = app->navigo_context;
  602. UNUSED(result);
  603. Widget* widget = app->widget;
  604. if(type == InputTypePress) {
  605. widget_reset(widget);
  606. FURI_LOG_I(TAG, "Page ID: %d -> %d", ctx->page_id, ctx->page_id - 1);
  607. if(ctx->page_id > 0) {
  608. if(ctx->page_id == 2 && ctx->card->contracts[1].present == 0) {
  609. ctx->page_id -= 1;
  610. }
  611. ctx->page_id -= 1;
  612. }
  613. FuriString* parsed_data = furi_string_alloc();
  614. // Ensure no nested mutexes
  615. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  616. update_page_info(app, parsed_data);
  617. furi_mutex_release(ctx->mutex);
  618. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  619. // widget_add_icon_element(widget, 0, 0, &I_RFIDDolphinReceive_97x61);
  620. // Ensure no nested mutexes
  621. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  622. update_widget_elements(app);
  623. furi_mutex_release(ctx->mutex);
  624. furi_string_free(parsed_data);
  625. }
  626. }
  627. void metroflip_next_button_widget_callback(GuiButtonType result, InputType type, void* context) {
  628. Metroflip* app = context;
  629. NavigoContext* ctx = app->navigo_context;
  630. UNUSED(result);
  631. Widget* widget = app->widget;
  632. if(type == InputTypePress) {
  633. widget_reset(widget);
  634. FURI_LOG_I(TAG, "Page ID: %d -> %d", ctx->page_id, ctx->page_id + 1);
  635. if(ctx->page_id < 5) {
  636. if(ctx->page_id == 0 && ctx->card->contracts[1].present == 0) {
  637. ctx->page_id += 1;
  638. }
  639. ctx->page_id += 1;
  640. } else {
  641. ctx->page_id = 0;
  642. scene_manager_search_and_switch_to_previous_scene(
  643. app->scene_manager, MetroflipSceneStart);
  644. return;
  645. }
  646. FuriString* parsed_data = furi_string_alloc();
  647. // Ensure no nested mutexes
  648. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  649. update_page_info(app, parsed_data);
  650. furi_mutex_release(ctx->mutex);
  651. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  652. // Ensure no nested mutexes
  653. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  654. update_widget_elements(app);
  655. furi_mutex_release(ctx->mutex);
  656. furi_string_free(parsed_data);
  657. }
  658. }
  659. void delay(int milliseconds) {
  660. furi_thread_flags_wait(0, FuriFlagWaitAny, milliseconds);
  661. }
  662. static NfcCommand metroflip_scene_navigo_poller_callback(NfcGenericEvent event, void* context) {
  663. furi_assert(event.protocol == NfcProtocolIso14443_4b);
  664. NfcCommand next_command = NfcCommandContinue;
  665. MetroflipPollerEventType stage = MetroflipPollerEventTypeStart;
  666. Metroflip* app = context;
  667. FuriString* parsed_data = furi_string_alloc();
  668. Widget* widget = app->widget;
  669. furi_string_reset(app->text_box_store);
  670. const Iso14443_4bPollerEvent* iso14443_4b_event = event.event_data;
  671. Iso14443_4bPoller* iso14443_4b_poller = event.instance;
  672. BitBuffer* tx_buffer = bit_buffer_alloc(Metroflip_POLLER_MAX_BUFFER_SIZE);
  673. BitBuffer* rx_buffer = bit_buffer_alloc(Metroflip_POLLER_MAX_BUFFER_SIZE);
  674. if(iso14443_4b_event->type == Iso14443_4bPollerEventTypeReady) {
  675. if(stage == MetroflipPollerEventTypeStart) {
  676. // Start Flipper vibration
  677. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  678. notification_message(notification, &sequence_set_vibro_on);
  679. delay(50);
  680. notification_message(notification, &sequence_reset_vibro);
  681. nfc_device_set_data(
  682. app->nfc_device, NfcProtocolIso14443_4b, nfc_poller_get_data(app->poller));
  683. Iso14443_4bError error;
  684. size_t response_length = 0;
  685. do {
  686. // Initialize the card data
  687. NavigoCardData* card = malloc(sizeof(NavigoCardData));
  688. // Select app ICC
  689. error = select_new_app(
  690. 0x00, 0x02, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  691. if(error != 0) {
  692. break;
  693. }
  694. // Check the response after selecting app
  695. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  696. break;
  697. }
  698. // Now send the read command for ICC
  699. error = read_new_file(0x01, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  700. if(error != 0) {
  701. break;
  702. }
  703. // Check the response after reading the file
  704. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  705. break;
  706. }
  707. char icc_bit_representation[response_length * 8 + 1];
  708. icc_bit_representation[0] = '\0';
  709. for(size_t i = 0; i < response_length; i++) {
  710. char bits[9];
  711. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  712. byte_to_binary(byte, bits);
  713. strlcat(icc_bit_representation, bits, sizeof(icc_bit_representation));
  714. }
  715. icc_bit_representation[response_length * 8] = '\0';
  716. FURI_LOG_I(TAG, "ICC bit representation: %s", icc_bit_representation);
  717. int start = 128, end = 159;
  718. card->card_number = bit_slice_to_dec(icc_bit_representation, start, end);
  719. // Select app for contracts
  720. error = select_new_app(
  721. 0x20, 0x20, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  722. if(error != 0) {
  723. break;
  724. }
  725. // Check the response after selecting app
  726. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  727. break;
  728. }
  729. // Prepare calypso structure
  730. CalypsoApp* NavigoContractStructure = get_navigo_contract_structure();
  731. if(!NavigoContractStructure) {
  732. FURI_LOG_E(TAG, "Failed to load Navigo Contract structure");
  733. break;
  734. }
  735. // Now send the read command for contracts
  736. for(size_t i = 1; i < 3; i++) {
  737. error =
  738. read_new_file(i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  739. if(error != 0) {
  740. break;
  741. }
  742. // Check the response after reading the file
  743. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  744. break;
  745. }
  746. char bit_representation[response_length * 8 + 1];
  747. bit_representation[0] = '\0';
  748. for(size_t i = 0; i < response_length; i++) {
  749. char bits[9];
  750. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  751. byte_to_binary(byte, bits);
  752. strlcat(bit_representation, bits, sizeof(bit_representation));
  753. }
  754. bit_representation[response_length * 8] = '\0';
  755. /* int count = 0;
  756. int start = 0, end = NavigoContractStructure->elements[0].bitmap->size;
  757. char bit_slice[end - start + 1];
  758. strncpy(bit_slice, bit_representation + start, end - start);
  759. bit_slice[end - start] = '\0';
  760. int* positions = get_bit_positions(bit_slice, &count);
  761. FURI_LOG_I(TAG, "Contract %d bit positions: %d", i, count);
  762. // print positions
  763. for(int i = 0; i < count; i++) {
  764. char* key =
  765. (NavigoContractStructure->elements[0]
  766. .bitmap->elements[positions[i]]
  767. .type == CALYPSO_ELEMENT_TYPE_FINAL ?
  768. NavigoContractStructure->elements[0]
  769. .bitmap->elements[positions[i]]
  770. .final->key :
  771. NavigoContractStructure->elements[0]
  772. .bitmap->elements[positions[i]]
  773. .bitmap->key);
  774. int offset = get_calypso_node_offset(
  775. bit_representation, key, NavigoContractStructure);
  776. FURI_LOG_I(
  777. TAG, "Position: %d, Key: %s, Offset: %d", positions[i], key, offset);
  778. } */
  779. if(bit_slice_to_dec(
  780. bit_representation,
  781. 0,
  782. NavigoContractStructure->elements[0].bitmap->size - 1) == 0) {
  783. break;
  784. }
  785. card->contracts[i - 1].present = 1;
  786. // 2. ContractTariff
  787. const char* contract_key = "ContractTariff";
  788. if(is_calypso_node_present(
  789. bit_representation, contract_key, NavigoContractStructure)) {
  790. int positionOffset = get_calypso_node_offset(
  791. bit_representation, contract_key, NavigoContractStructure);
  792. int start = positionOffset,
  793. end = positionOffset +
  794. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  795. card->contracts[i - 1].tariff =
  796. bit_slice_to_dec(bit_representation, start, end);
  797. }
  798. // 3. ContractSerialNumber
  799. contract_key = "ContractSerialNumber";
  800. if(is_calypso_node_present(
  801. bit_representation, contract_key, NavigoContractStructure)) {
  802. int positionOffset = get_calypso_node_offset(
  803. bit_representation, contract_key, NavigoContractStructure);
  804. int start = positionOffset,
  805. end = positionOffset +
  806. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  807. card->contracts[i - 1].serial_number =
  808. bit_slice_to_dec(bit_representation, start, end);
  809. card->contracts[i - 1].serial_number_available = true;
  810. }
  811. // 8. ContractPayMethod
  812. contract_key = "ContractPayMethod";
  813. if(is_calypso_node_present(
  814. bit_representation, contract_key, NavigoContractStructure)) {
  815. int positionOffset = get_calypso_node_offset(
  816. bit_representation, contract_key, NavigoContractStructure);
  817. int start = positionOffset,
  818. end = positionOffset +
  819. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  820. card->contracts[i - 1].pay_method =
  821. bit_slice_to_dec(bit_representation, start, end);
  822. card->contracts[i - 1].pay_method_available = true;
  823. }
  824. // 10. ContractPriceAmount
  825. contract_key = "ContractPriceAmount";
  826. if(is_calypso_node_present(
  827. bit_representation, contract_key, NavigoContractStructure)) {
  828. int positionOffset = get_calypso_node_offset(
  829. bit_representation, contract_key, NavigoContractStructure);
  830. int start = positionOffset,
  831. end = positionOffset +
  832. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  833. card->contracts[i - 1].price_amount =
  834. bit_slice_to_dec(bit_representation, start, end) / 100.0;
  835. card->contracts[i - 1].price_amount_available = true;
  836. }
  837. // 13.0. ContractValidityStartDate
  838. contract_key = "ContractValidityStartDate";
  839. if(is_calypso_node_present(
  840. bit_representation, contract_key, NavigoContractStructure)) {
  841. int positionOffset = get_calypso_node_offset(
  842. bit_representation, contract_key, NavigoContractStructure);
  843. int start = positionOffset,
  844. end = positionOffset +
  845. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  846. float decimal_value =
  847. bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
  848. uint64_t start_validity_timestamp = (decimal_value + (float)epoch) + 3600;
  849. datetime_timestamp_to_datetime(
  850. start_validity_timestamp, &card->contracts[i - 1].start_date);
  851. }
  852. // 13.2. ContractValidityEndDate
  853. contract_key = "ContractValidityEndDate";
  854. if(is_calypso_node_present(
  855. bit_representation, contract_key, NavigoContractStructure)) {
  856. int positionOffset = get_calypso_node_offset(
  857. bit_representation, contract_key, NavigoContractStructure);
  858. int start = positionOffset,
  859. end = positionOffset +
  860. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  861. float decimal_value =
  862. bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
  863. uint64_t end_validity_timestamp = (decimal_value + (float)epoch) + 3600;
  864. datetime_timestamp_to_datetime(
  865. end_validity_timestamp, &card->contracts[i - 1].end_date);
  866. card->contracts[i - 1].end_date_available = true;
  867. }
  868. // 13.6. ContractValidityZones
  869. contract_key = "ContractValidityZones";
  870. if(is_calypso_node_present(
  871. bit_representation, contract_key, NavigoContractStructure)) {
  872. int start = get_calypso_node_offset(
  873. bit_representation, contract_key, NavigoContractStructure);
  874. // binary form is 00011111 for zones 5, 4, 3, 2, 1
  875. for(int j = 0; j < 5; j++) {
  876. card->contracts[i - 1].zones[j] =
  877. bit_slice_to_dec(bit_representation, start + 3 + j, start + 3 + j);
  878. }
  879. card->contracts[i - 1].zones_available = true;
  880. }
  881. // 13.7. ContractValidityJourneys -- pas sûr de le mettre lui
  882. // 15.0. ContractValiditySaleDate
  883. contract_key = "ContractValiditySaleDate";
  884. if(is_calypso_node_present(
  885. bit_representation, contract_key, NavigoContractStructure)) {
  886. int positionOffset = get_calypso_node_offset(
  887. bit_representation, contract_key, NavigoContractStructure);
  888. int start = positionOffset,
  889. end = positionOffset +
  890. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  891. float decimal_value =
  892. bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
  893. uint64_t sale_timestamp = (decimal_value + (float)epoch) + 3600;
  894. datetime_timestamp_to_datetime(
  895. sale_timestamp, &card->contracts[i - 1].sale_date);
  896. }
  897. // 15.2. ContractValiditySaleAgent - FIX NEEDED
  898. contract_key = "ContractValiditySaleAgent";
  899. /* if(is_calypso_node_present(
  900. bit_representation, contract_key, NavigoContractStructure)) { */
  901. int positionOffset = get_calypso_node_offset(
  902. bit_representation, contract_key, NavigoContractStructure);
  903. int start = positionOffset,
  904. end = positionOffset +
  905. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  906. card->contracts[i - 1].sale_agent =
  907. bit_slice_to_dec(bit_representation, start, end);
  908. // }
  909. // 15.3. ContractValiditySaleDevice
  910. contract_key = "ContractValiditySaleDevice";
  911. if(is_calypso_node_present(
  912. bit_representation, contract_key, NavigoContractStructure)) {
  913. int positionOffset = get_calypso_node_offset(
  914. bit_representation, contract_key, NavigoContractStructure);
  915. int start = positionOffset,
  916. end = positionOffset +
  917. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  918. card->contracts[i - 1].sale_device =
  919. bit_slice_to_dec(bit_representation, start, end);
  920. }
  921. // 16. ContractStatus -- 0x1 ou 0xff
  922. contract_key = "ContractStatus";
  923. if(is_calypso_node_present(
  924. bit_representation, contract_key, NavigoContractStructure)) {
  925. int positionOffset = get_calypso_node_offset(
  926. bit_representation, contract_key, NavigoContractStructure);
  927. int start = positionOffset,
  928. end = positionOffset +
  929. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  930. card->contracts[i - 1].status =
  931. bit_slice_to_dec(bit_representation, start, end);
  932. }
  933. // 18. ContractAuthenticator
  934. contract_key = "ContractAuthenticator";
  935. if(is_calypso_node_present(
  936. bit_representation, contract_key, NavigoContractStructure)) {
  937. int positionOffset = get_calypso_node_offset(
  938. bit_representation, contract_key, NavigoContractStructure);
  939. int start = positionOffset,
  940. end = positionOffset +
  941. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  942. card->contracts[i - 1].authenticator =
  943. bit_slice_to_dec(bit_representation, start, end);
  944. }
  945. }
  946. // Free the calypso structure
  947. free_calypso_structure(NavigoContractStructure);
  948. // Select app for environment
  949. error = select_new_app(
  950. 0x20, 0x1, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  951. if(error != 0) {
  952. break;
  953. }
  954. // Check the response after selecting app
  955. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  956. break;
  957. }
  958. // read file 1
  959. error = read_new_file(1, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  960. if(error != 0) {
  961. break;
  962. }
  963. // Check the response after reading the file
  964. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  965. break;
  966. }
  967. char environment_bit_representation[response_length * 8 + 1];
  968. environment_bit_representation[0] = '\0';
  969. for(size_t i = 0; i < response_length; i++) {
  970. char bits[9];
  971. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  972. byte_to_binary(byte, bits);
  973. strlcat(
  974. environment_bit_representation,
  975. bits,
  976. sizeof(environment_bit_representation));
  977. }
  978. // FURI_LOG_I(
  979. // TAG, "Environment bit_representation: %s", environment_bit_representation);
  980. start = 0;
  981. end = 5;
  982. card->environment.app_version =
  983. bit_slice_to_dec(environment_bit_representation, start, end);
  984. start = 13;
  985. end = 16;
  986. card->environment.country_num =
  987. bit_slice_to_dec(environment_bit_representation, start, end) * 100 +
  988. bit_slice_to_dec(environment_bit_representation, start + 4, end + 4) * 10 +
  989. bit_slice_to_dec(environment_bit_representation, start + 8, end + 8);
  990. start = 25;
  991. end = 28;
  992. card->environment.network_num =
  993. bit_slice_to_dec(environment_bit_representation, start, end) * 100 +
  994. bit_slice_to_dec(environment_bit_representation, start + 4, end + 4) * 10 +
  995. bit_slice_to_dec(environment_bit_representation, start + 8, end + 8);
  996. start = 45;
  997. end = 58;
  998. float decimal_value = bit_slice_to_dec(environment_bit_representation, start, end);
  999. uint64_t end_validity_timestamp =
  1000. (decimal_value * 24 * 3600) + (float)epoch + 3600;
  1001. datetime_timestamp_to_datetime(end_validity_timestamp, &card->environment.end_dt);
  1002. start = 95;
  1003. end = 98;
  1004. card->holder.card_status =
  1005. bit_slice_to_dec(environment_bit_representation, start, end);
  1006. start = 99;
  1007. end = 104;
  1008. card->holder.commercial_id =
  1009. bit_slice_to_dec(environment_bit_representation, start, end);
  1010. // Select app for counters (remaining tickets on Navigo Easy)
  1011. error = select_new_app(
  1012. 0x20, 0x69, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  1013. if(error != 0) {
  1014. break;
  1015. }
  1016. // Check the response after selecting app
  1017. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  1018. break;
  1019. }
  1020. // read file 1
  1021. error = read_new_file(1, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  1022. if(error != 0) {
  1023. break;
  1024. }
  1025. // Check the response after reading the file
  1026. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  1027. break;
  1028. }
  1029. char counter_bit_representation[response_length * 8 + 1];
  1030. counter_bit_representation[0] = '\0';
  1031. for(size_t i = 0; i < response_length; i++) {
  1032. char bits[9];
  1033. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  1034. byte_to_binary(byte, bits);
  1035. strlcat(counter_bit_representation, bits, sizeof(counter_bit_representation));
  1036. }
  1037. // FURI_LOG_I(TAG, "Counter bit_representation: %s", counter_bit_representation);
  1038. // Ticket count (contract 1)
  1039. start = 0;
  1040. end = 5;
  1041. card->contracts[0].counter.count =
  1042. bit_slice_to_dec(counter_bit_representation, start, end);
  1043. start = 6;
  1044. end = 23;
  1045. card->contracts[0].counter.relative_first_stamp_15mn =
  1046. bit_slice_to_dec(counter_bit_representation, start, end);
  1047. // Ticket count (contract 2)
  1048. start = 24;
  1049. end = 29;
  1050. card->contracts[1].counter.count =
  1051. bit_slice_to_dec(counter_bit_representation, start, end);
  1052. start = 30;
  1053. end = 47;
  1054. card->contracts[1].counter.relative_first_stamp_15mn =
  1055. bit_slice_to_dec(counter_bit_representation, start, end);
  1056. // FURI_LOG_I(TAG, "Ticket count 1: %d", card->ticket_counts[0]);
  1057. // FURI_LOG_I(TAG, "Ticket count 2: %d", card->ticket_counts[1]);
  1058. // Select app for events
  1059. error = select_new_app(
  1060. 0x20, 0x10, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  1061. if(error != 0) {
  1062. break;
  1063. }
  1064. // Check the response after selecting app
  1065. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  1066. break;
  1067. }
  1068. // Load the calypso structure for events
  1069. CalypsoApp* NavigoEventStructure = get_navigo_event_structure();
  1070. if(!NavigoEventStructure) {
  1071. FURI_LOG_E(TAG, "Failed to load Navigo Event structure");
  1072. break;
  1073. }
  1074. // furi_string_cat_printf(parsed_data, "\e#Events :\n");
  1075. // Now send the read command for events
  1076. for(size_t i = 1; i < 4; i++) {
  1077. error =
  1078. read_new_file(i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  1079. if(error != 0) {
  1080. break;
  1081. }
  1082. // Check the response after reading the file
  1083. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  1084. break;
  1085. }
  1086. char event_bit_representation[response_length * 8 + 1];
  1087. event_bit_representation[0] = '\0';
  1088. for(size_t i = 0; i < response_length; i++) {
  1089. char bits[9];
  1090. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  1091. byte_to_binary(byte, bits);
  1092. strlcat(event_bit_representation, bits, sizeof(event_bit_representation));
  1093. }
  1094. // furi_string_cat_printf(parsed_data, "Event 0%d :\n", i);
  1095. /* int count = 0;
  1096. int start = 25, end = 52;
  1097. char bit_slice[end - start + 2];
  1098. strncpy(bit_slice, event_bit_representation + start, end - start + 1);
  1099. bit_slice[end - start + 1] = '\0';
  1100. int* positions = get_bit_positions(bit_slice, &count);
  1101. FURI_LOG_I(TAG, "Positions: ");
  1102. for(int i = 0; i < count; i++) {
  1103. FURI_LOG_I(TAG, "%d ", positions[i]);
  1104. } */
  1105. // 2. EventCode
  1106. const char* event_key = "EventCode";
  1107. if(is_calypso_node_present(
  1108. event_bit_representation, event_key, NavigoEventStructure)) {
  1109. int positionOffset = get_calypso_node_offset(
  1110. event_bit_representation, event_key, NavigoEventStructure);
  1111. int start = positionOffset,
  1112. end = positionOffset +
  1113. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1114. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1115. card->events[i - 1].transport_type = decimal_value >> 4;
  1116. card->events[i - 1].transition = decimal_value & 15;
  1117. }
  1118. // 4. EventServiceProvider
  1119. event_key = "EventServiceProvider";
  1120. if(is_calypso_node_present(
  1121. event_bit_representation, event_key, NavigoEventStructure)) {
  1122. int positionOffset = get_calypso_node_offset(
  1123. event_bit_representation, event_key, NavigoEventStructure);
  1124. start = positionOffset,
  1125. end = positionOffset +
  1126. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1127. card->events[i - 1].service_provider =
  1128. bit_slice_to_dec(event_bit_representation, start, end);
  1129. }
  1130. // 8. EventLocationId
  1131. event_key = "EventLocationId";
  1132. if(is_calypso_node_present(
  1133. event_bit_representation, event_key, NavigoEventStructure)) {
  1134. int positionOffset = get_calypso_node_offset(
  1135. event_bit_representation, event_key, NavigoEventStructure);
  1136. start = positionOffset,
  1137. end = positionOffset +
  1138. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1139. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1140. card->events[i - 1].station_group_id = decimal_value >> 9;
  1141. card->events[i - 1].station_id = (decimal_value >> 4) & 31;
  1142. }
  1143. // 9. EventLocationGate
  1144. event_key = "EventLocationGate";
  1145. if(is_calypso_node_present(
  1146. event_bit_representation, event_key, NavigoEventStructure)) {
  1147. int positionOffset = get_calypso_node_offset(
  1148. event_bit_representation, event_key, NavigoEventStructure);
  1149. start = positionOffset,
  1150. end = positionOffset +
  1151. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1152. card->events[i - 1].location_gate =
  1153. bit_slice_to_dec(event_bit_representation, start, end);
  1154. card->events[i - 1].location_gate_available = true;
  1155. }
  1156. // 10. EventDevice
  1157. event_key = "EventDevice";
  1158. if(is_calypso_node_present(
  1159. event_bit_representation, event_key, NavigoEventStructure)) {
  1160. int positionOffset = get_calypso_node_offset(
  1161. event_bit_representation, event_key, NavigoEventStructure);
  1162. start = positionOffset,
  1163. end = positionOffset +
  1164. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1165. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1166. card->events[i - 1].device = decimal_value >> 8;
  1167. card->events[i - 1].door = card->events[i - 1].device / 2 + 1;
  1168. card->events[i - 1].side = card->events[i - 1].device % 2;
  1169. card->events[i - 1].device_available = true;
  1170. }
  1171. // 11. EventRouteNumber
  1172. event_key = "EventRouteNumber";
  1173. if(is_calypso_node_present(
  1174. event_bit_representation, event_key, NavigoEventStructure)) {
  1175. int positionOffset = get_calypso_node_offset(
  1176. event_bit_representation, event_key, NavigoEventStructure);
  1177. start = positionOffset,
  1178. end = positionOffset +
  1179. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1180. card->events[i - 1].route_number =
  1181. bit_slice_to_dec(event_bit_representation, start, end);
  1182. card->events[i - 1].route_number_available = true;
  1183. }
  1184. // 13. EventJourneyRun
  1185. event_key = "EventJourneyRun";
  1186. if(is_calypso_node_present(
  1187. event_bit_representation, event_key, NavigoEventStructure)) {
  1188. int positionOffset = get_calypso_node_offset(
  1189. event_bit_representation, event_key, NavigoEventStructure);
  1190. start = positionOffset,
  1191. end = positionOffset +
  1192. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1193. card->events[i - 1].mission =
  1194. bit_slice_to_dec(event_bit_representation, start, end);
  1195. card->events[i - 1].mission_available = true;
  1196. }
  1197. // 14. EventVehicleId
  1198. event_key = "EventVehicleId";
  1199. if(is_calypso_node_present(
  1200. event_bit_representation, event_key, NavigoEventStructure)) {
  1201. int positionOffset = get_calypso_node_offset(
  1202. event_bit_representation, event_key, NavigoEventStructure);
  1203. start = positionOffset,
  1204. end = positionOffset +
  1205. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1206. card->events[i - 1].vehicle_id =
  1207. bit_slice_to_dec(event_bit_representation, start, end);
  1208. card->events[i - 1].vehicle_id_available = true;
  1209. }
  1210. // 25. EventContractPointer
  1211. event_key = "EventContractPointer";
  1212. if(is_calypso_node_present(
  1213. event_bit_representation, event_key, NavigoEventStructure)) {
  1214. int positionOffset = get_calypso_node_offset(
  1215. event_bit_representation, event_key, NavigoEventStructure);
  1216. start = positionOffset,
  1217. end = positionOffset +
  1218. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1219. card->events[i - 1].used_contract =
  1220. bit_slice_to_dec(event_bit_representation, start, end);
  1221. card->events[i - 1].used_contract_available = true;
  1222. }
  1223. // EventDateStamp
  1224. event_key = "EventDateStamp";
  1225. int positionOffset = get_calypso_node_offset(
  1226. event_bit_representation, event_key, NavigoEventStructure);
  1227. start = positionOffset,
  1228. end = positionOffset + get_calypso_node_size(event_key, NavigoEventStructure) -
  1229. 1;
  1230. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1231. uint64_t date_timestamp = (decimal_value * 24 * 3600) + epoch + 3600;
  1232. datetime_timestamp_to_datetime(date_timestamp, &card->events[i - 1].date);
  1233. // EventTimeStamp
  1234. event_key = "EventTimeStamp";
  1235. positionOffset = get_calypso_node_offset(
  1236. event_bit_representation, event_key, NavigoEventStructure);
  1237. start = positionOffset,
  1238. end = positionOffset + get_calypso_node_size(event_key, NavigoEventStructure) -
  1239. 1;
  1240. decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1241. card->events[i - 1].date.hour = (decimal_value * 60) / 3600;
  1242. card->events[i - 1].date.minute = ((decimal_value * 60) % 3600) / 60;
  1243. card->events[i - 1].date.second = ((decimal_value * 60) % 3600) % 60;
  1244. }
  1245. // Free the calypso structure
  1246. free_calypso_structure(NavigoEventStructure);
  1247. UNUSED(TRANSITION_LIST);
  1248. UNUSED(TRANSPORT_LIST);
  1249. UNUSED(SERVICE_PROVIDERS);
  1250. widget_add_text_scroll_element(
  1251. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  1252. NavigoContext* context = malloc(sizeof(NavigoContext));
  1253. context->card = card;
  1254. context->page_id = 0;
  1255. context->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  1256. app->navigo_context = context;
  1257. // Ensure no nested mutexes
  1258. furi_mutex_acquire(context->mutex, FuriWaitForever);
  1259. update_page_info(app, parsed_data);
  1260. furi_mutex_release(context->mutex);
  1261. widget_add_text_scroll_element(
  1262. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  1263. // Ensure no nested mutexes
  1264. furi_mutex_acquire(context->mutex, FuriWaitForever);
  1265. update_widget_elements(app);
  1266. furi_mutex_release(context->mutex);
  1267. furi_string_free(parsed_data);
  1268. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  1269. metroflip_app_blink_stop(app);
  1270. stage = MetroflipPollerEventTypeSuccess;
  1271. next_command = NfcCommandStop;
  1272. } while(false);
  1273. if(stage != MetroflipPollerEventTypeSuccess) {
  1274. next_command = NfcCommandStop;
  1275. }
  1276. }
  1277. }
  1278. bit_buffer_free(tx_buffer);
  1279. bit_buffer_free(rx_buffer);
  1280. return next_command;
  1281. }
  1282. void metroflip_scene_navigo_on_enter(void* context) {
  1283. Metroflip* app = context;
  1284. dolphin_deed(DolphinDeedNfcRead);
  1285. // Setup view
  1286. Popup* popup = app->popup;
  1287. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  1288. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  1289. // Start worker
  1290. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  1291. nfc_scanner_alloc(app->nfc);
  1292. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolIso14443_4b);
  1293. nfc_poller_start(app->poller, metroflip_scene_navigo_poller_callback, app);
  1294. metroflip_app_blink_start(app);
  1295. }
  1296. bool metroflip_scene_navigo_on_event(void* context, SceneManagerEvent event) {
  1297. Metroflip* app = context;
  1298. bool consumed = false;
  1299. if(event.type == SceneManagerEventTypeCustom) {
  1300. if(event.event == MetroflipPollerEventTypeCardDetect) {
  1301. Popup* popup = app->popup;
  1302. popup_set_header(popup, "Scanning..", 68, 30, AlignLeft, AlignTop);
  1303. consumed = true;
  1304. } else if(event.event == MetroflipCustomEventPollerFileNotFound) {
  1305. Popup* popup = app->popup;
  1306. popup_set_header(popup, "Read Error,\n wrong card", 68, 30, AlignLeft, AlignTop);
  1307. consumed = true;
  1308. } else if(event.event == MetroflipCustomEventPollerFail) {
  1309. Popup* popup = app->popup;
  1310. popup_set_header(popup, "Error, try\n again", 68, 30, AlignLeft, AlignTop);
  1311. consumed = true;
  1312. }
  1313. } else if(event.type == SceneManagerEventTypeBack) {
  1314. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  1315. consumed = true;
  1316. }
  1317. return consumed;
  1318. }
  1319. void metroflip_scene_navigo_on_exit(void* context) {
  1320. Metroflip* app = context;
  1321. if(app->poller) {
  1322. nfc_poller_stop(app->poller);
  1323. nfc_poller_free(app->poller);
  1324. }
  1325. metroflip_app_blink_stop(app);
  1326. widget_reset(app->widget);
  1327. // Clear view
  1328. popup_reset(app->popup);
  1329. if(app->navigo_context) {
  1330. NavigoContext* ctx = app->navigo_context;
  1331. free(ctx->card);
  1332. furi_mutex_free(ctx->mutex);
  1333. free(ctx);
  1334. app->navigo_context = NULL;
  1335. }
  1336. }