blob: 8a8ad0a4230257841adb3f4f7477318f7549c6e6 [file] [log] [blame]
Polina Bondarenko254e2fe2016-10-11 16:39:23 +02001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "android.hardware.thermal@1.0-impl"
Polina Bondarenko254e2fe2016-10-11 16:39:23 +020018
19#include <errno.h>
Mark Salyzyn3ff52602017-01-10 10:16:48 -080020
21#include <vector>
22
23#include <log/log.h>
24
Polina Bondarenko254e2fe2016-10-11 16:39:23 +020025#include <hardware/hardware.h>
26#include <hardware/thermal.h>
27
28#include "Thermal.h"
29
30namespace android {
31namespace hardware {
32namespace thermal {
33namespace V1_0 {
34namespace implementation {
35
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010036Thermal::Thermal(thermal_module_t* module) : mModule(module) {}
Polina Bondarenko254e2fe2016-10-11 16:39:23 +020037
38// Methods from ::android::hardware::thermal::V1_0::IThermal follow.
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010039Return<void> Thermal::getTemperatures(getTemperatures_cb _hidl_cb) {
40 ThermalStatus status;
41 status.code = ThermalStatusCode::SUCCESS;
42 hidl_vec<Temperature> temperatures;
Polina Bondarenko254e2fe2016-10-11 16:39:23 +020043
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010044 if (!mModule || !mModule->getTemperatures) {
45 ALOGI("getTemperatures is not implemented in Thermal HAL.");
Polina Bondarenko254e2fe2016-10-11 16:39:23 +020046 _hidl_cb(status, temperatures);
47 return Void();
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010048 }
49
50 ssize_t size = mModule->getTemperatures(mModule, nullptr, 0);
51 if (size >= 0) {
52 std::vector<temperature_t> list;
53 list.resize(size);
54 size = mModule->getTemperatures(mModule, list.data(), list.size());
55 if (size >= 0) {
56 temperatures.resize(list.size());
57 for (size_t i = 0; i < list.size(); ++i) {
58 switch (list[i].type) {
59 case DEVICE_TEMPERATURE_UNKNOWN:
60 temperatures[i].type = TemperatureType::UNKNOWN;
61 break;
62 case DEVICE_TEMPERATURE_CPU:
63 temperatures[i].type = TemperatureType::CPU;
64 break;
65 case DEVICE_TEMPERATURE_GPU:
66 temperatures[i].type = TemperatureType::GPU;
67 break;
68 case DEVICE_TEMPERATURE_BATTERY:
69 temperatures[i].type = TemperatureType::BATTERY;
70 break;
71 case DEVICE_TEMPERATURE_SKIN:
72 temperatures[i].type = TemperatureType::SKIN;
73 break;
74 default:
75 ALOGE("Unknown temperature %s type", list[i].name);
76 ;
77 }
78 temperatures[i].name = list[i].name;
79 temperatures[i].currentValue = list[i].current_value;
80 temperatures[i].throttlingThreshold = list[i].throttling_threshold;
81 temperatures[i].shutdownThreshold = list[i].shutdown_threshold;
82 temperatures[i].vrThrottlingThreshold = list[i].vr_throttling_threshold;
83 }
84 }
85 }
86 if (size < 0) {
87 status.code = ThermalStatusCode::FAILURE;
88 status.debugMessage = strerror(-size);
89 }
90 _hidl_cb(status, temperatures);
91 return Void();
Polina Bondarenko254e2fe2016-10-11 16:39:23 +020092}
93
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010094Return<void> Thermal::getCpuUsages(getCpuUsages_cb _hidl_cb) {
95 ThermalStatus status;
96 hidl_vec<CpuUsage> cpuUsages;
97 status.code = ThermalStatusCode::SUCCESS;
Polina Bondarenko254e2fe2016-10-11 16:39:23 +020098
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010099 if (!mModule || !mModule->getCpuUsages) {
100 ALOGI("getCpuUsages is not implemented in Thermal HAL");
Polina Bondarenko254e2fe2016-10-11 16:39:23 +0200101 _hidl_cb(status, cpuUsages);
102 return Void();
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100103 }
104
105 ssize_t size = mModule->getCpuUsages(mModule, nullptr);
106 if (size >= 0) {
107 std::vector<cpu_usage_t> list;
108 list.resize(size);
109 size = mModule->getCpuUsages(mModule, list.data());
110 if (size >= 0) {
111 list.resize(size);
112 cpuUsages.resize(size);
113 for (size_t i = 0; i < list.size(); ++i) {
114 cpuUsages[i].name = list[i].name;
115 cpuUsages[i].active = list[i].active;
116 cpuUsages[i].total = list[i].total;
117 cpuUsages[i].isOnline = list[i].is_online;
118 }
119 } else {
120 status.code = ThermalStatusCode::FAILURE;
121 status.debugMessage = strerror(-size);
122 }
123 }
124 if (size < 0) {
125 status.code = ThermalStatusCode::FAILURE;
126 status.debugMessage = strerror(-size);
127 }
128 _hidl_cb(status, cpuUsages);
129 return Void();
Polina Bondarenko254e2fe2016-10-11 16:39:23 +0200130}
131
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100132Return<void> Thermal::getCoolingDevices(getCoolingDevices_cb _hidl_cb) {
133 ThermalStatus status;
134 status.code = ThermalStatusCode::SUCCESS;
135 hidl_vec<CoolingDevice> coolingDevices;
Polina Bondarenko254e2fe2016-10-11 16:39:23 +0200136
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100137 if (!mModule || !mModule->getCoolingDevices) {
138 ALOGI("getCoolingDevices is not implemented in Thermal HAL.");
Polina Bondarenko254e2fe2016-10-11 16:39:23 +0200139 _hidl_cb(status, coolingDevices);
140 return Void();
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100141 }
142
143 ssize_t size = mModule->getCoolingDevices(mModule, nullptr, 0);
144 if (size >= 0) {
145 std::vector<cooling_device_t> list;
146 list.resize(size);
147 size = mModule->getCoolingDevices(mModule, list.data(), list.size());
148 if (size >= 0) {
149 list.resize(size);
150 coolingDevices.resize(list.size());
151 for (size_t i = 0; i < list.size(); ++i) {
152 switch (list[i].type) {
153 case FAN_RPM:
154 coolingDevices[i].type = CoolingType::FAN_RPM;
155 break;
156 default:
157 ALOGE("Unknown cooling device %s type", list[i].name);
158 }
159 coolingDevices[i].name = list[i].name;
160 coolingDevices[i].currentValue = list[i].current_value;
161 }
162 }
163 }
164 if (size < 0) {
165 status.code = ThermalStatusCode::FAILURE;
166 status.debugMessage = strerror(-size);
167 }
168 _hidl_cb(status, coolingDevices);
169 return Void();
Polina Bondarenko254e2fe2016-10-11 16:39:23 +0200170}
171
172IThermal* HIDL_FETCH_IThermal(const char* /* name */) {
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100173 thermal_module_t* module;
174 status_t err = hw_get_module(THERMAL_HARDWARE_MODULE_ID,
175 const_cast<hw_module_t const**>(
176 reinterpret_cast<hw_module_t**>(&module)));
177 if (err || !module) {
178 ALOGE("Couldn't load %s module (%s)", THERMAL_HARDWARE_MODULE_ID,
179 strerror(-err));
180 }
Polina Bondarenko254e2fe2016-10-11 16:39:23 +0200181
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100182 if (err == 0 && module->common.methods->open) {
183 struct hw_device_t* device;
184 err = module->common.methods->open(&module->common,
185 THERMAL_HARDWARE_MODULE_ID, &device);
186 if (err) {
187 ALOGE("Couldn't open %s module (%s)", THERMAL_HARDWARE_MODULE_ID,
188 strerror(-err));
189 } else {
190 return new Thermal(reinterpret_cast<thermal_module_t*>(device));
Polina Bondarenko254e2fe2016-10-11 16:39:23 +0200191 }
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100192 }
193 return new Thermal(module);
Polina Bondarenko254e2fe2016-10-11 16:39:23 +0200194}
195
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100196} // namespace implementation
Polina Bondarenko254e2fe2016-10-11 16:39:23 +0200197} // namespace V1_0
198} // namespace thermal
199} // namespace hardware
200} // namespace android