metroflip_scene_navigo.c 55 KB

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