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 "typec_port_info.h"
17 
18 #include <dirent.h>
19 #include <fstream>
20 #include <string>
21 #include <sys/types.h>
22 
23 namespace OHOS::Rosen {
GetDirList()24 static std::vector<std::string> GetDirList()
25 {
26     DIR* dir = opendir("sys/class/thermal");
27     if (dir == nullptr) {
28         return {};
29     }
30 
31     std::vector<std::string> allPath;
32     struct dirent* entry;
33     while ((entry = readdir(dir)) != nullptr) {
34         std::string name = entry->d_name;
35         if (name.find("thermal_zone") == 0) {
36             allPath.emplace_back("sys/class/thermal/" + name);
37         }
38     }
39     closedir(dir);
40     return allPath;
41 }
42 
IsStringNumeric(const char * buffer)43 static bool IsStringNumeric(const char* buffer)
44 {
45     auto begin = buffer;
46     while (*begin) {
47         if (!std::isdigit(*begin)) {
48             return false;
49         }
50         begin++;
51     }
52     return true;
53 }
54 
GetTypeCFileNode(const std::vector<std::string> & allDirName,std::string & typeCMappingPath)55 static bool GetTypeCFileNode(const std::vector<std::string>& allDirName, std::string& typeCMappingPath)
56 {
57     for (const auto& dir : allDirName) {
58         std::string absoluteTypePath = dir + "/type";
59         char realPathResult[PATH_MAX] = { 0 };
60         if (!realpath(absoluteTypePath.c_str(), realPathResult)) {
61             return false;
62         }
63 
64         std::ifstream srcFile(realPathResult, std::ifstream::in);
65         if (!srcFile.is_open()) {
66             continue;
67         }
68         constexpr int bufferSize = 20;
69         char buffer[bufferSize] = {0};
70         srcFile.getline(buffer, bufferSize);
71         if (strcmp(buffer, "usb_port") == 0) {
72             typeCMappingPath = dir + "/temp";
73             return true;
74         }
75     }
76     return false;
77 }
78 
GetTypeCThermal(int32_t & thermal)79 bool TypeCPortInfo::GetTypeCThermal(int32_t& thermal)
80 {
81     static std::string typeCMappingPath;
82     if (typeCMappingPath.empty()) {
83         std::vector<std::string> allDirName = GetDirList();
84         if (allDirName.empty()) {
85             return false;
86         }
87         if (!GetTypeCFileNode(allDirName, typeCMappingPath)) {
88             return false;
89         }
90     }
91 
92     char realPathResult[PATH_MAX] = { 0 };
93     if (!realpath(typeCMappingPath.c_str(), realPathResult)) {
94         return false;
95     }
96 
97     std::ifstream typeCThermalFile(realPathResult, std::ifstream::in);
98     if (!typeCThermalFile.is_open()) {
99         return false;
100     }
101     constexpr int bufferSize = 20;
102     char buffer[bufferSize] = {0};
103     typeCThermalFile.getline(buffer, bufferSize);
104     if (!IsStringNumeric(buffer)) {
105         return false;
106     }
107     thermal = std::stoi(buffer);
108     return true;
109 }
110 } // namespace OHOS::Rosen