metroflip_scene_navigo.c 59 KB

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