blob: 428f08fec0ccf730daf60dc2a5f37a3b4735c3fc [file] [log] [blame]
Charlie Boutierd572dd02023-04-10 18:14:23 +00001use android_hardware_uwb::aidl::android::hardware::uwb::{IUwb, IUwbChip};
2use android_hardware_uwb::binder;
3use binder::{Result, Strong};
4use binder_tokio::TokioRuntime;
5use tokio::runtime::Handle as TokioHandle;
6
7use crate::uwb_chip;
8
9pub struct Uwb {
10 chips: Vec<Strong<dyn IUwbChip::IUwbChip>>,
11}
12
13impl Uwb {
14 pub fn from_chips(
15 chips: impl IntoIterator<Item = uwb_chip::UwbChip>,
16 handle: TokioHandle,
17 ) -> Self {
18 Self {
19 chips: chips
20 .into_iter()
21 .map(|chip| {
22 IUwbChip::BnUwbChip::new_async_binder(
23 chip,
24 TokioRuntime(handle.clone()),
25 binder::BinderFeatures::default(),
26 )
27 })
28 .collect(),
29 }
30 }
31}
32
33impl binder::Interface for Uwb {}
34
35impl IUwb::IUwb for Uwb {
36 fn getChips(&self) -> Result<Vec<String>> {
37 log::debug!("getChips");
38 self.chips.iter().map(|chip| chip.getName()).collect()
39 }
40
41 fn getChip(&self, name: &str) -> Result<Strong<dyn IUwbChip::IUwbChip>> {
42 log::debug!("getChip {}", name);
43 let chip = self
44 .chips
45 .iter()
46 .find(|chip| chip.getName().as_deref() == Ok(name));
47 if let Some(chip) = chip {
48 Ok(chip.clone())
49 } else {
50 Err(binder::ExceptionCode::ILLEGAL_ARGUMENT.into())
51 }
52 }
53}