1 /* 2 * Copyright (c) 2022 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 #ifndef THERMAL_ZONE_MANAGER_H 17 #define THERMAL_ZONE_MANAGER_H 18 19 #include <list> 20 #include <map> 21 #include <string> 22 #include <mutex> 23 #include "thermal_hdf_config.h" 24 #include "v1_0/thermal_types.h" 25 26 namespace OHOS { 27 namespace HDI { 28 namespace Thermal { 29 namespace V1_0 { 30 struct ThermalZoneSysfsPathInfo { 31 char* name; 32 char temperturePath[PATH_MAX]; 33 char typePath[PATH_MAX]; 34 int32_t fd; 35 }; 36 37 struct ThermalSysfsPathInfo { 38 char* name; 39 char thermalZonePath[PATH_MAX]; 40 char coolingDevicePath[PATH_MAX]; 41 int32_t fd; 42 }; 43 44 class ThermalZoneManager { 45 public: 46 ThermalZoneManager() = default; 47 ~ThermalZoneManager() = default; 48 GetTzPathInfo()49 ThermalZoneSysfsPathInfo GetTzPathInfo() 50 { 51 return tzSysPathInfo_; 52 }; 53 SetTzPathInfo(ThermalZoneSysfsPathInfo tzSysPathInfo)54 void SetTzPathInfo(ThermalZoneSysfsPathInfo tzSysPathInfo) 55 { 56 tzSysPathInfo_ = tzSysPathInfo; 57 } 58 GetLTZPathInfo()59 std::list<ThermalZoneSysfsPathInfo> GetLTZPathInfo() 60 { 61 return lTzSysPathInfo_; 62 } 63 GetlTzInfo()64 std::vector<ThermalZoneInfo> GetlTzInfo() 65 { 66 return tzInfoList_; 67 } 68 ConvertInt(const std::string & value)69 int32_t ConvertInt(const std::string &value) 70 { 71 return std::stoi(value.c_str()); 72 } 73 GetCallbackInfo()74 HdfThermalCallbackInfo GetCallbackInfo() 75 { 76 std::lock_guard<std::mutex> lock(mutex_); 77 return tzInfoAcaualEvent_; 78 } 79 80 int32_t InitThermalZoneSysfs(); 81 int32_t ParseThermalZoneInfo(); 82 void Trim(char* str) const; 83 int32_t ReadThermalSysfsToBuff(const char* path, char* buf, size_t size) const; 84 int32_t ReadSysfsFile(const char* path, char* buf, size_t size) const; 85 void FormatThermalSysfsPaths(struct ThermalSysfsPathInfo *pTSysPathInfo); 86 void FormatThermalPaths(char *path, size_t size, const char *format, const char* name); 87 void ClearThermalZoneInfo(); 88 int32_t GetMaxCommonDivisor(int32_t a, int32_t b); 89 int32_t GetIntervalCommonDivisor(std::vector<int32_t> intervalList); 90 void SetMultiples(); 91 void CalculateMaxCd(); 92 int32_t ReadFile(const char *path, char *buf, size_t size); 93 void ReportThermalZoneData(int32_t reportTime, std::vector<int32_t> &multipleList); 94 int32_t maxCd_; 95 private: 96 int32_t UpdateThermalZoneData(std::map<std::string, std::string> &tzPathMap); 97 void UpdateDataType(XMLThermalZoneInfo& tzIter, ReportedThermalData& data); 98 ThermalHdfConfig::ThermalTypeMap sensorTypeMap_; 99 HdfThermalCallbackInfo tzInfoAcaualEvent_; 100 struct ThermalZoneSysfsPathInfo tzSysPathInfo_; 101 std::list<ThermalZoneSysfsPathInfo> lTzSysPathInfo_; 102 std::vector<ThermalZoneInfo> tzInfoList_; 103 std::mutex mutex_; 104 bool flag_ {false}; 105 }; 106 } // V1_0 107 } // Thermal 108 } // HDI 109 } // OHOS 110 #endif // THERMAL_ZONE_MANAGER_H 111