1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "bluetooth_opp.h"
17 #include "bluetooth_host.h"
18 #include "bluetooth_profile_manager.h"
19 #include "bluetooth_log.h"
20 #include "bluetooth_observer_list.h"
21 #include "bluetooth_opp_observer_stub.h"
22 #include "bluetooth_utils.h"
23 #include "i_bluetooth_opp.h"
24 #include "i_bluetooth_host.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27
28 namespace OHOS {
29 namespace Bluetooth {
30 std::mutex g_oppProxyMutex;
BluetoothOppTransferInformation()31 BluetoothOppTransferInformation::BluetoothOppTransferInformation()
32 {}
33
~BluetoothOppTransferInformation()34 BluetoothOppTransferInformation::~BluetoothOppTransferInformation()
35 {}
36
GetId() const37 int BluetoothOppTransferInformation::GetId() const
38 {
39 return id_;
40 }
41
GetFileName() const42 std::string BluetoothOppTransferInformation::GetFileName() const
43 {
44 return fileName_;
45 }
46
GetFilePath() const47 std::string BluetoothOppTransferInformation::GetFilePath() const
48 {
49 return filePath_;
50 }
51
GetMimeType() const52 std::string BluetoothOppTransferInformation::GetMimeType() const
53 {
54 return mimeType_;
55 }
56
GetDeviceName() const57 std::string BluetoothOppTransferInformation::GetDeviceName() const
58 {
59 return deviceName_;
60 }
61
GetDeviceAddress() const62 std::string BluetoothOppTransferInformation::GetDeviceAddress() const
63 {
64 return deviceAddress_;
65 }
66
GetDirection() const67 int BluetoothOppTransferInformation::GetDirection() const
68 {
69 return direction_;
70 }
71
GetStatus() const72 int BluetoothOppTransferInformation::GetStatus() const
73 {
74 return status_;
75 }
76
GetFailedReason() const77 int BluetoothOppTransferInformation::GetFailedReason() const
78 {
79 return failedReason_;
80 }
81
GetTimeStamp() const82 uint64_t BluetoothOppTransferInformation::GetTimeStamp() const
83 {
84 return timeStamp_;
85 }
86
GetCurrentBytes() const87 uint64_t BluetoothOppTransferInformation::GetCurrentBytes() const
88 {
89 return currentBytes_;
90 }
91
GetTotalBytes() const92 uint64_t BluetoothOppTransferInformation::GetTotalBytes() const
93 {
94 return totalBytes_;
95 }
96
SetId(int id)97 void BluetoothOppTransferInformation::SetId(int id)
98 {
99 id_ = id;
100 }
101
SetFileName(std::string fileName)102 void BluetoothOppTransferInformation::SetFileName(std::string fileName)
103 {
104 fileName_ = fileName;
105 }
106
SetFilePath(std::string filePath)107 void BluetoothOppTransferInformation::SetFilePath(std::string filePath)
108 {
109 filePath_ = filePath;
110 }
111
SetMimeType(std::string mimeType)112 void BluetoothOppTransferInformation::SetMimeType(std::string mimeType)
113 {
114 mimeType_ = mimeType;
115 }
116
SetDeviceName(std::string deviceName)117 void BluetoothOppTransferInformation::SetDeviceName(std::string deviceName)
118 {
119 deviceName_ = deviceName;
120 }
121
SetDeviceAddress(std::string deviceAddress)122 void BluetoothOppTransferInformation::SetDeviceAddress(std::string deviceAddress)
123 {
124 deviceAddress_ = deviceAddress;
125 }
126
SetDirection(int direction)127 void BluetoothOppTransferInformation::SetDirection(int direction)
128 {
129 direction_ = direction;
130 }
131
SetStatus(int status)132 void BluetoothOppTransferInformation::SetStatus(int status)
133 {
134 status_ = status;
135 }
136
SetFailedReason(int failedReason)137 void BluetoothOppTransferInformation::SetFailedReason(int failedReason)
138 {
139 failedReason_ = failedReason;
140 }
141
SetTimeStamp(uint64_t timeStamp)142 void BluetoothOppTransferInformation::SetTimeStamp(uint64_t timeStamp)
143 {
144 timeStamp_ = timeStamp;
145 }
146
SetCurrentBytes(uint64_t currentBytes)147 void BluetoothOppTransferInformation::SetCurrentBytes(uint64_t currentBytes)
148 {
149 currentBytes_ = currentBytes;
150 }
151
SetTotalBytes(uint64_t totalBytes)152 void BluetoothOppTransferInformation::SetTotalBytes(uint64_t totalBytes)
153 {
154 totalBytes_ = totalBytes;
155 }
156
157 class OppInnerObserver : public BluetoothOppObserverStub {
158 public:
OppInnerObserver(BluetoothObserverList<OppObserver> & observers)159 explicit OppInnerObserver(BluetoothObserverList<OppObserver> &observers) : observers_(observers)
160 {
161 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
162 }
~OppInnerObserver()163 ~OppInnerObserver() override
164 {
165 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
166 }
167
OnReceiveIncomingFileChanged(const BluetoothIOppTransferInformation & transferInformation)168 ErrCode OnReceiveIncomingFileChanged(const BluetoothIOppTransferInformation &transferInformation) override
169 {
170 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
171 BluetoothOppTransferInformation oppTransferinformation;
172 oppTransferinformation.SetId(transferInformation.GetId());
173 oppTransferinformation.SetFileName(transferInformation.GetFileName());
174 oppTransferinformation.SetFilePath(transferInformation.GetFilePath());
175 oppTransferinformation.SetMimeType(transferInformation.GetFileType());
176 oppTransferinformation.SetDeviceName(transferInformation.GetDeviceName());
177 oppTransferinformation.SetDeviceAddress(transferInformation.GetDeviceAddress());
178 oppTransferinformation.SetFailedReason(transferInformation.GetFailedReason());
179 oppTransferinformation.SetStatus(transferInformation.GetStatus());
180 oppTransferinformation.SetDirection(transferInformation.GetDirection());
181 oppTransferinformation.SetTimeStamp(transferInformation.GetTimeStamp());
182 oppTransferinformation.SetCurrentBytes(transferInformation.GetCurrentBytes());
183 oppTransferinformation.SetTotalBytes(transferInformation.GetTotalBytes());
184 observers_.ForEach([oppTransferinformation](std::shared_ptr<OppObserver> observer) {
185 observer->OnReceiveIncomingFileChanged(oppTransferinformation);
186 });
187 return NO_ERROR;
188 }
OnTransferStateChanged(const BluetoothIOppTransferInformation & transferInformation)189 ErrCode OnTransferStateChanged(const BluetoothIOppTransferInformation &transferInformation) override
190 {
191 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
192 BluetoothOppTransferInformation oppTransferinformation;
193 oppTransferinformation.SetId(transferInformation.GetId());
194 oppTransferinformation.SetFileName(transferInformation.GetFileName());
195 oppTransferinformation.SetFilePath(transferInformation.GetFilePath());
196 oppTransferinformation.SetMimeType(transferInformation.GetFileType());
197 oppTransferinformation.SetDeviceName(transferInformation.GetDeviceName());
198 oppTransferinformation.SetDeviceAddress(transferInformation.GetDeviceAddress());
199 oppTransferinformation.SetFailedReason(transferInformation.GetFailedReason());
200 oppTransferinformation.SetStatus(transferInformation.GetStatus());
201 oppTransferinformation.SetDirection(transferInformation.GetDirection());
202 oppTransferinformation.SetTimeStamp(transferInformation.GetTimeStamp());
203 oppTransferinformation.SetCurrentBytes(transferInformation.GetCurrentBytes());
204 oppTransferinformation.SetTotalBytes(transferInformation.GetTotalBytes());
205 observers_.ForEach([oppTransferinformation](std::shared_ptr<OppObserver> observer) {
206 observer->OnTransferStateChanged(oppTransferinformation);
207 });
208 return NO_ERROR;
209 }
210
211 private:
212 BluetoothObserverList<OppObserver> &observers_;
213 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(OppInnerObserver);
214 };
215
216 struct Opp::impl {
217 impl();
218 ~impl();
219
GetDevicesByStatesOHOS::Bluetooth::Opp::impl220 std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states)
221 {
222 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
223 std::vector<BluetoothRemoteDevice> remoteDevices;
224 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
225 if (proxy != nullptr && IS_BT_ENABLED()) {
226 std::vector<BluetoothRawAddress> rawDevices;
227 std::vector<int32_t> tmpStates;
228 for (int state : states) {
229 tmpStates.push_back((int32_t)state);
230 }
231
232 proxy->GetDevicesByStates(tmpStates, rawDevices);
233 for (BluetoothRawAddress rawDevice : rawDevices) {
234 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
235 remoteDevices.push_back(remoteDevice);
236 }
237 }
238 return remoteDevices;
239 }
240
GetDeviceStateOHOS::Bluetooth::Opp::impl241 int GetDeviceState(const BluetoothRemoteDevice &device)
242 {
243 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
244 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
245 if (proxy != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
246 int state;
247 proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
248 return state;
249 }
250 return static_cast<int>(BTConnectState::DISCONNECTED);
251 }
252
SendFileOHOS::Bluetooth::Opp::impl253 bool SendFile(std::string device, std::vector<std::string>filePaths, std::vector<std::string>mimeTypes)
254 {
255 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
256 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
257 if (proxy != nullptr && IS_BT_ENABLED()) {
258 bool isOk;
259 proxy->SendFile(device, filePaths, mimeTypes, isOk);
260 return isOk;
261 }
262 HILOGE("[%{public}s]: %{public}s(): fw return false!", __FILE__, __FUNCTION__);
263 return false;
264 }
265
SetIncomingFileConfirmationOHOS::Bluetooth::Opp::impl266 bool SetIncomingFileConfirmation(bool accept)
267 {
268 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
269 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
270 if (proxy != nullptr && IS_BT_ENABLED()) {
271 bool isOk;
272 proxy->SetIncomingFileConfirmation(accept, isOk);
273 return isOk;
274 }
275 HILOGE("[%{public}s]: %{public}s(): fw return false!", __FILE__, __FUNCTION__);
276 return false;
277 }
278
GetCurrentTransferInformationOHOS::Bluetooth::Opp::impl279 BluetoothOppTransferInformation GetCurrentTransferInformation()
280 {
281 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
282 BluetoothIOppTransferInformation oppInformation;
283 BluetoothOppTransferInformation transferInformation;
284 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
285 if (proxy != nullptr && IS_BT_ENABLED()) {
286 proxy->GetCurrentTransferInformation(oppInformation);
287 transferInformation.SetId(oppInformation.GetId());
288 transferInformation.SetFileName(oppInformation.GetFileName());
289 transferInformation.SetFilePath(oppInformation.GetFilePath());
290 transferInformation.SetMimeType(oppInformation.GetFileType());
291 transferInformation.SetDeviceName(oppInformation.GetDeviceName());
292 transferInformation.SetDeviceAddress(oppInformation.GetDeviceAddress());
293 transferInformation.SetFailedReason(oppInformation.GetFailedReason());
294 transferInformation.SetStatus(oppInformation.GetStatus());
295 transferInformation.SetDirection(oppInformation.GetDirection());
296 transferInformation.SetTimeStamp(oppInformation.GetTimeStamp());
297 transferInformation.SetCurrentBytes(oppInformation.GetCurrentBytes());
298 transferInformation.SetTotalBytes(oppInformation.GetTotalBytes());
299 }
300 return transferInformation;
301 }
302
CancelTransferOHOS::Bluetooth::Opp::impl303 bool CancelTransfer()
304 {
305 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
306 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
307 if (proxy != nullptr && IS_BT_ENABLED()) {
308 bool isOk;
309 proxy->CancelTransfer(isOk);
310 return isOk;
311 }
312 HILOGE("[%{public}s]: %{public}s(): fw return false!", __FILE__, __FUNCTION__);
313 return false;
314 }
315
RegisterObserverOHOS::Bluetooth::Opp::impl316 void RegisterObserver(std::shared_ptr<OppObserver> observer)
317 {
318 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
319 observers_.Register(observer);
320 }
321
DeregisterObserverOHOS::Bluetooth::Opp::impl322 void DeregisterObserver(std::shared_ptr<OppObserver> observer)
323 {
324 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
325 observers_.Deregister(observer);
326 }
327
328 int32_t profileRegisterId = 0;
329 private:
330 BluetoothObserverList<OppObserver> observers_;
331 sptr<OppInnerObserver> innerObserver_;
332 };
333
impl()334 Opp::impl::impl()
335 {
336 innerObserver_ = new OppInnerObserver(observers_);
337 profileRegisterId = DelayedSingleton<BluetoothProfileManager>::GetInstance()->RegisterFunc(PROFILE_OPP_SERVER,
338 [this](sptr<IRemoteObject> remote) {
339 sptr<IBluetoothOpp> proxy = iface_cast<IBluetoothOpp>(remote);
340 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
341 proxy->RegisterObserver(innerObserver_);
342 });
343 }
344
~impl()345 Opp::impl::~impl()
346 {
347 HILOGD("start");
348 DelayedSingleton<BluetoothProfileManager>::GetInstance()->DeregisterFunc(profileRegisterId);
349 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
350 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
351 proxy->DeregisterObserver(innerObserver_);
352 }
353
Opp()354 Opp::Opp()
355 {
356 pimpl = std::make_unique<impl>();
357 }
358
~Opp()359 Opp::~Opp()
360 {}
361
GetProfile()362 Opp *Opp::GetProfile()
363 {
364 static Opp instance;
365 return &instance;
366 }
GetDevicesByStates(std::vector<int> states)367 std::vector<BluetoothRemoteDevice> Opp::GetDevicesByStates(std::vector<int> states)
368 {
369 if (!IS_BT_ENABLED()) {
370 HILOGE("bluetooth is off.");
371 return std::vector<BluetoothRemoteDevice>();
372 }
373 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
374 CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
375 std::vector<BluetoothRemoteDevice>(), "failed: no proxy");
376
377 return pimpl->GetDevicesByStates(states);
378 }
379
GetDeviceState(const BluetoothRemoteDevice & device)380 int Opp::GetDeviceState(const BluetoothRemoteDevice &device)
381 {
382 if (!IS_BT_ENABLED()) {
383 HILOGE("bluetooth is off.");
384 return static_cast<int>(BTConnectState::DISCONNECTED);
385 }
386 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
387 CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
388 static_cast<int>(BTConnectState::DISCONNECTED), "failed: no proxy");
389
390 return pimpl->GetDeviceState(device);
391 }
392
SendFile(std::string device,std::vector<std::string> filePaths,std::vector<std::string> mimeTypes)393 bool Opp::SendFile(std::string device, std::vector<std::string>filePaths, std::vector<std::string>mimeTypes)
394 {
395 if (!IS_BT_ENABLED()) {
396 HILOGE("bluetooth is off.");
397 return false;
398 }
399 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
400 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
401
402 return pimpl->SendFile(device, filePaths, mimeTypes);
403 }
404
SetIncomingFileConfirmation(bool accept)405 bool Opp::SetIncomingFileConfirmation(bool accept)
406 {
407 if (!IS_BT_ENABLED()) {
408 HILOGE("pimpl or opp proxy is nullptr");
409 return false;
410 }
411
412 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
413 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
414
415 return pimpl->SetIncomingFileConfirmation(accept);
416 }
417
GetCurrentTransferInformation()418 BluetoothOppTransferInformation Opp::GetCurrentTransferInformation()
419 {
420 BluetoothOppTransferInformation transferInformation;
421 if (!IS_BT_ENABLED()) {
422 HILOGE("bluetooth is off.");
423 return transferInformation;
424 }
425
426 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
427 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, transferInformation, "failed: no proxy");
428
429 transferInformation = pimpl->GetCurrentTransferInformation();
430 return transferInformation;
431 }
432
CancelTransfer()433 bool Opp::CancelTransfer()
434 {
435 if (!IS_BT_ENABLED()) {
436 HILOGE("bluetooth is off.");
437 return false;
438 }
439
440 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
441 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
442
443 return pimpl->CancelTransfer();
444 }
445
RegisterObserver(std::shared_ptr<OppObserver> observer)446 void Opp::RegisterObserver(std::shared_ptr<OppObserver> observer)
447 {
448 HILOGD("enter");
449 CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
450 pimpl->RegisterObserver(observer);
451 }
452
DeregisterObserver(std::shared_ptr<OppObserver> observer)453 void Opp::DeregisterObserver(std::shared_ptr<OppObserver> observer)
454 {
455 HILOGD("enter");
456 CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
457 pimpl->DeregisterObserver(observer);
458 }
459 } // namespace Bluetooth
460 } // namespace OHOS