1 /*
2  * Copyright (c) 2021-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 "thermal_service.h"
17 
18 #include "file_ex.h"
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "securec.h"
22 #include "system_ability_definition.h"
23 #include <algorithm>
24 #include <fcntl.h>
25 #include <ipc_skeleton.h>
26 #include <thread>
27 #include <unistd.h>
28 
29 #include "config_policy_utils.h"
30 #include "constants.h"
31 #include "ffrt_utils.h"
32 #include "permission.h"
33 #include "sysparam.h"
34 #include "thermal_common.h"
35 #include "thermal_mgr_dumper.h"
36 #include "xcollie/watchdog.h"
37 
38 namespace OHOS {
39 namespace PowerMgr {
40 sptr<ThermalService> ThermalService::instance_ = nullptr;
41 std::mutex ThermalService::singletonMutex_;
42 namespace {
43 const std::string THERMAL_SERVICE_CONFIG_PATH = "etc/thermal_config/thermal_service_config.xml";
44 const std::string VENDOR_THERMAL_SERVICE_CONFIG_PATH = "/vendor/etc/thermal_config/thermal_service_config.xml";
45 const std::string SYSTEM_THERMAL_SERVICE_CONFIG_PATH = "/system/etc/thermal_config/thermal_service_config.xml";
46 constexpr const char* THMERMAL_SERVICE_NAME = "ThermalService";
47 constexpr const char* HDI_SERVICE_NAME = "thermal_interface_service";
48 FFRTQueue g_queue("thermal_service");
49 constexpr uint32_t RETRY_TIME = 1000;
50 auto g_service = ThermalService::GetInstance();
51 const bool G_REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(g_service.GetRefPtr());
52 SysParam::BootCompletedCallback g_bootCompletedCallback;
53 } // namespace
54 std::atomic_bool ThermalService::isBootCompleted_ = false;
55 std::string ThermalService::scene_;
ThermalService()56 ThermalService::ThermalService() : SystemAbility(POWER_MANAGER_THERMAL_SERVICE_ID, true) {}
57 
~ThermalService()58 ThermalService::~ThermalService() {}
59 
GetInstance()60 sptr<ThermalService> ThermalService::GetInstance()
61 {
62     if (instance_ == nullptr) {
63         std::lock_guard<std::mutex> lock(singletonMutex_);
64         if (instance_ == nullptr) {
65             instance_ = new ThermalService();
66         }
67     }
68     return instance_;
69 }
70 
OnStart()71 void ThermalService::OnStart()
72 {
73     THERMAL_HILOGD(COMP_SVC, "Enter");
74     if (ready_) {
75         THERMAL_HILOGE(COMP_SVC, "OnStart is ready, nothing to do");
76         return;
77     }
78 
79     if (!(Init())) {
80         THERMAL_HILOGE(COMP_SVC, "OnStart call init fail");
81         return;
82     }
83 
84     AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
85     if (!Publish(ThermalService::GetInstance())) {
86         THERMAL_HILOGE(COMP_SVC, "OnStart register to system ability manager failed.");
87         return;
88     }
89     RegisterBootCompletedCallback();
90     ready_ = true;
91     THERMAL_HILOGD(COMP_SVC, "OnStart and add system ability success");
92 }
93 
RegisterBootCompletedCallback()94 void ThermalService::RegisterBootCompletedCallback()
95 {
96     g_bootCompletedCallback = []() {
97         isBootCompleted_ = true;
98     };
99     SysParam::RegisterBootCompletedCallback(g_bootCompletedCallback);
100 }
101 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)102 void ThermalService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
103 {
104     THERMAL_HILOGI(COMP_SVC, "systemAbilityId=%{public}d, deviceId=%{private}s", systemAbilityId, deviceId.c_str());
105     if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
106         InitStateMachine();
107     }
108 }
109 
Init()110 bool ThermalService::Init()
111 {
112     THERMAL_HILOGD(COMP_SVC, "Enter");
113     if (!CreateConfigModule()) {
114         return false;
115     }
116     if (!InitModules()) {
117         return false;
118     }
119     RegisterHdiStatusListener();
120     THERMAL_HILOGD(COMP_SVC, "Init success");
121     return true;
122 }
123 
CreateConfigModule()124 bool ThermalService::CreateConfigModule()
125 {
126     if (!baseInfo_) {
127         baseInfo_ = std::make_shared<ThermalConfigBaseInfo>();
128         if (baseInfo_ == nullptr) {
129             THERMAL_HILOGE(COMP_SVC, "failed to create base info");
130             return false;
131         }
132     }
133 
134     if (!state_) {
135         state_ = std::make_shared<StateMachine>();
136         if (state_ == nullptr) {
137             THERMAL_HILOGE(COMP_SVC, "failed to create state machine");
138             return false;
139         }
140     }
141 
142     if (!actionMgr_) {
143         actionMgr_ = std::make_shared<ThermalActionManager>();
144         if (actionMgr_ == nullptr) {
145             THERMAL_HILOGE(COMP_SVC, "failed to create action manager");
146             return false;
147         }
148     }
149 
150     if (!policy_) {
151         policy_ = std::make_shared<ThermalPolicy>();
152         if (policy_ == nullptr) {
153             THERMAL_HILOGE(COMP_SVC, "failed to create thermal policy");
154             return false;
155         }
156     }
157 
158     if (!fanFaultDetect_) {
159         fanFaultDetect_ = std::make_shared<FanFaultDetect>();
160         if (fanFaultDetect_ == nullptr) {
161             THERMAL_HILOGE(COMP_SVC, "failed to create fan fault detect");
162             return false;
163         }
164     }
165 
166     return true;
167 }
168 
InitConfigFile()169 bool ThermalService::InitConfigFile()
170 {
171     if (serviceConfigParsed) {
172         THERMAL_HILOGI(COMP_SVC, "system config file has parsed.");
173         return true;
174     }
175     char buf[MAX_PATH_LEN];
176     char* path = GetOneCfgFile(THERMAL_SERVICE_CONFIG_PATH.c_str(), buf, MAX_PATH_LEN);
177     if (path != nullptr && *path != '\0') {
178         if (configParser_.ThermalSrvConfigInit(path)) {
179             THERMAL_HILOGD(COMP_SVC, "match pliocy config file");
180             return true;
181         }
182         THERMAL_HILOGE(COMP_SVC, "pliocy config file config init err");
183         return false;
184     }
185 
186     if (configParser_.ThermalSrvConfigInit(VENDOR_THERMAL_SERVICE_CONFIG_PATH)) {
187         THERMAL_HILOGD(COMP_SVC, "thermal service config init suc:VENDOR_CONFIG");
188         return true;
189     }
190 
191     if (configParser_.ThermalSrvConfigInit(SYSTEM_THERMAL_SERVICE_CONFIG_PATH)) {
192         THERMAL_HILOGD(COMP_SVC, "thermal service config init suc:SYSTEM_CONFIG");
193         return true;
194     }
195 
196     return false;
197 }
198 
199 
InitConfigModule()200 bool ThermalService::InitConfigModule()
201 {
202     THERMAL_HILOGD(COMP_SVC, "InitVendor Enter");
203     if (!CreateConfigModule()) {
204         THERMAL_HILOGD(COMP_SVC, "CreateConfigModule fail");
205     }
206 
207     if (!configParser_.ThermalSrvConfigInit(SYSTEM_THERMAL_SERVICE_CONFIG_PATH)) {
208         THERMAL_HILOGE(COMP_SVC, "system thermal service config init failed.");
209         return false;
210     }
211     serviceConfigParsed = true;
212 
213     THERMAL_HILOGE(COMP_SVC, "system thermal service config init suc.");
214     return true;
215 }
216 
InitModules()217 bool ThermalService::InitModules()
218 {
219     if (!InitConfigFile()) {
220         return false;
221     }
222 
223     if (popup_ == nullptr) {
224         popup_ = std::make_shared<ActionPopup>(POPUP_ACTION_NAME);
225     }
226 
227     if (!InitThermalObserver()) {
228         THERMAL_HILOGE(COMP_SVC, "thermal observer start fail");
229         return false;
230     }
231 
232     if (!InitActionManager()) {
233         THERMAL_HILOGE(COMP_SVC, "action manager init fail");
234         return false;
235     }
236 
237     if (!InitThermalPolicy()) {
238         THERMAL_HILOGE(COMP_SVC, "thermal policy start fail");
239         return false;
240     }
241 
242     if (!InitThermalSubscriber()) {
243         THERMAL_HILOGE(COMP_SVC, "thermal subscriber init fail");
244         return false;
245     }
246     return true;
247 }
248 
InitThermalObserver()249 bool ThermalService::InitThermalObserver()
250 {
251     if (!InitBaseInfo()) {
252         return false;
253     }
254 
255     THERMAL_HILOGD(COMP_SVC, "Enter");
256     if (observer_ == nullptr) {
257         observer_ = std::make_shared<ThermalObserver>();
258         if (!(observer_->Init())) {
259             THERMAL_HILOGE(COMP_SVC, "InitThermalObserver: thermal observer start fail");
260             return false;
261         }
262     }
263     if (info_ == nullptr) {
264         info_ = std::make_shared<ThermalSensorInfo>();
265     }
266     THERMAL_HILOGI(COMP_SVC, "InitThermalObserver: Init Success");
267     return true;
268 }
269 
InitBaseInfo()270 bool ThermalService::InitBaseInfo()
271 {
272     THERMAL_HILOGD(COMP_SVC, "Enter");
273     if (!baseInfo_->Init()) {
274         THERMAL_HILOGE(COMP_SVC, "InitBaseInfo: base info init failed");
275         return false;
276     }
277     return true;
278 }
279 
InitStateMachine()280 bool ThermalService::InitStateMachine()
281 {
282     THERMAL_HILOGD(COMP_SVC, "Enter");
283     if (!state_->Init()) {
284         THERMAL_HILOGE(COMP_SVC, "InitStateMachine: state machine init failed");
285         return false;
286     }
287     return true;
288 }
289 
InitActionManager()290 bool ThermalService::InitActionManager()
291 {
292     THERMAL_HILOGD(COMP_SVC, "Enter");
293     if (!actionMgr_->Init()) {
294         THERMAL_HILOGE(COMP_SVC, "InitActionManager: action manager init failed");
295         return false;
296     }
297     return true;
298 }
299 
InitThermalPolicy()300 bool ThermalService::InitThermalPolicy()
301 {
302     THERMAL_HILOGD(COMP_SVC, "Enter");
303     if (!policy_->Init()) {
304         THERMAL_HILOGE(COMP_SVC, "InitThermalPolicy: policy init failed");
305         return false;
306     }
307     return true;
308 }
309 
InitThermalSubscriber()310 bool ThermalService::InitThermalSubscriber()
311 {
312     if (serviceSubscriber_ == nullptr) {
313         serviceSubscriber_ = std::make_shared<ThermalServiceSubscriber>();
314         if (!(serviceSubscriber_->Init())) {
315             THERMAL_HILOGE(COMP_SVC, "InitThermalSubscriber: thermal subscriber init failed");
316             return false;
317         }
318     }
319     return true;
320 }
321 
OnStop()322 void ThermalService::OnStop()
323 {
324     THERMAL_HILOGD(COMP_SVC, "Enter");
325     if (!ready_) {
326         return;
327     }
328     ready_ = false;
329     isBootCompleted_ = false;
330     RemoveSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
331     if (thermalInterface_) {
332         thermalInterface_->Unregister();
333         thermalInterface_->UnregisterFanCallback();
334         thermalInterface_ = nullptr;
335     }
336     if (hdiServiceMgr_) {
337         hdiServiceMgr_->UnregisterServiceStatusListener(hdiServStatListener_);
338         hdiServiceMgr_ = nullptr;
339     }
340 }
341 
SubscribeThermalTempCallback(const std::vector<std::string> & typeList,const sptr<IThermalTempCallback> & callback)342 bool ThermalService::SubscribeThermalTempCallback(
343     const std::vector<std::string>& typeList, const sptr<IThermalTempCallback>& callback)
344 {
345     if (!Permission::IsSystem()) {
346         return false;
347     }
348     auto uid = IPCSkeleton::GetCallingUid();
349     THERMAL_HILOGI(COMP_SVC, "%{public}s is called by uid=%{public}d", __func__, uid);
350     observer_->SubscribeThermalTempCallback(typeList, callback);
351     return true;
352 }
353 
UnSubscribeThermalTempCallback(const sptr<IThermalTempCallback> & callback)354 bool ThermalService::UnSubscribeThermalTempCallback(const sptr<IThermalTempCallback>& callback)
355 {
356     if (!Permission::IsSystem()) {
357         return false;
358     }
359     auto uid = IPCSkeleton::GetCallingUid();
360     THERMAL_HILOGI(COMP_SVC, "%{public}s is called by uid=%{public}d", __func__, uid);
361     observer_->UnSubscribeThermalTempCallback(callback);
362     return true;
363 }
364 
GetThermalSrvSensorInfo(const SensorType & type,ThermalSrvSensorInfo & sensorInfo)365 bool ThermalService::GetThermalSrvSensorInfo(const SensorType& type, ThermalSrvSensorInfo& sensorInfo)
366 {
367     THERMAL_HILOGD(COMP_SVC, "Enter");
368     if (!(observer_->GetThermalSrvSensorInfo(type, sensorInfo))) {
369         THERMAL_HILOGW(COMP_SVC, "failed to get sensor temp, type enum: %{public}u", static_cast<uint32_t>(type));
370         return false;
371     }
372     return true;
373 }
374 
SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback> & callback)375 bool ThermalService::SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback)
376 {
377     auto uid = IPCSkeleton::GetCallingUid();
378     THERMAL_HILOGI(COMP_SVC, "%{public}s is called by uid=%{public}d", __func__, uid);
379     actionMgr_->SubscribeThermalLevelCallback(callback);
380     return true;
381 }
382 
UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback> & callback)383 bool ThermalService::UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback)
384 {
385     auto uid = IPCSkeleton::GetCallingUid();
386     THERMAL_HILOGI(COMP_SVC, "%{public}s is called by uid=%{public}d", __func__, uid);
387     actionMgr_->UnSubscribeThermalLevelCallback(callback);
388     return true;
389 }
390 
SubscribeThermalActionCallback(const std::vector<std::string> & actionList,const std::string & desc,const sptr<IThermalActionCallback> & callback)391 bool ThermalService::SubscribeThermalActionCallback(
392     const std::vector<std::string>& actionList, const std::string& desc, const sptr<IThermalActionCallback>& callback)
393 {
394     if (!Permission::IsSystem()) {
395         return false;
396     }
397     auto pid = IPCSkeleton::GetCallingPid();
398     auto uid = IPCSkeleton::GetCallingUid();
399     THERMAL_HILOGI(COMP_SVC, "%{public}s is called by pid=%{public}d, uid=%{public}d", __func__, pid, uid);
400     observer_->SubscribeThermalActionCallback(actionList, desc, callback);
401     return true;
402 }
403 
UnSubscribeThermalActionCallback(const sptr<IThermalActionCallback> & callback)404 bool ThermalService::UnSubscribeThermalActionCallback(const sptr<IThermalActionCallback>& callback)
405 {
406     if (!Permission::IsSystem()) {
407         return false;
408     }
409     auto pid = IPCSkeleton::GetCallingPid();
410     auto uid = IPCSkeleton::GetCallingUid();
411     THERMAL_HILOGI(COMP_SVC, "%{public}s is called by pid=%{public}d, uid=%{public}d", __func__, pid, uid);
412     observer_->UnSubscribeThermalActionCallback(callback);
413     return true;
414 }
415 
GetThermalLevel(ThermalLevel & level)416 bool ThermalService::GetThermalLevel(ThermalLevel& level)
417 {
418     uint32_t levelValue = actionMgr_->GetThermalLevel();
419     level = static_cast<ThermalLevel>(levelValue);
420     return true;
421 }
422 
GetThermalInfo()423 bool ThermalService::GetThermalInfo()
424 {
425     THERMAL_HILOGD(COMP_SVC, "Enter");
426     HdfThermalCallbackInfo thermalInfo;
427     bool ret = false;
428     if (thermalInterface_ == nullptr) {
429         thermalInterface_ = IThermalInterface::Get();
430         if (thermalInterface_ == nullptr) {
431             THERMAL_HILOGD(COMP_SVC, "thermalInterface_ is nullptr");
432             return ret;
433         }
434     }
435 
436     if (thermalInterface_ != nullptr) {
437         int32_t res = thermalInterface_->GetThermalZoneInfo(thermalInfo);
438         HandleThermalCallbackEvent(thermalInfo);
439         if (!res) {
440             ret = true;
441         }
442     }
443     return ret;
444 }
445 
SetScene(const std::string & scene)446 bool ThermalService::SetScene(const std::string& scene)
447 {
448     if (!Permission::IsSystem()) {
449         return false;
450     }
451     scene_ = scene;
452     return true;
453 }
454 
UpdateThermalState(const std::string & tag,const std::string & val,bool isImmed)455 bool ThermalService::UpdateThermalState(const std::string& tag, const std::string& val, bool isImmed)
456 {
457     if (!Permission::IsSystem()) {
458         return false;
459     }
460     THERMAL_HILOGI(COMP_SVC, "tag %{public}s, val %{public}s, isImmed %{public}d",
461         tag.c_str(), val.c_str(), isImmed);
462     std::lock_guard<std::mutex> lock(mutex_);
463     state_->UpdateState(tag, val);
464     if (isImmed) {
465         policy_->ExecutePolicy();
466     }
467     return true;
468 }
469 
RegisterHdiStatusListener()470 void ThermalService::RegisterHdiStatusListener()
471 {
472     THERMAL_HILOGD(COMP_SVC, "Enter");
473     hdiServiceMgr_ = IServiceManager::Get();
474     if (hdiServiceMgr_ == nullptr) {
475         FFRTTask retryTask = [this] {
476             return RegisterHdiStatusListener();
477         };
478         FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, g_queue);
479         THERMAL_HILOGW(COMP_SVC, "hdi service manager is nullptr, try again after %{public}u ms", RETRY_TIME);
480         return;
481     }
482 
483     hdiServStatListener_ = new HdiServiceStatusListener(
484         HdiServiceStatusListener::StatusCallback([&](const OHOS::HDI::ServiceManager::V1_0::ServiceStatus& status) {
485             THERMAL_RETURN_IF(status.serviceName != HDI_SERVICE_NAME || status.deviceClass != DEVICE_CLASS_DEFAULT)
486 
487             if (status.status == SERVIE_STATUS_START) {
488                 FFRTTask task = [this] {
489                     RegisterThermalHdiCallback();
490                     RegisterFanHdiCallback();
491                 };
492                 FFRTUtils::SubmitTask(task);
493                 THERMAL_HILOGD(COMP_SVC, "thermal interface service start");
494             } else if (status.status == SERVIE_STATUS_STOP && thermalInterface_) {
495                 thermalInterface_->Unregister();
496                 thermalInterface_->UnregisterFanCallback();
497                 thermalInterface_ = nullptr;
498                 THERMAL_HILOGW(COMP_SVC, "thermal interface service, unregister interface");
499             }
500         }));
501 
502     int32_t status = hdiServiceMgr_->RegisterServiceStatusListener(hdiServStatListener_, DEVICE_CLASS_DEFAULT);
503     if (status != ERR_OK) {
504         THERMAL_HILOGW(COMP_SVC, "Register hdi failed, try again after %{public}u ms", RETRY_TIME);
505         FFRTTask retryTask = [this] {
506             return RegisterHdiStatusListener();
507         };
508         FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, g_queue);
509     }
510 }
511 
RegisterThermalHdiCallback()512 void ThermalService::RegisterThermalHdiCallback()
513 {
514     THERMAL_HILOGD(COMP_SVC, "register thermal hdi callback");
515     if (thermalInterface_ == nullptr) {
516         thermalInterface_ = IThermalInterface::Get();
517         THERMAL_RETURN_IF_WITH_LOG(thermalInterface_ == nullptr, "failed to get thermal hdi interface");
518     }
519 
520     sptr<IThermalCallback> callback = new ThermalCallback();
521     ThermalCallback::ThermalEventCallback eventCb =
522         [this](const HdfThermalCallbackInfo& event) -> int32_t { return this->HandleThermalCallbackEvent(event); };
523     ThermalCallback::RegisterThermalEvent(eventCb);
524     int32_t ret = thermalInterface_->Register(callback);
525     THERMAL_HILOGI(COMP_SVC, "register thermal hdi callback end, ret: %{public}d", ret);
526 }
527 
UnRegisterThermalHdiCallback()528 void ThermalService::UnRegisterThermalHdiCallback()
529 {
530     if (thermalInterface_ == nullptr) {
531         FFRTTask retryTask = [this] {
532             return UnRegisterThermalHdiCallback();
533         };
534         FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, g_queue);
535         THERMAL_HILOGW(COMP_SVC, "thermalInterface_ is nullptr, try again after %{public}u ms", RETRY_TIME);
536         return;
537     }
538     int32_t ret = thermalInterface_->Unregister();
539     THERMAL_HILOGI(COMP_SVC, "unregister thermal hdi callback end, ret: %{public}d", ret);
540 }
541 
HandleThermalCallbackEvent(const HdfThermalCallbackInfo & event)542 int32_t ThermalService::HandleThermalCallbackEvent(const HdfThermalCallbackInfo& event)
543 {
544 #ifndef THERMAL_USER_VERSION
545     if (!isTempReport_) {
546         return ERR_OK;
547     }
548 #endif
549     TypeTempMap typeTempMap;
550     if (!event.info.empty()) {
551         for (auto iter = event.info.begin(); iter != event.info.end(); iter++) {
552             typeTempMap.insert(std::make_pair(iter->type, iter->temp));
553         }
554     }
555     std::lock_guard<std::mutex> lock(mutex_);
556     serviceSubscriber_->OnTemperatureChanged(typeTempMap);
557     return ERR_OK;
558 }
559 
HandleTempEmulation(const TypeTempMap & typeTempMap)560 bool ThermalService::HandleTempEmulation(const TypeTempMap& typeTempMap)
561 {
562     if (isTempReport_) {
563         return false;
564     }
565     std::lock_guard<std::mutex> lock(mutex_);
566     serviceSubscriber_->OnTemperatureChanged(typeTempMap);
567     return true;
568 }
569 
RegisterFanHdiCallback()570 void ThermalService::RegisterFanHdiCallback()
571 {
572     if (!fanFaultDetect_->HasFanConfig()) {
573         return;
574     }
575 
576     if (thermalInterface_ == nullptr) {
577         thermalInterface_ = IThermalInterface::Get();
578         THERMAL_RETURN_IF_WITH_LOG(thermalInterface_ == nullptr, "failed to get thermal hdi interface");
579     }
580 
581     sptr<IFanCallback> callback = new FanCallback();
582     FanCallback::FanEventCallback eventCb =
583         [this](const HdfThermalCallbackInfo& event) -> int32_t { return this->HandleFanCallbackEvent(event); };
584     FanCallback::RegisterFanEvent(eventCb);
585     int32_t ret = thermalInterface_->RegisterFanCallback(callback);
586     THERMAL_HILOGI(COMP_SVC, "register fan hdi callback end, ret: %{public}d", ret);
587 
588     return;
589 }
590 
HandleFanCallbackEvent(const HdfThermalCallbackInfo & event)591 int32_t ThermalService::HandleFanCallbackEvent(const HdfThermalCallbackInfo& event)
592 {
593     FanSensorInfo report;
594     if (!event.info.empty()) {
595         for (auto iter = event.info.begin(); iter != event.info.end(); iter++) {
596             report.insert(std::make_pair(iter->type, iter->temp));
597         }
598     }
599 
600     fanFaultDetect_->OnFanSensorInfoChanged(report);
601     return ERR_OK;
602 }
603 
ShellDump(const std::vector<std::string> & args,uint32_t argc)604 std::string ThermalService::ShellDump(const std::vector<std::string>& args, uint32_t argc)
605 {
606     if (!Permission::IsSystem() || !isBootCompleted_) {
607         return "";
608     }
609     std::lock_guard<std::mutex> lock(mutex_);
610     pid_t pid = IPCSkeleton::GetCallingPid();
611     THERMAL_HILOGI(COMP_SVC, "PID: %{public}d", pid);
612     std::string result;
613     bool ret = ThermalMgrDumper::Dump(args, result);
614     THERMAL_HILOGI(COMP_SVC, "ThermalMgrDumper :%{public}d", ret);
615     return result;
616 }
617 
Dump(int fd,const std::vector<std::u16string> & args)618 int32_t ThermalService::Dump(int fd, const std::vector<std::u16string>& args)
619 {
620     if (!isBootCompleted_) {
621         return ERR_NO_INIT;
622     }
623     if (!Permission::IsSystem()) {
624         return ERR_PERMISSION_DENIED;
625     }
626     std::vector<std::string> argsInStr;
627     std::transform(args.begin(), args.end(), std::back_inserter(argsInStr), [](const std::u16string& arg) {
628         std::string ret = Str16ToStr8(arg);
629         THERMAL_HILOGI(COMP_SVC, "arg: %{public}s", ret.c_str());
630         return ret;
631     });
632     std::string result;
633     ThermalMgrDumper::Dump(argsInStr, result);
634     if (!SaveStringToFd(fd, result)) {
635         THERMAL_HILOGE(COMP_SVC, "ThermalService::Dump failed, save to fd failed.");
636         THERMAL_HILOGE(COMP_SVC, "Dump Info:\n");
637         THERMAL_HILOGE(COMP_SVC, "%{public}s", result.c_str());
638         return ERR_OK;
639     }
640     return ERR_OK;
641 }
642 
EnableMock(const std::string & actionName,void * mockObject)643 void ThermalService::EnableMock(const std::string& actionName, void* mockObject)
644 {
645     std::lock_guard<std::mutex> lock(mutex_);
646     actionMgr_->EnableMock(actionName, mockObject);
647 }
648 
DestroyInstance()649 void ThermalService::DestroyInstance()
650 {
651     std::lock_guard<std::mutex> lock(singletonMutex_);
652     if (instance_) {
653         instance_.clear();
654         instance_ = nullptr;
655     }
656 }
657 } // namespace PowerMgr
658 } // namespace OHOS
659