metroflip_scene_navigo.c 55 KB

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