| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688 |
- #include "metroflip_scene_calypso.h"
- #include "../metroflip_i.h"
- #include <datetime.h>
- #include <dolphin/dolphin.h>
- #include <notification/notification_messages.h>
- #include <locale/locale.h>
- #include <nfc/protocols/iso14443_4b/iso14443_4b_poller.h>
- #define TAG "Metroflip:Scene:Calypso"
- int select_new_app(
- int new_app_directory,
- int new_app,
- BitBuffer* tx_buffer,
- BitBuffer* rx_buffer,
- Iso14443_4bPoller* iso14443_4b_poller,
- Metroflip* app,
- MetroflipPollerEventType* stage) {
- select_app[5] = new_app_directory;
- select_app[6] = new_app;
- bit_buffer_reset(tx_buffer);
- bit_buffer_append_bytes(tx_buffer, select_app, sizeof(select_app));
- FURI_LOG_D(
- TAG,
- "SEND %02x %02x %02x %02x %02x %02x %02x %02x",
- select_app[0],
- select_app[1],
- select_app[2],
- select_app[3],
- select_app[4],
- select_app[5],
- select_app[6],
- select_app[7]);
- int error = iso14443_4b_poller_send_block(iso14443_4b_poller, tx_buffer, rx_buffer);
- if(error != Iso14443_4bErrorNone) {
- FURI_LOG_I(TAG, "Select File: iso14443_4b_poller_send_block error %d", error);
- *stage = MetroflipPollerEventTypeFail;
- view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventPollerFail);
- return error;
- }
- return 0;
- }
- int read_new_file(
- int new_file,
- BitBuffer* tx_buffer,
- BitBuffer* rx_buffer,
- Iso14443_4bPoller* iso14443_4b_poller,
- Metroflip* app,
- MetroflipPollerEventType* stage) {
- read_file[2] = new_file;
- bit_buffer_reset(tx_buffer);
- bit_buffer_append_bytes(tx_buffer, read_file, sizeof(read_file));
- FURI_LOG_D(
- TAG,
- "SEND %02x %02x %02x %02x %02x",
- read_file[0],
- read_file[1],
- read_file[2],
- read_file[3],
- read_file[4]);
- Iso14443_4bError error =
- iso14443_4b_poller_send_block(iso14443_4b_poller, tx_buffer, rx_buffer);
- if(error != Iso14443_4bErrorNone) {
- FURI_LOG_I(TAG, "Read File: iso14443_4b_poller_send_block error %d", error);
- *stage = MetroflipPollerEventTypeFail;
- view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventPollerFail);
- return error;
- }
- return 0;
- }
- int check_response(
- BitBuffer* rx_buffer,
- Metroflip* app,
- MetroflipPollerEventType* stage,
- size_t* response_length) {
- *response_length = bit_buffer_get_size_bytes(rx_buffer);
- if(bit_buffer_get_byte(rx_buffer, *response_length - 2) != apdu_success[0] ||
- bit_buffer_get_byte(rx_buffer, *response_length - 1) != apdu_success[1]) {
- int error_code_1 = bit_buffer_get_byte(rx_buffer, *response_length - 2);
- int error_code_2 = bit_buffer_get_byte(rx_buffer, *response_length - 1);
- FURI_LOG_E(TAG, "Select profile app/file failed: %02x%02x", error_code_1, error_code_2);
- if(error_code_1 == 0x6a && error_code_2 == 0x82) {
- FURI_LOG_E(TAG, "Wrong parameter(s) P1-P2 - File not found");
- } else if(error_code_1 == 0x69 && error_code_2 == 0x82) {
- FURI_LOG_E(TAG, "Command not allowed - Security status not satisfied");
- }
- *stage = MetroflipPollerEventTypeFail;
- view_dispatcher_send_custom_event(
- app->view_dispatcher, MetroflipCustomEventPollerFileNotFound);
- return 1;
- }
- return 0;
- }
- void update_page_info(void* context, FuriString* parsed_data) {
- Metroflip* app = context;
- CalypsoContext* ctx = app->calypso_context;
- if(ctx->card->card_type != CALYPSO_CARD_NAVIGO && ctx->card->card_type != CALYPSO_CARD_OPUS) {
- furi_string_cat_printf(
- parsed_data,
- "\e#%s %u:\n",
- get_network_string(ctx->card->card_type),
- ctx->card->card_number);
- return;
- }
- if(ctx->page_id == 0 || ctx->page_id == 1 || ctx->page_id == 2 || ctx->page_id == 3) {
- switch(ctx->card->card_type) {
- case CALYPSO_CARD_NAVIGO: {
- furi_string_cat_printf(
- parsed_data,
- "\e#%s %u:\n",
- get_navigo_type(ctx->card->navigo->holder.card_status),
- ctx->card->card_number);
- furi_string_cat_printf(parsed_data, "\e#Contract %d:\n", ctx->page_id + 1);
- show_navigo_contract_info(&ctx->card->navigo->contracts[ctx->page_id], parsed_data);
- break;
- }
- case CALYPSO_CARD_OPUS: {
- furi_string_cat_printf(parsed_data, "\e#Opus %u:\n", ctx->card->card_number);
- furi_string_cat_printf(parsed_data, "\e#Contract %d:\n", ctx->page_id + 1);
- show_opus_contract_info(&ctx->card->opus->contracts[ctx->page_id], parsed_data);
- break;
- }
- default: {
- furi_string_cat_printf(parsed_data, "\e#Unknown %u:\n", ctx->card->card_number);
- break;
- }
- }
- } else if(ctx->page_id == 4) {
- furi_string_cat_printf(parsed_data, "\e#Environment:\n");
- switch(ctx->card->card_type) {
- case CALYPSO_CARD_NAVIGO: {
- show_navigo_environment_info(&ctx->card->navigo->environment, parsed_data);
- break;
- }
- case CALYPSO_CARD_OPUS: {
- show_opus_environment_info(&ctx->card->opus->environment, parsed_data);
- break;
- }
- default: {
- break;
- }
- }
- } else if(ctx->page_id == 5 || ctx->page_id == 6 || ctx->page_id == 7) {
- furi_string_cat_printf(parsed_data, "\e#Event %d:\n", ctx->page_id - 4);
- switch(ctx->card->card_type) {
- case CALYPSO_CARD_NAVIGO: {
- show_navigo_event_info(
- &ctx->card->navigo->events[ctx->page_id - 5],
- ctx->card->navigo->contracts,
- parsed_data);
- break;
- }
- case CALYPSO_CARD_OPUS: {
- show_opus_event_info(
- &ctx->card->opus->events[ctx->page_id - 5],
- ctx->card->opus->contracts,
- parsed_data);
- break;
- }
- default: {
- break;
- }
- }
- } else if(ctx->page_id == 8 || ctx->page_id == 9 || ctx->page_id == 10) {
- furi_string_cat_printf(parsed_data, "\e#Special Event %d:\n", ctx->page_id - 7);
- switch(ctx->card->card_type) {
- case CALYPSO_CARD_NAVIGO: {
- show_navigo_special_event_info(
- &ctx->card->navigo->special_events[ctx->page_id - 8], parsed_data);
- break;
- }
- case CALYPSO_CARD_OPUS: {
- break;
- }
- default: {
- break;
- }
- }
- }
- }
- void update_widget_elements(void* context) {
- Metroflip* app = context;
- CalypsoContext* ctx = app->calypso_context;
- Widget* widget = app->widget;
- if(ctx->card->card_type != CALYPSO_CARD_NAVIGO && ctx->card->card_type != CALYPSO_CARD_OPUS) {
- widget_add_button_element(
- widget, GuiButtonTypeRight, "Exit", metroflip_next_button_widget_callback, context);
- return;
- }
- if(ctx->page_id < 10) {
- widget_add_button_element(
- widget, GuiButtonTypeRight, "Next", metroflip_next_button_widget_callback, context);
- } else {
- widget_add_button_element(
- widget, GuiButtonTypeRight, "Exit", metroflip_next_button_widget_callback, context);
- }
- if(ctx->page_id > 0) {
- widget_add_button_element(
- widget, GuiButtonTypeLeft, "Back", metroflip_back_button_widget_callback, context);
- }
- }
- void metroflip_back_button_widget_callback(GuiButtonType result, InputType type, void* context) {
- Metroflip* app = context;
- CalypsoContext* ctx = app->calypso_context;
- UNUSED(result);
- Widget* widget = app->widget;
- if(type == InputTypePress) {
- widget_reset(widget);
- FURI_LOG_I(TAG, "Page ID: %d -> %d", ctx->page_id, ctx->page_id - 1);
- if(ctx->page_id > 0) {
- if(ctx->page_id == 10 && ctx->card->special_events_count < 2) {
- ctx->page_id -= 1;
- }
- if(ctx->page_id == 9 && ctx->card->special_events_count < 1) {
- ctx->page_id -= 1;
- }
- if(ctx->page_id == 8 && ctx->card->events_count < 3) {
- ctx->page_id -= 1;
- }
- if(ctx->page_id == 7 && ctx->card->events_count < 2) {
- ctx->page_id -= 1;
- }
- if(ctx->page_id == 6 && ctx->card->events_count < 1) {
- ctx->page_id -= 1;
- }
- if(ctx->page_id == 4 && ctx->card->contracts_count < 4) {
- ctx->page_id -= 1;
- }
- if(ctx->page_id == 3 && ctx->card->contracts_count < 3) {
- ctx->page_id -= 1;
- }
- if(ctx->page_id == 2 && ctx->card->contracts_count < 2) {
- ctx->page_id -= 1;
- }
- ctx->page_id -= 1;
- }
- FuriString* parsed_data = furi_string_alloc();
- // Ensure no nested mutexes
- furi_mutex_acquire(ctx->mutex, FuriWaitForever);
- update_page_info(app, parsed_data);
- furi_mutex_release(ctx->mutex);
- widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
- // widget_add_icon_element(widget, 0, 0, &I_RFIDDolphinReceive_97x61);
- // Ensure no nested mutexes
- furi_mutex_acquire(ctx->mutex, FuriWaitForever);
- update_widget_elements(app);
- furi_mutex_release(ctx->mutex);
- furi_string_free(parsed_data);
- }
- }
- void metroflip_next_button_widget_callback(GuiButtonType result, InputType type, void* context) {
- Metroflip* app = context;
- CalypsoContext* ctx = app->calypso_context;
- UNUSED(result);
- Widget* widget = app->widget;
- if(type == InputTypePress) {
- widget_reset(widget);
- FURI_LOG_I(TAG, "Page ID: %d -> %d", ctx->page_id, ctx->page_id + 1);
- if(ctx->card->card_type != CALYPSO_CARD_NAVIGO &&
- ctx->card->card_type != CALYPSO_CARD_OPUS) {
- ctx->page_id = 0;
- scene_manager_search_and_switch_to_previous_scene(
- app->scene_manager, MetroflipSceneStart);
- return;
- }
- if(ctx->page_id < 10) {
- if(ctx->page_id == 0 && ctx->card->contracts_count < 2) {
- ctx->page_id += 1;
- }
- if(ctx->page_id == 1 && ctx->card->contracts_count < 3) {
- ctx->page_id += 1;
- }
- if(ctx->page_id == 2 && ctx->card->contracts_count < 4) {
- ctx->page_id += 1;
- }
- if(ctx->page_id == 4 && ctx->card->events_count < 1) {
- ctx->page_id += 1;
- }
- if(ctx->page_id == 5 && ctx->card->events_count < 2) {
- ctx->page_id += 1;
- }
- if(ctx->page_id == 6 && ctx->card->events_count < 3) {
- ctx->page_id += 1;
- }
- if(ctx->page_id == 7 && ctx->card->special_events_count < 1) {
- ctx->page_id += 1;
- }
- if(ctx->page_id == 8 && ctx->card->special_events_count < 2) {
- ctx->page_id += 1;
- }
- if(ctx->page_id == 9 && ctx->card->special_events_count < 3) {
- ctx->page_id = 0;
- scene_manager_search_and_switch_to_previous_scene(
- app->scene_manager, MetroflipSceneStart);
- return;
- }
- ctx->page_id += 1;
- } else {
- ctx->page_id = 0;
- scene_manager_search_and_switch_to_previous_scene(
- app->scene_manager, MetroflipSceneStart);
- return;
- }
- FuriString* parsed_data = furi_string_alloc();
- // Ensure no nested mutexes
- furi_mutex_acquire(ctx->mutex, FuriWaitForever);
- update_page_info(app, parsed_data);
- furi_mutex_release(ctx->mutex);
- widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
- // Ensure no nested mutexes
- furi_mutex_acquire(ctx->mutex, FuriWaitForever);
- update_widget_elements(app);
- furi_mutex_release(ctx->mutex);
- furi_string_free(parsed_data);
- }
- }
- void delay(int milliseconds) {
- furi_thread_flags_wait(0, FuriFlagWaitAny, milliseconds);
- }
- static NfcCommand metroflip_scene_navigo_poller_callback(NfcGenericEvent event, void* context) {
- furi_assert(event.protocol == NfcProtocolIso14443_4b);
- NfcCommand next_command = NfcCommandContinue;
- MetroflipPollerEventType stage = MetroflipPollerEventTypeStart;
- Metroflip* app = context;
- FuriString* parsed_data = furi_string_alloc();
- Widget* widget = app->widget;
- furi_string_reset(app->text_box_store);
- const Iso14443_4bPollerEvent* iso14443_4b_event = event.event_data;
- Iso14443_4bPoller* iso14443_4b_poller = event.instance;
- BitBuffer* tx_buffer = bit_buffer_alloc(Metroflip_POLLER_MAX_BUFFER_SIZE);
- BitBuffer* rx_buffer = bit_buffer_alloc(Metroflip_POLLER_MAX_BUFFER_SIZE);
- if(iso14443_4b_event->type == Iso14443_4bPollerEventTypeReady) {
- if(stage == MetroflipPollerEventTypeStart) {
- // Start Flipper vibration
- NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
- notification_message(notification, &sequence_set_vibro_on);
- delay(50);
- notification_message(notification, &sequence_reset_vibro);
- nfc_device_set_data(
- app->nfc_device, NfcProtocolIso14443_4b, nfc_poller_get_data(app->poller));
- Iso14443_4bError error;
- size_t response_length = 0;
- do {
- // Initialize the card data
- CalypsoCardData* card = malloc(sizeof(CalypsoCardData));
- // Select app ICC
- error = select_new_app(
- 0x00, 0x02, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- break;
- }
- // Check the response after selecting app
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- break;
- }
- // Now send the read command for ICC
- error = read_new_file(0x01, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- break;
- }
- // Check the response after reading the file
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- break;
- }
- char icc_bit_representation[response_length * 8 + 1];
- icc_bit_representation[0] = '\0';
- for(size_t i = 0; i < response_length; i++) {
- char bits[9];
- uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
- byte_to_binary(byte, bits);
- strlcat(icc_bit_representation, bits, sizeof(icc_bit_representation));
- }
- icc_bit_representation[response_length * 8] = '\0';
- int start = 128, end = 159;
- card->card_number = bit_slice_to_dec(icc_bit_representation, start, end);
- // Select app for ticketing
- error = select_new_app(
- 0x20, 0x00, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- FURI_LOG_E(TAG, "Failed to select app for ticketing");
- break;
- }
- // Check the response after selecting app
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- FURI_LOG_E(TAG, "Failed to check response after selecting app for ticketing");
- break;
- }
- // Select app for environment
- error = select_new_app(
- 0x20, 0x1, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- break;
- }
- // Check the response after selecting app
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- break;
- }
- // read file 1
- error = read_new_file(1, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- break;
- }
- // Check the response after reading the file
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- break;
- }
- char environment_bit_representation[response_length * 8 + 1];
- environment_bit_representation[0] = '\0';
- for(size_t i = 0; i < response_length; i++) {
- char bits[9];
- uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
- byte_to_binary(byte, bits);
- strlcat(
- environment_bit_representation,
- bits,
- sizeof(environment_bit_representation));
- }
- // FURI_LOG_I(
- // TAG, "Environment bit_representation: %s", environment_bit_representation);
- start = 13;
- end = 16;
- int country_num =
- bit_slice_to_dec(environment_bit_representation, start, end) * 100 +
- bit_slice_to_dec(environment_bit_representation, start + 4, end + 4) * 10 +
- bit_slice_to_dec(environment_bit_representation, start + 8, end + 8);
- start = 25;
- end = 28;
- int network_num =
- bit_slice_to_dec(environment_bit_representation, start, end) * 100 +
- bit_slice_to_dec(environment_bit_representation, start + 4, end + 4) * 10 +
- bit_slice_to_dec(environment_bit_representation, start + 8, end + 8);
- card->card_type = guess_card_type(country_num, network_num);
- switch(card->card_type) {
- case CALYPSO_CARD_NAVIGO: {
- card->navigo = malloc(sizeof(NavigoCardData));
- card->navigo->environment.country_num = country_num;
- card->navigo->environment.network_num = network_num;
- CalypsoApp* IntercodeEnvHolderStructure = get_intercode_structure_env_holder();
- // EnvApplicationVersionNumber
- const char* env_key = "EnvApplicationVersionNumber";
- int positionOffset = get_calypso_node_offset(
- environment_bit_representation, env_key, IntercodeEnvHolderStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(env_key, IntercodeEnvHolderStructure) - 1;
- card->navigo->environment.app_version =
- bit_slice_to_dec(environment_bit_representation, start, end);
- // EnvApplicationValidityEndDate
- env_key = "EnvApplicationValidityEndDate";
- positionOffset = get_calypso_node_offset(
- environment_bit_representation, env_key, IntercodeEnvHolderStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(env_key, IntercodeEnvHolderStructure) - 1;
- float decimal_value =
- bit_slice_to_dec(environment_bit_representation, start, end);
- uint64_t end_validity_timestamp =
- (decimal_value * 24 * 3600) + (float)epoch + 3600;
- datetime_timestamp_to_datetime(
- end_validity_timestamp, &card->navigo->environment.end_dt);
- // HolderDataCardStatus
- env_key = "HolderDataCardStatus";
- positionOffset = get_calypso_node_offset(
- environment_bit_representation, env_key, IntercodeEnvHolderStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(env_key, IntercodeEnvHolderStructure) - 1;
- card->navigo->holder.card_status =
- bit_slice_to_dec(environment_bit_representation, start, end);
- // HolderDataCommercialID
- env_key = "HolderDataCommercialID";
- positionOffset = get_calypso_node_offset(
- environment_bit_representation, env_key, IntercodeEnvHolderStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(env_key, IntercodeEnvHolderStructure) - 1;
- card->navigo->holder.commercial_id =
- bit_slice_to_dec(environment_bit_representation, start, end);
- // Free the calypso structure
- free_calypso_structure(IntercodeEnvHolderStructure);
- // Select app for contracts
- error = select_new_app(
- 0x20, 0x20, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- FURI_LOG_E(TAG, "Failed to select app for contracts");
- break;
- }
- // Check the response after selecting app
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- FURI_LOG_E(
- TAG, "Failed to check response after selecting app for contracts");
- break;
- }
- // Prepare calypso structure
- CalypsoApp* IntercodeContractStructure = get_intercode_structure_contract();
- if(!IntercodeContractStructure) {
- FURI_LOG_E(TAG, "Failed to load Intercode Contract structure");
- break;
- }
- // Now send the read command for contracts
- for(size_t i = 1; i < 5; i++) {
- error = read_new_file(
- i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- FURI_LOG_E(TAG, "Failed to read contract %d", i);
- break;
- }
- // Check the response after reading the file
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- FURI_LOG_E(
- TAG, "Failed to check response after reading contract %d", i);
- break;
- }
- char bit_representation[response_length * 8 + 1];
- bit_representation[0] = '\0';
- for(size_t i = 0; i < response_length; i++) {
- char bits[9];
- uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
- byte_to_binary(byte, bits);
- strlcat(bit_representation, bits, sizeof(bit_representation));
- }
- bit_representation[response_length * 8] = '\0';
- if(bit_slice_to_dec(
- bit_representation,
- 0,
- IntercodeContractStructure->container->elements[0].bitmap->size -
- 1) == 0) {
- break;
- }
- card->navigo->contracts[i - 1].present = 1;
- card->contracts_count++;
- // 2. ContractTariff
- const char* contract_key = "ContractTariff";
- if(is_calypso_node_present(
- bit_representation, contract_key, IntercodeContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, IntercodeContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(
- contract_key, IntercodeContractStructure) -
- 1;
- card->navigo->contracts[i - 1].tariff =
- bit_slice_to_dec(bit_representation, start, end);
- }
- // 3. ContractSerialNumber
- contract_key = "ContractSerialNumber";
- if(is_calypso_node_present(
- bit_representation, contract_key, IntercodeContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, IntercodeContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(
- contract_key, IntercodeContractStructure) -
- 1;
- card->navigo->contracts[i - 1].serial_number =
- bit_slice_to_dec(bit_representation, start, end);
- card->navigo->contracts[i - 1].serial_number_available = true;
- }
- // 8. ContractPayMethod
- contract_key = "ContractPayMethod";
- if(is_calypso_node_present(
- bit_representation, contract_key, IntercodeContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, IntercodeContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(
- contract_key, IntercodeContractStructure) -
- 1;
- card->navigo->contracts[i - 1].pay_method =
- bit_slice_to_dec(bit_representation, start, end);
- card->navigo->contracts[i - 1].pay_method_available = true;
- }
- // 10. ContractPriceAmount
- contract_key = "ContractPriceAmount";
- if(is_calypso_node_present(
- bit_representation, contract_key, IntercodeContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, IntercodeContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(
- contract_key, IntercodeContractStructure) -
- 1;
- card->navigo->contracts[i - 1].price_amount =
- bit_slice_to_dec(bit_representation, start, end) / 100.0;
- card->navigo->contracts[i - 1].price_amount_available = true;
- }
- // 13.0. ContractValidityStartDate
- contract_key = "ContractValidityStartDate";
- if(is_calypso_node_present(
- bit_representation, contract_key, IntercodeContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, IntercodeContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(
- contract_key, IntercodeContractStructure) -
- 1;
- float decimal_value =
- bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
- uint64_t start_validity_timestamp =
- (decimal_value + (float)epoch) + 3600;
- datetime_timestamp_to_datetime(
- start_validity_timestamp,
- &card->navigo->contracts[i - 1].start_date);
- }
- // 13.2. ContractValidityEndDate
- contract_key = "ContractValidityEndDate";
- if(is_calypso_node_present(
- bit_representation, contract_key, IntercodeContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, IntercodeContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(
- contract_key, IntercodeContractStructure) -
- 1;
- float decimal_value =
- bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
- uint64_t end_validity_timestamp =
- (decimal_value + (float)epoch) + 3600;
- datetime_timestamp_to_datetime(
- end_validity_timestamp, &card->navigo->contracts[i - 1].end_date);
- card->navigo->contracts[i - 1].end_date_available = true;
- }
- // 13.6. ContractValidityZones
- contract_key = "ContractValidityZones";
- if(is_calypso_node_present(
- bit_representation, contract_key, IntercodeContractStructure)) {
- int start = get_calypso_node_offset(
- bit_representation, contract_key, IntercodeContractStructure);
- // binary form is 00011111 for zones 5, 4, 3, 2, 1
- for(int j = 0; j < 5; j++) {
- card->navigo->contracts[i - 1].zones[j] = bit_slice_to_dec(
- bit_representation, start + 3 + j, start + 3 + j);
- }
- card->navigo->contracts[i - 1].zones_available = true;
- }
- // 13.7. ContractValidityJourneys -- pas sûr de le mettre lui
- // 15.0. ContractValiditySaleDate
- contract_key = "ContractValiditySaleDate";
- if(is_calypso_node_present(
- bit_representation, contract_key, IntercodeContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, IntercodeContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(
- contract_key, IntercodeContractStructure) -
- 1;
- float decimal_value =
- bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
- uint64_t sale_timestamp = (decimal_value + (float)epoch) + 3600;
- datetime_timestamp_to_datetime(
- sale_timestamp, &card->navigo->contracts[i - 1].sale_date);
- }
- // 15.2. ContractValiditySaleAgent - FIX NEEDED
- contract_key = "ContractValiditySaleAgent";
- /* if(is_calypso_node_present(
- bit_representation, contract_key, NavigoContractStructure)) { */
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, IntercodeContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(contract_key, IntercodeContractStructure) -
- 1;
- card->navigo->contracts[i - 1].sale_agent =
- bit_slice_to_dec(bit_representation, start, end);
- // }
- // 15.3. ContractValiditySaleDevice
- contract_key = "ContractValiditySaleDevice";
- if(is_calypso_node_present(
- bit_representation, contract_key, IntercodeContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, IntercodeContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(
- contract_key, IntercodeContractStructure) -
- 1;
- card->navigo->contracts[i - 1].sale_device =
- bit_slice_to_dec(bit_representation, start, end);
- }
- // 16. ContractStatus -- 0x1 ou 0xff
- contract_key = "ContractStatus";
- if(is_calypso_node_present(
- bit_representation, contract_key, IntercodeContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, IntercodeContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(
- contract_key, IntercodeContractStructure) -
- 1;
- card->navigo->contracts[i - 1].status =
- bit_slice_to_dec(bit_representation, start, end);
- }
- // 18. ContractAuthenticator
- contract_key = "ContractAuthenticator";
- if(is_calypso_node_present(
- bit_representation, contract_key, IntercodeContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, IntercodeContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(
- contract_key, IntercodeContractStructure) -
- 1;
- card->navigo->contracts[i - 1].authenticator =
- bit_slice_to_dec(bit_representation, start, end);
- }
- }
- // Free the calypso structure
- free_calypso_structure(IntercodeContractStructure);
- // Select app for counters (remaining tickets on Navigo Easy)
- error = select_new_app(
- 0x20, 0x69, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- break;
- }
- // Check the response after selecting app
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- break;
- }
- // read file 1
- error =
- read_new_file(1, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- break;
- }
- // Check the response after reading the file
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- break;
- }
- char counter_bit_representation[response_length * 8 + 1];
- counter_bit_representation[0] = '\0';
- for(size_t i = 0; i < response_length; i++) {
- char bits[9];
- uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
- byte_to_binary(byte, bits);
- strlcat(
- counter_bit_representation, bits, sizeof(counter_bit_representation));
- }
- // FURI_LOG_I(TAG, "Counter bit_representation: %s", counter_bit_representation);
- // Ticket counts (contracts 1-4)
- for(int i = 0; i < 4; i++) {
- start = 0;
- end = 5;
- card->navigo->contracts[i].counter.count = bit_slice_to_dec(
- counter_bit_representation, 24 * i + start, 24 * i + end);
- start = 6;
- end = 23;
- card->navigo->contracts[i].counter.relative_first_stamp_15mn =
- bit_slice_to_dec(
- counter_bit_representation, 24 * i + start, 24 * i + end);
- }
- // Select app for events
- error = select_new_app(
- 0x20, 0x10, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- break;
- }
- // Check the response after selecting app
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- break;
- }
- // Load the calypso structure for events
- CalypsoApp* IntercodeEventStructure = get_intercode_structure_event();
- if(!IntercodeEventStructure) {
- FURI_LOG_E(TAG, "Failed to load Intercode Event structure");
- break;
- }
- // furi_string_cat_printf(parsed_data, "\e#Events :\n");
- // Now send the read command for events
- for(size_t i = 1; i < 4; i++) {
- error = read_new_file(
- i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- break;
- }
- // Check the response after reading the file
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- break;
- }
- char event_bit_representation[response_length * 8 + 1];
- event_bit_representation[0] = '\0';
- for(size_t i = 0; i < response_length; i++) {
- char bits[9];
- uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
- byte_to_binary(byte, bits);
- strlcat(
- event_bit_representation, bits, sizeof(event_bit_representation));
- }
- // 2. EventCode
- const char* event_key = "EventCode";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) -
- 1;
- int decimal_value =
- bit_slice_to_dec(event_bit_representation, start, end);
- card->navigo->events[i - 1].transport_type = decimal_value >> 4;
- card->navigo->events[i - 1].transition = decimal_value & 15;
- }
- // 4. EventServiceProvider
- event_key = "EventServiceProvider";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
- card->navigo->events[i - 1].service_provider =
- bit_slice_to_dec(event_bit_representation, start, end);
- }
- // 8. EventLocationId
- event_key = "EventLocationId";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
- int decimal_value =
- bit_slice_to_dec(event_bit_representation, start, end);
- card->navigo->events[i - 1].station_group_id = decimal_value >> 9;
- card->navigo->events[i - 1].station_id = (decimal_value >> 4) & 31;
- }
- // 9. EventLocationGate
- event_key = "EventLocationGate";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
- card->navigo->events[i - 1].location_gate =
- bit_slice_to_dec(event_bit_representation, start, end);
- card->navigo->events[i - 1].location_gate_available = true;
- }
- // 10. EventDevice
- event_key = "EventDevice";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
- int decimal_value =
- bit_slice_to_dec(event_bit_representation, start, end);
- card->navigo->events[i - 1].device = decimal_value;
- int bus_device = decimal_value >> 8;
- card->navigo->events[i - 1].door = bus_device / 2 + 1;
- card->navigo->events[i - 1].side = bus_device % 2;
- card->navigo->events[i - 1].device_available = true;
- }
- // 11. EventRouteNumber
- event_key = "EventRouteNumber";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
- card->navigo->events[i - 1].route_number =
- bit_slice_to_dec(event_bit_representation, start, end);
- card->navigo->events[i - 1].route_number_available = true;
- }
- // 13. EventJourneyRun
- event_key = "EventJourneyRun";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
- card->navigo->events[i - 1].mission =
- bit_slice_to_dec(event_bit_representation, start, end);
- card->navigo->events[i - 1].mission_available = true;
- }
- // 14. EventVehicleId
- event_key = "EventVehicleId";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
- card->navigo->events[i - 1].vehicle_id =
- bit_slice_to_dec(event_bit_representation, start, end);
- card->navigo->events[i - 1].vehicle_id_available = true;
- }
- // 25. EventContractPointer
- event_key = "EventContractPointer";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
- card->navigo->events[i - 1].used_contract =
- bit_slice_to_dec(event_bit_representation, start, end);
- card->navigo->events[i - 1].used_contract_available = true;
- if(card->navigo->events[i - 1].used_contract > 0) {
- card->events_count++;
- }
- }
- // EventDateStamp
- event_key = "EventDateStamp";
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
- int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
- uint64_t date_timestamp = (decimal_value * 24 * 3600) + epoch + 3600;
- datetime_timestamp_to_datetime(
- date_timestamp, &card->navigo->events[i - 1].date);
- // EventTimeStamp
- event_key = "EventTimeStamp";
- positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
- decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
- card->navigo->events[i - 1].date.hour = (decimal_value * 60) / 3600;
- card->navigo->events[i - 1].date.minute =
- ((decimal_value * 60) % 3600) / 60;
- card->navigo->events[i - 1].date.second =
- ((decimal_value * 60) % 3600) % 60;
- }
- // Select app for special events
- error = select_new_app(
- 0x20, 0x40, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- break;
- }
- // Check the response after selecting app
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- break;
- }
- // Now send the read command for special events
- for(size_t i = 1; i < 4; i++) {
- error = read_new_file(
- i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- break;
- }
- // Check the response after reading the file
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- break;
- }
- char event_bit_representation[response_length * 8 + 1];
- event_bit_representation[0] = '\0';
- for(size_t i = 0; i < response_length; i++) {
- char bits[9];
- uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
- byte_to_binary(byte, bits);
- strlcat(
- event_bit_representation, bits, sizeof(event_bit_representation));
- }
- if(bit_slice_to_dec(
- event_bit_representation,
- 0,
- IntercodeEventStructure->container->elements[0].bitmap->size - 1) ==
- 0) {
- break;
- } else {
- card->special_events_count++;
- }
- // 2. EventCode
- const char* event_key = "EventCode";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) -
- 1;
- int decimal_value =
- bit_slice_to_dec(event_bit_representation, start, end);
- card->navigo->special_events[i - 1].transport_type = decimal_value >>
- 4;
- card->navigo->special_events[i - 1].transition = decimal_value & 15;
- }
- // 3. EventResult
- event_key = "EventResult";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) -
- 1;
- card->navigo->special_events[i - 1].result =
- bit_slice_to_dec(event_bit_representation, start, end);
- }
- // 4. EventServiceProvider
- event_key = "EventServiceProvider";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) -
- 1;
- card->navigo->special_events[i - 1].service_provider =
- bit_slice_to_dec(event_bit_representation, start, end);
- }
- // 8. EventLocationId
- event_key = "EventLocationId";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) -
- 1;
- int decimal_value =
- bit_slice_to_dec(event_bit_representation, start, end);
- card->navigo->special_events[i - 1].station_group_id = decimal_value >>
- 9;
- card->navigo->special_events[i - 1].station_id = (decimal_value >> 4) &
- 31;
- }
- // 10. EventDevice
- event_key = "EventDevice";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) -
- 1;
- int decimal_value =
- bit_slice_to_dec(event_bit_representation, start, end);
- card->navigo->special_events[i - 1].device = decimal_value;
- }
- // 11. EventRouteNumber
- event_key = "EventRouteNumber";
- if(is_calypso_node_present(
- event_bit_representation, event_key, IntercodeEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) -
- 1;
- card->navigo->special_events[i - 1].route_number =
- bit_slice_to_dec(event_bit_representation, start, end);
- card->navigo->special_events[i - 1].route_number_available = true;
- }
- // EventDateStamp
- event_key = "EventDateStamp";
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
- int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
- uint64_t date_timestamp = (decimal_value * 24 * 3600) + epoch + 3600;
- datetime_timestamp_to_datetime(
- date_timestamp, &card->navigo->special_events[i - 1].date);
- // EventTimeStamp
- event_key = "EventTimeStamp";
- positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, IntercodeEventStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, IntercodeEventStructure) - 1;
- decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
- card->navigo->special_events[i - 1].date.hour =
- (decimal_value * 60) / 3600;
- card->navigo->special_events[i - 1].date.minute =
- ((decimal_value * 60) % 3600) / 60;
- card->navigo->special_events[i - 1].date.second =
- ((decimal_value * 60) % 3600) % 60;
- }
- // Free the calypso structure
- free_calypso_structure(IntercodeEventStructure);
- break;
- }
- case CALYPSO_CARD_OPUS: {
- card->opus = malloc(sizeof(OpusCardData));
- card->opus->environment.country_num = country_num;
- card->opus->environment.network_num = network_num;
- CalypsoApp* OpusEnvHolderStructure = get_opus_env_holder_structure();
- // EnvApplicationVersionNumber
- const char* env_key = "EnvApplicationVersionNumber";
- int positionOffset = get_calypso_node_offset(
- environment_bit_representation, env_key, OpusEnvHolderStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(env_key, OpusEnvHolderStructure) - 1;
- card->opus->environment.app_version =
- bit_slice_to_dec(environment_bit_representation, start, end);
- // EnvApplicationValidityEndDate
- env_key = "EnvApplicationValidityEndDate";
- positionOffset = get_calypso_node_offset(
- environment_bit_representation, env_key, OpusEnvHolderStructure);
- start = positionOffset,
- end = positionOffset + get_calypso_node_size(env_key, OpusEnvHolderStructure) -
- 1;
- float decimal_value =
- bit_slice_to_dec(environment_bit_representation, start, end);
- uint64_t end_validity_timestamp =
- (decimal_value * 24 * 3600) + (float)epoch + 3600;
- datetime_timestamp_to_datetime(
- end_validity_timestamp, &card->opus->environment.end_dt);
- // HolderDataCardStatus
- env_key = "HolderDataCardStatus";
- positionOffset = get_calypso_node_offset(
- environment_bit_representation, env_key, OpusEnvHolderStructure);
- start = positionOffset,
- end = positionOffset + get_calypso_node_size(env_key, OpusEnvHolderStructure) -
- 1;
- card->opus->holder.card_status =
- bit_slice_to_dec(environment_bit_representation, start, end);
- // HolderDataCommercialID
- env_key = "HolderDataCommercialID";
- positionOffset = get_calypso_node_offset(
- environment_bit_representation, env_key, OpusEnvHolderStructure);
- start = positionOffset,
- end = positionOffset + get_calypso_node_size(env_key, OpusEnvHolderStructure) -
- 1;
- card->opus->holder.commercial_id =
- bit_slice_to_dec(environment_bit_representation, start, end);
- // Free the calypso structure
- free_calypso_structure(OpusEnvHolderStructure);
- // Select app for contracts
- error = select_new_app(
- 0x20, 0x20, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- FURI_LOG_E(TAG, "Failed to select app for contracts");
- break;
- }
- // Check the response after selecting app
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- FURI_LOG_E(
- TAG, "Failed to check response after selecting app for contracts");
- break;
- }
- // Prepare calypso structure
- CalypsoApp* OpusContractStructure = get_opus_contract_structure();
- if(!OpusContractStructure) {
- FURI_LOG_E(TAG, "Failed to load Opus Contract structure");
- break;
- }
- // Now send the read command for contracts
- for(size_t i = 1; i < 5; i++) {
- error = read_new_file(
- i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- FURI_LOG_E(TAG, "Failed to read contract %d", i);
- break;
- }
- // Check the response after reading the file
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- FURI_LOG_E(
- TAG, "Failed to check response after reading contract %d", i);
- break;
- }
- char bit_representation[response_length * 8 + 1];
- bit_representation[0] = '\0';
- for(size_t i = 0; i < response_length; i++) {
- char bits[9];
- uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
- byte_to_binary(byte, bits);
- strlcat(bit_representation, bits, sizeof(bit_representation));
- }
- bit_representation[response_length * 8] = '\0';
- if(bit_slice_to_dec(
- bit_representation,
- 0,
- OpusContractStructure->container->elements[1].bitmap->size - 1) ==
- 0) {
- break;
- }
- card->opus->contracts[i - 1].present = 1;
- card->contracts_count++;
- // ContractProvider
- const char* contract_key = "ContractProvider";
- if(is_calypso_node_present(
- bit_representation, contract_key, OpusContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, OpusContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(contract_key, OpusContractStructure) -
- 1;
- card->opus->contracts[i - 1].provider =
- bit_slice_to_dec(bit_representation, start, end);
- }
- // ContractTariff
- contract_key = "ContractTariff";
- if(is_calypso_node_present(
- bit_representation, contract_key, OpusContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, OpusContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(contract_key, OpusContractStructure) -
- 1;
- card->opus->contracts[i - 1].tariff =
- bit_slice_to_dec(bit_representation, start, end);
- }
- // ContractStartDate
- contract_key = "ContractStartDate";
- if(is_calypso_node_present(
- bit_representation, contract_key, OpusContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, OpusContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(contract_key, OpusContractStructure) -
- 1;
- float decimal_value =
- bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
- uint64_t start_validity_timestamp =
- (decimal_value + (float)epoch) + 3600;
- datetime_timestamp_to_datetime(
- start_validity_timestamp,
- &card->opus->contracts[i - 1].start_date);
- }
- // ContractEndDate
- contract_key = "ContractEndDate";
- if(is_calypso_node_present(
- bit_representation, contract_key, OpusContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, OpusContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(contract_key, OpusContractStructure) -
- 1;
- float decimal_value =
- bit_slice_to_dec(bit_representation, start, end) * 24 * 3600;
- uint64_t end_validity_timestamp =
- (decimal_value + (float)epoch) + 3600;
- datetime_timestamp_to_datetime(
- end_validity_timestamp, &card->opus->contracts[i - 1].end_date);
- }
- // ContractStatus
- contract_key = "ContractStatus";
- if(is_calypso_node_present(
- bit_representation, contract_key, OpusContractStructure)) {
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, OpusContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(contract_key, OpusContractStructure) -
- 1;
- card->opus->contracts[i - 1].status =
- bit_slice_to_dec(bit_representation, start, end);
- }
- // ContractSaleDate + ContractSaleTime
- contract_key = "ContractSaleDate";
- int positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, OpusContractStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(contract_key, OpusContractStructure) - 1;
- uint64_t sale_date_timestamp =
- (bit_slice_to_dec(bit_representation, start, end) + (float)epoch) +
- 3600;
- datetime_timestamp_to_datetime(
- sale_date_timestamp, &card->opus->contracts[i - 1].sale_date);
- contract_key = "ContractSaleTime";
- positionOffset = get_calypso_node_offset(
- bit_representation, contract_key, OpusContractStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(contract_key, OpusContractStructure) - 1;
- int decimal_value = bit_slice_to_dec(bit_representation, start, end);
- card->opus->contracts[i - 1].sale_date.hour = (decimal_value * 60) / 3600;
- card->opus->contracts[i - 1].sale_date.minute =
- ((decimal_value * 60) % 3600) / 60;
- card->opus->contracts[i - 1].sale_date.second =
- ((decimal_value * 60) % 3600) % 60;
- }
- // Free the calypso structure
- free_calypso_structure(OpusContractStructure);
- // Select app for events
- error = select_new_app(
- 0x20, 0x10, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- break;
- }
- // Check the response after selecting app
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- break;
- }
- // Load the calypso structure for events
- CalypsoApp* OpusEventStructure = get_opus_event_structure();
- if(!OpusEventStructure) {
- FURI_LOG_E(TAG, "Failed to load Opus Event structure");
- break;
- }
- // Now send the read command for events
- for(size_t i = 1; i < 4; i++) {
- error = read_new_file(
- i, tx_buffer, rx_buffer, iso14443_4b_poller, app, &stage);
- if(error != 0) {
- break;
- }
- // Check the response after reading the file
- if(check_response(rx_buffer, app, &stage, &response_length) != 0) {
- break;
- }
- char event_bit_representation[response_length * 8 + 1];
- event_bit_representation[0] = '\0';
- for(size_t i = 0; i < response_length; i++) {
- char bits[9];
- uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
- byte_to_binary(byte, bits);
- strlcat(
- event_bit_representation, bits, sizeof(event_bit_representation));
- }
- // EventServiceProvider
- const char* event_key = "EventServiceProvider";
- if(is_calypso_node_present(
- event_bit_representation, event_key, OpusEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, OpusEventStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, OpusEventStructure) - 1;
- card->opus->events[i - 1].service_provider =
- bit_slice_to_dec(event_bit_representation, start, end);
- }
- // EventRouteNumber
- event_key = "EventRouteNumber";
- if(is_calypso_node_present(
- event_bit_representation, event_key, OpusEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, OpusEventStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, OpusEventStructure) - 1;
- card->opus->events[i - 1].route_number =
- bit_slice_to_dec(event_bit_representation, start, end);
- card->opus->events[i - 1].route_number_available = true;
- }
- // EventContractPointer
- event_key = "EventContractPointer";
- if(is_calypso_node_present(
- event_bit_representation, event_key, OpusEventStructure)) {
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, OpusEventStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, OpusEventStructure) - 1;
- card->opus->events[i - 1].used_contract =
- bit_slice_to_dec(event_bit_representation, start, end);
- card->opus->events[i - 1].used_contract_available = true;
- if(card->opus->events[i - 1].used_contract > 0) {
- card->events_count++;
- }
- }
- // EventDate + EventTime
- event_key = "EventDate";
- int positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, OpusEventStructure);
- int start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, OpusEventStructure) - 1;
- uint64_t date_timestamp =
- (bit_slice_to_dec(event_bit_representation, start, end) +
- (float)epoch) +
- 3600;
- datetime_timestamp_to_datetime(
- date_timestamp, &card->opus->events[i - 1].date);
- event_key = "EventTime";
- positionOffset = get_calypso_node_offset(
- event_bit_representation, event_key, OpusEventStructure);
- start = positionOffset,
- end = positionOffset +
- get_calypso_node_size(event_key, OpusEventStructure) - 1;
- int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
- card->opus->events[i - 1].date.hour = (decimal_value * 60) / 3600;
- card->opus->events[i - 1].date.minute = ((decimal_value * 60) % 3600) / 60;
- card->opus->events[i - 1].date.second = ((decimal_value * 60) % 3600) % 60;
- }
- // Free the calypso structure
- free_calypso_structure(OpusEventStructure);
- break;
- }
- case CALYPSO_CARD_UNKNOWN: {
- start = 3;
- end = 6;
- country_num =
- bit_slice_to_dec(environment_bit_representation, start, end) * 100 +
- bit_slice_to_dec(environment_bit_representation, start + 4, end + 4) * 10 +
- bit_slice_to_dec(environment_bit_representation, start + 8, end + 8);
- start = 15;
- end = 18;
- network_num =
- bit_slice_to_dec(environment_bit_representation, start, end) * 100 +
- bit_slice_to_dec(environment_bit_representation, start + 4, end + 4) * 10 +
- bit_slice_to_dec(environment_bit_representation, start + 8, end + 8);
- card->card_type = guess_card_type(country_num, network_num);
- if(card->card_type == CALYPSO_CARD_RAVKAV) {
- }
- break;
- }
- default:
- break;
- }
- widget_add_text_scroll_element(
- widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
- CalypsoContext* context = malloc(sizeof(CalypsoContext));
- context->card = card;
- context->page_id = 0;
- context->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
- app->calypso_context = context;
- // Ensure no nested mutexes
- furi_mutex_acquire(context->mutex, FuriWaitForever);
- update_page_info(app, parsed_data);
- furi_mutex_release(context->mutex);
- widget_add_text_scroll_element(
- widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
- // Ensure no nested mutexes
- furi_mutex_acquire(context->mutex, FuriWaitForever);
- update_widget_elements(app);
- furi_mutex_release(context->mutex);
- furi_string_free(parsed_data);
- view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
- metroflip_app_blink_stop(app);
- stage = MetroflipPollerEventTypeSuccess;
- next_command = NfcCommandStop;
- } while(false);
- if(stage != MetroflipPollerEventTypeSuccess) {
- next_command = NfcCommandStop;
- }
- }
- }
- bit_buffer_free(tx_buffer);
- bit_buffer_free(rx_buffer);
- return next_command;
- }
- void metroflip_scene_navigo_on_enter(void* context) {
- Metroflip* app = context;
- dolphin_deed(DolphinDeedNfcRead);
- // Setup view
- Popup* popup = app->popup;
- popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
- popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
- // Start worker
- view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
- nfc_scanner_alloc(app->nfc);
- app->poller = nfc_poller_alloc(app->nfc, NfcProtocolIso14443_4b);
- nfc_poller_start(app->poller, metroflip_scene_navigo_poller_callback, app);
- metroflip_app_blink_start(app);
- }
- bool metroflip_scene_navigo_on_event(void* context, SceneManagerEvent event) {
- Metroflip* app = context;
- bool consumed = false;
- if(event.type == SceneManagerEventTypeCustom) {
- if(event.event == MetroflipPollerEventTypeCardDetect) {
- Popup* popup = app->popup;
- popup_set_header(popup, "Scanning..", 68, 30, AlignLeft, AlignTop);
- consumed = true;
- } else if(event.event == MetroflipCustomEventPollerFileNotFound) {
- Popup* popup = app->popup;
- popup_set_header(popup, "Read Error,\n wrong card", 68, 30, AlignLeft, AlignTop);
- consumed = true;
- } else if(event.event == MetroflipCustomEventPollerFail) {
- Popup* popup = app->popup;
- popup_set_header(popup, "Error, try\n again", 68, 30, AlignLeft, AlignTop);
- consumed = true;
- }
- } else if(event.type == SceneManagerEventTypeBack) {
- scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
- consumed = true;
- }
- return consumed;
- }
- void metroflip_scene_navigo_on_exit(void* context) {
- Metroflip* app = context;
- if(app->poller) {
- nfc_poller_stop(app->poller);
- nfc_poller_free(app->poller);
- }
- metroflip_app_blink_stop(app);
- widget_reset(app->widget);
- // Clear view
- popup_reset(app->popup);
- if(app->calypso_context) {
- CalypsoContext* ctx = app->calypso_context;
- free(ctx->card->navigo);
- free(ctx->card->opus);
- free(ctx->card);
- furi_mutex_free(ctx->mutex);
- free(ctx);
- app->calypso_context = NULL;
- }
- }
|