forked from google/nearby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_pair_controller.h
201 lines (181 loc) · 6.99 KB
/
fast_pair_controller.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// 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.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_FAST_PAIR_CONTROLLER_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_FAST_PAIR_CONTROLLER_H_
#include <atomic>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/functional/any_invocable.h"
#include "absl/status/status.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/handshake/fast_pair_data_encryptor.h"
#include "fastpair/handshake/fast_pair_gatt_service_client_impl.h"
#include "fastpair/message_stream/message_stream.h"
#include "internal/base/observer_list.h"
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/bluetooth_classic.h"
#include "internal/platform/borrowable.h"
#include "internal/platform/single_thread_executor.h"
namespace nearby {
namespace fastpair {
class FastPairController : public MessageStream::Observer {
public:
struct MessageStreamConnectionObserver {
// Callback triggered when a Message Stream connection is opened.
absl::AnyInvocable<void(const FastPairDevice& device)> on_connected;
// Callback triggered when a Message Stream connection is terminated.
absl::AnyInvocable<void(const FastPairDevice& device, absl::Status status)>
on_disconnected;
};
struct BleAddressRotationObserver {
absl::AnyInvocable<void(const FastPairDevice& device,
absl::string_view old_address)>
on_ble_address_rotated;
};
struct ModelIdObserver {
absl::AnyInvocable<void(const FastPairDevice& device)> on_model_id;
};
// RAII accessor to GattClient
class GattClientRef {
public:
GattClientRef() = default;
explicit GattClientRef(FastPairController* controller)
: controller_(controller) {
CHECK(controller_ != nullptr);
controller_->AcquireGattClient();
}
GattClientRef(const GattClientRef& other) {
controller_ = other.controller_;
if (controller_ != nullptr) {
controller_->AcquireGattClient();
}
}
GattClientRef(GattClientRef&& other) {
controller_ = other.controller_;
other.controller_ = nullptr;
}
GattClientRef& operator=(const GattClientRef& other) {
if (controller_ != nullptr) {
controller_->ReleaseGattClient();
}
controller_ = other.controller_;
if (controller_ != nullptr) {
controller_->AcquireGattClient();
}
return *this;
}
GattClientRef& operator=(GattClientRef&& other) {
if (controller_ != nullptr) {
controller_->ReleaseGattClient();
}
controller_ = other.controller_;
other.controller_ = nullptr;
return *this;
}
~GattClientRef() {
if (controller_ != nullptr) {
controller_->ReleaseGattClient();
}
}
FastPairGattServiceClient* operator->() {
CHECK(controller_ != nullptr);
return controller_->GetGattClient();
}
private:
FastPairController* controller_ = nullptr;
};
// Creates a device controller in retroactive pairing path.
FastPairController(Mediums* mediums, FastPairDevice* device,
SingleThreadExecutor* executor);
~FastPairController() override {
NEARBY_LOGS(INFO) << "Destroy FastPairController";
lender_.Release();
connection_observers_.Clear();
address_rotation_observers_.Clear();
model_id_observers_.Clear();
message_stream_.reset();
NEARBY_LOGS(INFO) << "Destroyed FastPairController";
}
absl::Status OpenMessageStream();
// Registers an observer for connection events. The observer must stay alive
// until it is removed.
// The callback is also called during registration with the current state
// of the connection.
void AddMessageStreamConnectionObserver(
MessageStreamConnectionObserver* observer);
void RemoveMessageStreamConnectionObserver(
MessageStreamConnectionObserver* observer);
// Registers an observer for BLE address rotation.
// The callback is also called during registration with the current BLE
// address unless FP does not know the device's address.
void AddAddressRotationObserver(BleAddressRotationObserver* observer);
void RemoveAddressRotationObserver(BleAddressRotationObserver* observer);
void AddModelIdObserver(ModelIdObserver* observer);
void RemoveModelIdObserver(ModelIdObserver* observer);
Future<std::shared_ptr<FastPairDataEncryptor>> GetDataEncryptor();
Future<GattClientRef> GetGattClientRef();
// MessageStream::Observer callbacks
void OnConnectionResult(absl::Status result) override;
void OnDisconnected(absl::Status status) override;
void OnEnableSilenceMode(bool enable) override;
void OnLogBufferFull() override;
void OnModelId(absl::string_view model_id) override;
void OnBleAddressUpdated(absl::string_view address) override;
void OnBatteryUpdated(
std::vector<MessageStream::BatteryInfo> battery_levels) override;
void OnRemainingBatteryTime(absl::Duration duration) override;
bool OnRing(uint8_t components, absl::Duration duration) override;
FastPairDevice& GetDevice() { return *device_; }
std::string GetSeekerMacAddress() const {
return mediums_->GetBluetoothClassic().GetMedium().GetMacAddress();
}
private:
void SetDataEncryptor(std::unique_ptr<FastPairDataEncryptor> encryptor) {
if (encryptor != nullptr) {
encryptor_->Set(std::move(encryptor));
} else {
encryptor_->SetException({Exception::kFailed});
}
}
void AcquireGattClient() { ++gatt_client_ref_count_; }
void ReleaseGattClient() {
if (--gatt_client_ref_count_ == 0) {
gatt_client_.reset();
}
}
FastPairGattServiceClient* GetGattClient() {
CHECK(gatt_client_ != nullptr);
return gatt_client_.get();
}
void CreateDataEncryptor();
Mediums* mediums_;
FastPairDevice* device_;
SingleThreadExecutor* executor_;
std::unique_ptr<Future<std::shared_ptr<FastPairDataEncryptor>>> encryptor_;
std::unique_ptr<MessageStream> message_stream_;
absl::Status message_stream_status_ =
absl::FailedPreconditionError("not connected");
ObserverList<MessageStreamConnectionObserver> connection_observers_;
ObserverList<BleAddressRotationObserver> address_rotation_observers_;
ObserverList<ModelIdObserver> model_id_observers_;
// TODO(jsobczak): Improve locking around gatt_client_
std::unique_ptr<FastPairGattServiceClient> gatt_client_;
std::atomic_int gatt_client_ref_count_;
Lender<FastPairController*> lender_{this};
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_FAST_PAIR_CONTROLLER_H_