• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2024 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 "brightness_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_config";
28 } // namespace
29 
30 using namespace OHOS::DisplayPowerMgr;
31 
ParseConfig(BrightnessConfig::Data & data)32 bool BrightnessConfigParser::ParseConfig(BrightnessConfig::Data& data)
33 {
34     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "parse BrightnessConfigParser start!");
35     const std::string fileContent = ConfigParserBase::Get().LoadConfigRoot(0, CONFIG_NAME);
36     if (!ParseConfigJsonRoot(fileContent, data)) {
37         DISPLAY_HILOGI(FEAT_BRIGHTNESS, "parse BrightnessConfigParser error!");
38         return false;
39     }
40     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "parse BrightnessConfigParser over!");
41     return true;
42 }
43 
ParseConfigJsonRoot(const std::string & fileContent,BrightnessConfig::Data & data)44 bool BrightnessConfigParser::ParseConfigJsonRoot(const std::string& fileContent, BrightnessConfig::Data& data)
45 {
46     const cJSON* root = cJSON_Parse(fileContent.c_str());
47     if (!root) {
48         DISPLAY_HILOGE(FEAT_BRIGHTNESS, "Parse file %{public}s failure.", fileContent.c_str());
49         return false;
50     }
51     if (!cJSON_IsObject(root)) {
52         DISPLAY_HILOGE(FEAT_BRIGHTNESS, "root is not an object!");
53         cJSON_Delete(const_cast<cJSON*>(root));
54         return false;
55     }
56 
57     ConfigParserBase::Get().ParseScreenData(root, "displayModeData", data.displayModeMap, "displayMode");
58     ConfigParserBase::Get().ParseScreenData(root, "foldStatus", data.foldStatusModeMap, "foldStatus");
59 
60     cJSON_Delete(const_cast<cJSON*>(root));
61     return true;
62 }
63 
PrintConfig(const BrightnessConfig::Data & data)64 void BrightnessConfigParser::PrintConfig(const BrightnessConfig::Data& data)
65 {
66     std::string text = "";
67     text.append("screenData: ");
68     text.append(ConfigParserBase::Get().ScreenDataToString("displayModeData", data.displayModeMap, "displayMode"));
69     text.append(ConfigParserBase::Get().ScreenDataToString("foldStatus", data.foldStatusModeMap, "foldStatus"));
70     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "%{public}s", text.c_str());
71 }
72 } // namespace DisplayPowerMgr
73 } // namespace OHOS
74