use super::{Command, Response};
use session::SessionErrorKind::ProtocolError;
use {Adapter, CommandType, Session, SessionError};
pub(crate) const MAX_RAND_BYTES: u16 = 2048 - 1 - 2;
pub fn get_pseudo_random<A: Adapter>(
session: &mut Session<A>,
bytes: u16,
) -> Result<Vec<u8>, SessionError> {
if bytes >= MAX_RAND_BYTES {
fail!(
ProtocolError,
"Requested too many random bytes (>= 2045) to fit in response packet"
);
}
session
.send_command(GetPseudoRandomCommand { bytes })
.map(|response| response.bytes)
}
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct GetPseudoRandomCommand {
pub bytes: u16,
}
impl Command for GetPseudoRandomCommand {
type ResponseType = GetPseudoRandomResponse;
}
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct GetPseudoRandomResponse {
pub bytes: Vec<u8>,
}
impl Response for GetPseudoRandomResponse {
const COMMAND_TYPE: CommandType = CommandType::GetPseudoRandom;
}