bt_rfcomm/frame/mux_commands/
flow_control.rs
1use packet_encoding::{Decodable, Encodable};
6
7use crate::frame::FrameParseError;
8
9const FLOW_CONTROL_COMMAND_LENGTH: usize = 0;
13
14#[derive(Clone, Debug, PartialEq)]
18pub struct FlowControlParams {}
19
20impl Decodable for FlowControlParams {
21 type Error = FrameParseError;
22
23 fn decode(buf: &[u8]) -> Result<Self, FrameParseError> {
24 if buf.len() != FLOW_CONTROL_COMMAND_LENGTH {
25 return Err(FrameParseError::InvalidBufferLength(
26 FLOW_CONTROL_COMMAND_LENGTH,
27 buf.len(),
28 ));
29 }
30 Ok(Self {})
31 }
32}
33
34impl Encodable for FlowControlParams {
35 type Error = FrameParseError;
36
37 fn encoded_len(&self) -> usize {
38 FLOW_CONTROL_COMMAND_LENGTH
39 }
40
41 fn encode(&self, buf: &mut [u8]) -> Result<(), FrameParseError> {
42 if buf.len() < self.encoded_len() {
43 return Err(FrameParseError::BufferTooSmall);
44 }
45 Ok(())
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 use assert_matches::assert_matches;
54
55 #[test]
56 fn test_decode_flow_control_invalid_buf() {
57 let buf = [0x00, 0x01, 0x02];
58 assert_matches!(
59 FlowControlParams::decode(&buf[..]),
60 Err(FrameParseError::InvalidBufferLength(FLOW_CONTROL_COMMAND_LENGTH, 3))
61 );
62 }
63
64 #[test]
65 fn test_decode_flow_control() {
66 let buf = [];
67 assert_eq!(FlowControlParams::decode(&buf[..]).unwrap(), FlowControlParams {});
68 }
69
70 #[test]
71 fn test_encode_flow_control() {
72 let mut buf = [];
73 let expected: [u8; 0] = [];
74 let command = FlowControlParams {};
75 assert!(command.encode(&mut buf[..]).is_ok());
76 assert_eq!(buf, expected);
77 }
78}