metroflip_scene_navigo.c 62 KB

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