chuniio-yubideck/common/src/lib.rs
2024-01-02 11:27:10 +07:00

62 lines
1.6 KiB
Rust

pub mod log;
use std::fmt::Display;
use anyhow::{anyhow, Result};
pub use log::init_logger;
use shared_memory::{Shmem, ShmemConf, ShmemError};
// #[repr(C)]
// struct YubideckInput {
// pub ir_value: u8,
// pub buttons: u8,
// pub touch_data: [u8; 32],
// pub card_status: u8,
// pub card_id: [u8; 10],
// }
pub const INPUT_SHMEM_SIZE: usize = 45;
// #[repr(C)]
// struct YubideckOutputGaosan {
// // Always 0
// pub packet_type: u8,
// pub slider_led_data: [u8; 60],
// }
//
// #[repr(C)]
// struct YubideckOutputDasi {
// // Always 1
// pub packet_type: u8,
// pub slider_led_data: [u8; 33],
// pub left_air_led: [u8; 3],
// pub right_air_led: [u8; 3],
// pub card_reader_led: [u8; 3],
// pub padding: [u8; 18],
// }
//
// There's an extra byte (byte 122) for AimeIO to communicate to ChuniIO
// that reader LED has changed and ChuniIO should update it.
pub const OUTPUT_SHMEM_SIZE: usize = 123;
pub fn create_shared_memory<S>(os_id: S, size: usize, is_owner: bool) -> Result<Shmem>
where
S: AsRef<str> + Display + Copy,
{
let shmem_conf = ShmemConf::new().size(size).os_id(os_id);
let shmem_result = match shmem_conf.clone().create() {
Ok(s) => Ok(s),
Err(ShmemError::MappingIdExists) => shmem_conf.open(),
Err(e) => {
return Err(anyhow!(
"Failed to create/open shared memory {os_id}: {e:#}"
));
}
};
shmem_result
.map(|mut m| {
m.set_owner(is_owner);
m
})
.map_err(|e| anyhow!("Failed to create/open shared memory {os_id}: {e:#}"))
}