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 "ble_config.h"
17 
18 #include "ble_defs.h"
19 #include "log.h"
20 #include "xml_parse.h"
21 
22 namespace OHOS {
23 namespace bluetooth {
GetInstance()24 BleConfig &BleConfig::GetInstance()
25 {
26     static BleConfig instance;
27     return instance;
28 }
29 
BleConfig()30 BleConfig::BleConfig()
31 {
32     config_ = AdapterDeviceConfig::GetInstance();
33 }
34 
~BleConfig()35 BleConfig::~BleConfig()
36 {}
37 
LoadConfigInfo() const38 bool BleConfig::LoadConfigInfo() const
39 {
40     LOG_DEBUG("[BleConfig] %{public}s", __func__);
41 
42     bool ret = config_->Load();
43     if (!ret) {
44         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Load device config file failed!");
45     }
46     return ret;
47 }
48 
Save() const49 bool BleConfig::Save() const
50 {
51     LOG_DEBUG("[BleConfig] %{public}s", __func__);
52 
53     return config_->Save();
54 }
55 
GetLocalName() const56 std::string BleConfig::GetLocalName() const
57 {
58     LOG_DEBUG("[BleConfig] %{public}s", __func__);
59 
60     std::string name = "";
61     bool ret = config_->GetValue(SECTION_HOST, PROPERTY_DEVICE_NAME, name);
62     if (!ret) {
63         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Get local name failed!");
64     }
65     return name;
66 }
67 
SetLocalName(const std::string & name) const68 bool BleConfig::SetLocalName(const std::string &name) const
69 {
70     LOG_DEBUG("[BleConfig] %{public}s", __func__);
71 
72     bool ret = config_->SetValue(SECTION_HOST, PROPERTY_DEVICE_NAME, name);
73     if (!ret) {
74         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set local name failed!");
75     }
76     return ret;
77 }
78 
GetLocalAddress() const79 std::string BleConfig::GetLocalAddress() const
80 {
81     LOG_DEBUG("[BleConfig] %{public}s", __func__);
82 
83     std::string addr = BLE_INVALID_MAC_ADDRESS;
84     bool ret = config_->GetValue(SECTION_HOST, PROPERTY_DEVICE_ADDR, addr);
85     if (!ret) {
86         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Get local address failed!");
87     }
88     return addr;
89 }
90 
SetLocalAddress(const std::string & addr) const91 bool BleConfig::SetLocalAddress(const std::string &addr) const
92 {
93     LOG_DEBUG("[BleConfig] %{public}s", __func__);
94 
95     bool ret = config_->SetValue(SECTION_HOST, PROPERTY_DEVICE_ADDR, addr);
96     if (!ret) {
97         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set local address failed!");
98     }
99     return ret;
100 }
101 
GetIoCapability() const102 int BleConfig::GetIoCapability() const
103 {
104     LOG_DEBUG("[BleConfig] %{public}s", __func__);
105 
106     int io = 0;
107     bool ret = config_->GetValue(SECTION_HOST, PROPERTY_IO_CAPABILITY, io);
108     if (!ret) {
109         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Get local IO capability failed!");
110     }
111     return io;
112 }
113 
GetLoaclPasskey() const114 std::string BleConfig::GetLoaclPasskey() const
115 {
116     LOG_DEBUG("[BleConfig] %{public}s", __func__);
117 
118     std::string passkey = "";
119     bool ret = config_->GetValue(SECTION_HOST, PROPERTY_LOCAL_PASSKEY, passkey);
120     if (!ret) {
121         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Get local passkey failed!");
122     }
123     return passkey;
124 }
125 
GetBleRoles() const126 int BleConfig::GetBleRoles() const
127 {
128     LOG_DEBUG("[BleConfig] %{public}s", __func__);
129 
130     int roles = (GAP_LE_ROLE_BROADCASTER | GAP_LE_ROLE_OBSERVER | GAP_LE_ROLE_PREIPHERAL | GAP_LE_ROLE_CENTRAL);
131     bool ret = config_->GetValue(SECTION_HOST, PROPERTY_BLE_ROLES, roles);
132     if (!ret) {
133         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Get ble roles failed!");
134     }
135     return roles;
136 }
137 
SetBleRoles(uint8_t roles) const138 bool BleConfig::SetBleRoles(uint8_t roles) const
139 {
140     LOG_DEBUG("[BleConfig] %{public}s", __func__);
141 
142     bool ret = config_->SetValue(SECTION_HOST, PROPERTY_BLE_ROLES, roles);
143     if (!ret) {
144         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble roles failed!");
145     }
146     return ret;
147 }
148 
SetPasskey(const std::string & passkey) const149 bool BleConfig::SetPasskey(const std::string &passkey) const
150 {
151     LOG_DEBUG("[BleConfig] %{public}s", __func__);
152 
153     bool ret = config_->SetValue(SECTION_HOST, PROPERTY_LOCAL_PASSKEY, passkey);
154     if (!ret) {
155         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set local pass key failed!");
156     }
157     return ret;
158 }
159 
GetBleModel1Level() const160 int BleConfig::GetBleModel1Level() const
161 {
162     LOG_DEBUG("[BleConfig] %{public}s", __func__);
163 
164     int level = LE_MODE_1_LEVEL_1;
165     bool ret = config_->GetValue(SECTION_HOST, PROPERTY_BLE_MODE_1_LEVEL, level);
166     if (!ret) {
167         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Get ble model1 level failed!");
168     }
169     return level;
170 }
171 
GetBleModel2Level() const172 int BleConfig::GetBleModel2Level() const
173 {
174     LOG_DEBUG("[BleConfig] %{public}s", __func__);
175 
176     int level = LE_MODE_2_LEVEL_1;
177     bool ret = config_->GetValue(SECTION_HOST, PROPERTY_BLE_MODE_2_LEVEL, level);
178     if (!ret) {
179         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Get ble model2 level failed!");
180     }
181     return level;
182 }
183 
GetBleSecurity() const184 bool BleConfig::GetBleSecurity() const
185 {
186     LOG_DEBUG("[BleConfig] %{public}s", __func__);
187 
188     bool sc = false;
189     bool ret = config_->GetValue(SECTION_HOST, PROPERTY_BLE_SECURITY, sc);
190     if (!ret) {
191         sc = true;
192         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Get ble security failed!");
193     }
194     return sc;
195 }
196 
GetBleScanMode() const197 int BleConfig::GetBleScanMode() const
198 {
199     LOG_DEBUG("[BleConfig] %{public}s", __func__);
200 
201     int scanMode = BLE_SCAN_MODE_ALL;
202     bool ret = config_->GetValue(SECTION_HOST, PROPERTY_BLE_SCAN_MODE, scanMode);
203     if (!ret) {
204         scanMode = BLE_SCAN_MODE_ALL;
205         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble non disc mode is valid failed!");
206     }
207     return scanMode;
208 }
209 
GetBleLocalAddrType() const210 int BleConfig::GetBleLocalAddrType() const
211 {
212     LOG_DEBUG("[BleConfig] %{public}s", __func__);
213 
214     int localAddrType = BLE_ADDR_TYPE_PUBLIC;
215     bool ret = config_->GetValue(SECTION_HOST, PROPERTY_BLE_LOCAL_ADDR_TYPE, localAddrType);
216     if (!ret) {
217         localAddrType = BLE_ADDR_TYPE_PUBLIC;
218         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble non disc mode is valid failed!");
219     }
220     return localAddrType;
221 }
222 
GetBleAddrType() const223 int BleConfig::GetBleAddrType() const
224 {
225     LOG_DEBUG("[BleConfig] %{public}s", __func__);
226 
227     int addrType = BLE_ADDR_TYPE_PUBLIC;
228     bool ret = config_->GetValue(SECTION_HOST, PROPERTY_BLE_ADDR_TYPE, addrType);
229     if (!ret) {
230         addrType = BLE_ADDR_TYPE_PUBLIC;
231         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble non disc mode is valid failed!");
232     }
233     return addrType;
234 }
235 
SetBleModel1Level(int level) const236 bool BleConfig::SetBleModel1Level(int level) const
237 {
238     LOG_DEBUG("[BleConfig] %{public}s", __func__);
239 
240     bool ret = config_->SetValue(SECTION_HOST, PROPERTY_BLE_MODE_1_LEVEL, level);
241     if (!ret) {
242         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble model1 level failed!");
243     }
244     return ret;
245 }
246 
SetBleModel2Level(int level) const247 bool BleConfig::SetBleModel2Level(int level) const
248 {
249     LOG_DEBUG("[BleConfig] %{public}s", __func__);
250 
251     bool ret = config_->SetValue(SECTION_HOST, PROPERTY_BLE_MODE_2_LEVEL, level);
252     if (!ret) {
253         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble model1 level failed!");
254     }
255     return ret;
256 }
257 
SetBleSecurity(bool security) const258 bool BleConfig::SetBleSecurity(bool security) const
259 {
260     LOG_DEBUG("[BleConfig] %{public}s", __func__);
261 
262     bool ret = config_->SetValue(SECTION_HOST, PROPERTY_BLE_SECURITY, security);
263     if (!ret) {
264         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble security failed!");
265     }
266     return ret;
267 }
268 
SetBleScanMode(int scanmode) const269 bool BleConfig::SetBleScanMode(int scanmode) const
270 {
271     LOG_DEBUG("[BleConfig] %{public}s", __func__);
272 
273     bool ret = config_->SetValue(SECTION_HOST, PROPERTY_BLE_SCAN_MODE, scanmode);
274     if (!ret) {
275         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble scan mode failed!");
276     }
277     return ret;
278 }
279 
SetBleLocalAddrType(int localAddrType) const280 bool BleConfig::SetBleLocalAddrType(int localAddrType) const
281 {
282     LOG_DEBUG("[BleConfig] %{public}s", __func__);
283 
284     bool ret = config_->SetValue(SECTION_HOST, PROPERTY_BLE_LOCAL_ADDR_TYPE, localAddrType);
285     if (!ret) {
286         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble scan mode failed!");
287     }
288     return ret;
289 }
290 
GetAppearance() const291 int BleConfig::GetAppearance() const
292 {
293     LOG_DEBUG("[BleConfig] %{public}s", __func__);
294 
295     int appearance = 0;
296     bool ret = config_->GetValue(SECTION_HOST, PROPERTY_BLE_APPEARANCE, appearance);
297     if (!ret) {
298         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Get ble appearance failed!");
299     }
300     return appearance;
301 }
302 
SetLocalIdentityAddr(const std::string & addr) const303 bool BleConfig::SetLocalIdentityAddr(const std::string &addr) const
304 {
305     LOG_DEBUG("[BleConfig] %{public}s", __func__);
306 
307     bool ret = config_->SetValue(SECTION_HOST, PROPERTY_BLE_LOCAL_IDENTITY_ADDR, addr);
308     if (!ret) {
309         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble local identity addr failed!");
310     }
311     return ret;
312 }
313 
SetLocalIrk(const std::string & irk) const314 bool BleConfig::SetLocalIrk(const std::string &irk) const
315 {
316     LOG_DEBUG("[BleConfig] %{public}s", __func__);
317 
318     bool ret = config_->SetValue(SECTION_HOST, PROPERTY_BLE_LOCAL_IRK, irk);
319     if (!ret) {
320         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble local csrk failed!");
321     }
322     return ret;
323 }
324 
SetLocalLtk(const std::string & section,const std::string & ltk) const325 bool BleConfig::SetLocalLtk(const std::string &section, const std::string &ltk) const
326 {
327     LOG_DEBUG("[BleConfig] %{public}s", __func__);
328 
329     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_LOCAL_LTK, ltk);
330     if (!ret) {
331         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble local ltk failed!");
332     }
333     return ret;
334 }
335 
SetLocalKeySize(const std::string & section,const std::string & keysize) const336 bool BleConfig::SetLocalKeySize(const std::string &section, const std::string &keysize) const
337 {
338     LOG_DEBUG("[BleConfig] %{public}s", __func__);
339 
340     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_LOCAL_KEY_SIZE, keysize);
341     if (!ret) {
342         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble local keysize failed!");
343     }
344     return ret;
345 }
346 
SetLocalEdivRand(const std::string & section,const std::string & ediv,const std::string & rand) const347 bool BleConfig::SetLocalEdivRand(const std::string &section, const std::string &ediv, const std::string &rand) const
348 {
349     LOG_DEBUG("[BleConfig] %{public}s", __func__);
350 
351     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_LOCAL_EDIV, ediv);
352     if (!ret) {
353         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble local ediv failed!");
354     }
355 
356     ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_LOCAL_RAND, rand);
357     if (!ret) {
358         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble local rand failed!");
359     }
360     return ret;
361 }
362 
SetLocalCsrk(const std::string & section,const std::string & csrk) const363 bool BleConfig::SetLocalCsrk(const std::string &section, const std::string &csrk) const
364 {
365     LOG_DEBUG("[BleConfig] %{public}s", __func__);
366 
367     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_LOCAL_CSRK, csrk);
368     if (!ret) {
369         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble local csrk failed!");
370     }
371     return ret;
372 }
373 
SetLocalSignCounter(const std::string & section,uint32_t signCounter) const374 bool BleConfig::SetLocalSignCounter(const std::string &section, uint32_t signCounter) const
375 {
376     LOG_DEBUG("[BleConfig] %{public}s", __func__);
377 
378     bool ret = config_->SetValue(
379         SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_LOCAL_SIGN_COUNTER, std::to_string(signCounter));
380     if (!ret) {
381         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble local signCounter failed!");
382     }
383     return ret;
384 }
385 
SetPeerKeyType(const std::string & section,const std::string & keytype) const386 bool BleConfig::SetPeerKeyType(const std::string &section, const std::string &keytype) const
387 {
388     LOG_DEBUG("[BleConfig] %{public}s", __func__);
389 
390     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_KEY_TYPE, keytype);
391     if (!ret) {
392         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble peer keytype failed!");
393     }
394     return ret;
395 }
396 
SetPeerLtk(const std::string & section,const std::string & ltk) const397 bool BleConfig::SetPeerLtk(const std::string &section, const std::string &ltk) const
398 {
399     LOG_DEBUG("[BleConfig] %{public}s", __func__);
400 
401     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_LTK, ltk);
402     if (!ret) {
403         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble peer ltk failed!");
404     }
405     return ret;
406 }
407 
SetPeerKeySize(const std::string & section,const std::string & keysize) const408 bool BleConfig::SetPeerKeySize(const std::string &section, const std::string &keysize) const
409 {
410     LOG_DEBUG("[BleConfig] %{public}s", __func__);
411 
412     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_KEY_SIZE, keysize);
413     if (!ret) {
414         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble peer keysize failed!");
415     }
416     return ret;
417 }
418 
SetPeerEdivRand(const std::string & section,const std::string & ediv,const std::string & rand) const419 bool BleConfig::SetPeerEdivRand(const std::string &section, const std::string &ediv, const std::string &rand) const
420 {
421     LOG_DEBUG("[BleConfig] %{public}s", __func__);
422 
423     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_EDIV, ediv);
424     if (!ret) {
425         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble peer ediv failed!");
426     }
427 
428     ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_RAND, rand);
429     if (!ret) {
430         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble peer rand failed!");
431     }
432     return ret;
433 }
434 
SetPeerIdentityAddr(const std::string & section,uint8_t type,const std::string & peerAddress) const435 bool BleConfig::SetPeerIdentityAddr(const std::string &section, uint8_t type, const std::string &peerAddress) const
436 {
437     LOG_DEBUG("[BleConfig] %{public}s", __func__);
438 
439     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_IDENTITY_ADDR_TYPE, type);
440     if (!ret) {
441         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble peer identity addr type failed!");
442     }
443 
444     ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_IDENTITY_ADDR, peerAddress);
445     if (!ret) {
446         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble peer identity addr failed!");
447     }
448     return ret;
449 }
450 
SetPeerIrk(const std::string & section,const std::string & irk) const451 bool BleConfig::SetPeerIrk(const std::string &section, const std::string &irk) const
452 {
453     LOG_DEBUG("[BleConfig] %{public}s", __func__);
454 
455     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_IRK, irk);
456     if (!ret) {
457         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble peer irk failed!");
458     }
459     return ret;
460 }
461 
SetPeerCsrk(const std::string & section,const std::string & csrk) const462 bool BleConfig::SetPeerCsrk(const std::string &section, const std::string &csrk) const
463 {
464     LOG_DEBUG("[BleConfig] %{public}s", __func__);
465 
466     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_CSRK, csrk);
467     if (!ret) {
468         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble peer csrk failed!");
469     }
470     return ret;
471 }
472 
SetPeerSignCounter(const std::string & section,uint32_t signCounter) const473 bool BleConfig::SetPeerSignCounter(const std::string &section, uint32_t signCounter) const
474 {
475     LOG_DEBUG("[BleConfig] %{public}s", __func__);
476 
477     bool ret = config_->SetValue(
478         SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_SIGN_COUNTER, std::to_string(signCounter));
479     if (!ret) {
480         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set ble peer signCounter failed!");
481     }
482     return ret;
483 }
484 
GetLocalIrk() const485 std::string BleConfig::GetLocalIrk() const
486 {
487     LOG_DEBUG("[BleConfig] %{public}s", __func__);
488 
489     std::string irk;
490     bool ret = config_->GetValue(SECTION_HOST, PROPERTY_BLE_LOCAL_IRK, irk);
491     if (ret) {
492         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble local irk failed!");
493     }
494     return irk;
495 }
496 
GetLocalLtk(const std::string & section) const497 std::string BleConfig::GetLocalLtk(const std::string &section) const
498 {
499     LOG_DEBUG("[BleConfig] %{public}s", __func__);
500 
501     std::string ltk;
502     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_LOCAL_LTK, ltk);
503     if (ret) {
504         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble local ltk failed!");
505     }
506     return ltk;
507 }
508 
GetLocalEdiv(const std::string & section) const509 std::string BleConfig::GetLocalEdiv(const std::string &section) const
510 {
511     LOG_DEBUG("[BleConfig] %{public}s", __func__);
512 
513     std::string ediv;
514     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_LOCAL_EDIV, ediv);
515     if (!ret) {
516         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble local ediv failed!");
517     }
518     return ediv;
519 }
520 
GetLocalRand(const std::string & section) const521 std::string BleConfig::GetLocalRand(const std::string &section) const
522 {
523     LOG_DEBUG("[BleConfig] %{public}s", __func__);
524 
525     std::string rand;
526     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_LOCAL_RAND, rand);
527     if (!ret) {
528         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble local rand failed!");
529     }
530     return rand;
531 }
532 
GetLocalCsrk(const std::string & section) const533 std::string BleConfig::GetLocalCsrk(const std::string &section) const
534 {
535     LOG_DEBUG("[BleConfig] %{public}s", __func__);
536 
537     std::string csrk;
538     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_LOCAL_CSRK, csrk);
539     if (!ret) {
540         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble local csrk failed!");
541     }
542     return csrk;
543 }
544 
GetLocalSignCounter(const std::string & section) const545 uint32_t BleConfig::GetLocalSignCounter(const std::string &section) const
546 {
547     LOG_DEBUG("[BleConfig] %{public}s", __func__);
548 
549     std::string signCounter = "0";
550     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_LOCAL_SIGN_COUNTER, signCounter);
551     if (!ret) {
552         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble local signCounter failed!");
553     }
554     return std::stoul(signCounter);
555 }
556 
GetPeerLtk(const std::string & section) const557 std::string BleConfig::GetPeerLtk(const std::string &section) const
558 {
559     LOG_DEBUG("[BleConfig] %{public}s", __func__);
560 
561     std::string ltk;
562     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_LTK, ltk);
563     if (!ret) {
564         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble pear ltk failed!");
565     }
566     return ltk;
567 }
568 
GetPeerEdiv(const std::string & section) const569 std::string BleConfig::GetPeerEdiv(const std::string &section) const
570 {
571     LOG_DEBUG("[BleConfig] %{public}s", __func__);
572 
573     std::string ediv;
574     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_EDIV, ediv);
575     if (!ret) {
576         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble pear ediv failed!");
577     }
578     return ediv;
579 }
580 
GetPeerRand(const std::string & section) const581 std::string BleConfig::GetPeerRand(const std::string &section) const
582 {
583     LOG_DEBUG("[BleConfig] %{public}s", __func__);
584 
585     std::string rand;
586     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_RAND, rand);
587     if (!ret) {
588         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble pear rand failed!");
589     }
590     return rand;
591 }
592 
GetPeerIdentityAddr(const std::string & section) const593 std::string BleConfig::GetPeerIdentityAddr(const std::string &section) const
594 {
595     LOG_DEBUG("[BleConfig] %{public}s", __func__);
596 
597     std::string peerAddress;
598     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_IDENTITY_ADDR, peerAddress);
599     if (!ret) {
600         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble pear identity addr failed!");
601     }
602     return peerAddress;
603 }
604 
GetPeerIrk(const std::string & section) const605 std::string BleConfig::GetPeerIrk(const std::string &section) const
606 {
607     LOG_DEBUG("[BleConfig] %{public}s", __func__);
608 
609     std::string irk;
610     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_IRK, irk);
611     if (!ret) {
612         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble pear irk failed!");
613     }
614     return irk;
615 }
616 
GetPeerCsrk(const std::string & section) const617 std::string BleConfig::GetPeerCsrk(const std::string &section) const
618 {
619     LOG_DEBUG("[BleConfig] %{public}s", __func__);
620 
621     std::string csrk;
622     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_CSRK, csrk);
623     if (!ret) {
624         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble pear csrk failed!");
625     }
626     return csrk;
627 }
628 
GetPeerSignCounter(const std::string & section) const629 uint32_t BleConfig::GetPeerSignCounter(const std::string &section) const
630 {
631     LOG_DEBUG("[BleConfig] %{public}s", __func__);
632 
633     std::string signCounter = "0";
634     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, section, PROPERTY_BLE_PEER_SIGN_COUNTER, signCounter);
635     if (!ret) {
636         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get ble pear signCounter failed!");
637     }
638     return std::stoul(signCounter);
639 }
640 
GetPeerName(const std::string & subSection) const641 std::string BleConfig::GetPeerName(const std::string &subSection) const
642 {
643     LOG_DEBUG("[BleConfig] %{public}s", __func__);
644 
645     std::string name = "";
646     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, subSection, PROPERTY_DEVICE_NAME, name);
647     if (!ret) {
648         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get peer name failed!");
649     }
650     return name;
651 }
652 
GetPeerAlias(const std::string & subSection) const653 std::string BleConfig::GetPeerAlias(const std::string &subSection) const
654 {
655     LOG_DEBUG("[BleConfig] %{public}s", __func__);
656 
657     std::string name = "";
658     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, subSection, PROPERTY_ALIAS_NAME, name);
659     if (!ret) {
660         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get peer alias name failed!");
661     }
662     return name;
663 }
664 
GetPeerDeviceType(const std::string & subSection) const665 int BleConfig::GetPeerDeviceType(const std::string &subSection) const
666 {
667     LOG_DEBUG("[BleConfig] %{public}s", __func__);
668 
669     int type = 0;
670     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, subSection, PROPERTY_DEVICE_TYPE, type);
671     if (!ret) {
672         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get peer device type failed!");
673     }
674     return type;
675 }
676 
GetPeerDeviceIoCapability(const std::string & subSection) const677 int BleConfig::GetPeerDeviceIoCapability(const std::string &subSection) const
678 {
679     LOG_DEBUG("[BleConfig] %{public}s", __func__);
680 
681     int io = 0;
682     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, subSection, PROPERTY_IO_CAPABILITY, io);
683     if (!ret) {
684         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get peer io capability failed!");
685     }
686     return io;
687 }
688 
SetPeerName(const std::string & subSection,const std::string & name) const689 bool BleConfig::SetPeerName(const std::string &subSection, const std::string &name) const
690 {
691     LOG_DEBUG("[BleConfig] %{public}s", __func__);
692 
693     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, subSection, PROPERTY_DEVICE_NAME, name);
694     if (!ret) {
695         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set peer device name failed!");
696     }
697     return ret;
698 }
699 
SetPeerDeviceType(const std::string & subSection,const int type) const700 bool BleConfig::SetPeerDeviceType(const std::string &subSection, const int type) const
701 {
702     LOG_DEBUG("[BleConfig] %{public}s", __func__);
703 
704     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, subSection, PROPERTY_DEVICE_TYPE, type);
705     if (!ret) {
706         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set peer device type failed!");
707     }
708     return ret;
709 }
710 
SetPeerAddressType(const std::string & subSection,const int type) const711 bool BleConfig::SetPeerAddressType(const std::string &subSection, const int type) const
712 {
713     LOG_DEBUG("[BleConfig] %{public}s", __func__);
714 
715     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, subSection, PROPERTY_BLE_PEER_ADDR_TYPE, type);
716     if (!ret) {
717         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set peer address type failed!");
718     }
719     return ret;
720 }
721 
GetPeerAddressType(const std::string & subSection) const722 int BleConfig::GetPeerAddressType(const std::string &subSection) const
723 {
724     LOG_DEBUG("[BleConfig] %{public}s", __func__);
725 
726     int type = 0;
727     bool ret = config_->GetValue(SECTION_BLE_PAIRED_LIST, subSection, PROPERTY_BLE_PEER_ADDR_TYPE, type);
728     if (!ret) {
729         LOG_DEBUG("[BleConfig] %{public}s:%{public}s", __func__, "Get peer address type failed!");
730     }
731     return type;
732 }
733 
RemovePairedDevice(const std::string & subSection) const734 bool BleConfig::RemovePairedDevice(const std::string &subSection) const
735 {
736     LOG_DEBUG("[BleConfig] %{public}s", __func__);
737 
738     bool ret = config_->RemoveSection(SECTION_BLE_PAIRED_LIST, subSection);
739     if (!ret) {
740         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Remove paired device info failed!");
741     }
742     return ret;
743 }
744 
GetPairedAddrList() const745 std::vector<std::string> BleConfig::GetPairedAddrList() const
746 {
747     LOG_DEBUG("[BleConfig] %{public}s", __func__);
748 
749     std::vector<std::string> pairedList;
750     bool ret = config_->GetSubSections(SECTION_BLE_PAIRED_LIST, pairedList);
751     if (!ret) {
752         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Get Paired Device List failed!");
753     }
754     return pairedList;
755 }
756 
SetPeerDeviceIoCapability(const std::string & subSection,int io) const757 bool BleConfig::SetPeerDeviceIoCapability(const std::string &subSection, int io) const
758 {
759     LOG_DEBUG("[BleConfig] %{public}s", __func__);
760 
761     bool ret = config_->SetValue(SECTION_BLE_PAIRED_LIST, subSection, PROPERTY_IO_CAPABILITY, io);
762     if (!ret) {
763         LOG_ERROR("[BleConfig] %{public}s:%{public}s", __func__, "Set peer device name failed!");
764     }
765     return ret;
766 }
767 }  // namespace bluetooth
768 }  // namespace OHOS
769