blob: 442af61fa55c69a279d05fafc8c03c2e80961eb7 [file] [log] [blame]
Wei Wang84ce54e2018-10-18 13:56:03 -07001/*
2 * Copyright (C) 2018 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@2.0-service-mock"
18
19#include <cmath>
20#include <set>
21
22#include <android-base/logging.h>
23#include <hidl/HidlTransportSupport.h>
24
25#include "Thermal.h"
26
27namespace android {
28namespace hardware {
29namespace thermal {
30namespace V2_0 {
31namespace implementation {
32
33using ::android::sp;
34using ::android::hardware::interfacesEqual;
35using ::android::hardware::thermal::V1_0::ThermalStatus;
36using ::android::hardware::thermal::V1_0::ThermalStatusCode;
37
38std::set<sp<IThermalChangedCallback>> gCallbacks;
39
40static const Temperature_1_0 kTemp_1_0 = {
41 .type = static_cast<::android::hardware::thermal::V1_0::TemperatureType>(TemperatureType::CPU),
42 .name = "test temperature sensor",
43 .currentValue = 98.6,
44 .throttlingThreshold = 58,
45 .shutdownThreshold = 60.0,
46 .vrThrottlingThreshold = 59.0,
47};
48
49static const Temperature_2_0 kTemp_2_0 = {
50 .type = TemperatureType::SKIN,
51 .name = "test temperature sensor",
52 .value = 98.6,
53 .throttlingStatus = ThrottlingSeverity::CRITICAL,
54};
55
56static const TemperatureThreshold kTempThreshold = {
57 .type = TemperatureType::SKIN,
58 .name = "test temperature sensor",
59 .hotThrottlingThresholds = {{NAN, NAN, NAN, NAN, NAN, NAN, NAN}},
60 .coldThrottlingThresholds = {{NAN, NAN, NAN, NAN, NAN, NAN, NAN}},
61 .vrThrottlingThreshold = NAN,
62};
63
64static const CoolingDevice_1_0 kCooling_1_0 = {
65 .type = ::android::hardware::thermal::V1_0::CoolingType::FAN_RPM,
66 .name = "test cooling device",
67 .currentValue = 100.0,
68};
69
70static const CoolingDevice_2_0 kCooling_2_0 = {
71 .type = CoolingType::CPU,
72 .name = "test cooling device",
73 .value = 1,
74};
75
76static const CpuUsage kCpuUsage = {
77 .name = "cpu_name",
78 .active = 0,
79 .total = 0,
80 .isOnline = true,
81};
82
83// Methods from ::android::hardware::thermal::V1_0::IThermal follow.
84Return<void> Thermal::getTemperatures(getTemperatures_cb _hidl_cb) {
85 ThermalStatus status;
86 status.code = ThermalStatusCode::SUCCESS;
87 std::vector<Temperature_1_0> temperatures = {kTemp_1_0};
88 _hidl_cb(status, temperatures);
89 return Void();
90}
91
92Return<void> Thermal::getCpuUsages(getCpuUsages_cb _hidl_cb) {
93 ThermalStatus status;
94 status.code = ThermalStatusCode::SUCCESS;
95 std::vector<CpuUsage> cpu_usages = {kCpuUsage};
96 _hidl_cb(status, cpu_usages);
97 return Void();
98}
99
100Return<void> Thermal::getCoolingDevices(getCoolingDevices_cb _hidl_cb) {
101 ThermalStatus status;
102 status.code = ThermalStatusCode::SUCCESS;
103 std::vector<CoolingDevice_1_0> cooling_devices = {kCooling_1_0};
104 _hidl_cb(status, cooling_devices);
105 return Void();
106}
107
108// Methods from ::android::hardware::thermal::V2_0::IThermal follow.
109Return<void> Thermal::getCurrentTemperatures(bool filterType, TemperatureType type,
110 getCurrentTemperatures_cb _hidl_cb) {
111 ThermalStatus status;
112 status.code = ThermalStatusCode::SUCCESS;
113 std::vector<Temperature_2_0> temperatures;
114 if (filterType && type != kTemp_2_0.type) {
115 status.code = ThermalStatusCode::FAILURE;
116 status.debugMessage = "Failed to read data";
117 } else {
118 temperatures = {kTemp_2_0};
119 }
120 _hidl_cb(status, temperatures);
121 return Void();
122}
123
124Return<void> Thermal::getTemperatureThresholds(bool filterType, TemperatureType type,
125 getTemperatureThresholds_cb _hidl_cb) {
126 ThermalStatus status;
127 status.code = ThermalStatusCode::SUCCESS;
128 std::vector<TemperatureThreshold> temperature_thresholds;
129 if (filterType && type != kTempThreshold.type) {
130 status.code = ThermalStatusCode::FAILURE;
131 status.debugMessage = "Failed to read data";
132 } else {
133 temperature_thresholds = {kTempThreshold};
134 }
135 _hidl_cb(status, temperature_thresholds);
136 return Void();
137}
138
139Return<void> Thermal::getCurrentCoolingDevices(bool filterType, CoolingType type,
140 getCurrentCoolingDevices_cb _hidl_cb) {
141 ThermalStatus status;
142 status.code = ThermalStatusCode::SUCCESS;
143 std::vector<CoolingDevice_2_0> cooling_devices;
144 if (filterType && type != kCooling_2_0.type) {
145 status.code = ThermalStatusCode::FAILURE;
146 status.debugMessage = "Failed to read data";
147 } else {
148 cooling_devices = {kCooling_2_0};
149 }
150 _hidl_cb(status, cooling_devices);
151 return Void();
152}
153
154Return<void> Thermal::registerThermalChangedCallback(const sp<IThermalChangedCallback>& callback,
155 bool filterType, TemperatureType type,
156 registerThermalChangedCallback_cb _hidl_cb) {
157 ThermalStatus status;
158 status.code = ThermalStatusCode::SUCCESS;
159 std::lock_guard<std::mutex> _lock(thermal_callback_mutex_);
160 if (std::any_of(callbacks_.begin(), callbacks_.end(), [&](const CallbackSetting& c) {
161 return interfacesEqual(c.callback, callback);
162 })) {
163 status.code = ThermalStatusCode::FAILURE;
164 status.debugMessage = "Same callback interface registered already";
165 LOG(ERROR) << status.debugMessage;
166 } else {
167 callbacks_.emplace_back(callback, filterType, type);
168 LOG(INFO) << "A callback has been registered to ThermalHAL, isFilter: " << filterType
169 << " Type: " << android::hardware::thermal::V2_0::toString(type);
170 }
171 _hidl_cb(status);
172 return Void();
173}
174
175Return<void> Thermal::unregisterThermalChangedCallback(
176 const sp<IThermalChangedCallback>& callback, unregisterThermalChangedCallback_cb _hidl_cb) {
177 ThermalStatus status;
178 status.code = ThermalStatusCode::SUCCESS;
179 bool removed = false;
180 std::lock_guard<std::mutex> _lock(thermal_callback_mutex_);
181 callbacks_.erase(
182 std::remove_if(callbacks_.begin(), callbacks_.end(),
183 [&](const CallbackSetting& c) {
184 if (interfacesEqual(c.callback, callback)) {
185 LOG(INFO)
186 << "A callback has been unregistered from ThermalHAL, isFilter: "
187 << c.is_filter_type << " Type: "
188 << android::hardware::thermal::V2_0::toString(c.type);
189 removed = true;
190 return true;
191 }
192 return false;
193 }),
194 callbacks_.end());
195 if (!removed) {
196 status.code = ThermalStatusCode::FAILURE;
197 status.debugMessage = "The callback was not registered before";
198 LOG(ERROR) << status.debugMessage;
199 }
200 _hidl_cb(status);
201 return Void();
202}
203
204} // namespace implementation
205} // namespace V2_0
206} // namespace thermal
207} // namespace hardware
208} // namespace android