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 if (!IS_BT_ENABLED()) {
164 HILOGE("bluetooth is off.");
165 return INVALID_NAME;
166 }
167 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
168 if (hostProxy == nullptr) {
169 HILOGE("fails: no proxy");
170 return INVALID_NAME;
171 }
172 std::string name = INVALID_NAME;
173 hostProxy->GetDeviceName(transport_, address_, name);
174 return name;
175 }
176
GetDeviceName(std::string & name) const177 int BluetoothRemoteDevice::GetDeviceName(std::string &name) const
178 {
179 if (!IsValidBluetoothRemoteDevice()) {
180 HILOGW("Invalid remote device");
181 return BT_ERR_INTERNAL_ERROR;
182 }
183 if (!IS_BT_ENABLED()) {
184 HILOGE("bluetooth is off.");
185 return BT_ERR_INVALID_STATE;
186 }
187 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
188 if (hostProxy == nullptr) {
189 HILOGE("fails: no proxy");
190 return BT_ERR_INTERNAL_ERROR;
191 }
192 return hostProxy->GetDeviceName(transport_, address_, name);
193 }
194
GetDeviceAlias() const195 std::string BluetoothRemoteDevice::GetDeviceAlias() const
196 {
197 HILOGI("enter");
198 if (!IsValidBluetoothRemoteDevice()) {
199 HILOGW("Invalid remote device");
200 return INVALID_NAME;
201 }
202 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
203 if (hostProxy == nullptr) {
204 HILOGE("fails: no proxy");
205 return INVALID_NAME;
206 }
207 return hostProxy->GetDeviceAlias(address_);
208 }
209
SetDeviceAlias(const std::string & aliasName)210 bool BluetoothRemoteDevice::SetDeviceAlias(const std::string &aliasName)
211 {
212 HILOGI("enter");
213 if (!IsValidBluetoothRemoteDevice()) {
214 HILOGW("Invalid remote device");
215 return false;
216 }
217 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
218 if (hostProxy == nullptr) {
219 HILOGE("fails: no proxy");
220 return false;
221 }
222 return hostProxy->SetDeviceAlias(address_, aliasName);
223 }
224
GetDeviceBatteryLevel() const225 int BluetoothRemoteDevice::GetDeviceBatteryLevel() const
226 {
227 HILOGI("enter");
228 if (!IsValidBluetoothRemoteDevice()) {
229 HILOGW("Invalid remote device");
230 return INVALID_VALUE;
231 }
232 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
233 if (hostProxy == nullptr) {
234 HILOGE("fails: no proxy");
235 return INVALID_VALUE;
236 }
237 return hostProxy->GetDeviceBatteryLevel(address_);
238 }
239
GetPairState() const240 int BluetoothRemoteDevice::GetPairState() const
241 {
242 HILOGI("enter");
243 if (!IsValidBluetoothRemoteDevice()) {
244 HILOGW("Invalid remote device");
245 return INVALID_VALUE;
246 }
247 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
248 if (hostProxy == nullptr) {
249 HILOGE("fails: no proxy");
250 return INVALID_VALUE;
251 }
252 return hostProxy->GetPairState(transport_, address_);
253 }
254
StartPair()255 int BluetoothRemoteDevice::StartPair()
256 {
257 HILOGI("enter");
258 if (!IsValidBluetoothRemoteDevice()) {
259 HILOGW("Invalid remote device");
260 return BT_ERR_INTERNAL_ERROR;
261 }
262 if (!IS_BT_ENABLED()) {
263 HILOGE("bluetooth is off.");
264 return BT_ERR_INVALID_STATE;
265 }
266 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
267 if (hostProxy == nullptr) {
268 HILOGE("fails: no proxy");
269 return BT_ERR_INTERNAL_ERROR;
270 }
271 return hostProxy->StartPair(transport_, address_);
272 }
273
CancelPairing()274 int BluetoothRemoteDevice::CancelPairing()
275 {
276 HILOGI("enter");
277 if (!IsValidBluetoothRemoteDevice()) {
278 HILOGW("Invalid remote device");
279 return BT_ERR_INTERNAL_ERROR;
280 }
281 if (!IS_BT_ENABLED()) {
282 HILOGE("bluetooth is off.");
283 return BT_ERR_INVALID_STATE;
284 }
285 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
286 if (hostProxy == nullptr) {
287 HILOGE("fails: no proxy");
288 return BT_ERR_INTERNAL_ERROR;
289 }
290 if (hostProxy->CancelPairing(transport_, address_)) {
291 return BT_NO_ERROR;
292 }
293 return BT_ERR_INTERNAL_ERROR;
294 }
295
IsBondedFromLocal() const296 bool BluetoothRemoteDevice::IsBondedFromLocal() const
297 {
298 HILOGI("enter");
299 if (!IsValidBluetoothRemoteDevice()) {
300 HILOGW("Invalid remote device");
301 return false;
302 }
303 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
304 if (hostProxy == nullptr) {
305 HILOGE("fails: no proxy");
306 return false;
307 }
308 return hostProxy->IsBondedFromLocal(transport_, address_);
309 }
310
IsAclConnected() const311 bool BluetoothRemoteDevice::IsAclConnected() const
312 {
313 HILOGI("enter");
314 if (!IsValidBluetoothRemoteDevice()) {
315 HILOGW("Invalid remote device");
316 return false;
317 }
318 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
319 if (hostProxy == nullptr) {
320 HILOGE("fails: no proxy");
321 return false;
322 }
323 return hostProxy->IsAclConnected(transport_, address_);
324 }
325
IsAclEncrypted() const326 bool BluetoothRemoteDevice::IsAclEncrypted() const
327 {
328 HILOGI("enter");
329 if (!IsValidBluetoothRemoteDevice()) {
330 HILOGW("Invalid remote device");
331 return false;
332 }
333 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
334 if (hostProxy == nullptr) {
335 HILOGE("fails: no proxy");
336 return false;
337 }
338 return hostProxy->IsAclEncrypted(transport_, address_);
339 }
340
GetDeviceClass(int & cod) const341 int BluetoothRemoteDevice::GetDeviceClass(int &cod) const
342 {
343 HILOGI("enter");
344 if (!IsValidBluetoothRemoteDevice()) {
345 HILOGW("Invalid remote device");
346 return BT_ERR_INTERNAL_ERROR;
347 }
348 if (!IS_BT_ENABLED()) {
349 HILOGE("bluetooth is off.");
350 return BT_ERR_INVALID_STATE;
351 }
352 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
353 if (hostProxy == nullptr) {
354 HILOGE("fails: no proxy");
355 return BT_ERR_INTERNAL_ERROR;
356 }
357 int ret = hostProxy->GetDeviceClass(address_, cod);
358 return ret;
359 }
360
GetDeviceUuids(std::vector<std::string> & uuids) const361 int BluetoothRemoteDevice::GetDeviceUuids(std::vector<std::string> &uuids) const
362 {
363 HILOGI("enter");
364 if (!IsValidBluetoothRemoteDevice()) {
365 HILOGW("Invalid remote device");
366 return BT_ERR_INTERNAL_ERROR;
367 }
368 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
369 if (hostProxy == nullptr) {
370 HILOGE("fails: no proxy");
371 return BT_ERR_INTERNAL_ERROR;
372 }
373 return hostProxy->GetDeviceUuids(address_, uuids);
374 }
375
SetDevicePin(const std::string & pin)376 int BluetoothRemoteDevice::SetDevicePin(const std::string &pin)
377 {
378 HILOGI("enter");
379 if (!IsValidBluetoothRemoteDevice()) {
380 HILOGW("Invalid remote device");
381 return BT_ERR_INTERNAL_ERROR;
382 }
383 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
384 if (hostProxy == nullptr) {
385 HILOGE("fails: no proxy");
386 return BT_ERR_INTERNAL_ERROR;
387 }
388 return hostProxy->SetDevicePin(address_, pin);
389 }
390
SetDevicePairingConfirmation(bool accept)391 int BluetoothRemoteDevice::SetDevicePairingConfirmation(bool accept)
392 {
393 HILOGI("enter, accept: %{public}d", accept);
394 if (!IsValidBluetoothRemoteDevice()) {
395 HILOGW("Invalid remote device");
396 return BT_ERR_INTERNAL_ERROR;
397 }
398 if (!IS_BT_ENABLED()) {
399 HILOGE("bluetooth is off.");
400 return BT_ERR_INVALID_STATE;
401 }
402 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
403 if (hostProxy == nullptr) {
404 HILOGE("fails: no proxy");
405 return BT_ERR_INTERNAL_ERROR;
406 }
407 return hostProxy->SetDevicePairingConfirmation(transport_, address_, accept);
408 }
409
SetDevicePasskey(int passkey,bool accept)410 bool BluetoothRemoteDevice::SetDevicePasskey(int passkey, bool accept)
411 {
412 HILOGI("enter, accept: %{public}d", accept);
413 if (!IsValidBluetoothRemoteDevice()) {
414 HILOGW("Invalid remote device");
415 return false;
416 }
417 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
418 if (hostProxy == nullptr) {
419 HILOGE("fails: no proxy");
420 return false;
421 }
422 return hostProxy->SetDevicePasskey(transport_, address_, passkey, accept);
423 }
424
PairRequestReply(bool accept)425 bool BluetoothRemoteDevice::PairRequestReply(bool accept)
426 {
427 HILOGI("enter, accept: %{public}d", accept);
428 if (!IsValidBluetoothRemoteDevice()) {
429 HILOGW("Invalid remote device");
430 return false;
431 }
432 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
433 if (hostProxy == nullptr) {
434 HILOGE("fails: no proxy");
435 return false;
436 }
437 return hostProxy->PairRequestReply(transport_, address_, accept);
438 }
439
ReadRemoteRssiValue()440 bool BluetoothRemoteDevice::ReadRemoteRssiValue()
441 {
442 HILOGI("enter");
443 if (!IsValidBluetoothRemoteDevice()) {
444 HILOGW("Invalid remote device");
445 return false;
446 }
447 sptr<BluetoothHostProxy> hostProxy = GetHostProxy();
448 if (hostProxy == nullptr) {
449 HILOGE("fails: no proxy");
450 return false;
451 }
452 return hostProxy->ReadRemoteRssiValue(address_);
453 }
454 } // namespace Bluetooth
455 } // namespace OHOS
456