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 "config_data_manager.h"
17 #include <cinttypes>
18 #include "security_guard_log.h"
19 
20 
21 namespace OHOS::Security::SecurityGuard {
GetInstance()22 ConfigDataManager &ConfigDataManager::GetInstance()
23 {
24     static ConfigDataManager instance;
25     return instance;
26 }
27 
InsertModelMap(uint32_t modelId, const ModelCfg &config)28 void ConfigDataManager::InsertModelMap(uint32_t modelId, const ModelCfg &config)
29 {
30     std::lock_guard<std::mutex> lock(modelMutex_);
31     modelMap_[modelId] = config;
32 }
33 
InsertEventMap(int64_t eventId, const EventCfg &config)34 void ConfigDataManager::InsertEventMap(int64_t eventId, const EventCfg &config)
35 {
36     std::lock_guard<std::mutex> lock(eventMutex_);
37     eventMap_[eventId] = config;
38 }
39 
InsertModelToEventMap(uint32_t modelId, std::set<int64_t> eventIds)40 void ConfigDataManager::InsertModelToEventMap(uint32_t modelId, std::set<int64_t> eventIds)
41 {
42     std::lock_guard<std::mutex> lock(modelToEventMutex_);
43     modelToEventMap_[modelId] = eventIds;
44 }
45 
InsertEventToTableMap(int64_t eventId, std::string table)46 void ConfigDataManager::InsertEventToTableMap(int64_t eventId, std::string table)
47 {
48     std::lock_guard<std::mutex> lock(eventToTableMutex_);
49     eventToTableMap_[eventId] = table;
50 }
51 
ResetModelMap()52 void ConfigDataManager::ResetModelMap()
53 {
54     std::lock_guard<std::mutex> lock(modelMutex_);
55     modelMap_.clear();
56 }
57 
ResetEventMap()58 void ConfigDataManager::ResetEventMap()
59 {
60     std::lock_guard<std::mutex> lock(eventMutex_);
61     eventMap_.clear();
62 }
63 
ResetModelToEventMap()64 void ConfigDataManager::ResetModelToEventMap()
65 {
66     std::lock_guard<std::mutex> lock(modelToEventMutex_);
67     modelToEventMap_.clear();
68 }
69 
ResetEventToTableMap()70 void ConfigDataManager::ResetEventToTableMap()
71 {
72     std::lock_guard<std::mutex> lock(eventToTableMutex_);
73     eventToTableMap_.clear();
74 }
75 
GetEventIds(uint32_t modelId)76 std::vector<int64_t> ConfigDataManager::GetEventIds(uint32_t modelId)
77 {
78     SGLOGD("modelId=%{public}u", modelId);
79     std::lock_guard<std::mutex> lock(modelToEventMutex_);
80     std::vector<int64_t> vector;
81     if (modelToEventMap_.find(modelId) != modelToEventMap_.end()) {
82         SGLOGD("map contains modelId=%{public}u", modelId);
83         vector.assign(modelToEventMap_[modelId].begin(), modelToEventMap_[modelId].end());
84     }
85     return vector;
86 }
87 
GetAllEventIds()88 std::vector<int64_t> ConfigDataManager::GetAllEventIds()
89 {
90     std::lock_guard<std::mutex> lock(eventMutex_);
91     std::vector<int64_t> vector;
92     for (const auto &entry : eventMap_) {
93         SGLOGD("eventId=%{public}" PRId64, entry.first);
94         vector.emplace_back(entry.first);
95     }
96     return vector;
97 }
98 
GetAllModelIds()99 std::vector<uint32_t> ConfigDataManager::GetAllModelIds()
100 {
101     std::lock_guard<std::mutex> lock(modelMutex_);
102     std::vector<uint32_t> vector;
103     for (const auto &entry : modelMap_) {
104         SGLOGD("modelId=%{public}u", entry.first);
105         vector.emplace_back(entry.first);
106     }
107     return vector;
108 }
109 
GetAllEventConfigs()110 std::vector<EventCfg> ConfigDataManager::GetAllEventConfigs()
111 {
112     std::lock_guard<std::mutex> lock(eventMutex_);
113     std::vector<EventCfg> vector;
114     for (const auto &entry : eventMap_) {
115         vector.emplace_back(entry.second);
116     }
117     return vector;
118 }
119 
GetModelConfig(uint32_t modelId, ModelCfg &config)120 bool ConfigDataManager::GetModelConfig(uint32_t modelId, ModelCfg &config)
121 {
122     std::lock_guard<std::mutex> lock(modelMutex_);
123     auto it = modelMap_.find(modelId);
124     if (it != modelMap_.end()) {
125         config = it->second;
126         return true;
127     }
128     return false;
129 }
130 
GetEventConfig(int64_t eventId, EventCfg &config)131 bool ConfigDataManager::GetEventConfig(int64_t eventId, EventCfg &config)
132 {
133     std::lock_guard<std::mutex> lock(eventMutex_);
134     auto it = eventMap_.find(eventId);
135     if (it != eventMap_.end()) {
136         config = it->second;
137         return true;
138     }
139     return false;
140 }
141 
GetTableFromEventId(int64_t eventId)142 std::string ConfigDataManager::GetTableFromEventId(int64_t eventId)
143 {
144     std::lock_guard<std::mutex> lock(eventToTableMutex_);
145     if (eventToTableMap_.find(eventId) == eventToTableMap_.end()) {
146         SGLOGE("eventToTableMap_ did not find eventId=%{public}" PRId64, eventId);
147         return "";
148     }
149     return eventToTableMap_[eventId];
150 }
151 } // OHOS::Security::SecurityGuard