• 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 "lux_threshold_config_parser.h"
17 
18 #include <unistd.h>
19 
20 #include "config_parser_base.h"
21 #include "display_log.h"
22 
23 namespace OHOS {
24 namespace DisplayPowerMgr {
25 namespace {
26 const std::string CONFIG_NAME = "brightness_lux_threshold_config";
SetDefault(LuxThresholdConfig::Data & data)27 void SetDefault(LuxThresholdConfig::Data& data)
28 {
29     // default value
30     LuxThresholdConfig::Mode config{
31         1200, 1500,
32         {
33             { 0.0f, 5.0f },       { 3.0f, 5.0f },       { 5.0f, 10.0f },        { 10.0f, 20.0f },
34             { 20.0f, 50.0f },     { 30.0f, 100.0f },    { 100.0f, 300.0f },     { 500.0f, 500.0f },
35             { 1000.0f, 1000.0f }, { 2000.0f, 2000.0f }, { 10000.0f, 10000.0f }, { 70000.0f, 10000.0f }
36         },
37         {
38             { 0.0f, 1.0f },        { 1.0f, 1.0f },      { 3.0f, 2.0f },       { 10.0f, 7.0f },
39             { 20.0f, 15.0f },      { 50.0f, 35.0f },    { 100.0f, 80.0f },    { 200.0f, 170.0f },
40             { 500.0f, 350.0f },    { 1000.0f, 600.0f }, { 2000.0f, 1200.0f }, { 10000.0f, 8000.0f },
41             { 70000.0f, 60000.0f }
42         }
43     };
44     data.modeArray.insert(std::make_pair("DefaultMode", config));
45     data.isLevelEnable = true;
46     data.brightenPointsForLevel = {
47         { 4.0f,   1.0f },
48         { 10.0f,  5.0f },
49         { 20.0f,  10.0f },
50         { 60.0f,  30.0f },
51         { 100.0f, 100.0f },
52         { 225.0f, 200.0f },
53         { 255.0f, 300.0f }
54     };
55     data.darkenPointsForLevel = {
56         { 4.0f,   1.0f },
57         { 10.0f,  5.0f },
58         { 20.0f,  5.0f },
59         { 60.0f,  15.0f },
60         { 100.0f, 50.0f },
61         { 225.0f, 50.0f },
62         { 255.0f, 100.0f }
63     };
64 }
65 } // namespace
66 
67 using namespace OHOS::DisplayPowerMgr;
68 
LuxThresholdParseConfigParams(cJSON * item,LuxThresholdConfig::Data & data)69 void LuxThresholdConfigParser::LuxThresholdParseConfigParams(cJSON* item, LuxThresholdConfig::Data& data)
70 {
71     LuxThresholdConfig::Mode config{};
72     std::string name;
73 
74     const cJSON* modeNameNode = cJSON_GetObjectItemCaseSensitive(item, "modeName");
75     if (DisplayJsonUtils::IsValidJsonStringAndNoEmpty(modeNameNode)) {
76         name = modeNameNode->valuestring;
77     } else {
78         DISPLAY_HILOGW(FEAT_BRIGHTNESS, "<%{public}s> modeName is not found!", CONFIG_NAME.c_str());
79         return;
80     }
81 
82     const cJSON* brightenDebounceTimeNode = cJSON_GetObjectItemCaseSensitive(item, "brightenDebounceTime");
83     if (DisplayJsonUtils::IsValidJsonNumber(brightenDebounceTimeNode)) {
84         config.brightenDebounceTime = brightenDebounceTimeNode->valueint;
85     }
86 
87     const cJSON* darkenDebounceTimeNode = cJSON_GetObjectItemCaseSensitive(item, "darkenDebounceTime");
88     if (DisplayJsonUtils::IsValidJsonNumber(darkenDebounceTimeNode)) {
89         config.darkenDebounceTime = darkenDebounceTimeNode->valueint;
90     }
91 
92     ConfigParserBase::Get().ParsePointXy(item, "brightenPoints", config.brightenPoints);
93     ConfigParserBase::Get().ParsePointXy(item, "darkenPoints", config.darkenPoints);
94 
95     data.modeArray.insert(std::make_pair(name, config));
96     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "<%{public}s> is insert!", name.c_str());
97 }
98 
ParseConfig(int displayId,LuxThresholdConfig::Data & data)99 bool LuxThresholdConfigParser::ParseConfig(int displayId, LuxThresholdConfig::Data& data)
100 {
101     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "[%{public}d] parse LuxThresholdConfigParser start!", displayId);
102     const std::string fileContent = ConfigParserBase::Get().LoadConfigRoot(displayId, CONFIG_NAME);
103     if (!ParseConfigJsonRoot(fileContent, data)) {
104         DISPLAY_HILOGI(FEAT_BRIGHTNESS, "[%{public}d] parse LuxThresholdConfigParser error!", displayId);
105         return false;
106     }
107 
108     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "[%{public}d] parse LuxThresholdConfigParser over!", displayId);
109     return true;
110 }
111 
ParseConfigJsonRoot(const std::string & fileContent,LuxThresholdConfig::Data & data)112 bool LuxThresholdConfigParser::ParseConfigJsonRoot(const std::string& fileContent, LuxThresholdConfig::Data& data)
113 {
114     const cJSON* root = cJSON_Parse(fileContent.c_str());
115     if (!root) {
116         SetDefault(data);
117         DISPLAY_HILOGE(FEAT_BRIGHTNESS, "Parse file %{public}s failure.", fileContent.c_str());
118         return false;
119     }
120     if (!cJSON_IsObject(root)) {
121         DISPLAY_HILOGI(FEAT_BRIGHTNESS, "root is not an object!");
122         cJSON_Delete(const_cast<cJSON*>(root));
123         return false;
124     }
125     const cJSON* isLevelEnableNode = cJSON_GetObjectItemCaseSensitive(root, "isLevelEnable");
126     if (DisplayJsonUtils::IsValidJsonBool(isLevelEnableNode)) {
127         data.isLevelEnable = cJSON_IsTrue(isLevelEnableNode);
128     }
129 
130     ConfigParserBase::Get().ParsePointXy(
131         root, "brightenPointsForLevel", data.brightenPointsForLevel);
132     ConfigParserBase::Get().ParsePointXy(
133         root, "darkenPointsForLevel", data.darkenPointsForLevel);
134 
135     const cJSON* thresholdModeArray = cJSON_GetObjectItemCaseSensitive(root, "thresholdMode");
136     if (!DisplayJsonUtils::IsValidJsonArray(thresholdModeArray)) {
137         DISPLAY_HILOGW(FEAT_BRIGHTNESS, "root <%{public}s> is not Array!", CONFIG_NAME.c_str());
138         cJSON_Delete(const_cast<cJSON*>(root));
139         return false;
140     }
141 
142     cJSON* item = nullptr;
143     cJSON_ArrayForEach(item, thresholdModeArray) {
144         if (!cJSON_IsObject(item)) {
145             continue;
146         }
147         LuxThresholdParseConfigParams(item, data);
148     }
149     cJSON_Delete(const_cast<cJSON*>(root));
150     return true;
151 }
152 
PrintConfig(int displayId,const LuxThresholdConfig::Data & data)153 void LuxThresholdConfigParser::PrintConfig(
154     int displayId, const LuxThresholdConfig::Data& data)
155 {
156     std::string text = std::to_string(displayId).append(" ");
157     for (auto [key, value] : data.modeArray) {
158         text.append("[modeName: ");
159         text.append(key).append(", ");
160         text.append("brightenDebounceTime: ");
161         text.append(std::to_string(value.brightenDebounceTime)).append(", ");
162         text.append("darkenDebounceTime: ");
163         text.append(std::to_string(value.darkenDebounceTime)).append(", ");
164         text.append(ConfigParserBase::Get().PointXyToString("brightenPoints", value.brightenPoints));
165         text.append(ConfigParserBase::Get().PointXyToString("darkenPoints", value.brightenPoints));
166         text.append("over]");
167     }
168     text.append(" isLevelEnable: ");
169     text.append(data.isLevelEnable ? "true" : "false").append(", ");
170     text.append(ConfigParserBase::Get().PointXyToString(
171         "brightenPointsForLevel", data.brightenPointsForLevel));
172     text.append(ConfigParserBase::Get().PointXyToString("darkenPointsForLevel", data.brightenPointsForLevel));
173     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "%{public}s", text.c_str());
174 }
175 } // namespace DisplayPowerMgr
176 } // namespace OHOS
177