metroflip_scene_navigo.c 62 KB

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