• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2025 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 "feature/param_update/param_manager.h"
17 
18 #include <iostream>
19 #include <fstream>
20 #include "form_constants.h"
21 #include "feature/param_update/param_reader.h"
22 #include "common/util/string_utils.h"
23 #include "data_center/database/form_rdb_data_mgr.h"
24 #include "fms_log_wrapper.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29     const std::string PARAM_INSTALL_PATH = "/data/service/el1/public/update/param_service/install/system/";
30     constexpr const char* FORM_MGR_CONFIG_VERSION = "FormMgrConfig_version";
31     constexpr const char* FORM_MGR_CONFIG_DATA = "FormMgrConfig_data";
32     const std::string CONFIG_FILE_NAME = "form_mgr_config.json";
33     const std::string PARAM_PRESET_PATH = "/system/etc/FormMgrConfig/";
34 }
35 
ParamManager()36 ParamManager::ParamManager()
37 {
38     HILOG_INFO("init");
39 }
40 
~ParamManager()41 ParamManager::~ParamManager()
42 {
43     HILOG_INFO("destory");
44 }
45 
InitParam()46 void ParamManager::InitParam()
47 {
48     HILOG_INFO("call");
49     g_paramStr = LoadParamStr();
50     g_currentVersion = LoadVersion();
51     std::string presetVersion = ParamReader::GetInstance().GetPathVersion(PARAM_PRESET_PATH);
52     long long presetVersionNum;
53     if (!StringUtils::VersionStrToNumber(presetVersion, presetVersionNum)) {
54         HILOG_ERROR("path version error:%{public}s", presetVersion.c_str());
55         return;
56     }
57     std::string pathVersion = ParamReader::GetInstance().GetPathVersion(Constants::FORM_MGR_CONFIG_DIR);
58     long long pathVersionNum;
59     if (!StringUtils::VersionStrToNumber(pathVersion, pathVersionNum)) {
60         HILOG_ERROR("path version error:%{public}s", pathVersion.c_str());
61         return;
62     }
63     HILOG_INFO("presetVersion: %{public}s  pathVersion:%{public}s", presetVersion.c_str(), pathVersion.c_str());
64     std::string path = Constants::FORM_MGR_CONFIG_DIR;
65     if (presetVersionNum > pathVersionNum) {
66         pathVersionNum = presetVersionNum;
67         pathVersion = presetVersion;
68         path = PARAM_PRESET_PATH;
69     }
70     std::string currentVersion = g_currentVersion;
71     long long currentVersionNum;
72     if (!StringUtils::VersionStrToNumber(currentVersion, currentVersionNum)) {
73         HILOG_ERROR("current version error:%{public}s", currentVersion.c_str());
74         return;
75     }
76     HILOG_INFO("currentVersion: %{public}s", currentVersion.c_str());
77     if (currentVersionNum < pathVersionNum) {
78         ReloadParam(pathVersion, path);
79     }
80 }
81 
GetParamStr()82 const std::string &ParamManager::GetParamStr()
83 {
84     return g_paramStr;
85 }
86 
GetParamVersion()87 const std::string &ParamManager::GetParamVersion()
88 {
89     return g_currentVersion;
90 }
91 
ReloadParam(const std::string & versionStr,const std::string path)92 void ParamManager::ReloadParam(const std::string &versionStr, const std::string path)
93 {
94     HILOG_INFO("reloadParam version:%{public}s,path:%{public}s", versionStr.c_str(), path.c_str());
95     if (path.find(PARAM_INSTALL_PATH) != std::string::npos) {
96         if (!ParamReader::GetInstance().VerifyCertSfFile()) {
97             HILOG_ERROR("verify CertSf file error");
98             return;
99         }
100         if (!ParamReader::GetInstance().VerifyParamFile(Constants::VERSION_FILE_NAME)) {
101             HILOG_ERROR("vrify version file error");
102             return;
103         }
104 
105         if (!ParamReader::GetInstance().VerifyParamFile(CONFIG_FILE_NAME)) {
106             HILOG_ERROR("vrify config file error");
107             return;
108         }
109     }
110     g_paramStr = ParamReader::GetInstance().GetParamInfoStr(path + CONFIG_FILE_NAME);
111     g_currentVersion = versionStr;
112     SaveVersionStr(g_currentVersion);
113     SaveParamStr(g_paramStr);
114 }
115 
LoadVersion()116 std::string ParamManager::LoadVersion()
117 {
118     std::string versionStr;
119     ErrCode result = FormRdbDataMgr::GetInstance().QueryData(
120         Constants::FORM_RDB_TABLE_NAME, FORM_MGR_CONFIG_VERSION, versionStr);
121     if (result != ERR_OK) {
122         HILOG_ERROR("get formMgrConfig version error");
123         return Constants::FMC_DEFAULT_VERSION;
124     }
125     return versionStr;
126 }
127 
LoadParamStr()128 std::string ParamManager::LoadParamStr()
129 {
130     std::string paramStr;
131     ErrCode result = FormRdbDataMgr::GetInstance().QueryData(
132         Constants::FORM_RDB_TABLE_NAME, FORM_MGR_CONFIG_DATA, paramStr);
133     if (result != ERR_OK) {
134         HILOG_ERROR("get formMgrConfig param error");
135     }
136     return paramStr;
137 }
138 
SaveVersionStr(const std::string & versionStr)139 void ParamManager::SaveVersionStr(const std::string &versionStr)
140 {
141     ErrCode result = FormRdbDataMgr::GetInstance().InsertData(Constants::FORM_RDB_TABLE_NAME,
142         FORM_MGR_CONFIG_VERSION, versionStr);
143     if (result != ERR_OK) {
144         HILOG_ERROR("update formMgrConfig version to rdbstore failed, code is %{public}d", result);
145     }
146 }
147 
SaveParamStr(const std::string & paramStr)148 void ParamManager::SaveParamStr(const std::string &paramStr)
149 {
150      ErrCode result = FormRdbDataMgr::GetInstance().InsertData(Constants::FORM_RDB_TABLE_NAME,
151         FORM_MGR_CONFIG_DATA, paramStr);
152     if (result != ERR_OK) {
153         HILOG_ERROR("update formMgrConfig param to rdbstore failed, code is %{public}d", result);
154     }
155 }
156 }  // namespace AppExecFwk
157 }  // namespace OHOS