Charlie Boutier | d572dd0 | 2023-04-10 18:14:23 +0000 | [diff] [blame^] | 1 | use android_hardware_uwb::aidl::android::hardware::uwb::{IUwb, IUwbChip}; |
| 2 | use android_hardware_uwb::binder; |
| 3 | use binder::{Result, Strong}; |
| 4 | use binder_tokio::TokioRuntime; |
| 5 | use tokio::runtime::Handle as TokioHandle; |
| 6 | |
| 7 | use crate::uwb_chip; |
| 8 | |
| 9 | pub struct Uwb { |
| 10 | chips: Vec<Strong<dyn IUwbChip::IUwbChip>>, |
| 11 | } |
| 12 | |
| 13 | impl 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 | |
| 33 | impl binder::Interface for Uwb {} |
| 34 | |
| 35 | impl 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 | } |