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"
20namespace OHOS {
21namespace SmartPerf {
22std::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            std::string filename(dirp->d_name);
33            if (filename.find("cooling") == std::string::npos) {
34                dirs.push_back(SPUtils::IncludePathDelimiter(thermalBasePath) + filename);
35            }
36        }
37    }
38    closedir(dp);
39    std::map<std::string, std::string> result;
40    for (auto dir : dirs) {
41        std::string dirType = dir + "/type";
42        LOGI("dirType====: %s", dirType.c_str());
43        std::string dirTemp = dir + "/temp";
44        LOGI("dirTemp====: %s", dirTemp.c_str());
45
46        if (!SPUtils::FileAccess(dirType)) {
47            continue;
48        }
49
50        std::string type;
51        std::string temp;
52        SPUtils::LoadFile(dirType, type);
53        SPUtils::LoadFile(dirTemp, temp);
54        for (auto node : collectNodes) {
55            if (type.find(node) != std::string::npos) {
56                LOGI("type====: %s", type.c_str());
57                float t = std::stof(temp);
58                LOGI("temp====: %s", temp.c_str());
59                if (node == "gpu") {
60                    result[type] = std::to_string(t);
61                } else {
62                    result[type] = std::to_string(t / 1e3);
63                }
64            }
65        }
66    }
67
68    LOGI("Temperature::ItemData map size(%u)", result.size());
69    return result;
70}
71}
72}
73