metroflip_scene_navigo.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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_counts[0], 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_counts[1], 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(0x69, 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 (contract 1)
  886. start = 0;
  887. end = 5;
  888. card->ticket_counts[0] = bit_slice_to_dec(counter_bit_representation, start, end);
  889. // Ticket count (contract 2)
  890. start = 24;
  891. end = 29;
  892. card->ticket_counts[1] = bit_slice_to_dec(counter_bit_representation, start, end);
  893. FURI_LOG_I(TAG, "Ticket count 1: %d", card->ticket_counts[0]);
  894. FURI_LOG_I(TAG, "Ticket count 2: %d", card->ticket_counts[1]);
  895. // Select app for events
  896. error =
  897. select_new_app(0x10, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  898. if(error != 0) {
  899. break;
  900. }
  901. // Check the response after selecting app
  902. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  903. break;
  904. }
  905. // Load the calypso structure for events
  906. CalypsoApp* NavigoEventStructure = get_navigo_event_structure();
  907. if(!NavigoEventStructure) {
  908. FURI_LOG_E(TAG, "Failed to load Navigo Event structure");
  909. break;
  910. }
  911. // furi_string_cat_printf(parsed_data, "\e#Events :\n");
  912. // Now send the read command for events
  913. for(size_t i = 1; i < 4; i++) {
  914. error =
  915. read_new_file(i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  916. if(error != 0) {
  917. break;
  918. }
  919. // Check the response after reading the file
  920. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  921. break;
  922. }
  923. char event_bit_representation[response_length * 8 + 1];
  924. event_bit_representation[0] = '\0';
  925. for(size_t i = 0; i < response_length; i++) {
  926. char bits[9];
  927. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  928. byte_to_binary(byte, bits);
  929. strlcat(event_bit_representation, bits, sizeof(event_bit_representation));
  930. }
  931. // furi_string_cat_printf(parsed_data, "Event 0%d :\n", i);
  932. /* int count = 0;
  933. int start = 25, end = 52;
  934. char bit_slice[end - start + 2];
  935. strncpy(bit_slice, event_bit_representation + start, end - start + 1);
  936. bit_slice[end - start + 1] = '\0';
  937. int* positions = get_bit_positions(bit_slice, &count);
  938. FURI_LOG_I(TAG, "Positions: ");
  939. for(int i = 0; i < count; i++) {
  940. FURI_LOG_I(TAG, "%d ", positions[i]);
  941. } */
  942. // 2. EventCode
  943. const char* event_key = "EventCode";
  944. if(is_calypso_node_present(
  945. event_bit_representation, event_key, NavigoEventStructure)) {
  946. int positionOffset = get_calypso_node_offset(
  947. event_bit_representation, event_key, NavigoEventStructure);
  948. int start = positionOffset,
  949. end = positionOffset +
  950. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  951. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  952. card->events[i - 1].transport_type = decimal_value >> 4;
  953. card->events[i - 1].transition = decimal_value & 15;
  954. }
  955. // 4. EventServiceProvider
  956. event_key = "EventServiceProvider";
  957. if(is_calypso_node_present(
  958. event_bit_representation, event_key, NavigoEventStructure)) {
  959. int positionOffset = get_calypso_node_offset(
  960. event_bit_representation, event_key, NavigoEventStructure);
  961. start = positionOffset,
  962. end = positionOffset +
  963. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  964. card->events[i - 1].service_provider =
  965. bit_slice_to_dec(event_bit_representation, start, end);
  966. }
  967. // 8. EventLocationId
  968. event_key = "EventLocationId";
  969. if(is_calypso_node_present(
  970. event_bit_representation, event_key, NavigoEventStructure)) {
  971. int positionOffset = get_calypso_node_offset(
  972. event_bit_representation, event_key, NavigoEventStructure);
  973. start = positionOffset,
  974. end = positionOffset +
  975. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  976. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  977. card->events[i - 1].station_group_id = decimal_value >> 9;
  978. card->events[i - 1].station_id = (decimal_value >> 4) & 31;
  979. }
  980. // 9. EventLocationGate
  981. event_key = "EventLocationGate";
  982. if(is_calypso_node_present(
  983. event_bit_representation, event_key, NavigoEventStructure)) {
  984. int positionOffset = get_calypso_node_offset(
  985. event_bit_representation, event_key, NavigoEventStructure);
  986. start = positionOffset,
  987. end = positionOffset +
  988. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  989. card->events[i - 1].location_gate =
  990. bit_slice_to_dec(event_bit_representation, start, end);
  991. card->events[i - 1].location_gate_available = true;
  992. }
  993. // 10. EventDevice
  994. event_key = "EventDevice";
  995. if(is_calypso_node_present(
  996. event_bit_representation, event_key, NavigoEventStructure)) {
  997. int positionOffset = get_calypso_node_offset(
  998. event_bit_representation, event_key, NavigoEventStructure);
  999. start = positionOffset,
  1000. end = positionOffset +
  1001. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1002. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1003. card->events[i - 1].device = decimal_value >> 8;
  1004. card->events[i - 1].door = card->events[i - 1].device / 2 + 1;
  1005. card->events[i - 1].side = card->events[i - 1].device % 2;
  1006. card->events[i - 1].device_available = true;
  1007. }
  1008. // 11. EventRouteNumber
  1009. event_key = "EventRouteNumber";
  1010. if(is_calypso_node_present(
  1011. event_bit_representation, event_key, NavigoEventStructure)) {
  1012. int positionOffset = get_calypso_node_offset(
  1013. event_bit_representation, event_key, NavigoEventStructure);
  1014. start = positionOffset,
  1015. end = positionOffset +
  1016. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1017. card->events[i - 1].route_number =
  1018. bit_slice_to_dec(event_bit_representation, start, end);
  1019. card->events[i - 1].route_number_available = true;
  1020. }
  1021. // 13. EventJourneyRun
  1022. event_key = "EventJourneyRun";
  1023. if(is_calypso_node_present(
  1024. event_bit_representation, event_key, NavigoEventStructure)) {
  1025. int positionOffset = get_calypso_node_offset(
  1026. event_bit_representation, event_key, NavigoEventStructure);
  1027. start = positionOffset,
  1028. end = positionOffset +
  1029. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1030. card->events[i - 1].mission =
  1031. bit_slice_to_dec(event_bit_representation, start, end);
  1032. card->events[i - 1].mission_available = true;
  1033. }
  1034. // 14. EventVehicleId
  1035. event_key = "EventVehicleId";
  1036. if(is_calypso_node_present(
  1037. event_bit_representation, event_key, NavigoEventStructure)) {
  1038. int positionOffset = get_calypso_node_offset(
  1039. event_bit_representation, event_key, NavigoEventStructure);
  1040. start = positionOffset,
  1041. end = positionOffset +
  1042. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1043. card->events[i - 1].vehicle_id =
  1044. bit_slice_to_dec(event_bit_representation, start, end);
  1045. card->events[i - 1].vehicle_id_available = true;
  1046. }
  1047. // 25. EventContractPointer
  1048. event_key = "EventContractPointer";
  1049. if(is_calypso_node_present(
  1050. event_bit_representation, event_key, NavigoEventStructure)) {
  1051. int positionOffset = get_calypso_node_offset(
  1052. event_bit_representation, event_key, NavigoEventStructure);
  1053. start = positionOffset,
  1054. end = positionOffset +
  1055. get_calypso_node_size(event_key, NavigoEventStructure) - 1;
  1056. card->events[i - 1].used_contract =
  1057. bit_slice_to_dec(event_bit_representation, start, end);
  1058. card->events[i - 1].used_contract_available = true;
  1059. }
  1060. // EventDateStamp
  1061. event_key = "EventDateStamp";
  1062. int 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. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1068. uint64_t date_timestamp = (decimal_value * 24 * 3600) + epoch + 3600;
  1069. datetime_timestamp_to_datetime(date_timestamp, &card->events[i - 1].date);
  1070. // EventTimeStamp
  1071. event_key = "EventTimeStamp";
  1072. positionOffset = get_calypso_node_offset(
  1073. event_bit_representation, event_key, NavigoEventStructure);
  1074. start = positionOffset,
  1075. end = positionOffset + get_calypso_node_size(event_key, NavigoEventStructure) -
  1076. 1;
  1077. decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1078. card->events[i - 1].date.hour = (decimal_value * 60) / 3600;
  1079. card->events[i - 1].date.minute = ((decimal_value * 60) % 3600) / 60;
  1080. card->events[i - 1].date.second = ((decimal_value * 60) % 3600) % 60;
  1081. }
  1082. // Free the calypso structure
  1083. free_calypso_structure(NavigoEventStructure);
  1084. UNUSED(TRANSITION_LIST);
  1085. UNUSED(TRANSPORT_LIST);
  1086. UNUSED(SERVICE_PROVIDERS);
  1087. widget_add_text_scroll_element(
  1088. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  1089. NavigoContext* context = malloc(sizeof(NavigoContext));
  1090. context->card = card;
  1091. context->page_id = 0;
  1092. context->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  1093. app->navigo_context = context;
  1094. // Ensure no nested mutexes
  1095. furi_mutex_acquire(context->mutex, FuriWaitForever);
  1096. update_page_info(app, parsed_data);
  1097. furi_mutex_release(context->mutex);
  1098. widget_add_text_scroll_element(
  1099. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  1100. // Ensure no nested mutexes
  1101. furi_mutex_acquire(context->mutex, FuriWaitForever);
  1102. update_widget_elements(app);
  1103. furi_mutex_release(context->mutex);
  1104. furi_string_free(parsed_data);
  1105. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  1106. metroflip_app_blink_stop(app);
  1107. stage = MetroflipPollerEventTypeSuccess;
  1108. next_command = NfcCommandStop;
  1109. } while(false);
  1110. if(stage != MetroflipPollerEventTypeSuccess) {
  1111. next_command = NfcCommandStop;
  1112. }
  1113. }
  1114. }
  1115. bit_buffer_free(tx_buffer);
  1116. bit_buffer_free(rx_buffer);
  1117. return next_command;
  1118. }
  1119. void metroflip_scene_navigo_on_enter(void* context) {
  1120. Metroflip* app = context;
  1121. dolphin_deed(DolphinDeedNfcRead);
  1122. // Setup view
  1123. Popup* popup = app->popup;
  1124. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  1125. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  1126. // Start worker
  1127. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  1128. nfc_scanner_alloc(app->nfc);
  1129. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolIso14443_4b);
  1130. nfc_poller_start(app->poller, metroflip_scene_navigo_poller_callback, app);
  1131. metroflip_app_blink_start(app);
  1132. }
  1133. bool metroflip_scene_navigo_on_event(void* context, SceneManagerEvent event) {
  1134. Metroflip* app = context;
  1135. bool consumed = false;
  1136. if(event.type == SceneManagerEventTypeCustom) {
  1137. if(event.event == MetroflipPollerEventTypeCardDetect) {
  1138. Popup* popup = app->popup;
  1139. popup_set_header(popup, "Scanning..", 68, 30, AlignLeft, AlignTop);
  1140. consumed = true;
  1141. } else if(event.event == MetroflipCustomEventPollerFileNotFound) {
  1142. Popup* popup = app->popup;
  1143. popup_set_header(popup, "Read Error,\n wrong card", 68, 30, AlignLeft, AlignTop);
  1144. consumed = true;
  1145. } else if(event.event == MetroflipCustomEventPollerFail) {
  1146. Popup* popup = app->popup;
  1147. popup_set_header(popup, "Error, try\n again", 68, 30, AlignLeft, AlignTop);
  1148. consumed = true;
  1149. }
  1150. } else if(event.type == SceneManagerEventTypeBack) {
  1151. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  1152. consumed = true;
  1153. }
  1154. return consumed;
  1155. }
  1156. void metroflip_scene_navigo_on_exit(void* context) {
  1157. Metroflip* app = context;
  1158. if(app->poller) {
  1159. nfc_poller_stop(app->poller);
  1160. nfc_poller_free(app->poller);
  1161. }
  1162. metroflip_app_blink_stop(app);
  1163. widget_reset(app->widget);
  1164. // Clear view
  1165. popup_reset(app->popup);
  1166. if(app->navigo_context) {
  1167. NavigoContext* ctx = app->navigo_context;
  1168. free(ctx->card);
  1169. furi_mutex_free(ctx->mutex);
  1170. free(ctx);
  1171. app->navigo_context = NULL;
  1172. }
  1173. }