metroflip_scene_calypso.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  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. card->navigo->events[i - 1].station_sub_id = decimal_value & 15;
  836. }
  837. // 9. EventLocationGate
  838. event_key = "EventLocationGate";
  839. if(is_calypso_node_present(
  840. event_bit_representation, event_key, IntercodeEventStructure)) {
  841. int positionOffset = get_calypso_node_offset(
  842. event_bit_representation, event_key, IntercodeEventStructure);
  843. start = positionOffset,
  844. end = positionOffset +
  845. get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
  846. card->navigo->events[i - 1].location_gate =
  847. bit_slice_to_dec(event_bit_representation, start, end);
  848. card->navigo->events[i - 1].location_gate_available = true;
  849. }
  850. // 10. EventDevice
  851. event_key = "EventDevice";
  852. if(is_calypso_node_present(
  853. event_bit_representation, event_key, IntercodeEventStructure)) {
  854. int positionOffset = get_calypso_node_offset(
  855. event_bit_representation, event_key, IntercodeEventStructure);
  856. start = positionOffset,
  857. end = positionOffset +
  858. get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
  859. int decimal_value =
  860. bit_slice_to_dec(event_bit_representation, start, end);
  861. card->navigo->events[i - 1].device = decimal_value;
  862. int bus_device = decimal_value >> 8;
  863. card->navigo->events[i - 1].door = bus_device / 2 + 1;
  864. card->navigo->events[i - 1].side = bus_device % 2;
  865. card->navigo->events[i - 1].device_available = true;
  866. }
  867. // 11. EventRouteNumber
  868. event_key = "EventRouteNumber";
  869. if(is_calypso_node_present(
  870. event_bit_representation, event_key, IntercodeEventStructure)) {
  871. int positionOffset = get_calypso_node_offset(
  872. event_bit_representation, event_key, IntercodeEventStructure);
  873. start = positionOffset,
  874. end = positionOffset +
  875. get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
  876. card->navigo->events[i - 1].route_number =
  877. bit_slice_to_dec(event_bit_representation, start, end);
  878. card->navigo->events[i - 1].route_number_available = true;
  879. }
  880. // 13. EventJourneyRun
  881. event_key = "EventJourneyRun";
  882. if(is_calypso_node_present(
  883. event_bit_representation, event_key, IntercodeEventStructure)) {
  884. int positionOffset = get_calypso_node_offset(
  885. event_bit_representation, event_key, IntercodeEventStructure);
  886. start = positionOffset,
  887. end = positionOffset +
  888. get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
  889. card->navigo->events[i - 1].mission =
  890. bit_slice_to_dec(event_bit_representation, start, end);
  891. card->navigo->events[i - 1].mission_available = true;
  892. }
  893. // 14. EventVehicleId
  894. event_key = "EventVehicleId";
  895. if(is_calypso_node_present(
  896. event_bit_representation, event_key, IntercodeEventStructure)) {
  897. int positionOffset = get_calypso_node_offset(
  898. event_bit_representation, event_key, IntercodeEventStructure);
  899. start = positionOffset,
  900. end = positionOffset +
  901. get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
  902. card->navigo->events[i - 1].vehicle_id =
  903. bit_slice_to_dec(event_bit_representation, start, end);
  904. card->navigo->events[i - 1].vehicle_id_available = true;
  905. }
  906. // 25. EventContractPointer
  907. event_key = "EventContractPointer";
  908. if(is_calypso_node_present(
  909. event_bit_representation, event_key, IntercodeEventStructure)) {
  910. int positionOffset = get_calypso_node_offset(
  911. event_bit_representation, event_key, IntercodeEventStructure);
  912. start = positionOffset,
  913. end = positionOffset +
  914. get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
  915. card->navigo->events[i - 1].used_contract =
  916. bit_slice_to_dec(event_bit_representation, start, end);
  917. card->navigo->events[i - 1].used_contract_available = true;
  918. if(card->navigo->events[i - 1].used_contract > 0) {
  919. card->events_count++;
  920. }
  921. }
  922. // EventDateStamp
  923. event_key = "EventDateStamp";
  924. int positionOffset = get_calypso_node_offset(
  925. event_bit_representation, event_key, IntercodeEventStructure);
  926. start = positionOffset,
  927. end = positionOffset +
  928. get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
  929. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  930. uint64_t date_timestamp = (decimal_value * 24 * 3600) + epoch + 3600;
  931. datetime_timestamp_to_datetime(
  932. date_timestamp, &card->navigo->events[i - 1].date);
  933. // EventTimeStamp
  934. event_key = "EventTimeStamp";
  935. positionOffset = get_calypso_node_offset(
  936. event_bit_representation, event_key, IntercodeEventStructure);
  937. start = positionOffset,
  938. end = positionOffset +
  939. get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
  940. decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  941. card->navigo->events[i - 1].date.hour = (decimal_value * 60) / 3600;
  942. card->navigo->events[i - 1].date.minute =
  943. ((decimal_value * 60) % 3600) / 60;
  944. card->navigo->events[i - 1].date.second =
  945. ((decimal_value * 60) % 3600) % 60;
  946. }
  947. // Select app for special events
  948. error = select_new_app(
  949. 0x20, 0x40, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  950. if(error != 0) {
  951. break;
  952. }
  953. // Check the response after selecting app
  954. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  955. break;
  956. }
  957. // Now send the read command for special events
  958. for(size_t i = 1; i < 4; i++) {
  959. error = read_new_file(
  960. i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  961. if(error != 0) {
  962. break;
  963. }
  964. // Check the response after reading the file
  965. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  966. break;
  967. }
  968. char event_bit_representation[response_length * 8 + 1];
  969. event_bit_representation[0] = '\0';
  970. for(size_t i = 0; i < response_length; i++) {
  971. char bits[9];
  972. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  973. byte_to_binary(byte, bits);
  974. strlcat(
  975. event_bit_representation, bits, sizeof(event_bit_representation));
  976. }
  977. if(bit_slice_to_dec(
  978. event_bit_representation,
  979. 0,
  980. IntercodeEventStructure->container->elements[0].bitmap->size - 1) ==
  981. 0) {
  982. break;
  983. } else {
  984. card->special_events_count++;
  985. }
  986. // 2. EventCode
  987. const char* event_key = "EventCode";
  988. if(is_calypso_node_present(
  989. event_bit_representation, event_key, IntercodeEventStructure)) {
  990. int positionOffset = get_calypso_node_offset(
  991. event_bit_representation, event_key, IntercodeEventStructure);
  992. int start = positionOffset,
  993. end = positionOffset +
  994. get_calypso_node_size(event_key, IntercodeEventStructure) -
  995. 1;
  996. int decimal_value =
  997. bit_slice_to_dec(event_bit_representation, start, end);
  998. card->navigo->special_events[i - 1].transport_type = decimal_value >>
  999. 4;
  1000. card->navigo->special_events[i - 1].transition = decimal_value & 15;
  1001. }
  1002. // 3. EventResult
  1003. event_key = "EventResult";
  1004. if(is_calypso_node_present(
  1005. event_bit_representation, event_key, IntercodeEventStructure)) {
  1006. int positionOffset = get_calypso_node_offset(
  1007. event_bit_representation, event_key, IntercodeEventStructure);
  1008. int start = positionOffset,
  1009. end = positionOffset +
  1010. get_calypso_node_size(event_key, IntercodeEventStructure) -
  1011. 1;
  1012. card->navigo->special_events[i - 1].result =
  1013. bit_slice_to_dec(event_bit_representation, start, end);
  1014. }
  1015. // 4. EventServiceProvider
  1016. event_key = "EventServiceProvider";
  1017. if(is_calypso_node_present(
  1018. event_bit_representation, event_key, IntercodeEventStructure)) {
  1019. int positionOffset = get_calypso_node_offset(
  1020. event_bit_representation, event_key, IntercodeEventStructure);
  1021. int start = positionOffset,
  1022. end = positionOffset +
  1023. get_calypso_node_size(event_key, IntercodeEventStructure) -
  1024. 1;
  1025. card->navigo->special_events[i - 1].service_provider =
  1026. bit_slice_to_dec(event_bit_representation, start, end);
  1027. }
  1028. // 8. EventLocationId
  1029. event_key = "EventLocationId";
  1030. if(is_calypso_node_present(
  1031. event_bit_representation, event_key, IntercodeEventStructure)) {
  1032. int positionOffset = get_calypso_node_offset(
  1033. event_bit_representation, event_key, IntercodeEventStructure);
  1034. int start = positionOffset,
  1035. end = positionOffset +
  1036. get_calypso_node_size(event_key, IntercodeEventStructure) -
  1037. 1;
  1038. int decimal_value =
  1039. bit_slice_to_dec(event_bit_representation, start, end);
  1040. card->navigo->special_events[i - 1].station_group_id = decimal_value >>
  1041. 9;
  1042. card->navigo->special_events[i - 1].station_id = (decimal_value >> 4) &
  1043. 31;
  1044. card->navigo->special_events[i - 1].station_sub_id = decimal_value &
  1045. 15;
  1046. }
  1047. // 10. EventDevice
  1048. event_key = "EventDevice";
  1049. if(is_calypso_node_present(
  1050. event_bit_representation, event_key, IntercodeEventStructure)) {
  1051. int positionOffset = get_calypso_node_offset(
  1052. event_bit_representation, event_key, IntercodeEventStructure);
  1053. int start = positionOffset,
  1054. end = positionOffset +
  1055. get_calypso_node_size(event_key, IntercodeEventStructure) -
  1056. 1;
  1057. int decimal_value =
  1058. bit_slice_to_dec(event_bit_representation, start, end);
  1059. card->navigo->special_events[i - 1].device = decimal_value;
  1060. }
  1061. // 11. EventRouteNumber
  1062. event_key = "EventRouteNumber";
  1063. if(is_calypso_node_present(
  1064. event_bit_representation, event_key, IntercodeEventStructure)) {
  1065. int positionOffset = get_calypso_node_offset(
  1066. event_bit_representation, event_key, IntercodeEventStructure);
  1067. int start = positionOffset,
  1068. end = positionOffset +
  1069. get_calypso_node_size(event_key, IntercodeEventStructure) -
  1070. 1;
  1071. card->navigo->special_events[i - 1].route_number =
  1072. bit_slice_to_dec(event_bit_representation, start, end);
  1073. card->navigo->special_events[i - 1].route_number_available = true;
  1074. }
  1075. // EventDateStamp
  1076. event_key = "EventDateStamp";
  1077. int positionOffset = get_calypso_node_offset(
  1078. event_bit_representation, event_key, IntercodeEventStructure);
  1079. int start = positionOffset,
  1080. end = positionOffset +
  1081. get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
  1082. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1083. uint64_t date_timestamp = (decimal_value * 24 * 3600) + epoch + 3600;
  1084. datetime_timestamp_to_datetime(
  1085. date_timestamp, &card->navigo->special_events[i - 1].date);
  1086. // EventTimeStamp
  1087. event_key = "EventTimeStamp";
  1088. positionOffset = get_calypso_node_offset(
  1089. event_bit_representation, event_key, IntercodeEventStructure);
  1090. start = positionOffset,
  1091. end = positionOffset +
  1092. get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
  1093. decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1094. card->navigo->special_events[i - 1].date.hour =
  1095. (decimal_value * 60) / 3600;
  1096. card->navigo->special_events[i - 1].date.minute =
  1097. ((decimal_value * 60) % 3600) / 60;
  1098. card->navigo->special_events[i - 1].date.second =
  1099. ((decimal_value * 60) % 3600) % 60;
  1100. }
  1101. // Free the calypso structure
  1102. free_calypso_structure(IntercodeEventStructure);
  1103. break;
  1104. }
  1105. case CALYPSO_CARD_OPUS: {
  1106. card->opus = malloc(sizeof(OpusCardData));
  1107. card->opus->environment.country_num = country_num;
  1108. card->opus->environment.network_num = network_num;
  1109. CalypsoApp* OpusEnvHolderStructure = get_opus_env_holder_structure();
  1110. // EnvApplicationVersionNumber
  1111. const char* env_key = "EnvApplicationVersionNumber";
  1112. int positionOffset = get_calypso_node_offset(
  1113. environment_bit_representation, env_key, OpusEnvHolderStructure);
  1114. int start = positionOffset,
  1115. end = positionOffset +
  1116. get_calypso_node_size(env_key, OpusEnvHolderStructure) - 1;
  1117. card->opus->environment.app_version =
  1118. bit_slice_to_dec(environment_bit_representation, start, end);
  1119. // EnvApplicationValidityEndDate
  1120. env_key = "EnvApplicationValidityEndDate";
  1121. positionOffset = get_calypso_node_offset(
  1122. environment_bit_representation, env_key, OpusEnvHolderStructure);
  1123. start = positionOffset,
  1124. end = positionOffset + get_calypso_node_size(env_key, OpusEnvHolderStructure) -
  1125. 1;
  1126. float decimal_value =
  1127. bit_slice_to_dec(environment_bit_representation, start, end);
  1128. uint64_t end_validity_timestamp =
  1129. (decimal_value * 24 * 3600) + (float)epoch + 3600;
  1130. datetime_timestamp_to_datetime(
  1131. end_validity_timestamp, &card->opus->environment.end_dt);
  1132. // HolderDataCardStatus
  1133. env_key = "HolderDataCardStatus";
  1134. positionOffset = get_calypso_node_offset(
  1135. environment_bit_representation, env_key, OpusEnvHolderStructure);
  1136. start = positionOffset,
  1137. end = positionOffset + get_calypso_node_size(env_key, OpusEnvHolderStructure) -
  1138. 1;
  1139. card->opus->holder.card_status =
  1140. bit_slice_to_dec(environment_bit_representation, start, end);
  1141. // HolderDataCommercialID
  1142. env_key = "HolderDataCommercialID";
  1143. positionOffset = get_calypso_node_offset(
  1144. environment_bit_representation, env_key, OpusEnvHolderStructure);
  1145. start = positionOffset,
  1146. end = positionOffset + get_calypso_node_size(env_key, OpusEnvHolderStructure) -
  1147. 1;
  1148. card->opus->holder.commercial_id =
  1149. bit_slice_to_dec(environment_bit_representation, start, end);
  1150. // Free the calypso structure
  1151. free_calypso_structure(OpusEnvHolderStructure);
  1152. // Select app for contracts
  1153. error = select_new_app(
  1154. 0x20, 0x20, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  1155. if(error != 0) {
  1156. FURI_LOG_E(TAG, "Failed to select app for contracts");
  1157. break;
  1158. }
  1159. // Check the response after selecting app
  1160. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  1161. FURI_LOG_E(
  1162. TAG, "Failed to check response after selecting app for contracts");
  1163. break;
  1164. }
  1165. // Prepare calypso structure
  1166. CalypsoApp* OpusContractStructure = get_opus_contract_structure();
  1167. if(!OpusContractStructure) {
  1168. FURI_LOG_E(TAG, "Failed to load Opus Contract structure");
  1169. break;
  1170. }
  1171. // Now send the read command for contracts
  1172. for(size_t i = 1; i < 5; i++) {
  1173. error = read_new_file(
  1174. i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  1175. if(error != 0) {
  1176. FURI_LOG_E(TAG, "Failed to read contract %d", i);
  1177. break;
  1178. }
  1179. // Check the response after reading the file
  1180. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  1181. FURI_LOG_E(
  1182. TAG, "Failed to check response after reading contract %d", i);
  1183. break;
  1184. }
  1185. char bit_representation[response_length * 8 + 1];
  1186. bit_representation[0] = '\0';
  1187. for(size_t i = 0; i < response_length; i++) {
  1188. char bits[9];
  1189. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  1190. byte_to_binary(byte, bits);
  1191. strlcat(bit_representation, bits, sizeof(bit_representation));
  1192. }
  1193. bit_representation[response_length * 8] = '\0';
  1194. if(bit_slice_to_dec(
  1195. bit_representation,
  1196. 0,
  1197. OpusContractStructure->container->elements[1].bitmap->size - 1) ==
  1198. 0) {
  1199. break;
  1200. }
  1201. card->opus->contracts[i - 1].present = 1;
  1202. card->contracts_count++;
  1203. // ContractProvider
  1204. const char* contract_key = "ContractProvider";
  1205. if(is_calypso_node_present(
  1206. bit_representation, contract_key, OpusContractStructure)) {
  1207. int positionOffset = get_calypso_node_offset(
  1208. bit_representation, contract_key, OpusContractStructure);
  1209. int start = positionOffset,
  1210. end = positionOffset +
  1211. get_calypso_node_size(contract_key, OpusContractStructure) -
  1212. 1;
  1213. card->opus->contracts[i - 1].provider =
  1214. bit_slice_to_dec(bit_representation, start, end);
  1215. }
  1216. // ContractTariff
  1217. contract_key = "ContractTariff";
  1218. if(is_calypso_node_present(
  1219. bit_representation, contract_key, OpusContractStructure)) {
  1220. int positionOffset = get_calypso_node_offset(
  1221. bit_representation, contract_key, OpusContractStructure);
  1222. int start = positionOffset,
  1223. end = positionOffset +
  1224. get_calypso_node_size(contract_key, OpusContractStructure) -
  1225. 1;
  1226. card->opus->contracts[i - 1].tariff =
  1227. bit_slice_to_dec(bit_representation, start, end);
  1228. }
  1229. // ContractStartDate
  1230. contract_key = "ContractStartDate";
  1231. if(is_calypso_node_present(
  1232. bit_representation, contract_key, OpusContractStructure)) {
  1233. int positionOffset = get_calypso_node_offset(
  1234. bit_representation, contract_key, OpusContractStructure);
  1235. int start = positionOffset,
  1236. end = positionOffset +
  1237. get_calypso_node_size(contract_key, OpusContractStructure) -
  1238. 1;
  1239. float decimal_value =
  1240. bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
  1241. uint64_t start_validity_timestamp =
  1242. (decimal_value + (float)epoch) + 3600;
  1243. datetime_timestamp_to_datetime(
  1244. start_validity_timestamp,
  1245. &card->opus->contracts[i - 1].start_date);
  1246. }
  1247. // ContractEndDate
  1248. contract_key = "ContractEndDate";
  1249. if(is_calypso_node_present(
  1250. bit_representation, contract_key, OpusContractStructure)) {
  1251. int positionOffset = get_calypso_node_offset(
  1252. bit_representation, contract_key, OpusContractStructure);
  1253. int start = positionOffset,
  1254. end = positionOffset +
  1255. get_calypso_node_size(contract_key, OpusContractStructure) -
  1256. 1;
  1257. float decimal_value =
  1258. bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
  1259. uint64_t end_validity_timestamp =
  1260. (decimal_value + (float)epoch) + 3600;
  1261. datetime_timestamp_to_datetime(
  1262. end_validity_timestamp, &card->opus->contracts[i - 1].end_date);
  1263. }
  1264. // ContractStatus
  1265. contract_key = "ContractStatus";
  1266. if(is_calypso_node_present(
  1267. bit_representation, contract_key, OpusContractStructure)) {
  1268. int positionOffset = get_calypso_node_offset(
  1269. bit_representation, contract_key, OpusContractStructure);
  1270. int start = positionOffset,
  1271. end = positionOffset +
  1272. get_calypso_node_size(contract_key, OpusContractStructure) -
  1273. 1;
  1274. card->opus->contracts[i - 1].status =
  1275. bit_slice_to_dec(bit_representation, start, end);
  1276. }
  1277. // ContractSaleDate + ContractSaleTime
  1278. contract_key = "ContractSaleDate";
  1279. int positionOffset = get_calypso_node_offset(
  1280. bit_representation, contract_key, OpusContractStructure);
  1281. int start = positionOffset,
  1282. end = positionOffset +
  1283. get_calypso_node_size(contract_key, OpusContractStructure) - 1;
  1284. uint64_t sale_date_timestamp =
  1285. (bit_slice_to_dec(bit_representation, start, end) + (float)epoch) +
  1286. 3600;
  1287. datetime_timestamp_to_datetime(
  1288. sale_date_timestamp, &card->opus->contracts[i - 1].sale_date);
  1289. contract_key = "ContractSaleTime";
  1290. positionOffset = get_calypso_node_offset(
  1291. bit_representation, contract_key, OpusContractStructure);
  1292. start = positionOffset,
  1293. end = positionOffset +
  1294. get_calypso_node_size(contract_key, OpusContractStructure) - 1;
  1295. int decimal_value = bit_slice_to_dec(bit_representation, start, end);
  1296. card->opus->contracts[i - 1].sale_date.hour = (decimal_value * 60) / 3600;
  1297. card->opus->contracts[i - 1].sale_date.minute =
  1298. ((decimal_value * 60) % 3600) / 60;
  1299. card->opus->contracts[i - 1].sale_date.second =
  1300. ((decimal_value * 60) % 3600) % 60;
  1301. }
  1302. // Free the calypso structure
  1303. free_calypso_structure(OpusContractStructure);
  1304. // Select app for events
  1305. error = select_new_app(
  1306. 0x20, 0x10, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  1307. if(error != 0) {
  1308. break;
  1309. }
  1310. // Check the response after selecting app
  1311. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  1312. break;
  1313. }
  1314. // Load the calypso structure for events
  1315. CalypsoApp* OpusEventStructure = get_opus_event_structure();
  1316. if(!OpusEventStructure) {
  1317. FURI_LOG_E(TAG, "Failed to load Opus Event structure");
  1318. break;
  1319. }
  1320. // Now send the read command for events
  1321. for(size_t i = 1; i < 4; i++) {
  1322. error = read_new_file(
  1323. i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
  1324. if(error != 0) {
  1325. break;
  1326. }
  1327. // Check the response after reading the file
  1328. if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
  1329. break;
  1330. }
  1331. char event_bit_representation[response_length * 8 + 1];
  1332. event_bit_representation[0] = '\0';
  1333. for(size_t i = 0; i < response_length; i++) {
  1334. char bits[9];
  1335. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  1336. byte_to_binary(byte, bits);
  1337. strlcat(
  1338. event_bit_representation, bits, sizeof(event_bit_representation));
  1339. }
  1340. // EventServiceProvider
  1341. const char* event_key = "EventServiceProvider";
  1342. if(is_calypso_node_present(
  1343. event_bit_representation, event_key, OpusEventStructure)) {
  1344. int positionOffset = get_calypso_node_offset(
  1345. event_bit_representation, event_key, OpusEventStructure);
  1346. int start = positionOffset,
  1347. end = positionOffset +
  1348. get_calypso_node_size(event_key, OpusEventStructure) - 1;
  1349. card->opus->events[i - 1].service_provider =
  1350. bit_slice_to_dec(event_bit_representation, start, end);
  1351. }
  1352. // EventRouteNumber
  1353. event_key = "EventRouteNumber";
  1354. if(is_calypso_node_present(
  1355. event_bit_representation, event_key, OpusEventStructure)) {
  1356. int positionOffset = get_calypso_node_offset(
  1357. event_bit_representation, event_key, OpusEventStructure);
  1358. int start = positionOffset,
  1359. end = positionOffset +
  1360. get_calypso_node_size(event_key, OpusEventStructure) - 1;
  1361. card->opus->events[i - 1].route_number =
  1362. bit_slice_to_dec(event_bit_representation, start, end);
  1363. card->opus->events[i - 1].route_number_available = true;
  1364. }
  1365. // EventContractPointer
  1366. event_key = "EventContractPointer";
  1367. if(is_calypso_node_present(
  1368. event_bit_representation, event_key, OpusEventStructure)) {
  1369. int positionOffset = get_calypso_node_offset(
  1370. event_bit_representation, event_key, OpusEventStructure);
  1371. int start = positionOffset,
  1372. end = positionOffset +
  1373. get_calypso_node_size(event_key, OpusEventStructure) - 1;
  1374. card->opus->events[i - 1].used_contract =
  1375. bit_slice_to_dec(event_bit_representation, start, end);
  1376. card->opus->events[i - 1].used_contract_available = true;
  1377. if(card->opus->events[i - 1].used_contract > 0) {
  1378. card->events_count++;
  1379. }
  1380. }
  1381. // EventDateStamp
  1382. event_key = "EventDateStamp";
  1383. int positionOffset = get_calypso_node_offset(
  1384. event_bit_representation, event_key, OpusEventStructure);
  1385. int start = positionOffset,
  1386. end = positionOffset +
  1387. get_calypso_node_size(event_key, OpusEventStructure) - 1;
  1388. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1389. uint64_t date_timestamp = (decimal_value * 24 * 3600) + epoch + 3600;
  1390. datetime_timestamp_to_datetime(
  1391. date_timestamp, &card->opus->events[i - 1].date);
  1392. // EventTimeStamp
  1393. event_key = "EventTimeStamp";
  1394. positionOffset = get_calypso_node_offset(
  1395. event_bit_representation, event_key, OpusEventStructure);
  1396. start = positionOffset,
  1397. end = positionOffset +
  1398. get_calypso_node_size(event_key, OpusEventStructure) - 1;
  1399. decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1400. card->opus->events[i - 1].date.hour = (decimal_value * 60) / 3600;
  1401. card->opus->events[i - 1].date.minute = ((decimal_value * 60) % 3600) / 60;
  1402. card->opus->events[i - 1].date.second = ((decimal_value * 60) % 3600) % 60;
  1403. }
  1404. // Free the calypso structure
  1405. free_calypso_structure(OpusEventStructure);
  1406. break;
  1407. }
  1408. case CALYPSO_CARD_UNKNOWN: {
  1409. start = 3;
  1410. end = 6;
  1411. country_num =
  1412. bit_slice_to_dec(environment_bit_representation, start, end) * 100 +
  1413. bit_slice_to_dec(environment_bit_representation, start + 4, end + 4) * 10 +
  1414. bit_slice_to_dec(environment_bit_representation, start + 8, end + 8);
  1415. start = 15;
  1416. end = 18;
  1417. network_num =
  1418. bit_slice_to_dec(environment_bit_representation, start, end) * 100 +
  1419. bit_slice_to_dec(environment_bit_representation, start + 4, end + 4) * 10 +
  1420. bit_slice_to_dec(environment_bit_representation, start + 8, end + 8);
  1421. card->card_type = guess_card_type(country_num, network_num);
  1422. if(card->card_type == CALYPSO_CARD_RAVKAV) {
  1423. }
  1424. break;
  1425. }
  1426. default:
  1427. break;
  1428. }
  1429. widget_add_text_scroll_element(
  1430. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  1431. CalypsoContext* context = malloc(sizeof(CalypsoContext));
  1432. context->card = card;
  1433. context->page_id = 0;
  1434. context->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  1435. app->calypso_context = context;
  1436. // Ensure no nested mutexes
  1437. furi_mutex_acquire(context->mutex, FuriWaitForever);
  1438. update_page_info(app, parsed_data);
  1439. furi_mutex_release(context->mutex);
  1440. widget_add_text_scroll_element(
  1441. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  1442. // Ensure no nested mutexes
  1443. furi_mutex_acquire(context->mutex, FuriWaitForever);
  1444. update_widget_elements(app);
  1445. furi_mutex_release(context->mutex);
  1446. furi_string_free(parsed_data);
  1447. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  1448. metroflip_app_blink_stop(app);
  1449. stage = MetroflipPollerEventTypeSuccess;
  1450. next_command = NfcCommandStop;
  1451. } while(false);
  1452. if(stage != MetroflipPollerEventTypeSuccess) {
  1453. next_command = NfcCommandStop;
  1454. }
  1455. }
  1456. }
  1457. bit_buffer_free(tx_buffer);
  1458. bit_buffer_free(rx_buffer);
  1459. return next_command;
  1460. }
  1461. void metroflip_scene_navigo_on_enter(void* context) {
  1462. Metroflip* app = context;
  1463. dolphin_deed(DolphinDeedNfcRead);
  1464. // Setup view
  1465. Popup* popup = app->popup;
  1466. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  1467. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  1468. // Start worker
  1469. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  1470. nfc_scanner_alloc(app->nfc);
  1471. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolIso14443_4b);
  1472. nfc_poller_start(app->poller, metroflip_scene_navigo_poller_callback, app);
  1473. metroflip_app_blink_start(app);
  1474. }
  1475. bool metroflip_scene_navigo_on_event(void* context, SceneManagerEvent event) {
  1476. Metroflip* app = context;
  1477. bool consumed = false;
  1478. if(event.type == SceneManagerEventTypeCustom) {
  1479. if(event.event == MetroflipPollerEventTypeCardDetect) {
  1480. Popup* popup = app->popup;
  1481. popup_set_header(popup, "Scanning..", 68, 30, AlignLeft, AlignTop);
  1482. consumed = true;
  1483. } else if(event.event == MetroflipCustomEventPollerFileNotFound) {
  1484. Popup* popup = app->popup;
  1485. popup_set_header(popup, "Read Error,\n wrong card", 68, 30, AlignLeft, AlignTop);
  1486. consumed = true;
  1487. } else if(event.event == MetroflipCustomEventPollerFail) {
  1488. Popup* popup = app->popup;
  1489. popup_set_header(popup, "Error, try\n again", 68, 30, AlignLeft, AlignTop);
  1490. consumed = true;
  1491. }
  1492. } else if(event.type == SceneManagerEventTypeBack) {
  1493. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  1494. consumed = true;
  1495. }
  1496. return consumed;
  1497. }
  1498. void metroflip_scene_navigo_on_exit(void* context) {
  1499. Metroflip* app = context;
  1500. if(app->poller) {
  1501. nfc_poller_stop(app->poller);
  1502. nfc_poller_free(app->poller);
  1503. }
  1504. metroflip_app_blink_stop(app);
  1505. widget_reset(app->widget);
  1506. // Clear view
  1507. popup_reset(app->popup);
  1508. if(app->calypso_context) {
  1509. CalypsoContext* ctx = app->calypso_context;
  1510. free(ctx->card->navigo);
  1511. free(ctx->card->opus);
  1512. free(ctx->card);
  1513. furi_mutex_free(ctx->mutex);
  1514. free(ctx);
  1515. app->calypso_context = NULL;
  1516. }
  1517. }