• 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 "stdexcept"
17 #include "thermal_mgr_client.h"
18 #include "thermal_level_callback.h"
19 #include "thermal_log.h"
20 #include "refbase.h"
21 
22 using namespace ohos::thermal;
23 using namespace OHOS;
24 using namespace OHOS::PowerMgr;
25 
26 namespace {
27 thread_local auto& g_thermalMgrClient = ThermalMgrClient::GetInstance();
28 thread_local sptr<ThermalLevelCallback> g_thermalLevelCallback = new (std::nothrow) ThermalLevelCallback();
29 
RegisterThermalLevelCallback(callback_view<void (ohos::thermal::ThermalLevel)> thermalCb)30 void RegisterThermalLevelCallback(callback_view<void(ohos::thermal::ThermalLevel)> thermalCb)
31 {
32     THERMAL_HILOGD(COMP_FWK, "ets RegisterThermalLevelCallback interface");
33     if (g_thermalLevelCallback == nullptr) {
34         THERMAL_HILOGE(COMP_FWK, "g_thermalLevelCallback is nullptr");
35         return;
36     }
37     g_thermalLevelCallback->UpdateCallback(thermalCb);
38     g_thermalMgrClient.SubscribeThermalLevelCallback(g_thermalLevelCallback);
39 }
40 
UnregisterThermalLevelCallback(optional_view<callback<void (MyUndefined const &)>> thermalCb)41 void UnregisterThermalLevelCallback(optional_view<callback<void(MyUndefined const&)>> thermalCb)
42 {
43     THERMAL_HILOGD(COMP_FWK, "ets UnregisterThermalLevelCallback interface");
44     if (g_thermalLevelCallback == nullptr) {
45         THERMAL_HILOGE(COMP_FWK, "g_thermalLevelCallback is nullptr");
46         return;
47     }
48     g_thermalMgrClient.UnSubscribeThermalLevelCallback(g_thermalLevelCallback);
49     if (thermalCb) {
50         THERMAL_HILOGI(COMP_FWK, "UnregisterThermalLevelCallback thermalCb start");
51         (*thermalCb)(MyUndefined::make_Undefined());
52     } else {
53         THERMAL_HILOGI(COMP_FWK, "UnregisterThermalLevelCallback thermalCb is nullptr");
54     }
55 }
56 
GetLevel()57 ohos::thermal::ThermalLevel GetLevel()
58 {
59     THERMAL_HILOGD(COMP_FWK, "ets GetLevel interface");
60     OHOS::PowerMgr::ThermalLevel level = g_thermalMgrClient.GetThermalLevel();
61     ohos::thermal::ThermalLevel levelValue = static_cast<ohos::thermal::ThermalLevel::key_t>(level);
62     THERMAL_HILOGI(COMP_FWK, "level is %{public}d", static_cast<int32_t>(levelValue));
63     return levelValue;
64 }
65 }  // namespace
66 
67 // Since these macros are auto-generate, lint will cause false positive
68 // NOLINTBEGIN
69 TH_EXPORT_CPP_API_RegisterThermalLevelCallback(RegisterThermalLevelCallback);
70 TH_EXPORT_CPP_API_UnregisterThermalLevelCallback(UnregisterThermalLevelCallback);
71 TH_EXPORT_CPP_API_GetLevel(GetLevel);
72 // NOLINTEND