• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_load_system_ability.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     bool InitOppServerProxy(void);
220 
GetDevicesByStatesOHOS::Bluetooth::Opp::impl221     std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states)
222     {
223         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
224         std::vector<BluetoothRemoteDevice> remoteDevices;
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         if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
245             int state;
246             proxy_->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
247             return state;
248         }
249         return static_cast<int>(BTConnectState::DISCONNECTED);
250     }
251 
SendFileOHOS::Bluetooth::Opp::impl252     bool SendFile(std::string device, std::vector<std::string>filePaths, std::vector<std::string>mimeTypes)
253     {
254         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
255         if (proxy_ != nullptr && IS_BT_ENABLED()) {
256             bool isOk;
257             proxy_->SendFile(device, filePaths, mimeTypes, isOk);
258             return isOk;
259         }
260         HILOGE("[%{public}s]: %{public}s(): fw return false!", __FILE__, __FUNCTION__);
261         return false;
262     }
263 
SetIncomingFileConfirmationOHOS::Bluetooth::Opp::impl264     bool SetIncomingFileConfirmation(bool accept)
265     {
266         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
267         if (proxy_ != nullptr && IS_BT_ENABLED()) {
268             bool isOk;
269             proxy_->SetIncomingFileConfirmation(accept, isOk);
270             return isOk;
271         }
272         HILOGE("[%{public}s]: %{public}s(): fw return false!", __FILE__, __FUNCTION__);
273         return false;
274     }
275 
GetCurrentTransferInformationOHOS::Bluetooth::Opp::impl276     BluetoothOppTransferInformation GetCurrentTransferInformation()
277     {
278         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
279         BluetoothIOppTransferInformation oppInformation;
280         BluetoothOppTransferInformation transferInformation;
281         if (proxy_ != nullptr && IS_BT_ENABLED()) {
282             proxy_->GetCurrentTransferInformation(oppInformation);
283             transferInformation.SetId(oppInformation.GetId());
284             transferInformation.SetFileName(oppInformation.GetFileName());
285             transferInformation.SetFilePath(oppInformation.GetFilePath());
286             transferInformation.SetMimeType(oppInformation.GetFileType());
287             transferInformation.SetDeviceName(oppInformation.GetDeviceName());
288             transferInformation.SetDeviceAddress(oppInformation.GetDeviceAddress());
289             transferInformation.SetFailedReason(oppInformation.GetFailedReason());
290             transferInformation.SetStatus(oppInformation.GetStatus());
291             transferInformation.SetDirection(oppInformation.GetDirection());
292             transferInformation.SetTimeStamp(oppInformation.GetTimeStamp());
293             transferInformation.SetCurrentBytes(oppInformation.GetCurrentBytes());
294             transferInformation.SetTotalBytes(oppInformation.GetTotalBytes());
295         }
296         return transferInformation;
297     }
298 
CancelTransferOHOS::Bluetooth::Opp::impl299     bool CancelTransfer()
300     {
301         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
302         if (proxy_ != nullptr && IS_BT_ENABLED()) {
303             bool isOk;
304             proxy_->CancelTransfer(isOk);
305             return isOk;
306         }
307         HILOGE("[%{public}s]: %{public}s(): fw return false!", __FILE__, __FUNCTION__);
308         return false;
309     }
310 
RegisterObserverOHOS::Bluetooth::Opp::impl311     void RegisterObserver(std::shared_ptr<OppObserver> observer)
312     {
313         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
314         observers_.Register(observer);
315     }
316 
DeregisterObserverOHOS::Bluetooth::Opp::impl317     void DeregisterObserver(std::shared_ptr<OppObserver> observer)
318     {
319         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
320         observers_.Deregister(observer);
321     }
322 
323     sptr<IBluetoothOpp> proxy_;
324 private:
325     BluetoothObserverList<OppObserver> observers_;
326     sptr<OppInnerObserver> innerObserver_;
327     class OppDeathRecipient;
328     sptr<OppDeathRecipient> deathRecipient_;
329 };
330 
331 class Opp::impl::OppDeathRecipient final : public IRemoteObject::DeathRecipient {
332 public:
OppDeathRecipient(Opp::impl & impl)333     explicit OppDeathRecipient(Opp::impl &impl) : impl_(impl)
334     {};
335     ~OppDeathRecipient() final = default;
336     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(OppDeathRecipient);
337 
OnRemoteDied(const wptr<IRemoteObject> & remote)338     void OnRemoteDied(const wptr<IRemoteObject> &remote) final
339     {
340         HILOGI("Opp::impl::OppDeathRecipient::OnRemoteDied starts");
341         std::lock_guard<std::mutex> lock(g_oppProxyMutex);
342         if (!impl_.proxy_) {
343             return;
344         }
345         impl_.proxy_ = nullptr;
346     }
347 
348 private:
349     Opp::impl &impl_;
350 };
351 
impl()352 Opp::impl::impl()
353 {
354     if (proxy_) {
355         return;
356     }
357     BluetootLoadSystemAbility::GetInstance().RegisterNotifyMsg(PROFILE_ID_OPP_SERVER);
358     if (!BluetootLoadSystemAbility::GetInstance().HasSubscribedBluetoothSystemAbility()) {
359         BluetootLoadSystemAbility::GetInstance().SubScribeBluetoothSystemAbility();
360         return;
361     }
362     InitOppServerProxy();
363 }
364 
~impl()365 Opp::impl::~impl()
366 {
367     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
368     if (proxy_ == nullptr) {
369         HILOGE("Opp::impl:~impl() failed: no proxy_");
370         return;
371     }
372     proxy_->DeregisterObserver(innerObserver_);
373     proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
374 }
375 
InitOppServerProxy(void)376 bool Opp::impl::InitOppServerProxy(void)
377 {
378     std::lock_guard<std::mutex> lock(g_oppProxyMutex);
379     if (proxy_) {
380         return true;
381     }
382     HILOGI("enter");
383     proxy_ = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
384     if (!proxy_) {
385         HILOGE("get Opp proxy_ failed");
386         return false;
387     }
388 
389     innerObserver_ = new OppInnerObserver(observers_);
390     if (innerObserver_ != nullptr) {
391         proxy_->RegisterObserver(innerObserver_);
392     }
393 
394     deathRecipient_ = new OppDeathRecipient(*this);
395     if (deathRecipient_ != nullptr) {
396         proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
397     }
398     return true;
399 }
400 
Opp()401 Opp::Opp()
402 {
403     pimpl = std::make_unique<impl>();
404 }
405 
~Opp()406 Opp::~Opp()
407 {}
408 
Init()409 void Opp::Init()
410 {
411     if (!pimpl) {
412         HILOGE("fails: no pimpl");
413         return;
414     }
415     if (!pimpl->InitOppServerProxy()) {
416         HILOGE("Opp proxy_ is nullptr");
417         return;
418     }
419 }
420 
GetProfile()421 Opp *Opp::GetProfile()
422 {
423     static Opp instance;
424     return &instance;
425 }
GetDevicesByStates(std::vector<int> states)426 std::vector<BluetoothRemoteDevice> Opp::GetDevicesByStates(std::vector<int> states)
427 {
428     if (!IS_BT_ENABLED()) {
429         HILOGE("bluetooth is off.");
430         return std::vector<BluetoothRemoteDevice>();
431     }
432 
433     if (pimpl == nullptr || !pimpl->proxy_) {
434         HILOGE("pimpl or opp proxy_ is nullptr");
435         return std::vector<BluetoothRemoteDevice>();
436     }
437 
438     return pimpl->GetDevicesByStates(states);
439 }
440 
GetDeviceState(const BluetoothRemoteDevice & device)441 int Opp::GetDeviceState(const BluetoothRemoteDevice &device)
442 {
443     if (!IS_BT_ENABLED()) {
444         HILOGE("bluetooth is off.");
445         return static_cast<int>(BTConnectState::DISCONNECTED);
446     }
447 
448     if (pimpl == nullptr || !pimpl->proxy_) {
449         HILOGE("pimpl or opp proxy_ is nullptr");
450         return static_cast<int>(BTConnectState::DISCONNECTED);
451     }
452 
453     return pimpl->GetDeviceState(device);
454 }
455 
SendFile(std::string device,std::vector<std::string> filePaths,std::vector<std::string> mimeTypes)456 bool Opp::SendFile(std::string device, std::vector<std::string>filePaths, std::vector<std::string>mimeTypes)
457 {
458     if (!IS_BT_ENABLED()) {
459         HILOGE("bluetooth is off.");
460         return false;
461     }
462 
463     if (pimpl == nullptr || !pimpl->proxy_) {
464         HILOGE("pimpl or opp proxy_ is nullptr");
465         return false;
466     }
467 
468     return pimpl->SendFile(device, filePaths, mimeTypes);
469 }
470 
SetIncomingFileConfirmation(bool accept)471 bool Opp::SetIncomingFileConfirmation(bool accept)
472 {
473     if (!IS_BT_ENABLED()) {
474         HILOGE("pimpl or opp proxy_ is nullptr");
475         return false;
476     }
477 
478     if (pimpl == nullptr || !pimpl->proxy_) {
479         HILOGE("get Opp proxy_ failed");
480         return false;
481     }
482 
483     return pimpl->SetIncomingFileConfirmation(accept);
484 }
485 
GetCurrentTransferInformation()486 BluetoothOppTransferInformation Opp::GetCurrentTransferInformation()
487 {
488     BluetoothOppTransferInformation transferInformation;
489     if (!IS_BT_ENABLED()) {
490         HILOGE("bluetooth is off.");
491         return transferInformation;
492     }
493 
494     if (pimpl == nullptr || !pimpl->proxy_) {
495         HILOGE("pimpl or opp proxy_ is nullptr");
496         return transferInformation;
497     }
498 
499     transferInformation = pimpl->GetCurrentTransferInformation();
500     return transferInformation;
501 }
502 
CancelTransfer()503 bool Opp::CancelTransfer()
504 {
505     if (!IS_BT_ENABLED()) {
506         HILOGE("bluetooth is off.");
507         return false;
508     }
509 
510     if (pimpl == nullptr || !pimpl->proxy_) {
511         HILOGE("pimpl or opp proxy_ is nullptr");
512         return false;
513     }
514 
515     return pimpl->CancelTransfer();
516 }
517 
RegisterObserver(OppObserver * observer)518 void Opp::RegisterObserver(OppObserver *observer)
519 {
520     std::shared_ptr<OppObserver> observerPtr(observer, [](OppObserver *) {});
521     return pimpl->RegisterObserver(observerPtr);
522 }
523 
DeregisterObserver(OppObserver * observer)524 void Opp::DeregisterObserver(OppObserver *observer)
525 {
526     std::shared_ptr<OppObserver> observerPtr(observer, [](OppObserver *) {});
527     return pimpl->DeregisterObserver(observerPtr);
528 }
529 }  // namespace Bluetooth
530 }  // namespace OHOS