From 4a7aea406a79268dcd36ef34cda2e230ab25b62d Mon Sep 17 00:00:00 2001 From: ppc Date: Mon, 5 Jan 2026 21:18:13 +0000 Subject: [PATCH] update file-based read logic --- amnet-server/src/lib.rs | 78 +++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/amnet-server/src/lib.rs b/amnet-server/src/lib.rs index f971761..83b78d4 100644 --- a/amnet-server/src/lib.rs +++ b/amnet-server/src/lib.rs @@ -2,7 +2,7 @@ use std::sync::OnceLock; use tokio::net::TcpListener; use tokio::runtime::Runtime; -use tracing::{info, warn}; +use tracing::{debug, info, warn}; use crate::card::CardPresenter; use crate::config::Config; @@ -45,60 +45,54 @@ pub extern "C" fn aime_io_nfc_poll(unit_no: u8) -> i32 { return 1; } - let enter_pressed: bool; #[cfg(target_os = "windows")] { use windows_sys::Win32::UI::Input::KeyboardAndMouse::GetAsyncKeyState; - enter_pressed = unsafe { (GetAsyncKeyState(0x0D) & (0x8000u16 as i16)) != 0 }; - } - #[cfg(not(target_os = "windows"))] - { - enter_pressed = false; // Default to false on non-Windows platforms - } - - if !enter_pressed { - return 1; - } - - let aime_file_path = &Config::instance().aime_txt_path; - let aime_code = match std::fs::read_to_string(aime_file_path) { - Ok(content) => content.trim().to_string(), - Err(e) => { - warn!("Failed to read text file at {}: {}", aime_file_path.display(), e); + + let config = Config::instance(); + if !config.enable_aime_txt { return 1; } - }; - if aime_code.is_empty() || !utils::is_valid_access_code(&aime_code) { - warn!("AIME code in {} is invalid", aime_file_path.display()); - return 1; - } - - info!("AIME code read from {}: {}", aime_file_path.display(), aime_code); - let presenter_result = CardPresenter::instance().present_card( - &aime_code, - None, - std::time::Duration::from_secs(5), - ); - - match presenter_result { - crate::card::CardPresentResult::Accepted => (), - crate::card::CardPresentResult::Rejected(reason) => { - warn!("AIME code from {} was rejected: {}", aime_file_path.display(), reason); + let enter_pressed = unsafe { (GetAsyncKeyState(0x0D) & (0x8000u16 as i16)) != 0 }; + if !enter_pressed { return 1; } - crate::card::CardPresentResult::SlotUnavailable(release_time) => { - warn!( - "AIME code from {} could not be presented: slot unavailable until {:?}", - aime_file_path.display(), - release_time, - ); + let aime_code = match std::fs::read_to_string(&config.aime_txt_path) { + Ok(content) => content.trim().to_string(), + Err(e) => { + warn!("Failed to read text file at {}: {}", config.aime_txt_path.display(), e); + return 1; + } + }; + + if aime_code.is_empty() || !utils::is_valid_access_code(&aime_code) { + warn!("Access code file is either empty or contains invalid data"); return 1; } + + debug!("Access code read from file"); + let presenter_result = CardPresenter::instance().present_card( + &aime_code, + None, + std::time::Duration::from_secs(5), + ); + + match presenter_result { + crate::card::CardPresentResult::Accepted => { + return 0; + }, + crate::card::CardPresentResult::Rejected(reason) => { + warn!("Read access code was rejected: {}", reason); + } + crate::card::CardPresentResult::SlotUnavailable(release_time) => { + warn!("Access code was not accepted as there is already a card in the active slot until {:?}", release_time); + } + } } - return 0; + return 1; } #[unsafe(no_mangle)]