• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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", tag.c_str(), val.c_str());
461     std::lock_guard<std::mutex> lock(mutex_);
462     state_->UpdateState(tag, val);
463     if (isImmed) {
464         policy_->ExecutePolicy();
465     }
466     return true;
467 }
468 
RegisterHdiStatusListener()469 void ThermalService::RegisterHdiStatusListener()
470 {
471     THERMAL_HILOGD(COMP_SVC, "Enter");
472     hdiServiceMgr_ = IServiceManager::Get();
473     if (hdiServiceMgr_ == nullptr) {
474         FFRTTask retryTask = [this] {
475             return RegisterHdiStatusListener();
476         };
477         FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, g_queue);
478         THERMAL_HILOGW(COMP_SVC, "hdi service manager is nullptr, try again after %{public}u ms", RETRY_TIME);
479         return;
480     }
481 
482     hdiServStatListener_ = new HdiServiceStatusListener(
483         HdiServiceStatusListener::StatusCallback([&](const OHOS::HDI::ServiceManager::V1_0::ServiceStatus& status) {
484             THERMAL_RETURN_IF(status.serviceName != HDI_SERVICE_NAME || status.deviceClass != DEVICE_CLASS_DEFAULT)
485 
486             if (status.status == SERVIE_STATUS_START) {
487                 FFRTTask task = [this] {
488                     RegisterThermalHdiCallback();
489                     RegisterFanHdiCallback();
490                 };
491                 FFRTUtils::SubmitTask(task);
492                 THERMAL_HILOGD(COMP_SVC, "thermal interface service start");
493             } else if (status.status == SERVIE_STATUS_STOP && thermalInterface_) {
494                 thermalInterface_->Unregister();
495                 thermalInterface_->UnregisterFanCallback();
496                 thermalInterface_ = nullptr;
497                 THERMAL_HILOGW(COMP_SVC, "thermal interface service, unregister interface");
498             }
499         }));
500 
501     int32_t status = hdiServiceMgr_->RegisterServiceStatusListener(hdiServStatListener_, DEVICE_CLASS_DEFAULT);
502     if (status != ERR_OK) {
503         THERMAL_HILOGW(COMP_SVC, "Register hdi failed, try again after %{public}u ms", RETRY_TIME);
504         FFRTTask retryTask = [this] {
505             return RegisterHdiStatusListener();
506         };
507         FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, g_queue);
508     }
509 }
510 
RegisterThermalHdiCallback()511 void ThermalService::RegisterThermalHdiCallback()
512 {
513     THERMAL_HILOGD(COMP_SVC, "register thermal hdi callback");
514     if (thermalInterface_ == nullptr) {
515         thermalInterface_ = IThermalInterface::Get();
516         THERMAL_RETURN_IF_WITH_LOG(thermalInterface_ == nullptr, "failed to get thermal hdi interface");
517     }
518 
519     sptr<IThermalCallback> callback = new ThermalCallback();
520     ThermalCallback::ThermalEventCallback eventCb =
521         [this](const HdfThermalCallbackInfo& event) -> int32_t { return this->HandleThermalCallbackEvent(event); };
522     ThermalCallback::RegisterThermalEvent(eventCb);
523     int32_t ret = thermalInterface_->Register(callback);
524     THERMAL_HILOGI(COMP_SVC, "register thermal hdi callback end, ret: %{public}d", ret);
525 }
526 
UnRegisterThermalHdiCallback()527 void ThermalService::UnRegisterThermalHdiCallback()
528 {
529     if (thermalInterface_ == nullptr) {
530         FFRTTask retryTask = [this] {
531             return UnRegisterThermalHdiCallback();
532         };
533         FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, g_queue);
534         THERMAL_HILOGW(COMP_SVC, "thermalInterface_ is nullptr, try again after %{public}u ms", RETRY_TIME);
535         return;
536     }
537     int32_t ret = thermalInterface_->Unregister();
538     THERMAL_HILOGI(COMP_SVC, "unregister thermal hdi callback end, ret: %{public}d", ret);
539 }
540 
HandleThermalCallbackEvent(const HdfThermalCallbackInfo & event)541 int32_t ThermalService::HandleThermalCallbackEvent(const HdfThermalCallbackInfo& event)
542 {
543 #ifndef THERMAL_USER_VERSION
544     if (!isTempReport_) {
545         return ERR_OK;
546     }
547 #endif
548     TypeTempMap typeTempMap;
549     if (!event.info.empty()) {
550         for (auto iter = event.info.begin(); iter != event.info.end(); iter++) {
551             typeTempMap.insert(std::make_pair(iter->type, iter->temp));
552         }
553     }
554     std::lock_guard<std::mutex> lock(mutex_);
555     serviceSubscriber_->OnTemperatureChanged(typeTempMap);
556     return ERR_OK;
557 }
558 
HandleTempEmulation(const TypeTempMap & typeTempMap)559 bool ThermalService::HandleTempEmulation(const TypeTempMap& typeTempMap)
560 {
561     if (isTempReport_) {
562         return false;
563     }
564     std::lock_guard<std::mutex> lock(mutex_);
565     serviceSubscriber_->OnTemperatureChanged(typeTempMap);
566     return true;
567 }
568 
RegisterFanHdiCallback()569 void ThermalService::RegisterFanHdiCallback()
570 {
571     if (!fanFaultDetect_->HasFanConfig()) {
572         return;
573     }
574 
575     if (thermalInterface_ == nullptr) {
576         thermalInterface_ = IThermalInterface::Get();
577         THERMAL_RETURN_IF_WITH_LOG(thermalInterface_ == nullptr, "failed to get thermal hdi interface");
578     }
579 
580     sptr<IFanCallback> callback = new FanCallback();
581     FanCallback::FanEventCallback eventCb =
582         [this](const HdfThermalCallbackInfo& event) -> int32_t { return this->HandleFanCallbackEvent(event); };
583     FanCallback::RegisterFanEvent(eventCb);
584     int32_t ret = thermalInterface_->RegisterFanCallback(callback);
585     THERMAL_HILOGI(COMP_SVC, "register fan hdi callback end, ret: %{public}d", ret);
586 
587     return;
588 }
589 
HandleFanCallbackEvent(const HdfThermalCallbackInfo & event)590 int32_t ThermalService::HandleFanCallbackEvent(const HdfThermalCallbackInfo& event)
591 {
592     FanSensorInfo report;
593     if (!event.info.empty()) {
594         for (auto iter = event.info.begin(); iter != event.info.end(); iter++) {
595             report.insert(std::make_pair(iter->type, iter->temp));
596         }
597     }
598 
599     fanFaultDetect_->OnFanSensorInfoChanged(report);
600     return ERR_OK;
601 }
602 
ShellDump(const std::vector<std::string> & args,uint32_t argc)603 std::string ThermalService::ShellDump(const std::vector<std::string>& args, uint32_t argc)
604 {
605     if (!Permission::IsSystem() || !isBootCompleted_) {
606         return "";
607     }
608     std::lock_guard<std::mutex> lock(mutex_);
609     pid_t pid = IPCSkeleton::GetCallingPid();
610     THERMAL_HILOGI(COMP_SVC, "PID: %{public}d", pid);
611     std::string result;
612     bool ret = ThermalMgrDumper::Dump(args, result);
613     THERMAL_HILOGI(COMP_SVC, "ThermalMgrDumper :%{public}d", ret);
614     return result;
615 }
616 
Dump(int fd,const std::vector<std::u16string> & args)617 int32_t ThermalService::Dump(int fd, const std::vector<std::u16string>& args)
618 {
619     if (!isBootCompleted_) {
620         return ERR_NO_INIT;
621     }
622     if (!Permission::IsSystem()) {
623         return ERR_PERMISSION_DENIED;
624     }
625     std::vector<std::string> argsInStr;
626     std::transform(args.begin(), args.end(), std::back_inserter(argsInStr), [](const std::u16string& arg) {
627         std::string ret = Str16ToStr8(arg);
628         THERMAL_HILOGI(COMP_SVC, "arg: %{public}s", ret.c_str());
629         return ret;
630     });
631     std::string result;
632     ThermalMgrDumper::Dump(argsInStr, result);
633     if (!SaveStringToFd(fd, result)) {
634         THERMAL_HILOGE(COMP_SVC, "ThermalService::Dump failed, save to fd failed.");
635         THERMAL_HILOGE(COMP_SVC, "Dump Info:\n");
636         THERMAL_HILOGE(COMP_SVC, "%{public}s", result.c_str());
637         return ERR_OK;
638     }
639     return ERR_OK;
640 }
641 
EnableMock(const std::string & actionName,void * mockObject)642 void ThermalService::EnableMock(const std::string& actionName, void* mockObject)
643 {
644     std::lock_guard<std::mutex> lock(mutex_);
645     actionMgr_->EnableMock(actionName, mockObject);
646 }
647 
DestroyInstance()648 void ThermalService::DestroyInstance()
649 {
650     std::lock_guard<std::mutex> lock(singletonMutex_);
651     if (instance_) {
652         instance_.clear();
653         instance_ = nullptr;
654     }
655 }
656 } // namespace PowerMgr
657 } // namespace OHOS
658