xref: /aosp_15_r20/tools/netsim/rust/daemon/src/wireless/wireless_adaptor.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 bytes::Bytes;
16 
17 use crate::{
18     devices::chip::ChipIdentifier,
19     wireless::{ble_beacon, mocked},
20 };
21 use netsim_proto::model::Chip as ProtoChip;
22 use netsim_proto::stats::NetsimRadioStats as ProtoRadioStats;
23 
24 pub type WirelessAdaptorImpl = Box<dyn WirelessAdaptor + Send + Sync>;
25 
26 #[cfg(not(test))]
27 use crate::wireless::{bluetooth, uwb, wifi};
28 
29 /// Parameter for each constructor of Emulated Chips
30 #[allow(clippy::large_enum_variant, dead_code)]
31 pub enum CreateParam {
32     BleBeacon(ble_beacon::CreateParams),
33     #[cfg(not(test))]
34     Bluetooth(bluetooth::CreateParams),
35     #[cfg(not(test))]
36     Wifi(wifi::CreateParams),
37     #[cfg(not(test))]
38     Uwb(uwb::CreateParams),
39     Mock(mocked::CreateParams),
40 }
41 
42 // TODO: Factory trait to include start, stop, and add
43 /// WirelessAdaptor is a trait that provides interface between the generic Chip
44 /// and Radio specific library (rootcanal, libslirp, pica).
45 pub trait WirelessAdaptor {
46     /// This is the main entry for incoming host-to-controller packets
47     /// from virtual devices called by the transport module. The format of the
48     /// packet depends on the emulated chip kind:
49     /// * Bluetooth - packet is H4 HCI format
50     /// * Wi-Fi - packet is Radiotap format
51     /// * UWB - packet is UCI format
52     /// * NFC - packet is NCI format
handle_request(&self, packet: &Bytes)53     fn handle_request(&self, packet: &Bytes);
54 
55     /// Reset the internal state of the emulated chip for the virtual device.
56     /// The transmitted and received packet count will be set to 0 and the chip
57     /// shall be in the enabled state following a call to this function.
reset(&self)58     fn reset(&self);
59 
60     /// Return the Chip model protobuf from the emulated chip. This is part of
61     /// the Frontend API.
get(&self) -> ProtoChip62     fn get(&self) -> ProtoChip;
63 
64     /// Patch the state of the emulated chip. For example enable/disable the
65     /// chip's host-to-controller packet processing. This is part of the
66     /// Frontend API
patch(&self, chip: &ProtoChip)67     fn patch(&self, chip: &ProtoChip);
68 
69     /// Return the NetsimRadioStats protobuf from the emulated chip. This is
70     /// part of NetsimStats protobuf.
get_stats(&self, duration_secs: u64) -> Vec<ProtoRadioStats>71     fn get_stats(&self, duration_secs: u64) -> Vec<ProtoRadioStats>;
72 }
73 
74 /// This is called when the transport module receives a new packet stream
75 /// connection from a virtual device.
new(create_param: &CreateParam, chip_id: ChipIdentifier) -> WirelessAdaptorImpl76 pub fn new(create_param: &CreateParam, chip_id: ChipIdentifier) -> WirelessAdaptorImpl {
77     // Based on create_param, construct WirelessAdaptor.
78     match create_param {
79         CreateParam::BleBeacon(params) => ble_beacon::new(params, chip_id),
80         #[cfg(not(test))]
81         CreateParam::Bluetooth(params) => bluetooth::new(params, chip_id),
82         #[cfg(not(test))]
83         CreateParam::Wifi(params) => wifi::new(params, chip_id),
84         #[cfg(not(test))]
85         CreateParam::Uwb(params) => uwb::new(params, chip_id),
86         CreateParam::Mock(params) => mocked::new(params, chip_id),
87     }
88 }
89 
90 // TODO(b/309529194):
91 // 1. Create Mock wireless adaptor, patch and get
92 // 2. Create Mock wireless adptor, patch and reset
93 #[cfg(test)]
94 mod tests {
95     #[test]
test_wireless_adaptor_new()96     fn test_wireless_adaptor_new() {
97         // TODO
98     }
99 }
100