Peiyong Lin | 5696075 | 2022-09-30 21:52:52 +0000 | [diff] [blame] | 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 | |
Xiang Wang | d43e873 | 2023-02-07 12:10:33 -0800 | [diff] [blame] | 17 | #define LOG_TAG "thermal_service_example" |
| 18 | |
Peiyong Lin | 5696075 | 2022-09-30 21:52:52 +0000 | [diff] [blame] | 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 | |
Xiang Wang | d43e873 | 2023-02-07 12:10:33 -0800 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 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 | |
Peiyong Lin | 5696075 | 2022-09-30 21:52:52 +0000 | [diff] [blame] | 39 | ScopedAStatus Thermal::getCoolingDevices(std::vector<CoolingDevice>* /* out_devices */) { |
| 40 | LOG(VERBOSE) << __func__; |
| 41 | return ScopedAStatus::ok(); |
| 42 | } |
| 43 | |
| 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 | |
| 50 | ScopedAStatus Thermal::getTemperatures(std::vector<Temperature>* /* out_temperatures */) { |
| 51 | LOG(VERBOSE) << __func__; |
| 52 | return ScopedAStatus::ok(); |
| 53 | } |
| 54 | |
| 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 | |
| 61 | ScopedAStatus Thermal::getTemperatureThresholds( |
| 62 | std::vector<TemperatureThreshold>* /* out_temperatureThresholds */) { |
| 63 | LOG(VERBOSE) << __func__; |
| 64 | return ScopedAStatus::ok(); |
| 65 | } |
| 66 | |
| 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 | |
| 74 | ScopedAStatus Thermal::registerThermalChangedCallback( |
| 75 | const std::shared_ptr<IThermalChangedCallback>& in_callback) { |
| 76 | LOG(VERBOSE) << __func__ << " IThermalChangedCallback: " << in_callback; |
| 77 | if (in_callback == nullptr) { |
Xiang Wang | d43e873 | 2023-02-07 12:10:33 -0800 | [diff] [blame] | 78 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 79 | "Invalid nullptr callback"); |
Peiyong Lin | 5696075 | 2022-09-30 21:52:52 +0000 | [diff] [blame] | 80 | } |
Xiang Wang | d43e873 | 2023-02-07 12:10:33 -0800 | [diff] [blame] | 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); |
Peiyong Lin | 5696075 | 2022-09-30 21:52:52 +0000 | [diff] [blame] | 91 | } |
Peiyong Lin | 5696075 | 2022-09-30 21:52:52 +0000 | [diff] [blame] | 92 | return ScopedAStatus::ok(); |
| 93 | } |
| 94 | |
| 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) { |
Xiang Wang | d43e873 | 2023-02-07 12:10:33 -0800 | [diff] [blame] | 100 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 101 | "Invalid nullptr callback"); |
Peiyong Lin | 5696075 | 2022-09-30 21:52:52 +0000 | [diff] [blame] | 102 | } |
Xiang Wang | d43e873 | 2023-02-07 12:10:33 -0800 | [diff] [blame] | 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); |
Peiyong Lin | 5696075 | 2022-09-30 21:52:52 +0000 | [diff] [blame] | 113 | } |
Peiyong Lin | 5696075 | 2022-09-30 21:52:52 +0000 | [diff] [blame] | 114 | return ScopedAStatus::ok(); |
| 115 | } |
| 116 | |
| 117 | ScopedAStatus Thermal::unregisterThermalChangedCallback( |
| 118 | const std::shared_ptr<IThermalChangedCallback>& in_callback) { |
| 119 | LOG(VERBOSE) << __func__ << " IThermalChangedCallback: " << in_callback; |
Peiyong Lin | 5696075 | 2022-09-30 21:52:52 +0000 | [diff] [blame] | 120 | if (in_callback == nullptr) { |
Xiang Wang | d43e873 | 2023-02-07 12:10:33 -0800 | [diff] [blame] | 121 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 122 | "Invalid nullptr callback"); |
Peiyong Lin | 5696075 | 2022-09-30 21:52:52 +0000 | [diff] [blame] | 123 | } |
Xiang Wang | d43e873 | 2023-02-07 12:10:33 -0800 | [diff] [blame] | 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 | } |
Peiyong Lin | 5696075 | 2022-09-30 21:52:52 +0000 | [diff] [blame] | 141 | } |
Peiyong Lin | 5696075 | 2022-09-30 21:52:52 +0000 | [diff] [blame] | 142 | return ScopedAStatus::ok(); |
| 143 | } |
| 144 | |
| 145 | } // namespace aidl::android::hardware::thermal::impl::example |