1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 //! Implements LMP Event AIDL Interface.
17 
18 use android_hardware_bluetooth_lmp_event::aidl::android::hardware::bluetooth::lmp_event::{
19     Direction::Direction, AddressType::AddressType, IBluetoothLmpEvent::IBluetoothLmpEvent,
20     IBluetoothLmpEventCallback::IBluetoothLmpEventCallback, LmpEventId::LmpEventId,
21     Timestamp::Timestamp,
22 };
23 
24 use binder::{Interface, Result, Strong};
25 
26 use log::info;
27 use std::thread;
28 use std::time;
29 
30 
31 pub struct LmpEvent;
32 
33 impl LmpEvent {
new() -> Self34     pub fn new() -> Self {
35         Self
36     }
37 }
38 
39 impl Interface for LmpEvent {}
40 
41 impl IBluetoothLmpEvent for LmpEvent {
registerForLmpEvents(&self, callback: &Strong<dyn IBluetoothLmpEventCallback>, address_type: AddressType, address: &[u8; 6], lmp_event_ids: &[LmpEventId] ) -> Result<()>42     fn registerForLmpEvents(&self,
43                             callback: &Strong<dyn IBluetoothLmpEventCallback>,
44                             address_type: AddressType,
45                             address: &[u8; 6],
46                             lmp_event_ids: &[LmpEventId]
47     ) -> Result<()> {
48         info!("registerForLmpEvents");
49 
50         let cb = callback.clone();
51         let addr_type = address_type;
52         let addr = address.clone();
53         let lmp_event = lmp_event_ids.to_vec();
54 
55         let thread_handle = thread::spawn(move || {
56             let ts = Timestamp {
57                 bluetoothTimeUs: 1000000,
58                 systemTimeUs: 2000000,
59             };
60 
61             info!("sleep for 1000 ms");
62             thread::sleep(time::Duration::from_millis(1000));
63 
64             info!("callback event");
65             cb.onEventGenerated(&ts, addr_type, &addr, Direction::RX, lmp_event[0], 1)
66                 .expect("onEventGenerated failed");
67         });
68 
69         info!("callback register");
70         callback.onRegistered(true)?;
71 
72         thread_handle.join().expect("join failed");
73         Ok(())
74     }
unregisterLmpEvents(&self, _address_type: AddressType, _address: &[u8; 6]) -> Result<()>75     fn unregisterLmpEvents(&self, _address_type: AddressType, _address: &[u8; 6]) -> Result<()> {
76         info!("unregisterLmpEvents");
77 
78         Ok(())
79     }
80 }
81