• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 use crate::uci::UwbErr;
18 use bytes::Bytes;
19 use log::error;
20 use num_traits::FromPrimitive;
21 use uwb_uci_packets::{
22     AndroidSetCountryCodeCmdBuilder, AppConfigTlv, Controlee, DeviceResetCmdBuilder, GroupId,
23     ResetConfig, SessionInitCmdBuilder, SessionSetAppConfigCmdBuilder, SessionType,
24     SessionUpdateControllerMulticastListCmdBuilder, UciCommandPacket, UciVendor_9_CommandBuilder,
25     UciVendor_A_CommandBuilder, UciVendor_B_CommandBuilder, UciVendor_E_CommandBuilder,
26     UciVendor_F_CommandBuilder, UpdateMulticastListAction,
27 };
28 
build_session_init_cmd( session_id: u32, session_type: u8, ) -> Result<SessionInitCmdBuilder, UwbErr>29 pub fn build_session_init_cmd(
30     session_id: u32,
31     session_type: u8,
32 ) -> Result<SessionInitCmdBuilder, UwbErr> {
33     Ok(SessionInitCmdBuilder {
34         session_id,
35         session_type: SessionType::from_u8(session_type).ok_or(UwbErr::InvalidArgs)?,
36     })
37 }
38 
build_set_country_code_cmd(code: &[u8]) -> Result<AndroidSetCountryCodeCmdBuilder, UwbErr>39 pub fn build_set_country_code_cmd(code: &[u8]) -> Result<AndroidSetCountryCodeCmdBuilder, UwbErr> {
40     Ok(AndroidSetCountryCodeCmdBuilder { country_code: code.try_into()? })
41 }
42 
build_multicast_list_update_cmd( session_id: u32, action: u8, no_of_controlee: u8, address_list: &[i16], sub_session_id_list: &[i32], ) -> Result<SessionUpdateControllerMulticastListCmdBuilder, UwbErr>43 pub fn build_multicast_list_update_cmd(
44     session_id: u32,
45     action: u8,
46     no_of_controlee: u8,
47     address_list: &[i16],
48     sub_session_id_list: &[i32],
49 ) -> Result<SessionUpdateControllerMulticastListCmdBuilder, UwbErr> {
50     if usize::from(no_of_controlee) != address_list.len()
51         || usize::from(no_of_controlee) != sub_session_id_list.len()
52     {
53         return Err(UwbErr::InvalidArgs);
54     }
55     let mut controlees = Vec::new();
56     for i in 0..no_of_controlee {
57         controlees.push(Controlee {
58             short_address: address_list[i as usize] as u16,
59             subsession_id: sub_session_id_list[i as usize] as u32,
60         });
61     }
62     Ok(SessionUpdateControllerMulticastListCmdBuilder {
63         session_id,
64         action: UpdateMulticastListAction::from_u8(action).ok_or(UwbErr::InvalidArgs)?,
65         controlees,
66     })
67 }
68 
build_set_app_config_cmd( session_id: u32, no_of_params: u32, mut app_configs: &[u8], ) -> Result<SessionSetAppConfigCmdBuilder, UwbErr>69 pub fn build_set_app_config_cmd(
70     session_id: u32,
71     no_of_params: u32,
72     mut app_configs: &[u8],
73 ) -> Result<SessionSetAppConfigCmdBuilder, UwbErr> {
74     let mut tlvs = Vec::new();
75     let received_tlvs_len = app_configs.len();
76     let mut parsed_tlvs_len = 0;
77     for _ in 0..no_of_params {
78         let tlv = AppConfigTlv::parse(app_configs)?;
79         app_configs = app_configs.get(tlv.v.len() + 2..).ok_or(UwbErr::InvalidArgs)?;
80         parsed_tlvs_len += tlv.v.len() + 2;
81         tlvs.push(tlv);
82     }
83     if parsed_tlvs_len != received_tlvs_len {
84         error!("Parsed TLV len: {:?}, received len: {:?}", parsed_tlvs_len, received_tlvs_len);
85         return Err(UwbErr::InvalidArgs);
86     }
87     Ok(SessionSetAppConfigCmdBuilder { session_id, tlvs })
88 }
89 
build_uci_vendor_cmd_packet( gid: u32, oid: u32, payload: Vec<u8>, ) -> Result<UciCommandPacket, UwbErr>90 pub fn build_uci_vendor_cmd_packet(
91     gid: u32,
92     oid: u32,
93     payload: Vec<u8>,
94 ) -> Result<UciCommandPacket, UwbErr> {
95     use GroupId::*;
96     let group_id: GroupId = GroupId::from_u32(gid).ok_or(UwbErr::InvalidArgs)?;
97     let payload = if payload.is_empty() { None } else { Some(Bytes::from(payload)) };
98     let opcode: u8 = oid.try_into()?;
99     let packet: UciCommandPacket = match group_id {
100         VendorReserved9 => UciVendor_9_CommandBuilder { opcode, payload }.build().into(),
101         VendorReservedA => UciVendor_A_CommandBuilder { opcode, payload }.build().into(),
102         VendorReservedB => UciVendor_B_CommandBuilder { opcode, payload }.build().into(),
103         VendorReservedE => UciVendor_E_CommandBuilder { opcode, payload }.build().into(),
104         VendorReservedF => UciVendor_F_CommandBuilder { opcode, payload }.build().into(),
105         _ => {
106             error!("Invalid vendor gid {:?}", gid);
107             return Err(UwbErr::InvalidArgs);
108         }
109     };
110     Ok(packet)
111 }
112 
build_device_reset_cmd(reset_config: u8) -> Result<DeviceResetCmdBuilder, UwbErr>113 pub fn build_device_reset_cmd(reset_config: u8) -> Result<DeviceResetCmdBuilder, UwbErr> {
114     Ok(DeviceResetCmdBuilder {
115         reset_config: ResetConfig::from_u8(reset_config).ok_or(UwbErr::InvalidArgs)?,
116     })
117 }
118 
119 #[cfg(test)]
120 mod tests {
121     use super::*;
122     use num_traits::ToPrimitive;
123     use uwb_uci_packets::*;
124 
125     #[test]
test_build_uci_vendor_cmd_packet()126     fn test_build_uci_vendor_cmd_packet() {
127         let oid: u8 = 6;
128         let gid = GroupId::VendorReserved9;
129         let payload = vec![0x5, 0x5, 0x5, 0x5];
130         assert_eq!(
131             build_uci_vendor_cmd_packet(gid.to_u32().unwrap(), oid.into(), payload.clone())
132                 .unwrap()
133                 .to_bytes(),
134             UciVendor_9_CommandBuilder { opcode: oid, payload: Some(Bytes::from(payload)) }
135                 .build()
136                 .to_bytes()
137         );
138     }
139 }
140