• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_collector_impl.h"
17 
18 #include <string>
19 #include <map>
20 
21 #include "common_util.h"
22 #include "file_util.h"
23 #include "hiview_logger.h"
24 #include "thermal_decorator.h"
25 #ifdef THERMAL_MANAGER_ENABLE
26 #include "thermal_mgr_client.h"
27 #endif
28 
29 using namespace OHOS::HiviewDFX::UCollect;
30 
31 namespace OHOS {
32 namespace HiviewDFX {
33 namespace UCollectUtil {
34 namespace {
35 DEFINE_LOG_TAG("ThermalCollector");
36 constexpr char THERMAL_PATH[] = "/sys/class/thermal/";
37 
GetThermalType(const std::string & thermalZone)38 std::string GetThermalType(const std::string& thermalZone)
39 {
40     std::string tmp;
41     if (!FileUtil::LoadStringFromFile(thermalZone + "/type", tmp)) {
42         HIVIEW_LOGW("read node failed");
43         return "";
44     }
45     std::stringstream ss(tmp); // tmp str load from node may have a newline character at the end
46     std::string type;
47     ss >> type; // type str without newline character
48     return type;
49 }
50 
GetThermalValue(const std::string & thermalZone)51 int32_t GetThermalValue(const std::string& thermalZone)
52 {
53     return CommonUtil::ReadNodeWithOnlyNumber(thermalZone + "/temp");
54 }
55 
GetZoneTypeStr(ThermalZone thermalZone)56 std::string GetZoneTypeStr(ThermalZone thermalZone)
57 {
58     const std::map<ThermalZone, std::string> thermalZoneMap = {
59         {SHELL_FRONT, "shell_front"},
60         {SHELL_FRAME, "shell_frame"},
61         {SHELL_BACK, "shell_back"},
62         {SOC_THERMAL, "soc_thermal"},
63         {SYSTEM, "system_h"}
64     };
65     auto it = thermalZoneMap.find(thermalZone);
66     return it != thermalZoneMap.end() ? it->second : "";
67 }
68 
GetZonePathByType(const std::vector<std::string> & thermalZones,const std::string & zoneTypeStr)69 std::string GetZonePathByType(const std::vector<std::string>& thermalZones, const std::string& zoneTypeStr)
70 {
71     for (const auto& zone : thermalZones) {
72         if (zoneTypeStr == GetThermalType(zone)) {
73             return zone;
74         }
75     }
76     return "";
77 }
78 }
79 
Create()80 std::shared_ptr<ThermalCollector> ThermalCollector::Create()
81 {
82     return std::make_shared<ThermalDecorator>(std::make_shared<ThermalCollectorImpl>());
83 }
84 
CollectDevThermal(ThermalZone thermalZone)85 CollectResult<int32_t> ThermalCollectorImpl::CollectDevThermal(ThermalZone thermalZone)
86 {
87     CollectResult<int32_t> result;
88     std::string zoneTypeStr = GetZoneTypeStr(thermalZone);
89     if (zoneTypeStr.empty()) {
90         return result;
91     }
92     std::vector<std::string> thermalZones;
93     FileUtil::GetDirFiles(THERMAL_PATH, thermalZones);
94     std::string zonePath = GetZonePathByType(thermalZones, zoneTypeStr);
95     if (zonePath.empty()) {
96         return result;
97     }
98     result.data = GetThermalValue(zonePath);
99     result.retCode = UcError::SUCCESS;
100     return result;
101 }
102 
CollectThermaLevel()103 CollectResult<uint32_t> ThermalCollectorImpl::CollectThermaLevel()
104 {
105     CollectResult<uint32_t> result;
106 #ifdef THERMAL_MANAGER_ENABLE
107     auto& thermalMgrClient = PowerMgr::ThermalMgrClient::GetInstance();
108     PowerMgr::ThermalLevel thermalLevel = thermalMgrClient.GetThermalLevel();
109     result.retCode = UcError::SUCCESS;
110     result.data = static_cast<uint32_t>(thermalLevel);
111 #endif
112     return result;
113 }
114 } // namespace UCollectUtil
115 } // namespace HiviewDFX
116 } // namespace OHOS
117