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_config.h"
17
18 #include <fstream>
19
20 #include "log.h"
21 #include "xml_parse.h"
22
23 namespace bluetooth {
24 AdapterDeviceConfig *AdapterDeviceConfig::g_instance = nullptr;
25
26 struct AdapterDeviceConfig::impl {
27 utility::XmlParse parse_ {};
28 std::string fileName_ {"bt_device_config.xml"};
29 std::string filePath_ {BT_CONFIG_PATH + fileName_};
30 std::string fileBasePath_ {BT_CONFIG_PATH_BASE + fileName_};
31 };
32
GetInstance()33 IAdapterDeviceConfig *AdapterDeviceConfig::GetInstance()
34 {
35 if (g_instance == nullptr) {
36 static AdapterDeviceConfig instance;
37 g_instance = &instance;
38 }
39
40 return static_cast<IAdapterDeviceConfig *>(g_instance);
41 }
42
AdapterDeviceConfig()43 AdapterDeviceConfig::AdapterDeviceConfig() : pimpl(std::make_unique<impl>()){};
44
~AdapterDeviceConfig()45 AdapterDeviceConfig::~AdapterDeviceConfig()
46 {}
47
Load()48 bool AdapterDeviceConfig::Load()
49 {
50 std::lock_guard<std::mutex> lg(mutex_);
51 if (pimpl->parse_.Load(pimpl->filePath_)) {
52 return true;
53 } else {
54 if (!Reload()) {
55 return false;
56 }
57 return pimpl->parse_.Load(pimpl->filePath_);
58 }
59 }
60
Reload()61 bool AdapterDeviceConfig::Reload()
62 {
63 std::ifstream fin(pimpl->fileBasePath_, std::ios::in | std::ios::binary);
64 if (!fin) {
65 return false;
66 }
67 std::ofstream fout(pimpl->filePath_, std::ios::out | std::ios::trunc);
68 if (!fout) {
69 return false;
70 }
71 fout << fin.rdbuf();
72 return true;
73 }
74
Save()75 bool AdapterDeviceConfig::Save()
76 {
77 std::lock_guard<std::mutex> lg(mutex_);
78 return pimpl->parse_.Save();
79 }
80
SetValue(const std::string & section,const std::string & property,const int & value)81 bool AdapterDeviceConfig::SetValue(const std::string §ion, const std::string &property, const int &value)
82 {
83 std::lock_guard<std::mutex> lg(mutex_);
84 return pimpl->parse_.SetValue(section, property, value);
85 }
86
SetValue(const std::string & section,const std::string & property,const std::string & value)87 bool AdapterDeviceConfig::SetValue(const std::string §ion, const std::string &property, const std::string &value)
88 {
89 std::lock_guard<std::mutex> lg(mutex_);
90 return pimpl->parse_.SetValue(section, property, value);
91 }
92
GetValue(const std::string & section,const std::string & property,int & value)93 bool AdapterDeviceConfig::GetValue(const std::string §ion, const std::string &property, int &value)
94 {
95 std::lock_guard<std::mutex> lg(mutex_);
96 return pimpl->parse_.GetValue(section, property, value);
97 }
98
GetValue(const std::string & section,const std::string & property,std::string & value)99 bool AdapterDeviceConfig::GetValue(const std::string §ion, const std::string &property, std::string &value)
100 {
101 std::lock_guard<std::mutex> lg(mutex_);
102
103 return pimpl->parse_.GetValue(section, property, value);
104 }
105
GetValue(const std::string & section,const std::string & property,bool & value)106 bool AdapterDeviceConfig::GetValue(const std::string §ion, const std::string &property, bool &value)
107 {
108 std::lock_guard<std::mutex> lg(mutex_);
109 return pimpl->parse_.GetValue(section, property, value);
110 }
111
SetValue(const std::string & section,const std::string & subSection,const std::string & property,const int & value)112 bool AdapterDeviceConfig::SetValue(
113 const std::string §ion, const std::string &subSection, const std::string &property, const int &value)
114 {
115 std::lock_guard<std::mutex> lg(mutex_);
116 return pimpl->parse_.SetValue(section, subSection, property, value);
117 }
SetValue(const std::string & section,const std::string & subSection,const std::string & property,const std::string & value)118 bool AdapterDeviceConfig::SetValue(
119 const std::string §ion, const std::string &subSection, const std::string &property, const std::string &value)
120 {
121 std::lock_guard<std::mutex> lg(mutex_);
122 return pimpl->parse_.SetValue(section, subSection, property, value);
123 }
124
SetValue(const std::string & section,const std::string & subSection,const std::string & property,const bool & value)125 bool AdapterDeviceConfig::SetValue(
126 const std::string §ion, const std::string &subSection, const std::string &property, const bool &value)
127 {
128 std::lock_guard<std::mutex> lg(mutex_);
129 return pimpl->parse_.SetValue(section, subSection, property, value);
130 }
131
GetValue(const std::string & section,const std::string & subSection,const std::string & property,int & value)132 bool AdapterDeviceConfig::GetValue(
133 const std::string §ion, const std::string &subSection, const std::string &property, int &value)
134 {
135 std::lock_guard<std::mutex> lg(mutex_);
136 return pimpl->parse_.GetValue(section, subSection, property, value);
137 }
138
GetValue(const std::string & section,const std::string & subSection,const std::string & property,std::string & value)139 bool AdapterDeviceConfig::GetValue(
140 const std::string §ion, const std::string &subSection, const std::string &property, std::string &value)
141 {
142 std::lock_guard<std::mutex> lg(mutex_);
143 return pimpl->parse_.GetValue(section, subSection, property, value);
144 }
145
GetValue(const std::string & section,const std::string & subSection,const std::string & property,bool & value)146 bool AdapterDeviceConfig::GetValue(
147 const std::string §ion, const std::string &subSection, const std::string &property, bool &value)
148 {
149 std::lock_guard<std::mutex> lg(mutex_);
150 return pimpl->parse_.GetValue(section, subSection, property, value);
151 }
152
GetSubSections(const std::string & section,std::vector<std::string> & subSections)153 bool AdapterDeviceConfig::GetSubSections(const std::string §ion, std::vector<std::string> &subSections)
154 {
155 std::lock_guard<std::mutex> lg(mutex_);
156 return pimpl->parse_.GetSubSections(section, subSections);
157 }
158
RemoveSection(const std::string & section,const std::string & subSection)159 bool AdapterDeviceConfig::RemoveSection(const std::string §ion, const std::string &subSection)
160 {
161 std::lock_guard<std::mutex> lg(mutex_);
162 return pimpl->parse_.RemoveSection(section, subSection);
163 }
164 } // namespace bluetooth