metroflip_scene_calypso.c 83 KB

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