use crate::{os_io_rx_evt, os_io_tx_cmd};
mod canary {
unsafe extern "C" {
static mut app_stack_canary: u32;
}
const APP_STACK_CANARY_MAGIC: u32 = 0xDEAD0031;
static mut CANARY_INITIALIZED: bool = false;
fn init_canary() {
unsafe {
core::ptr::write_volatile(&raw mut app_stack_canary, APP_STACK_CANARY_MAGIC);
CANARY_INITIALIZED = true;
}
}
#[inline]
pub(super) fn init_and_check() {
unsafe {
if !CANARY_INITIALIZED {
init_canary();
}
let canary_value = core::ptr::read_volatile(&raw const app_stack_canary);
if canary_value != APP_STACK_CANARY_MAGIC {
panic!("Stack canary corruption detected!");
}
}
}
}
pub fn io_rx(buffer: &mut [u8], check_se_event: bool) -> i32 {
canary::init_and_check();
unsafe {
os_io_rx_evt(
buffer.as_ptr() as _,
buffer.len() as u16,
core::ptr::null_mut(),
check_se_event,
)
}
}
pub fn io_tx(apdu_type: u8, buffer: &[u8], length: usize) -> i32 {
canary::init_and_check();
unsafe {
os_io_tx_cmd(
apdu_type,
buffer.as_ptr() as _,
length as u16,
core::ptr::null_mut(),
)
}
}