1 // Copyright 2022, The Android Open Source Project
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 // 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 //! Define the structs and enums for the parameters or responses of the UciManager's methods.
16 //! Most of them are re-exported uwb_uci_packets's structure.
17
18 #![allow(clippy::eq_op)]
19
20 use std::collections::{hash_map::RandomState, HashMap};
21 use std::iter::{zip, FromIterator};
22
23 use crate::uci::error::StatusCode;
24
25 // Re-export enums and structs from uwb_uci_packets.
26 pub use uwb_uci_packets::{
27 AppConfigStatus, AppConfigTlv, AppConfigTlvType, CapTlv, CapTlvType, Controlee,
28 ControleeStatus, DeviceConfigId, DeviceConfigStatus, DeviceConfigTlv, DeviceState,
29 ExtendedAddressTwoWayRangingMeasurement, MulticastUpdateStatusCode, PowerStats,
30 RangingMeasurementType, ReasonCode, ResetConfig, SessionState, SessionType,
31 ShortAddressTwoWayRangingMeasurement, UpdateMulticastListAction,
32 };
33
34 pub type SessionId = u32;
35 pub type SubSessionId = u32;
36
37 // Workaround: uwb_uci_packets's struct doesn't derive PartialEq trait.
38 // Implement the eq functions for each struct instead.
app_config_status_eq(a: &AppConfigStatus, b: &AppConfigStatus) -> bool39 pub fn app_config_status_eq(a: &AppConfigStatus, b: &AppConfigStatus) -> bool {
40 a.cfg_id == a.cfg_id && a.status == b.status
41 }
42
device_config_status_eq(a: &DeviceConfigStatus, b: &DeviceConfigStatus) -> bool43 pub fn device_config_status_eq(a: &DeviceConfigStatus, b: &DeviceConfigStatus) -> bool {
44 a.cfg_id == b.cfg_id && a.status == b.status
45 }
46
power_stats_eq(a: &PowerStats, b: &PowerStats) -> bool47 pub fn power_stats_eq(a: &PowerStats, b: &PowerStats) -> bool {
48 a.status == b.status
49 && a.idle_time_ms == b.idle_time_ms
50 && a.tx_time_ms == b.tx_time_ms
51 && a.rx_time_ms == b.rx_time_ms
52 && a.total_wake_count == b.total_wake_count
53 }
54
cap_tlv_eq(a: &CapTlv, b: &CapTlv) -> bool55 pub fn cap_tlv_eq(a: &CapTlv, b: &CapTlv) -> bool {
56 a.t == b.t && a.v == b.v
57 }
58
app_config_tlvs_eq(a: &[AppConfigTlv], b: &[AppConfigTlv]) -> bool59 pub fn app_config_tlvs_eq(a: &[AppConfigTlv], b: &[AppConfigTlv]) -> bool {
60 app_config_tlvs_to_map(a) == app_config_tlvs_to_map(b)
61 }
62
app_config_tlvs_to_map( tlvs: &[AppConfigTlv], ) -> HashMap<AppConfigTlvType, &Vec<u8>, RandomState>63 fn app_config_tlvs_to_map(
64 tlvs: &[AppConfigTlv],
65 ) -> HashMap<AppConfigTlvType, &Vec<u8>, RandomState> {
66 HashMap::from_iter(tlvs.iter().map(|config| (config.cfg_id, &config.v)))
67 }
68
device_config_tlvs_eq(a: &[DeviceConfigTlv], b: &[DeviceConfigTlv]) -> bool69 pub fn device_config_tlvs_eq(a: &[DeviceConfigTlv], b: &[DeviceConfigTlv]) -> bool {
70 device_config_tlvs_to_map(a) == device_config_tlvs_to_map(b)
71 }
72
device_config_tlvs_to_map( tlvs: &[DeviceConfigTlv], ) -> HashMap<DeviceConfigId, &Vec<u8>, RandomState>73 fn device_config_tlvs_to_map(
74 tlvs: &[DeviceConfigTlv],
75 ) -> HashMap<DeviceConfigId, &Vec<u8>, RandomState> {
76 HashMap::from_iter(tlvs.iter().map(|config| (config.cfg_id, &config.v)))
77 }
78
79 #[derive(Debug, Clone)]
80 pub struct CoreSetConfigResponse {
81 pub status: StatusCode,
82 pub config_status: Vec<DeviceConfigStatus>,
83 }
84
85 impl PartialEq for CoreSetConfigResponse {
eq(&self, other: &Self) -> bool86 fn eq(&self, other: &Self) -> bool {
87 self.status == other.status
88 && zip(&self.config_status, &other.config_status)
89 .all(|(a, b)| device_config_status_eq(a, b))
90 }
91 }
92
93 #[derive(Debug, Clone)]
94 pub struct SetAppConfigResponse {
95 pub status: StatusCode,
96 pub config_status: Vec<AppConfigStatus>,
97 }
98
99 impl PartialEq for SetAppConfigResponse {
eq(&self, other: &Self) -> bool100 fn eq(&self, other: &Self) -> bool {
101 self.status == other.status
102 && zip(&self.config_status, &other.config_status)
103 .all(|(a, b)| app_config_status_eq(a, b))
104 }
105 }
106
107 #[derive(Debug, Clone, PartialEq, Eq)]
108 pub struct CountryCode([u8; 2]);
109
110 impl CountryCode {
new(code: &[u8; 2]) -> Option<Self>111 pub fn new(code: &[u8; 2]) -> Option<Self> {
112 if !code[0].is_ascii_uppercase() || !code[1].is_ascii_uppercase() {
113 None
114 } else {
115 Some(Self(*code))
116 }
117 }
118 }
119
120 impl From<CountryCode> for [u8; 2] {
from(item: CountryCode) -> [u8; 2]121 fn from(item: CountryCode) -> [u8; 2] {
122 item.0
123 }
124 }
125
126 #[derive(Debug, Clone, PartialEq, Eq)]
127 pub struct GetDeviceInfoResponse {
128 pub uci_version: u16,
129 pub mac_version: u16,
130 pub phy_version: u16,
131 pub uci_test_version: u16,
132 pub vendor_spec_info: Vec<u8>,
133 }
134
135 #[derive(Debug, Clone, PartialEq, Eq)]
136 pub struct RawVendorMessage {
137 pub gid: u32,
138 pub oid: u32,
139 pub payload: Vec<u8>,
140 }
141