1 /*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "json_parser.h"
17 #include <errors.h>
18 #include <fstream>
19 #include <memory>
20 #include <unistd.h>
21 #include "common/common_macro.h"
22 #include "media_log.h"
23
24 namespace OHOS {
25 namespace Sharing {
GetConfig(SharingData::Ptr & value)26 int32_t JsonParser::GetConfig(SharingData::Ptr &value)
27 {
28 SHARING_LOGD("trace.");
29 Json::Value root;
30 std::ifstream ifs;
31 ifs.open("/etc/sharing_config.json");
32
33 if (!ifs.is_open()) {
34 SHARING_LOGE("cannot open configuration file.");
35 return -1;
36 }
37
38 Json::CharReaderBuilder builder;
39 builder["collectComments"] = false;
40 JSONCPP_STRING errs;
41
42 if (!parseFromStream(builder, ifs, &root, &errs)) {
43 SHARING_LOGE("parse file error: %{public}s.", errs.c_str());
44 ifs.close();
45 return -1;
46 }
47
48 SHARING_LOGD("parse json:\n%{public}s.", root.toStyledString().c_str());
49 for (auto &modules : root) {
50 ReadModuleConfig(modules, value);
51 }
52
53 ifs.close();
54 return 0;
55 }
56
SaveConfig(SharingData::Ptr & value)57 int32_t JsonParser::SaveConfig(SharingData::Ptr &value)
58 {
59 SHARING_LOGD("trace.");
60 RETURN_INVALID_IF_NULL(value);
61 std::ofstream ofs;
62 ofs.open("/data/sharing_config.json", std::ios::out | std::ios::trunc | std::ios::binary);
63
64 if (!ofs.is_open()) {
65 SHARING_LOGE("open configuration file err.");
66 return -1;
67 }
68
69 Json::Value root(Json::ValueType::objectValue);
70 Json::StreamWriterBuilder builder;
71 builder["commentStyle"] = "All";
72 Json::StreamWriter *writer(builder.newStreamWriter());
73
74 value->ForEach([&](const std::string &moduleName, const SharingDataGroupByModule::Ptr &moduleValue) {
75 Json::Value moduleArray(Json::ValueType::arrayValue);
76 SaveModuleConfig(moduleArray, moduleValue);
77 root[moduleName] = moduleArray;
78 });
79
80 MEDIA_LOGD("save json:\n%{public}s.", root.toStyledString().c_str());
81 writer->write(root, &ofs);
82 ofs.close();
83 return 0;
84 }
85
ReadModuleConfig(Json::Value & modules,SharingData::Ptr & value)86 int32_t JsonParser::ReadModuleConfig(Json::Value &modules, SharingData::Ptr &value)
87 {
88 if (modules.empty() || !modules.isObject()) {
89 SHARING_LOGE("this module invalid");
90 return -1;
91 }
92
93 auto moduleNames = modules.getMemberNames();
94 for (auto &moduleName : moduleNames) {
95 auto moduleValue = std::make_shared<SharingDataGroupByModule>(moduleName);
96 auto module = modules[moduleName];
97 for (auto &item : module) {
98 if (!item["tag"].isString()) {
99 continue;
100 }
101 auto tag = item["tag"].asString();
102 auto tagValue = std::make_shared<SharingDataGroupByTag>(tag);
103 moduleValue->PutSharingValues(tag, tagValue);
104 auto keyNames = item.getMemberNames();
105 for (auto &key : keyNames) {
106 if (key == "tag") {
107 continue;
108 }
109 if (key == "isEnable" && tag == "mediaLog" && item[key].isBool()) {
110 g_logOn = item[key].asBool();
111 }
112 SharingValue::Ptr sharingData = nullptr;
113 if (item[key].isString()) {
114 std::string value = item[key].asString();
115 sharingData = std::make_shared<SharingValue>(value);
116 } else if (item[key].isInt()) {
117 int32_t value = item[key].asInt();
118 sharingData = std::make_shared<SharingValue>(value);
119 } else if (item[key].isBool()) {
120 bool value = item[key].asBool();
121 sharingData = std::make_shared<SharingValue>(value);
122 } else if (item[key].isArray()) {
123 std::vector<int32_t> value;
124 for (auto &it : item[key]) {
125 value.emplace_back(it.asInt());
126 }
127 sharingData = std::make_shared<SharingValue>(value);
128 }
129 tagValue->PutSharingValue(key, sharingData);
130 }
131 }
132 value->PutSharingValues(moduleValue, moduleName);
133 }
134
135 return 0;
136 }
137
SaveModuleConfig(Json::Value & module,const SharingDataGroupByModule::Ptr & moduleValue)138 int32_t JsonParser::SaveModuleConfig(Json::Value &module, const SharingDataGroupByModule::Ptr &moduleValue)
139 {
140 SHARING_LOGD("trace.");
141 RETURN_INVALID_IF_NULL(moduleValue);
142 moduleValue->ForEach([&](const std::string &tagName, const SharingDataGroupByTag::Ptr &tagValue) {
143 Json::Value tagObject(Json::ValueType::objectValue);
144 tagObject["tag"] = tagName;
145 tagValue->ForEach([&](const std::string &key, const SharingValue::Ptr &value) {
146 if (value->IsBool()) {
147 bool data;
148 if (value->GetValue(data)) {
149 tagObject[key] = data;
150 }
151 } else if (value->IsInt32()) {
152 int32_t data;
153 if (value->GetValue(data)) {
154 tagObject[key] = data;
155 }
156 } else if (value->IsString()) {
157 std::string data;
158 if (value->GetValue(data)) {
159 tagObject[key] = data;
160 }
161 } else if (value->IsVector()) {
162 std::vector<int32_t> data;
163 if (value->GetValue(data)) {
164 Json::Value vec(Json::ValueType::arrayValue);
165 for (auto &item : data) {
166 vec.append(item);
167 }
168 tagObject[key] = vec;
169 }
170 }
171 });
172 module.append(tagObject);
173 });
174
175 return 0;
176 }
177 } // namespace Sharing
178 } // namespace OHOS