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_log.h"
19 #include "bluetooth_observer_list.h"
20 #include "bluetooth_opp_observer_stub.h"
21 #include "i_bluetooth_opp.h"
22 #include "i_bluetooth_host.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace Bluetooth {
BluetoothOppTransferInformation()28 BluetoothOppTransferInformation::BluetoothOppTransferInformation()
29 {}
30
~BluetoothOppTransferInformation()31 BluetoothOppTransferInformation::~BluetoothOppTransferInformation()
32 {}
33
GetId() const34 int BluetoothOppTransferInformation::GetId() const
35 {
36 return id_;
37 }
38
GetFileName() const39 std::string BluetoothOppTransferInformation::GetFileName() const
40 {
41 return fileName_;
42 }
43
GetFilePath() const44 std::string BluetoothOppTransferInformation::GetFilePath() const
45 {
46 return filePath_;
47 }
48
GetMimeType() const49 std::string BluetoothOppTransferInformation::GetMimeType() const
50 {
51 return mimeType_;
52 }
53
GetDeviceName() const54 std::string BluetoothOppTransferInformation::GetDeviceName() const
55 {
56 return deviceName_;
57 }
58
GetDeviceAddress() const59 std::string BluetoothOppTransferInformation::GetDeviceAddress() const
60 {
61 return deviceAddress_;
62 }
63
GetDirection() const64 int BluetoothOppTransferInformation::GetDirection() const
65 {
66 return direction_;
67 }
68
GetStatus() const69 int BluetoothOppTransferInformation::GetStatus() const
70 {
71 return status_;
72 }
73
GetFailedReason() const74 int BluetoothOppTransferInformation::GetFailedReason() const
75 {
76 return failedReason_;
77 }
78
GetTimeStamp() const79 uint64_t BluetoothOppTransferInformation::GetTimeStamp() const
80 {
81 return timeStamp_;
82 }
83
GetCurrentBytes() const84 uint64_t BluetoothOppTransferInformation::GetCurrentBytes() const
85 {
86 return currentBytes_;
87 }
88
GetTotalBytes() const89 uint64_t BluetoothOppTransferInformation::GetTotalBytes() const
90 {
91 return totalBytes_;
92 }
93
SetId(int id)94 void BluetoothOppTransferInformation::SetId(int id)
95 {
96 id_ = id;
97 }
98
SetFileName(std::string fileName)99 void BluetoothOppTransferInformation::SetFileName(std::string fileName)
100 {
101 fileName_ = fileName;
102 }
103
SetFilePath(std::string filePath)104 void BluetoothOppTransferInformation::SetFilePath(std::string filePath)
105 {
106 filePath_ = filePath;
107 }
108
SetMimeType(std::string mimeType)109 void BluetoothOppTransferInformation::SetMimeType(std::string mimeType)
110 {
111 mimeType_ = mimeType;
112 }
113
SetDeviceName(std::string deviceName)114 void BluetoothOppTransferInformation::SetDeviceName(std::string deviceName)
115 {
116 deviceName_ = deviceName;
117 }
118
SetDeviceAddress(std::string deviceAddress)119 void BluetoothOppTransferInformation::SetDeviceAddress(std::string deviceAddress)
120 {
121 deviceAddress_ = deviceAddress;
122 }
123
SetDirection(int direction)124 void BluetoothOppTransferInformation::SetDirection(int direction)
125 {
126 direction_ = direction;
127 }
128
SetStatus(int status)129 void BluetoothOppTransferInformation::SetStatus(int status)
130 {
131 status_ = status;
132 }
133
SetFailedReason(int failedReason)134 void BluetoothOppTransferInformation::SetFailedReason(int failedReason)
135 {
136 failedReason_ = failedReason;
137 }
138
SetTimeStamp(uint64_t timeStamp)139 void BluetoothOppTransferInformation::SetTimeStamp(uint64_t timeStamp)
140 {
141 timeStamp_ = timeStamp;
142 }
143
SetCurrentBytes(uint64_t currentBytes)144 void BluetoothOppTransferInformation::SetCurrentBytes(uint64_t currentBytes)
145 {
146 currentBytes_ = currentBytes;
147 }
148
SetTotalBytes(uint64_t totalBytes)149 void BluetoothOppTransferInformation::SetTotalBytes(uint64_t totalBytes)
150 {
151 totalBytes_ = totalBytes;
152 }
153
154 class OppInnerObserver : public BluetoothOppObserverStub {
155 public:
OppInnerObserver(BluetoothObserverList<OppObserver> & observers)156 explicit OppInnerObserver(BluetoothObserverList<OppObserver> &observers) : observers_(observers)
157 {
158 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
159 }
~OppInnerObserver()160 ~OppInnerObserver() override
161 {
162 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
163 }
164
OnReceiveIncomingFileChanged(const BluetoothIOppTransferInformation & transferInformation)165 ErrCode OnReceiveIncomingFileChanged(const BluetoothIOppTransferInformation &transferInformation) override
166 {
167 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
168 BluetoothOppTransferInformation oppTransferinformation;
169 oppTransferinformation.SetId(transferInformation.GetId());
170 oppTransferinformation.SetFileName(transferInformation.GetFileName());
171 oppTransferinformation.SetFilePath(transferInformation.GetFilePath());
172 oppTransferinformation.SetMimeType(transferInformation.GetFileType());
173 oppTransferinformation.SetDeviceName(transferInformation.GetDeviceName());
174 oppTransferinformation.SetDeviceAddress(transferInformation.GetDeviceAddress());
175 oppTransferinformation.SetFailedReason(transferInformation.GetFailedReason());
176 oppTransferinformation.SetStatus(transferInformation.GetStatus());
177 oppTransferinformation.SetDirection(transferInformation.GetDirection());
178 oppTransferinformation.SetTimeStamp(transferInformation.GetTimeStamp());
179 oppTransferinformation.SetCurrentBytes(transferInformation.GetCurrentBytes());
180 oppTransferinformation.SetTotalBytes(transferInformation.GetTotalBytes());
181 observers_.ForEach([oppTransferinformation](std::shared_ptr<OppObserver> observer) {
182 observer->OnReceiveIncomingFileChanged(oppTransferinformation);
183 });
184 return NO_ERROR;
185 }
OnTransferStateChanged(const BluetoothIOppTransferInformation & transferInformation)186 ErrCode OnTransferStateChanged(const BluetoothIOppTransferInformation &transferInformation) override
187 {
188 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
189 BluetoothOppTransferInformation oppTransferinformation;
190 oppTransferinformation.SetId(transferInformation.GetId());
191 oppTransferinformation.SetFileName(transferInformation.GetFileName());
192 oppTransferinformation.SetFilePath(transferInformation.GetFilePath());
193 oppTransferinformation.SetMimeType(transferInformation.GetFileType());
194 oppTransferinformation.SetDeviceName(transferInformation.GetDeviceName());
195 oppTransferinformation.SetDeviceAddress(transferInformation.GetDeviceAddress());
196 oppTransferinformation.SetFailedReason(transferInformation.GetFailedReason());
197 oppTransferinformation.SetStatus(transferInformation.GetStatus());
198 oppTransferinformation.SetDirection(transferInformation.GetDirection());
199 oppTransferinformation.SetTimeStamp(transferInformation.GetTimeStamp());
200 oppTransferinformation.SetCurrentBytes(transferInformation.GetCurrentBytes());
201 oppTransferinformation.SetTotalBytes(transferInformation.GetTotalBytes());
202 observers_.ForEach([oppTransferinformation](std::shared_ptr<OppObserver> observer) {
203 observer->OnTransferStateChanged(oppTransferinformation);
204 });
205 return NO_ERROR;
206 }
207
208 private:
209 BluetoothObserverList<OppObserver> &observers_;
210 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(OppInnerObserver);
211 };
212
213 struct Opp::impl {
214 impl();
215 ~impl();
GetDevicesByStatesOHOS::Bluetooth::Opp::impl216 std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states)
217 {
218 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
219 std::vector<BluetoothRemoteDevice> remoteDevices;
220 if (proxy_ != nullptr && IS_BT_ENABLED()) {
221 std::vector<BluetoothRawAddress> rawDevices;
222 std::vector<int32_t> tmpStates;
223 for (int state : states) {
224 tmpStates.push_back((int32_t)state);
225 }
226
227 proxy_->GetDevicesByStates(tmpStates, rawDevices);
228 for (BluetoothRawAddress rawDevice : rawDevices) {
229 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
230 remoteDevices.push_back(remoteDevice);
231 }
232 }
233 return remoteDevices;
234 }
235
GetDeviceStateOHOS::Bluetooth::Opp::impl236 int GetDeviceState(const BluetoothRemoteDevice &device)
237 {
238 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
239 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
240 int state;
241 proxy_->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
242 return state;
243 }
244 return (int)BTConnectState::DISCONNECTED;
245 }
246
SendFileOHOS::Bluetooth::Opp::impl247 bool SendFile(std::string device, std::vector<std::string>filePaths, std::vector<std::string>mimeTypes)
248 {
249 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
250 if (proxy_ != nullptr && IS_BT_ENABLED()) {
251 bool isOk;
252 proxy_->SendFile(device, filePaths, mimeTypes, isOk);
253 return isOk;
254 }
255 HILOGE("[%{public}s]: %{public}s(): fw return false!", __FILE__, __FUNCTION__);
256 return false;
257 }
258
SetIncomingFileConfirmationOHOS::Bluetooth::Opp::impl259 bool SetIncomingFileConfirmation(bool accept)
260 {
261 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
262 if (proxy_ != nullptr && IS_BT_ENABLED()) {
263 bool isOk;
264 proxy_->SetIncomingFileConfirmation(accept, isOk);
265 return isOk;
266 }
267 HILOGE("[%{public}s]: %{public}s(): fw return false!", __FILE__, __FUNCTION__);
268 return false;
269 }
270
GetCurrentTransferInformationOHOS::Bluetooth::Opp::impl271 BluetoothOppTransferInformation GetCurrentTransferInformation()
272 {
273 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
274 BluetoothIOppTransferInformation oppInformation;
275 BluetoothOppTransferInformation transferInformation;
276 if (proxy_ != nullptr && IS_BT_ENABLED()) {
277 proxy_->GetCurrentTransferInformation(oppInformation);
278 transferInformation.SetId(oppInformation.GetId());
279 transferInformation.SetFileName(oppInformation.GetFileName());
280 transferInformation.SetFilePath(oppInformation.GetFilePath());
281 transferInformation.SetMimeType(oppInformation.GetFileType());
282 transferInformation.SetDeviceName(oppInformation.GetDeviceName());
283 transferInformation.SetDeviceAddress(oppInformation.GetDeviceAddress());
284 transferInformation.SetFailedReason(oppInformation.GetFailedReason());
285 transferInformation.SetStatus(oppInformation.GetStatus());
286 transferInformation.SetDirection(oppInformation.GetDirection());
287 transferInformation.SetTimeStamp(oppInformation.GetTimeStamp());
288 transferInformation.SetCurrentBytes(oppInformation.GetCurrentBytes());
289 transferInformation.SetTotalBytes(oppInformation.GetTotalBytes());
290 }
291 return transferInformation;
292 }
293
CancelTransferOHOS::Bluetooth::Opp::impl294 bool CancelTransfer()
295 {
296 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
297 if (proxy_ != nullptr && IS_BT_ENABLED()) {
298 bool isOk;
299 proxy_->CancelTransfer(isOk);
300 return isOk;
301 }
302 HILOGE("[%{public}s]: %{public}s(): fw return false!", __FILE__, __FUNCTION__);
303 return false;
304 }
305
RegisterObserverOHOS::Bluetooth::Opp::impl306 void RegisterObserver(std::shared_ptr<OppObserver> observer)
307 {
308 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
309 observers_.Register(observer);
310 }
311
DeregisterObserverOHOS::Bluetooth::Opp::impl312 void DeregisterObserver(std::shared_ptr<OppObserver> observer)
313 {
314 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
315 observers_.Deregister(observer);
316 }
317
318 private:
319 BluetoothObserverList<OppObserver> observers_;
320 sptr<OppInnerObserver> innerObserver_;
321 sptr<IBluetoothOpp> proxy_;
322 class OppDeathRecipient;
323 sptr<OppDeathRecipient> deathRecipient_;
324 };
325
326 class Opp::impl::OppDeathRecipient final : public IRemoteObject::DeathRecipient {
327 public:
OppDeathRecipient(Opp::impl & impl)328 OppDeathRecipient(Opp::impl &impl) : impl_(impl)
329 {};
330 ~OppDeathRecipient() final = default;
331 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(OppDeathRecipient);
332
OnRemoteDied(const wptr<IRemoteObject> & remote)333 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
334 {
335 HILOGI("Opp::impl::OppDeathRecipient::OnRemoteDied starts");
336 impl_.proxy_->AsObject()->RemoveDeathRecipient(impl_.deathRecipient_);
337 impl_.proxy_ = nullptr;
338 }
339
340 private:
341 Opp::impl &impl_;
342 };
343
impl()344 Opp::impl::impl()
345 {
346 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
347 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
348 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
349
350 if (!hostRemote) {
351 HILOGE("Opp::impl:impl() failed: no hostRemote");
352 return;
353 }
354 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
355 sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_OPP_SERVER);
356
357 if (!remote) {
358 HILOGE("Opp::impl:impl() failed: no remote");
359 return;
360 }
361 HILOGI("Opp::impl:impl() remote obtained");
362
363 innerObserver_ = new OppInnerObserver(observers_);
364 proxy_ = iface_cast<IBluetoothOpp>(remote);
365 proxy_->RegisterObserver(innerObserver_);
366 deathRecipient_ = new OppDeathRecipient(*this);
367 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
368 }
369
~impl()370 Opp::impl::~impl()
371 {
372 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
373 if (proxy_ != nullptr) {
374 proxy_->DeregisterObserver(innerObserver_);
375 }
376 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
377 }
378
Opp()379 Opp::Opp()
380 {
381 pimpl = std::make_unique<impl>();
382 }
383
~Opp()384 Opp::~Opp()
385 {}
386
GetProfile()387 Opp *Opp::GetProfile()
388 {
389 static Opp instance;
390 return &instance;
391 }
GetDevicesByStates(std::vector<int> states)392 std::vector<BluetoothRemoteDevice> Opp::GetDevicesByStates(std::vector<int> states)
393 {
394 return pimpl->GetDevicesByStates(states);
395 }
396
GetDeviceState(const BluetoothRemoteDevice & device)397 int Opp::GetDeviceState(const BluetoothRemoteDevice &device)
398 {
399 return pimpl->GetDeviceState(device);
400 }
401
SendFile(std::string device,std::vector<std::string> filePaths,std::vector<std::string> mimeTypes)402 bool Opp::SendFile(std::string device, std::vector<std::string>filePaths, std::vector<std::string>mimeTypes)
403 {
404 return pimpl->SendFile(device, filePaths, mimeTypes);
405 }
406
SetIncomingFileConfirmation(bool accept)407 bool Opp::SetIncomingFileConfirmation(bool accept)
408 {
409 return pimpl->SetIncomingFileConfirmation(accept);
410 }
411
GetCurrentTransferInformation()412 BluetoothOppTransferInformation Opp::GetCurrentTransferInformation()
413 {
414 BluetoothOppTransferInformation transferInformation = pimpl->GetCurrentTransferInformation();
415 return transferInformation;
416 }
417
CancelTransfer()418 bool Opp::CancelTransfer()
419 {
420 return pimpl->CancelTransfer();
421 }
422
RegisterObserver(OppObserver * observer)423 void Opp::RegisterObserver(OppObserver *observer)
424 {
425 std::shared_ptr<OppObserver> observerPtr(observer, [](OppObserver *) {});
426 return pimpl->RegisterObserver(observerPtr);
427 }
428
DeregisterObserver(OppObserver * observer)429 void Opp::DeregisterObserver(OppObserver *observer)
430 {
431 std::shared_ptr<OppObserver> observerPtr(observer, [](OppObserver *) {});
432 return pimpl->DeregisterObserver(observerPtr);
433 }
434 } // namespace Bluetooth
435 } // namespace OHOS