xref: /aosp_15_r20/tools/netsim/rust/daemon/src/bluetooth/mocked.rs (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
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::bluetooth::{get_beacon_chips, BeaconChip};
16 use crate::devices::chip::{ChipIdentifier, FacadeIdentifier};
17 use crate::devices::device::{AddChipResult, DeviceIdentifier};
18 use ::protobuf::MessageField;
19 use log::info;
20 use netsim_proto::config::Bluetooth as BluetoothConfig;
21 use netsim_proto::configuration::Controller as RootcanalController;
22 use netsim_proto::model::chip::{BleBeacon, Bluetooth};
23 use netsim_proto::model::chip_create::Chip as Builtin;
24 use netsim_proto::model::{ChipCreate, DeviceCreate};
25 use std::sync::atomic::{AtomicU32, Ordering};
26 use std::sync::Mutex;
27 use std::sync::RwLock;
28 use std::{collections::HashMap, ptr::null};
29 
30 static IDS: AtomicU32 = AtomicU32::new(0);
31 
next_id() -> FacadeIdentifier32 fn next_id() -> FacadeIdentifier {
33     FacadeIdentifier(IDS.fetch_add(1, Ordering::SeqCst))
34 }
35 
36 // Avoid crossing cxx boundary in tests
ble_beacon_add( device_name: String, chip_id: ChipIdentifier, chip_proto: &ChipCreate, ) -> Result<FacadeIdentifier, String>37 pub fn ble_beacon_add(
38     device_name: String,
39     chip_id: ChipIdentifier,
40     chip_proto: &ChipCreate,
41 ) -> Result<FacadeIdentifier, String> {
42     let beacon_proto = match &chip_proto.chip {
43         Some(Builtin::BleBeacon(beacon_proto)) => beacon_proto,
44         _ => return Err(String::from("failed to create ble beacon: unexpected chip type")),
45     };
46 
47     let beacon_chip = BeaconChip::from_proto(device_name, chip_id, beacon_proto)?;
48     if get_beacon_chips().write().unwrap().insert(chip_id, Mutex::new(beacon_chip)).is_some() {
49         return Err(format!(
50             "failed to create a bluetooth beacon chip with id {chip_id}: chip id already exists.",
51         ));
52     }
53 
54     let facade_id = next_id();
55 
56     info!("ble_beacon_add successful with chip_id: {chip_id}");
57     Ok(facade_id)
58 }
59 
ble_beacon_remove( chip_id: ChipIdentifier, facade_id: FacadeIdentifier, ) -> Result<(), String>60 pub fn ble_beacon_remove(
61     chip_id: ChipIdentifier,
62     facade_id: FacadeIdentifier,
63 ) -> Result<(), String> {
64     info!("{:?}", get_beacon_chips().read().unwrap().keys());
65     if get_beacon_chips().write().unwrap().remove(&chip_id).is_none() {
66         Err(format!("failed to delete ble beacon chip: chip with id {chip_id} does not exist"))
67     } else {
68         Ok(())
69     }
70 }
71