1 // Copyright 2023 Google LLC
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 //     https://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 use crate::{devices::chip::ChipIdentifier, ffi::ffi_bluetooth::RustBluetoothChip};
16 use cxx::{let_cxx_string, UniquePtr};
17 
18 /// Rust bluetooth chip trait.
19 pub trait RustBluetoothChipCallbacks {
tick(&mut self)20     fn tick(&mut self);
21 
22     // TODO: include the pdl library in Rust for reading the packet contents.
receive_link_layer_packet( &mut self, source_address: String, destination_address: String, packet_type: u8, packet: &[u8], )23     fn receive_link_layer_packet(
24         &mut self,
25         source_address: String,
26         destination_address: String,
27         packet_type: u8,
28         packet: &[u8],
29     );
30 }
31 
32 /// AddRustDeviceResult for the returned object of AddRustDevice() in C++
33 pub struct AddRustDeviceResult {
34     pub rust_chip: UniquePtr<RustBluetoothChip>,
35     pub facade_id: u32,
36 }
37 
create_add_rust_device_result( facade_id: u32, rust_chip: UniquePtr<RustBluetoothChip>, ) -> Box<AddRustDeviceResult>38 pub fn create_add_rust_device_result(
39     facade_id: u32,
40     rust_chip: UniquePtr<RustBluetoothChip>,
41 ) -> Box<AddRustDeviceResult> {
42     Box::new(AddRustDeviceResult { facade_id, rust_chip })
43 }
44 
45 /// Add a bluetooth chip by an object implements RustBluetoothChipCallbacks trait.
rust_bluetooth_add( chip_id: ChipIdentifier, callbacks: Box<dyn RustBluetoothChipCallbacks>, string_type: String, address: String, ) -> Box<AddRustDeviceResult>46 pub fn rust_bluetooth_add(
47     chip_id: ChipIdentifier,
48     callbacks: Box<dyn RustBluetoothChipCallbacks>,
49     string_type: String,
50     address: String,
51 ) -> Box<AddRustDeviceResult> {
52     let_cxx_string!(cxx_string_type = string_type);
53     let_cxx_string!(cxx_address = address);
54     crate::ffi::ffi_bluetooth::bluetooth_add_rust_device(
55         chip_id.0,
56         Box::new(callbacks),
57         &cxx_string_type,
58         &cxx_address,
59     )
60 }
61