metroflip_scene_navigo.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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. const char* get_tram_line(int route_number) {
  350. switch(route_number) {
  351. case 16:
  352. return "T6";
  353. default: {
  354. char* line = malloc(3 * sizeof(char));
  355. if(!line) {
  356. return "Unknown";
  357. }
  358. snprintf(line, 3, "T%d", route_number);
  359. return line;
  360. }
  361. }
  362. }
  363. void show_event_info(
  364. NavigoCardEvent* event,
  365. NavigoCardContract* contracts,
  366. FuriString* parsed_data) {
  367. if(event->used_contract == 0) {
  368. furi_string_cat_printf(parsed_data, "No event data\n");
  369. return;
  370. }
  371. if(event->transport_type == BUS_URBAIN || event->transport_type == BUS_INTERURBAIN ||
  372. event->transport_type == METRO || event->transport_type == TRAM) {
  373. if(event->route_number_available) {
  374. if(event->transport_type == METRO && event->route_number == 103) {
  375. furi_string_cat_printf(
  376. parsed_data,
  377. "%s 3 bis\n%s\n",
  378. get_transport_type(event->transport_type),
  379. get_transition_type(event->transition));
  380. } else if(event->transport_type == TRAM) {
  381. furi_string_cat_printf(
  382. parsed_data,
  383. "%s %s\n%s\n",
  384. get_transport_type(event->transport_type),
  385. get_tram_line(event->route_number),
  386. get_transition_type(event->transition));
  387. } else {
  388. furi_string_cat_printf(
  389. parsed_data,
  390. "%s %d\n%s\n",
  391. get_transport_type(event->transport_type),
  392. event->route_number,
  393. get_transition_type(event->transition));
  394. }
  395. } else {
  396. furi_string_cat_printf(
  397. parsed_data,
  398. "%s\n%s\n",
  399. get_transport_type(event->transport_type),
  400. get_transition_type(event->transition));
  401. }
  402. furi_string_cat_printf(
  403. parsed_data, "Transporter: %s\n", get_service_provider(event->service_provider));
  404. if(event->transport_type == METRO) {
  405. furi_string_cat_printf(
  406. parsed_data,
  407. "Station: %s\nSector: %s\n",
  408. get_metro_station(event->station_group_id, event->station_id),
  409. get_metro_station(event->station_group_id, 0));
  410. } else {
  411. furi_string_cat_printf(
  412. parsed_data, "Station ID: %d-%d\n", event->station_group_id, event->station_id);
  413. }
  414. if(event->location_gate_available) {
  415. furi_string_cat_printf(parsed_data, "Gate: %d\n", event->location_gate);
  416. }
  417. if(event->device_available) {
  418. if(event->transport_type == BUS_URBAIN || event->transport_type == BUS_INTERURBAIN) {
  419. const char* side = event->side == 0 ? "right" : "left";
  420. furi_string_cat_printf(parsed_data, "Door: %d\nSide: %s\n", event->door, side);
  421. } else {
  422. furi_string_cat_printf(parsed_data, "Device: %d\n", event->device);
  423. }
  424. }
  425. if(event->mission_available) {
  426. furi_string_cat_printf(parsed_data, "Mission: %d\n", event->mission);
  427. }
  428. if(event->vehicle_id_available) {
  429. furi_string_cat_printf(parsed_data, "Vehicle: %d\n", event->vehicle_id);
  430. }
  431. if(event->used_contract_available) {
  432. furi_string_cat_printf(
  433. parsed_data,
  434. "Contract: %d - %s\n",
  435. event->used_contract,
  436. get_tariff(contracts[event->used_contract - 1].tariff));
  437. }
  438. locale_format_datetime_cat(parsed_data, &event->date, true);
  439. furi_string_cat_printf(parsed_data, "\n");
  440. } else if(event->transport_type == TRAIN) {
  441. if(event->route_number_available) {
  442. furi_string_cat_printf(
  443. parsed_data,
  444. "RER %c\n%s\n",
  445. (65 + event->route_number - 17),
  446. get_transition_type(event->transition));
  447. } else {
  448. furi_string_cat_printf(
  449. parsed_data,
  450. "%s %s\n%s\n",
  451. get_transport_type(event->transport_type),
  452. get_train_line(event->station_group_id),
  453. get_transition_type(event->transition));
  454. }
  455. furi_string_cat_printf(
  456. parsed_data, "Transporter: %s\n", get_service_provider(event->service_provider));
  457. furi_string_cat_printf(
  458. parsed_data,
  459. "Station: %s\n",
  460. get_train_station(event->station_group_id, event->station_id));
  461. if(event->route_number_available) {
  462. furi_string_cat_printf(parsed_data, "Route: %d\n", event->route_number);
  463. }
  464. if(event->location_gate_available) {
  465. furi_string_cat_printf(parsed_data, "Gate: %d\n", event->location_gate);
  466. }
  467. if(event->device_available) {
  468. furi_string_cat_printf(parsed_data, "Device: %d\n", event->device);
  469. }
  470. if(event->mission_available) {
  471. furi_string_cat_printf(parsed_data, "Mission: %d\n", event->mission);
  472. }
  473. if(event->vehicle_id_available) {
  474. furi_string_cat_printf(parsed_data, "Vehicle: %d\n", event->vehicle_id);
  475. }
  476. if(event->used_contract_available) {
  477. furi_string_cat_printf(
  478. parsed_data,
  479. "Contract: %d - %s\n",
  480. event->used_contract,
  481. get_tariff(contracts[event->used_contract - 1].tariff));
  482. }
  483. locale_format_datetime_cat(parsed_data, &event->date, true);
  484. furi_string_cat_printf(parsed_data, "\n");
  485. } else {
  486. furi_string_cat_printf(
  487. parsed_data,
  488. "%s - %s\n",
  489. get_transport_type(event->transport_type),
  490. get_transition_type(event->transition));
  491. furi_string_cat_printf(
  492. parsed_data, "Transporter: %s\n", get_service_provider(event->service_provider));
  493. furi_string_cat_printf(
  494. parsed_data, "Station ID: %d-%d\n", event->station_group_id, event->station_id);
  495. if(event->location_gate_available) {
  496. furi_string_cat_printf(parsed_data, "Gate: %d\n", event->location_gate);
  497. }
  498. if(event->device_available) {
  499. furi_string_cat_printf(parsed_data, "Device: %d\n", event->device);
  500. }
  501. if(event->mission_available) {
  502. furi_string_cat_printf(parsed_data, "Mission: %d\n", event->mission);
  503. }
  504. if(event->vehicle_id_available) {
  505. furi_string_cat_printf(parsed_data, "Vehicle: %d\n", event->vehicle_id);
  506. }
  507. if(event->used_contract_available) {
  508. furi_string_cat_printf(
  509. parsed_data,
  510. "Contract: %d - %s\n",
  511. event->used_contract,
  512. get_tariff(contracts[event->used_contract - 1].tariff));
  513. }
  514. locale_format_datetime_cat(parsed_data, &event->date, true);
  515. furi_string_cat_printf(parsed_data, "\n");
  516. }
  517. }
  518. void show_contract_info(NavigoCardContract* contract, FuriString* parsed_data) {
  519. furi_string_cat_printf(parsed_data, "Type: %s\n", get_tariff(contract->tariff));
  520. if(is_ticket_count_available(contract->tariff)) {
  521. furi_string_cat_printf(parsed_data, "Remaining Tickets: %d\n", contract->counter.count);
  522. }
  523. if(contract->serial_number_available) {
  524. furi_string_cat_printf(parsed_data, "TCN Number: %d\n", contract->serial_number);
  525. }
  526. if(contract->pay_method_available) {
  527. furi_string_cat_printf(
  528. parsed_data, "Payment Method: %s\n", get_pay_method(contract->pay_method));
  529. }
  530. if(contract->price_amount_available) {
  531. furi_string_cat_printf(parsed_data, "Amount: %.2f EUR\n", contract->price_amount);
  532. }
  533. if(contract->end_date_available) {
  534. furi_string_cat_printf(parsed_data, "Valid\nfrom: ");
  535. locale_format_datetime_cat(parsed_data, &contract->start_date, false);
  536. furi_string_cat_printf(parsed_data, "\nto: ");
  537. locale_format_datetime_cat(parsed_data, &contract->end_date, false);
  538. furi_string_cat_printf(parsed_data, "\n");
  539. } else {
  540. furi_string_cat_printf(parsed_data, "Valid from\n");
  541. locale_format_datetime_cat(parsed_data, &contract->start_date, false);
  542. furi_string_cat_printf(parsed_data, "\n");
  543. }
  544. if(contract->zones_available) {
  545. furi_string_cat_printf(parsed_data, "%s\n", get_zones(contract->zones));
  546. }
  547. furi_string_cat_printf(parsed_data, "Sold on: ");
  548. locale_format_datetime_cat(parsed_data, &contract->sale_date, false);
  549. furi_string_cat_printf(parsed_data, "\n");
  550. furi_string_cat_printf(
  551. parsed_data, "Sales Agent: %s\n", get_service_provider(contract->sale_agent));
  552. furi_string_cat_printf(parsed_data, "Sales Terminal: %d\n", contract->sale_device);
  553. furi_string_cat_printf(parsed_data, "Status: %d\n", contract->status);
  554. furi_string_cat_printf(parsed_data, "Authenticity Code: %d\n", contract->authenticator);
  555. }
  556. void show_environment_info(NavigoCardEnv* environment, FuriString* parsed_data) {
  557. furi_string_cat_printf(
  558. parsed_data,
  559. "App Version: %s - v%d\n",
  560. get_intercode_version(environment->app_version),
  561. get_intercode_subversion(environment->app_version));
  562. furi_string_cat_printf(parsed_data, "Country: %s\n", get_country(environment->country_num));
  563. furi_string_cat_printf(
  564. parsed_data,
  565. "Network: %s\n",
  566. get_network(environment->country_num, environment->network_num));
  567. furi_string_cat_printf(parsed_data, "End of validity:\n");
  568. locale_format_datetime_cat(parsed_data, &environment->end_dt, false);
  569. furi_string_cat_printf(parsed_data, "\n");
  570. }
  571. void update_page_info(void* context, FuriString* parsed_data) {
  572. Metroflip* app = context;
  573. NavigoContext* ctx = app->navigo_context;
  574. if(ctx->page_id == 0) {
  575. furi_string_cat_printf(
  576. parsed_data,
  577. "\e#%s %u:\n",
  578. get_navigo_type(ctx->card->holder.card_status),
  579. ctx->card->card_number);
  580. furi_string_cat_printf(parsed_data, "\e#Contract 1:\n");
  581. show_contract_info(&ctx->card->contracts[0], parsed_data);
  582. } else if(ctx->page_id == 1) {
  583. furi_string_cat_printf(
  584. parsed_data,
  585. "\e#%s %u:\n",
  586. get_navigo_type(ctx->card->holder.card_status),
  587. ctx->card->card_number);
  588. furi_string_cat_printf(parsed_data, "\e#Contract 2:\n");
  589. show_contract_info(&ctx->card->contracts[1], parsed_data);
  590. } else if(ctx->page_id == 2) {
  591. furi_string_cat_printf(parsed_data, "\e#Environment:\n");
  592. show_environment_info(&ctx->card->environment, parsed_data);
  593. } else if(ctx->page_id == 3) {
  594. furi_string_cat_printf(parsed_data, "\e#Event 1:\n");
  595. show_event_info(&ctx->card->events[0], ctx->card->contracts, parsed_data);
  596. } else if(ctx->page_id == 4) {
  597. furi_string_cat_printf(parsed_data, "\e#Event 2:\n");
  598. show_event_info(&ctx->card->events[1], ctx->card->contracts, parsed_data);
  599. } else if(ctx->page_id == 5) {
  600. furi_string_cat_printf(parsed_data, "\e#Event 3:\n");
  601. show_event_info(&ctx->card->events[2], ctx->card->contracts, parsed_data);
  602. }
  603. }
  604. void update_widget_elements(void* context) {
  605. Metroflip* app = context;
  606. NavigoContext* ctx = app->navigo_context;
  607. Widget* widget = app->widget;
  608. if(ctx->page_id < 5) {
  609. widget_add_button_element(
  610. widget, GuiButtonTypeRight, "Next", metroflip_next_button_widget_callback, context);
  611. } else {
  612. widget_add_button_element(
  613. widget, GuiButtonTypeRight, "Exit", metroflip_next_button_widget_callback, context);
  614. }
  615. if(ctx->page_id > 0) {
  616. widget_add_button_element(
  617. widget, GuiButtonTypeLeft, "Back", metroflip_back_button_widget_callback, context);
  618. }
  619. }
  620. void metroflip_back_button_widget_callback(GuiButtonType result, InputType type, void* context) {
  621. Metroflip* app = context;
  622. NavigoContext* ctx = app->navigo_context;
  623. UNUSED(result);
  624. Widget* widget = app->widget;
  625. if(type == InputTypePress) {
  626. widget_reset(widget);
  627. FURI_LOG_I(TAG, "Page ID: %d -> %d", ctx->page_id, ctx->page_id - 1);
  628. if(ctx->page_id > 0) {
  629. if(ctx->page_id == 2 && ctx->card->contracts[1].present == 0) {
  630. ctx->page_id -= 1;
  631. }
  632. ctx->page_id -= 1;
  633. }
  634. FuriString* parsed_data = furi_string_alloc();
  635. // Ensure no nested mutexes
  636. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  637. update_page_info(app, parsed_data);
  638. furi_mutex_release(ctx->mutex);
  639. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  640. // widget_add_icon_element(widget, 0, 0, &I_RFIDDolphinReceive_97x61);
  641. // Ensure no nested mutexes
  642. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  643. update_widget_elements(app);
  644. furi_mutex_release(ctx->mutex);
  645. furi_string_free(parsed_data);
  646. }
  647. }
  648. void metroflip_next_button_widget_callback(GuiButtonType result, InputType type, void* context) {
  649. Metroflip* app = context;
  650. NavigoContext* ctx = app->navigo_context;
  651. UNUSED(result);
  652. Widget* widget = app->widget;
  653. if(type == InputTypePress) {
  654. widget_reset(widget);
  655. FURI_LOG_I(TAG, "Page ID: %d -> %d", ctx->page_id, ctx->page_id + 1);
  656. if(ctx->page_id < 5) {
  657. if(ctx->page_id == 0 && ctx->card->contracts[1].present == 0) {
  658. ctx->page_id += 1;
  659. }
  660. ctx->page_id += 1;
  661. } else {
  662. ctx->page_id = 0;
  663. scene_manager_search_and_switch_to_previous_scene(
  664. app->scene_manager, MetroflipSceneStart);
  665. return;
  666. }
  667. FuriString* parsed_data = furi_string_alloc();
  668. // Ensure no nested mutexes
  669. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  670. update_page_info(app, parsed_data);
  671. furi_mutex_release(ctx->mutex);
  672. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  673. // Ensure no nested mutexes
  674. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  675. update_widget_elements(app);
  676. furi_mutex_release(ctx->mutex);
  677. furi_string_free(parsed_data);
  678. }
  679. }
  680. void delay(int milliseconds) {
  681. furi_thread_flags_wait(0, FuriFlagWaitAny, milliseconds);
  682. }
  683. static NfcCommand metroflip_scene_navigo_poller_callback(NfcGenericEvent event, void* context) {
  684. furi_assert(event.protocol == NfcProtocolIso14443_4b);
  685. NfcCommand next_command = NfcCommandContinue;
  686. MetroflipPollerEventType stage = MetroflipPollerEventTypeStart;
  687. Metroflip* app = context;
  688. FuriString* parsed_data = furi_string_alloc();
  689. Widget* widget = app->widget;
  690. furi_string_reset(app->text_box_store);
  691. const Iso14443_4bPollerEvent* iso14443_4b_event = event.event_data;
  692. Iso14443_4bPoller* iso14443_4b_poller = event.instance;
  693. BitBuffer* tx_buffer = bit_buffer_alloc(Metroflip_POLLER_MAX_BUFFER_SIZE);
  694. BitBuffer* rx_buffer = bit_buffer_alloc(Metroflip_POLLER_MAX_BUFFER_SIZE);
  695. if(iso14443_4b_event->type == Iso14443_4bPollerEventTypeReady) {
  696. if(stage == MetroflipPollerEventTypeStart) {
  697. // Start Flipper vibration
  698. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  699. notification_message(notification, &sequence_set_vibro_on);
  700. delay(50);
  701. notification_message(notification, &sequence_reset_vibro);
  702. nfc_device_set_data(
  703. app->nfc_device, NfcProtocolIso14443_4b, nfc_poller_get_data(app->poller));
  704. Iso14443_4bError error;
  705. size_t response_length = 0;
  706. do {
  707. // Initialize the card data
  708. NavigoCardData* card = malloc(sizeof(NavigoCardData));
  709. // Select app ICC
  710. error = select_new_app(
  711. 0x00, 0x02, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  712. if(error != 0) {
  713. break;
  714. }
  715. // Check the response after selecting app
  716. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  717. break;
  718. }
  719. // Now send the read command for ICC
  720. error = read_new_file(0x01, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  721. if(error != 0) {
  722. break;
  723. }
  724. // Check the response after reading the file
  725. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  726. break;
  727. }
  728. char icc_bit_representation[response_length * 8 + 1];
  729. icc_bit_representation[0] = '\0';
  730. for(size_t i = 0; i < response_length; i++) {
  731. char bits[9];
  732. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  733. byte_to_binary(byte, bits);
  734. strlcat(icc_bit_representation, bits, sizeof(icc_bit_representation));
  735. }
  736. icc_bit_representation[response_length * 8] = '\0';
  737. FURI_LOG_I(TAG, "ICC bit representation: %s", icc_bit_representation);
  738. int start = 128, end = 159;
  739. card->card_number = bit_slice_to_dec(icc_bit_representation, start, end);
  740. // Select app for contracts
  741. error = select_new_app(
  742. 0x20, 0x20, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  743. if(error != 0) {
  744. break;
  745. }
  746. // Check the response after selecting app
  747. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  748. break;
  749. }
  750. // Prepare calypso structure
  751. CalypsoApp* NavigoContractStructure = get_navigo_contract_structure();
  752. if(!NavigoContractStructure) {
  753. FURI_LOG_E(TAG, "Failed to load Navigo Contract structure");
  754. break;
  755. }
  756. // Now send the read command for contracts
  757. for(size_t i = 1; i < 3; i++) {
  758. error =
  759. read_new_file(i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  760. if(error != 0) {
  761. break;
  762. }
  763. // Check the response after reading the file
  764. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  765. break;
  766. }
  767. char bit_representation[response_length * 8 + 1];
  768. bit_representation[0] = '\0';
  769. for(size_t i = 0; i < response_length; i++) {
  770. char bits[9];
  771. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  772. byte_to_binary(byte, bits);
  773. strlcat(bit_representation, bits, sizeof(bit_representation));
  774. }
  775. bit_representation[response_length * 8] = '\0';
  776. /* int count = 0;
  777. int start = 0, end = NavigoContractStructure->elements[0].bitmap->size;
  778. char bit_slice[end - start + 1];
  779. strncpy(bit_slice, bit_representation + start, end - start);
  780. bit_slice[end - start] = '\0';
  781. int* positions = get_bit_positions(bit_slice, &count);
  782. FURI_LOG_I(TAG, "Contract %d bit positions: %d", i, count);
  783. // print positions
  784. for(int i = 0; i < count; i++) {
  785. char* key =
  786. (NavigoContractStructure->elements[0]
  787. .bitmap->elements[positions[i]]
  788. .type == CALYPSO_ELEMENT_TYPE_FINAL ?
  789. NavigoContractStructure->elements[0]
  790. .bitmap->elements[positions[i]]
  791. .final->key :
  792. NavigoContractStructure->elements[0]
  793. .bitmap->elements[positions[i]]
  794. .bitmap->key);
  795. int offset = get_calypso_node_offset(
  796. bit_representation, key, NavigoContractStructure);
  797. FURI_LOG_I(
  798. TAG, "Position: %d, Key: %s, Offset: %d", positions[i], key, offset);
  799. } */
  800. if(bit_slice_to_dec(
  801. bit_representation,
  802. 0,
  803. NavigoContractStructure->elements[0].bitmap->size - 1) == 0) {
  804. break;
  805. }
  806. card->contracts[i - 1].present = 1;
  807. // 2. ContractTariff
  808. const char* contract_key = "ContractTariff";
  809. if(is_calypso_node_present(
  810. bit_representation, contract_key, NavigoContractStructure)) {
  811. int positionOffset = get_calypso_node_offset(
  812. bit_representation, contract_key, NavigoContractStructure);
  813. int start = positionOffset,
  814. end = positionOffset +
  815. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  816. card->contracts[i - 1].tariff =
  817. bit_slice_to_dec(bit_representation, start, end);
  818. }
  819. // 3. ContractSerialNumber
  820. contract_key = "ContractSerialNumber";
  821. if(is_calypso_node_present(
  822. bit_representation, contract_key, NavigoContractStructure)) {
  823. int positionOffset = get_calypso_node_offset(
  824. bit_representation, contract_key, NavigoContractStructure);
  825. int start = positionOffset,
  826. end = positionOffset +
  827. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  828. card->contracts[i - 1].serial_number =
  829. bit_slice_to_dec(bit_representation, start, end);
  830. card->contracts[i - 1].serial_number_available = true;
  831. }
  832. // 8. ContractPayMethod
  833. contract_key = "ContractPayMethod";
  834. if(is_calypso_node_present(
  835. bit_representation, contract_key, NavigoContractStructure)) {
  836. int positionOffset = get_calypso_node_offset(
  837. bit_representation, contract_key, NavigoContractStructure);
  838. int start = positionOffset,
  839. end = positionOffset +
  840. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  841. card->contracts[i - 1].pay_method =
  842. bit_slice_to_dec(bit_representation, start, end);
  843. card->contracts[i - 1].pay_method_available = true;
  844. }
  845. // 10. ContractPriceAmount
  846. contract_key = "ContractPriceAmount";
  847. if(is_calypso_node_present(
  848. bit_representation, contract_key, NavigoContractStructure)) {
  849. int positionOffset = get_calypso_node_offset(
  850. bit_representation, contract_key, NavigoContractStructure);
  851. int start = positionOffset,
  852. end = positionOffset +
  853. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  854. card->contracts[i - 1].price_amount =
  855. bit_slice_to_dec(bit_representation, start, end) / 100.0;
  856. card->contracts[i - 1].price_amount_available = true;
  857. }
  858. // 13.0. ContractValidityStartDate
  859. contract_key = "ContractValidityStartDate";
  860. if(is_calypso_node_present(
  861. bit_representation, contract_key, NavigoContractStructure)) {
  862. int positionOffset = get_calypso_node_offset(
  863. bit_representation, contract_key, NavigoContractStructure);
  864. int start = positionOffset,
  865. end = positionOffset +
  866. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  867. float decimal_value =
  868. bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
  869. uint64_t start_validity_timestamp = (decimal_value + (float)epoch) + 3600;
  870. datetime_timestamp_to_datetime(
  871. start_validity_timestamp, &card->contracts[i - 1].start_date);
  872. }
  873. // 13.2. ContractValidityEndDate
  874. contract_key = "ContractValidityEndDate";
  875. if(is_calypso_node_present(
  876. bit_representation, contract_key, NavigoContractStructure)) {
  877. int positionOffset = get_calypso_node_offset(
  878. bit_representation, contract_key, NavigoContractStructure);
  879. int start = positionOffset,
  880. end = positionOffset +
  881. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  882. float decimal_value =
  883. bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
  884. uint64_t end_validity_timestamp = (decimal_value + (float)epoch) + 3600;
  885. datetime_timestamp_to_datetime(
  886. end_validity_timestamp, &card->contracts[i - 1].end_date);
  887. card->contracts[i - 1].end_date_available = true;
  888. }
  889. // 13.6. ContractValidityZones
  890. contract_key = "ContractValidityZones";
  891. if(is_calypso_node_present(
  892. bit_representation, contract_key, NavigoContractStructure)) {
  893. int start = get_calypso_node_offset(
  894. bit_representation, contract_key, NavigoContractStructure);
  895. // binary form is 00011111 for zones 5, 4, 3, 2, 1
  896. for(int j = 0; j < 5; j++) {
  897. card->contracts[i - 1].zones[j] =
  898. bit_slice_to_dec(bit_representation, start + 3 + j, start + 3 + j);
  899. }
  900. card->contracts[i - 1].zones_available = true;
  901. }
  902. // 13.7. ContractValidityJourneys -- pas sûr de le mettre lui
  903. // 15.0. ContractValiditySaleDate
  904. contract_key = "ContractValiditySaleDate";
  905. if(is_calypso_node_present(
  906. bit_representation, contract_key, NavigoContractStructure)) {
  907. int positionOffset = get_calypso_node_offset(
  908. bit_representation, contract_key, NavigoContractStructure);
  909. int start = positionOffset,
  910. end = positionOffset +
  911. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  912. float decimal_value =
  913. bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
  914. uint64_t sale_timestamp = (decimal_value + (float)epoch) + 3600;
  915. datetime_timestamp_to_datetime(
  916. sale_timestamp, &card->contracts[i - 1].sale_date);
  917. }
  918. // 15.2. ContractValiditySaleAgent - FIX NEEDED
  919. contract_key = "ContractValiditySaleAgent";
  920. /* if(is_calypso_node_present(
  921. bit_representation, contract_key, NavigoContractStructure)) { */
  922. int positionOffset = get_calypso_node_offset(
  923. bit_representation, contract_key, NavigoContractStructure);
  924. int start = positionOffset,
  925. end = positionOffset +
  926. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  927. card->contracts[i - 1].sale_agent =
  928. bit_slice_to_dec(bit_representation, start, end);
  929. // }
  930. // 15.3. ContractValiditySaleDevice
  931. contract_key = "ContractValiditySaleDevice";
  932. if(is_calypso_node_present(
  933. bit_representation, contract_key, NavigoContractStructure)) {
  934. int positionOffset = get_calypso_node_offset(
  935. bit_representation, contract_key, NavigoContractStructure);
  936. int start = positionOffset,
  937. end = positionOffset +
  938. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  939. card->contracts[i - 1].sale_device =
  940. bit_slice_to_dec(bit_representation, start, end);
  941. }
  942. // 16. ContractStatus -- 0x1 ou 0xff
  943. contract_key = "ContractStatus";
  944. if(is_calypso_node_present(
  945. bit_representation, contract_key, NavigoContractStructure)) {
  946. int positionOffset = get_calypso_node_offset(
  947. bit_representation, contract_key, NavigoContractStructure);
  948. int start = positionOffset,
  949. end = positionOffset +
  950. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  951. card->contracts[i - 1].status =
  952. bit_slice_to_dec(bit_representation, start, end);
  953. }
  954. // 18. ContractAuthenticator
  955. contract_key = "ContractAuthenticator";
  956. if(is_calypso_node_present(
  957. bit_representation, contract_key, NavigoContractStructure)) {
  958. int positionOffset = get_calypso_node_offset(
  959. bit_representation, contract_key, NavigoContractStructure);
  960. int start = positionOffset,
  961. end = positionOffset +
  962. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  963. card->contracts[i - 1].authenticator =
  964. bit_slice_to_dec(bit_representation, start, end);
  965. }
  966. }
  967. // Free the calypso structure
  968. free_calypso_structure(NavigoContractStructure);
  969. // Select app for environment
  970. error = select_new_app(
  971. 0x20, 0x1, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  972. if(error != 0) {
  973. break;
  974. }
  975. // Check the response after selecting app
  976. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  977. break;
  978. }
  979. // read file 1
  980. error = read_new_file(1, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  981. if(error != 0) {
  982. break;
  983. }
  984. // Check the response after reading the file
  985. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  986. break;
  987. }
  988. char environment_bit_representation[response_length * 8 + 1];
  989. environment_bit_representation[0] = '\0';
  990. for(size_t i = 0; i < response_length; i++) {
  991. char bits[9];
  992. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  993. byte_to_binary(byte, bits);
  994. strlcat(
  995. environment_bit_representation,
  996. bits,
  997. sizeof(environment_bit_representation));
  998. }
  999. // FURI_LOG_I(
  1000. // TAG, "Environment bit_representation: %s", environment_bit_representation);
  1001. start = 0;
  1002. end = 5;
  1003. card->environment.app_version =
  1004. bit_slice_to_dec(environment_bit_representation, start, end);
  1005. start = 13;
  1006. end = 16;
  1007. card->environment.country_num =
  1008. bit_slice_to_dec(environment_bit_representation, start, end) * 100 +
  1009. bit_slice_to_dec(environment_bit_representation, start + 4, end + 4) * 10 +
  1010. bit_slice_to_dec(environment_bit_representation, start + 8, end + 8);
  1011. start = 25;
  1012. end = 28;
  1013. card->environment.network_num =
  1014. bit_slice_to_dec(environment_bit_representation, start, end) * 100 +
  1015. bit_slice_to_dec(environment_bit_representation, start + 4, end + 4) * 10 +
  1016. bit_slice_to_dec(environment_bit_representation, start + 8, end + 8);
  1017. start = 45;
  1018. end = 58;
  1019. float decimal_value = bit_slice_to_dec(environment_bit_representation, start, end);
  1020. uint64_t end_validity_timestamp =
  1021. (decimal_value * 24 * 3600) + (float)epoch + 3600;
  1022. datetime_timestamp_to_datetime(end_validity_timestamp, &card->environment.end_dt);
  1023. start = 95;
  1024. end = 98;
  1025. card->holder.card_status =
  1026. bit_slice_to_dec(environment_bit_representation, start, end);
  1027. start = 99;
  1028. end = 104;
  1029. card->holder.commercial_id =
  1030. bit_slice_to_dec(environment_bit_representation, start, end);
  1031. // Select app for counters (remaining tickets on Navigo Easy)
  1032. error = select_new_app(
  1033. 0x20, 0x69, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  1034. if(error != 0) {
  1035. break;
  1036. }
  1037. // Check the response after selecting app
  1038. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  1039. break;
  1040. }
  1041. // read file 1
  1042. error = read_new_file(1, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  1043. if(error != 0) {
  1044. break;
  1045. }
  1046. // Check the response after reading the file
  1047. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  1048. break;
  1049. }
  1050. char counter_bit_representation[response_length * 8 + 1];
  1051. counter_bit_representation[0] = '\0';
  1052. for(size_t i = 0; i < response_length; i++) {
  1053. char bits[9];
  1054. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  1055. byte_to_binary(byte, bits);
  1056. strlcat(counter_bit_representation, bits, sizeof(counter_bit_representation));
  1057. }
  1058. // FURI_LOG_I(TAG, "Counter bit_representation: %s", counter_bit_representation);
  1059. // Ticket count (contract 1)
  1060. start = 0;
  1061. end = 5;
  1062. card->contracts[0].counter.count =
  1063. bit_slice_to_dec(counter_bit_representation, start, end);
  1064. start = 6;
  1065. end = 23;
  1066. card->contracts[0].counter.relative_first_stamp_15mn =
  1067. bit_slice_to_dec(counter_bit_representation, start, end);
  1068. // Ticket count (contract 2)
  1069. start = 24;
  1070. end = 29;
  1071. card->contracts[1].counter.count =
  1072. bit_slice_to_dec(counter_bit_representation, start, end);
  1073. start = 30;
  1074. end = 47;
  1075. card->contracts[1].counter.relative_first_stamp_15mn =
  1076. bit_slice_to_dec(counter_bit_representation, start, end);
  1077. // FURI_LOG_I(TAG, "Ticket count 1: %d", card->ticket_counts[0]);
  1078. // FURI_LOG_I(TAG, "Ticket count 2: %d", card->ticket_counts[1]);
  1079. // Select app for events
  1080. error = select_new_app(
  1081. 0x20, 0x10, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  1082. if(error != 0) {
  1083. break;
  1084. }
  1085. // Check the response after selecting app
  1086. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  1087. break;
  1088. }
  1089. // Load the calypso structure for events
  1090. CalypsoApp* NavigoEventStructure = get_navigo_event_structure();
  1091. if(!NavigoEventStructure) {
  1092. FURI_LOG_E(TAG, "Failed to load Navigo Event structure");
  1093. break;
  1094. }
  1095. // furi_string_cat_printf(parsed_data, "\e#Events :\n");
  1096. // Now send the read command for events
  1097. for(size_t i = 1; i < 4; i++) {
  1098. error =
  1099. read_new_file(i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  1100. if(error != 0) {
  1101. break;
  1102. }
  1103. // Check the response after reading the file
  1104. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  1105. break;
  1106. }
  1107. char event_bit_representation[response_length * 8 + 1];
  1108. event_bit_representation[0] = '\0';
  1109. for(size_t i = 0; i < response_length; i++) {
  1110. char bits[9];
  1111. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  1112. byte_to_binary(byte, bits);
  1113. strlcat(event_bit_representation, bits, sizeof(event_bit_representation));
  1114. }
  1115. // furi_string_cat_printf(parsed_data, "Event 0%d :\n", i);
  1116. /* int count = 0;
  1117. int start = 25, end = 52;
  1118. char bit_slice[end - start + 2];
  1119. strncpy(bit_slice, event_bit_representation + start, end - start + 1);
  1120. bit_slice[end - start + 1] = '\0';
  1121. int* positions = get_bit_positions(bit_slice, &count);
  1122. FURI_LOG_I(TAG, "Positions: ");
  1123. for(int i = 0; i < count; i++) {
  1124. FURI_LOG_I(TAG, "%d ", positions[i]);
  1125. } */
  1126. // 2. EventCode
  1127. const char* event_key = "EventCode";
  1128. if(is_calypso_node_present(
  1129. event_bit_representation, event_key, NavigoEventStructure)) {
  1130. int positionOffset = get_calypso_node_offset(
  1131. event_bit_representation, event_key, NavigoEventStructure);
  1132. int start = positionOffset,
  1133. end = positionOffset +
  1134. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1135. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1136. card->events[i - 1].transport_type = decimal_value >> 4;
  1137. card->events[i - 1].transition = decimal_value & 15;
  1138. }
  1139. // 4. EventServiceProvider
  1140. event_key = "EventServiceProvider";
  1141. if(is_calypso_node_present(
  1142. event_bit_representation, event_key, NavigoEventStructure)) {
  1143. int positionOffset = get_calypso_node_offset(
  1144. event_bit_representation, event_key, NavigoEventStructure);
  1145. start = positionOffset,
  1146. end = positionOffset +
  1147. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1148. card->events[i - 1].service_provider =
  1149. bit_slice_to_dec(event_bit_representation, start, end);
  1150. }
  1151. // 8. EventLocationId
  1152. event_key = "EventLocationId";
  1153. if(is_calypso_node_present(
  1154. event_bit_representation, event_key, NavigoEventStructure)) {
  1155. int positionOffset = get_calypso_node_offset(
  1156. event_bit_representation, event_key, NavigoEventStructure);
  1157. start = positionOffset,
  1158. end = positionOffset +
  1159. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1160. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1161. card->events[i - 1].station_group_id = decimal_value >> 9;
  1162. card->events[i - 1].station_id = (decimal_value >> 4) & 31;
  1163. }
  1164. // 9. EventLocationGate
  1165. event_key = "EventLocationGate";
  1166. if(is_calypso_node_present(
  1167. event_bit_representation, event_key, NavigoEventStructure)) {
  1168. int positionOffset = get_calypso_node_offset(
  1169. event_bit_representation, event_key, NavigoEventStructure);
  1170. start = positionOffset,
  1171. end = positionOffset +
  1172. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1173. card->events[i - 1].location_gate =
  1174. bit_slice_to_dec(event_bit_representation, start, end);
  1175. card->events[i - 1].location_gate_available = true;
  1176. }
  1177. // 10. EventDevice
  1178. event_key = "EventDevice";
  1179. if(is_calypso_node_present(
  1180. event_bit_representation, event_key, NavigoEventStructure)) {
  1181. int positionOffset = get_calypso_node_offset(
  1182. event_bit_representation, event_key, NavigoEventStructure);
  1183. start = positionOffset,
  1184. end = positionOffset +
  1185. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1186. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1187. card->events[i - 1].device = decimal_value >> 8;
  1188. card->events[i - 1].door = card->events[i - 1].device / 2 + 1;
  1189. card->events[i - 1].side = card->events[i - 1].device % 2;
  1190. card->events[i - 1].device_available = true;
  1191. }
  1192. // 11. EventRouteNumber
  1193. event_key = "EventRouteNumber";
  1194. if(is_calypso_node_present(
  1195. event_bit_representation, event_key, NavigoEventStructure)) {
  1196. int positionOffset = get_calypso_node_offset(
  1197. event_bit_representation, event_key, NavigoEventStructure);
  1198. start = positionOffset,
  1199. end = positionOffset +
  1200. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1201. card->events[i - 1].route_number =
  1202. bit_slice_to_dec(event_bit_representation, start, end);
  1203. card->events[i - 1].route_number_available = true;
  1204. }
  1205. // 13. EventJourneyRun
  1206. event_key = "EventJourneyRun";
  1207. if(is_calypso_node_present(
  1208. event_bit_representation, event_key, NavigoEventStructure)) {
  1209. int positionOffset = get_calypso_node_offset(
  1210. event_bit_representation, event_key, NavigoEventStructure);
  1211. start = positionOffset,
  1212. end = positionOffset +
  1213. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1214. card->events[i - 1].mission =
  1215. bit_slice_to_dec(event_bit_representation, start, end);
  1216. card->events[i - 1].mission_available = true;
  1217. }
  1218. // 14. EventVehicleId
  1219. event_key = "EventVehicleId";
  1220. if(is_calypso_node_present(
  1221. event_bit_representation, event_key, NavigoEventStructure)) {
  1222. int positionOffset = get_calypso_node_offset(
  1223. event_bit_representation, event_key, NavigoEventStructure);
  1224. start = positionOffset,
  1225. end = positionOffset +
  1226. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1227. card->events[i - 1].vehicle_id =
  1228. bit_slice_to_dec(event_bit_representation, start, end);
  1229. card->events[i - 1].vehicle_id_available = true;
  1230. }
  1231. // 25. EventContractPointer
  1232. event_key = "EventContractPointer";
  1233. if(is_calypso_node_present(
  1234. event_bit_representation, event_key, NavigoEventStructure)) {
  1235. int positionOffset = get_calypso_node_offset(
  1236. event_bit_representation, event_key, NavigoEventStructure);
  1237. start = positionOffset,
  1238. end = positionOffset +
  1239. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1240. card->events[i - 1].used_contract =
  1241. bit_slice_to_dec(event_bit_representation, start, end);
  1242. card->events[i - 1].used_contract_available = true;
  1243. }
  1244. // EventDateStamp
  1245. event_key = "EventDateStamp";
  1246. int positionOffset = get_calypso_node_offset(
  1247. event_bit_representation, event_key, NavigoEventStructure);
  1248. start = positionOffset,
  1249. end = positionOffset + get_calypso_node_size(event_key, NavigoEventStructure) -
  1250. 1;
  1251. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1252. uint64_t date_timestamp = (decimal_value * 24 * 3600) + epoch + 3600;
  1253. datetime_timestamp_to_datetime(date_timestamp, &card->events[i - 1].date);
  1254. // EventTimeStamp
  1255. event_key = "EventTimeStamp";
  1256. positionOffset = get_calypso_node_offset(
  1257. event_bit_representation, event_key, NavigoEventStructure);
  1258. start = positionOffset,
  1259. end = positionOffset + get_calypso_node_size(event_key, NavigoEventStructure) -
  1260. 1;
  1261. decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1262. card->events[i - 1].date.hour = (decimal_value * 60) / 3600;
  1263. card->events[i - 1].date.minute = ((decimal_value * 60) % 3600) / 60;
  1264. card->events[i - 1].date.second = ((decimal_value * 60) % 3600) % 60;
  1265. }
  1266. // Free the calypso structure
  1267. free_calypso_structure(NavigoEventStructure);
  1268. UNUSED(TRANSITION_LIST);
  1269. UNUSED(TRANSPORT_LIST);
  1270. UNUSED(SERVICE_PROVIDERS);
  1271. widget_add_text_scroll_element(
  1272. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  1273. NavigoContext* context = malloc(sizeof(NavigoContext));
  1274. context->card = card;
  1275. context->page_id = 0;
  1276. context->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  1277. app->navigo_context = context;
  1278. // Ensure no nested mutexes
  1279. furi_mutex_acquire(context->mutex, FuriWaitForever);
  1280. update_page_info(app, parsed_data);
  1281. furi_mutex_release(context->mutex);
  1282. widget_add_text_scroll_element(
  1283. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  1284. // Ensure no nested mutexes
  1285. furi_mutex_acquire(context->mutex, FuriWaitForever);
  1286. update_widget_elements(app);
  1287. furi_mutex_release(context->mutex);
  1288. furi_string_free(parsed_data);
  1289. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  1290. metroflip_app_blink_stop(app);
  1291. stage = MetroflipPollerEventTypeSuccess;
  1292. next_command = NfcCommandStop;
  1293. } while(false);
  1294. if(stage != MetroflipPollerEventTypeSuccess) {
  1295. next_command = NfcCommandStop;
  1296. }
  1297. }
  1298. }
  1299. bit_buffer_free(tx_buffer);
  1300. bit_buffer_free(rx_buffer);
  1301. return next_command;
  1302. }
  1303. void metroflip_scene_navigo_on_enter(void* context) {
  1304. Metroflip* app = context;
  1305. dolphin_deed(DolphinDeedNfcRead);
  1306. // Setup view
  1307. Popup* popup = app->popup;
  1308. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  1309. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  1310. // Start worker
  1311. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  1312. nfc_scanner_alloc(app->nfc);
  1313. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolIso14443_4b);
  1314. nfc_poller_start(app->poller, metroflip_scene_navigo_poller_callback, app);
  1315. metroflip_app_blink_start(app);
  1316. }
  1317. bool metroflip_scene_navigo_on_event(void* context, SceneManagerEvent event) {
  1318. Metroflip* app = context;
  1319. bool consumed = false;
  1320. if(event.type == SceneManagerEventTypeCustom) {
  1321. if(event.event == MetroflipPollerEventTypeCardDetect) {
  1322. Popup* popup = app->popup;
  1323. popup_set_header(popup, "Scanning..", 68, 30, AlignLeft, AlignTop);
  1324. consumed = true;
  1325. } else if(event.event == MetroflipCustomEventPollerFileNotFound) {
  1326. Popup* popup = app->popup;
  1327. popup_set_header(popup, "Read Error,\n wrong card", 68, 30, AlignLeft, AlignTop);
  1328. consumed = true;
  1329. } else if(event.event == MetroflipCustomEventPollerFail) {
  1330. Popup* popup = app->popup;
  1331. popup_set_header(popup, "Error, try\n again", 68, 30, AlignLeft, AlignTop);
  1332. consumed = true;
  1333. }
  1334. } else if(event.type == SceneManagerEventTypeBack) {
  1335. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  1336. consumed = true;
  1337. }
  1338. return consumed;
  1339. }
  1340. void metroflip_scene_navigo_on_exit(void* context) {
  1341. Metroflip* app = context;
  1342. if(app->poller) {
  1343. nfc_poller_stop(app->poller);
  1344. nfc_poller_free(app->poller);
  1345. }
  1346. metroflip_app_blink_stop(app);
  1347. widget_reset(app->widget);
  1348. // Clear view
  1349. popup_reset(app->popup);
  1350. if(app->navigo_context) {
  1351. NavigoContext* ctx = app->navigo_context;
  1352. free(ctx->card);
  1353. furi_mutex_free(ctx->mutex);
  1354. free(ctx);
  1355. app->navigo_context = NULL;
  1356. }
  1357. }