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.SetFailedReason(other.GetFailedReason());
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 return oppTransferinformation;
50 }
51
52 class BluetoothOppObserverImpl : public BluetoothOppObserverStub {
53 public:
BluetoothOppObserverImpl(BluetoothObserverList<OppObserver> & observers)54 explicit BluetoothOppObserverImpl(BluetoothObserverList<OppObserver> &observers)
55 : observers_(observers)
56 {}
~BluetoothOppObserverImpl()57 ~BluetoothOppObserverImpl() override
58 {}
59
OnReceiveIncomingFileChanged(const BluetoothIOppTransferInformation & transferInformation)60 void OnReceiveIncomingFileChanged(const BluetoothIOppTransferInformation &transferInformation) override
61 {
62 BluetoothOppTransferInformation oppTransferinformation = TransferInformation(transferInformation);
63 observers_.ForEach([oppTransferinformation](std::shared_ptr<OppObserver> observer) {
64 observer->OnReceiveIncomingFileChanged(oppTransferinformation);
65 });
66 return;
67 }
68
OnTransferStateChanged(const BluetoothIOppTransferInformation & transferInformation)69 void OnTransferStateChanged(const BluetoothIOppTransferInformation &transferInformation) override
70 {
71 BluetoothOppTransferInformation oppTransferinformation = TransferInformation(transferInformation);
72 observers_.ForEach([oppTransferinformation](std::shared_ptr<OppObserver> observer) {
73 observer->OnTransferStateChanged(oppTransferinformation);
74 });
75 return;
76 }
77
78 private:
79 BluetoothObserverList<OppObserver> &observers_;
80 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothOppObserverImpl);
81 };
82
83 struct Opp::impl {
implOHOS::Bluetooth::Opp::impl84 impl()
85 {
86 serviceObserverImp_ = new BluetoothOppObserverImpl(observers_);
87 profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(PROFILE_OPP_SERVER,
88 [this](sptr<IRemoteObject> remote) {
89 sptr<IBluetoothOpp> proxy = iface_cast<IBluetoothOpp>(remote);
90 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
91 proxy->RegisterObserver(serviceObserverImp_);
92 });
93 }
~implOHOS::Bluetooth::Opp::impl94 ~impl()
95 {
96 BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
97 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
98 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
99 proxy->DeregisterObserver(serviceObserverImp_);
100 }
RegisterObserverOHOS::Bluetooth::Opp::impl101 void RegisterObserver(std::shared_ptr<OppObserver> &observer)
102 {
103 HILOGI("enter");
104 if (observer) {
105 observers_.Register(observer);
106 }
107 }
DeregisterObserverOHOS::Bluetooth::Opp::impl108 void DeregisterObserver(std::shared_ptr<OppObserver> &observer)
109 {
110 HILOGI("enter");
111 if (observer) {
112 observers_.Deregister(observer);
113 }
114 }
115 int32_t profileRegisterId = 0;
116 private:
117 BluetoothObserverList<OppObserver> observers_;
118 sptr<BluetoothOppObserverImpl> serviceObserverImp_ = nullptr;
119 };
120
121 std::mutex g_oppProxyMutex;
BluetoothOppTransferInformation()122 BluetoothOppTransferInformation::BluetoothOppTransferInformation()
123 {}
124
~BluetoothOppTransferInformation()125 BluetoothOppTransferInformation::~BluetoothOppTransferInformation()
126 {}
127
GetId() const128 int BluetoothOppTransferInformation::GetId() const
129 {
130 return id_;
131 }
132
GetFileName() const133 std::string BluetoothOppTransferInformation::GetFileName() const
134 {
135 return fileName_;
136 }
137
GetFilePath() const138 std::string BluetoothOppTransferInformation::GetFilePath() const
139 {
140 return filePath_;
141 }
142
GetMimeType() const143 std::string BluetoothOppTransferInformation::GetMimeType() const
144 {
145 return mimeType_;
146 }
147
GetDeviceName() const148 std::string BluetoothOppTransferInformation::GetDeviceName() const
149 {
150 return deviceName_;
151 }
152
GetDeviceAddress() const153 std::string BluetoothOppTransferInformation::GetDeviceAddress() const
154 {
155 return deviceAddress_;
156 }
157
GetDirection() const158 int BluetoothOppTransferInformation::GetDirection() const
159 {
160 return direction_;
161 }
162
GetStatus() const163 int BluetoothOppTransferInformation::GetStatus() const
164 {
165 return status_;
166 }
167
GetFailedReason() const168 int BluetoothOppTransferInformation::GetFailedReason() const
169 {
170 return failedReason_;
171 }
172
GetTimeStamp() const173 uint64_t BluetoothOppTransferInformation::GetTimeStamp() const
174 {
175 return timeStamp_;
176 }
177
GetCurrentBytes() const178 uint64_t BluetoothOppTransferInformation::GetCurrentBytes() const
179 {
180 return currentBytes_;
181 }
182
GetTotalBytes() const183 uint64_t BluetoothOppTransferInformation::GetTotalBytes() const
184 {
185 return totalBytes_;
186 }
187
SetId(int id)188 void BluetoothOppTransferInformation::SetId(int id)
189 {
190 id_ = id;
191 }
192
SetFileName(std::string fileName)193 void BluetoothOppTransferInformation::SetFileName(std::string fileName)
194 {
195 fileName_ = fileName;
196 }
197
SetFilePath(std::string filePath)198 void BluetoothOppTransferInformation::SetFilePath(std::string filePath)
199 {
200 filePath_ = filePath;
201 }
202
SetMimeType(std::string mimeType)203 void BluetoothOppTransferInformation::SetMimeType(std::string mimeType)
204 {
205 mimeType_ = mimeType;
206 }
207
SetDeviceName(std::string deviceName)208 void BluetoothOppTransferInformation::SetDeviceName(std::string deviceName)
209 {
210 deviceName_ = deviceName;
211 }
212
SetDeviceAddress(std::string deviceAddress)213 void BluetoothOppTransferInformation::SetDeviceAddress(std::string deviceAddress)
214 {
215 deviceAddress_ = deviceAddress;
216 }
217
SetDirection(int direction)218 void BluetoothOppTransferInformation::SetDirection(int direction)
219 {
220 direction_ = direction;
221 }
222
SetStatus(int status)223 void BluetoothOppTransferInformation::SetStatus(int status)
224 {
225 status_ = status;
226 }
227
SetFailedReason(int failedReason)228 void BluetoothOppTransferInformation::SetFailedReason(int failedReason)
229 {
230 failedReason_ = failedReason;
231 }
232
SetTimeStamp(uint64_t timeStamp)233 void BluetoothOppTransferInformation::SetTimeStamp(uint64_t timeStamp)
234 {
235 timeStamp_ = timeStamp;
236 }
237
SetCurrentBytes(uint64_t currentBytes)238 void BluetoothOppTransferInformation::SetCurrentBytes(uint64_t currentBytes)
239 {
240 currentBytes_ = currentBytes;
241 }
242
SetTotalBytes(uint64_t totalBytes)243 void BluetoothOppTransferInformation::SetTotalBytes(uint64_t totalBytes)
244 {
245 totalBytes_ = totalBytes;
246 }
247
Opp()248 Opp::Opp()
249 {
250 pimpl = std::make_unique<impl>();
251 }
252
~Opp()253 Opp::~Opp()
254 {}
255
GetProfile()256 Opp *Opp::GetProfile()
257 {
258 #ifdef DTFUZZ_TEST
259 static BluetoothNoDestructor<Opp> instance;
260 return instance.get();
261 #else
262 static Opp instance;
263 return &instance;
264 #endif
265 }
266
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRemoteDevice> & result) const267 int32_t Opp::GetDevicesByStates(const std::vector<int32_t> &states,
268 std::vector<BluetoothRemoteDevice> &result) const
269 {
270 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
271 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
272 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
273
274 std::vector<BluetoothRawAddress> rawAddress {};
275 int32_t ret = proxy->GetDevicesByStates(states, rawAddress);
276 CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "inner error");
277
278 for (BluetoothRawAddress rawAddr : rawAddress) {
279 BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
280 result.push_back(device);
281 }
282 return BT_NO_ERROR;
283 }
284
GetDeviceState(const BluetoothRemoteDevice & device,int32_t & state) const285 int32_t Opp::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state) const
286 {
287 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
288 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
289 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
290 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
291 CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device param error");
292
293 return proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
294 }
295
SendFile(std::string device,std::vector<std::string> filePaths,std::vector<std::string> mimeTypes,bool & result)296 int32_t Opp::SendFile(std::string device, std::vector<std::string> filePaths,
297 std::vector<std::string> mimeTypes, bool& result)
298 {
299 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
300 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
301 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
302 int ret = proxy->SendFile(device, filePaths, mimeTypes, result);
303 HILOGI("send file result is : %{public}d", result);
304 return ret;
305 }
306
SetIncomingFileConfirmation(bool accept)307 int32_t Opp::SetIncomingFileConfirmation(bool accept)
308 {
309 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
310
311 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
312 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
313 int ret = proxy->SetIncomingFileConfirmation(accept);
314 HILOGI("setIncomingFileConfirmation result is : %{public}d", ret);
315 return ret;
316 }
317
GetCurrentTransferInformation(BluetoothOppTransferInformation & transferInformation)318 int32_t Opp::GetCurrentTransferInformation(BluetoothOppTransferInformation &transferInformation)
319 {
320 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
321 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
322 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
323
324 BluetoothIOppTransferInformation oppInformation;
325 int ret = proxy->GetCurrentTransferInformation(oppInformation);
326 HILOGI("getCurrentTransferInformation result is : %{public}d", ret);
327 if (ret == BT_NO_ERROR) {
328 transferInformation = TransferInformation(oppInformation);
329 }
330 return ret;
331 }
332
CancelTransfer(bool & result)333 int32_t Opp::CancelTransfer(bool &result)
334 {
335 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
336 sptr<IBluetoothOpp> proxy = GetRemoteProxy<IBluetoothOpp>(PROFILE_OPP_SERVER);
337 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
338
339 int ret = proxy->CancelTransfer(result);
340 HILOGI("cancelTransfer result is : %{public}d", ret);
341 return ret;
342 }
343
RegisterObserver(std::shared_ptr<OppObserver> observer)344 void Opp::RegisterObserver(std::shared_ptr<OppObserver> observer)
345 {
346 HILOGD("enter");
347 CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
348 pimpl->RegisterObserver(observer);
349 }
350
DeregisterObserver(std::shared_ptr<OppObserver> observer)351 void Opp::DeregisterObserver(std::shared_ptr<OppObserver> observer)
352 {
353 HILOGD("enter");
354 CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
355 pimpl->DeregisterObserver(observer);
356 }
357 } // namespace Bluetooth
358 } // namespace OHOS