bt_rfcomm/frame/mux_commands/
flow_control.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use packet_encoding::{Decodable, Encodable};
6
7use crate::frame::FrameParseError;
8
9/// The length (in bytes) of a Flow Control Command. Both Flow Control On and Off commands
10/// contain no parameters.
11/// Defined in GSM 7.10 Section 5.4.6.3.5 & 5.4.6.3.6.
12const FLOW_CONTROL_COMMAND_LENGTH: usize = 0;
13
14/// The Flow Control Command is used to enable/disable aggregate flow.
15/// The command contains no parameters.
16/// Defined in GSM 7.10 Section 5.4.6.3.5 & 5.4.6.3.6.
17#[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}