• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 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 "cj_thermal_manager.h"
17 #include "thermal_common.h"
18 #include "thermal_level_info.h"
19 #include "cj_lambda.h"
20 #include "thermal_log.h"
21 
22 using namespace OHOS;
23 using namespace OHOS::PowerMgr;
~CjThermalLevelCallback()24 CjThermalLevelCallback::~CjThermalLevelCallback()
25 {
26     ReleaseCallback();
27 }
28 
UpdateCallback(int64_t id)29 void CjThermalLevelCallback::UpdateCallback(int64_t id)
30 {
31     auto callback = reinterpret_cast<void(*)(int32_t)>(id);
32     callbackFunc_ = CJLambda::Create(callback);
33 }
34 
ReleaseCallback()35 void CjThermalLevelCallback::ReleaseCallback()
36 {
37     callbackFunc_ = nullptr;
38 }
39 
OnThermalLevelChanged(ThermalLevel level)40 bool CjThermalLevelCallback::OnThermalLevelChanged(ThermalLevel level)
41 {
42     if (callbackFunc_) {
43         callbackFunc_(static_cast<int32_t>(level));
44     }
45     return true;
46 }
47 
48 
GetThreadLocalInstance()49 CjThermalManager& CjThermalManager::GetThreadLocalInstance()
50 {
51     thread_local CjThermalManager thermalMgr;
52     return thermalMgr;
53 }
54 
GetThermalLevel()55 int32_t CjThermalManager::GetThermalLevel()
56 {
57     ThermalLevel level = thermalMgrClient_.GetThermalLevel();
58     return static_cast<int32_t>(level);
59 }
60 
SubscribeThermalLevel(int64_t id)61 void CjThermalManager::SubscribeThermalLevel(int64_t id)
62 {
63     thermalLevelCallback_->UpdateCallback(id);
64     thermalMgrClient_.SubscribeThermalLevelCallback(thermalLevelCallback_);
65 }
66 
UnSubscribeThermalLevel(int64_t id)67 void CjThermalManager::UnSubscribeThermalLevel(int64_t id)
68 {
69     thermalLevelCallback_->ReleaseCallback();
70     thermalMgrClient_.UnSubscribeThermalLevelCallback(thermalLevelCallback_);
71     if (id != 0) {
72         auto callback = reinterpret_cast<void(*)()>(id);
73         CJLambda::Create(callback)();
74     }
75 }