summaryrefslogtreecommitdiff
path: root/thermal/utils/cooling_devices.cpp
blob: 4f23bebe1bcaa25fea59d33d38a64f3056d58c3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <fstream>

#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>

#include "cooling_devices.h"

namespace android {
namespace hardware {
namespace thermal {
namespace V1_1 {
namespace implementation {

constexpr char kCoolingDeviceCurStateSuffix[] = "cur_state";

bool CoolingDevices::addCoolingDevice(
        const std::string& cooling_device_name, const std::string& path) {
    return cooling_device_name_to_path_map_.emplace(
        cooling_device_name, path).second;
}

bool CoolingDevices::getCoolingDeviceState(
        const std::string& cooling_device_name, int* data) const {
    auto cooling_device_itr = cooling_device_name_to_path_map_.find(
        cooling_device_name);
    if (cooling_device_itr == cooling_device_name_to_path_map_.end()) {
        return false;
    }

    std::string path = android::base::StringPrintf(
        "%s/%s", cooling_device_itr->second.c_str(),
        kCoolingDeviceCurStateSuffix);

    std::string cooling_device_data;
    android::base::ReadFileToString(path, &cooling_device_data);
    cooling_device_data = android::base::Trim(cooling_device_data);

    if (cooling_device_data.empty()) {
        LOG(ERROR) << "Could not read "
                   << getCoolingDevicePath(cooling_device_name);
        return false;
    }

    *data = std::stoi(cooling_device_data);
    return true;
}

std::string CoolingDevices::getCoolingDevicePath(
        const std::string& cooling_device_name) const {
    auto cooling_device_itr = cooling_device_name_to_path_map_.find(
        cooling_device_name);
    if (cooling_device_itr == cooling_device_name_to_path_map_.end()) {
        return "";
    }
    return cooling_device_itr->second;
}

}  // namespace implementation
}  // namespace V1_1
}  // namespace thermal
}  // namespace hardware
}  // namespace android