use packed_struct::prelude::PackedStruct;
use crate::traits::CommandTrait;
#[derive(PackedStruct, Debug)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "0x50")]
pub struct AuthenticationMessage {
#[packed_field(bytes = "0x04:0x14")]
id: [u8; 16],
#[packed_field(bytes = "0x1E")]
magic0: u8,
#[packed_field(bytes = "0x2D")]
magic1: u8,
#[packed_field(bytes = "0x30:0x50")]
name: [u8; 0x20],
}
#[derive(PackedStruct, Debug)]
#[packed_struct(bit_numbering = "msb0", endian = "lsb", size_bytes = "0x14")]
pub struct AuthenticationResponse {
#[packed_field(bytes = "0x00:0x03")]
pub id: u32,
#[packed_field(bytes = "0x04:0x13")]
pub key: [u8; 16],
}
impl AuthenticationMessage {
pub fn new(name: &str) -> AuthenticationMessage {
let mut fixed_name = [0u8; 0x20];
let name_bytes = name.as_bytes();
let name_len = name_bytes.len();
let max = if name_len > 0x20 { 0x20 } else { name_len };
for i in 0..max {
fixed_name[i] = name_bytes[i];
}
return AuthenticationMessage {
id: [0x31u8; 16],
magic0: 0x1,
magic1: 0x1,
name: fixed_name,
};
}
}
impl CommandTrait for AuthenticationMessage {
fn packet_type() -> u16 {
return 0x0065;
}
}