use crate::Error;
use bytemuck::{Pod, Zeroable};
const DEVICE_INFO_MAGIC: u32 = 0x0044_4556;
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct DeviceInfo {
magic: u32, hardware_id: [u8; 32], secret: [u8; 128], }
impl Default for DeviceInfo {
fn default() -> Self {
Self {
magic: DEVICE_INFO_MAGIC,
hardware_id: [0; 32],
secret: [0; 128],
}
}
}
impl DeviceInfo {
pub fn new(hardware_id: &[u8], secret: &[u8]) -> Result<Self, Error> {
let mut di = Self::default();
di.set_hardware_id(hardware_id)?;
di.set_secret(secret)?;
Ok(di)
}
#[must_use]
pub fn is_valid(&self) -> bool {
self.magic == DEVICE_INFO_MAGIC
}
pub fn set_hardware_id(&mut self, hardware_id: &[u8]) -> Result<(), Error> {
if hardware_id.len() > 32 {
return Err(Error::IdentityLengthExceeded);
}
self.hardware_id[..hardware_id.len()].copy_from_slice(hardware_id);
Ok(())
}
pub fn set_secret(&mut self, secret: &[u8]) -> Result<(), Error> {
if secret.len() > 128 {
return Err(Error::IdentityLengthExceeded);
}
self.secret[..secret.len()].copy_from_slice(secret);
Ok(())
}
#[must_use]
pub fn hardware_id(&self) -> &[u8] {
&self.hardware_id
}
#[must_use]
pub fn secret(&self) -> &[u8] {
&self.secret
}
}