use super::{Request, RequestResponseType, Response, HEADER};
use bincode::{Decode, Encode};
#[derive(Encode, Request)]
pub struct GetCardsRequest {
header: u8,
message_type: u8,
_unused: u16,
device_id: u32,
}
impl GetCardsRequest {
pub fn new(device_id: u32) -> Self {
GetCardsRequest {
header: HEADER,
message_type: RequestResponseType::GetCards.into(),
_unused: 0,
device_id,
}
}
}
#[test]
fn get_cards_request_to_bytes() {
let expected = [
0x17, 0x58, 0x00, 0x00, 0x2d, 0x55, 0x39, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
];
let r = GetCardsRequest::new(423187757);
let actual = r.to_bytes();
assert_eq!(expected, actual);
}
#[derive(Decode, Response, Debug)]
pub struct GetCardsResponse {
pub header: u8,
pub message_type: u8,
_unused: u16,
pub device_id: u32,
pub records: u32,
}
#[test]
fn get_cards_response_from_bytes() {
let bytes: [u8; 64] = [
0x17, 0x58, 0x00, 0x00, 0x2d, 0x55, 0x39, 0x19, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
];
let r = GetCardsResponse::from_bytes(&bytes).unwrap();
assert_eq!(r.message_type, RequestResponseType::GetCards.into());
assert_eq!(r.device_id, 423187757);
assert_eq!(r.records, 13);
}