forked from SpriteOvO/AirPodsDesktop
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAirPods.h
More file actions
241 lines (193 loc) · 7.52 KB
/
AirPods.h
File metadata and controls
241 lines (193 loc) · 7.52 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// AirPodsWindows - AirPods Desktop User Experience Enhancement Program.
// Copyright (C) 2021-2022 SpriteOvO
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
#pragma once
#include <optional>
#include <functional>
#include "Bluetooth.h"
#include "AppleCP.h"
#include "AAP.h"
#include "AAPManager.h"
namespace Core::AirPods {
//
// Structures
//
namespace Details {
struct BasicState {
Battery battery;
bool isCharging{false};
bool operator==(const BasicState &rhs) const = default;
};
} // namespace Details
struct PodState : Details::BasicState {
bool isInEar{false};
bool operator==(const PodState &rhs) const = default;
};
struct CaseState : Details::BasicState {
bool isBothPodsInCase{false};
bool isLidOpened{false};
bool operator==(const CaseState &rhs) const = default;
};
struct PodsState {
PodState left, right;
bool operator==(const PodsState &rhs) const = default;
};
struct State {
Model model{Model::Unknown};
PodsState pods;
CaseState caseBox;
QString displayName;
// AAP Protocol states (for AirPods Pro and Max with ANC support)
std::optional<AAP::NoiseControlMode> noiseControlMode;
std::optional<AAP::ConversationalAwarenessState> conversationalAwareness;
std::optional<AAP::PersonalizedVolumeState> personalizedVolume;
std::optional<AAP::LoudSoundReductionState> loudSoundReduction;
std::optional<bool> automaticEarDetectionEnabled;
std::optional<uint8_t> adaptiveTransparencyLevel;
bool operator==(const State &rhs) const = default;
};
//
// Classes
//
namespace Details {
class Advertisement
{
public:
using AddressType = decltype(Bluetooth::AdvertisementWatcher::ReceivedData::address);
struct AdvState : AirPods::State {
Side side;
};
static bool IsDesiredAdv(const Bluetooth::AdvertisementWatcher::ReceivedData &data);
Advertisement(const Bluetooth::AdvertisementWatcher::ReceivedData &data);
int16_t GetRssi() const;
const auto &GetTimestamp() const;
AddressType GetAddress() const;
std::vector<uint8_t> GetDesensitizedData() const;
const AdvState &GetAdvState() const;
private:
Bluetooth::AdvertisementWatcher::ReceivedData _data;
AppleCP::AirPods _protocol;
AdvState _state;
const std::vector<uint8_t> &GetMfrData() const;
};
// AirPods use Random Non-resolvable device addresses for privacy reasons. This means we
// can't "Remember" the user's AirPods by any device property. Here we track our desired
// devices in some non-elegant ways, but obviously it is sometimes unreliable.
//
class StateManager
{
public:
struct UpdateEvent {
std::optional<State> oldState;
State newState;
};
StateManager();
std::optional<State> GetCurrentState() const;
std::optional<UpdateEvent> OnAdvReceived(Advertisement adv);
void Disconnect();
void OnRssiMinChanged(int16_t rssiMin);
private:
using Clock = std::chrono::steady_clock;
using Timestamp = std::chrono::time_point<Clock>;
mutable std::mutex _mutex;
Helper::Timer _lostTimer;
Helper::Sides<Helper::Timer> _stateResetTimer;
Helper::Sides<std::optional<std::pair<Advertisement, Timestamp>>> _adv;
std::optional<State> _cachedState;
int16_t _rssiMin{std::numeric_limits<int16_t>::max()};
bool IsPossibleDesiredAdv(const Advertisement &adv) const;
void UpdateAdv(Advertisement adv);
std::optional<UpdateEvent> UpdateState();
void ResetAll();
void DoLost();
void DoStateReset(Side side);
};
} // namespace Details
class Manager
{
public:
Manager();
void StartScanner();
void StopScanner();
void OnRssiMinChanged(int16_t rssiMin);
void OnAutomaticEarDetectionChanged(bool enable);
void OnBoundDeviceAddressChanged(uint64_t address);
void OnConversationalAwarenessChanged(bool enable);
void OnConversationalAwarenessVolumePercentChanged(uint8_t percent);
void OnPersonalizedVolumeChanged(bool enable);
void OnLoudSoundReductionChanged(bool enable);
void OnAdaptiveTransparencyLevelChanged(uint8_t level);
void OnNoiseControlModeChanged(AAP::NoiseControlMode mode);
// AAP Protocol features
bool SetNoiseControlMode(AAP::NoiseControlMode mode);
std::optional<AAP::NoiseControlMode> GetNoiseControlMode() const;
bool SetConversationalAwareness(bool enable);
std::optional<AAP::ConversationalAwarenessState> GetConversationalAwarenessState() const;
bool SetPersonalizedVolume(bool enable);
std::optional<AAP::PersonalizedVolumeState> GetPersonalizedVolumeState() const;
bool SetLoudSoundReduction(bool enable);
std::optional<AAP::LoudSoundReductionState> GetLoudSoundReductionState() const;
bool SetAdaptiveTransparencyLevel(uint8_t level);
std::optional<uint8_t> GetAdaptiveTransparencyLevel() const;
bool SetAdaptiveNoiseLevel(uint8_t level);
bool IsAAPConnected() const;
// Head tracking
bool StartHeadTracking();
bool StopHeadTracking();
bool IsHeadTrackingActive() const;
// Check if the model supports ANC features
static bool SupportsANC(Model model);
private:
std::mutex _mutex;
Bluetooth::AdvertisementWatcher _adWatcher;
Details::StateManager _stateMgr;
std::optional<Bluetooth::Device> _boundDevice;
std::optional<Model> _modelOverride;
QString _deviceName;
bool _deviceConnected{false};
bool _automaticEarDetection{false};
bool _conversationalAwarenessEnabled{false};
uint8_t _conversationalAwarenessVolumePercent{40};
bool _personalizedVolumeEnabled{false};
bool _loudSoundReductionEnabled{false};
uint8_t _adaptiveTransparencyLevel{25};
std::optional<AAP::NoiseControlMode> _currentNoiseControlMode;
// AAP Manager for L2CAP protocol communication
AAP::Manager _aapMgr;
void OnBoundDeviceConnectionStateChanged(Bluetooth::DeviceState state);
void OnStateChanged(Details::StateManager::UpdateEvent updateEvent);
void OnLidOpened(bool opened);
void OnBothInEar(bool isBothInEar);
bool OnAdvertisementReceived(const Bluetooth::AdvertisementWatcher::ReceivedData &data);
void OnAdvWatcherStateChanged(
Bluetooth::AdvertisementWatcher::State state, const std::optional<std::string> &optError);
// AAP callbacks
void OnNoiseControlModeNotification(AAP::NoiseControlMode mode);
void OnConversationalAwarenessStateChanged(AAP::ConversationalAwarenessState state);
void OnPersonalizedVolumeStateChanged(AAP::PersonalizedVolumeState state);
void OnLoudSoundReductionStateChanged(AAP::LoudSoundReductionState state);
void OnAdaptiveTransparencyLevelNotification(uint8_t level);
void OnSpeakingLevelChanged(AAP::SpeakingLevel level);
void OnEarDetectionChanged(AAP::EarStatus primary, AAP::EarStatus secondary);
void OnHeadTrackingData(AAP::HeadTrackingData data);
void OnAAPConnected();
void OnAAPDisconnected();
void SetupAAPCallbacks();
void ConnectAAP();
};
std::vector<Core::Bluetooth::Device> GetDevices();
} // namespace Core::AirPods