This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 305
/
BluetoothScoManager.cpp
291 lines (237 loc) · 6.65 KB
/
BluetoothScoManager.cpp
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "base/basictypes.h"
#include "BluetoothScoManager.h"
#include "BluetoothReplyRunnable.h"
#include "BluetoothService.h"
#include "BluetoothUtils.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
#include "nsContentUtils.h"
#include "nsIAudioManager.h"
#include "nsIObserverService.h"
#define BLUETOOTH_SCO_STATUS_CHANGED "bluetooth-sco-status-changed"
using namespace mozilla;
using namespace mozilla::ipc;
USING_BLUETOOTH_NAMESPACE
class mozilla::dom::bluetooth::BluetoothScoManagerObserver : public nsIObserver
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
BluetoothScoManagerObserver()
{
}
bool Init()
{
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
MOZ_ASSERT(obs);
if (NS_FAILED(obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false))) {
NS_WARNING("Failed to add shutdown observer!");
return false;
}
return true;
}
bool Shutdown()
{
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
if (!obs ||
(NS_FAILED(obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID)))) {
NS_WARNING("Can't unregister observers!");
return false;
}
return true;
}
~BluetoothScoManagerObserver()
{
Shutdown();
}
};
void
BluetoothScoManager::NotifyAudioManager(const nsAString& aAddress) {
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1");
if (!obs) {
NS_WARNING("Failed to get observser service!");
return;
}
if (aAddress.IsEmpty()) {
if (NS_FAILED(obs->NotifyObservers(nullptr, BLUETOOTH_SCO_STATUS_CHANGED, nullptr))) {
NS_WARNING("Failed to notify bluetooth-sco-status-changed observsers!");
return;
}
} else {
if (NS_FAILED(obs->NotifyObservers(nullptr, BLUETOOTH_SCO_STATUS_CHANGED, aAddress.BeginReading()))) {
NS_WARNING("Failed to notify bluetooth-sco-status-changed observsers!");
return;
}
}
nsCOMPtr<nsIAudioManager> am = do_GetService("@mozilla.org/telephony/audiomanager;1");
if (!am) {
NS_WARNING("Failed to get AudioManager service!");
return;
}
am->SetForceForUse(am->USE_COMMUNICATION, am->FORCE_BT_SCO);
}
NS_IMPL_ISUPPORTS1(BluetoothScoManagerObserver, nsIObserver)
namespace {
StaticRefPtr<BluetoothScoManager> gBluetoothScoManager;
StaticRefPtr<BluetoothScoManagerObserver> sScoObserver;
bool gInShutdown = false;
} // anonymous namespace
NS_IMETHODIMP
BluetoothScoManagerObserver::Observe(nsISupports* aSubject,
const char* aTopic,
const PRUnichar* aData)
{
MOZ_ASSERT(gBluetoothScoManager);
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
return gBluetoothScoManager->HandleShutdown();
}
MOZ_ASSERT(false, "BluetoothScoManager got unexpected topic!");
return NS_ERROR_UNEXPECTED;
}
BluetoothScoManager::BluetoothScoManager()
{
}
bool
BluetoothScoManager::Init()
{
mSocketStatus = GetConnectionStatus();
sScoObserver = new BluetoothScoManagerObserver();
if (sScoObserver->Init()) {
NS_WARNING("Cannot set up SCO observers!");
}
return true;
}
BluetoothScoManager::~BluetoothScoManager()
{
Cleanup();
}
void
BluetoothScoManager::Cleanup()
{
sScoObserver->Shutdown();
sScoObserver = nullptr;
}
//static
BluetoothScoManager*
BluetoothScoManager::Get()
{
MOZ_ASSERT(NS_IsMainThread());
// If we already exist, exit early
if (gBluetoothScoManager) {
return gBluetoothScoManager;
}
// If we're in shutdown, don't create a new instance
if (gInShutdown) {
NS_WARNING("BluetoothScoManager can't be created during shutdown");
return nullptr;
}
// Create new instance, register, return
nsRefPtr<BluetoothScoManager> manager = new BluetoothScoManager();
NS_ENSURE_TRUE(manager, nullptr);
if (!manager->Init()) {
return nullptr;
}
gBluetoothScoManager = manager;
return gBluetoothScoManager;
}
// Virtual function of class SocketConsumer
void
BluetoothScoManager::ReceiveSocketData(mozilla::ipc::UnixSocketRawData* aMessage)
{
// SCO socket do nothing here
MOZ_NOT_REACHED("This should never be called!");
}
nsresult
BluetoothScoManager::HandleShutdown()
{
MOZ_ASSERT(NS_IsMainThread());
gInShutdown = true;
CloseSocket();
gBluetoothScoManager = nullptr;
return NS_OK;
}
bool
BluetoothScoManager::Connect(const nsAString& aDeviceAddress)
{
MOZ_ASSERT(NS_IsMainThread());
if (gInShutdown) {
MOZ_ASSERT(false, "Connect called while in shutdown!");
return false;
}
if (GetConnectionStatus() == SocketConnectionStatus::SOCKET_CONNECTED) {
NS_WARNING("Sco socket has been connected");
return false;
}
CloseSocket();
BluetoothService* bs = BluetoothService::Get();
if (!bs) {
NS_WARNING("BluetoothService not available!");
return false;
}
nsresult rv = bs->GetScoSocket(aDeviceAddress,
true,
false,
this);
return NS_FAILED(rv) ? false : true;
}
bool
BluetoothScoManager::Listen()
{
MOZ_ASSERT(NS_IsMainThread());
if (gInShutdown) {
MOZ_ASSERT(false, "Listen called while in shutdown!");
return false;
}
CloseSocket();
BluetoothService* bs = BluetoothService::Get();
if (!bs) {
NS_WARNING("BluetoothService not available!");
return false;
}
nsresult rv = bs->ListenSocketViaService(-1,
BluetoothSocketType::SCO,
true,
true,
this);
mSocketStatus = GetConnectionStatus();
return NS_FAILED(rv) ? false : true;
}
void
BluetoothScoManager::Disconnect()
{
if (GetConnectionStatus() == SocketConnectionStatus::SOCKET_DISCONNECTED) {
return;
}
CloseSocket();
}
void
BluetoothScoManager::OnConnectSuccess()
{
nsString address;
GetSocketAddr(address);
NotifyAudioManager(address);
mSocketStatus = GetConnectionStatus();
}
void
BluetoothScoManager::OnConnectError()
{
CloseSocket();
mSocketStatus = GetConnectionStatus();
Listen();
}
void
BluetoothScoManager::OnDisconnect()
{
if (mSocketStatus == SocketConnectionStatus::SOCKET_CONNECTED) {
Listen();
nsString address = NS_LITERAL_STRING("");
NotifyAudioManager(address);
}
}