xref: /aosp_15_r20/tools/netsim/rust/daemon/src/bluetooth/mocked.rs (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
1*cf78ab8cSAndroid Build Coastguard Worker // Copyright 2023 Google LLC
2*cf78ab8cSAndroid Build Coastguard Worker //
3*cf78ab8cSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*cf78ab8cSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*cf78ab8cSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*cf78ab8cSAndroid Build Coastguard Worker //
7*cf78ab8cSAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*cf78ab8cSAndroid Build Coastguard Worker //
9*cf78ab8cSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*cf78ab8cSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*cf78ab8cSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*cf78ab8cSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*cf78ab8cSAndroid Build Coastguard Worker // limitations under the License.
14*cf78ab8cSAndroid Build Coastguard Worker 
15*cf78ab8cSAndroid Build Coastguard Worker use crate::bluetooth::{get_beacon_chips, BeaconChip};
16*cf78ab8cSAndroid Build Coastguard Worker use crate::devices::chip::{ChipIdentifier, FacadeIdentifier};
17*cf78ab8cSAndroid Build Coastguard Worker use crate::devices::device::{AddChipResult, DeviceIdentifier};
18*cf78ab8cSAndroid Build Coastguard Worker use ::protobuf::MessageField;
19*cf78ab8cSAndroid Build Coastguard Worker use log::info;
20*cf78ab8cSAndroid Build Coastguard Worker use netsim_proto::config::Bluetooth as BluetoothConfig;
21*cf78ab8cSAndroid Build Coastguard Worker use netsim_proto::configuration::Controller as RootcanalController;
22*cf78ab8cSAndroid Build Coastguard Worker use netsim_proto::model::chip::{BleBeacon, Bluetooth};
23*cf78ab8cSAndroid Build Coastguard Worker use netsim_proto::model::chip_create::Chip as Builtin;
24*cf78ab8cSAndroid Build Coastguard Worker use netsim_proto::model::{ChipCreate, DeviceCreate};
25*cf78ab8cSAndroid Build Coastguard Worker use std::sync::atomic::{AtomicU32, Ordering};
26*cf78ab8cSAndroid Build Coastguard Worker use std::sync::Mutex;
27*cf78ab8cSAndroid Build Coastguard Worker use std::sync::RwLock;
28*cf78ab8cSAndroid Build Coastguard Worker use std::{collections::HashMap, ptr::null};
29*cf78ab8cSAndroid Build Coastguard Worker 
30*cf78ab8cSAndroid Build Coastguard Worker static IDS: AtomicU32 = AtomicU32::new(0);
31*cf78ab8cSAndroid Build Coastguard Worker 
next_id() -> FacadeIdentifier32*cf78ab8cSAndroid Build Coastguard Worker fn next_id() -> FacadeIdentifier {
33*cf78ab8cSAndroid Build Coastguard Worker     FacadeIdentifier(IDS.fetch_add(1, Ordering::SeqCst))
34*cf78ab8cSAndroid Build Coastguard Worker }
35*cf78ab8cSAndroid Build Coastguard Worker 
36*cf78ab8cSAndroid Build Coastguard Worker // Avoid crossing cxx boundary in tests
ble_beacon_add( device_name: String, chip_id: ChipIdentifier, chip_proto: &ChipCreate, ) -> Result<FacadeIdentifier, String>37*cf78ab8cSAndroid Build Coastguard Worker pub fn ble_beacon_add(
38*cf78ab8cSAndroid Build Coastguard Worker     device_name: String,
39*cf78ab8cSAndroid Build Coastguard Worker     chip_id: ChipIdentifier,
40*cf78ab8cSAndroid Build Coastguard Worker     chip_proto: &ChipCreate,
41*cf78ab8cSAndroid Build Coastguard Worker ) -> Result<FacadeIdentifier, String> {
42*cf78ab8cSAndroid Build Coastguard Worker     let beacon_proto = match &chip_proto.chip {
43*cf78ab8cSAndroid Build Coastguard Worker         Some(Builtin::BleBeacon(beacon_proto)) => beacon_proto,
44*cf78ab8cSAndroid Build Coastguard Worker         _ => return Err(String::from("failed to create ble beacon: unexpected chip type")),
45*cf78ab8cSAndroid Build Coastguard Worker     };
46*cf78ab8cSAndroid Build Coastguard Worker 
47*cf78ab8cSAndroid Build Coastguard Worker     let beacon_chip = BeaconChip::from_proto(device_name, chip_id, beacon_proto)?;
48*cf78ab8cSAndroid Build Coastguard Worker     if get_beacon_chips().write().unwrap().insert(chip_id, Mutex::new(beacon_chip)).is_some() {
49*cf78ab8cSAndroid Build Coastguard Worker         return Err(format!(
50*cf78ab8cSAndroid Build Coastguard Worker             "failed to create a bluetooth beacon chip with id {chip_id}: chip id already exists.",
51*cf78ab8cSAndroid Build Coastguard Worker         ));
52*cf78ab8cSAndroid Build Coastguard Worker     }
53*cf78ab8cSAndroid Build Coastguard Worker 
54*cf78ab8cSAndroid Build Coastguard Worker     let facade_id = next_id();
55*cf78ab8cSAndroid Build Coastguard Worker 
56*cf78ab8cSAndroid Build Coastguard Worker     info!("ble_beacon_add successful with chip_id: {chip_id}");
57*cf78ab8cSAndroid Build Coastguard Worker     Ok(facade_id)
58*cf78ab8cSAndroid Build Coastguard Worker }
59*cf78ab8cSAndroid Build Coastguard Worker 
ble_beacon_remove( chip_id: ChipIdentifier, facade_id: FacadeIdentifier, ) -> Result<(), String>60*cf78ab8cSAndroid Build Coastguard Worker pub fn ble_beacon_remove(
61*cf78ab8cSAndroid Build Coastguard Worker     chip_id: ChipIdentifier,
62*cf78ab8cSAndroid Build Coastguard Worker     facade_id: FacadeIdentifier,
63*cf78ab8cSAndroid Build Coastguard Worker ) -> Result<(), String> {
64*cf78ab8cSAndroid Build Coastguard Worker     info!("{:?}", get_beacon_chips().read().unwrap().keys());
65*cf78ab8cSAndroid Build Coastguard Worker     if get_beacon_chips().write().unwrap().remove(&chip_id).is_none() {
66*cf78ab8cSAndroid Build Coastguard Worker         Err(format!("failed to delete ble beacon chip: chip with id {chip_id} does not exist"))
67*cf78ab8cSAndroid Build Coastguard Worker     } else {
68*cf78ab8cSAndroid Build Coastguard Worker         Ok(())
69*cf78ab8cSAndroid Build Coastguard Worker     }
70*cf78ab8cSAndroid Build Coastguard Worker }
71