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 "adapter_device_info.h"
17
18 #include <fstream>
19
20 #include "log.h"
21 #include "xml_parse.h"
22
23 namespace OHOS {
24 namespace bluetooth {
25 AdapterDeviceInfo *AdapterDeviceInfo::g_instance = nullptr;
26
27 struct AdapterDeviceInfo::impl {
28 utility::XmlParse parse_ = {};
29 std::string fileName_ = {"bt_device_info.xml"};
30 std::string filePath_ = {BT_CONFIG_PATH + fileName_};
31 std::string fileBasePath_ = {BT_CONFIG_PATH_BASE + fileName_};
32 };
33
GetInstance()34 IAdapterDeviceInfo *AdapterDeviceInfo::GetInstance()
35 {
36 if (g_instance == nullptr) {
37 static AdapterDeviceInfo instance;
38 g_instance = &instance;
39 }
40
41 return static_cast<IAdapterDeviceInfo *>(g_instance);
42 }
43
AdapterDeviceInfo()44 AdapterDeviceInfo::AdapterDeviceInfo() : pimpl(std::make_unique<impl>()){};
45
~AdapterDeviceInfo()46 AdapterDeviceInfo::~AdapterDeviceInfo()
47 {}
48
Load()49 bool AdapterDeviceInfo::Load()
50 {
51 std::lock_guard<std::mutex> lg(mutex_);
52 if (pimpl->parse_.Load(pimpl->filePath_)) {
53 return true;
54 } else {
55 if (!Reload()) {
56 return false;
57 }
58 return pimpl->parse_.Load(pimpl->filePath_);
59 }
60 }
61
Reload()62 bool AdapterDeviceInfo::Reload()
63 {
64 std::ifstream fin(pimpl->fileBasePath_, std::ios::in | std::ios::binary);
65 if (!fin) {
66 return false;
67 }
68 std::ofstream fout(pimpl->filePath_, std::ios::out | std::ios::trunc);
69 if (!fout) {
70 return false;
71 }
72 fout << fin.rdbuf();
73 return true;
74 }
75
GetValue(const std::string & section,const std::string & property,std::string & value)76 bool AdapterDeviceInfo::GetValue(const std::string §ion, const std::string &property, std::string &value)
77 {
78 std::lock_guard<std::mutex> lg(mutex_);
79 return pimpl->parse_.GetValue(section, property, value);
80 }
81
GetValue(const std::string & section,const std::string & property,bool & value)82 bool AdapterDeviceInfo::GetValue(const std::string §ion, const std::string &property, bool &value)
83 {
84 std::lock_guard<std::mutex> lg(mutex_);
85 return pimpl->parse_.GetValue(section, property, value);
86 }
87
GetValue(const std::string & section,const std::string & property,int & value)88 bool AdapterDeviceInfo::GetValue(const std::string §ion, const std::string &property, int &value)
89 {
90 std::lock_guard<std::mutex> lg(mutex_);
91 return pimpl->parse_.GetValue(section, property, value);
92 }
93 } // namespace bluetooth
94 } // namespace OHOS