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_config.h"
17
18 #include <vector>
19
20 #include "bt_def.h"
21 #include "classic_defs.h"
22 #include "log.h"
23
24 namespace OHOS {
25 namespace bluetooth {
GetInstance()26 ClassicConfig &ClassicConfig::GetInstance()
27 {
28 static ClassicConfig instance;
29 return instance;
30 }
31
ClassicConfig()32 ClassicConfig::ClassicConfig() : config_(AdapterDeviceConfig::GetInstance())
33 {}
34
~ClassicConfig()35 ClassicConfig::~ClassicConfig()
36 {}
37
LoadConfigFile() const38 bool ClassicConfig::LoadConfigFile() const
39 {
40 /// Load Device Config File.
41 bool ret = config_->Load();
42 if (!ret) {
43 LOG_ERROR("[ClassicConfig]::%{public}s failed!", __func__);
44 }
45
46 return ret;
47 }
48
Save() const49 bool ClassicConfig::Save() const
50 {
51 bool ret = config_->Save();
52 if (!ret) {
53 LOG_ERROR("[ClassicConfig]::%{public}s failed!", __func__);
54 }
55
56 return ret;
57 }
58
GetLocalName() const59 std::string ClassicConfig::GetLocalName() const
60 {
61 std::string name = "";
62 if (!config_->GetValue(SECTION_HOST, PROPERTY_DEVICE_NAME, name)) {
63 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
64 }
65
66 return name;
67 }
68
SetLocalName(const std::string & name) const69 bool ClassicConfig::SetLocalName(const std::string &name) const
70 {
71 if (!config_->SetValue(SECTION_HOST, PROPERTY_DEVICE_NAME, name)) {
72 LOG_WARN("[ClassicConfig]::%{public}s failed!", __func__);
73 return false;
74 }
75
76 return true;
77 }
SetLocalAddress(const std::string & addr) const78 bool ClassicConfig::SetLocalAddress(const std::string &addr) const
79 {
80 if (!config_->SetValue(SECTION_HOST, PROPERTY_DEVICE_ADDR, addr)) {
81 LOG_WARN("[ClassicConfig]::%{public}s failed!", __func__);
82 return false;
83 }
84
85 return true;
86 }
87
GetLocalDeviceClass() const88 int ClassicConfig::GetLocalDeviceClass() const
89 {
90 int cod = 0;
91 if (!config_->GetValue(SECTION_HOST, PROPERTY_CLASS_OF_DEVICE, cod)) {
92 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
93 }
94
95 return cod;
96 }
97
SetLocalDeviceClass(int cod) const98 bool ClassicConfig::SetLocalDeviceClass(int cod) const
99 {
100 if (!config_->SetValue(SECTION_HOST, PROPERTY_CLASS_OF_DEVICE, cod)) {
101 LOG_WARN("[ClassicConfig]::%{public}s failed!", __func__);
102 return false;
103 }
104
105 return true;
106 }
107
GetIoCapability() const108 int ClassicConfig::GetIoCapability() const
109 {
110 int io = 0;
111 if (!config_->GetValue(SECTION_HOST, PROPERTY_IO_CAPABILITY, io)) {
112 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
113 }
114
115 return io;
116 }
117
GetDiscoverableTimeout() const118 int ClassicConfig::GetDiscoverableTimeout() const
119 {
120 int time = 0;
121 if (!config_->GetValue(SECTION_HOST, PROPERTY_DISCOVERABLE_TIMEOUT, time)) {
122 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
123 }
124
125 return time;
126 }
127
SetDiscoverableTimeout(int time) const128 bool ClassicConfig::SetDiscoverableTimeout(int time) const
129 {
130 if (!config_->SetValue(SECTION_HOST, PROPERTY_DISCOVERABLE_TIMEOUT, time)) {
131 LOG_WARN("[ClassicConfig]::%{public}s failed!", __func__);
132 return false;
133 }
134
135 return true;
136 }
137
GetLocalPasskey() const138 std::string ClassicConfig::GetLocalPasskey() const
139 {
140 std::string passkey = "";
141 if (!config_->GetValue(SECTION_HOST, PROPERTY_LOCAL_PASSKEY, passkey)) {
142 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
143 }
144
145 return passkey;
146 }
147
GetSecurityMode() const148 int ClassicConfig::GetSecurityMode() const
149 {
150 int securityMode = 0;
151 if (!config_->GetValue(SECTION_HOST, PROPERTY_SECURITY_MODE, securityMode)) {
152 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
153 }
154
155 return securityMode;
156 }
157
GetPairedAddrList() const158 std::vector<std::string> ClassicConfig::GetPairedAddrList() const
159 {
160 std::vector<std::string> pairedList;
161 if (!config_->GetSubSections(SECTION_BREDR_PAIRED_LIST, pairedList)) {
162 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
163 }
164
165 return pairedList;
166 }
167
GetRemoteName(const std::string & subSection) const168 std::string ClassicConfig::GetRemoteName(const std::string &subSection) const
169 {
170 std::string name = "";
171 if (!config_->GetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_DEVICE_NAME, name)) {
172 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
173 }
174
175 return name;
176 }
177
GetRemoteAlias(const std::string & subSection) const178 std::string ClassicConfig::GetRemoteAlias(const std::string &subSection) const
179 {
180 std::string name = "";
181 if (!config_->GetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_ALIAS_NAME, name)) {
182 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
183 }
184
185 return name;
186 }
187
GetRemoteLinkkey(const std::string & subSection) const188 std::string ClassicConfig::GetRemoteLinkkey(const std::string &subSection) const
189 {
190 std::string key = "";
191 if (!config_->GetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_LINK_KEY, key)) {
192 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
193 }
194
195 return key;
196 }
197
GetRemoteDeviceType(const std::string & subSection) const198 int ClassicConfig::GetRemoteDeviceType(const std::string &subSection) const
199 {
200 int type = 0;
201 if (!config_->GetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_DEVICE_TYPE, type)) {
202 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
203 }
204
205 return type;
206 }
207
GetRemoteLinkkeyType(const std::string & subSection) const208 int ClassicConfig::GetRemoteLinkkeyType(const std::string &subSection) const
209 {
210 int type = 0;
211 if (!config_->GetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_LINK_KEY_TYPE, type)) {
212 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
213 }
214
215 return type;
216 }
217
GetRemoteDeviceClass(const std::string & subSection) const218 int ClassicConfig::GetRemoteDeviceClass(const std::string &subSection) const
219 {
220 int cod = 0;
221 if (!config_->GetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_CLASS_OF_DEVICE, cod)) {
222 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
223 }
224
225 return cod;
226 }
227
GetRemoteDeviceIoCapability(const std::string & subSection) const228 int ClassicConfig::GetRemoteDeviceIoCapability(const std::string &subSection) const
229 {
230 int io = 0;
231 if (!config_->GetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_IO_CAPABILITY, io)) {
232 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
233 }
234
235 return io;
236 }
237
SetRemoteName(const std::string & subSection,const std::string & name) const238 bool ClassicConfig::SetRemoteName(const std::string &subSection, const std::string &name) const
239 {
240 if (!config_->SetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_DEVICE_NAME, name)) {
241 LOG_WARN("[ClassicConfig]::%{public}s failed!", __func__);
242 return false;
243 }
244
245 return true;
246 }
247
SetRemoteAlias(const std::string & subSection,const std::string & name) const248 bool ClassicConfig::SetRemoteAlias(const std::string &subSection, const std::string &name) const
249 {
250 if (!config_->SetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_ALIAS_NAME, name)) {
251 LOG_WARN("[ClassicConfig]::%{public}s failed!", __func__);
252 return false;
253 }
254
255 return true;
256 }
257
SetRemoteDeviceType(const std::string & subSection,int type) const258 bool ClassicConfig::SetRemoteDeviceType(const std::string &subSection, int type) const
259 {
260 if (!config_->SetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_DEVICE_TYPE, type)) {
261 LOG_WARN("[ClassicConfig]::%{public}s failed!", __func__);
262 return false;
263 }
264
265 return true;
266 }
267
SetRemoteLinkkey(const std::string & subSection,const std::string & linkKey) const268 bool ClassicConfig::SetRemoteLinkkey(const std::string &subSection, const std::string &linkKey) const
269 {
270 if (!config_->SetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_LINK_KEY, linkKey)) {
271 LOG_WARN("[ClassicConfig]::%{public}s failed!", __func__);
272 return false;
273 }
274
275 return true;
276 }
277
SetRemoteLinkkeyType(const std::string & subSection,int type) const278 bool ClassicConfig::SetRemoteLinkkeyType(const std::string &subSection, int type) const
279 {
280 if (!config_->SetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_LINK_KEY_TYPE, type)) {
281 LOG_WARN("[ClassicConfig]::%{public}s failed!", __func__);
282 return false;
283 }
284
285 return true;
286 }
287
SetRemoteDeviceClass(const std::string & subSection,int cod) const288 bool ClassicConfig::SetRemoteDeviceClass(const std::string &subSection, int cod) const
289 {
290 if (!config_->SetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_CLASS_OF_DEVICE, cod)) {
291 LOG_WARN("[ClassicConfig]::%{public}s failed!", __func__);
292 return false;
293 }
294
295 return true;
296 }
297
SetRemoteDeviceIoCapability(const std::string & subSection,int io) const298 bool ClassicConfig::SetRemoteDeviceIoCapability(const std::string &subSection, int io) const
299 {
300 if (!config_->SetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_IO_CAPABILITY, io)) {
301 LOG_WARN("[ClassicConfig]::%{public}s failed!", __func__);
302 return false;
303 }
304
305 return true;
306 }
307
GetRemoteDevicePairFlag(const std::string & subSection) const308 bool ClassicConfig::GetRemoteDevicePairFlag(const std::string &subSection) const
309 {
310 bool flag = false;
311 if (!config_->GetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_PAIR_FLAG, flag)) {
312 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
313 }
314
315 return flag;
316 }
317
GetRemoteDeviceBondFromLocal(const std::string & subSection) const318 bool ClassicConfig::GetRemoteDeviceBondFromLocal(const std::string &subSection) const
319 {
320 bool flag = false;
321 if (!config_->GetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_BOND_FROM_LOCAL, flag)) {
322 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
323 }
324
325 return flag;
326 }
327
SetRemoteDevicePairFlag(const std::string & subSection,const bool flag) const328 bool ClassicConfig::SetRemoteDevicePairFlag(const std::string &subSection, const bool flag) const
329 {
330 if (!config_->SetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_PAIR_FLAG, flag)) {
331 LOG_WARN("[ClassicConfig]::%{public}s failed!", __func__);
332 return false;
333 }
334
335 return true;
336 }
337
SetRemoteDeviceBondFromLocal(const std::string & subSection,const bool flag) const338 bool ClassicConfig::SetRemoteDeviceBondFromLocal(const std::string &subSection, const bool flag) const
339 {
340 if (!config_->SetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_BOND_FROM_LOCAL, flag)) {
341 LOG_WARN("[ClassicConfig]::%{public}s failed!", __func__);
342 return false;
343 }
344
345 return true;
346 }
347
RemovePairedDevice(const std::string & subSection) const348 bool ClassicConfig::RemovePairedDevice(const std::string &subSection) const
349 {
350 if (!config_->RemoveSection(SECTION_BREDR_PAIRED_LIST, subSection)) {
351 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
352 return false;
353 }
354
355 return true;
356 }
357
SetRemoteUuids(const std::string & subSection,const std::string & uuids) const358 bool ClassicConfig::SetRemoteUuids(const std::string &subSection, const std::string &uuids) const
359 {
360 if (!config_->SetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_REMOTE_UUIDS, uuids)) {
361 LOG_WARN("[ClassicConfig]::%{public}s failed!", __func__);
362 return false;
363 }
364
365 return true;
366 }
367
GetRemoteUuids(const std::string & subSection) const368 std::string ClassicConfig::GetRemoteUuids(const std::string &subSection) const
369 {
370 std::string uuids = "";
371 if (!config_->GetValue(SECTION_BREDR_PAIRED_LIST, subSection, PROPERTY_REMOTE_UUIDS, uuids)) {
372 LOG_INFO("[ClassicConfig]::%{public}s failed!", __func__);
373 }
374
375 return uuids;
376 }
377 } // namespace bluetooth
378 } // namespace OHOS