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 <json/json.h>
19 #include <unistd.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_lux_threshold_config";
SetDefault(LuxThresholdConfig::Data & data)28 void SetDefault(LuxThresholdConfig::Data& data)
29 {
30     // default value
31     LuxThresholdConfig::Mode config{
32         1200, 1500,
33         {
34             { 0.0f, 5.0f },       { 3.0f, 5.0f },       { 5.0f, 10.0f },        { 10.0f, 20.0f },
35             { 20.0f, 50.0f },     { 30.0f, 100.0f },    { 100.0f, 300.0f },     { 500.0f, 500.0f },
36             { 1000.0f, 1000.0f }, { 2000.0f, 2000.0f }, { 10000.0f, 10000.0f }, { 70000.0f, 10000.0f }
37         },
38         {
39             { 0.0f, 1.0f },        { 1.0f, 1.0f },      { 3.0f, 2.0f },       { 10.0f, 7.0f },
40             { 20.0f, 15.0f },      { 50.0f, 35.0f },    { 100.0f, 80.0f },    { 200.0f, 170.0f },
41             { 500.0f, 350.0f },    { 1000.0f, 600.0f }, { 2000.0f, 1200.0f }, { 10000.0f, 8000.0f },
42             { 70000.0f, 60000.0f }
43         }
44     };
45     data.modeArray.insert(std::make_pair("DefaultMode", config));
46     data.isLevelEnable = true;
47     data.brightenPointsForLevel = {
48         { 4.0f,   1.0f },
49         { 10.0f,  5.0f },
50         { 20.0f,  10.0f },
51         { 60.0f,  30.0f },
52         { 100.0f, 100.0f },
53         { 225.0f, 200.0f },
54         { 255.0f, 300.0f }
55     };
56     data.darkenPointsForLevel = {
57         { 4.0f,   1.0f },
58         { 10.0f,  5.0f },
59         { 20.0f,  5.0f },
60         { 60.0f,  15.0f },
61         { 100.0f, 50.0f },
62         { 225.0f, 50.0f },
63         { 255.0f, 100.0f }
64     };
65 }
66 } // namespace
67 
68 using namespace OHOS::DisplayPowerMgr;
69 
ParseConfig(int displayId,LuxThresholdConfig::Data & data)70 bool LuxThresholdConfigParser::ParseConfig(int displayId, LuxThresholdConfig::Data& data)
71 {
72     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "[%{public}d] parse LuxThresholdConfigParser start!", displayId);
73     const Json::Value root = ConfigParserBase::Get().LoadConfigRoot(displayId, CONFIG_NAME);
74     if (root.isNull()) {
75         SetDefault(data);
76         return false;
77     }
78     if (root["isLevelEnable"].isBool()) {
79         data.isLevelEnable = root["isLevelEnable"].asBool();
80     }
81     ConfigParserBase::Get().ParsePointXy(
82         root, "brightenPointsForLevel", data.brightenPointsForLevel);
83     ConfigParserBase::Get().ParsePointXy(
84         root, "darkenPointsForLevel", data.darkenPointsForLevel);
85     if (!root["thresholdMode"].isArray()) {
86         DISPLAY_HILOGW(FEAT_BRIGHTNESS, "root <%{public}s> is not Array!", CONFIG_NAME.c_str());
87         return false;
88     }
89     for (auto value : root["thresholdMode"]) {
90         LuxThresholdConfig::Mode config{};
91         if (!value["modeName"].isString() || value["modeName"].asString().empty()) {
92             DISPLAY_HILOGW(FEAT_BRIGHTNESS, "<%{public}s> modeName is not find!", CONFIG_NAME.c_str());
93             continue;
94         }
95         std::string name = value["modeName"].asString();
96         if (value["brightenDebounceTime"].isInt()) {
97             config.brightenDebounceTime = value["brightenDebounceTime"].asInt();
98         }
99         if (value["darkenDebounceTime"].isInt()) {
100             config.darkenDebounceTime = value["darkenDebounceTime"].asInt();
101         }
102         ConfigParserBase::Get().ParsePointXy(value, "brightenPoints", config.brightenPoints);
103         ConfigParserBase::Get().ParsePointXy(value, "darkenPoints", config.darkenPoints);
104         data.modeArray.insert(std::make_pair(name, config));
105         DISPLAY_HILOGI(FEAT_BRIGHTNESS, "<%{public}s> is insert!", name.c_str());
106     }
107     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "[%{public}d] parse LuxThresholdConfigParser over!", displayId);
108     return true;
109 }
110 
PrintConfig(int displayId,const LuxThresholdConfig::Data & data)111 void LuxThresholdConfigParser::PrintConfig(
112     int displayId, const LuxThresholdConfig::Data& data)
113 {
114     std::string text = std::to_string(displayId).append(" ");
115     for (auto [key, value] : data.modeArray) {
116         text.append("[modeName: ");
117         text.append(key).append(", ");
118         text.append("brightenDebounceTime: ");
119         text.append(std::to_string(value.brightenDebounceTime)).append(", ");
120         text.append("darkenDebounceTime: ");
121         text.append(std::to_string(value.darkenDebounceTime)).append(", ");
122         text.append(ConfigParserBase::Get().PointXyToString("brightenPoints", value.brightenPoints));
123         text.append(ConfigParserBase::Get().PointXyToString("darkenPoints", value.brightenPoints));
124         text.append("over]");
125     }
126     text.append(" isLevelEnable: ");
127     text.append(data.isLevelEnable ? "true" : "false").append(", ");
128     text.append(ConfigParserBase::Get().PointXyToString(
129         "brightenPointsForLevel", data.brightenPointsForLevel));
130     text.append(ConfigParserBase::Get().PointXyToString("darkenPointsForLevel", data.brightenPointsForLevel));
131     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "%{public}s", text.c_str());
132 }
133 } // namespace DisplayPowerMgr
134 } // namespace OHOS
135