diff --git a/fastpair/rust/Cargo.toml b/fastpair/rust/Cargo.toml new file mode 100644 index 00000000..be7a0a1c --- /dev/null +++ b/fastpair/rust/Cargo.toml @@ -0,0 +1,22 @@ +# Copyright 2020 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. + +[package] +name = "fastpair" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/fastpair/rust/src/main.rs b/fastpair/rust/src/main.rs new file mode 100644 index 00000000..548089bc --- /dev/null +++ b/fastpair/rust/src/main.rs @@ -0,0 +1,20 @@ +// Copyright 2020 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. + +mod message_stream; +mod types; + +fn main() { + println!("Fastpair Rust!"); +} diff --git a/fastpair/rust/src/message_stream.rs b/fastpair/rust/src/message_stream.rs new file mode 100644 index 00000000..e7266fe4 --- /dev/null +++ b/fastpair/rust/src/message_stream.rs @@ -0,0 +1,52 @@ +// Copyright 2020 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. + +// https://source.corp.google.com/piper///depot/google3/third_party/nearby/fastpair/message_stream/message_stream.h +use crate::types::packets::{MessageGroup, MessageStreamPacket}; + +struct BatteryInfo { + is_charging: bool, +} + +pub struct MessageStream { + battery_info: BatteryInfo, +} + +impl MessageStream { + pub fn on_received(&mut self, packet: MessageStreamPacket) { + match packet.group { + MessageGroup::Bluetooth(b) => println!("It's Bluetooth {:?}", b), + MessageGroup::CompanionAppEvent(c) => println!("It's CompanionAppEvent :{:?}", c), + _ => println!("whatever else"), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::types::packets::{BluetoothCode, MessageGroup, MessageStreamPacket}; + + #[test] + fn test_on_received() { + let packet = MessageStreamPacket { + group: MessageGroup::Bluetooth(BluetoothCode::DisableSilenceMode), + additional_data: vec![0, 1, 2], + }; + let mut message = MessageStream { + battery_info: BatteryInfo { is_charging: false } + }; + message.on_received(packet); + } +} diff --git a/fastpair/rust/src/types.rs b/fastpair/rust/src/types.rs new file mode 100644 index 00000000..b0a402b9 --- /dev/null +++ b/fastpair/rust/src/types.rs @@ -0,0 +1,15 @@ +// Copyright 2020 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 packets; \ No newline at end of file diff --git a/fastpair/rust/src/types/packets.rs b/fastpair/rust/src/types/packets.rs new file mode 100644 index 00000000..42e444e4 --- /dev/null +++ b/fastpair/rust/src/types/packets.rs @@ -0,0 +1,87 @@ +// 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 +// +// http://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. + +// https://source.corp.google.com/piper///depot/google3/third_party/nearby/fastpair/message_stream/message.h + +#[derive(Debug)] +pub enum BluetoothCode { + EnableSilenceMode = 0x01, + DisableSilenceMode = 0x02, +} + +#[derive(Debug)] +pub enum CompanionAppEventCode { + LogBufferFull = 0x01, +} + +#[derive(Debug)] +pub enum DeviceInformationEventCode { + ModelId = 0x01, + BleAddressUpdated = 0x02, + BatteryUpdated = 0x03, + RemainingBattery = 0x04, + ActiveComponentsRequest = 0x05, + ActiveComponentsResponse = 0x06, + Capabilities = 0x07, + PlatformType = 0x08, + SessionNonce = 0x0A, +} + +#[derive(Debug)] +pub enum SassCode { + Acknowledgement = 0xFF, + SassGetCapability = 0x10, + SassNotifyCapability = 0x11, + SassSetMultipointState = 0x12, + SassSetSwitchingPreference = 0x20, + SassGetSwitchingPreference = 0x21, + SassNotifySwitchingPreference = 0x22, + SassSwitchActiveSourceCode = 0x30, + SassSwitchBackAudioSource = 0x31, + SassNotifyMultipointSwitchEvent = 0x32, + SassGetConnectionStatus = 0x33, + SassNotifyConnectionStatus = 0x34, + SassNotifySassInitiatedConnection = 0x40, + SassInUseAccountKey = 0x41, + SassSendCustomData = 0x42, + SassSetDropConnectionTarget = 0x43, +} + +#[derive(Debug)] +pub enum DeviceActionEventCode { + Ring = 1, +} + +#[derive(Debug)] +pub enum AcknowledgementCode { + Ack = 1, + Nack = 2, +} + +#[derive(Debug)] +pub enum MessageGroup { + Bluetooth(BluetoothCode), + CompanionAppEvent(CompanionAppEventCode), + DeviceInformationEvent(DeviceInformationEventCode), + DeviceActionEvent(DeviceActionEventCode), + Sass(SassCode), + Acknowledgement(AcknowledgementCode), +} + + +/// A packet that is sent over the RFCOMM Message Stream. +pub struct MessageStreamPacket { + pub group: MessageGroup, + pub additional_data: Vec, +} \ No newline at end of file