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