• 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 #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 #ifndef CROSS_PLATFORM
31 #include "ipc_skeleton.h"
32 #endif
33 #ifdef RES_SCHED_SUPPORT
34 #include "res_type.h"
35 #include "res_sched_client.h"
36 #endif
37 
38 namespace OHOS {
39 namespace Bluetooth {
ReportDataToRss(const std::string & action,int id,const std::string & address,int pid,int uid)40 static void ReportDataToRss(const std::string &action, int id, const std::string &address, int pid, int uid)
41 {
42 #ifdef RES_SCHED_SUPPORT
43     HILOGI("report SPP_CONNECT_STATE enter");
44     std::unordered_map<std::string, std::string> payload;
45     payload["ACTION"] = action;
46     payload["ID"] = std::to_string(id);
47     payload["ADDRESS"] = address;
48     payload["PID"] = std::to_string(pid);
49     payload["UID"] = std::to_string(uid);
50     ResourceSchedule::ResSchedClient::GetInstance().ReportData(
51         OHOS::ResourceSchedule::ResType::RES_TYPE_BT_SERVICE_EVENT,
52         OHOS::ResourceSchedule::ResType::BtServiceEvent::SPP_CONNECT_STATE,
53         payload);
54     HILOGI("report SPP_CONNECT_STATE end");
55 #endif
56 }
57 
TransferInformation(const BluetoothIOppTransferInformation & other)58 BluetoothOppTransferInformation TransferInformation(const BluetoothIOppTransferInformation &other)
59 {
60     BluetoothOppTransferInformation oppTransferinformation;
61     oppTransferinformation.SetId(other.GetId());
62     oppTransferinformation.SetFileName(other.GetFileName());
63     oppTransferinformation.SetFilePath(other.GetFilePath());
64     oppTransferinformation.SetMimeType(other.GetFileType());
65     oppTransferinformation.SetDeviceName(other.GetDeviceName());
66     oppTransferinformation.SetDeviceAddress(other.GetDeviceAddress());
67     oppTransferinformation.SetResult(other.GetResult());
68     oppTransferinformation.SetStatus(other.GetStatus());
69     oppTransferinformation.SetDirection(other.GetDirection());
70     oppTransferinformation.SetTimeStamp(other.GetTimeStamp());
71     oppTransferinformation.SetCurrentBytes(other.GetCurrentBytes());
72     oppTransferinformation.SetTotalBytes(other.GetTotalBytes());
73     oppTransferinformation.SetCurrentCount(other.GetCurrentCount());
74     oppTransferinformation.SetTotalCount(other.GetTotalCount());
75     return oppTransferinformation;
76 }
77 
78 class BluetoothOppObserverImpl : public BluetoothOppObserverStub {
79 public:
BluetoothOppObserverImpl(BluetoothObserverList<OppObserver> & observers)80     explicit BluetoothOppObserverImpl(BluetoothObserverList<OppObserver> &observers)
81         : observers_(observers)
82     {}
~BluetoothOppObserverImpl()83     ~BluetoothOppObserverImpl() override
84     {}
85 
OnReceiveIncomingFileChanged(const BluetoothIOppTransferInformation & transferInformation)86     void OnReceiveIncomingFileChanged(const BluetoothIOppTransferInformation &transferInformation) override
87     {
88         BluetoothOppTransferInformation oppTransferinformation = TransferInformation(transferInformation);
89         observers_.ForEach([oppTransferinformation](std::shared_ptr<OppObserver> observer) {
90             observer->OnReceiveIncomingFileChanged(oppTransferinformation);
91         });
92         return;
93     }
94 
OnTransferStateChanged(const BluetoothIOppTransferInformation & transferInformation)95     void OnTransferStateChanged(const BluetoothIOppTransferInformation &transferInformation) override
96     {
97         BluetoothOppTransferInformation oppTransferinformation = TransferInformation(transferInformation);
98         observers_.ForEach([oppTransferinformation](std::shared_ptr<OppObserver> observer) {
99             observer->OnTransferStateChanged(oppTransferinformation);
100         });
101         return;
102     }
103 
104 private:
105     BluetoothObserverList<OppObserver> &observers_;
106     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothOppObserverImpl);
107 };
108 
109 struct Opp::impl {
implOHOS::Bluetooth::Opp::impl110     impl()
111     {
112         serviceObserverImp_ = new BluetoothOppObserverImpl(observers_);
113         profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(PROFILE_OPP_SERVER,
114         [this](sptr<IRemoteObject> remote) {
115             sptr<IBluetoothOpp> proxy = iface_cast<IBluetoothOpp>(remote);
116             CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
117             proxy->RegisterObserver(serviceObserverImp_);
118         });
119     }
~implOHOS::Bluetooth::Opp::impl120     ~impl()
121     {
122         BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
123         sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
124         CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
125         proxy->DeregisterObserver(serviceObserverImp_);
126     }
RegisterObserverOHOS::Bluetooth::Opp::impl127     void RegisterObserver(std::shared_ptr<OppObserver> &observer)
128     {
129         HILOGI("enter");
130         if (observer) {
131             observers_.Register(observer);
132         }
133     }
DeregisterObserverOHOS::Bluetooth::Opp::impl134     void DeregisterObserver(std::shared_ptr<OppObserver> &observer)
135     {
136         HILOGI("enter");
137         if (observer) {
138             observers_.Deregister(observer);
139         }
140     }
141     int32_t profileRegisterId = 0;
142 private:
143     BluetoothObserverList<OppObserver> observers_;
144     sptr<BluetoothOppObserverImpl> serviceObserverImp_ = nullptr;
145 };
146 
147 std::mutex g_oppProxyMutex;
BluetoothOppTransferInformation()148 BluetoothOppTransferInformation::BluetoothOppTransferInformation()
149 {}
150 
~BluetoothOppTransferInformation()151 BluetoothOppTransferInformation::~BluetoothOppTransferInformation()
152 {}
153 
GetId() const154 int BluetoothOppTransferInformation::GetId() const
155 {
156     return id_;
157 }
158 
GetFileName() const159 std::string BluetoothOppTransferInformation::GetFileName() const
160 {
161     return fileName_;
162 }
163 
GetFilePath() const164 std::string BluetoothOppTransferInformation::GetFilePath() const
165 {
166     return filePath_;
167 }
168 
GetMimeType() const169 std::string BluetoothOppTransferInformation::GetMimeType() const
170 {
171     return mimeType_;
172 }
173 
GetDeviceName() const174 std::string BluetoothOppTransferInformation::GetDeviceName() const
175 {
176     return deviceName_;
177 }
178 
GetDeviceAddress() const179 std::string BluetoothOppTransferInformation::GetDeviceAddress() const
180 {
181     return deviceAddress_;
182 }
183 
GetDirection() const184 int BluetoothOppTransferInformation::GetDirection() const
185 {
186     return direction_;
187 }
188 
GetStatus() const189 int BluetoothOppTransferInformation::GetStatus() const
190 {
191     return status_;
192 }
193 
GetResult() const194 int BluetoothOppTransferInformation::GetResult() const
195 {
196     return result_;
197 }
198 
GetTimeStamp() const199 uint64_t BluetoothOppTransferInformation::GetTimeStamp() const
200 {
201     return timeStamp_;
202 }
203 
GetCurrentBytes() const204 uint64_t BluetoothOppTransferInformation::GetCurrentBytes() const
205 {
206     return currentBytes_;
207 }
208 
GetTotalBytes() const209 uint64_t BluetoothOppTransferInformation::GetTotalBytes() const
210 {
211     return totalBytes_;
212 }
213 
GetCurrentCount() const214 int BluetoothOppTransferInformation::GetCurrentCount() const
215 {
216     return currentCount_;
217 }
218 
GetTotalCount() const219 int BluetoothOppTransferInformation::GetTotalCount() const
220 {
221     return totalCount_;
222 }
223 
SetId(int id)224 void BluetoothOppTransferInformation::SetId(int id)
225 {
226     id_ = id;
227 }
228 
SetFileName(std::string fileName)229 void BluetoothOppTransferInformation::SetFileName(std::string fileName)
230 {
231     fileName_ = fileName;
232 }
233 
SetFilePath(std::string filePath)234 void BluetoothOppTransferInformation::SetFilePath(std::string filePath)
235 {
236     filePath_ = filePath;
237 }
238 
SetMimeType(std::string mimeType)239 void BluetoothOppTransferInformation::SetMimeType(std::string mimeType)
240 {
241     mimeType_ = mimeType;
242 }
243 
SetDeviceName(std::string deviceName)244 void BluetoothOppTransferInformation::SetDeviceName(std::string deviceName)
245 {
246     deviceName_ = deviceName;
247 }
248 
SetDeviceAddress(std::string deviceAddress)249 void BluetoothOppTransferInformation::SetDeviceAddress(std::string deviceAddress)
250 {
251     deviceAddress_ = deviceAddress;
252 }
253 
SetDirection(int direction)254 void BluetoothOppTransferInformation::SetDirection(int direction)
255 {
256     direction_ = direction;
257 }
258 
SetStatus(int status)259 void BluetoothOppTransferInformation::SetStatus(int status)
260 {
261     status_ = status;
262 }
263 
SetResult(int result)264 void BluetoothOppTransferInformation::SetResult(int result)
265 {
266     result_ = result;
267 }
268 
SetTimeStamp(uint64_t timeStamp)269 void BluetoothOppTransferInformation::SetTimeStamp(uint64_t timeStamp)
270 {
271     timeStamp_ = timeStamp;
272 }
273 
SetCurrentBytes(uint64_t currentBytes)274 void BluetoothOppTransferInformation::SetCurrentBytes(uint64_t currentBytes)
275 {
276     currentBytes_ = currentBytes;
277 }
278 
SetTotalBytes(uint64_t totalBytes)279 void BluetoothOppTransferInformation::SetTotalBytes(uint64_t totalBytes)
280 {
281     totalBytes_ = totalBytes;
282 }
283 
SetCurrentCount(int currentCount)284 void BluetoothOppTransferInformation::SetCurrentCount(int currentCount)
285 {
286     currentCount_ = currentCount;
287 }
288 
SetTotalCount(int totalCount)289 void BluetoothOppTransferInformation::SetTotalCount(int totalCount)
290 {
291     totalCount_ = totalCount;
292 }
293 
BluetoothOppFileHolder()294 BluetoothOppFileHolder::BluetoothOppFileHolder()
295 {}
296 
BluetoothOppFileHolder(const std::string & filePath,const int64_t & fileSize,const int32_t & fileFd)297 BluetoothOppFileHolder::BluetoothOppFileHolder(const std::string &filePath,
298     const int64_t &fileSize, const int32_t &fileFd)
299 {
300     filePath_ = filePath;
301     fileSize_ = fileSize;
302     fileFd_ = fileFd;
303 }
304 
~BluetoothOppFileHolder()305 BluetoothOppFileHolder::~BluetoothOppFileHolder()
306 {}
307 
GetFilePath() const308 std::string BluetoothOppFileHolder::GetFilePath() const
309 {
310     return filePath_;
311 }
312 
GetFileSize() const313 int64_t BluetoothOppFileHolder::GetFileSize() const
314 {
315     return fileSize_;
316 }
317 
GetFileFd() const318 int32_t BluetoothOppFileHolder::GetFileFd() const
319 {
320     return fileFd_;
321 }
322 
SetFilePath(const std::string & filePath)323 void BluetoothOppFileHolder::SetFilePath(const std::string &filePath)
324 {
325     filePath_ = filePath;
326 }
327 
SetFileFd(const int32_t & fileFd)328 void BluetoothOppFileHolder::SetFileFd(const int32_t &fileFd)
329 {
330     fileFd_ = fileFd;
331 }
332 
SetFileSize(const int64_t & fileSize)333 void BluetoothOppFileHolder::SetFileSize(const int64_t &fileSize)
334 {
335     fileSize_ = fileSize;
336 }
337 
Opp()338 Opp::Opp()
339 {
340     pimpl = std::make_unique<impl>();
341 }
342 
~Opp()343 Opp::~Opp()
344 {}
345 
GetProfile()346 Opp *Opp::GetProfile()
347 {
348 #ifdef DTFUZZ_TEST
349     static BluetoothNoDestructor<Opp> instance;
350     return instance.get();
351 #else
352     static Opp instance;
353     return &instance;
354 #endif
355 }
356 
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRemoteDevice> & result) const357 int32_t Opp::GetDevicesByStates(const std::vector<int32_t> &states,
358     std::vector<BluetoothRemoteDevice> &result) const
359 {
360     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
361     sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
362     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
363 
364     std::vector<BluetoothRawAddress> rawAddress {};
365     int32_t ret = proxy->GetDevicesByStates(states, rawAddress);
366     CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "inner error");
367 
368     for (BluetoothRawAddress rawAddr : rawAddress) {
369         BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
370         result.push_back(device);
371     }
372     return BT_NO_ERROR;
373 }
374 
GetDeviceState(const BluetoothRemoteDevice & device,int32_t & state) const375 int32_t Opp::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state) const
376 {
377     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
378     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
379     sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
380     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
381     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
382 
383     return proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
384 }
385 
SendFile(std::string device,std::vector<BluetoothOppFileHolder> fileHolders,bool & result)386 int32_t Opp::SendFile(std::string device, std::vector<BluetoothOppFileHolder> fileHolders, bool& result)
387 {
388     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
389     sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
390     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
391     std::vector<BluetoothIOppTransferFileHolder> fileHoldersInterface;
392     for (BluetoothOppFileHolder fileHolder : fileHolders) {
393         fileHoldersInterface.push_back(BluetoothIOppTransferFileHolder(fileHolder.GetFilePath(),
394             fileHolder.GetFileSize(), fileHolder.GetFileFd()));
395     }
396     int ret = proxy->SendFile(device, fileHoldersInterface, result);
397     HILOGI("send file result is : %{public}d", result);
398     return ret;
399 }
400 
SetLastReceivedFileUri(const std::string & uri)401 int32_t Opp::SetLastReceivedFileUri(const std::string &uri)
402 {
403     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
404     sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
405     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
406     int ret = proxy->SetLastReceivedFileUri(uri);
407     HILOGI("setLastReceivedFileUri is : %{public}d", ret);
408     return ret;
409 }
410 
SetIncomingFileConfirmation(bool accept,int32_t fileFd)411 int32_t Opp::SetIncomingFileConfirmation(bool accept, int32_t fileFd)
412 {
413     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
414 
415     sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
416     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
417     int ret = proxy->SetIncomingFileConfirmation(accept, fileFd);
418     HILOGI("setIncomingFileConfirmation result is : %{public}d", ret);
419     return ret;
420 }
421 
GetCurrentTransferInformation(BluetoothOppTransferInformation & transferInformation)422 int32_t Opp::GetCurrentTransferInformation(BluetoothOppTransferInformation &transferInformation)
423 {
424     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
425     sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
426     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
427 
428     BluetoothIOppTransferInformation oppInformation;
429     int ret = proxy->GetCurrentTransferInformation(oppInformation);
430     HILOGI("getCurrentTransferInformation result is : %{public}d", ret);
431     if (ret == BT_NO_ERROR) {
432         transferInformation = TransferInformation(oppInformation);
433     }
434     return ret;
435 }
436 
CancelTransfer(bool & result)437 int32_t Opp::CancelTransfer(bool &result)
438 {
439     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
440     sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
441     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
442 
443     int ret = proxy->CancelTransfer();
444     result = (ret == BT_NO_ERROR) ? true : false;
445     HILOGI("cancelTransfer result is : %{public}d", ret);
446     return ret;
447 }
448 
RegisterObserver(std::shared_ptr<OppObserver> observer)449 void Opp::RegisterObserver(std::shared_ptr<OppObserver> observer)
450 {
451     HILOGI("enter");
452     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
453     pimpl->RegisterObserver(observer);
454 #ifndef CROSS_PLATFORM
455     ReportDataToRss("connect", -1, "empty", IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
456 #endif
457 }
458 
DeregisterObserver(std::shared_ptr<OppObserver> observer)459 void Opp::DeregisterObserver(std::shared_ptr<OppObserver> observer)
460 {
461     HILOGI("enter");
462     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
463     pimpl->DeregisterObserver(observer);
464 #ifndef CROSS_PLATFORM
465     ReportDataToRss("close", -1, "empty", IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
466 #endif
467 }
468 }  // namespace Bluetooth
469 }  // namespace OHOS