xref: /aosp_15_r20/tools/netsim/rust/daemon/src/transport/uci.rs (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use bytes::Bytes;
16 
17 use std::io::{Error, Read};
18 
19 /// This module implements control packet parsing for UWB.
20 ///
21 /// UWB Command Interface Specification, UCI Generic Specification
22 /// Version 1.1
23 ///
24 /// 2.3.2 Format of Control Packets
25 
26 const UCI_HEADER_SIZE: usize = 4;
27 const UCI_PAYLOAD_LENGTH_FIELD: usize = 3;
28 
29 #[derive(Debug)]
30 pub struct Packet {
31     pub payload: Bytes,
32 }
33 
34 #[derive(Debug)]
35 pub enum PacketError {
36     IoError(Error),
37 }
38 
read_uci_packet<R: Read>(reader: &mut R) -> Result<Packet, PacketError>39 pub fn read_uci_packet<R: Read>(reader: &mut R) -> Result<Packet, PacketError> {
40     // Read the UCI header
41     let mut buffer = vec![0; UCI_HEADER_SIZE];
42     reader.read_exact(&mut buffer[0..]).map_err(PacketError::IoError)?;
43     // Extract the control packet payload length and read
44     let length = buffer[UCI_PAYLOAD_LENGTH_FIELD] as usize + UCI_HEADER_SIZE;
45     buffer.resize(length, 0);
46     reader.read_exact(&mut buffer[UCI_HEADER_SIZE..]).map_err(PacketError::IoError)?;
47     Ok(Packet { payload: Bytes::from(buffer) })
48 }
49