137a09cd7Sopenharmony_ci/*
237a09cd7Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
337a09cd7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
437a09cd7Sopenharmony_ci * you may not use this file except in compliance with the License.
537a09cd7Sopenharmony_ci * You may obtain a copy of the License at
637a09cd7Sopenharmony_ci *
737a09cd7Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
837a09cd7Sopenharmony_ci *
937a09cd7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1037a09cd7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1137a09cd7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1237a09cd7Sopenharmony_ci * See the License for the specific language governing permissions and
1337a09cd7Sopenharmony_ci * limitations under the License.
1437a09cd7Sopenharmony_ci */
1537a09cd7Sopenharmony_ci
1637a09cd7Sopenharmony_ci#include "thermal_service.h"
1737a09cd7Sopenharmony_ci
1837a09cd7Sopenharmony_ci#include "file_ex.h"
1937a09cd7Sopenharmony_ci#include "if_system_ability_manager.h"
2037a09cd7Sopenharmony_ci#include "iservice_registry.h"
2137a09cd7Sopenharmony_ci#include "securec.h"
2237a09cd7Sopenharmony_ci#include "system_ability_definition.h"
2337a09cd7Sopenharmony_ci#include <algorithm>
2437a09cd7Sopenharmony_ci#include <fcntl.h>
2537a09cd7Sopenharmony_ci#include <ipc_skeleton.h>
2637a09cd7Sopenharmony_ci#include <thread>
2737a09cd7Sopenharmony_ci#include <unistd.h>
2837a09cd7Sopenharmony_ci
2937a09cd7Sopenharmony_ci#ifdef HAS_THERMAL_CONFIG_POLICY_PART
3037a09cd7Sopenharmony_ci#include "config_policy_utils.h"
3137a09cd7Sopenharmony_ci#endif
3237a09cd7Sopenharmony_ci#include "constants.h"
3337a09cd7Sopenharmony_ci#include "ffrt_utils.h"
3437a09cd7Sopenharmony_ci#include "permission.h"
3537a09cd7Sopenharmony_ci#include "sysparam.h"
3637a09cd7Sopenharmony_ci#include "thermal_common.h"
3737a09cd7Sopenharmony_ci#include "thermal_mgr_dumper.h"
3837a09cd7Sopenharmony_ci#include "xcollie/watchdog.h"
3937a09cd7Sopenharmony_ci
4037a09cd7Sopenharmony_cinamespace OHOS {
4137a09cd7Sopenharmony_cinamespace PowerMgr {
4237a09cd7Sopenharmony_cisptr<ThermalService> ThermalService::instance_ = nullptr;
4337a09cd7Sopenharmony_cistd::mutex ThermalService::singletonMutex_;
4437a09cd7Sopenharmony_cinamespace {
4537a09cd7Sopenharmony_ciconst std::string THERMAL_SERVICE_CONFIG_PATH = "etc/thermal_config/thermal_service_config.xml";
4637a09cd7Sopenharmony_ciconst std::string VENDOR_THERMAL_SERVICE_CONFIG_PATH = "/vendor/etc/thermal_config/thermal_service_config.xml";
4737a09cd7Sopenharmony_ciconst std::string SYSTEM_THERMAL_SERVICE_CONFIG_PATH = "/system/etc/thermal_config/thermal_service_config.xml";
4837a09cd7Sopenharmony_ciconstexpr const char* HDI_SERVICE_NAME = "thermal_interface_service";
4937a09cd7Sopenharmony_ciFFRTQueue g_queue("thermal_service");
5037a09cd7Sopenharmony_ciconstexpr uint32_t RETRY_TIME = 1000;
5137a09cd7Sopenharmony_ciauto g_service = ThermalService::GetInstance();
5237a09cd7Sopenharmony_ciconst bool G_REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(g_service.GetRefPtr());
5337a09cd7Sopenharmony_ciSysParam::BootCompletedCallback g_bootCompletedCallback;
5437a09cd7Sopenharmony_ci} // namespace
5537a09cd7Sopenharmony_cistd::atomic_bool ThermalService::isBootCompleted_ = false;
5637a09cd7Sopenharmony_cistd::string ThermalService::scene_;
5737a09cd7Sopenharmony_ci#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART
5837a09cd7Sopenharmony_cibool ThermalService::userAirplaneState_ = false;
5937a09cd7Sopenharmony_cibool ThermalService::isThermalAirplane_ = false;
6037a09cd7Sopenharmony_ci#endif
6137a09cd7Sopenharmony_ciThermalService::ThermalService() : SystemAbility(POWER_MANAGER_THERMAL_SERVICE_ID, true) {}
6237a09cd7Sopenharmony_ci
6337a09cd7Sopenharmony_ciThermalService::~ThermalService() {}
6437a09cd7Sopenharmony_ci
6537a09cd7Sopenharmony_cisptr<ThermalService> ThermalService::GetInstance()
6637a09cd7Sopenharmony_ci{
6737a09cd7Sopenharmony_ci    if (instance_ == nullptr) {
6837a09cd7Sopenharmony_ci        std::lock_guard<std::mutex> lock(singletonMutex_);
6937a09cd7Sopenharmony_ci        if (instance_ == nullptr) {
7037a09cd7Sopenharmony_ci            instance_ = new ThermalService();
7137a09cd7Sopenharmony_ci        }
7237a09cd7Sopenharmony_ci    }
7337a09cd7Sopenharmony_ci    return instance_;
7437a09cd7Sopenharmony_ci}
7537a09cd7Sopenharmony_ci
7637a09cd7Sopenharmony_civoid ThermalService::OnStart()
7737a09cd7Sopenharmony_ci{
7837a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
7937a09cd7Sopenharmony_ci    if (ready_) {
8037a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "OnStart is ready, nothing to do");
8137a09cd7Sopenharmony_ci        return;
8237a09cd7Sopenharmony_ci    }
8337a09cd7Sopenharmony_ci
8437a09cd7Sopenharmony_ci    if (!(Init())) {
8537a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "OnStart call init fail");
8637a09cd7Sopenharmony_ci        return;
8737a09cd7Sopenharmony_ci    }
8837a09cd7Sopenharmony_ci
8937a09cd7Sopenharmony_ci    AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
9037a09cd7Sopenharmony_ci    if (!Publish(ThermalService::GetInstance())) {
9137a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "OnStart register to system ability manager failed.");
9237a09cd7Sopenharmony_ci        return;
9337a09cd7Sopenharmony_ci    }
9437a09cd7Sopenharmony_ci    RegisterBootCompletedCallback();
9537a09cd7Sopenharmony_ci    ready_ = true;
9637a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "OnStart and add system ability success");
9737a09cd7Sopenharmony_ci}
9837a09cd7Sopenharmony_ci
9937a09cd7Sopenharmony_civoid ThermalService::RegisterBootCompletedCallback()
10037a09cd7Sopenharmony_ci{
10137a09cd7Sopenharmony_ci    g_bootCompletedCallback = []() {
10237a09cd7Sopenharmony_ci        isBootCompleted_ = true;
10337a09cd7Sopenharmony_ci    };
10437a09cd7Sopenharmony_ci    SysParam::RegisterBootCompletedCallback(g_bootCompletedCallback);
10537a09cd7Sopenharmony_ci}
10637a09cd7Sopenharmony_ci
10737a09cd7Sopenharmony_civoid ThermalService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
10837a09cd7Sopenharmony_ci{
10937a09cd7Sopenharmony_ci    THERMAL_HILOGI(COMP_SVC, "systemAbilityId=%{public}d, deviceId=%{private}s", systemAbilityId, deviceId.c_str());
11037a09cd7Sopenharmony_ci    if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
11137a09cd7Sopenharmony_ci        InitStateMachine();
11237a09cd7Sopenharmony_ci#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART
11337a09cd7Sopenharmony_ci        SubscribeCommonEvent();
11437a09cd7Sopenharmony_ci#endif
11537a09cd7Sopenharmony_ci    }
11637a09cd7Sopenharmony_ci}
11737a09cd7Sopenharmony_ci
11837a09cd7Sopenharmony_ci#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART
11937a09cd7Sopenharmony_cibool ThermalService::SubscribeCommonEvent()
12037a09cd7Sopenharmony_ci{
12137a09cd7Sopenharmony_ci    using namespace OHOS::EventFwk;
12237a09cd7Sopenharmony_ci    bool result = false;
12337a09cd7Sopenharmony_ci    MatchingSkills matchingSkills;
12437a09cd7Sopenharmony_ci    matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_AIRPLANE_MODE_CHANGED);
12537a09cd7Sopenharmony_ci    CommonEventSubscribeInfo subscribeInfo(matchingSkills);
12637a09cd7Sopenharmony_ci    subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::COMMON);
12737a09cd7Sopenharmony_ci    if (!subscriberPtr_) {
12837a09cd7Sopenharmony_ci        subscriberPtr_ = std::make_shared<AirplaneCommonEventSubscriber>(subscribeInfo);
12937a09cd7Sopenharmony_ci    }
13037a09cd7Sopenharmony_ci    result = CommonEventManager::SubscribeCommonEvent(subscriberPtr_);
13137a09cd7Sopenharmony_ci    if (!result) {
13237a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "Subscribe CommonEvent failed");
13337a09cd7Sopenharmony_ci    } else {
13437a09cd7Sopenharmony_ci        THERMAL_HILOGD(COMP_SVC, "Subscribe CommonEvent success");
13537a09cd7Sopenharmony_ci    }
13637a09cd7Sopenharmony_ci    return result;
13737a09cd7Sopenharmony_ci}
13837a09cd7Sopenharmony_ci
13937a09cd7Sopenharmony_civoid AirplaneCommonEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data)
14037a09cd7Sopenharmony_ci{
14137a09cd7Sopenharmony_ci    if (!ThermalService::isThermalAirplane_) {
14237a09cd7Sopenharmony_ci        int32_t code = data.GetCode();
14337a09cd7Sopenharmony_ci        ThermalService::userAirplaneState_ = static_cast<bool>(code);
14437a09cd7Sopenharmony_ci        THERMAL_HILOGD(COMP_SVC, "user %{public}s Airplane mode",
14537a09cd7Sopenharmony_ci            ThermalService::userAirplaneState_ ? "open" : "close");
14637a09cd7Sopenharmony_ci    } else {
14737a09cd7Sopenharmony_ci        ThermalService::isThermalAirplane_ = false;
14837a09cd7Sopenharmony_ci        THERMAL_HILOGD(COMP_SVC, "thermal change Airplane mode");
14937a09cd7Sopenharmony_ci    }
15037a09cd7Sopenharmony_ci}
15137a09cd7Sopenharmony_ci#endif
15237a09cd7Sopenharmony_ci
15337a09cd7Sopenharmony_cibool ThermalService::Init()
15437a09cd7Sopenharmony_ci{
15537a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
15637a09cd7Sopenharmony_ci    if (!CreateConfigModule()) {
15737a09cd7Sopenharmony_ci        return false;
15837a09cd7Sopenharmony_ci    }
15937a09cd7Sopenharmony_ci    if (!InitModules()) {
16037a09cd7Sopenharmony_ci        return false;
16137a09cd7Sopenharmony_ci    }
16237a09cd7Sopenharmony_ci    RegisterHdiStatusListener();
16337a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Init success");
16437a09cd7Sopenharmony_ci    return true;
16537a09cd7Sopenharmony_ci}
16637a09cd7Sopenharmony_ci
16737a09cd7Sopenharmony_cibool ThermalService::CreateConfigModule()
16837a09cd7Sopenharmony_ci{
16937a09cd7Sopenharmony_ci    if (!baseInfo_) {
17037a09cd7Sopenharmony_ci        baseInfo_ = std::make_shared<ThermalConfigBaseInfo>();
17137a09cd7Sopenharmony_ci        if (baseInfo_ == nullptr) {
17237a09cd7Sopenharmony_ci            THERMAL_HILOGE(COMP_SVC, "failed to create base info");
17337a09cd7Sopenharmony_ci            return false;
17437a09cd7Sopenharmony_ci        }
17537a09cd7Sopenharmony_ci    }
17637a09cd7Sopenharmony_ci
17737a09cd7Sopenharmony_ci    if (!state_) {
17837a09cd7Sopenharmony_ci        state_ = std::make_shared<StateMachine>();
17937a09cd7Sopenharmony_ci        if (state_ == nullptr) {
18037a09cd7Sopenharmony_ci            THERMAL_HILOGE(COMP_SVC, "failed to create state machine");
18137a09cd7Sopenharmony_ci            return false;
18237a09cd7Sopenharmony_ci        }
18337a09cd7Sopenharmony_ci    }
18437a09cd7Sopenharmony_ci
18537a09cd7Sopenharmony_ci    if (!actionMgr_) {
18637a09cd7Sopenharmony_ci        actionMgr_ = std::make_shared<ThermalActionManager>();
18737a09cd7Sopenharmony_ci        if (actionMgr_ == nullptr) {
18837a09cd7Sopenharmony_ci            THERMAL_HILOGE(COMP_SVC, "failed to create action manager");
18937a09cd7Sopenharmony_ci            return false;
19037a09cd7Sopenharmony_ci        }
19137a09cd7Sopenharmony_ci    }
19237a09cd7Sopenharmony_ci
19337a09cd7Sopenharmony_ci    if (!policy_) {
19437a09cd7Sopenharmony_ci        policy_ = std::make_shared<ThermalPolicy>();
19537a09cd7Sopenharmony_ci        if (policy_ == nullptr) {
19637a09cd7Sopenharmony_ci            THERMAL_HILOGE(COMP_SVC, "failed to create thermal policy");
19737a09cd7Sopenharmony_ci            return false;
19837a09cd7Sopenharmony_ci        }
19937a09cd7Sopenharmony_ci    }
20037a09cd7Sopenharmony_ci
20137a09cd7Sopenharmony_ci    if (!fanFaultDetect_) {
20237a09cd7Sopenharmony_ci        fanFaultDetect_ = std::make_shared<FanFaultDetect>();
20337a09cd7Sopenharmony_ci        if (fanFaultDetect_ == nullptr) {
20437a09cd7Sopenharmony_ci            THERMAL_HILOGE(COMP_SVC, "failed to create fan fault detect");
20537a09cd7Sopenharmony_ci            return false;
20637a09cd7Sopenharmony_ci        }
20737a09cd7Sopenharmony_ci    }
20837a09cd7Sopenharmony_ci
20937a09cd7Sopenharmony_ci    return true;
21037a09cd7Sopenharmony_ci}
21137a09cd7Sopenharmony_ci
21237a09cd7Sopenharmony_cibool ThermalService::InitConfigFile()
21337a09cd7Sopenharmony_ci{
21437a09cd7Sopenharmony_ci    if (serviceConfigParsed) {
21537a09cd7Sopenharmony_ci        THERMAL_HILOGI(COMP_SVC, "system config file has parsed.");
21637a09cd7Sopenharmony_ci        return true;
21737a09cd7Sopenharmony_ci    }
21837a09cd7Sopenharmony_ci#ifdef HAS_THERMAL_CONFIG_POLICY_PART
21937a09cd7Sopenharmony_ci    char buf[MAX_PATH_LEN];
22037a09cd7Sopenharmony_ci    char* path = GetOneCfgFile(THERMAL_SERVICE_CONFIG_PATH.c_str(), buf, MAX_PATH_LEN);
22137a09cd7Sopenharmony_ci    if (path != nullptr && *path != '\0') {
22237a09cd7Sopenharmony_ci        if (configParser_.ThermalSrvConfigInit(path)) {
22337a09cd7Sopenharmony_ci            THERMAL_HILOGD(COMP_SVC, "match pliocy config file");
22437a09cd7Sopenharmony_ci            return true;
22537a09cd7Sopenharmony_ci        }
22637a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "pliocy config file config init err");
22737a09cd7Sopenharmony_ci        return false;
22837a09cd7Sopenharmony_ci    }
22937a09cd7Sopenharmony_ci#endif
23037a09cd7Sopenharmony_ci
23137a09cd7Sopenharmony_ci    if (configParser_.ThermalSrvConfigInit(VENDOR_THERMAL_SERVICE_CONFIG_PATH)) {
23237a09cd7Sopenharmony_ci        THERMAL_HILOGD(COMP_SVC, "thermal service config init suc:VENDOR_CONFIG");
23337a09cd7Sopenharmony_ci        return true;
23437a09cd7Sopenharmony_ci    }
23537a09cd7Sopenharmony_ci
23637a09cd7Sopenharmony_ci    if (configParser_.ThermalSrvConfigInit(SYSTEM_THERMAL_SERVICE_CONFIG_PATH)) {
23737a09cd7Sopenharmony_ci        THERMAL_HILOGD(COMP_SVC, "thermal service config init suc:SYSTEM_CONFIG");
23837a09cd7Sopenharmony_ci        return true;
23937a09cd7Sopenharmony_ci    }
24037a09cd7Sopenharmony_ci
24137a09cd7Sopenharmony_ci    return false;
24237a09cd7Sopenharmony_ci}
24337a09cd7Sopenharmony_ci
24437a09cd7Sopenharmony_ci
24537a09cd7Sopenharmony_cibool ThermalService::InitConfigModule()
24637a09cd7Sopenharmony_ci{
24737a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "InitVendor Enter");
24837a09cd7Sopenharmony_ci    if (!CreateConfigModule()) {
24937a09cd7Sopenharmony_ci        THERMAL_HILOGD(COMP_SVC, "CreateConfigModule fail");
25037a09cd7Sopenharmony_ci    }
25137a09cd7Sopenharmony_ci
25237a09cd7Sopenharmony_ci    if (!configParser_.ThermalSrvConfigInit(SYSTEM_THERMAL_SERVICE_CONFIG_PATH)) {
25337a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "system thermal service config init failed.");
25437a09cd7Sopenharmony_ci        return false;
25537a09cd7Sopenharmony_ci    }
25637a09cd7Sopenharmony_ci    serviceConfigParsed = true;
25737a09cd7Sopenharmony_ci
25837a09cd7Sopenharmony_ci    THERMAL_HILOGE(COMP_SVC, "system thermal service config init suc.");
25937a09cd7Sopenharmony_ci    return true;
26037a09cd7Sopenharmony_ci}
26137a09cd7Sopenharmony_ci
26237a09cd7Sopenharmony_cibool ThermalService::InitModules()
26337a09cd7Sopenharmony_ci{
26437a09cd7Sopenharmony_ci    if (!InitConfigFile()) {
26537a09cd7Sopenharmony_ci        return false;
26637a09cd7Sopenharmony_ci    }
26737a09cd7Sopenharmony_ci
26837a09cd7Sopenharmony_ci    if (popup_ == nullptr) {
26937a09cd7Sopenharmony_ci        popup_ = std::make_shared<ActionPopup>(POPUP_ACTION_NAME);
27037a09cd7Sopenharmony_ci    }
27137a09cd7Sopenharmony_ci
27237a09cd7Sopenharmony_ci    if (!InitThermalObserver()) {
27337a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "thermal observer start fail");
27437a09cd7Sopenharmony_ci        return false;
27537a09cd7Sopenharmony_ci    }
27637a09cd7Sopenharmony_ci
27737a09cd7Sopenharmony_ci    if (!InitActionManager()) {
27837a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "action manager init fail");
27937a09cd7Sopenharmony_ci        return false;
28037a09cd7Sopenharmony_ci    }
28137a09cd7Sopenharmony_ci
28237a09cd7Sopenharmony_ci    if (!InitThermalPolicy()) {
28337a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "thermal policy start fail");
28437a09cd7Sopenharmony_ci        return false;
28537a09cd7Sopenharmony_ci    }
28637a09cd7Sopenharmony_ci
28737a09cd7Sopenharmony_ci    if (!InitThermalSubscriber()) {
28837a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "thermal subscriber init fail");
28937a09cd7Sopenharmony_ci        return false;
29037a09cd7Sopenharmony_ci    }
29137a09cd7Sopenharmony_ci    return true;
29237a09cd7Sopenharmony_ci}
29337a09cd7Sopenharmony_ci
29437a09cd7Sopenharmony_cibool ThermalService::InitThermalObserver()
29537a09cd7Sopenharmony_ci{
29637a09cd7Sopenharmony_ci    if (!InitBaseInfo()) {
29737a09cd7Sopenharmony_ci        return false;
29837a09cd7Sopenharmony_ci    }
29937a09cd7Sopenharmony_ci
30037a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
30137a09cd7Sopenharmony_ci    if (observer_ == nullptr) {
30237a09cd7Sopenharmony_ci        observer_ = std::make_shared<ThermalObserver>();
30337a09cd7Sopenharmony_ci        if (!(observer_->Init())) {
30437a09cd7Sopenharmony_ci            THERMAL_HILOGE(COMP_SVC, "InitThermalObserver: thermal observer start fail");
30537a09cd7Sopenharmony_ci            return false;
30637a09cd7Sopenharmony_ci        }
30737a09cd7Sopenharmony_ci    }
30837a09cd7Sopenharmony_ci    if (info_ == nullptr) {
30937a09cd7Sopenharmony_ci        info_ = std::make_shared<ThermalSensorInfo>();
31037a09cd7Sopenharmony_ci    }
31137a09cd7Sopenharmony_ci    THERMAL_HILOGI(COMP_SVC, "InitThermalObserver: Init Success");
31237a09cd7Sopenharmony_ci    return true;
31337a09cd7Sopenharmony_ci}
31437a09cd7Sopenharmony_ci
31537a09cd7Sopenharmony_cibool ThermalService::InitBaseInfo()
31637a09cd7Sopenharmony_ci{
31737a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
31837a09cd7Sopenharmony_ci    if (!baseInfo_->Init()) {
31937a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "InitBaseInfo: base info init failed");
32037a09cd7Sopenharmony_ci        return false;
32137a09cd7Sopenharmony_ci    }
32237a09cd7Sopenharmony_ci    return true;
32337a09cd7Sopenharmony_ci}
32437a09cd7Sopenharmony_ci
32537a09cd7Sopenharmony_cibool ThermalService::InitStateMachine()
32637a09cd7Sopenharmony_ci{
32737a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
32837a09cd7Sopenharmony_ci    if (!state_->Init()) {
32937a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "InitStateMachine: state machine init failed");
33037a09cd7Sopenharmony_ci        return false;
33137a09cd7Sopenharmony_ci    }
33237a09cd7Sopenharmony_ci    return true;
33337a09cd7Sopenharmony_ci}
33437a09cd7Sopenharmony_ci
33537a09cd7Sopenharmony_cibool ThermalService::InitActionManager()
33637a09cd7Sopenharmony_ci{
33737a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
33837a09cd7Sopenharmony_ci    if (!actionMgr_->Init()) {
33937a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "InitActionManager: action manager init failed");
34037a09cd7Sopenharmony_ci        return false;
34137a09cd7Sopenharmony_ci    }
34237a09cd7Sopenharmony_ci    return true;
34337a09cd7Sopenharmony_ci}
34437a09cd7Sopenharmony_ci
34537a09cd7Sopenharmony_cibool ThermalService::InitThermalPolicy()
34637a09cd7Sopenharmony_ci{
34737a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
34837a09cd7Sopenharmony_ci    if (!policy_->Init()) {
34937a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "InitThermalPolicy: policy init failed");
35037a09cd7Sopenharmony_ci        return false;
35137a09cd7Sopenharmony_ci    }
35237a09cd7Sopenharmony_ci    return true;
35337a09cd7Sopenharmony_ci}
35437a09cd7Sopenharmony_ci
35537a09cd7Sopenharmony_cibool ThermalService::InitThermalSubscriber()
35637a09cd7Sopenharmony_ci{
35737a09cd7Sopenharmony_ci    if (serviceSubscriber_ == nullptr) {
35837a09cd7Sopenharmony_ci        serviceSubscriber_ = std::make_shared<ThermalServiceSubscriber>();
35937a09cd7Sopenharmony_ci        if (!(serviceSubscriber_->Init())) {
36037a09cd7Sopenharmony_ci            THERMAL_HILOGE(COMP_SVC, "InitThermalSubscriber: thermal subscriber init failed");
36137a09cd7Sopenharmony_ci            return false;
36237a09cd7Sopenharmony_ci        }
36337a09cd7Sopenharmony_ci    }
36437a09cd7Sopenharmony_ci    return true;
36537a09cd7Sopenharmony_ci}
36637a09cd7Sopenharmony_ci
36737a09cd7Sopenharmony_civoid ThermalService::OnStop()
36837a09cd7Sopenharmony_ci{
36937a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
37037a09cd7Sopenharmony_ci    if (!ready_) {
37137a09cd7Sopenharmony_ci        return;
37237a09cd7Sopenharmony_ci    }
37337a09cd7Sopenharmony_ci    ready_ = false;
37437a09cd7Sopenharmony_ci    isBootCompleted_ = false;
37537a09cd7Sopenharmony_ci    RemoveSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
37637a09cd7Sopenharmony_ci    if (thermalInterface_) {
37737a09cd7Sopenharmony_ci        thermalInterface_->Unregister();
37837a09cd7Sopenharmony_ci        thermalInterface_->UnregisterFanCallback();
37937a09cd7Sopenharmony_ci        thermalInterface_ = nullptr;
38037a09cd7Sopenharmony_ci    }
38137a09cd7Sopenharmony_ci    if (hdiServiceMgr_) {
38237a09cd7Sopenharmony_ci        hdiServiceMgr_->UnregisterServiceStatusListener(hdiServStatListener_);
38337a09cd7Sopenharmony_ci        hdiServiceMgr_ = nullptr;
38437a09cd7Sopenharmony_ci    }
38537a09cd7Sopenharmony_ci#ifdef HAS_THERMAL_AIRPLANE_MANAGER_PART
38637a09cd7Sopenharmony_ci    if (!OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriberPtr_)) {
38737a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "Thermal Onstop unregister to commonevent manager failed!");
38837a09cd7Sopenharmony_ci    } else {
38937a09cd7Sopenharmony_ci        THERMAL_HILOGD(COMP_SVC, "Thermal Onstop unregister to commonevent manager success!");
39037a09cd7Sopenharmony_ci    }
39137a09cd7Sopenharmony_ci#endif
39237a09cd7Sopenharmony_ci}
39337a09cd7Sopenharmony_ci
39437a09cd7Sopenharmony_cibool ThermalService::SubscribeThermalTempCallback(
39537a09cd7Sopenharmony_ci    const std::vector<std::string>& typeList, const sptr<IThermalTempCallback>& callback)
39637a09cd7Sopenharmony_ci{
39737a09cd7Sopenharmony_ci    if (!Permission::IsSystem()) {
39837a09cd7Sopenharmony_ci        return false;
39937a09cd7Sopenharmony_ci    }
40037a09cd7Sopenharmony_ci    auto uid = IPCSkeleton::GetCallingUid();
40137a09cd7Sopenharmony_ci    THERMAL_HILOGI(COMP_SVC, "%{public}s is called by uid=%{public}d", __func__, uid);
40237a09cd7Sopenharmony_ci    observer_->SubscribeThermalTempCallback(typeList, callback);
40337a09cd7Sopenharmony_ci    return true;
40437a09cd7Sopenharmony_ci}
40537a09cd7Sopenharmony_ci
40637a09cd7Sopenharmony_cibool ThermalService::UnSubscribeThermalTempCallback(const sptr<IThermalTempCallback>& callback)
40737a09cd7Sopenharmony_ci{
40837a09cd7Sopenharmony_ci    if (!Permission::IsSystem()) {
40937a09cd7Sopenharmony_ci        return false;
41037a09cd7Sopenharmony_ci    }
41137a09cd7Sopenharmony_ci    auto uid = IPCSkeleton::GetCallingUid();
41237a09cd7Sopenharmony_ci    THERMAL_HILOGI(COMP_SVC, "%{public}s is called by uid=%{public}d", __func__, uid);
41337a09cd7Sopenharmony_ci    observer_->UnSubscribeThermalTempCallback(callback);
41437a09cd7Sopenharmony_ci    return true;
41537a09cd7Sopenharmony_ci}
41637a09cd7Sopenharmony_ci
41737a09cd7Sopenharmony_cibool ThermalService::GetThermalSrvSensorInfo(const SensorType& type, ThermalSrvSensorInfo& sensorInfo)
41837a09cd7Sopenharmony_ci{
41937a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
42037a09cd7Sopenharmony_ci    if (!(observer_->GetThermalSrvSensorInfo(type, sensorInfo))) {
42137a09cd7Sopenharmony_ci        THERMAL_HILOGW(COMP_SVC, "failed to get sensor temp, type enum: %{public}u", static_cast<uint32_t>(type));
42237a09cd7Sopenharmony_ci        return false;
42337a09cd7Sopenharmony_ci    }
42437a09cd7Sopenharmony_ci    return true;
42537a09cd7Sopenharmony_ci}
42637a09cd7Sopenharmony_ci
42737a09cd7Sopenharmony_cibool ThermalService::SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback)
42837a09cd7Sopenharmony_ci{
42937a09cd7Sopenharmony_ci    auto uid = IPCSkeleton::GetCallingUid();
43037a09cd7Sopenharmony_ci    THERMAL_HILOGI(COMP_SVC, "%{public}s is called by uid=%{public}d", __func__, uid);
43137a09cd7Sopenharmony_ci    actionMgr_->SubscribeThermalLevelCallback(callback);
43237a09cd7Sopenharmony_ci    return true;
43337a09cd7Sopenharmony_ci}
43437a09cd7Sopenharmony_ci
43537a09cd7Sopenharmony_cibool ThermalService::UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback)
43637a09cd7Sopenharmony_ci{
43737a09cd7Sopenharmony_ci    auto uid = IPCSkeleton::GetCallingUid();
43837a09cd7Sopenharmony_ci    THERMAL_HILOGI(COMP_SVC, "%{public}s is called by uid=%{public}d", __func__, uid);
43937a09cd7Sopenharmony_ci    actionMgr_->UnSubscribeThermalLevelCallback(callback);
44037a09cd7Sopenharmony_ci    return true;
44137a09cd7Sopenharmony_ci}
44237a09cd7Sopenharmony_ci
44337a09cd7Sopenharmony_cibool ThermalService::SubscribeThermalActionCallback(
44437a09cd7Sopenharmony_ci    const std::vector<std::string>& actionList, const std::string& desc, const sptr<IThermalActionCallback>& callback)
44537a09cd7Sopenharmony_ci{
44637a09cd7Sopenharmony_ci    if (!Permission::IsSystem()) {
44737a09cd7Sopenharmony_ci        return false;
44837a09cd7Sopenharmony_ci    }
44937a09cd7Sopenharmony_ci    auto pid = IPCSkeleton::GetCallingPid();
45037a09cd7Sopenharmony_ci    auto uid = IPCSkeleton::GetCallingUid();
45137a09cd7Sopenharmony_ci    THERMAL_HILOGI(COMP_SVC, "%{public}s is called by pid=%{public}d, uid=%{public}d", __func__, pid, uid);
45237a09cd7Sopenharmony_ci    observer_->SubscribeThermalActionCallback(actionList, desc, callback);
45337a09cd7Sopenharmony_ci    return true;
45437a09cd7Sopenharmony_ci}
45537a09cd7Sopenharmony_ci
45637a09cd7Sopenharmony_cibool ThermalService::UnSubscribeThermalActionCallback(const sptr<IThermalActionCallback>& callback)
45737a09cd7Sopenharmony_ci{
45837a09cd7Sopenharmony_ci    if (!Permission::IsSystem()) {
45937a09cd7Sopenharmony_ci        return false;
46037a09cd7Sopenharmony_ci    }
46137a09cd7Sopenharmony_ci    auto pid = IPCSkeleton::GetCallingPid();
46237a09cd7Sopenharmony_ci    auto uid = IPCSkeleton::GetCallingUid();
46337a09cd7Sopenharmony_ci    THERMAL_HILOGI(COMP_SVC, "%{public}s is called by pid=%{public}d, uid=%{public}d", __func__, pid, uid);
46437a09cd7Sopenharmony_ci    observer_->UnSubscribeThermalActionCallback(callback);
46537a09cd7Sopenharmony_ci    return true;
46637a09cd7Sopenharmony_ci}
46737a09cd7Sopenharmony_ci
46837a09cd7Sopenharmony_cibool ThermalService::GetThermalLevel(ThermalLevel& level)
46937a09cd7Sopenharmony_ci{
47037a09cd7Sopenharmony_ci    uint32_t levelValue = actionMgr_->GetThermalLevel();
47137a09cd7Sopenharmony_ci    level = static_cast<ThermalLevel>(levelValue);
47237a09cd7Sopenharmony_ci    return true;
47337a09cd7Sopenharmony_ci}
47437a09cd7Sopenharmony_ci
47537a09cd7Sopenharmony_cibool ThermalService::GetThermalInfo()
47637a09cd7Sopenharmony_ci{
47737a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
47837a09cd7Sopenharmony_ci    HdfThermalCallbackInfo thermalInfo;
47937a09cd7Sopenharmony_ci    bool ret = false;
48037a09cd7Sopenharmony_ci    if (thermalInterface_ == nullptr) {
48137a09cd7Sopenharmony_ci        thermalInterface_ = IThermalInterface::Get();
48237a09cd7Sopenharmony_ci        if (thermalInterface_ == nullptr) {
48337a09cd7Sopenharmony_ci            THERMAL_HILOGD(COMP_SVC, "thermalInterface_ is nullptr");
48437a09cd7Sopenharmony_ci            return ret;
48537a09cd7Sopenharmony_ci        }
48637a09cd7Sopenharmony_ci    }
48737a09cd7Sopenharmony_ci
48837a09cd7Sopenharmony_ci    if (thermalInterface_ != nullptr) {
48937a09cd7Sopenharmony_ci        int32_t res = thermalInterface_->GetThermalZoneInfo(thermalInfo);
49037a09cd7Sopenharmony_ci        HandleThermalCallbackEvent(thermalInfo);
49137a09cd7Sopenharmony_ci        if (!res) {
49237a09cd7Sopenharmony_ci            ret = true;
49337a09cd7Sopenharmony_ci        }
49437a09cd7Sopenharmony_ci    }
49537a09cd7Sopenharmony_ci    return ret;
49637a09cd7Sopenharmony_ci}
49737a09cd7Sopenharmony_ci
49837a09cd7Sopenharmony_cibool ThermalService::SetScene(const std::string& scene)
49937a09cd7Sopenharmony_ci{
50037a09cd7Sopenharmony_ci    if (!Permission::IsSystem()) {
50137a09cd7Sopenharmony_ci        return false;
50237a09cd7Sopenharmony_ci    }
50337a09cd7Sopenharmony_ci    scene_ = scene;
50437a09cd7Sopenharmony_ci    return true;
50537a09cd7Sopenharmony_ci}
50637a09cd7Sopenharmony_ci
50737a09cd7Sopenharmony_cibool ThermalService::UpdateThermalState(const std::string& tag, const std::string& val, bool isImmed)
50837a09cd7Sopenharmony_ci{
50937a09cd7Sopenharmony_ci    if (!Permission::IsSystem()) {
51037a09cd7Sopenharmony_ci        return false;
51137a09cd7Sopenharmony_ci    }
51237a09cd7Sopenharmony_ci    THERMAL_HILOGI(COMP_SVC, "tag %{public}s, val %{public}s", tag.c_str(), val.c_str());
51337a09cd7Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
51437a09cd7Sopenharmony_ci    state_->UpdateState(tag, val);
51537a09cd7Sopenharmony_ci    if (isImmed) {
51637a09cd7Sopenharmony_ci        policy_->ExecutePolicy();
51737a09cd7Sopenharmony_ci    }
51837a09cd7Sopenharmony_ci    return true;
51937a09cd7Sopenharmony_ci}
52037a09cd7Sopenharmony_ci
52137a09cd7Sopenharmony_civoid ThermalService::RegisterHdiStatusListener()
52237a09cd7Sopenharmony_ci{
52337a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
52437a09cd7Sopenharmony_ci    hdiServiceMgr_ = IServiceManager::Get();
52537a09cd7Sopenharmony_ci    if (hdiServiceMgr_ == nullptr) {
52637a09cd7Sopenharmony_ci        FFRTTask retryTask = [this] {
52737a09cd7Sopenharmony_ci            return RegisterHdiStatusListener();
52837a09cd7Sopenharmony_ci        };
52937a09cd7Sopenharmony_ci        FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, g_queue);
53037a09cd7Sopenharmony_ci        THERMAL_HILOGW(COMP_SVC, "hdi service manager is nullptr, try again after %{public}u ms", RETRY_TIME);
53137a09cd7Sopenharmony_ci        return;
53237a09cd7Sopenharmony_ci    }
53337a09cd7Sopenharmony_ci
53437a09cd7Sopenharmony_ci    hdiServStatListener_ = new HdiServiceStatusListener(
53537a09cd7Sopenharmony_ci        HdiServiceStatusListener::StatusCallback([&](const OHOS::HDI::ServiceManager::V1_0::ServiceStatus& status) {
53637a09cd7Sopenharmony_ci            THERMAL_RETURN_IF(status.serviceName != HDI_SERVICE_NAME || status.deviceClass != DEVICE_CLASS_DEFAULT)
53737a09cd7Sopenharmony_ci
53837a09cd7Sopenharmony_ci            if (status.status == SERVIE_STATUS_START) {
53937a09cd7Sopenharmony_ci                FFRTTask task = [this] {
54037a09cd7Sopenharmony_ci                    RegisterThermalHdiCallback();
54137a09cd7Sopenharmony_ci                    RegisterFanHdiCallback();
54237a09cd7Sopenharmony_ci                };
54337a09cd7Sopenharmony_ci                FFRTUtils::SubmitTask(task);
54437a09cd7Sopenharmony_ci                THERMAL_HILOGD(COMP_SVC, "thermal interface service start");
54537a09cd7Sopenharmony_ci            } else if (status.status == SERVIE_STATUS_STOP && thermalInterface_) {
54637a09cd7Sopenharmony_ci                thermalInterface_->Unregister();
54737a09cd7Sopenharmony_ci                thermalInterface_->UnregisterFanCallback();
54837a09cd7Sopenharmony_ci                thermalInterface_ = nullptr;
54937a09cd7Sopenharmony_ci                THERMAL_HILOGW(COMP_SVC, "thermal interface service, unregister interface");
55037a09cd7Sopenharmony_ci            }
55137a09cd7Sopenharmony_ci        }));
55237a09cd7Sopenharmony_ci
55337a09cd7Sopenharmony_ci    int32_t status = hdiServiceMgr_->RegisterServiceStatusListener(hdiServStatListener_, DEVICE_CLASS_DEFAULT);
55437a09cd7Sopenharmony_ci    if (status != ERR_OK) {
55537a09cd7Sopenharmony_ci        THERMAL_HILOGW(COMP_SVC, "Register hdi failed, try again after %{public}u ms", RETRY_TIME);
55637a09cd7Sopenharmony_ci        FFRTTask retryTask = [this] {
55737a09cd7Sopenharmony_ci            return RegisterHdiStatusListener();
55837a09cd7Sopenharmony_ci        };
55937a09cd7Sopenharmony_ci        FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, g_queue);
56037a09cd7Sopenharmony_ci    }
56137a09cd7Sopenharmony_ci}
56237a09cd7Sopenharmony_ci
56337a09cd7Sopenharmony_civoid ThermalService::RegisterThermalHdiCallback()
56437a09cd7Sopenharmony_ci{
56537a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "register thermal hdi callback");
56637a09cd7Sopenharmony_ci    if (thermalInterface_ == nullptr) {
56737a09cd7Sopenharmony_ci        thermalInterface_ = IThermalInterface::Get();
56837a09cd7Sopenharmony_ci        THERMAL_RETURN_IF_WITH_LOG(thermalInterface_ == nullptr, "failed to get thermal hdi interface");
56937a09cd7Sopenharmony_ci    }
57037a09cd7Sopenharmony_ci
57137a09cd7Sopenharmony_ci    sptr<IThermalCallback> callback = new ThermalCallback();
57237a09cd7Sopenharmony_ci    ThermalCallback::ThermalEventCallback eventCb =
57337a09cd7Sopenharmony_ci        [this](const HdfThermalCallbackInfo& event) -> int32_t { return this->HandleThermalCallbackEvent(event); };
57437a09cd7Sopenharmony_ci    ThermalCallback::RegisterThermalEvent(eventCb);
57537a09cd7Sopenharmony_ci    int32_t ret = thermalInterface_->Register(callback);
57637a09cd7Sopenharmony_ci    THERMAL_HILOGI(COMP_SVC, "register thermal hdi callback end, ret: %{public}d", ret);
57737a09cd7Sopenharmony_ci}
57837a09cd7Sopenharmony_ci
57937a09cd7Sopenharmony_civoid ThermalService::UnRegisterThermalHdiCallback()
58037a09cd7Sopenharmony_ci{
58137a09cd7Sopenharmony_ci    if (thermalInterface_ == nullptr) {
58237a09cd7Sopenharmony_ci        FFRTTask retryTask = [this] {
58337a09cd7Sopenharmony_ci            return UnRegisterThermalHdiCallback();
58437a09cd7Sopenharmony_ci        };
58537a09cd7Sopenharmony_ci        FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, g_queue);
58637a09cd7Sopenharmony_ci        THERMAL_HILOGW(COMP_SVC, "thermalInterface_ is nullptr, try again after %{public}u ms", RETRY_TIME);
58737a09cd7Sopenharmony_ci        return;
58837a09cd7Sopenharmony_ci    }
58937a09cd7Sopenharmony_ci    int32_t ret = thermalInterface_->Unregister();
59037a09cd7Sopenharmony_ci    THERMAL_HILOGI(COMP_SVC, "unregister thermal hdi callback end, ret: %{public}d", ret);
59137a09cd7Sopenharmony_ci}
59237a09cd7Sopenharmony_ci
59337a09cd7Sopenharmony_ciint32_t ThermalService::HandleThermalCallbackEvent(const HdfThermalCallbackInfo& event)
59437a09cd7Sopenharmony_ci{
59537a09cd7Sopenharmony_ci#ifndef THERMAL_USER_VERSION
59637a09cd7Sopenharmony_ci    if (!isTempReport_) {
59737a09cd7Sopenharmony_ci        return ERR_OK;
59837a09cd7Sopenharmony_ci    }
59937a09cd7Sopenharmony_ci#endif
60037a09cd7Sopenharmony_ci    TypeTempMap typeTempMap;
60137a09cd7Sopenharmony_ci    if (!event.info.empty()) {
60237a09cd7Sopenharmony_ci        for (auto iter = event.info.begin(); iter != event.info.end(); iter++) {
60337a09cd7Sopenharmony_ci            typeTempMap.insert(std::make_pair(iter->type, iter->temp));
60437a09cd7Sopenharmony_ci        }
60537a09cd7Sopenharmony_ci    }
60637a09cd7Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
60737a09cd7Sopenharmony_ci    serviceSubscriber_->OnTemperatureChanged(typeTempMap);
60837a09cd7Sopenharmony_ci    return ERR_OK;
60937a09cd7Sopenharmony_ci}
61037a09cd7Sopenharmony_ci
61137a09cd7Sopenharmony_cibool ThermalService::HandleTempEmulation(const TypeTempMap& typeTempMap)
61237a09cd7Sopenharmony_ci{
61337a09cd7Sopenharmony_ci    if (isTempReport_) {
61437a09cd7Sopenharmony_ci        return false;
61537a09cd7Sopenharmony_ci    }
61637a09cd7Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
61737a09cd7Sopenharmony_ci    serviceSubscriber_->OnTemperatureChanged(typeTempMap);
61837a09cd7Sopenharmony_ci    return true;
61937a09cd7Sopenharmony_ci}
62037a09cd7Sopenharmony_ci
62137a09cd7Sopenharmony_civoid ThermalService::RegisterFanHdiCallback()
62237a09cd7Sopenharmony_ci{
62337a09cd7Sopenharmony_ci    if (!fanFaultDetect_->HasFanConfig()) {
62437a09cd7Sopenharmony_ci        return;
62537a09cd7Sopenharmony_ci    }
62637a09cd7Sopenharmony_ci
62737a09cd7Sopenharmony_ci    if (thermalInterface_ == nullptr) {
62837a09cd7Sopenharmony_ci        thermalInterface_ = IThermalInterface::Get();
62937a09cd7Sopenharmony_ci        THERMAL_RETURN_IF_WITH_LOG(thermalInterface_ == nullptr, "failed to get thermal hdi interface");
63037a09cd7Sopenharmony_ci    }
63137a09cd7Sopenharmony_ci
63237a09cd7Sopenharmony_ci    sptr<IFanCallback> callback = new FanCallback();
63337a09cd7Sopenharmony_ci    FanCallback::FanEventCallback eventCb =
63437a09cd7Sopenharmony_ci        [this](const HdfThermalCallbackInfo& event) -> int32_t { return this->HandleFanCallbackEvent(event); };
63537a09cd7Sopenharmony_ci    FanCallback::RegisterFanEvent(eventCb);
63637a09cd7Sopenharmony_ci    int32_t ret = thermalInterface_->RegisterFanCallback(callback);
63737a09cd7Sopenharmony_ci    THERMAL_HILOGI(COMP_SVC, "register fan hdi callback end, ret: %{public}d", ret);
63837a09cd7Sopenharmony_ci
63937a09cd7Sopenharmony_ci    return;
64037a09cd7Sopenharmony_ci}
64137a09cd7Sopenharmony_ci
64237a09cd7Sopenharmony_ciint32_t ThermalService::HandleFanCallbackEvent(const HdfThermalCallbackInfo& event)
64337a09cd7Sopenharmony_ci{
64437a09cd7Sopenharmony_ci    FanSensorInfo report;
64537a09cd7Sopenharmony_ci    if (!event.info.empty()) {
64637a09cd7Sopenharmony_ci        for (auto iter = event.info.begin(); iter != event.info.end(); iter++) {
64737a09cd7Sopenharmony_ci            report.insert(std::make_pair(iter->type, iter->temp));
64837a09cd7Sopenharmony_ci        }
64937a09cd7Sopenharmony_ci    }
65037a09cd7Sopenharmony_ci
65137a09cd7Sopenharmony_ci    fanFaultDetect_->OnFanSensorInfoChanged(report);
65237a09cd7Sopenharmony_ci    return ERR_OK;
65337a09cd7Sopenharmony_ci}
65437a09cd7Sopenharmony_ci
65537a09cd7Sopenharmony_cistd::string ThermalService::ShellDump(const std::vector<std::string>& args, uint32_t argc)
65637a09cd7Sopenharmony_ci{
65737a09cd7Sopenharmony_ci    if (!Permission::IsSystem() || !isBootCompleted_) {
65837a09cd7Sopenharmony_ci        return "";
65937a09cd7Sopenharmony_ci    }
66037a09cd7Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
66137a09cd7Sopenharmony_ci    pid_t pid = IPCSkeleton::GetCallingPid();
66237a09cd7Sopenharmony_ci    THERMAL_HILOGI(COMP_SVC, "PID: %{public}d", pid);
66337a09cd7Sopenharmony_ci    std::string result;
66437a09cd7Sopenharmony_ci    bool ret = ThermalMgrDumper::Dump(args, result);
66537a09cd7Sopenharmony_ci    THERMAL_HILOGI(COMP_SVC, "ThermalMgrDumper :%{public}d", ret);
66637a09cd7Sopenharmony_ci    return result;
66737a09cd7Sopenharmony_ci}
66837a09cd7Sopenharmony_ci
66937a09cd7Sopenharmony_ciint32_t ThermalService::Dump(int fd, const std::vector<std::u16string>& args)
67037a09cd7Sopenharmony_ci{
67137a09cd7Sopenharmony_ci    if (!isBootCompleted_) {
67237a09cd7Sopenharmony_ci        return ERR_NO_INIT;
67337a09cd7Sopenharmony_ci    }
67437a09cd7Sopenharmony_ci    if (!Permission::IsSystem()) {
67537a09cd7Sopenharmony_ci        return ERR_PERMISSION_DENIED;
67637a09cd7Sopenharmony_ci    }
67737a09cd7Sopenharmony_ci    std::vector<std::string> argsInStr;
67837a09cd7Sopenharmony_ci    std::transform(args.begin(), args.end(), std::back_inserter(argsInStr), [](const std::u16string& arg) {
67937a09cd7Sopenharmony_ci        std::string ret = Str16ToStr8(arg);
68037a09cd7Sopenharmony_ci        THERMAL_HILOGI(COMP_SVC, "arg: %{public}s", ret.c_str());
68137a09cd7Sopenharmony_ci        return ret;
68237a09cd7Sopenharmony_ci    });
68337a09cd7Sopenharmony_ci    std::string result;
68437a09cd7Sopenharmony_ci    ThermalMgrDumper::Dump(argsInStr, result);
68537a09cd7Sopenharmony_ci    if (!SaveStringToFd(fd, result)) {
68637a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "ThermalService::Dump failed, save to fd failed.");
68737a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "Dump Info:\n");
68837a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "%{public}s", result.c_str());
68937a09cd7Sopenharmony_ci        return ERR_OK;
69037a09cd7Sopenharmony_ci    }
69137a09cd7Sopenharmony_ci    return ERR_OK;
69237a09cd7Sopenharmony_ci}
69337a09cd7Sopenharmony_ci
69437a09cd7Sopenharmony_civoid ThermalService::EnableMock(const std::string& actionName, void* mockObject)
69537a09cd7Sopenharmony_ci{
69637a09cd7Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
69737a09cd7Sopenharmony_ci    actionMgr_->EnableMock(actionName, mockObject);
69837a09cd7Sopenharmony_ci}
69937a09cd7Sopenharmony_ci
70037a09cd7Sopenharmony_civoid ThermalService::DestroyInstance()
70137a09cd7Sopenharmony_ci{
70237a09cd7Sopenharmony_ci    std::lock_guard<std::mutex> lock(singletonMutex_);
70337a09cd7Sopenharmony_ci    if (instance_) {
70437a09cd7Sopenharmony_ci        instance_.clear();
70537a09cd7Sopenharmony_ci        instance_ = nullptr;
70637a09cd7Sopenharmony_ci    }
70737a09cd7Sopenharmony_ci}
70837a09cd7Sopenharmony_ci} // namespace PowerMgr
70937a09cd7Sopenharmony_ci} // namespace OHOS
710