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 "profile_config.h"
17
18 #include <fstream>
19 #include <mutex>
20
21 #include "xml_parse.h"
22
23 namespace OHOS {
24 namespace bluetooth {
25 struct ProfileConfig::impl {
26 utility::XmlParse parse_ = {};
27 std::mutex mutex_ = {};
28 std::string fileName_ = {"bt_profile_config.xml"};
29 std::string filePath_ = {BT_CONFIG_PATH + fileName_};
30 std::string fileBasePath_ = {BT_CONFIG_PATH_BASE + fileName_};
31 };
32
GetInstance()33 IProfileConfig *ProfileConfig::GetInstance()
34 {
35 static ProfileConfig instance;
36 return reinterpret_cast<IProfileConfig *>(&instance);
37 }
38
ProfileConfig()39 ProfileConfig::ProfileConfig() : pimpl(std::make_unique<impl>()){};
40
~ProfileConfig()41 ProfileConfig::~ProfileConfig(){};
42
Load()43 bool ProfileConfig::Load()
44 {
45 std::lock_guard<std::mutex> lock(pimpl->mutex_);
46 if (pimpl->parse_.Load(pimpl->filePath_)) {
47 return true;
48 } else {
49 if (!Reload()) {
50 return false;
51 }
52 return pimpl->parse_.Load(pimpl->filePath_);
53 }
54 }
55
Reload()56 bool ProfileConfig::Reload()
57 {
58 std::ifstream fin(pimpl->fileBasePath_, std::ios::in | std::ios::binary);
59 if (!fin) {
60 return false;
61 }
62 std::ofstream fout(pimpl->filePath_, std::ios::out | std::ios::trunc);
63 if (!fout) {
64 return false;
65 }
66 fout << fin.rdbuf();
67 return true;
68 }
69
GetValue(const std::string & addr,const std::string & section,const std::string & property,int & value)70 bool ProfileConfig::GetValue(
71 const std::string &addr, const std::string §ion, const std::string &property, int &value)
72 {
73 std::lock_guard<std::mutex> lock(pimpl->mutex_);
74 return pimpl->parse_.GetValue(addr, section, property, value);
75 }
76
GetValue(const std::string & addr,const std::string & section,const std::string & property,bool & value)77 bool ProfileConfig::GetValue(
78 const std::string &addr, const std::string §ion, const std::string &property, bool &value)
79 {
80 std::lock_guard<std::mutex> lock(pimpl->mutex_);
81 return pimpl->parse_.GetValue(addr, section, property, value);
82 }
83
SetValue(const std::string & addr,const std::string & section,const std::string & property,int & value)84 bool ProfileConfig::SetValue(
85 const std::string &addr, const std::string §ion, const std::string &property, int &value)
86 {
87 std::lock_guard<std::mutex> lock(pimpl->mutex_);
88 if (!pimpl->parse_.SetValue(addr, section, property, value)) {
89 return false;
90 }
91 return pimpl->parse_.Save();
92 }
93
SetValue(const std::string & addr,const std::string & section,const std::string & property,bool & value)94 bool ProfileConfig::SetValue(
95 const std::string &addr, const std::string §ion, const std::string &property, bool &value)
96 {
97 std::lock_guard<std::mutex> lock(pimpl->mutex_);
98 if (!pimpl->parse_.SetValue(addr, section, property, value)) {
99 return false;
100 }
101 return pimpl->parse_.Save();
102 }
103
RemoveAddr(const std::string & addr)104 bool ProfileConfig::RemoveAddr(const std::string &addr)
105 {
106 std::lock_guard<std::mutex> lock(pimpl->mutex_);
107 if (!pimpl->parse_.RemoveSection(addr)) {
108 return false;
109 }
110 return pimpl->parse_.Save();
111 }
112
RemoveProperty(const std::string & addr,const std::string & section,const std::string & property)113 bool ProfileConfig::RemoveProperty(const std::string &addr, const std::string §ion, const std::string &property)
114 {
115 std::lock_guard<std::mutex> lock(pimpl->mutex_);
116 if (!pimpl->parse_.RemoveProperty(addr, section, property)) {
117 return false;
118 }
119 return pimpl->parse_.Save();
120 }
121
HasSection(const std::string & addr,const std::string & section)122 bool ProfileConfig::HasSection(const std::string &addr, const std::string §ion)
123 {
124 std::lock_guard<std::mutex> lock(pimpl->mutex_);
125 return pimpl->parse_.HasSection(addr, section);
126 }
127 } // namespace bluetooth
128 } // namespace OHOS