1 /*
2 * Copyright (C) 2022 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 #include "location_config_manager.h"
16
17 #include <fstream>
18
19 #include "common_utils.h"
20 #include "constant_definition.h"
21 #include "location_log.h"
22
23 #include "parameter.h"
24
25 namespace OHOS {
26 namespace Location {
GetInstance()27 LocationConfigManager &LocationConfigManager::GetInstance()
28 {
29 static LocationConfigManager gLocationConfigManager;
30 return gLocationConfigManager;
31 }
32
LocationConfigManager()33 LocationConfigManager::LocationConfigManager()
34 {
35 mLocationSwitchState = STATE_CLOSE;
36 mPrivacyTypeState[PRIVACY_TYPE_OTHERS] = STATE_CLOSE; // for OTHERS
37 mPrivacyTypeState[PRIVACY_TYPE_STARTUP] = STATE_CLOSE; // for STARTUP
38 mPrivacyTypeState[PRIVACY_TYPE_CORE_LOCATION] = STATE_CLOSE; // for CORE_LOCATION
39 }
40
~LocationConfigManager()41 LocationConfigManager::~LocationConfigManager()
42 {
43 }
44
Init()45 int LocationConfigManager::Init()
46 {
47 LBSLOGI(LOCATOR, "LocationConfigManager::Init");
48 if (!IsExistFile(GetLocationSwitchConfigPath())) {
49 CreateFile(GetLocationSwitchConfigPath(), "0");
50 }
51 return 0;
52 }
53
IsExistFile(const std::string & filename)54 bool LocationConfigManager::IsExistFile(const std::string& filename)
55 {
56 bool bExist = false;
57 std::fstream ioFile;
58 char path[PATH_MAX + 1] = {0x00};
59 if (strlen(filename.c_str()) > PATH_MAX || realpath(filename.c_str(), path) == NULL) {
60 return false;
61 }
62 ioFile.open(path, std::ios::in);
63 if (ioFile) {
64 bExist = true;
65 } else {
66 return false;
67 }
68 ioFile.clear();
69 ioFile.close();
70 LBSLOGD(LOCATOR, "IsExistFile = %{public}d", bExist ? 1 : 0);
71 return bExist;
72 }
73
CreateFile(const std::string & filename,const std::string & filedata)74 bool LocationConfigManager::CreateFile(const std::string& filename, const std::string& filedata)
75 {
76 LBSLOGD(LOCATOR, "CreateFile");
77 std::ofstream outFile;
78 outFile.open(filename.c_str());
79 if (!outFile) {
80 LBSLOGE(LOCATOR, "file open failed");
81 return false;
82 }
83 outFile.flush();
84 outFile << filedata << std::endl;
85 outFile.clear();
86 outFile.close();
87 return true;
88 }
89
GetLocationSwitchConfigPath()90 std::string LocationConfigManager::GetLocationSwitchConfigPath()
91 {
92 int userId = 0;
93 bool ret = CommonUtils::GetCurrentUserId(userId);
94 if (!ret) {
95 LBSLOGE(LOCATOR, "%{public}s GetCurrentUserId failed", __func__);
96 }
97 std::string filePath = LOCATION_DIR + SWITCH_CONFIG_NAME + "_" + std::to_string(userId) + ".conf";
98 return filePath;
99 }
100
GetPrivacyTypeConfigPath(const int type)101 std::string LocationConfigManager::GetPrivacyTypeConfigPath(const int type)
102 {
103 int userId = 0;
104 bool ret = CommonUtils::GetCurrentUserId(userId);
105 if (!ret) {
106 LBSLOGE(LOCATOR, "%{public}s GetCurrentUserId failed", __func__);
107 }
108 std::string filePath;
109 switch (type) {
110 case PRIVACY_TYPE_OTHERS: {
111 filePath = "others_";
112 break;
113 }
114 case PRIVACY_TYPE_STARTUP: {
115 filePath = "startup_";
116 break;
117 }
118 case PRIVACY_TYPE_CORE_LOCATION: {
119 filePath = "core_location_";
120 break;
121 }
122 default: {
123 filePath = "";
124 break;
125 }
126 }
127 return LOCATION_DIR + PRIVACY_CONFIG_NAME + "_" + filePath + std::to_string(userId) + ".conf";
128 }
129
GetLocationSwitchState()130 int LocationConfigManager::GetLocationSwitchState()
131 {
132 std::unique_lock<std::mutex> lock(mutex_);
133 if (!IsExistFile(GetLocationSwitchConfigPath())) {
134 CreateFile(GetLocationSwitchConfigPath(), "0");
135 }
136 std::ifstream fs(GetLocationSwitchConfigPath());
137 if (!fs.is_open()) {
138 LBSLOGE(LOCATOR, "LocationConfigManager: fs.is_open false, return");
139 return -1;
140 }
141 std::string line;
142 while (std::getline(fs, line)) {
143 if (line.empty()) {
144 break;
145 }
146 if (line[0] == '0') {
147 mLocationSwitchState = STATE_CLOSE;
148 } else if (line[0] == '1') {
149 mLocationSwitchState = STATE_OPEN;
150 }
151 break;
152 }
153 fs.clear();
154 fs.close();
155 return mLocationSwitchState;
156 }
157
GetStringParameter(const std::string & type,std::string & value)158 bool LocationConfigManager::GetStringParameter(const std::string& type, std::string& value)
159 {
160 char result[MAX_BUFF_SIZE] = {0};
161 auto res = GetParameter(type.c_str(), "", result, MAX_BUFF_SIZE);
162 if (res <= 0) {
163 LBSLOGE(LOCATOR, "%{public}s get para value failed, res: %{public}d",
164 __func__, res);
165 return false;
166 }
167 value = result;
168 return true;
169 }
170
GetIntParameter(const std::string & type)171 int LocationConfigManager::GetIntParameter(const std::string& type)
172 {
173 char result[MAX_BUFF_SIZE] = {0};
174 std::string value = "";
175 auto res = GetParameter(type.c_str(), "", result, MAX_BUFF_SIZE);
176 if (res <= 0) {
177 LBSLOGE(LOCATOR, "%{public}s get para value failed, res: %{public}d",
178 __func__, res);
179 return -1;
180 }
181 value = result;
182 for (auto ch : value) {
183 if (std::isdigit(ch) == 0) {
184 LBSLOGE(LOCATOR, "wrong para");
185 return -1;
186 }
187 }
188 return std::stoi(value);
189 }
190
GetNlpServiceName(std::string & name)191 bool LocationConfigManager::GetNlpServiceName(std::string& name)
192 {
193 return GetStringParameter(NLP_SERVICE_NAME, name);
194 }
195
GetNlpAbilityName(std::string & name)196 bool LocationConfigManager::GetNlpAbilityName(std::string& name)
197 {
198 return GetStringParameter(NLP_ABILITY_NAME, name);
199 }
200
GetGeocodeServiceName(std::string & name)201 bool LocationConfigManager::GetGeocodeServiceName(std::string& name)
202 {
203 return GetStringParameter(GEOCODE_SERVICE_NAME, name);
204 }
205
GetGeocodeAbilityName(std::string & name)206 bool LocationConfigManager::GetGeocodeAbilityName(std::string& name)
207 {
208 return GetStringParameter(GEOCODE_ABILITY_NAME, name);
209 }
210
GetSuplMode()211 int LocationConfigManager::GetSuplMode()
212 {
213 return GetIntParameter(SUPL_MODE_NAME);
214 }
215
GetAgnssServerAddr(std::string & name)216 bool LocationConfigManager::GetAgnssServerAddr(std::string& name)
217 {
218 return GetStringParameter(AGNSS_SERVER_ADDR, name);
219 }
220
GetAgnssServerPort()221 int LocationConfigManager::GetAgnssServerPort()
222 {
223 return GetIntParameter(AGNSS_SERVER_PORT);
224 }
225
SetLocationSwitchState(int state)226 int LocationConfigManager::SetLocationSwitchState(int state)
227 {
228 std::unique_lock<std::mutex> lock(mutex_);
229 if (!IsExistFile(GetLocationSwitchConfigPath())) {
230 CreateFile(GetLocationSwitchConfigPath(), "0");
231 }
232 std::fstream fs(GetLocationSwitchConfigPath());
233 if (state != STATE_CLOSE && state != STATE_OPEN) {
234 LBSLOGE(LOCATOR, "LocationConfigManager:SetLocationSwitchState state = %{public}d, return", state);
235 return -1;
236 }
237 if (!fs.is_open()) {
238 LBSLOGE(LOCATOR, "LocationConfigManager: fs.is_open false, return");
239 return -1;
240 }
241 std::string content = "1";
242 if (state == STATE_CLOSE) {
243 content = "0";
244 }
245 fs.write(content.c_str(), content.length());
246 fs.clear();
247 fs.close();
248 mLocationSwitchState = state;
249 return 0;
250 }
251
GetPrivacyTypeState(const int type,bool & isConfirmed)252 LocationErrCode LocationConfigManager::GetPrivacyTypeState(const int type, bool& isConfirmed)
253 {
254 if (type < PRIVACY_TYPE_OTHERS || type > PRIVACY_TYPE_CORE_LOCATION) {
255 LBSLOGI(LOCATOR, "GetPrivacyTypeState,invalid types");
256 isConfirmed = false;
257 return ERRCODE_INVALID_PARAM;
258 }
259 std::unique_lock<std::mutex> lock(mutex_);
260 if (!IsExistFile(GetPrivacyTypeConfigPath(type))) {
261 CreateFile(GetPrivacyTypeConfigPath(type), "0");
262 }
263 std::ifstream fs(GetPrivacyTypeConfigPath(type));
264 if (!fs.is_open()) {
265 LBSLOGE(LOCATOR, "LocationConfigManager: fs.is_open false, return");
266 isConfirmed = false;
267 return ERRCODE_SERVICE_UNAVAILABLE;
268 }
269 std::string line;
270 while (std::getline(fs, line)) {
271 if (line.empty()) {
272 break;
273 }
274 if (line[0] == '0') {
275 mPrivacyTypeState[type] = STATE_CLOSE;
276 } else if (line[0] == '1') {
277 mPrivacyTypeState[type] = STATE_OPEN;
278 }
279 break;
280 }
281 fs.clear();
282 fs.close();
283 isConfirmed = (mPrivacyTypeState[type] == STATE_OPEN) ? true : false;
284 return ERRCODE_SUCCESS;
285 }
286
SetPrivacyTypeState(const int type,bool isConfirmed)287 LocationErrCode LocationConfigManager::SetPrivacyTypeState(const int type, bool isConfirmed)
288 {
289 if (type < PRIVACY_TYPE_OTHERS || type > PRIVACY_TYPE_CORE_LOCATION) {
290 LBSLOGE(LOCATOR, "SetPrivacyTypeState,invalid types");
291 return ERRCODE_INVALID_PARAM;
292 }
293 std::unique_lock<std::mutex> lock(mutex_);
294 if (!IsExistFile(GetPrivacyTypeConfigPath(type))) {
295 CreateFile(GetPrivacyTypeConfigPath(type), "0");
296 }
297 std::fstream fs(GetPrivacyTypeConfigPath(type));
298 if (!fs.is_open()) {
299 LBSLOGE(LOCATOR, "LocationConfigManager: fs.is_open false, return");
300 return ERRCODE_SERVICE_UNAVAILABLE;
301 }
302 std::string content = "0";
303 if (isConfirmed) {
304 content = "1";
305 }
306 fs.write(content.c_str(), content.length());
307 fs.clear();
308 fs.close();
309 mPrivacyTypeState[type] = isConfirmed ? 1 : 0;
310 return ERRCODE_SUCCESS;
311 }
312 } // namespace Location
313 } // namespace OHOS
314