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 OHOS {
24 namespace bluetooth {
25 AdapterDeviceConfig *AdapterDeviceConfig::g_instance = nullptr;
26 
27 struct AdapterDeviceConfig::impl {
28     utility::XmlParse parse_ {};
29     std::string fileName_ {"bt_device_config.xml"};
30     std::string filePath_ {BT_CONFIG_PATH + fileName_};
31     std::string fileBasePath_ {BT_CONFIG_PATH_BASE + fileName_};
32 };
33 
GetInstance()34 IAdapterDeviceConfig *AdapterDeviceConfig::GetInstance()
35 {
36     if (g_instance == nullptr) {
37         static AdapterDeviceConfig instance;
38         g_instance = &instance;
39     }
40 
41     return static_cast<IAdapterDeviceConfig *>(g_instance);
42 }
43 
AdapterDeviceConfig()44 AdapterDeviceConfig::AdapterDeviceConfig() : pimpl(std::make_unique<impl>()){};
45 
~AdapterDeviceConfig()46 AdapterDeviceConfig::~AdapterDeviceConfig()
47 {}
48 
Load()49 bool AdapterDeviceConfig::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 AdapterDeviceConfig::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 
Save()76 bool AdapterDeviceConfig::Save()
77 {
78     std::lock_guard<std::mutex> lg(mutex_);
79     return pimpl->parse_.Save();
80 }
81 
SetValue(const std::string & section,const std::string & property,const int & value)82 bool AdapterDeviceConfig::SetValue(const std::string &section, const std::string &property, const int &value)
83 {
84     std::lock_guard<std::mutex> lg(mutex_);
85     return pimpl->parse_.SetValue(section, property, value);
86 }
87 
SetValue(const std::string & section,const std::string & property,const std::string & value)88 bool AdapterDeviceConfig::SetValue(const std::string &section, const std::string &property, const std::string &value)
89 {
90     std::lock_guard<std::mutex> lg(mutex_);
91     return pimpl->parse_.SetValue(section, property, value);
92 }
93 
GetValue(const std::string & section,const std::string & property,int & value)94 bool AdapterDeviceConfig::GetValue(const std::string &section, const std::string &property, int &value)
95 {
96     std::lock_guard<std::mutex> lg(mutex_);
97     return pimpl->parse_.GetValue(section, property, value);
98 }
99 
GetValue(const std::string & section,const std::string & property,std::string & value)100 bool AdapterDeviceConfig::GetValue(const std::string &section, const std::string &property, std::string &value)
101 {
102     std::lock_guard<std::mutex> lg(mutex_);
103 
104     return pimpl->parse_.GetValue(section, property, value);
105 }
106 
GetValue(const std::string & section,const std::string & property,bool & value)107 bool AdapterDeviceConfig::GetValue(const std::string &section, const std::string &property, bool &value)
108 {
109     std::lock_guard<std::mutex> lg(mutex_);
110     return pimpl->parse_.GetValue(section, property, value);
111 }
112 
SetValue(const std::string & section,const std::string & subSection,const std::string & property,const int & value)113 bool AdapterDeviceConfig::SetValue(
114     const std::string &section, const std::string &subSection, const std::string &property, const int &value)
115 {
116     std::lock_guard<std::mutex> lg(mutex_);
117     return pimpl->parse_.SetValue(section, subSection, property, value);
118 }
SetValue(const std::string & section,const std::string & subSection,const std::string & property,const std::string & value)119 bool AdapterDeviceConfig::SetValue(
120     const std::string &section, const std::string &subSection, const std::string &property, const std::string &value)
121 {
122     std::lock_guard<std::mutex> lg(mutex_);
123     return pimpl->parse_.SetValue(section, subSection, property, value);
124 }
125 
SetValue(const std::string & section,const std::string & subSection,const std::string & property,const bool & value)126 bool AdapterDeviceConfig::SetValue(
127     const std::string &section, const std::string &subSection, const std::string &property, const bool &value)
128 {
129     std::lock_guard<std::mutex> lg(mutex_);
130     return pimpl->parse_.SetValue(section, subSection, property, value);
131 }
132 
GetValue(const std::string & section,const std::string & subSection,const std::string & property,int & value)133 bool AdapterDeviceConfig::GetValue(
134     const std::string &section, const std::string &subSection, const std::string &property, int &value)
135 {
136     std::lock_guard<std::mutex> lg(mutex_);
137     return pimpl->parse_.GetValue(section, subSection, property, value);
138 }
139 
GetValue(const std::string & section,const std::string & subSection,const std::string & property,std::string & value)140 bool AdapterDeviceConfig::GetValue(
141     const std::string &section, const std::string &subSection, const std::string &property, std::string &value)
142 {
143     std::lock_guard<std::mutex> lg(mutex_);
144     return pimpl->parse_.GetValue(section, subSection, property, value);
145 }
146 
GetValue(const std::string & section,const std::string & subSection,const std::string & property,bool & value)147 bool AdapterDeviceConfig::GetValue(
148     const std::string &section, const std::string &subSection, const std::string &property, bool &value)
149 {
150     std::lock_guard<std::mutex> lg(mutex_);
151     return pimpl->parse_.GetValue(section, subSection, property, value);
152 }
153 
GetSubSections(const std::string & section,std::vector<std::string> & subSections)154 bool AdapterDeviceConfig::GetSubSections(const std::string &section, std::vector<std::string> &subSections)
155 {
156     std::lock_guard<std::mutex> lg(mutex_);
157     return pimpl->parse_.GetSubSections(section, subSections);
158 }
159 
RemoveSection(const std::string & section,const std::string & subSection)160 bool AdapterDeviceConfig::RemoveSection(const std::string &section, const std::string &subSection)
161 {
162     std::lock_guard<std::mutex> lg(mutex_);
163     return pimpl->parse_.RemoveSection(section, subSection);
164 }
165 }  // namespace bluetooth
166 }  // namespace OHOS