This commit is contained in:
Suet-fei Li
2023-05-27 01:08:14 +00:00
parent 4527c42480
commit 9e37ea08be
5 changed files with 196 additions and 0 deletions
+22
View File
@@ -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]
+20
View File
@@ -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!");
}
+52
View File
@@ -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);
}
}
+15
View File
@@ -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;
+87
View File
@@ -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<u8>,
}