metroflip_scene_navigo.c 59 KB

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