1 //! SecureClock HAL device implementation. 2 3 use super::{ChannelHalService, SerializedChannel}; 4 use crate::binder; 5 use crate::hal::secureclock::{ISecureClock, TimeStampToken::TimeStampToken}; 6 use crate::hal::Innto; 7 use kmr_wire::*; 8 use std::sync::{Arc, Mutex, MutexGuard}; 9 10 /// `ISecureClock` implementation which converts all method invocations to serialized requests that 11 /// are sent down the associated channel. 12 pub struct Device<T: SerializedChannel + 'static> { 13 channel: Arc<Mutex<T>>, 14 } 15 16 impl<T: SerializedChannel + Send> binder::Interface for Device<T> {} 17 18 impl<T: SerializedChannel + 'static> Device<T> { 19 /// Construct a new instance that uses the provided channel. new(channel: Arc<Mutex<T>>) -> Self20 pub fn new(channel: Arc<Mutex<T>>) -> Self { 21 Self { channel } 22 } 23 /// Create a new instance wrapped in a proxy object. new_as_binder(channel: Arc<Mutex<T>>) -> binder::Strong<dyn ISecureClock::ISecureClock>24 pub fn new_as_binder(channel: Arc<Mutex<T>>) -> binder::Strong<dyn ISecureClock::ISecureClock> { 25 ISecureClock::BnSecureClock::new_binder( 26 Self::new(channel), 27 binder::BinderFeatures::default(), 28 ) 29 } 30 } 31 32 impl<T: SerializedChannel> ChannelHalService<T> for Device<T> { channel(&self) -> MutexGuard<T>33 fn channel(&self) -> MutexGuard<T> { 34 self.channel.lock().unwrap() 35 } 36 } 37 38 impl<T: SerializedChannel> ISecureClock::ISecureClock for Device<T> { generateTimeStamp(&self, challenge: i64) -> binder::Result<TimeStampToken>39 fn generateTimeStamp(&self, challenge: i64) -> binder::Result<TimeStampToken> { 40 let rsp: GenerateTimeStampResponse = 41 self.execute(GenerateTimeStampRequest { challenge })?; 42 Ok(rsp.ret.innto()) 43 } 44 } 45