1 /*
2 * Copyright (C) 2022 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 "thermal_service_example"
18
19 #include "Thermal.h"
20
21 #include <android-base/logging.h>
22
23 namespace aidl::android::hardware::thermal::impl::example {
24
25 using ndk::ScopedAStatus;
26
27 namespace {
28
interfacesEqual(const std::shared_ptr<::ndk::ICInterface> & left,const std::shared_ptr<::ndk::ICInterface> & right)29 bool interfacesEqual(const std::shared_ptr<::ndk::ICInterface>& left,
30 const std::shared_ptr<::ndk::ICInterface>& right) {
31 if (left == nullptr || right == nullptr || !left->isRemote() || !right->isRemote()) {
32 return left == right;
33 }
34 return left->asBinder() == right->asBinder();
35 }
36
37 } // namespace
38
getCoolingDevices(std::vector<CoolingDevice> *)39 ScopedAStatus Thermal::getCoolingDevices(std::vector<CoolingDevice>* /* out_devices */) {
40 LOG(VERBOSE) << __func__;
41 return ScopedAStatus::ok();
42 }
43
getCoolingDevicesWithType(CoolingType in_type,std::vector<CoolingDevice> *)44 ScopedAStatus Thermal::getCoolingDevicesWithType(CoolingType in_type,
45 std::vector<CoolingDevice>* /* out_devices */) {
46 LOG(VERBOSE) << __func__ << " CoolingType: " << static_cast<int32_t>(in_type);
47 return ScopedAStatus::ok();
48 }
49
getTemperatures(std::vector<Temperature> *)50 ScopedAStatus Thermal::getTemperatures(std::vector<Temperature>* /* out_temperatures */) {
51 LOG(VERBOSE) << __func__;
52 return ScopedAStatus::ok();
53 }
54
getTemperaturesWithType(TemperatureType in_type,std::vector<Temperature> *)55 ScopedAStatus Thermal::getTemperaturesWithType(TemperatureType in_type,
56 std::vector<Temperature>* /* out_temperatures */) {
57 LOG(VERBOSE) << __func__ << " TemperatureType: " << static_cast<int32_t>(in_type);
58 return ScopedAStatus::ok();
59 }
60
getTemperatureThresholds(std::vector<TemperatureThreshold> *)61 ScopedAStatus Thermal::getTemperatureThresholds(
62 std::vector<TemperatureThreshold>* /* out_temperatureThresholds */) {
63 LOG(VERBOSE) << __func__;
64 return ScopedAStatus::ok();
65 }
66
getTemperatureThresholdsWithType(TemperatureType in_type,std::vector<TemperatureThreshold> *)67 ScopedAStatus Thermal::getTemperatureThresholdsWithType(
68 TemperatureType in_type,
69 std::vector<TemperatureThreshold>* /* out_temperatureThresholds */) {
70 LOG(VERBOSE) << __func__ << " TemperatureType: " << static_cast<int32_t>(in_type);
71 return ScopedAStatus::ok();
72 }
73
registerThermalChangedCallback(const std::shared_ptr<IThermalChangedCallback> & in_callback)74 ScopedAStatus Thermal::registerThermalChangedCallback(
75 const std::shared_ptr<IThermalChangedCallback>& in_callback) {
76 LOG(VERBOSE) << __func__ << " IThermalChangedCallback: " << in_callback;
77 if (in_callback == nullptr) {
78 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
79 "Invalid nullptr callback");
80 }
81 {
82 std::lock_guard<std::mutex> _lock(thermal_callback_mutex_);
83 if (std::any_of(thermal_callbacks_.begin(), thermal_callbacks_.end(),
84 [&](const std::shared_ptr<IThermalChangedCallback>& c) {
85 return interfacesEqual(c, in_callback);
86 })) {
87 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
88 "Callback already registered");
89 }
90 thermal_callbacks_.push_back(in_callback);
91 }
92 return ScopedAStatus::ok();
93 }
94
registerThermalChangedCallbackWithType(const std::shared_ptr<IThermalChangedCallback> & in_callback,TemperatureType in_type)95 ScopedAStatus Thermal::registerThermalChangedCallbackWithType(
96 const std::shared_ptr<IThermalChangedCallback>& in_callback, TemperatureType in_type) {
97 LOG(VERBOSE) << __func__ << " IThermalChangedCallback: " << in_callback
98 << ", TemperatureType: " << static_cast<int32_t>(in_type);
99 if (in_callback == nullptr) {
100 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
101 "Invalid nullptr callback");
102 }
103 {
104 std::lock_guard<std::mutex> _lock(thermal_callback_mutex_);
105 if (std::any_of(thermal_callbacks_.begin(), thermal_callbacks_.end(),
106 [&](const std::shared_ptr<IThermalChangedCallback>& c) {
107 return interfacesEqual(c, in_callback);
108 })) {
109 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
110 "Callback already registered");
111 }
112 thermal_callbacks_.push_back(in_callback);
113 }
114 return ScopedAStatus::ok();
115 }
116
unregisterThermalChangedCallback(const std::shared_ptr<IThermalChangedCallback> & in_callback)117 ScopedAStatus Thermal::unregisterThermalChangedCallback(
118 const std::shared_ptr<IThermalChangedCallback>& in_callback) {
119 LOG(VERBOSE) << __func__ << " IThermalChangedCallback: " << in_callback;
120 if (in_callback == nullptr) {
121 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
122 "Invalid nullptr callback");
123 }
124 {
125 std::lock_guard<std::mutex> _lock(thermal_callback_mutex_);
126 bool removed = false;
127 thermal_callbacks_.erase(
128 std::remove_if(thermal_callbacks_.begin(), thermal_callbacks_.end(),
129 [&](const std::shared_ptr<IThermalChangedCallback>& c) {
130 if (interfacesEqual(c, in_callback)) {
131 removed = true;
132 return true;
133 }
134 return false;
135 }),
136 thermal_callbacks_.end());
137 if (!removed) {
138 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
139 "Callback wasn't registered");
140 }
141 }
142 return ScopedAStatus::ok();
143 }
144
registerCoolingDeviceChangedCallbackWithType(const std::shared_ptr<ICoolingDeviceChangedCallback> & in_callback,CoolingType in_type)145 ScopedAStatus Thermal::registerCoolingDeviceChangedCallbackWithType(
146 const std::shared_ptr<ICoolingDeviceChangedCallback>& in_callback, CoolingType in_type) {
147 LOG(VERBOSE) << __func__ << " ICoolingDeviceChangedCallback: " << in_callback
148 << ", CoolingType: " << static_cast<int32_t>(in_type);
149 if (in_callback == nullptr) {
150 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
151 "Invalid nullptr callback");
152 }
153 {
154 std::lock_guard<std::mutex> _lock(cdev_callback_mutex_);
155 if (std::any_of(cdev_callbacks_.begin(), cdev_callbacks_.end(),
156 [&](const std::shared_ptr<ICoolingDeviceChangedCallback>& c) {
157 return interfacesEqual(c, in_callback);
158 })) {
159 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
160 "Callback already registered");
161 }
162 cdev_callbacks_.push_back(in_callback);
163 }
164 return ScopedAStatus::ok();
165 }
166
unregisterCoolingDeviceChangedCallback(const std::shared_ptr<ICoolingDeviceChangedCallback> & in_callback)167 ScopedAStatus Thermal::unregisterCoolingDeviceChangedCallback(
168 const std::shared_ptr<ICoolingDeviceChangedCallback>& in_callback) {
169 LOG(VERBOSE) << __func__ << " ICoolingDeviceChangedCallback: " << in_callback;
170 if (in_callback == nullptr) {
171 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
172 "Invalid nullptr callback");
173 }
174 {
175 std::lock_guard<std::mutex> _lock(cdev_callback_mutex_);
176 bool removed = false;
177 cdev_callbacks_.erase(
178 std::remove_if(cdev_callbacks_.begin(), cdev_callbacks_.end(),
179 [&](const std::shared_ptr<ICoolingDeviceChangedCallback>& c) {
180 if (interfacesEqual(c, in_callback)) {
181 removed = true;
182 return true;
183 }
184 return false;
185 }),
186 cdev_callbacks_.end());
187 if (!removed) {
188 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
189 "Callback wasn't registered");
190 }
191 }
192 return ScopedAStatus::ok();
193 }
194 } // namespace aidl::android::hardware::thermal::impl::example
195