Surface devices module in netsim-cxx for testing in presubmit
- netsim-cxx/src/lib.rs includes `mod devices`
This allows for presubmit to run test cases in device resource.
- Included license headers
Bug: 278294035
Change-Id: Ib1a957081c9c29b859f21b085111d23caec44e1c
diff --git a/rust/netsim-cxx/src/devices/chip.rs b/rust/netsim-cxx/src/devices/chip.rs
index 7c0b251..2982739 100644
--- a/rust/netsim-cxx/src/devices/chip.rs
+++ b/rust/netsim-cxx/src/devices/chip.rs
@@ -1,3 +1,17 @@
+// Copyright 2023 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
/// A Chip is a generic emulated radio that connects to Chip Facade
/// library.
///
@@ -6,9 +20,9 @@
use crate::devices::facades::FacadeIdentifier;
use crate::devices::facades::*;
use crate::devices::id_factory::IdFactory;
-use crate::proto::common::ChipKind as ProtoChipKind;
-use crate::proto::model::Chip as ProtoChip;
-use crate::proto::model::State as ProtoState;
+use frontend_proto::common::ChipKind as ProtoChipKind;
+use frontend_proto::model::Chip as ProtoChip;
+use frontend_proto::model::State as ProtoState;
use lazy_static::lazy_static;
use protobuf::EnumOrUnknown;
diff --git a/rust/netsim-cxx/src/devices/device.rs b/rust/netsim-cxx/src/devices/device.rs
index 0d20b89..57b0709 100644
--- a/rust/netsim-cxx/src/devices/device.rs
+++ b/rust/netsim-cxx/src/devices/device.rs
@@ -1,3 +1,17 @@
+// Copyright 2023 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
// Device.rs
use protobuf::Message;
@@ -6,10 +20,10 @@
use crate::devices::chip::Chip;
use crate::devices::chip::ChipIdentifier;
use crate::devices::facades::FacadeIdentifier;
-use crate::proto::common::ChipKind as ProtoChipKind;
-use crate::proto::model::Device as ProtoDevice;
-use crate::proto::model::Orientation as ProtoOrientation;
-use crate::proto::model::Position as ProtoPosition;
+use frontend_proto::common::ChipKind as ProtoChipKind;
+use frontend_proto::model::Device as ProtoDevice;
+use frontend_proto::model::Orientation as ProtoOrientation;
+use frontend_proto::model::Position as ProtoPosition;
use std::collections::HashMap;
pub type DeviceIdentifier = i32;
diff --git a/rust/netsim-cxx/src/devices/devices_handler.rs b/rust/netsim-cxx/src/devices/devices_handler.rs
index 12b4725..f103db2 100644
--- a/rust/netsim-cxx/src/devices/devices_handler.rs
+++ b/rust/netsim-cxx/src/devices/devices_handler.rs
@@ -1,3 +1,17 @@
+// Copyright 2023 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
// devices_handler.rs
//
// Provides the API for the frontend and backend to interact with devices.
@@ -13,10 +27,10 @@
use super::id_factory::IdFactory;
use crate::devices::device::AddChipResult;
use crate::devices::device::Device;
-use crate::proto::common::ChipKind as ProtoChipKind;
-use crate::proto::model::Device as ProtoDevice;
-use crate::proto::model::Position as ProtoPosition;
-use crate::proto::model::Scene as ProtoScene;
+use frontend_proto::common::ChipKind as ProtoChipKind;
+use frontend_proto::model::Device as ProtoDevice;
+use frontend_proto::model::Position as ProtoPosition;
+use frontend_proto::model::Scene as ProtoScene;
use lazy_static::lazy_static;
use protobuf_json_mapping::merge_from_str;
use protobuf_json_mapping::print_to_string;
@@ -147,11 +161,8 @@
}
}
-impl ProtoPosition {
- fn distance(&self, other: &Self) -> f32 {
- ((other.x - self.x).powf(2.0) + (other.y - self.y).powf(2.0) + (other.z - self.z).powf(2.0))
- .sqrt()
- }
+fn distance(a: &ProtoPosition, b: &ProtoPosition) -> f32 {
+ ((b.x - a.x).powf(2.0) + (b.y - a.y).powf(2.0) + (b.z - a.z).powf(2.0)).sqrt()
}
#[allow(dead_code)]
@@ -167,7 +178,7 @@
None
});
match (a, b) {
- (Some(a), Some(b)) => a.distance(&b),
+ (Some(a), Some(b)) => distance(&a, &b),
_ => 0.0,
}
}
@@ -212,23 +223,17 @@
mod tests {
use super::*;
- impl ProtoPosition {
- fn new_with_xyz(x: f32, y: f32, z: f32) -> Self {
- let mut p = ProtoPosition::new();
- p.x = x;
- p.y = y;
- p.z = z;
- p
- }
+ fn new_with_xyz(x: f32, y: f32, z: f32) -> ProtoPosition {
+ ProtoPosition { x, y, z, ..Default::default() }
}
#[test]
fn test_distance() {
// Pythagorean quadruples
- let a = ProtoPosition::new_with_xyz(0.0, 0.0, 0.0);
- let mut b = ProtoPosition::new_with_xyz(1.0, 2.0, 2.0);
- assert_eq!(a.distance(&b), 3.0);
- b = ProtoPosition::new_with_xyz(2.0, 3.0, 6.0);
- assert_eq!(a.distance(&b), 7.0);
+ let a = new_with_xyz(0.0, 0.0, 0.0);
+ let mut b = new_with_xyz(1.0, 2.0, 2.0);
+ assert_eq!(distance(&a, &b), 3.0);
+ b = new_with_xyz(2.0, 3.0, 6.0);
+ assert_eq!(distance(&a, &b), 7.0);
}
}
diff --git a/rust/netsim-cxx/src/devices/facades.rs b/rust/netsim-cxx/src/devices/facades.rs
index 802ac6e..27c6292 100644
--- a/rust/netsim-cxx/src/devices/facades.rs
+++ b/rust/netsim-cxx/src/devices/facades.rs
@@ -1,5 +1,19 @@
-use crate::proto::model::chip::Bluetooth as ProtoBluetoothChip;
-use crate::proto::model::chip::Radio as ProtoRadioChip;
+// Copyright 2023 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use frontend_proto::model::chip::Bluetooth as ProtoBluetoothChip;
+use frontend_proto::model::chip::Radio as ProtoRadioChip;
use super::device::DeviceIdentifier;
diff --git a/rust/netsim-cxx/src/devices/id_factory.rs b/rust/netsim-cxx/src/devices/id_factory.rs
index 1ba4123..554cf2f 100644
--- a/rust/netsim-cxx/src/devices/id_factory.rs
+++ b/rust/netsim-cxx/src/devices/id_factory.rs
@@ -1,3 +1,17 @@
+// Copyright 2023 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
/// A factory for generating typed identifiers.
///
use std::ops::Add;
diff --git a/rust/netsim-cxx/src/devices/mod.rs b/rust/netsim-cxx/src/devices/mod.rs
index 5da3da7..94f7b6a 100644
--- a/rust/netsim-cxx/src/devices/mod.rs
+++ b/rust/netsim-cxx/src/devices/mod.rs
@@ -1,3 +1,17 @@
+// Copyright 2023 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
pub mod chip;
pub mod device;
pub mod devices_handler;
diff --git a/rust/netsim-cxx/src/lib.rs b/rust/netsim-cxx/src/lib.rs
index 06e363c..22cb7c9 100644
--- a/rust/netsim-cxx/src/lib.rs
+++ b/rust/netsim-cxx/src/lib.rs
@@ -17,6 +17,7 @@
#![allow(dead_code)]
mod captures;
+mod devices;
mod http_server;
mod ranging;
mod transport;