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 "params_config_operator.h"
17 #include "avsession_errors.h"
18 #include "avsession_log.h"
19 #include "file_ex.h"
20 #include "cJSON.h"
21
22 namespace OHOS::AVSession {
23 // LCOV_EXCL_START
ParamsConfigOperator()24 ParamsConfigOperator::ParamsConfigOperator()
25 {
26 SLOGI("construct");
27 }
28 // LCOV_EXCL_STOP
29
~ParamsConfigOperator()30 ParamsConfigOperator::~ParamsConfigOperator()
31 {
32 SLOGI("destroy");
33 }
34
GetInstance()35 ParamsConfigOperator& ParamsConfigOperator::GetInstance()
36 {
37 static ParamsConfigOperator paramsConfigOperator;
38 return paramsConfigOperator;
39 }
40
InitConfig()41 void ParamsConfigOperator::InitConfig()
42 {
43 SLOGI("Init configuration params");
44 std::string content;
45 if (!LoadStringFromFile(avsessionFileDir + PARAMS_FILE_NAME, content)) {
46 SLOGE("LoadStringFromFile failed, filename=%{public}s", PARAMS_FILE_NAME);
47 return;
48 }
49 // LCOV_EXCL_START
50 cJSON* configsArray = cJSON_Parse(content.c_str());
51 CHECK_AND_RETURN_LOG(configsArray != nullptr, "configs is invalid");
52 if (cJSON_IsInvalid(configsArray) || !cJSON_IsArray(configsArray)) {
53 SLOGE("CheckBundleSupport parse profile not valid json");
54 cJSON_Delete(configsArray);
55 return;
56 }
57 SLOGD("InitConfig::parse json object finished");
58
59 cJSON* configItem = nullptr;
60 cJSON_ArrayForEach(configItem, configsArray) {
61 if (configItem == nullptr || cJSON_IsInvalid(configItem)) {
62 SLOGE("get config item null");
63 continue;
64 }
65 cJSON* propItem = configItem->child;
66 while (propItem != nullptr) {
67 std::string keyStr = propItem->string;
68 cJSON* valueItem = cJSON_GetObjectItem(propItem, propItem->string);
69 if (valueItem == nullptr) {
70 propItem = propItem->next;
71 continue;
72 }
73 if (cJSON_IsNumber(valueItem)) {
74 configIntParams.insert(std::pair<std::string, int32_t>(keyStr, valueItem->valueint));
75 } else if (cJSON_IsString(valueItem)) {
76 configStringParams.insert(std::pair<std::string, std::string>(keyStr,
77 std::string(valueItem->valuestring)));
78 }
79 propItem = propItem->next;
80 }
81 }
82 cJSON_Delete(configsArray);
83 // LCOV_EXCL_STOP
84 }
85
GetValueIntByKey(const std::string & key,int32_t * value)86 int32_t ParamsConfigOperator::GetValueIntByKey(const std::string& key, int32_t *value)
87 {
88 auto param = configIntParams.find(key);
89 if (param == configIntParams.end()) {
90 SLOGE("GetValueIntByKey failed, key=%{public}s", key.c_str());
91 return AVSESSION_ERROR;
92 }
93 *value = static_cast<int>(param->second);
94 return AVSESSION_SUCCESS;
95 }
96 }