1 /*
2 * Copyright (C) 2021 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 #include <iostream>
16 #include "include/sp_utils.h"
17 #include <dirent.h>
18 #include "include/Temperature.h"
19 #include "include/sp_log.h"
20 namespace OHOS {
21 namespace SmartPerf {
ItemData()22 std::map<std::string, std::string> Temperature::ItemData()
23 {
24 DIR *dp = opendir(thermalBasePath.c_str());
25 struct dirent *dirp;
26 std::vector<std::string> dirs;
27 if (dp == nullptr) {
28 std::cout << "Open directory failed!" << std::endl;
29 }
30 while ((dirp = readdir(dp)) != nullptr) {
31 if (strcmp(dirp->d_name, ".") != 0 && strcmp(dirp->d_name, "..") != 0) {
32 dirs.push_back(SPUtils::IncludePathDelimiter(thermalBasePath) + std::string(dirp->d_name));
33 }
34 }
35 closedir(dp);
36 std::map<std::string, std::string> result;
37 for (auto dir : dirs) {
38 std::string dirType = dir + "/type";
39 LOGI("dirType====: %s", dirType.c_str());
40 std::string dirTemp = dir + "/temp";
41 LOGI("dirTemp====: %s", dirTemp.c_str());
42
43 if (!SPUtils::FileAccess(dirType)) {
44 continue;
45 }
46
47 std::string type;
48 std::string temp;
49 SPUtils::LoadFile(dirType, type);
50 SPUtils::LoadFile(dirTemp, temp);
51 GetTempInfos(result, type, temp);
52 }
53
54 LOGI("Temperature::ItemData map size(%u)", result.size());
55 return result;
56 }
57
GetTempInfos(std::map<std::string,std::string> & result,std::string type,std::string temp)58 void Temperature::GetTempInfos(std::map<std::string, std::string> &result, std::string type, std::string temp)
59 {
60 for (auto node : collectNodes) {
61 if (type.find(node) != std::string::npos) {
62 LOGI("type: %s", type.c_str());
63 float t = std::stof(temp);
64 LOGI("temp: %s", temp.c_str());
65 if (node == "gpu") {
66 result[type] = std::to_string(t);
67 } else {
68 result[type] = std::to_string(t / 1e3);
69 }
70 }
71 }
72 }
73 }
74 }
75