• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "calculation_config_parser.h"
17 
18 #include <unistd.h>
19 #include <cJSON.h>
20 
21 #include "config_parser_base.h"
22 #include "display_log.h"
23 
24 namespace OHOS {
25 namespace DisplayPowerMgr {
26 namespace {
27 const std::string CONFIG_NAME = "brightness_curve_config";
28 } // namespace
29 
30 using namespace OHOS::DisplayPowerMgr;
31 
ParseConfig(int displayId,CalculationConfig::Data & data)32 bool CalculationConfigParser::ParseConfig(int displayId, CalculationConfig::Data& data)
33 {
34     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "[%{public}d] parse CalculationConfig start!", displayId);
35     const std::string fileContent = ConfigParserBase::Get().LoadConfigRoot(displayId, CONFIG_NAME);
36     if (!ParseConfigJsonRoot(displayId, fileContent, data)) {
37         DISPLAY_HILOGI(FEAT_BRIGHTNESS, "[%{public}d] parse CalculationConfig error!", displayId);
38         return false;
39     }
40 
41     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "[%{public}d] parse CalculationConfig over!", displayId);
42     return true;
43 }
44 
ParseConfigJsonRoot(int displayId,const std::string & fileContent,CalculationConfig::Data & data)45 bool CalculationConfigParser::ParseConfigJsonRoot(
46     int displayId, const std::string& fileContent, CalculationConfig::Data& data)
47 {
48     const cJSON* root = cJSON_Parse(fileContent.c_str());
49     if (!root) {
50         DISPLAY_HILOGE(FEAT_BRIGHTNESS, "Parse file %{public}s failure.", fileContent.c_str());
51         return false;
52     }
53     if (!cJSON_IsObject(root)) {
54         DISPLAY_HILOGE(FEAT_BRIGHTNESS, "root is not an object!");
55         cJSON_Delete(const_cast<cJSON*>(root));
56         return false;
57     }
58 
59     const cJSON* defaultBrightnessNode = cJSON_GetObjectItemCaseSensitive(root, "defaultBrightness");
60     if (DisplayJsonUtils::IsValidJsonNumber(defaultBrightnessNode)) {
61         data.defaultBrightness = static_cast<float>(defaultBrightnessNode->valuedouble);
62     } else {
63         DISPLAY_HILOGW(FEAT_BRIGHTNESS, "[%{public}d] parse defaultBrightness error!", displayId);
64     }
65 
66     ConfigParserBase::Get().ParsePointXy(root, "defaultPoints", data.defaultPoints);
67 
68     cJSON_Delete(const_cast<cJSON*>(root));
69     return true;
70 }
71 
PrintConfig(int displayId,const CalculationConfig::Data & data)72 void CalculationConfigParser::PrintConfig(int displayId, const CalculationConfig::Data& data)
73 {
74     std::string text = std::to_string(displayId);
75     text.append(" defaultBrightness: ");
76     text.append(std::to_string(data.defaultBrightness)).append(", ");
77     text.append(ConfigParserBase::Get().PointXyToString("defaultPoints", data.defaultPoints));
78     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "%{public}s", text.c_str());
79 }
80 } // namespace DisplayPowerMgr
81 } // namespace OHOS
82