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