• 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_device.h"
22 #include "bluetooth_host_proxy.h"
23 #include "bluetooth_log.h"
24 #include "bluetooth_utils.h"
25 #include "bluetooth_profile_manager.h"
26 #include "bluetooth_remote_device.h"
27 #include "iservice_registry.h"
28 #include "system_ability_definition.h"
29 
30 namespace OHOS {
31 namespace Bluetooth {
BluetoothRemoteDevice(const std::string & addr,const int transport)32 BluetoothRemoteDevice::BluetoothRemoteDevice(const std::string &addr, const int transport)
33 {
34     address_ = addr;
35     transport_ = transport;
36 }
37 
GetDeviceType() const38 int BluetoothRemoteDevice::GetDeviceType() const
39 {
40     HILOGI("enter");
41     int type = 0;
42     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), type, "Invalid remote device.");
43 
44     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
45     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, type, "proxy is nullptr.");
46 
47     return hostProxy->GetDeviceType(transport_, address_);
48 }
49 
IsValidBluetoothRemoteDevice() const50 bool BluetoothRemoteDevice::IsValidBluetoothRemoteDevice() const
51 {
52     CHECK_AND_RETURN_LOG_RET(BluetoothHost::IsValidBluetoothAddr(address_), false,
53         "invalid bluetooth addr, address_: %{public}s", GetEncryptAddr(address_).c_str());
54 
55     CHECK_AND_RETURN_LOG_RET(transport_ == BT_TRANSPORT_BREDR ||
56         transport_ == BT_TRANSPORT_BLE || transport_ == BT_TRANSPORT_NONE,
57         false, "invalid transport type.");
58     return true;
59 }
60 
GetTransportType() const61 int BluetoothRemoteDevice::GetTransportType() const
62 {
63     return transport_;
64 }
65 
GetPhonebookPermission() const66 int BluetoothRemoteDevice::GetPhonebookPermission() const
67 {
68     HILOGI("enter");
69     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_VALUE, "Invalid remote device.");
70     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
71     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_VALUE, "proxy is nullptr.");
72 
73     return hostProxy->GetPhonebookPermission(address_);
74 }
75 
SetPhonebookPermission(int permission)76 bool BluetoothRemoteDevice::SetPhonebookPermission(int permission)
77 {
78     HILOGI("enter, permission: %{public}d", permission);
79     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device.");
80     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
81     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
82     return hostProxy->SetPhonebookPermission(address_, permission);
83 }
84 
GetMessagePermission() const85 int BluetoothRemoteDevice::GetMessagePermission() const
86 {
87     HILOGI("enter");
88     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_VALUE, "Invalid remote device.");
89     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
90     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_VALUE, "proxy is nullptr.");
91     return hostProxy->GetMessagePermission(address_);
92 }
93 
SetMessagePermission(int permission)94 bool BluetoothRemoteDevice::SetMessagePermission(int permission)
95 {
96     HILOGI("enter, permission: %{public}d", permission);
97     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device.");
98     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
99     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
100     return hostProxy->SetMessagePermission(address_, permission);
101 }
102 
GetPowerMode(void) const103 int BluetoothRemoteDevice::GetPowerMode(void) const
104 {
105     HILOGI("enter");
106     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_VALUE, "Invalid remote device.");
107     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
108     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_VALUE, "proxy is nullptr.");
109     return hostProxy->GetPowerMode(address_);
110 }
111 
GetDeviceName() const112 std::string BluetoothRemoteDevice::GetDeviceName() const
113 {
114     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_NAME, "Invalid remote device");
115     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), INVALID_NAME, "bluetooth is off.");
116     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
117     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_NAME, "proxy is nullptr.");
118     std::string name = INVALID_NAME;
119     hostProxy->GetDeviceName(transport_, address_, name);
120     return name;
121 }
122 
GetDeviceName(std::string & name) const123 int BluetoothRemoteDevice::GetDeviceName(std::string &name) const
124 {
125     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
126     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
127     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
128     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
129     return hostProxy->GetDeviceName(transport_, address_, name);
130 }
131 
GetDeviceAlias() const132 std::string BluetoothRemoteDevice::GetDeviceAlias() const
133 {
134     HILOGI("enter");
135     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_NAME, "Invalid remote device");
136     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
137     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_NAME, "proxy is nullptr.");
138     return hostProxy->GetDeviceAlias(address_);
139 }
140 
SetDeviceAlias(const std::string & aliasName)141 bool BluetoothRemoteDevice::SetDeviceAlias(const std::string &aliasName)
142 {
143     HILOGI("enter");
144     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
145 
146     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
147     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
148     return hostProxy->SetDeviceAlias(address_, aliasName);
149 }
150 
GetDeviceBatteryLevel() const151 int BluetoothRemoteDevice::GetDeviceBatteryLevel() const
152 {
153     HILOGI("enter");
154     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_VALUE, "Invalid remote device.");
155     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
156     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_VALUE, "proxy is nullptr.");
157     return hostProxy->GetDeviceBatteryLevel(address_);
158 }
159 
GetPairState(int & pairState) const160 int BluetoothRemoteDevice::GetPairState(int &pairState) const
161 {
162     HILOGI("enter");
163     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_VALUE, "Invalid remote device.");
164     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
165     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_VALUE, "proxy is nullptr.");
166     return hostProxy->GetPairState(transport_, address_, pairState);
167 }
168 
StartPair()169 int BluetoothRemoteDevice::StartPair()
170 {
171     HILOGI("enter");
172     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
173     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
174     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
175     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
176     return hostProxy->StartPair(transport_, address_);
177 }
178 
StartCrediblePair()179 int BluetoothRemoteDevice::StartCrediblePair()
180 {
181     HILOGI("enter");
182     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
183     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
184     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
185     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
186     return hostProxy->StartCrediblePair(transport_, address_);
187 }
188 
CancelPairing()189 int BluetoothRemoteDevice::CancelPairing()
190 {
191     HILOGI("enter");
192     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
193     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
194     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
195     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
196     if (hostProxy->CancelPairing(transport_, address_)) {
197         return BT_NO_ERROR;
198     }
199     return  BT_ERR_INTERNAL_ERROR;
200 }
201 
IsBondedFromLocal() const202 bool BluetoothRemoteDevice::IsBondedFromLocal() const
203 {
204     HILOGI("enter");
205     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
206     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
207     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
208     return hostProxy->IsBondedFromLocal(transport_, address_);
209 }
210 
IsAclConnected() const211 bool BluetoothRemoteDevice::IsAclConnected() const
212 {
213     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
214     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
215     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
216     return hostProxy->IsAclConnected(transport_, address_);
217 }
218 
IsAclEncrypted() const219 bool BluetoothRemoteDevice::IsAclEncrypted() const
220 {
221     HILOGI("enter");
222     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
223     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
224     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
225     return hostProxy->IsAclEncrypted(transport_, address_);
226 }
227 
GetDeviceClass(int & cod) const228 int BluetoothRemoteDevice::GetDeviceClass(int &cod) const
229 {
230     HILOGD("enter");
231     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
232     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
233     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
234     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
235     int ret = hostProxy->GetDeviceClass(address_, cod);
236     return ret;
237 }
238 
GetDeviceProductId(std::string & prodcutId) const239 int BluetoothRemoteDevice::GetDeviceProductId(std::string &prodcutId) const
240 {
241     HILOGD("enter");
242     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
243     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
244 
245     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
246     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
247 
248     return hostProxy->GetDeviceProductId(address_, prodcutId);
249 }
250 
GetDeviceUuids(std::vector<std::string> & uuids) const251 int BluetoothRemoteDevice::GetDeviceUuids(std::vector<std::string> &uuids) const
252 {
253     HILOGI("enter");
254     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
255     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
256     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
257     return hostProxy->GetDeviceUuids(address_, uuids);
258 }
259 
SetDevicePin(const std::string & pin)260 int BluetoothRemoteDevice::SetDevicePin(const std::string &pin)
261 {
262     HILOGI("enter");
263     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
264     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
265     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
266     return hostProxy->SetDevicePin(address_, pin);
267 }
268 
SetDevicePairingConfirmation(bool accept)269 int BluetoothRemoteDevice::SetDevicePairingConfirmation(bool accept)
270 {
271     HILOGI("enter, accept: %{public}d", accept);
272     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
273     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
274     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
275     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
276     return hostProxy->SetDevicePairingConfirmation(transport_, address_, accept);
277 }
278 
SetDevicePasskey(int passkey,bool accept)279 bool BluetoothRemoteDevice::SetDevicePasskey(int passkey, bool accept)
280 {
281     HILOGI("enter, accept: %{public}d", accept);
282     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
283     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
284     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
285     return hostProxy->SetDevicePasskey(transport_, address_, passkey, accept);
286 }
287 
PairRequestReply(bool accept)288 bool BluetoothRemoteDevice::PairRequestReply(bool accept)
289 {
290     HILOGI("enter, accept: %{public}d", accept);
291     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
292     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
293     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
294     return hostProxy->PairRequestReply(transport_, address_, accept);
295 }
296 
ReadRemoteRssiValue()297 bool BluetoothRemoteDevice::ReadRemoteRssiValue()
298 {
299     HILOGI("enter");
300     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
301     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
302     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
303     return hostProxy->ReadRemoteRssiValue(address_);
304 }
305 
GetDeviceProductType(int & cod,int & majorClass,int & majorMinorClass) const306 int BluetoothRemoteDevice::GetDeviceProductType(int &cod, int &majorClass, int &majorMinorClass) const
307 {
308     HILOGI("enter");
309 
310     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "Invalid remote device");
311 
312     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
313 
314     int deviceCod = 0;
315     int ret = GetDeviceClass(deviceCod);
316     BluetoothDeviceClass deviceClass = BluetoothDeviceClass(deviceCod);
317     cod = deviceClass.GetClassOfDevice();
318     majorClass = deviceClass.GetMajorClass();
319     majorMinorClass = deviceClass.GetMajorMinorClass();
320     if (cod == 0) {
321         HILOGW("cod = %{public}d", cod);
322         cod = BluetoothDevice::MAJOR_UNCATEGORIZED;
323         majorClass = BluetoothDevice::MAJOR_UNCATEGORIZED;
324         majorMinorClass = BluetoothDevice::MAJOR_UNCATEGORIZED;
325     }
326     HILOGD("cod = %{public}#X, majorClass = %{public}#X, majorMinorClass = %{public}#X",
327         cod, majorClass, majorMinorClass);
328 
329     return ret;
330 }
331 
332 }  // namespace Bluetooth
333 }  // namespace OHOS
334