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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_fwk_opp"
17 #endif
18
19 #include "bluetooth_opp.h"
20 #include "bluetooth_host.h"
21 #include "bluetooth_profile_manager.h"
22 #include "bluetooth_log.h"
23 #include "bluetooth_observer_list.h"
24 #include "bluetooth_opp_observer_stub.h"
25 #include "bluetooth_utils.h"
26 #include "i_bluetooth_opp.h"
27 #include "i_bluetooth_host.h"
28 #include "iservice_registry.h"
29 #include "system_ability_definition.h"
30
31 namespace OHOS {
32 namespace Bluetooth {
33
TransferInformation(const BluetoothIOppTransferInformation & other)34 BluetoothOppTransferInformation TransferInformation(const BluetoothIOppTransferInformation &other)
35 {
36 BluetoothOppTransferInformation oppTransferinformation;
37 oppTransferinformation.SetId(other.GetId());
38 oppTransferinformation.SetFileName(other.GetFileName());
39 oppTransferinformation.SetFilePath(other.GetFilePath());
40 oppTransferinformation.SetMimeType(other.GetFileType());
41 oppTransferinformation.SetDeviceName(other.GetDeviceName());
42 oppTransferinformation.SetDeviceAddress(other.GetDeviceAddress());
43 oppTransferinformation.SetResult(other.GetResult());
44 oppTransferinformation.SetStatus(other.GetStatus());
45 oppTransferinformation.SetDirection(other.GetDirection());
46 oppTransferinformation.SetTimeStamp(other.GetTimeStamp());
47 oppTransferinformation.SetCurrentBytes(other.GetCurrentBytes());
48 oppTransferinformation.SetTotalBytes(other.GetTotalBytes());
49 oppTransferinformation.SetCurrentCount(other.GetCurrentCount());
50 oppTransferinformation.SetTotalCount(other.GetTotalCount());
51 return oppTransferinformation;
52 }
53
54 class BluetoothOppObserverImpl : public BluetoothOppObserverStub {
55 public:
BluetoothOppObserverImpl(BluetoothObserverList<OppObserver> & observers)56 explicit BluetoothOppObserverImpl(BluetoothObserverList<OppObserver> &observers)
57 : observers_(observers)
58 {}
~BluetoothOppObserverImpl()59 ~BluetoothOppObserverImpl() override
60 {}
61
OnReceiveIncomingFileChanged(const BluetoothIOppTransferInformation & transferInformation)62 void OnReceiveIncomingFileChanged(const BluetoothIOppTransferInformation &transferInformation) override
63 {
64 BluetoothOppTransferInformation oppTransferinformation = TransferInformation(transferInformation);
65 observers_.ForEach([oppTransferinformation](std::shared_ptr<OppObserver> observer) {
66 observer->OnReceiveIncomingFileChanged(oppTransferinformation);
67 });
68 return;
69 }
70
OnTransferStateChanged(const BluetoothIOppTransferInformation & transferInformation)71 void OnTransferStateChanged(const BluetoothIOppTransferInformation &transferInformation) override
72 {
73 BluetoothOppTransferInformation oppTransferinformation = TransferInformation(transferInformation);
74 observers_.ForEach([oppTransferinformation](std::shared_ptr<OppObserver> observer) {
75 observer->OnTransferStateChanged(oppTransferinformation);
76 });
77 return;
78 }
79
80 private:
81 BluetoothObserverList<OppObserver> &observers_;
82 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothOppObserverImpl);
83 };
84
85 struct Opp::impl {
implOHOS::Bluetooth::Opp::impl86 impl()
87 {
88 serviceObserverImp_ = new BluetoothOppObserverImpl(observers_);
89 profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(PROFILE_OPP_SERVER,
90 [this](sptr<IRemoteObject> remote) {
91 sptr<IBluetoothOpp> proxy = iface_cast<IBluetoothOpp>(remote);
92 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
93 proxy->RegisterObserver(serviceObserverImp_);
94 });
95 }
~implOHOS::Bluetooth::Opp::impl96 ~impl()
97 {
98 BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
99 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
100 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
101 proxy->DeregisterObserver(serviceObserverImp_);
102 }
RegisterObserverOHOS::Bluetooth::Opp::impl103 void RegisterObserver(std::shared_ptr<OppObserver> &observer)
104 {
105 HILOGI("enter");
106 if (observer) {
107 observers_.Register(observer);
108 }
109 }
DeregisterObserverOHOS::Bluetooth::Opp::impl110 void DeregisterObserver(std::shared_ptr<OppObserver> &observer)
111 {
112 HILOGI("enter");
113 if (observer) {
114 observers_.Deregister(observer);
115 }
116 }
117 int32_t profileRegisterId = 0;
118 private:
119 BluetoothObserverList<OppObserver> observers_;
120 sptr<BluetoothOppObserverImpl> serviceObserverImp_ = nullptr;
121 };
122
123 std::mutex g_oppProxyMutex;
BluetoothOppTransferInformation()124 BluetoothOppTransferInformation::BluetoothOppTransferInformation()
125 {}
126
~BluetoothOppTransferInformation()127 BluetoothOppTransferInformation::~BluetoothOppTransferInformation()
128 {}
129
GetId() const130 int BluetoothOppTransferInformation::GetId() const
131 {
132 return id_;
133 }
134
GetFileName() const135 std::string BluetoothOppTransferInformation::GetFileName() const
136 {
137 return fileName_;
138 }
139
GetFilePath() const140 std::string BluetoothOppTransferInformation::GetFilePath() const
141 {
142 return filePath_;
143 }
144
GetMimeType() const145 std::string BluetoothOppTransferInformation::GetMimeType() const
146 {
147 return mimeType_;
148 }
149
GetDeviceName() const150 std::string BluetoothOppTransferInformation::GetDeviceName() const
151 {
152 return deviceName_;
153 }
154
GetDeviceAddress() const155 std::string BluetoothOppTransferInformation::GetDeviceAddress() const
156 {
157 return deviceAddress_;
158 }
159
GetDirection() const160 int BluetoothOppTransferInformation::GetDirection() const
161 {
162 return direction_;
163 }
164
GetStatus() const165 int BluetoothOppTransferInformation::GetStatus() const
166 {
167 return status_;
168 }
169
GetResult() const170 int BluetoothOppTransferInformation::GetResult() const
171 {
172 return result_;
173 }
174
GetTimeStamp() const175 uint64_t BluetoothOppTransferInformation::GetTimeStamp() const
176 {
177 return timeStamp_;
178 }
179
GetCurrentBytes() const180 uint64_t BluetoothOppTransferInformation::GetCurrentBytes() const
181 {
182 return currentBytes_;
183 }
184
GetTotalBytes() const185 uint64_t BluetoothOppTransferInformation::GetTotalBytes() const
186 {
187 return totalBytes_;
188 }
189
GetCurrentCount() const190 int BluetoothOppTransferInformation::GetCurrentCount() const
191 {
192 return currentCount_;
193 }
194
GetTotalCount() const195 int BluetoothOppTransferInformation::GetTotalCount() const
196 {
197 return totalCount_;
198 }
199
SetId(int id)200 void BluetoothOppTransferInformation::SetId(int id)
201 {
202 id_ = id;
203 }
204
SetFileName(std::string fileName)205 void BluetoothOppTransferInformation::SetFileName(std::string fileName)
206 {
207 fileName_ = fileName;
208 }
209
SetFilePath(std::string filePath)210 void BluetoothOppTransferInformation::SetFilePath(std::string filePath)
211 {
212 filePath_ = filePath;
213 }
214
SetMimeType(std::string mimeType)215 void BluetoothOppTransferInformation::SetMimeType(std::string mimeType)
216 {
217 mimeType_ = mimeType;
218 }
219
SetDeviceName(std::string deviceName)220 void BluetoothOppTransferInformation::SetDeviceName(std::string deviceName)
221 {
222 deviceName_ = deviceName;
223 }
224
SetDeviceAddress(std::string deviceAddress)225 void BluetoothOppTransferInformation::SetDeviceAddress(std::string deviceAddress)
226 {
227 deviceAddress_ = deviceAddress;
228 }
229
SetDirection(int direction)230 void BluetoothOppTransferInformation::SetDirection(int direction)
231 {
232 direction_ = direction;
233 }
234
SetStatus(int status)235 void BluetoothOppTransferInformation::SetStatus(int status)
236 {
237 status_ = status;
238 }
239
SetResult(int result)240 void BluetoothOppTransferInformation::SetResult(int result)
241 {
242 result_ = result;
243 }
244
SetTimeStamp(uint64_t timeStamp)245 void BluetoothOppTransferInformation::SetTimeStamp(uint64_t timeStamp)
246 {
247 timeStamp_ = timeStamp;
248 }
249
SetCurrentBytes(uint64_t currentBytes)250 void BluetoothOppTransferInformation::SetCurrentBytes(uint64_t currentBytes)
251 {
252 currentBytes_ = currentBytes;
253 }
254
SetTotalBytes(uint64_t totalBytes)255 void BluetoothOppTransferInformation::SetTotalBytes(uint64_t totalBytes)
256 {
257 totalBytes_ = totalBytes;
258 }
259
SetCurrentCount(int currentCount)260 void BluetoothOppTransferInformation::SetCurrentCount(int currentCount)
261 {
262 currentCount_ = currentCount;
263 }
264
SetTotalCount(int totalCount)265 void BluetoothOppTransferInformation::SetTotalCount(int totalCount)
266 {
267 totalCount_ = totalCount;
268 }
269
BluetoothOppFileHolder()270 BluetoothOppFileHolder::BluetoothOppFileHolder()
271 {}
272
BluetoothOppFileHolder(const std::string & filePath,const int64_t & fileSize,const int32_t & fileFd)273 BluetoothOppFileHolder::BluetoothOppFileHolder(const std::string &filePath,
274 const int64_t &fileSize, const int32_t &fileFd)
275 {
276 filePath_ = filePath;
277 fileSize_ = fileSize;
278 fileFd_ = fileFd;
279 }
280
~BluetoothOppFileHolder()281 BluetoothOppFileHolder::~BluetoothOppFileHolder()
282 {}
283
GetFilePath() const284 std::string BluetoothOppFileHolder::GetFilePath() const
285 {
286 return filePath_;
287 }
288
GetFileSize() const289 int64_t BluetoothOppFileHolder::GetFileSize() const
290 {
291 return fileSize_;
292 }
293
GetFileFd() const294 int32_t BluetoothOppFileHolder::GetFileFd() const
295 {
296 return fileFd_;
297 }
298
SetFilePath(const std::string & filePath)299 void BluetoothOppFileHolder::SetFilePath(const std::string &filePath)
300 {
301 filePath_ = filePath;
302 }
303
SetFileFd(const int32_t & fileFd)304 void BluetoothOppFileHolder::SetFileFd(const int32_t &fileFd)
305 {
306 fileFd_ = fileFd;
307 }
308
SetFileSize(const int64_t & fileSize)309 void BluetoothOppFileHolder::SetFileSize(const int64_t &fileSize)
310 {
311 fileSize_ = fileSize;
312 }
313
Opp()314 Opp::Opp()
315 {
316 pimpl = std::make_unique<impl>();
317 }
318
~Opp()319 Opp::~Opp()
320 {}
321
GetProfile()322 Opp *Opp::GetProfile()
323 {
324 #ifdef DTFUZZ_TEST
325 static BluetoothNoDestructor<Opp> instance;
326 return instance.get();
327 #else
328 static Opp instance;
329 return &instance;
330 #endif
331 }
332
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRemoteDevice> & result) const333 int32_t Opp::GetDevicesByStates(const std::vector<int32_t> &states,
334 std::vector<BluetoothRemoteDevice> &result) const
335 {
336 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
337 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
338 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
339
340 std::vector<BluetoothRawAddress> rawAddress {};
341 int32_t ret = proxy->GetDevicesByStates(states, rawAddress);
342 CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "inner error");
343
344 for (BluetoothRawAddress rawAddr : rawAddress) {
345 BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
346 result.push_back(device);
347 }
348 return BT_NO_ERROR;
349 }
350
GetDeviceState(const BluetoothRemoteDevice & device,int32_t & state) const351 int32_t Opp::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state) const
352 {
353 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
354 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
355 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
356 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
357 CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
358
359 return proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
360 }
361
SendFile(std::string device,std::vector<BluetoothOppFileHolder> fileHolders,bool & result)362 int32_t Opp::SendFile(std::string device, std::vector<BluetoothOppFileHolder> fileHolders, bool& result)
363 {
364 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
365 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
366 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
367 std::vector<BluetoothIOppTransferFileHolder> fileHoldersInterface;
368 for (BluetoothOppFileHolder fileHolder : fileHolders) {
369 fileHoldersInterface.push_back(BluetoothIOppTransferFileHolder(fileHolder.GetFilePath(),
370 fileHolder.GetFileSize(), fileHolder.GetFileFd()));
371 }
372 int ret = proxy->SendFile(device, fileHoldersInterface, result);
373 HILOGI("send file result is : %{public}d", result);
374 return ret;
375 }
376
SetLastReceivedFileUri(const std::string & uri)377 int32_t Opp::SetLastReceivedFileUri(const std::string &uri)
378 {
379 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
380 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
381 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
382 int ret = proxy->SetLastReceivedFileUri(uri);
383 HILOGI("setLastReceivedFileUri is : %{public}d", ret);
384 return ret;
385 }
386
SetIncomingFileConfirmation(bool accept,int32_t fileFd)387 int32_t Opp::SetIncomingFileConfirmation(bool accept, int32_t fileFd)
388 {
389 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
390
391 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
392 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
393 int ret = proxy->SetIncomingFileConfirmation(accept, fileFd);
394 HILOGI("setIncomingFileConfirmation result is : %{public}d", ret);
395 return ret;
396 }
397
GetCurrentTransferInformation(BluetoothOppTransferInformation & transferInformation)398 int32_t Opp::GetCurrentTransferInformation(BluetoothOppTransferInformation &transferInformation)
399 {
400 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
401 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
402 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
403
404 BluetoothIOppTransferInformation oppInformation;
405 int ret = proxy->GetCurrentTransferInformation(oppInformation);
406 HILOGI("getCurrentTransferInformation result is : %{public}d", ret);
407 if (ret == BT_NO_ERROR) {
408 transferInformation = TransferInformation(oppInformation);
409 }
410 return ret;
411 }
412
CancelTransfer(bool & result)413 int32_t Opp::CancelTransfer(bool &result)
414 {
415 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
416 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
417 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
418
419 int ret = proxy->CancelTransfer();
420 result = (ret == BT_NO_ERROR) ? true : false;
421 HILOGI("cancelTransfer result is : %{public}d", ret);
422 return ret;
423 }
424
RegisterObserver(std::shared_ptr<OppObserver> observer)425 void Opp::RegisterObserver(std::shared_ptr<OppObserver> observer)
426 {
427 HILOGD("enter");
428 CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
429 pimpl->RegisterObserver(observer);
430 }
431
DeregisterObserver(std::shared_ptr<OppObserver> observer)432 void Opp::DeregisterObserver(std::shared_ptr<OppObserver> observer)
433 {
434 HILOGD("enter");
435 CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
436 pimpl->DeregisterObserver(observer);
437 }
438 } // namespace Bluetooth
439 } // namespace OHOS