1 /*
2  * Copyright (c) 2021 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 "thermal_kernel_service.h"
17 #include "thermal_common.h"
18 #include "thermal_kernel_config_file.h"
19 #ifdef HAS_THERMAL_CONFIG_POLICY_PART
20 #include "config_policy_utils.h"
21 #endif
22 
23 namespace OHOS {
24 namespace PowerMgr {
25 namespace {
26 constexpr const char* THERMAL_KERNEL_CONFIG_PATH = "etc/thermal_config/thermal_kernel_config.xml";
27 constexpr const char* VENDOR_THERMAL_KERNEL_CONFIG_PATH = "/vendor/etc/thermal_config/thermal_kernel_config.xml";
28 constexpr const char* SYSTEM_THERMAL_KERNEL_CONFIG_PATH = "/system/etc/thermal_config/thermal_kernel_config.xml";
29 }
OnStart()30 void ThermalKernelService::OnStart()
31 {
32     if (!Init()) {
33         THERMAL_HILOGE(FEATURE_PROTECTOR, "failed to init service");
34     }
35 }
36 
Init()37 bool ThermalKernelService::Init()
38 {
39     if (provision_ == nullptr) {
40         provision_ = std::make_shared<ThermalSensorProvision>();
41     }
42 
43     if (control_ == nullptr) {
44         control_ = std::make_shared<ThermalDeviceControl>();
45     }
46 
47     if (policy_ == nullptr) {
48         policy_ = std::make_shared<ThermalKernelPolicy>();
49     }
50 
51     if (timer_ == nullptr) {
52         timer_ = std::make_shared<ThermalProtectorTimer>(provision_);
53     }
54 
55     bool parseConfigSuc = false;
56 #ifdef HAS_THERMAL_CONFIG_POLICY_PART
57     char buf[MAX_PATH_LEN];
58     char* path = GetOneCfgFile(THERMAL_KERNEL_CONFIG_PATH, buf, MAX_PATH_LEN);
59     if (path != nullptr && *path != '\0') {
60         if (!ThermalKernelConfigFile::GetInstance().Init(path)) {
61             THERMAL_HILOGE(FEATURE_PROTECTOR, "failed to parse config");
62             return false;
63         }
64         parseConfigSuc = true;
65     }
66 #endif
67     if (!parseConfigSuc) {
68         if (!ThermalKernelConfigFile::GetInstance().Init(VENDOR_THERMAL_KERNEL_CONFIG_PATH)) {
69             THERMAL_HILOGE(FEATURE_PROTECTOR, "failed to parse vendor config");
70             if (!ThermalKernelConfigFile::GetInstance().Init(SYSTEM_THERMAL_KERNEL_CONFIG_PATH)) {
71                 THERMAL_HILOGE(FEATURE_PROTECTOR, "failed to parse system config");
72                 return false;
73             }
74         }
75     }
76 
77     if (!policy_->Init()) {
78         THERMAL_HILOGE(FEATURE_PROTECTOR, "failed to init policy");
79         return false;
80     }
81 
82     if (!control_->Init()) {
83         THERMAL_HILOGE(FEATURE_PROTECTOR, "failed to init device control");
84         return false;
85     }
86 
87     provision_->InitProvision();
88 
89     timer_->Init();
90     return true;
91 }
92 } // namespace PowerMgr
93 } // namespace OHOS
94