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 "collector_cfg_marshalling.h"
17 #include "security_guard_utils.h"
18 #include "json_cfg.h"
19
20 namespace OHOS::Security::SecurityCollector {
21 using nlohmann::json;
22
to_json(json & jsonObj,const ModuleCfgSt & moduleCfg)23 void to_json(json &jsonObj, const ModuleCfgSt &moduleCfg)
24 {
25 std::vector<std::string> eventIds;
26 std::transform(moduleCfg.eventId.begin(), moduleCfg.eventId.end(), std::back_inserter(eventIds),
27 [](int64_t eventId) { return std::to_string(eventId); });
28 jsonObj = json {
29 { MODULE_ID, moduleCfg.moduleId },
30 { EVENT_ID, eventIds },
31 { MODULE_NAME, moduleCfg.moduleName },
32 { MODULE_PATH, moduleCfg.modulePath },
33 { MODULE_VERSION, moduleCfg.version },
34 { MODULE_COLLECTOR_TYPE, moduleCfg.collectorType}
35 };
36 }
37
from_json(const json & jsonObj,ModuleCfgSt & moduleCfg)38 void from_json(const json &jsonObj, ModuleCfgSt &moduleCfg)
39 {
40 std::vector<std::string> eventList;
41 SecurityGuard::JsonCfg::Unmarshal(eventList, jsonObj, EVENT_ID);
42 for (const std::string& eventId : eventList) {
43 int64_t tmp = 0;
44 if (eventId.empty() || !SecurityGuard::SecurityGuardUtils::StrToI64Hex(eventId, tmp)) {
45 continue;
46 }
47 moduleCfg.eventId.emplace_back(tmp);
48 }
49 SecurityGuard::JsonCfg::Unmarshal(moduleCfg.moduleId, jsonObj, MODULE_ID);
50
51 SecurityGuard::JsonCfg::Unmarshal(moduleCfg.moduleName, jsonObj, MODULE_NAME);
52 SecurityGuard::JsonCfg::Unmarshal(moduleCfg.modulePath, jsonObj, MODULE_PATH);
53 SecurityGuard::JsonCfg::Unmarshal(moduleCfg.version, jsonObj, MODULE_VERSION);
54 SecurityGuard::JsonCfg::Unmarshal(moduleCfg.collectorType, jsonObj, MODULE_COLLECTOR_TYPE);
55 }
56 }