blob: f643d22f1f3e6f82ffbfa6c19974c14b33f874dc [file] [log] [blame]
Peiyong Lin56960752022-09-30 21:52:52 +00001/*
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
Xiang Wangd43e8732023-02-07 12:10:33 -080017#define LOG_TAG "thermal_service_example"
18
Peiyong Lin56960752022-09-30 21:52:52 +000019#include "Thermal.h"
20
21#include <android-base/logging.h>
22
23namespace aidl::android::hardware::thermal::impl::example {
24
25using ndk::ScopedAStatus;
26
Xiang Wangd43e8732023-02-07 12:10:33 -080027namespace {
28
29bool 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
Peiyong Lin56960752022-09-30 21:52:52 +000039ScopedAStatus Thermal::getCoolingDevices(std::vector<CoolingDevice>* /* out_devices */) {
40 LOG(VERBOSE) << __func__;
41 return ScopedAStatus::ok();
42}
43
44ScopedAStatus 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
50ScopedAStatus Thermal::getTemperatures(std::vector<Temperature>* /* out_temperatures */) {
51 LOG(VERBOSE) << __func__;
52 return ScopedAStatus::ok();
53}
54
55ScopedAStatus 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
61ScopedAStatus Thermal::getTemperatureThresholds(
62 std::vector<TemperatureThreshold>* /* out_temperatureThresholds */) {
63 LOG(VERBOSE) << __func__;
64 return ScopedAStatus::ok();
65}
66
67ScopedAStatus 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
74ScopedAStatus Thermal::registerThermalChangedCallback(
75 const std::shared_ptr<IThermalChangedCallback>& in_callback) {
76 LOG(VERBOSE) << __func__ << " IThermalChangedCallback: " << in_callback;
77 if (in_callback == nullptr) {
Xiang Wangd43e8732023-02-07 12:10:33 -080078 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
79 "Invalid nullptr callback");
Peiyong Lin56960752022-09-30 21:52:52 +000080 }
Xiang Wangd43e8732023-02-07 12:10:33 -080081 {
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);
Peiyong Lin56960752022-09-30 21:52:52 +000091 }
Peiyong Lin56960752022-09-30 21:52:52 +000092 return ScopedAStatus::ok();
93}
94
95ScopedAStatus 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) {
Xiang Wangd43e8732023-02-07 12:10:33 -0800100 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
101 "Invalid nullptr callback");
Peiyong Lin56960752022-09-30 21:52:52 +0000102 }
Xiang Wangd43e8732023-02-07 12:10:33 -0800103 {
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);
Peiyong Lin56960752022-09-30 21:52:52 +0000113 }
Peiyong Lin56960752022-09-30 21:52:52 +0000114 return ScopedAStatus::ok();
115}
116
117ScopedAStatus Thermal::unregisterThermalChangedCallback(
118 const std::shared_ptr<IThermalChangedCallback>& in_callback) {
119 LOG(VERBOSE) << __func__ << " IThermalChangedCallback: " << in_callback;
Peiyong Lin56960752022-09-30 21:52:52 +0000120 if (in_callback == nullptr) {
Xiang Wangd43e8732023-02-07 12:10:33 -0800121 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
122 "Invalid nullptr callback");
Peiyong Lin56960752022-09-30 21:52:52 +0000123 }
Xiang Wangd43e8732023-02-07 12:10:33 -0800124 {
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 }
Peiyong Lin56960752022-09-30 21:52:52 +0000141 }
Peiyong Lin56960752022-09-30 21:52:52 +0000142 return ScopedAStatus::ok();
143}
144
145} // namespace aidl::android::hardware::thermal::impl::example