mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 07:06:11 -04:00
171 lines
5.7 KiB
C++
171 lines
5.7 KiB
C++
// 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.
|
|
|
|
#include "fastpair/fast_pair_controller.h"
|
|
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "absl/status/status.h"
|
|
#include "fastpair/common/protocol.h"
|
|
#include "fastpair/handshake/fast_pair_data_encryptor_impl.h"
|
|
#include "fastpair/message_stream/message_stream.h"
|
|
#include "internal/platform/bluetooth_adapter.h"
|
|
#include "internal/platform/bluetooth_classic.h"
|
|
|
|
namespace nearby {
|
|
namespace fastpair {
|
|
|
|
FastPairController::FastPairController(Mediums* mediums,
|
|
const BluetoothDevice& device)
|
|
: mediums_(mediums), device_(Protocol::kFastPairRetroactivePairing) {
|
|
device_.SetPublicAddress(device.GetMacAddress());
|
|
}
|
|
|
|
absl::Status FastPairController::OpenMessageStream() {
|
|
message_stream_ = std::make_unique<MessageStream>(
|
|
device_, &mediums_->GetBluetoothClassic().GetMedium(), *this);
|
|
return message_stream_->OpenRfcomm();
|
|
}
|
|
|
|
void FastPairController::AddMessageStreamConnectionObserver(
|
|
MessageStreamConnectionObserver* observer) {
|
|
connection_observers_.AddObserver(observer);
|
|
if (message_stream_status_.ok() && observer->on_connected != nullptr) {
|
|
observer->on_connected(device_);
|
|
} else if (!message_stream_status_.ok() &&
|
|
observer->on_disconnected != nullptr) {
|
|
observer->on_disconnected(device_, message_stream_status_);
|
|
}
|
|
}
|
|
|
|
void FastPairController::RemoveMessageStreamConnectionObserver(
|
|
MessageStreamConnectionObserver* observer) {
|
|
connection_observers_.RemoveObserver(observer);
|
|
}
|
|
|
|
void FastPairController::AddAddressRotationObserver(
|
|
BleAddressRotationObserver* observer) {
|
|
address_rotation_observers_.AddObserver(observer);
|
|
if (!device_.GetBleAddress().empty()) {
|
|
observer->on_ble_address_rotated(device_, device_.GetBleAddress());
|
|
}
|
|
}
|
|
|
|
void FastPairController::RemoveAddressRotationObserver(
|
|
BleAddressRotationObserver* observer) {
|
|
address_rotation_observers_.RemoveObserver(observer);
|
|
}
|
|
|
|
void FastPairController::AddModelIdObserver(ModelIdObserver* observer) {
|
|
model_id_observers_.AddObserver(observer);
|
|
if (!device_.GetModelId().empty()) {
|
|
observer->on_model_id(device_);
|
|
}
|
|
}
|
|
|
|
void FastPairController::RemoveModelIdObserver(ModelIdObserver* observer) {
|
|
model_id_observers_.RemoveObserver(observer);
|
|
}
|
|
|
|
Future<std::shared_ptr<FastPairDataEncryptor>>
|
|
FastPairController::GetDataEncryptor() {
|
|
if (!encryptor_) {
|
|
encryptor_ =
|
|
std::make_unique<Future<std::shared_ptr<FastPairDataEncryptor>>>();
|
|
FastPairDataEncryptorImpl::Factory::CreateAsync(
|
|
device_, [borrowable = lender_.GetBorrowable()](
|
|
std::unique_ptr<FastPairDataEncryptor> encryptor) mutable {
|
|
auto borrowed = borrowable.Borrow();
|
|
if (borrowed) {
|
|
(*borrowed)->SetDataEncryptor(std::move(encryptor));
|
|
}
|
|
});
|
|
}
|
|
return *encryptor_;
|
|
}
|
|
|
|
Future<FastPairController::GattClientRef>
|
|
FastPairController::GetGattClientRef() {
|
|
Future<FastPairController::GattClientRef> result;
|
|
if (gatt_client_ == nullptr) {
|
|
gatt_client_ref_count_ = 0;
|
|
gatt_client_ =
|
|
FastPairGattServiceClientImpl::Factory::Create(device_, *mediums_);
|
|
gatt_client_->InitializeGattConnection(
|
|
[](std::optional<PairFailure> result) {
|
|
if (result.has_value()) {
|
|
NEARBY_LOGS(INFO) << "InitializeGattConnection: " << result.value();
|
|
} else {
|
|
NEARBY_LOGS(INFO) << "InitializeGattConnection: OK";
|
|
}
|
|
});
|
|
}
|
|
result.Set(GattClientRef(this));
|
|
return result;
|
|
}
|
|
|
|
// MessageStream::Observer callbacks
|
|
void FastPairController::OnConnectionResult(absl::Status result) {
|
|
NEARBY_LOGS(INFO) << "OnConnectionResult " << result;
|
|
message_stream_status_ = result;
|
|
if (!result.ok()) {
|
|
NEARBY_LOGS(INFO) << "Connection attempt failed with " << result;
|
|
return;
|
|
}
|
|
for (auto* observer : connection_observers_.GetObservers()) {
|
|
if (observer->on_connected != nullptr) {
|
|
observer->on_connected(device_);
|
|
}
|
|
}
|
|
}
|
|
void FastPairController::OnDisconnected(absl::Status status) {
|
|
NEARBY_LOGS(INFO) << "OnDisconnected " << status;
|
|
message_stream_status_ = status;
|
|
for (auto* observer : connection_observers_.GetObservers()) {
|
|
if (observer->on_disconnected != nullptr) {
|
|
observer->on_disconnected(device_, status);
|
|
}
|
|
}
|
|
}
|
|
void FastPairController::OnEnableSilenceMode(bool enable) {}
|
|
void FastPairController::OnLogBufferFull() {}
|
|
void FastPairController::OnModelId(absl::string_view model_id) {
|
|
device_.SetModelId(model_id);
|
|
for (auto* observer : model_id_observers_.GetObservers()) {
|
|
observer->on_model_id(device_);
|
|
}
|
|
}
|
|
|
|
void FastPairController::OnBleAddressUpdated(absl::string_view address) {
|
|
std::string old_address = std::string(device_.GetBleAddress());
|
|
device_.SetBleAddress(address);
|
|
for (auto* observer : address_rotation_observers_.GetObservers()) {
|
|
observer->on_ble_address_rotated(device_, old_address);
|
|
}
|
|
}
|
|
|
|
void FastPairController::OnBatteryUpdated(
|
|
std::vector<MessageStream::BatteryInfo> battery_levels) {}
|
|
void FastPairController::OnRemainingBatteryTime(absl::Duration duration) {}
|
|
bool FastPairController::OnRing(uint8_t components, absl::Duration duration) {
|
|
return false;
|
|
}
|
|
|
|
} // namespace fastpair
|
|
} // namespace nearby
|