• 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 "classic_remote_device.h"
17 
18 #include <cstring>
19 #include "log.h"
20 #include "log_util.h"
21 
22 namespace OHOS {
23 namespace bluetooth {
ClassicRemoteDevice()24 ClassicRemoteDevice::ClassicRemoteDevice()
25 {
26     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s", __func__);
27     cod_ = INVALID_VALUE;
28     rssi_ = INVALID_VALUE;
29     connectionHandle_ = INVALID_VALUE;
30     pairState_ = PAIR_NONE;
31     ioCapability_ = INVALID_VALUE;
32     batteryLevel_ = INVALID_VALUE;
33 }
34 
ClassicRemoteDevice(const std::string & addr)35 ClassicRemoteDevice::ClassicRemoteDevice(const std::string &addr) : macAddr_(addr)
36 {
37     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s", __func__);
38     cod_ = INVALID_VALUE;
39     rssi_ = INVALID_VALUE;
40     connectionHandle_ = INVALID_VALUE;
41     pairState_ = PAIR_NONE;
42     ioCapability_ = INVALID_VALUE;
43     batteryLevel_ = INVALID_VALUE;
44 }
45 
~ClassicRemoteDevice()46 ClassicRemoteDevice::~ClassicRemoteDevice()
47 {
48     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s", __func__);
49 }
50 
GetAddress() const51 std::string ClassicRemoteDevice::GetAddress() const
52 {
53     HILOGI("addr = %{public}s", GetEncryptAddr(macAddr_).c_str());
54 
55     return macAddr_;
56 }
57 
SetAddress(const std::string & addr)58 void ClassicRemoteDevice::SetAddress(const std::string &addr)
59 {
60     HILOGI("addr = %{public}s", GetEncryptAddr(addr).c_str());
61 
62     macAddr_ = addr;
63 }
64 
GetRemoteName() const65 std::string ClassicRemoteDevice::GetRemoteName() const
66 {
67     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s, name = %{public}s", __func__, deviceName_.c_str());
68 
69     return deviceName_;
70 }
71 
SetRemoteName(const std::string & name)72 void ClassicRemoteDevice::SetRemoteName(const std::string &name)
73 {
74     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s, name = %{public}s", __func__, name.c_str());
75 
76     if (name.length() >= MAX_LOC_BT_NAME_LEN) {
77         deviceName_ = name.substr(0, MAX_LOC_BT_NAME_LEN);
78     } else {
79         deviceName_ = name;
80     }
81 }
82 
GetAliasName() const83 std::string ClassicRemoteDevice::GetAliasName() const
84 {
85     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s, name = %{public}s", __func__, aliasName_.c_str());
86 
87     if (aliasName_ == INVALID_NAME) {
88         return deviceName_;
89     }
90     return aliasName_;
91 }
92 
SetAliasName(const std::string & name)93 bool ClassicRemoteDevice::SetAliasName(const std::string &name)
94 {
95     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s, name = %{public}s", __func__, name.c_str());
96 
97     if (name.empty()) {
98         LOG_ERROR("%{public}s failed, because of alias name is NULL", __func__);
99         return false;
100     }
101 
102     if (name.length() >= MAX_LOC_BT_NAME_LEN) {
103         aliasName_ = name.substr(0, MAX_LOC_BT_NAME_LEN);
104     } else {
105         aliasName_ = name;
106     }
107     return true;
108 }
109 
GetDeviceClass() const110 int ClassicRemoteDevice::GetDeviceClass() const
111 {
112     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s, cod = %{public}d", __func__, cod_);
113 
114     return cod_;
115 }
116 
SetDeviceClass(int deviceClass)117 void ClassicRemoteDevice::SetDeviceClass(int deviceClass)
118 {
119     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s, cod = %{public}d", __func__, deviceClass);
120 
121     cod_ = deviceClass;
122 }
123 
IsBondedFromLocal() const124 bool ClassicRemoteDevice::IsBondedFromLocal() const
125 {
126     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s, bondedFromLocal_ = %{public}d", __func__, bondedFromLocal_);
127 
128     return bondedFromLocal_;
129 }
130 
SetBondedFromLocal(bool flag)131 void ClassicRemoteDevice::SetBondedFromLocal(bool flag)
132 {
133     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s, bondedFromLocal_ = %{public}d", __func__, flag);
134 
135     bondedFromLocal_ = flag;
136 }
137 
IsAclConnected() const138 bool ClassicRemoteDevice::IsAclConnected() const
139 {
140     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s, state = %{public}d", __func__, aclConnected_);
141 
142     return (aclConnected_ != CONNECTION_STATE_DISCONNECTED);
143 }
144 
IsAclEncrypted() const145 bool ClassicRemoteDevice::IsAclEncrypted() const
146 {
147     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s, state = %{public}d", __func__, aclConnected_);
148 
149     return (aclConnected_ >= CONNECTION_STATE_CONNECTED);
150 }
151 
SetAclConnectState(int connectState)152 void ClassicRemoteDevice::SetAclConnectState(int connectState)
153 {
154     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s, state = %{public}d", __func__, connectState);
155 
156     aclConnected_ = connectState;
157 }
158 
GetDeviceUuids() const159 std::vector<Uuid> ClassicRemoteDevice::GetDeviceUuids() const
160 {
161     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s", __func__);
162 
163     return uuids_;
164 }
165 
SetDeviceUuids(const std::vector<Uuid> & uuids)166 void ClassicRemoteDevice::SetDeviceUuids(const std::vector<Uuid> &uuids)
167 {
168     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s", __func__);
169 
170     uuids_.assign(uuids.begin(), uuids.end());
171 }
172 
GetDeviceType() const173 int ClassicRemoteDevice::GetDeviceType() const
174 {
175     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s type = %{public}d", __func__, deviceType_);
176 
177     return deviceType_;
178 }
179 
SetDeviceType(int type)180 void ClassicRemoteDevice::SetDeviceType(int type)
181 {
182     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s type = %{public}d", __func__, type);
183 
184     deviceType_ = type;
185 }
186 
SetRssi(int rssi)187 void ClassicRemoteDevice::SetRssi(int rssi)
188 {
189     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s rssi = %{public}d", __func__, rssi);
190 
191     rssi_ = rssi;
192 }
193 
SetConnectionHandle(int handle)194 void ClassicRemoteDevice::SetConnectionHandle(int handle)
195 {
196     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s connectionHandle = %{public}d", __func__, handle);
197 
198     connectionHandle_ = handle;
199 }
200 
GetConnectionHandle() const201 int ClassicRemoteDevice::GetConnectionHandle() const
202 {
203     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s connectionHandle = %{public}d", __func__, connectionHandle_);
204 
205     return connectionHandle_;
206 }
207 
SetPairedStatus(int status)208 bool ClassicRemoteDevice::SetPairedStatus(int status)
209 {
210     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s pairedStatus = %{public}d", __func__, status);
211 
212     if (PAIR_NONE > status || PAIR_CANCELING < status) {
213         LOG_ERROR("[ClassicRemoteDevice]::%{public}s. Invalid Parameter", __func__);
214         return false;
215     }
216 
217     pairState_ = status;
218     return true;
219 }
220 
GetPairedStatus() const221 int ClassicRemoteDevice::GetPairedStatus() const
222 {
223     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s pairedStatus = %{public}d", __func__, pairState_);
224 
225     return pairState_;
226 }
227 
GetLinkKey() const228 std::vector<uint8_t> ClassicRemoteDevice::GetLinkKey() const
229 {
230     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s", __func__);
231 
232     return linkKey_;
233 }
234 
SetLinkKey(const std::vector<uint8_t> & linkKey)235 void ClassicRemoteDevice::SetLinkKey(const std::vector<uint8_t> &linkKey)
236 {
237     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s", __func__);
238 
239     linkKey_ = linkKey;
240 }
241 
DeleteLinkKey()242 void ClassicRemoteDevice::DeleteLinkKey()
243 {
244     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s", __func__);
245 
246     linkKey_.clear();
247 }
248 
GetLinkKeyType() const249 int ClassicRemoteDevice::GetLinkKeyType() const
250 {
251     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s linkKeyType = %{public}d", __func__, linkKeyType_);
252 
253     return linkKeyType_;
254 }
255 
SetLinkKeyType(int linkKeyType)256 void ClassicRemoteDevice::SetLinkKeyType(int linkKeyType)
257 {
258     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s linkKeyType = %{public}d", __func__, linkKeyType);
259 
260     linkKeyType_ = linkKeyType;
261 }
262 
GetIoCapability() const263 int ClassicRemoteDevice::GetIoCapability() const
264 {
265     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s ioCapability = %{public}d", __func__, ioCapability_);
266 
267     return ioCapability_;
268 }
269 
SetIoCapability(int io)270 void ClassicRemoteDevice::SetIoCapability(int io)
271 {
272     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s ioCapability = %{public}d", __func__, io);
273 
274     ioCapability_ = io;
275 }
276 
GetFlags() const277 uint8_t ClassicRemoteDevice::GetFlags() const
278 {
279     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s flags = %{public}d", __func__, flags_);
280 
281     return flags_;
282 }
283 
SetFlags(uint8_t flags)284 void ClassicRemoteDevice::SetFlags(uint8_t flags)
285 {
286     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s flags = %{public}d", __func__, flags);
287 
288     flags_ = flags;
289 }
290 
SetManufacturerSpecificData(const std::vector<uint8_t> & data)291 void ClassicRemoteDevice::SetManufacturerSpecificData(const std::vector<uint8_t> &data)
292 {
293     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s", __func__);
294 
295     manuSpecData_ = data;
296 }
297 
IsPaired() const298 bool ClassicRemoteDevice::IsPaired() const
299 {
300     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s pairState = %{public}d", __func__, pairState_);
301 
302     return (pairState_ == PAIR_PAIRED);
303 }
304 
SetTxPower(uint8_t power)305 void ClassicRemoteDevice::SetTxPower(uint8_t power)
306 {
307     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s txPower = %u", __func__, power);
308 
309     txPower_ = power;
310 }
311 
SetURI(const std::string & uri)312 void ClassicRemoteDevice::SetURI(const std::string &uri)
313 {
314     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s uri = %{public}s", __func__, uri.c_str());
315 
316     uri_ = uri;
317 }
318 
GetPairConfirmState() const319 int ClassicRemoteDevice::GetPairConfirmState() const
320 {
321     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s state = %{public}d", __func__, pairConfirmState_);
322 
323     return pairConfirmState_;
324 }
325 
SetPairConfirmState(int state)326 void ClassicRemoteDevice::SetPairConfirmState(int state)
327 {
328     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s state = %{public}d", __func__, state);
329 
330     pairConfirmState_ = state;
331 }
332 
SetPairConfirmType(int type)333 void ClassicRemoteDevice::SetPairConfirmType(int type)
334 {
335     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s Type = %{public}d", __func__, type);
336 
337     pairConfirmType_ = type;
338 }
339 
GetPairConfirmType() const340 int ClassicRemoteDevice::GetPairConfirmType() const
341 {
342     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s Type = %{public}d", __func__, pairConfirmType_);
343 
344     return pairConfirmType_;
345 }
346 
SetNameNeedGet(bool nameUnknowned)347 void ClassicRemoteDevice::SetNameNeedGet(bool nameUnknowned)
348 {
349     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s nameNeedGet_ = %{public}d", __func__, nameUnknowned);
350     nameNeedGet_ = nameUnknowned;
351 }
352 
GetNameNeedGet() const353 bool ClassicRemoteDevice::GetNameNeedGet() const
354 {
355     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s nameNeedGet_ = %{public}d", __func__, nameNeedGet_);
356 
357     return nameNeedGet_;
358 }
359 
GetBatteryLevel() const360 int ClassicRemoteDevice::GetBatteryLevel() const
361 {
362     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s batteryLevel = %{public}d", __func__, batteryLevel_);
363 
364     return batteryLevel_;
365 }
366 
SetBatteryLevel(int batteryLevel)367 void ClassicRemoteDevice::SetBatteryLevel(int batteryLevel)
368 {
369     LOG_DEBUG("[ClassicRemoteDevice]::%{public}s batteryLevel = %{public}d", __func__, batteryLevel);
370 
371     batteryLevel_ = batteryLevel;
372 }
373 
CheckCod(uint32_t cod) const374 bool ClassicRemoteDevice::CheckCod(uint32_t cod) const
375 {
376     uint32_t tmpCod = cod_;
377     return (tmpCod & CLASS_OF_DEVICE_MASK) == cod;
378 }
379 }  // namespace bluetooth
380 }  // namespace OHOS