metroflip_scene_navigo.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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 - entree";
  128. case 2:
  129. return "Validation - sortie";
  130. case 4:
  131. return "Controle volant (a bord)";
  132. case 5:
  133. return "Validation de test";
  134. case 6:
  135. return "Validation en correspondance - entree";
  136. case 7:
  137. return "Validation en correspondance - sortie";
  138. case 9:
  139. return "Annulation de validation";
  140. case 10:
  141. return "Validation - entree";
  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 "Espece";
  193. case 0xA0:
  194. return "Cheque mobilite";
  195. case 0xB3:
  196. return "Carte de paiement";
  197. case 0xA4:
  198. return "Cheque";
  199. case 0xA5:
  200. return "Cheque vacance";
  201. case 0xB7:
  202. return "Telepaiement";
  203. case 0xD0:
  204. return "Telereglement";
  205. case 0xD7:
  206. return "Bon de caisse, Versement prealable, Bon d’echange, Bon Voyage";
  207. case 0xD9:
  208. return "Bon de reduction";
  209. default:
  210. return "Inconnu";
  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, "Transporteur : %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\nSecteur : %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, "ID Station : %d-%d\n", event->station_group_id, event->station_id);
  280. }
  281. if(event->location_gate_available) {
  282. furi_string_cat_printf(parsed_data, "Passage : %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 ? "droit" : "gauche";
  287. furi_string_cat_printf(parsed_data, "Porte : %d\nCote %s\n", event->door, side);
  288. } else {
  289. furi_string_cat_printf(parsed_data, "Equipement : %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, "Vehicule : %d\n", event->vehicle_id);
  297. }
  298. if(event->used_contract_available) {
  299. furi_string_cat_printf(parsed_data, "Contrat : %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, "Transporteur : %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, "Passage : %d\n", event->location_gate);
  329. }
  330. if(event->device_available) {
  331. furi_string_cat_printf(parsed_data, "Equipement : %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, "Vehicule : %d\n", event->vehicle_id);
  338. }
  339. if(event->used_contract_available) {
  340. furi_string_cat_printf(parsed_data, "Contrat : %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, "Transporteur : %s\n", get_service_provider(event->service_provider));
  352. furi_string_cat_printf(
  353. parsed_data, "ID Station : %d-%d\n", event->station_group_id, event->station_id);
  354. if(event->location_gate_available) {
  355. furi_string_cat_printf(parsed_data, "Passage : %d\n", event->location_gate);
  356. }
  357. if(event->device_available) {
  358. furi_string_cat_printf(parsed_data, "Equipement : %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, "Vehicule : %d\n", event->vehicle_id);
  365. }
  366. if(event->used_contract_available) {
  367. furi_string_cat_printf(parsed_data, "Contrat : %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, "Numero TCN : %d\n", contract->serial_number);
  377. }
  378. if(contract->pay_method_available) {
  379. furi_string_cat_printf(
  380. parsed_data, "Methode de paiement : %s\n", get_pay_method(contract->pay_method));
  381. }
  382. if(contract->price_amount_available) {
  383. furi_string_cat_printf(parsed_data, "Montant : %.2f EUR\n", contract->price_amount);
  384. }
  385. if(contract->tariff == 0x5000) {
  386. furi_string_cat_printf(parsed_data, "Titres restants : %d\n", ticket_count);
  387. }
  388. if(contract->end_date_available) {
  389. furi_string_cat_printf(parsed_data, "Valide\ndu : ");
  390. locale_format_datetime_cat(parsed_data, &contract->start_date, false);
  391. furi_string_cat_printf(parsed_data, "\nau : ");
  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, "Valide a partir du\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, "Vendu le : ");
  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. "Agent de vente : %s (%d)\n",
  417. get_service_provider(contract->sale_agent),
  418. contract->sale_agent);
  419. furi_string_cat_printf(parsed_data, "Terminal de vente : %d\n", contract->sale_device);
  420. furi_string_cat_printf(parsed_data, "Etat : %d\n", contract->status);
  421. furi_string_cat_printf(parsed_data, "Code authenticite : %d\n", contract->authenticator);
  422. }
  423. void show_environment_info(NavigoCardEnv* environment, FuriString* parsed_data) {
  424. furi_string_cat_printf(
  425. parsed_data, "Version de l'application : %d\n", environment->app_version);
  426. if(environment->country_num == 250) {
  427. furi_string_cat_printf(parsed_data, "Pays : France\n");
  428. } else {
  429. furi_string_cat_printf(parsed_data, "Pays : %d\n", environment->country_num);
  430. }
  431. if(environment->network_num == 901) {
  432. furi_string_cat_printf(parsed_data, "Reseau : Ile-de-France Mobilites\n");
  433. } else {
  434. furi_string_cat_printf(parsed_data, "Reseau : %d\n", environment->network_num);
  435. }
  436. furi_string_cat_printf(parsed_data, "Fin de validite:\n");
  437. locale_format_datetime_cat(parsed_data, &environment->end_dt, false);
  438. furi_string_cat_printf(parsed_data, "\n");
  439. }
  440. void update_page_info(void* context, FuriString* parsed_data) {
  441. Metroflip* app = context;
  442. NavigoContext* ctx = app->navigo_context;
  443. if(ctx->page_id == 0) {
  444. furi_string_cat_printf(
  445. parsed_data, "\e#%s :\n", get_navigo_type(ctx->card->holder.card_status));
  446. furi_string_cat_printf(parsed_data, "\e#Contrat 1 :\n");
  447. show_contract_info(&ctx->card->contracts[0], ctx->card->ticket_count, parsed_data);
  448. } else if(ctx->page_id == 1) {
  449. furi_string_cat_printf(
  450. parsed_data, "\e#%s :\n", get_navigo_type(ctx->card->holder.card_status));
  451. furi_string_cat_printf(parsed_data, "\e#Contrat 2 :\n");
  452. show_contract_info(&ctx->card->contracts[1], ctx->card->ticket_count, parsed_data);
  453. } else if(ctx->page_id == 2) {
  454. furi_string_cat_printf(parsed_data, "\e#Environnement :\n");
  455. show_environment_info(&ctx->card->environment, parsed_data);
  456. } else if(ctx->page_id == 3) {
  457. furi_string_cat_printf(parsed_data, "\e#Event 1 :\n");
  458. show_event_info(&ctx->card->events[0], parsed_data);
  459. } else if(ctx->page_id == 4) {
  460. furi_string_cat_printf(parsed_data, "\e#Event 2 :\n");
  461. show_event_info(&ctx->card->events[1], parsed_data);
  462. } else if(ctx->page_id == 5) {
  463. furi_string_cat_printf(parsed_data, "\e#Event 3 :\n");
  464. show_event_info(&ctx->card->events[2], parsed_data);
  465. }
  466. }
  467. void update_widget_elements(void* context) {
  468. Metroflip* app = context;
  469. NavigoContext* ctx = app->navigo_context;
  470. Widget* widget = app->widget;
  471. if(ctx->page_id < 5) {
  472. widget_add_button_element(
  473. widget, GuiButtonTypeRight, "Next", metroflip_next_button_widget_callback, context);
  474. } else {
  475. widget_add_button_element(
  476. widget, GuiButtonTypeRight, "Exit", metroflip_next_button_widget_callback, context);
  477. }
  478. if(ctx->page_id > 0) {
  479. widget_add_button_element(
  480. widget, GuiButtonTypeLeft, "Back", metroflip_back_button_widget_callback, context);
  481. }
  482. }
  483. void metroflip_back_button_widget_callback(GuiButtonType result, InputType type, void* context) {
  484. Metroflip* app = context;
  485. NavigoContext* ctx = app->navigo_context;
  486. UNUSED(result);
  487. Widget* widget = app->widget;
  488. if(type == InputTypePress) {
  489. widget_reset(widget);
  490. FURI_LOG_I(TAG, "Page ID: %d -> %d", ctx->page_id, ctx->page_id - 1);
  491. if(ctx->page_id > 0) {
  492. if(ctx->page_id == 2 && ctx->card->contracts[1].tariff == 0) {
  493. ctx->page_id -= 1;
  494. }
  495. ctx->page_id -= 1;
  496. }
  497. FuriString* parsed_data = furi_string_alloc();
  498. // Ensure no nested mutexes
  499. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  500. update_page_info(app, parsed_data);
  501. furi_mutex_release(ctx->mutex);
  502. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  503. // widget_add_icon_element(widget, 0, 0, &I_RFIDDolphinReceive_97x61);
  504. // Ensure no nested mutexes
  505. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  506. update_widget_elements(app);
  507. furi_mutex_release(ctx->mutex);
  508. furi_string_free(parsed_data);
  509. }
  510. }
  511. void metroflip_next_button_widget_callback(GuiButtonType result, InputType type, void* context) {
  512. Metroflip* app = context;
  513. NavigoContext* ctx = app->navigo_context;
  514. UNUSED(result);
  515. Widget* widget = app->widget;
  516. if(type == InputTypePress) {
  517. widget_reset(widget);
  518. FURI_LOG_I(TAG, "Page ID: %d -> %d", ctx->page_id, ctx->page_id + 1);
  519. if(ctx->page_id < 5) {
  520. if(ctx->page_id == 0 && ctx->card->contracts[1].tariff == 0) {
  521. ctx->page_id += 1;
  522. }
  523. ctx->page_id += 1;
  524. } else {
  525. ctx->page_id = 0;
  526. scene_manager_search_and_switch_to_previous_scene(
  527. app->scene_manager, MetroflipSceneStart);
  528. return;
  529. }
  530. FuriString* parsed_data = furi_string_alloc();
  531. // Ensure no nested mutexes
  532. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  533. update_page_info(app, parsed_data);
  534. furi_mutex_release(ctx->mutex);
  535. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  536. // Ensure no nested mutexes
  537. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  538. update_widget_elements(app);
  539. furi_mutex_release(ctx->mutex);
  540. furi_string_free(parsed_data);
  541. }
  542. }
  543. void delay(int milliseconds) {
  544. furi_thread_flags_wait(0, FuriFlagWaitAny, milliseconds);
  545. }
  546. static NfcCommand metroflip_scene_navigo_poller_callback(NfcGenericEvent event, void* context) {
  547. furi_assert(event.protocol == NfcProtocolIso14443_4b);
  548. NfcCommand next_command = NfcCommandContinue;
  549. MetroflipPollerEventType stage = MetroflipPollerEventTypeStart;
  550. Metroflip* app = context;
  551. FuriString* parsed_data = furi_string_alloc();
  552. Widget* widget = app->widget;
  553. furi_string_reset(app->text_box_store);
  554. const Iso14443_4bPollerEvent* iso14443_4b_event = event.event_data;
  555. Iso14443_4bPoller* iso14443_4b_poller = event.instance;
  556. BitBuffer* tx_buffer = bit_buffer_alloc(Metroflip_POLLER_MAX_BUFFER_SIZE);
  557. BitBuffer* rx_buffer = bit_buffer_alloc(Metroflip_POLLER_MAX_BUFFER_SIZE);
  558. if(iso14443_4b_event->type == Iso14443_4bPollerEventTypeReady) {
  559. if(stage == MetroflipPollerEventTypeStart) {
  560. // Start Flipper vibration
  561. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  562. notification_message(notification, &sequence_set_vibro_on);
  563. delay(50);
  564. notification_message(notification, &sequence_reset_vibro);
  565. nfc_device_set_data(
  566. app->nfc_device, NfcProtocolIso14443_4b, nfc_poller_get_data(app->poller));
  567. Iso14443_4bError error;
  568. size_t response_length = 0;
  569. do {
  570. // Initialize the card data
  571. NavigoCardData* card = malloc(sizeof(NavigoCardData));
  572. // Select app for contracts
  573. error =
  574. select_new_app(0x20, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  575. if(error != 0) {
  576. break;
  577. }
  578. // Check the response after selecting app
  579. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  580. break;
  581. }
  582. // Prepare calypso structure
  583. CalypsoApp* NavigoContractStructure = get_navigo_contract_structure();
  584. if(!NavigoContractStructure) {
  585. FURI_LOG_E(TAG, "Failed to load Navigo Contract structure");
  586. break;
  587. }
  588. // Now send the read command for contracts
  589. for(size_t i = 1; i < 3; i++) {
  590. error =
  591. read_new_file(i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  592. if(error != 0) {
  593. break;
  594. }
  595. // Check the response after reading the file
  596. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  597. break;
  598. }
  599. char bit_representation[response_length * 8 + 1];
  600. bit_representation[0] = '\0';
  601. for(size_t i = 0; i < response_length; i++) {
  602. char bits[9];
  603. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  604. byte_to_binary(byte, bits);
  605. strlcat(bit_representation, bits, sizeof(bit_representation));
  606. }
  607. bit_representation[response_length * 8] = '\0';
  608. /* int count = 0;
  609. int start = 0, end = NavigoContractStructure->elements[0].bitmap->size;
  610. char bit_slice[end - start + 1];
  611. strncpy(bit_slice, bit_representation + start, end - start);
  612. bit_slice[end - start] = '\0';
  613. int* positions = get_bit_positions(bit_slice, &count);
  614. FURI_LOG_I(TAG, "Contract %d bit positions: %d", i, count);
  615. // print positions
  616. for(int i = 0; i < count; i++) {
  617. char* key =
  618. (NavigoContractStructure->elements[0]
  619. .bitmap->elements[positions[i]]
  620. .type == CALYPSO_ELEMENT_TYPE_FINAL ?
  621. NavigoContractStructure->elements[0]
  622. .bitmap->elements[positions[i]]
  623. .final->key :
  624. NavigoContractStructure->elements[0]
  625. .bitmap->elements[positions[i]]
  626. .bitmap->key);
  627. int offset = get_calypso_node_offset(
  628. bit_representation, key, NavigoContractStructure);
  629. FURI_LOG_I(
  630. TAG, "Position: %d, Key: %s, Offset: %d", positions[i], key, offset);
  631. } */
  632. // 2. ContractTariff
  633. const char* contract_key = "ContractTariff";
  634. if(is_calypso_node_present(
  635. bit_representation, contract_key, NavigoContractStructure)) {
  636. int positionOffset = get_calypso_node_offset(
  637. bit_representation, contract_key, NavigoContractStructure);
  638. int start = positionOffset,
  639. end = positionOffset +
  640. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  641. card->contracts[i - 1].tariff =
  642. bit_slice_to_dec(bit_representation, start, end);
  643. }
  644. // 3. ContractSerialNumber
  645. contract_key = "ContractSerialNumber";
  646. if(is_calypso_node_present(
  647. bit_representation, contract_key, NavigoContractStructure)) {
  648. int positionOffset = get_calypso_node_offset(
  649. bit_representation, contract_key, NavigoContractStructure);
  650. int start = positionOffset,
  651. end = positionOffset +
  652. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  653. card->contracts[i - 1].serial_number =
  654. bit_slice_to_dec(bit_representation, start, end);
  655. card->contracts[i - 1].serial_number_available = true;
  656. }
  657. // 8. ContractPayMethod
  658. contract_key = "ContractPayMethod";
  659. if(is_calypso_node_present(
  660. bit_representation, contract_key, NavigoContractStructure)) {
  661. int positionOffset = get_calypso_node_offset(
  662. bit_representation, contract_key, NavigoContractStructure);
  663. int start = positionOffset,
  664. end = positionOffset +
  665. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  666. card->contracts[i - 1].pay_method =
  667. bit_slice_to_dec(bit_representation, start, end);
  668. card->contracts[i - 1].pay_method_available = true;
  669. }
  670. // 10. ContractPriceAmount
  671. contract_key = "ContractPriceAmount";
  672. if(is_calypso_node_present(
  673. bit_representation, contract_key, NavigoContractStructure)) {
  674. int positionOffset = get_calypso_node_offset(
  675. bit_representation, contract_key, NavigoContractStructure);
  676. int start = positionOffset,
  677. end = positionOffset +
  678. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  679. card->contracts[i - 1].price_amount =
  680. bit_slice_to_dec(bit_representation, start, end) / 100.0;
  681. card->contracts[i - 1].price_amount_available = true;
  682. }
  683. // 13.0. ContractValidityStartDate
  684. contract_key = "ContractValidityStartDate";
  685. if(is_calypso_node_present(
  686. bit_representation, contract_key, NavigoContractStructure)) {
  687. int positionOffset = get_calypso_node_offset(
  688. bit_representation, contract_key, NavigoContractStructure);
  689. int start = positionOffset,
  690. end = positionOffset +
  691. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  692. float decimal_value =
  693. bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
  694. uint64_t start_validity_timestamp = (decimal_value + (float)epoch) + 3600;
  695. datetime_timestamp_to_datetime(
  696. start_validity_timestamp, &card->contracts[i - 1].start_date);
  697. }
  698. // 13.2. ContractValidityEndDate
  699. contract_key = "ContractValidityEndDate";
  700. if(is_calypso_node_present(
  701. bit_representation, contract_key, NavigoContractStructure)) {
  702. int positionOffset = get_calypso_node_offset(
  703. bit_representation, contract_key, NavigoContractStructure);
  704. int start = positionOffset,
  705. end = positionOffset +
  706. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  707. float decimal_value =
  708. bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
  709. uint64_t end_validity_timestamp = (decimal_value + (float)epoch) + 3600;
  710. datetime_timestamp_to_datetime(
  711. end_validity_timestamp, &card->contracts[i - 1].end_date);
  712. card->contracts[i - 1].end_date_available = true;
  713. }
  714. // 13.6. ContractValidityZones
  715. contract_key = "ContractValidityZones";
  716. if(is_calypso_node_present(
  717. bit_representation, contract_key, NavigoContractStructure)) {
  718. int start = get_calypso_node_offset(
  719. bit_representation, contract_key, NavigoContractStructure);
  720. // binary form is 00011111 for zones 5, 4, 3, 2, 1
  721. for(int j = 0; j < 5; j++) {
  722. card->contracts[i - 1].zones[j] =
  723. bit_slice_to_dec(bit_representation, start + 3 + j, start + 3 + j);
  724. }
  725. card->contracts[i - 1].zones_available = true;
  726. }
  727. // 13.7. ContractValidityJourneys -- pas sûr de le mettre lui
  728. // 15.0. ContractValiditySaleDate
  729. contract_key = "ContractValiditySaleDate";
  730. if(is_calypso_node_present(
  731. bit_representation, contract_key, NavigoContractStructure)) {
  732. int positionOffset = get_calypso_node_offset(
  733. bit_representation, contract_key, NavigoContractStructure);
  734. int start = positionOffset,
  735. end = positionOffset +
  736. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  737. float decimal_value =
  738. bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
  739. uint64_t sale_timestamp = (decimal_value + (float)epoch) + 3600;
  740. datetime_timestamp_to_datetime(
  741. sale_timestamp, &card->contracts[i - 1].sale_date);
  742. }
  743. // 15.2. ContractValiditySaleAgent
  744. contract_key = "ContractValiditySaleAgent";
  745. if(is_calypso_node_present(
  746. bit_representation, contract_key, NavigoContractStructure)) {
  747. int positionOffset = get_calypso_node_offset(
  748. bit_representation, contract_key, NavigoContractStructure);
  749. int start = positionOffset,
  750. end = positionOffset +
  751. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  752. card->contracts[i - 1].sale_agent =
  753. bit_slice_to_dec(bit_representation, start, end);
  754. } else {
  755. card->contracts[i - 1].sale_agent = -1;
  756. }
  757. // 15.3. ContractValiditySaleDevice
  758. contract_key = "ContractValiditySaleDevice";
  759. if(is_calypso_node_present(
  760. bit_representation, contract_key, NavigoContractStructure)) {
  761. int positionOffset = get_calypso_node_offset(
  762. bit_representation, contract_key, NavigoContractStructure);
  763. int start = positionOffset,
  764. end = positionOffset +
  765. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  766. card->contracts[i - 1].sale_device =
  767. bit_slice_to_dec(bit_representation, start, end);
  768. }
  769. // 16. ContractStatus -- 0x1 ou 0xff
  770. contract_key = "ContractStatus";
  771. if(is_calypso_node_present(
  772. bit_representation, contract_key, NavigoContractStructure)) {
  773. int positionOffset = get_calypso_node_offset(
  774. bit_representation, contract_key, NavigoContractStructure);
  775. int start = positionOffset,
  776. end = positionOffset +
  777. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  778. card->contracts[i - 1].status =
  779. bit_slice_to_dec(bit_representation, start, end);
  780. }
  781. // 18. ContractAuthenticator
  782. contract_key = "ContractAuthenticator";
  783. if(is_calypso_node_present(
  784. bit_representation, contract_key, NavigoContractStructure)) {
  785. int positionOffset = get_calypso_node_offset(
  786. bit_representation, contract_key, NavigoContractStructure);
  787. int start = positionOffset,
  788. end = positionOffset +
  789. get_calypso_node_size(contract_key, NavigoContractStructure) - 1;
  790. card->contracts[i - 1].authenticator =
  791. bit_slice_to_dec(bit_representation, start, end);
  792. }
  793. }
  794. // Free the calypso structure
  795. free_calypso_structure(NavigoContractStructure);
  796. // Select app for environment
  797. error = select_new_app(0x1, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  798. if(error != 0) {
  799. break;
  800. }
  801. // Check the response after selecting app
  802. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  803. break;
  804. }
  805. // read file 1
  806. error = read_new_file(1, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  807. if(error != 0) {
  808. break;
  809. }
  810. // Check the response after reading the file
  811. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  812. break;
  813. }
  814. char environment_bit_representation[response_length * 8 + 1];
  815. environment_bit_representation[0] = '\0';
  816. for(size_t i = 0; i < response_length; i++) {
  817. char bits[9];
  818. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  819. byte_to_binary(byte, bits);
  820. strlcat(
  821. environment_bit_representation,
  822. bits,
  823. sizeof(environment_bit_representation));
  824. }
  825. FURI_LOG_I(
  826. TAG, "Environment bit_representation: %s", environment_bit_representation);
  827. int start = 0;
  828. int end = 5;
  829. card->environment.app_version =
  830. bit_slice_to_dec(environment_bit_representation, start, end);
  831. start = 13;
  832. end = 16;
  833. card->environment.country_num =
  834. bit_slice_to_dec(environment_bit_representation, start, end) * 100 +
  835. bit_slice_to_dec(environment_bit_representation, start + 4, end + 4) * 10 +
  836. bit_slice_to_dec(environment_bit_representation, start + 8, end + 8);
  837. start = 25;
  838. end = 28;
  839. card->environment.network_num =
  840. bit_slice_to_dec(environment_bit_representation, start, end) * 100 +
  841. bit_slice_to_dec(environment_bit_representation, start + 4, end + 4) * 10 +
  842. bit_slice_to_dec(environment_bit_representation, start + 8, end + 8);
  843. start = 45;
  844. end = 58;
  845. float decimal_value = bit_slice_to_dec(environment_bit_representation, start, end);
  846. uint64_t end_validity_timestamp =
  847. (decimal_value * 24 * 3600) + (float)epoch + 3600;
  848. datetime_timestamp_to_datetime(end_validity_timestamp, &card->environment.end_dt);
  849. start = 95;
  850. end = 98;
  851. card->holder.card_status =
  852. bit_slice_to_dec(environment_bit_representation, start, end);
  853. start = 99;
  854. end = 104;
  855. card->holder.commercial_id =
  856. bit_slice_to_dec(environment_bit_representation, start, end);
  857. // Select app for counters (remaining tickets on Navigo Easy)
  858. error =
  859. select_new_app(0x2A, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  860. if(error != 0) {
  861. break;
  862. }
  863. // Check the response after selecting app
  864. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  865. break;
  866. }
  867. // read file 1
  868. error = read_new_file(1, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  869. if(error != 0) {
  870. break;
  871. }
  872. // Check the response after reading the file
  873. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  874. break;
  875. }
  876. char counter_bit_representation[response_length * 8 + 1];
  877. counter_bit_representation[0] = '\0';
  878. for(size_t i = 0; i < response_length; i++) {
  879. char bits[9];
  880. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  881. byte_to_binary(byte, bits);
  882. strlcat(counter_bit_representation, bits, sizeof(counter_bit_representation));
  883. }
  884. FURI_LOG_I(TAG, "Counter bit_representation: %s", counter_bit_representation);
  885. // Ticket count
  886. start = 2;
  887. end = 5;
  888. card->ticket_count = bit_slice_to_dec(counter_bit_representation, start, end);
  889. // Select app for events
  890. error =
  891. select_new_app(0x10, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  892. if(error != 0) {
  893. break;
  894. }
  895. // Check the response after selecting app
  896. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  897. break;
  898. }
  899. // Load the calypso structure for events
  900. CalypsoApp* NavigoEventStructure = get_navigo_event_structure();
  901. if(!NavigoEventStructure) {
  902. FURI_LOG_E(TAG, "Failed to load Navigo Event structure");
  903. break;
  904. }
  905. // furi_string_cat_printf(parsed_data, "\e#Events :\n");
  906. // Now send the read command for events
  907. for(size_t i = 1; i < 4; i++) {
  908. error =
  909. read_new_file(i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  910. if(error != 0) {
  911. break;
  912. }
  913. // Check the response after reading the file
  914. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  915. break;
  916. }
  917. char event_bit_representation[response_length * 8 + 1];
  918. event_bit_representation[0] = '\0';
  919. for(size_t i = 0; i < response_length; i++) {
  920. char bits[9];
  921. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  922. byte_to_binary(byte, bits);
  923. strlcat(event_bit_representation, bits, sizeof(event_bit_representation));
  924. }
  925. // furi_string_cat_printf(parsed_data, "Event 0%d :\n", i);
  926. /* int count = 0;
  927. int start = 25, end = 52;
  928. char bit_slice[end - start + 2];
  929. strncpy(bit_slice, event_bit_representation + start, end - start + 1);
  930. bit_slice[end - start + 1] = '\0';
  931. int* positions = get_bit_positions(bit_slice, &count);
  932. FURI_LOG_I(TAG, "Positions: ");
  933. for(int i = 0; i < count; i++) {
  934. FURI_LOG_I(TAG, "%d ", positions[i]);
  935. } */
  936. // 2. EventCode
  937. const char* event_key = "EventCode";
  938. if(is_calypso_node_present(
  939. event_bit_representation, event_key, NavigoEventStructure)) {
  940. int positionOffset = get_calypso_node_offset(
  941. event_bit_representation, event_key, NavigoEventStructure);
  942. int start = positionOffset,
  943. end = positionOffset +
  944. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  945. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  946. card->events[i - 1].transport_type = decimal_value >> 4;
  947. card->events[i - 1].transition = decimal_value & 15;
  948. }
  949. // 4. EventServiceProvider
  950. event_key = "EventServiceProvider";
  951. if(is_calypso_node_present(
  952. event_bit_representation, event_key, NavigoEventStructure)) {
  953. int positionOffset = get_calypso_node_offset(
  954. event_bit_representation, event_key, NavigoEventStructure);
  955. start = positionOffset,
  956. end = positionOffset +
  957. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  958. card->events[i - 1].service_provider =
  959. bit_slice_to_dec(event_bit_representation, start, end);
  960. }
  961. // 8. EventLocationId
  962. event_key = "EventLocationId";
  963. if(is_calypso_node_present(
  964. event_bit_representation, event_key, NavigoEventStructure)) {
  965. int positionOffset = get_calypso_node_offset(
  966. event_bit_representation, event_key, NavigoEventStructure);
  967. start = positionOffset,
  968. end = positionOffset +
  969. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  970. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  971. card->events[i - 1].station_group_id = decimal_value >> 9;
  972. card->events[i - 1].station_id = (decimal_value >> 4) & 31;
  973. }
  974. // 9. EventLocationGate
  975. event_key = "EventLocationGate";
  976. if(is_calypso_node_present(
  977. event_bit_representation, event_key, NavigoEventStructure)) {
  978. int positionOffset = get_calypso_node_offset(
  979. event_bit_representation, event_key, NavigoEventStructure);
  980. start = positionOffset,
  981. end = positionOffset +
  982. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  983. card->events[i - 1].location_gate =
  984. bit_slice_to_dec(event_bit_representation, start, end);
  985. card->events[i - 1].location_gate_available = true;
  986. }
  987. // 10. EventDevice
  988. event_key = "EventDevice";
  989. if(is_calypso_node_present(
  990. event_bit_representation, event_key, NavigoEventStructure)) {
  991. int positionOffset = get_calypso_node_offset(
  992. event_bit_representation, event_key, NavigoEventStructure);
  993. start = positionOffset,
  994. end = positionOffset +
  995. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  996. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  997. card->events[i - 1].device = decimal_value >> 8;
  998. card->events[i - 1].door = card->events[i - 1].device / 2 + 1;
  999. card->events[i - 1].side = card->events[i - 1].device % 2;
  1000. card->events[i - 1].device_available = true;
  1001. }
  1002. // 11. EventRouteNumber
  1003. event_key = "EventRouteNumber";
  1004. if(is_calypso_node_present(
  1005. event_bit_representation, event_key, NavigoEventStructure)) {
  1006. int positionOffset = get_calypso_node_offset(
  1007. event_bit_representation, event_key, NavigoEventStructure);
  1008. start = positionOffset,
  1009. end = positionOffset +
  1010. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1011. card->events[i - 1].route_number =
  1012. bit_slice_to_dec(event_bit_representation, start, end);
  1013. card->events[i - 1].route_number_available = true;
  1014. }
  1015. // 13. EventJourneyRun
  1016. event_key = "EventJourneyRun";
  1017. if(is_calypso_node_present(
  1018. event_bit_representation, event_key, NavigoEventStructure)) {
  1019. int positionOffset = get_calypso_node_offset(
  1020. event_bit_representation, event_key, NavigoEventStructure);
  1021. start = positionOffset,
  1022. end = positionOffset +
  1023. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1024. card->events[i - 1].mission =
  1025. bit_slice_to_dec(event_bit_representation, start, end);
  1026. card->events[i - 1].mission_available = true;
  1027. }
  1028. // 14. EventVehicleId
  1029. event_key = "EventVehicleId";
  1030. if(is_calypso_node_present(
  1031. event_bit_representation, event_key, NavigoEventStructure)) {
  1032. int positionOffset = get_calypso_node_offset(
  1033. event_bit_representation, event_key, NavigoEventStructure);
  1034. start = positionOffset,
  1035. end = positionOffset +
  1036. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1037. card->events[i - 1].vehicle_id =
  1038. bit_slice_to_dec(event_bit_representation, start, end);
  1039. card->events[i - 1].vehicle_id_available = true;
  1040. }
  1041. // 25. EventContractPointer
  1042. event_key = "EventContractPointer";
  1043. if(is_calypso_node_present(
  1044. event_bit_representation, event_key, NavigoEventStructure)) {
  1045. int positionOffset = get_calypso_node_offset(
  1046. event_bit_representation, event_key, NavigoEventStructure);
  1047. start = positionOffset,
  1048. end = positionOffset +
  1049. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1050. card->events[i - 1].used_contract =
  1051. bit_slice_to_dec(event_bit_representation, start, end);
  1052. card->events[i - 1].used_contract_available = true;
  1053. }
  1054. // EventDateStamp
  1055. event_key = "EventDateStamp";
  1056. int positionOffset = get_calypso_node_offset(
  1057. event_bit_representation, event_key, NavigoEventStructure);
  1058. start = positionOffset,
  1059. end = positionOffset + get_calypso_node_size(event_key, NavigoEventStructure) -
  1060. 1;
  1061. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1062. uint64_t date_timestamp = (decimal_value * 24 * 3600) + epoch + 3600;
  1063. datetime_timestamp_to_datetime(date_timestamp, &card->events[i - 1].date);
  1064. // EventTimeStamp
  1065. event_key = "EventTimeStamp";
  1066. positionOffset = get_calypso_node_offset(
  1067. event_bit_representation, event_key, NavigoEventStructure);
  1068. start = positionOffset,
  1069. end = positionOffset + get_calypso_node_size(event_key, NavigoEventStructure) -
  1070. 1;
  1071. decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1072. card->events[i - 1].date.hour = (decimal_value * 60) / 3600;
  1073. card->events[i - 1].date.minute = ((decimal_value * 60) % 3600) / 60;
  1074. card->events[i - 1].date.second = ((decimal_value * 60) % 3600) % 60;
  1075. }
  1076. // Free the calypso structure
  1077. free_calypso_structure(NavigoEventStructure);
  1078. UNUSED(TRANSITION_LIST);
  1079. UNUSED(TRANSPORT_LIST);
  1080. UNUSED(SERVICE_PROVIDERS);
  1081. widget_add_text_scroll_element(
  1082. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  1083. NavigoContext* context = malloc(sizeof(NavigoContext));
  1084. context->card = card;
  1085. context->page_id = 0;
  1086. context->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  1087. app->navigo_context = context;
  1088. // Ensure no nested mutexes
  1089. furi_mutex_acquire(context->mutex, FuriWaitForever);
  1090. update_page_info(app, parsed_data);
  1091. furi_mutex_release(context->mutex);
  1092. widget_add_text_scroll_element(
  1093. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  1094. // Ensure no nested mutexes
  1095. furi_mutex_acquire(context->mutex, FuriWaitForever);
  1096. update_widget_elements(app);
  1097. furi_mutex_release(context->mutex);
  1098. furi_string_free(parsed_data);
  1099. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  1100. metroflip_app_blink_stop(app);
  1101. stage = MetroflipPollerEventTypeSuccess;
  1102. next_command = NfcCommandStop;
  1103. } while(false);
  1104. if(stage != MetroflipPollerEventTypeSuccess) {
  1105. next_command = NfcCommandStop;
  1106. }
  1107. }
  1108. }
  1109. bit_buffer_free(tx_buffer);
  1110. bit_buffer_free(rx_buffer);
  1111. return next_command;
  1112. }
  1113. void metroflip_scene_navigo_on_enter(void* context) {
  1114. Metroflip* app = context;
  1115. dolphin_deed(DolphinDeedNfcRead);
  1116. // Setup view
  1117. Popup* popup = app->popup;
  1118. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  1119. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  1120. // Start worker
  1121. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  1122. nfc_scanner_alloc(app->nfc);
  1123. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolIso14443_4b);
  1124. nfc_poller_start(app->poller, metroflip_scene_navigo_poller_callback, app);
  1125. metroflip_app_blink_start(app);
  1126. }
  1127. bool metroflip_scene_navigo_on_event(void* context, SceneManagerEvent event) {
  1128. Metroflip* app = context;
  1129. bool consumed = false;
  1130. if(event.type == SceneManagerEventTypeCustom) {
  1131. if(event.event == MetroflipPollerEventTypeCardDetect) {
  1132. Popup* popup = app->popup;
  1133. popup_set_header(popup, "Scanning..", 68, 30, AlignLeft, AlignTop);
  1134. consumed = true;
  1135. } else if(event.event == MetroflipCustomEventPollerFileNotFound) {
  1136. Popup* popup = app->popup;
  1137. popup_set_header(popup, "Read Error,\n wrong card", 68, 30, AlignLeft, AlignTop);
  1138. consumed = true;
  1139. } else if(event.event == MetroflipCustomEventPollerFail) {
  1140. Popup* popup = app->popup;
  1141. popup_set_header(popup, "Error, try\n again", 68, 30, AlignLeft, AlignTop);
  1142. consumed = true;
  1143. }
  1144. } else if(event.type == SceneManagerEventTypeBack) {
  1145. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  1146. consumed = true;
  1147. }
  1148. return consumed;
  1149. }
  1150. void metroflip_scene_navigo_on_exit(void* context) {
  1151. Metroflip* app = context;
  1152. if(app->poller) {
  1153. nfc_poller_stop(app->poller);
  1154. nfc_poller_free(app->poller);
  1155. }
  1156. metroflip_app_blink_stop(app);
  1157. widget_reset(app->widget);
  1158. // Clear view
  1159. popup_reset(app->popup);
  1160. if(app->navigo_context) {
  1161. NavigoContext* ctx = app->navigo_context;
  1162. free(ctx->card);
  1163. furi_mutex_free(ctx->mutex);
  1164. free(ctx);
  1165. app->navigo_context = NULL;
  1166. }
  1167. }