1 /*
2 * Copyright (c) 2023 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 "model_config.h"
17
18 #include "file_ex.h"
19
20 #include "config_data_manager.h"
21 #include "json_cfg.h"
22 #include "model_analysis_define.h"
23 #include "model_cfg_marshalling.h"
24 #include "security_guard_log.h"
25 #include "security_guard_utils.h"
26 #include "config_define.h"
27
28 namespace OHOS::Security::SecurityGuard {
Load(int mode)29 bool ModelConfig::Load(int mode)
30 {
31 std::string path;
32 if (mode == INIT_MODE) {
33 if (FileExists(CONFIG_UPTATE_FILES[MODEL_CFG_INDEX])) {
34 path = CONFIG_UPTATE_FILES[MODEL_CFG_INDEX];
35 } else if (FileExists(CONFIG_PRESET_FILES[MODEL_CFG_INDEX])) {
36 path = CONFIG_PRESET_FILES[MODEL_CFG_INDEX];
37 }
38 } else if (mode == UPDATE_MODE) {
39 if (FileExists(CONFIG_CACHE_FILES[MODEL_CFG_INDEX])) {
40 path = CONFIG_CACHE_FILES[MODEL_CFG_INDEX];
41 }
42 }
43 SGLOGD("path=%{public}s", path.c_str());
44 if (path.empty()) {
45 SGLOGE("path is empty");
46 return false;
47 }
48 stream_ = std::ifstream(path, std::ios::in);
49 if (!stream_.is_open()) {
50 SGLOGE("stream error, %{public}s", strerror(errno));
51 return false;
52 }
53 return true;
54 }
55
Parse()56 bool ModelConfig::Parse()
57 {
58 if (!stream_.is_open()) {
59 SGLOGE("stream error");
60 return false;
61 }
62 nlohmann::json jsonObj = nlohmann::json::parse(stream_, nullptr, false);
63 stream_.close();
64
65 if (jsonObj.is_discarded()) {
66 SGLOGI("json is discarded");
67 return false;
68 }
69
70 std::vector<ModelCfg> configs;
71 bool success = ParseModelConfig(configs, jsonObj);
72 if (!success) {
73 SGLOGE("parse ModelConfig error");
74 return false;
75 }
76 CacheModelConfig(configs);
77 CacheModelToEvent(configs);
78 SGLOGI("cache ModelConfig success");
79 return true;
80 }
81
Update()82 bool ModelConfig::Update()
83 {
84 if (!stream_.is_open()) {
85 SGLOGE("stream error");
86 return false;
87 }
88 nlohmann::json jsonObj = nlohmann::json::parse(stream_, nullptr, false);
89 stream_.close();
90
91 if (jsonObj.is_discarded()) {
92 SGLOGI("json is discarded");
93 return false;
94 }
95
96 std::vector<ModelCfg> configs;
97 bool success = ParseModelConfig(configs, jsonObj);
98 if (!success) {
99 SGLOGE("parse EventConfig error");
100 return false;
101 }
102
103 if (!SecurityGuardUtils::CopyFile(CONFIG_CACHE_FILES[MODEL_CFG_INDEX], CONFIG_UPTATE_FILES[MODEL_CFG_INDEX])) {
104 SGLOGE("copyFile error");
105 return false;
106 }
107 ConfigDataManager::GetInstance().ResetModelMap();
108 CacheModelConfig(configs);
109 CacheModelToEvent(configs);
110 SGLOGI("cache ModelConfig success");
111 return true;
112 }
113
ParseModelConfig(std::vector<ModelCfg> & configs,nlohmann::json & jsonObj)114 bool ModelConfig::ParseModelConfig(std::vector<ModelCfg> &configs, nlohmann::json &jsonObj)
115 {
116 return JsonCfg::Unmarshal<ModelCfg>(configs, jsonObj, MODEL_CFG_KEY);
117 };
118
CacheModelConfig(const std::vector<ModelCfg> & configs)119 void ModelConfig::CacheModelConfig(const std::vector<ModelCfg> &configs)
120 {
121 for (const ModelCfg &config : configs) {
122 SGLOGD("modelId=%{public}u", config.modelId);
123 ConfigDataManager::GetInstance().InsertModelMap(config.modelId, config);
124 }
125 }
126
CacheModelToEvent(const std::vector<ModelCfg> & configs)127 void ModelConfig::CacheModelToEvent(const std::vector<ModelCfg> &configs)
128 {
129 for (const ModelCfg &config : configs) {
130 SGLOGD("modelId=%{public}u", config.modelId);
131 std::set<int64_t> set;
132 for (int64_t event : config.eventList) {
133 set.emplace(event);
134 }
135 ConfigDataManager::GetInstance().InsertModelToEventMap(config.modelId, set);
136 }
137 }
138 } // OHOS::Security::SecurityGuard