1 /*
2 * Copyright (C) 2021 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 <string>
17
18 #include "bluetooth_raw_address.h"
19 #include "bluetooth_def.h"
20 #include "bluetooth_host.h"
21 #include "bluetooth_host_proxy.h"
22 #include "bluetooth_log.h"
23 #include "bluetooth_utils.h"
24 #include "bluetooth_remote_device.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27
28 namespace OHOS {
29 namespace Bluetooth {
GetHostProxy()30 sptr<BluetoothHostProxy> GetHostProxy()
31 {
32 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
33 sptr<IRemoteObject> remote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
34
35 sptr<BluetoothHostProxy> hostProxy = new BluetoothHostProxy(remote);
36 return hostProxy;
37 }
38
BluetoothRemoteDevice(const std::string & addr,const int transport)39 BluetoothRemoteDevice::BluetoothRemoteDevice(const std::string &addr, const int transport)
40 {
41 address_ = addr;
42 transport_ = transport;
43 }
44
GetDeviceType() const45 int BluetoothRemoteDevice::GetDeviceType() const
46 {
47 HILOGI("enter");
48 int type = 0;
49 if (!IsValidBluetoothRemoteDevice()) {
50 HILOGI("Invalid remote device.");
51 return type;
52 }
53
54 sptr<BluetoothHostProxy> proxy = GetHostProxy();
55 if (proxy == nullptr) {
56 HILOGI("BT host server not created.");
57 return type;
58 }
59
60 return proxy->GetDeviceType(transport_, address_);
61 }
62
IsValidBluetoothRemoteDevice() const63 bool BluetoothRemoteDevice::IsValidBluetoothRemoteDevice() const
64 {
65 if (!BluetoothHost::IsValidBluetoothAddr(address_)) {
66 HILOGI("invalid bluetooth addr, address_: %{public}s", GetEncryptAddr(address_).c_str());
67 return false;
68 }
69
70 if ((transport_ != BT_TRANSPORT_BREDR) && (transport_ != BT_TRANSPORT_BLE)) {
71 HILOGI("invalid transport type.");
72 return false;
73 }
74 return true;
75 }
GetTransportType() const76 int BluetoothRemoteDevice::GetTransportType() const
77 {
78 return transport_;
79 }
80
GetPhonebookPermission() const81 int BluetoothRemoteDevice::GetPhonebookPermission() const
82 {
83 HILOGI("enter");
84 if (!IsValidBluetoothRemoteDevice()) {
85 HILOGW("Invalid remote device.");
86 return INVALID_VALUE;
87 }
88 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
89 if (hostProxy == nullptr) {
90 HILOGE("fails: no proxy");
91 return INVALID_VALUE;
92 }
93
94 return hostProxy->GetPhonebookPermission(address_);
95 }
96
SetPhonebookPermission(int permission)97 bool BluetoothRemoteDevice::SetPhonebookPermission(int permission)
98 {
99 HILOGI("enter, permission: %{public}d", permission);
100 if (!IsValidBluetoothRemoteDevice()) {
101 HILOGW("Invalid remote device.");
102 return false;
103 }
104 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
105 if (hostProxy == nullptr) {
106 HILOGE("fails: no proxy");
107 return false;
108 }
109 return hostProxy->SetPhonebookPermission(address_, permission);
110 }
111
GetMessagePermission() const112 int BluetoothRemoteDevice::GetMessagePermission() const
113 {
114 HILOGI("enter");
115 if (!IsValidBluetoothRemoteDevice()) {
116 HILOGW("Invalid remote device");
117 return INVALID_VALUE;
118 }
119 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
120 if (hostProxy == nullptr) {
121 HILOGE("fails: no proxy");
122 return INVALID_VALUE;
123 }
124 return hostProxy->GetMessagePermission(address_);
125 }
126
SetMessagePermission(int permission)127 bool BluetoothRemoteDevice::SetMessagePermission(int permission)
128 {
129 HILOGI("enter, permission: %{public}d", permission);
130 if (!IsValidBluetoothRemoteDevice()) {
131 HILOGW("Invalid remote device");
132 return false;
133 }
134 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
135 if (hostProxy == nullptr) {
136 HILOGE("fails: no proxy");
137 return false;
138 }
139 return hostProxy->SetMessagePermission(address_, permission);
140 }
141
GetPowerMode(void) const142 int BluetoothRemoteDevice::GetPowerMode(void) const
143 {
144 HILOGI("enter");
145 if (!IsValidBluetoothRemoteDevice()) {
146 HILOGW("Invalid remote device");
147 return INVALID_VALUE;
148 }
149 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
150 if (hostProxy == nullptr) {
151 HILOGE("fails: no proxy");
152 return INVALID_VALUE;
153 }
154 return hostProxy->GetPowerMode(address_);
155 }
156
GetDeviceName() const157 std::string BluetoothRemoteDevice::GetDeviceName() const
158 {
159 if (!IsValidBluetoothRemoteDevice()) {
160 HILOGW("Invalid remote device");
161 return INVALID_NAME;
162 }
163 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
164 if (hostProxy == nullptr) {
165 HILOGE("fails: no proxy");
166 return INVALID_NAME;
167 }
168 std::string name = INVALID_NAME;
169 hostProxy->GetDeviceName(transport_, address_, name);
170 return name;
171 }
172
GetDeviceName(std::string & name) const173 int BluetoothRemoteDevice::GetDeviceName(std::string &name) const
174 {
175 if (!IsValidBluetoothRemoteDevice()) {
176 HILOGW("Invalid remote device");
177 return BT_ERR_INTERNAL_ERROR;
178 }
179 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
180 if (hostProxy == nullptr) {
181 HILOGE("fails: no proxy");
182 return BT_ERR_INTERNAL_ERROR;
183 }
184 return hostProxy->GetDeviceName(transport_, address_, name);
185 }
186
GetDeviceAlias() const187 std::string BluetoothRemoteDevice::GetDeviceAlias() const
188 {
189 HILOGI("enter");
190 if (!IsValidBluetoothRemoteDevice()) {
191 HILOGW("Invalid remote device");
192 return INVALID_NAME;
193 }
194 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
195 if (hostProxy == nullptr) {
196 HILOGE("fails: no proxy");
197 return INVALID_NAME;
198 }
199 return hostProxy->GetDeviceAlias(address_);
200 }
201
SetDeviceAlias(const std::string & aliasName)202 bool BluetoothRemoteDevice::SetDeviceAlias(const std::string &aliasName)
203 {
204 HILOGI("enter");
205 if (!IsValidBluetoothRemoteDevice()) {
206 HILOGW("Invalid remote device");
207 return false;
208 }
209 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
210 if (hostProxy == nullptr) {
211 HILOGE("fails: no proxy");
212 return false;
213 }
214 return hostProxy->SetDeviceAlias(address_, aliasName);
215 }
216
GetDeviceBatteryLevel() const217 int BluetoothRemoteDevice::GetDeviceBatteryLevel() const
218 {
219 HILOGI("enter");
220 if (!IsValidBluetoothRemoteDevice()) {
221 HILOGW("Invalid remote device");
222 return INVALID_VALUE;
223 }
224 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
225 if (hostProxy == nullptr) {
226 HILOGE("fails: no proxy");
227 return INVALID_VALUE;
228 }
229 return hostProxy->GetDeviceBatteryLevel(address_);
230 }
231
GetPairState() const232 int BluetoothRemoteDevice::GetPairState() const
233 {
234 HILOGI("enter");
235 if (!IsValidBluetoothRemoteDevice()) {
236 HILOGW("Invalid remote device");
237 return INVALID_VALUE;
238 }
239 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
240 if (hostProxy == nullptr) {
241 HILOGE("fails: no proxy");
242 return INVALID_VALUE;
243 }
244 return hostProxy->GetPairState(transport_, address_);
245 }
246
StartPair()247 int BluetoothRemoteDevice::StartPair()
248 {
249 HILOGI("enter");
250 if (!IsValidBluetoothRemoteDevice()) {
251 HILOGW("Invalid remote device");
252 return BT_ERR_INTERNAL_ERROR;
253 }
254 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
255 if (hostProxy == nullptr) {
256 HILOGE("fails: no proxy");
257 return BT_ERR_INTERNAL_ERROR;
258 }
259 return hostProxy->StartPair(transport_, address_);
260 }
261
CancelPairing()262 bool BluetoothRemoteDevice::CancelPairing()
263 {
264 HILOGI("enter");
265 if (!IsValidBluetoothRemoteDevice()) {
266 HILOGW("Invalid remote device");
267 return false;
268 }
269 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
270 if (hostProxy == nullptr) {
271 HILOGE("fails: no proxy");
272 return false;
273 }
274 return hostProxy->CancelPairing(transport_, address_);
275 }
276
IsBondedFromLocal() const277 bool BluetoothRemoteDevice::IsBondedFromLocal() const
278 {
279 HILOGI("enter");
280 if (!IsValidBluetoothRemoteDevice()) {
281 HILOGW("Invalid remote device");
282 return false;
283 }
284 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
285 if (hostProxy == nullptr) {
286 HILOGE("fails: no proxy");
287 return false;
288 }
289 return hostProxy->IsBondedFromLocal(transport_, address_);
290 }
291
IsAclConnected() const292 bool BluetoothRemoteDevice::IsAclConnected() const
293 {
294 HILOGI("enter");
295 if (!IsValidBluetoothRemoteDevice()) {
296 HILOGW("Invalid remote device");
297 return false;
298 }
299 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
300 if (hostProxy == nullptr) {
301 HILOGE("fails: no proxy");
302 return false;
303 }
304 return hostProxy->IsAclConnected(transport_, address_);
305 }
306
IsAclEncrypted() const307 bool BluetoothRemoteDevice::IsAclEncrypted() const
308 {
309 HILOGI("enter");
310 if (!IsValidBluetoothRemoteDevice()) {
311 HILOGW("Invalid remote device");
312 return false;
313 }
314 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
315 if (hostProxy == nullptr) {
316 HILOGE("fails: no proxy");
317 return false;
318 }
319 return hostProxy->IsAclEncrypted(transport_, address_);
320 }
321
GetDeviceClass(int & cod) const322 int BluetoothRemoteDevice::GetDeviceClass(int &cod) const
323 {
324 HILOGI("enter");
325 if (!IsValidBluetoothRemoteDevice()) {
326 HILOGW("Invalid remote device");
327 return BT_ERR_INTERNAL_ERROR;
328 }
329 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
330 if (hostProxy == nullptr) {
331 HILOGE("fails: no proxy");
332 return BT_ERR_INTERNAL_ERROR;
333 }
334 int ret = hostProxy->GetDeviceClass(address_, cod);
335 return ret;
336 }
337
GetDeviceUuids() const338 std::vector<ParcelUuid> BluetoothRemoteDevice::GetDeviceUuids() const
339 {
340 HILOGI("enter");
341 std::vector<ParcelUuid> parcelUuids;
342 if (!IsValidBluetoothRemoteDevice()) {
343 HILOGW("Invalid remote device");
344 return parcelUuids;
345 }
346 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
347 if (hostProxy == nullptr) {
348 HILOGE("fails: no proxy");
349 return parcelUuids;
350 }
351 std::vector<bluetooth::Uuid> uuids = hostProxy->GetDeviceUuids(transport_, address_);
352 for (auto iter : uuids) {
353 ParcelUuid parcelUuid = UUID::ConvertFrom128Bits(iter.ConvertTo128Bits());
354 parcelUuids.push_back(parcelUuid);
355 }
356 return parcelUuids;
357 }
358
SetDevicePin(const std::string & pin)359 bool BluetoothRemoteDevice::SetDevicePin(const std::string &pin)
360 {
361 HILOGI("enter");
362 if (!IsValidBluetoothRemoteDevice()) {
363 HILOGW("Invalid remote device");
364 return false;
365 }
366 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
367 if (hostProxy == nullptr) {
368 HILOGE("fails: no proxy");
369 return false;
370 }
371 return hostProxy->SetDevicePin(address_, pin);
372 }
373
SetDevicePairingConfirmation(bool accept)374 int BluetoothRemoteDevice::SetDevicePairingConfirmation(bool accept)
375 {
376 HILOGI("enter, accept: %{public}d", accept);
377 if (!IsValidBluetoothRemoteDevice()) {
378 HILOGW("Invalid remote device");
379 return BT_ERR_INTERNAL_ERROR;
380 }
381 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
382 if (hostProxy == nullptr) {
383 HILOGE("fails: no proxy");
384 return BT_ERR_INTERNAL_ERROR;
385 }
386 return hostProxy->SetDevicePairingConfirmation(transport_, address_, accept);
387 }
388
SetDevicePasskey(int passkey,bool accept)389 bool BluetoothRemoteDevice::SetDevicePasskey(int passkey, bool accept)
390 {
391 HILOGI("enter, accept: %{public}d", accept);
392 if (!IsValidBluetoothRemoteDevice()) {
393 HILOGW("Invalid remote device");
394 return false;
395 }
396 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
397 if (hostProxy == nullptr) {
398 HILOGE("fails: no proxy");
399 return false;
400 }
401 return hostProxy->SetDevicePasskey(transport_, address_, passkey, accept);
402 }
403
PairRequestReply(bool accept)404 bool BluetoothRemoteDevice::PairRequestReply(bool accept)
405 {
406 HILOGI("enter, accept: %{public}d", accept);
407 if (!IsValidBluetoothRemoteDevice()) {
408 HILOGW("Invalid remote device");
409 return false;
410 }
411 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
412 if (hostProxy == nullptr) {
413 HILOGE("fails: no proxy");
414 return false;
415 }
416 return hostProxy->PairRequestReply(transport_, address_, accept);
417 }
418
ReadRemoteRssiValue()419 bool BluetoothRemoteDevice::ReadRemoteRssiValue()
420 {
421 HILOGI("enter");
422 if (!IsValidBluetoothRemoteDevice()) {
423 HILOGW("Invalid remote device");
424 return false;
425 }
426 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
427 if (hostProxy == nullptr) {
428 HILOGE("fails: no proxy");
429 return false;
430 }
431 return hostProxy->ReadRemoteRssiValue(address_);
432 }
433 } // namespace Bluetooth
434 } // namespace OHOS
435