metroflip_scene_calypso.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  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. // EventDate + EventTime
  1379. event_key = "EventDate";
  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. uint64_t date_timestamp =
  1386. (bit_slice_to_dec(event_bit_representation, start, end) +
  1387. (float)epoch) +
  1388. 3600;
  1389. datetime_timestamp_to_datetime(
  1390. date_timestamp, &card->opus->events[i - 1].date);
  1391. event_key = "EventTime";
  1392. positionOffset = get_calypso_node_offset(
  1393. event_bit_representation, event_key, OpusEventStructure);
  1394. start = positionOffset,
  1395. end = positionOffset +
  1396. get_calypso_node_size(event_key, OpusEventStructure) - 1;
  1397. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  1398. card->opus->events[i - 1].date.hour = (decimal_value * 60) / 3600;
  1399. card->opus->events[i - 1].date.minute = ((decimal_value * 60) % 3600) / 60;
  1400. card->opus->events[i - 1].date.second = ((decimal_value * 60) % 3600) % 60;
  1401. }
  1402. // Free the calypso structure
  1403. free_calypso_structure(OpusEventStructure);
  1404. break;
  1405. }
  1406. case CALYPSO_CARD_UNKNOWN: {
  1407. start = 3;
  1408. end = 6;
  1409. country_num =
  1410. bit_slice_to_dec(environment_bit_representation, start, end) * 100 +
  1411. bit_slice_to_dec(environment_bit_representation, start + 4, end + 4) * 10 +
  1412. bit_slice_to_dec(environment_bit_representation, start + 8, end + 8);
  1413. start = 15;
  1414. end = 18;
  1415. network_num =
  1416. bit_slice_to_dec(environment_bit_representation, start, end) * 100 +
  1417. bit_slice_to_dec(environment_bit_representation, start + 4, end + 4) * 10 +
  1418. bit_slice_to_dec(environment_bit_representation, start + 8, end + 8);
  1419. card->card_type = guess_card_type(country_num, network_num);
  1420. if(card->card_type == CALYPSO_CARD_RAVKAV) {
  1421. }
  1422. break;
  1423. }
  1424. default:
  1425. break;
  1426. }
  1427. widget_add_text_scroll_element(
  1428. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  1429. CalypsoContext* context = malloc(sizeof(CalypsoContext));
  1430. context->card = card;
  1431. context->page_id = 0;
  1432. context->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  1433. app->calypso_context = context;
  1434. // Ensure no nested mutexes
  1435. furi_mutex_acquire(context->mutex, FuriWaitForever);
  1436. update_page_info(app, parsed_data);
  1437. furi_mutex_release(context->mutex);
  1438. widget_add_text_scroll_element(
  1439. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  1440. // Ensure no nested mutexes
  1441. furi_mutex_acquire(context->mutex, FuriWaitForever);
  1442. update_widget_elements(app);
  1443. furi_mutex_release(context->mutex);
  1444. furi_string_free(parsed_data);
  1445. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  1446. metroflip_app_blink_stop(app);
  1447. stage = MetroflipPollerEventTypeSuccess;
  1448. next_command = NfcCommandStop;
  1449. } while(false);
  1450. if(stage != MetroflipPollerEventTypeSuccess) {
  1451. next_command = NfcCommandStop;
  1452. }
  1453. }
  1454. }
  1455. bit_buffer_free(tx_buffer);
  1456. bit_buffer_free(rx_buffer);
  1457. return next_command;
  1458. }
  1459. void metroflip_scene_navigo_on_enter(void* context) {
  1460. Metroflip* app = context;
  1461. dolphin_deed(DolphinDeedNfcRead);
  1462. // Setup view
  1463. Popup* popup = app->popup;
  1464. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  1465. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  1466. // Start worker
  1467. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  1468. nfc_scanner_alloc(app->nfc);
  1469. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolIso14443_4b);
  1470. nfc_poller_start(app->poller, metroflip_scene_navigo_poller_callback, app);
  1471. metroflip_app_blink_start(app);
  1472. }
  1473. bool metroflip_scene_navigo_on_event(void* context, SceneManagerEvent event) {
  1474. Metroflip* app = context;
  1475. bool consumed = false;
  1476. if(event.type == SceneManagerEventTypeCustom) {
  1477. if(event.event == MetroflipPollerEventTypeCardDetect) {
  1478. Popup* popup = app->popup;
  1479. popup_set_header(popup, "Scanning..", 68, 30, AlignLeft, AlignTop);
  1480. consumed = true;
  1481. } else if(event.event == MetroflipCustomEventPollerFileNotFound) {
  1482. Popup* popup = app->popup;
  1483. popup_set_header(popup, "Read Error,\n wrong card", 68, 30, AlignLeft, AlignTop);
  1484. consumed = true;
  1485. } else if(event.event == MetroflipCustomEventPollerFail) {
  1486. Popup* popup = app->popup;
  1487. popup_set_header(popup, "Error, try\n again", 68, 30, AlignLeft, AlignTop);
  1488. consumed = true;
  1489. }
  1490. } else if(event.type == SceneManagerEventTypeBack) {
  1491. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  1492. consumed = true;
  1493. }
  1494. return consumed;
  1495. }
  1496. void metroflip_scene_navigo_on_exit(void* context) {
  1497. Metroflip* app = context;
  1498. if(app->poller) {
  1499. nfc_poller_stop(app->poller);
  1500. nfc_poller_free(app->poller);
  1501. }
  1502. metroflip_app_blink_stop(app);
  1503. widget_reset(app->widget);
  1504. // Clear view
  1505. popup_reset(app->popup);
  1506. if(app->calypso_context) {
  1507. CalypsoContext* ctx = app->calypso_context;
  1508. free(ctx->card->navigo);
  1509. free(ctx->card->opus);
  1510. free(ctx->card);
  1511. furi_mutex_free(ctx->mutex);
  1512. free(ctx);
  1513. app->calypso_context = NULL;
  1514. }
  1515. }