bt_rfcomm/frame/mux_commands/
test_command.rsuse fuchsia_bluetooth::types::Channel;
use packet_encoding::{Decodable, Encodable};
use crate::frame::FrameParseError;
const TEST_COMMAND_MAX_PATTERN_LENGTH: usize = Channel::DEFAULT_MAX_TX;
#[derive(Clone, Debug, PartialEq)]
pub struct TestCommandParams {
pub test_pattern: Vec<u8>,
}
impl Decodable for TestCommandParams {
type Error = FrameParseError;
fn decode(buf: &[u8]) -> Result<Self, FrameParseError> {
if buf.len() > TEST_COMMAND_MAX_PATTERN_LENGTH {
return Err(FrameParseError::InvalidFrame);
}
Ok(Self { test_pattern: buf.to_vec() })
}
}
impl Encodable for TestCommandParams {
type Error = FrameParseError;
fn encoded_len(&self) -> usize {
self.test_pattern.len()
}
fn encode(&self, buf: &mut [u8]) -> Result<(), FrameParseError> {
if buf.len() < self.encoded_len() {
return Err(FrameParseError::BufferTooSmall);
}
buf[..self.encoded_len()].copy_from_slice(&self.test_pattern);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use assert_matches::assert_matches;
#[test]
fn test_decode_test_command_with_empty_buf() {
let buf = [];
let expected = TestCommandParams { test_pattern: vec![] };
assert_eq!(TestCommandParams::decode(&buf[..]).unwrap(), expected);
}
#[test]
fn test_decode_test_command_with_nonempty_buf() {
let buf = [0x00, 0x01, 0x02, 0x03];
let expected = TestCommandParams { test_pattern: buf.to_vec() };
assert_eq!(TestCommandParams::decode(&buf[..]).unwrap(), expected);
}
#[test]
fn test_encode_buf_too_small() {
let mut small_buf = [];
let command = TestCommandParams { test_pattern: vec![0x01, 0x02] };
assert_matches!(command.encode(&mut small_buf[..]), Err(FrameParseError::BufferTooSmall));
}
#[test]
fn test_encode_larger_buf_is_ok() {
let mut buf = [0; 3];
let command = TestCommandParams { test_pattern: vec![0x01, 0x02] };
assert!(command.encode(&mut buf[..]).is_ok());
let expected = [0x01, 0x02, 0x00];
assert_eq!(buf, expected);
}
#[test]
fn test_encode_buf_success() {
let test_pattern = vec![0x01, 0x02, 0x03];
let mut buf = [0; 3];
let command = TestCommandParams { test_pattern: test_pattern.clone() };
assert!(command.encode(&mut buf[..]).is_ok());
assert_eq!(test_pattern, buf);
}
}