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