xref: /aosp_15_r20/external/uwb/src/rust/uwb_core/src/params/ccc_started_app_config_params.rs (revision e0df40009cb5d71e642272d38ba1bb7ffccfce41)
1 // Copyright 2022, The Android Open Source Project
2 //
3 // Licensed under the Apache License, item 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 //     http://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 #![allow(missing_docs)]
16 
17 use crate::params::app_config_params::AppConfigTlvMap;
18 use crate::params::ccc_app_config_params::MINIMUM_BLOCK_DURATION_MS;
19 use crate::params::uci_packets::AppConfigTlvType;
20 use crate::params::utils::{bytes_to_u32, bytes_to_u64, bytes_to_u8};
21 
22 #[derive(Debug, Clone, PartialEq, Eq)]
23 pub struct CccStartedAppConfigParams {
24     pub sts_index: u32,
25     pub hop_mode_key: u32,
26     pub uwb_time0: u64,
27     pub ran_multiplier: u32,
28     pub sync_code_index: u8,
29 }
30 
31 impl CccStartedAppConfigParams {
from_config_map(mut config_map: AppConfigTlvMap) -> Option<Self>32     pub fn from_config_map(mut config_map: AppConfigTlvMap) -> Option<Self> {
33         Some(Self {
34             sts_index: bytes_to_u32(config_map.remove(&AppConfigTlvType::StsIndex)?)?,
35             hop_mode_key: bytes_to_u32(config_map.remove(&AppConfigTlvType::CccHopModeKey)?)?,
36             uwb_time0: bytes_to_u64(config_map.remove(&AppConfigTlvType::CccUwbTime0)?)?,
37             ran_multiplier: bytes_to_u32(config_map.remove(&AppConfigTlvType::RangingDuration)?)?
38                 / MINIMUM_BLOCK_DURATION_MS,
39             sync_code_index: bytes_to_u8(config_map.remove(&AppConfigTlvType::PreambleCodeIndex)?)?,
40         })
41     }
42 }
43 
44 #[cfg(test)]
45 mod tests {
46     use super::*;
47 
48     use std::collections::HashMap;
49 
50     use crate::params::utils::{u32_to_bytes, u64_to_bytes, u8_to_bytes};
51 
52     #[test]
test_from_config_map()53     fn test_from_config_map() {
54         let sts_index = 3;
55         let hop_mode_key = 5;
56         let uwb_time0 = 7;
57         let ran_multiplier = 4;
58         let sync_code_index = 9;
59 
60         let config_map = HashMap::from([
61             (AppConfigTlvType::StsIndex, u32_to_bytes(sts_index)),
62             (AppConfigTlvType::CccHopModeKey, u32_to_bytes(hop_mode_key)),
63             (AppConfigTlvType::CccUwbTime0, u64_to_bytes(uwb_time0)),
64             (
65                 AppConfigTlvType::RangingDuration,
66                 u32_to_bytes(ran_multiplier * MINIMUM_BLOCK_DURATION_MS),
67             ),
68             (AppConfigTlvType::PreambleCodeIndex, u8_to_bytes(sync_code_index)),
69         ]);
70         let params = CccStartedAppConfigParams::from_config_map(config_map).unwrap();
71 
72         assert_eq!(params.sts_index, sts_index);
73         assert_eq!(params.hop_mode_key, hop_mode_key);
74         assert_eq!(params.uwb_time0, uwb_time0);
75         assert_eq!(params.ran_multiplier, ran_multiplier);
76         assert_eq!(params.sync_code_index, sync_code_index);
77     }
78 }
79