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